1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2023 Advanced Micro Devices, Inc */
3
4 #include <linux/pci.h>
5
6 #include "core.h"
7 #include <linux/pds/pds_auxbus.h>
8
9 /**
10 * pds_client_register - Link the client to the firmware
11 * @pf: ptr to the PF driver's private data struct
12 * @devname: name that includes service into, e.g. pds_core.vDPA
13 *
14 * Return: positive client ID (ci) on success, or
15 * negative for error
16 */
pds_client_register(struct pdsc * pf,char * devname)17 int pds_client_register(struct pdsc *pf, char *devname)
18 {
19 union pds_core_adminq_comp comp = {};
20 union pds_core_adminq_cmd cmd = {};
21 int err;
22 u16 ci;
23
24 cmd.client_reg.opcode = PDS_AQ_CMD_CLIENT_REG;
25 strscpy(cmd.client_reg.devname, devname,
26 sizeof(cmd.client_reg.devname));
27
28 err = pdsc_adminq_post(pf, &cmd, &comp, false);
29 if (err) {
30 dev_info(pf->dev, "register dev_name %s with DSC failed, status %d: %pe\n",
31 devname, comp.status, ERR_PTR(err));
32 return err;
33 }
34
35 ci = le16_to_cpu(comp.client_reg.client_id);
36 if (!ci) {
37 dev_err(pf->dev, "%s: device returned null client_id\n",
38 __func__);
39 return -EIO;
40 }
41
42 dev_dbg(pf->dev, "%s: device returned client_id %d for %s\n",
43 __func__, ci, devname);
44
45 return ci;
46 }
47 EXPORT_SYMBOL_GPL(pds_client_register);
48
49 /**
50 * pds_client_unregister - Unlink the client from the firmware
51 * @pf: ptr to the PF driver's private data struct
52 * @client_id: id returned from pds_client_register()
53 *
54 * Return: 0 on success, or
55 * negative for error
56 */
pds_client_unregister(struct pdsc * pf,u16 client_id)57 int pds_client_unregister(struct pdsc *pf, u16 client_id)
58 {
59 union pds_core_adminq_comp comp = {};
60 union pds_core_adminq_cmd cmd = {};
61 int err;
62
63 cmd.client_unreg.opcode = PDS_AQ_CMD_CLIENT_UNREG;
64 cmd.client_unreg.client_id = cpu_to_le16(client_id);
65
66 err = pdsc_adminq_post(pf, &cmd, &comp, false);
67 if (err)
68 dev_info(pf->dev, "unregister client_id %d failed, status %d: %pe\n",
69 client_id, comp.status, ERR_PTR(err));
70
71 return err;
72 }
73 EXPORT_SYMBOL_GPL(pds_client_unregister);
74
75 /**
76 * pds_client_adminq_cmd - Process an adminq request for the client
77 * @padev: ptr to the client device
78 * @req: ptr to buffer with request
79 * @req_len: length of actual struct used for request
80 * @resp: ptr to buffer where answer is to be copied
81 * @flags: optional flags from pds_core_adminq_flags
82 *
83 * Return: 0 on success, or
84 * negative for error
85 *
86 * Client sends pointers to request and response buffers
87 * Core copies request data into pds_core_client_request_cmd
88 * Core sets other fields as needed
89 * Core posts to AdminQ
90 * Core copies completion data into response buffer
91 */
pds_client_adminq_cmd(struct pds_auxiliary_dev * padev,union pds_core_adminq_cmd * req,size_t req_len,union pds_core_adminq_comp * resp,u64 flags)92 int pds_client_adminq_cmd(struct pds_auxiliary_dev *padev,
93 union pds_core_adminq_cmd *req,
94 size_t req_len,
95 union pds_core_adminq_comp *resp,
96 u64 flags)
97 {
98 union pds_core_adminq_cmd cmd = {};
99 struct pci_dev *pf_pdev;
100 struct pdsc *pf;
101 size_t cp_len;
102 int err;
103
104 pf_pdev = pci_physfn(padev->vf_pdev);
105 pf = pci_get_drvdata(pf_pdev);
106
107 dev_dbg(pf->dev, "%s: %s opcode %d\n",
108 __func__, dev_name(&padev->aux_dev.dev), req->opcode);
109
110 /* Wrap the client's request */
111 cmd.client_request.opcode = PDS_AQ_CMD_CLIENT_CMD;
112 cmd.client_request.client_id = cpu_to_le16(padev->client_id);
113 cp_len = min_t(size_t, req_len, sizeof(cmd.client_request.client_cmd));
114 memcpy(cmd.client_request.client_cmd, req, cp_len);
115
116 err = pdsc_adminq_post(pf, &cmd, resp,
117 !!(flags & PDS_AQ_FLAG_FASTPOLL));
118 if (err && err != -EAGAIN)
119 dev_info(pf->dev, "client admin cmd failed: %pe\n",
120 ERR_PTR(err));
121
122 return err;
123 }
124 EXPORT_SYMBOL_GPL(pds_client_adminq_cmd);
125
pdsc_auxbus_dev_release(struct device * dev)126 static void pdsc_auxbus_dev_release(struct device *dev)
127 {
128 struct pds_auxiliary_dev *padev =
129 container_of(dev, struct pds_auxiliary_dev, aux_dev.dev);
130
131 kfree(padev);
132 }
133
pdsc_auxbus_dev_register(struct pdsc * cf,struct pdsc * pf,u16 client_id,char * name)134 static struct pds_auxiliary_dev *pdsc_auxbus_dev_register(struct pdsc *cf,
135 struct pdsc *pf,
136 u16 client_id,
137 char *name)
138 {
139 struct auxiliary_device *aux_dev;
140 struct pds_auxiliary_dev *padev;
141 int err;
142
143 padev = kzalloc_obj(*padev);
144 if (!padev)
145 return ERR_PTR(-ENOMEM);
146
147 padev->vf_pdev = cf->pdev;
148 padev->client_id = client_id;
149
150 aux_dev = &padev->aux_dev;
151 aux_dev->name = name;
152 aux_dev->id = cf->uid;
153 aux_dev->dev.parent = cf->dev;
154 aux_dev->dev.release = pdsc_auxbus_dev_release;
155
156 err = auxiliary_device_init(aux_dev);
157 if (err < 0) {
158 dev_warn(cf->dev, "auxiliary_device_init of %s failed: %pe\n",
159 name, ERR_PTR(err));
160 kfree(padev);
161 return ERR_PTR(err);
162 }
163
164 err = auxiliary_device_add(aux_dev);
165 if (err) {
166 dev_warn(cf->dev, "auxiliary_device_add of %s failed: %pe\n",
167 name, ERR_PTR(err));
168 auxiliary_device_uninit(aux_dev);
169 return ERR_PTR(err);
170 }
171
172 return padev;
173 }
174
pdsc_auxbus_dev_del(struct pdsc * cf,struct pdsc * pf,struct pds_auxiliary_dev ** pd_ptr)175 void pdsc_auxbus_dev_del(struct pdsc *cf, struct pdsc *pf,
176 struct pds_auxiliary_dev **pd_ptr)
177 {
178 struct pds_auxiliary_dev *padev;
179
180 mutex_lock(&pf->config_lock);
181
182 /* A concurrent del may have already torn this device down and
183 * cleared it.
184 */
185 padev = *pd_ptr;
186 if (!padev)
187 goto out_unlock;
188
189 pds_client_unregister(pf, padev->client_id);
190 auxiliary_device_delete(&padev->aux_dev);
191 auxiliary_device_uninit(&padev->aux_dev);
192 *pd_ptr = NULL;
193
194 out_unlock:
195 mutex_unlock(&pf->config_lock);
196 }
197
pdsc_auxbus_dev_add(struct pdsc * cf,struct pdsc * pf,enum pds_core_vif_types vt,struct pds_auxiliary_dev ** pd_ptr)198 int pdsc_auxbus_dev_add(struct pdsc *cf, struct pdsc *pf,
199 enum pds_core_vif_types vt,
200 struct pds_auxiliary_dev **pd_ptr)
201 {
202 struct pds_auxiliary_dev *padev;
203 char devname[PDS_DEVNAME_LEN];
204 unsigned long mask;
205 u16 vt_support;
206 int client_id;
207 int err = 0;
208
209 if (!cf)
210 return -ENODEV;
211
212 if (vt >= PDS_DEV_TYPE_MAX)
213 return -EINVAL;
214
215 mutex_lock(&pf->config_lock);
216
217 /* Nothing to do if the aux device is already present. This also
218 * guards against a second add overwriting *pd_ptr and leaking the
219 * first, symmetric with the check in pdsc_auxbus_dev_del().
220 */
221 if (*pd_ptr)
222 goto out_unlock;
223
224 mask = BIT_ULL(PDSC_S_FW_DEAD) |
225 BIT_ULL(PDSC_S_STOPPING_DRIVER);
226 if (cf->state & mask) {
227 dev_err(pf->dev, "%s: can't add dev, VF client in bad state %#lx\n",
228 __func__, cf->state);
229 err = -ENXIO;
230 goto out_unlock;
231 }
232
233 /* Verify that the type is supported and enabled. It is not
234 * an error if the firmware doesn't support the feature, the
235 * driver just won't set up an auxiliary_device for it.
236 */
237 vt_support = !!le16_to_cpu(pf->dev_ident.vif_types[vt]);
238 if (!(vt_support &&
239 pf->viftype_status[vt].supported &&
240 pf->viftype_status[vt].enabled))
241 goto out_unlock;
242
243 /* Need to register with FW and get the client_id before
244 * creating the aux device so that the aux client can run
245 * adminq commands as part its probe
246 */
247 snprintf(devname, sizeof(devname), "%s.%s.%d",
248 PDS_CORE_DRV_NAME, pf->viftype_status[vt].name, cf->uid);
249 client_id = pds_client_register(pf, devname);
250 if (client_id < 0) {
251 err = client_id;
252 goto out_unlock;
253 }
254
255 padev = pdsc_auxbus_dev_register(cf, pf, client_id,
256 pf->viftype_status[vt].name);
257 if (IS_ERR(padev)) {
258 pds_client_unregister(pf, client_id);
259 err = PTR_ERR(padev);
260 goto out_unlock;
261 }
262 *pd_ptr = padev;
263
264 out_unlock:
265 mutex_unlock(&pf->config_lock);
266 return err;
267 }
268