xref: /openbmc/linux/drivers/net/ethernet/marvell/octeontx2/nic/otx2_pf.c (revision 0760aad038b5a032c31ea124feed63d88627d2f1)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Marvell OcteonTx2 RVU Physcial Function ethernet driver
3  *
4  * Copyright (C) 2020 Marvell International Ltd.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10 
11 #include <linux/module.h>
12 #include <linux/interrupt.h>
13 #include <linux/pci.h>
14 #include <linux/etherdevice.h>
15 #include <linux/of.h>
16 #include <linux/if_vlan.h>
17 #include <linux/iommu.h>
18 #include <net/ip.h>
19 
20 #include "otx2_reg.h"
21 #include "otx2_common.h"
22 #include "otx2_txrx.h"
23 #include "otx2_struct.h"
24 #include "otx2_ptp.h"
25 
26 #define DRV_NAME	"octeontx2-nicpf"
27 #define DRV_STRING	"Marvell OcteonTX2 NIC Physical Function Driver"
28 
29 /* Supported devices */
30 static const struct pci_device_id otx2_pf_id_table[] = {
31 	{ PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVID_OCTEONTX2_RVU_PF) },
32 	{ 0, }  /* end of table */
33 };
34 
35 MODULE_AUTHOR("Sunil Goutham <sgoutham@marvell.com>");
36 MODULE_DESCRIPTION(DRV_STRING);
37 MODULE_LICENSE("GPL v2");
38 MODULE_DEVICE_TABLE(pci, otx2_pf_id_table);
39 
40 enum {
41 	TYPE_PFAF,
42 	TYPE_PFVF,
43 };
44 
45 static int otx2_config_hw_tx_tstamp(struct otx2_nic *pfvf, bool enable);
46 static int otx2_config_hw_rx_tstamp(struct otx2_nic *pfvf, bool enable);
47 
48 static int otx2_change_mtu(struct net_device *netdev, int new_mtu)
49 {
50 	bool if_up = netif_running(netdev);
51 	int err = 0;
52 
53 	if (if_up)
54 		otx2_stop(netdev);
55 
56 	netdev_info(netdev, "Changing MTU from %d to %d\n",
57 		    netdev->mtu, new_mtu);
58 	netdev->mtu = new_mtu;
59 
60 	if (if_up)
61 		err = otx2_open(netdev);
62 
63 	return err;
64 }
65 
66 static void otx2_disable_flr_me_intr(struct otx2_nic *pf)
67 {
68 	int irq, vfs = pf->total_vfs;
69 
70 	/* Disable VFs ME interrupts */
71 	otx2_write64(pf, RVU_PF_VFME_INT_ENA_W1CX(0), INTR_MASK(vfs));
72 	irq = pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFME0);
73 	free_irq(irq, pf);
74 
75 	/* Disable VFs FLR interrupts */
76 	otx2_write64(pf, RVU_PF_VFFLR_INT_ENA_W1CX(0), INTR_MASK(vfs));
77 	irq = pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFFLR0);
78 	free_irq(irq, pf);
79 
80 	if (vfs <= 64)
81 		return;
82 
83 	otx2_write64(pf, RVU_PF_VFME_INT_ENA_W1CX(1), INTR_MASK(vfs - 64));
84 	irq = pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFME1);
85 	free_irq(irq, pf);
86 
87 	otx2_write64(pf, RVU_PF_VFFLR_INT_ENA_W1CX(1), INTR_MASK(vfs - 64));
88 	irq = pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFFLR1);
89 	free_irq(irq, pf);
90 }
91 
92 static void otx2_flr_wq_destroy(struct otx2_nic *pf)
93 {
94 	if (!pf->flr_wq)
95 		return;
96 	destroy_workqueue(pf->flr_wq);
97 	pf->flr_wq = NULL;
98 	devm_kfree(pf->dev, pf->flr_wrk);
99 }
100 
101 static void otx2_flr_handler(struct work_struct *work)
102 {
103 	struct flr_work *flrwork = container_of(work, struct flr_work, work);
104 	struct otx2_nic *pf = flrwork->pf;
105 	struct mbox *mbox = &pf->mbox;
106 	struct msg_req *req;
107 	int vf, reg = 0;
108 
109 	vf = flrwork - pf->flr_wrk;
110 
111 	mutex_lock(&mbox->lock);
112 	req = otx2_mbox_alloc_msg_vf_flr(mbox);
113 	if (!req) {
114 		mutex_unlock(&mbox->lock);
115 		return;
116 	}
117 	req->hdr.pcifunc &= RVU_PFVF_FUNC_MASK;
118 	req->hdr.pcifunc |= (vf + 1) & RVU_PFVF_FUNC_MASK;
119 
120 	if (!otx2_sync_mbox_msg(&pf->mbox)) {
121 		if (vf >= 64) {
122 			reg = 1;
123 			vf = vf - 64;
124 		}
125 		/* clear transcation pending bit */
126 		otx2_write64(pf, RVU_PF_VFTRPENDX(reg), BIT_ULL(vf));
127 		otx2_write64(pf, RVU_PF_VFFLR_INT_ENA_W1SX(reg), BIT_ULL(vf));
128 	}
129 
130 	mutex_unlock(&mbox->lock);
131 }
132 
133 static irqreturn_t otx2_pf_flr_intr_handler(int irq, void *pf_irq)
134 {
135 	struct otx2_nic *pf = (struct otx2_nic *)pf_irq;
136 	int reg, dev, vf, start_vf, num_reg = 1;
137 	u64 intr;
138 
139 	if (pf->total_vfs > 64)
140 		num_reg = 2;
141 
142 	for (reg = 0; reg < num_reg; reg++) {
143 		intr = otx2_read64(pf, RVU_PF_VFFLR_INTX(reg));
144 		if (!intr)
145 			continue;
146 		start_vf = 64 * reg;
147 		for (vf = 0; vf < 64; vf++) {
148 			if (!(intr & BIT_ULL(vf)))
149 				continue;
150 			dev = vf + start_vf;
151 			queue_work(pf->flr_wq, &pf->flr_wrk[dev].work);
152 			/* Clear interrupt */
153 			otx2_write64(pf, RVU_PF_VFFLR_INTX(reg), BIT_ULL(vf));
154 			/* Disable the interrupt */
155 			otx2_write64(pf, RVU_PF_VFFLR_INT_ENA_W1CX(reg),
156 				     BIT_ULL(vf));
157 		}
158 	}
159 	return IRQ_HANDLED;
160 }
161 
162 static irqreturn_t otx2_pf_me_intr_handler(int irq, void *pf_irq)
163 {
164 	struct otx2_nic *pf = (struct otx2_nic *)pf_irq;
165 	int vf, reg, num_reg = 1;
166 	u64 intr;
167 
168 	if (pf->total_vfs > 64)
169 		num_reg = 2;
170 
171 	for (reg = 0; reg < num_reg; reg++) {
172 		intr = otx2_read64(pf, RVU_PF_VFME_INTX(reg));
173 		if (!intr)
174 			continue;
175 		for (vf = 0; vf < 64; vf++) {
176 			if (!(intr & BIT_ULL(vf)))
177 				continue;
178 			/* clear trpend bit */
179 			otx2_write64(pf, RVU_PF_VFTRPENDX(reg), BIT_ULL(vf));
180 			/* clear interrupt */
181 			otx2_write64(pf, RVU_PF_VFME_INTX(reg), BIT_ULL(vf));
182 		}
183 	}
184 	return IRQ_HANDLED;
185 }
186 
187 static int otx2_register_flr_me_intr(struct otx2_nic *pf, int numvfs)
188 {
189 	struct otx2_hw *hw = &pf->hw;
190 	char *irq_name;
191 	int ret;
192 
193 	/* Register ME interrupt handler*/
194 	irq_name = &hw->irq_name[RVU_PF_INT_VEC_VFME0 * NAME_SIZE];
195 	snprintf(irq_name, NAME_SIZE, "RVUPF%d_ME0", rvu_get_pf(pf->pcifunc));
196 	ret = request_irq(pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFME0),
197 			  otx2_pf_me_intr_handler, 0, irq_name, pf);
198 	if (ret) {
199 		dev_err(pf->dev,
200 			"RVUPF: IRQ registration failed for ME0\n");
201 	}
202 
203 	/* Register FLR interrupt handler */
204 	irq_name = &hw->irq_name[RVU_PF_INT_VEC_VFFLR0 * NAME_SIZE];
205 	snprintf(irq_name, NAME_SIZE, "RVUPF%d_FLR0", rvu_get_pf(pf->pcifunc));
206 	ret = request_irq(pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFFLR0),
207 			  otx2_pf_flr_intr_handler, 0, irq_name, pf);
208 	if (ret) {
209 		dev_err(pf->dev,
210 			"RVUPF: IRQ registration failed for FLR0\n");
211 		return ret;
212 	}
213 
214 	if (numvfs > 64) {
215 		irq_name = &hw->irq_name[RVU_PF_INT_VEC_VFME1 * NAME_SIZE];
216 		snprintf(irq_name, NAME_SIZE, "RVUPF%d_ME1",
217 			 rvu_get_pf(pf->pcifunc));
218 		ret = request_irq(pci_irq_vector
219 				  (pf->pdev, RVU_PF_INT_VEC_VFME1),
220 				  otx2_pf_me_intr_handler, 0, irq_name, pf);
221 		if (ret) {
222 			dev_err(pf->dev,
223 				"RVUPF: IRQ registration failed for ME1\n");
224 		}
225 		irq_name = &hw->irq_name[RVU_PF_INT_VEC_VFFLR1 * NAME_SIZE];
226 		snprintf(irq_name, NAME_SIZE, "RVUPF%d_FLR1",
227 			 rvu_get_pf(pf->pcifunc));
228 		ret = request_irq(pci_irq_vector
229 				  (pf->pdev, RVU_PF_INT_VEC_VFFLR1),
230 				  otx2_pf_flr_intr_handler, 0, irq_name, pf);
231 		if (ret) {
232 			dev_err(pf->dev,
233 				"RVUPF: IRQ registration failed for FLR1\n");
234 			return ret;
235 		}
236 	}
237 
238 	/* Enable ME interrupt for all VFs*/
239 	otx2_write64(pf, RVU_PF_VFME_INTX(0), INTR_MASK(numvfs));
240 	otx2_write64(pf, RVU_PF_VFME_INT_ENA_W1SX(0), INTR_MASK(numvfs));
241 
242 	/* Enable FLR interrupt for all VFs*/
243 	otx2_write64(pf, RVU_PF_VFFLR_INTX(0), INTR_MASK(numvfs));
244 	otx2_write64(pf, RVU_PF_VFFLR_INT_ENA_W1SX(0), INTR_MASK(numvfs));
245 
246 	if (numvfs > 64) {
247 		numvfs -= 64;
248 
249 		otx2_write64(pf, RVU_PF_VFME_INTX(1), INTR_MASK(numvfs));
250 		otx2_write64(pf, RVU_PF_VFME_INT_ENA_W1SX(1),
251 			     INTR_MASK(numvfs));
252 
253 		otx2_write64(pf, RVU_PF_VFFLR_INTX(1), INTR_MASK(numvfs));
254 		otx2_write64(pf, RVU_PF_VFFLR_INT_ENA_W1SX(1),
255 			     INTR_MASK(numvfs));
256 	}
257 	return 0;
258 }
259 
260 static int otx2_pf_flr_init(struct otx2_nic *pf, int num_vfs)
261 {
262 	int vf;
263 
264 	pf->flr_wq = alloc_workqueue("otx2_pf_flr_wq",
265 				     WQ_UNBOUND | WQ_HIGHPRI, 1);
266 	if (!pf->flr_wq)
267 		return -ENOMEM;
268 
269 	pf->flr_wrk = devm_kcalloc(pf->dev, num_vfs,
270 				   sizeof(struct flr_work), GFP_KERNEL);
271 	if (!pf->flr_wrk) {
272 		destroy_workqueue(pf->flr_wq);
273 		return -ENOMEM;
274 	}
275 
276 	for (vf = 0; vf < num_vfs; vf++) {
277 		pf->flr_wrk[vf].pf = pf;
278 		INIT_WORK(&pf->flr_wrk[vf].work, otx2_flr_handler);
279 	}
280 
281 	return 0;
282 }
283 
284 static void otx2_queue_work(struct mbox *mw, struct workqueue_struct *mbox_wq,
285 			    int first, int mdevs, u64 intr, int type)
286 {
287 	struct otx2_mbox_dev *mdev;
288 	struct otx2_mbox *mbox;
289 	struct mbox_hdr *hdr;
290 	int i;
291 
292 	for (i = first; i < mdevs; i++) {
293 		/* start from 0 */
294 		if (!(intr & BIT_ULL(i - first)))
295 			continue;
296 
297 		mbox = &mw->mbox;
298 		mdev = &mbox->dev[i];
299 		if (type == TYPE_PFAF)
300 			otx2_sync_mbox_bbuf(mbox, i);
301 		hdr = mdev->mbase + mbox->rx_start;
302 		/* The hdr->num_msgs is set to zero immediately in the interrupt
303 		 * handler to  ensure that it holds a correct value next time
304 		 * when the interrupt handler is called.
305 		 * pf->mbox.num_msgs holds the data for use in pfaf_mbox_handler
306 		 * pf>mbox.up_num_msgs holds the data for use in
307 		 * pfaf_mbox_up_handler.
308 		 */
309 		if (hdr->num_msgs) {
310 			mw[i].num_msgs = hdr->num_msgs;
311 			hdr->num_msgs = 0;
312 			if (type == TYPE_PFAF)
313 				memset(mbox->hwbase + mbox->rx_start, 0,
314 				       ALIGN(sizeof(struct mbox_hdr),
315 					     sizeof(u64)));
316 
317 			queue_work(mbox_wq, &mw[i].mbox_wrk);
318 		}
319 
320 		mbox = &mw->mbox_up;
321 		mdev = &mbox->dev[i];
322 		if (type == TYPE_PFAF)
323 			otx2_sync_mbox_bbuf(mbox, i);
324 		hdr = mdev->mbase + mbox->rx_start;
325 		if (hdr->num_msgs) {
326 			mw[i].up_num_msgs = hdr->num_msgs;
327 			hdr->num_msgs = 0;
328 			if (type == TYPE_PFAF)
329 				memset(mbox->hwbase + mbox->rx_start, 0,
330 				       ALIGN(sizeof(struct mbox_hdr),
331 					     sizeof(u64)));
332 
333 			queue_work(mbox_wq, &mw[i].mbox_up_wrk);
334 		}
335 	}
336 }
337 
338 static void otx2_forward_msg_pfvf(struct otx2_mbox_dev *mdev,
339 				  struct otx2_mbox *pfvf_mbox, void *bbuf_base,
340 				  int devid)
341 {
342 	struct otx2_mbox_dev *src_mdev = mdev;
343 	int offset;
344 
345 	/* Msgs are already copied, trigger VF's mbox irq */
346 	smp_wmb();
347 
348 	offset = pfvf_mbox->trigger | (devid << pfvf_mbox->tr_shift);
349 	writeq(1, (void __iomem *)pfvf_mbox->reg_base + offset);
350 
351 	/* Restore VF's mbox bounce buffer region address */
352 	src_mdev->mbase = bbuf_base;
353 }
354 
355 static int otx2_forward_vf_mbox_msgs(struct otx2_nic *pf,
356 				     struct otx2_mbox *src_mbox,
357 				     int dir, int vf, int num_msgs)
358 {
359 	struct otx2_mbox_dev *src_mdev, *dst_mdev;
360 	struct mbox_hdr *mbox_hdr;
361 	struct mbox_hdr *req_hdr;
362 	struct mbox *dst_mbox;
363 	int dst_size, err;
364 
365 	if (dir == MBOX_DIR_PFAF) {
366 		/* Set VF's mailbox memory as PF's bounce buffer memory, so
367 		 * that explicit copying of VF's msgs to PF=>AF mbox region
368 		 * and AF=>PF responses to VF's mbox region can be avoided.
369 		 */
370 		src_mdev = &src_mbox->dev[vf];
371 		mbox_hdr = src_mbox->hwbase +
372 				src_mbox->rx_start + (vf * MBOX_SIZE);
373 
374 		dst_mbox = &pf->mbox;
375 		dst_size = dst_mbox->mbox.tx_size -
376 				ALIGN(sizeof(*mbox_hdr), MBOX_MSG_ALIGN);
377 		/* Check if msgs fit into destination area */
378 		if (mbox_hdr->msg_size > dst_size)
379 			return -EINVAL;
380 
381 		dst_mdev = &dst_mbox->mbox.dev[0];
382 
383 		mutex_lock(&pf->mbox.lock);
384 		dst_mdev->mbase = src_mdev->mbase;
385 		dst_mdev->msg_size = mbox_hdr->msg_size;
386 		dst_mdev->num_msgs = num_msgs;
387 		err = otx2_sync_mbox_msg(dst_mbox);
388 		if (err) {
389 			dev_warn(pf->dev,
390 				 "AF not responding to VF%d messages\n", vf);
391 			/* restore PF mbase and exit */
392 			dst_mdev->mbase = pf->mbox.bbuf_base;
393 			mutex_unlock(&pf->mbox.lock);
394 			return err;
395 		}
396 		/* At this point, all the VF messages sent to AF are acked
397 		 * with proper responses and responses are copied to VF
398 		 * mailbox hence raise interrupt to VF.
399 		 */
400 		req_hdr = (struct mbox_hdr *)(dst_mdev->mbase +
401 					      dst_mbox->mbox.rx_start);
402 		req_hdr->num_msgs = num_msgs;
403 
404 		otx2_forward_msg_pfvf(dst_mdev, &pf->mbox_pfvf[0].mbox,
405 				      pf->mbox.bbuf_base, vf);
406 		mutex_unlock(&pf->mbox.lock);
407 	} else if (dir == MBOX_DIR_PFVF_UP) {
408 		src_mdev = &src_mbox->dev[0];
409 		mbox_hdr = src_mbox->hwbase + src_mbox->rx_start;
410 		req_hdr = (struct mbox_hdr *)(src_mdev->mbase +
411 					      src_mbox->rx_start);
412 		req_hdr->num_msgs = num_msgs;
413 
414 		dst_mbox = &pf->mbox_pfvf[0];
415 		dst_size = dst_mbox->mbox_up.tx_size -
416 				ALIGN(sizeof(*mbox_hdr), MBOX_MSG_ALIGN);
417 		/* Check if msgs fit into destination area */
418 		if (mbox_hdr->msg_size > dst_size)
419 			return -EINVAL;
420 
421 		dst_mdev = &dst_mbox->mbox_up.dev[vf];
422 		dst_mdev->mbase = src_mdev->mbase;
423 		dst_mdev->msg_size = mbox_hdr->msg_size;
424 		dst_mdev->num_msgs = mbox_hdr->num_msgs;
425 		err = otx2_sync_mbox_up_msg(dst_mbox, vf);
426 		if (err) {
427 			dev_warn(pf->dev,
428 				 "VF%d is not responding to mailbox\n", vf);
429 			return err;
430 		}
431 	} else if (dir == MBOX_DIR_VFPF_UP) {
432 		req_hdr = (struct mbox_hdr *)(src_mbox->dev[0].mbase +
433 					      src_mbox->rx_start);
434 		req_hdr->num_msgs = num_msgs;
435 		otx2_forward_msg_pfvf(&pf->mbox_pfvf->mbox_up.dev[vf],
436 				      &pf->mbox.mbox_up,
437 				      pf->mbox_pfvf[vf].bbuf_base,
438 				      0);
439 	}
440 
441 	return 0;
442 }
443 
444 static void otx2_pfvf_mbox_handler(struct work_struct *work)
445 {
446 	struct mbox_msghdr *msg = NULL;
447 	int offset, vf_idx, id, err;
448 	struct otx2_mbox_dev *mdev;
449 	struct mbox_hdr *req_hdr;
450 	struct otx2_mbox *mbox;
451 	struct mbox *vf_mbox;
452 	struct otx2_nic *pf;
453 
454 	vf_mbox = container_of(work, struct mbox, mbox_wrk);
455 	pf = vf_mbox->pfvf;
456 	vf_idx = vf_mbox - pf->mbox_pfvf;
457 
458 	mbox = &pf->mbox_pfvf[0].mbox;
459 	mdev = &mbox->dev[vf_idx];
460 	req_hdr = (struct mbox_hdr *)(mdev->mbase + mbox->rx_start);
461 
462 	offset = ALIGN(sizeof(*req_hdr), MBOX_MSG_ALIGN);
463 
464 	for (id = 0; id < vf_mbox->num_msgs; id++) {
465 		msg = (struct mbox_msghdr *)(mdev->mbase + mbox->rx_start +
466 					     offset);
467 
468 		if (msg->sig != OTX2_MBOX_REQ_SIG)
469 			goto inval_msg;
470 
471 		/* Set VF's number in each of the msg */
472 		msg->pcifunc &= RVU_PFVF_FUNC_MASK;
473 		msg->pcifunc |= (vf_idx + 1) & RVU_PFVF_FUNC_MASK;
474 		offset = msg->next_msgoff;
475 	}
476 	err = otx2_forward_vf_mbox_msgs(pf, mbox, MBOX_DIR_PFAF, vf_idx,
477 					vf_mbox->num_msgs);
478 	if (err)
479 		goto inval_msg;
480 	return;
481 
482 inval_msg:
483 	otx2_reply_invalid_msg(mbox, vf_idx, 0, msg->id);
484 	otx2_mbox_msg_send(mbox, vf_idx);
485 }
486 
487 static void otx2_pfvf_mbox_up_handler(struct work_struct *work)
488 {
489 	struct mbox *vf_mbox = container_of(work, struct mbox, mbox_up_wrk);
490 	struct otx2_nic *pf = vf_mbox->pfvf;
491 	struct otx2_mbox_dev *mdev;
492 	int offset, id, vf_idx = 0;
493 	struct mbox_hdr *rsp_hdr;
494 	struct mbox_msghdr *msg;
495 	struct otx2_mbox *mbox;
496 
497 	vf_idx = vf_mbox - pf->mbox_pfvf;
498 	mbox = &pf->mbox_pfvf[0].mbox_up;
499 	mdev = &mbox->dev[vf_idx];
500 
501 	rsp_hdr = (struct mbox_hdr *)(mdev->mbase + mbox->rx_start);
502 	offset = mbox->rx_start + ALIGN(sizeof(*rsp_hdr), MBOX_MSG_ALIGN);
503 
504 	for (id = 0; id < vf_mbox->up_num_msgs; id++) {
505 		msg = mdev->mbase + offset;
506 
507 		if (msg->id >= MBOX_MSG_MAX) {
508 			dev_err(pf->dev,
509 				"Mbox msg with unknown ID 0x%x\n", msg->id);
510 			goto end;
511 		}
512 
513 		if (msg->sig != OTX2_MBOX_RSP_SIG) {
514 			dev_err(pf->dev,
515 				"Mbox msg with wrong signature %x, ID 0x%x\n",
516 				msg->sig, msg->id);
517 			goto end;
518 		}
519 
520 		switch (msg->id) {
521 		case MBOX_MSG_CGX_LINK_EVENT:
522 			break;
523 		default:
524 			if (msg->rc)
525 				dev_err(pf->dev,
526 					"Mbox msg response has err %d, ID 0x%x\n",
527 					msg->rc, msg->id);
528 			break;
529 		}
530 
531 end:
532 		offset = mbox->rx_start + msg->next_msgoff;
533 		mdev->msgs_acked++;
534 	}
535 
536 	otx2_mbox_reset(mbox, vf_idx);
537 }
538 
539 static irqreturn_t otx2_pfvf_mbox_intr_handler(int irq, void *pf_irq)
540 {
541 	struct otx2_nic *pf = (struct otx2_nic *)(pf_irq);
542 	int vfs = pf->total_vfs;
543 	struct mbox *mbox;
544 	u64 intr;
545 
546 	mbox = pf->mbox_pfvf;
547 	/* Handle VF interrupts */
548 	if (vfs > 64) {
549 		intr = otx2_read64(pf, RVU_PF_VFPF_MBOX_INTX(1));
550 		otx2_write64(pf, RVU_PF_VFPF_MBOX_INTX(1), intr);
551 		otx2_queue_work(mbox, pf->mbox_pfvf_wq, 64, vfs, intr,
552 				TYPE_PFVF);
553 		vfs -= 64;
554 	}
555 
556 	intr = otx2_read64(pf, RVU_PF_VFPF_MBOX_INTX(0));
557 	otx2_write64(pf, RVU_PF_VFPF_MBOX_INTX(0), intr);
558 
559 	otx2_queue_work(mbox, pf->mbox_pfvf_wq, 0, vfs, intr, TYPE_PFVF);
560 
561 	return IRQ_HANDLED;
562 }
563 
564 static int otx2_pfvf_mbox_init(struct otx2_nic *pf, int numvfs)
565 {
566 	void __iomem *hwbase;
567 	struct mbox *mbox;
568 	int err, vf;
569 	u64 base;
570 
571 	if (!numvfs)
572 		return -EINVAL;
573 
574 	pf->mbox_pfvf = devm_kcalloc(&pf->pdev->dev, numvfs,
575 				     sizeof(struct mbox), GFP_KERNEL);
576 	if (!pf->mbox_pfvf)
577 		return -ENOMEM;
578 
579 	pf->mbox_pfvf_wq = alloc_workqueue("otx2_pfvf_mailbox",
580 					   WQ_UNBOUND | WQ_HIGHPRI |
581 					   WQ_MEM_RECLAIM, 1);
582 	if (!pf->mbox_pfvf_wq)
583 		return -ENOMEM;
584 
585 	base = readq((void __iomem *)((u64)pf->reg_base + RVU_PF_VF_BAR4_ADDR));
586 	hwbase = ioremap_wc(base, MBOX_SIZE * pf->total_vfs);
587 
588 	if (!hwbase) {
589 		err = -ENOMEM;
590 		goto free_wq;
591 	}
592 
593 	mbox = &pf->mbox_pfvf[0];
594 	err = otx2_mbox_init(&mbox->mbox, hwbase, pf->pdev, pf->reg_base,
595 			     MBOX_DIR_PFVF, numvfs);
596 	if (err)
597 		goto free_iomem;
598 
599 	err = otx2_mbox_init(&mbox->mbox_up, hwbase, pf->pdev, pf->reg_base,
600 			     MBOX_DIR_PFVF_UP, numvfs);
601 	if (err)
602 		goto free_iomem;
603 
604 	for (vf = 0; vf < numvfs; vf++) {
605 		mbox->pfvf = pf;
606 		INIT_WORK(&mbox->mbox_wrk, otx2_pfvf_mbox_handler);
607 		INIT_WORK(&mbox->mbox_up_wrk, otx2_pfvf_mbox_up_handler);
608 		mbox++;
609 	}
610 
611 	return 0;
612 
613 free_iomem:
614 	if (hwbase)
615 		iounmap(hwbase);
616 free_wq:
617 	destroy_workqueue(pf->mbox_pfvf_wq);
618 	return err;
619 }
620 
621 static void otx2_pfvf_mbox_destroy(struct otx2_nic *pf)
622 {
623 	struct mbox *mbox = &pf->mbox_pfvf[0];
624 
625 	if (!mbox)
626 		return;
627 
628 	if (pf->mbox_pfvf_wq) {
629 		destroy_workqueue(pf->mbox_pfvf_wq);
630 		pf->mbox_pfvf_wq = NULL;
631 	}
632 
633 	if (mbox->mbox.hwbase)
634 		iounmap(mbox->mbox.hwbase);
635 
636 	otx2_mbox_destroy(&mbox->mbox);
637 }
638 
639 static void otx2_enable_pfvf_mbox_intr(struct otx2_nic *pf, int numvfs)
640 {
641 	/* Clear PF <=> VF mailbox IRQ */
642 	otx2_write64(pf, RVU_PF_VFPF_MBOX_INTX(0), ~0ull);
643 	otx2_write64(pf, RVU_PF_VFPF_MBOX_INTX(1), ~0ull);
644 
645 	/* Enable PF <=> VF mailbox IRQ */
646 	otx2_write64(pf, RVU_PF_VFPF_MBOX_INT_ENA_W1SX(0), INTR_MASK(numvfs));
647 	if (numvfs > 64) {
648 		numvfs -= 64;
649 		otx2_write64(pf, RVU_PF_VFPF_MBOX_INT_ENA_W1SX(1),
650 			     INTR_MASK(numvfs));
651 	}
652 }
653 
654 static void otx2_disable_pfvf_mbox_intr(struct otx2_nic *pf, int numvfs)
655 {
656 	int vector;
657 
658 	/* Disable PF <=> VF mailbox IRQ */
659 	otx2_write64(pf, RVU_PF_VFPF_MBOX_INT_ENA_W1CX(0), ~0ull);
660 	otx2_write64(pf, RVU_PF_VFPF_MBOX_INT_ENA_W1CX(1), ~0ull);
661 
662 	otx2_write64(pf, RVU_PF_VFPF_MBOX_INTX(0), ~0ull);
663 	vector = pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFPF_MBOX0);
664 	free_irq(vector, pf);
665 
666 	if (numvfs > 64) {
667 		otx2_write64(pf, RVU_PF_VFPF_MBOX_INTX(1), ~0ull);
668 		vector = pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFPF_MBOX1);
669 		free_irq(vector, pf);
670 	}
671 }
672 
673 static int otx2_register_pfvf_mbox_intr(struct otx2_nic *pf, int numvfs)
674 {
675 	struct otx2_hw *hw = &pf->hw;
676 	char *irq_name;
677 	int err;
678 
679 	/* Register MBOX0 interrupt handler */
680 	irq_name = &hw->irq_name[RVU_PF_INT_VEC_VFPF_MBOX0 * NAME_SIZE];
681 	if (pf->pcifunc)
682 		snprintf(irq_name, NAME_SIZE,
683 			 "RVUPF%d_VF Mbox0", rvu_get_pf(pf->pcifunc));
684 	else
685 		snprintf(irq_name, NAME_SIZE, "RVUPF_VF Mbox0");
686 	err = request_irq(pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFPF_MBOX0),
687 			  otx2_pfvf_mbox_intr_handler, 0, irq_name, pf);
688 	if (err) {
689 		dev_err(pf->dev,
690 			"RVUPF: IRQ registration failed for PFVF mbox0 irq\n");
691 		return err;
692 	}
693 
694 	if (numvfs > 64) {
695 		/* Register MBOX1 interrupt handler */
696 		irq_name = &hw->irq_name[RVU_PF_INT_VEC_VFPF_MBOX1 * NAME_SIZE];
697 		if (pf->pcifunc)
698 			snprintf(irq_name, NAME_SIZE,
699 				 "RVUPF%d_VF Mbox1", rvu_get_pf(pf->pcifunc));
700 		else
701 			snprintf(irq_name, NAME_SIZE, "RVUPF_VF Mbox1");
702 		err = request_irq(pci_irq_vector(pf->pdev,
703 						 RVU_PF_INT_VEC_VFPF_MBOX1),
704 						 otx2_pfvf_mbox_intr_handler,
705 						 0, irq_name, pf);
706 		if (err) {
707 			dev_err(pf->dev,
708 				"RVUPF: IRQ registration failed for PFVF mbox1 irq\n");
709 			return err;
710 		}
711 	}
712 
713 	otx2_enable_pfvf_mbox_intr(pf, numvfs);
714 
715 	return 0;
716 }
717 
718 static void otx2_process_pfaf_mbox_msg(struct otx2_nic *pf,
719 				       struct mbox_msghdr *msg)
720 {
721 	int devid;
722 
723 	if (msg->id >= MBOX_MSG_MAX) {
724 		dev_err(pf->dev,
725 			"Mbox msg with unknown ID 0x%x\n", msg->id);
726 		return;
727 	}
728 
729 	if (msg->sig != OTX2_MBOX_RSP_SIG) {
730 		dev_err(pf->dev,
731 			"Mbox msg with wrong signature %x, ID 0x%x\n",
732 			 msg->sig, msg->id);
733 		return;
734 	}
735 
736 	/* message response heading VF */
737 	devid = msg->pcifunc & RVU_PFVF_FUNC_MASK;
738 	if (devid) {
739 		struct otx2_vf_config *config = &pf->vf_configs[devid - 1];
740 		struct delayed_work *dwork;
741 
742 		switch (msg->id) {
743 		case MBOX_MSG_NIX_LF_START_RX:
744 			config->intf_down = false;
745 			dwork = &config->link_event_work;
746 			schedule_delayed_work(dwork, msecs_to_jiffies(100));
747 			break;
748 		case MBOX_MSG_NIX_LF_STOP_RX:
749 			config->intf_down = true;
750 			break;
751 		}
752 
753 		return;
754 	}
755 
756 	switch (msg->id) {
757 	case MBOX_MSG_READY:
758 		pf->pcifunc = msg->pcifunc;
759 		break;
760 	case MBOX_MSG_MSIX_OFFSET:
761 		mbox_handler_msix_offset(pf, (struct msix_offset_rsp *)msg);
762 		break;
763 	case MBOX_MSG_NPA_LF_ALLOC:
764 		mbox_handler_npa_lf_alloc(pf, (struct npa_lf_alloc_rsp *)msg);
765 		break;
766 	case MBOX_MSG_NIX_LF_ALLOC:
767 		mbox_handler_nix_lf_alloc(pf, (struct nix_lf_alloc_rsp *)msg);
768 		break;
769 	case MBOX_MSG_NIX_TXSCH_ALLOC:
770 		mbox_handler_nix_txsch_alloc(pf,
771 					     (struct nix_txsch_alloc_rsp *)msg);
772 		break;
773 	case MBOX_MSG_NIX_BP_ENABLE:
774 		mbox_handler_nix_bp_enable(pf, (struct nix_bp_cfg_rsp *)msg);
775 		break;
776 	case MBOX_MSG_CGX_STATS:
777 		mbox_handler_cgx_stats(pf, (struct cgx_stats_rsp *)msg);
778 		break;
779 	default:
780 		if (msg->rc)
781 			dev_err(pf->dev,
782 				"Mbox msg response has err %d, ID 0x%x\n",
783 				msg->rc, msg->id);
784 		break;
785 	}
786 }
787 
788 static void otx2_pfaf_mbox_handler(struct work_struct *work)
789 {
790 	struct otx2_mbox_dev *mdev;
791 	struct mbox_hdr *rsp_hdr;
792 	struct mbox_msghdr *msg;
793 	struct otx2_mbox *mbox;
794 	struct mbox *af_mbox;
795 	struct otx2_nic *pf;
796 	int offset, id;
797 
798 	af_mbox = container_of(work, struct mbox, mbox_wrk);
799 	mbox = &af_mbox->mbox;
800 	mdev = &mbox->dev[0];
801 	rsp_hdr = (struct mbox_hdr *)(mdev->mbase + mbox->rx_start);
802 
803 	offset = mbox->rx_start + ALIGN(sizeof(*rsp_hdr), MBOX_MSG_ALIGN);
804 	pf = af_mbox->pfvf;
805 
806 	for (id = 0; id < af_mbox->num_msgs; id++) {
807 		msg = (struct mbox_msghdr *)(mdev->mbase + offset);
808 		otx2_process_pfaf_mbox_msg(pf, msg);
809 		offset = mbox->rx_start + msg->next_msgoff;
810 		mdev->msgs_acked++;
811 	}
812 
813 	otx2_mbox_reset(mbox, 0);
814 }
815 
816 static void otx2_handle_link_event(struct otx2_nic *pf)
817 {
818 	struct cgx_link_user_info *linfo = &pf->linfo;
819 	struct net_device *netdev = pf->netdev;
820 
821 	pr_info("%s NIC Link is %s %d Mbps %s duplex\n", netdev->name,
822 		linfo->link_up ? "UP" : "DOWN", linfo->speed,
823 		linfo->full_duplex ? "Full" : "Half");
824 	if (linfo->link_up) {
825 		netif_carrier_on(netdev);
826 		netif_tx_start_all_queues(netdev);
827 	} else {
828 		netif_tx_stop_all_queues(netdev);
829 		netif_carrier_off(netdev);
830 	}
831 }
832 
833 int otx2_mbox_up_handler_cgx_link_event(struct otx2_nic *pf,
834 					struct cgx_link_info_msg *msg,
835 					struct msg_rsp *rsp)
836 {
837 	int i;
838 
839 	/* Copy the link info sent by AF */
840 	pf->linfo = msg->link_info;
841 
842 	/* notify VFs about link event */
843 	for (i = 0; i < pci_num_vf(pf->pdev); i++) {
844 		struct otx2_vf_config *config = &pf->vf_configs[i];
845 		struct delayed_work *dwork = &config->link_event_work;
846 
847 		if (config->intf_down)
848 			continue;
849 
850 		schedule_delayed_work(dwork, msecs_to_jiffies(100));
851 	}
852 
853 	/* interface has not been fully configured yet */
854 	if (pf->flags & OTX2_FLAG_INTF_DOWN)
855 		return 0;
856 
857 	otx2_handle_link_event(pf);
858 	return 0;
859 }
860 
861 static int otx2_process_mbox_msg_up(struct otx2_nic *pf,
862 				    struct mbox_msghdr *req)
863 {
864 	/* Check if valid, if not reply with a invalid msg */
865 	if (req->sig != OTX2_MBOX_REQ_SIG) {
866 		otx2_reply_invalid_msg(&pf->mbox.mbox_up, 0, 0, req->id);
867 		return -ENODEV;
868 	}
869 
870 	switch (req->id) {
871 #define M(_name, _id, _fn_name, _req_type, _rsp_type)			\
872 	case _id: {							\
873 		struct _rsp_type *rsp;					\
874 		int err;						\
875 									\
876 		rsp = (struct _rsp_type *)otx2_mbox_alloc_msg(		\
877 			&pf->mbox.mbox_up, 0,				\
878 			sizeof(struct _rsp_type));			\
879 		if (!rsp)						\
880 			return -ENOMEM;					\
881 									\
882 		rsp->hdr.id = _id;					\
883 		rsp->hdr.sig = OTX2_MBOX_RSP_SIG;			\
884 		rsp->hdr.pcifunc = 0;					\
885 		rsp->hdr.rc = 0;					\
886 									\
887 		err = otx2_mbox_up_handler_ ## _fn_name(		\
888 			pf, (struct _req_type *)req, rsp);		\
889 		return err;						\
890 	}
891 MBOX_UP_CGX_MESSAGES
892 #undef M
893 		break;
894 	default:
895 		otx2_reply_invalid_msg(&pf->mbox.mbox_up, 0, 0, req->id);
896 		return -ENODEV;
897 	}
898 	return 0;
899 }
900 
901 static void otx2_pfaf_mbox_up_handler(struct work_struct *work)
902 {
903 	struct mbox *af_mbox = container_of(work, struct mbox, mbox_up_wrk);
904 	struct otx2_mbox *mbox = &af_mbox->mbox_up;
905 	struct otx2_mbox_dev *mdev = &mbox->dev[0];
906 	struct otx2_nic *pf = af_mbox->pfvf;
907 	int offset, id, devid = 0;
908 	struct mbox_hdr *rsp_hdr;
909 	struct mbox_msghdr *msg;
910 
911 	rsp_hdr = (struct mbox_hdr *)(mdev->mbase + mbox->rx_start);
912 
913 	offset = mbox->rx_start + ALIGN(sizeof(*rsp_hdr), MBOX_MSG_ALIGN);
914 
915 	for (id = 0; id < af_mbox->up_num_msgs; id++) {
916 		msg = (struct mbox_msghdr *)(mdev->mbase + offset);
917 
918 		devid = msg->pcifunc & RVU_PFVF_FUNC_MASK;
919 		/* Skip processing VF's messages */
920 		if (!devid)
921 			otx2_process_mbox_msg_up(pf, msg);
922 		offset = mbox->rx_start + msg->next_msgoff;
923 	}
924 	if (devid) {
925 		otx2_forward_vf_mbox_msgs(pf, &pf->mbox.mbox_up,
926 					  MBOX_DIR_PFVF_UP, devid - 1,
927 					  af_mbox->up_num_msgs);
928 		return;
929 	}
930 
931 	otx2_mbox_msg_send(mbox, 0);
932 }
933 
934 static irqreturn_t otx2_pfaf_mbox_intr_handler(int irq, void *pf_irq)
935 {
936 	struct otx2_nic *pf = (struct otx2_nic *)pf_irq;
937 	struct mbox *mbox;
938 
939 	/* Clear the IRQ */
940 	otx2_write64(pf, RVU_PF_INT, BIT_ULL(0));
941 
942 	mbox = &pf->mbox;
943 	otx2_queue_work(mbox, pf->mbox_wq, 0, 1, 1, TYPE_PFAF);
944 
945 	return IRQ_HANDLED;
946 }
947 
948 static void otx2_disable_mbox_intr(struct otx2_nic *pf)
949 {
950 	int vector = pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_AFPF_MBOX);
951 
952 	/* Disable AF => PF mailbox IRQ */
953 	otx2_write64(pf, RVU_PF_INT_ENA_W1C, BIT_ULL(0));
954 	free_irq(vector, pf);
955 }
956 
957 static int otx2_register_mbox_intr(struct otx2_nic *pf, bool probe_af)
958 {
959 	struct otx2_hw *hw = &pf->hw;
960 	struct msg_req *req;
961 	char *irq_name;
962 	int err;
963 
964 	/* Register mailbox interrupt handler */
965 	irq_name = &hw->irq_name[RVU_PF_INT_VEC_AFPF_MBOX * NAME_SIZE];
966 	snprintf(irq_name, NAME_SIZE, "RVUPFAF Mbox");
967 	err = request_irq(pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_AFPF_MBOX),
968 			  otx2_pfaf_mbox_intr_handler, 0, irq_name, pf);
969 	if (err) {
970 		dev_err(pf->dev,
971 			"RVUPF: IRQ registration failed for PFAF mbox irq\n");
972 		return err;
973 	}
974 
975 	/* Enable mailbox interrupt for msgs coming from AF.
976 	 * First clear to avoid spurious interrupts, if any.
977 	 */
978 	otx2_write64(pf, RVU_PF_INT, BIT_ULL(0));
979 	otx2_write64(pf, RVU_PF_INT_ENA_W1S, BIT_ULL(0));
980 
981 	if (!probe_af)
982 		return 0;
983 
984 	/* Check mailbox communication with AF */
985 	req = otx2_mbox_alloc_msg_ready(&pf->mbox);
986 	if (!req) {
987 		otx2_disable_mbox_intr(pf);
988 		return -ENOMEM;
989 	}
990 	err = otx2_sync_mbox_msg(&pf->mbox);
991 	if (err) {
992 		dev_warn(pf->dev,
993 			 "AF not responding to mailbox, deferring probe\n");
994 		otx2_disable_mbox_intr(pf);
995 		return -EPROBE_DEFER;
996 	}
997 
998 	return 0;
999 }
1000 
1001 static void otx2_pfaf_mbox_destroy(struct otx2_nic *pf)
1002 {
1003 	struct mbox *mbox = &pf->mbox;
1004 
1005 	if (pf->mbox_wq) {
1006 		destroy_workqueue(pf->mbox_wq);
1007 		pf->mbox_wq = NULL;
1008 	}
1009 
1010 	if (mbox->mbox.hwbase)
1011 		iounmap((void __iomem *)mbox->mbox.hwbase);
1012 
1013 	otx2_mbox_destroy(&mbox->mbox);
1014 	otx2_mbox_destroy(&mbox->mbox_up);
1015 }
1016 
1017 static int otx2_pfaf_mbox_init(struct otx2_nic *pf)
1018 {
1019 	struct mbox *mbox = &pf->mbox;
1020 	void __iomem *hwbase;
1021 	int err;
1022 
1023 	mbox->pfvf = pf;
1024 	pf->mbox_wq = alloc_workqueue("otx2_pfaf_mailbox",
1025 				      WQ_UNBOUND | WQ_HIGHPRI |
1026 				      WQ_MEM_RECLAIM, 1);
1027 	if (!pf->mbox_wq)
1028 		return -ENOMEM;
1029 
1030 	/* Mailbox is a reserved memory (in RAM) region shared between
1031 	 * admin function (i.e AF) and this PF, shouldn't be mapped as
1032 	 * device memory to allow unaligned accesses.
1033 	 */
1034 	hwbase = ioremap_wc(pci_resource_start(pf->pdev, PCI_MBOX_BAR_NUM),
1035 			    pci_resource_len(pf->pdev, PCI_MBOX_BAR_NUM));
1036 	if (!hwbase) {
1037 		dev_err(pf->dev, "Unable to map PFAF mailbox region\n");
1038 		err = -ENOMEM;
1039 		goto exit;
1040 	}
1041 
1042 	err = otx2_mbox_init(&mbox->mbox, hwbase, pf->pdev, pf->reg_base,
1043 			     MBOX_DIR_PFAF, 1);
1044 	if (err)
1045 		goto exit;
1046 
1047 	err = otx2_mbox_init(&mbox->mbox_up, hwbase, pf->pdev, pf->reg_base,
1048 			     MBOX_DIR_PFAF_UP, 1);
1049 	if (err)
1050 		goto exit;
1051 
1052 	err = otx2_mbox_bbuf_init(mbox, pf->pdev);
1053 	if (err)
1054 		goto exit;
1055 
1056 	INIT_WORK(&mbox->mbox_wrk, otx2_pfaf_mbox_handler);
1057 	INIT_WORK(&mbox->mbox_up_wrk, otx2_pfaf_mbox_up_handler);
1058 	mutex_init(&mbox->lock);
1059 
1060 	return 0;
1061 exit:
1062 	otx2_pfaf_mbox_destroy(pf);
1063 	return err;
1064 }
1065 
1066 static int otx2_cgx_config_linkevents(struct otx2_nic *pf, bool enable)
1067 {
1068 	struct msg_req *msg;
1069 	int err;
1070 
1071 	mutex_lock(&pf->mbox.lock);
1072 	if (enable)
1073 		msg = otx2_mbox_alloc_msg_cgx_start_linkevents(&pf->mbox);
1074 	else
1075 		msg = otx2_mbox_alloc_msg_cgx_stop_linkevents(&pf->mbox);
1076 
1077 	if (!msg) {
1078 		mutex_unlock(&pf->mbox.lock);
1079 		return -ENOMEM;
1080 	}
1081 
1082 	err = otx2_sync_mbox_msg(&pf->mbox);
1083 	mutex_unlock(&pf->mbox.lock);
1084 	return err;
1085 }
1086 
1087 static int otx2_cgx_config_loopback(struct otx2_nic *pf, bool enable)
1088 {
1089 	struct msg_req *msg;
1090 	int err;
1091 
1092 	mutex_lock(&pf->mbox.lock);
1093 	if (enable)
1094 		msg = otx2_mbox_alloc_msg_cgx_intlbk_enable(&pf->mbox);
1095 	else
1096 		msg = otx2_mbox_alloc_msg_cgx_intlbk_disable(&pf->mbox);
1097 
1098 	if (!msg) {
1099 		mutex_unlock(&pf->mbox.lock);
1100 		return -ENOMEM;
1101 	}
1102 
1103 	err = otx2_sync_mbox_msg(&pf->mbox);
1104 	mutex_unlock(&pf->mbox.lock);
1105 	return err;
1106 }
1107 
1108 int otx2_set_real_num_queues(struct net_device *netdev,
1109 			     int tx_queues, int rx_queues)
1110 {
1111 	int err;
1112 
1113 	err = netif_set_real_num_tx_queues(netdev, tx_queues);
1114 	if (err) {
1115 		netdev_err(netdev,
1116 			   "Failed to set no of Tx queues: %d\n", tx_queues);
1117 		return err;
1118 	}
1119 
1120 	err = netif_set_real_num_rx_queues(netdev, rx_queues);
1121 	if (err)
1122 		netdev_err(netdev,
1123 			   "Failed to set no of Rx queues: %d\n", rx_queues);
1124 	return err;
1125 }
1126 EXPORT_SYMBOL(otx2_set_real_num_queues);
1127 
1128 static irqreturn_t otx2_q_intr_handler(int irq, void *data)
1129 {
1130 	struct otx2_nic *pf = data;
1131 	u64 val, *ptr;
1132 	u64 qidx = 0;
1133 
1134 	/* CQ */
1135 	for (qidx = 0; qidx < pf->qset.cq_cnt; qidx++) {
1136 		ptr = otx2_get_regaddr(pf, NIX_LF_CQ_OP_INT);
1137 		val = otx2_atomic64_add((qidx << 44), ptr);
1138 
1139 		otx2_write64(pf, NIX_LF_CQ_OP_INT, (qidx << 44) |
1140 			     (val & NIX_CQERRINT_BITS));
1141 		if (!(val & (NIX_CQERRINT_BITS | BIT_ULL(42))))
1142 			continue;
1143 
1144 		if (val & BIT_ULL(42)) {
1145 			netdev_err(pf->netdev, "CQ%lld: error reading NIX_LF_CQ_OP_INT, NIX_LF_ERR_INT 0x%llx\n",
1146 				   qidx, otx2_read64(pf, NIX_LF_ERR_INT));
1147 		} else {
1148 			if (val & BIT_ULL(NIX_CQERRINT_DOOR_ERR))
1149 				netdev_err(pf->netdev, "CQ%lld: Doorbell error",
1150 					   qidx);
1151 			if (val & BIT_ULL(NIX_CQERRINT_CQE_FAULT))
1152 				netdev_err(pf->netdev, "CQ%lld: Memory fault on CQE write to LLC/DRAM",
1153 					   qidx);
1154 		}
1155 
1156 		schedule_work(&pf->reset_task);
1157 	}
1158 
1159 	/* SQ */
1160 	for (qidx = 0; qidx < pf->hw.tx_queues; qidx++) {
1161 		ptr = otx2_get_regaddr(pf, NIX_LF_SQ_OP_INT);
1162 		val = otx2_atomic64_add((qidx << 44), ptr);
1163 		otx2_write64(pf, NIX_LF_SQ_OP_INT, (qidx << 44) |
1164 			     (val & NIX_SQINT_BITS));
1165 
1166 		if (!(val & (NIX_SQINT_BITS | BIT_ULL(42))))
1167 			continue;
1168 
1169 		if (val & BIT_ULL(42)) {
1170 			netdev_err(pf->netdev, "SQ%lld: error reading NIX_LF_SQ_OP_INT, NIX_LF_ERR_INT 0x%llx\n",
1171 				   qidx, otx2_read64(pf, NIX_LF_ERR_INT));
1172 		} else {
1173 			if (val & BIT_ULL(NIX_SQINT_LMT_ERR)) {
1174 				netdev_err(pf->netdev, "SQ%lld: LMT store error NIX_LF_SQ_OP_ERR_DBG:0x%llx",
1175 					   qidx,
1176 					   otx2_read64(pf,
1177 						       NIX_LF_SQ_OP_ERR_DBG));
1178 				otx2_write64(pf, NIX_LF_SQ_OP_ERR_DBG,
1179 					     BIT_ULL(44));
1180 			}
1181 			if (val & BIT_ULL(NIX_SQINT_MNQ_ERR)) {
1182 				netdev_err(pf->netdev, "SQ%lld: Meta-descriptor enqueue error NIX_LF_MNQ_ERR_DGB:0x%llx\n",
1183 					   qidx,
1184 					   otx2_read64(pf, NIX_LF_MNQ_ERR_DBG));
1185 				otx2_write64(pf, NIX_LF_MNQ_ERR_DBG,
1186 					     BIT_ULL(44));
1187 			}
1188 			if (val & BIT_ULL(NIX_SQINT_SEND_ERR)) {
1189 				netdev_err(pf->netdev, "SQ%lld: Send error, NIX_LF_SEND_ERR_DBG 0x%llx",
1190 					   qidx,
1191 					   otx2_read64(pf,
1192 						       NIX_LF_SEND_ERR_DBG));
1193 				otx2_write64(pf, NIX_LF_SEND_ERR_DBG,
1194 					     BIT_ULL(44));
1195 			}
1196 			if (val & BIT_ULL(NIX_SQINT_SQB_ALLOC_FAIL))
1197 				netdev_err(pf->netdev, "SQ%lld: SQB allocation failed",
1198 					   qidx);
1199 		}
1200 
1201 		schedule_work(&pf->reset_task);
1202 	}
1203 
1204 	return IRQ_HANDLED;
1205 }
1206 
1207 static irqreturn_t otx2_cq_intr_handler(int irq, void *cq_irq)
1208 {
1209 	struct otx2_cq_poll *cq_poll = (struct otx2_cq_poll *)cq_irq;
1210 	struct otx2_nic *pf = (struct otx2_nic *)cq_poll->dev;
1211 	int qidx = cq_poll->cint_idx;
1212 
1213 	/* Disable interrupts.
1214 	 *
1215 	 * Completion interrupts behave in a level-triggered interrupt
1216 	 * fashion, and hence have to be cleared only after it is serviced.
1217 	 */
1218 	otx2_write64(pf, NIX_LF_CINTX_ENA_W1C(qidx), BIT_ULL(0));
1219 
1220 	/* Schedule NAPI */
1221 	napi_schedule_irqoff(&cq_poll->napi);
1222 
1223 	return IRQ_HANDLED;
1224 }
1225 
1226 static void otx2_disable_napi(struct otx2_nic *pf)
1227 {
1228 	struct otx2_qset *qset = &pf->qset;
1229 	struct otx2_cq_poll *cq_poll;
1230 	int qidx;
1231 
1232 	for (qidx = 0; qidx < pf->hw.cint_cnt; qidx++) {
1233 		cq_poll = &qset->napi[qidx];
1234 		napi_disable(&cq_poll->napi);
1235 		netif_napi_del(&cq_poll->napi);
1236 	}
1237 }
1238 
1239 static void otx2_free_cq_res(struct otx2_nic *pf)
1240 {
1241 	struct otx2_qset *qset = &pf->qset;
1242 	struct otx2_cq_queue *cq;
1243 	int qidx;
1244 
1245 	/* Disable CQs */
1246 	otx2_ctx_disable(&pf->mbox, NIX_AQ_CTYPE_CQ, false);
1247 	for (qidx = 0; qidx < qset->cq_cnt; qidx++) {
1248 		cq = &qset->cq[qidx];
1249 		qmem_free(pf->dev, cq->cqe);
1250 	}
1251 }
1252 
1253 static void otx2_free_sq_res(struct otx2_nic *pf)
1254 {
1255 	struct otx2_qset *qset = &pf->qset;
1256 	struct otx2_snd_queue *sq;
1257 	int qidx;
1258 
1259 	/* Disable SQs */
1260 	otx2_ctx_disable(&pf->mbox, NIX_AQ_CTYPE_SQ, false);
1261 	/* Free SQB pointers */
1262 	otx2_sq_free_sqbs(pf);
1263 	for (qidx = 0; qidx < pf->hw.tx_queues; qidx++) {
1264 		sq = &qset->sq[qidx];
1265 		qmem_free(pf->dev, sq->sqe);
1266 		qmem_free(pf->dev, sq->tso_hdrs);
1267 		kfree(sq->sg);
1268 		kfree(sq->sqb_ptrs);
1269 	}
1270 }
1271 
1272 static int otx2_init_hw_resources(struct otx2_nic *pf)
1273 {
1274 	struct mbox *mbox = &pf->mbox;
1275 	struct otx2_hw *hw = &pf->hw;
1276 	struct msg_req *req;
1277 	int err = 0, lvl;
1278 
1279 	/* Set required NPA LF's pool counts
1280 	 * Auras and Pools are used in a 1:1 mapping,
1281 	 * so, aura count = pool count.
1282 	 */
1283 	hw->rqpool_cnt = hw->rx_queues;
1284 	hw->sqpool_cnt = hw->tx_queues;
1285 	hw->pool_cnt = hw->rqpool_cnt + hw->sqpool_cnt;
1286 
1287 	/* Get the size of receive buffers to allocate */
1288 	pf->rbsize = RCV_FRAG_LEN(OTX2_HW_TIMESTAMP_LEN + pf->netdev->mtu +
1289 				  OTX2_ETH_HLEN);
1290 
1291 	mutex_lock(&mbox->lock);
1292 	/* NPA init */
1293 	err = otx2_config_npa(pf);
1294 	if (err)
1295 		goto exit;
1296 
1297 	/* NIX init */
1298 	err = otx2_config_nix(pf);
1299 	if (err)
1300 		goto err_free_npa_lf;
1301 
1302 	/* Enable backpressure */
1303 	otx2_nix_config_bp(pf, true);
1304 
1305 	/* Init Auras and pools used by NIX RQ, for free buffer ptrs */
1306 	err = otx2_rq_aura_pool_init(pf);
1307 	if (err) {
1308 		mutex_unlock(&mbox->lock);
1309 		goto err_free_nix_lf;
1310 	}
1311 	/* Init Auras and pools used by NIX SQ, for queueing SQEs */
1312 	err = otx2_sq_aura_pool_init(pf);
1313 	if (err) {
1314 		mutex_unlock(&mbox->lock);
1315 		goto err_free_rq_ptrs;
1316 	}
1317 
1318 	err = otx2_txsch_alloc(pf);
1319 	if (err) {
1320 		mutex_unlock(&mbox->lock);
1321 		goto err_free_sq_ptrs;
1322 	}
1323 
1324 	err = otx2_config_nix_queues(pf);
1325 	if (err) {
1326 		mutex_unlock(&mbox->lock);
1327 		goto err_free_txsch;
1328 	}
1329 	for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
1330 		err = otx2_txschq_config(pf, lvl);
1331 		if (err) {
1332 			mutex_unlock(&mbox->lock);
1333 			goto err_free_nix_queues;
1334 		}
1335 	}
1336 	mutex_unlock(&mbox->lock);
1337 	return err;
1338 
1339 err_free_nix_queues:
1340 	otx2_free_sq_res(pf);
1341 	otx2_free_cq_res(pf);
1342 	otx2_ctx_disable(mbox, NIX_AQ_CTYPE_RQ, false);
1343 err_free_txsch:
1344 	if (otx2_txschq_stop(pf))
1345 		dev_err(pf->dev, "%s failed to stop TX schedulers\n", __func__);
1346 err_free_sq_ptrs:
1347 	otx2_sq_free_sqbs(pf);
1348 err_free_rq_ptrs:
1349 	otx2_free_aura_ptr(pf, AURA_NIX_RQ);
1350 	otx2_ctx_disable(mbox, NPA_AQ_CTYPE_POOL, true);
1351 	otx2_ctx_disable(mbox, NPA_AQ_CTYPE_AURA, true);
1352 	otx2_aura_pool_free(pf);
1353 err_free_nix_lf:
1354 	mutex_lock(&mbox->lock);
1355 	req = otx2_mbox_alloc_msg_nix_lf_free(mbox);
1356 	if (req) {
1357 		if (otx2_sync_mbox_msg(mbox))
1358 			dev_err(pf->dev, "%s failed to free nixlf\n", __func__);
1359 	}
1360 err_free_npa_lf:
1361 	/* Reset NPA LF */
1362 	req = otx2_mbox_alloc_msg_npa_lf_free(mbox);
1363 	if (req) {
1364 		if (otx2_sync_mbox_msg(mbox))
1365 			dev_err(pf->dev, "%s failed to free npalf\n", __func__);
1366 	}
1367 exit:
1368 	mutex_unlock(&mbox->lock);
1369 	return err;
1370 }
1371 
1372 static void otx2_free_hw_resources(struct otx2_nic *pf)
1373 {
1374 	struct otx2_qset *qset = &pf->qset;
1375 	struct mbox *mbox = &pf->mbox;
1376 	struct otx2_cq_queue *cq;
1377 	struct msg_req *req;
1378 	int qidx, err;
1379 
1380 	/* Ensure all SQE are processed */
1381 	otx2_sqb_flush(pf);
1382 
1383 	/* Stop transmission */
1384 	err = otx2_txschq_stop(pf);
1385 	if (err)
1386 		dev_err(pf->dev, "RVUPF: Failed to stop/free TX schedulers\n");
1387 
1388 	mutex_lock(&mbox->lock);
1389 	/* Disable backpressure */
1390 	if (!(pf->pcifunc & RVU_PFVF_FUNC_MASK))
1391 		otx2_nix_config_bp(pf, false);
1392 	mutex_unlock(&mbox->lock);
1393 
1394 	/* Disable RQs */
1395 	otx2_ctx_disable(mbox, NIX_AQ_CTYPE_RQ, false);
1396 
1397 	/*Dequeue all CQEs */
1398 	for (qidx = 0; qidx < qset->cq_cnt; qidx++) {
1399 		cq = &qset->cq[qidx];
1400 		if (cq->cq_type == CQ_RX)
1401 			otx2_cleanup_rx_cqes(pf, cq);
1402 		else
1403 			otx2_cleanup_tx_cqes(pf, cq);
1404 	}
1405 
1406 	otx2_free_sq_res(pf);
1407 
1408 	/* Free RQ buffer pointers*/
1409 	otx2_free_aura_ptr(pf, AURA_NIX_RQ);
1410 
1411 	otx2_free_cq_res(pf);
1412 
1413 	mutex_lock(&mbox->lock);
1414 	/* Reset NIX LF */
1415 	req = otx2_mbox_alloc_msg_nix_lf_free(mbox);
1416 	if (req) {
1417 		if (otx2_sync_mbox_msg(mbox))
1418 			dev_err(pf->dev, "%s failed to free nixlf\n", __func__);
1419 	}
1420 	mutex_unlock(&mbox->lock);
1421 
1422 	/* Disable NPA Pool and Aura hw context */
1423 	otx2_ctx_disable(mbox, NPA_AQ_CTYPE_POOL, true);
1424 	otx2_ctx_disable(mbox, NPA_AQ_CTYPE_AURA, true);
1425 	otx2_aura_pool_free(pf);
1426 
1427 	mutex_lock(&mbox->lock);
1428 	/* Reset NPA LF */
1429 	req = otx2_mbox_alloc_msg_npa_lf_free(mbox);
1430 	if (req) {
1431 		if (otx2_sync_mbox_msg(mbox))
1432 			dev_err(pf->dev, "%s failed to free npalf\n", __func__);
1433 	}
1434 	mutex_unlock(&mbox->lock);
1435 }
1436 
1437 int otx2_open(struct net_device *netdev)
1438 {
1439 	struct otx2_nic *pf = netdev_priv(netdev);
1440 	struct otx2_cq_poll *cq_poll = NULL;
1441 	struct otx2_qset *qset = &pf->qset;
1442 	int err = 0, qidx, vec;
1443 	char *irq_name;
1444 
1445 	netif_carrier_off(netdev);
1446 
1447 	pf->qset.cq_cnt = pf->hw.rx_queues + pf->hw.tx_queues;
1448 	/* RQ and SQs are mapped to different CQs,
1449 	 * so find out max CQ IRQs (i.e CINTs) needed.
1450 	 */
1451 	pf->hw.cint_cnt = max(pf->hw.rx_queues, pf->hw.tx_queues);
1452 	qset->napi = kcalloc(pf->hw.cint_cnt, sizeof(*cq_poll), GFP_KERNEL);
1453 	if (!qset->napi)
1454 		return -ENOMEM;
1455 
1456 	/* CQ size of RQ */
1457 	qset->rqe_cnt = qset->rqe_cnt ? qset->rqe_cnt : Q_COUNT(Q_SIZE_256);
1458 	/* CQ size of SQ */
1459 	qset->sqe_cnt = qset->sqe_cnt ? qset->sqe_cnt : Q_COUNT(Q_SIZE_4K);
1460 
1461 	err = -ENOMEM;
1462 	qset->cq = kcalloc(pf->qset.cq_cnt,
1463 			   sizeof(struct otx2_cq_queue), GFP_KERNEL);
1464 	if (!qset->cq)
1465 		goto err_free_mem;
1466 
1467 	qset->sq = kcalloc(pf->hw.tx_queues,
1468 			   sizeof(struct otx2_snd_queue), GFP_KERNEL);
1469 	if (!qset->sq)
1470 		goto err_free_mem;
1471 
1472 	qset->rq = kcalloc(pf->hw.rx_queues,
1473 			   sizeof(struct otx2_rcv_queue), GFP_KERNEL);
1474 	if (!qset->rq)
1475 		goto err_free_mem;
1476 
1477 	err = otx2_init_hw_resources(pf);
1478 	if (err)
1479 		goto err_free_mem;
1480 
1481 	/* Register NAPI handler */
1482 	for (qidx = 0; qidx < pf->hw.cint_cnt; qidx++) {
1483 		cq_poll = &qset->napi[qidx];
1484 		cq_poll->cint_idx = qidx;
1485 		/* RQ0 & SQ0 are mapped to CINT0 and so on..
1486 		 * 'cq_ids[0]' points to RQ's CQ and
1487 		 * 'cq_ids[1]' points to SQ's CQ and
1488 		 */
1489 		cq_poll->cq_ids[CQ_RX] =
1490 			(qidx <  pf->hw.rx_queues) ? qidx : CINT_INVALID_CQ;
1491 		cq_poll->cq_ids[CQ_TX] = (qidx < pf->hw.tx_queues) ?
1492 				      qidx + pf->hw.rx_queues : CINT_INVALID_CQ;
1493 		cq_poll->dev = (void *)pf;
1494 		netif_napi_add(netdev, &cq_poll->napi,
1495 			       otx2_napi_handler, NAPI_POLL_WEIGHT);
1496 		napi_enable(&cq_poll->napi);
1497 	}
1498 
1499 	/* Set maximum frame size allowed in HW */
1500 	err = otx2_hw_set_mtu(pf, netdev->mtu);
1501 	if (err)
1502 		goto err_disable_napi;
1503 
1504 	/* Setup segmentation algorithms, if failed, clear offload capability */
1505 	otx2_setup_segmentation(pf);
1506 
1507 	/* Initialize RSS */
1508 	err = otx2_rss_init(pf);
1509 	if (err)
1510 		goto err_disable_napi;
1511 
1512 	/* Register Queue IRQ handlers */
1513 	vec = pf->hw.nix_msixoff + NIX_LF_QINT_VEC_START;
1514 	irq_name = &pf->hw.irq_name[vec * NAME_SIZE];
1515 
1516 	snprintf(irq_name, NAME_SIZE, "%s-qerr", pf->netdev->name);
1517 
1518 	err = request_irq(pci_irq_vector(pf->pdev, vec),
1519 			  otx2_q_intr_handler, 0, irq_name, pf);
1520 	if (err) {
1521 		dev_err(pf->dev,
1522 			"RVUPF%d: IRQ registration failed for QERR\n",
1523 			rvu_get_pf(pf->pcifunc));
1524 		goto err_disable_napi;
1525 	}
1526 
1527 	/* Enable QINT IRQ */
1528 	otx2_write64(pf, NIX_LF_QINTX_ENA_W1S(0), BIT_ULL(0));
1529 
1530 	/* Register CQ IRQ handlers */
1531 	vec = pf->hw.nix_msixoff + NIX_LF_CINT_VEC_START;
1532 	for (qidx = 0; qidx < pf->hw.cint_cnt; qidx++) {
1533 		irq_name = &pf->hw.irq_name[vec * NAME_SIZE];
1534 
1535 		snprintf(irq_name, NAME_SIZE, "%s-rxtx-%d", pf->netdev->name,
1536 			 qidx);
1537 
1538 		err = request_irq(pci_irq_vector(pf->pdev, vec),
1539 				  otx2_cq_intr_handler, 0, irq_name,
1540 				  &qset->napi[qidx]);
1541 		if (err) {
1542 			dev_err(pf->dev,
1543 				"RVUPF%d: IRQ registration failed for CQ%d\n",
1544 				rvu_get_pf(pf->pcifunc), qidx);
1545 			goto err_free_cints;
1546 		}
1547 		vec++;
1548 
1549 		otx2_config_irq_coalescing(pf, qidx);
1550 
1551 		/* Enable CQ IRQ */
1552 		otx2_write64(pf, NIX_LF_CINTX_INT(qidx), BIT_ULL(0));
1553 		otx2_write64(pf, NIX_LF_CINTX_ENA_W1S(qidx), BIT_ULL(0));
1554 	}
1555 
1556 	otx2_set_cints_affinity(pf);
1557 
1558 	/* When reinitializing enable time stamping if it is enabled before */
1559 	if (pf->flags & OTX2_FLAG_TX_TSTAMP_ENABLED) {
1560 		pf->flags &= ~OTX2_FLAG_TX_TSTAMP_ENABLED;
1561 		otx2_config_hw_tx_tstamp(pf, true);
1562 	}
1563 	if (pf->flags & OTX2_FLAG_RX_TSTAMP_ENABLED) {
1564 		pf->flags &= ~OTX2_FLAG_RX_TSTAMP_ENABLED;
1565 		otx2_config_hw_rx_tstamp(pf, true);
1566 	}
1567 
1568 	pf->flags &= ~OTX2_FLAG_INTF_DOWN;
1569 	/* 'intf_down' may be checked on any cpu */
1570 	smp_wmb();
1571 
1572 	/* we have already received link status notification */
1573 	if (pf->linfo.link_up && !(pf->pcifunc & RVU_PFVF_FUNC_MASK))
1574 		otx2_handle_link_event(pf);
1575 
1576 	/* Restore pause frame settings */
1577 	otx2_config_pause_frm(pf);
1578 
1579 	err = otx2_rxtx_enable(pf, true);
1580 	if (err)
1581 		goto err_free_cints;
1582 
1583 	return 0;
1584 
1585 err_free_cints:
1586 	otx2_free_cints(pf, qidx);
1587 	vec = pci_irq_vector(pf->pdev,
1588 			     pf->hw.nix_msixoff + NIX_LF_QINT_VEC_START);
1589 	otx2_write64(pf, NIX_LF_QINTX_ENA_W1C(0), BIT_ULL(0));
1590 	synchronize_irq(vec);
1591 	free_irq(vec, pf);
1592 err_disable_napi:
1593 	otx2_disable_napi(pf);
1594 	otx2_free_hw_resources(pf);
1595 err_free_mem:
1596 	kfree(qset->sq);
1597 	kfree(qset->cq);
1598 	kfree(qset->rq);
1599 	kfree(qset->napi);
1600 	return err;
1601 }
1602 EXPORT_SYMBOL(otx2_open);
1603 
1604 int otx2_stop(struct net_device *netdev)
1605 {
1606 	struct otx2_nic *pf = netdev_priv(netdev);
1607 	struct otx2_cq_poll *cq_poll = NULL;
1608 	struct otx2_qset *qset = &pf->qset;
1609 	int qidx, vec, wrk;
1610 
1611 	netif_carrier_off(netdev);
1612 	netif_tx_stop_all_queues(netdev);
1613 
1614 	pf->flags |= OTX2_FLAG_INTF_DOWN;
1615 	/* 'intf_down' may be checked on any cpu */
1616 	smp_wmb();
1617 
1618 	/* First stop packet Rx/Tx */
1619 	otx2_rxtx_enable(pf, false);
1620 
1621 	/* Cleanup Queue IRQ */
1622 	vec = pci_irq_vector(pf->pdev,
1623 			     pf->hw.nix_msixoff + NIX_LF_QINT_VEC_START);
1624 	otx2_write64(pf, NIX_LF_QINTX_ENA_W1C(0), BIT_ULL(0));
1625 	synchronize_irq(vec);
1626 	free_irq(vec, pf);
1627 
1628 	/* Cleanup CQ NAPI and IRQ */
1629 	vec = pf->hw.nix_msixoff + NIX_LF_CINT_VEC_START;
1630 	for (qidx = 0; qidx < pf->hw.cint_cnt; qidx++) {
1631 		/* Disable interrupt */
1632 		otx2_write64(pf, NIX_LF_CINTX_ENA_W1C(qidx), BIT_ULL(0));
1633 
1634 		synchronize_irq(pci_irq_vector(pf->pdev, vec));
1635 
1636 		cq_poll = &qset->napi[qidx];
1637 		napi_synchronize(&cq_poll->napi);
1638 		vec++;
1639 	}
1640 
1641 	netif_tx_disable(netdev);
1642 
1643 	otx2_free_hw_resources(pf);
1644 	otx2_free_cints(pf, pf->hw.cint_cnt);
1645 	otx2_disable_napi(pf);
1646 
1647 	for (qidx = 0; qidx < netdev->num_tx_queues; qidx++)
1648 		netdev_tx_reset_queue(netdev_get_tx_queue(netdev, qidx));
1649 
1650 	for (wrk = 0; wrk < pf->qset.cq_cnt; wrk++)
1651 		cancel_delayed_work_sync(&pf->refill_wrk[wrk].pool_refill_work);
1652 	devm_kfree(pf->dev, pf->refill_wrk);
1653 
1654 	kfree(qset->sq);
1655 	kfree(qset->cq);
1656 	kfree(qset->rq);
1657 	kfree(qset->napi);
1658 	/* Do not clear RQ/SQ ringsize settings */
1659 	memset((void *)qset + offsetof(struct otx2_qset, sqe_cnt), 0,
1660 	       sizeof(*qset) - offsetof(struct otx2_qset, sqe_cnt));
1661 	return 0;
1662 }
1663 EXPORT_SYMBOL(otx2_stop);
1664 
1665 static netdev_tx_t otx2_xmit(struct sk_buff *skb, struct net_device *netdev)
1666 {
1667 	struct otx2_nic *pf = netdev_priv(netdev);
1668 	int qidx = skb_get_queue_mapping(skb);
1669 	struct otx2_snd_queue *sq;
1670 	struct netdev_queue *txq;
1671 
1672 	/* Check for minimum and maximum packet length */
1673 	if (skb->len <= ETH_HLEN ||
1674 	    (!skb_shinfo(skb)->gso_size && skb->len > pf->max_frs)) {
1675 		dev_kfree_skb(skb);
1676 		return NETDEV_TX_OK;
1677 	}
1678 
1679 	sq = &pf->qset.sq[qidx];
1680 	txq = netdev_get_tx_queue(netdev, qidx);
1681 
1682 	if (!otx2_sq_append_skb(netdev, sq, skb, qidx)) {
1683 		netif_tx_stop_queue(txq);
1684 
1685 		/* Check again, incase SQBs got freed up */
1686 		smp_mb();
1687 		if (((sq->num_sqbs - *sq->aura_fc_addr) * sq->sqe_per_sqb)
1688 							> sq->sqe_thresh)
1689 			netif_tx_wake_queue(txq);
1690 
1691 		return NETDEV_TX_BUSY;
1692 	}
1693 
1694 	return NETDEV_TX_OK;
1695 }
1696 
1697 static void otx2_set_rx_mode(struct net_device *netdev)
1698 {
1699 	struct otx2_nic *pf = netdev_priv(netdev);
1700 
1701 	queue_work(pf->otx2_wq, &pf->rx_mode_work);
1702 }
1703 
1704 static void otx2_do_set_rx_mode(struct work_struct *work)
1705 {
1706 	struct otx2_nic *pf = container_of(work, struct otx2_nic, rx_mode_work);
1707 	struct net_device *netdev = pf->netdev;
1708 	struct nix_rx_mode *req;
1709 
1710 	if (!(netdev->flags & IFF_UP))
1711 		return;
1712 
1713 	mutex_lock(&pf->mbox.lock);
1714 	req = otx2_mbox_alloc_msg_nix_set_rx_mode(&pf->mbox);
1715 	if (!req) {
1716 		mutex_unlock(&pf->mbox.lock);
1717 		return;
1718 	}
1719 
1720 	req->mode = NIX_RX_MODE_UCAST;
1721 
1722 	/* We don't support MAC address filtering yet */
1723 	if (netdev->flags & IFF_PROMISC)
1724 		req->mode |= NIX_RX_MODE_PROMISC;
1725 	else if (netdev->flags & (IFF_ALLMULTI | IFF_MULTICAST))
1726 		req->mode |= NIX_RX_MODE_ALLMULTI;
1727 
1728 	otx2_sync_mbox_msg(&pf->mbox);
1729 	mutex_unlock(&pf->mbox.lock);
1730 }
1731 
1732 static int otx2_set_features(struct net_device *netdev,
1733 			     netdev_features_t features)
1734 {
1735 	netdev_features_t changed = features ^ netdev->features;
1736 	struct otx2_nic *pf = netdev_priv(netdev);
1737 
1738 	if ((changed & NETIF_F_LOOPBACK) && netif_running(netdev))
1739 		return otx2_cgx_config_loopback(pf,
1740 						features & NETIF_F_LOOPBACK);
1741 	return 0;
1742 }
1743 
1744 static void otx2_reset_task(struct work_struct *work)
1745 {
1746 	struct otx2_nic *pf = container_of(work, struct otx2_nic, reset_task);
1747 
1748 	if (!netif_running(pf->netdev))
1749 		return;
1750 
1751 	rtnl_lock();
1752 	otx2_stop(pf->netdev);
1753 	pf->reset_count++;
1754 	otx2_open(pf->netdev);
1755 	netif_trans_update(pf->netdev);
1756 	rtnl_unlock();
1757 }
1758 
1759 static int otx2_config_hw_rx_tstamp(struct otx2_nic *pfvf, bool enable)
1760 {
1761 	struct msg_req *req;
1762 	int err;
1763 
1764 	if (pfvf->flags & OTX2_FLAG_RX_TSTAMP_ENABLED && enable)
1765 		return 0;
1766 
1767 	mutex_lock(&pfvf->mbox.lock);
1768 	if (enable)
1769 		req = otx2_mbox_alloc_msg_cgx_ptp_rx_enable(&pfvf->mbox);
1770 	else
1771 		req = otx2_mbox_alloc_msg_cgx_ptp_rx_disable(&pfvf->mbox);
1772 	if (!req) {
1773 		mutex_unlock(&pfvf->mbox.lock);
1774 		return -ENOMEM;
1775 	}
1776 
1777 	err = otx2_sync_mbox_msg(&pfvf->mbox);
1778 	if (err) {
1779 		mutex_unlock(&pfvf->mbox.lock);
1780 		return err;
1781 	}
1782 
1783 	mutex_unlock(&pfvf->mbox.lock);
1784 	if (enable)
1785 		pfvf->flags |= OTX2_FLAG_RX_TSTAMP_ENABLED;
1786 	else
1787 		pfvf->flags &= ~OTX2_FLAG_RX_TSTAMP_ENABLED;
1788 	return 0;
1789 }
1790 
1791 static int otx2_config_hw_tx_tstamp(struct otx2_nic *pfvf, bool enable)
1792 {
1793 	struct msg_req *req;
1794 	int err;
1795 
1796 	if (pfvf->flags & OTX2_FLAG_TX_TSTAMP_ENABLED && enable)
1797 		return 0;
1798 
1799 	mutex_lock(&pfvf->mbox.lock);
1800 	if (enable)
1801 		req = otx2_mbox_alloc_msg_nix_lf_ptp_tx_enable(&pfvf->mbox);
1802 	else
1803 		req = otx2_mbox_alloc_msg_nix_lf_ptp_tx_disable(&pfvf->mbox);
1804 	if (!req) {
1805 		mutex_unlock(&pfvf->mbox.lock);
1806 		return -ENOMEM;
1807 	}
1808 
1809 	err = otx2_sync_mbox_msg(&pfvf->mbox);
1810 	if (err) {
1811 		mutex_unlock(&pfvf->mbox.lock);
1812 		return err;
1813 	}
1814 
1815 	mutex_unlock(&pfvf->mbox.lock);
1816 	if (enable)
1817 		pfvf->flags |= OTX2_FLAG_TX_TSTAMP_ENABLED;
1818 	else
1819 		pfvf->flags &= ~OTX2_FLAG_TX_TSTAMP_ENABLED;
1820 	return 0;
1821 }
1822 
1823 static int otx2_config_hwtstamp(struct net_device *netdev, struct ifreq *ifr)
1824 {
1825 	struct otx2_nic *pfvf = netdev_priv(netdev);
1826 	struct hwtstamp_config config;
1827 
1828 	if (!pfvf->ptp)
1829 		return -ENODEV;
1830 
1831 	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
1832 		return -EFAULT;
1833 
1834 	/* reserved for future extensions */
1835 	if (config.flags)
1836 		return -EINVAL;
1837 
1838 	switch (config.tx_type) {
1839 	case HWTSTAMP_TX_OFF:
1840 		otx2_config_hw_tx_tstamp(pfvf, false);
1841 		break;
1842 	case HWTSTAMP_TX_ON:
1843 		otx2_config_hw_tx_tstamp(pfvf, true);
1844 		break;
1845 	default:
1846 		return -ERANGE;
1847 	}
1848 
1849 	switch (config.rx_filter) {
1850 	case HWTSTAMP_FILTER_NONE:
1851 		otx2_config_hw_rx_tstamp(pfvf, false);
1852 		break;
1853 	case HWTSTAMP_FILTER_ALL:
1854 	case HWTSTAMP_FILTER_SOME:
1855 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1856 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1857 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1858 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1859 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1860 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1861 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1862 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1863 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1864 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
1865 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
1866 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1867 		otx2_config_hw_rx_tstamp(pfvf, true);
1868 		config.rx_filter = HWTSTAMP_FILTER_ALL;
1869 		break;
1870 	default:
1871 		return -ERANGE;
1872 	}
1873 
1874 	memcpy(&pfvf->tstamp, &config, sizeof(config));
1875 
1876 	return copy_to_user(ifr->ifr_data, &config,
1877 			    sizeof(config)) ? -EFAULT : 0;
1878 }
1879 
1880 static int otx2_ioctl(struct net_device *netdev, struct ifreq *req, int cmd)
1881 {
1882 	struct otx2_nic *pfvf = netdev_priv(netdev);
1883 	struct hwtstamp_config *cfg = &pfvf->tstamp;
1884 
1885 	switch (cmd) {
1886 	case SIOCSHWTSTAMP:
1887 		return otx2_config_hwtstamp(netdev, req);
1888 	case SIOCGHWTSTAMP:
1889 		return copy_to_user(req->ifr_data, cfg,
1890 				    sizeof(*cfg)) ? -EFAULT : 0;
1891 	default:
1892 		return -EOPNOTSUPP;
1893 	}
1894 }
1895 
1896 static const struct net_device_ops otx2_netdev_ops = {
1897 	.ndo_open		= otx2_open,
1898 	.ndo_stop		= otx2_stop,
1899 	.ndo_start_xmit		= otx2_xmit,
1900 	.ndo_set_mac_address    = otx2_set_mac_address,
1901 	.ndo_change_mtu		= otx2_change_mtu,
1902 	.ndo_set_rx_mode	= otx2_set_rx_mode,
1903 	.ndo_set_features	= otx2_set_features,
1904 	.ndo_tx_timeout		= otx2_tx_timeout,
1905 	.ndo_get_stats64	= otx2_get_stats64,
1906 	.ndo_do_ioctl		= otx2_ioctl,
1907 };
1908 
1909 static int otx2_wq_init(struct otx2_nic *pf)
1910 {
1911 	pf->otx2_wq = create_singlethread_workqueue("otx2_wq");
1912 	if (!pf->otx2_wq)
1913 		return -ENOMEM;
1914 
1915 	INIT_WORK(&pf->rx_mode_work, otx2_do_set_rx_mode);
1916 	INIT_WORK(&pf->reset_task, otx2_reset_task);
1917 	return 0;
1918 }
1919 
1920 static int otx2_check_pf_usable(struct otx2_nic *nic)
1921 {
1922 	u64 rev;
1923 
1924 	rev = otx2_read64(nic, RVU_PF_BLOCK_ADDRX_DISC(BLKADDR_RVUM));
1925 	rev = (rev >> 12) & 0xFF;
1926 	/* Check if AF has setup revision for RVUM block,
1927 	 * otherwise this driver probe should be deferred
1928 	 * until AF driver comes up.
1929 	 */
1930 	if (!rev) {
1931 		dev_warn(nic->dev,
1932 			 "AF is not initialized, deferring probe\n");
1933 		return -EPROBE_DEFER;
1934 	}
1935 	return 0;
1936 }
1937 
1938 static int otx2_realloc_msix_vectors(struct otx2_nic *pf)
1939 {
1940 	struct otx2_hw *hw = &pf->hw;
1941 	int num_vec, err;
1942 
1943 	/* NPA interrupts are inot registered, so alloc only
1944 	 * upto NIX vector offset.
1945 	 */
1946 	num_vec = hw->nix_msixoff;
1947 	num_vec += NIX_LF_CINT_VEC_START + hw->max_queues;
1948 
1949 	otx2_disable_mbox_intr(pf);
1950 	pci_free_irq_vectors(hw->pdev);
1951 	err = pci_alloc_irq_vectors(hw->pdev, num_vec, num_vec, PCI_IRQ_MSIX);
1952 	if (err < 0) {
1953 		dev_err(pf->dev, "%s: Failed to realloc %d IRQ vectors\n",
1954 			__func__, num_vec);
1955 		return err;
1956 	}
1957 
1958 	return otx2_register_mbox_intr(pf, false);
1959 }
1960 
1961 static int otx2_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1962 {
1963 	struct device *dev = &pdev->dev;
1964 	struct net_device *netdev;
1965 	struct otx2_nic *pf;
1966 	struct otx2_hw *hw;
1967 	int err, qcount;
1968 	int num_vec;
1969 
1970 	err = pcim_enable_device(pdev);
1971 	if (err) {
1972 		dev_err(dev, "Failed to enable PCI device\n");
1973 		return err;
1974 	}
1975 
1976 	err = pci_request_regions(pdev, DRV_NAME);
1977 	if (err) {
1978 		dev_err(dev, "PCI request regions failed 0x%x\n", err);
1979 		return err;
1980 	}
1981 
1982 	err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48));
1983 	if (err) {
1984 		dev_err(dev, "DMA mask config failed, abort\n");
1985 		goto err_release_regions;
1986 	}
1987 
1988 	pci_set_master(pdev);
1989 
1990 	/* Set number of queues */
1991 	qcount = min_t(int, num_online_cpus(), OTX2_MAX_CQ_CNT);
1992 
1993 	netdev = alloc_etherdev_mqs(sizeof(*pf), qcount, qcount);
1994 	if (!netdev) {
1995 		err = -ENOMEM;
1996 		goto err_release_regions;
1997 	}
1998 
1999 	pci_set_drvdata(pdev, netdev);
2000 	SET_NETDEV_DEV(netdev, &pdev->dev);
2001 	pf = netdev_priv(netdev);
2002 	pf->netdev = netdev;
2003 	pf->pdev = pdev;
2004 	pf->dev = dev;
2005 	pf->total_vfs = pci_sriov_get_totalvfs(pdev);
2006 	pf->flags |= OTX2_FLAG_INTF_DOWN;
2007 
2008 	hw = &pf->hw;
2009 	hw->pdev = pdev;
2010 	hw->rx_queues = qcount;
2011 	hw->tx_queues = qcount;
2012 	hw->max_queues = qcount;
2013 
2014 	num_vec = pci_msix_vec_count(pdev);
2015 	hw->irq_name = devm_kmalloc_array(&hw->pdev->dev, num_vec, NAME_SIZE,
2016 					  GFP_KERNEL);
2017 	if (!hw->irq_name) {
2018 		err = -ENOMEM;
2019 		goto err_free_netdev;
2020 	}
2021 
2022 	hw->affinity_mask = devm_kcalloc(&hw->pdev->dev, num_vec,
2023 					 sizeof(cpumask_var_t), GFP_KERNEL);
2024 	if (!hw->affinity_mask) {
2025 		err = -ENOMEM;
2026 		goto err_free_netdev;
2027 	}
2028 
2029 	/* Map CSRs */
2030 	pf->reg_base = pcim_iomap(pdev, PCI_CFG_REG_BAR_NUM, 0);
2031 	if (!pf->reg_base) {
2032 		dev_err(dev, "Unable to map physical function CSRs, aborting\n");
2033 		err = -ENOMEM;
2034 		goto err_free_netdev;
2035 	}
2036 
2037 	err = otx2_check_pf_usable(pf);
2038 	if (err)
2039 		goto err_free_netdev;
2040 
2041 	err = pci_alloc_irq_vectors(hw->pdev, RVU_PF_INT_VEC_CNT,
2042 				    RVU_PF_INT_VEC_CNT, PCI_IRQ_MSIX);
2043 	if (err < 0) {
2044 		dev_err(dev, "%s: Failed to alloc %d IRQ vectors\n",
2045 			__func__, num_vec);
2046 		goto err_free_netdev;
2047 	}
2048 
2049 	/* Init PF <=> AF mailbox stuff */
2050 	err = otx2_pfaf_mbox_init(pf);
2051 	if (err)
2052 		goto err_free_irq_vectors;
2053 
2054 	/* Register mailbox interrupt */
2055 	err = otx2_register_mbox_intr(pf, true);
2056 	if (err)
2057 		goto err_mbox_destroy;
2058 
2059 	/* Request AF to attach NPA and NIX LFs to this PF.
2060 	 * NIX and NPA LFs are needed for this PF to function as a NIC.
2061 	 */
2062 	err = otx2_attach_npa_nix(pf);
2063 	if (err)
2064 		goto err_disable_mbox_intr;
2065 
2066 	err = otx2_realloc_msix_vectors(pf);
2067 	if (err)
2068 		goto err_detach_rsrc;
2069 
2070 	err = otx2_set_real_num_queues(netdev, hw->tx_queues, hw->rx_queues);
2071 	if (err)
2072 		goto err_detach_rsrc;
2073 
2074 	otx2_setup_dev_hw_settings(pf);
2075 
2076 	/* Assign default mac address */
2077 	otx2_get_mac_from_af(netdev);
2078 
2079 	/* Don't check for error.  Proceed without ptp */
2080 	otx2_ptp_init(pf);
2081 
2082 	/* NPA's pool is a stack to which SW frees buffer pointers via Aura.
2083 	 * HW allocates buffer pointer from stack and uses it for DMA'ing
2084 	 * ingress packet. In some scenarios HW can free back allocated buffer
2085 	 * pointers to pool. This makes it impossible for SW to maintain a
2086 	 * parallel list where physical addresses of buffer pointers (IOVAs)
2087 	 * given to HW can be saved for later reference.
2088 	 *
2089 	 * So the only way to convert Rx packet's buffer address is to use
2090 	 * IOMMU's iova_to_phys() handler which translates the address by
2091 	 * walking through the translation tables.
2092 	 */
2093 	pf->iommu_domain = iommu_get_domain_for_dev(dev);
2094 
2095 	netdev->hw_features = (NETIF_F_RXCSUM | NETIF_F_IP_CSUM |
2096 			       NETIF_F_IPV6_CSUM | NETIF_F_RXHASH |
2097 			       NETIF_F_SG | NETIF_F_TSO | NETIF_F_TSO6 |
2098 			       NETIF_F_GSO_UDP_L4);
2099 	netdev->features |= netdev->hw_features;
2100 
2101 	netdev->hw_features |= NETIF_F_LOOPBACK | NETIF_F_RXALL;
2102 
2103 	netdev->gso_max_segs = OTX2_MAX_GSO_SEGS;
2104 	netdev->watchdog_timeo = OTX2_TX_TIMEOUT;
2105 
2106 	netdev->netdev_ops = &otx2_netdev_ops;
2107 
2108 	/* MTU range: 64 - 9190 */
2109 	netdev->min_mtu = OTX2_MIN_MTU;
2110 	netdev->max_mtu = OTX2_MAX_MTU;
2111 
2112 	err = register_netdev(netdev);
2113 	if (err) {
2114 		dev_err(dev, "Failed to register netdevice\n");
2115 		goto err_ptp_destroy;
2116 	}
2117 
2118 	err = otx2_wq_init(pf);
2119 	if (err)
2120 		goto err_unreg_netdev;
2121 
2122 	otx2_set_ethtool_ops(netdev);
2123 
2124 	/* Enable link notifications */
2125 	otx2_cgx_config_linkevents(pf, true);
2126 
2127 	/* Enable pause frames by default */
2128 	pf->flags |= OTX2_FLAG_RX_PAUSE_ENABLED;
2129 	pf->flags |= OTX2_FLAG_TX_PAUSE_ENABLED;
2130 
2131 	return 0;
2132 
2133 err_unreg_netdev:
2134 	unregister_netdev(netdev);
2135 err_ptp_destroy:
2136 	otx2_ptp_destroy(pf);
2137 err_detach_rsrc:
2138 	otx2_detach_resources(&pf->mbox);
2139 err_disable_mbox_intr:
2140 	otx2_disable_mbox_intr(pf);
2141 err_mbox_destroy:
2142 	otx2_pfaf_mbox_destroy(pf);
2143 err_free_irq_vectors:
2144 	pci_free_irq_vectors(hw->pdev);
2145 err_free_netdev:
2146 	pci_set_drvdata(pdev, NULL);
2147 	free_netdev(netdev);
2148 err_release_regions:
2149 	pci_release_regions(pdev);
2150 	return err;
2151 }
2152 
2153 static void otx2_vf_link_event_task(struct work_struct *work)
2154 {
2155 	struct otx2_vf_config *config;
2156 	struct cgx_link_info_msg *req;
2157 	struct mbox_msghdr *msghdr;
2158 	struct otx2_nic *pf;
2159 	int vf_idx;
2160 
2161 	config = container_of(work, struct otx2_vf_config,
2162 			      link_event_work.work);
2163 	vf_idx = config - config->pf->vf_configs;
2164 	pf = config->pf;
2165 
2166 	msghdr = otx2_mbox_alloc_msg_rsp(&pf->mbox_pfvf[0].mbox_up, vf_idx,
2167 					 sizeof(*req), sizeof(struct msg_rsp));
2168 	if (!msghdr) {
2169 		dev_err(pf->dev, "Failed to create VF%d link event\n", vf_idx);
2170 		return;
2171 	}
2172 
2173 	req = (struct cgx_link_info_msg *)msghdr;
2174 	req->hdr.id = MBOX_MSG_CGX_LINK_EVENT;
2175 	req->hdr.sig = OTX2_MBOX_REQ_SIG;
2176 	memcpy(&req->link_info, &pf->linfo, sizeof(req->link_info));
2177 
2178 	otx2_sync_mbox_up_msg(&pf->mbox_pfvf[0], vf_idx);
2179 }
2180 
2181 static int otx2_sriov_enable(struct pci_dev *pdev, int numvfs)
2182 {
2183 	struct net_device *netdev = pci_get_drvdata(pdev);
2184 	struct otx2_nic *pf = netdev_priv(netdev);
2185 	int ret, i;
2186 
2187 	/* Init PF <=> VF mailbox stuff */
2188 	ret = otx2_pfvf_mbox_init(pf, numvfs);
2189 	if (ret)
2190 		return ret;
2191 
2192 	ret = otx2_register_pfvf_mbox_intr(pf, numvfs);
2193 	if (ret)
2194 		goto free_mbox;
2195 
2196 	pf->vf_configs = kcalloc(numvfs, sizeof(struct otx2_vf_config),
2197 				 GFP_KERNEL);
2198 	if (!pf->vf_configs) {
2199 		ret = -ENOMEM;
2200 		goto free_intr;
2201 	}
2202 
2203 	for (i = 0; i < numvfs; i++) {
2204 		pf->vf_configs[i].pf = pf;
2205 		pf->vf_configs[i].intf_down = true;
2206 		INIT_DELAYED_WORK(&pf->vf_configs[i].link_event_work,
2207 				  otx2_vf_link_event_task);
2208 	}
2209 
2210 	ret = otx2_pf_flr_init(pf, numvfs);
2211 	if (ret)
2212 		goto free_configs;
2213 
2214 	ret = otx2_register_flr_me_intr(pf, numvfs);
2215 	if (ret)
2216 		goto free_flr;
2217 
2218 	ret = pci_enable_sriov(pdev, numvfs);
2219 	if (ret)
2220 		goto free_flr_intr;
2221 
2222 	return numvfs;
2223 free_flr_intr:
2224 	otx2_disable_flr_me_intr(pf);
2225 free_flr:
2226 	otx2_flr_wq_destroy(pf);
2227 free_configs:
2228 	kfree(pf->vf_configs);
2229 free_intr:
2230 	otx2_disable_pfvf_mbox_intr(pf, numvfs);
2231 free_mbox:
2232 	otx2_pfvf_mbox_destroy(pf);
2233 	return ret;
2234 }
2235 
2236 static int otx2_sriov_disable(struct pci_dev *pdev)
2237 {
2238 	struct net_device *netdev = pci_get_drvdata(pdev);
2239 	struct otx2_nic *pf = netdev_priv(netdev);
2240 	int numvfs = pci_num_vf(pdev);
2241 	int i;
2242 
2243 	if (!numvfs)
2244 		return 0;
2245 
2246 	pci_disable_sriov(pdev);
2247 
2248 	for (i = 0; i < pci_num_vf(pdev); i++)
2249 		cancel_delayed_work_sync(&pf->vf_configs[i].link_event_work);
2250 	kfree(pf->vf_configs);
2251 
2252 	otx2_disable_flr_me_intr(pf);
2253 	otx2_flr_wq_destroy(pf);
2254 	otx2_disable_pfvf_mbox_intr(pf, numvfs);
2255 	otx2_pfvf_mbox_destroy(pf);
2256 
2257 	return 0;
2258 }
2259 
2260 static int otx2_sriov_configure(struct pci_dev *pdev, int numvfs)
2261 {
2262 	if (numvfs == 0)
2263 		return otx2_sriov_disable(pdev);
2264 	else
2265 		return otx2_sriov_enable(pdev, numvfs);
2266 }
2267 
2268 static void otx2_remove(struct pci_dev *pdev)
2269 {
2270 	struct net_device *netdev = pci_get_drvdata(pdev);
2271 	struct otx2_nic *pf;
2272 
2273 	if (!netdev)
2274 		return;
2275 
2276 	pf = netdev_priv(netdev);
2277 
2278 	if (pf->flags & OTX2_FLAG_TX_TSTAMP_ENABLED)
2279 		otx2_config_hw_tx_tstamp(pf, false);
2280 	if (pf->flags & OTX2_FLAG_RX_TSTAMP_ENABLED)
2281 		otx2_config_hw_rx_tstamp(pf, false);
2282 
2283 	cancel_work_sync(&pf->reset_task);
2284 	/* Disable link notifications */
2285 	otx2_cgx_config_linkevents(pf, false);
2286 
2287 	unregister_netdev(netdev);
2288 	otx2_sriov_disable(pf->pdev);
2289 	if (pf->otx2_wq)
2290 		destroy_workqueue(pf->otx2_wq);
2291 
2292 	otx2_ptp_destroy(pf);
2293 	otx2_detach_resources(&pf->mbox);
2294 	otx2_disable_mbox_intr(pf);
2295 	otx2_pfaf_mbox_destroy(pf);
2296 	pci_free_irq_vectors(pf->pdev);
2297 	pci_set_drvdata(pdev, NULL);
2298 	free_netdev(netdev);
2299 
2300 	pci_release_regions(pdev);
2301 }
2302 
2303 static struct pci_driver otx2_pf_driver = {
2304 	.name = DRV_NAME,
2305 	.id_table = otx2_pf_id_table,
2306 	.probe = otx2_probe,
2307 	.shutdown = otx2_remove,
2308 	.remove = otx2_remove,
2309 	.sriov_configure = otx2_sriov_configure
2310 };
2311 
2312 static int __init otx2_rvupf_init_module(void)
2313 {
2314 	pr_info("%s: %s\n", DRV_NAME, DRV_STRING);
2315 
2316 	return pci_register_driver(&otx2_pf_driver);
2317 }
2318 
2319 static void __exit otx2_rvupf_cleanup_module(void)
2320 {
2321 	pci_unregister_driver(&otx2_pf_driver);
2322 }
2323 
2324 module_init(otx2_rvupf_init_module);
2325 module_exit(otx2_rvupf_cleanup_module);
2326