xref: /openbmc/linux/drivers/vdpa/pds/vdpa_dev.c (revision ed88863040daad18d3f9b12f7c9c1c3da3731e1f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright(c) 2023 Advanced Micro Devices, Inc */
3 
4 #include <linux/pci.h>
5 #include <linux/vdpa.h>
6 #include <uapi/linux/vdpa.h>
7 #include <linux/virtio_pci_modern.h>
8 
9 #include <linux/pds/pds_common.h>
10 #include <linux/pds/pds_core_if.h>
11 #include <linux/pds/pds_adminq.h>
12 #include <linux/pds/pds_auxbus.h>
13 
14 #include "vdpa_dev.h"
15 #include "aux_drv.h"
16 #include "cmds.h"
17 #include "debugfs.h"
18 
19 static u64 pds_vdpa_get_driver_features(struct vdpa_device *vdpa_dev);
20 
21 static struct pds_vdpa_device *vdpa_to_pdsv(struct vdpa_device *vdpa_dev)
22 {
23 	return container_of(vdpa_dev, struct pds_vdpa_device, vdpa_dev);
24 }
25 
26 static int pds_vdpa_notify_handler(struct notifier_block *nb,
27 				   unsigned long ecode,
28 				   void *data)
29 {
30 	struct pds_vdpa_device *pdsv = container_of(nb, struct pds_vdpa_device, nb);
31 	struct device *dev = &pdsv->vdpa_aux->padev->aux_dev.dev;
32 
33 	dev_dbg(dev, "%s: event code %lu\n", __func__, ecode);
34 
35 	if (ecode == PDS_EVENT_RESET || ecode == PDS_EVENT_LINK_CHANGE) {
36 		if (pdsv->config_cb.callback)
37 			pdsv->config_cb.callback(pdsv->config_cb.private);
38 	}
39 
40 	return 0;
41 }
42 
43 static int pds_vdpa_register_event_handler(struct pds_vdpa_device *pdsv)
44 {
45 	struct device *dev = &pdsv->vdpa_aux->padev->aux_dev.dev;
46 	struct notifier_block *nb = &pdsv->nb;
47 	int err;
48 
49 	if (!nb->notifier_call) {
50 		nb->notifier_call = pds_vdpa_notify_handler;
51 		err = pdsc_register_notify(nb);
52 		if (err) {
53 			nb->notifier_call = NULL;
54 			dev_err(dev, "failed to register pds event handler: %ps\n",
55 				ERR_PTR(err));
56 			return -EINVAL;
57 		}
58 		dev_dbg(dev, "pds event handler registered\n");
59 	}
60 
61 	return 0;
62 }
63 
64 static void pds_vdpa_unregister_event_handler(struct pds_vdpa_device *pdsv)
65 {
66 	if (pdsv->nb.notifier_call) {
67 		pdsc_unregister_notify(&pdsv->nb);
68 		pdsv->nb.notifier_call = NULL;
69 	}
70 }
71 
72 static int pds_vdpa_set_vq_address(struct vdpa_device *vdpa_dev, u16 qid,
73 				   u64 desc_addr, u64 driver_addr, u64 device_addr)
74 {
75 	struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
76 
77 	pdsv->vqs[qid].desc_addr = desc_addr;
78 	pdsv->vqs[qid].avail_addr = driver_addr;
79 	pdsv->vqs[qid].used_addr = device_addr;
80 
81 	return 0;
82 }
83 
84 static void pds_vdpa_set_vq_num(struct vdpa_device *vdpa_dev, u16 qid, u32 num)
85 {
86 	struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
87 
88 	pdsv->vqs[qid].q_len = num;
89 }
90 
91 static void pds_vdpa_kick_vq(struct vdpa_device *vdpa_dev, u16 qid)
92 {
93 	struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
94 
95 	iowrite16(qid, pdsv->vqs[qid].notify);
96 }
97 
98 static void pds_vdpa_set_vq_cb(struct vdpa_device *vdpa_dev, u16 qid,
99 			       struct vdpa_callback *cb)
100 {
101 	struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
102 
103 	pdsv->vqs[qid].event_cb = *cb;
104 }
105 
106 static irqreturn_t pds_vdpa_isr(int irq, void *data)
107 {
108 	struct pds_vdpa_vq_info *vq;
109 
110 	vq = data;
111 	if (vq->event_cb.callback)
112 		vq->event_cb.callback(vq->event_cb.private);
113 
114 	return IRQ_HANDLED;
115 }
116 
117 static void pds_vdpa_release_irq(struct pds_vdpa_device *pdsv, int qid)
118 {
119 	if (pdsv->vqs[qid].irq == VIRTIO_MSI_NO_VECTOR)
120 		return;
121 
122 	free_irq(pdsv->vqs[qid].irq, &pdsv->vqs[qid]);
123 	pdsv->vqs[qid].irq = VIRTIO_MSI_NO_VECTOR;
124 }
125 
126 static void pds_vdpa_set_vq_ready(struct vdpa_device *vdpa_dev, u16 qid, bool ready)
127 {
128 	struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
129 	struct pci_dev *pdev = pdsv->vdpa_aux->padev->vf_pdev;
130 	struct device *dev = &pdsv->vdpa_dev.dev;
131 	u64 driver_features;
132 	u16 invert_idx = 0;
133 	int irq;
134 	int err;
135 
136 	dev_dbg(dev, "%s: qid %d ready %d => %d\n",
137 		__func__, qid, pdsv->vqs[qid].ready, ready);
138 	if (ready == pdsv->vqs[qid].ready)
139 		return;
140 
141 	driver_features = pds_vdpa_get_driver_features(vdpa_dev);
142 	if (driver_features & BIT_ULL(VIRTIO_F_RING_PACKED))
143 		invert_idx = PDS_VDPA_PACKED_INVERT_IDX;
144 
145 	if (ready) {
146 		irq = pci_irq_vector(pdev, qid);
147 		snprintf(pdsv->vqs[qid].irq_name, sizeof(pdsv->vqs[qid].irq_name),
148 			 "vdpa-%s-%d", dev_name(dev), qid);
149 
150 		err = request_irq(irq, pds_vdpa_isr, 0,
151 				  pdsv->vqs[qid].irq_name, &pdsv->vqs[qid]);
152 		if (err) {
153 			dev_err(dev, "%s: no irq for qid %d: %pe\n",
154 				__func__, qid, ERR_PTR(err));
155 			return;
156 		}
157 		pdsv->vqs[qid].irq = irq;
158 
159 		/* Pass vq setup info to DSC using adminq to gather up and
160 		 * send all info at once so FW can do its full set up in
161 		 * one easy operation
162 		 */
163 		err = pds_vdpa_cmd_init_vq(pdsv, qid, invert_idx, &pdsv->vqs[qid]);
164 		if (err) {
165 			dev_err(dev, "Failed to init vq %d: %pe\n",
166 				qid, ERR_PTR(err));
167 			pds_vdpa_release_irq(pdsv, qid);
168 			ready = false;
169 		}
170 	} else {
171 		err = pds_vdpa_cmd_reset_vq(pdsv, qid, invert_idx, &pdsv->vqs[qid]);
172 		if (err)
173 			dev_err(dev, "%s: reset_vq failed qid %d: %pe\n",
174 				__func__, qid, ERR_PTR(err));
175 		pds_vdpa_release_irq(pdsv, qid);
176 	}
177 
178 	pdsv->vqs[qid].ready = ready;
179 }
180 
181 static bool pds_vdpa_get_vq_ready(struct vdpa_device *vdpa_dev, u16 qid)
182 {
183 	struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
184 
185 	return pdsv->vqs[qid].ready;
186 }
187 
188 static int pds_vdpa_set_vq_state(struct vdpa_device *vdpa_dev, u16 qid,
189 				 const struct vdpa_vq_state *state)
190 {
191 	struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
192 	struct pds_auxiliary_dev *padev = pdsv->vdpa_aux->padev;
193 	struct device *dev = &padev->aux_dev.dev;
194 	u64 driver_features;
195 	u16 avail;
196 	u16 used;
197 
198 	if (pdsv->vqs[qid].ready) {
199 		dev_err(dev, "Setting device position is denied while vq is enabled\n");
200 		return -EINVAL;
201 	}
202 
203 	driver_features = pds_vdpa_get_driver_features(vdpa_dev);
204 	if (driver_features & BIT_ULL(VIRTIO_F_RING_PACKED)) {
205 		avail = state->packed.last_avail_idx |
206 			(state->packed.last_avail_counter << 15);
207 		used = state->packed.last_used_idx |
208 		       (state->packed.last_used_counter << 15);
209 
210 		/* The avail and used index are stored with the packed wrap
211 		 * counter bit inverted.  This way, in case set_vq_state is
212 		 * not called, the initial value can be set to zero prior to
213 		 * feature negotiation, and it is good for both packed and
214 		 * split vq.
215 		 */
216 		avail ^= PDS_VDPA_PACKED_INVERT_IDX;
217 		used ^= PDS_VDPA_PACKED_INVERT_IDX;
218 	} else {
219 		avail = state->split.avail_index;
220 		/* state->split does not provide a used_index:
221 		 * the vq will be set to "empty" here, and the vq will read
222 		 * the current used index the next time the vq is kicked.
223 		 */
224 		used = avail;
225 	}
226 
227 	if (used != avail) {
228 		dev_dbg(dev, "Setting used equal to avail, for interoperability\n");
229 		used = avail;
230 	}
231 
232 	pdsv->vqs[qid].avail_idx = avail;
233 	pdsv->vqs[qid].used_idx = used;
234 
235 	return 0;
236 }
237 
238 static int pds_vdpa_get_vq_state(struct vdpa_device *vdpa_dev, u16 qid,
239 				 struct vdpa_vq_state *state)
240 {
241 	struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
242 	struct pds_auxiliary_dev *padev = pdsv->vdpa_aux->padev;
243 	struct device *dev = &padev->aux_dev.dev;
244 	u64 driver_features;
245 	u16 avail;
246 	u16 used;
247 
248 	if (pdsv->vqs[qid].ready) {
249 		dev_err(dev, "Getting device position is denied while vq is enabled\n");
250 		return -EINVAL;
251 	}
252 
253 	avail = pdsv->vqs[qid].avail_idx;
254 	used = pdsv->vqs[qid].used_idx;
255 
256 	driver_features = pds_vdpa_get_driver_features(vdpa_dev);
257 	if (driver_features & BIT_ULL(VIRTIO_F_RING_PACKED)) {
258 		avail ^= PDS_VDPA_PACKED_INVERT_IDX;
259 		used ^= PDS_VDPA_PACKED_INVERT_IDX;
260 
261 		state->packed.last_avail_idx = avail & 0x7fff;
262 		state->packed.last_avail_counter = avail >> 15;
263 		state->packed.last_used_idx = used & 0x7fff;
264 		state->packed.last_used_counter = used >> 15;
265 	} else {
266 		state->split.avail_index = avail;
267 		/* state->split does not provide a used_index. */
268 	}
269 
270 	return 0;
271 }
272 
273 static struct vdpa_notification_area
274 pds_vdpa_get_vq_notification(struct vdpa_device *vdpa_dev, u16 qid)
275 {
276 	struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
277 	struct virtio_pci_modern_device *vd_mdev;
278 	struct vdpa_notification_area area;
279 
280 	area.addr = pdsv->vqs[qid].notify_pa;
281 
282 	vd_mdev = &pdsv->vdpa_aux->vd_mdev;
283 	if (!vd_mdev->notify_offset_multiplier)
284 		area.size = PDS_PAGE_SIZE;
285 	else
286 		area.size = vd_mdev->notify_offset_multiplier;
287 
288 	return area;
289 }
290 
291 static int pds_vdpa_get_vq_irq(struct vdpa_device *vdpa_dev, u16 qid)
292 {
293 	struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
294 
295 	return pdsv->vqs[qid].irq;
296 }
297 
298 static u32 pds_vdpa_get_vq_align(struct vdpa_device *vdpa_dev)
299 {
300 	return PDS_PAGE_SIZE;
301 }
302 
303 static u32 pds_vdpa_get_vq_group(struct vdpa_device *vdpa_dev, u16 idx)
304 {
305 	return 0;
306 }
307 
308 static u64 pds_vdpa_get_device_features(struct vdpa_device *vdpa_dev)
309 {
310 	struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
311 
312 	return pdsv->supported_features;
313 }
314 
315 static int pds_vdpa_set_driver_features(struct vdpa_device *vdpa_dev, u64 features)
316 {
317 	struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
318 	struct device *dev = &pdsv->vdpa_dev.dev;
319 	u64 driver_features;
320 	u64 nego_features;
321 	u64 hw_features;
322 	u64 missing;
323 
324 	if (!(features & BIT_ULL(VIRTIO_F_ACCESS_PLATFORM)) && features) {
325 		dev_err(dev, "VIRTIO_F_ACCESS_PLATFORM is not negotiated\n");
326 		return -EOPNOTSUPP;
327 	}
328 
329 	/* Check for valid feature bits */
330 	nego_features = features & pdsv->supported_features;
331 	missing = features & ~nego_features;
332 	if (missing) {
333 		dev_err(dev, "Can't support all requested features in %#llx, missing %#llx features\n",
334 			features, missing);
335 		return -EOPNOTSUPP;
336 	}
337 
338 	pdsv->negotiated_features = nego_features;
339 
340 	driver_features = pds_vdpa_get_driver_features(vdpa_dev);
341 	dev_dbg(dev, "%s: %#llx => %#llx\n",
342 		__func__, driver_features, nego_features);
343 
344 	/* if we're faking the F_MAC, strip it before writing to device */
345 	hw_features = le64_to_cpu(pdsv->vdpa_aux->ident.hw_features);
346 	if (!(hw_features & BIT_ULL(VIRTIO_NET_F_MAC)))
347 		nego_features &= ~BIT_ULL(VIRTIO_NET_F_MAC);
348 
349 	if (driver_features == nego_features)
350 		return 0;
351 
352 	vp_modern_set_features(&pdsv->vdpa_aux->vd_mdev, nego_features);
353 
354 	return 0;
355 }
356 
357 static u64 pds_vdpa_get_driver_features(struct vdpa_device *vdpa_dev)
358 {
359 	struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
360 
361 	return pdsv->negotiated_features;
362 }
363 
364 static void pds_vdpa_set_config_cb(struct vdpa_device *vdpa_dev,
365 				   struct vdpa_callback *cb)
366 {
367 	struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
368 
369 	pdsv->config_cb.callback = cb->callback;
370 	pdsv->config_cb.private = cb->private;
371 }
372 
373 static u16 pds_vdpa_get_vq_num_max(struct vdpa_device *vdpa_dev)
374 {
375 	struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
376 
377 	/* qemu has assert() that vq_num_max <= VIRTQUEUE_MAX_SIZE (1024) */
378 	return min_t(u16, 1024, BIT(le16_to_cpu(pdsv->vdpa_aux->ident.max_qlen)));
379 }
380 
381 static u32 pds_vdpa_get_device_id(struct vdpa_device *vdpa_dev)
382 {
383 	return VIRTIO_ID_NET;
384 }
385 
386 static u32 pds_vdpa_get_vendor_id(struct vdpa_device *vdpa_dev)
387 {
388 	return PCI_VENDOR_ID_PENSANDO;
389 }
390 
391 static u8 pds_vdpa_get_status(struct vdpa_device *vdpa_dev)
392 {
393 	struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
394 
395 	return vp_modern_get_status(&pdsv->vdpa_aux->vd_mdev);
396 }
397 
398 static void pds_vdpa_set_status(struct vdpa_device *vdpa_dev, u8 status)
399 {
400 	struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
401 	struct device *dev = &pdsv->vdpa_dev.dev;
402 	u8 old_status;
403 	int i;
404 
405 	old_status = pds_vdpa_get_status(vdpa_dev);
406 	dev_dbg(dev, "%s: old %#x new %#x\n", __func__, old_status, status);
407 
408 	pds_vdpa_cmd_set_status(pdsv, status);
409 
410 	/* Note: still working with FW on the need for this reset cmd */
411 	if (status == 0) {
412 		pds_vdpa_cmd_reset(pdsv);
413 
414 		for (i = 0; i < pdsv->num_vqs; i++) {
415 			pdsv->vqs[i].avail_idx = 0;
416 			pdsv->vqs[i].used_idx = 0;
417 		}
418 
419 		pds_vdpa_cmd_set_mac(pdsv, pdsv->mac);
420 	}
421 
422 	if (status & ~old_status & VIRTIO_CONFIG_S_FEATURES_OK) {
423 		for (i = 0; i < pdsv->num_vqs; i++) {
424 			pdsv->vqs[i].notify =
425 				vp_modern_map_vq_notify(&pdsv->vdpa_aux->vd_mdev,
426 							i, &pdsv->vqs[i].notify_pa);
427 		}
428 	}
429 }
430 
431 static void pds_vdpa_init_vqs_entry(struct pds_vdpa_device *pdsv, int qid,
432 				    void __iomem *notify)
433 {
434 	memset(&pdsv->vqs[qid], 0, sizeof(pdsv->vqs[0]));
435 	pdsv->vqs[qid].qid = qid;
436 	pdsv->vqs[qid].pdsv = pdsv;
437 	pdsv->vqs[qid].ready = false;
438 	pdsv->vqs[qid].irq = VIRTIO_MSI_NO_VECTOR;
439 	pdsv->vqs[qid].notify = notify;
440 }
441 
442 static int pds_vdpa_reset(struct vdpa_device *vdpa_dev)
443 {
444 	struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
445 	struct device *dev;
446 	int err = 0;
447 	u8 status;
448 	int i;
449 
450 	dev = &pdsv->vdpa_aux->padev->aux_dev.dev;
451 	status = pds_vdpa_get_status(vdpa_dev);
452 
453 	if (status == 0)
454 		return 0;
455 
456 	if (status & VIRTIO_CONFIG_S_DRIVER_OK) {
457 		/* Reset the vqs */
458 		for (i = 0; i < pdsv->num_vqs && !err; i++) {
459 			err = pds_vdpa_cmd_reset_vq(pdsv, i, 0, &pdsv->vqs[i]);
460 			if (err)
461 				dev_err(dev, "%s: reset_vq failed qid %d: %pe\n",
462 					__func__, i, ERR_PTR(err));
463 			pds_vdpa_release_irq(pdsv, i);
464 			pds_vdpa_init_vqs_entry(pdsv, i, pdsv->vqs[i].notify);
465 		}
466 	}
467 
468 	pds_vdpa_set_status(vdpa_dev, 0);
469 
470 	return 0;
471 }
472 
473 static size_t pds_vdpa_get_config_size(struct vdpa_device *vdpa_dev)
474 {
475 	return sizeof(struct virtio_net_config);
476 }
477 
478 static void pds_vdpa_get_config(struct vdpa_device *vdpa_dev,
479 				unsigned int offset,
480 				void *buf, unsigned int len)
481 {
482 	struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
483 	void __iomem *device;
484 
485 	if (offset + len > sizeof(struct virtio_net_config)) {
486 		WARN(true, "%s: bad read, offset %d len %d\n", __func__, offset, len);
487 		return;
488 	}
489 
490 	device = pdsv->vdpa_aux->vd_mdev.device;
491 	memcpy_fromio(buf, device + offset, len);
492 }
493 
494 static void pds_vdpa_set_config(struct vdpa_device *vdpa_dev,
495 				unsigned int offset, const void *buf,
496 				unsigned int len)
497 {
498 	struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
499 	void __iomem *device;
500 
501 	if (offset + len > sizeof(struct virtio_net_config)) {
502 		WARN(true, "%s: bad read, offset %d len %d\n", __func__, offset, len);
503 		return;
504 	}
505 
506 	device = pdsv->vdpa_aux->vd_mdev.device;
507 	memcpy_toio(device + offset, buf, len);
508 }
509 
510 static const struct vdpa_config_ops pds_vdpa_ops = {
511 	.set_vq_address		= pds_vdpa_set_vq_address,
512 	.set_vq_num		= pds_vdpa_set_vq_num,
513 	.kick_vq		= pds_vdpa_kick_vq,
514 	.set_vq_cb		= pds_vdpa_set_vq_cb,
515 	.set_vq_ready		= pds_vdpa_set_vq_ready,
516 	.get_vq_ready		= pds_vdpa_get_vq_ready,
517 	.set_vq_state		= pds_vdpa_set_vq_state,
518 	.get_vq_state		= pds_vdpa_get_vq_state,
519 	.get_vq_notification	= pds_vdpa_get_vq_notification,
520 	.get_vq_irq		= pds_vdpa_get_vq_irq,
521 	.get_vq_align		= pds_vdpa_get_vq_align,
522 	.get_vq_group		= pds_vdpa_get_vq_group,
523 
524 	.get_device_features	= pds_vdpa_get_device_features,
525 	.set_driver_features	= pds_vdpa_set_driver_features,
526 	.get_driver_features	= pds_vdpa_get_driver_features,
527 	.set_config_cb		= pds_vdpa_set_config_cb,
528 	.get_vq_num_max		= pds_vdpa_get_vq_num_max,
529 	.get_device_id		= pds_vdpa_get_device_id,
530 	.get_vendor_id		= pds_vdpa_get_vendor_id,
531 	.get_status		= pds_vdpa_get_status,
532 	.set_status		= pds_vdpa_set_status,
533 	.reset			= pds_vdpa_reset,
534 	.get_config_size	= pds_vdpa_get_config_size,
535 	.get_config		= pds_vdpa_get_config,
536 	.set_config		= pds_vdpa_set_config,
537 };
538 static struct virtio_device_id pds_vdpa_id_table[] = {
539 	{VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID},
540 	{0},
541 };
542 
543 static int pds_vdpa_dev_add(struct vdpa_mgmt_dev *mdev, const char *name,
544 			    const struct vdpa_dev_set_config *add_config)
545 {
546 	struct pds_vdpa_aux *vdpa_aux;
547 	struct pds_vdpa_device *pdsv;
548 	struct vdpa_mgmt_dev *mgmt;
549 	u16 fw_max_vqs, vq_pairs;
550 	struct device *dma_dev;
551 	struct pci_dev *pdev;
552 	struct device *dev;
553 	int err;
554 	int i;
555 
556 	vdpa_aux = container_of(mdev, struct pds_vdpa_aux, vdpa_mdev);
557 	dev = &vdpa_aux->padev->aux_dev.dev;
558 	mgmt = &vdpa_aux->vdpa_mdev;
559 
560 	if (vdpa_aux->pdsv) {
561 		dev_warn(dev, "Multiple vDPA devices on a VF is not supported.\n");
562 		return -EOPNOTSUPP;
563 	}
564 
565 	pdsv = vdpa_alloc_device(struct pds_vdpa_device, vdpa_dev,
566 				 dev, &pds_vdpa_ops, 1, 1, name, false);
567 	if (IS_ERR(pdsv)) {
568 		dev_err(dev, "Failed to allocate vDPA structure: %pe\n", pdsv);
569 		return PTR_ERR(pdsv);
570 	}
571 
572 	vdpa_aux->pdsv = pdsv;
573 	pdsv->vdpa_aux = vdpa_aux;
574 
575 	pdev = vdpa_aux->padev->vf_pdev;
576 	dma_dev = &pdev->dev;
577 	pdsv->vdpa_dev.dma_dev = dma_dev;
578 
579 	pdsv->supported_features = mgmt->supported_features;
580 
581 	if (add_config->mask & BIT_ULL(VDPA_ATTR_DEV_FEATURES)) {
582 		u64 unsupp_features =
583 			add_config->device_features & ~pdsv->supported_features;
584 
585 		if (unsupp_features) {
586 			dev_err(dev, "Unsupported features: %#llx\n", unsupp_features);
587 			err = -EOPNOTSUPP;
588 			goto err_unmap;
589 		}
590 
591 		pdsv->supported_features = add_config->device_features;
592 	}
593 
594 	err = pds_vdpa_cmd_reset(pdsv);
595 	if (err) {
596 		dev_err(dev, "Failed to reset hw: %pe\n", ERR_PTR(err));
597 		goto err_unmap;
598 	}
599 
600 	err = pds_vdpa_init_hw(pdsv);
601 	if (err) {
602 		dev_err(dev, "Failed to init hw: %pe\n", ERR_PTR(err));
603 		goto err_unmap;
604 	}
605 
606 	fw_max_vqs = le16_to_cpu(pdsv->vdpa_aux->ident.max_vqs);
607 	vq_pairs = fw_max_vqs / 2;
608 
609 	/* Make sure we have the queues being requested */
610 	if (add_config->mask & (1 << VDPA_ATTR_DEV_NET_CFG_MAX_VQP))
611 		vq_pairs = add_config->net.max_vq_pairs;
612 
613 	pdsv->num_vqs = 2 * vq_pairs;
614 	if (pdsv->supported_features & BIT_ULL(VIRTIO_NET_F_CTRL_VQ))
615 		pdsv->num_vqs++;
616 
617 	if (pdsv->num_vqs > fw_max_vqs) {
618 		dev_err(dev, "%s: queue count requested %u greater than max %u\n",
619 			__func__, pdsv->num_vqs, fw_max_vqs);
620 		err = -ENOSPC;
621 		goto err_unmap;
622 	}
623 
624 	if (pdsv->num_vqs != fw_max_vqs) {
625 		err = pds_vdpa_cmd_set_max_vq_pairs(pdsv, vq_pairs);
626 		if (err) {
627 			dev_err(dev, "Failed to set max_vq_pairs: %pe\n",
628 				ERR_PTR(err));
629 			goto err_unmap;
630 		}
631 	}
632 
633 	/* Set a mac, either from the user config if provided
634 	 * or use the device's mac if not 00:..:00
635 	 * or set a random mac
636 	 */
637 	if (add_config->mask & BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MACADDR)) {
638 		ether_addr_copy(pdsv->mac, add_config->net.mac);
639 	} else {
640 		struct virtio_net_config __iomem *vc;
641 
642 		vc = pdsv->vdpa_aux->vd_mdev.device;
643 		memcpy_fromio(pdsv->mac, vc->mac, sizeof(pdsv->mac));
644 		if (is_zero_ether_addr(pdsv->mac) &&
645 		    (pdsv->supported_features & BIT_ULL(VIRTIO_NET_F_MAC))) {
646 			eth_random_addr(pdsv->mac);
647 			dev_info(dev, "setting random mac %pM\n", pdsv->mac);
648 		}
649 	}
650 	pds_vdpa_cmd_set_mac(pdsv, pdsv->mac);
651 
652 	for (i = 0; i < pdsv->num_vqs; i++) {
653 		void __iomem *notify;
654 
655 		notify = vp_modern_map_vq_notify(&pdsv->vdpa_aux->vd_mdev,
656 						 i, &pdsv->vqs[i].notify_pa);
657 		pds_vdpa_init_vqs_entry(pdsv, i, notify);
658 	}
659 
660 	pdsv->vdpa_dev.mdev = &vdpa_aux->vdpa_mdev;
661 
662 	err = pds_vdpa_register_event_handler(pdsv);
663 	if (err) {
664 		dev_err(dev, "Failed to register for PDS events: %pe\n", ERR_PTR(err));
665 		goto err_unmap;
666 	}
667 
668 	/* We use the _vdpa_register_device() call rather than the
669 	 * vdpa_register_device() to avoid a deadlock because our
670 	 * dev_add() is called with the vdpa_dev_lock already set
671 	 * by vdpa_nl_cmd_dev_add_set_doit()
672 	 */
673 	err = _vdpa_register_device(&pdsv->vdpa_dev, pdsv->num_vqs);
674 	if (err) {
675 		dev_err(dev, "Failed to register to vDPA bus: %pe\n", ERR_PTR(err));
676 		goto err_unevent;
677 	}
678 
679 	pds_vdpa_debugfs_add_vdpadev(vdpa_aux);
680 
681 	return 0;
682 
683 err_unevent:
684 	pds_vdpa_unregister_event_handler(pdsv);
685 err_unmap:
686 	put_device(&pdsv->vdpa_dev.dev);
687 	vdpa_aux->pdsv = NULL;
688 	return err;
689 }
690 
691 static void pds_vdpa_dev_del(struct vdpa_mgmt_dev *mdev,
692 			     struct vdpa_device *vdpa_dev)
693 {
694 	struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
695 	struct pds_vdpa_aux *vdpa_aux;
696 
697 	pds_vdpa_unregister_event_handler(pdsv);
698 
699 	vdpa_aux = container_of(mdev, struct pds_vdpa_aux, vdpa_mdev);
700 	_vdpa_unregister_device(vdpa_dev);
701 
702 	pds_vdpa_cmd_reset(vdpa_aux->pdsv);
703 	pds_vdpa_debugfs_reset_vdpadev(vdpa_aux);
704 
705 	vdpa_aux->pdsv = NULL;
706 
707 	dev_info(&vdpa_aux->padev->aux_dev.dev, "Removed vdpa device\n");
708 }
709 
710 static const struct vdpa_mgmtdev_ops pds_vdpa_mgmt_dev_ops = {
711 	.dev_add = pds_vdpa_dev_add,
712 	.dev_del = pds_vdpa_dev_del
713 };
714 
715 int pds_vdpa_get_mgmt_info(struct pds_vdpa_aux *vdpa_aux)
716 {
717 	union pds_core_adminq_cmd cmd = {
718 		.vdpa_ident.opcode = PDS_VDPA_CMD_IDENT,
719 		.vdpa_ident.vf_id = cpu_to_le16(vdpa_aux->vf_id),
720 	};
721 	union pds_core_adminq_comp comp = {};
722 	struct vdpa_mgmt_dev *mgmt;
723 	struct pci_dev *pf_pdev;
724 	struct device *pf_dev;
725 	struct pci_dev *pdev;
726 	dma_addr_t ident_pa;
727 	struct device *dev;
728 	u16 dev_intrs;
729 	u16 max_vqs;
730 	int err;
731 
732 	dev = &vdpa_aux->padev->aux_dev.dev;
733 	pdev = vdpa_aux->padev->vf_pdev;
734 	mgmt = &vdpa_aux->vdpa_mdev;
735 
736 	/* Get resource info through the PF's adminq.  It is a block of info,
737 	 * so we need to map some memory for PF to make available to the
738 	 * firmware for writing the data.
739 	 */
740 	pf_pdev = pci_physfn(vdpa_aux->padev->vf_pdev);
741 	pf_dev = &pf_pdev->dev;
742 	ident_pa = dma_map_single(pf_dev, &vdpa_aux->ident,
743 				  sizeof(vdpa_aux->ident), DMA_FROM_DEVICE);
744 	if (dma_mapping_error(pf_dev, ident_pa)) {
745 		dev_err(dev, "Failed to map ident space\n");
746 		return -ENOMEM;
747 	}
748 
749 	cmd.vdpa_ident.ident_pa = cpu_to_le64(ident_pa);
750 	cmd.vdpa_ident.len = cpu_to_le32(sizeof(vdpa_aux->ident));
751 	err = pds_client_adminq_cmd(vdpa_aux->padev, &cmd,
752 				    sizeof(cmd.vdpa_ident), &comp, 0);
753 	dma_unmap_single(pf_dev, ident_pa,
754 			 sizeof(vdpa_aux->ident), DMA_FROM_DEVICE);
755 	if (err) {
756 		dev_err(dev, "Failed to ident hw, status %d: %pe\n",
757 			comp.status, ERR_PTR(err));
758 		return err;
759 	}
760 
761 	max_vqs = le16_to_cpu(vdpa_aux->ident.max_vqs);
762 	dev_intrs = pci_msix_vec_count(pdev);
763 	dev_dbg(dev, "ident.max_vqs %d dev_intrs %d\n", max_vqs, dev_intrs);
764 
765 	max_vqs = min_t(u16, dev_intrs, max_vqs);
766 	mgmt->max_supported_vqs = min_t(u16, PDS_VDPA_MAX_QUEUES, max_vqs);
767 	vdpa_aux->nintrs = mgmt->max_supported_vqs;
768 
769 	mgmt->ops = &pds_vdpa_mgmt_dev_ops;
770 	mgmt->id_table = pds_vdpa_id_table;
771 	mgmt->device = dev;
772 	mgmt->supported_features = le64_to_cpu(vdpa_aux->ident.hw_features);
773 
774 	/* advertise F_MAC even if the device doesn't */
775 	mgmt->supported_features |= BIT_ULL(VIRTIO_NET_F_MAC);
776 
777 	mgmt->config_attr_mask = BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MACADDR);
778 	mgmt->config_attr_mask |= BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MAX_VQP);
779 	mgmt->config_attr_mask |= BIT_ULL(VDPA_ATTR_DEV_FEATURES);
780 
781 	err = pci_alloc_irq_vectors(pdev, vdpa_aux->nintrs, vdpa_aux->nintrs,
782 				    PCI_IRQ_MSIX);
783 	if (err < 0) {
784 		dev_err(dev, "Couldn't get %d msix vectors: %pe\n",
785 			vdpa_aux->nintrs, ERR_PTR(err));
786 		return err;
787 	}
788 	vdpa_aux->nintrs = err;
789 
790 	return 0;
791 }
792