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>
21 #include "otx2_common.h"
22 #include "otx2_txrx.h"
23 #include "otx2_struct.h"
27 #include <rvu_trace.h>
29 #define DRV_NAME "rvu_nicpf"
30 #define DRV_STRING "Marvell RVU NIC Physical Function Driver"
32 /* Supported devices */
33 static const struct pci_device_id otx2_pf_id_table[] = {
34 { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVID_OCTEONTX2_RVU_PF) },
35 { 0, } /* end of table */
38 MODULE_AUTHOR("Sunil Goutham <sgoutham@marvell.com>");
39 MODULE_DESCRIPTION(DRV_STRING);
40 MODULE_LICENSE("GPL v2");
41 MODULE_DEVICE_TABLE(pci, otx2_pf_id_table);
43 static void otx2_vf_link_event_task(struct work_struct *work);
50 static int otx2_config_hw_tx_tstamp(struct otx2_nic *pfvf, bool enable);
51 static int otx2_config_hw_rx_tstamp(struct otx2_nic *pfvf, bool enable);
53 static int otx2_change_mtu(struct net_device *netdev, int new_mtu)
55 struct otx2_nic *pf = netdev_priv(netdev);
56 bool if_up = netif_running(netdev);
59 if (pf->xdp_prog && new_mtu > MAX_XDP_MTU) {
60 netdev_warn(netdev, "Jumbo frames not yet supported with XDP, current MTU %d.\n",
67 netdev_info(netdev, "Changing MTU from %d to %d\n",
68 netdev->mtu, new_mtu);
69 netdev->mtu = new_mtu;
72 err = otx2_open(netdev);
77 static void otx2_disable_flr_me_intr(struct otx2_nic *pf)
79 int irq, vfs = pf->total_vfs;
81 /* Disable VFs ME interrupts */
82 otx2_write64(pf, RVU_PF_VFME_INT_ENA_W1CX(0), INTR_MASK(vfs));
83 irq = pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFME0);
86 /* Disable VFs FLR interrupts */
87 otx2_write64(pf, RVU_PF_VFFLR_INT_ENA_W1CX(0), INTR_MASK(vfs));
88 irq = pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFFLR0);
94 otx2_write64(pf, RVU_PF_VFME_INT_ENA_W1CX(1), INTR_MASK(vfs - 64));
95 irq = pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFME1);
98 otx2_write64(pf, RVU_PF_VFFLR_INT_ENA_W1CX(1), INTR_MASK(vfs - 64));
99 irq = pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFFLR1);
103 static void otx2_flr_wq_destroy(struct otx2_nic *pf)
107 destroy_workqueue(pf->flr_wq);
109 devm_kfree(pf->dev, pf->flr_wrk);
112 static void otx2_flr_handler(struct work_struct *work)
114 struct flr_work *flrwork = container_of(work, struct flr_work, work);
115 struct otx2_nic *pf = flrwork->pf;
116 struct mbox *mbox = &pf->mbox;
120 vf = flrwork - pf->flr_wrk;
122 mutex_lock(&mbox->lock);
123 req = otx2_mbox_alloc_msg_vf_flr(mbox);
125 mutex_unlock(&mbox->lock);
128 req->hdr.pcifunc &= RVU_PFVF_FUNC_MASK;
129 req->hdr.pcifunc |= (vf + 1) & RVU_PFVF_FUNC_MASK;
131 if (!otx2_sync_mbox_msg(&pf->mbox)) {
136 /* clear transcation pending bit */
137 otx2_write64(pf, RVU_PF_VFTRPENDX(reg), BIT_ULL(vf));
138 otx2_write64(pf, RVU_PF_VFFLR_INT_ENA_W1SX(reg), BIT_ULL(vf));
141 mutex_unlock(&mbox->lock);
144 static irqreturn_t otx2_pf_flr_intr_handler(int irq, void *pf_irq)
146 struct otx2_nic *pf = (struct otx2_nic *)pf_irq;
147 int reg, dev, vf, start_vf, num_reg = 1;
150 if (pf->total_vfs > 64)
153 for (reg = 0; reg < num_reg; reg++) {
154 intr = otx2_read64(pf, RVU_PF_VFFLR_INTX(reg));
158 for (vf = 0; vf < 64; vf++) {
159 if (!(intr & BIT_ULL(vf)))
162 queue_work(pf->flr_wq, &pf->flr_wrk[dev].work);
163 /* Clear interrupt */
164 otx2_write64(pf, RVU_PF_VFFLR_INTX(reg), BIT_ULL(vf));
165 /* Disable the interrupt */
166 otx2_write64(pf, RVU_PF_VFFLR_INT_ENA_W1CX(reg),
173 static irqreturn_t otx2_pf_me_intr_handler(int irq, void *pf_irq)
175 struct otx2_nic *pf = (struct otx2_nic *)pf_irq;
176 int vf, reg, num_reg = 1;
179 if (pf->total_vfs > 64)
182 for (reg = 0; reg < num_reg; reg++) {
183 intr = otx2_read64(pf, RVU_PF_VFME_INTX(reg));
186 for (vf = 0; vf < 64; vf++) {
187 if (!(intr & BIT_ULL(vf)))
189 /* clear trpend bit */
190 otx2_write64(pf, RVU_PF_VFTRPENDX(reg), BIT_ULL(vf));
191 /* clear interrupt */
192 otx2_write64(pf, RVU_PF_VFME_INTX(reg), BIT_ULL(vf));
198 static int otx2_register_flr_me_intr(struct otx2_nic *pf, int numvfs)
200 struct otx2_hw *hw = &pf->hw;
204 /* Register ME interrupt handler*/
205 irq_name = &hw->irq_name[RVU_PF_INT_VEC_VFME0 * NAME_SIZE];
206 snprintf(irq_name, NAME_SIZE, "RVUPF%d_ME0", rvu_get_pf(pf->pcifunc));
207 ret = request_irq(pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFME0),
208 otx2_pf_me_intr_handler, 0, irq_name, pf);
211 "RVUPF: IRQ registration failed for ME0\n");
214 /* Register FLR interrupt handler */
215 irq_name = &hw->irq_name[RVU_PF_INT_VEC_VFFLR0 * NAME_SIZE];
216 snprintf(irq_name, NAME_SIZE, "RVUPF%d_FLR0", rvu_get_pf(pf->pcifunc));
217 ret = request_irq(pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFFLR0),
218 otx2_pf_flr_intr_handler, 0, irq_name, pf);
221 "RVUPF: IRQ registration failed for FLR0\n");
226 irq_name = &hw->irq_name[RVU_PF_INT_VEC_VFME1 * NAME_SIZE];
227 snprintf(irq_name, NAME_SIZE, "RVUPF%d_ME1",
228 rvu_get_pf(pf->pcifunc));
229 ret = request_irq(pci_irq_vector
230 (pf->pdev, RVU_PF_INT_VEC_VFME1),
231 otx2_pf_me_intr_handler, 0, irq_name, pf);
234 "RVUPF: IRQ registration failed for ME1\n");
236 irq_name = &hw->irq_name[RVU_PF_INT_VEC_VFFLR1 * NAME_SIZE];
237 snprintf(irq_name, NAME_SIZE, "RVUPF%d_FLR1",
238 rvu_get_pf(pf->pcifunc));
239 ret = request_irq(pci_irq_vector
240 (pf->pdev, RVU_PF_INT_VEC_VFFLR1),
241 otx2_pf_flr_intr_handler, 0, irq_name, pf);
244 "RVUPF: IRQ registration failed for FLR1\n");
249 /* Enable ME interrupt for all VFs*/
250 otx2_write64(pf, RVU_PF_VFME_INTX(0), INTR_MASK(numvfs));
251 otx2_write64(pf, RVU_PF_VFME_INT_ENA_W1SX(0), INTR_MASK(numvfs));
253 /* Enable FLR interrupt for all VFs*/
254 otx2_write64(pf, RVU_PF_VFFLR_INTX(0), INTR_MASK(numvfs));
255 otx2_write64(pf, RVU_PF_VFFLR_INT_ENA_W1SX(0), INTR_MASK(numvfs));
260 otx2_write64(pf, RVU_PF_VFME_INTX(1), INTR_MASK(numvfs));
261 otx2_write64(pf, RVU_PF_VFME_INT_ENA_W1SX(1),
264 otx2_write64(pf, RVU_PF_VFFLR_INTX(1), INTR_MASK(numvfs));
265 otx2_write64(pf, RVU_PF_VFFLR_INT_ENA_W1SX(1),
271 static int otx2_pf_flr_init(struct otx2_nic *pf, int num_vfs)
275 pf->flr_wq = alloc_ordered_workqueue("otx2_pf_flr_wq", WQ_HIGHPRI);
279 pf->flr_wrk = devm_kcalloc(pf->dev, num_vfs,
280 sizeof(struct flr_work), GFP_KERNEL);
282 destroy_workqueue(pf->flr_wq);
286 for (vf = 0; vf < num_vfs; vf++) {
287 pf->flr_wrk[vf].pf = pf;
288 INIT_WORK(&pf->flr_wrk[vf].work, otx2_flr_handler);
294 static void otx2_queue_work(struct mbox *mw, struct workqueue_struct *mbox_wq,
295 int first, int mdevs, u64 intr, int type)
297 struct otx2_mbox_dev *mdev;
298 struct otx2_mbox *mbox;
299 struct mbox_hdr *hdr;
302 for (i = first; i < mdevs; i++) {
304 if (!(intr & BIT_ULL(i - first)))
308 mdev = &mbox->dev[i];
309 if (type == TYPE_PFAF)
310 otx2_sync_mbox_bbuf(mbox, i);
311 hdr = mdev->mbase + mbox->rx_start;
312 /* The hdr->num_msgs is set to zero immediately in the interrupt
313 * handler to ensure that it holds a correct value next time
314 * when the interrupt handler is called.
315 * pf->mbox.num_msgs holds the data for use in pfaf_mbox_handler
316 * pf>mbox.up_num_msgs holds the data for use in
317 * pfaf_mbox_up_handler.
320 mw[i].num_msgs = hdr->num_msgs;
322 if (type == TYPE_PFAF)
323 memset(mbox->hwbase + mbox->rx_start, 0,
324 ALIGN(sizeof(struct mbox_hdr),
327 queue_work(mbox_wq, &mw[i].mbox_wrk);
331 mdev = &mbox->dev[i];
332 if (type == TYPE_PFAF)
333 otx2_sync_mbox_bbuf(mbox, i);
334 hdr = mdev->mbase + mbox->rx_start;
336 mw[i].up_num_msgs = hdr->num_msgs;
338 if (type == TYPE_PFAF)
339 memset(mbox->hwbase + mbox->rx_start, 0,
340 ALIGN(sizeof(struct mbox_hdr),
343 queue_work(mbox_wq, &mw[i].mbox_up_wrk);
348 static void otx2_forward_msg_pfvf(struct otx2_mbox_dev *mdev,
349 struct otx2_mbox *pfvf_mbox, void *bbuf_base,
352 struct otx2_mbox_dev *src_mdev = mdev;
355 /* Msgs are already copied, trigger VF's mbox irq */
358 offset = pfvf_mbox->trigger | (devid << pfvf_mbox->tr_shift);
359 writeq(1, (void __iomem *)pfvf_mbox->reg_base + offset);
361 /* Restore VF's mbox bounce buffer region address */
362 src_mdev->mbase = bbuf_base;
365 static int otx2_forward_vf_mbox_msgs(struct otx2_nic *pf,
366 struct otx2_mbox *src_mbox,
367 int dir, int vf, int num_msgs)
369 struct otx2_mbox_dev *src_mdev, *dst_mdev;
370 struct mbox_hdr *mbox_hdr;
371 struct mbox_hdr *req_hdr;
372 struct mbox *dst_mbox;
375 if (dir == MBOX_DIR_PFAF) {
376 /* Set VF's mailbox memory as PF's bounce buffer memory, so
377 * that explicit copying of VF's msgs to PF=>AF mbox region
378 * and AF=>PF responses to VF's mbox region can be avoided.
380 src_mdev = &src_mbox->dev[vf];
381 mbox_hdr = src_mbox->hwbase +
382 src_mbox->rx_start + (vf * MBOX_SIZE);
384 dst_mbox = &pf->mbox;
385 dst_size = dst_mbox->mbox.tx_size -
386 ALIGN(sizeof(*mbox_hdr), MBOX_MSG_ALIGN);
387 /* Check if msgs fit into destination area and has valid size */
388 if (mbox_hdr->msg_size > dst_size || !mbox_hdr->msg_size)
391 dst_mdev = &dst_mbox->mbox.dev[0];
393 mutex_lock(&pf->mbox.lock);
394 dst_mdev->mbase = src_mdev->mbase;
395 dst_mdev->msg_size = mbox_hdr->msg_size;
396 dst_mdev->num_msgs = num_msgs;
397 err = otx2_sync_mbox_msg(dst_mbox);
398 /* Error code -EIO indicate there is a communication failure
399 * to the AF. Rest of the error codes indicate that AF processed
400 * VF messages and set the error codes in response messages
401 * (if any) so simply forward responses to VF.
405 "AF not responding to VF%d messages\n", vf);
406 /* restore PF mbase and exit */
407 dst_mdev->mbase = pf->mbox.bbuf_base;
408 mutex_unlock(&pf->mbox.lock);
411 /* At this point, all the VF messages sent to AF are acked
412 * with proper responses and responses are copied to VF
413 * mailbox hence raise interrupt to VF.
415 req_hdr = (struct mbox_hdr *)(dst_mdev->mbase +
416 dst_mbox->mbox.rx_start);
417 req_hdr->num_msgs = num_msgs;
419 otx2_forward_msg_pfvf(dst_mdev, &pf->mbox_pfvf[0].mbox,
420 pf->mbox.bbuf_base, vf);
421 mutex_unlock(&pf->mbox.lock);
422 } else if (dir == MBOX_DIR_PFVF_UP) {
423 src_mdev = &src_mbox->dev[0];
424 mbox_hdr = src_mbox->hwbase + src_mbox->rx_start;
425 req_hdr = (struct mbox_hdr *)(src_mdev->mbase +
427 req_hdr->num_msgs = num_msgs;
429 dst_mbox = &pf->mbox_pfvf[0];
430 dst_size = dst_mbox->mbox_up.tx_size -
431 ALIGN(sizeof(*mbox_hdr), MBOX_MSG_ALIGN);
432 /* Check if msgs fit into destination area */
433 if (mbox_hdr->msg_size > dst_size)
436 dst_mdev = &dst_mbox->mbox_up.dev[vf];
437 dst_mdev->mbase = src_mdev->mbase;
438 dst_mdev->msg_size = mbox_hdr->msg_size;
439 dst_mdev->num_msgs = mbox_hdr->num_msgs;
440 err = otx2_sync_mbox_up_msg(dst_mbox, vf);
443 "VF%d is not responding to mailbox\n", vf);
446 } else if (dir == MBOX_DIR_VFPF_UP) {
447 req_hdr = (struct mbox_hdr *)(src_mbox->dev[0].mbase +
449 req_hdr->num_msgs = num_msgs;
450 otx2_forward_msg_pfvf(&pf->mbox_pfvf->mbox_up.dev[vf],
452 pf->mbox_pfvf[vf].bbuf_base,
459 static void otx2_pfvf_mbox_handler(struct work_struct *work)
461 struct mbox_msghdr *msg = NULL;
462 int offset, vf_idx, id, err;
463 struct otx2_mbox_dev *mdev;
464 struct mbox_hdr *req_hdr;
465 struct otx2_mbox *mbox;
466 struct mbox *vf_mbox;
469 vf_mbox = container_of(work, struct mbox, mbox_wrk);
471 vf_idx = vf_mbox - pf->mbox_pfvf;
473 mbox = &pf->mbox_pfvf[0].mbox;
474 mdev = &mbox->dev[vf_idx];
475 req_hdr = (struct mbox_hdr *)(mdev->mbase + mbox->rx_start);
477 offset = ALIGN(sizeof(*req_hdr), MBOX_MSG_ALIGN);
479 for (id = 0; id < vf_mbox->num_msgs; id++) {
480 msg = (struct mbox_msghdr *)(mdev->mbase + mbox->rx_start +
483 if (msg->sig != OTX2_MBOX_REQ_SIG)
486 /* Set VF's number in each of the msg */
487 msg->pcifunc &= RVU_PFVF_FUNC_MASK;
488 msg->pcifunc |= (vf_idx + 1) & RVU_PFVF_FUNC_MASK;
489 offset = msg->next_msgoff;
491 err = otx2_forward_vf_mbox_msgs(pf, mbox, MBOX_DIR_PFAF, vf_idx,
498 otx2_reply_invalid_msg(mbox, vf_idx, 0, msg->id);
499 otx2_mbox_msg_send(mbox, vf_idx);
502 static void otx2_pfvf_mbox_up_handler(struct work_struct *work)
504 struct mbox *vf_mbox = container_of(work, struct mbox, mbox_up_wrk);
505 struct otx2_nic *pf = vf_mbox->pfvf;
506 struct otx2_mbox_dev *mdev;
507 int offset, id, vf_idx = 0;
508 struct mbox_hdr *rsp_hdr;
509 struct mbox_msghdr *msg;
510 struct otx2_mbox *mbox;
512 vf_idx = vf_mbox - pf->mbox_pfvf;
513 mbox = &pf->mbox_pfvf[0].mbox_up;
514 mdev = &mbox->dev[vf_idx];
516 rsp_hdr = (struct mbox_hdr *)(mdev->mbase + mbox->rx_start);
517 offset = mbox->rx_start + ALIGN(sizeof(*rsp_hdr), MBOX_MSG_ALIGN);
519 for (id = 0; id < vf_mbox->up_num_msgs; id++) {
520 msg = mdev->mbase + offset;
522 if (msg->id >= MBOX_MSG_MAX) {
524 "Mbox msg with unknown ID 0x%x\n", msg->id);
528 if (msg->sig != OTX2_MBOX_RSP_SIG) {
530 "Mbox msg with wrong signature %x, ID 0x%x\n",
536 case MBOX_MSG_CGX_LINK_EVENT:
541 "Mbox msg response has err %d, ID 0x%x\n",
547 offset = mbox->rx_start + msg->next_msgoff;
548 if (mdev->msgs_acked == (vf_mbox->up_num_msgs - 1))
549 __otx2_mbox_reset(mbox, 0);
554 static irqreturn_t otx2_pfvf_mbox_intr_handler(int irq, void *pf_irq)
556 struct otx2_nic *pf = (struct otx2_nic *)(pf_irq);
557 int vfs = pf->total_vfs;
561 mbox = pf->mbox_pfvf;
562 /* Handle VF interrupts */
564 intr = otx2_read64(pf, RVU_PF_VFPF_MBOX_INTX(1));
565 otx2_write64(pf, RVU_PF_VFPF_MBOX_INTX(1), intr);
566 otx2_queue_work(mbox, pf->mbox_pfvf_wq, 64, vfs, intr,
571 intr = otx2_read64(pf, RVU_PF_VFPF_MBOX_INTX(0));
572 otx2_write64(pf, RVU_PF_VFPF_MBOX_INTX(0), intr);
574 otx2_queue_work(mbox, pf->mbox_pfvf_wq, 0, vfs, intr, TYPE_PFVF);
576 trace_otx2_msg_interrupt(mbox->mbox.pdev, "VF(s) to PF", intr);
581 static int otx2_pfvf_mbox_init(struct otx2_nic *pf, int numvfs)
583 void __iomem *hwbase;
591 pf->mbox_pfvf = devm_kcalloc(&pf->pdev->dev, numvfs,
592 sizeof(struct mbox), GFP_KERNEL);
596 pf->mbox_pfvf_wq = alloc_ordered_workqueue("otx2_pfvf_mailbox",
597 WQ_HIGHPRI | WQ_MEM_RECLAIM);
598 if (!pf->mbox_pfvf_wq)
601 /* On CN10K platform, PF <-> VF mailbox region follows after
602 * PF <-> AF mailbox region.
604 if (test_bit(CN10K_MBOX, &pf->hw.cap_flag))
605 base = pci_resource_start(pf->pdev, PCI_MBOX_BAR_NUM) +
608 base = readq((void __iomem *)((u64)pf->reg_base +
609 RVU_PF_VF_BAR4_ADDR));
611 hwbase = ioremap_wc(base, MBOX_SIZE * pf->total_vfs);
617 mbox = &pf->mbox_pfvf[0];
618 err = otx2_mbox_init(&mbox->mbox, hwbase, pf->pdev, pf->reg_base,
619 MBOX_DIR_PFVF, numvfs);
623 err = otx2_mbox_init(&mbox->mbox_up, hwbase, pf->pdev, pf->reg_base,
624 MBOX_DIR_PFVF_UP, numvfs);
628 for (vf = 0; vf < numvfs; vf++) {
630 INIT_WORK(&mbox->mbox_wrk, otx2_pfvf_mbox_handler);
631 INIT_WORK(&mbox->mbox_up_wrk, otx2_pfvf_mbox_up_handler);
641 destroy_workqueue(pf->mbox_pfvf_wq);
645 static void otx2_pfvf_mbox_destroy(struct otx2_nic *pf)
647 struct mbox *mbox = &pf->mbox_pfvf[0];
652 if (pf->mbox_pfvf_wq) {
653 destroy_workqueue(pf->mbox_pfvf_wq);
654 pf->mbox_pfvf_wq = NULL;
657 if (mbox->mbox.hwbase)
658 iounmap(mbox->mbox.hwbase);
660 otx2_mbox_destroy(&mbox->mbox);
663 static void otx2_enable_pfvf_mbox_intr(struct otx2_nic *pf, int numvfs)
665 /* Clear PF <=> VF mailbox IRQ */
666 otx2_write64(pf, RVU_PF_VFPF_MBOX_INTX(0), ~0ull);
667 otx2_write64(pf, RVU_PF_VFPF_MBOX_INTX(1), ~0ull);
669 /* Enable PF <=> VF mailbox IRQ */
670 otx2_write64(pf, RVU_PF_VFPF_MBOX_INT_ENA_W1SX(0), INTR_MASK(numvfs));
673 otx2_write64(pf, RVU_PF_VFPF_MBOX_INT_ENA_W1SX(1),
678 static void otx2_disable_pfvf_mbox_intr(struct otx2_nic *pf, int numvfs)
682 /* Disable PF <=> VF mailbox IRQ */
683 otx2_write64(pf, RVU_PF_VFPF_MBOX_INT_ENA_W1CX(0), ~0ull);
684 otx2_write64(pf, RVU_PF_VFPF_MBOX_INT_ENA_W1CX(1), ~0ull);
686 otx2_write64(pf, RVU_PF_VFPF_MBOX_INTX(0), ~0ull);
687 vector = pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFPF_MBOX0);
688 free_irq(vector, pf);
691 otx2_write64(pf, RVU_PF_VFPF_MBOX_INTX(1), ~0ull);
692 vector = pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFPF_MBOX1);
693 free_irq(vector, pf);
697 static int otx2_register_pfvf_mbox_intr(struct otx2_nic *pf, int numvfs)
699 struct otx2_hw *hw = &pf->hw;
703 /* Register MBOX0 interrupt handler */
704 irq_name = &hw->irq_name[RVU_PF_INT_VEC_VFPF_MBOX0 * NAME_SIZE];
706 snprintf(irq_name, NAME_SIZE,
707 "RVUPF%d_VF Mbox0", rvu_get_pf(pf->pcifunc));
709 snprintf(irq_name, NAME_SIZE, "RVUPF_VF Mbox0");
710 err = request_irq(pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFPF_MBOX0),
711 otx2_pfvf_mbox_intr_handler, 0, irq_name, pf);
714 "RVUPF: IRQ registration failed for PFVF mbox0 irq\n");
719 /* Register MBOX1 interrupt handler */
720 irq_name = &hw->irq_name[RVU_PF_INT_VEC_VFPF_MBOX1 * NAME_SIZE];
722 snprintf(irq_name, NAME_SIZE,
723 "RVUPF%d_VF Mbox1", rvu_get_pf(pf->pcifunc));
725 snprintf(irq_name, NAME_SIZE, "RVUPF_VF Mbox1");
726 err = request_irq(pci_irq_vector(pf->pdev,
727 RVU_PF_INT_VEC_VFPF_MBOX1),
728 otx2_pfvf_mbox_intr_handler,
732 "RVUPF: IRQ registration failed for PFVF mbox1 irq\n");
737 otx2_enable_pfvf_mbox_intr(pf, numvfs);
742 static void otx2_process_pfaf_mbox_msg(struct otx2_nic *pf,
743 struct mbox_msghdr *msg)
747 if (msg->id >= MBOX_MSG_MAX) {
749 "Mbox msg with unknown ID 0x%x\n", msg->id);
753 if (msg->sig != OTX2_MBOX_RSP_SIG) {
755 "Mbox msg with wrong signature %x, ID 0x%x\n",
760 /* message response heading VF */
761 devid = msg->pcifunc & RVU_PFVF_FUNC_MASK;
763 struct otx2_vf_config *config = &pf->vf_configs[devid - 1];
764 struct delayed_work *dwork;
767 case MBOX_MSG_NIX_LF_START_RX:
768 config->intf_down = false;
769 dwork = &config->link_event_work;
770 schedule_delayed_work(dwork, msecs_to_jiffies(100));
772 case MBOX_MSG_NIX_LF_STOP_RX:
773 config->intf_down = true;
782 pf->pcifunc = msg->pcifunc;
784 case MBOX_MSG_MSIX_OFFSET:
785 mbox_handler_msix_offset(pf, (struct msix_offset_rsp *)msg);
787 case MBOX_MSG_NPA_LF_ALLOC:
788 mbox_handler_npa_lf_alloc(pf, (struct npa_lf_alloc_rsp *)msg);
790 case MBOX_MSG_NIX_LF_ALLOC:
791 mbox_handler_nix_lf_alloc(pf, (struct nix_lf_alloc_rsp *)msg);
793 case MBOX_MSG_NIX_BP_ENABLE:
794 mbox_handler_nix_bp_enable(pf, (struct nix_bp_cfg_rsp *)msg);
796 case MBOX_MSG_CGX_STATS:
797 mbox_handler_cgx_stats(pf, (struct cgx_stats_rsp *)msg);
799 case MBOX_MSG_CGX_FEC_STATS:
800 mbox_handler_cgx_fec_stats(pf, (struct cgx_fec_stats_rsp *)msg);
805 "Mbox msg response has err %d, ID 0x%x\n",
811 static void otx2_pfaf_mbox_handler(struct work_struct *work)
813 struct otx2_mbox_dev *mdev;
814 struct mbox_hdr *rsp_hdr;
815 struct mbox_msghdr *msg;
816 struct otx2_mbox *mbox;
817 struct mbox *af_mbox;
821 af_mbox = container_of(work, struct mbox, mbox_wrk);
822 mbox = &af_mbox->mbox;
823 mdev = &mbox->dev[0];
824 rsp_hdr = (struct mbox_hdr *)(mdev->mbase + mbox->rx_start);
826 offset = mbox->rx_start + ALIGN(sizeof(*rsp_hdr), MBOX_MSG_ALIGN);
829 for (id = 0; id < af_mbox->num_msgs; id++) {
830 msg = (struct mbox_msghdr *)(mdev->mbase + offset);
831 otx2_process_pfaf_mbox_msg(pf, msg);
832 offset = mbox->rx_start + msg->next_msgoff;
833 if (mdev->msgs_acked == (af_mbox->num_msgs - 1))
834 __otx2_mbox_reset(mbox, 0);
840 static void otx2_handle_link_event(struct otx2_nic *pf)
842 struct cgx_link_user_info *linfo = &pf->linfo;
843 struct net_device *netdev = pf->netdev;
845 pr_info("%s NIC Link is %s %d Mbps %s duplex\n", netdev->name,
846 linfo->link_up ? "UP" : "DOWN", linfo->speed,
847 linfo->full_duplex ? "Full" : "Half");
848 if (linfo->link_up) {
849 netif_carrier_on(netdev);
850 netif_tx_start_all_queues(netdev);
852 netif_tx_stop_all_queues(netdev);
853 netif_carrier_off(netdev);
857 int otx2_mbox_up_handler_mcs_intr_notify(struct otx2_nic *pf,
858 struct mcs_intr_info *event,
861 cn10k_handle_mcs_event(pf, event);
866 int otx2_mbox_up_handler_cgx_link_event(struct otx2_nic *pf,
867 struct cgx_link_info_msg *msg,
872 /* Copy the link info sent by AF */
873 pf->linfo = msg->link_info;
875 /* notify VFs about link event */
876 for (i = 0; i < pci_num_vf(pf->pdev); i++) {
877 struct otx2_vf_config *config = &pf->vf_configs[i];
878 struct delayed_work *dwork = &config->link_event_work;
880 if (config->intf_down)
883 schedule_delayed_work(dwork, msecs_to_jiffies(100));
886 /* interface has not been fully configured yet */
887 if (pf->flags & OTX2_FLAG_INTF_DOWN)
890 otx2_handle_link_event(pf);
894 static int otx2_process_mbox_msg_up(struct otx2_nic *pf,
895 struct mbox_msghdr *req)
897 /* Check if valid, if not reply with a invalid msg */
898 if (req->sig != OTX2_MBOX_REQ_SIG) {
899 otx2_reply_invalid_msg(&pf->mbox.mbox_up, 0, 0, req->id);
904 #define M(_name, _id, _fn_name, _req_type, _rsp_type) \
906 struct _rsp_type *rsp; \
909 rsp = (struct _rsp_type *)otx2_mbox_alloc_msg( \
910 &pf->mbox.mbox_up, 0, \
911 sizeof(struct _rsp_type)); \
916 rsp->hdr.sig = OTX2_MBOX_RSP_SIG; \
917 rsp->hdr.pcifunc = 0; \
920 err = otx2_mbox_up_handler_ ## _fn_name( \
921 pf, (struct _req_type *)req, rsp); \
929 otx2_reply_invalid_msg(&pf->mbox.mbox_up, 0, 0, req->id);
935 static void otx2_pfaf_mbox_up_handler(struct work_struct *work)
937 struct mbox *af_mbox = container_of(work, struct mbox, mbox_up_wrk);
938 struct otx2_mbox *mbox = &af_mbox->mbox_up;
939 struct otx2_mbox_dev *mdev = &mbox->dev[0];
940 struct otx2_nic *pf = af_mbox->pfvf;
941 int offset, id, devid = 0;
942 struct mbox_hdr *rsp_hdr;
943 struct mbox_msghdr *msg;
945 rsp_hdr = (struct mbox_hdr *)(mdev->mbase + mbox->rx_start);
947 offset = mbox->rx_start + ALIGN(sizeof(*rsp_hdr), MBOX_MSG_ALIGN);
949 for (id = 0; id < af_mbox->up_num_msgs; id++) {
950 msg = (struct mbox_msghdr *)(mdev->mbase + offset);
952 devid = msg->pcifunc & RVU_PFVF_FUNC_MASK;
953 /* Skip processing VF's messages */
955 otx2_process_mbox_msg_up(pf, msg);
956 offset = mbox->rx_start + msg->next_msgoff;
959 otx2_forward_vf_mbox_msgs(pf, &pf->mbox.mbox_up,
960 MBOX_DIR_PFVF_UP, devid - 1,
961 af_mbox->up_num_msgs);
965 otx2_mbox_msg_send(mbox, 0);
968 static irqreturn_t otx2_pfaf_mbox_intr_handler(int irq, void *pf_irq)
970 struct otx2_nic *pf = (struct otx2_nic *)pf_irq;
974 otx2_write64(pf, RVU_PF_INT, BIT_ULL(0));
978 trace_otx2_msg_interrupt(mbox->mbox.pdev, "AF to PF", BIT_ULL(0));
980 otx2_queue_work(mbox, pf->mbox_wq, 0, 1, 1, TYPE_PFAF);
985 static void otx2_disable_mbox_intr(struct otx2_nic *pf)
987 int vector = pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_AFPF_MBOX);
989 /* Disable AF => PF mailbox IRQ */
990 otx2_write64(pf, RVU_PF_INT_ENA_W1C, BIT_ULL(0));
991 free_irq(vector, pf);
994 static int otx2_register_mbox_intr(struct otx2_nic *pf, bool probe_af)
996 struct otx2_hw *hw = &pf->hw;
1001 /* Register mailbox interrupt handler */
1002 irq_name = &hw->irq_name[RVU_PF_INT_VEC_AFPF_MBOX * NAME_SIZE];
1003 snprintf(irq_name, NAME_SIZE, "RVUPFAF Mbox");
1004 err = request_irq(pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_AFPF_MBOX),
1005 otx2_pfaf_mbox_intr_handler, 0, irq_name, pf);
1008 "RVUPF: IRQ registration failed for PFAF mbox irq\n");
1012 /* Enable mailbox interrupt for msgs coming from AF.
1013 * First clear to avoid spurious interrupts, if any.
1015 otx2_write64(pf, RVU_PF_INT, BIT_ULL(0));
1016 otx2_write64(pf, RVU_PF_INT_ENA_W1S, BIT_ULL(0));
1021 /* Check mailbox communication with AF */
1022 req = otx2_mbox_alloc_msg_ready(&pf->mbox);
1024 otx2_disable_mbox_intr(pf);
1027 err = otx2_sync_mbox_msg(&pf->mbox);
1030 "AF not responding to mailbox, deferring probe\n");
1031 otx2_disable_mbox_intr(pf);
1032 return -EPROBE_DEFER;
1038 static void otx2_pfaf_mbox_destroy(struct otx2_nic *pf)
1040 struct mbox *mbox = &pf->mbox;
1043 destroy_workqueue(pf->mbox_wq);
1047 if (mbox->mbox.hwbase)
1048 iounmap((void __iomem *)mbox->mbox.hwbase);
1050 otx2_mbox_destroy(&mbox->mbox);
1051 otx2_mbox_destroy(&mbox->mbox_up);
1054 static int otx2_pfaf_mbox_init(struct otx2_nic *pf)
1056 struct mbox *mbox = &pf->mbox;
1057 void __iomem *hwbase;
1061 pf->mbox_wq = alloc_ordered_workqueue("otx2_pfaf_mailbox",
1062 WQ_HIGHPRI | WQ_MEM_RECLAIM);
1066 /* Mailbox is a reserved memory (in RAM) region shared between
1067 * admin function (i.e AF) and this PF, shouldn't be mapped as
1068 * device memory to allow unaligned accesses.
1070 hwbase = ioremap_wc(pci_resource_start(pf->pdev, PCI_MBOX_BAR_NUM),
1073 dev_err(pf->dev, "Unable to map PFAF mailbox region\n");
1078 err = otx2_mbox_init(&mbox->mbox, hwbase, pf->pdev, pf->reg_base,
1083 err = otx2_mbox_init(&mbox->mbox_up, hwbase, pf->pdev, pf->reg_base,
1084 MBOX_DIR_PFAF_UP, 1);
1088 err = otx2_mbox_bbuf_init(mbox, pf->pdev);
1092 INIT_WORK(&mbox->mbox_wrk, otx2_pfaf_mbox_handler);
1093 INIT_WORK(&mbox->mbox_up_wrk, otx2_pfaf_mbox_up_handler);
1094 mutex_init(&mbox->lock);
1098 otx2_pfaf_mbox_destroy(pf);
1102 static int otx2_cgx_config_linkevents(struct otx2_nic *pf, bool enable)
1104 struct msg_req *msg;
1107 mutex_lock(&pf->mbox.lock);
1109 msg = otx2_mbox_alloc_msg_cgx_start_linkevents(&pf->mbox);
1111 msg = otx2_mbox_alloc_msg_cgx_stop_linkevents(&pf->mbox);
1114 mutex_unlock(&pf->mbox.lock);
1118 err = otx2_sync_mbox_msg(&pf->mbox);
1119 mutex_unlock(&pf->mbox.lock);
1123 static int otx2_cgx_config_loopback(struct otx2_nic *pf, bool enable)
1125 struct msg_req *msg;
1128 if (enable && !bitmap_empty(pf->flow_cfg->dmacflt_bmap,
1129 pf->flow_cfg->dmacflt_max_flows))
1130 netdev_warn(pf->netdev,
1131 "CGX/RPM internal loopback might not work as DMAC filters are active\n");
1133 mutex_lock(&pf->mbox.lock);
1135 msg = otx2_mbox_alloc_msg_cgx_intlbk_enable(&pf->mbox);
1137 msg = otx2_mbox_alloc_msg_cgx_intlbk_disable(&pf->mbox);
1140 mutex_unlock(&pf->mbox.lock);
1144 err = otx2_sync_mbox_msg(&pf->mbox);
1145 mutex_unlock(&pf->mbox.lock);
1149 int otx2_set_real_num_queues(struct net_device *netdev,
1150 int tx_queues, int rx_queues)
1154 err = netif_set_real_num_tx_queues(netdev, tx_queues);
1157 "Failed to set no of Tx queues: %d\n", tx_queues);
1161 err = netif_set_real_num_rx_queues(netdev, rx_queues);
1164 "Failed to set no of Rx queues: %d\n", rx_queues);
1167 EXPORT_SYMBOL(otx2_set_real_num_queues);
1169 static char *nix_sqoperr_e_str[NIX_SQOPERR_MAX] = {
1171 "NIX_SQOPERR_CTX_FAULT",
1172 "NIX_SQOPERR_CTX_POISON",
1173 "NIX_SQOPERR_DISABLED",
1174 "NIX_SQOPERR_SIZE_ERR",
1175 "NIX_SQOPERR_OFLOW",
1176 "NIX_SQOPERR_SQB_NULL",
1177 "NIX_SQOPERR_SQB_FAULT",
1178 "NIX_SQOPERR_SQE_SZ_ZERO",
1181 static char *nix_mnqerr_e_str[NIX_MNQERR_MAX] = {
1182 "NIX_MNQERR_SQ_CTX_FAULT",
1183 "NIX_MNQERR_SQ_CTX_POISON",
1184 "NIX_MNQERR_SQB_FAULT",
1185 "NIX_MNQERR_SQB_POISON",
1186 "NIX_MNQERR_TOTAL_ERR",
1187 "NIX_MNQERR_LSO_ERR",
1188 "NIX_MNQERR_CQ_QUERY_ERR",
1189 "NIX_MNQERR_MAX_SQE_SIZE_ERR",
1190 "NIX_MNQERR_MAXLEN_ERR",
1191 "NIX_MNQERR_SQE_SIZEM1_ZERO",
1194 static char *nix_snd_status_e_str[NIX_SND_STATUS_MAX] = {
1195 "NIX_SND_STATUS_GOOD",
1196 "NIX_SND_STATUS_SQ_CTX_FAULT",
1197 "NIX_SND_STATUS_SQ_CTX_POISON",
1198 "NIX_SND_STATUS_SQB_FAULT",
1199 "NIX_SND_STATUS_SQB_POISON",
1200 "NIX_SND_STATUS_HDR_ERR",
1201 "NIX_SND_STATUS_EXT_ERR",
1202 "NIX_SND_STATUS_JUMP_FAULT",
1203 "NIX_SND_STATUS_JUMP_POISON",
1204 "NIX_SND_STATUS_CRC_ERR",
1205 "NIX_SND_STATUS_IMM_ERR",
1206 "NIX_SND_STATUS_SG_ERR",
1207 "NIX_SND_STATUS_MEM_ERR",
1208 "NIX_SND_STATUS_INVALID_SUBDC",
1209 "NIX_SND_STATUS_SUBDC_ORDER_ERR",
1210 "NIX_SND_STATUS_DATA_FAULT",
1211 "NIX_SND_STATUS_DATA_POISON",
1212 "NIX_SND_STATUS_NPC_DROP_ACTION",
1213 "NIX_SND_STATUS_LOCK_VIOL",
1214 "NIX_SND_STATUS_NPC_UCAST_CHAN_ERR",
1215 "NIX_SND_STATUS_NPC_MCAST_CHAN_ERR",
1216 "NIX_SND_STATUS_NPC_MCAST_ABORT",
1217 "NIX_SND_STATUS_NPC_VTAG_PTR_ERR",
1218 "NIX_SND_STATUS_NPC_VTAG_SIZE_ERR",
1219 "NIX_SND_STATUS_SEND_STATS_ERR",
1222 static irqreturn_t otx2_q_intr_handler(int irq, void *data)
1224 struct otx2_nic *pf = data;
1225 struct otx2_snd_queue *sq;
1230 for (qidx = 0; qidx < pf->qset.cq_cnt; qidx++) {
1231 ptr = otx2_get_regaddr(pf, NIX_LF_CQ_OP_INT);
1232 val = otx2_atomic64_add((qidx << 44), ptr);
1234 otx2_write64(pf, NIX_LF_CQ_OP_INT, (qidx << 44) |
1235 (val & NIX_CQERRINT_BITS));
1236 if (!(val & (NIX_CQERRINT_BITS | BIT_ULL(42))))
1239 if (val & BIT_ULL(42)) {
1240 netdev_err(pf->netdev, "CQ%lld: error reading NIX_LF_CQ_OP_INT, NIX_LF_ERR_INT 0x%llx\n",
1241 qidx, otx2_read64(pf, NIX_LF_ERR_INT));
1243 if (val & BIT_ULL(NIX_CQERRINT_DOOR_ERR))
1244 netdev_err(pf->netdev, "CQ%lld: Doorbell error",
1246 if (val & BIT_ULL(NIX_CQERRINT_CQE_FAULT))
1247 netdev_err(pf->netdev, "CQ%lld: Memory fault on CQE write to LLC/DRAM",
1251 schedule_work(&pf->reset_task);
1255 for (qidx = 0; qidx < otx2_get_total_tx_queues(pf); qidx++) {
1256 u64 sq_op_err_dbg, mnq_err_dbg, snd_err_dbg;
1257 u8 sq_op_err_code, mnq_err_code, snd_err_code;
1259 sq = &pf->qset.sq[qidx];
1263 /* Below debug registers captures first errors corresponding to
1264 * those registers. We don't have to check against SQ qid as
1265 * these are fatal errors.
1268 ptr = otx2_get_regaddr(pf, NIX_LF_SQ_OP_INT);
1269 val = otx2_atomic64_add((qidx << 44), ptr);
1270 otx2_write64(pf, NIX_LF_SQ_OP_INT, (qidx << 44) |
1271 (val & NIX_SQINT_BITS));
1273 if (val & BIT_ULL(42)) {
1274 netdev_err(pf->netdev, "SQ%lld: error reading NIX_LF_SQ_OP_INT, NIX_LF_ERR_INT 0x%llx\n",
1275 qidx, otx2_read64(pf, NIX_LF_ERR_INT));
1279 sq_op_err_dbg = otx2_read64(pf, NIX_LF_SQ_OP_ERR_DBG);
1280 if (!(sq_op_err_dbg & BIT(44)))
1281 goto chk_mnq_err_dbg;
1283 sq_op_err_code = FIELD_GET(GENMASK(7, 0), sq_op_err_dbg);
1284 netdev_err(pf->netdev, "SQ%lld: NIX_LF_SQ_OP_ERR_DBG(%llx) err=%s\n",
1285 qidx, sq_op_err_dbg, nix_sqoperr_e_str[sq_op_err_code]);
1287 otx2_write64(pf, NIX_LF_SQ_OP_ERR_DBG, BIT_ULL(44));
1289 if (sq_op_err_code == NIX_SQOPERR_SQB_NULL)
1290 goto chk_mnq_err_dbg;
1292 /* Err is not NIX_SQOPERR_SQB_NULL, call aq function to read SQ structure.
1293 * TODO: But we are in irq context. How to call mbox functions which does sleep
1297 mnq_err_dbg = otx2_read64(pf, NIX_LF_MNQ_ERR_DBG);
1298 if (!(mnq_err_dbg & BIT(44)))
1299 goto chk_snd_err_dbg;
1301 mnq_err_code = FIELD_GET(GENMASK(7, 0), mnq_err_dbg);
1302 netdev_err(pf->netdev, "SQ%lld: NIX_LF_MNQ_ERR_DBG(%llx) err=%s\n",
1303 qidx, mnq_err_dbg, nix_mnqerr_e_str[mnq_err_code]);
1304 otx2_write64(pf, NIX_LF_MNQ_ERR_DBG, BIT_ULL(44));
1307 snd_err_dbg = otx2_read64(pf, NIX_LF_SEND_ERR_DBG);
1308 if (snd_err_dbg & BIT(44)) {
1309 snd_err_code = FIELD_GET(GENMASK(7, 0), snd_err_dbg);
1310 netdev_err(pf->netdev, "SQ%lld: NIX_LF_SND_ERR_DBG:0x%llx err=%s\n",
1311 qidx, snd_err_dbg, nix_snd_status_e_str[snd_err_code]);
1312 otx2_write64(pf, NIX_LF_SEND_ERR_DBG, BIT_ULL(44));
1316 /* Print values and reset */
1317 if (val & BIT_ULL(NIX_SQINT_SQB_ALLOC_FAIL))
1318 netdev_err(pf->netdev, "SQ%lld: SQB allocation failed",
1321 schedule_work(&pf->reset_task);
1327 static irqreturn_t otx2_cq_intr_handler(int irq, void *cq_irq)
1329 struct otx2_cq_poll *cq_poll = (struct otx2_cq_poll *)cq_irq;
1330 struct otx2_nic *pf = (struct otx2_nic *)cq_poll->dev;
1331 int qidx = cq_poll->cint_idx;
1333 /* Disable interrupts.
1335 * Completion interrupts behave in a level-triggered interrupt
1336 * fashion, and hence have to be cleared only after it is serviced.
1338 otx2_write64(pf, NIX_LF_CINTX_ENA_W1C(qidx), BIT_ULL(0));
1342 napi_schedule_irqoff(&cq_poll->napi);
1347 static void otx2_disable_napi(struct otx2_nic *pf)
1349 struct otx2_qset *qset = &pf->qset;
1350 struct otx2_cq_poll *cq_poll;
1353 for (qidx = 0; qidx < pf->hw.cint_cnt; qidx++) {
1354 cq_poll = &qset->napi[qidx];
1355 cancel_work_sync(&cq_poll->dim.work);
1356 napi_disable(&cq_poll->napi);
1357 netif_napi_del(&cq_poll->napi);
1361 static void otx2_free_cq_res(struct otx2_nic *pf)
1363 struct otx2_qset *qset = &pf->qset;
1364 struct otx2_cq_queue *cq;
1368 otx2_ctx_disable(&pf->mbox, NIX_AQ_CTYPE_CQ, false);
1369 for (qidx = 0; qidx < qset->cq_cnt; qidx++) {
1370 cq = &qset->cq[qidx];
1371 qmem_free(pf->dev, cq->cqe);
1375 static void otx2_free_sq_res(struct otx2_nic *pf)
1377 struct otx2_qset *qset = &pf->qset;
1378 struct otx2_snd_queue *sq;
1382 otx2_ctx_disable(&pf->mbox, NIX_AQ_CTYPE_SQ, false);
1383 /* Free SQB pointers */
1384 otx2_sq_free_sqbs(pf);
1385 for (qidx = 0; qidx < otx2_get_total_tx_queues(pf); qidx++) {
1386 sq = &qset->sq[qidx];
1387 /* Skip freeing Qos queues if they are not initialized */
1390 qmem_free(pf->dev, sq->sqe);
1391 qmem_free(pf->dev, sq->tso_hdrs);
1393 kfree(sq->sqb_ptrs);
1397 static int otx2_get_rbuf_size(struct otx2_nic *pf, int mtu)
1403 if (pf->hw.rbuf_len)
1404 return ALIGN(pf->hw.rbuf_len, OTX2_ALIGN) + OTX2_HEAD_ROOM;
1406 /* The data transferred by NIX to memory consists of actual packet
1407 * plus additional data which has timestamp and/or EDSA/HIGIG2
1408 * headers if interface is configured in corresponding modes.
1409 * NIX transfers entire data using 6 segments/buffers and writes
1410 * a CQE_RX descriptor with those segment addresses. First segment
1411 * has additional data prepended to packet. Also software omits a
1412 * headroom of 128 bytes in each segment. Hence the total size of
1413 * memory needed to receive a packet with 'mtu' is:
1414 * frame size = mtu + additional data;
1415 * memory = frame_size + headroom * 6;
1416 * each receive buffer size = memory / 6;
1418 frame_size = mtu + OTX2_ETH_HLEN + OTX2_HW_TIMESTAMP_LEN;
1419 total_size = frame_size + OTX2_HEAD_ROOM * 6;
1420 rbuf_size = total_size / 6;
1422 return ALIGN(rbuf_size, 2048);
1425 static int otx2_init_hw_resources(struct otx2_nic *pf)
1427 struct nix_lf_free_req *free_req;
1428 struct mbox *mbox = &pf->mbox;
1429 struct otx2_hw *hw = &pf->hw;
1430 struct msg_req *req;
1433 /* Set required NPA LF's pool counts
1434 * Auras and Pools are used in a 1:1 mapping,
1435 * so, aura count = pool count.
1437 hw->rqpool_cnt = hw->rx_queues;
1438 hw->sqpool_cnt = otx2_get_total_tx_queues(pf);
1439 hw->pool_cnt = hw->rqpool_cnt + hw->sqpool_cnt;
1441 /* Maximum hardware supported transmit length */
1442 pf->tx_max_pktlen = pf->netdev->max_mtu + OTX2_ETH_HLEN;
1444 pf->rbsize = otx2_get_rbuf_size(pf, pf->netdev->mtu);
1446 mutex_lock(&mbox->lock);
1448 err = otx2_config_npa(pf);
1453 err = otx2_config_nix(pf);
1455 goto err_free_npa_lf;
1457 /* Enable backpressure */
1458 otx2_nix_config_bp(pf, true);
1460 /* Init Auras and pools used by NIX RQ, for free buffer ptrs */
1461 err = otx2_rq_aura_pool_init(pf);
1463 mutex_unlock(&mbox->lock);
1464 goto err_free_nix_lf;
1466 /* Init Auras and pools used by NIX SQ, for queueing SQEs */
1467 err = otx2_sq_aura_pool_init(pf);
1469 mutex_unlock(&mbox->lock);
1470 goto err_free_rq_ptrs;
1473 err = otx2_txsch_alloc(pf);
1475 mutex_unlock(&mbox->lock);
1476 goto err_free_sq_ptrs;
1481 err = otx2_pfc_txschq_alloc(pf);
1483 mutex_unlock(&mbox->lock);
1484 goto err_free_sq_ptrs;
1489 err = otx2_config_nix_queues(pf);
1491 mutex_unlock(&mbox->lock);
1492 goto err_free_txsch;
1495 for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
1496 err = otx2_txschq_config(pf, lvl, 0, false);
1498 mutex_unlock(&mbox->lock);
1499 goto err_free_nix_queues;
1505 err = otx2_pfc_txschq_config(pf);
1507 mutex_unlock(&mbox->lock);
1508 goto err_free_nix_queues;
1513 mutex_unlock(&mbox->lock);
1516 err_free_nix_queues:
1517 otx2_free_sq_res(pf);
1518 otx2_free_cq_res(pf);
1519 otx2_ctx_disable(mbox, NIX_AQ_CTYPE_RQ, false);
1521 otx2_txschq_stop(pf);
1523 otx2_sq_free_sqbs(pf);
1525 otx2_free_aura_ptr(pf, AURA_NIX_RQ);
1526 otx2_ctx_disable(mbox, NPA_AQ_CTYPE_POOL, true);
1527 otx2_ctx_disable(mbox, NPA_AQ_CTYPE_AURA, true);
1528 otx2_aura_pool_free(pf);
1530 mutex_lock(&mbox->lock);
1531 free_req = otx2_mbox_alloc_msg_nix_lf_free(mbox);
1533 free_req->flags = NIX_LF_DISABLE_FLOWS;
1534 if (otx2_sync_mbox_msg(mbox))
1535 dev_err(pf->dev, "%s failed to free nixlf\n", __func__);
1539 req = otx2_mbox_alloc_msg_npa_lf_free(mbox);
1541 if (otx2_sync_mbox_msg(mbox))
1542 dev_err(pf->dev, "%s failed to free npalf\n", __func__);
1545 mutex_unlock(&mbox->lock);
1549 static void otx2_free_hw_resources(struct otx2_nic *pf)
1551 struct otx2_qset *qset = &pf->qset;
1552 struct nix_lf_free_req *free_req;
1553 struct mbox *mbox = &pf->mbox;
1554 struct otx2_cq_queue *cq;
1555 struct otx2_pool *pool;
1556 struct msg_req *req;
1560 /* Ensure all SQE are processed */
1563 /* Stop transmission */
1564 otx2_txschq_stop(pf);
1568 otx2_pfc_txschq_stop(pf);
1571 otx2_clean_qos_queues(pf);
1573 mutex_lock(&mbox->lock);
1574 /* Disable backpressure */
1575 if (!(pf->pcifunc & RVU_PFVF_FUNC_MASK))
1576 otx2_nix_config_bp(pf, false);
1577 mutex_unlock(&mbox->lock);
1580 otx2_ctx_disable(mbox, NIX_AQ_CTYPE_RQ, false);
1582 /*Dequeue all CQEs */
1583 for (qidx = 0; qidx < qset->cq_cnt; qidx++) {
1584 cq = &qset->cq[qidx];
1585 if (cq->cq_type == CQ_RX)
1586 otx2_cleanup_rx_cqes(pf, cq, qidx);
1588 otx2_cleanup_tx_cqes(pf, cq);
1591 otx2_free_sq_res(pf);
1593 /* Free RQ buffer pointers*/
1594 otx2_free_aura_ptr(pf, AURA_NIX_RQ);
1596 for (qidx = 0; qidx < pf->hw.rx_queues; qidx++) {
1597 pool_id = otx2_get_pool_idx(pf, AURA_NIX_RQ, qidx);
1598 pool = &pf->qset.pool[pool_id];
1599 page_pool_destroy(pool->page_pool);
1600 pool->page_pool = NULL;
1603 otx2_free_cq_res(pf);
1605 /* Free all ingress bandwidth profiles allocated */
1606 cn10k_free_all_ipolicers(pf);
1608 mutex_lock(&mbox->lock);
1610 free_req = otx2_mbox_alloc_msg_nix_lf_free(mbox);
1612 free_req->flags = NIX_LF_DISABLE_FLOWS;
1613 if (!(pf->flags & OTX2_FLAG_PF_SHUTDOWN))
1614 free_req->flags |= NIX_LF_DONT_FREE_TX_VTAG;
1615 if (otx2_sync_mbox_msg(mbox))
1616 dev_err(pf->dev, "%s failed to free nixlf\n", __func__);
1618 mutex_unlock(&mbox->lock);
1620 /* Disable NPA Pool and Aura hw context */
1621 otx2_ctx_disable(mbox, NPA_AQ_CTYPE_POOL, true);
1622 otx2_ctx_disable(mbox, NPA_AQ_CTYPE_AURA, true);
1623 otx2_aura_pool_free(pf);
1625 mutex_lock(&mbox->lock);
1627 req = otx2_mbox_alloc_msg_npa_lf_free(mbox);
1629 if (otx2_sync_mbox_msg(mbox))
1630 dev_err(pf->dev, "%s failed to free npalf\n", __func__);
1632 mutex_unlock(&mbox->lock);
1635 static void otx2_do_set_rx_mode(struct otx2_nic *pf)
1637 struct net_device *netdev = pf->netdev;
1638 struct nix_rx_mode *req;
1639 bool promisc = false;
1641 if (!(netdev->flags & IFF_UP))
1644 if ((netdev->flags & IFF_PROMISC) ||
1645 (netdev_uc_count(netdev) > OTX2_MAX_UNICAST_FLOWS)) {
1649 /* Write unicast address to mcam entries or del from mcam */
1650 if (!promisc && netdev->priv_flags & IFF_UNICAST_FLT)
1651 __dev_uc_sync(netdev, otx2_add_macfilter, otx2_del_macfilter);
1653 mutex_lock(&pf->mbox.lock);
1654 req = otx2_mbox_alloc_msg_nix_set_rx_mode(&pf->mbox);
1656 mutex_unlock(&pf->mbox.lock);
1660 req->mode = NIX_RX_MODE_UCAST;
1663 req->mode |= NIX_RX_MODE_PROMISC;
1664 if (netdev->flags & (IFF_ALLMULTI | IFF_MULTICAST))
1665 req->mode |= NIX_RX_MODE_ALLMULTI;
1667 req->mode |= NIX_RX_MODE_USE_MCE;
1669 otx2_sync_mbox_msg(&pf->mbox);
1670 mutex_unlock(&pf->mbox.lock);
1673 static void otx2_dim_work(struct work_struct *w)
1675 struct dim_cq_moder cur_moder;
1676 struct otx2_cq_poll *cq_poll;
1677 struct otx2_nic *pfvf;
1680 dim = container_of(w, struct dim, work);
1681 cur_moder = net_dim_get_rx_moderation(dim->mode, dim->profile_ix);
1682 cq_poll = container_of(dim, struct otx2_cq_poll, dim);
1683 pfvf = (struct otx2_nic *)cq_poll->dev;
1684 pfvf->hw.cq_time_wait = (cur_moder.usec > CQ_TIMER_THRESH_MAX) ?
1685 CQ_TIMER_THRESH_MAX : cur_moder.usec;
1686 pfvf->hw.cq_ecount_wait = (cur_moder.pkts > NAPI_POLL_WEIGHT) ?
1687 NAPI_POLL_WEIGHT : cur_moder.pkts;
1688 dim->state = DIM_START_MEASURE;
1691 int otx2_open(struct net_device *netdev)
1693 struct otx2_nic *pf = netdev_priv(netdev);
1694 struct otx2_cq_poll *cq_poll = NULL;
1695 struct otx2_qset *qset = &pf->qset;
1696 int err = 0, qidx, vec;
1699 netif_carrier_off(netdev);
1701 /* RQ and SQs are mapped to different CQs,
1702 * so find out max CQ IRQs (i.e CINTs) needed.
1704 pf->hw.cint_cnt = max3(pf->hw.rx_queues, pf->hw.tx_queues,
1705 pf->hw.tc_tx_queues);
1707 pf->qset.cq_cnt = pf->hw.rx_queues + otx2_get_total_tx_queues(pf);
1709 qset->napi = kcalloc(pf->hw.cint_cnt, sizeof(*cq_poll), GFP_KERNEL);
1714 qset->rqe_cnt = qset->rqe_cnt ? qset->rqe_cnt : Q_COUNT(Q_SIZE_256);
1716 qset->sqe_cnt = qset->sqe_cnt ? qset->sqe_cnt : Q_COUNT(Q_SIZE_4K);
1719 qset->cq = kcalloc(pf->qset.cq_cnt,
1720 sizeof(struct otx2_cq_queue), GFP_KERNEL);
1724 qset->sq = kcalloc(otx2_get_total_tx_queues(pf),
1725 sizeof(struct otx2_snd_queue), GFP_KERNEL);
1729 qset->rq = kcalloc(pf->hw.rx_queues,
1730 sizeof(struct otx2_rcv_queue), GFP_KERNEL);
1734 err = otx2_init_hw_resources(pf);
1738 /* Register NAPI handler */
1739 for (qidx = 0; qidx < pf->hw.cint_cnt; qidx++) {
1740 cq_poll = &qset->napi[qidx];
1741 cq_poll->cint_idx = qidx;
1742 /* RQ0 & SQ0 are mapped to CINT0 and so on..
1743 * 'cq_ids[0]' points to RQ's CQ and
1744 * 'cq_ids[1]' points to SQ's CQ and
1745 * 'cq_ids[2]' points to XDP's CQ and
1747 cq_poll->cq_ids[CQ_RX] =
1748 (qidx < pf->hw.rx_queues) ? qidx : CINT_INVALID_CQ;
1749 cq_poll->cq_ids[CQ_TX] = (qidx < pf->hw.tx_queues) ?
1750 qidx + pf->hw.rx_queues : CINT_INVALID_CQ;
1752 cq_poll->cq_ids[CQ_XDP] = (qidx < pf->hw.xdp_queues) ?
1753 (qidx + pf->hw.rx_queues +
1757 cq_poll->cq_ids[CQ_XDP] = CINT_INVALID_CQ;
1759 cq_poll->cq_ids[CQ_QOS] = (qidx < pf->hw.tc_tx_queues) ?
1760 (qidx + pf->hw.rx_queues +
1761 pf->hw.non_qos_queues) :
1764 cq_poll->dev = (void *)pf;
1765 cq_poll->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_CQE;
1766 INIT_WORK(&cq_poll->dim.work, otx2_dim_work);
1767 netif_napi_add(netdev, &cq_poll->napi, otx2_napi_handler);
1768 napi_enable(&cq_poll->napi);
1771 /* Set maximum frame size allowed in HW */
1772 err = otx2_hw_set_mtu(pf, netdev->mtu);
1774 goto err_disable_napi;
1776 /* Setup segmentation algorithms, if failed, clear offload capability */
1777 otx2_setup_segmentation(pf);
1779 /* Initialize RSS */
1780 err = otx2_rss_init(pf);
1782 goto err_disable_napi;
1784 /* Register Queue IRQ handlers */
1785 vec = pf->hw.nix_msixoff + NIX_LF_QINT_VEC_START;
1786 irq_name = &pf->hw.irq_name[vec * NAME_SIZE];
1788 snprintf(irq_name, NAME_SIZE, "%s-qerr", pf->netdev->name);
1790 err = request_irq(pci_irq_vector(pf->pdev, vec),
1791 otx2_q_intr_handler, 0, irq_name, pf);
1794 "RVUPF%d: IRQ registration failed for QERR\n",
1795 rvu_get_pf(pf->pcifunc));
1796 goto err_disable_napi;
1799 /* Enable QINT IRQ */
1800 otx2_write64(pf, NIX_LF_QINTX_ENA_W1S(0), BIT_ULL(0));
1802 /* Register CQ IRQ handlers */
1803 vec = pf->hw.nix_msixoff + NIX_LF_CINT_VEC_START;
1804 for (qidx = 0; qidx < pf->hw.cint_cnt; qidx++) {
1805 irq_name = &pf->hw.irq_name[vec * NAME_SIZE];
1807 snprintf(irq_name, NAME_SIZE, "%s-rxtx-%d", pf->netdev->name,
1810 err = request_irq(pci_irq_vector(pf->pdev, vec),
1811 otx2_cq_intr_handler, 0, irq_name,
1815 "RVUPF%d: IRQ registration failed for CQ%d\n",
1816 rvu_get_pf(pf->pcifunc), qidx);
1817 goto err_free_cints;
1821 otx2_config_irq_coalescing(pf, qidx);
1824 otx2_write64(pf, NIX_LF_CINTX_INT(qidx), BIT_ULL(0));
1825 otx2_write64(pf, NIX_LF_CINTX_ENA_W1S(qidx), BIT_ULL(0));
1828 otx2_set_cints_affinity(pf);
1830 if (pf->flags & OTX2_FLAG_RX_VLAN_SUPPORT)
1831 otx2_enable_rxvlan(pf, true);
1833 /* When reinitializing enable time stamping if it is enabled before */
1834 if (pf->flags & OTX2_FLAG_TX_TSTAMP_ENABLED) {
1835 pf->flags &= ~OTX2_FLAG_TX_TSTAMP_ENABLED;
1836 otx2_config_hw_tx_tstamp(pf, true);
1838 if (pf->flags & OTX2_FLAG_RX_TSTAMP_ENABLED) {
1839 pf->flags &= ~OTX2_FLAG_RX_TSTAMP_ENABLED;
1840 otx2_config_hw_rx_tstamp(pf, true);
1843 pf->flags &= ~OTX2_FLAG_INTF_DOWN;
1844 /* 'intf_down' may be checked on any cpu */
1847 /* Enable QoS configuration before starting tx queues */
1848 otx2_qos_config_txschq(pf);
1850 /* we have already received link status notification */
1851 if (pf->linfo.link_up && !(pf->pcifunc & RVU_PFVF_FUNC_MASK))
1852 otx2_handle_link_event(pf);
1854 /* Install DMAC Filters */
1855 if (pf->flags & OTX2_FLAG_DMACFLTR_SUPPORT)
1856 otx2_dmacflt_reinstall_flows(pf);
1858 err = otx2_rxtx_enable(pf, true);
1859 /* If a mbox communication error happens at this point then interface
1860 * will end up in a state such that it is in down state but hardware
1861 * mcam entries are enabled to receive the packets. Hence disable the
1865 goto err_disable_rxtx;
1867 goto err_tx_stop_queues;
1869 otx2_do_set_rx_mode(pf);
1874 otx2_rxtx_enable(pf, false);
1876 netif_tx_stop_all_queues(netdev);
1877 netif_carrier_off(netdev);
1878 pf->flags |= OTX2_FLAG_INTF_DOWN;
1880 otx2_free_cints(pf, qidx);
1881 vec = pci_irq_vector(pf->pdev,
1882 pf->hw.nix_msixoff + NIX_LF_QINT_VEC_START);
1883 otx2_write64(pf, NIX_LF_QINTX_ENA_W1C(0), BIT_ULL(0));
1886 otx2_disable_napi(pf);
1887 otx2_free_hw_resources(pf);
1895 EXPORT_SYMBOL(otx2_open);
1897 int otx2_stop(struct net_device *netdev)
1899 struct otx2_nic *pf = netdev_priv(netdev);
1900 struct otx2_cq_poll *cq_poll = NULL;
1901 struct otx2_qset *qset = &pf->qset;
1902 struct otx2_rss_info *rss;
1905 /* If the DOWN flag is set resources are already freed */
1906 if (pf->flags & OTX2_FLAG_INTF_DOWN)
1909 netif_carrier_off(netdev);
1910 netif_tx_stop_all_queues(netdev);
1912 pf->flags |= OTX2_FLAG_INTF_DOWN;
1913 /* 'intf_down' may be checked on any cpu */
1916 /* First stop packet Rx/Tx */
1917 otx2_rxtx_enable(pf, false);
1919 /* Clear RSS enable flag */
1920 rss = &pf->hw.rss_info;
1921 rss->enable = false;
1923 /* Cleanup Queue IRQ */
1924 vec = pci_irq_vector(pf->pdev,
1925 pf->hw.nix_msixoff + NIX_LF_QINT_VEC_START);
1926 otx2_write64(pf, NIX_LF_QINTX_ENA_W1C(0), BIT_ULL(0));
1929 /* Cleanup CQ NAPI and IRQ */
1930 vec = pf->hw.nix_msixoff + NIX_LF_CINT_VEC_START;
1931 for (qidx = 0; qidx < pf->hw.cint_cnt; qidx++) {
1932 /* Disable interrupt */
1933 otx2_write64(pf, NIX_LF_CINTX_ENA_W1C(qidx), BIT_ULL(0));
1935 synchronize_irq(pci_irq_vector(pf->pdev, vec));
1937 cq_poll = &qset->napi[qidx];
1938 napi_synchronize(&cq_poll->napi);
1942 netif_tx_disable(netdev);
1944 otx2_free_hw_resources(pf);
1945 otx2_free_cints(pf, pf->hw.cint_cnt);
1946 otx2_disable_napi(pf);
1948 for (qidx = 0; qidx < netdev->num_tx_queues; qidx++)
1949 netdev_tx_reset_queue(netdev_get_tx_queue(netdev, qidx));
1951 for (wrk = 0; wrk < pf->qset.cq_cnt; wrk++)
1952 cancel_delayed_work_sync(&pf->refill_wrk[wrk].pool_refill_work);
1953 devm_kfree(pf->dev, pf->refill_wrk);
1959 /* Do not clear RQ/SQ ringsize settings */
1960 memset_startat(qset, 0, sqe_cnt);
1963 EXPORT_SYMBOL(otx2_stop);
1965 static netdev_tx_t otx2_xmit(struct sk_buff *skb, struct net_device *netdev)
1967 struct otx2_nic *pf = netdev_priv(netdev);
1968 int qidx = skb_get_queue_mapping(skb);
1969 struct otx2_snd_queue *sq;
1970 struct netdev_queue *txq;
1973 /* XDP SQs are not mapped with TXQs
1974 * advance qid to derive correct sq mapped with QOS
1976 sq_idx = (qidx >= pf->hw.tx_queues) ? (qidx + pf->hw.xdp_queues) : qidx;
1978 /* Check for minimum and maximum packet length */
1979 if (skb->len <= ETH_HLEN ||
1980 (!skb_shinfo(skb)->gso_size && skb->len > pf->tx_max_pktlen)) {
1982 return NETDEV_TX_OK;
1985 sq = &pf->qset.sq[sq_idx];
1986 txq = netdev_get_tx_queue(netdev, qidx);
1988 if (!otx2_sq_append_skb(netdev, sq, skb, qidx)) {
1989 netif_tx_stop_queue(txq);
1991 /* Check again, incase SQBs got freed up */
1993 if (((sq->num_sqbs - *sq->aura_fc_addr) * sq->sqe_per_sqb)
1995 netif_tx_wake_queue(txq);
1997 return NETDEV_TX_BUSY;
2000 return NETDEV_TX_OK;
2003 static int otx2_qos_select_htb_queue(struct otx2_nic *pf, struct sk_buff *skb,
2008 if ((TC_H_MAJ(skb->priority) >> 16) == htb_maj_id)
2009 classid = TC_H_MIN(skb->priority);
2011 classid = READ_ONCE(pf->qos.defcls);
2016 return otx2_get_txq_by_classid(pf, classid);
2019 u16 otx2_select_queue(struct net_device *netdev, struct sk_buff *skb,
2020 struct net_device *sb_dev)
2022 struct otx2_nic *pf = netdev_priv(netdev);
2029 qos_enabled = (netdev->real_num_tx_queues > pf->hw.tx_queues) ? true : false;
2030 if (unlikely(qos_enabled)) {
2031 /* This smp_load_acquire() pairs with smp_store_release() in
2032 * otx2_qos_root_add() called from htb offload root creation
2034 u16 htb_maj_id = smp_load_acquire(&pf->qos.maj_id);
2036 if (unlikely(htb_maj_id)) {
2037 txq = otx2_qos_select_htb_queue(pf, skb, htb_maj_id);
2046 if (!skb_vlan_tag_present(skb))
2049 vlan_prio = skb->vlan_tci >> 13;
2050 if ((vlan_prio > pf->hw.tx_queues - 1) ||
2051 !pf->pfc_alloc_status[vlan_prio])
2058 txq = netdev_pick_tx(netdev, skb, NULL);
2059 if (unlikely(qos_enabled))
2060 return txq % pf->hw.tx_queues;
2064 EXPORT_SYMBOL(otx2_select_queue);
2066 static netdev_features_t otx2_fix_features(struct net_device *dev,
2067 netdev_features_t features)
2069 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2070 features |= NETIF_F_HW_VLAN_STAG_RX;
2072 features &= ~NETIF_F_HW_VLAN_STAG_RX;
2077 static void otx2_set_rx_mode(struct net_device *netdev)
2079 struct otx2_nic *pf = netdev_priv(netdev);
2081 queue_work(pf->otx2_wq, &pf->rx_mode_work);
2084 static void otx2_rx_mode_wrk_handler(struct work_struct *work)
2086 struct otx2_nic *pf = container_of(work, struct otx2_nic, rx_mode_work);
2088 otx2_do_set_rx_mode(pf);
2091 static int otx2_set_features(struct net_device *netdev,
2092 netdev_features_t features)
2094 netdev_features_t changed = features ^ netdev->features;
2095 struct otx2_nic *pf = netdev_priv(netdev);
2097 if ((changed & NETIF_F_LOOPBACK) && netif_running(netdev))
2098 return otx2_cgx_config_loopback(pf,
2099 features & NETIF_F_LOOPBACK);
2101 if ((changed & NETIF_F_HW_VLAN_CTAG_RX) && netif_running(netdev))
2102 return otx2_enable_rxvlan(pf,
2103 features & NETIF_F_HW_VLAN_CTAG_RX);
2105 return otx2_handle_ntuple_tc_features(netdev, features);
2108 static void otx2_reset_task(struct work_struct *work)
2110 struct otx2_nic *pf = container_of(work, struct otx2_nic, reset_task);
2112 if (!netif_running(pf->netdev))
2116 otx2_stop(pf->netdev);
2118 otx2_open(pf->netdev);
2119 netif_trans_update(pf->netdev);
2123 static int otx2_config_hw_rx_tstamp(struct otx2_nic *pfvf, bool enable)
2125 struct msg_req *req;
2128 if (pfvf->flags & OTX2_FLAG_RX_TSTAMP_ENABLED && enable)
2131 mutex_lock(&pfvf->mbox.lock);
2133 req = otx2_mbox_alloc_msg_cgx_ptp_rx_enable(&pfvf->mbox);
2135 req = otx2_mbox_alloc_msg_cgx_ptp_rx_disable(&pfvf->mbox);
2137 mutex_unlock(&pfvf->mbox.lock);
2141 err = otx2_sync_mbox_msg(&pfvf->mbox);
2143 mutex_unlock(&pfvf->mbox.lock);
2147 mutex_unlock(&pfvf->mbox.lock);
2149 pfvf->flags |= OTX2_FLAG_RX_TSTAMP_ENABLED;
2151 pfvf->flags &= ~OTX2_FLAG_RX_TSTAMP_ENABLED;
2155 static int otx2_config_hw_tx_tstamp(struct otx2_nic *pfvf, bool enable)
2157 struct msg_req *req;
2160 if (pfvf->flags & OTX2_FLAG_TX_TSTAMP_ENABLED && enable)
2163 mutex_lock(&pfvf->mbox.lock);
2165 req = otx2_mbox_alloc_msg_nix_lf_ptp_tx_enable(&pfvf->mbox);
2167 req = otx2_mbox_alloc_msg_nix_lf_ptp_tx_disable(&pfvf->mbox);
2169 mutex_unlock(&pfvf->mbox.lock);
2173 err = otx2_sync_mbox_msg(&pfvf->mbox);
2175 mutex_unlock(&pfvf->mbox.lock);
2179 mutex_unlock(&pfvf->mbox.lock);
2181 pfvf->flags |= OTX2_FLAG_TX_TSTAMP_ENABLED;
2183 pfvf->flags &= ~OTX2_FLAG_TX_TSTAMP_ENABLED;
2187 int otx2_config_hwtstamp(struct net_device *netdev, struct ifreq *ifr)
2189 struct otx2_nic *pfvf = netdev_priv(netdev);
2190 struct hwtstamp_config config;
2195 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
2198 switch (config.tx_type) {
2199 case HWTSTAMP_TX_OFF:
2200 if (pfvf->flags & OTX2_FLAG_PTP_ONESTEP_SYNC)
2201 pfvf->flags &= ~OTX2_FLAG_PTP_ONESTEP_SYNC;
2203 cancel_delayed_work(&pfvf->ptp->synctstamp_work);
2204 otx2_config_hw_tx_tstamp(pfvf, false);
2206 case HWTSTAMP_TX_ONESTEP_SYNC:
2207 if (!test_bit(CN10K_PTP_ONESTEP, &pfvf->hw.cap_flag))
2209 pfvf->flags |= OTX2_FLAG_PTP_ONESTEP_SYNC;
2210 schedule_delayed_work(&pfvf->ptp->synctstamp_work,
2211 msecs_to_jiffies(500));
2213 case HWTSTAMP_TX_ON:
2214 otx2_config_hw_tx_tstamp(pfvf, true);
2220 switch (config.rx_filter) {
2221 case HWTSTAMP_FILTER_NONE:
2222 otx2_config_hw_rx_tstamp(pfvf, false);
2224 case HWTSTAMP_FILTER_ALL:
2225 case HWTSTAMP_FILTER_SOME:
2226 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
2227 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
2228 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2229 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2230 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2231 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2232 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
2233 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
2234 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
2235 case HWTSTAMP_FILTER_PTP_V2_EVENT:
2236 case HWTSTAMP_FILTER_PTP_V2_SYNC:
2237 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
2238 otx2_config_hw_rx_tstamp(pfvf, true);
2239 config.rx_filter = HWTSTAMP_FILTER_ALL;
2245 memcpy(&pfvf->tstamp, &config, sizeof(config));
2247 return copy_to_user(ifr->ifr_data, &config,
2248 sizeof(config)) ? -EFAULT : 0;
2250 EXPORT_SYMBOL(otx2_config_hwtstamp);
2252 int otx2_ioctl(struct net_device *netdev, struct ifreq *req, int cmd)
2254 struct otx2_nic *pfvf = netdev_priv(netdev);
2255 struct hwtstamp_config *cfg = &pfvf->tstamp;
2259 return otx2_config_hwtstamp(netdev, req);
2261 return copy_to_user(req->ifr_data, cfg,
2262 sizeof(*cfg)) ? -EFAULT : 0;
2267 EXPORT_SYMBOL(otx2_ioctl);
2269 static int otx2_do_set_vf_mac(struct otx2_nic *pf, int vf, const u8 *mac)
2271 struct npc_install_flow_req *req;
2274 mutex_lock(&pf->mbox.lock);
2275 req = otx2_mbox_alloc_msg_npc_install_flow(&pf->mbox);
2281 ether_addr_copy(req->packet.dmac, mac);
2282 eth_broadcast_addr((u8 *)&req->mask.dmac);
2283 req->features = BIT_ULL(NPC_DMAC);
2284 req->channel = pf->hw.rx_chan_base;
2285 req->intf = NIX_INTF_RX;
2286 req->default_rule = 1;
2289 req->op = NIX_RX_ACTION_DEFAULT;
2291 err = otx2_sync_mbox_msg(&pf->mbox);
2293 mutex_unlock(&pf->mbox.lock);
2297 static int otx2_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
2299 struct otx2_nic *pf = netdev_priv(netdev);
2300 struct pci_dev *pdev = pf->pdev;
2301 struct otx2_vf_config *config;
2304 if (!netif_running(netdev))
2307 if (vf >= pf->total_vfs)
2310 if (!is_valid_ether_addr(mac))
2313 config = &pf->vf_configs[vf];
2314 ether_addr_copy(config->mac, mac);
2316 ret = otx2_do_set_vf_mac(pf, vf, mac);
2318 dev_info(&pdev->dev,
2319 "Load/Reload VF driver\n");
2324 static int otx2_do_set_vf_vlan(struct otx2_nic *pf, int vf, u16 vlan, u8 qos,
2327 struct otx2_flow_config *flow_cfg = pf->flow_cfg;
2328 struct nix_vtag_config_rsp *vtag_rsp;
2329 struct npc_delete_flow_req *del_req;
2330 struct nix_vtag_config *vtag_req;
2331 struct npc_install_flow_req *req;
2332 struct otx2_vf_config *config;
2336 config = &pf->vf_configs[vf];
2338 if (!vlan && !config->vlan)
2341 mutex_lock(&pf->mbox.lock);
2343 /* free old tx vtag entry */
2345 vtag_req = otx2_mbox_alloc_msg_nix_vtag_cfg(&pf->mbox);
2350 vtag_req->cfg_type = 0;
2351 vtag_req->tx.free_vtag0 = 1;
2352 vtag_req->tx.vtag0_idx = config->tx_vtag_idx;
2354 err = otx2_sync_mbox_msg(&pf->mbox);
2359 if (!vlan && config->vlan) {
2361 del_req = otx2_mbox_alloc_msg_npc_delete_flow(&pf->mbox);
2366 idx = ((vf * OTX2_PER_VF_VLAN_FLOWS) + OTX2_VF_VLAN_RX_INDEX);
2368 flow_cfg->def_ent[flow_cfg->vf_vlan_offset + idx];
2369 err = otx2_sync_mbox_msg(&pf->mbox);
2374 del_req = otx2_mbox_alloc_msg_npc_delete_flow(&pf->mbox);
2379 idx = ((vf * OTX2_PER_VF_VLAN_FLOWS) + OTX2_VF_VLAN_TX_INDEX);
2381 flow_cfg->def_ent[flow_cfg->vf_vlan_offset + idx];
2382 err = otx2_sync_mbox_msg(&pf->mbox);
2388 req = otx2_mbox_alloc_msg_npc_install_flow(&pf->mbox);
2394 idx = ((vf * OTX2_PER_VF_VLAN_FLOWS) + OTX2_VF_VLAN_RX_INDEX);
2395 req->entry = flow_cfg->def_ent[flow_cfg->vf_vlan_offset + idx];
2396 req->packet.vlan_tci = htons(vlan);
2397 req->mask.vlan_tci = htons(VLAN_VID_MASK);
2398 /* af fills the destination mac addr */
2399 eth_broadcast_addr((u8 *)&req->mask.dmac);
2400 req->features = BIT_ULL(NPC_OUTER_VID) | BIT_ULL(NPC_DMAC);
2401 req->channel = pf->hw.rx_chan_base;
2402 req->intf = NIX_INTF_RX;
2404 req->op = NIX_RX_ACTION_DEFAULT;
2405 req->vtag0_valid = true;
2406 req->vtag0_type = NIX_AF_LFX_RX_VTAG_TYPE7;
2409 err = otx2_sync_mbox_msg(&pf->mbox);
2414 vtag_req = otx2_mbox_alloc_msg_nix_vtag_cfg(&pf->mbox);
2420 /* configure tx vtag params */
2421 vtag_req->vtag_size = VTAGSIZE_T4;
2422 vtag_req->cfg_type = 0; /* tx vlan cfg */
2423 vtag_req->tx.cfg_vtag0 = 1;
2424 vtag_req->tx.vtag0 = ((u64)ntohs(proto) << 16) | vlan;
2426 err = otx2_sync_mbox_msg(&pf->mbox);
2430 vtag_rsp = (struct nix_vtag_config_rsp *)otx2_mbox_get_rsp
2431 (&pf->mbox.mbox, 0, &vtag_req->hdr);
2432 if (IS_ERR(vtag_rsp)) {
2433 err = PTR_ERR(vtag_rsp);
2436 config->tx_vtag_idx = vtag_rsp->vtag0_idx;
2438 req = otx2_mbox_alloc_msg_npc_install_flow(&pf->mbox);
2444 eth_zero_addr((u8 *)&req->mask.dmac);
2445 idx = ((vf * OTX2_PER_VF_VLAN_FLOWS) + OTX2_VF_VLAN_TX_INDEX);
2446 req->entry = flow_cfg->def_ent[flow_cfg->vf_vlan_offset + idx];
2447 req->features = BIT_ULL(NPC_DMAC);
2448 req->channel = pf->hw.tx_chan_base;
2449 req->intf = NIX_INTF_TX;
2451 req->op = NIX_TX_ACTIONOP_UCAST_DEFAULT;
2452 req->vtag0_def = vtag_rsp->vtag0_idx;
2453 req->vtag0_op = VTAG_INSERT;
2456 err = otx2_sync_mbox_msg(&pf->mbox);
2458 config->vlan = vlan;
2459 mutex_unlock(&pf->mbox.lock);
2463 static int otx2_set_vf_vlan(struct net_device *netdev, int vf, u16 vlan, u8 qos,
2466 struct otx2_nic *pf = netdev_priv(netdev);
2467 struct pci_dev *pdev = pf->pdev;
2469 if (!netif_running(netdev))
2472 if (vf >= pci_num_vf(pdev))
2475 /* qos is currently unsupported */
2476 if (vlan >= VLAN_N_VID || qos)
2479 if (proto != htons(ETH_P_8021Q))
2480 return -EPROTONOSUPPORT;
2482 if (!(pf->flags & OTX2_FLAG_VF_VLAN_SUPPORT))
2485 return otx2_do_set_vf_vlan(pf, vf, vlan, qos, proto);
2488 static int otx2_get_vf_config(struct net_device *netdev, int vf,
2489 struct ifla_vf_info *ivi)
2491 struct otx2_nic *pf = netdev_priv(netdev);
2492 struct pci_dev *pdev = pf->pdev;
2493 struct otx2_vf_config *config;
2495 if (!netif_running(netdev))
2498 if (vf >= pci_num_vf(pdev))
2501 config = &pf->vf_configs[vf];
2503 ether_addr_copy(ivi->mac, config->mac);
2504 ivi->vlan = config->vlan;
2505 ivi->trusted = config->trusted;
2510 static int otx2_xdp_xmit_tx(struct otx2_nic *pf, struct xdp_frame *xdpf,
2517 dma_addr = otx2_dma_map_page(pf, virt_to_page(xdpf->data),
2518 offset_in_page(xdpf->data), xdpf->len,
2520 if (dma_mapping_error(pf->dev, dma_addr))
2523 err = otx2_xdp_sq_append_pkt(pf, dma_addr, xdpf->len, qidx);
2525 otx2_dma_unmap_page(pf, dma_addr, xdpf->len, DMA_TO_DEVICE);
2526 page = virt_to_page(xdpf->data);
2533 static int otx2_xdp_xmit(struct net_device *netdev, int n,
2534 struct xdp_frame **frames, u32 flags)
2536 struct otx2_nic *pf = netdev_priv(netdev);
2537 int qidx = smp_processor_id();
2538 struct otx2_snd_queue *sq;
2541 if (!netif_running(netdev))
2544 qidx += pf->hw.tx_queues;
2545 sq = pf->xdp_prog ? &pf->qset.sq[qidx] : NULL;
2547 /* Abort xmit if xdp queue is not */
2551 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
2554 for (i = 0; i < n; i++) {
2555 struct xdp_frame *xdpf = frames[i];
2558 err = otx2_xdp_xmit_tx(pf, xdpf, qidx);
2565 static int otx2_xdp_setup(struct otx2_nic *pf, struct bpf_prog *prog)
2567 struct net_device *dev = pf->netdev;
2568 bool if_up = netif_running(pf->netdev);
2569 struct bpf_prog *old_prog;
2571 if (prog && dev->mtu > MAX_XDP_MTU) {
2572 netdev_warn(dev, "Jumbo frames not yet supported with XDP\n");
2577 otx2_stop(pf->netdev);
2579 old_prog = xchg(&pf->xdp_prog, prog);
2582 bpf_prog_put(old_prog);
2585 bpf_prog_add(pf->xdp_prog, pf->hw.rx_queues - 1);
2587 /* Network stack and XDP shared same rx queues.
2588 * Use separate tx queues for XDP and network stack.
2591 pf->hw.xdp_queues = pf->hw.rx_queues;
2592 xdp_features_set_redirect_target(dev, false);
2594 pf->hw.xdp_queues = 0;
2595 xdp_features_clear_redirect_target(dev);
2598 pf->hw.non_qos_queues += pf->hw.xdp_queues;
2601 otx2_open(pf->netdev);
2606 static int otx2_xdp(struct net_device *netdev, struct netdev_bpf *xdp)
2608 struct otx2_nic *pf = netdev_priv(netdev);
2610 switch (xdp->command) {
2611 case XDP_SETUP_PROG:
2612 return otx2_xdp_setup(pf, xdp->prog);
2618 static int otx2_set_vf_permissions(struct otx2_nic *pf, int vf,
2621 struct set_vf_perm *req;
2624 mutex_lock(&pf->mbox.lock);
2625 req = otx2_mbox_alloc_msg_set_vf_perm(&pf->mbox);
2631 /* Let AF reset VF permissions as sriov is disabled */
2632 if (req_perm == OTX2_RESET_VF_PERM) {
2633 req->flags |= RESET_VF_PERM;
2634 } else if (req_perm == OTX2_TRUSTED_VF) {
2635 if (pf->vf_configs[vf].trusted)
2636 req->flags |= VF_TRUSTED;
2640 rc = otx2_sync_mbox_msg(&pf->mbox);
2642 mutex_unlock(&pf->mbox.lock);
2646 static int otx2_ndo_set_vf_trust(struct net_device *netdev, int vf,
2649 struct otx2_nic *pf = netdev_priv(netdev);
2650 struct pci_dev *pdev = pf->pdev;
2653 if (vf >= pci_num_vf(pdev))
2656 if (pf->vf_configs[vf].trusted == enable)
2659 pf->vf_configs[vf].trusted = enable;
2660 rc = otx2_set_vf_permissions(pf, vf, OTX2_TRUSTED_VF);
2663 pf->vf_configs[vf].trusted = !enable;
2665 netdev_info(pf->netdev, "VF %d is %strusted\n",
2666 vf, enable ? "" : "not ");
2670 static const struct net_device_ops otx2_netdev_ops = {
2671 .ndo_open = otx2_open,
2672 .ndo_stop = otx2_stop,
2673 .ndo_start_xmit = otx2_xmit,
2674 .ndo_select_queue = otx2_select_queue,
2675 .ndo_fix_features = otx2_fix_features,
2676 .ndo_set_mac_address = otx2_set_mac_address,
2677 .ndo_change_mtu = otx2_change_mtu,
2678 .ndo_set_rx_mode = otx2_set_rx_mode,
2679 .ndo_set_features = otx2_set_features,
2680 .ndo_tx_timeout = otx2_tx_timeout,
2681 .ndo_get_stats64 = otx2_get_stats64,
2682 .ndo_eth_ioctl = otx2_ioctl,
2683 .ndo_set_vf_mac = otx2_set_vf_mac,
2684 .ndo_set_vf_vlan = otx2_set_vf_vlan,
2685 .ndo_get_vf_config = otx2_get_vf_config,
2686 .ndo_bpf = otx2_xdp,
2687 .ndo_xdp_xmit = otx2_xdp_xmit,
2688 .ndo_setup_tc = otx2_setup_tc,
2689 .ndo_set_vf_trust = otx2_ndo_set_vf_trust,
2692 static int otx2_wq_init(struct otx2_nic *pf)
2694 pf->otx2_wq = create_singlethread_workqueue("otx2_wq");
2698 INIT_WORK(&pf->rx_mode_work, otx2_rx_mode_wrk_handler);
2699 INIT_WORK(&pf->reset_task, otx2_reset_task);
2703 static int otx2_check_pf_usable(struct otx2_nic *nic)
2707 rev = otx2_read64(nic, RVU_PF_BLOCK_ADDRX_DISC(BLKADDR_RVUM));
2708 rev = (rev >> 12) & 0xFF;
2709 /* Check if AF has setup revision for RVUM block,
2710 * otherwise this driver probe should be deferred
2711 * until AF driver comes up.
2715 "AF is not initialized, deferring probe\n");
2716 return -EPROBE_DEFER;
2721 static int otx2_realloc_msix_vectors(struct otx2_nic *pf)
2723 struct otx2_hw *hw = &pf->hw;
2726 /* NPA interrupts are inot registered, so alloc only
2727 * upto NIX vector offset.
2729 num_vec = hw->nix_msixoff;
2730 num_vec += NIX_LF_CINT_VEC_START + hw->max_queues;
2732 otx2_disable_mbox_intr(pf);
2733 pci_free_irq_vectors(hw->pdev);
2734 err = pci_alloc_irq_vectors(hw->pdev, num_vec, num_vec, PCI_IRQ_MSIX);
2736 dev_err(pf->dev, "%s: Failed to realloc %d IRQ vectors\n",
2741 return otx2_register_mbox_intr(pf, false);
2744 static int otx2_sriov_vfcfg_init(struct otx2_nic *pf)
2748 pf->vf_configs = devm_kcalloc(pf->dev, pf->total_vfs,
2749 sizeof(struct otx2_vf_config),
2751 if (!pf->vf_configs)
2754 for (i = 0; i < pf->total_vfs; i++) {
2755 pf->vf_configs[i].pf = pf;
2756 pf->vf_configs[i].intf_down = true;
2757 pf->vf_configs[i].trusted = false;
2758 INIT_DELAYED_WORK(&pf->vf_configs[i].link_event_work,
2759 otx2_vf_link_event_task);
2765 static void otx2_sriov_vfcfg_cleanup(struct otx2_nic *pf)
2769 if (!pf->vf_configs)
2772 for (i = 0; i < pf->total_vfs; i++) {
2773 cancel_delayed_work_sync(&pf->vf_configs[i].link_event_work);
2774 otx2_set_vf_permissions(pf, i, OTX2_RESET_VF_PERM);
2778 static int otx2_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2780 struct device *dev = &pdev->dev;
2781 int err, qcount, qos_txqs;
2782 struct net_device *netdev;
2783 struct otx2_nic *pf;
2787 err = pcim_enable_device(pdev);
2789 dev_err(dev, "Failed to enable PCI device\n");
2793 err = pci_request_regions(pdev, DRV_NAME);
2795 dev_err(dev, "PCI request regions failed 0x%x\n", err);
2799 err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48));
2801 dev_err(dev, "DMA mask config failed, abort\n");
2802 goto err_release_regions;
2805 pci_set_master(pdev);
2807 /* Set number of queues */
2808 qcount = min_t(int, num_online_cpus(), OTX2_MAX_CQ_CNT);
2809 qos_txqs = min_t(int, qcount, OTX2_QOS_MAX_LEAF_NODES);
2811 netdev = alloc_etherdev_mqs(sizeof(*pf), qcount + qos_txqs, qcount);
2814 goto err_release_regions;
2817 pci_set_drvdata(pdev, netdev);
2818 SET_NETDEV_DEV(netdev, &pdev->dev);
2819 pf = netdev_priv(netdev);
2820 pf->netdev = netdev;
2823 pf->total_vfs = pci_sriov_get_totalvfs(pdev);
2824 pf->flags |= OTX2_FLAG_INTF_DOWN;
2828 hw->rx_queues = qcount;
2829 hw->tx_queues = qcount;
2830 hw->non_qos_queues = qcount;
2831 hw->max_queues = qcount;
2832 hw->rbuf_len = OTX2_DEFAULT_RBUF_LEN;
2833 /* Use CQE of 128 byte descriptor size by default */
2836 num_vec = pci_msix_vec_count(pdev);
2837 hw->irq_name = devm_kmalloc_array(&hw->pdev->dev, num_vec, NAME_SIZE,
2839 if (!hw->irq_name) {
2841 goto err_free_netdev;
2844 hw->affinity_mask = devm_kcalloc(&hw->pdev->dev, num_vec,
2845 sizeof(cpumask_var_t), GFP_KERNEL);
2846 if (!hw->affinity_mask) {
2848 goto err_free_netdev;
2852 pf->reg_base = pcim_iomap(pdev, PCI_CFG_REG_BAR_NUM, 0);
2853 if (!pf->reg_base) {
2854 dev_err(dev, "Unable to map physical function CSRs, aborting\n");
2856 goto err_free_netdev;
2859 err = otx2_check_pf_usable(pf);
2861 goto err_free_netdev;
2863 err = pci_alloc_irq_vectors(hw->pdev, RVU_PF_INT_VEC_CNT,
2864 RVU_PF_INT_VEC_CNT, PCI_IRQ_MSIX);
2866 dev_err(dev, "%s: Failed to alloc %d IRQ vectors\n",
2868 goto err_free_netdev;
2871 otx2_setup_dev_hw_settings(pf);
2873 /* Init PF <=> AF mailbox stuff */
2874 err = otx2_pfaf_mbox_init(pf);
2876 goto err_free_irq_vectors;
2878 /* Register mailbox interrupt */
2879 err = otx2_register_mbox_intr(pf, true);
2881 goto err_mbox_destroy;
2883 /* Request AF to attach NPA and NIX LFs to this PF.
2884 * NIX and NPA LFs are needed for this PF to function as a NIC.
2886 err = otx2_attach_npa_nix(pf);
2888 goto err_disable_mbox_intr;
2890 err = otx2_realloc_msix_vectors(pf);
2892 goto err_detach_rsrc;
2894 err = otx2_set_real_num_queues(netdev, hw->tx_queues, hw->rx_queues);
2896 goto err_detach_rsrc;
2898 err = cn10k_lmtst_init(pf);
2900 goto err_detach_rsrc;
2902 /* Assign default mac address */
2903 otx2_get_mac_from_af(netdev);
2905 /* Don't check for error. Proceed without ptp */
2908 /* NPA's pool is a stack to which SW frees buffer pointers via Aura.
2909 * HW allocates buffer pointer from stack and uses it for DMA'ing
2910 * ingress packet. In some scenarios HW can free back allocated buffer
2911 * pointers to pool. This makes it impossible for SW to maintain a
2912 * parallel list where physical addresses of buffer pointers (IOVAs)
2913 * given to HW can be saved for later reference.
2915 * So the only way to convert Rx packet's buffer address is to use
2916 * IOMMU's iova_to_phys() handler which translates the address by
2917 * walking through the translation tables.
2919 pf->iommu_domain = iommu_get_domain_for_dev(dev);
2921 netdev->hw_features = (NETIF_F_RXCSUM | NETIF_F_IP_CSUM |
2922 NETIF_F_IPV6_CSUM | NETIF_F_RXHASH |
2923 NETIF_F_SG | NETIF_F_TSO | NETIF_F_TSO6 |
2924 NETIF_F_GSO_UDP_L4);
2925 netdev->features |= netdev->hw_features;
2927 err = otx2_mcam_flow_init(pf);
2929 goto err_ptp_destroy;
2931 err = cn10k_mcs_init(pf);
2933 goto err_del_mcam_entries;
2935 if (pf->flags & OTX2_FLAG_NTUPLE_SUPPORT)
2936 netdev->hw_features |= NETIF_F_NTUPLE;
2938 if (pf->flags & OTX2_FLAG_UCAST_FLTR_SUPPORT)
2939 netdev->priv_flags |= IFF_UNICAST_FLT;
2941 /* Support TSO on tag interface */
2942 netdev->vlan_features |= netdev->features;
2943 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX |
2944 NETIF_F_HW_VLAN_STAG_TX;
2945 if (pf->flags & OTX2_FLAG_RX_VLAN_SUPPORT)
2946 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX |
2947 NETIF_F_HW_VLAN_STAG_RX;
2948 netdev->features |= netdev->hw_features;
2950 /* HW supports tc offload but mutually exclusive with n-tuple filters */
2951 if (pf->flags & OTX2_FLAG_TC_FLOWER_SUPPORT)
2952 netdev->hw_features |= NETIF_F_HW_TC;
2954 netdev->hw_features |= NETIF_F_LOOPBACK | NETIF_F_RXALL;
2956 netif_set_tso_max_segs(netdev, OTX2_MAX_GSO_SEGS);
2957 netdev->watchdog_timeo = OTX2_TX_TIMEOUT;
2959 netdev->netdev_ops = &otx2_netdev_ops;
2960 netdev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT;
2962 netdev->min_mtu = OTX2_MIN_MTU;
2963 netdev->max_mtu = otx2_get_max_mtu(pf);
2965 err = register_netdev(netdev);
2967 dev_err(dev, "Failed to register netdevice\n");
2971 err = otx2_wq_init(pf);
2973 goto err_unreg_netdev;
2975 otx2_set_ethtool_ops(netdev);
2977 err = otx2_init_tc(pf);
2979 goto err_mcam_flow_del;
2981 err = otx2_register_dl(pf);
2983 goto err_mcam_flow_del;
2985 /* Initialize SR-IOV resources */
2986 err = otx2_sriov_vfcfg_init(pf);
2988 goto err_pf_sriov_init;
2990 /* Enable link notifications */
2991 otx2_cgx_config_linkevents(pf, true);
2994 err = otx2_dcbnl_set_ops(netdev);
2996 goto err_pf_sriov_init;
2999 otx2_qos_init(pf, qos_txqs);
3004 otx2_shutdown_tc(pf);
3006 otx2_mcam_flow_del(pf);
3008 unregister_netdev(netdev);
3011 err_del_mcam_entries:
3012 otx2_mcam_flow_del(pf);
3014 otx2_ptp_destroy(pf);
3016 if (pf->hw.lmt_info)
3017 free_percpu(pf->hw.lmt_info);
3018 if (test_bit(CN10K_LMTST, &pf->hw.cap_flag))
3019 qmem_free(pf->dev, pf->dync_lmt);
3020 otx2_detach_resources(&pf->mbox);
3021 err_disable_mbox_intr:
3022 otx2_disable_mbox_intr(pf);
3024 otx2_pfaf_mbox_destroy(pf);
3025 err_free_irq_vectors:
3026 pci_free_irq_vectors(hw->pdev);
3028 pci_set_drvdata(pdev, NULL);
3029 free_netdev(netdev);
3030 err_release_regions:
3031 pci_release_regions(pdev);
3035 static void otx2_vf_link_event_task(struct work_struct *work)
3037 struct otx2_vf_config *config;
3038 struct cgx_link_info_msg *req;
3039 struct mbox_msghdr *msghdr;
3040 struct otx2_nic *pf;
3043 config = container_of(work, struct otx2_vf_config,
3044 link_event_work.work);
3045 vf_idx = config - config->pf->vf_configs;
3048 msghdr = otx2_mbox_alloc_msg_rsp(&pf->mbox_pfvf[0].mbox_up, vf_idx,
3049 sizeof(*req), sizeof(struct msg_rsp));
3051 dev_err(pf->dev, "Failed to create VF%d link event\n", vf_idx);
3055 req = (struct cgx_link_info_msg *)msghdr;
3056 req->hdr.id = MBOX_MSG_CGX_LINK_EVENT;
3057 req->hdr.sig = OTX2_MBOX_REQ_SIG;
3058 memcpy(&req->link_info, &pf->linfo, sizeof(req->link_info));
3060 otx2_sync_mbox_up_msg(&pf->mbox_pfvf[0], vf_idx);
3063 static int otx2_sriov_enable(struct pci_dev *pdev, int numvfs)
3065 struct net_device *netdev = pci_get_drvdata(pdev);
3066 struct otx2_nic *pf = netdev_priv(netdev);
3069 /* Init PF <=> VF mailbox stuff */
3070 ret = otx2_pfvf_mbox_init(pf, numvfs);
3074 ret = otx2_register_pfvf_mbox_intr(pf, numvfs);
3078 ret = otx2_pf_flr_init(pf, numvfs);
3082 ret = otx2_register_flr_me_intr(pf, numvfs);
3086 ret = pci_enable_sriov(pdev, numvfs);
3092 otx2_disable_flr_me_intr(pf);
3094 otx2_flr_wq_destroy(pf);
3096 otx2_disable_pfvf_mbox_intr(pf, numvfs);
3098 otx2_pfvf_mbox_destroy(pf);
3102 static int otx2_sriov_disable(struct pci_dev *pdev)
3104 struct net_device *netdev = pci_get_drvdata(pdev);
3105 struct otx2_nic *pf = netdev_priv(netdev);
3106 int numvfs = pci_num_vf(pdev);
3111 pci_disable_sriov(pdev);
3113 otx2_disable_flr_me_intr(pf);
3114 otx2_flr_wq_destroy(pf);
3115 otx2_disable_pfvf_mbox_intr(pf, numvfs);
3116 otx2_pfvf_mbox_destroy(pf);
3121 static int otx2_sriov_configure(struct pci_dev *pdev, int numvfs)
3124 return otx2_sriov_disable(pdev);
3126 return otx2_sriov_enable(pdev, numvfs);
3129 static void otx2_remove(struct pci_dev *pdev)
3131 struct net_device *netdev = pci_get_drvdata(pdev);
3132 struct otx2_nic *pf;
3137 pf = netdev_priv(netdev);
3139 pf->flags |= OTX2_FLAG_PF_SHUTDOWN;
3141 if (pf->flags & OTX2_FLAG_TX_TSTAMP_ENABLED)
3142 otx2_config_hw_tx_tstamp(pf, false);
3143 if (pf->flags & OTX2_FLAG_RX_TSTAMP_ENABLED)
3144 otx2_config_hw_rx_tstamp(pf, false);
3146 /* Disable 802.3x pause frames */
3147 if (pf->flags & OTX2_FLAG_RX_PAUSE_ENABLED ||
3148 (pf->flags & OTX2_FLAG_TX_PAUSE_ENABLED)) {
3149 pf->flags &= ~OTX2_FLAG_RX_PAUSE_ENABLED;
3150 pf->flags &= ~OTX2_FLAG_TX_PAUSE_ENABLED;
3151 otx2_config_pause_frm(pf);
3155 /* Disable PFC config */
3158 otx2_config_priority_flow_ctrl(pf);
3161 cancel_work_sync(&pf->reset_task);
3162 /* Disable link notifications */
3163 otx2_cgx_config_linkevents(pf, false);
3165 otx2_unregister_dl(pf);
3166 unregister_netdev(netdev);
3168 otx2_sriov_disable(pf->pdev);
3169 otx2_sriov_vfcfg_cleanup(pf);
3171 destroy_workqueue(pf->otx2_wq);
3173 otx2_ptp_destroy(pf);
3174 otx2_mcam_flow_del(pf);
3175 otx2_shutdown_tc(pf);
3176 otx2_shutdown_qos(pf);
3177 otx2_detach_resources(&pf->mbox);
3178 if (pf->hw.lmt_info)
3179 free_percpu(pf->hw.lmt_info);
3180 if (test_bit(CN10K_LMTST, &pf->hw.cap_flag))
3181 qmem_free(pf->dev, pf->dync_lmt);
3182 otx2_disable_mbox_intr(pf);
3183 otx2_pfaf_mbox_destroy(pf);
3184 pci_free_irq_vectors(pf->pdev);
3185 pci_set_drvdata(pdev, NULL);
3186 free_netdev(netdev);
3188 pci_release_regions(pdev);
3191 static struct pci_driver otx2_pf_driver = {
3193 .id_table = otx2_pf_id_table,
3194 .probe = otx2_probe,
3195 .shutdown = otx2_remove,
3196 .remove = otx2_remove,
3197 .sriov_configure = otx2_sriov_configure
3200 static int __init otx2_rvupf_init_module(void)
3202 pr_info("%s: %s\n", DRV_NAME, DRV_STRING);
3204 return pci_register_driver(&otx2_pf_driver);
3207 static void __exit otx2_rvupf_cleanup_module(void)
3209 pci_unregister_driver(&otx2_pf_driver);
3212 module_init(otx2_rvupf_init_module);
3213 module_exit(otx2_rvupf_cleanup_module);