1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2017 - 2019 Pensando Systems, Inc */
4 #include <linux/ethtool.h>
5 #include <linux/printk.h>
6 #include <linux/dynamic_debug.h>
7 #include <linux/netdevice.h>
8 #include <linux/etherdevice.h>
9 #include <linux/if_vlan.h>
10 #include <linux/rtnetlink.h>
11 #include <linux/interrupt.h>
12 #include <linux/pci.h>
13 #include <linux/cpumask.h>
14 #include <linux/crash_dump.h>
15 #include <linux/vmalloc.h>
16 #include <net/page_pool/helpers.h>
19 #include "ionic_bus.h"
20 #include "ionic_dev.h"
21 #include "ionic_lif.h"
22 #include "ionic_txrx.h"
23 #include "ionic_ethtool.h"
24 #include "ionic_debugfs.h"
26 /* queuetype support level */
27 static const u8 ionic_qtype_versions[IONIC_QTYPE_MAX] = {
28 [IONIC_QTYPE_ADMINQ] = 0, /* 0 = Base version with CQ support */
29 [IONIC_QTYPE_NOTIFYQ] = 0, /* 0 = Base version */
30 [IONIC_QTYPE_RXQ] = 2, /* 0 = Base version with CQ+SG support
31 * 2 = ... with CMB rings
33 [IONIC_QTYPE_TXQ] = 3, /* 0 = Base version with CQ+SG support
34 * 1 = ... with Tx SG version 1
35 * 3 = ... with CMB rings
39 static void ionic_link_status_check(struct ionic_lif *lif);
40 static void ionic_lif_handle_fw_down(struct ionic_lif *lif);
41 static void ionic_lif_handle_fw_up(struct ionic_lif *lif);
42 static void ionic_lif_set_netdev_info(struct ionic_lif *lif);
44 static void ionic_txrx_deinit(struct ionic_lif *lif);
45 static int ionic_txrx_init(struct ionic_lif *lif);
46 static int ionic_start_queues(struct ionic_lif *lif);
47 static void ionic_stop_queues(struct ionic_lif *lif);
48 static void ionic_lif_queue_identify(struct ionic_lif *lif);
50 static void ionic_xdp_rxqs_prog_update(struct ionic_lif *lif);
51 static void ionic_unregister_rxq_info(struct ionic_queue *q);
52 static int ionic_register_rxq_info(struct ionic_queue *q, unsigned int napi_id);
54 static void ionic_dim_work(struct work_struct *work)
56 struct dim *dim = container_of(work, struct dim, work);
57 struct dim_cq_moder cur_moder;
58 struct ionic_intr_info *intr;
59 struct ionic_qcq *qcq;
60 struct ionic_lif *lif;
61 struct ionic_queue *q;
64 qcq = container_of(dim, struct ionic_qcq, dim);
66 if (q->type == IONIC_QTYPE_RXQ)
67 cur_moder = net_dim_get_rx_moderation(dim->mode, dim->profile_ix);
69 cur_moder = net_dim_get_tx_moderation(dim->mode, dim->profile_ix);
71 new_coal = ionic_coal_usec_to_hw(lif->ionic, cur_moder.usec);
72 new_coal = new_coal ? new_coal : 1;
75 if (intr->dim_coal_hw != new_coal) {
76 intr->dim_coal_hw = new_coal;
78 ionic_intr_coal_init(lif->ionic->idev.intr_ctrl,
79 intr->index, intr->dim_coal_hw);
82 dim->state = DIM_START_MEASURE;
85 static void ionic_lif_deferred_work(struct work_struct *work)
87 struct ionic_lif *lif = container_of(work, struct ionic_lif, deferred.work);
88 struct ionic_deferred *def = &lif->deferred;
89 struct ionic_deferred_work *w = NULL;
92 spin_lock_bh(&def->lock);
93 if (!list_empty(&def->list)) {
94 w = list_first_entry(&def->list,
95 struct ionic_deferred_work, list);
98 spin_unlock_bh(&def->lock);
104 case IONIC_DW_TYPE_RX_MODE:
105 ionic_lif_rx_mode(lif);
107 case IONIC_DW_TYPE_LINK_STATUS:
108 ionic_link_status_check(lif);
110 case IONIC_DW_TYPE_LIF_RESET:
112 ionic_lif_handle_fw_up(lif);
114 ionic_lif_handle_fw_down(lif);
116 /* Fire off another watchdog to see
117 * if the FW is already back rather than
118 * waiting another whole cycle
120 mod_timer(&lif->ionic->watchdog_timer, jiffies + 1);
131 void ionic_lif_deferred_enqueue(struct ionic_lif *lif,
132 struct ionic_deferred_work *work)
134 spin_lock_bh(&lif->deferred.lock);
135 list_add_tail(&work->list, &lif->deferred.list);
136 spin_unlock_bh(&lif->deferred.lock);
137 queue_work(lif->ionic->wq, &lif->deferred.work);
140 static void ionic_link_status_check(struct ionic_lif *lif)
142 struct net_device *netdev = lif->netdev;
146 if (!test_bit(IONIC_LIF_F_LINK_CHECK_REQUESTED, lif->state))
149 /* Don't put carrier back up if we're in a broken state */
150 if (test_bit(IONIC_LIF_F_BROKEN, lif->state)) {
151 clear_bit(IONIC_LIF_F_LINK_CHECK_REQUESTED, lif->state);
155 link_status = le16_to_cpu(lif->info->status.link_status);
156 link_up = link_status == IONIC_PORT_OPER_STATUS_UP;
161 if (netdev->flags & IFF_UP && netif_running(netdev)) {
162 mutex_lock(&lif->queue_lock);
163 err = ionic_start_queues(lif);
164 if (err && err != -EBUSY) {
166 "Failed to start queues: %d\n", err);
167 set_bit(IONIC_LIF_F_BROKEN, lif->state);
168 netif_carrier_off(lif->netdev);
170 mutex_unlock(&lif->queue_lock);
173 if (!err && !netif_carrier_ok(netdev)) {
174 ionic_port_identify(lif->ionic);
175 netdev_info(netdev, "Link up - %d Gbps\n",
176 le32_to_cpu(lif->info->status.link_speed) / 1000);
177 netif_carrier_on(netdev);
180 if (netif_carrier_ok(netdev)) {
181 lif->link_down_count++;
182 netdev_info(netdev, "Link down\n");
183 netif_carrier_off(netdev);
186 if (netdev->flags & IFF_UP && netif_running(netdev)) {
187 mutex_lock(&lif->queue_lock);
188 ionic_stop_queues(lif);
189 mutex_unlock(&lif->queue_lock);
193 clear_bit(IONIC_LIF_F_LINK_CHECK_REQUESTED, lif->state);
196 void ionic_link_status_check_request(struct ionic_lif *lif, bool can_sleep)
198 struct ionic_deferred_work *work;
200 /* we only need one request outstanding at a time */
201 if (test_and_set_bit(IONIC_LIF_F_LINK_CHECK_REQUESTED, lif->state))
205 work = kzalloc(sizeof(*work), GFP_ATOMIC);
207 clear_bit(IONIC_LIF_F_LINK_CHECK_REQUESTED, lif->state);
211 work->type = IONIC_DW_TYPE_LINK_STATUS;
212 ionic_lif_deferred_enqueue(lif, work);
214 ionic_link_status_check(lif);
218 static irqreturn_t ionic_isr(int irq, void *data)
220 struct napi_struct *napi = data;
222 napi_schedule_irqoff(napi);
227 static int ionic_request_irq(struct ionic_lif *lif, struct ionic_qcq *qcq)
229 struct ionic_intr_info *intr = &qcq->intr;
230 struct device *dev = lif->ionic->dev;
231 struct ionic_queue *q = &qcq->q;
235 name = netdev_name(lif->netdev);
237 name = dev_name(dev);
239 snprintf(intr->name, sizeof(intr->name),
240 "%.5s-%.16s-%.8s", IONIC_DRV_NAME, name, q->name);
242 return devm_request_irq(dev, intr->vector, ionic_isr,
243 0, intr->name, &qcq->napi);
246 static int ionic_intr_alloc(struct ionic_lif *lif, struct ionic_intr_info *intr)
248 struct ionic *ionic = lif->ionic;
251 index = find_first_zero_bit(ionic->intrs, ionic->nintrs);
252 if (index == ionic->nintrs) {
253 netdev_warn(lif->netdev, "%s: no intr, index=%d nintrs=%d\n",
254 __func__, index, ionic->nintrs);
258 set_bit(index, ionic->intrs);
259 ionic_intr_init(&ionic->idev, intr, index);
264 static void ionic_intr_free(struct ionic *ionic, int index)
266 if (index != IONIC_INTR_INDEX_NOT_ASSIGNED && index < ionic->nintrs)
267 clear_bit(index, ionic->intrs);
270 static void ionic_irq_aff_notify(struct irq_affinity_notify *notify,
271 const cpumask_t *mask)
273 struct ionic_intr_info *intr = container_of(notify, struct ionic_intr_info, aff_notify);
275 cpumask_copy(*intr->affinity_mask, mask);
278 static void ionic_irq_aff_release(struct kref __always_unused *ref)
282 static int ionic_qcq_enable(struct ionic_qcq *qcq)
284 struct ionic_queue *q = &qcq->q;
285 struct ionic_lif *lif = q->lif;
286 struct ionic_dev *idev;
289 struct ionic_admin_ctx ctx = {
290 .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
292 .opcode = IONIC_CMD_Q_CONTROL,
293 .lif_index = cpu_to_le16(lif->index),
295 .index = cpu_to_le32(q->index),
296 .oper = IONIC_Q_ENABLE,
301 idev = &lif->ionic->idev;
302 dev = lif->ionic->dev;
304 dev_dbg(dev, "q_enable.index %d q_enable.qtype %d\n",
305 ctx.cmd.q_control.index, ctx.cmd.q_control.type);
307 if (qcq->flags & IONIC_QCQ_F_INTR)
308 ionic_intr_clean(idev->intr_ctrl, qcq->intr.index);
310 ret = ionic_adminq_post_wait(lif, &ctx);
314 if (qcq->flags & IONIC_QCQ_F_INTR) {
315 napi_enable(&qcq->napi);
316 irq_set_affinity_notifier(qcq->intr.vector,
317 &qcq->intr.aff_notify);
318 irq_set_affinity_hint(qcq->intr.vector,
319 *qcq->intr.affinity_mask);
320 ionic_intr_mask(idev->intr_ctrl, qcq->intr.index,
321 IONIC_INTR_MASK_CLEAR);
327 static int ionic_qcq_disable(struct ionic_lif *lif, struct ionic_qcq *qcq, int fw_err)
329 struct ionic_queue *q;
331 struct ionic_admin_ctx ctx = {
332 .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
334 .opcode = IONIC_CMD_Q_CONTROL,
335 .oper = IONIC_Q_DISABLE,
340 netdev_err(lif->netdev, "%s: bad qcq\n", __func__);
346 if (qcq->flags & IONIC_QCQ_F_INTR) {
347 struct ionic_dev *idev = &lif->ionic->idev;
349 if (lif->doorbell_wa)
350 cancel_work_sync(&qcq->doorbell_napi_work);
351 cancel_work_sync(&qcq->dim.work);
352 ionic_intr_mask(idev->intr_ctrl, qcq->intr.index,
353 IONIC_INTR_MASK_SET);
354 synchronize_irq(qcq->intr.vector);
355 irq_set_affinity_notifier(qcq->intr.vector, NULL);
356 irq_set_affinity_hint(qcq->intr.vector, NULL);
357 napi_disable(&qcq->napi);
360 /* If there was a previous fw communcation error, don't bother with
361 * sending the adminq command and just return the same error value.
363 if (fw_err == -ETIMEDOUT || fw_err == -ENXIO)
366 ctx.cmd.q_control.lif_index = cpu_to_le16(lif->index);
367 ctx.cmd.q_control.type = q->type;
368 ctx.cmd.q_control.index = cpu_to_le32(q->index);
369 dev_dbg(lif->ionic->dev, "q_disable.index %d q_disable.qtype %d\n",
370 ctx.cmd.q_control.index, ctx.cmd.q_control.type);
372 return ionic_adminq_post_wait(lif, &ctx);
375 static void ionic_lif_qcq_deinit(struct ionic_lif *lif, struct ionic_qcq *qcq)
377 struct ionic_dev *idev = &lif->ionic->idev;
382 if (!(qcq->flags & IONIC_QCQ_F_INITED))
385 ionic_unregister_rxq_info(&qcq->q);
386 if (qcq->flags & IONIC_QCQ_F_INTR) {
387 ionic_intr_mask(idev->intr_ctrl, qcq->intr.index,
388 IONIC_INTR_MASK_SET);
389 netif_napi_del(&qcq->napi);
392 qcq->flags &= ~IONIC_QCQ_F_INITED;
395 static void ionic_qcq_intr_free(struct ionic_lif *lif, struct ionic_qcq *qcq)
397 if (!(qcq->flags & IONIC_QCQ_F_INTR) || qcq->intr.vector == 0)
400 irq_set_affinity_hint(qcq->intr.vector, NULL);
401 devm_free_irq(lif->ionic->dev, qcq->intr.vector, &qcq->napi);
402 qcq->intr.vector = 0;
403 ionic_intr_free(lif->ionic, qcq->intr.index);
404 qcq->intr.index = IONIC_INTR_INDEX_NOT_ASSIGNED;
407 static void ionic_qcq_free(struct ionic_lif *lif, struct ionic_qcq *qcq)
409 struct device *dev = lif->ionic->dev;
414 ionic_debugfs_del_qcq(qcq);
417 dma_free_coherent(dev, qcq->q_size, qcq->q_base, qcq->q_base_pa);
422 if (qcq->cmb_q_base) {
423 iounmap(qcq->cmb_q_base);
424 ionic_put_cmb(lif, qcq->cmb_pgid, qcq->cmb_order);
427 qcq->cmb_q_base = NULL;
428 qcq->cmb_q_base_pa = 0;
432 dma_free_coherent(dev, qcq->cq_size, qcq->cq_base, qcq->cq_base_pa);
438 dma_free_coherent(dev, qcq->sg_size, qcq->sg_base, qcq->sg_base_pa);
443 page_pool_destroy(qcq->q.page_pool);
444 qcq->q.page_pool = NULL;
446 ionic_qcq_intr_free(lif, qcq);
451 void ionic_qcqs_free(struct ionic_lif *lif)
453 struct device *dev = lif->ionic->dev;
454 struct ionic_qcq *adminqcq;
455 unsigned long irqflags;
457 if (lif->notifyqcq) {
458 ionic_qcq_free(lif, lif->notifyqcq);
459 devm_kfree(dev, lif->notifyqcq);
460 lif->notifyqcq = NULL;
464 spin_lock_irqsave(&lif->adminq_lock, irqflags);
465 adminqcq = READ_ONCE(lif->adminqcq);
466 lif->adminqcq = NULL;
467 spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
469 ionic_qcq_free(lif, adminqcq);
470 devm_kfree(dev, adminqcq);
475 devm_kfree(dev, lif->rxqstats);
476 lif->rxqstats = NULL;
477 devm_kfree(dev, lif->rxqcqs);
482 devm_kfree(dev, lif->txqstats);
483 lif->txqstats = NULL;
484 devm_kfree(dev, lif->txqcqs);
489 static void ionic_link_qcq_interrupts(struct ionic_qcq *src_qcq,
490 struct ionic_qcq *n_qcq)
492 n_qcq->intr.vector = src_qcq->intr.vector;
493 n_qcq->intr.index = src_qcq->intr.index;
496 static int ionic_alloc_qcq_interrupt(struct ionic_lif *lif, struct ionic_qcq *qcq)
498 cpumask_var_t *affinity_mask;
501 if (!(qcq->flags & IONIC_QCQ_F_INTR)) {
502 qcq->intr.index = IONIC_INTR_INDEX_NOT_ASSIGNED;
506 err = ionic_intr_alloc(lif, &qcq->intr);
508 netdev_warn(lif->netdev, "no intr for %s: %d\n",
513 err = ionic_bus_get_irq(lif->ionic, qcq->intr.index);
515 netdev_warn(lif->netdev, "no vector for %s: %d\n",
517 goto err_out_free_intr;
519 qcq->intr.vector = err;
520 ionic_intr_mask_assert(lif->ionic->idev.intr_ctrl, qcq->intr.index,
521 IONIC_INTR_MASK_SET);
523 err = ionic_request_irq(lif, qcq);
525 netdev_warn(lif->netdev, "irq request failed %d\n", err);
526 goto err_out_free_intr;
529 /* try to get the irq on the local numa node first */
530 affinity_mask = &lif->ionic->affinity_masks[qcq->intr.index];
531 if (cpumask_empty(*affinity_mask)) {
534 cpu = cpumask_local_spread(qcq->intr.index,
535 dev_to_node(lif->ionic->dev));
537 cpumask_set_cpu(cpu, *affinity_mask);
540 qcq->intr.affinity_mask = affinity_mask;
541 qcq->intr.aff_notify.notify = ionic_irq_aff_notify;
542 qcq->intr.aff_notify.release = ionic_irq_aff_release;
544 netdev_dbg(lif->netdev, "%s: Interrupt index %d\n", qcq->q.name, qcq->intr.index);
548 ionic_intr_free(lif->ionic, qcq->intr.index);
553 static int ionic_qcq_alloc(struct ionic_lif *lif, unsigned int type,
555 const char *name, unsigned int flags,
556 unsigned int num_descs, unsigned int desc_size,
557 unsigned int cq_desc_size,
558 unsigned int sg_desc_size,
559 unsigned int desc_info_size,
560 unsigned int pid, struct bpf_prog *xdp_prog,
561 struct ionic_qcq **qcq)
563 struct ionic_dev *idev = &lif->ionic->idev;
564 struct device *dev = lif->ionic->dev;
565 struct ionic_qcq *new;
570 new = devm_kzalloc(dev, sizeof(*new), GFP_KERNEL);
572 netdev_err(lif->netdev, "Cannot allocate queue structure\n");
580 new->q.info = vcalloc(num_descs, desc_info_size);
582 netdev_err(lif->netdev, "Cannot allocate queue info\n");
584 goto err_out_free_qcq;
587 if (type == IONIC_QTYPE_RXQ) {
588 struct page_pool_params pp_params = {
589 .flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV,
591 .pool_size = num_descs,
593 .dev = lif->ionic->dev,
595 .dma_dir = DMA_FROM_DEVICE,
596 .max_len = PAGE_SIZE,
597 .netdev = lif->netdev,
601 pp_params.dma_dir = DMA_BIDIRECTIONAL;
603 new->q.page_pool = page_pool_create(&pp_params);
604 if (IS_ERR(new->q.page_pool)) {
605 netdev_err(lif->netdev, "Cannot create page_pool\n");
606 err = PTR_ERR(new->q.page_pool);
607 new->q.page_pool = NULL;
608 goto err_out_free_q_info;
613 new->q.max_sg_elems = lif->qtype_info[type].max_sg_elems;
615 err = ionic_q_init(lif, idev, &new->q, index, name, num_descs,
616 desc_size, sg_desc_size, pid);
618 netdev_err(lif->netdev, "Cannot initialize queue\n");
619 goto err_out_free_page_pool;
622 err = ionic_alloc_qcq_interrupt(lif, new);
624 goto err_out_free_page_pool;
626 err = ionic_cq_init(lif, &new->cq, &new->intr, num_descs, cq_desc_size);
628 netdev_err(lif->netdev, "Cannot initialize completion queue\n");
629 goto err_out_free_irq;
632 if (flags & IONIC_QCQ_F_NOTIFYQ) {
635 /* q & cq need to be contiguous in NotifyQ, so alloc it all in q
636 * and don't alloc qc. We leave new->qc_size and new->qc_base
637 * as 0 to be sure we don't try to free it later.
639 q_size = ALIGN(num_descs * desc_size, PAGE_SIZE);
640 new->q_size = PAGE_SIZE + q_size +
641 ALIGN(num_descs * cq_desc_size, PAGE_SIZE);
642 new->q_base = dma_alloc_coherent(dev, new->q_size,
643 &new->q_base_pa, GFP_KERNEL);
645 netdev_err(lif->netdev, "Cannot allocate qcq DMA memory\n");
647 goto err_out_free_irq;
649 new->q.base = PTR_ALIGN(new->q_base, PAGE_SIZE);
650 new->q.base_pa = ALIGN(new->q_base_pa, PAGE_SIZE);
652 /* Base the NotifyQ cq.base off of the ALIGNed q.base */
653 new->cq.base = PTR_ALIGN(new->q.base + q_size, PAGE_SIZE);
654 new->cq.base_pa = ALIGN(new->q_base_pa + q_size, PAGE_SIZE);
655 new->cq.bound_q = &new->q;
657 /* regular DMA q descriptors */
658 new->q_size = PAGE_SIZE + (num_descs * desc_size);
659 new->q_base = dma_alloc_coherent(dev, new->q_size, &new->q_base_pa,
662 netdev_err(lif->netdev, "Cannot allocate queue DMA memory\n");
664 goto err_out_free_irq;
666 new->q.base = PTR_ALIGN(new->q_base, PAGE_SIZE);
667 new->q.base_pa = ALIGN(new->q_base_pa, PAGE_SIZE);
669 if (flags & IONIC_QCQ_F_CMB_RINGS) {
670 /* on-chip CMB q descriptors */
671 new->cmb_q_size = num_descs * desc_size;
672 new->cmb_order = order_base_2(new->cmb_q_size / PAGE_SIZE);
674 err = ionic_get_cmb(lif, &new->cmb_pgid, &new->cmb_q_base_pa,
677 netdev_err(lif->netdev,
678 "Cannot allocate queue order %d from cmb: err %d\n",
679 new->cmb_order, err);
683 new->cmb_q_base = ioremap_wc(new->cmb_q_base_pa, new->cmb_q_size);
684 if (!new->cmb_q_base) {
685 netdev_err(lif->netdev, "Cannot map queue from cmb\n");
686 ionic_put_cmb(lif, new->cmb_pgid, new->cmb_order);
691 new->cmb_q_base_pa -= idev->phy_cmb_pages;
692 new->q.cmb_base = new->cmb_q_base;
693 new->q.cmb_base_pa = new->cmb_q_base_pa;
696 /* cq DMA descriptors */
697 new->cq_size = PAGE_SIZE + (num_descs * cq_desc_size);
698 new->cq_base = dma_alloc_coherent(dev, new->cq_size, &new->cq_base_pa,
701 netdev_err(lif->netdev, "Cannot allocate cq DMA memory\n");
705 new->cq.base = PTR_ALIGN(new->cq_base, PAGE_SIZE);
706 new->cq.base_pa = ALIGN(new->cq_base_pa, PAGE_SIZE);
707 new->cq.bound_q = &new->q;
710 if (flags & IONIC_QCQ_F_SG) {
711 new->sg_size = PAGE_SIZE + (num_descs * sg_desc_size);
712 new->sg_base = dma_alloc_coherent(dev, new->sg_size, &new->sg_base_pa,
715 netdev_err(lif->netdev, "Cannot allocate sg DMA memory\n");
717 goto err_out_free_cq;
719 new->q.sg_base = PTR_ALIGN(new->sg_base, PAGE_SIZE);
720 new->q.sg_base_pa = ALIGN(new->sg_base_pa, PAGE_SIZE);
723 INIT_WORK(&new->dim.work, ionic_dim_work);
724 new->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_CQE;
725 if (lif->doorbell_wa)
726 INIT_WORK(&new->doorbell_napi_work, ionic_doorbell_napi_work);
733 dma_free_coherent(dev, new->cq_size, new->cq_base, new->cq_base_pa);
735 if (new->cmb_q_base) {
736 iounmap(new->cmb_q_base);
737 ionic_put_cmb(lif, new->cmb_pgid, new->cmb_order);
739 dma_free_coherent(dev, new->q_size, new->q_base, new->q_base_pa);
741 if (flags & IONIC_QCQ_F_INTR) {
742 devm_free_irq(dev, new->intr.vector, &new->napi);
743 ionic_intr_free(lif->ionic, new->intr.index);
745 err_out_free_page_pool:
746 page_pool_destroy(new->q.page_pool);
750 devm_kfree(dev, new);
752 dev_err(dev, "qcq alloc of %s%d failed %d\n", name, index, err);
756 static int ionic_qcqs_alloc(struct ionic_lif *lif)
758 struct device *dev = lif->ionic->dev;
762 flags = IONIC_QCQ_F_INTR;
763 err = ionic_qcq_alloc(lif, IONIC_QTYPE_ADMINQ, 0, "admin", flags,
765 sizeof(struct ionic_admin_cmd),
766 sizeof(struct ionic_admin_comp),
768 sizeof(struct ionic_admin_desc_info),
769 lif->kern_pid, NULL, &lif->adminqcq);
772 ionic_debugfs_add_qcq(lif, lif->adminqcq);
774 if (lif->ionic->nnqs_per_lif) {
775 flags = IONIC_QCQ_F_NOTIFYQ;
776 err = ionic_qcq_alloc(lif, IONIC_QTYPE_NOTIFYQ, 0, "notifyq",
777 flags, IONIC_NOTIFYQ_LENGTH,
778 sizeof(struct ionic_notifyq_cmd),
779 sizeof(union ionic_notifyq_comp),
781 sizeof(struct ionic_admin_desc_info),
782 lif->kern_pid, NULL, &lif->notifyqcq);
785 ionic_debugfs_add_qcq(lif, lif->notifyqcq);
787 /* Let the notifyq ride on the adminq interrupt */
788 ionic_link_qcq_interrupts(lif->adminqcq, lif->notifyqcq);
792 lif->txqcqs = devm_kcalloc(dev, lif->ionic->ntxqs_per_lif,
793 sizeof(*lif->txqcqs), GFP_KERNEL);
796 lif->rxqcqs = devm_kcalloc(dev, lif->ionic->nrxqs_per_lif,
797 sizeof(*lif->rxqcqs), GFP_KERNEL);
801 lif->txqstats = devm_kcalloc(dev, lif->ionic->ntxqs_per_lif + 1,
802 sizeof(*lif->txqstats), GFP_KERNEL);
805 lif->rxqstats = devm_kcalloc(dev, lif->ionic->nrxqs_per_lif + 1,
806 sizeof(*lif->rxqstats), GFP_KERNEL);
813 ionic_qcqs_free(lif);
817 static void ionic_qcq_sanitize(struct ionic_qcq *qcq)
821 qcq->cq.tail_idx = 0;
822 qcq->cq.done_color = 1;
823 memset(qcq->q_base, 0, qcq->q_size);
825 memset_io(qcq->cmb_q_base, 0, qcq->cmb_q_size);
826 memset(qcq->cq_base, 0, qcq->cq_size);
827 memset(qcq->sg_base, 0, qcq->sg_size);
830 static int ionic_lif_txq_init(struct ionic_lif *lif, struct ionic_qcq *qcq)
832 struct device *dev = lif->ionic->dev;
833 struct ionic_queue *q = &qcq->q;
834 struct ionic_cq *cq = &qcq->cq;
835 struct ionic_admin_ctx ctx = {
836 .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
838 .opcode = IONIC_CMD_Q_INIT,
839 .lif_index = cpu_to_le16(lif->index),
841 .ver = lif->qtype_info[q->type].version,
842 .index = cpu_to_le32(q->index),
843 .flags = cpu_to_le16(IONIC_QINIT_F_IRQ |
845 .intr_index = cpu_to_le16(qcq->intr.index),
846 .pid = cpu_to_le16(q->pid),
847 .ring_size = ilog2(q->num_descs),
848 .ring_base = cpu_to_le64(q->base_pa),
849 .cq_ring_base = cpu_to_le64(cq->base_pa),
850 .sg_ring_base = cpu_to_le64(q->sg_base_pa),
851 .features = cpu_to_le64(q->features),
856 if (qcq->flags & IONIC_QCQ_F_CMB_RINGS) {
857 ctx.cmd.q_init.flags |= cpu_to_le16(IONIC_QINIT_F_CMB);
858 ctx.cmd.q_init.ring_base = cpu_to_le64(qcq->cmb_q_base_pa);
861 dev_dbg(dev, "txq_init.pid %d\n", ctx.cmd.q_init.pid);
862 dev_dbg(dev, "txq_init.index %d\n", ctx.cmd.q_init.index);
863 dev_dbg(dev, "txq_init.ring_base 0x%llx\n", ctx.cmd.q_init.ring_base);
864 dev_dbg(dev, "txq_init.ring_size %d\n", ctx.cmd.q_init.ring_size);
865 dev_dbg(dev, "txq_init.cq_ring_base 0x%llx\n", ctx.cmd.q_init.cq_ring_base);
866 dev_dbg(dev, "txq_init.sg_ring_base 0x%llx\n", ctx.cmd.q_init.sg_ring_base);
867 dev_dbg(dev, "txq_init.flags 0x%x\n", ctx.cmd.q_init.flags);
868 dev_dbg(dev, "txq_init.ver %d\n", ctx.cmd.q_init.ver);
869 dev_dbg(dev, "txq_init.intr_index %d\n", ctx.cmd.q_init.intr_index);
871 ionic_qcq_sanitize(qcq);
873 err = ionic_adminq_post_wait(lif, &ctx);
877 q->hw_type = ctx.comp.q_init.hw_type;
878 q->hw_index = le32_to_cpu(ctx.comp.q_init.hw_index);
879 q->dbval = IONIC_DBELL_QID(q->hw_index);
881 dev_dbg(dev, "txq->hw_type %d\n", q->hw_type);
882 dev_dbg(dev, "txq->hw_index %d\n", q->hw_index);
884 q->dbell_deadline = IONIC_TX_DOORBELL_DEADLINE;
885 q->dbell_jiffies = jiffies;
887 if (test_bit(IONIC_LIF_F_SPLIT_INTR, lif->state))
888 netif_napi_add(lif->netdev, &qcq->napi, ionic_tx_napi);
890 qcq->flags |= IONIC_QCQ_F_INITED;
895 static int ionic_lif_rxq_init(struct ionic_lif *lif, struct ionic_qcq *qcq)
897 struct device *dev = lif->ionic->dev;
898 struct ionic_queue *q = &qcq->q;
899 struct ionic_cq *cq = &qcq->cq;
900 struct ionic_admin_ctx ctx = {
901 .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
903 .opcode = IONIC_CMD_Q_INIT,
904 .lif_index = cpu_to_le16(lif->index),
906 .ver = lif->qtype_info[q->type].version,
907 .index = cpu_to_le32(q->index),
908 .flags = cpu_to_le16(IONIC_QINIT_F_IRQ),
909 .intr_index = cpu_to_le16(cq->bound_intr->index),
910 .pid = cpu_to_le16(q->pid),
911 .ring_size = ilog2(q->num_descs),
912 .ring_base = cpu_to_le64(q->base_pa),
913 .cq_ring_base = cpu_to_le64(cq->base_pa),
914 .sg_ring_base = cpu_to_le64(q->sg_base_pa),
915 .features = cpu_to_le64(q->features),
920 q->partner = &lif->txqcqs[q->index]->q;
921 q->partner->partner = q;
923 if (!lif->xdp_prog ||
924 (lif->xdp_prog->aux && lif->xdp_prog->aux->xdp_has_frags))
925 ctx.cmd.q_init.flags |= cpu_to_le16(IONIC_QINIT_F_SG);
927 if (qcq->flags & IONIC_QCQ_F_CMB_RINGS) {
928 ctx.cmd.q_init.flags |= cpu_to_le16(IONIC_QINIT_F_CMB);
929 ctx.cmd.q_init.ring_base = cpu_to_le64(qcq->cmb_q_base_pa);
932 dev_dbg(dev, "rxq_init.pid %d\n", ctx.cmd.q_init.pid);
933 dev_dbg(dev, "rxq_init.index %d\n", ctx.cmd.q_init.index);
934 dev_dbg(dev, "rxq_init.ring_base 0x%llx\n", ctx.cmd.q_init.ring_base);
935 dev_dbg(dev, "rxq_init.ring_size %d\n", ctx.cmd.q_init.ring_size);
936 dev_dbg(dev, "rxq_init.flags 0x%x\n", ctx.cmd.q_init.flags);
937 dev_dbg(dev, "rxq_init.ver %d\n", ctx.cmd.q_init.ver);
938 dev_dbg(dev, "rxq_init.intr_index %d\n", ctx.cmd.q_init.intr_index);
940 ionic_qcq_sanitize(qcq);
942 err = ionic_adminq_post_wait(lif, &ctx);
946 q->hw_type = ctx.comp.q_init.hw_type;
947 q->hw_index = le32_to_cpu(ctx.comp.q_init.hw_index);
948 q->dbval = IONIC_DBELL_QID(q->hw_index);
950 dev_dbg(dev, "rxq->hw_type %d\n", q->hw_type);
951 dev_dbg(dev, "rxq->hw_index %d\n", q->hw_index);
953 q->dbell_deadline = IONIC_RX_MIN_DOORBELL_DEADLINE;
954 q->dbell_jiffies = jiffies;
956 if (test_bit(IONIC_LIF_F_SPLIT_INTR, lif->state))
957 netif_napi_add(lif->netdev, &qcq->napi, ionic_rx_napi);
959 netif_napi_add(lif->netdev, &qcq->napi, ionic_txrx_napi);
960 err = ionic_register_rxq_info(q, qcq->napi.napi_id);
962 netif_napi_del(&qcq->napi);
966 qcq->flags |= IONIC_QCQ_F_INITED;
971 int ionic_lif_create_hwstamp_txq(struct ionic_lif *lif)
973 unsigned int num_desc, desc_sz, comp_sz, sg_desc_sz;
974 unsigned int txq_i, flags;
975 struct ionic_qcq *txq;
979 if (lif->hwstamp_txq)
982 features = IONIC_Q_F_2X_CQ_DESC | IONIC_TXQ_F_HWSTAMP;
984 num_desc = IONIC_MIN_TXRX_DESC;
985 desc_sz = sizeof(struct ionic_txq_desc);
986 comp_sz = 2 * sizeof(struct ionic_txq_comp);
988 if (lif->qtype_info[IONIC_QTYPE_TXQ].version >= 1 &&
989 lif->qtype_info[IONIC_QTYPE_TXQ].sg_desc_sz == sizeof(struct ionic_txq_sg_desc_v1))
990 sg_desc_sz = sizeof(struct ionic_txq_sg_desc_v1);
992 sg_desc_sz = sizeof(struct ionic_txq_sg_desc);
994 txq_i = lif->ionic->ntxqs_per_lif;
995 flags = IONIC_QCQ_F_TX_STATS | IONIC_QCQ_F_SG;
997 err = ionic_qcq_alloc(lif, IONIC_QTYPE_TXQ, txq_i, "hwstamp_tx", flags,
998 num_desc, desc_sz, comp_sz, sg_desc_sz,
999 sizeof(struct ionic_tx_desc_info),
1000 lif->kern_pid, NULL, &txq);
1004 txq->q.features = features;
1006 ionic_link_qcq_interrupts(lif->adminqcq, txq);
1007 ionic_debugfs_add_qcq(lif, txq);
1009 lif->hwstamp_txq = txq;
1011 if (netif_running(lif->netdev)) {
1012 err = ionic_lif_txq_init(lif, txq);
1016 if (test_bit(IONIC_LIF_F_UP, lif->state)) {
1017 err = ionic_qcq_enable(txq);
1019 goto err_qcq_enable;
1026 ionic_lif_qcq_deinit(lif, txq);
1028 lif->hwstamp_txq = NULL;
1029 ionic_debugfs_del_qcq(txq);
1030 ionic_qcq_free(lif, txq);
1031 devm_kfree(lif->ionic->dev, txq);
1036 int ionic_lif_create_hwstamp_rxq(struct ionic_lif *lif)
1038 unsigned int num_desc, desc_sz, comp_sz, sg_desc_sz;
1039 unsigned int rxq_i, flags;
1040 struct ionic_qcq *rxq;
1044 if (lif->hwstamp_rxq)
1047 features = IONIC_Q_F_2X_CQ_DESC | IONIC_RXQ_F_HWSTAMP;
1049 num_desc = IONIC_MIN_TXRX_DESC;
1050 desc_sz = sizeof(struct ionic_rxq_desc);
1051 comp_sz = 2 * sizeof(struct ionic_rxq_comp);
1052 sg_desc_sz = sizeof(struct ionic_rxq_sg_desc);
1054 rxq_i = lif->ionic->nrxqs_per_lif;
1055 flags = IONIC_QCQ_F_RX_STATS | IONIC_QCQ_F_SG;
1057 err = ionic_qcq_alloc(lif, IONIC_QTYPE_RXQ, rxq_i, "hwstamp_rx", flags,
1058 num_desc, desc_sz, comp_sz, sg_desc_sz,
1059 sizeof(struct ionic_rx_desc_info),
1060 lif->kern_pid, NULL, &rxq);
1064 rxq->q.features = features;
1066 ionic_link_qcq_interrupts(lif->adminqcq, rxq);
1067 ionic_debugfs_add_qcq(lif, rxq);
1069 lif->hwstamp_rxq = rxq;
1071 if (netif_running(lif->netdev)) {
1072 err = ionic_lif_rxq_init(lif, rxq);
1076 if (test_bit(IONIC_LIF_F_UP, lif->state)) {
1077 ionic_rx_fill(&rxq->q, NULL);
1078 err = ionic_qcq_enable(rxq);
1080 goto err_qcq_enable;
1087 ionic_lif_qcq_deinit(lif, rxq);
1089 lif->hwstamp_rxq = NULL;
1090 ionic_debugfs_del_qcq(rxq);
1091 ionic_qcq_free(lif, rxq);
1092 devm_kfree(lif->ionic->dev, rxq);
1097 int ionic_lif_config_hwstamp_rxq_all(struct ionic_lif *lif, bool rx_all)
1099 struct ionic_queue_params qparam;
1101 ionic_init_queue_params(lif, &qparam);
1104 qparam.rxq_features = IONIC_Q_F_2X_CQ_DESC | IONIC_RXQ_F_HWSTAMP;
1106 qparam.rxq_features = 0;
1108 /* if we're not running, just set the values and return */
1109 if (!netif_running(lif->netdev)) {
1110 lif->rxq_features = qparam.rxq_features;
1114 return ionic_reconfigure_queues(lif, &qparam);
1117 int ionic_lif_set_hwstamp_txmode(struct ionic_lif *lif, u16 txstamp_mode)
1119 struct ionic_admin_ctx ctx = {
1120 .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
1121 .cmd.lif_setattr = {
1122 .opcode = IONIC_CMD_LIF_SETATTR,
1123 .index = cpu_to_le16(lif->index),
1124 .attr = IONIC_LIF_ATTR_TXSTAMP,
1125 .txstamp_mode = cpu_to_le16(txstamp_mode),
1129 return ionic_adminq_post_wait(lif, &ctx);
1132 static void ionic_lif_del_hwstamp_rxfilt(struct ionic_lif *lif)
1134 struct ionic_admin_ctx ctx = {
1135 .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
1136 .cmd.rx_filter_del = {
1137 .opcode = IONIC_CMD_RX_FILTER_DEL,
1138 .lif_index = cpu_to_le16(lif->index),
1141 struct ionic_rx_filter *f;
1145 spin_lock_bh(&lif->rx_filters.lock);
1147 f = ionic_rx_filter_rxsteer(lif);
1149 spin_unlock_bh(&lif->rx_filters.lock);
1153 filter_id = f->filter_id;
1154 ionic_rx_filter_free(lif, f);
1156 spin_unlock_bh(&lif->rx_filters.lock);
1158 netdev_dbg(lif->netdev, "rx_filter del RXSTEER (id %d)\n", filter_id);
1160 ctx.cmd.rx_filter_del.filter_id = cpu_to_le32(filter_id);
1162 err = ionic_adminq_post_wait(lif, &ctx);
1163 if (err && err != -EEXIST)
1164 netdev_dbg(lif->netdev, "failed to delete rx_filter RXSTEER (id %d)\n", filter_id);
1167 static int ionic_lif_add_hwstamp_rxfilt(struct ionic_lif *lif, u64 pkt_class)
1169 struct ionic_admin_ctx ctx = {
1170 .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
1171 .cmd.rx_filter_add = {
1172 .opcode = IONIC_CMD_RX_FILTER_ADD,
1173 .lif_index = cpu_to_le16(lif->index),
1174 .match = cpu_to_le16(IONIC_RX_FILTER_STEER_PKTCLASS),
1175 .pkt_class = cpu_to_le64(pkt_class),
1182 if (!lif->hwstamp_rxq)
1185 qtype = lif->hwstamp_rxq->q.type;
1186 ctx.cmd.rx_filter_add.qtype = qtype;
1188 qid = lif->hwstamp_rxq->q.index;
1189 ctx.cmd.rx_filter_add.qid = cpu_to_le32(qid);
1191 netdev_dbg(lif->netdev, "rx_filter add RXSTEER\n");
1192 err = ionic_adminq_post_wait(lif, &ctx);
1193 if (err && err != -EEXIST)
1196 spin_lock_bh(&lif->rx_filters.lock);
1197 err = ionic_rx_filter_save(lif, 0, qid, 0, &ctx, IONIC_FILTER_STATE_SYNCED);
1198 spin_unlock_bh(&lif->rx_filters.lock);
1203 int ionic_lif_set_hwstamp_rxfilt(struct ionic_lif *lif, u64 pkt_class)
1205 ionic_lif_del_hwstamp_rxfilt(lif);
1210 return ionic_lif_add_hwstamp_rxfilt(lif, pkt_class);
1213 static int ionic_adminq_napi(struct napi_struct *napi, int budget)
1215 struct ionic_intr_info *intr = napi_to_cq(napi)->bound_intr;
1216 struct ionic_lif *lif = napi_to_cq(napi)->lif;
1217 struct ionic_dev *idev = &lif->ionic->idev;
1218 unsigned long irqflags;
1219 unsigned int flags = 0;
1227 if (lif->notifyqcq && lif->notifyqcq->flags & IONIC_QCQ_F_INITED)
1228 n_work = ionic_cq_service(&lif->notifyqcq->cq, budget,
1229 ionic_notifyq_service, NULL, NULL);
1231 spin_lock_irqsave(&lif->adminq_lock, irqflags);
1232 if (lif->adminqcq && lif->adminqcq->flags & IONIC_QCQ_F_INITED)
1233 a_work = ionic_cq_service(&lif->adminqcq->cq, budget,
1234 ionic_adminq_service, NULL, NULL);
1236 spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
1238 if (lif->hwstamp_rxq)
1239 rx_work = ionic_cq_service(&lif->hwstamp_rxq->cq, budget,
1240 ionic_rx_service, NULL, NULL);
1242 if (lif->hwstamp_txq)
1243 tx_work = ionic_tx_cq_service(&lif->hwstamp_txq->cq, budget, !!budget);
1245 work_done = max(max(n_work, a_work), max(rx_work, tx_work));
1246 if (work_done < budget && napi_complete_done(napi, work_done)) {
1247 flags |= IONIC_INTR_CRED_UNMASK;
1248 intr->rearm_count++;
1251 if (work_done || flags) {
1252 flags |= IONIC_INTR_CRED_RESET_COALESCE;
1253 credits = n_work + a_work + rx_work + tx_work;
1254 ionic_intr_credits(idev->intr_ctrl, intr->index, credits, flags);
1257 if (lif->doorbell_wa) {
1259 ionic_adminq_poke_doorbell(&lif->adminqcq->q);
1260 if (lif->hwstamp_rxq && !rx_work)
1261 ionic_rxq_poke_doorbell(&lif->hwstamp_rxq->q);
1262 if (lif->hwstamp_txq && !tx_work)
1263 ionic_txq_poke_doorbell(&lif->hwstamp_txq->q);
1269 void ionic_get_stats64(struct net_device *netdev,
1270 struct rtnl_link_stats64 *ns)
1272 struct ionic_lif *lif = netdev_priv(netdev);
1273 struct ionic_lif_stats *ls;
1275 memset(ns, 0, sizeof(*ns));
1276 ls = &lif->info->stats;
1278 ns->rx_packets = le64_to_cpu(ls->rx_ucast_packets) +
1279 le64_to_cpu(ls->rx_mcast_packets) +
1280 le64_to_cpu(ls->rx_bcast_packets);
1282 ns->tx_packets = le64_to_cpu(ls->tx_ucast_packets) +
1283 le64_to_cpu(ls->tx_mcast_packets) +
1284 le64_to_cpu(ls->tx_bcast_packets);
1286 ns->rx_bytes = le64_to_cpu(ls->rx_ucast_bytes) +
1287 le64_to_cpu(ls->rx_mcast_bytes) +
1288 le64_to_cpu(ls->rx_bcast_bytes);
1290 ns->tx_bytes = le64_to_cpu(ls->tx_ucast_bytes) +
1291 le64_to_cpu(ls->tx_mcast_bytes) +
1292 le64_to_cpu(ls->tx_bcast_bytes);
1294 ns->rx_dropped = le64_to_cpu(ls->rx_ucast_drop_packets) +
1295 le64_to_cpu(ls->rx_mcast_drop_packets) +
1296 le64_to_cpu(ls->rx_bcast_drop_packets);
1298 ns->tx_dropped = le64_to_cpu(ls->tx_ucast_drop_packets) +
1299 le64_to_cpu(ls->tx_mcast_drop_packets) +
1300 le64_to_cpu(ls->tx_bcast_drop_packets);
1302 ns->multicast = le64_to_cpu(ls->rx_mcast_packets);
1304 ns->rx_over_errors = le64_to_cpu(ls->rx_queue_empty);
1306 ns->rx_missed_errors = le64_to_cpu(ls->rx_dma_error) +
1307 le64_to_cpu(ls->rx_queue_disabled) +
1308 le64_to_cpu(ls->rx_desc_fetch_error) +
1309 le64_to_cpu(ls->rx_desc_data_error);
1311 ns->tx_aborted_errors = le64_to_cpu(ls->tx_dma_error) +
1312 le64_to_cpu(ls->tx_queue_disabled) +
1313 le64_to_cpu(ls->tx_desc_fetch_error) +
1314 le64_to_cpu(ls->tx_desc_data_error);
1316 ns->rx_errors = ns->rx_over_errors +
1317 ns->rx_missed_errors;
1319 ns->tx_errors = ns->tx_aborted_errors;
1322 static int ionic_addr_add(struct net_device *netdev, const u8 *addr)
1324 return ionic_lif_list_addr(netdev_priv(netdev), addr, ADD_ADDR);
1327 static int ionic_addr_del(struct net_device *netdev, const u8 *addr)
1329 /* Don't delete our own address from the uc list */
1330 if (ether_addr_equal(addr, netdev->dev_addr))
1333 return ionic_lif_list_addr(netdev_priv(netdev), addr, DEL_ADDR);
1336 void ionic_lif_rx_mode(struct ionic_lif *lif)
1338 struct net_device *netdev = lif->netdev;
1339 unsigned int nfilters;
1340 unsigned int nd_flags;
1344 #define REMAIN(__x) (sizeof(buf) - (__x))
1346 mutex_lock(&lif->config_lock);
1348 /* grab the flags once for local use */
1349 nd_flags = netdev->flags;
1351 rx_mode = IONIC_RX_MODE_F_UNICAST;
1352 rx_mode |= (nd_flags & IFF_MULTICAST) ? IONIC_RX_MODE_F_MULTICAST : 0;
1353 rx_mode |= (nd_flags & IFF_BROADCAST) ? IONIC_RX_MODE_F_BROADCAST : 0;
1354 rx_mode |= (nd_flags & IFF_PROMISC) ? IONIC_RX_MODE_F_PROMISC : 0;
1355 rx_mode |= (nd_flags & IFF_ALLMULTI) ? IONIC_RX_MODE_F_ALLMULTI : 0;
1357 /* sync the filters */
1358 ionic_rx_filter_sync(lif);
1360 /* check for overflow state
1361 * if so, we track that we overflowed and enable NIC PROMISC
1362 * else if the overflow is set and not needed
1363 * we remove our overflow flag and check the netdev flags
1364 * to see if we can disable NIC PROMISC
1366 nfilters = le32_to_cpu(lif->identity->eth.max_ucast_filters);
1368 if (((lif->nucast + lif->nmcast) >= nfilters) ||
1369 (lif->max_vlans && lif->nvlans >= lif->max_vlans)) {
1370 rx_mode |= IONIC_RX_MODE_F_PROMISC;
1371 rx_mode |= IONIC_RX_MODE_F_ALLMULTI;
1373 if (!(nd_flags & IFF_PROMISC))
1374 rx_mode &= ~IONIC_RX_MODE_F_PROMISC;
1375 if (!(nd_flags & IFF_ALLMULTI))
1376 rx_mode &= ~IONIC_RX_MODE_F_ALLMULTI;
1379 i = scnprintf(buf, sizeof(buf), "rx_mode 0x%04x -> 0x%04x:",
1380 lif->rx_mode, rx_mode);
1381 if (rx_mode & IONIC_RX_MODE_F_UNICAST)
1382 i += scnprintf(&buf[i], REMAIN(i), " RX_MODE_F_UNICAST");
1383 if (rx_mode & IONIC_RX_MODE_F_MULTICAST)
1384 i += scnprintf(&buf[i], REMAIN(i), " RX_MODE_F_MULTICAST");
1385 if (rx_mode & IONIC_RX_MODE_F_BROADCAST)
1386 i += scnprintf(&buf[i], REMAIN(i), " RX_MODE_F_BROADCAST");
1387 if (rx_mode & IONIC_RX_MODE_F_PROMISC)
1388 i += scnprintf(&buf[i], REMAIN(i), " RX_MODE_F_PROMISC");
1389 if (rx_mode & IONIC_RX_MODE_F_ALLMULTI)
1390 i += scnprintf(&buf[i], REMAIN(i), " RX_MODE_F_ALLMULTI");
1391 if (rx_mode & IONIC_RX_MODE_F_RDMA_SNIFFER)
1392 i += scnprintf(&buf[i], REMAIN(i), " RX_MODE_F_RDMA_SNIFFER");
1393 netdev_dbg(netdev, "lif%d %s\n", lif->index, buf);
1395 if (lif->rx_mode != rx_mode) {
1396 struct ionic_admin_ctx ctx = {
1397 .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
1398 .cmd.rx_mode_set = {
1399 .opcode = IONIC_CMD_RX_MODE_SET,
1400 .lif_index = cpu_to_le16(lif->index),
1405 ctx.cmd.rx_mode_set.rx_mode = cpu_to_le16(rx_mode);
1406 err = ionic_adminq_post_wait(lif, &ctx);
1408 netdev_warn(netdev, "set rx_mode 0x%04x failed: %d\n",
1411 lif->rx_mode = rx_mode;
1414 mutex_unlock(&lif->config_lock);
1417 static void ionic_ndo_set_rx_mode(struct net_device *netdev)
1419 struct ionic_lif *lif = netdev_priv(netdev);
1420 struct ionic_deferred_work *work;
1422 /* Sync the kernel filter list with the driver filter list */
1423 __dev_uc_sync(netdev, ionic_addr_add, ionic_addr_del);
1424 __dev_mc_sync(netdev, ionic_addr_add, ionic_addr_del);
1426 /* Shove off the rest of the rxmode work to the work task
1427 * which will include syncing the filters to the firmware.
1429 work = kzalloc(sizeof(*work), GFP_ATOMIC);
1431 netdev_err(lif->netdev, "rxmode change dropped\n");
1434 work->type = IONIC_DW_TYPE_RX_MODE;
1435 netdev_dbg(lif->netdev, "deferred: rx_mode\n");
1436 ionic_lif_deferred_enqueue(lif, work);
1439 static __le64 ionic_netdev_features_to_nic(netdev_features_t features)
1443 if (features & NETIF_F_HW_VLAN_CTAG_TX)
1444 wanted |= IONIC_ETH_HW_VLAN_TX_TAG;
1445 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1446 wanted |= IONIC_ETH_HW_VLAN_RX_STRIP;
1447 if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
1448 wanted |= IONIC_ETH_HW_VLAN_RX_FILTER;
1449 if (features & NETIF_F_RXHASH)
1450 wanted |= IONIC_ETH_HW_RX_HASH;
1451 if (features & NETIF_F_RXCSUM)
1452 wanted |= IONIC_ETH_HW_RX_CSUM;
1453 if (features & NETIF_F_SG)
1454 wanted |= IONIC_ETH_HW_TX_SG;
1455 if (features & NETIF_F_HW_CSUM)
1456 wanted |= IONIC_ETH_HW_TX_CSUM;
1457 if (features & NETIF_F_TSO)
1458 wanted |= IONIC_ETH_HW_TSO;
1459 if (features & NETIF_F_TSO6)
1460 wanted |= IONIC_ETH_HW_TSO_IPV6;
1461 if (features & NETIF_F_TSO_ECN)
1462 wanted |= IONIC_ETH_HW_TSO_ECN;
1463 if (features & NETIF_F_GSO_GRE)
1464 wanted |= IONIC_ETH_HW_TSO_GRE;
1465 if (features & NETIF_F_GSO_GRE_CSUM)
1466 wanted |= IONIC_ETH_HW_TSO_GRE_CSUM;
1467 if (features & NETIF_F_GSO_IPXIP4)
1468 wanted |= IONIC_ETH_HW_TSO_IPXIP4;
1469 if (features & NETIF_F_GSO_IPXIP6)
1470 wanted |= IONIC_ETH_HW_TSO_IPXIP6;
1471 if (features & NETIF_F_GSO_UDP_TUNNEL)
1472 wanted |= IONIC_ETH_HW_TSO_UDP;
1473 if (features & NETIF_F_GSO_UDP_TUNNEL_CSUM)
1474 wanted |= IONIC_ETH_HW_TSO_UDP_CSUM;
1476 return cpu_to_le64(wanted);
1479 static int ionic_set_nic_features(struct ionic_lif *lif,
1480 netdev_features_t features)
1482 struct device *dev = lif->ionic->dev;
1483 struct ionic_admin_ctx ctx = {
1484 .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
1485 .cmd.lif_setattr = {
1486 .opcode = IONIC_CMD_LIF_SETATTR,
1487 .index = cpu_to_le16(lif->index),
1488 .attr = IONIC_LIF_ATTR_FEATURES,
1491 u64 vlan_flags = IONIC_ETH_HW_VLAN_TX_TAG |
1492 IONIC_ETH_HW_VLAN_RX_STRIP |
1493 IONIC_ETH_HW_VLAN_RX_FILTER;
1494 u64 old_hw_features;
1497 ctx.cmd.lif_setattr.features = ionic_netdev_features_to_nic(features);
1500 ctx.cmd.lif_setattr.features |= cpu_to_le64(IONIC_ETH_HW_TIMESTAMP);
1502 err = ionic_adminq_post_wait(lif, &ctx);
1506 old_hw_features = lif->hw_features;
1507 lif->hw_features = le64_to_cpu(ctx.cmd.lif_setattr.features &
1508 ctx.comp.lif_setattr.features);
1510 if ((old_hw_features ^ lif->hw_features) & IONIC_ETH_HW_RX_HASH)
1511 ionic_lif_rss_config(lif, lif->rss_types, NULL, NULL);
1513 if ((vlan_flags & le64_to_cpu(ctx.cmd.lif_setattr.features)) &&
1514 !(vlan_flags & le64_to_cpu(ctx.comp.lif_setattr.features)))
1515 dev_info_once(lif->ionic->dev, "NIC is not supporting vlan offload, likely in SmartNIC mode\n");
1517 if (lif->hw_features & IONIC_ETH_HW_VLAN_TX_TAG)
1518 dev_dbg(dev, "feature ETH_HW_VLAN_TX_TAG\n");
1519 if (lif->hw_features & IONIC_ETH_HW_VLAN_RX_STRIP)
1520 dev_dbg(dev, "feature ETH_HW_VLAN_RX_STRIP\n");
1521 if (lif->hw_features & IONIC_ETH_HW_VLAN_RX_FILTER)
1522 dev_dbg(dev, "feature ETH_HW_VLAN_RX_FILTER\n");
1523 if (lif->hw_features & IONIC_ETH_HW_RX_HASH)
1524 dev_dbg(dev, "feature ETH_HW_RX_HASH\n");
1525 if (lif->hw_features & IONIC_ETH_HW_TX_SG)
1526 dev_dbg(dev, "feature ETH_HW_TX_SG\n");
1527 if (lif->hw_features & IONIC_ETH_HW_TX_CSUM)
1528 dev_dbg(dev, "feature ETH_HW_TX_CSUM\n");
1529 if (lif->hw_features & IONIC_ETH_HW_RX_CSUM)
1530 dev_dbg(dev, "feature ETH_HW_RX_CSUM\n");
1531 if (lif->hw_features & IONIC_ETH_HW_TSO)
1532 dev_dbg(dev, "feature ETH_HW_TSO\n");
1533 if (lif->hw_features & IONIC_ETH_HW_TSO_IPV6)
1534 dev_dbg(dev, "feature ETH_HW_TSO_IPV6\n");
1535 if (lif->hw_features & IONIC_ETH_HW_TSO_ECN)
1536 dev_dbg(dev, "feature ETH_HW_TSO_ECN\n");
1537 if (lif->hw_features & IONIC_ETH_HW_TSO_GRE)
1538 dev_dbg(dev, "feature ETH_HW_TSO_GRE\n");
1539 if (lif->hw_features & IONIC_ETH_HW_TSO_GRE_CSUM)
1540 dev_dbg(dev, "feature ETH_HW_TSO_GRE_CSUM\n");
1541 if (lif->hw_features & IONIC_ETH_HW_TSO_IPXIP4)
1542 dev_dbg(dev, "feature ETH_HW_TSO_IPXIP4\n");
1543 if (lif->hw_features & IONIC_ETH_HW_TSO_IPXIP6)
1544 dev_dbg(dev, "feature ETH_HW_TSO_IPXIP6\n");
1545 if (lif->hw_features & IONIC_ETH_HW_TSO_UDP)
1546 dev_dbg(dev, "feature ETH_HW_TSO_UDP\n");
1547 if (lif->hw_features & IONIC_ETH_HW_TSO_UDP_CSUM)
1548 dev_dbg(dev, "feature ETH_HW_TSO_UDP_CSUM\n");
1549 if (lif->hw_features & IONIC_ETH_HW_TIMESTAMP)
1550 dev_dbg(dev, "feature ETH_HW_TIMESTAMP\n");
1555 static int ionic_init_nic_features(struct ionic_lif *lif)
1557 struct net_device *netdev = lif->netdev;
1558 netdev_features_t features;
1561 /* set up what we expect to support by default */
1562 features = NETIF_F_HW_VLAN_CTAG_TX |
1563 NETIF_F_HW_VLAN_CTAG_RX |
1564 NETIF_F_HW_VLAN_CTAG_FILTER |
1572 NETIF_F_GSO_GRE_CSUM |
1573 NETIF_F_GSO_IPXIP4 |
1574 NETIF_F_GSO_IPXIP6 |
1575 NETIF_F_GSO_UDP_TUNNEL |
1576 NETIF_F_GSO_UDP_TUNNEL_CSUM;
1579 features |= NETIF_F_RXHASH;
1581 err = ionic_set_nic_features(lif, features);
1585 /* tell the netdev what we actually can support */
1586 netdev->features |= NETIF_F_HIGHDMA;
1588 if (lif->hw_features & IONIC_ETH_HW_VLAN_TX_TAG)
1589 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
1590 if (lif->hw_features & IONIC_ETH_HW_VLAN_RX_STRIP)
1591 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
1592 if (lif->hw_features & IONIC_ETH_HW_VLAN_RX_FILTER)
1593 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1594 if (lif->hw_features & IONIC_ETH_HW_RX_HASH)
1595 netdev->hw_features |= NETIF_F_RXHASH;
1596 if (lif->hw_features & IONIC_ETH_HW_TX_SG)
1597 netdev->hw_features |= NETIF_F_SG;
1599 if (lif->hw_features & IONIC_ETH_HW_TX_CSUM)
1600 netdev->hw_enc_features |= NETIF_F_HW_CSUM;
1601 if (lif->hw_features & IONIC_ETH_HW_RX_CSUM)
1602 netdev->hw_enc_features |= NETIF_F_RXCSUM;
1603 if (lif->hw_features & IONIC_ETH_HW_TSO)
1604 netdev->hw_enc_features |= NETIF_F_TSO;
1605 if (lif->hw_features & IONIC_ETH_HW_TSO_IPV6)
1606 netdev->hw_enc_features |= NETIF_F_TSO6;
1607 if (lif->hw_features & IONIC_ETH_HW_TSO_ECN)
1608 netdev->hw_enc_features |= NETIF_F_TSO_ECN;
1609 if (lif->hw_features & IONIC_ETH_HW_TSO_GRE)
1610 netdev->hw_enc_features |= NETIF_F_GSO_GRE;
1611 if (lif->hw_features & IONIC_ETH_HW_TSO_GRE_CSUM)
1612 netdev->hw_enc_features |= NETIF_F_GSO_GRE_CSUM;
1613 if (lif->hw_features & IONIC_ETH_HW_TSO_IPXIP4)
1614 netdev->hw_enc_features |= NETIF_F_GSO_IPXIP4;
1615 if (lif->hw_features & IONIC_ETH_HW_TSO_IPXIP6)
1616 netdev->hw_enc_features |= NETIF_F_GSO_IPXIP6;
1617 if (lif->hw_features & IONIC_ETH_HW_TSO_UDP)
1618 netdev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL;
1619 if (lif->hw_features & IONIC_ETH_HW_TSO_UDP_CSUM)
1620 netdev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
1622 netdev->hw_features |= netdev->hw_enc_features;
1623 netdev->features |= netdev->hw_features;
1624 netdev->vlan_features |= netdev->features & ~NETIF_F_VLAN_FEATURES;
1626 netdev->priv_flags |= IFF_UNICAST_FLT |
1627 IFF_LIVE_ADDR_CHANGE;
1629 netdev->xdp_features = NETDEV_XDP_ACT_BASIC |
1630 NETDEV_XDP_ACT_REDIRECT |
1631 NETDEV_XDP_ACT_RX_SG |
1632 NETDEV_XDP_ACT_NDO_XMIT |
1633 NETDEV_XDP_ACT_NDO_XMIT_SG;
1638 static int ionic_set_features(struct net_device *netdev,
1639 netdev_features_t features)
1641 struct ionic_lif *lif = netdev_priv(netdev);
1644 netdev_dbg(netdev, "%s: lif->features=0x%08llx new_features=0x%08llx\n",
1645 __func__, (u64)lif->netdev->features, (u64)features);
1647 err = ionic_set_nic_features(lif, features);
1652 static int ionic_set_attr_mac(struct ionic_lif *lif, u8 *mac)
1654 struct ionic_admin_ctx ctx = {
1655 .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
1656 .cmd.lif_setattr = {
1657 .opcode = IONIC_CMD_LIF_SETATTR,
1658 .index = cpu_to_le16(lif->index),
1659 .attr = IONIC_LIF_ATTR_MAC,
1663 ether_addr_copy(ctx.cmd.lif_setattr.mac, mac);
1664 return ionic_adminq_post_wait(lif, &ctx);
1667 static int ionic_get_attr_mac(struct ionic_lif *lif, u8 *mac_addr)
1669 struct ionic_admin_ctx ctx = {
1670 .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
1671 .cmd.lif_getattr = {
1672 .opcode = IONIC_CMD_LIF_GETATTR,
1673 .index = cpu_to_le16(lif->index),
1674 .attr = IONIC_LIF_ATTR_MAC,
1679 err = ionic_adminq_post_wait(lif, &ctx);
1683 ether_addr_copy(mac_addr, ctx.comp.lif_getattr.mac);
1687 static int ionic_program_mac(struct ionic_lif *lif, u8 *mac)
1689 u8 get_mac[ETH_ALEN];
1692 err = ionic_set_attr_mac(lif, mac);
1696 err = ionic_get_attr_mac(lif, get_mac);
1700 /* To deal with older firmware that silently ignores the set attr mac:
1701 * doesn't actually change the mac and doesn't return an error, so we
1702 * do the get attr to verify whether or not the set actually happened
1704 if (!ether_addr_equal(get_mac, mac))
1710 static int ionic_set_mac_address(struct net_device *netdev, void *sa)
1712 struct ionic_lif *lif = netdev_priv(netdev);
1713 struct sockaddr *addr = sa;
1717 mac = (u8 *)addr->sa_data;
1718 if (ether_addr_equal(netdev->dev_addr, mac))
1721 err = ionic_program_mac(lif, mac);
1726 netdev_dbg(netdev, "%s: SET and GET ATTR Mac are not equal-due to old FW running\n",
1729 err = eth_prepare_mac_addr_change(netdev, addr);
1733 if (!is_zero_ether_addr(netdev->dev_addr)) {
1734 netdev_info(netdev, "deleting mac addr %pM\n",
1736 ionic_lif_addr_del(netdev_priv(netdev), netdev->dev_addr);
1739 eth_commit_mac_addr_change(netdev, addr);
1740 netdev_info(netdev, "updating mac addr %pM\n", mac);
1742 return ionic_lif_addr_add(netdev_priv(netdev), mac);
1745 void ionic_stop_queues_reconfig(struct ionic_lif *lif)
1747 /* Stop and clean the queues before reconfiguration */
1748 netif_device_detach(lif->netdev);
1749 ionic_stop_queues(lif);
1750 ionic_txrx_deinit(lif);
1753 static int ionic_start_queues_reconfig(struct ionic_lif *lif)
1757 /* Re-init the queues after reconfiguration */
1759 /* The only way txrx_init can fail here is if communication
1760 * with FW is suddenly broken. There's not much we can do
1761 * at this point - error messages have already been printed,
1762 * so we can continue on and the user can eventually do a
1763 * DOWN and UP to try to reset and clear the issue.
1765 err = ionic_txrx_init(lif);
1766 ionic_link_status_check_request(lif, CAN_NOT_SLEEP);
1767 netif_device_attach(lif->netdev);
1772 static bool ionic_xdp_is_valid_mtu(struct ionic_lif *lif, u32 mtu,
1773 struct bpf_prog *xdp_prog)
1778 if (mtu <= IONIC_XDP_MAX_LINEAR_MTU)
1781 if (xdp_prog->aux && xdp_prog->aux->xdp_has_frags)
1787 static int ionic_change_mtu(struct net_device *netdev, int new_mtu)
1789 struct ionic_lif *lif = netdev_priv(netdev);
1790 struct ionic_admin_ctx ctx = {
1791 .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
1792 .cmd.lif_setattr = {
1793 .opcode = IONIC_CMD_LIF_SETATTR,
1794 .index = cpu_to_le16(lif->index),
1795 .attr = IONIC_LIF_ATTR_MTU,
1796 .mtu = cpu_to_le32(new_mtu),
1799 struct bpf_prog *xdp_prog;
1802 xdp_prog = READ_ONCE(lif->xdp_prog);
1803 if (!ionic_xdp_is_valid_mtu(lif, new_mtu, xdp_prog))
1806 err = ionic_adminq_post_wait(lif, &ctx);
1810 /* if we're not running, nothing more to do */
1811 if (!netif_running(netdev)) {
1812 WRITE_ONCE(netdev->mtu, new_mtu);
1816 mutex_lock(&lif->queue_lock);
1817 ionic_stop_queues_reconfig(lif);
1818 WRITE_ONCE(netdev->mtu, new_mtu);
1819 err = ionic_start_queues_reconfig(lif);
1820 mutex_unlock(&lif->queue_lock);
1825 static void ionic_tx_timeout_work(struct work_struct *ws)
1827 struct ionic_lif *lif = container_of(ws, struct ionic_lif, tx_timeout_work);
1830 if (test_bit(IONIC_LIF_F_FW_RESET, lif->state))
1833 /* if we were stopped before this scheduled job was launched,
1834 * don't bother the queues as they are already stopped.
1836 if (!netif_running(lif->netdev))
1839 mutex_lock(&lif->queue_lock);
1840 ionic_stop_queues_reconfig(lif);
1841 err = ionic_start_queues_reconfig(lif);
1842 mutex_unlock(&lif->queue_lock);
1845 dev_err(lif->ionic->dev, "%s: Restarting queues failed\n", __func__);
1848 static void ionic_tx_timeout(struct net_device *netdev, unsigned int txqueue)
1850 struct ionic_lif *lif = netdev_priv(netdev);
1852 netdev_info(lif->netdev, "Tx Timeout triggered - txq %d\n", txqueue);
1853 schedule_work(&lif->tx_timeout_work);
1856 static int ionic_vlan_rx_add_vid(struct net_device *netdev, __be16 proto,
1859 struct ionic_lif *lif = netdev_priv(netdev);
1862 err = ionic_lif_vlan_add(lif, vid);
1866 ionic_lif_rx_mode(lif);
1871 static int ionic_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto,
1874 struct ionic_lif *lif = netdev_priv(netdev);
1877 err = ionic_lif_vlan_del(lif, vid);
1881 ionic_lif_rx_mode(lif);
1886 int ionic_lif_rss_config(struct ionic_lif *lif, const u16 types,
1887 const u8 *key, const u32 *indir)
1889 struct ionic_admin_ctx ctx = {
1890 .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
1891 .cmd.lif_setattr = {
1892 .opcode = IONIC_CMD_LIF_SETATTR,
1893 .attr = IONIC_LIF_ATTR_RSS,
1894 .rss.addr = cpu_to_le64(lif->rss_ind_tbl_pa),
1897 unsigned int i, tbl_sz;
1899 if (lif->hw_features & IONIC_ETH_HW_RX_HASH) {
1900 lif->rss_types = types;
1901 ctx.cmd.lif_setattr.rss.types = cpu_to_le16(types);
1905 memcpy(lif->rss_hash_key, key, IONIC_RSS_HASH_KEY_SIZE);
1908 tbl_sz = le16_to_cpu(lif->ionic->ident.lif.eth.rss_ind_tbl_sz);
1909 for (i = 0; i < tbl_sz; i++)
1910 lif->rss_ind_tbl[i] = indir[i];
1913 memcpy(ctx.cmd.lif_setattr.rss.key, lif->rss_hash_key,
1914 IONIC_RSS_HASH_KEY_SIZE);
1916 return ionic_adminq_post_wait(lif, &ctx);
1919 static int ionic_lif_rss_init(struct ionic_lif *lif)
1921 unsigned int tbl_sz;
1924 lif->rss_types = IONIC_RSS_TYPE_IPV4 |
1925 IONIC_RSS_TYPE_IPV4_TCP |
1926 IONIC_RSS_TYPE_IPV4_UDP |
1927 IONIC_RSS_TYPE_IPV6 |
1928 IONIC_RSS_TYPE_IPV6_TCP |
1929 IONIC_RSS_TYPE_IPV6_UDP;
1931 /* Fill indirection table with 'default' values */
1932 tbl_sz = le16_to_cpu(lif->ionic->ident.lif.eth.rss_ind_tbl_sz);
1933 for (i = 0; i < tbl_sz; i++)
1934 lif->rss_ind_tbl[i] = ethtool_rxfh_indir_default(i, lif->nxqs);
1936 return ionic_lif_rss_config(lif, lif->rss_types, NULL, NULL);
1939 static void ionic_lif_rss_deinit(struct ionic_lif *lif)
1943 tbl_sz = le16_to_cpu(lif->ionic->ident.lif.eth.rss_ind_tbl_sz);
1944 memset(lif->rss_ind_tbl, 0, tbl_sz);
1945 memset(lif->rss_hash_key, 0, IONIC_RSS_HASH_KEY_SIZE);
1947 ionic_lif_rss_config(lif, 0x0, NULL, NULL);
1950 static void ionic_lif_quiesce(struct ionic_lif *lif)
1952 struct ionic_admin_ctx ctx = {
1953 .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
1954 .cmd.lif_setattr = {
1955 .opcode = IONIC_CMD_LIF_SETATTR,
1956 .index = cpu_to_le16(lif->index),
1957 .attr = IONIC_LIF_ATTR_STATE,
1958 .state = IONIC_LIF_QUIESCE,
1963 err = ionic_adminq_post_wait(lif, &ctx);
1965 netdev_dbg(lif->netdev, "lif quiesce failed %d\n", err);
1968 static void ionic_txrx_disable(struct ionic_lif *lif)
1974 for (i = 0; i < lif->nxqs; i++)
1975 err = ionic_qcq_disable(lif, lif->txqcqs[i], err);
1978 if (lif->hwstamp_txq)
1979 err = ionic_qcq_disable(lif, lif->hwstamp_txq, err);
1982 for (i = 0; i < lif->nxqs; i++)
1983 err = ionic_qcq_disable(lif, lif->rxqcqs[i], err);
1986 if (lif->hwstamp_rxq)
1987 err = ionic_qcq_disable(lif, lif->hwstamp_rxq, err);
1989 ionic_lif_quiesce(lif);
1992 static void ionic_txrx_deinit(struct ionic_lif *lif)
1997 for (i = 0; i < lif->nxqs && lif->txqcqs[i]; i++) {
1998 ionic_lif_qcq_deinit(lif, lif->txqcqs[i]);
1999 ionic_tx_flush(&lif->txqcqs[i]->cq);
2000 ionic_tx_empty(&lif->txqcqs[i]->q);
2005 for (i = 0; i < lif->nxqs && lif->rxqcqs[i]; i++) {
2006 ionic_lif_qcq_deinit(lif, lif->rxqcqs[i]);
2007 ionic_rx_empty(&lif->rxqcqs[i]->q);
2012 if (lif->hwstamp_txq) {
2013 ionic_lif_qcq_deinit(lif, lif->hwstamp_txq);
2014 ionic_tx_flush(&lif->hwstamp_txq->cq);
2015 ionic_tx_empty(&lif->hwstamp_txq->q);
2018 if (lif->hwstamp_rxq) {
2019 ionic_lif_qcq_deinit(lif, lif->hwstamp_rxq);
2020 ionic_rx_empty(&lif->hwstamp_rxq->q);
2024 void ionic_txrx_free(struct ionic_lif *lif)
2029 for (i = 0; i < lif->ionic->ntxqs_per_lif && lif->txqcqs[i]; i++) {
2030 ionic_qcq_free(lif, lif->txqcqs[i]);
2031 devm_kfree(lif->ionic->dev, lif->txqcqs[i]);
2032 lif->txqcqs[i] = NULL;
2037 for (i = 0; i < lif->ionic->nrxqs_per_lif && lif->rxqcqs[i]; i++) {
2038 ionic_qcq_free(lif, lif->rxqcqs[i]);
2039 devm_kfree(lif->ionic->dev, lif->rxqcqs[i]);
2040 lif->rxqcqs[i] = NULL;
2044 if (lif->hwstamp_txq) {
2045 ionic_qcq_free(lif, lif->hwstamp_txq);
2046 devm_kfree(lif->ionic->dev, lif->hwstamp_txq);
2047 lif->hwstamp_txq = NULL;
2050 if (lif->hwstamp_rxq) {
2051 ionic_qcq_free(lif, lif->hwstamp_rxq);
2052 devm_kfree(lif->ionic->dev, lif->hwstamp_rxq);
2053 lif->hwstamp_rxq = NULL;
2057 static int ionic_txrx_alloc(struct ionic_lif *lif)
2059 unsigned int comp_sz, desc_sz, num_desc, sg_desc_sz;
2060 unsigned int flags, i;
2063 num_desc = lif->ntxq_descs;
2064 desc_sz = sizeof(struct ionic_txq_desc);
2065 comp_sz = sizeof(struct ionic_txq_comp);
2067 if (lif->qtype_info[IONIC_QTYPE_TXQ].version >= 1 &&
2068 lif->qtype_info[IONIC_QTYPE_TXQ].sg_desc_sz ==
2069 sizeof(struct ionic_txq_sg_desc_v1))
2070 sg_desc_sz = sizeof(struct ionic_txq_sg_desc_v1);
2072 sg_desc_sz = sizeof(struct ionic_txq_sg_desc);
2074 flags = IONIC_QCQ_F_TX_STATS | IONIC_QCQ_F_SG;
2076 if (test_bit(IONIC_LIF_F_CMB_TX_RINGS, lif->state))
2077 flags |= IONIC_QCQ_F_CMB_RINGS;
2079 if (test_bit(IONIC_LIF_F_SPLIT_INTR, lif->state))
2080 flags |= IONIC_QCQ_F_INTR;
2082 for (i = 0; i < lif->nxqs; i++) {
2083 err = ionic_qcq_alloc(lif, IONIC_QTYPE_TXQ, i, "tx", flags,
2084 num_desc, desc_sz, comp_sz, sg_desc_sz,
2085 sizeof(struct ionic_tx_desc_info),
2086 lif->kern_pid, NULL, &lif->txqcqs[i]);
2090 if (flags & IONIC_QCQ_F_INTR) {
2091 ionic_intr_coal_init(lif->ionic->idev.intr_ctrl,
2092 lif->txqcqs[i]->intr.index,
2093 lif->tx_coalesce_hw);
2094 if (test_bit(IONIC_LIF_F_TX_DIM_INTR, lif->state))
2095 lif->txqcqs[i]->intr.dim_coal_hw = lif->tx_coalesce_hw;
2098 ionic_debugfs_add_qcq(lif, lif->txqcqs[i]);
2101 flags = IONIC_QCQ_F_RX_STATS | IONIC_QCQ_F_SG | IONIC_QCQ_F_INTR;
2103 if (test_bit(IONIC_LIF_F_CMB_RX_RINGS, lif->state))
2104 flags |= IONIC_QCQ_F_CMB_RINGS;
2106 num_desc = lif->nrxq_descs;
2107 desc_sz = sizeof(struct ionic_rxq_desc);
2108 comp_sz = sizeof(struct ionic_rxq_comp);
2109 sg_desc_sz = sizeof(struct ionic_rxq_sg_desc);
2111 if (lif->rxq_features & IONIC_Q_F_2X_CQ_DESC)
2114 for (i = 0; i < lif->nxqs; i++) {
2115 err = ionic_qcq_alloc(lif, IONIC_QTYPE_RXQ, i, "rx", flags,
2116 num_desc, desc_sz, comp_sz, sg_desc_sz,
2117 sizeof(struct ionic_rx_desc_info),
2118 lif->kern_pid, lif->xdp_prog,
2123 lif->rxqcqs[i]->q.features = lif->rxq_features;
2125 ionic_intr_coal_init(lif->ionic->idev.intr_ctrl,
2126 lif->rxqcqs[i]->intr.index,
2127 lif->rx_coalesce_hw);
2128 if (test_bit(IONIC_LIF_F_RX_DIM_INTR, lif->state))
2129 lif->rxqcqs[i]->intr.dim_coal_hw = lif->rx_coalesce_hw;
2131 if (!test_bit(IONIC_LIF_F_SPLIT_INTR, lif->state))
2132 ionic_link_qcq_interrupts(lif->rxqcqs[i],
2135 ionic_debugfs_add_qcq(lif, lif->rxqcqs[i]);
2141 ionic_txrx_free(lif);
2146 static int ionic_txrx_init(struct ionic_lif *lif)
2151 for (i = 0; i < lif->nxqs; i++) {
2152 err = ionic_lif_txq_init(lif, lif->txqcqs[i]);
2156 err = ionic_lif_rxq_init(lif, lif->rxqcqs[i]);
2158 ionic_lif_qcq_deinit(lif, lif->txqcqs[i]);
2163 if (lif->netdev->features & NETIF_F_RXHASH)
2164 ionic_lif_rss_init(lif);
2166 ionic_lif_rx_mode(lif);
2172 ionic_lif_qcq_deinit(lif, lif->txqcqs[i]);
2173 ionic_lif_qcq_deinit(lif, lif->rxqcqs[i]);
2179 static int ionic_txrx_enable(struct ionic_lif *lif)
2184 ionic_xdp_rxqs_prog_update(lif);
2186 for (i = 0; i < lif->nxqs; i++) {
2187 if (!(lif->rxqcqs[i] && lif->txqcqs[i])) {
2188 dev_err(lif->ionic->dev, "%s: bad qcq %d\n", __func__, i);
2193 ionic_rx_fill(&lif->rxqcqs[i]->q,
2194 READ_ONCE(lif->rxqcqs[i]->q.xdp_prog));
2195 err = ionic_qcq_enable(lif->rxqcqs[i]);
2199 err = ionic_qcq_enable(lif->txqcqs[i]);
2201 derr = ionic_qcq_disable(lif, lif->rxqcqs[i], err);
2206 if (lif->hwstamp_rxq) {
2207 ionic_rx_fill(&lif->hwstamp_rxq->q, NULL);
2208 err = ionic_qcq_enable(lif->hwstamp_rxq);
2210 goto err_out_hwstamp_rx;
2213 if (lif->hwstamp_txq) {
2214 err = ionic_qcq_enable(lif->hwstamp_txq);
2216 goto err_out_hwstamp_tx;
2222 if (lif->hwstamp_rxq)
2223 derr = ionic_qcq_disable(lif, lif->hwstamp_rxq, derr);
2228 derr = ionic_qcq_disable(lif, lif->txqcqs[i], derr);
2229 derr = ionic_qcq_disable(lif, lif->rxqcqs[i], derr);
2232 ionic_xdp_rxqs_prog_update(lif);
2237 static int ionic_start_queues(struct ionic_lif *lif)
2241 if (test_bit(IONIC_LIF_F_BROKEN, lif->state))
2244 if (test_bit(IONIC_LIF_F_FW_RESET, lif->state))
2247 if (test_and_set_bit(IONIC_LIF_F_UP, lif->state))
2250 err = ionic_txrx_enable(lif);
2252 clear_bit(IONIC_LIF_F_UP, lif->state);
2255 netif_tx_wake_all_queues(lif->netdev);
2260 static int ionic_open(struct net_device *netdev)
2262 struct ionic_lif *lif = netdev_priv(netdev);
2265 /* If recovering from a broken state, clear the bit and we'll try again */
2266 if (test_and_clear_bit(IONIC_LIF_F_BROKEN, lif->state))
2267 netdev_info(netdev, "clearing broken state\n");
2269 mutex_lock(&lif->queue_lock);
2271 err = ionic_txrx_alloc(lif);
2275 err = ionic_txrx_init(lif);
2279 err = netif_set_real_num_tx_queues(netdev, lif->nxqs);
2281 goto err_txrx_deinit;
2283 err = netif_set_real_num_rx_queues(netdev, lif->nxqs);
2285 goto err_txrx_deinit;
2287 /* don't start the queues until we have link */
2288 if (netif_carrier_ok(netdev)) {
2289 err = ionic_start_queues(lif);
2291 goto err_txrx_deinit;
2294 /* If hardware timestamping is enabled, but the queues were freed by
2295 * ionic_stop, those need to be reallocated and initialized, too.
2297 ionic_lif_hwstamp_recreate_queues(lif);
2299 mutex_unlock(&lif->queue_lock);
2304 ionic_txrx_deinit(lif);
2306 ionic_txrx_free(lif);
2308 mutex_unlock(&lif->queue_lock);
2312 static void ionic_stop_queues(struct ionic_lif *lif)
2314 if (!test_and_clear_bit(IONIC_LIF_F_UP, lif->state))
2317 netif_tx_disable(lif->netdev);
2318 ionic_txrx_disable(lif);
2321 static int ionic_stop(struct net_device *netdev)
2323 struct ionic_lif *lif = netdev_priv(netdev);
2325 if (test_bit(IONIC_LIF_F_FW_RESET, lif->state))
2328 mutex_lock(&lif->queue_lock);
2329 ionic_stop_queues(lif);
2330 ionic_txrx_deinit(lif);
2331 ionic_txrx_free(lif);
2332 mutex_unlock(&lif->queue_lock);
2337 static int ionic_eth_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2339 struct ionic_lif *lif = netdev_priv(netdev);
2343 return ionic_lif_hwstamp_set(lif, ifr);
2345 return ionic_lif_hwstamp_get(lif, ifr);
2351 static int ionic_get_vf_config(struct net_device *netdev,
2352 int vf, struct ifla_vf_info *ivf)
2354 struct ionic_lif *lif = netdev_priv(netdev);
2355 struct ionic *ionic = lif->ionic;
2358 if (!netif_device_present(netdev))
2361 down_read(&ionic->vf_op_lock);
2363 if (vf >= pci_num_vf(ionic->pdev) || !ionic->vfs) {
2366 struct ionic_vf *vfdata = &ionic->vfs[vf];
2370 ivf->vlan = le16_to_cpu(vfdata->vlanid);
2371 ivf->spoofchk = vfdata->spoofchk;
2372 ivf->linkstate = vfdata->linkstate;
2373 ivf->max_tx_rate = le32_to_cpu(vfdata->maxrate);
2374 ivf->trusted = vfdata->trusted;
2375 ether_addr_copy(ivf->mac, vfdata->macaddr);
2378 up_read(&ionic->vf_op_lock);
2382 static int ionic_get_vf_stats(struct net_device *netdev, int vf,
2383 struct ifla_vf_stats *vf_stats)
2385 struct ionic_lif *lif = netdev_priv(netdev);
2386 struct ionic *ionic = lif->ionic;
2387 struct ionic_lif_stats *vs;
2390 if (!netif_device_present(netdev))
2393 down_read(&ionic->vf_op_lock);
2395 if (vf >= pci_num_vf(ionic->pdev) || !ionic->vfs) {
2398 memset(vf_stats, 0, sizeof(*vf_stats));
2399 vs = &ionic->vfs[vf].stats;
2401 vf_stats->rx_packets = le64_to_cpu(vs->rx_ucast_packets);
2402 vf_stats->tx_packets = le64_to_cpu(vs->tx_ucast_packets);
2403 vf_stats->rx_bytes = le64_to_cpu(vs->rx_ucast_bytes);
2404 vf_stats->tx_bytes = le64_to_cpu(vs->tx_ucast_bytes);
2405 vf_stats->broadcast = le64_to_cpu(vs->rx_bcast_packets);
2406 vf_stats->multicast = le64_to_cpu(vs->rx_mcast_packets);
2407 vf_stats->rx_dropped = le64_to_cpu(vs->rx_ucast_drop_packets) +
2408 le64_to_cpu(vs->rx_mcast_drop_packets) +
2409 le64_to_cpu(vs->rx_bcast_drop_packets);
2410 vf_stats->tx_dropped = le64_to_cpu(vs->tx_ucast_drop_packets) +
2411 le64_to_cpu(vs->tx_mcast_drop_packets) +
2412 le64_to_cpu(vs->tx_bcast_drop_packets);
2415 up_read(&ionic->vf_op_lock);
2419 static int ionic_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
2421 struct ionic_vf_setattr_cmd vfc = { .attr = IONIC_VF_ATTR_MAC };
2422 struct ionic_lif *lif = netdev_priv(netdev);
2423 struct ionic *ionic = lif->ionic;
2426 if (!(is_zero_ether_addr(mac) || is_valid_ether_addr(mac)))
2429 if (!netif_device_present(netdev))
2432 down_write(&ionic->vf_op_lock);
2434 if (vf >= pci_num_vf(ionic->pdev) || !ionic->vfs) {
2437 ether_addr_copy(vfc.macaddr, mac);
2438 dev_dbg(ionic->dev, "%s: vf %d macaddr %pM\n",
2439 __func__, vf, vfc.macaddr);
2441 ret = ionic_set_vf_config(ionic, vf, &vfc);
2443 ether_addr_copy(ionic->vfs[vf].macaddr, mac);
2446 up_write(&ionic->vf_op_lock);
2450 static int ionic_set_vf_vlan(struct net_device *netdev, int vf, u16 vlan,
2451 u8 qos, __be16 proto)
2453 struct ionic_vf_setattr_cmd vfc = { .attr = IONIC_VF_ATTR_VLAN };
2454 struct ionic_lif *lif = netdev_priv(netdev);
2455 struct ionic *ionic = lif->ionic;
2458 /* until someday when we support qos */
2465 if (proto != htons(ETH_P_8021Q))
2466 return -EPROTONOSUPPORT;
2468 if (!netif_device_present(netdev))
2471 down_write(&ionic->vf_op_lock);
2473 if (vf >= pci_num_vf(ionic->pdev) || !ionic->vfs) {
2476 vfc.vlanid = cpu_to_le16(vlan);
2477 dev_dbg(ionic->dev, "%s: vf %d vlan %d\n",
2478 __func__, vf, le16_to_cpu(vfc.vlanid));
2480 ret = ionic_set_vf_config(ionic, vf, &vfc);
2482 ionic->vfs[vf].vlanid = cpu_to_le16(vlan);
2485 up_write(&ionic->vf_op_lock);
2489 static int ionic_set_vf_rate(struct net_device *netdev, int vf,
2490 int tx_min, int tx_max)
2492 struct ionic_vf_setattr_cmd vfc = { .attr = IONIC_VF_ATTR_RATE };
2493 struct ionic_lif *lif = netdev_priv(netdev);
2494 struct ionic *ionic = lif->ionic;
2497 /* setting the min just seems silly */
2501 if (!netif_device_present(netdev))
2504 down_write(&ionic->vf_op_lock);
2506 if (vf >= pci_num_vf(ionic->pdev) || !ionic->vfs) {
2509 vfc.maxrate = cpu_to_le32(tx_max);
2510 dev_dbg(ionic->dev, "%s: vf %d maxrate %d\n",
2511 __func__, vf, le32_to_cpu(vfc.maxrate));
2513 ret = ionic_set_vf_config(ionic, vf, &vfc);
2515 ionic->vfs[vf].maxrate = cpu_to_le32(tx_max);
2518 up_write(&ionic->vf_op_lock);
2522 static int ionic_set_vf_spoofchk(struct net_device *netdev, int vf, bool set)
2524 struct ionic_vf_setattr_cmd vfc = { .attr = IONIC_VF_ATTR_SPOOFCHK };
2525 struct ionic_lif *lif = netdev_priv(netdev);
2526 struct ionic *ionic = lif->ionic;
2529 if (!netif_device_present(netdev))
2532 down_write(&ionic->vf_op_lock);
2534 if (vf >= pci_num_vf(ionic->pdev) || !ionic->vfs) {
2538 dev_dbg(ionic->dev, "%s: vf %d spoof %d\n",
2539 __func__, vf, vfc.spoofchk);
2541 ret = ionic_set_vf_config(ionic, vf, &vfc);
2543 ionic->vfs[vf].spoofchk = set;
2546 up_write(&ionic->vf_op_lock);
2550 static int ionic_set_vf_trust(struct net_device *netdev, int vf, bool set)
2552 struct ionic_vf_setattr_cmd vfc = { .attr = IONIC_VF_ATTR_TRUST };
2553 struct ionic_lif *lif = netdev_priv(netdev);
2554 struct ionic *ionic = lif->ionic;
2557 if (!netif_device_present(netdev))
2560 down_write(&ionic->vf_op_lock);
2562 if (vf >= pci_num_vf(ionic->pdev) || !ionic->vfs) {
2566 dev_dbg(ionic->dev, "%s: vf %d trust %d\n",
2567 __func__, vf, vfc.trust);
2569 ret = ionic_set_vf_config(ionic, vf, &vfc);
2571 ionic->vfs[vf].trusted = set;
2574 up_write(&ionic->vf_op_lock);
2578 static int ionic_set_vf_link_state(struct net_device *netdev, int vf, int set)
2580 struct ionic_vf_setattr_cmd vfc = { .attr = IONIC_VF_ATTR_LINKSTATE };
2581 struct ionic_lif *lif = netdev_priv(netdev);
2582 struct ionic *ionic = lif->ionic;
2587 case IFLA_VF_LINK_STATE_ENABLE:
2588 vfls = IONIC_VF_LINK_STATUS_UP;
2590 case IFLA_VF_LINK_STATE_DISABLE:
2591 vfls = IONIC_VF_LINK_STATUS_DOWN;
2593 case IFLA_VF_LINK_STATE_AUTO:
2594 vfls = IONIC_VF_LINK_STATUS_AUTO;
2600 if (!netif_device_present(netdev))
2603 down_write(&ionic->vf_op_lock);
2605 if (vf >= pci_num_vf(ionic->pdev) || !ionic->vfs) {
2608 vfc.linkstate = vfls;
2609 dev_dbg(ionic->dev, "%s: vf %d linkstate %d\n",
2610 __func__, vf, vfc.linkstate);
2612 ret = ionic_set_vf_config(ionic, vf, &vfc);
2614 ionic->vfs[vf].linkstate = set;
2617 up_write(&ionic->vf_op_lock);
2621 static void ionic_vf_attr_replay(struct ionic_lif *lif)
2623 struct ionic_vf_setattr_cmd vfc = { };
2624 struct ionic *ionic = lif->ionic;
2631 down_read(&ionic->vf_op_lock);
2633 for (i = 0; i < ionic->num_vfs; i++) {
2637 vfc.attr = IONIC_VF_ATTR_STATSADDR;
2638 vfc.stats_pa = cpu_to_le64(v->stats_pa);
2639 ionic_set_vf_config(ionic, i, &vfc);
2643 if (!is_zero_ether_addr(v->macaddr)) {
2644 vfc.attr = IONIC_VF_ATTR_MAC;
2645 ether_addr_copy(vfc.macaddr, v->macaddr);
2646 ionic_set_vf_config(ionic, i, &vfc);
2647 eth_zero_addr(vfc.macaddr);
2651 vfc.attr = IONIC_VF_ATTR_VLAN;
2652 vfc.vlanid = v->vlanid;
2653 ionic_set_vf_config(ionic, i, &vfc);
2658 vfc.attr = IONIC_VF_ATTR_RATE;
2659 vfc.maxrate = v->maxrate;
2660 ionic_set_vf_config(ionic, i, &vfc);
2665 vfc.attr = IONIC_VF_ATTR_SPOOFCHK;
2666 vfc.spoofchk = v->spoofchk;
2667 ionic_set_vf_config(ionic, i, &vfc);
2672 vfc.attr = IONIC_VF_ATTR_TRUST;
2673 vfc.trust = v->trusted;
2674 ionic_set_vf_config(ionic, i, &vfc);
2679 vfc.attr = IONIC_VF_ATTR_LINKSTATE;
2680 vfc.linkstate = v->linkstate;
2681 ionic_set_vf_config(ionic, i, &vfc);
2686 up_read(&ionic->vf_op_lock);
2688 ionic_vf_start(ionic);
2691 static void ionic_unregister_rxq_info(struct ionic_queue *q)
2693 struct xdp_rxq_info *xi;
2695 if (!q->xdp_rxq_info)
2698 xi = q->xdp_rxq_info;
2699 q->xdp_rxq_info = NULL;
2701 xdp_rxq_info_unreg(xi);
2705 static int ionic_register_rxq_info(struct ionic_queue *q, unsigned int napi_id)
2707 struct xdp_rxq_info *rxq_info;
2710 rxq_info = kzalloc(sizeof(*rxq_info), GFP_KERNEL);
2714 err = xdp_rxq_info_reg(rxq_info, q->lif->netdev, q->index, napi_id);
2716 netdev_err(q->lif->netdev, "q%d xdp_rxq_info_reg failed, err %d\n",
2721 err = xdp_rxq_info_reg_mem_model(rxq_info, MEM_TYPE_PAGE_POOL, q->page_pool);
2723 netdev_err(q->lif->netdev, "q%d xdp_rxq_info_reg_mem_model failed, err %d\n",
2725 xdp_rxq_info_unreg(rxq_info);
2729 q->xdp_rxq_info = rxq_info;
2738 static void ionic_xdp_rxqs_prog_update(struct ionic_lif *lif)
2740 struct bpf_prog *xdp_prog;
2746 xdp_prog = READ_ONCE(lif->xdp_prog);
2747 for (i = 0; i < lif->ionic->nrxqs_per_lif && lif->rxqcqs[i]; i++) {
2748 struct ionic_queue *q = &lif->rxqcqs[i]->q;
2750 WRITE_ONCE(q->xdp_prog, xdp_prog);
2754 static int ionic_xdp_config(struct net_device *netdev, struct netdev_bpf *bpf)
2756 struct ionic_lif *lif = netdev_priv(netdev);
2757 struct bpf_prog *old_prog;
2760 if (test_bit(IONIC_LIF_F_SPLIT_INTR, lif->state)) {
2761 #define XDP_ERR_SPLIT "XDP not available with split Tx/Rx interrupts"
2762 NL_SET_ERR_MSG_MOD(bpf->extack, XDP_ERR_SPLIT);
2763 netdev_info(lif->netdev, XDP_ERR_SPLIT);
2767 if (!ionic_xdp_is_valid_mtu(lif, netdev->mtu, bpf->prog)) {
2768 #define XDP_ERR_MTU "MTU is too large for XDP without frags support"
2769 NL_SET_ERR_MSG_MOD(bpf->extack, XDP_ERR_MTU);
2770 netdev_info(lif->netdev, XDP_ERR_MTU);
2774 maxfs = __le32_to_cpu(lif->identity->eth.max_frame_size) - VLAN_ETH_HLEN;
2775 if (bpf->prog && !(bpf->prog->aux && bpf->prog->aux->xdp_has_frags))
2776 maxfs = min_t(u32, maxfs, IONIC_XDP_MAX_LINEAR_MTU);
2777 netdev->max_mtu = maxfs;
2779 if (!netif_running(netdev)) {
2780 old_prog = xchg(&lif->xdp_prog, bpf->prog);
2781 } else if (lif->xdp_prog && bpf->prog) {
2782 old_prog = xchg(&lif->xdp_prog, bpf->prog);
2783 ionic_xdp_rxqs_prog_update(lif);
2785 struct ionic_queue_params qparams;
2787 ionic_init_queue_params(lif, &qparams);
2788 qparams.xdp_prog = bpf->prog;
2789 mutex_lock(&lif->queue_lock);
2790 ionic_reconfigure_queues(lif, &qparams);
2791 old_prog = xchg(&lif->xdp_prog, bpf->prog);
2792 mutex_unlock(&lif->queue_lock);
2796 bpf_prog_put(old_prog);
2801 static int ionic_xdp(struct net_device *netdev, struct netdev_bpf *bpf)
2803 switch (bpf->command) {
2804 case XDP_SETUP_PROG:
2805 return ionic_xdp_config(netdev, bpf);
2811 static const struct net_device_ops ionic_netdev_ops = {
2812 .ndo_open = ionic_open,
2813 .ndo_stop = ionic_stop,
2814 .ndo_eth_ioctl = ionic_eth_ioctl,
2815 .ndo_start_xmit = ionic_start_xmit,
2816 .ndo_bpf = ionic_xdp,
2817 .ndo_xdp_xmit = ionic_xdp_xmit,
2818 .ndo_get_stats64 = ionic_get_stats64,
2819 .ndo_set_rx_mode = ionic_ndo_set_rx_mode,
2820 .ndo_set_features = ionic_set_features,
2821 .ndo_set_mac_address = ionic_set_mac_address,
2822 .ndo_validate_addr = eth_validate_addr,
2823 .ndo_tx_timeout = ionic_tx_timeout,
2824 .ndo_change_mtu = ionic_change_mtu,
2825 .ndo_vlan_rx_add_vid = ionic_vlan_rx_add_vid,
2826 .ndo_vlan_rx_kill_vid = ionic_vlan_rx_kill_vid,
2827 .ndo_set_vf_vlan = ionic_set_vf_vlan,
2828 .ndo_set_vf_trust = ionic_set_vf_trust,
2829 .ndo_set_vf_mac = ionic_set_vf_mac,
2830 .ndo_set_vf_rate = ionic_set_vf_rate,
2831 .ndo_set_vf_spoofchk = ionic_set_vf_spoofchk,
2832 .ndo_get_vf_config = ionic_get_vf_config,
2833 .ndo_set_vf_link_state = ionic_set_vf_link_state,
2834 .ndo_get_vf_stats = ionic_get_vf_stats,
2837 static int ionic_cmb_reconfig(struct ionic_lif *lif,
2838 struct ionic_queue_params *qparam)
2840 struct ionic_queue_params start_qparams;
2843 /* When changing CMB queue parameters, we're using limited
2844 * on-device memory and don't have extra memory to use for
2845 * duplicate allocations, so we free it all first then
2846 * re-allocate with the new parameters.
2849 /* Checkpoint for possible unwind */
2850 ionic_init_queue_params(lif, &start_qparams);
2852 /* Stop and free the queues */
2853 ionic_stop_queues_reconfig(lif);
2854 ionic_txrx_free(lif);
2856 /* Set up new qparams */
2857 ionic_set_queue_params(lif, qparam);
2859 if (netif_running(lif->netdev)) {
2860 /* Alloc and start the new configuration */
2861 err = ionic_txrx_alloc(lif);
2863 dev_warn(lif->ionic->dev,
2864 "CMB reconfig failed, restoring values: %d\n", err);
2866 /* Back out the changes */
2867 ionic_set_queue_params(lif, &start_qparams);
2868 err = ionic_txrx_alloc(lif);
2870 dev_err(lif->ionic->dev,
2871 "CMB restore failed: %d\n", err);
2876 err = ionic_start_queues_reconfig(lif);
2878 dev_err(lif->ionic->dev,
2879 "CMB reconfig failed: %d\n", err);
2885 /* This was detached in ionic_stop_queues_reconfig() */
2886 netif_device_attach(lif->netdev);
2891 static void ionic_swap_queues(struct ionic_qcq *a, struct ionic_qcq *b)
2893 /* only swapping the queues and napi, not flags or other stuff */
2894 swap(a->napi, b->napi);
2896 if (a->q.type == IONIC_QTYPE_RXQ) {
2897 swap(a->q.page_pool, b->q.page_pool);
2898 a->q.page_pool->p.napi = &a->napi;
2899 if (b->q.page_pool) /* is NULL when increasing queue count */
2900 b->q.page_pool->p.napi = &b->napi;
2903 swap(a->q.features, b->q.features);
2904 swap(a->q.num_descs, b->q.num_descs);
2905 swap(a->q.desc_size, b->q.desc_size);
2906 swap(a->q.base, b->q.base);
2907 swap(a->q.base_pa, b->q.base_pa);
2908 swap(a->q.info, b->q.info);
2909 swap(a->q.xdp_prog, b->q.xdp_prog);
2910 swap(a->q.xdp_rxq_info, b->q.xdp_rxq_info);
2911 swap(a->q.partner, b->q.partner);
2912 swap(a->q_base, b->q_base);
2913 swap(a->q_base_pa, b->q_base_pa);
2914 swap(a->q_size, b->q_size);
2916 swap(a->q.sg_desc_size, b->q.sg_desc_size);
2917 swap(a->q.sg_base, b->q.sg_base);
2918 swap(a->q.sg_base_pa, b->q.sg_base_pa);
2919 swap(a->sg_base, b->sg_base);
2920 swap(a->sg_base_pa, b->sg_base_pa);
2921 swap(a->sg_size, b->sg_size);
2923 swap(a->cq.num_descs, b->cq.num_descs);
2924 swap(a->cq.desc_size, b->cq.desc_size);
2925 swap(a->cq.base, b->cq.base);
2926 swap(a->cq.base_pa, b->cq.base_pa);
2927 swap(a->cq_base, b->cq_base);
2928 swap(a->cq_base_pa, b->cq_base_pa);
2929 swap(a->cq_size, b->cq_size);
2931 ionic_debugfs_del_qcq(a);
2932 ionic_debugfs_add_qcq(a->q.lif, a);
2935 int ionic_reconfigure_queues(struct ionic_lif *lif,
2936 struct ionic_queue_params *qparam)
2938 unsigned int comp_sz, desc_sz, num_desc, sg_desc_sz;
2939 struct ionic_qcq **tx_qcqs = NULL;
2940 struct ionic_qcq **rx_qcqs = NULL;
2941 unsigned int flags, i;
2944 /* Are we changing q params while CMB is on */
2945 if ((test_bit(IONIC_LIF_F_CMB_TX_RINGS, lif->state) && qparam->cmb_tx) ||
2946 (test_bit(IONIC_LIF_F_CMB_RX_RINGS, lif->state) && qparam->cmb_rx))
2947 return ionic_cmb_reconfig(lif, qparam);
2949 /* allocate temporary qcq arrays to hold new queue structs */
2950 if (qparam->nxqs != lif->nxqs || qparam->ntxq_descs != lif->ntxq_descs) {
2951 tx_qcqs = devm_kcalloc(lif->ionic->dev, lif->ionic->ntxqs_per_lif,
2952 sizeof(struct ionic_qcq *), GFP_KERNEL);
2958 if (qparam->nxqs != lif->nxqs ||
2959 qparam->nrxq_descs != lif->nrxq_descs ||
2960 qparam->rxq_features != lif->rxq_features ||
2961 qparam->xdp_prog != lif->xdp_prog) {
2962 rx_qcqs = devm_kcalloc(lif->ionic->dev, lif->ionic->nrxqs_per_lif,
2963 sizeof(struct ionic_qcq *), GFP_KERNEL);
2970 /* allocate new desc_info and rings, but leave the interrupt setup
2971 * until later so as to not mess with the still-running queues
2974 num_desc = qparam->ntxq_descs;
2975 desc_sz = sizeof(struct ionic_txq_desc);
2976 comp_sz = sizeof(struct ionic_txq_comp);
2978 if (lif->qtype_info[IONIC_QTYPE_TXQ].version >= 1 &&
2979 lif->qtype_info[IONIC_QTYPE_TXQ].sg_desc_sz ==
2980 sizeof(struct ionic_txq_sg_desc_v1))
2981 sg_desc_sz = sizeof(struct ionic_txq_sg_desc_v1);
2983 sg_desc_sz = sizeof(struct ionic_txq_sg_desc);
2985 for (i = 0; i < qparam->nxqs; i++) {
2986 /* If missing, short placeholder qcq needed for swap */
2987 if (!lif->txqcqs[i]) {
2988 flags = IONIC_QCQ_F_TX_STATS | IONIC_QCQ_F_SG;
2989 err = ionic_qcq_alloc(lif, IONIC_QTYPE_TXQ, i, "tx", flags,
2990 4, desc_sz, comp_sz, sg_desc_sz,
2991 sizeof(struct ionic_tx_desc_info),
2992 lif->kern_pid, NULL, &lif->txqcqs[i]);
2997 flags = lif->txqcqs[i]->flags & ~IONIC_QCQ_F_INTR;
2998 err = ionic_qcq_alloc(lif, IONIC_QTYPE_TXQ, i, "tx", flags,
2999 num_desc, desc_sz, comp_sz, sg_desc_sz,
3000 sizeof(struct ionic_tx_desc_info),
3001 lif->kern_pid, NULL, &tx_qcqs[i]);
3008 num_desc = qparam->nrxq_descs;
3009 desc_sz = sizeof(struct ionic_rxq_desc);
3010 comp_sz = sizeof(struct ionic_rxq_comp);
3011 sg_desc_sz = sizeof(struct ionic_rxq_sg_desc);
3013 if (qparam->rxq_features & IONIC_Q_F_2X_CQ_DESC)
3016 for (i = 0; i < qparam->nxqs; i++) {
3017 /* If missing, short placeholder qcq needed for swap */
3018 if (!lif->rxqcqs[i]) {
3019 flags = IONIC_QCQ_F_RX_STATS | IONIC_QCQ_F_SG;
3020 err = ionic_qcq_alloc(lif, IONIC_QTYPE_RXQ, i, "rx", flags,
3021 4, desc_sz, comp_sz, sg_desc_sz,
3022 sizeof(struct ionic_rx_desc_info),
3023 lif->kern_pid, NULL, &lif->rxqcqs[i]);
3028 flags = lif->rxqcqs[i]->flags & ~IONIC_QCQ_F_INTR;
3029 err = ionic_qcq_alloc(lif, IONIC_QTYPE_RXQ, i, "rx", flags,
3030 num_desc, desc_sz, comp_sz, sg_desc_sz,
3031 sizeof(struct ionic_rx_desc_info),
3032 lif->kern_pid, qparam->xdp_prog, &rx_qcqs[i]);
3036 rx_qcqs[i]->q.features = qparam->rxq_features;
3037 rx_qcqs[i]->q.xdp_prog = qparam->xdp_prog;
3041 /* stop and clean the queues */
3042 ionic_stop_queues_reconfig(lif);
3044 if (qparam->nxqs != lif->nxqs) {
3045 err = netif_set_real_num_tx_queues(lif->netdev, qparam->nxqs);
3047 goto err_out_reinit_unlock;
3048 err = netif_set_real_num_rx_queues(lif->netdev, qparam->nxqs);
3050 netif_set_real_num_tx_queues(lif->netdev, lif->nxqs);
3051 goto err_out_reinit_unlock;
3055 /* swap new desc_info and rings, keeping existing interrupt config */
3057 lif->ntxq_descs = qparam->ntxq_descs;
3058 for (i = 0; i < qparam->nxqs; i++)
3059 ionic_swap_queues(lif->txqcqs[i], tx_qcqs[i]);
3063 lif->nrxq_descs = qparam->nrxq_descs;
3064 for (i = 0; i < qparam->nxqs; i++)
3065 ionic_swap_queues(lif->rxqcqs[i], rx_qcqs[i]);
3068 /* if we need to change the interrupt layout, this is the time */
3069 if (qparam->intr_split != test_bit(IONIC_LIF_F_SPLIT_INTR, lif->state) ||
3070 qparam->nxqs != lif->nxqs) {
3071 if (qparam->intr_split) {
3072 set_bit(IONIC_LIF_F_SPLIT_INTR, lif->state);
3074 clear_bit(IONIC_LIF_F_SPLIT_INTR, lif->state);
3075 lif->tx_coalesce_usecs = lif->rx_coalesce_usecs;
3076 lif->tx_coalesce_hw = lif->rx_coalesce_hw;
3079 /* Clear existing interrupt assignments. We check for NULL here
3080 * because we're checking the whole array for potential qcqs, not
3081 * just those qcqs that have just been set up.
3083 for (i = 0; i < lif->ionic->ntxqs_per_lif; i++) {
3085 ionic_qcq_intr_free(lif, lif->txqcqs[i]);
3087 ionic_qcq_intr_free(lif, lif->rxqcqs[i]);
3090 /* re-assign the interrupts */
3091 for (i = 0; i < qparam->nxqs; i++) {
3092 lif->rxqcqs[i]->flags |= IONIC_QCQ_F_INTR;
3093 err = ionic_alloc_qcq_interrupt(lif, lif->rxqcqs[i]);
3094 ionic_intr_coal_init(lif->ionic->idev.intr_ctrl,
3095 lif->rxqcqs[i]->intr.index,
3096 lif->rx_coalesce_hw);
3098 if (qparam->intr_split) {
3099 lif->txqcqs[i]->flags |= IONIC_QCQ_F_INTR;
3100 err = ionic_alloc_qcq_interrupt(lif, lif->txqcqs[i]);
3101 ionic_intr_coal_init(lif->ionic->idev.intr_ctrl,
3102 lif->txqcqs[i]->intr.index,
3103 lif->tx_coalesce_hw);
3104 if (test_bit(IONIC_LIF_F_TX_DIM_INTR, lif->state))
3105 lif->txqcqs[i]->intr.dim_coal_hw = lif->tx_coalesce_hw;
3107 lif->txqcqs[i]->flags &= ~IONIC_QCQ_F_INTR;
3108 ionic_link_qcq_interrupts(lif->rxqcqs[i], lif->txqcqs[i]);
3113 /* now we can rework the debugfs mappings */
3115 for (i = 0; i < qparam->nxqs; i++) {
3116 ionic_debugfs_del_qcq(lif->txqcqs[i]);
3117 ionic_debugfs_add_qcq(lif, lif->txqcqs[i]);
3122 for (i = 0; i < qparam->nxqs; i++) {
3123 ionic_debugfs_del_qcq(lif->rxqcqs[i]);
3124 ionic_debugfs_add_qcq(lif, lif->rxqcqs[i]);
3128 swap(lif->nxqs, qparam->nxqs);
3129 swap(lif->rxq_features, qparam->rxq_features);
3131 err_out_reinit_unlock:
3132 /* re-init the queues, but don't lose an error code */
3134 ionic_start_queues_reconfig(lif);
3136 err = ionic_start_queues_reconfig(lif);
3139 /* free old allocs without cleaning intr */
3140 for (i = 0; i < qparam->nxqs; i++) {
3141 if (tx_qcqs && tx_qcqs[i]) {
3142 tx_qcqs[i]->flags &= ~IONIC_QCQ_F_INTR;
3143 ionic_qcq_free(lif, tx_qcqs[i]);
3144 devm_kfree(lif->ionic->dev, tx_qcqs[i]);
3147 if (rx_qcqs && rx_qcqs[i]) {
3148 rx_qcqs[i]->flags &= ~IONIC_QCQ_F_INTR;
3149 ionic_qcq_free(lif, rx_qcqs[i]);
3150 devm_kfree(lif->ionic->dev, rx_qcqs[i]);
3157 devm_kfree(lif->ionic->dev, rx_qcqs);
3161 devm_kfree(lif->ionic->dev, tx_qcqs);
3165 /* clean the unused dma and info allocations when new set is smaller
3166 * than the full array, but leave the qcq shells in place
3168 for (i = lif->nxqs; i < lif->ionic->ntxqs_per_lif; i++) {
3169 if (lif->txqcqs && lif->txqcqs[i]) {
3170 lif->txqcqs[i]->flags &= ~IONIC_QCQ_F_INTR;
3171 ionic_qcq_free(lif, lif->txqcqs[i]);
3174 if (lif->rxqcqs && lif->rxqcqs[i]) {
3175 lif->rxqcqs[i]->flags &= ~IONIC_QCQ_F_INTR;
3176 ionic_qcq_free(lif, lif->rxqcqs[i]);
3181 netdev_info(lif->netdev, "%s: failed %d\n", __func__, err);
3186 static int ionic_affinity_masks_alloc(struct ionic *ionic)
3188 cpumask_var_t *affinity_masks;
3189 int nintrs = ionic->nintrs;
3192 affinity_masks = kcalloc(nintrs, sizeof(cpumask_var_t), GFP_KERNEL);
3193 if (!affinity_masks)
3196 for (i = 0; i < nintrs; i++) {
3197 if (!zalloc_cpumask_var_node(&affinity_masks[i], GFP_KERNEL,
3198 dev_to_node(ionic->dev)))
3202 ionic->affinity_masks = affinity_masks;
3207 for (--i; i >= 0; i--)
3208 free_cpumask_var(affinity_masks[i]);
3209 kfree(affinity_masks);
3214 static void ionic_affinity_masks_free(struct ionic *ionic)
3218 for (i = 0; i < ionic->nintrs; i++)
3219 free_cpumask_var(ionic->affinity_masks[i]);
3220 kfree(ionic->affinity_masks);
3221 ionic->affinity_masks = NULL;
3224 int ionic_lif_alloc(struct ionic *ionic)
3226 struct device *dev = ionic->dev;
3227 union ionic_lif_identity *lid;
3228 struct net_device *netdev;
3229 struct ionic_lif *lif;
3233 lid = kzalloc(sizeof(*lid), GFP_KERNEL);
3237 netdev = alloc_etherdev_mqs(sizeof(*lif),
3238 ionic->ntxqs_per_lif, ionic->ntxqs_per_lif);
3240 dev_err(dev, "Cannot allocate netdev, aborting\n");
3242 goto err_out_free_lid;
3245 SET_NETDEV_DEV(netdev, dev);
3247 lif = netdev_priv(netdev);
3248 lif->netdev = netdev;
3251 netdev->netdev_ops = &ionic_netdev_ops;
3252 ionic_ethtool_set_ops(netdev);
3254 netdev->watchdog_timeo = 5 * HZ;
3255 netif_carrier_off(netdev);
3257 lif->identity = lid;
3258 lif->lif_type = IONIC_LIF_TYPE_CLASSIC;
3259 err = ionic_lif_identify(ionic, lif->lif_type, lif->identity);
3261 dev_err(ionic->dev, "Cannot identify type %d: %d\n",
3262 lif->lif_type, err);
3263 goto err_out_free_netdev;
3265 lif->netdev->min_mtu = max_t(unsigned int, ETH_MIN_MTU,
3266 le32_to_cpu(lif->identity->eth.min_frame_size));
3267 lif->netdev->max_mtu =
3268 le32_to_cpu(lif->identity->eth.max_frame_size) - ETH_HLEN - VLAN_HLEN;
3270 lif->neqs = ionic->neqs_per_lif;
3271 lif->nxqs = ionic->ntxqs_per_lif;
3275 if (is_kdump_kernel()) {
3276 lif->ntxq_descs = IONIC_MIN_TXRX_DESC;
3277 lif->nrxq_descs = IONIC_MIN_TXRX_DESC;
3279 lif->ntxq_descs = IONIC_DEF_TXRX_DESC;
3280 lif->nrxq_descs = IONIC_DEF_TXRX_DESC;
3283 /* Convert the default coalesce value to actual hw resolution */
3284 lif->rx_coalesce_usecs = IONIC_ITR_COAL_USEC_DEFAULT;
3285 lif->rx_coalesce_hw = ionic_coal_usec_to_hw(lif->ionic,
3286 lif->rx_coalesce_usecs);
3287 lif->tx_coalesce_usecs = lif->rx_coalesce_usecs;
3288 lif->tx_coalesce_hw = lif->rx_coalesce_hw;
3289 set_bit(IONIC_LIF_F_RX_DIM_INTR, lif->state);
3290 set_bit(IONIC_LIF_F_TX_DIM_INTR, lif->state);
3292 snprintf(lif->name, sizeof(lif->name), "lif%u", lif->index);
3294 mutex_init(&lif->queue_lock);
3295 mutex_init(&lif->config_lock);
3297 spin_lock_init(&lif->adminq_lock);
3299 spin_lock_init(&lif->deferred.lock);
3300 INIT_LIST_HEAD(&lif->deferred.list);
3301 INIT_WORK(&lif->deferred.work, ionic_lif_deferred_work);
3303 /* allocate lif info */
3304 lif->info_sz = ALIGN(sizeof(*lif->info), PAGE_SIZE);
3305 lif->info = dma_alloc_coherent(dev, lif->info_sz,
3306 &lif->info_pa, GFP_KERNEL);
3308 dev_err(dev, "Failed to allocate lif info, aborting\n");
3310 goto err_out_free_mutex;
3313 ionic_debugfs_add_lif(lif);
3315 err = ionic_affinity_masks_alloc(ionic);
3317 goto err_out_free_lif_info;
3319 /* allocate control queues and txrx queue arrays */
3320 ionic_lif_queue_identify(lif);
3321 err = ionic_qcqs_alloc(lif);
3323 goto err_out_free_affinity_masks;
3325 /* allocate rss indirection table */
3326 tbl_sz = le16_to_cpu(lif->ionic->ident.lif.eth.rss_ind_tbl_sz);
3327 lif->rss_ind_tbl_sz = sizeof(*lif->rss_ind_tbl) * tbl_sz;
3328 lif->rss_ind_tbl = dma_alloc_coherent(dev, lif->rss_ind_tbl_sz,
3329 &lif->rss_ind_tbl_pa,
3332 if (!lif->rss_ind_tbl) {
3334 dev_err(dev, "Failed to allocate rss indirection table, aborting\n");
3335 goto err_out_free_qcqs;
3337 netdev_rss_key_fill(lif->rss_hash_key, IONIC_RSS_HASH_KEY_SIZE);
3339 ionic_lif_alloc_phc(lif);
3344 ionic_qcqs_free(lif);
3345 err_out_free_affinity_masks:
3346 ionic_affinity_masks_free(lif->ionic);
3347 err_out_free_lif_info:
3348 dma_free_coherent(dev, lif->info_sz, lif->info, lif->info_pa);
3352 mutex_destroy(&lif->config_lock);
3353 mutex_destroy(&lif->queue_lock);
3354 err_out_free_netdev:
3355 free_netdev(lif->netdev);
3363 static void ionic_lif_reset(struct ionic_lif *lif)
3365 struct ionic_dev *idev = &lif->ionic->idev;
3367 if (!ionic_is_fw_running(idev))
3370 mutex_lock(&lif->ionic->dev_cmd_lock);
3371 ionic_dev_cmd_lif_reset(idev, lif->index);
3372 ionic_dev_cmd_wait(lif->ionic, DEVCMD_TIMEOUT);
3373 mutex_unlock(&lif->ionic->dev_cmd_lock);
3376 static void ionic_lif_handle_fw_down(struct ionic_lif *lif)
3378 struct ionic *ionic = lif->ionic;
3380 if (test_and_set_bit(IONIC_LIF_F_FW_RESET, lif->state))
3383 dev_info(ionic->dev, "FW Down: Stopping LIFs\n");
3385 netif_device_detach(lif->netdev);
3387 mutex_lock(&lif->queue_lock);
3388 if (test_bit(IONIC_LIF_F_UP, lif->state)) {
3389 dev_info(ionic->dev, "Surprise FW stop, stopping queues\n");
3390 ionic_stop_queues(lif);
3393 if (netif_running(lif->netdev)) {
3394 ionic_txrx_deinit(lif);
3395 ionic_txrx_free(lif);
3397 ionic_lif_deinit(lif);
3399 ionic_qcqs_free(lif);
3401 mutex_unlock(&lif->queue_lock);
3403 clear_bit(IONIC_LIF_F_FW_STOPPING, lif->state);
3404 dev_info(ionic->dev, "FW Down: LIFs stopped\n");
3407 int ionic_restart_lif(struct ionic_lif *lif)
3409 struct ionic *ionic = lif->ionic;
3412 mutex_lock(&lif->queue_lock);
3414 if (test_and_clear_bit(IONIC_LIF_F_BROKEN, lif->state))
3415 dev_info(ionic->dev, "FW Up: clearing broken state\n");
3417 err = ionic_qcqs_alloc(lif);
3421 err = ionic_lif_init(lif);
3425 ionic_vf_attr_replay(lif);
3427 if (lif->registered)
3428 ionic_lif_set_netdev_info(lif);
3430 ionic_rx_filter_replay(lif);
3432 if (netif_running(lif->netdev)) {
3433 err = ionic_txrx_alloc(lif);
3435 goto err_lifs_deinit;
3437 err = ionic_txrx_init(lif);
3442 mutex_unlock(&lif->queue_lock);
3444 clear_bit(IONIC_LIF_F_FW_RESET, lif->state);
3445 ionic_link_status_check_request(lif, CAN_SLEEP);
3446 netif_device_attach(lif->netdev);
3447 ionic_queue_doorbell_check(ionic, IONIC_NAPI_DEADLINE);
3452 ionic_txrx_free(lif);
3454 ionic_lif_deinit(lif);
3456 ionic_qcqs_free(lif);
3458 mutex_unlock(&lif->queue_lock);
3463 static void ionic_lif_handle_fw_up(struct ionic_lif *lif)
3465 struct ionic *ionic = lif->ionic;
3468 if (!test_bit(IONIC_LIF_F_FW_RESET, lif->state))
3471 dev_info(ionic->dev, "FW Up: restarting LIFs\n");
3473 /* This is a little different from what happens at
3474 * probe time because the LIF already exists so we
3475 * just need to reanimate it.
3477 ionic_init_devinfo(ionic);
3479 err = ionic_identify(ionic);
3482 err = ionic_port_identify(ionic);
3485 err = ionic_port_init(ionic);
3489 err = ionic_restart_lif(lif);
3493 dev_info(ionic->dev, "FW Up: LIFs restarted\n");
3495 /* restore the hardware timestamping queues */
3496 ionic_lif_hwstamp_replay(lif);
3501 dev_err(ionic->dev, "FW Up: LIFs restart failed - err %d\n", err);
3504 void ionic_lif_free(struct ionic_lif *lif)
3506 struct device *dev = lif->ionic->dev;
3508 ionic_lif_free_phc(lif);
3510 /* free rss indirection table */
3511 dma_free_coherent(dev, lif->rss_ind_tbl_sz, lif->rss_ind_tbl,
3512 lif->rss_ind_tbl_pa);
3513 lif->rss_ind_tbl = NULL;
3514 lif->rss_ind_tbl_pa = 0;
3517 ionic_qcqs_free(lif);
3518 if (!test_bit(IONIC_LIF_F_FW_RESET, lif->state))
3519 ionic_lif_reset(lif);
3521 ionic_affinity_masks_free(lif->ionic);
3524 kfree(lif->identity);
3525 dma_free_coherent(dev, lif->info_sz, lif->info, lif->info_pa);
3529 /* unmap doorbell page */
3530 ionic_bus_unmap_dbpage(lif->ionic, lif->kern_dbpage);
3531 lif->kern_dbpage = NULL;
3533 mutex_destroy(&lif->config_lock);
3534 mutex_destroy(&lif->queue_lock);
3536 /* free netdev & lif */
3537 ionic_debugfs_del_lif(lif);
3538 free_netdev(lif->netdev);
3541 void ionic_lif_deinit(struct ionic_lif *lif)
3543 if (!test_and_clear_bit(IONIC_LIF_F_INITED, lif->state))
3546 if (!test_bit(IONIC_LIF_F_FW_RESET, lif->state)) {
3547 cancel_work_sync(&lif->deferred.work);
3548 cancel_work_sync(&lif->tx_timeout_work);
3549 ionic_rx_filters_deinit(lif);
3550 if (lif->netdev->features & NETIF_F_RXHASH)
3551 ionic_lif_rss_deinit(lif);
3554 napi_disable(&lif->adminqcq->napi);
3555 ionic_lif_qcq_deinit(lif, lif->notifyqcq);
3556 ionic_lif_qcq_deinit(lif, lif->adminqcq);
3558 ionic_lif_reset(lif);
3561 static int ionic_lif_adminq_init(struct ionic_lif *lif)
3563 struct device *dev = lif->ionic->dev;
3564 struct ionic_q_init_comp comp;
3565 struct ionic_dev *idev;
3566 struct ionic_qcq *qcq;
3567 struct ionic_queue *q;
3570 idev = &lif->ionic->idev;
3571 qcq = lif->adminqcq;
3574 mutex_lock(&lif->ionic->dev_cmd_lock);
3575 ionic_dev_cmd_adminq_init(idev, qcq, lif->index, qcq->intr.index);
3576 err = ionic_dev_cmd_wait(lif->ionic, DEVCMD_TIMEOUT);
3577 ionic_dev_cmd_comp(idev, (union ionic_dev_cmd_comp *)&comp);
3578 mutex_unlock(&lif->ionic->dev_cmd_lock);
3580 netdev_err(lif->netdev, "adminq init failed %d\n", err);
3584 q->hw_type = comp.hw_type;
3585 q->hw_index = le32_to_cpu(comp.hw_index);
3586 q->dbval = IONIC_DBELL_QID(q->hw_index);
3588 dev_dbg(dev, "adminq->hw_type %d\n", q->hw_type);
3589 dev_dbg(dev, "adminq->hw_index %d\n", q->hw_index);
3591 q->dbell_deadline = IONIC_ADMIN_DOORBELL_DEADLINE;
3592 q->dbell_jiffies = jiffies;
3594 netif_napi_add(lif->netdev, &qcq->napi, ionic_adminq_napi);
3596 napi_enable(&qcq->napi);
3598 if (qcq->flags & IONIC_QCQ_F_INTR) {
3599 irq_set_affinity_hint(qcq->intr.vector,
3600 *qcq->intr.affinity_mask);
3601 ionic_intr_mask(idev->intr_ctrl, qcq->intr.index,
3602 IONIC_INTR_MASK_CLEAR);
3605 qcq->flags |= IONIC_QCQ_F_INITED;
3610 static int ionic_lif_notifyq_init(struct ionic_lif *lif)
3612 struct ionic_qcq *qcq = lif->notifyqcq;
3613 struct device *dev = lif->ionic->dev;
3614 struct ionic_queue *q = &qcq->q;
3617 struct ionic_admin_ctx ctx = {
3618 .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
3620 .opcode = IONIC_CMD_Q_INIT,
3621 .lif_index = cpu_to_le16(lif->index),
3623 .ver = lif->qtype_info[q->type].version,
3624 .index = cpu_to_le32(q->index),
3625 .flags = cpu_to_le16(IONIC_QINIT_F_IRQ |
3627 .intr_index = cpu_to_le16(lif->adminqcq->intr.index),
3628 .pid = cpu_to_le16(q->pid),
3629 .ring_size = ilog2(q->num_descs),
3630 .ring_base = cpu_to_le64(q->base_pa),
3634 dev_dbg(dev, "notifyq_init.pid %d\n", ctx.cmd.q_init.pid);
3635 dev_dbg(dev, "notifyq_init.index %d\n", ctx.cmd.q_init.index);
3636 dev_dbg(dev, "notifyq_init.ring_base 0x%llx\n", ctx.cmd.q_init.ring_base);
3637 dev_dbg(dev, "notifyq_init.ring_size %d\n", ctx.cmd.q_init.ring_size);
3639 err = ionic_adminq_post_wait(lif, &ctx);
3644 q->hw_type = ctx.comp.q_init.hw_type;
3645 q->hw_index = le32_to_cpu(ctx.comp.q_init.hw_index);
3646 q->dbval = IONIC_DBELL_QID(q->hw_index);
3648 dev_dbg(dev, "notifyq->hw_type %d\n", q->hw_type);
3649 dev_dbg(dev, "notifyq->hw_index %d\n", q->hw_index);
3651 /* preset the callback info */
3652 q->admin_info[0].ctx = lif;
3654 qcq->flags |= IONIC_QCQ_F_INITED;
3659 static int ionic_station_set(struct ionic_lif *lif)
3661 struct net_device *netdev = lif->netdev;
3662 struct ionic_admin_ctx ctx = {
3663 .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
3664 .cmd.lif_getattr = {
3665 .opcode = IONIC_CMD_LIF_GETATTR,
3666 .index = cpu_to_le16(lif->index),
3667 .attr = IONIC_LIF_ATTR_MAC,
3670 u8 mac_address[ETH_ALEN];
3671 struct sockaddr addr;
3674 err = ionic_adminq_post_wait(lif, &ctx);
3677 netdev_dbg(lif->netdev, "found initial MAC addr %pM\n",
3678 ctx.comp.lif_getattr.mac);
3679 ether_addr_copy(mac_address, ctx.comp.lif_getattr.mac);
3681 if (is_zero_ether_addr(mac_address)) {
3682 eth_hw_addr_random(netdev);
3683 netdev_dbg(netdev, "Random Mac generated: %pM\n", netdev->dev_addr);
3684 ether_addr_copy(mac_address, netdev->dev_addr);
3686 err = ionic_program_mac(lif, mac_address);
3691 netdev_dbg(netdev, "%s:SET/GET ATTR Mac are not same-due to old FW running\n",
3697 if (!is_zero_ether_addr(netdev->dev_addr)) {
3698 /* If the netdev mac is non-zero and doesn't match the default
3699 * device address, it was set by something earlier and we're
3700 * likely here again after a fw-upgrade reset. We need to be
3701 * sure the netdev mac is in our filter list.
3703 if (!ether_addr_equal(mac_address, netdev->dev_addr))
3704 ionic_lif_addr_add(lif, netdev->dev_addr);
3706 /* Update the netdev mac with the device's mac */
3707 ether_addr_copy(addr.sa_data, mac_address);
3708 addr.sa_family = AF_INET;
3709 err = eth_prepare_mac_addr_change(netdev, &addr);
3711 netdev_warn(lif->netdev, "ignoring bad MAC addr from NIC %pM - err %d\n",
3716 eth_commit_mac_addr_change(netdev, &addr);
3719 netdev_dbg(lif->netdev, "adding station MAC addr %pM\n",
3721 ionic_lif_addr_add(lif, netdev->dev_addr);
3726 int ionic_lif_init(struct ionic_lif *lif)
3728 struct ionic_dev *idev = &lif->ionic->idev;
3729 struct device *dev = lif->ionic->dev;
3730 struct ionic_lif_init_comp comp;
3734 mutex_lock(&lif->ionic->dev_cmd_lock);
3735 ionic_dev_cmd_lif_init(idev, lif->index, lif->info_pa);
3736 err = ionic_dev_cmd_wait(lif->ionic, DEVCMD_TIMEOUT);
3737 ionic_dev_cmd_comp(idev, (union ionic_dev_cmd_comp *)&comp);
3738 mutex_unlock(&lif->ionic->dev_cmd_lock);
3742 lif->hw_index = le16_to_cpu(comp.hw_index);
3744 /* now that we have the hw_index we can figure out our doorbell page */
3745 lif->dbid_count = le32_to_cpu(lif->ionic->ident.dev.ndbpgs_per_lif);
3746 if (!lif->dbid_count) {
3747 dev_err(dev, "No doorbell pages, aborting\n");
3752 dbpage_num = ionic_db_page_num(lif, lif->kern_pid);
3753 lif->kern_dbpage = ionic_bus_map_dbpage(lif->ionic, dbpage_num);
3754 if (!lif->kern_dbpage) {
3755 dev_err(dev, "Cannot map dbpage, aborting\n");
3759 err = ionic_lif_adminq_init(lif);
3761 goto err_out_adminq_deinit;
3763 if (lif->ionic->nnqs_per_lif) {
3764 err = ionic_lif_notifyq_init(lif);
3766 goto err_out_notifyq_deinit;
3769 if (test_bit(IONIC_LIF_F_FW_RESET, lif->state))
3770 err = ionic_set_nic_features(lif, lif->netdev->features);
3772 err = ionic_init_nic_features(lif);
3774 goto err_out_notifyq_deinit;
3776 if (!test_bit(IONIC_LIF_F_FW_RESET, lif->state)) {
3777 err = ionic_rx_filters_init(lif);
3779 goto err_out_notifyq_deinit;
3782 err = ionic_station_set(lif);
3784 goto err_out_notifyq_deinit;
3786 lif->rx_copybreak = IONIC_RX_COPYBREAK_DEFAULT;
3787 lif->doorbell_wa = ionic_doorbell_wa(lif->ionic);
3789 set_bit(IONIC_LIF_F_INITED, lif->state);
3791 INIT_WORK(&lif->tx_timeout_work, ionic_tx_timeout_work);
3795 err_out_notifyq_deinit:
3796 napi_disable(&lif->adminqcq->napi);
3797 ionic_lif_qcq_deinit(lif, lif->notifyqcq);
3798 err_out_adminq_deinit:
3799 ionic_lif_qcq_deinit(lif, lif->adminqcq);
3800 ionic_lif_reset(lif);
3801 ionic_bus_unmap_dbpage(lif->ionic, lif->kern_dbpage);
3802 lif->kern_dbpage = NULL;
3807 static void ionic_lif_notify_work(struct work_struct *ws)
3811 static void ionic_lif_set_netdev_info(struct ionic_lif *lif)
3813 struct ionic_admin_ctx ctx = {
3814 .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
3815 .cmd.lif_setattr = {
3816 .opcode = IONIC_CMD_LIF_SETATTR,
3817 .index = cpu_to_le16(lif->index),
3818 .attr = IONIC_LIF_ATTR_NAME,
3822 strscpy(ctx.cmd.lif_setattr.name, netdev_name(lif->netdev),
3823 sizeof(ctx.cmd.lif_setattr.name));
3825 ionic_adminq_post_wait(lif, &ctx);
3828 static struct ionic_lif *ionic_netdev_lif(struct net_device *netdev)
3830 if (!netdev || netdev->netdev_ops->ndo_start_xmit != ionic_start_xmit)
3833 return netdev_priv(netdev);
3836 static int ionic_lif_notify(struct notifier_block *nb,
3837 unsigned long event, void *info)
3839 struct net_device *ndev = netdev_notifier_info_to_dev(info);
3840 struct ionic *ionic = container_of(nb, struct ionic, nb);
3841 struct ionic_lif *lif = ionic_netdev_lif(ndev);
3843 if (!lif || lif->ionic != ionic)
3847 case NETDEV_CHANGENAME:
3848 ionic_lif_set_netdev_info(lif);
3855 int ionic_lif_register(struct ionic_lif *lif)
3859 ionic_lif_register_phc(lif);
3861 INIT_WORK(&lif->ionic->nb_work, ionic_lif_notify_work);
3863 lif->ionic->nb.notifier_call = ionic_lif_notify;
3865 err = register_netdevice_notifier(&lif->ionic->nb);
3867 lif->ionic->nb.notifier_call = NULL;
3869 /* only register LIF0 for now */
3870 err = register_netdev(lif->netdev);
3872 dev_err(lif->ionic->dev, "Cannot register net device: %d, aborting\n", err);
3873 ionic_lif_unregister(lif);
3877 ionic_link_status_check_request(lif, CAN_SLEEP);
3878 lif->registered = true;
3879 ionic_lif_set_netdev_info(lif);
3884 void ionic_lif_unregister(struct ionic_lif *lif)
3886 if (lif->ionic->nb.notifier_call) {
3887 unregister_netdevice_notifier(&lif->ionic->nb);
3888 cancel_work_sync(&lif->ionic->nb_work);
3889 lif->ionic->nb.notifier_call = NULL;
3892 if (lif->netdev->reg_state == NETREG_REGISTERED)
3893 unregister_netdev(lif->netdev);
3895 ionic_lif_unregister_phc(lif);
3897 lif->registered = false;
3900 static void ionic_lif_queue_identify(struct ionic_lif *lif)
3902 union ionic_q_identity __iomem *q_ident;
3903 struct ionic *ionic = lif->ionic;
3904 struct ionic_dev *idev;
3909 idev = &lif->ionic->idev;
3910 q_ident = (union ionic_q_identity __iomem *)&idev->dev_cmd_regs->data;
3912 for (qtype = 0; qtype < ARRAY_SIZE(ionic_qtype_versions); qtype++) {
3913 struct ionic_qtype_info *qti = &lif->qtype_info[qtype];
3915 /* filter out the ones we know about */
3917 case IONIC_QTYPE_ADMINQ:
3918 case IONIC_QTYPE_NOTIFYQ:
3919 case IONIC_QTYPE_RXQ:
3920 case IONIC_QTYPE_TXQ:
3926 memset(qti, 0, sizeof(*qti));
3928 mutex_lock(&ionic->dev_cmd_lock);
3929 ionic_dev_cmd_queue_identify(idev, lif->lif_type, qtype,
3930 ionic_qtype_versions[qtype]);
3931 err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
3933 qti->version = readb(&q_ident->version);
3934 qti->supported = readb(&q_ident->supported);
3935 qti->features = readq(&q_ident->features);
3936 qti->desc_sz = readw(&q_ident->desc_sz);
3937 qti->comp_sz = readw(&q_ident->comp_sz);
3938 qti->sg_desc_sz = readw(&q_ident->sg_desc_sz);
3939 qti->max_sg_elems = readw(&q_ident->max_sg_elems);
3940 qti->sg_desc_stride = readw(&q_ident->sg_desc_stride);
3942 mutex_unlock(&ionic->dev_cmd_lock);
3944 if (err == -EINVAL) {
3945 dev_err(ionic->dev, "qtype %d not supported\n", qtype);
3947 } else if (err == -EIO) {
3948 dev_err(ionic->dev, "q_ident failed, not supported on older FW\n");
3951 dev_err(ionic->dev, "q_ident failed, qtype %d: %d\n",
3956 dev_dbg(ionic->dev, " qtype[%d].version = %d\n",
3957 qtype, qti->version);
3958 dev_dbg(ionic->dev, " qtype[%d].supported = 0x%02x\n",
3959 qtype, qti->supported);
3960 dev_dbg(ionic->dev, " qtype[%d].features = 0x%04llx\n",
3961 qtype, qti->features);
3962 dev_dbg(ionic->dev, " qtype[%d].desc_sz = %d\n",
3963 qtype, qti->desc_sz);
3964 dev_dbg(ionic->dev, " qtype[%d].comp_sz = %d\n",
3965 qtype, qti->comp_sz);
3966 dev_dbg(ionic->dev, " qtype[%d].sg_desc_sz = %d\n",
3967 qtype, qti->sg_desc_sz);
3968 dev_dbg(ionic->dev, " qtype[%d].max_sg_elems = %d\n",
3969 qtype, qti->max_sg_elems);
3970 dev_dbg(ionic->dev, " qtype[%d].sg_desc_stride = %d\n",
3971 qtype, qti->sg_desc_stride);
3973 if (qtype == IONIC_QTYPE_TXQ)
3974 max_frags = IONIC_TX_MAX_FRAGS;
3975 else if (qtype == IONIC_QTYPE_RXQ)
3976 max_frags = IONIC_RX_MAX_FRAGS;
3980 qti->max_sg_elems = min_t(u16, max_frags - 1, MAX_SKB_FRAGS);
3981 dev_dbg(ionic->dev, "qtype %d max_sg_elems %d\n",
3982 qtype, qti->max_sg_elems);
3986 int ionic_lif_identify(struct ionic *ionic, u8 lif_type,
3987 union ionic_lif_identity *lid)
3989 struct ionic_dev *idev = &ionic->idev;
3993 sz = min(sizeof(*lid), sizeof(idev->dev_cmd_regs->data));
3995 mutex_lock(&ionic->dev_cmd_lock);
3996 ionic_dev_cmd_lif_identify(idev, lif_type, IONIC_IDENTITY_VERSION_1);
3997 err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
3998 memcpy_fromio(lid, &idev->dev_cmd_regs->data, sz);
3999 mutex_unlock(&ionic->dev_cmd_lock);
4003 dev_dbg(ionic->dev, "capabilities 0x%llx\n",
4004 le64_to_cpu(lid->capabilities));
4006 dev_dbg(ionic->dev, "eth.max_ucast_filters %d\n",
4007 le32_to_cpu(lid->eth.max_ucast_filters));
4008 dev_dbg(ionic->dev, "eth.max_mcast_filters %d\n",
4009 le32_to_cpu(lid->eth.max_mcast_filters));
4010 dev_dbg(ionic->dev, "eth.features 0x%llx\n",
4011 le64_to_cpu(lid->eth.config.features));
4012 dev_dbg(ionic->dev, "eth.queue_count[IONIC_QTYPE_ADMINQ] %d\n",
4013 le32_to_cpu(lid->eth.config.queue_count[IONIC_QTYPE_ADMINQ]));
4014 dev_dbg(ionic->dev, "eth.queue_count[IONIC_QTYPE_NOTIFYQ] %d\n",
4015 le32_to_cpu(lid->eth.config.queue_count[IONIC_QTYPE_NOTIFYQ]));
4016 dev_dbg(ionic->dev, "eth.queue_count[IONIC_QTYPE_RXQ] %d\n",
4017 le32_to_cpu(lid->eth.config.queue_count[IONIC_QTYPE_RXQ]));
4018 dev_dbg(ionic->dev, "eth.queue_count[IONIC_QTYPE_TXQ] %d\n",
4019 le32_to_cpu(lid->eth.config.queue_count[IONIC_QTYPE_TXQ]));
4020 dev_dbg(ionic->dev, "eth.config.name %s\n", lid->eth.config.name);
4021 dev_dbg(ionic->dev, "eth.config.mac %pM\n", lid->eth.config.mac);
4022 dev_dbg(ionic->dev, "eth.config.mtu %d\n",
4023 le32_to_cpu(lid->eth.config.mtu));
4028 int ionic_lif_size(struct ionic *ionic)
4030 struct ionic_identity *ident = &ionic->ident;
4031 unsigned int nintrs, dev_nintrs;
4032 union ionic_lif_config *lc;
4033 unsigned int ntxqs_per_lif;
4034 unsigned int nrxqs_per_lif;
4035 unsigned int neqs_per_lif;
4036 unsigned int nnqs_per_lif;
4037 unsigned int nxqs, neqs;
4038 unsigned int min_intrs;
4041 /* retrieve basic values from FW */
4042 lc = &ident->lif.eth.config;
4043 dev_nintrs = le32_to_cpu(ident->dev.nintrs);
4044 neqs_per_lif = le32_to_cpu(ident->lif.rdma.eq_qtype.qid_count);
4045 nnqs_per_lif = le32_to_cpu(lc->queue_count[IONIC_QTYPE_NOTIFYQ]);
4046 ntxqs_per_lif = le32_to_cpu(lc->queue_count[IONIC_QTYPE_TXQ]);
4047 nrxqs_per_lif = le32_to_cpu(lc->queue_count[IONIC_QTYPE_RXQ]);
4049 /* limit values to play nice with kdump */
4050 if (is_kdump_kernel()) {
4058 /* reserve last queue id for hardware timestamping */
4059 if (lc->features & cpu_to_le64(IONIC_ETH_HW_TIMESTAMP)) {
4060 if (ntxqs_per_lif <= 1 || nrxqs_per_lif <= 1) {
4061 lc->features &= cpu_to_le64(~IONIC_ETH_HW_TIMESTAMP);
4068 nxqs = min(ntxqs_per_lif, nrxqs_per_lif);
4069 nxqs = min(nxqs, num_online_cpus());
4070 neqs = min(neqs_per_lif, num_online_cpus());
4074 * 1 for master lif adminq/notifyq
4075 * 1 for each CPU for master lif TxRx queue pairs
4076 * whatever's left is for RDMA queues
4078 nintrs = 1 + nxqs + neqs;
4079 min_intrs = 2; /* adminq + 1 TxRx queue pair */
4081 if (nintrs > dev_nintrs)
4084 err = ionic_bus_alloc_irq_vectors(ionic, nintrs);
4085 if (err < 0 && err != -ENOSPC) {
4086 dev_err(ionic->dev, "Can't get intrs from OS: %d\n", err);
4092 if (err != nintrs) {
4093 ionic_bus_free_irq_vectors(ionic);
4097 ionic->nnqs_per_lif = nnqs_per_lif;
4098 ionic->neqs_per_lif = neqs;
4099 ionic->ntxqs_per_lif = nxqs;
4100 ionic->nrxqs_per_lif = nxqs;
4101 ionic->nintrs = nintrs;
4103 ionic_debugfs_add_sizes(ionic);
4108 if (nnqs_per_lif > 1) {
4120 dev_err(ionic->dev, "Can't get minimum %d intrs from OS\n", min_intrs);