1 // SPDX-License-Identifier: GPL-2.0
2 /* Marvell OcteonTx2 RVU Physical Function ethernet driver
4 * Copyright (C) 2020 Marvell International Ltd.
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.
11 #include <linux/module.h>
12 #include <linux/interrupt.h>
13 #include <linux/pci.h>
14 #include <linux/etherdevice.h>
16 #include <linux/if_vlan.h>
17 #include <linux/iommu.h>
21 #include "otx2_common.h"
22 #include "otx2_txrx.h"
23 #include "otx2_struct.h"
25 #include <rvu_trace.h>
27 #define DRV_NAME "octeontx2-nicpf"
28 #define DRV_STRING "Marvell OcteonTX2 NIC Physical Function Driver"
30 /* Supported devices */
31 static const struct pci_device_id otx2_pf_id_table[] = {
32 { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVID_OCTEONTX2_RVU_PF) },
33 { 0, } /* end of table */
36 MODULE_AUTHOR("Sunil Goutham <sgoutham@marvell.com>");
37 MODULE_DESCRIPTION(DRV_STRING);
38 MODULE_LICENSE("GPL v2");
39 MODULE_DEVICE_TABLE(pci, otx2_pf_id_table);
46 static int otx2_config_hw_tx_tstamp(struct otx2_nic *pfvf, bool enable);
47 static int otx2_config_hw_rx_tstamp(struct otx2_nic *pfvf, bool enable);
49 static int otx2_change_mtu(struct net_device *netdev, int new_mtu)
51 bool if_up = netif_running(netdev);
57 netdev_info(netdev, "Changing MTU from %d to %d\n",
58 netdev->mtu, new_mtu);
59 netdev->mtu = new_mtu;
62 err = otx2_open(netdev);
67 static void otx2_disable_flr_me_intr(struct otx2_nic *pf)
69 int irq, vfs = pf->total_vfs;
71 /* Disable VFs ME interrupts */
72 otx2_write64(pf, RVU_PF_VFME_INT_ENA_W1CX(0), INTR_MASK(vfs));
73 irq = pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFME0);
76 /* Disable VFs FLR interrupts */
77 otx2_write64(pf, RVU_PF_VFFLR_INT_ENA_W1CX(0), INTR_MASK(vfs));
78 irq = pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFFLR0);
84 otx2_write64(pf, RVU_PF_VFME_INT_ENA_W1CX(1), INTR_MASK(vfs - 64));
85 irq = pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFME1);
88 otx2_write64(pf, RVU_PF_VFFLR_INT_ENA_W1CX(1), INTR_MASK(vfs - 64));
89 irq = pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFFLR1);
93 static void otx2_flr_wq_destroy(struct otx2_nic *pf)
97 destroy_workqueue(pf->flr_wq);
99 devm_kfree(pf->dev, pf->flr_wrk);
102 static void otx2_flr_handler(struct work_struct *work)
104 struct flr_work *flrwork = container_of(work, struct flr_work, work);
105 struct otx2_nic *pf = flrwork->pf;
106 struct mbox *mbox = &pf->mbox;
110 vf = flrwork - pf->flr_wrk;
112 mutex_lock(&mbox->lock);
113 req = otx2_mbox_alloc_msg_vf_flr(mbox);
115 mutex_unlock(&mbox->lock);
118 req->hdr.pcifunc &= RVU_PFVF_FUNC_MASK;
119 req->hdr.pcifunc |= (vf + 1) & RVU_PFVF_FUNC_MASK;
121 if (!otx2_sync_mbox_msg(&pf->mbox)) {
126 /* clear transcation pending bit */
127 otx2_write64(pf, RVU_PF_VFTRPENDX(reg), BIT_ULL(vf));
128 otx2_write64(pf, RVU_PF_VFFLR_INT_ENA_W1SX(reg), BIT_ULL(vf));
131 mutex_unlock(&mbox->lock);
134 static irqreturn_t otx2_pf_flr_intr_handler(int irq, void *pf_irq)
136 struct otx2_nic *pf = (struct otx2_nic *)pf_irq;
137 int reg, dev, vf, start_vf, num_reg = 1;
140 if (pf->total_vfs > 64)
143 for (reg = 0; reg < num_reg; reg++) {
144 intr = otx2_read64(pf, RVU_PF_VFFLR_INTX(reg));
148 for (vf = 0; vf < 64; vf++) {
149 if (!(intr & BIT_ULL(vf)))
152 queue_work(pf->flr_wq, &pf->flr_wrk[dev].work);
153 /* Clear interrupt */
154 otx2_write64(pf, RVU_PF_VFFLR_INTX(reg), BIT_ULL(vf));
155 /* Disable the interrupt */
156 otx2_write64(pf, RVU_PF_VFFLR_INT_ENA_W1CX(reg),
163 static irqreturn_t otx2_pf_me_intr_handler(int irq, void *pf_irq)
165 struct otx2_nic *pf = (struct otx2_nic *)pf_irq;
166 int vf, reg, num_reg = 1;
169 if (pf->total_vfs > 64)
172 for (reg = 0; reg < num_reg; reg++) {
173 intr = otx2_read64(pf, RVU_PF_VFME_INTX(reg));
176 for (vf = 0; vf < 64; vf++) {
177 if (!(intr & BIT_ULL(vf)))
179 /* clear trpend bit */
180 otx2_write64(pf, RVU_PF_VFTRPENDX(reg), BIT_ULL(vf));
181 /* clear interrupt */
182 otx2_write64(pf, RVU_PF_VFME_INTX(reg), BIT_ULL(vf));
188 static int otx2_register_flr_me_intr(struct otx2_nic *pf, int numvfs)
190 struct otx2_hw *hw = &pf->hw;
194 /* Register ME interrupt handler*/
195 irq_name = &hw->irq_name[RVU_PF_INT_VEC_VFME0 * NAME_SIZE];
196 snprintf(irq_name, NAME_SIZE, "RVUPF%d_ME0", rvu_get_pf(pf->pcifunc));
197 ret = request_irq(pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFME0),
198 otx2_pf_me_intr_handler, 0, irq_name, pf);
201 "RVUPF: IRQ registration failed for ME0\n");
204 /* Register FLR interrupt handler */
205 irq_name = &hw->irq_name[RVU_PF_INT_VEC_VFFLR0 * NAME_SIZE];
206 snprintf(irq_name, NAME_SIZE, "RVUPF%d_FLR0", rvu_get_pf(pf->pcifunc));
207 ret = request_irq(pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFFLR0),
208 otx2_pf_flr_intr_handler, 0, irq_name, pf);
211 "RVUPF: IRQ registration failed for FLR0\n");
216 irq_name = &hw->irq_name[RVU_PF_INT_VEC_VFME1 * NAME_SIZE];
217 snprintf(irq_name, NAME_SIZE, "RVUPF%d_ME1",
218 rvu_get_pf(pf->pcifunc));
219 ret = request_irq(pci_irq_vector
220 (pf->pdev, RVU_PF_INT_VEC_VFME1),
221 otx2_pf_me_intr_handler, 0, irq_name, pf);
224 "RVUPF: IRQ registration failed for ME1\n");
226 irq_name = &hw->irq_name[RVU_PF_INT_VEC_VFFLR1 * NAME_SIZE];
227 snprintf(irq_name, NAME_SIZE, "RVUPF%d_FLR1",
228 rvu_get_pf(pf->pcifunc));
229 ret = request_irq(pci_irq_vector
230 (pf->pdev, RVU_PF_INT_VEC_VFFLR1),
231 otx2_pf_flr_intr_handler, 0, irq_name, pf);
234 "RVUPF: IRQ registration failed for FLR1\n");
239 /* Enable ME interrupt for all VFs*/
240 otx2_write64(pf, RVU_PF_VFME_INTX(0), INTR_MASK(numvfs));
241 otx2_write64(pf, RVU_PF_VFME_INT_ENA_W1SX(0), INTR_MASK(numvfs));
243 /* Enable FLR interrupt for all VFs*/
244 otx2_write64(pf, RVU_PF_VFFLR_INTX(0), INTR_MASK(numvfs));
245 otx2_write64(pf, RVU_PF_VFFLR_INT_ENA_W1SX(0), INTR_MASK(numvfs));
250 otx2_write64(pf, RVU_PF_VFME_INTX(1), INTR_MASK(numvfs));
251 otx2_write64(pf, RVU_PF_VFME_INT_ENA_W1SX(1),
254 otx2_write64(pf, RVU_PF_VFFLR_INTX(1), INTR_MASK(numvfs));
255 otx2_write64(pf, RVU_PF_VFFLR_INT_ENA_W1SX(1),
261 static int otx2_pf_flr_init(struct otx2_nic *pf, int num_vfs)
265 pf->flr_wq = alloc_workqueue("otx2_pf_flr_wq",
266 WQ_UNBOUND | WQ_HIGHPRI, 1);
270 pf->flr_wrk = devm_kcalloc(pf->dev, num_vfs,
271 sizeof(struct flr_work), GFP_KERNEL);
273 destroy_workqueue(pf->flr_wq);
277 for (vf = 0; vf < num_vfs; vf++) {
278 pf->flr_wrk[vf].pf = pf;
279 INIT_WORK(&pf->flr_wrk[vf].work, otx2_flr_handler);
285 static void otx2_queue_work(struct mbox *mw, struct workqueue_struct *mbox_wq,
286 int first, int mdevs, u64 intr, int type)
288 struct otx2_mbox_dev *mdev;
289 struct otx2_mbox *mbox;
290 struct mbox_hdr *hdr;
293 for (i = first; i < mdevs; i++) {
295 if (!(intr & BIT_ULL(i - first)))
299 mdev = &mbox->dev[i];
300 if (type == TYPE_PFAF)
301 otx2_sync_mbox_bbuf(mbox, i);
302 hdr = mdev->mbase + mbox->rx_start;
303 /* The hdr->num_msgs is set to zero immediately in the interrupt
304 * handler to ensure that it holds a correct value next time
305 * when the interrupt handler is called.
306 * pf->mbox.num_msgs holds the data for use in pfaf_mbox_handler
307 * pf>mbox.up_num_msgs holds the data for use in
308 * pfaf_mbox_up_handler.
311 mw[i].num_msgs = hdr->num_msgs;
313 if (type == TYPE_PFAF)
314 memset(mbox->hwbase + mbox->rx_start, 0,
315 ALIGN(sizeof(struct mbox_hdr),
318 queue_work(mbox_wq, &mw[i].mbox_wrk);
322 mdev = &mbox->dev[i];
323 if (type == TYPE_PFAF)
324 otx2_sync_mbox_bbuf(mbox, i);
325 hdr = mdev->mbase + mbox->rx_start;
327 mw[i].up_num_msgs = hdr->num_msgs;
329 if (type == TYPE_PFAF)
330 memset(mbox->hwbase + mbox->rx_start, 0,
331 ALIGN(sizeof(struct mbox_hdr),
334 queue_work(mbox_wq, &mw[i].mbox_up_wrk);
339 static void otx2_forward_msg_pfvf(struct otx2_mbox_dev *mdev,
340 struct otx2_mbox *pfvf_mbox, void *bbuf_base,
343 struct otx2_mbox_dev *src_mdev = mdev;
346 /* Msgs are already copied, trigger VF's mbox irq */
349 offset = pfvf_mbox->trigger | (devid << pfvf_mbox->tr_shift);
350 writeq(1, (void __iomem *)pfvf_mbox->reg_base + offset);
352 /* Restore VF's mbox bounce buffer region address */
353 src_mdev->mbase = bbuf_base;
356 static int otx2_forward_vf_mbox_msgs(struct otx2_nic *pf,
357 struct otx2_mbox *src_mbox,
358 int dir, int vf, int num_msgs)
360 struct otx2_mbox_dev *src_mdev, *dst_mdev;
361 struct mbox_hdr *mbox_hdr;
362 struct mbox_hdr *req_hdr;
363 struct mbox *dst_mbox;
366 if (dir == MBOX_DIR_PFAF) {
367 /* Set VF's mailbox memory as PF's bounce buffer memory, so
368 * that explicit copying of VF's msgs to PF=>AF mbox region
369 * and AF=>PF responses to VF's mbox region can be avoided.
371 src_mdev = &src_mbox->dev[vf];
372 mbox_hdr = src_mbox->hwbase +
373 src_mbox->rx_start + (vf * MBOX_SIZE);
375 dst_mbox = &pf->mbox;
376 dst_size = dst_mbox->mbox.tx_size -
377 ALIGN(sizeof(*mbox_hdr), MBOX_MSG_ALIGN);
378 /* Check if msgs fit into destination area and has valid size */
379 if (mbox_hdr->msg_size > dst_size || !mbox_hdr->msg_size)
382 dst_mdev = &dst_mbox->mbox.dev[0];
384 mutex_lock(&pf->mbox.lock);
385 dst_mdev->mbase = src_mdev->mbase;
386 dst_mdev->msg_size = mbox_hdr->msg_size;
387 dst_mdev->num_msgs = num_msgs;
388 err = otx2_sync_mbox_msg(dst_mbox);
391 "AF not responding to VF%d messages\n", vf);
392 /* restore PF mbase and exit */
393 dst_mdev->mbase = pf->mbox.bbuf_base;
394 mutex_unlock(&pf->mbox.lock);
397 /* At this point, all the VF messages sent to AF are acked
398 * with proper responses and responses are copied to VF
399 * mailbox hence raise interrupt to VF.
401 req_hdr = (struct mbox_hdr *)(dst_mdev->mbase +
402 dst_mbox->mbox.rx_start);
403 req_hdr->num_msgs = num_msgs;
405 otx2_forward_msg_pfvf(dst_mdev, &pf->mbox_pfvf[0].mbox,
406 pf->mbox.bbuf_base, vf);
407 mutex_unlock(&pf->mbox.lock);
408 } else if (dir == MBOX_DIR_PFVF_UP) {
409 src_mdev = &src_mbox->dev[0];
410 mbox_hdr = src_mbox->hwbase + src_mbox->rx_start;
411 req_hdr = (struct mbox_hdr *)(src_mdev->mbase +
413 req_hdr->num_msgs = num_msgs;
415 dst_mbox = &pf->mbox_pfvf[0];
416 dst_size = dst_mbox->mbox_up.tx_size -
417 ALIGN(sizeof(*mbox_hdr), MBOX_MSG_ALIGN);
418 /* Check if msgs fit into destination area */
419 if (mbox_hdr->msg_size > dst_size)
422 dst_mdev = &dst_mbox->mbox_up.dev[vf];
423 dst_mdev->mbase = src_mdev->mbase;
424 dst_mdev->msg_size = mbox_hdr->msg_size;
425 dst_mdev->num_msgs = mbox_hdr->num_msgs;
426 err = otx2_sync_mbox_up_msg(dst_mbox, vf);
429 "VF%d is not responding to mailbox\n", vf);
432 } else if (dir == MBOX_DIR_VFPF_UP) {
433 req_hdr = (struct mbox_hdr *)(src_mbox->dev[0].mbase +
435 req_hdr->num_msgs = num_msgs;
436 otx2_forward_msg_pfvf(&pf->mbox_pfvf->mbox_up.dev[vf],
438 pf->mbox_pfvf[vf].bbuf_base,
445 static void otx2_pfvf_mbox_handler(struct work_struct *work)
447 struct mbox_msghdr *msg = NULL;
448 int offset, vf_idx, id, err;
449 struct otx2_mbox_dev *mdev;
450 struct mbox_hdr *req_hdr;
451 struct otx2_mbox *mbox;
452 struct mbox *vf_mbox;
455 vf_mbox = container_of(work, struct mbox, mbox_wrk);
457 vf_idx = vf_mbox - pf->mbox_pfvf;
459 mbox = &pf->mbox_pfvf[0].mbox;
460 mdev = &mbox->dev[vf_idx];
461 req_hdr = (struct mbox_hdr *)(mdev->mbase + mbox->rx_start);
463 offset = ALIGN(sizeof(*req_hdr), MBOX_MSG_ALIGN);
465 for (id = 0; id < vf_mbox->num_msgs; id++) {
466 msg = (struct mbox_msghdr *)(mdev->mbase + mbox->rx_start +
469 if (msg->sig != OTX2_MBOX_REQ_SIG)
472 /* Set VF's number in each of the msg */
473 msg->pcifunc &= RVU_PFVF_FUNC_MASK;
474 msg->pcifunc |= (vf_idx + 1) & RVU_PFVF_FUNC_MASK;
475 offset = msg->next_msgoff;
477 err = otx2_forward_vf_mbox_msgs(pf, mbox, MBOX_DIR_PFAF, vf_idx,
484 otx2_reply_invalid_msg(mbox, vf_idx, 0, msg->id);
485 otx2_mbox_msg_send(mbox, vf_idx);
488 static void otx2_pfvf_mbox_up_handler(struct work_struct *work)
490 struct mbox *vf_mbox = container_of(work, struct mbox, mbox_up_wrk);
491 struct otx2_nic *pf = vf_mbox->pfvf;
492 struct otx2_mbox_dev *mdev;
493 int offset, id, vf_idx = 0;
494 struct mbox_hdr *rsp_hdr;
495 struct mbox_msghdr *msg;
496 struct otx2_mbox *mbox;
498 vf_idx = vf_mbox - pf->mbox_pfvf;
499 mbox = &pf->mbox_pfvf[0].mbox_up;
500 mdev = &mbox->dev[vf_idx];
502 rsp_hdr = (struct mbox_hdr *)(mdev->mbase + mbox->rx_start);
503 offset = mbox->rx_start + ALIGN(sizeof(*rsp_hdr), MBOX_MSG_ALIGN);
505 for (id = 0; id < vf_mbox->up_num_msgs; id++) {
506 msg = mdev->mbase + offset;
508 if (msg->id >= MBOX_MSG_MAX) {
510 "Mbox msg with unknown ID 0x%x\n", msg->id);
514 if (msg->sig != OTX2_MBOX_RSP_SIG) {
516 "Mbox msg with wrong signature %x, ID 0x%x\n",
522 case MBOX_MSG_CGX_LINK_EVENT:
527 "Mbox msg response has err %d, ID 0x%x\n",
533 offset = mbox->rx_start + msg->next_msgoff;
534 if (mdev->msgs_acked == (vf_mbox->up_num_msgs - 1))
535 __otx2_mbox_reset(mbox, 0);
540 static irqreturn_t otx2_pfvf_mbox_intr_handler(int irq, void *pf_irq)
542 struct otx2_nic *pf = (struct otx2_nic *)(pf_irq);
543 int vfs = pf->total_vfs;
547 mbox = pf->mbox_pfvf;
548 /* Handle VF interrupts */
550 intr = otx2_read64(pf, RVU_PF_VFPF_MBOX_INTX(1));
551 otx2_write64(pf, RVU_PF_VFPF_MBOX_INTX(1), intr);
552 otx2_queue_work(mbox, pf->mbox_pfvf_wq, 64, vfs, intr,
557 intr = otx2_read64(pf, RVU_PF_VFPF_MBOX_INTX(0));
558 otx2_write64(pf, RVU_PF_VFPF_MBOX_INTX(0), intr);
560 otx2_queue_work(mbox, pf->mbox_pfvf_wq, 0, vfs, intr, TYPE_PFVF);
562 trace_otx2_msg_interrupt(mbox->mbox.pdev, "VF(s) to PF", intr);
567 static int otx2_pfvf_mbox_init(struct otx2_nic *pf, int numvfs)
569 void __iomem *hwbase;
577 pf->mbox_pfvf = devm_kcalloc(&pf->pdev->dev, numvfs,
578 sizeof(struct mbox), GFP_KERNEL);
582 pf->mbox_pfvf_wq = alloc_workqueue("otx2_pfvf_mailbox",
583 WQ_UNBOUND | WQ_HIGHPRI |
585 if (!pf->mbox_pfvf_wq)
588 base = readq((void __iomem *)((u64)pf->reg_base + RVU_PF_VF_BAR4_ADDR));
589 hwbase = ioremap_wc(base, MBOX_SIZE * pf->total_vfs);
596 mbox = &pf->mbox_pfvf[0];
597 err = otx2_mbox_init(&mbox->mbox, hwbase, pf->pdev, pf->reg_base,
598 MBOX_DIR_PFVF, numvfs);
602 err = otx2_mbox_init(&mbox->mbox_up, hwbase, pf->pdev, pf->reg_base,
603 MBOX_DIR_PFVF_UP, numvfs);
607 for (vf = 0; vf < numvfs; vf++) {
609 INIT_WORK(&mbox->mbox_wrk, otx2_pfvf_mbox_handler);
610 INIT_WORK(&mbox->mbox_up_wrk, otx2_pfvf_mbox_up_handler);
620 destroy_workqueue(pf->mbox_pfvf_wq);
624 static void otx2_pfvf_mbox_destroy(struct otx2_nic *pf)
626 struct mbox *mbox = &pf->mbox_pfvf[0];
631 if (pf->mbox_pfvf_wq) {
632 destroy_workqueue(pf->mbox_pfvf_wq);
633 pf->mbox_pfvf_wq = NULL;
636 if (mbox->mbox.hwbase)
637 iounmap(mbox->mbox.hwbase);
639 otx2_mbox_destroy(&mbox->mbox);
642 static void otx2_enable_pfvf_mbox_intr(struct otx2_nic *pf, int numvfs)
644 /* Clear PF <=> VF mailbox IRQ */
645 otx2_write64(pf, RVU_PF_VFPF_MBOX_INTX(0), ~0ull);
646 otx2_write64(pf, RVU_PF_VFPF_MBOX_INTX(1), ~0ull);
648 /* Enable PF <=> VF mailbox IRQ */
649 otx2_write64(pf, RVU_PF_VFPF_MBOX_INT_ENA_W1SX(0), INTR_MASK(numvfs));
652 otx2_write64(pf, RVU_PF_VFPF_MBOX_INT_ENA_W1SX(1),
657 static void otx2_disable_pfvf_mbox_intr(struct otx2_nic *pf, int numvfs)
661 /* Disable PF <=> VF mailbox IRQ */
662 otx2_write64(pf, RVU_PF_VFPF_MBOX_INT_ENA_W1CX(0), ~0ull);
663 otx2_write64(pf, RVU_PF_VFPF_MBOX_INT_ENA_W1CX(1), ~0ull);
665 otx2_write64(pf, RVU_PF_VFPF_MBOX_INTX(0), ~0ull);
666 vector = pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFPF_MBOX0);
667 free_irq(vector, pf);
670 otx2_write64(pf, RVU_PF_VFPF_MBOX_INTX(1), ~0ull);
671 vector = pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFPF_MBOX1);
672 free_irq(vector, pf);
676 static int otx2_register_pfvf_mbox_intr(struct otx2_nic *pf, int numvfs)
678 struct otx2_hw *hw = &pf->hw;
682 /* Register MBOX0 interrupt handler */
683 irq_name = &hw->irq_name[RVU_PF_INT_VEC_VFPF_MBOX0 * NAME_SIZE];
685 snprintf(irq_name, NAME_SIZE,
686 "RVUPF%d_VF Mbox0", rvu_get_pf(pf->pcifunc));
688 snprintf(irq_name, NAME_SIZE, "RVUPF_VF Mbox0");
689 err = request_irq(pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFPF_MBOX0),
690 otx2_pfvf_mbox_intr_handler, 0, irq_name, pf);
693 "RVUPF: IRQ registration failed for PFVF mbox0 irq\n");
698 /* Register MBOX1 interrupt handler */
699 irq_name = &hw->irq_name[RVU_PF_INT_VEC_VFPF_MBOX1 * NAME_SIZE];
701 snprintf(irq_name, NAME_SIZE,
702 "RVUPF%d_VF Mbox1", rvu_get_pf(pf->pcifunc));
704 snprintf(irq_name, NAME_SIZE, "RVUPF_VF Mbox1");
705 err = request_irq(pci_irq_vector(pf->pdev,
706 RVU_PF_INT_VEC_VFPF_MBOX1),
707 otx2_pfvf_mbox_intr_handler,
711 "RVUPF: IRQ registration failed for PFVF mbox1 irq\n");
716 otx2_enable_pfvf_mbox_intr(pf, numvfs);
721 static void otx2_process_pfaf_mbox_msg(struct otx2_nic *pf,
722 struct mbox_msghdr *msg)
726 if (msg->id >= MBOX_MSG_MAX) {
728 "Mbox msg with unknown ID 0x%x\n", msg->id);
732 if (msg->sig != OTX2_MBOX_RSP_SIG) {
734 "Mbox msg with wrong signature %x, ID 0x%x\n",
739 /* message response heading VF */
740 devid = msg->pcifunc & RVU_PFVF_FUNC_MASK;
742 struct otx2_vf_config *config = &pf->vf_configs[devid - 1];
743 struct delayed_work *dwork;
746 case MBOX_MSG_NIX_LF_START_RX:
747 config->intf_down = false;
748 dwork = &config->link_event_work;
749 schedule_delayed_work(dwork, msecs_to_jiffies(100));
751 case MBOX_MSG_NIX_LF_STOP_RX:
752 config->intf_down = true;
761 pf->pcifunc = msg->pcifunc;
763 case MBOX_MSG_MSIX_OFFSET:
764 mbox_handler_msix_offset(pf, (struct msix_offset_rsp *)msg);
766 case MBOX_MSG_NPA_LF_ALLOC:
767 mbox_handler_npa_lf_alloc(pf, (struct npa_lf_alloc_rsp *)msg);
769 case MBOX_MSG_NIX_LF_ALLOC:
770 mbox_handler_nix_lf_alloc(pf, (struct nix_lf_alloc_rsp *)msg);
772 case MBOX_MSG_NIX_TXSCH_ALLOC:
773 mbox_handler_nix_txsch_alloc(pf,
774 (struct nix_txsch_alloc_rsp *)msg);
776 case MBOX_MSG_NIX_BP_ENABLE:
777 mbox_handler_nix_bp_enable(pf, (struct nix_bp_cfg_rsp *)msg);
779 case MBOX_MSG_CGX_STATS:
780 mbox_handler_cgx_stats(pf, (struct cgx_stats_rsp *)msg);
785 "Mbox msg response has err %d, ID 0x%x\n",
791 static void otx2_pfaf_mbox_handler(struct work_struct *work)
793 struct otx2_mbox_dev *mdev;
794 struct mbox_hdr *rsp_hdr;
795 struct mbox_msghdr *msg;
796 struct otx2_mbox *mbox;
797 struct mbox *af_mbox;
801 af_mbox = container_of(work, struct mbox, mbox_wrk);
802 mbox = &af_mbox->mbox;
803 mdev = &mbox->dev[0];
804 rsp_hdr = (struct mbox_hdr *)(mdev->mbase + mbox->rx_start);
806 offset = mbox->rx_start + ALIGN(sizeof(*rsp_hdr), MBOX_MSG_ALIGN);
809 for (id = 0; id < af_mbox->num_msgs; id++) {
810 msg = (struct mbox_msghdr *)(mdev->mbase + offset);
811 otx2_process_pfaf_mbox_msg(pf, msg);
812 offset = mbox->rx_start + msg->next_msgoff;
813 if (mdev->msgs_acked == (af_mbox->num_msgs - 1))
814 __otx2_mbox_reset(mbox, 0);
820 static void otx2_handle_link_event(struct otx2_nic *pf)
822 struct cgx_link_user_info *linfo = &pf->linfo;
823 struct net_device *netdev = pf->netdev;
825 pr_info("%s NIC Link is %s %d Mbps %s duplex\n", netdev->name,
826 linfo->link_up ? "UP" : "DOWN", linfo->speed,
827 linfo->full_duplex ? "Full" : "Half");
828 if (linfo->link_up) {
829 netif_carrier_on(netdev);
830 netif_tx_start_all_queues(netdev);
832 netif_tx_stop_all_queues(netdev);
833 netif_carrier_off(netdev);
837 int otx2_mbox_up_handler_cgx_link_event(struct otx2_nic *pf,
838 struct cgx_link_info_msg *msg,
843 /* Copy the link info sent by AF */
844 pf->linfo = msg->link_info;
846 /* notify VFs about link event */
847 for (i = 0; i < pci_num_vf(pf->pdev); i++) {
848 struct otx2_vf_config *config = &pf->vf_configs[i];
849 struct delayed_work *dwork = &config->link_event_work;
851 if (config->intf_down)
854 schedule_delayed_work(dwork, msecs_to_jiffies(100));
857 /* interface has not been fully configured yet */
858 if (pf->flags & OTX2_FLAG_INTF_DOWN)
861 otx2_handle_link_event(pf);
865 static int otx2_process_mbox_msg_up(struct otx2_nic *pf,
866 struct mbox_msghdr *req)
868 /* Check if valid, if not reply with a invalid msg */
869 if (req->sig != OTX2_MBOX_REQ_SIG) {
870 otx2_reply_invalid_msg(&pf->mbox.mbox_up, 0, 0, req->id);
875 #define M(_name, _id, _fn_name, _req_type, _rsp_type) \
877 struct _rsp_type *rsp; \
880 rsp = (struct _rsp_type *)otx2_mbox_alloc_msg( \
881 &pf->mbox.mbox_up, 0, \
882 sizeof(struct _rsp_type)); \
887 rsp->hdr.sig = OTX2_MBOX_RSP_SIG; \
888 rsp->hdr.pcifunc = 0; \
891 err = otx2_mbox_up_handler_ ## _fn_name( \
892 pf, (struct _req_type *)req, rsp); \
899 otx2_reply_invalid_msg(&pf->mbox.mbox_up, 0, 0, req->id);
905 static void otx2_pfaf_mbox_up_handler(struct work_struct *work)
907 struct mbox *af_mbox = container_of(work, struct mbox, mbox_up_wrk);
908 struct otx2_mbox *mbox = &af_mbox->mbox_up;
909 struct otx2_mbox_dev *mdev = &mbox->dev[0];
910 struct otx2_nic *pf = af_mbox->pfvf;
911 int offset, id, devid = 0;
912 struct mbox_hdr *rsp_hdr;
913 struct mbox_msghdr *msg;
915 rsp_hdr = (struct mbox_hdr *)(mdev->mbase + mbox->rx_start);
917 offset = mbox->rx_start + ALIGN(sizeof(*rsp_hdr), MBOX_MSG_ALIGN);
919 for (id = 0; id < af_mbox->up_num_msgs; id++) {
920 msg = (struct mbox_msghdr *)(mdev->mbase + offset);
922 devid = msg->pcifunc & RVU_PFVF_FUNC_MASK;
923 /* Skip processing VF's messages */
925 otx2_process_mbox_msg_up(pf, msg);
926 offset = mbox->rx_start + msg->next_msgoff;
929 otx2_forward_vf_mbox_msgs(pf, &pf->mbox.mbox_up,
930 MBOX_DIR_PFVF_UP, devid - 1,
931 af_mbox->up_num_msgs);
935 otx2_mbox_msg_send(mbox, 0);
938 static irqreturn_t otx2_pfaf_mbox_intr_handler(int irq, void *pf_irq)
940 struct otx2_nic *pf = (struct otx2_nic *)pf_irq;
944 otx2_write64(pf, RVU_PF_INT, BIT_ULL(0));
948 trace_otx2_msg_interrupt(mbox->mbox.pdev, "AF to PF", BIT_ULL(0));
950 otx2_queue_work(mbox, pf->mbox_wq, 0, 1, 1, TYPE_PFAF);
955 static void otx2_disable_mbox_intr(struct otx2_nic *pf)
957 int vector = pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_AFPF_MBOX);
959 /* Disable AF => PF mailbox IRQ */
960 otx2_write64(pf, RVU_PF_INT_ENA_W1C, BIT_ULL(0));
961 free_irq(vector, pf);
964 static int otx2_register_mbox_intr(struct otx2_nic *pf, bool probe_af)
966 struct otx2_hw *hw = &pf->hw;
971 /* Register mailbox interrupt handler */
972 irq_name = &hw->irq_name[RVU_PF_INT_VEC_AFPF_MBOX * NAME_SIZE];
973 snprintf(irq_name, NAME_SIZE, "RVUPFAF Mbox");
974 err = request_irq(pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_AFPF_MBOX),
975 otx2_pfaf_mbox_intr_handler, 0, irq_name, pf);
978 "RVUPF: IRQ registration failed for PFAF mbox irq\n");
982 /* Enable mailbox interrupt for msgs coming from AF.
983 * First clear to avoid spurious interrupts, if any.
985 otx2_write64(pf, RVU_PF_INT, BIT_ULL(0));
986 otx2_write64(pf, RVU_PF_INT_ENA_W1S, BIT_ULL(0));
991 /* Check mailbox communication with AF */
992 req = otx2_mbox_alloc_msg_ready(&pf->mbox);
994 otx2_disable_mbox_intr(pf);
997 err = otx2_sync_mbox_msg(&pf->mbox);
1000 "AF not responding to mailbox, deferring probe\n");
1001 otx2_disable_mbox_intr(pf);
1002 return -EPROBE_DEFER;
1008 static void otx2_pfaf_mbox_destroy(struct otx2_nic *pf)
1010 struct mbox *mbox = &pf->mbox;
1013 destroy_workqueue(pf->mbox_wq);
1017 if (mbox->mbox.hwbase)
1018 iounmap((void __iomem *)mbox->mbox.hwbase);
1020 otx2_mbox_destroy(&mbox->mbox);
1021 otx2_mbox_destroy(&mbox->mbox_up);
1024 static int otx2_pfaf_mbox_init(struct otx2_nic *pf)
1026 struct mbox *mbox = &pf->mbox;
1027 void __iomem *hwbase;
1031 pf->mbox_wq = alloc_workqueue("otx2_pfaf_mailbox",
1032 WQ_UNBOUND | WQ_HIGHPRI |
1037 /* Mailbox is a reserved memory (in RAM) region shared between
1038 * admin function (i.e AF) and this PF, shouldn't be mapped as
1039 * device memory to allow unaligned accesses.
1041 hwbase = ioremap_wc(pci_resource_start(pf->pdev, PCI_MBOX_BAR_NUM),
1042 pci_resource_len(pf->pdev, PCI_MBOX_BAR_NUM));
1044 dev_err(pf->dev, "Unable to map PFAF mailbox region\n");
1049 err = otx2_mbox_init(&mbox->mbox, hwbase, pf->pdev, pf->reg_base,
1054 err = otx2_mbox_init(&mbox->mbox_up, hwbase, pf->pdev, pf->reg_base,
1055 MBOX_DIR_PFAF_UP, 1);
1059 err = otx2_mbox_bbuf_init(mbox, pf->pdev);
1063 INIT_WORK(&mbox->mbox_wrk, otx2_pfaf_mbox_handler);
1064 INIT_WORK(&mbox->mbox_up_wrk, otx2_pfaf_mbox_up_handler);
1065 mutex_init(&mbox->lock);
1069 otx2_pfaf_mbox_destroy(pf);
1073 static int otx2_cgx_config_linkevents(struct otx2_nic *pf, bool enable)
1075 struct msg_req *msg;
1078 mutex_lock(&pf->mbox.lock);
1080 msg = otx2_mbox_alloc_msg_cgx_start_linkevents(&pf->mbox);
1082 msg = otx2_mbox_alloc_msg_cgx_stop_linkevents(&pf->mbox);
1085 mutex_unlock(&pf->mbox.lock);
1089 err = otx2_sync_mbox_msg(&pf->mbox);
1090 mutex_unlock(&pf->mbox.lock);
1094 static int otx2_cgx_config_loopback(struct otx2_nic *pf, bool enable)
1096 struct msg_req *msg;
1099 mutex_lock(&pf->mbox.lock);
1101 msg = otx2_mbox_alloc_msg_cgx_intlbk_enable(&pf->mbox);
1103 msg = otx2_mbox_alloc_msg_cgx_intlbk_disable(&pf->mbox);
1106 mutex_unlock(&pf->mbox.lock);
1110 err = otx2_sync_mbox_msg(&pf->mbox);
1111 mutex_unlock(&pf->mbox.lock);
1115 int otx2_set_real_num_queues(struct net_device *netdev,
1116 int tx_queues, int rx_queues)
1120 err = netif_set_real_num_tx_queues(netdev, tx_queues);
1123 "Failed to set no of Tx queues: %d\n", tx_queues);
1127 err = netif_set_real_num_rx_queues(netdev, rx_queues);
1130 "Failed to set no of Rx queues: %d\n", rx_queues);
1133 EXPORT_SYMBOL(otx2_set_real_num_queues);
1135 static irqreturn_t otx2_q_intr_handler(int irq, void *data)
1137 struct otx2_nic *pf = data;
1142 for (qidx = 0; qidx < pf->qset.cq_cnt; qidx++) {
1143 ptr = otx2_get_regaddr(pf, NIX_LF_CQ_OP_INT);
1144 val = otx2_atomic64_add((qidx << 44), ptr);
1146 otx2_write64(pf, NIX_LF_CQ_OP_INT, (qidx << 44) |
1147 (val & NIX_CQERRINT_BITS));
1148 if (!(val & (NIX_CQERRINT_BITS | BIT_ULL(42))))
1151 if (val & BIT_ULL(42)) {
1152 netdev_err(pf->netdev, "CQ%lld: error reading NIX_LF_CQ_OP_INT, NIX_LF_ERR_INT 0x%llx\n",
1153 qidx, otx2_read64(pf, NIX_LF_ERR_INT));
1155 if (val & BIT_ULL(NIX_CQERRINT_DOOR_ERR))
1156 netdev_err(pf->netdev, "CQ%lld: Doorbell error",
1158 if (val & BIT_ULL(NIX_CQERRINT_CQE_FAULT))
1159 netdev_err(pf->netdev, "CQ%lld: Memory fault on CQE write to LLC/DRAM",
1163 schedule_work(&pf->reset_task);
1167 for (qidx = 0; qidx < pf->hw.tx_queues; qidx++) {
1168 ptr = otx2_get_regaddr(pf, NIX_LF_SQ_OP_INT);
1169 val = otx2_atomic64_add((qidx << 44), ptr);
1170 otx2_write64(pf, NIX_LF_SQ_OP_INT, (qidx << 44) |
1171 (val & NIX_SQINT_BITS));
1173 if (!(val & (NIX_SQINT_BITS | BIT_ULL(42))))
1176 if (val & BIT_ULL(42)) {
1177 netdev_err(pf->netdev, "SQ%lld: error reading NIX_LF_SQ_OP_INT, NIX_LF_ERR_INT 0x%llx\n",
1178 qidx, otx2_read64(pf, NIX_LF_ERR_INT));
1180 if (val & BIT_ULL(NIX_SQINT_LMT_ERR)) {
1181 netdev_err(pf->netdev, "SQ%lld: LMT store error NIX_LF_SQ_OP_ERR_DBG:0x%llx",
1184 NIX_LF_SQ_OP_ERR_DBG));
1185 otx2_write64(pf, NIX_LF_SQ_OP_ERR_DBG,
1188 if (val & BIT_ULL(NIX_SQINT_MNQ_ERR)) {
1189 netdev_err(pf->netdev, "SQ%lld: Meta-descriptor enqueue error NIX_LF_MNQ_ERR_DGB:0x%llx\n",
1191 otx2_read64(pf, NIX_LF_MNQ_ERR_DBG));
1192 otx2_write64(pf, NIX_LF_MNQ_ERR_DBG,
1195 if (val & BIT_ULL(NIX_SQINT_SEND_ERR)) {
1196 netdev_err(pf->netdev, "SQ%lld: Send error, NIX_LF_SEND_ERR_DBG 0x%llx",
1199 NIX_LF_SEND_ERR_DBG));
1200 otx2_write64(pf, NIX_LF_SEND_ERR_DBG,
1203 if (val & BIT_ULL(NIX_SQINT_SQB_ALLOC_FAIL))
1204 netdev_err(pf->netdev, "SQ%lld: SQB allocation failed",
1208 schedule_work(&pf->reset_task);
1214 static irqreturn_t otx2_cq_intr_handler(int irq, void *cq_irq)
1216 struct otx2_cq_poll *cq_poll = (struct otx2_cq_poll *)cq_irq;
1217 struct otx2_nic *pf = (struct otx2_nic *)cq_poll->dev;
1218 int qidx = cq_poll->cint_idx;
1220 /* Disable interrupts.
1222 * Completion interrupts behave in a level-triggered interrupt
1223 * fashion, and hence have to be cleared only after it is serviced.
1225 otx2_write64(pf, NIX_LF_CINTX_ENA_W1C(qidx), BIT_ULL(0));
1228 napi_schedule_irqoff(&cq_poll->napi);
1233 static void otx2_disable_napi(struct otx2_nic *pf)
1235 struct otx2_qset *qset = &pf->qset;
1236 struct otx2_cq_poll *cq_poll;
1239 for (qidx = 0; qidx < pf->hw.cint_cnt; qidx++) {
1240 cq_poll = &qset->napi[qidx];
1241 napi_disable(&cq_poll->napi);
1242 netif_napi_del(&cq_poll->napi);
1246 static void otx2_free_cq_res(struct otx2_nic *pf)
1248 struct otx2_qset *qset = &pf->qset;
1249 struct otx2_cq_queue *cq;
1253 otx2_ctx_disable(&pf->mbox, NIX_AQ_CTYPE_CQ, false);
1254 for (qidx = 0; qidx < qset->cq_cnt; qidx++) {
1255 cq = &qset->cq[qidx];
1256 qmem_free(pf->dev, cq->cqe);
1260 static void otx2_free_sq_res(struct otx2_nic *pf)
1262 struct otx2_qset *qset = &pf->qset;
1263 struct otx2_snd_queue *sq;
1267 otx2_ctx_disable(&pf->mbox, NIX_AQ_CTYPE_SQ, false);
1268 /* Free SQB pointers */
1269 otx2_sq_free_sqbs(pf);
1270 for (qidx = 0; qidx < pf->hw.tx_queues; qidx++) {
1271 sq = &qset->sq[qidx];
1272 qmem_free(pf->dev, sq->sqe);
1273 qmem_free(pf->dev, sq->tso_hdrs);
1275 kfree(sq->sqb_ptrs);
1279 static int otx2_init_hw_resources(struct otx2_nic *pf)
1281 struct nix_lf_free_req *free_req;
1282 struct mbox *mbox = &pf->mbox;
1283 struct otx2_hw *hw = &pf->hw;
1284 struct msg_req *req;
1287 /* Set required NPA LF's pool counts
1288 * Auras and Pools are used in a 1:1 mapping,
1289 * so, aura count = pool count.
1291 hw->rqpool_cnt = hw->rx_queues;
1292 hw->sqpool_cnt = hw->tx_queues;
1293 hw->pool_cnt = hw->rqpool_cnt + hw->sqpool_cnt;
1295 /* Get the size of receive buffers to allocate */
1296 pf->rbsize = RCV_FRAG_LEN(OTX2_HW_TIMESTAMP_LEN + pf->netdev->mtu +
1299 mutex_lock(&mbox->lock);
1301 err = otx2_config_npa(pf);
1306 err = otx2_config_nix(pf);
1308 goto err_free_npa_lf;
1310 /* Enable backpressure */
1311 otx2_nix_config_bp(pf, true);
1313 /* Init Auras and pools used by NIX RQ, for free buffer ptrs */
1314 err = otx2_rq_aura_pool_init(pf);
1316 mutex_unlock(&mbox->lock);
1317 goto err_free_nix_lf;
1319 /* Init Auras and pools used by NIX SQ, for queueing SQEs */
1320 err = otx2_sq_aura_pool_init(pf);
1322 mutex_unlock(&mbox->lock);
1323 goto err_free_rq_ptrs;
1326 err = otx2_txsch_alloc(pf);
1328 mutex_unlock(&mbox->lock);
1329 goto err_free_sq_ptrs;
1332 err = otx2_config_nix_queues(pf);
1334 mutex_unlock(&mbox->lock);
1335 goto err_free_txsch;
1337 for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
1338 err = otx2_txschq_config(pf, lvl);
1340 mutex_unlock(&mbox->lock);
1341 goto err_free_nix_queues;
1344 mutex_unlock(&mbox->lock);
1347 err_free_nix_queues:
1348 otx2_free_sq_res(pf);
1349 otx2_free_cq_res(pf);
1350 otx2_ctx_disable(mbox, NIX_AQ_CTYPE_RQ, false);
1352 if (otx2_txschq_stop(pf))
1353 dev_err(pf->dev, "%s failed to stop TX schedulers\n", __func__);
1355 otx2_sq_free_sqbs(pf);
1357 otx2_free_aura_ptr(pf, AURA_NIX_RQ);
1358 otx2_ctx_disable(mbox, NPA_AQ_CTYPE_POOL, true);
1359 otx2_ctx_disable(mbox, NPA_AQ_CTYPE_AURA, true);
1360 otx2_aura_pool_free(pf);
1362 mutex_lock(&mbox->lock);
1363 free_req = otx2_mbox_alloc_msg_nix_lf_free(mbox);
1365 free_req->flags = NIX_LF_DISABLE_FLOWS;
1366 if (otx2_sync_mbox_msg(mbox))
1367 dev_err(pf->dev, "%s failed to free nixlf\n", __func__);
1371 req = otx2_mbox_alloc_msg_npa_lf_free(mbox);
1373 if (otx2_sync_mbox_msg(mbox))
1374 dev_err(pf->dev, "%s failed to free npalf\n", __func__);
1377 mutex_unlock(&mbox->lock);
1381 static void otx2_free_hw_resources(struct otx2_nic *pf)
1383 struct otx2_qset *qset = &pf->qset;
1384 struct nix_lf_free_req *free_req;
1385 struct mbox *mbox = &pf->mbox;
1386 struct otx2_cq_queue *cq;
1387 struct msg_req *req;
1390 /* Ensure all SQE are processed */
1393 /* Stop transmission */
1394 err = otx2_txschq_stop(pf);
1396 dev_err(pf->dev, "RVUPF: Failed to stop/free TX schedulers\n");
1398 mutex_lock(&mbox->lock);
1399 /* Disable backpressure */
1400 if (!(pf->pcifunc & RVU_PFVF_FUNC_MASK))
1401 otx2_nix_config_bp(pf, false);
1402 mutex_unlock(&mbox->lock);
1405 otx2_ctx_disable(mbox, NIX_AQ_CTYPE_RQ, false);
1407 /*Dequeue all CQEs */
1408 for (qidx = 0; qidx < qset->cq_cnt; qidx++) {
1409 cq = &qset->cq[qidx];
1410 if (cq->cq_type == CQ_RX)
1411 otx2_cleanup_rx_cqes(pf, cq);
1413 otx2_cleanup_tx_cqes(pf, cq);
1416 otx2_free_sq_res(pf);
1418 /* Free RQ buffer pointers*/
1419 otx2_free_aura_ptr(pf, AURA_NIX_RQ);
1421 otx2_free_cq_res(pf);
1423 mutex_lock(&mbox->lock);
1425 free_req = otx2_mbox_alloc_msg_nix_lf_free(mbox);
1427 free_req->flags = NIX_LF_DISABLE_FLOWS;
1428 if (!(pf->flags & OTX2_FLAG_PF_SHUTDOWN))
1429 free_req->flags |= NIX_LF_DONT_FREE_TX_VTAG;
1430 if (otx2_sync_mbox_msg(mbox))
1431 dev_err(pf->dev, "%s failed to free nixlf\n", __func__);
1433 mutex_unlock(&mbox->lock);
1435 /* Disable NPA Pool and Aura hw context */
1436 otx2_ctx_disable(mbox, NPA_AQ_CTYPE_POOL, true);
1437 otx2_ctx_disable(mbox, NPA_AQ_CTYPE_AURA, true);
1438 otx2_aura_pool_free(pf);
1440 mutex_lock(&mbox->lock);
1442 req = otx2_mbox_alloc_msg_npa_lf_free(mbox);
1444 if (otx2_sync_mbox_msg(mbox))
1445 dev_err(pf->dev, "%s failed to free npalf\n", __func__);
1447 mutex_unlock(&mbox->lock);
1450 int otx2_open(struct net_device *netdev)
1452 struct otx2_nic *pf = netdev_priv(netdev);
1453 struct otx2_cq_poll *cq_poll = NULL;
1454 struct otx2_qset *qset = &pf->qset;
1455 int err = 0, qidx, vec;
1458 netif_carrier_off(netdev);
1460 pf->qset.cq_cnt = pf->hw.rx_queues + pf->hw.tx_queues;
1461 /* RQ and SQs are mapped to different CQs,
1462 * so find out max CQ IRQs (i.e CINTs) needed.
1464 pf->hw.cint_cnt = max(pf->hw.rx_queues, pf->hw.tx_queues);
1465 qset->napi = kcalloc(pf->hw.cint_cnt, sizeof(*cq_poll), GFP_KERNEL);
1470 qset->rqe_cnt = qset->rqe_cnt ? qset->rqe_cnt : Q_COUNT(Q_SIZE_256);
1472 qset->sqe_cnt = qset->sqe_cnt ? qset->sqe_cnt : Q_COUNT(Q_SIZE_4K);
1475 qset->cq = kcalloc(pf->qset.cq_cnt,
1476 sizeof(struct otx2_cq_queue), GFP_KERNEL);
1480 qset->sq = kcalloc(pf->hw.tx_queues,
1481 sizeof(struct otx2_snd_queue), GFP_KERNEL);
1485 qset->rq = kcalloc(pf->hw.rx_queues,
1486 sizeof(struct otx2_rcv_queue), GFP_KERNEL);
1490 err = otx2_init_hw_resources(pf);
1494 /* Register NAPI handler */
1495 for (qidx = 0; qidx < pf->hw.cint_cnt; qidx++) {
1496 cq_poll = &qset->napi[qidx];
1497 cq_poll->cint_idx = qidx;
1498 /* RQ0 & SQ0 are mapped to CINT0 and so on..
1499 * 'cq_ids[0]' points to RQ's CQ and
1500 * 'cq_ids[1]' points to SQ's CQ and
1502 cq_poll->cq_ids[CQ_RX] =
1503 (qidx < pf->hw.rx_queues) ? qidx : CINT_INVALID_CQ;
1504 cq_poll->cq_ids[CQ_TX] = (qidx < pf->hw.tx_queues) ?
1505 qidx + pf->hw.rx_queues : CINT_INVALID_CQ;
1506 cq_poll->dev = (void *)pf;
1507 netif_napi_add(netdev, &cq_poll->napi,
1508 otx2_napi_handler, NAPI_POLL_WEIGHT);
1509 napi_enable(&cq_poll->napi);
1512 /* Set maximum frame size allowed in HW */
1513 err = otx2_hw_set_mtu(pf, netdev->mtu);
1515 goto err_disable_napi;
1517 /* Setup segmentation algorithms, if failed, clear offload capability */
1518 otx2_setup_segmentation(pf);
1520 /* Initialize RSS */
1521 err = otx2_rss_init(pf);
1523 goto err_disable_napi;
1525 /* Register Queue IRQ handlers */
1526 vec = pf->hw.nix_msixoff + NIX_LF_QINT_VEC_START;
1527 irq_name = &pf->hw.irq_name[vec * NAME_SIZE];
1529 snprintf(irq_name, NAME_SIZE, "%s-qerr", pf->netdev->name);
1531 err = request_irq(pci_irq_vector(pf->pdev, vec),
1532 otx2_q_intr_handler, 0, irq_name, pf);
1535 "RVUPF%d: IRQ registration failed for QERR\n",
1536 rvu_get_pf(pf->pcifunc));
1537 goto err_disable_napi;
1540 /* Enable QINT IRQ */
1541 otx2_write64(pf, NIX_LF_QINTX_ENA_W1S(0), BIT_ULL(0));
1543 /* Register CQ IRQ handlers */
1544 vec = pf->hw.nix_msixoff + NIX_LF_CINT_VEC_START;
1545 for (qidx = 0; qidx < pf->hw.cint_cnt; qidx++) {
1546 irq_name = &pf->hw.irq_name[vec * NAME_SIZE];
1548 snprintf(irq_name, NAME_SIZE, "%s-rxtx-%d", pf->netdev->name,
1551 err = request_irq(pci_irq_vector(pf->pdev, vec),
1552 otx2_cq_intr_handler, 0, irq_name,
1556 "RVUPF%d: IRQ registration failed for CQ%d\n",
1557 rvu_get_pf(pf->pcifunc), qidx);
1558 goto err_free_cints;
1562 otx2_config_irq_coalescing(pf, qidx);
1565 otx2_write64(pf, NIX_LF_CINTX_INT(qidx), BIT_ULL(0));
1566 otx2_write64(pf, NIX_LF_CINTX_ENA_W1S(qidx), BIT_ULL(0));
1569 otx2_set_cints_affinity(pf);
1571 if (pf->flags & OTX2_FLAG_RX_VLAN_SUPPORT)
1572 otx2_enable_rxvlan(pf, true);
1574 /* When reinitializing enable time stamping if it is enabled before */
1575 if (pf->flags & OTX2_FLAG_TX_TSTAMP_ENABLED) {
1576 pf->flags &= ~OTX2_FLAG_TX_TSTAMP_ENABLED;
1577 otx2_config_hw_tx_tstamp(pf, true);
1579 if (pf->flags & OTX2_FLAG_RX_TSTAMP_ENABLED) {
1580 pf->flags &= ~OTX2_FLAG_RX_TSTAMP_ENABLED;
1581 otx2_config_hw_rx_tstamp(pf, true);
1584 pf->flags &= ~OTX2_FLAG_INTF_DOWN;
1585 /* 'intf_down' may be checked on any cpu */
1588 /* we have already received link status notification */
1589 if (pf->linfo.link_up && !(pf->pcifunc & RVU_PFVF_FUNC_MASK))
1590 otx2_handle_link_event(pf);
1592 /* Restore pause frame settings */
1593 otx2_config_pause_frm(pf);
1595 err = otx2_rxtx_enable(pf, true);
1597 goto err_tx_stop_queues;
1602 netif_tx_stop_all_queues(netdev);
1603 netif_carrier_off(netdev);
1605 otx2_free_cints(pf, qidx);
1606 vec = pci_irq_vector(pf->pdev,
1607 pf->hw.nix_msixoff + NIX_LF_QINT_VEC_START);
1608 otx2_write64(pf, NIX_LF_QINTX_ENA_W1C(0), BIT_ULL(0));
1609 synchronize_irq(vec);
1612 otx2_disable_napi(pf);
1613 otx2_free_hw_resources(pf);
1621 EXPORT_SYMBOL(otx2_open);
1623 int otx2_stop(struct net_device *netdev)
1625 struct otx2_nic *pf = netdev_priv(netdev);
1626 struct otx2_cq_poll *cq_poll = NULL;
1627 struct otx2_qset *qset = &pf->qset;
1630 netif_carrier_off(netdev);
1631 netif_tx_stop_all_queues(netdev);
1633 pf->flags |= OTX2_FLAG_INTF_DOWN;
1634 /* 'intf_down' may be checked on any cpu */
1637 /* First stop packet Rx/Tx */
1638 otx2_rxtx_enable(pf, false);
1640 /* Cleanup Queue IRQ */
1641 vec = pci_irq_vector(pf->pdev,
1642 pf->hw.nix_msixoff + NIX_LF_QINT_VEC_START);
1643 otx2_write64(pf, NIX_LF_QINTX_ENA_W1C(0), BIT_ULL(0));
1644 synchronize_irq(vec);
1647 /* Cleanup CQ NAPI and IRQ */
1648 vec = pf->hw.nix_msixoff + NIX_LF_CINT_VEC_START;
1649 for (qidx = 0; qidx < pf->hw.cint_cnt; qidx++) {
1650 /* Disable interrupt */
1651 otx2_write64(pf, NIX_LF_CINTX_ENA_W1C(qidx), BIT_ULL(0));
1653 synchronize_irq(pci_irq_vector(pf->pdev, vec));
1655 cq_poll = &qset->napi[qidx];
1656 napi_synchronize(&cq_poll->napi);
1660 netif_tx_disable(netdev);
1662 otx2_free_hw_resources(pf);
1663 otx2_free_cints(pf, pf->hw.cint_cnt);
1664 otx2_disable_napi(pf);
1666 for (qidx = 0; qidx < netdev->num_tx_queues; qidx++)
1667 netdev_tx_reset_queue(netdev_get_tx_queue(netdev, qidx));
1669 for (wrk = 0; wrk < pf->qset.cq_cnt; wrk++)
1670 cancel_delayed_work_sync(&pf->refill_wrk[wrk].pool_refill_work);
1671 devm_kfree(pf->dev, pf->refill_wrk);
1677 /* Do not clear RQ/SQ ringsize settings */
1678 memset((void *)qset + offsetof(struct otx2_qset, sqe_cnt), 0,
1679 sizeof(*qset) - offsetof(struct otx2_qset, sqe_cnt));
1682 EXPORT_SYMBOL(otx2_stop);
1684 static netdev_tx_t otx2_xmit(struct sk_buff *skb, struct net_device *netdev)
1686 struct otx2_nic *pf = netdev_priv(netdev);
1687 int qidx = skb_get_queue_mapping(skb);
1688 struct otx2_snd_queue *sq;
1689 struct netdev_queue *txq;
1691 /* Check for minimum and maximum packet length */
1692 if (skb->len <= ETH_HLEN ||
1693 (!skb_shinfo(skb)->gso_size && skb->len > pf->max_frs)) {
1695 return NETDEV_TX_OK;
1698 sq = &pf->qset.sq[qidx];
1699 txq = netdev_get_tx_queue(netdev, qidx);
1701 if (!otx2_sq_append_skb(netdev, sq, skb, qidx)) {
1702 netif_tx_stop_queue(txq);
1704 /* Check again, incase SQBs got freed up */
1706 if (((sq->num_sqbs - *sq->aura_fc_addr) * sq->sqe_per_sqb)
1708 netif_tx_wake_queue(txq);
1710 return NETDEV_TX_BUSY;
1713 return NETDEV_TX_OK;
1716 static void otx2_set_rx_mode(struct net_device *netdev)
1718 struct otx2_nic *pf = netdev_priv(netdev);
1720 queue_work(pf->otx2_wq, &pf->rx_mode_work);
1723 static void otx2_do_set_rx_mode(struct work_struct *work)
1725 struct otx2_nic *pf = container_of(work, struct otx2_nic, rx_mode_work);
1726 struct net_device *netdev = pf->netdev;
1727 struct nix_rx_mode *req;
1728 bool promisc = false;
1730 if (!(netdev->flags & IFF_UP))
1733 if ((netdev->flags & IFF_PROMISC) ||
1734 (netdev_uc_count(netdev) > OTX2_MAX_UNICAST_FLOWS)) {
1738 /* Write unicast address to mcam entries or del from mcam */
1739 if (!promisc && netdev->priv_flags & IFF_UNICAST_FLT)
1740 __dev_uc_sync(netdev, otx2_add_macfilter, otx2_del_macfilter);
1742 mutex_lock(&pf->mbox.lock);
1743 req = otx2_mbox_alloc_msg_nix_set_rx_mode(&pf->mbox);
1745 mutex_unlock(&pf->mbox.lock);
1749 req->mode = NIX_RX_MODE_UCAST;
1752 req->mode |= NIX_RX_MODE_PROMISC;
1753 else if (netdev->flags & (IFF_ALLMULTI | IFF_MULTICAST))
1754 req->mode |= NIX_RX_MODE_ALLMULTI;
1756 otx2_sync_mbox_msg(&pf->mbox);
1757 mutex_unlock(&pf->mbox.lock);
1760 static int otx2_set_features(struct net_device *netdev,
1761 netdev_features_t features)
1763 netdev_features_t changed = features ^ netdev->features;
1764 bool ntuple = !!(features & NETIF_F_NTUPLE);
1765 struct otx2_nic *pf = netdev_priv(netdev);
1767 if ((changed & NETIF_F_LOOPBACK) && netif_running(netdev))
1768 return otx2_cgx_config_loopback(pf,
1769 features & NETIF_F_LOOPBACK);
1771 if ((changed & NETIF_F_HW_VLAN_CTAG_RX) && netif_running(netdev))
1772 return otx2_enable_rxvlan(pf,
1773 features & NETIF_F_HW_VLAN_CTAG_RX);
1775 if ((changed & NETIF_F_NTUPLE) && !ntuple)
1776 otx2_destroy_ntuple_flows(pf);
1781 static void otx2_reset_task(struct work_struct *work)
1783 struct otx2_nic *pf = container_of(work, struct otx2_nic, reset_task);
1785 if (!netif_running(pf->netdev))
1789 otx2_stop(pf->netdev);
1791 otx2_open(pf->netdev);
1792 netif_trans_update(pf->netdev);
1796 static int otx2_config_hw_rx_tstamp(struct otx2_nic *pfvf, bool enable)
1798 struct msg_req *req;
1801 if (pfvf->flags & OTX2_FLAG_RX_TSTAMP_ENABLED && enable)
1804 mutex_lock(&pfvf->mbox.lock);
1806 req = otx2_mbox_alloc_msg_cgx_ptp_rx_enable(&pfvf->mbox);
1808 req = otx2_mbox_alloc_msg_cgx_ptp_rx_disable(&pfvf->mbox);
1810 mutex_unlock(&pfvf->mbox.lock);
1814 err = otx2_sync_mbox_msg(&pfvf->mbox);
1816 mutex_unlock(&pfvf->mbox.lock);
1820 mutex_unlock(&pfvf->mbox.lock);
1822 pfvf->flags |= OTX2_FLAG_RX_TSTAMP_ENABLED;
1824 pfvf->flags &= ~OTX2_FLAG_RX_TSTAMP_ENABLED;
1828 static int otx2_config_hw_tx_tstamp(struct otx2_nic *pfvf, bool enable)
1830 struct msg_req *req;
1833 if (pfvf->flags & OTX2_FLAG_TX_TSTAMP_ENABLED && enable)
1836 mutex_lock(&pfvf->mbox.lock);
1838 req = otx2_mbox_alloc_msg_nix_lf_ptp_tx_enable(&pfvf->mbox);
1840 req = otx2_mbox_alloc_msg_nix_lf_ptp_tx_disable(&pfvf->mbox);
1842 mutex_unlock(&pfvf->mbox.lock);
1846 err = otx2_sync_mbox_msg(&pfvf->mbox);
1848 mutex_unlock(&pfvf->mbox.lock);
1852 mutex_unlock(&pfvf->mbox.lock);
1854 pfvf->flags |= OTX2_FLAG_TX_TSTAMP_ENABLED;
1856 pfvf->flags &= ~OTX2_FLAG_TX_TSTAMP_ENABLED;
1860 static int otx2_config_hwtstamp(struct net_device *netdev, struct ifreq *ifr)
1862 struct otx2_nic *pfvf = netdev_priv(netdev);
1863 struct hwtstamp_config config;
1868 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
1871 /* reserved for future extensions */
1875 switch (config.tx_type) {
1876 case HWTSTAMP_TX_OFF:
1877 otx2_config_hw_tx_tstamp(pfvf, false);
1879 case HWTSTAMP_TX_ON:
1880 otx2_config_hw_tx_tstamp(pfvf, true);
1886 switch (config.rx_filter) {
1887 case HWTSTAMP_FILTER_NONE:
1888 otx2_config_hw_rx_tstamp(pfvf, false);
1890 case HWTSTAMP_FILTER_ALL:
1891 case HWTSTAMP_FILTER_SOME:
1892 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1893 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1894 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1895 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1896 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1897 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1898 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1899 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1900 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1901 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1902 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1903 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1904 otx2_config_hw_rx_tstamp(pfvf, true);
1905 config.rx_filter = HWTSTAMP_FILTER_ALL;
1911 memcpy(&pfvf->tstamp, &config, sizeof(config));
1913 return copy_to_user(ifr->ifr_data, &config,
1914 sizeof(config)) ? -EFAULT : 0;
1917 static int otx2_ioctl(struct net_device *netdev, struct ifreq *req, int cmd)
1919 struct otx2_nic *pfvf = netdev_priv(netdev);
1920 struct hwtstamp_config *cfg = &pfvf->tstamp;
1924 return otx2_config_hwtstamp(netdev, req);
1926 return copy_to_user(req->ifr_data, cfg,
1927 sizeof(*cfg)) ? -EFAULT : 0;
1933 static int otx2_do_set_vf_mac(struct otx2_nic *pf, int vf, const u8 *mac)
1935 struct npc_install_flow_req *req;
1938 mutex_lock(&pf->mbox.lock);
1939 req = otx2_mbox_alloc_msg_npc_install_flow(&pf->mbox);
1945 ether_addr_copy(req->packet.dmac, mac);
1946 eth_broadcast_addr((u8 *)&req->mask.dmac);
1947 req->features = BIT_ULL(NPC_DMAC);
1948 req->channel = pf->hw.rx_chan_base;
1949 req->intf = NIX_INTF_RX;
1950 req->default_rule = 1;
1953 req->op = NIX_RX_ACTION_DEFAULT;
1955 err = otx2_sync_mbox_msg(&pf->mbox);
1957 mutex_unlock(&pf->mbox.lock);
1961 static int otx2_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
1963 struct otx2_nic *pf = netdev_priv(netdev);
1964 struct pci_dev *pdev = pf->pdev;
1965 struct otx2_vf_config *config;
1968 if (!netif_running(netdev))
1971 if (vf >= pci_num_vf(pdev))
1974 if (!is_valid_ether_addr(mac))
1977 config = &pf->vf_configs[vf];
1978 ether_addr_copy(config->mac, mac);
1980 ret = otx2_do_set_vf_mac(pf, vf, mac);
1982 dev_info(&pdev->dev, "Reload VF driver to apply the changes\n");
1987 static int otx2_do_set_vf_vlan(struct otx2_nic *pf, int vf, u16 vlan, u8 qos,
1990 struct otx2_flow_config *flow_cfg = pf->flow_cfg;
1991 struct nix_vtag_config_rsp *vtag_rsp;
1992 struct npc_delete_flow_req *del_req;
1993 struct nix_vtag_config *vtag_req;
1994 struct npc_install_flow_req *req;
1995 struct otx2_vf_config *config;
1999 config = &pf->vf_configs[vf];
2001 if (!vlan && !config->vlan)
2004 mutex_lock(&pf->mbox.lock);
2006 /* free old tx vtag entry */
2008 vtag_req = otx2_mbox_alloc_msg_nix_vtag_cfg(&pf->mbox);
2013 vtag_req->cfg_type = 0;
2014 vtag_req->tx.free_vtag0 = 1;
2015 vtag_req->tx.vtag0_idx = config->tx_vtag_idx;
2017 err = otx2_sync_mbox_msg(&pf->mbox);
2022 if (!vlan && config->vlan) {
2024 del_req = otx2_mbox_alloc_msg_npc_delete_flow(&pf->mbox);
2029 idx = ((vf * OTX2_PER_VF_VLAN_FLOWS) + OTX2_VF_VLAN_RX_INDEX);
2031 flow_cfg->entry[flow_cfg->vf_vlan_offset + idx];
2032 err = otx2_sync_mbox_msg(&pf->mbox);
2037 del_req = otx2_mbox_alloc_msg_npc_delete_flow(&pf->mbox);
2042 idx = ((vf * OTX2_PER_VF_VLAN_FLOWS) + OTX2_VF_VLAN_TX_INDEX);
2044 flow_cfg->entry[flow_cfg->vf_vlan_offset + idx];
2045 err = otx2_sync_mbox_msg(&pf->mbox);
2051 req = otx2_mbox_alloc_msg_npc_install_flow(&pf->mbox);
2057 idx = ((vf * OTX2_PER_VF_VLAN_FLOWS) + OTX2_VF_VLAN_RX_INDEX);
2058 req->entry = flow_cfg->entry[flow_cfg->vf_vlan_offset + idx];
2059 req->packet.vlan_tci = htons(vlan);
2060 req->mask.vlan_tci = htons(VLAN_VID_MASK);
2061 /* af fills the destination mac addr */
2062 eth_broadcast_addr((u8 *)&req->mask.dmac);
2063 req->features = BIT_ULL(NPC_OUTER_VID) | BIT_ULL(NPC_DMAC);
2064 req->channel = pf->hw.rx_chan_base;
2065 req->intf = NIX_INTF_RX;
2067 req->op = NIX_RX_ACTION_DEFAULT;
2068 req->vtag0_valid = true;
2069 req->vtag0_type = NIX_AF_LFX_RX_VTAG_TYPE7;
2072 err = otx2_sync_mbox_msg(&pf->mbox);
2077 vtag_req = otx2_mbox_alloc_msg_nix_vtag_cfg(&pf->mbox);
2083 /* configure tx vtag params */
2084 vtag_req->vtag_size = VTAGSIZE_T4;
2085 vtag_req->cfg_type = 0; /* tx vlan cfg */
2086 vtag_req->tx.cfg_vtag0 = 1;
2087 vtag_req->tx.vtag0 = ((u64)ntohs(proto) << 16) | vlan;
2089 err = otx2_sync_mbox_msg(&pf->mbox);
2093 vtag_rsp = (struct nix_vtag_config_rsp *)otx2_mbox_get_rsp
2094 (&pf->mbox.mbox, 0, &vtag_req->hdr);
2095 if (IS_ERR(vtag_rsp)) {
2096 err = PTR_ERR(vtag_rsp);
2099 config->tx_vtag_idx = vtag_rsp->vtag0_idx;
2101 req = otx2_mbox_alloc_msg_npc_install_flow(&pf->mbox);
2107 eth_zero_addr((u8 *)&req->mask.dmac);
2108 idx = ((vf * OTX2_PER_VF_VLAN_FLOWS) + OTX2_VF_VLAN_TX_INDEX);
2109 req->entry = flow_cfg->entry[flow_cfg->vf_vlan_offset + idx];
2110 req->features = BIT_ULL(NPC_DMAC);
2111 req->channel = pf->hw.tx_chan_base;
2112 req->intf = NIX_INTF_TX;
2114 req->op = NIX_TX_ACTIONOP_UCAST_DEFAULT;
2115 req->vtag0_def = vtag_rsp->vtag0_idx;
2116 req->vtag0_op = VTAG_INSERT;
2119 err = otx2_sync_mbox_msg(&pf->mbox);
2121 config->vlan = vlan;
2122 mutex_unlock(&pf->mbox.lock);
2126 static int otx2_set_vf_vlan(struct net_device *netdev, int vf, u16 vlan, u8 qos,
2129 struct otx2_nic *pf = netdev_priv(netdev);
2130 struct pci_dev *pdev = pf->pdev;
2132 if (!netif_running(netdev))
2135 if (vf >= pci_num_vf(pdev))
2138 /* qos is currently unsupported */
2139 if (vlan >= VLAN_N_VID || qos)
2142 if (proto != htons(ETH_P_8021Q))
2143 return -EPROTONOSUPPORT;
2145 if (!(pf->flags & OTX2_FLAG_VF_VLAN_SUPPORT))
2148 return otx2_do_set_vf_vlan(pf, vf, vlan, qos, proto);
2151 static int otx2_get_vf_config(struct net_device *netdev, int vf,
2152 struct ifla_vf_info *ivi)
2154 struct otx2_nic *pf = netdev_priv(netdev);
2155 struct pci_dev *pdev = pf->pdev;
2156 struct otx2_vf_config *config;
2158 if (!netif_running(netdev))
2161 if (vf >= pci_num_vf(pdev))
2164 config = &pf->vf_configs[vf];
2166 ether_addr_copy(ivi->mac, config->mac);
2167 ivi->vlan = config->vlan;
2172 static const struct net_device_ops otx2_netdev_ops = {
2173 .ndo_open = otx2_open,
2174 .ndo_stop = otx2_stop,
2175 .ndo_start_xmit = otx2_xmit,
2176 .ndo_set_mac_address = otx2_set_mac_address,
2177 .ndo_change_mtu = otx2_change_mtu,
2178 .ndo_set_rx_mode = otx2_set_rx_mode,
2179 .ndo_set_features = otx2_set_features,
2180 .ndo_tx_timeout = otx2_tx_timeout,
2181 .ndo_get_stats64 = otx2_get_stats64,
2182 .ndo_do_ioctl = otx2_ioctl,
2183 .ndo_set_vf_mac = otx2_set_vf_mac,
2184 .ndo_set_vf_vlan = otx2_set_vf_vlan,
2185 .ndo_get_vf_config = otx2_get_vf_config,
2188 static int otx2_wq_init(struct otx2_nic *pf)
2190 pf->otx2_wq = create_singlethread_workqueue("otx2_wq");
2194 INIT_WORK(&pf->rx_mode_work, otx2_do_set_rx_mode);
2195 INIT_WORK(&pf->reset_task, otx2_reset_task);
2199 static int otx2_check_pf_usable(struct otx2_nic *nic)
2203 rev = otx2_read64(nic, RVU_PF_BLOCK_ADDRX_DISC(BLKADDR_RVUM));
2204 rev = (rev >> 12) & 0xFF;
2205 /* Check if AF has setup revision for RVUM block,
2206 * otherwise this driver probe should be deferred
2207 * until AF driver comes up.
2211 "AF is not initialized, deferring probe\n");
2212 return -EPROBE_DEFER;
2217 static int otx2_realloc_msix_vectors(struct otx2_nic *pf)
2219 struct otx2_hw *hw = &pf->hw;
2222 /* NPA interrupts are inot registered, so alloc only
2223 * upto NIX vector offset.
2225 num_vec = hw->nix_msixoff;
2226 num_vec += NIX_LF_CINT_VEC_START + hw->max_queues;
2228 otx2_disable_mbox_intr(pf);
2229 pci_free_irq_vectors(hw->pdev);
2230 err = pci_alloc_irq_vectors(hw->pdev, num_vec, num_vec, PCI_IRQ_MSIX);
2232 dev_err(pf->dev, "%s: Failed to realloc %d IRQ vectors\n",
2237 return otx2_register_mbox_intr(pf, false);
2240 static int otx2_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2242 struct device *dev = &pdev->dev;
2243 struct net_device *netdev;
2244 struct otx2_nic *pf;
2249 err = pcim_enable_device(pdev);
2251 dev_err(dev, "Failed to enable PCI device\n");
2255 err = pci_request_regions(pdev, DRV_NAME);
2257 dev_err(dev, "PCI request regions failed 0x%x\n", err);
2261 err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48));
2263 dev_err(dev, "DMA mask config failed, abort\n");
2264 goto err_release_regions;
2267 pci_set_master(pdev);
2269 /* Set number of queues */
2270 qcount = min_t(int, num_online_cpus(), OTX2_MAX_CQ_CNT);
2272 netdev = alloc_etherdev_mqs(sizeof(*pf), qcount, qcount);
2275 goto err_release_regions;
2278 pci_set_drvdata(pdev, netdev);
2279 SET_NETDEV_DEV(netdev, &pdev->dev);
2280 pf = netdev_priv(netdev);
2281 pf->netdev = netdev;
2284 pf->total_vfs = pci_sriov_get_totalvfs(pdev);
2285 pf->flags |= OTX2_FLAG_INTF_DOWN;
2289 hw->rx_queues = qcount;
2290 hw->tx_queues = qcount;
2291 hw->max_queues = qcount;
2293 num_vec = pci_msix_vec_count(pdev);
2294 hw->irq_name = devm_kmalloc_array(&hw->pdev->dev, num_vec, NAME_SIZE,
2296 if (!hw->irq_name) {
2298 goto err_free_netdev;
2301 hw->affinity_mask = devm_kcalloc(&hw->pdev->dev, num_vec,
2302 sizeof(cpumask_var_t), GFP_KERNEL);
2303 if (!hw->affinity_mask) {
2305 goto err_free_netdev;
2309 pf->reg_base = pcim_iomap(pdev, PCI_CFG_REG_BAR_NUM, 0);
2310 if (!pf->reg_base) {
2311 dev_err(dev, "Unable to map physical function CSRs, aborting\n");
2313 goto err_free_netdev;
2316 err = otx2_check_pf_usable(pf);
2318 goto err_free_netdev;
2320 err = pci_alloc_irq_vectors(hw->pdev, RVU_PF_INT_VEC_CNT,
2321 RVU_PF_INT_VEC_CNT, PCI_IRQ_MSIX);
2323 dev_err(dev, "%s: Failed to alloc %d IRQ vectors\n",
2325 goto err_free_netdev;
2328 /* Init PF <=> AF mailbox stuff */
2329 err = otx2_pfaf_mbox_init(pf);
2331 goto err_free_irq_vectors;
2333 /* Register mailbox interrupt */
2334 err = otx2_register_mbox_intr(pf, true);
2336 goto err_mbox_destroy;
2338 /* Request AF to attach NPA and NIX LFs to this PF.
2339 * NIX and NPA LFs are needed for this PF to function as a NIC.
2341 err = otx2_attach_npa_nix(pf);
2343 goto err_disable_mbox_intr;
2345 err = otx2_realloc_msix_vectors(pf);
2347 goto err_detach_rsrc;
2349 err = otx2_set_real_num_queues(netdev, hw->tx_queues, hw->rx_queues);
2351 goto err_detach_rsrc;
2353 otx2_setup_dev_hw_settings(pf);
2355 /* Assign default mac address */
2356 otx2_get_mac_from_af(netdev);
2358 /* Don't check for error. Proceed without ptp */
2361 /* NPA's pool is a stack to which SW frees buffer pointers via Aura.
2362 * HW allocates buffer pointer from stack and uses it for DMA'ing
2363 * ingress packet. In some scenarios HW can free back allocated buffer
2364 * pointers to pool. This makes it impossible for SW to maintain a
2365 * parallel list where physical addresses of buffer pointers (IOVAs)
2366 * given to HW can be saved for later reference.
2368 * So the only way to convert Rx packet's buffer address is to use
2369 * IOMMU's iova_to_phys() handler which translates the address by
2370 * walking through the translation tables.
2372 pf->iommu_domain = iommu_get_domain_for_dev(dev);
2374 netdev->hw_features = (NETIF_F_RXCSUM | NETIF_F_IP_CSUM |
2375 NETIF_F_IPV6_CSUM | NETIF_F_RXHASH |
2376 NETIF_F_SG | NETIF_F_TSO | NETIF_F_TSO6 |
2377 NETIF_F_GSO_UDP_L4);
2378 netdev->features |= netdev->hw_features;
2380 netdev->hw_features |= NETIF_F_LOOPBACK | NETIF_F_RXALL;
2382 err = otx2_mcam_flow_init(pf);
2384 goto err_ptp_destroy;
2386 if (pf->flags & OTX2_FLAG_NTUPLE_SUPPORT)
2387 netdev->hw_features |= NETIF_F_NTUPLE;
2389 if (pf->flags & OTX2_FLAG_UCAST_FLTR_SUPPORT)
2390 netdev->priv_flags |= IFF_UNICAST_FLT;
2392 /* Support TSO on tag interface */
2393 netdev->vlan_features |= netdev->features;
2394 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX |
2395 NETIF_F_HW_VLAN_STAG_TX;
2396 if (pf->flags & OTX2_FLAG_RX_VLAN_SUPPORT)
2397 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX |
2398 NETIF_F_HW_VLAN_STAG_RX;
2399 netdev->features |= netdev->hw_features;
2401 netdev->gso_max_segs = OTX2_MAX_GSO_SEGS;
2402 netdev->watchdog_timeo = OTX2_TX_TIMEOUT;
2404 netdev->netdev_ops = &otx2_netdev_ops;
2406 /* MTU range: 64 - 9190 */
2407 netdev->min_mtu = OTX2_MIN_MTU;
2408 netdev->max_mtu = OTX2_MAX_MTU;
2410 err = register_netdev(netdev);
2412 dev_err(dev, "Failed to register netdevice\n");
2413 goto err_del_mcam_entries;
2416 err = otx2_wq_init(pf);
2418 goto err_unreg_netdev;
2420 otx2_set_ethtool_ops(netdev);
2422 /* Enable link notifications */
2423 otx2_cgx_config_linkevents(pf, true);
2425 /* Enable pause frames by default */
2426 pf->flags |= OTX2_FLAG_RX_PAUSE_ENABLED;
2427 pf->flags |= OTX2_FLAG_TX_PAUSE_ENABLED;
2432 unregister_netdev(netdev);
2433 err_del_mcam_entries:
2434 otx2_mcam_flow_del(pf);
2436 otx2_ptp_destroy(pf);
2438 otx2_detach_resources(&pf->mbox);
2439 err_disable_mbox_intr:
2440 otx2_disable_mbox_intr(pf);
2442 otx2_pfaf_mbox_destroy(pf);
2443 err_free_irq_vectors:
2444 pci_free_irq_vectors(hw->pdev);
2446 pci_set_drvdata(pdev, NULL);
2447 free_netdev(netdev);
2448 err_release_regions:
2449 pci_release_regions(pdev);
2453 static void otx2_vf_link_event_task(struct work_struct *work)
2455 struct otx2_vf_config *config;
2456 struct cgx_link_info_msg *req;
2457 struct mbox_msghdr *msghdr;
2458 struct otx2_nic *pf;
2461 config = container_of(work, struct otx2_vf_config,
2462 link_event_work.work);
2463 vf_idx = config - config->pf->vf_configs;
2466 msghdr = otx2_mbox_alloc_msg_rsp(&pf->mbox_pfvf[0].mbox_up, vf_idx,
2467 sizeof(*req), sizeof(struct msg_rsp));
2469 dev_err(pf->dev, "Failed to create VF%d link event\n", vf_idx);
2473 req = (struct cgx_link_info_msg *)msghdr;
2474 req->hdr.id = MBOX_MSG_CGX_LINK_EVENT;
2475 req->hdr.sig = OTX2_MBOX_REQ_SIG;
2476 memcpy(&req->link_info, &pf->linfo, sizeof(req->link_info));
2478 otx2_sync_mbox_up_msg(&pf->mbox_pfvf[0], vf_idx);
2481 static int otx2_sriov_enable(struct pci_dev *pdev, int numvfs)
2483 struct net_device *netdev = pci_get_drvdata(pdev);
2484 struct otx2_nic *pf = netdev_priv(netdev);
2487 /* Init PF <=> VF mailbox stuff */
2488 ret = otx2_pfvf_mbox_init(pf, numvfs);
2492 ret = otx2_register_pfvf_mbox_intr(pf, numvfs);
2496 pf->vf_configs = kcalloc(numvfs, sizeof(struct otx2_vf_config),
2498 if (!pf->vf_configs) {
2503 for (i = 0; i < numvfs; i++) {
2504 pf->vf_configs[i].pf = pf;
2505 pf->vf_configs[i].intf_down = true;
2506 INIT_DELAYED_WORK(&pf->vf_configs[i].link_event_work,
2507 otx2_vf_link_event_task);
2510 ret = otx2_pf_flr_init(pf, numvfs);
2514 ret = otx2_register_flr_me_intr(pf, numvfs);
2518 ret = pci_enable_sriov(pdev, numvfs);
2524 otx2_disable_flr_me_intr(pf);
2526 otx2_flr_wq_destroy(pf);
2528 kfree(pf->vf_configs);
2530 otx2_disable_pfvf_mbox_intr(pf, numvfs);
2532 otx2_pfvf_mbox_destroy(pf);
2536 static int otx2_sriov_disable(struct pci_dev *pdev)
2538 struct net_device *netdev = pci_get_drvdata(pdev);
2539 struct otx2_nic *pf = netdev_priv(netdev);
2540 int numvfs = pci_num_vf(pdev);
2546 pci_disable_sriov(pdev);
2548 for (i = 0; i < pci_num_vf(pdev); i++)
2549 cancel_delayed_work_sync(&pf->vf_configs[i].link_event_work);
2550 kfree(pf->vf_configs);
2552 otx2_disable_flr_me_intr(pf);
2553 otx2_flr_wq_destroy(pf);
2554 otx2_disable_pfvf_mbox_intr(pf, numvfs);
2555 otx2_pfvf_mbox_destroy(pf);
2560 static int otx2_sriov_configure(struct pci_dev *pdev, int numvfs)
2563 return otx2_sriov_disable(pdev);
2565 return otx2_sriov_enable(pdev, numvfs);
2568 static void otx2_remove(struct pci_dev *pdev)
2570 struct net_device *netdev = pci_get_drvdata(pdev);
2571 struct otx2_nic *pf;
2576 pf = netdev_priv(netdev);
2578 pf->flags |= OTX2_FLAG_PF_SHUTDOWN;
2580 if (pf->flags & OTX2_FLAG_TX_TSTAMP_ENABLED)
2581 otx2_config_hw_tx_tstamp(pf, false);
2582 if (pf->flags & OTX2_FLAG_RX_TSTAMP_ENABLED)
2583 otx2_config_hw_rx_tstamp(pf, false);
2585 cancel_work_sync(&pf->reset_task);
2586 /* Disable link notifications */
2587 otx2_cgx_config_linkevents(pf, false);
2589 unregister_netdev(netdev);
2590 otx2_sriov_disable(pf->pdev);
2592 destroy_workqueue(pf->otx2_wq);
2594 otx2_ptp_destroy(pf);
2595 otx2_mcam_flow_del(pf);
2596 otx2_detach_resources(&pf->mbox);
2597 otx2_disable_mbox_intr(pf);
2598 otx2_pfaf_mbox_destroy(pf);
2599 pci_free_irq_vectors(pf->pdev);
2600 pci_set_drvdata(pdev, NULL);
2601 free_netdev(netdev);
2603 pci_release_regions(pdev);
2606 static struct pci_driver otx2_pf_driver = {
2608 .id_table = otx2_pf_id_table,
2609 .probe = otx2_probe,
2610 .shutdown = otx2_remove,
2611 .remove = otx2_remove,
2612 .sriov_configure = otx2_sriov_configure
2615 static int __init otx2_rvupf_init_module(void)
2617 pr_info("%s: %s\n", DRV_NAME, DRV_STRING);
2619 return pci_register_driver(&otx2_pf_driver);
2622 static void __exit otx2_rvupf_cleanup_module(void)
2624 pci_unregister_driver(&otx2_pf_driver);
2627 module_init(otx2_rvupf_init_module);
2628 module_exit(otx2_rvupf_cleanup_module);