1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2023 Advanced Micro Devices, Inc */
7 #include <linux/pds/pds_auxbus.h>
10 * pds_client_register - Link the client to the firmware
11 * @pf: a pointer to the pdsc structure
12 * @devname: name that includes service into, e.g. pds_core.vDPA
14 * Return: positive client ID (ci) on success, or
17 int pds_client_register(struct pci_dev *pf_pdev, char *devname)
19 union pds_core_adminq_comp comp = {};
20 union pds_core_adminq_cmd cmd = {};
25 pf = pci_get_drvdata(pf_pdev);
29 cmd.client_reg.opcode = PDS_AQ_CMD_CLIENT_REG;
30 strscpy(cmd.client_reg.devname, devname,
31 sizeof(cmd.client_reg.devname));
33 err = pdsc_adminq_post(pf, &cmd, &comp, false);
35 dev_info(pf->dev, "register dev_name %s with DSC failed, status %d: %pe\n",
36 devname, comp.status, ERR_PTR(err));
40 ci = le16_to_cpu(comp.client_reg.client_id);
42 dev_err(pf->dev, "%s: device returned null client_id\n",
47 dev_dbg(pf->dev, "%s: device returned client_id %d for %s\n",
48 __func__, ci, devname);
52 EXPORT_SYMBOL_GPL(pds_client_register);
55 * pds_client_unregister - Unlink the client from the firmware
56 * @pf: a pointer to the pdsc structure
57 * @client_id: id returned from pds_client_register()
59 * Return: 0 on success, or
62 int pds_client_unregister(struct pci_dev *pf_pdev, u16 client_id)
64 union pds_core_adminq_comp comp = {};
65 union pds_core_adminq_cmd cmd = {};
69 pf = pci_get_drvdata(pf_pdev);
73 cmd.client_unreg.opcode = PDS_AQ_CMD_CLIENT_UNREG;
74 cmd.client_unreg.client_id = cpu_to_le16(client_id);
76 err = pdsc_adminq_post(pf, &cmd, &comp, false);
78 dev_info(pf->dev, "unregister client_id %d failed, status %d: %pe\n",
79 client_id, comp.status, ERR_PTR(err));
83 EXPORT_SYMBOL_GPL(pds_client_unregister);
86 * pds_client_adminq_cmd - Process an adminq request for the client
87 * @padev: ptr to the client device
88 * @req: ptr to buffer with request
89 * @req_len: length of actual struct used for request
90 * @resp: ptr to buffer where answer is to be copied
91 * @flags: optional flags from pds_core_adminq_flags
93 * Return: 0 on success, or
96 * Client sends pointers to request and response buffers
97 * Core copies request data into pds_core_client_request_cmd
98 * Core sets other fields as needed
99 * Core posts to AdminQ
100 * Core copies completion data into response buffer
102 int pds_client_adminq_cmd(struct pds_auxiliary_dev *padev,
103 union pds_core_adminq_cmd *req,
105 union pds_core_adminq_comp *resp,
108 union pds_core_adminq_cmd cmd = {};
109 struct pci_dev *pf_pdev;
114 pf_pdev = pci_physfn(padev->vf_pdev);
115 pf = pci_get_drvdata(pf_pdev);
117 dev_dbg(pf->dev, "%s: %s opcode %d\n",
118 __func__, dev_name(&padev->aux_dev.dev), req->opcode);
123 /* Wrap the client's request */
124 cmd.client_request.opcode = PDS_AQ_CMD_CLIENT_CMD;
125 cmd.client_request.client_id = cpu_to_le16(padev->client_id);
126 cp_len = min_t(size_t, req_len, sizeof(cmd.client_request.client_cmd));
127 memcpy(cmd.client_request.client_cmd, req, cp_len);
129 err = pdsc_adminq_post(pf, &cmd, resp,
130 !!(flags & PDS_AQ_FLAG_FASTPOLL));
131 if (err && err != -EAGAIN)
132 dev_info(pf->dev, "client admin cmd failed: %pe\n",
137 EXPORT_SYMBOL_GPL(pds_client_adminq_cmd);
139 static void pdsc_auxbus_dev_release(struct device *dev)
141 struct pds_auxiliary_dev *padev =
142 container_of(dev, struct pds_auxiliary_dev, aux_dev.dev);
147 static struct pds_auxiliary_dev *pdsc_auxbus_dev_register(struct pdsc *cf,
152 struct auxiliary_device *aux_dev;
153 struct pds_auxiliary_dev *padev;
156 padev = kzalloc(sizeof(*padev), GFP_KERNEL);
158 return ERR_PTR(-ENOMEM);
160 padev->vf_pdev = cf->pdev;
161 padev->client_id = client_id;
163 aux_dev = &padev->aux_dev;
164 aux_dev->name = name;
165 aux_dev->id = cf->uid;
166 aux_dev->dev.parent = cf->dev;
167 aux_dev->dev.release = pdsc_auxbus_dev_release;
169 err = auxiliary_device_init(aux_dev);
171 dev_warn(cf->dev, "auxiliary_device_init of %s failed: %pe\n",
176 err = auxiliary_device_add(aux_dev);
178 dev_warn(cf->dev, "auxiliary_device_add of %s failed: %pe\n",
186 auxiliary_device_uninit(aux_dev);
192 int pdsc_auxbus_dev_del(struct pdsc *cf, struct pdsc *pf)
194 struct pds_auxiliary_dev *padev;
197 mutex_lock(&pf->config_lock);
199 padev = pf->vfs[cf->vf_id].padev;
201 pds_client_unregister(pf->pdev, padev->client_id);
202 auxiliary_device_delete(&padev->aux_dev);
203 auxiliary_device_uninit(&padev->aux_dev);
204 padev->client_id = 0;
206 pf->vfs[cf->vf_id].padev = NULL;
208 mutex_unlock(&pf->config_lock);
212 int pdsc_auxbus_dev_add(struct pdsc *cf, struct pdsc *pf)
214 struct pds_auxiliary_dev *padev;
215 enum pds_core_vif_types vt;
216 char devname[PDS_DEVNAME_LEN];
221 mutex_lock(&pf->config_lock);
223 /* We only support vDPA so far, so it is the only one to
224 * be verified that it is available in the Core device and
225 * enabled in the devlink param. In the future this might
226 * become a loop for several VIF types.
229 /* Verify that the type is supported and enabled. It is not
230 * an error if there is no auxbus device support for this
231 * VF, it just means something else needs to happen with it.
233 vt = PDS_DEV_TYPE_VDPA;
234 vt_support = !!le16_to_cpu(pf->dev_ident.vif_types[vt]);
236 pf->viftype_status[vt].supported &&
237 pf->viftype_status[vt].enabled))
240 /* Need to register with FW and get the client_id before
241 * creating the aux device so that the aux client can run
242 * adminq commands as part its probe
244 snprintf(devname, sizeof(devname), "%s.%s.%d",
245 PDS_CORE_DRV_NAME, pf->viftype_status[vt].name, cf->uid);
246 client_id = pds_client_register(pf->pdev, devname);
252 padev = pdsc_auxbus_dev_register(cf, pf, client_id,
253 pf->viftype_status[vt].name);
255 pds_client_unregister(pf->pdev, client_id);
256 err = PTR_ERR(padev);
259 pf->vfs[cf->vf_id].padev = padev;
262 mutex_unlock(&pf->config_lock);