xref: /linux/drivers/net/ethernet/microsoft/mana/gdma_pci.c (revision 3d6792ea46056816196bf72243903b216a2fb06f)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /* Copyright (c) 2025, Microsoft Corporation. */
3 
4 #include <linux/bitmap.h>
5 #include <linux/cpumask.h>
6 #include <linux/debugfs.h>
7 #include <linux/delay.h>
8 #include <linux/list.h>
9 #include <linux/module.h>
10 #include <linux/msi.h>
11 #include <linux/pci.h>
12 #include <linux/slab.h>
13 #include <linux/topology.h>
14 #include <linux/xarray.h>
15 
16 #include <net/mana/mana.h>
17 #include <net/mana/hw_channel.h>
18 
19 struct mana_dev_recovery {
20 	struct list_head list;
21 	struct pci_dev *pdev;
22 	enum gdma_eqe_type type;
23 };
24 
25 static struct mana_dev_recovery_work {
26 	struct list_head dev_list;
27 	struct delayed_work work;
28 
29 	/* Lock for dev_list above */
30 	spinlock_t lock;
31 } mana_dev_recovery_work;
32 
33 static int mana_gd_suspend(struct pci_dev *pdev, pm_message_t state);
34 static int mana_gd_resume(struct pci_dev *pdev);
35 
36 #define MANA_SERVICE_PERIOD 10
37 
38 static void mana_serv_rescan(struct pci_dev *pdev)
39 {
40 	struct pci_bus *parent;
41 
42 	pci_lock_rescan_remove();
43 
44 	parent = pdev->bus;
45 	if (!parent) {
46 		dev_err(&pdev->dev, "MANA service: no parent bus\n");
47 		goto out;
48 	}
49 
50 	pci_stop_and_remove_bus_device(pdev);
51 	pci_rescan_bus(parent);
52 
53 out:
54 	pci_unlock_rescan_remove();
55 }
56 
57 static void mana_serv_fpga(struct pci_dev *pdev)
58 {
59 	struct pci_bus *bus, *parent;
60 
61 	pci_lock_rescan_remove();
62 
63 	bus = pdev->bus;
64 	if (!bus) {
65 		dev_err(&pdev->dev, "MANA service: no bus\n");
66 		goto out;
67 	}
68 
69 	parent = bus->parent;
70 	if (!parent) {
71 		dev_err(&pdev->dev, "MANA service: no parent bus\n");
72 		goto out;
73 	}
74 
75 	pci_stop_and_remove_bus_device(bus->self);
76 
77 	msleep(MANA_SERVICE_PERIOD * 1000);
78 
79 	pci_rescan_bus(parent);
80 
81 out:
82 	pci_unlock_rescan_remove();
83 }
84 
85 static void mana_serv_reset(struct pci_dev *pdev)
86 {
87 	struct gdma_context *gc = pci_get_drvdata(pdev);
88 	struct hw_channel_context *hwc;
89 	int ret;
90 
91 	if (!gc) {
92 		/* Perform PCI rescan on device if GC is not set up */
93 		dev_err(&pdev->dev, "MANA service: GC not setup, rescanning\n");
94 		mana_serv_rescan(pdev);
95 		return;
96 	}
97 
98 	hwc = gc->hwc.driver_data;
99 	if (!hwc) {
100 		dev_err(&pdev->dev, "MANA service: no HWC\n");
101 		goto out;
102 	}
103 
104 	/* HWC is not responding in this case, so don't wait */
105 	hwc->hwc_timeout = 0;
106 
107 	dev_info(&pdev->dev, "MANA reset cycle start\n");
108 
109 	mana_gd_suspend(pdev, PMSG_SUSPEND);
110 
111 	msleep(MANA_SERVICE_PERIOD * 1000);
112 
113 	ret = mana_gd_resume(pdev);
114 	if (ret == -ETIMEDOUT || ret == -EPROTO) {
115 		/* Perform PCI rescan on device if we failed on HWC */
116 		dev_err(&pdev->dev, "MANA service: resume failed, rescanning\n");
117 		mana_serv_rescan(pdev);
118 		return;
119 	}
120 
121 	if (ret)
122 		dev_info(&pdev->dev, "MANA reset cycle failed err %d\n", ret);
123 	else
124 		dev_info(&pdev->dev, "MANA reset cycle completed\n");
125 
126 out:
127 	clear_bit(GC_IN_SERVICE, &gc->flags);
128 }
129 
130 static void mana_do_service(enum gdma_eqe_type type, struct pci_dev *pdev)
131 {
132 	switch (type) {
133 	case GDMA_EQE_HWC_FPGA_RECONFIG:
134 		mana_serv_fpga(pdev);
135 		break;
136 
137 	case GDMA_EQE_HWC_RESET_REQUEST:
138 		mana_serv_reset(pdev);
139 		break;
140 
141 	default:
142 		dev_err(&pdev->dev, "MANA service: unknown type %d\n", type);
143 		break;
144 	}
145 }
146 
147 static void mana_recovery_delayed_func(struct work_struct *w)
148 {
149 	struct mana_dev_recovery_work *work;
150 	struct mana_dev_recovery *dev;
151 	unsigned long flags;
152 
153 	work = container_of(w, struct mana_dev_recovery_work, work.work);
154 
155 	spin_lock_irqsave(&work->lock, flags);
156 
157 	while (!list_empty(&work->dev_list)) {
158 		dev = list_first_entry(&work->dev_list,
159 				       struct mana_dev_recovery, list);
160 		list_del(&dev->list);
161 		spin_unlock_irqrestore(&work->lock, flags);
162 
163 		mana_do_service(dev->type, dev->pdev);
164 		pci_dev_put(dev->pdev);
165 		kfree(dev);
166 
167 		spin_lock_irqsave(&work->lock, flags);
168 	}
169 
170 	spin_unlock_irqrestore(&work->lock, flags);
171 }
172 
173 static void mana_serv_func(struct work_struct *w)
174 {
175 	struct mana_serv_work *mns_wk;
176 	struct pci_dev *pdev;
177 
178 	mns_wk = container_of(w, struct mana_serv_work, serv_work);
179 	pdev = mns_wk->pdev;
180 
181 	if (pdev)
182 		mana_do_service(mns_wk->type, pdev);
183 
184 	pci_dev_put(pdev);
185 	kfree(mns_wk);
186 	module_put(THIS_MODULE);
187 }
188 
189 static int mana_pci_schedule_serv_work(struct gdma_context *gc,
190 				       enum gdma_eqe_type type)
191 {
192 	struct mana_serv_work *mns_wk;
193 
194 	if (test_and_set_bit(GC_IN_SERVICE, &gc->flags)) {
195 		dev_info(gc->dev, "Already in service\n");
196 		return -EBUSY;
197 	}
198 
199 	if (!try_module_get(THIS_MODULE)) {
200 		dev_info(gc->dev, "Module is unloading\n");
201 		clear_bit(GC_IN_SERVICE, &gc->flags);
202 		return -ENODEV;
203 	}
204 
205 	mns_wk = kzalloc(sizeof(*mns_wk), GFP_ATOMIC);
206 	if (!mns_wk) {
207 		module_put(THIS_MODULE);
208 		clear_bit(GC_IN_SERVICE, &gc->flags);
209 		return -ENOMEM;
210 	}
211 
212 	dev_info(gc->dev, "Start MANA service type:%d\n", type);
213 	mns_wk->pdev = to_pci_dev(gc->dev);
214 	mns_wk->type = type;
215 	pci_dev_get(mns_wk->pdev);
216 	INIT_WORK(&mns_wk->serv_work, mana_serv_func);
217 	schedule_work(&mns_wk->serv_work);
218 	return 0;
219 }
220 
221 static bool mana_pci_msix_can_alloc_dyn(struct gdma_context *gc)
222 {
223 	return pci_msix_can_alloc_dyn(to_pci_dev(gc->dev));
224 }
225 
226 static int mana_pci_msix_virq(struct gdma_context *gc, int msi)
227 {
228 	return pci_irq_vector(to_pci_dev(gc->dev), msi);
229 }
230 
231 static int mana_pci_msix_alloc_at(struct gdma_context *gc, int *msi)
232 {
233 	struct msi_map irq_map;
234 
235 	irq_map = pci_msix_alloc_irq_at(to_pci_dev(gc->dev), *msi, NULL);
236 	if (!irq_map.virq)
237 		return irq_map.index;
238 
239 	*msi = irq_map.index;
240 	return irq_map.virq;
241 }
242 
243 static void mana_pci_msix_free(struct gdma_context *gc, int msi, int irq)
244 {
245 	struct msi_map irq_map = { .virq = irq, .index = msi };
246 
247 	pci_msix_free_irq(to_pci_dev(gc->dev), irq_map);
248 }
249 
250 static int mana_pci_msix_vec_count(struct gdma_context *gc)
251 {
252 	return pci_msix_vec_count(to_pci_dev(gc->dev));
253 }
254 
255 static int mana_pci_dev_reset(struct gdma_context *gc)
256 {
257 	return pcie_flr(to_pci_dev(gc->dev));
258 }
259 
260 /*
261  * Spread on CPUs with the following heuristics:
262  *
263  * 1. No more than one IRQ per CPU, if possible;
264  * 2. NUMA locality is the second priority;
265  * 3. Sibling dislocality is the last priority.
266  *
267  * Let's consider this topology:
268  *
269  * Node            0               1
270  * Core        0       1       2       3
271  * CPU       0   1   2   3   4   5   6   7
272  *
273  * The most performant IRQ distribution based on the above topology
274  * and heuristics may look like this:
275  *
276  * IRQ     Nodes   Cores   CPUs
277  * 0       1       0       0-1
278  * 1       1       1       2-3
279  * 2       1       0       0-1
280  * 3       1       1       2-3
281  * 4       2       2       4-5
282  * 5       2       3       6-7
283  * 6       2       2       4-5
284  * 7       2       3       6-7
285  *
286  * The heuristics is implemented as follows.
287  *
288  * The outer for_each() loop resets the 'weight' to the actual number
289  * of CPUs in the hop. Then inner for_each() loop decrements it by the
290  * number of sibling groups (cores) while assigning first set of IRQs
291  * to each group. IRQs 0 and 1 above are distributed this way.
292  *
293  * Now, because NUMA locality is more important, we should walk the
294  * same set of siblings and assign 2nd set of IRQs (2 and 3), and it's
295  * implemented by the medium while() loop. We do like this unless the
296  * number of IRQs assigned on this hop will not become equal to number
297  * of CPUs in the hop (weight == 0). Then we switch to the next hop and
298  * do the same thing.
299  */
300 
301 static int mana_irq_setup_numa_aware(unsigned int *irqs, unsigned int len,
302 				     int node, bool skip_first_cpu)
303 {
304 	const struct cpumask *next, *prev = cpu_none_mask;
305 	cpumask_var_t cpus __free(free_cpumask_var);
306 	int cpu, weight;
307 
308 	if (!alloc_cpumask_var(&cpus, GFP_KERNEL))
309 		return -ENOMEM;
310 
311 	rcu_read_lock();
312 	for_each_numa_hop_mask(next, node) {
313 		weight = cpumask_weight_andnot(next, prev);
314 		while (weight > 0) {
315 			cpumask_andnot(cpus, next, prev);
316 			for_each_cpu(cpu, cpus) {
317 				cpumask_andnot(cpus, cpus, topology_sibling_cpumask(cpu));
318 				--weight;
319 
320 				if (unlikely(skip_first_cpu)) {
321 					skip_first_cpu = false;
322 					continue;
323 				}
324 
325 				if (len-- == 0)
326 					goto done;
327 
328 				irq_set_affinity_and_hint(*irqs++, topology_sibling_cpumask(cpu));
329 			}
330 		}
331 		prev = next;
332 	}
333 done:
334 	rcu_read_unlock();
335 	return 0;
336 }
337 
338 /* must be called with cpus_read_lock() held */
339 static void mana_irq_setup_linear(unsigned int *irqs, unsigned int len)
340 {
341 	int cpu;
342 
343 	for_each_online_cpu(cpu) {
344 		if (len == 0)
345 			break;
346 
347 		irq_set_affinity_and_hint(*irqs++, cpumask_of(cpu));
348 		len--;
349 	}
350 }
351 
352 static int mana_gd_setup_dyn_irqs(struct pci_dev *pdev, int nvec)
353 {
354 	struct gdma_context *gc = pci_get_drvdata(pdev);
355 	struct gdma_irq_context *gic;
356 	int *irqs, err, i, msi;
357 
358 	irqs = kmalloc_objs(int, nvec);
359 	if (!irqs)
360 		return -ENOMEM;
361 
362 	/*
363 	 * In this function, num_msix_usable = HWC IRQ + Queue IRQ.
364 	 * nvec is only Queue IRQ (HWC already setup).
365 	 * While processing the next pci irq vector, we start with index 1,
366 	 * as IRQ vector at index 0 is already processed for HWC.
367 	 * However, the population of irqs array starts with index 0, to be
368 	 * further used in mana_irq_setup_numa_aware()
369 	 */
370 	for (i = 1; i <= nvec; i++) {
371 		msi = i;
372 		gic = mana_gd_get_gic(gc, false, &msi);
373 		if (IS_ERR(gic)) {
374 			err = PTR_ERR(gic);
375 			goto free_irq;
376 		}
377 
378 		irqs[i - 1] = gic->irq;
379 	}
380 
381 	/*
382 	 * When calling mana_irq_setup_numa_aware() for dynamically added IRQs,
383 	 * if number of CPUs is more than or equal to allocated MSI-X, we need to
384 	 * skip the first CPU sibling group since they are already affinitized to
385 	 * HWC IRQ
386 	 */
387 	cpus_read_lock();
388 	if (gc->num_msix_usable <= num_online_cpus()) {
389 		err = mana_irq_setup_numa_aware(irqs, nvec, gc->numa_node,
390 						true);
391 		if (err) {
392 			cpus_read_unlock();
393 			goto free_irq;
394 		}
395 	} else {
396 		/*
397 		 * When num_msix_usable are more than num_online_cpus, our
398 		 * queue IRQs should be equal to num of online vCPUs.
399 		 * We try to make sure queue IRQs spread across all vCPUs.
400 		 * In such a case NUMA or CPU core affinity does not matter.
401 		 * Note: in this case the total mana IRQ should always be
402 		 * num_online_cpus + 1. The first HWC IRQ is already handled
403 		 * in HWC setup calls
404 		 * However, if CPUs went offline since num_msix_usable was
405 		 * computed, queue IRQs will be more than num_online_cpus().
406 		 * In such cases remaining extra IRQs will retain their default
407 		 * affinity.
408 		 */
409 		int first_unassigned = num_online_cpus();
410 
411 		if (nvec > first_unassigned) {
412 			char buf[32];
413 
414 			if (first_unassigned == nvec - 1)
415 				snprintf(buf, sizeof(buf), "%d",
416 					 first_unassigned);
417 			else
418 				snprintf(buf, sizeof(buf), "%d-%d",
419 					 first_unassigned, nvec - 1);
420 
421 			dev_dbg(&pdev->dev,
422 				"MANA IRQ indices #%s will retain the default CPU affinity\n",
423 				buf);
424 		}
425 
426 		mana_irq_setup_linear(irqs, nvec);
427 	}
428 
429 	cpus_read_unlock();
430 	kfree(irqs);
431 	return 0;
432 
433 free_irq:
434 	for (i -= 1; i > 0; i--)
435 		mana_gd_put_gic(gc, false, i);
436 	kfree(irqs);
437 	return err;
438 }
439 
440 static int mana_gd_setup_irqs(struct pci_dev *pdev, int nvec)
441 {
442 	struct gdma_context *gc = pci_get_drvdata(pdev);
443 	struct gdma_irq_context *gic;
444 	int *irqs, *start_irqs;
445 	unsigned int cpu;
446 	int err, i, msi;
447 
448 	irqs = kmalloc_objs(int, nvec);
449 	if (!irqs)
450 		return -ENOMEM;
451 
452 	start_irqs = irqs;
453 
454 	for (i = 0; i < nvec; i++) {
455 		msi = i;
456 		gic = mana_gd_get_gic(gc, false, &msi);
457 		if (IS_ERR(gic)) {
458 			err = PTR_ERR(gic);
459 			goto free_irq;
460 		}
461 
462 		irqs[i] = gic->irq;
463 	}
464 
465 	/* If number of IRQ is one extra than number of online CPUs,
466 	 * then we need to assign IRQ0 (hwc irq) and IRQ1 to
467 	 * same CPU.
468 	 * Else we will use different CPUs for IRQ0 and IRQ1.
469 	 * Also we are using cpumask_local_spread instead of
470 	 * cpumask_first for the node, because the node can be
471 	 * mem only.
472 	 */
473 	cpus_read_lock();
474 	if (nvec > num_online_cpus()) {
475 		cpu = cpumask_local_spread(0, gc->numa_node);
476 		irq_set_affinity_and_hint(irqs[0], cpumask_of(cpu));
477 		irqs++;
478 		nvec -= 1;
479 	}
480 
481 	err = mana_irq_setup_numa_aware(irqs, nvec, gc->numa_node, false);
482 	if (err) {
483 		cpus_read_unlock();
484 		goto free_irq;
485 	}
486 
487 	cpus_read_unlock();
488 	kfree(start_irqs);
489 	return 0;
490 
491 free_irq:
492 	for (i -= 1; i >= 0; i--)
493 		mana_gd_put_gic(gc, false, i);
494 
495 	kfree(start_irqs);
496 	return err;
497 }
498 
499 static int mana_gd_setup_hwc_irqs(struct pci_dev *pdev)
500 {
501 	struct gdma_context *gc = pci_get_drvdata(pdev);
502 	unsigned int max_irqs, min_irqs;
503 	int nvec, err;
504 
505 	if (pci_msix_can_alloc_dyn(pdev)) {
506 		max_irqs = 1;
507 		min_irqs = 1;
508 	} else {
509 		/* Need 1 interrupt for HWC */
510 		max_irqs = min(num_online_cpus(), MANA_MAX_NUM_QUEUES) + 1;
511 		min_irqs = 2;
512 		gc->msi_sharing = true;
513 	}
514 
515 	nvec = pci_alloc_irq_vectors(pdev, min_irqs, max_irqs, PCI_IRQ_MSIX);
516 	if (nvec < 0)
517 		return nvec;
518 
519 	err = mana_gd_setup_irqs(pdev, nvec);
520 	if (err) {
521 		pci_free_irq_vectors(pdev);
522 		return err;
523 	}
524 
525 	gc->num_msix_usable = nvec;
526 	gc->max_num_msix = nvec;
527 
528 	return 0;
529 }
530 
531 static int mana_gd_setup_remaining_irqs(struct pci_dev *pdev)
532 {
533 	struct gdma_context *gc = pci_get_drvdata(pdev);
534 	struct msi_map irq_map;
535 	int max_irqs, i, err;
536 
537 	if (!pci_msix_can_alloc_dyn(pdev))
538 		/* remain irqs are already allocated with HWC IRQ */
539 		return 0;
540 
541 	/* allocate only remaining IRQs*/
542 	max_irqs = gc->num_msix_usable - 1;
543 
544 	for (i = 1; i <= max_irqs; i++) {
545 		irq_map = pci_msix_alloc_irq_at(pdev, i, NULL);
546 		if (!irq_map.virq) {
547 			err = irq_map.index;
548 			/* caller will handle cleaning up all allocated
549 			 * irqs, after HWC is destroyed
550 			 */
551 			return err;
552 		}
553 	}
554 
555 	err = mana_gd_setup_dyn_irqs(pdev, max_irqs);
556 	if (err)
557 		return err;
558 
559 	gc->max_num_msix = gc->max_num_msix + max_irqs;
560 
561 	return 0;
562 }
563 
564 static void mana_gd_remove_irqs(struct pci_dev *pdev)
565 {
566 	struct gdma_context *gc = pci_get_drvdata(pdev);
567 	int i;
568 
569 	if (gc->max_num_msix < 1)
570 		return;
571 
572 	for (i = 0; i < gc->max_num_msix; i++) {
573 		if (!xa_load(&gc->irq_contexts, i))
574 			continue;
575 
576 		mana_gd_put_gic(gc, false, i);
577 	}
578 
579 	WARN_ON(!xa_empty(&gc->irq_contexts));
580 
581 	pci_free_irq_vectors(pdev);
582 
583 	bitmap_free(gc->msi_bitmap);
584 	gc->msi_bitmap = NULL;
585 	gc->max_num_msix = 0;
586 	gc->num_msix_usable = 0;
587 }
588 
589 static int mana_pci_setup_hwc_irqs(struct gdma_context *gc)
590 {
591 	return mana_gd_setup_hwc_irqs(to_pci_dev(gc->dev));
592 }
593 
594 static int mana_pci_setup_remaining_irqs(struct gdma_context *gc)
595 {
596 	int err;
597 
598 	err = mana_gd_setup_remaining_irqs(to_pci_dev(gc->dev));
599 	if (err) {
600 		dev_err(gc->dev, "Failed to setup remaining IRQs: %d", err);
601 		return err;
602 	}
603 
604 	if (!gc->msi_sharing) {
605 		gc->msi_bitmap = bitmap_zalloc(gc->num_msix_usable, GFP_KERNEL);
606 		if (!gc->msi_bitmap)
607 			return -ENOMEM;
608 		/* Set bit for HWC */
609 		set_bit(0, gc->msi_bitmap);
610 	}
611 
612 	return 0;
613 }
614 
615 static void mana_pci_remove_irqs(struct gdma_context *gc)
616 {
617 	mana_gd_remove_irqs(to_pci_dev(gc->dev));
618 }
619 
620 static const struct gdma_bus_ops mana_pci_bus_ops = {
621 	.bus_name		= "pci",
622 	.adev_prefix		= "mana",
623 	.msix_can_alloc_dyn	= mana_pci_msix_can_alloc_dyn,
624 	.msix_virq		= mana_pci_msix_virq,
625 	.msix_alloc_at		= mana_pci_msix_alloc_at,
626 	.msix_free		= mana_pci_msix_free,
627 	.msix_vec_count		= mana_pci_msix_vec_count,
628 	.setup_hwc_irqs		= mana_pci_setup_hwc_irqs,
629 	.setup_remaining_irqs	= mana_pci_setup_remaining_irqs,
630 	.remove_irqs		= mana_pci_remove_irqs,
631 	.dev_reset		= mana_pci_dev_reset,
632 	.schedule_serv_work	= mana_pci_schedule_serv_work,
633 	.drv_cap_flags1		= GDMA_DRV_CAP_FLAGS1_PCI,
634 };
635 
636 static bool mana_is_pf(unsigned short dev_id)
637 {
638 	return dev_id == MANA_PF_DEVICE_ID || dev_id == MANA_PF2_DEVICE_ID;
639 }
640 
641 static int mana_gd_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
642 {
643 	struct gdma_context *gc;
644 	void __iomem *bar0_va;
645 	int bar = 0;
646 	int err;
647 
648 	/* Each port has 2 CQs, each CQ has at most 1 EQE at a time */
649 	BUILD_BUG_ON(2 * MAX_PORTS_IN_MANA_DEV * GDMA_EQE_SIZE > EQ_SIZE);
650 
651 	err = pci_enable_device(pdev);
652 	if (err) {
653 		dev_err(&pdev->dev, "Failed to enable pci device (err=%d)\n", err);
654 		return -ENXIO;
655 	}
656 
657 	pci_set_master(pdev);
658 
659 	err = pci_request_regions(pdev, "mana");
660 	if (err)
661 		goto disable_dev;
662 
663 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
664 	if (err) {
665 		dev_err(&pdev->dev, "DMA set mask failed: %d\n", err);
666 		goto release_region;
667 	}
668 	dma_set_max_seg_size(&pdev->dev, UINT_MAX);
669 
670 	err = -ENOMEM;
671 	gc = vzalloc(sizeof(*gc));
672 	if (!gc)
673 		goto release_region;
674 
675 	mutex_init(&gc->eq_test_event_mutex);
676 	mutex_init(&gc->gic_mutex);
677 	pci_set_drvdata(pdev, gc);
678 	gc->bar0_pa = pci_resource_start(pdev, 0);
679 	gc->bar0_size = pci_resource_len(pdev, 0);
680 
681 	bar0_va = pci_iomap(pdev, bar, 0);
682 	if (!bar0_va)
683 		goto free_gc;
684 
685 	gc->numa_node = dev_to_node(&pdev->dev);
686 	gc->is_pf = mana_is_pf(pdev->device);
687 	gc->is_pf2 = (pdev->device == MANA_PF2_DEVICE_ID);
688 
689 	gc->bar0_va = bar0_va;
690 	gc->dev = &pdev->dev;
691 	gc->bus_ops = &mana_pci_bus_ops;
692 	xa_init(&gc->irq_contexts);
693 
694 	err = mana_gd_setup(gc);
695 	if (err)
696 		goto unmap_bar;
697 
698 	err = mana_probe(&gc->mana, false);
699 	if (err)
700 		goto cleanup_gd;
701 
702 	err = mana_rdma_probe(&gc->mana_ib);
703 	if (err)
704 		goto cleanup_mana;
705 
706 	/*
707 	 * If a hardware reset event has occurred over HWC during probe,
708 	 * rollback and perform hardware reset procedure.
709 	 */
710 	if (test_and_set_bit(GC_PROBE_SUCCEEDED, &gc->flags)) {
711 		err = -EPROTO;
712 		goto cleanup_mana_rdma;
713 	}
714 
715 	return 0;
716 
717 cleanup_mana_rdma:
718 	mana_rdma_remove(&gc->mana_ib);
719 cleanup_mana:
720 	mana_remove(&gc->mana, false);
721 cleanup_gd:
722 	mana_gd_cleanup(gc);
723 unmap_bar:
724 	xa_destroy(&gc->irq_contexts);
725 	pci_iounmap(pdev, bar0_va);
726 free_gc:
727 	pci_set_drvdata(pdev, NULL);
728 	vfree(gc);
729 release_region:
730 	pci_release_regions(pdev);
731 disable_dev:
732 	pci_disable_device(pdev);
733 	dev_err(&pdev->dev, "gdma probe failed: err = %d\n", err);
734 
735 	/*
736 	 * Hardware could be in recovery mode and the HWC returns TIMEDOUT or
737 	 * EPROTO from mana_gd_setup(), mana_probe() or mana_rdma_probe(), or
738 	 * we received a hardware reset event over HWC interrupt. In this case,
739 	 * perform the device recovery procedure after MANA_SERVICE_PERIOD
740 	 * seconds.
741 	 */
742 	if (err == -ETIMEDOUT || err == -EPROTO) {
743 		struct mana_dev_recovery *dev;
744 		unsigned long flags;
745 
746 		dev_info(&pdev->dev, "Start MANA recovery mode\n");
747 
748 		dev = kzalloc_obj(*dev);
749 		if (!dev)
750 			return err;
751 
752 		dev->pdev = pci_dev_get(pdev);
753 		dev->type = GDMA_EQE_HWC_RESET_REQUEST;
754 
755 		spin_lock_irqsave(&mana_dev_recovery_work.lock, flags);
756 		list_add_tail(&dev->list, &mana_dev_recovery_work.dev_list);
757 		spin_unlock_irqrestore(&mana_dev_recovery_work.lock, flags);
758 
759 		schedule_delayed_work(&mana_dev_recovery_work.work,
760 				      secs_to_jiffies(MANA_SERVICE_PERIOD));
761 	}
762 
763 	return err;
764 }
765 
766 static void mana_gd_remove(struct pci_dev *pdev)
767 {
768 	struct gdma_context *gc = pci_get_drvdata(pdev);
769 
770 	pci_disable_sriov(pdev);
771 
772 	mana_rdma_remove(&gc->mana_ib);
773 	mana_remove(&gc->mana, false);
774 
775 	mana_gd_cleanup(gc);
776 
777 	xa_destroy(&gc->irq_contexts);
778 
779 	pci_iounmap(pdev, gc->bar0_va);
780 
781 	vfree(gc);
782 
783 	pci_release_regions(pdev);
784 	pci_disable_device(pdev);
785 
786 	dev_dbg(&pdev->dev, "mana gdma remove successful\n");
787 }
788 
789 /* The 'state' parameter is not used. */
790 static int mana_gd_suspend(struct pci_dev *pdev, pm_message_t state)
791 {
792 	struct gdma_context *gc = pci_get_drvdata(pdev);
793 
794 	mana_rdma_remove(&gc->mana_ib);
795 	mana_remove(&gc->mana, true);
796 
797 	mana_gd_cleanup(gc);
798 
799 	return 0;
800 }
801 
802 static int mana_gd_resume(struct pci_dev *pdev)
803 {
804 	struct gdma_context *gc = pci_get_drvdata(pdev);
805 	int err;
806 
807 	err = mana_gd_setup(gc);
808 	if (err)
809 		return err;
810 
811 	err = mana_probe(&gc->mana, true);
812 	if (err)
813 		goto cleanup_gd;
814 
815 	err = mana_rdma_probe(&gc->mana_ib);
816 	if (err)
817 		mana_rdma_remove(&gc->mana_ib);
818 
819 	return err;
820 
821 cleanup_gd:
822 	mana_gd_cleanup(gc);
823 	return err;
824 }
825 
826 /* Quiesce the device for kexec. This is also called upon reboot/shutdown. */
827 static void mana_gd_shutdown(struct pci_dev *pdev)
828 {
829 	struct gdma_context *gc = pci_get_drvdata(pdev);
830 
831 	dev_info(&pdev->dev, "Shutdown was called\n");
832 
833 	mana_rdma_remove(&gc->mana_ib);
834 	mana_remove(&gc->mana, true);
835 
836 	mana_gd_cleanup(gc);
837 
838 	pci_disable_device(pdev);
839 }
840 
841 static int mana_sriov_configure(struct pci_dev *pdev, int numvfs)
842 {
843 	int err = 0;
844 
845 	dev_info(&pdev->dev, "Requested num VFs: %d\n", numvfs);
846 
847 	if (numvfs > 0) {
848 		err = pci_enable_sriov(pdev, numvfs);
849 	} else {
850 		if (pci_vfs_assigned(pdev)) {
851 			dev_warn(&pdev->dev,
852 				 "Cannot disable SR-IOV while VFs are assigned\n");
853 			return -EPERM;
854 		}
855 
856 		pci_disable_sriov(pdev);
857 	}
858 
859 	return err ? err : numvfs;
860 }
861 
862 static const struct pci_device_id mana_id_table[] = {
863 	{ PCI_DEVICE(PCI_VENDOR_ID_MICROSOFT, MANA_PF_DEVICE_ID) },
864 	{ PCI_DEVICE(PCI_VENDOR_ID_MICROSOFT, MANA_PF2_DEVICE_ID) },
865 	{ PCI_DEVICE(PCI_VENDOR_ID_MICROSOFT, MANA_VF_DEVICE_ID) },
866 	{ }
867 };
868 
869 static struct pci_driver mana_driver = {
870 	.name		= "mana",
871 	.id_table	= mana_id_table,
872 	.probe		= mana_gd_probe,
873 	.remove		= mana_gd_remove,
874 	.suspend	= mana_gd_suspend,
875 	.resume		= mana_gd_resume,
876 	.shutdown	= mana_gd_shutdown,
877 	.sriov_configure = mana_sriov_configure,
878 };
879 
880 MODULE_DEVICE_TABLE(pci, mana_id_table);
881 
882 static int __init mana_driver_init(void)
883 {
884 	INIT_LIST_HEAD(&mana_dev_recovery_work.dev_list);
885 	spin_lock_init(&mana_dev_recovery_work.lock);
886 	INIT_DELAYED_WORK(&mana_dev_recovery_work.work,
887 			  mana_recovery_delayed_func);
888 
889 	return pci_register_driver(&mana_driver);
890 }
891 
892 static void __exit mana_driver_exit(void)
893 {
894 	struct mana_dev_recovery *dev;
895 	unsigned long flags;
896 
897 	disable_delayed_work_sync(&mana_dev_recovery_work.work);
898 
899 	spin_lock_irqsave(&mana_dev_recovery_work.lock, flags);
900 	while (!list_empty(&mana_dev_recovery_work.dev_list)) {
901 		dev = list_first_entry(&mana_dev_recovery_work.dev_list,
902 				       struct mana_dev_recovery, list);
903 		list_del(&dev->list);
904 		pci_dev_put(dev->pdev);
905 		kfree(dev);
906 	}
907 	spin_unlock_irqrestore(&mana_dev_recovery_work.lock, flags);
908 
909 	pci_unregister_driver(&mana_driver);
910 }
911 
912 module_init(mana_driver_init);
913 module_exit(mana_driver_exit);
914 
915 MODULE_LICENSE("Dual BSD/GPL");
916 MODULE_DESCRIPTION("Microsoft Azure Network Adapter driver");
917 MODULE_IMPORT_NS("NET_MANA");
918