1 // SPDX-License-Identifier: GPL-2.0
2 /* Marvell RVU Physical Function ethernet driver
4 * Copyright (C) 2020 Marvell.
8 #include <linux/module.h>
9 #include <linux/interrupt.h>
10 #include <linux/pci.h>
11 #include <linux/etherdevice.h>
13 #include <linux/if_vlan.h>
14 #include <linux/iommu.h>
16 #include <linux/bpf.h>
17 #include <linux/bpf_trace.h>
18 #include <linux/bitfield.h>
19 #include <net/page_pool/types.h>
22 #include "otx2_common.h"
23 #include "otx2_txrx.h"
24 #include "otx2_struct.h"
28 #include <rvu_trace.h>
30 #define DRV_NAME "rvu_nicpf"
31 #define DRV_STRING "Marvell RVU NIC Physical Function Driver"
33 /* Supported devices */
34 static const struct pci_device_id otx2_pf_id_table[] = {
35 { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVID_OCTEONTX2_RVU_PF) },
36 { 0, } /* end of table */
39 MODULE_AUTHOR("Sunil Goutham <sgoutham@marvell.com>");
40 MODULE_DESCRIPTION(DRV_STRING);
41 MODULE_LICENSE("GPL v2");
42 MODULE_DEVICE_TABLE(pci, otx2_pf_id_table);
44 static void otx2_vf_link_event_task(struct work_struct *work);
51 static int otx2_config_hw_tx_tstamp(struct otx2_nic *pfvf, bool enable);
52 static int otx2_config_hw_rx_tstamp(struct otx2_nic *pfvf, bool enable);
54 static int otx2_change_mtu(struct net_device *netdev, int new_mtu)
56 struct otx2_nic *pf = netdev_priv(netdev);
57 bool if_up = netif_running(netdev);
60 if (pf->xdp_prog && new_mtu > MAX_XDP_MTU) {
61 netdev_warn(netdev, "Jumbo frames not yet supported with XDP, current MTU %d.\n",
68 netdev_info(netdev, "Changing MTU from %d to %d\n",
69 netdev->mtu, new_mtu);
70 netdev->mtu = new_mtu;
73 err = otx2_open(netdev);
78 static void otx2_disable_flr_me_intr(struct otx2_nic *pf)
80 int irq, vfs = pf->total_vfs;
82 /* Disable VFs ME interrupts */
83 otx2_write64(pf, RVU_PF_VFME_INT_ENA_W1CX(0), INTR_MASK(vfs));
84 irq = pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFME0);
87 /* Disable VFs FLR interrupts */
88 otx2_write64(pf, RVU_PF_VFFLR_INT_ENA_W1CX(0), INTR_MASK(vfs));
89 irq = pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFFLR0);
95 otx2_write64(pf, RVU_PF_VFME_INT_ENA_W1CX(1), INTR_MASK(vfs - 64));
96 irq = pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFME1);
99 otx2_write64(pf, RVU_PF_VFFLR_INT_ENA_W1CX(1), INTR_MASK(vfs - 64));
100 irq = pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFFLR1);
104 static void otx2_flr_wq_destroy(struct otx2_nic *pf)
108 destroy_workqueue(pf->flr_wq);
110 devm_kfree(pf->dev, pf->flr_wrk);
113 static void otx2_flr_handler(struct work_struct *work)
115 struct flr_work *flrwork = container_of(work, struct flr_work, work);
116 struct otx2_nic *pf = flrwork->pf;
117 struct mbox *mbox = &pf->mbox;
121 vf = flrwork - pf->flr_wrk;
123 mutex_lock(&mbox->lock);
124 req = otx2_mbox_alloc_msg_vf_flr(mbox);
126 mutex_unlock(&mbox->lock);
129 req->hdr.pcifunc &= RVU_PFVF_FUNC_MASK;
130 req->hdr.pcifunc |= (vf + 1) & RVU_PFVF_FUNC_MASK;
132 if (!otx2_sync_mbox_msg(&pf->mbox)) {
137 /* clear transcation pending bit */
138 otx2_write64(pf, RVU_PF_VFTRPENDX(reg), BIT_ULL(vf));
139 otx2_write64(pf, RVU_PF_VFFLR_INT_ENA_W1SX(reg), BIT_ULL(vf));
142 mutex_unlock(&mbox->lock);
145 static irqreturn_t otx2_pf_flr_intr_handler(int irq, void *pf_irq)
147 struct otx2_nic *pf = (struct otx2_nic *)pf_irq;
148 int reg, dev, vf, start_vf, num_reg = 1;
151 if (pf->total_vfs > 64)
154 for (reg = 0; reg < num_reg; reg++) {
155 intr = otx2_read64(pf, RVU_PF_VFFLR_INTX(reg));
159 for (vf = 0; vf < 64; vf++) {
160 if (!(intr & BIT_ULL(vf)))
163 queue_work(pf->flr_wq, &pf->flr_wrk[dev].work);
164 /* Clear interrupt */
165 otx2_write64(pf, RVU_PF_VFFLR_INTX(reg), BIT_ULL(vf));
166 /* Disable the interrupt */
167 otx2_write64(pf, RVU_PF_VFFLR_INT_ENA_W1CX(reg),
174 static irqreturn_t otx2_pf_me_intr_handler(int irq, void *pf_irq)
176 struct otx2_nic *pf = (struct otx2_nic *)pf_irq;
177 int vf, reg, num_reg = 1;
180 if (pf->total_vfs > 64)
183 for (reg = 0; reg < num_reg; reg++) {
184 intr = otx2_read64(pf, RVU_PF_VFME_INTX(reg));
187 for (vf = 0; vf < 64; vf++) {
188 if (!(intr & BIT_ULL(vf)))
190 /* clear trpend bit */
191 otx2_write64(pf, RVU_PF_VFTRPENDX(reg), BIT_ULL(vf));
192 /* clear interrupt */
193 otx2_write64(pf, RVU_PF_VFME_INTX(reg), BIT_ULL(vf));
199 static int otx2_register_flr_me_intr(struct otx2_nic *pf, int numvfs)
201 struct otx2_hw *hw = &pf->hw;
205 /* Register ME interrupt handler*/
206 irq_name = &hw->irq_name[RVU_PF_INT_VEC_VFME0 * NAME_SIZE];
207 snprintf(irq_name, NAME_SIZE, "RVUPF%d_ME0", rvu_get_pf(pf->pcifunc));
208 ret = request_irq(pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFME0),
209 otx2_pf_me_intr_handler, 0, irq_name, pf);
212 "RVUPF: IRQ registration failed for ME0\n");
215 /* Register FLR interrupt handler */
216 irq_name = &hw->irq_name[RVU_PF_INT_VEC_VFFLR0 * NAME_SIZE];
217 snprintf(irq_name, NAME_SIZE, "RVUPF%d_FLR0", rvu_get_pf(pf->pcifunc));
218 ret = request_irq(pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFFLR0),
219 otx2_pf_flr_intr_handler, 0, irq_name, pf);
222 "RVUPF: IRQ registration failed for FLR0\n");
227 irq_name = &hw->irq_name[RVU_PF_INT_VEC_VFME1 * NAME_SIZE];
228 snprintf(irq_name, NAME_SIZE, "RVUPF%d_ME1",
229 rvu_get_pf(pf->pcifunc));
230 ret = request_irq(pci_irq_vector
231 (pf->pdev, RVU_PF_INT_VEC_VFME1),
232 otx2_pf_me_intr_handler, 0, irq_name, pf);
235 "RVUPF: IRQ registration failed for ME1\n");
237 irq_name = &hw->irq_name[RVU_PF_INT_VEC_VFFLR1 * NAME_SIZE];
238 snprintf(irq_name, NAME_SIZE, "RVUPF%d_FLR1",
239 rvu_get_pf(pf->pcifunc));
240 ret = request_irq(pci_irq_vector
241 (pf->pdev, RVU_PF_INT_VEC_VFFLR1),
242 otx2_pf_flr_intr_handler, 0, irq_name, pf);
245 "RVUPF: IRQ registration failed for FLR1\n");
250 /* Enable ME interrupt for all VFs*/
251 otx2_write64(pf, RVU_PF_VFME_INTX(0), INTR_MASK(numvfs));
252 otx2_write64(pf, RVU_PF_VFME_INT_ENA_W1SX(0), INTR_MASK(numvfs));
254 /* Enable FLR interrupt for all VFs*/
255 otx2_write64(pf, RVU_PF_VFFLR_INTX(0), INTR_MASK(numvfs));
256 otx2_write64(pf, RVU_PF_VFFLR_INT_ENA_W1SX(0), INTR_MASK(numvfs));
261 otx2_write64(pf, RVU_PF_VFME_INTX(1), INTR_MASK(numvfs));
262 otx2_write64(pf, RVU_PF_VFME_INT_ENA_W1SX(1),
265 otx2_write64(pf, RVU_PF_VFFLR_INTX(1), INTR_MASK(numvfs));
266 otx2_write64(pf, RVU_PF_VFFLR_INT_ENA_W1SX(1),
272 static int otx2_pf_flr_init(struct otx2_nic *pf, int num_vfs)
276 pf->flr_wq = alloc_ordered_workqueue("otx2_pf_flr_wq", WQ_HIGHPRI);
280 pf->flr_wrk = devm_kcalloc(pf->dev, num_vfs,
281 sizeof(struct flr_work), GFP_KERNEL);
283 destroy_workqueue(pf->flr_wq);
287 for (vf = 0; vf < num_vfs; vf++) {
288 pf->flr_wrk[vf].pf = pf;
289 INIT_WORK(&pf->flr_wrk[vf].work, otx2_flr_handler);
295 static void otx2_queue_work(struct mbox *mw, struct workqueue_struct *mbox_wq,
296 int first, int mdevs, u64 intr, int type)
298 struct otx2_mbox_dev *mdev;
299 struct otx2_mbox *mbox;
300 struct mbox_hdr *hdr;
303 for (i = first; i < mdevs; i++) {
305 if (!(intr & BIT_ULL(i - first)))
309 mdev = &mbox->dev[i];
310 if (type == TYPE_PFAF)
311 otx2_sync_mbox_bbuf(mbox, i);
312 hdr = mdev->mbase + mbox->rx_start;
313 /* The hdr->num_msgs is set to zero immediately in the interrupt
314 * handler to ensure that it holds a correct value next time
315 * when the interrupt handler is called.
316 * pf->mbox.num_msgs holds the data for use in pfaf_mbox_handler
317 * pf>mbox.up_num_msgs holds the data for use in
318 * pfaf_mbox_up_handler.
321 mw[i].num_msgs = hdr->num_msgs;
323 if (type == TYPE_PFAF)
324 memset(mbox->hwbase + mbox->rx_start, 0,
325 ALIGN(sizeof(struct mbox_hdr),
328 queue_work(mbox_wq, &mw[i].mbox_wrk);
332 mdev = &mbox->dev[i];
333 if (type == TYPE_PFAF)
334 otx2_sync_mbox_bbuf(mbox, i);
335 hdr = mdev->mbase + mbox->rx_start;
337 mw[i].up_num_msgs = hdr->num_msgs;
339 if (type == TYPE_PFAF)
340 memset(mbox->hwbase + mbox->rx_start, 0,
341 ALIGN(sizeof(struct mbox_hdr),
344 queue_work(mbox_wq, &mw[i].mbox_up_wrk);
349 static void otx2_forward_msg_pfvf(struct otx2_mbox_dev *mdev,
350 struct otx2_mbox *pfvf_mbox, void *bbuf_base,
353 struct otx2_mbox_dev *src_mdev = mdev;
356 /* Msgs are already copied, trigger VF's mbox irq */
359 offset = pfvf_mbox->trigger | (devid << pfvf_mbox->tr_shift);
360 writeq(1, (void __iomem *)pfvf_mbox->reg_base + offset);
362 /* Restore VF's mbox bounce buffer region address */
363 src_mdev->mbase = bbuf_base;
366 static int otx2_forward_vf_mbox_msgs(struct otx2_nic *pf,
367 struct otx2_mbox *src_mbox,
368 int dir, int vf, int num_msgs)
370 struct otx2_mbox_dev *src_mdev, *dst_mdev;
371 struct mbox_hdr *mbox_hdr;
372 struct mbox_hdr *req_hdr;
373 struct mbox *dst_mbox;
376 if (dir == MBOX_DIR_PFAF) {
377 /* Set VF's mailbox memory as PF's bounce buffer memory, so
378 * that explicit copying of VF's msgs to PF=>AF mbox region
379 * and AF=>PF responses to VF's mbox region can be avoided.
381 src_mdev = &src_mbox->dev[vf];
382 mbox_hdr = src_mbox->hwbase +
383 src_mbox->rx_start + (vf * MBOX_SIZE);
385 dst_mbox = &pf->mbox;
386 dst_size = dst_mbox->mbox.tx_size -
387 ALIGN(sizeof(*mbox_hdr), MBOX_MSG_ALIGN);
388 /* Check if msgs fit into destination area and has valid size */
389 if (mbox_hdr->msg_size > dst_size || !mbox_hdr->msg_size)
392 dst_mdev = &dst_mbox->mbox.dev[0];
394 mutex_lock(&pf->mbox.lock);
395 dst_mdev->mbase = src_mdev->mbase;
396 dst_mdev->msg_size = mbox_hdr->msg_size;
397 dst_mdev->num_msgs = num_msgs;
398 err = otx2_sync_mbox_msg(dst_mbox);
399 /* Error code -EIO indicate there is a communication failure
400 * to the AF. Rest of the error codes indicate that AF processed
401 * VF messages and set the error codes in response messages
402 * (if any) so simply forward responses to VF.
406 "AF not responding to VF%d messages\n", vf);
407 /* restore PF mbase and exit */
408 dst_mdev->mbase = pf->mbox.bbuf_base;
409 mutex_unlock(&pf->mbox.lock);
412 /* At this point, all the VF messages sent to AF are acked
413 * with proper responses and responses are copied to VF
414 * mailbox hence raise interrupt to VF.
416 req_hdr = (struct mbox_hdr *)(dst_mdev->mbase +
417 dst_mbox->mbox.rx_start);
418 req_hdr->num_msgs = num_msgs;
420 otx2_forward_msg_pfvf(dst_mdev, &pf->mbox_pfvf[0].mbox,
421 pf->mbox.bbuf_base, vf);
422 mutex_unlock(&pf->mbox.lock);
423 } else if (dir == MBOX_DIR_PFVF_UP) {
424 src_mdev = &src_mbox->dev[0];
425 mbox_hdr = src_mbox->hwbase + src_mbox->rx_start;
426 req_hdr = (struct mbox_hdr *)(src_mdev->mbase +
428 req_hdr->num_msgs = num_msgs;
430 dst_mbox = &pf->mbox_pfvf[0];
431 dst_size = dst_mbox->mbox_up.tx_size -
432 ALIGN(sizeof(*mbox_hdr), MBOX_MSG_ALIGN);
433 /* Check if msgs fit into destination area */
434 if (mbox_hdr->msg_size > dst_size)
437 dst_mdev = &dst_mbox->mbox_up.dev[vf];
438 dst_mdev->mbase = src_mdev->mbase;
439 dst_mdev->msg_size = mbox_hdr->msg_size;
440 dst_mdev->num_msgs = mbox_hdr->num_msgs;
441 err = otx2_sync_mbox_up_msg(dst_mbox, vf);
444 "VF%d is not responding to mailbox\n", vf);
447 } else if (dir == MBOX_DIR_VFPF_UP) {
448 req_hdr = (struct mbox_hdr *)(src_mbox->dev[0].mbase +
450 req_hdr->num_msgs = num_msgs;
451 otx2_forward_msg_pfvf(&pf->mbox_pfvf->mbox_up.dev[vf],
453 pf->mbox_pfvf[vf].bbuf_base,
460 static void otx2_pfvf_mbox_handler(struct work_struct *work)
462 struct mbox_msghdr *msg = NULL;
463 int offset, vf_idx, id, err;
464 struct otx2_mbox_dev *mdev;
465 struct mbox_hdr *req_hdr;
466 struct otx2_mbox *mbox;
467 struct mbox *vf_mbox;
470 vf_mbox = container_of(work, struct mbox, mbox_wrk);
472 vf_idx = vf_mbox - pf->mbox_pfvf;
474 mbox = &pf->mbox_pfvf[0].mbox;
475 mdev = &mbox->dev[vf_idx];
476 req_hdr = (struct mbox_hdr *)(mdev->mbase + mbox->rx_start);
478 offset = ALIGN(sizeof(*req_hdr), MBOX_MSG_ALIGN);
480 for (id = 0; id < vf_mbox->num_msgs; id++) {
481 msg = (struct mbox_msghdr *)(mdev->mbase + mbox->rx_start +
484 if (msg->sig != OTX2_MBOX_REQ_SIG)
487 /* Set VF's number in each of the msg */
488 msg->pcifunc &= RVU_PFVF_FUNC_MASK;
489 msg->pcifunc |= (vf_idx + 1) & RVU_PFVF_FUNC_MASK;
490 offset = msg->next_msgoff;
492 err = otx2_forward_vf_mbox_msgs(pf, mbox, MBOX_DIR_PFAF, vf_idx,
499 otx2_reply_invalid_msg(mbox, vf_idx, 0, msg->id);
500 otx2_mbox_msg_send(mbox, vf_idx);
503 static void otx2_pfvf_mbox_up_handler(struct work_struct *work)
505 struct mbox *vf_mbox = container_of(work, struct mbox, mbox_up_wrk);
506 struct otx2_nic *pf = vf_mbox->pfvf;
507 struct otx2_mbox_dev *mdev;
508 int offset, id, vf_idx = 0;
509 struct mbox_hdr *rsp_hdr;
510 struct mbox_msghdr *msg;
511 struct otx2_mbox *mbox;
513 vf_idx = vf_mbox - pf->mbox_pfvf;
514 mbox = &pf->mbox_pfvf[0].mbox_up;
515 mdev = &mbox->dev[vf_idx];
517 rsp_hdr = (struct mbox_hdr *)(mdev->mbase + mbox->rx_start);
518 offset = mbox->rx_start + ALIGN(sizeof(*rsp_hdr), MBOX_MSG_ALIGN);
520 for (id = 0; id < vf_mbox->up_num_msgs; id++) {
521 msg = mdev->mbase + offset;
523 if (msg->id >= MBOX_MSG_MAX) {
525 "Mbox msg with unknown ID 0x%x\n", msg->id);
529 if (msg->sig != OTX2_MBOX_RSP_SIG) {
531 "Mbox msg with wrong signature %x, ID 0x%x\n",
537 case MBOX_MSG_CGX_LINK_EVENT:
542 "Mbox msg response has err %d, ID 0x%x\n",
548 offset = mbox->rx_start + msg->next_msgoff;
549 if (mdev->msgs_acked == (vf_mbox->up_num_msgs - 1))
550 __otx2_mbox_reset(mbox, 0);
555 static irqreturn_t otx2_pfvf_mbox_intr_handler(int irq, void *pf_irq)
557 struct otx2_nic *pf = (struct otx2_nic *)(pf_irq);
558 int vfs = pf->total_vfs;
562 mbox = pf->mbox_pfvf;
563 /* Handle VF interrupts */
565 intr = otx2_read64(pf, RVU_PF_VFPF_MBOX_INTX(1));
566 otx2_write64(pf, RVU_PF_VFPF_MBOX_INTX(1), intr);
567 otx2_queue_work(mbox, pf->mbox_pfvf_wq, 64, vfs, intr,
572 intr = otx2_read64(pf, RVU_PF_VFPF_MBOX_INTX(0));
573 otx2_write64(pf, RVU_PF_VFPF_MBOX_INTX(0), intr);
575 otx2_queue_work(mbox, pf->mbox_pfvf_wq, 0, vfs, intr, TYPE_PFVF);
577 trace_otx2_msg_interrupt(mbox->mbox.pdev, "VF(s) to PF", intr);
582 static int otx2_pfvf_mbox_init(struct otx2_nic *pf, int numvfs)
584 void __iomem *hwbase;
592 pf->mbox_pfvf = devm_kcalloc(&pf->pdev->dev, numvfs,
593 sizeof(struct mbox), GFP_KERNEL);
597 pf->mbox_pfvf_wq = alloc_ordered_workqueue("otx2_pfvf_mailbox",
598 WQ_HIGHPRI | WQ_MEM_RECLAIM);
599 if (!pf->mbox_pfvf_wq)
602 /* On CN10K platform, PF <-> VF mailbox region follows after
603 * PF <-> AF mailbox region.
605 if (test_bit(CN10K_MBOX, &pf->hw.cap_flag))
606 base = pci_resource_start(pf->pdev, PCI_MBOX_BAR_NUM) +
609 base = readq((void __iomem *)((u64)pf->reg_base +
610 RVU_PF_VF_BAR4_ADDR));
612 hwbase = ioremap_wc(base, MBOX_SIZE * pf->total_vfs);
618 mbox = &pf->mbox_pfvf[0];
619 err = otx2_mbox_init(&mbox->mbox, hwbase, pf->pdev, pf->reg_base,
620 MBOX_DIR_PFVF, numvfs);
624 err = otx2_mbox_init(&mbox->mbox_up, hwbase, pf->pdev, pf->reg_base,
625 MBOX_DIR_PFVF_UP, numvfs);
629 for (vf = 0; vf < numvfs; vf++) {
631 INIT_WORK(&mbox->mbox_wrk, otx2_pfvf_mbox_handler);
632 INIT_WORK(&mbox->mbox_up_wrk, otx2_pfvf_mbox_up_handler);
642 destroy_workqueue(pf->mbox_pfvf_wq);
646 static void otx2_pfvf_mbox_destroy(struct otx2_nic *pf)
648 struct mbox *mbox = &pf->mbox_pfvf[0];
653 if (pf->mbox_pfvf_wq) {
654 destroy_workqueue(pf->mbox_pfvf_wq);
655 pf->mbox_pfvf_wq = NULL;
658 if (mbox->mbox.hwbase)
659 iounmap(mbox->mbox.hwbase);
661 otx2_mbox_destroy(&mbox->mbox);
664 static void otx2_enable_pfvf_mbox_intr(struct otx2_nic *pf, int numvfs)
666 /* Clear PF <=> VF mailbox IRQ */
667 otx2_write64(pf, RVU_PF_VFPF_MBOX_INTX(0), ~0ull);
668 otx2_write64(pf, RVU_PF_VFPF_MBOX_INTX(1), ~0ull);
670 /* Enable PF <=> VF mailbox IRQ */
671 otx2_write64(pf, RVU_PF_VFPF_MBOX_INT_ENA_W1SX(0), INTR_MASK(numvfs));
674 otx2_write64(pf, RVU_PF_VFPF_MBOX_INT_ENA_W1SX(1),
679 static void otx2_disable_pfvf_mbox_intr(struct otx2_nic *pf, int numvfs)
683 /* Disable PF <=> VF mailbox IRQ */
684 otx2_write64(pf, RVU_PF_VFPF_MBOX_INT_ENA_W1CX(0), ~0ull);
685 otx2_write64(pf, RVU_PF_VFPF_MBOX_INT_ENA_W1CX(1), ~0ull);
687 otx2_write64(pf, RVU_PF_VFPF_MBOX_INTX(0), ~0ull);
688 vector = pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFPF_MBOX0);
689 free_irq(vector, pf);
692 otx2_write64(pf, RVU_PF_VFPF_MBOX_INTX(1), ~0ull);
693 vector = pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFPF_MBOX1);
694 free_irq(vector, pf);
698 static int otx2_register_pfvf_mbox_intr(struct otx2_nic *pf, int numvfs)
700 struct otx2_hw *hw = &pf->hw;
704 /* Register MBOX0 interrupt handler */
705 irq_name = &hw->irq_name[RVU_PF_INT_VEC_VFPF_MBOX0 * NAME_SIZE];
707 snprintf(irq_name, NAME_SIZE,
708 "RVUPF%d_VF Mbox0", rvu_get_pf(pf->pcifunc));
710 snprintf(irq_name, NAME_SIZE, "RVUPF_VF Mbox0");
711 err = request_irq(pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFPF_MBOX0),
712 otx2_pfvf_mbox_intr_handler, 0, irq_name, pf);
715 "RVUPF: IRQ registration failed for PFVF mbox0 irq\n");
720 /* Register MBOX1 interrupt handler */
721 irq_name = &hw->irq_name[RVU_PF_INT_VEC_VFPF_MBOX1 * NAME_SIZE];
723 snprintf(irq_name, NAME_SIZE,
724 "RVUPF%d_VF Mbox1", rvu_get_pf(pf->pcifunc));
726 snprintf(irq_name, NAME_SIZE, "RVUPF_VF Mbox1");
727 err = request_irq(pci_irq_vector(pf->pdev,
728 RVU_PF_INT_VEC_VFPF_MBOX1),
729 otx2_pfvf_mbox_intr_handler,
733 "RVUPF: IRQ registration failed for PFVF mbox1 irq\n");
738 otx2_enable_pfvf_mbox_intr(pf, numvfs);
743 static void otx2_process_pfaf_mbox_msg(struct otx2_nic *pf,
744 struct mbox_msghdr *msg)
748 if (msg->id >= MBOX_MSG_MAX) {
750 "Mbox msg with unknown ID 0x%x\n", msg->id);
754 if (msg->sig != OTX2_MBOX_RSP_SIG) {
756 "Mbox msg with wrong signature %x, ID 0x%x\n",
761 /* message response heading VF */
762 devid = msg->pcifunc & RVU_PFVF_FUNC_MASK;
764 struct otx2_vf_config *config = &pf->vf_configs[devid - 1];
765 struct delayed_work *dwork;
768 case MBOX_MSG_NIX_LF_START_RX:
769 config->intf_down = false;
770 dwork = &config->link_event_work;
771 schedule_delayed_work(dwork, msecs_to_jiffies(100));
773 case MBOX_MSG_NIX_LF_STOP_RX:
774 config->intf_down = true;
783 pf->pcifunc = msg->pcifunc;
785 case MBOX_MSG_MSIX_OFFSET:
786 mbox_handler_msix_offset(pf, (struct msix_offset_rsp *)msg);
788 case MBOX_MSG_NPA_LF_ALLOC:
789 mbox_handler_npa_lf_alloc(pf, (struct npa_lf_alloc_rsp *)msg);
791 case MBOX_MSG_NIX_LF_ALLOC:
792 mbox_handler_nix_lf_alloc(pf, (struct nix_lf_alloc_rsp *)msg);
794 case MBOX_MSG_NIX_BP_ENABLE:
795 mbox_handler_nix_bp_enable(pf, (struct nix_bp_cfg_rsp *)msg);
797 case MBOX_MSG_CGX_STATS:
798 mbox_handler_cgx_stats(pf, (struct cgx_stats_rsp *)msg);
800 case MBOX_MSG_CGX_FEC_STATS:
801 mbox_handler_cgx_fec_stats(pf, (struct cgx_fec_stats_rsp *)msg);
806 "Mbox msg response has err %d, ID 0x%x\n",
812 static void otx2_pfaf_mbox_handler(struct work_struct *work)
814 struct otx2_mbox_dev *mdev;
815 struct mbox_hdr *rsp_hdr;
816 struct mbox_msghdr *msg;
817 struct otx2_mbox *mbox;
818 struct mbox *af_mbox;
822 af_mbox = container_of(work, struct mbox, mbox_wrk);
823 mbox = &af_mbox->mbox;
824 mdev = &mbox->dev[0];
825 rsp_hdr = (struct mbox_hdr *)(mdev->mbase + mbox->rx_start);
827 offset = mbox->rx_start + ALIGN(sizeof(*rsp_hdr), MBOX_MSG_ALIGN);
830 for (id = 0; id < af_mbox->num_msgs; id++) {
831 msg = (struct mbox_msghdr *)(mdev->mbase + offset);
832 otx2_process_pfaf_mbox_msg(pf, msg);
833 offset = mbox->rx_start + msg->next_msgoff;
834 if (mdev->msgs_acked == (af_mbox->num_msgs - 1))
835 __otx2_mbox_reset(mbox, 0);
841 static void otx2_handle_link_event(struct otx2_nic *pf)
843 struct cgx_link_user_info *linfo = &pf->linfo;
844 struct net_device *netdev = pf->netdev;
846 pr_info("%s NIC Link is %s %d Mbps %s duplex\n", netdev->name,
847 linfo->link_up ? "UP" : "DOWN", linfo->speed,
848 linfo->full_duplex ? "Full" : "Half");
849 if (linfo->link_up) {
850 netif_carrier_on(netdev);
851 netif_tx_start_all_queues(netdev);
853 netif_tx_stop_all_queues(netdev);
854 netif_carrier_off(netdev);
858 int otx2_mbox_up_handler_mcs_intr_notify(struct otx2_nic *pf,
859 struct mcs_intr_info *event,
862 cn10k_handle_mcs_event(pf, event);
867 int otx2_mbox_up_handler_cgx_link_event(struct otx2_nic *pf,
868 struct cgx_link_info_msg *msg,
873 /* Copy the link info sent by AF */
874 pf->linfo = msg->link_info;
876 /* notify VFs about link event */
877 for (i = 0; i < pci_num_vf(pf->pdev); i++) {
878 struct otx2_vf_config *config = &pf->vf_configs[i];
879 struct delayed_work *dwork = &config->link_event_work;
881 if (config->intf_down)
884 schedule_delayed_work(dwork, msecs_to_jiffies(100));
887 /* interface has not been fully configured yet */
888 if (pf->flags & OTX2_FLAG_INTF_DOWN)
891 otx2_handle_link_event(pf);
895 static int otx2_process_mbox_msg_up(struct otx2_nic *pf,
896 struct mbox_msghdr *req)
898 /* Check if valid, if not reply with a invalid msg */
899 if (req->sig != OTX2_MBOX_REQ_SIG) {
900 otx2_reply_invalid_msg(&pf->mbox.mbox_up, 0, 0, req->id);
905 #define M(_name, _id, _fn_name, _req_type, _rsp_type) \
907 struct _rsp_type *rsp; \
910 rsp = (struct _rsp_type *)otx2_mbox_alloc_msg( \
911 &pf->mbox.mbox_up, 0, \
912 sizeof(struct _rsp_type)); \
917 rsp->hdr.sig = OTX2_MBOX_RSP_SIG; \
918 rsp->hdr.pcifunc = 0; \
921 err = otx2_mbox_up_handler_ ## _fn_name( \
922 pf, (struct _req_type *)req, rsp); \
930 otx2_reply_invalid_msg(&pf->mbox.mbox_up, 0, 0, req->id);
936 static void otx2_pfaf_mbox_up_handler(struct work_struct *work)
938 struct mbox *af_mbox = container_of(work, struct mbox, mbox_up_wrk);
939 struct otx2_mbox *mbox = &af_mbox->mbox_up;
940 struct otx2_mbox_dev *mdev = &mbox->dev[0];
941 struct otx2_nic *pf = af_mbox->pfvf;
942 int offset, id, devid = 0;
943 struct mbox_hdr *rsp_hdr;
944 struct mbox_msghdr *msg;
946 rsp_hdr = (struct mbox_hdr *)(mdev->mbase + mbox->rx_start);
948 offset = mbox->rx_start + ALIGN(sizeof(*rsp_hdr), MBOX_MSG_ALIGN);
950 for (id = 0; id < af_mbox->up_num_msgs; id++) {
951 msg = (struct mbox_msghdr *)(mdev->mbase + offset);
953 devid = msg->pcifunc & RVU_PFVF_FUNC_MASK;
954 /* Skip processing VF's messages */
956 otx2_process_mbox_msg_up(pf, msg);
957 offset = mbox->rx_start + msg->next_msgoff;
960 otx2_forward_vf_mbox_msgs(pf, &pf->mbox.mbox_up,
961 MBOX_DIR_PFVF_UP, devid - 1,
962 af_mbox->up_num_msgs);
966 otx2_mbox_msg_send(mbox, 0);
969 static irqreturn_t otx2_pfaf_mbox_intr_handler(int irq, void *pf_irq)
971 struct otx2_nic *pf = (struct otx2_nic *)pf_irq;
975 otx2_write64(pf, RVU_PF_INT, BIT_ULL(0));
979 trace_otx2_msg_interrupt(mbox->mbox.pdev, "AF to PF", BIT_ULL(0));
981 otx2_queue_work(mbox, pf->mbox_wq, 0, 1, 1, TYPE_PFAF);
986 static void otx2_disable_mbox_intr(struct otx2_nic *pf)
988 int vector = pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_AFPF_MBOX);
990 /* Disable AF => PF mailbox IRQ */
991 otx2_write64(pf, RVU_PF_INT_ENA_W1C, BIT_ULL(0));
992 free_irq(vector, pf);
995 static int otx2_register_mbox_intr(struct otx2_nic *pf, bool probe_af)
997 struct otx2_hw *hw = &pf->hw;
1002 /* Register mailbox interrupt handler */
1003 irq_name = &hw->irq_name[RVU_PF_INT_VEC_AFPF_MBOX * NAME_SIZE];
1004 snprintf(irq_name, NAME_SIZE, "RVUPFAF Mbox");
1005 err = request_irq(pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_AFPF_MBOX),
1006 otx2_pfaf_mbox_intr_handler, 0, irq_name, pf);
1009 "RVUPF: IRQ registration failed for PFAF mbox irq\n");
1013 /* Enable mailbox interrupt for msgs coming from AF.
1014 * First clear to avoid spurious interrupts, if any.
1016 otx2_write64(pf, RVU_PF_INT, BIT_ULL(0));
1017 otx2_write64(pf, RVU_PF_INT_ENA_W1S, BIT_ULL(0));
1022 /* Check mailbox communication with AF */
1023 req = otx2_mbox_alloc_msg_ready(&pf->mbox);
1025 otx2_disable_mbox_intr(pf);
1028 err = otx2_sync_mbox_msg(&pf->mbox);
1031 "AF not responding to mailbox, deferring probe\n");
1032 otx2_disable_mbox_intr(pf);
1033 return -EPROBE_DEFER;
1039 static void otx2_pfaf_mbox_destroy(struct otx2_nic *pf)
1041 struct mbox *mbox = &pf->mbox;
1044 destroy_workqueue(pf->mbox_wq);
1048 if (mbox->mbox.hwbase)
1049 iounmap((void __iomem *)mbox->mbox.hwbase);
1051 otx2_mbox_destroy(&mbox->mbox);
1052 otx2_mbox_destroy(&mbox->mbox_up);
1055 static int otx2_pfaf_mbox_init(struct otx2_nic *pf)
1057 struct mbox *mbox = &pf->mbox;
1058 void __iomem *hwbase;
1062 pf->mbox_wq = alloc_ordered_workqueue("otx2_pfaf_mailbox",
1063 WQ_HIGHPRI | WQ_MEM_RECLAIM);
1067 /* Mailbox is a reserved memory (in RAM) region shared between
1068 * admin function (i.e AF) and this PF, shouldn't be mapped as
1069 * device memory to allow unaligned accesses.
1071 hwbase = ioremap_wc(pci_resource_start(pf->pdev, PCI_MBOX_BAR_NUM),
1074 dev_err(pf->dev, "Unable to map PFAF mailbox region\n");
1079 err = otx2_mbox_init(&mbox->mbox, hwbase, pf->pdev, pf->reg_base,
1084 err = otx2_mbox_init(&mbox->mbox_up, hwbase, pf->pdev, pf->reg_base,
1085 MBOX_DIR_PFAF_UP, 1);
1089 err = otx2_mbox_bbuf_init(mbox, pf->pdev);
1093 INIT_WORK(&mbox->mbox_wrk, otx2_pfaf_mbox_handler);
1094 INIT_WORK(&mbox->mbox_up_wrk, otx2_pfaf_mbox_up_handler);
1095 mutex_init(&mbox->lock);
1099 otx2_pfaf_mbox_destroy(pf);
1103 static int otx2_cgx_config_linkevents(struct otx2_nic *pf, bool enable)
1105 struct msg_req *msg;
1108 mutex_lock(&pf->mbox.lock);
1110 msg = otx2_mbox_alloc_msg_cgx_start_linkevents(&pf->mbox);
1112 msg = otx2_mbox_alloc_msg_cgx_stop_linkevents(&pf->mbox);
1115 mutex_unlock(&pf->mbox.lock);
1119 err = otx2_sync_mbox_msg(&pf->mbox);
1120 mutex_unlock(&pf->mbox.lock);
1124 static int otx2_cgx_config_loopback(struct otx2_nic *pf, bool enable)
1126 struct msg_req *msg;
1129 if (enable && !bitmap_empty(pf->flow_cfg->dmacflt_bmap,
1130 pf->flow_cfg->dmacflt_max_flows))
1131 netdev_warn(pf->netdev,
1132 "CGX/RPM internal loopback might not work as DMAC filters are active\n");
1134 mutex_lock(&pf->mbox.lock);
1136 msg = otx2_mbox_alloc_msg_cgx_intlbk_enable(&pf->mbox);
1138 msg = otx2_mbox_alloc_msg_cgx_intlbk_disable(&pf->mbox);
1141 mutex_unlock(&pf->mbox.lock);
1145 err = otx2_sync_mbox_msg(&pf->mbox);
1146 mutex_unlock(&pf->mbox.lock);
1150 int otx2_set_real_num_queues(struct net_device *netdev,
1151 int tx_queues, int rx_queues)
1155 err = netif_set_real_num_tx_queues(netdev, tx_queues);
1158 "Failed to set no of Tx queues: %d\n", tx_queues);
1162 err = netif_set_real_num_rx_queues(netdev, rx_queues);
1165 "Failed to set no of Rx queues: %d\n", rx_queues);
1168 EXPORT_SYMBOL(otx2_set_real_num_queues);
1170 static char *nix_sqoperr_e_str[NIX_SQOPERR_MAX] = {
1172 "NIX_SQOPERR_CTX_FAULT",
1173 "NIX_SQOPERR_CTX_POISON",
1174 "NIX_SQOPERR_DISABLED",
1175 "NIX_SQOPERR_SIZE_ERR",
1176 "NIX_SQOPERR_OFLOW",
1177 "NIX_SQOPERR_SQB_NULL",
1178 "NIX_SQOPERR_SQB_FAULT",
1179 "NIX_SQOPERR_SQE_SZ_ZERO",
1182 static char *nix_mnqerr_e_str[NIX_MNQERR_MAX] = {
1183 "NIX_MNQERR_SQ_CTX_FAULT",
1184 "NIX_MNQERR_SQ_CTX_POISON",
1185 "NIX_MNQERR_SQB_FAULT",
1186 "NIX_MNQERR_SQB_POISON",
1187 "NIX_MNQERR_TOTAL_ERR",
1188 "NIX_MNQERR_LSO_ERR",
1189 "NIX_MNQERR_CQ_QUERY_ERR",
1190 "NIX_MNQERR_MAX_SQE_SIZE_ERR",
1191 "NIX_MNQERR_MAXLEN_ERR",
1192 "NIX_MNQERR_SQE_SIZEM1_ZERO",
1195 static char *nix_snd_status_e_str[NIX_SND_STATUS_MAX] = {
1196 [NIX_SND_STATUS_GOOD] = "NIX_SND_STATUS_GOOD",
1197 [NIX_SND_STATUS_SQ_CTX_FAULT] = "NIX_SND_STATUS_SQ_CTX_FAULT",
1198 [NIX_SND_STATUS_SQ_CTX_POISON] = "NIX_SND_STATUS_SQ_CTX_POISON",
1199 [NIX_SND_STATUS_SQB_FAULT] = "NIX_SND_STATUS_SQB_FAULT",
1200 [NIX_SND_STATUS_SQB_POISON] = "NIX_SND_STATUS_SQB_POISON",
1201 [NIX_SND_STATUS_HDR_ERR] = "NIX_SND_STATUS_HDR_ERR",
1202 [NIX_SND_STATUS_EXT_ERR] = "NIX_SND_STATUS_EXT_ERR",
1203 [NIX_SND_STATUS_JUMP_FAULT] = "NIX_SND_STATUS_JUMP_FAULT",
1204 [NIX_SND_STATUS_JUMP_POISON] = "NIX_SND_STATUS_JUMP_POISON",
1205 [NIX_SND_STATUS_CRC_ERR] = "NIX_SND_STATUS_CRC_ERR",
1206 [NIX_SND_STATUS_IMM_ERR] = "NIX_SND_STATUS_IMM_ERR",
1207 [NIX_SND_STATUS_SG_ERR] = "NIX_SND_STATUS_SG_ERR",
1208 [NIX_SND_STATUS_MEM_ERR] = "NIX_SND_STATUS_MEM_ERR",
1209 [NIX_SND_STATUS_INVALID_SUBDC] = "NIX_SND_STATUS_INVALID_SUBDC",
1210 [NIX_SND_STATUS_SUBDC_ORDER_ERR] = "NIX_SND_STATUS_SUBDC_ORDER_ERR",
1211 [NIX_SND_STATUS_DATA_FAULT] = "NIX_SND_STATUS_DATA_FAULT",
1212 [NIX_SND_STATUS_DATA_POISON] = "NIX_SND_STATUS_DATA_POISON",
1213 [NIX_SND_STATUS_NPC_DROP_ACTION] = "NIX_SND_STATUS_NPC_DROP_ACTION",
1214 [NIX_SND_STATUS_LOCK_VIOL] = "NIX_SND_STATUS_LOCK_VIOL",
1215 [NIX_SND_STATUS_NPC_UCAST_CHAN_ERR] = "NIX_SND_STAT_NPC_UCAST_CHAN_ERR",
1216 [NIX_SND_STATUS_NPC_MCAST_CHAN_ERR] = "NIX_SND_STAT_NPC_MCAST_CHAN_ERR",
1217 [NIX_SND_STATUS_NPC_MCAST_ABORT] = "NIX_SND_STATUS_NPC_MCAST_ABORT",
1218 [NIX_SND_STATUS_NPC_VTAG_PTR_ERR] = "NIX_SND_STATUS_NPC_VTAG_PTR_ERR",
1219 [NIX_SND_STATUS_NPC_VTAG_SIZE_ERR] = "NIX_SND_STATUS_NPC_VTAG_SIZE_ERR",
1220 [NIX_SND_STATUS_SEND_MEM_FAULT] = "NIX_SND_STATUS_SEND_MEM_FAULT",
1221 [NIX_SND_STATUS_SEND_STATS_ERR] = "NIX_SND_STATUS_SEND_STATS_ERR",
1224 static irqreturn_t otx2_q_intr_handler(int irq, void *data)
1226 struct otx2_nic *pf = data;
1227 struct otx2_snd_queue *sq;
1232 for (qidx = 0; qidx < pf->qset.cq_cnt; qidx++) {
1233 ptr = otx2_get_regaddr(pf, NIX_LF_CQ_OP_INT);
1234 val = otx2_atomic64_add((qidx << 44), ptr);
1236 otx2_write64(pf, NIX_LF_CQ_OP_INT, (qidx << 44) |
1237 (val & NIX_CQERRINT_BITS));
1238 if (!(val & (NIX_CQERRINT_BITS | BIT_ULL(42))))
1241 if (val & BIT_ULL(42)) {
1242 netdev_err(pf->netdev,
1243 "CQ%lld: error reading NIX_LF_CQ_OP_INT, NIX_LF_ERR_INT 0x%llx\n",
1244 qidx, otx2_read64(pf, NIX_LF_ERR_INT));
1246 if (val & BIT_ULL(NIX_CQERRINT_DOOR_ERR))
1247 netdev_err(pf->netdev, "CQ%lld: Doorbell error",
1249 if (val & BIT_ULL(NIX_CQERRINT_CQE_FAULT))
1250 netdev_err(pf->netdev,
1251 "CQ%lld: Memory fault on CQE write to LLC/DRAM",
1255 schedule_work(&pf->reset_task);
1259 for (qidx = 0; qidx < otx2_get_total_tx_queues(pf); qidx++) {
1260 u64 sq_op_err_dbg, mnq_err_dbg, snd_err_dbg;
1261 u8 sq_op_err_code, mnq_err_code, snd_err_code;
1263 sq = &pf->qset.sq[qidx];
1267 /* Below debug registers captures first errors corresponding to
1268 * those registers. We don't have to check against SQ qid as
1269 * these are fatal errors.
1272 ptr = otx2_get_regaddr(pf, NIX_LF_SQ_OP_INT);
1273 val = otx2_atomic64_add((qidx << 44), ptr);
1274 otx2_write64(pf, NIX_LF_SQ_OP_INT, (qidx << 44) |
1275 (val & NIX_SQINT_BITS));
1277 if (val & BIT_ULL(42)) {
1278 netdev_err(pf->netdev,
1279 "SQ%lld: error reading NIX_LF_SQ_OP_INT, NIX_LF_ERR_INT 0x%llx\n",
1280 qidx, otx2_read64(pf, NIX_LF_ERR_INT));
1284 sq_op_err_dbg = otx2_read64(pf, NIX_LF_SQ_OP_ERR_DBG);
1285 if (!(sq_op_err_dbg & BIT(44)))
1286 goto chk_mnq_err_dbg;
1288 sq_op_err_code = FIELD_GET(GENMASK(7, 0), sq_op_err_dbg);
1289 netdev_err(pf->netdev,
1290 "SQ%lld: NIX_LF_SQ_OP_ERR_DBG(0x%llx) err=%s(%#x)\n",
1291 qidx, sq_op_err_dbg,
1292 nix_sqoperr_e_str[sq_op_err_code],
1295 otx2_write64(pf, NIX_LF_SQ_OP_ERR_DBG, BIT_ULL(44));
1297 if (sq_op_err_code == NIX_SQOPERR_SQB_NULL)
1298 goto chk_mnq_err_dbg;
1300 /* Err is not NIX_SQOPERR_SQB_NULL, call aq function to read SQ structure.
1301 * TODO: But we are in irq context. How to call mbox functions which does sleep
1305 mnq_err_dbg = otx2_read64(pf, NIX_LF_MNQ_ERR_DBG);
1306 if (!(mnq_err_dbg & BIT(44)))
1307 goto chk_snd_err_dbg;
1309 mnq_err_code = FIELD_GET(GENMASK(7, 0), mnq_err_dbg);
1310 netdev_err(pf->netdev,
1311 "SQ%lld: NIX_LF_MNQ_ERR_DBG(0x%llx) err=%s(%#x)\n",
1312 qidx, mnq_err_dbg, nix_mnqerr_e_str[mnq_err_code],
1314 otx2_write64(pf, NIX_LF_MNQ_ERR_DBG, BIT_ULL(44));
1317 snd_err_dbg = otx2_read64(pf, NIX_LF_SEND_ERR_DBG);
1318 if (snd_err_dbg & BIT(44)) {
1319 snd_err_code = FIELD_GET(GENMASK(7, 0), snd_err_dbg);
1320 netdev_err(pf->netdev,
1321 "SQ%lld: NIX_LF_SND_ERR_DBG:0x%llx err=%s(%#x)\n",
1323 nix_snd_status_e_str[snd_err_code],
1325 otx2_write64(pf, NIX_LF_SEND_ERR_DBG, BIT_ULL(44));
1329 /* Print values and reset */
1330 if (val & BIT_ULL(NIX_SQINT_SQB_ALLOC_FAIL))
1331 netdev_err(pf->netdev, "SQ%lld: SQB allocation failed",
1334 schedule_work(&pf->reset_task);
1340 static irqreturn_t otx2_cq_intr_handler(int irq, void *cq_irq)
1342 struct otx2_cq_poll *cq_poll = (struct otx2_cq_poll *)cq_irq;
1343 struct otx2_nic *pf = (struct otx2_nic *)cq_poll->dev;
1344 int qidx = cq_poll->cint_idx;
1346 /* Disable interrupts.
1348 * Completion interrupts behave in a level-triggered interrupt
1349 * fashion, and hence have to be cleared only after it is serviced.
1351 otx2_write64(pf, NIX_LF_CINTX_ENA_W1C(qidx), BIT_ULL(0));
1355 napi_schedule_irqoff(&cq_poll->napi);
1360 static void otx2_disable_napi(struct otx2_nic *pf)
1362 struct otx2_qset *qset = &pf->qset;
1363 struct otx2_cq_poll *cq_poll;
1366 for (qidx = 0; qidx < pf->hw.cint_cnt; qidx++) {
1367 cq_poll = &qset->napi[qidx];
1368 cancel_work_sync(&cq_poll->dim.work);
1369 napi_disable(&cq_poll->napi);
1370 netif_napi_del(&cq_poll->napi);
1374 static void otx2_free_cq_res(struct otx2_nic *pf)
1376 struct otx2_qset *qset = &pf->qset;
1377 struct otx2_cq_queue *cq;
1381 otx2_ctx_disable(&pf->mbox, NIX_AQ_CTYPE_CQ, false);
1382 for (qidx = 0; qidx < qset->cq_cnt; qidx++) {
1383 cq = &qset->cq[qidx];
1384 qmem_free(pf->dev, cq->cqe);
1388 static void otx2_free_sq_res(struct otx2_nic *pf)
1390 struct otx2_qset *qset = &pf->qset;
1391 struct otx2_snd_queue *sq;
1395 otx2_ctx_disable(&pf->mbox, NIX_AQ_CTYPE_SQ, false);
1396 /* Free SQB pointers */
1397 otx2_sq_free_sqbs(pf);
1398 for (qidx = 0; qidx < otx2_get_total_tx_queues(pf); qidx++) {
1399 sq = &qset->sq[qidx];
1400 /* Skip freeing Qos queues if they are not initialized */
1403 qmem_free(pf->dev, sq->sqe);
1404 qmem_free(pf->dev, sq->tso_hdrs);
1406 kfree(sq->sqb_ptrs);
1410 static int otx2_get_rbuf_size(struct otx2_nic *pf, int mtu)
1416 if (pf->hw.rbuf_len)
1417 return ALIGN(pf->hw.rbuf_len, OTX2_ALIGN) + OTX2_HEAD_ROOM;
1419 /* The data transferred by NIX to memory consists of actual packet
1420 * plus additional data which has timestamp and/or EDSA/HIGIG2
1421 * headers if interface is configured in corresponding modes.
1422 * NIX transfers entire data using 6 segments/buffers and writes
1423 * a CQE_RX descriptor with those segment addresses. First segment
1424 * has additional data prepended to packet. Also software omits a
1425 * headroom of 128 bytes in each segment. Hence the total size of
1426 * memory needed to receive a packet with 'mtu' is:
1427 * frame size = mtu + additional data;
1428 * memory = frame_size + headroom * 6;
1429 * each receive buffer size = memory / 6;
1431 frame_size = mtu + OTX2_ETH_HLEN + OTX2_HW_TIMESTAMP_LEN;
1432 total_size = frame_size + OTX2_HEAD_ROOM * 6;
1433 rbuf_size = total_size / 6;
1435 return ALIGN(rbuf_size, 2048);
1438 static int otx2_init_hw_resources(struct otx2_nic *pf)
1440 struct nix_lf_free_req *free_req;
1441 struct mbox *mbox = &pf->mbox;
1442 struct otx2_hw *hw = &pf->hw;
1443 struct msg_req *req;
1446 /* Set required NPA LF's pool counts
1447 * Auras and Pools are used in a 1:1 mapping,
1448 * so, aura count = pool count.
1450 hw->rqpool_cnt = hw->rx_queues;
1451 hw->sqpool_cnt = otx2_get_total_tx_queues(pf);
1452 hw->pool_cnt = hw->rqpool_cnt + hw->sqpool_cnt;
1454 /* Maximum hardware supported transmit length */
1455 pf->tx_max_pktlen = pf->netdev->max_mtu + OTX2_ETH_HLEN;
1457 pf->rbsize = otx2_get_rbuf_size(pf, pf->netdev->mtu);
1459 mutex_lock(&mbox->lock);
1461 err = otx2_config_npa(pf);
1466 err = otx2_config_nix(pf);
1468 goto err_free_npa_lf;
1470 /* Enable backpressure for CGX mapped PF/VFs */
1471 if (!is_otx2_lbkvf(pf->pdev))
1472 otx2_nix_config_bp(pf, true);
1474 /* Init Auras and pools used by NIX RQ, for free buffer ptrs */
1475 err = otx2_rq_aura_pool_init(pf);
1477 mutex_unlock(&mbox->lock);
1478 goto err_free_nix_lf;
1480 /* Init Auras and pools used by NIX SQ, for queueing SQEs */
1481 err = otx2_sq_aura_pool_init(pf);
1483 mutex_unlock(&mbox->lock);
1484 goto err_free_rq_ptrs;
1487 err = otx2_txsch_alloc(pf);
1489 mutex_unlock(&mbox->lock);
1490 goto err_free_sq_ptrs;
1495 err = otx2_pfc_txschq_alloc(pf);
1497 mutex_unlock(&mbox->lock);
1498 goto err_free_sq_ptrs;
1503 err = otx2_config_nix_queues(pf);
1505 mutex_unlock(&mbox->lock);
1506 goto err_free_txsch;
1509 for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
1510 err = otx2_txschq_config(pf, lvl, 0, false);
1512 mutex_unlock(&mbox->lock);
1513 goto err_free_nix_queues;
1519 err = otx2_pfc_txschq_config(pf);
1521 mutex_unlock(&mbox->lock);
1522 goto err_free_nix_queues;
1527 mutex_unlock(&mbox->lock);
1530 err_free_nix_queues:
1531 otx2_free_sq_res(pf);
1532 otx2_free_cq_res(pf);
1533 otx2_ctx_disable(mbox, NIX_AQ_CTYPE_RQ, false);
1535 otx2_txschq_stop(pf);
1537 otx2_sq_free_sqbs(pf);
1539 otx2_free_aura_ptr(pf, AURA_NIX_RQ);
1540 otx2_ctx_disable(mbox, NPA_AQ_CTYPE_POOL, true);
1541 otx2_ctx_disable(mbox, NPA_AQ_CTYPE_AURA, true);
1542 otx2_aura_pool_free(pf);
1544 mutex_lock(&mbox->lock);
1545 free_req = otx2_mbox_alloc_msg_nix_lf_free(mbox);
1547 free_req->flags = NIX_LF_DISABLE_FLOWS;
1548 if (otx2_sync_mbox_msg(mbox))
1549 dev_err(pf->dev, "%s failed to free nixlf\n", __func__);
1553 req = otx2_mbox_alloc_msg_npa_lf_free(mbox);
1555 if (otx2_sync_mbox_msg(mbox))
1556 dev_err(pf->dev, "%s failed to free npalf\n", __func__);
1559 mutex_unlock(&mbox->lock);
1563 static void otx2_free_hw_resources(struct otx2_nic *pf)
1565 struct otx2_qset *qset = &pf->qset;
1566 struct nix_lf_free_req *free_req;
1567 struct mbox *mbox = &pf->mbox;
1568 struct otx2_cq_queue *cq;
1569 struct otx2_pool *pool;
1570 struct msg_req *req;
1574 /* Ensure all SQE are processed */
1577 /* Stop transmission */
1578 otx2_txschq_stop(pf);
1582 otx2_pfc_txschq_stop(pf);
1585 otx2_clean_qos_queues(pf);
1587 mutex_lock(&mbox->lock);
1588 /* Disable backpressure */
1589 if (!(pf->pcifunc & RVU_PFVF_FUNC_MASK))
1590 otx2_nix_config_bp(pf, false);
1591 mutex_unlock(&mbox->lock);
1594 otx2_ctx_disable(mbox, NIX_AQ_CTYPE_RQ, false);
1596 /*Dequeue all CQEs */
1597 for (qidx = 0; qidx < qset->cq_cnt; qidx++) {
1598 cq = &qset->cq[qidx];
1599 if (cq->cq_type == CQ_RX)
1600 otx2_cleanup_rx_cqes(pf, cq, qidx);
1602 otx2_cleanup_tx_cqes(pf, cq);
1604 otx2_free_pending_sqe(pf);
1606 otx2_free_sq_res(pf);
1608 /* Free RQ buffer pointers*/
1609 otx2_free_aura_ptr(pf, AURA_NIX_RQ);
1611 for (qidx = 0; qidx < pf->hw.rx_queues; qidx++) {
1612 pool_id = otx2_get_pool_idx(pf, AURA_NIX_RQ, qidx);
1613 pool = &pf->qset.pool[pool_id];
1614 page_pool_destroy(pool->page_pool);
1615 pool->page_pool = NULL;
1618 otx2_free_cq_res(pf);
1620 /* Free all ingress bandwidth profiles allocated */
1621 cn10k_free_all_ipolicers(pf);
1623 mutex_lock(&mbox->lock);
1625 free_req = otx2_mbox_alloc_msg_nix_lf_free(mbox);
1627 free_req->flags = NIX_LF_DISABLE_FLOWS;
1628 if (!(pf->flags & OTX2_FLAG_PF_SHUTDOWN))
1629 free_req->flags |= NIX_LF_DONT_FREE_TX_VTAG;
1630 if (otx2_sync_mbox_msg(mbox))
1631 dev_err(pf->dev, "%s failed to free nixlf\n", __func__);
1633 mutex_unlock(&mbox->lock);
1635 /* Disable NPA Pool and Aura hw context */
1636 otx2_ctx_disable(mbox, NPA_AQ_CTYPE_POOL, true);
1637 otx2_ctx_disable(mbox, NPA_AQ_CTYPE_AURA, true);
1638 otx2_aura_pool_free(pf);
1640 mutex_lock(&mbox->lock);
1642 req = otx2_mbox_alloc_msg_npa_lf_free(mbox);
1644 if (otx2_sync_mbox_msg(mbox))
1645 dev_err(pf->dev, "%s failed to free npalf\n", __func__);
1647 mutex_unlock(&mbox->lock);
1650 static void otx2_do_set_rx_mode(struct otx2_nic *pf)
1652 struct net_device *netdev = pf->netdev;
1653 struct nix_rx_mode *req;
1654 bool promisc = false;
1656 if (!(netdev->flags & IFF_UP))
1659 if ((netdev->flags & IFF_PROMISC) ||
1660 (netdev_uc_count(netdev) > OTX2_MAX_UNICAST_FLOWS)) {
1664 /* Write unicast address to mcam entries or del from mcam */
1665 if (!promisc && netdev->priv_flags & IFF_UNICAST_FLT)
1666 __dev_uc_sync(netdev, otx2_add_macfilter, otx2_del_macfilter);
1668 mutex_lock(&pf->mbox.lock);
1669 req = otx2_mbox_alloc_msg_nix_set_rx_mode(&pf->mbox);
1671 mutex_unlock(&pf->mbox.lock);
1675 req->mode = NIX_RX_MODE_UCAST;
1678 req->mode |= NIX_RX_MODE_PROMISC;
1679 if (netdev->flags & (IFF_ALLMULTI | IFF_MULTICAST))
1680 req->mode |= NIX_RX_MODE_ALLMULTI;
1682 req->mode |= NIX_RX_MODE_USE_MCE;
1684 otx2_sync_mbox_msg(&pf->mbox);
1685 mutex_unlock(&pf->mbox.lock);
1688 static void otx2_dim_work(struct work_struct *w)
1690 struct dim_cq_moder cur_moder;
1691 struct otx2_cq_poll *cq_poll;
1692 struct otx2_nic *pfvf;
1695 dim = container_of(w, struct dim, work);
1696 cur_moder = net_dim_get_rx_moderation(dim->mode, dim->profile_ix);
1697 cq_poll = container_of(dim, struct otx2_cq_poll, dim);
1698 pfvf = (struct otx2_nic *)cq_poll->dev;
1699 pfvf->hw.cq_time_wait = (cur_moder.usec > CQ_TIMER_THRESH_MAX) ?
1700 CQ_TIMER_THRESH_MAX : cur_moder.usec;
1701 pfvf->hw.cq_ecount_wait = (cur_moder.pkts > NAPI_POLL_WEIGHT) ?
1702 NAPI_POLL_WEIGHT : cur_moder.pkts;
1703 dim->state = DIM_START_MEASURE;
1706 int otx2_open(struct net_device *netdev)
1708 struct otx2_nic *pf = netdev_priv(netdev);
1709 struct otx2_cq_poll *cq_poll = NULL;
1710 struct otx2_qset *qset = &pf->qset;
1711 int err = 0, qidx, vec;
1714 netif_carrier_off(netdev);
1716 /* RQ and SQs are mapped to different CQs,
1717 * so find out max CQ IRQs (i.e CINTs) needed.
1719 pf->hw.cint_cnt = max3(pf->hw.rx_queues, pf->hw.tx_queues,
1720 pf->hw.tc_tx_queues);
1722 pf->qset.cq_cnt = pf->hw.rx_queues + otx2_get_total_tx_queues(pf);
1724 qset->napi = kcalloc(pf->hw.cint_cnt, sizeof(*cq_poll), GFP_KERNEL);
1729 qset->rqe_cnt = qset->rqe_cnt ? qset->rqe_cnt : Q_COUNT(Q_SIZE_256);
1731 qset->sqe_cnt = qset->sqe_cnt ? qset->sqe_cnt : Q_COUNT(Q_SIZE_4K);
1734 qset->cq = kcalloc(pf->qset.cq_cnt,
1735 sizeof(struct otx2_cq_queue), GFP_KERNEL);
1739 qset->sq = kcalloc(otx2_get_total_tx_queues(pf),
1740 sizeof(struct otx2_snd_queue), GFP_KERNEL);
1744 qset->rq = kcalloc(pf->hw.rx_queues,
1745 sizeof(struct otx2_rcv_queue), GFP_KERNEL);
1749 err = otx2_init_hw_resources(pf);
1753 /* Register NAPI handler */
1754 for (qidx = 0; qidx < pf->hw.cint_cnt; qidx++) {
1755 cq_poll = &qset->napi[qidx];
1756 cq_poll->cint_idx = qidx;
1757 /* RQ0 & SQ0 are mapped to CINT0 and so on..
1758 * 'cq_ids[0]' points to RQ's CQ and
1759 * 'cq_ids[1]' points to SQ's CQ and
1760 * 'cq_ids[2]' points to XDP's CQ and
1762 cq_poll->cq_ids[CQ_RX] =
1763 (qidx < pf->hw.rx_queues) ? qidx : CINT_INVALID_CQ;
1764 cq_poll->cq_ids[CQ_TX] = (qidx < pf->hw.tx_queues) ?
1765 qidx + pf->hw.rx_queues : CINT_INVALID_CQ;
1767 cq_poll->cq_ids[CQ_XDP] = (qidx < pf->hw.xdp_queues) ?
1768 (qidx + pf->hw.rx_queues +
1772 cq_poll->cq_ids[CQ_XDP] = CINT_INVALID_CQ;
1774 cq_poll->cq_ids[CQ_QOS] = (qidx < pf->hw.tc_tx_queues) ?
1775 (qidx + pf->hw.rx_queues +
1776 pf->hw.non_qos_queues) :
1779 cq_poll->dev = (void *)pf;
1780 cq_poll->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_CQE;
1781 INIT_WORK(&cq_poll->dim.work, otx2_dim_work);
1782 netif_napi_add(netdev, &cq_poll->napi, otx2_napi_handler);
1783 napi_enable(&cq_poll->napi);
1786 /* Set maximum frame size allowed in HW */
1787 err = otx2_hw_set_mtu(pf, netdev->mtu);
1789 goto err_disable_napi;
1791 /* Setup segmentation algorithms, if failed, clear offload capability */
1792 otx2_setup_segmentation(pf);
1794 /* Initialize RSS */
1795 err = otx2_rss_init(pf);
1797 goto err_disable_napi;
1799 /* Register Queue IRQ handlers */
1800 vec = pf->hw.nix_msixoff + NIX_LF_QINT_VEC_START;
1801 irq_name = &pf->hw.irq_name[vec * NAME_SIZE];
1803 snprintf(irq_name, NAME_SIZE, "%s-qerr", pf->netdev->name);
1805 err = request_irq(pci_irq_vector(pf->pdev, vec),
1806 otx2_q_intr_handler, 0, irq_name, pf);
1809 "RVUPF%d: IRQ registration failed for QERR\n",
1810 rvu_get_pf(pf->pcifunc));
1811 goto err_disable_napi;
1814 /* Enable QINT IRQ */
1815 otx2_write64(pf, NIX_LF_QINTX_ENA_W1S(0), BIT_ULL(0));
1817 /* Register CQ IRQ handlers */
1818 vec = pf->hw.nix_msixoff + NIX_LF_CINT_VEC_START;
1819 for (qidx = 0; qidx < pf->hw.cint_cnt; qidx++) {
1820 irq_name = &pf->hw.irq_name[vec * NAME_SIZE];
1822 snprintf(irq_name, NAME_SIZE, "%s-rxtx-%d", pf->netdev->name,
1825 err = request_irq(pci_irq_vector(pf->pdev, vec),
1826 otx2_cq_intr_handler, 0, irq_name,
1830 "RVUPF%d: IRQ registration failed for CQ%d\n",
1831 rvu_get_pf(pf->pcifunc), qidx);
1832 goto err_free_cints;
1836 otx2_config_irq_coalescing(pf, qidx);
1839 otx2_write64(pf, NIX_LF_CINTX_INT(qidx), BIT_ULL(0));
1840 otx2_write64(pf, NIX_LF_CINTX_ENA_W1S(qidx), BIT_ULL(0));
1843 otx2_set_cints_affinity(pf);
1845 if (pf->flags & OTX2_FLAG_RX_VLAN_SUPPORT)
1846 otx2_enable_rxvlan(pf, true);
1848 /* When reinitializing enable time stamping if it is enabled before */
1849 if (pf->flags & OTX2_FLAG_TX_TSTAMP_ENABLED) {
1850 pf->flags &= ~OTX2_FLAG_TX_TSTAMP_ENABLED;
1851 otx2_config_hw_tx_tstamp(pf, true);
1853 if (pf->flags & OTX2_FLAG_RX_TSTAMP_ENABLED) {
1854 pf->flags &= ~OTX2_FLAG_RX_TSTAMP_ENABLED;
1855 otx2_config_hw_rx_tstamp(pf, true);
1858 pf->flags &= ~OTX2_FLAG_INTF_DOWN;
1859 /* 'intf_down' may be checked on any cpu */
1862 /* Enable QoS configuration before starting tx queues */
1863 otx2_qos_config_txschq(pf);
1865 /* we have already received link status notification */
1866 if (pf->linfo.link_up && !(pf->pcifunc & RVU_PFVF_FUNC_MASK))
1867 otx2_handle_link_event(pf);
1869 /* Install DMAC Filters */
1870 if (pf->flags & OTX2_FLAG_DMACFLTR_SUPPORT)
1871 otx2_dmacflt_reinstall_flows(pf);
1873 err = otx2_rxtx_enable(pf, true);
1874 /* If a mbox communication error happens at this point then interface
1875 * will end up in a state such that it is in down state but hardware
1876 * mcam entries are enabled to receive the packets. Hence disable the
1880 goto err_disable_rxtx;
1882 goto err_tx_stop_queues;
1884 otx2_do_set_rx_mode(pf);
1889 otx2_rxtx_enable(pf, false);
1891 netif_tx_stop_all_queues(netdev);
1892 netif_carrier_off(netdev);
1893 pf->flags |= OTX2_FLAG_INTF_DOWN;
1895 otx2_free_cints(pf, qidx);
1896 vec = pci_irq_vector(pf->pdev,
1897 pf->hw.nix_msixoff + NIX_LF_QINT_VEC_START);
1898 otx2_write64(pf, NIX_LF_QINTX_ENA_W1C(0), BIT_ULL(0));
1901 otx2_disable_napi(pf);
1902 otx2_free_hw_resources(pf);
1910 EXPORT_SYMBOL(otx2_open);
1912 int otx2_stop(struct net_device *netdev)
1914 struct otx2_nic *pf = netdev_priv(netdev);
1915 struct otx2_cq_poll *cq_poll = NULL;
1916 struct otx2_qset *qset = &pf->qset;
1917 struct otx2_rss_info *rss;
1920 /* If the DOWN flag is set resources are already freed */
1921 if (pf->flags & OTX2_FLAG_INTF_DOWN)
1924 netif_carrier_off(netdev);
1925 netif_tx_stop_all_queues(netdev);
1927 pf->flags |= OTX2_FLAG_INTF_DOWN;
1928 /* 'intf_down' may be checked on any cpu */
1931 /* First stop packet Rx/Tx */
1932 otx2_rxtx_enable(pf, false);
1934 /* Clear RSS enable flag */
1935 rss = &pf->hw.rss_info;
1936 rss->enable = false;
1938 /* Cleanup Queue IRQ */
1939 vec = pci_irq_vector(pf->pdev,
1940 pf->hw.nix_msixoff + NIX_LF_QINT_VEC_START);
1941 otx2_write64(pf, NIX_LF_QINTX_ENA_W1C(0), BIT_ULL(0));
1944 /* Cleanup CQ NAPI and IRQ */
1945 vec = pf->hw.nix_msixoff + NIX_LF_CINT_VEC_START;
1946 for (qidx = 0; qidx < pf->hw.cint_cnt; qidx++) {
1947 /* Disable interrupt */
1948 otx2_write64(pf, NIX_LF_CINTX_ENA_W1C(qidx), BIT_ULL(0));
1950 synchronize_irq(pci_irq_vector(pf->pdev, vec));
1952 cq_poll = &qset->napi[qidx];
1953 napi_synchronize(&cq_poll->napi);
1957 netif_tx_disable(netdev);
1959 for (wrk = 0; wrk < pf->qset.cq_cnt; wrk++)
1960 cancel_delayed_work_sync(&pf->refill_wrk[wrk].pool_refill_work);
1961 devm_kfree(pf->dev, pf->refill_wrk);
1963 otx2_free_hw_resources(pf);
1964 otx2_free_cints(pf, pf->hw.cint_cnt);
1965 otx2_disable_napi(pf);
1967 for (qidx = 0; qidx < netdev->num_tx_queues; qidx++)
1968 netdev_tx_reset_queue(netdev_get_tx_queue(netdev, qidx));
1975 /* Do not clear RQ/SQ ringsize settings */
1976 memset_startat(qset, 0, sqe_cnt);
1979 EXPORT_SYMBOL(otx2_stop);
1981 static netdev_tx_t otx2_xmit(struct sk_buff *skb, struct net_device *netdev)
1983 struct otx2_nic *pf = netdev_priv(netdev);
1984 int qidx = skb_get_queue_mapping(skb);
1985 struct otx2_snd_queue *sq;
1986 struct netdev_queue *txq;
1989 /* XDP SQs are not mapped with TXQs
1990 * advance qid to derive correct sq mapped with QOS
1992 sq_idx = (qidx >= pf->hw.tx_queues) ? (qidx + pf->hw.xdp_queues) : qidx;
1994 /* Check for minimum and maximum packet length */
1995 if (skb->len <= ETH_HLEN ||
1996 (!skb_shinfo(skb)->gso_size && skb->len > pf->tx_max_pktlen)) {
1998 return NETDEV_TX_OK;
2001 sq = &pf->qset.sq[sq_idx];
2002 txq = netdev_get_tx_queue(netdev, qidx);
2004 if (!otx2_sq_append_skb(netdev, sq, skb, qidx)) {
2005 netif_tx_stop_queue(txq);
2007 /* Check again, incase SQBs got freed up */
2009 if (((sq->num_sqbs - *sq->aura_fc_addr) * sq->sqe_per_sqb)
2011 netif_tx_wake_queue(txq);
2013 return NETDEV_TX_BUSY;
2016 return NETDEV_TX_OK;
2019 static int otx2_qos_select_htb_queue(struct otx2_nic *pf, struct sk_buff *skb,
2024 if ((TC_H_MAJ(skb->priority) >> 16) == htb_maj_id)
2025 classid = TC_H_MIN(skb->priority);
2027 classid = READ_ONCE(pf->qos.defcls);
2032 return otx2_get_txq_by_classid(pf, classid);
2035 u16 otx2_select_queue(struct net_device *netdev, struct sk_buff *skb,
2036 struct net_device *sb_dev)
2038 struct otx2_nic *pf = netdev_priv(netdev);
2045 qos_enabled = netdev->real_num_tx_queues > pf->hw.tx_queues;
2046 if (unlikely(qos_enabled)) {
2047 /* This smp_load_acquire() pairs with smp_store_release() in
2048 * otx2_qos_root_add() called from htb offload root creation
2050 u16 htb_maj_id = smp_load_acquire(&pf->qos.maj_id);
2052 if (unlikely(htb_maj_id)) {
2053 txq = otx2_qos_select_htb_queue(pf, skb, htb_maj_id);
2062 if (!skb_vlan_tag_present(skb))
2065 vlan_prio = skb->vlan_tci >> 13;
2066 if ((vlan_prio > pf->hw.tx_queues - 1) ||
2067 !pf->pfc_alloc_status[vlan_prio])
2074 txq = netdev_pick_tx(netdev, skb, NULL);
2075 if (unlikely(qos_enabled))
2076 return txq % pf->hw.tx_queues;
2080 EXPORT_SYMBOL(otx2_select_queue);
2082 static netdev_features_t otx2_fix_features(struct net_device *dev,
2083 netdev_features_t features)
2085 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2086 features |= NETIF_F_HW_VLAN_STAG_RX;
2088 features &= ~NETIF_F_HW_VLAN_STAG_RX;
2093 static void otx2_set_rx_mode(struct net_device *netdev)
2095 struct otx2_nic *pf = netdev_priv(netdev);
2097 queue_work(pf->otx2_wq, &pf->rx_mode_work);
2100 static void otx2_rx_mode_wrk_handler(struct work_struct *work)
2102 struct otx2_nic *pf = container_of(work, struct otx2_nic, rx_mode_work);
2104 otx2_do_set_rx_mode(pf);
2107 static int otx2_set_features(struct net_device *netdev,
2108 netdev_features_t features)
2110 netdev_features_t changed = features ^ netdev->features;
2111 struct otx2_nic *pf = netdev_priv(netdev);
2113 if ((changed & NETIF_F_LOOPBACK) && netif_running(netdev))
2114 return otx2_cgx_config_loopback(pf,
2115 features & NETIF_F_LOOPBACK);
2117 if ((changed & NETIF_F_HW_VLAN_CTAG_RX) && netif_running(netdev))
2118 return otx2_enable_rxvlan(pf,
2119 features & NETIF_F_HW_VLAN_CTAG_RX);
2121 return otx2_handle_ntuple_tc_features(netdev, features);
2124 static void otx2_reset_task(struct work_struct *work)
2126 struct otx2_nic *pf = container_of(work, struct otx2_nic, reset_task);
2128 if (!netif_running(pf->netdev))
2132 otx2_stop(pf->netdev);
2134 otx2_open(pf->netdev);
2135 netif_trans_update(pf->netdev);
2139 static int otx2_config_hw_rx_tstamp(struct otx2_nic *pfvf, bool enable)
2141 struct msg_req *req;
2144 if (pfvf->flags & OTX2_FLAG_RX_TSTAMP_ENABLED && enable)
2147 mutex_lock(&pfvf->mbox.lock);
2149 req = otx2_mbox_alloc_msg_cgx_ptp_rx_enable(&pfvf->mbox);
2151 req = otx2_mbox_alloc_msg_cgx_ptp_rx_disable(&pfvf->mbox);
2153 mutex_unlock(&pfvf->mbox.lock);
2157 err = otx2_sync_mbox_msg(&pfvf->mbox);
2159 mutex_unlock(&pfvf->mbox.lock);
2163 mutex_unlock(&pfvf->mbox.lock);
2165 pfvf->flags |= OTX2_FLAG_RX_TSTAMP_ENABLED;
2167 pfvf->flags &= ~OTX2_FLAG_RX_TSTAMP_ENABLED;
2171 static int otx2_config_hw_tx_tstamp(struct otx2_nic *pfvf, bool enable)
2173 struct msg_req *req;
2176 if (pfvf->flags & OTX2_FLAG_TX_TSTAMP_ENABLED && enable)
2179 mutex_lock(&pfvf->mbox.lock);
2181 req = otx2_mbox_alloc_msg_nix_lf_ptp_tx_enable(&pfvf->mbox);
2183 req = otx2_mbox_alloc_msg_nix_lf_ptp_tx_disable(&pfvf->mbox);
2185 mutex_unlock(&pfvf->mbox.lock);
2189 err = otx2_sync_mbox_msg(&pfvf->mbox);
2191 mutex_unlock(&pfvf->mbox.lock);
2195 mutex_unlock(&pfvf->mbox.lock);
2197 pfvf->flags |= OTX2_FLAG_TX_TSTAMP_ENABLED;
2199 pfvf->flags &= ~OTX2_FLAG_TX_TSTAMP_ENABLED;
2203 int otx2_config_hwtstamp(struct net_device *netdev, struct ifreq *ifr)
2205 struct otx2_nic *pfvf = netdev_priv(netdev);
2206 struct hwtstamp_config config;
2211 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
2214 switch (config.tx_type) {
2215 case HWTSTAMP_TX_OFF:
2216 if (pfvf->flags & OTX2_FLAG_PTP_ONESTEP_SYNC)
2217 pfvf->flags &= ~OTX2_FLAG_PTP_ONESTEP_SYNC;
2219 cancel_delayed_work(&pfvf->ptp->synctstamp_work);
2220 otx2_config_hw_tx_tstamp(pfvf, false);
2222 case HWTSTAMP_TX_ONESTEP_SYNC:
2223 if (!test_bit(CN10K_PTP_ONESTEP, &pfvf->hw.cap_flag))
2225 pfvf->flags |= OTX2_FLAG_PTP_ONESTEP_SYNC;
2226 schedule_delayed_work(&pfvf->ptp->synctstamp_work,
2227 msecs_to_jiffies(500));
2229 case HWTSTAMP_TX_ON:
2230 otx2_config_hw_tx_tstamp(pfvf, true);
2236 switch (config.rx_filter) {
2237 case HWTSTAMP_FILTER_NONE:
2238 otx2_config_hw_rx_tstamp(pfvf, false);
2240 case HWTSTAMP_FILTER_ALL:
2241 case HWTSTAMP_FILTER_SOME:
2242 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
2243 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
2244 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2245 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2246 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2247 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2248 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
2249 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
2250 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
2251 case HWTSTAMP_FILTER_PTP_V2_EVENT:
2252 case HWTSTAMP_FILTER_PTP_V2_SYNC:
2253 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
2254 otx2_config_hw_rx_tstamp(pfvf, true);
2255 config.rx_filter = HWTSTAMP_FILTER_ALL;
2261 memcpy(&pfvf->tstamp, &config, sizeof(config));
2263 return copy_to_user(ifr->ifr_data, &config,
2264 sizeof(config)) ? -EFAULT : 0;
2266 EXPORT_SYMBOL(otx2_config_hwtstamp);
2268 int otx2_ioctl(struct net_device *netdev, struct ifreq *req, int cmd)
2270 struct otx2_nic *pfvf = netdev_priv(netdev);
2271 struct hwtstamp_config *cfg = &pfvf->tstamp;
2275 return otx2_config_hwtstamp(netdev, req);
2277 return copy_to_user(req->ifr_data, cfg,
2278 sizeof(*cfg)) ? -EFAULT : 0;
2283 EXPORT_SYMBOL(otx2_ioctl);
2285 static int otx2_do_set_vf_mac(struct otx2_nic *pf, int vf, const u8 *mac)
2287 struct npc_install_flow_req *req;
2290 mutex_lock(&pf->mbox.lock);
2291 req = otx2_mbox_alloc_msg_npc_install_flow(&pf->mbox);
2297 ether_addr_copy(req->packet.dmac, mac);
2298 eth_broadcast_addr((u8 *)&req->mask.dmac);
2299 req->features = BIT_ULL(NPC_DMAC);
2300 req->channel = pf->hw.rx_chan_base;
2301 req->intf = NIX_INTF_RX;
2302 req->default_rule = 1;
2305 req->op = NIX_RX_ACTION_DEFAULT;
2307 err = otx2_sync_mbox_msg(&pf->mbox);
2309 mutex_unlock(&pf->mbox.lock);
2313 static int otx2_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
2315 struct otx2_nic *pf = netdev_priv(netdev);
2316 struct pci_dev *pdev = pf->pdev;
2317 struct otx2_vf_config *config;
2320 if (!netif_running(netdev))
2323 if (vf >= pf->total_vfs)
2326 if (!is_valid_ether_addr(mac))
2329 config = &pf->vf_configs[vf];
2330 ether_addr_copy(config->mac, mac);
2332 ret = otx2_do_set_vf_mac(pf, vf, mac);
2334 dev_info(&pdev->dev,
2335 "Load/Reload VF driver\n");
2340 static int otx2_do_set_vf_vlan(struct otx2_nic *pf, int vf, u16 vlan, u8 qos,
2343 struct otx2_flow_config *flow_cfg = pf->flow_cfg;
2344 struct nix_vtag_config_rsp *vtag_rsp;
2345 struct npc_delete_flow_req *del_req;
2346 struct nix_vtag_config *vtag_req;
2347 struct npc_install_flow_req *req;
2348 struct otx2_vf_config *config;
2352 config = &pf->vf_configs[vf];
2354 if (!vlan && !config->vlan)
2357 mutex_lock(&pf->mbox.lock);
2359 /* free old tx vtag entry */
2361 vtag_req = otx2_mbox_alloc_msg_nix_vtag_cfg(&pf->mbox);
2366 vtag_req->cfg_type = 0;
2367 vtag_req->tx.free_vtag0 = 1;
2368 vtag_req->tx.vtag0_idx = config->tx_vtag_idx;
2370 err = otx2_sync_mbox_msg(&pf->mbox);
2375 if (!vlan && config->vlan) {
2377 del_req = otx2_mbox_alloc_msg_npc_delete_flow(&pf->mbox);
2382 idx = ((vf * OTX2_PER_VF_VLAN_FLOWS) + OTX2_VF_VLAN_RX_INDEX);
2384 flow_cfg->def_ent[flow_cfg->vf_vlan_offset + idx];
2385 err = otx2_sync_mbox_msg(&pf->mbox);
2390 del_req = otx2_mbox_alloc_msg_npc_delete_flow(&pf->mbox);
2395 idx = ((vf * OTX2_PER_VF_VLAN_FLOWS) + OTX2_VF_VLAN_TX_INDEX);
2397 flow_cfg->def_ent[flow_cfg->vf_vlan_offset + idx];
2398 err = otx2_sync_mbox_msg(&pf->mbox);
2404 req = otx2_mbox_alloc_msg_npc_install_flow(&pf->mbox);
2410 idx = ((vf * OTX2_PER_VF_VLAN_FLOWS) + OTX2_VF_VLAN_RX_INDEX);
2411 req->entry = flow_cfg->def_ent[flow_cfg->vf_vlan_offset + idx];
2412 req->packet.vlan_tci = htons(vlan);
2413 req->mask.vlan_tci = htons(VLAN_VID_MASK);
2414 /* af fills the destination mac addr */
2415 eth_broadcast_addr((u8 *)&req->mask.dmac);
2416 req->features = BIT_ULL(NPC_OUTER_VID) | BIT_ULL(NPC_DMAC);
2417 req->channel = pf->hw.rx_chan_base;
2418 req->intf = NIX_INTF_RX;
2420 req->op = NIX_RX_ACTION_DEFAULT;
2421 req->vtag0_valid = true;
2422 req->vtag0_type = NIX_AF_LFX_RX_VTAG_TYPE7;
2425 err = otx2_sync_mbox_msg(&pf->mbox);
2430 vtag_req = otx2_mbox_alloc_msg_nix_vtag_cfg(&pf->mbox);
2436 /* configure tx vtag params */
2437 vtag_req->vtag_size = VTAGSIZE_T4;
2438 vtag_req->cfg_type = 0; /* tx vlan cfg */
2439 vtag_req->tx.cfg_vtag0 = 1;
2440 vtag_req->tx.vtag0 = ((u64)ntohs(proto) << 16) | vlan;
2442 err = otx2_sync_mbox_msg(&pf->mbox);
2446 vtag_rsp = (struct nix_vtag_config_rsp *)otx2_mbox_get_rsp
2447 (&pf->mbox.mbox, 0, &vtag_req->hdr);
2448 if (IS_ERR(vtag_rsp)) {
2449 err = PTR_ERR(vtag_rsp);
2452 config->tx_vtag_idx = vtag_rsp->vtag0_idx;
2454 req = otx2_mbox_alloc_msg_npc_install_flow(&pf->mbox);
2460 eth_zero_addr((u8 *)&req->mask.dmac);
2461 idx = ((vf * OTX2_PER_VF_VLAN_FLOWS) + OTX2_VF_VLAN_TX_INDEX);
2462 req->entry = flow_cfg->def_ent[flow_cfg->vf_vlan_offset + idx];
2463 req->features = BIT_ULL(NPC_DMAC);
2464 req->channel = pf->hw.tx_chan_base;
2465 req->intf = NIX_INTF_TX;
2467 req->op = NIX_TX_ACTIONOP_UCAST_DEFAULT;
2468 req->vtag0_def = vtag_rsp->vtag0_idx;
2469 req->vtag0_op = VTAG_INSERT;
2472 err = otx2_sync_mbox_msg(&pf->mbox);
2474 config->vlan = vlan;
2475 mutex_unlock(&pf->mbox.lock);
2479 static int otx2_set_vf_vlan(struct net_device *netdev, int vf, u16 vlan, u8 qos,
2482 struct otx2_nic *pf = netdev_priv(netdev);
2483 struct pci_dev *pdev = pf->pdev;
2485 if (!netif_running(netdev))
2488 if (vf >= pci_num_vf(pdev))
2491 /* qos is currently unsupported */
2492 if (vlan >= VLAN_N_VID || qos)
2495 if (proto != htons(ETH_P_8021Q))
2496 return -EPROTONOSUPPORT;
2498 if (!(pf->flags & OTX2_FLAG_VF_VLAN_SUPPORT))
2501 return otx2_do_set_vf_vlan(pf, vf, vlan, qos, proto);
2504 static int otx2_get_vf_config(struct net_device *netdev, int vf,
2505 struct ifla_vf_info *ivi)
2507 struct otx2_nic *pf = netdev_priv(netdev);
2508 struct pci_dev *pdev = pf->pdev;
2509 struct otx2_vf_config *config;
2511 if (!netif_running(netdev))
2514 if (vf >= pci_num_vf(pdev))
2517 config = &pf->vf_configs[vf];
2519 ether_addr_copy(ivi->mac, config->mac);
2520 ivi->vlan = config->vlan;
2521 ivi->trusted = config->trusted;
2526 static int otx2_xdp_xmit_tx(struct otx2_nic *pf, struct xdp_frame *xdpf,
2533 dma_addr = otx2_dma_map_page(pf, virt_to_page(xdpf->data),
2534 offset_in_page(xdpf->data), xdpf->len,
2536 if (dma_mapping_error(pf->dev, dma_addr))
2539 err = otx2_xdp_sq_append_pkt(pf, dma_addr, xdpf->len, qidx);
2541 otx2_dma_unmap_page(pf, dma_addr, xdpf->len, DMA_TO_DEVICE);
2542 page = virt_to_page(xdpf->data);
2549 static int otx2_xdp_xmit(struct net_device *netdev, int n,
2550 struct xdp_frame **frames, u32 flags)
2552 struct otx2_nic *pf = netdev_priv(netdev);
2553 int qidx = smp_processor_id();
2554 struct otx2_snd_queue *sq;
2557 if (!netif_running(netdev))
2560 qidx += pf->hw.tx_queues;
2561 sq = pf->xdp_prog ? &pf->qset.sq[qidx] : NULL;
2563 /* Abort xmit if xdp queue is not */
2567 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
2570 for (i = 0; i < n; i++) {
2571 struct xdp_frame *xdpf = frames[i];
2574 err = otx2_xdp_xmit_tx(pf, xdpf, qidx);
2581 static int otx2_xdp_setup(struct otx2_nic *pf, struct bpf_prog *prog)
2583 struct net_device *dev = pf->netdev;
2584 bool if_up = netif_running(pf->netdev);
2585 struct bpf_prog *old_prog;
2587 if (prog && dev->mtu > MAX_XDP_MTU) {
2588 netdev_warn(dev, "Jumbo frames not yet supported with XDP\n");
2593 otx2_stop(pf->netdev);
2595 old_prog = xchg(&pf->xdp_prog, prog);
2598 bpf_prog_put(old_prog);
2601 bpf_prog_add(pf->xdp_prog, pf->hw.rx_queues - 1);
2603 /* Network stack and XDP shared same rx queues.
2604 * Use separate tx queues for XDP and network stack.
2607 pf->hw.xdp_queues = pf->hw.rx_queues;
2608 xdp_features_set_redirect_target(dev, false);
2610 pf->hw.xdp_queues = 0;
2611 xdp_features_clear_redirect_target(dev);
2614 pf->hw.non_qos_queues += pf->hw.xdp_queues;
2617 otx2_open(pf->netdev);
2622 static int otx2_xdp(struct net_device *netdev, struct netdev_bpf *xdp)
2624 struct otx2_nic *pf = netdev_priv(netdev);
2626 switch (xdp->command) {
2627 case XDP_SETUP_PROG:
2628 return otx2_xdp_setup(pf, xdp->prog);
2634 static int otx2_set_vf_permissions(struct otx2_nic *pf, int vf,
2637 struct set_vf_perm *req;
2640 mutex_lock(&pf->mbox.lock);
2641 req = otx2_mbox_alloc_msg_set_vf_perm(&pf->mbox);
2647 /* Let AF reset VF permissions as sriov is disabled */
2648 if (req_perm == OTX2_RESET_VF_PERM) {
2649 req->flags |= RESET_VF_PERM;
2650 } else if (req_perm == OTX2_TRUSTED_VF) {
2651 if (pf->vf_configs[vf].trusted)
2652 req->flags |= VF_TRUSTED;
2656 rc = otx2_sync_mbox_msg(&pf->mbox);
2658 mutex_unlock(&pf->mbox.lock);
2662 static int otx2_ndo_set_vf_trust(struct net_device *netdev, int vf,
2665 struct otx2_nic *pf = netdev_priv(netdev);
2666 struct pci_dev *pdev = pf->pdev;
2669 if (vf >= pci_num_vf(pdev))
2672 if (pf->vf_configs[vf].trusted == enable)
2675 pf->vf_configs[vf].trusted = enable;
2676 rc = otx2_set_vf_permissions(pf, vf, OTX2_TRUSTED_VF);
2679 pf->vf_configs[vf].trusted = !enable;
2681 netdev_info(pf->netdev, "VF %d is %strusted\n",
2682 vf, enable ? "" : "not ");
2686 static const struct net_device_ops otx2_netdev_ops = {
2687 .ndo_open = otx2_open,
2688 .ndo_stop = otx2_stop,
2689 .ndo_start_xmit = otx2_xmit,
2690 .ndo_select_queue = otx2_select_queue,
2691 .ndo_fix_features = otx2_fix_features,
2692 .ndo_set_mac_address = otx2_set_mac_address,
2693 .ndo_change_mtu = otx2_change_mtu,
2694 .ndo_set_rx_mode = otx2_set_rx_mode,
2695 .ndo_set_features = otx2_set_features,
2696 .ndo_tx_timeout = otx2_tx_timeout,
2697 .ndo_get_stats64 = otx2_get_stats64,
2698 .ndo_eth_ioctl = otx2_ioctl,
2699 .ndo_set_vf_mac = otx2_set_vf_mac,
2700 .ndo_set_vf_vlan = otx2_set_vf_vlan,
2701 .ndo_get_vf_config = otx2_get_vf_config,
2702 .ndo_bpf = otx2_xdp,
2703 .ndo_xdp_xmit = otx2_xdp_xmit,
2704 .ndo_setup_tc = otx2_setup_tc,
2705 .ndo_set_vf_trust = otx2_ndo_set_vf_trust,
2708 static int otx2_wq_init(struct otx2_nic *pf)
2710 pf->otx2_wq = create_singlethread_workqueue("otx2_wq");
2714 INIT_WORK(&pf->rx_mode_work, otx2_rx_mode_wrk_handler);
2715 INIT_WORK(&pf->reset_task, otx2_reset_task);
2719 static int otx2_check_pf_usable(struct otx2_nic *nic)
2723 rev = otx2_read64(nic, RVU_PF_BLOCK_ADDRX_DISC(BLKADDR_RVUM));
2724 rev = (rev >> 12) & 0xFF;
2725 /* Check if AF has setup revision for RVUM block,
2726 * otherwise this driver probe should be deferred
2727 * until AF driver comes up.
2731 "AF is not initialized, deferring probe\n");
2732 return -EPROBE_DEFER;
2737 static int otx2_realloc_msix_vectors(struct otx2_nic *pf)
2739 struct otx2_hw *hw = &pf->hw;
2742 /* NPA interrupts are inot registered, so alloc only
2743 * upto NIX vector offset.
2745 num_vec = hw->nix_msixoff;
2746 num_vec += NIX_LF_CINT_VEC_START + hw->max_queues;
2748 otx2_disable_mbox_intr(pf);
2749 pci_free_irq_vectors(hw->pdev);
2750 err = pci_alloc_irq_vectors(hw->pdev, num_vec, num_vec, PCI_IRQ_MSIX);
2752 dev_err(pf->dev, "%s: Failed to realloc %d IRQ vectors\n",
2757 return otx2_register_mbox_intr(pf, false);
2760 static int otx2_sriov_vfcfg_init(struct otx2_nic *pf)
2764 pf->vf_configs = devm_kcalloc(pf->dev, pf->total_vfs,
2765 sizeof(struct otx2_vf_config),
2767 if (!pf->vf_configs)
2770 for (i = 0; i < pf->total_vfs; i++) {
2771 pf->vf_configs[i].pf = pf;
2772 pf->vf_configs[i].intf_down = true;
2773 pf->vf_configs[i].trusted = false;
2774 INIT_DELAYED_WORK(&pf->vf_configs[i].link_event_work,
2775 otx2_vf_link_event_task);
2781 static void otx2_sriov_vfcfg_cleanup(struct otx2_nic *pf)
2785 if (!pf->vf_configs)
2788 for (i = 0; i < pf->total_vfs; i++) {
2789 cancel_delayed_work_sync(&pf->vf_configs[i].link_event_work);
2790 otx2_set_vf_permissions(pf, i, OTX2_RESET_VF_PERM);
2794 static int otx2_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2796 struct device *dev = &pdev->dev;
2797 int err, qcount, qos_txqs;
2798 struct net_device *netdev;
2799 struct otx2_nic *pf;
2803 err = pcim_enable_device(pdev);
2805 dev_err(dev, "Failed to enable PCI device\n");
2809 err = pci_request_regions(pdev, DRV_NAME);
2811 dev_err(dev, "PCI request regions failed 0x%x\n", err);
2815 err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48));
2817 dev_err(dev, "DMA mask config failed, abort\n");
2818 goto err_release_regions;
2821 pci_set_master(pdev);
2823 /* Set number of queues */
2824 qcount = min_t(int, num_online_cpus(), OTX2_MAX_CQ_CNT);
2825 qos_txqs = min_t(int, qcount, OTX2_QOS_MAX_LEAF_NODES);
2827 netdev = alloc_etherdev_mqs(sizeof(*pf), qcount + qos_txqs, qcount);
2830 goto err_release_regions;
2833 pci_set_drvdata(pdev, netdev);
2834 SET_NETDEV_DEV(netdev, &pdev->dev);
2835 pf = netdev_priv(netdev);
2836 pf->netdev = netdev;
2839 pf->total_vfs = pci_sriov_get_totalvfs(pdev);
2840 pf->flags |= OTX2_FLAG_INTF_DOWN;
2844 hw->rx_queues = qcount;
2845 hw->tx_queues = qcount;
2846 hw->non_qos_queues = qcount;
2847 hw->max_queues = qcount;
2848 hw->rbuf_len = OTX2_DEFAULT_RBUF_LEN;
2849 /* Use CQE of 128 byte descriptor size by default */
2852 num_vec = pci_msix_vec_count(pdev);
2853 hw->irq_name = devm_kmalloc_array(&hw->pdev->dev, num_vec, NAME_SIZE,
2855 if (!hw->irq_name) {
2857 goto err_free_netdev;
2860 hw->affinity_mask = devm_kcalloc(&hw->pdev->dev, num_vec,
2861 sizeof(cpumask_var_t), GFP_KERNEL);
2862 if (!hw->affinity_mask) {
2864 goto err_free_netdev;
2868 pf->reg_base = pcim_iomap(pdev, PCI_CFG_REG_BAR_NUM, 0);
2869 if (!pf->reg_base) {
2870 dev_err(dev, "Unable to map physical function CSRs, aborting\n");
2872 goto err_free_netdev;
2875 err = otx2_check_pf_usable(pf);
2877 goto err_free_netdev;
2879 err = pci_alloc_irq_vectors(hw->pdev, RVU_PF_INT_VEC_CNT,
2880 RVU_PF_INT_VEC_CNT, PCI_IRQ_MSIX);
2882 dev_err(dev, "%s: Failed to alloc %d IRQ vectors\n",
2884 goto err_free_netdev;
2887 otx2_setup_dev_hw_settings(pf);
2889 /* Init PF <=> AF mailbox stuff */
2890 err = otx2_pfaf_mbox_init(pf);
2892 goto err_free_irq_vectors;
2894 /* Register mailbox interrupt */
2895 err = otx2_register_mbox_intr(pf, true);
2897 goto err_mbox_destroy;
2899 /* Request AF to attach NPA and NIX LFs to this PF.
2900 * NIX and NPA LFs are needed for this PF to function as a NIC.
2902 err = otx2_attach_npa_nix(pf);
2904 goto err_disable_mbox_intr;
2906 err = otx2_realloc_msix_vectors(pf);
2908 goto err_detach_rsrc;
2910 err = otx2_set_real_num_queues(netdev, hw->tx_queues, hw->rx_queues);
2912 goto err_detach_rsrc;
2914 err = cn10k_lmtst_init(pf);
2916 goto err_detach_rsrc;
2918 /* Assign default mac address */
2919 otx2_get_mac_from_af(netdev);
2921 /* Don't check for error. Proceed without ptp */
2924 /* NPA's pool is a stack to which SW frees buffer pointers via Aura.
2925 * HW allocates buffer pointer from stack and uses it for DMA'ing
2926 * ingress packet. In some scenarios HW can free back allocated buffer
2927 * pointers to pool. This makes it impossible for SW to maintain a
2928 * parallel list where physical addresses of buffer pointers (IOVAs)
2929 * given to HW can be saved for later reference.
2931 * So the only way to convert Rx packet's buffer address is to use
2932 * IOMMU's iova_to_phys() handler which translates the address by
2933 * walking through the translation tables.
2935 pf->iommu_domain = iommu_get_domain_for_dev(dev);
2937 netdev->hw_features = (NETIF_F_RXCSUM | NETIF_F_IP_CSUM |
2938 NETIF_F_IPV6_CSUM | NETIF_F_RXHASH |
2939 NETIF_F_SG | NETIF_F_TSO | NETIF_F_TSO6 |
2940 NETIF_F_GSO_UDP_L4);
2941 netdev->features |= netdev->hw_features;
2943 err = otx2_mcam_flow_init(pf);
2945 goto err_ptp_destroy;
2947 err = cn10k_mcs_init(pf);
2949 goto err_del_mcam_entries;
2951 if (pf->flags & OTX2_FLAG_NTUPLE_SUPPORT)
2952 netdev->hw_features |= NETIF_F_NTUPLE;
2954 if (pf->flags & OTX2_FLAG_UCAST_FLTR_SUPPORT)
2955 netdev->priv_flags |= IFF_UNICAST_FLT;
2957 /* Support TSO on tag interface */
2958 netdev->vlan_features |= netdev->features;
2959 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX |
2960 NETIF_F_HW_VLAN_STAG_TX;
2961 if (pf->flags & OTX2_FLAG_RX_VLAN_SUPPORT)
2962 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX |
2963 NETIF_F_HW_VLAN_STAG_RX;
2964 netdev->features |= netdev->hw_features;
2966 /* HW supports tc offload but mutually exclusive with n-tuple filters */
2967 if (pf->flags & OTX2_FLAG_TC_FLOWER_SUPPORT)
2968 netdev->hw_features |= NETIF_F_HW_TC;
2970 netdev->hw_features |= NETIF_F_LOOPBACK | NETIF_F_RXALL;
2972 netif_set_tso_max_segs(netdev, OTX2_MAX_GSO_SEGS);
2973 netdev->watchdog_timeo = OTX2_TX_TIMEOUT;
2975 netdev->netdev_ops = &otx2_netdev_ops;
2976 netdev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT;
2978 netdev->min_mtu = OTX2_MIN_MTU;
2979 netdev->max_mtu = otx2_get_max_mtu(pf);
2981 err = register_netdev(netdev);
2983 dev_err(dev, "Failed to register netdevice\n");
2987 err = otx2_wq_init(pf);
2989 goto err_unreg_netdev;
2991 otx2_set_ethtool_ops(netdev);
2993 err = otx2_init_tc(pf);
2995 goto err_mcam_flow_del;
2997 err = otx2_register_dl(pf);
2999 goto err_mcam_flow_del;
3001 /* Initialize SR-IOV resources */
3002 err = otx2_sriov_vfcfg_init(pf);
3004 goto err_pf_sriov_init;
3006 /* Enable link notifications */
3007 otx2_cgx_config_linkevents(pf, true);
3010 err = otx2_dcbnl_set_ops(netdev);
3012 goto err_pf_sriov_init;
3015 otx2_qos_init(pf, qos_txqs);
3020 otx2_shutdown_tc(pf);
3022 otx2_mcam_flow_del(pf);
3024 unregister_netdev(netdev);
3027 err_del_mcam_entries:
3028 otx2_mcam_flow_del(pf);
3030 otx2_ptp_destroy(pf);
3032 if (pf->hw.lmt_info)
3033 free_percpu(pf->hw.lmt_info);
3034 if (test_bit(CN10K_LMTST, &pf->hw.cap_flag))
3035 qmem_free(pf->dev, pf->dync_lmt);
3036 otx2_detach_resources(&pf->mbox);
3037 err_disable_mbox_intr:
3038 otx2_disable_mbox_intr(pf);
3040 otx2_pfaf_mbox_destroy(pf);
3041 err_free_irq_vectors:
3042 pci_free_irq_vectors(hw->pdev);
3044 pci_set_drvdata(pdev, NULL);
3045 free_netdev(netdev);
3046 err_release_regions:
3047 pci_release_regions(pdev);
3051 static void otx2_vf_link_event_task(struct work_struct *work)
3053 struct otx2_vf_config *config;
3054 struct cgx_link_info_msg *req;
3055 struct mbox_msghdr *msghdr;
3056 struct otx2_nic *pf;
3059 config = container_of(work, struct otx2_vf_config,
3060 link_event_work.work);
3061 vf_idx = config - config->pf->vf_configs;
3064 msghdr = otx2_mbox_alloc_msg_rsp(&pf->mbox_pfvf[0].mbox_up, vf_idx,
3065 sizeof(*req), sizeof(struct msg_rsp));
3067 dev_err(pf->dev, "Failed to create VF%d link event\n", vf_idx);
3071 req = (struct cgx_link_info_msg *)msghdr;
3072 req->hdr.id = MBOX_MSG_CGX_LINK_EVENT;
3073 req->hdr.sig = OTX2_MBOX_REQ_SIG;
3074 memcpy(&req->link_info, &pf->linfo, sizeof(req->link_info));
3076 otx2_sync_mbox_up_msg(&pf->mbox_pfvf[0], vf_idx);
3079 static int otx2_sriov_enable(struct pci_dev *pdev, int numvfs)
3081 struct net_device *netdev = pci_get_drvdata(pdev);
3082 struct otx2_nic *pf = netdev_priv(netdev);
3085 /* Init PF <=> VF mailbox stuff */
3086 ret = otx2_pfvf_mbox_init(pf, numvfs);
3090 ret = otx2_register_pfvf_mbox_intr(pf, numvfs);
3094 ret = otx2_pf_flr_init(pf, numvfs);
3098 ret = otx2_register_flr_me_intr(pf, numvfs);
3102 ret = pci_enable_sriov(pdev, numvfs);
3108 otx2_disable_flr_me_intr(pf);
3110 otx2_flr_wq_destroy(pf);
3112 otx2_disable_pfvf_mbox_intr(pf, numvfs);
3114 otx2_pfvf_mbox_destroy(pf);
3118 static int otx2_sriov_disable(struct pci_dev *pdev)
3120 struct net_device *netdev = pci_get_drvdata(pdev);
3121 struct otx2_nic *pf = netdev_priv(netdev);
3122 int numvfs = pci_num_vf(pdev);
3127 pci_disable_sriov(pdev);
3129 otx2_disable_flr_me_intr(pf);
3130 otx2_flr_wq_destroy(pf);
3131 otx2_disable_pfvf_mbox_intr(pf, numvfs);
3132 otx2_pfvf_mbox_destroy(pf);
3137 static int otx2_sriov_configure(struct pci_dev *pdev, int numvfs)
3140 return otx2_sriov_disable(pdev);
3142 return otx2_sriov_enable(pdev, numvfs);
3145 static void otx2_remove(struct pci_dev *pdev)
3147 struct net_device *netdev = pci_get_drvdata(pdev);
3148 struct otx2_nic *pf;
3153 pf = netdev_priv(netdev);
3155 pf->flags |= OTX2_FLAG_PF_SHUTDOWN;
3157 if (pf->flags & OTX2_FLAG_TX_TSTAMP_ENABLED)
3158 otx2_config_hw_tx_tstamp(pf, false);
3159 if (pf->flags & OTX2_FLAG_RX_TSTAMP_ENABLED)
3160 otx2_config_hw_rx_tstamp(pf, false);
3162 /* Disable 802.3x pause frames */
3163 if (pf->flags & OTX2_FLAG_RX_PAUSE_ENABLED ||
3164 (pf->flags & OTX2_FLAG_TX_PAUSE_ENABLED)) {
3165 pf->flags &= ~OTX2_FLAG_RX_PAUSE_ENABLED;
3166 pf->flags &= ~OTX2_FLAG_TX_PAUSE_ENABLED;
3167 otx2_config_pause_frm(pf);
3171 /* Disable PFC config */
3174 otx2_config_priority_flow_ctrl(pf);
3177 cancel_work_sync(&pf->reset_task);
3178 /* Disable link notifications */
3179 otx2_cgx_config_linkevents(pf, false);
3181 otx2_unregister_dl(pf);
3182 unregister_netdev(netdev);
3184 otx2_sriov_disable(pf->pdev);
3185 otx2_sriov_vfcfg_cleanup(pf);
3187 destroy_workqueue(pf->otx2_wq);
3189 otx2_ptp_destroy(pf);
3190 otx2_mcam_flow_del(pf);
3191 otx2_shutdown_tc(pf);
3192 otx2_shutdown_qos(pf);
3193 otx2_detach_resources(&pf->mbox);
3194 if (pf->hw.lmt_info)
3195 free_percpu(pf->hw.lmt_info);
3196 if (test_bit(CN10K_LMTST, &pf->hw.cap_flag))
3197 qmem_free(pf->dev, pf->dync_lmt);
3198 otx2_disable_mbox_intr(pf);
3199 otx2_pfaf_mbox_destroy(pf);
3200 pci_free_irq_vectors(pf->pdev);
3201 pci_set_drvdata(pdev, NULL);
3202 free_netdev(netdev);
3204 pci_release_regions(pdev);
3207 static struct pci_driver otx2_pf_driver = {
3209 .id_table = otx2_pf_id_table,
3210 .probe = otx2_probe,
3211 .shutdown = otx2_remove,
3212 .remove = otx2_remove,
3213 .sriov_configure = otx2_sriov_configure
3216 static int __init otx2_rvupf_init_module(void)
3218 pr_info("%s: %s\n", DRV_NAME, DRV_STRING);
3220 return pci_register_driver(&otx2_pf_driver);
3223 static void __exit otx2_rvupf_cleanup_module(void)
3225 pci_unregister_driver(&otx2_pf_driver);
3228 module_init(otx2_rvupf_init_module);
3229 module_exit(otx2_rvupf_cleanup_module);