|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
| 2 | +/* Copyright(c) 2024-2025 Intel Corporation. All rights reserved. */ |
| 3 | +#include <linux/device.h> |
| 4 | +#include <cxl/mailbox.h> |
| 5 | +#include <cxl/features.h> |
| 6 | +#include "cxl.h" |
| 7 | +#include "cxlmem.h" |
| 8 | + |
| 9 | +inline struct cxl_features_state *to_cxlfs(struct cxl_dev_state *cxlds) |
| 10 | +{ |
| 11 | + return cxlds->cxlfs; |
| 12 | +} |
| 13 | +EXPORT_SYMBOL_NS_GPL(to_cxlfs, "CXL"); |
| 14 | + |
| 15 | +static int cxl_get_supported_features_count(struct cxl_mailbox *cxl_mbox) |
| 16 | +{ |
| 17 | + struct cxl_mbox_get_sup_feats_out mbox_out; |
| 18 | + struct cxl_mbox_get_sup_feats_in mbox_in; |
| 19 | + struct cxl_mbox_cmd mbox_cmd; |
| 20 | + int rc; |
| 21 | + |
| 22 | + memset(&mbox_in, 0, sizeof(mbox_in)); |
| 23 | + mbox_in.count = cpu_to_le32(sizeof(mbox_out)); |
| 24 | + memset(&mbox_out, 0, sizeof(mbox_out)); |
| 25 | + mbox_cmd = (struct cxl_mbox_cmd) { |
| 26 | + .opcode = CXL_MBOX_OP_GET_SUPPORTED_FEATURES, |
| 27 | + .size_in = sizeof(mbox_in), |
| 28 | + .payload_in = &mbox_in, |
| 29 | + .size_out = sizeof(mbox_out), |
| 30 | + .payload_out = &mbox_out, |
| 31 | + .min_out = sizeof(mbox_out), |
| 32 | + }; |
| 33 | + rc = cxl_internal_send_cmd(cxl_mbox, &mbox_cmd); |
| 34 | + if (rc < 0) |
| 35 | + return rc; |
| 36 | + |
| 37 | + return le16_to_cpu(mbox_out.supported_feats); |
| 38 | +} |
| 39 | + |
| 40 | +static struct cxl_feat_entries * |
| 41 | +get_supported_features(struct cxl_features_state *cxlfs) |
| 42 | +{ |
| 43 | + int remain_feats, max_size, max_feats, start, rc, hdr_size; |
| 44 | + struct cxl_mailbox *cxl_mbox = &cxlfs->cxlds->cxl_mbox; |
| 45 | + int feat_size = sizeof(struct cxl_feat_entry); |
| 46 | + struct cxl_mbox_get_sup_feats_in mbox_in; |
| 47 | + struct cxl_feat_entry *entry; |
| 48 | + struct cxl_mbox_cmd mbox_cmd; |
| 49 | + int count; |
| 50 | + |
| 51 | + count = cxl_get_supported_features_count(cxl_mbox); |
| 52 | + if (count <= 0) |
| 53 | + return NULL; |
| 54 | + |
| 55 | + struct cxl_feat_entries *entries __free(kvfree) = |
| 56 | + kvmalloc(struct_size(entries, ent, count), GFP_KERNEL); |
| 57 | + if (!entries) |
| 58 | + return NULL; |
| 59 | + |
| 60 | + struct cxl_mbox_get_sup_feats_out *mbox_out __free(kvfree) = |
| 61 | + kvmalloc(cxl_mbox->payload_size, GFP_KERNEL); |
| 62 | + if (!mbox_out) |
| 63 | + return NULL; |
| 64 | + |
| 65 | + hdr_size = struct_size(mbox_out, ents, 0); |
| 66 | + max_size = cxl_mbox->payload_size - hdr_size; |
| 67 | + /* max feat entries that can fit in mailbox max payload size */ |
| 68 | + max_feats = max_size / feat_size; |
| 69 | + entry = entries->ent; |
| 70 | + |
| 71 | + start = 0; |
| 72 | + remain_feats = count; |
| 73 | + do { |
| 74 | + int retrieved, alloc_size, copy_feats; |
| 75 | + int num_entries; |
| 76 | + |
| 77 | + if (remain_feats > max_feats) { |
| 78 | + alloc_size = struct_size(mbox_out, ents, max_feats); |
| 79 | + remain_feats = remain_feats - max_feats; |
| 80 | + copy_feats = max_feats; |
| 81 | + } else { |
| 82 | + alloc_size = struct_size(mbox_out, ents, remain_feats); |
| 83 | + copy_feats = remain_feats; |
| 84 | + remain_feats = 0; |
| 85 | + } |
| 86 | + |
| 87 | + memset(&mbox_in, 0, sizeof(mbox_in)); |
| 88 | + mbox_in.count = cpu_to_le32(alloc_size); |
| 89 | + mbox_in.start_idx = cpu_to_le16(start); |
| 90 | + memset(mbox_out, 0, alloc_size); |
| 91 | + mbox_cmd = (struct cxl_mbox_cmd) { |
| 92 | + .opcode = CXL_MBOX_OP_GET_SUPPORTED_FEATURES, |
| 93 | + .size_in = sizeof(mbox_in), |
| 94 | + .payload_in = &mbox_in, |
| 95 | + .size_out = alloc_size, |
| 96 | + .payload_out = mbox_out, |
| 97 | + .min_out = hdr_size, |
| 98 | + }; |
| 99 | + rc = cxl_internal_send_cmd(cxl_mbox, &mbox_cmd); |
| 100 | + if (rc < 0) |
| 101 | + return NULL; |
| 102 | + |
| 103 | + if (mbox_cmd.size_out <= hdr_size) |
| 104 | + return NULL; |
| 105 | + |
| 106 | + /* |
| 107 | + * Make sure retrieved out buffer is multiple of feature |
| 108 | + * entries. |
| 109 | + */ |
| 110 | + retrieved = mbox_cmd.size_out - hdr_size; |
| 111 | + if (retrieved % feat_size) |
| 112 | + return NULL; |
| 113 | + |
| 114 | + num_entries = le16_to_cpu(mbox_out->num_entries); |
| 115 | + /* |
| 116 | + * If the reported output entries * defined entry size != |
| 117 | + * retrieved output bytes, then the output package is incorrect. |
| 118 | + */ |
| 119 | + if (num_entries * feat_size != retrieved) |
| 120 | + return NULL; |
| 121 | + |
| 122 | + memcpy(entry, mbox_out->ents, retrieved); |
| 123 | + entry += num_entries; |
| 124 | + /* |
| 125 | + * If the number of output entries is less than expected, add the |
| 126 | + * remaining entries to the next batch. |
| 127 | + */ |
| 128 | + remain_feats += copy_feats - num_entries; |
| 129 | + start += num_entries; |
| 130 | + } while (remain_feats); |
| 131 | + |
| 132 | + entries->num_features = count; |
| 133 | + |
| 134 | + return no_free_ptr(entries); |
| 135 | +} |
| 136 | + |
| 137 | +static void free_cxlfs(void *_cxlfs) |
| 138 | +{ |
| 139 | + struct cxl_features_state *cxlfs = _cxlfs; |
| 140 | + struct cxl_dev_state *cxlds = cxlfs->cxlds; |
| 141 | + |
| 142 | + cxlds->cxlfs = NULL; |
| 143 | + kvfree(cxlfs->entries); |
| 144 | + kfree(cxlfs); |
| 145 | +} |
| 146 | + |
| 147 | +/** |
| 148 | + * devm_cxl_setup_features() - Allocate and initialize features context |
| 149 | + * @cxlds: CXL device context |
| 150 | + * |
| 151 | + * Return 0 on success or -errno on failure. |
| 152 | + */ |
| 153 | +int devm_cxl_setup_features(struct cxl_dev_state *cxlds) |
| 154 | +{ |
| 155 | + struct cxl_mailbox *cxl_mbox = &cxlds->cxl_mbox; |
| 156 | + |
| 157 | + if (cxl_mbox->feat_cap < CXL_FEATURES_RO) |
| 158 | + return -ENODEV; |
| 159 | + |
| 160 | + struct cxl_features_state *cxlfs __free(kfree) = |
| 161 | + kzalloc(sizeof(*cxlfs), GFP_KERNEL); |
| 162 | + if (!cxlfs) |
| 163 | + return -ENOMEM; |
| 164 | + |
| 165 | + cxlfs->cxlds = cxlds; |
| 166 | + |
| 167 | + cxlfs->entries = get_supported_features(cxlfs); |
| 168 | + if (!cxlfs->entries) |
| 169 | + return -ENOMEM; |
| 170 | + |
| 171 | + cxlds->cxlfs = cxlfs; |
| 172 | + |
| 173 | + return devm_add_action_or_reset(cxlds->dev, free_cxlfs, no_free_ptr(cxlfs)); |
| 174 | +} |
| 175 | +EXPORT_SYMBOL_NS_GPL(devm_cxl_setup_features, "CXL"); |
0 commit comments