xref: /linux/drivers/net/ethernet/pensando/ionic/ionic_lif.c (revision 3277e605ac01fd11d0a7c6c68c617547ba66c87f)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2017 - 2019 Pensando Systems, Inc */
3 
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>
17 
18 #include "ionic.h"
19 #include "ionic_bus.h"
20 #include "ionic_dev.h"
21 #include "ionic_lif.h"
22 #include "ionic_aux.h"
23 #include "ionic_txrx.h"
24 #include "ionic_ethtool.h"
25 #include "ionic_debugfs.h"
26 
27 /* queuetype support level */
28 static const u8 ionic_qtype_versions[IONIC_QTYPE_MAX] = {
29 	[IONIC_QTYPE_ADMINQ]  = 0,   /* 0 = Base version with CQ support */
30 	[IONIC_QTYPE_NOTIFYQ] = 0,   /* 0 = Base version */
31 	[IONIC_QTYPE_RXQ]     = 2,   /* 0 = Base version with CQ+SG support
32 				      * 2 =       ... with CMB rings
33 				      */
34 	[IONIC_QTYPE_TXQ]     = 3,   /* 0 = Base version with CQ+SG support
35 				      * 1 =       ... with Tx SG version 1
36 				      * 3 =       ... with CMB rings
37 				      */
38 };
39 
40 static void ionic_link_status_check(struct ionic_lif *lif);
41 static void ionic_lif_handle_fw_down(struct ionic_lif *lif);
42 static void ionic_lif_handle_fw_up(struct ionic_lif *lif);
43 static void ionic_lif_set_netdev_info(struct ionic_lif *lif);
44 
45 static void ionic_txrx_deinit(struct ionic_lif *lif);
46 static int ionic_txrx_init(struct ionic_lif *lif);
47 static int ionic_start_queues(struct ionic_lif *lif);
48 static void ionic_stop_queues(struct ionic_lif *lif);
49 static void ionic_lif_queue_identify(struct ionic_lif *lif);
50 
51 static void ionic_xdp_rxqs_prog_update(struct ionic_lif *lif);
52 static void ionic_unregister_rxq_info(struct ionic_queue *q);
53 static int ionic_register_rxq_info(struct ionic_queue *q, unsigned int napi_id);
54 
55 static void ionic_dim_work(struct work_struct *work)
56 {
57 	struct dim *dim = container_of(work, struct dim, work);
58 	struct dim_cq_moder cur_moder;
59 	struct ionic_intr_info *intr;
60 	struct ionic_qcq *qcq;
61 	struct ionic_lif *lif;
62 	struct ionic_queue *q;
63 	u32 new_coal;
64 
65 	qcq = container_of(dim, struct ionic_qcq, dim);
66 	q = &qcq->q;
67 	if (q->type == IONIC_QTYPE_RXQ)
68 		cur_moder = net_dim_get_rx_moderation(dim->mode, dim->profile_ix);
69 	else
70 		cur_moder = net_dim_get_tx_moderation(dim->mode, dim->profile_ix);
71 	lif = q->lif;
72 	new_coal = ionic_coal_usec_to_hw(lif->ionic, cur_moder.usec);
73 	new_coal = new_coal ? new_coal : 1;
74 
75 	intr = &qcq->intr;
76 	if (intr->dim_coal_hw != new_coal) {
77 		intr->dim_coal_hw = new_coal;
78 
79 		ionic_intr_coal_init(lif->ionic->idev.intr_ctrl,
80 				     intr->index, intr->dim_coal_hw);
81 	}
82 
83 	dim->state = DIM_START_MEASURE;
84 }
85 
86 static void ionic_lif_deferred_work(struct work_struct *work)
87 {
88 	struct ionic_lif *lif = container_of(work, struct ionic_lif, deferred.work);
89 	struct ionic_deferred *def = &lif->deferred;
90 	struct ionic_deferred_work *w = NULL;
91 
92 	do {
93 		spin_lock_bh(&def->lock);
94 		if (!list_empty(&def->list)) {
95 			w = list_first_entry(&def->list,
96 					     struct ionic_deferred_work, list);
97 			list_del(&w->list);
98 		}
99 		spin_unlock_bh(&def->lock);
100 
101 		if (!w)
102 			break;
103 
104 		switch (w->type) {
105 		case IONIC_DW_TYPE_RX_MODE:
106 			ionic_lif_rx_mode(lif);
107 			break;
108 		case IONIC_DW_TYPE_LINK_STATUS:
109 			ionic_link_status_check(lif);
110 			break;
111 		case IONIC_DW_TYPE_LIF_RESET:
112 			if (w->fw_status) {
113 				ionic_lif_handle_fw_up(lif);
114 			} else {
115 				ionic_lif_handle_fw_down(lif);
116 
117 				/* Fire off another watchdog to see
118 				 * if the FW is already back rather than
119 				 * waiting another whole cycle
120 				 */
121 				mod_timer(&lif->ionic->watchdog_timer, jiffies + 1);
122 			}
123 			break;
124 		default:
125 			break;
126 		}
127 		kfree(w);
128 		w = NULL;
129 	} while (true);
130 }
131 
132 void ionic_lif_deferred_enqueue(struct ionic_lif *lif,
133 				struct ionic_deferred_work *work)
134 {
135 	spin_lock_bh(&lif->deferred.lock);
136 	list_add_tail(&work->list, &lif->deferred.list);
137 	spin_unlock_bh(&lif->deferred.lock);
138 	queue_work(lif->ionic->wq, &lif->deferred.work);
139 }
140 
141 static void ionic_link_status_check(struct ionic_lif *lif)
142 {
143 	struct ionic_dev *idev = &lif->ionic->idev;
144 	struct net_device *netdev = lif->netdev;
145 	u16 link_status;
146 	bool link_up;
147 
148 	if (!test_bit(IONIC_LIF_F_LINK_CHECK_REQUESTED, lif->state))
149 		return;
150 
151 	/* Don't put carrier back up if we're in a broken state */
152 	if (test_bit(IONIC_LIF_F_BROKEN, lif->state)) {
153 		clear_bit(IONIC_LIF_F_LINK_CHECK_REQUESTED, lif->state);
154 		return;
155 	}
156 
157 	ionic_reset_link_down_count(idev);
158 
159 	link_status = le16_to_cpu(lif->info->status.link_status);
160 	link_up = link_status == IONIC_PORT_OPER_STATUS_UP;
161 
162 	if (link_up) {
163 		int err = 0;
164 
165 		if (netdev->flags & IFF_UP && netif_running(netdev)) {
166 			mutex_lock(&lif->queue_lock);
167 			err = ionic_start_queues(lif);
168 			if (err && err != -EBUSY) {
169 				netdev_err(netdev,
170 					   "Failed to start queues: %d\n", err);
171 				set_bit(IONIC_LIF_F_BROKEN, lif->state);
172 				netif_carrier_off(lif->netdev);
173 			}
174 			mutex_unlock(&lif->queue_lock);
175 		}
176 
177 		if (!err && !netif_carrier_ok(netdev)) {
178 			ionic_port_identify(lif->ionic);
179 			netdev_info(netdev, "Link up - %d Gbps\n",
180 				    le32_to_cpu(lif->info->status.link_speed) / 1000);
181 			netif_carrier_on(netdev);
182 		}
183 	} else {
184 		if (netif_carrier_ok(netdev)) {
185 			netdev_info(netdev, "Link down\n");
186 			netif_carrier_off(netdev);
187 		}
188 
189 		if (netdev->flags & IFF_UP && netif_running(netdev)) {
190 			mutex_lock(&lif->queue_lock);
191 			ionic_stop_queues(lif);
192 			mutex_unlock(&lif->queue_lock);
193 		}
194 	}
195 
196 	clear_bit(IONIC_LIF_F_LINK_CHECK_REQUESTED, lif->state);
197 }
198 
199 void ionic_link_status_check_request(struct ionic_lif *lif, bool can_sleep)
200 {
201 	struct ionic_deferred_work *work;
202 
203 	/* we only need one request outstanding at a time */
204 	if (test_and_set_bit(IONIC_LIF_F_LINK_CHECK_REQUESTED, lif->state))
205 		return;
206 
207 	if (!can_sleep) {
208 		work = kzalloc_obj(*work, GFP_ATOMIC);
209 		if (!work) {
210 			clear_bit(IONIC_LIF_F_LINK_CHECK_REQUESTED, lif->state);
211 			return;
212 		}
213 
214 		work->type = IONIC_DW_TYPE_LINK_STATUS;
215 		ionic_lif_deferred_enqueue(lif, work);
216 	} else {
217 		ionic_link_status_check(lif);
218 	}
219 }
220 
221 static irqreturn_t ionic_isr(int irq, void *data)
222 {
223 	struct napi_struct *napi = data;
224 
225 	napi_schedule_irqoff(napi);
226 
227 	return IRQ_HANDLED;
228 }
229 
230 static int ionic_request_irq(struct ionic_lif *lif, struct ionic_qcq *qcq)
231 {
232 	struct ionic_intr_info *intr = &qcq->intr;
233 	struct device *dev = lif->ionic->dev;
234 	struct ionic_queue *q = &qcq->q;
235 	const char *name;
236 
237 	if (lif->registered)
238 		name = netdev_name(lif->netdev);
239 	else
240 		name = dev_name(dev);
241 
242 	snprintf(intr->name, sizeof(intr->name),
243 		 "%.5s-%.16s-%.8s", IONIC_DRV_NAME, name, q->name);
244 
245 	return devm_request_irq(dev, intr->vector, ionic_isr,
246 				0, intr->name, &qcq->napi);
247 }
248 
249 int ionic_intr_alloc(struct ionic_lif *lif, struct ionic_intr_info *intr)
250 {
251 	struct ionic *ionic = lif->ionic;
252 	int index, err;
253 
254 	index = find_first_zero_bit(ionic->intrs, ionic->nintrs);
255 	if (index == ionic->nintrs)
256 		return -ENOSPC;
257 
258 	set_bit(index, ionic->intrs);
259 	ionic_intr_init(&ionic->idev, intr, index);
260 
261 	err = ionic_bus_get_irq(ionic, intr->index);
262 	if (err < 0) {
263 		clear_bit(index, ionic->intrs);
264 		return err;
265 	}
266 
267 	intr->vector = err;
268 
269 	return 0;
270 }
271 EXPORT_SYMBOL_NS(ionic_intr_alloc, "NET_IONIC");
272 
273 void ionic_intr_free(struct ionic_lif *lif, int index)
274 {
275 	if (index != IONIC_INTR_INDEX_NOT_ASSIGNED && index < lif->ionic->nintrs)
276 		clear_bit(index, lif->ionic->intrs);
277 }
278 EXPORT_SYMBOL_NS(ionic_intr_free, "NET_IONIC");
279 
280 static void ionic_irq_aff_notify(struct irq_affinity_notify *notify,
281 				 const cpumask_t *mask)
282 {
283 	struct ionic_intr_info *intr = container_of(notify, struct ionic_intr_info, aff_notify);
284 
285 	cpumask_copy(*intr->affinity_mask, mask);
286 }
287 
288 static void ionic_irq_aff_release(struct kref __always_unused *ref)
289 {
290 }
291 
292 static int ionic_qcq_enable(struct ionic_qcq *qcq)
293 {
294 	struct ionic_queue *q = &qcq->q;
295 	struct ionic_lif *lif = q->lif;
296 	struct ionic_dev *idev;
297 	struct device *dev;
298 
299 	struct ionic_admin_ctx ctx = {
300 		.work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
301 		.cmd.q_control = {
302 			.opcode = IONIC_CMD_Q_CONTROL,
303 			.lif_index = cpu_to_le16(lif->index),
304 			.type = q->type,
305 			.index = cpu_to_le32(q->index),
306 			.oper = IONIC_Q_ENABLE,
307 		},
308 	};
309 	int ret;
310 
311 	idev = &lif->ionic->idev;
312 	dev = lif->ionic->dev;
313 
314 	dev_dbg(dev, "q_enable.index %d q_enable.qtype %d\n",
315 		ctx.cmd.q_control.index, ctx.cmd.q_control.type);
316 
317 	if (qcq->flags & IONIC_QCQ_F_INTR)
318 		ionic_intr_clean(idev->intr_ctrl, qcq->intr.index);
319 
320 	ret = ionic_adminq_post_wait(lif, &ctx);
321 	if (ret)
322 		return ret;
323 
324 	if (qcq->flags & IONIC_QCQ_F_INTR) {
325 		napi_enable(&qcq->napi);
326 		irq_set_affinity_notifier(qcq->intr.vector,
327 					  &qcq->intr.aff_notify);
328 		irq_set_affinity_hint(qcq->intr.vector,
329 				      *qcq->intr.affinity_mask);
330 		ionic_intr_mask(idev->intr_ctrl, qcq->intr.index,
331 				IONIC_INTR_MASK_CLEAR);
332 	}
333 
334 	return 0;
335 }
336 
337 static int ionic_qcq_disable(struct ionic_lif *lif, struct ionic_qcq *qcq, int fw_err)
338 {
339 	struct ionic_queue *q;
340 
341 	struct ionic_admin_ctx ctx = {
342 		.work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
343 		.cmd.q_control = {
344 			.opcode = IONIC_CMD_Q_CONTROL,
345 			.oper = IONIC_Q_DISABLE,
346 		},
347 	};
348 
349 	if (!qcq) {
350 		netdev_err(lif->netdev, "%s: bad qcq\n", __func__);
351 		return -ENXIO;
352 	}
353 
354 	q = &qcq->q;
355 
356 	if (qcq->flags & IONIC_QCQ_F_INTR) {
357 		struct ionic_dev *idev = &lif->ionic->idev;
358 
359 		if (lif->doorbell_wa)
360 			cancel_work_sync(&qcq->doorbell_napi_work);
361 		cancel_work_sync(&qcq->dim.work);
362 		ionic_intr_mask(idev->intr_ctrl, qcq->intr.index,
363 				IONIC_INTR_MASK_SET);
364 		synchronize_irq(qcq->intr.vector);
365 		irq_set_affinity_notifier(qcq->intr.vector, NULL);
366 		irq_set_affinity_hint(qcq->intr.vector, NULL);
367 		napi_disable(&qcq->napi);
368 	}
369 
370 	/* If there was a previous fw communcation error, don't bother with
371 	 * sending the adminq command and just return the same error value.
372 	 */
373 	if (fw_err == -ETIMEDOUT || fw_err == -ENXIO)
374 		return fw_err;
375 
376 	ctx.cmd.q_control.lif_index = cpu_to_le16(lif->index);
377 	ctx.cmd.q_control.type = q->type;
378 	ctx.cmd.q_control.index = cpu_to_le32(q->index);
379 	dev_dbg(lif->ionic->dev, "q_disable.index %d q_disable.qtype %d\n",
380 		ctx.cmd.q_control.index, ctx.cmd.q_control.type);
381 
382 	return ionic_adminq_post_wait(lif, &ctx);
383 }
384 
385 static void ionic_lif_qcq_deinit(struct ionic_lif *lif, struct ionic_qcq *qcq)
386 {
387 	struct ionic_dev *idev = &lif->ionic->idev;
388 
389 	if (!qcq)
390 		return;
391 
392 	if (!(qcq->flags & IONIC_QCQ_F_INITED))
393 		return;
394 
395 	ionic_unregister_rxq_info(&qcq->q);
396 	if (qcq->flags & IONIC_QCQ_F_INTR) {
397 		ionic_intr_mask(idev->intr_ctrl, qcq->intr.index,
398 				IONIC_INTR_MASK_SET);
399 		netif_napi_del(&qcq->napi);
400 	}
401 
402 	qcq->flags &= ~IONIC_QCQ_F_INITED;
403 }
404 
405 static void ionic_qcq_intr_free(struct ionic_lif *lif, struct ionic_qcq *qcq)
406 {
407 	if (!(qcq->flags & IONIC_QCQ_F_INTR) || qcq->intr.vector == 0)
408 		return;
409 
410 	irq_set_affinity_hint(qcq->intr.vector, NULL);
411 	devm_free_irq(lif->ionic->dev, qcq->intr.vector, &qcq->napi);
412 	qcq->intr.vector = 0;
413 	ionic_intr_free(lif, qcq->intr.index);
414 	qcq->intr.index = IONIC_INTR_INDEX_NOT_ASSIGNED;
415 }
416 
417 static void ionic_qcq_free(struct ionic_lif *lif, struct ionic_qcq *qcq)
418 {
419 	struct device *dev = lif->ionic->dev;
420 
421 	if (!qcq)
422 		return;
423 
424 	ionic_debugfs_del_qcq(qcq);
425 
426 	if (qcq->q_base) {
427 		dma_free_coherent(dev, qcq->q_size, qcq->q_base, qcq->q_base_pa);
428 		qcq->q_base = NULL;
429 		qcq->q_base_pa = 0;
430 	}
431 
432 	if (qcq->cmb_q_base) {
433 		iounmap(qcq->cmb_q_base);
434 		ionic_put_cmb(lif, qcq->cmb_pgid, qcq->cmb_order);
435 		qcq->cmb_pgid = 0;
436 		qcq->cmb_order = 0;
437 		qcq->cmb_q_base = NULL;
438 		qcq->cmb_q_base_pa = 0;
439 	}
440 
441 	if (qcq->cq_base) {
442 		dma_free_coherent(dev, qcq->cq_size, qcq->cq_base, qcq->cq_base_pa);
443 		qcq->cq_base = NULL;
444 		qcq->cq_base_pa = 0;
445 	}
446 
447 	if (qcq->sg_base) {
448 		dma_free_coherent(dev, qcq->sg_size, qcq->sg_base, qcq->sg_base_pa);
449 		qcq->sg_base = NULL;
450 		qcq->sg_base_pa = 0;
451 	}
452 
453 	page_pool_destroy(qcq->q.page_pool);
454 	qcq->q.page_pool = NULL;
455 
456 	ionic_qcq_intr_free(lif, qcq);
457 	vfree(qcq->q.info);
458 	qcq->q.info = NULL;
459 }
460 
461 void ionic_qcqs_free(struct ionic_lif *lif)
462 {
463 	struct device *dev = lif->ionic->dev;
464 	struct ionic_qcq *adminqcq;
465 	unsigned long irqflags;
466 
467 	if (lif->notifyqcq) {
468 		ionic_qcq_free(lif, lif->notifyqcq);
469 		devm_kfree(dev, lif->notifyqcq);
470 		lif->notifyqcq = NULL;
471 	}
472 
473 	if (lif->adminqcq) {
474 		spin_lock_irqsave(&lif->adminq_lock, irqflags);
475 		adminqcq = READ_ONCE(lif->adminqcq);
476 		lif->adminqcq = NULL;
477 		spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
478 		if (adminqcq) {
479 			ionic_qcq_free(lif, adminqcq);
480 			devm_kfree(dev, adminqcq);
481 		}
482 	}
483 
484 	if (lif->rxqcqs) {
485 		devm_kfree(dev, lif->rxqstats);
486 		lif->rxqstats = NULL;
487 		devm_kfree(dev, lif->rxqcqs);
488 		lif->rxqcqs = NULL;
489 	}
490 
491 	if (lif->txqcqs) {
492 		devm_kfree(dev, lif->txqstats);
493 		lif->txqstats = NULL;
494 		devm_kfree(dev, lif->txqcqs);
495 		lif->txqcqs = NULL;
496 	}
497 }
498 
499 static void ionic_link_qcq_interrupts(struct ionic_qcq *src_qcq,
500 				      struct ionic_qcq *n_qcq)
501 {
502 	n_qcq->intr.vector = src_qcq->intr.vector;
503 	n_qcq->intr.index = src_qcq->intr.index;
504 }
505 
506 static int ionic_alloc_qcq_interrupt(struct ionic_lif *lif, struct ionic_qcq *qcq)
507 {
508 	cpumask_var_t *affinity_mask;
509 	int err;
510 
511 	if (!(qcq->flags & IONIC_QCQ_F_INTR)) {
512 		qcq->intr.index = IONIC_INTR_INDEX_NOT_ASSIGNED;
513 		return 0;
514 	}
515 
516 	err = ionic_intr_alloc(lif, &qcq->intr);
517 	if (err) {
518 		netdev_warn(lif->netdev, "no intr for %s: %d\n",
519 			    qcq->q.name, err);
520 		goto err_out;
521 	}
522 
523 	ionic_intr_mask_assert(lif->ionic->idev.intr_ctrl, qcq->intr.index,
524 			       IONIC_INTR_MASK_SET);
525 
526 	err = ionic_request_irq(lif, qcq);
527 	if (err) {
528 		netdev_warn(lif->netdev, "irq request failed %d\n", err);
529 		goto err_out_free_intr;
530 	}
531 
532 	/* try to get the irq on the local numa node first */
533 	affinity_mask = &lif->ionic->affinity_masks[qcq->intr.index];
534 	if (cpumask_empty(*affinity_mask)) {
535 		unsigned int cpu;
536 
537 		cpu = cpumask_local_spread(qcq->intr.index,
538 					   dev_to_node(lif->ionic->dev));
539 		if (cpu != -1)
540 			cpumask_set_cpu(cpu, *affinity_mask);
541 	}
542 
543 	qcq->intr.affinity_mask = affinity_mask;
544 	qcq->intr.aff_notify.notify = ionic_irq_aff_notify;
545 	qcq->intr.aff_notify.release = ionic_irq_aff_release;
546 
547 	netdev_dbg(lif->netdev, "%s: Interrupt index %d\n", qcq->q.name, qcq->intr.index);
548 	return 0;
549 
550 err_out_free_intr:
551 	ionic_intr_free(lif, qcq->intr.index);
552 err_out:
553 	return err;
554 }
555 
556 static int ionic_qcq_alloc(struct ionic_lif *lif, unsigned int type,
557 			   unsigned int index,
558 			   const char *name, unsigned int flags,
559 			   unsigned int num_descs, unsigned int desc_size,
560 			   unsigned int cq_desc_size,
561 			   unsigned int sg_desc_size,
562 			   unsigned int desc_info_size,
563 			   unsigned int pid, struct bpf_prog *xdp_prog,
564 			   struct ionic_qcq **qcq)
565 {
566 	struct ionic_dev *idev = &lif->ionic->idev;
567 	struct device *dev = lif->ionic->dev;
568 	struct ionic_qcq *new;
569 	int err;
570 
571 	*qcq = NULL;
572 
573 	new = devm_kzalloc(dev, sizeof(*new), GFP_KERNEL);
574 	if (!new) {
575 		netdev_err(lif->netdev, "Cannot allocate queue structure\n");
576 		err = -ENOMEM;
577 		goto err_out;
578 	}
579 
580 	new->q.dev = dev;
581 	new->flags = flags;
582 
583 	new->q.info = vcalloc(num_descs, desc_info_size);
584 	if (!new->q.info) {
585 		netdev_err(lif->netdev, "Cannot allocate queue info\n");
586 		err = -ENOMEM;
587 		goto err_out_free_qcq;
588 	}
589 
590 	if (type == IONIC_QTYPE_RXQ) {
591 		struct page_pool_params pp_params = {
592 			.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV,
593 			.order = 0,
594 			.pool_size = num_descs,
595 			.nid = NUMA_NO_NODE,
596 			.dev = lif->ionic->dev,
597 			.napi = &new->napi,
598 			.dma_dir = DMA_FROM_DEVICE,
599 			.max_len = PAGE_SIZE,
600 			.netdev = lif->netdev,
601 		};
602 
603 		if (xdp_prog)
604 			pp_params.dma_dir = DMA_BIDIRECTIONAL;
605 
606 		new->q.page_pool = page_pool_create(&pp_params);
607 		if (IS_ERR(new->q.page_pool)) {
608 			netdev_err(lif->netdev, "Cannot create page_pool\n");
609 			err = PTR_ERR(new->q.page_pool);
610 			new->q.page_pool = NULL;
611 			goto err_out_free_q_info;
612 		}
613 	}
614 
615 	new->q.type = type;
616 	new->q.max_sg_elems = lif->qtype_info[type].max_sg_elems;
617 
618 	err = ionic_q_init(lif, idev, &new->q, index, name, num_descs,
619 			   desc_size, sg_desc_size, pid);
620 	if (err) {
621 		netdev_err(lif->netdev, "Cannot initialize queue\n");
622 		goto err_out_free_page_pool;
623 	}
624 
625 	err = ionic_alloc_qcq_interrupt(lif, new);
626 	if (err)
627 		goto err_out_free_page_pool;
628 
629 	err = ionic_cq_init(lif, &new->cq, &new->intr, num_descs, cq_desc_size);
630 	if (err) {
631 		netdev_err(lif->netdev, "Cannot initialize completion queue\n");
632 		goto err_out_free_irq;
633 	}
634 
635 	if (flags & IONIC_QCQ_F_NOTIFYQ) {
636 		int q_size;
637 
638 		/* q & cq need to be contiguous in NotifyQ, so alloc it all in q
639 		 * and don't alloc qc.  We leave new->qc_size and new->qc_base
640 		 * as 0 to be sure we don't try to free it later.
641 		 */
642 		q_size = ALIGN(num_descs * desc_size, PAGE_SIZE);
643 		new->q_size = PAGE_SIZE + q_size +
644 			      ALIGN(num_descs * cq_desc_size, PAGE_SIZE);
645 		new->q_base = dma_alloc_coherent(dev, new->q_size,
646 						 &new->q_base_pa, GFP_KERNEL);
647 		if (!new->q_base) {
648 			netdev_err(lif->netdev, "Cannot allocate qcq DMA memory\n");
649 			err = -ENOMEM;
650 			goto err_out_free_irq;
651 		}
652 		new->q.base = PTR_ALIGN(new->q_base, PAGE_SIZE);
653 		new->q.base_pa = ALIGN(new->q_base_pa, PAGE_SIZE);
654 
655 		/* Base the NotifyQ cq.base off of the ALIGNed q.base */
656 		new->cq.base = PTR_ALIGN(new->q.base + q_size, PAGE_SIZE);
657 		new->cq.base_pa = ALIGN(new->q_base_pa + q_size, PAGE_SIZE);
658 		new->cq.bound_q = &new->q;
659 	} else {
660 		/* regular DMA q descriptors */
661 		new->q_size = PAGE_SIZE + (num_descs * desc_size);
662 		new->q_base = dma_alloc_coherent(dev, new->q_size, &new->q_base_pa,
663 						 GFP_KERNEL);
664 		if (!new->q_base) {
665 			netdev_err(lif->netdev, "Cannot allocate queue DMA memory\n");
666 			err = -ENOMEM;
667 			goto err_out_free_irq;
668 		}
669 		new->q.base = PTR_ALIGN(new->q_base, PAGE_SIZE);
670 		new->q.base_pa = ALIGN(new->q_base_pa, PAGE_SIZE);
671 
672 		if (flags & IONIC_QCQ_F_CMB_RINGS) {
673 			/* on-chip CMB q descriptors */
674 			new->cmb_q_size = num_descs * desc_size;
675 			new->cmb_order = order_base_2(new->cmb_q_size / PAGE_SIZE);
676 
677 			err = ionic_get_cmb(lif, &new->cmb_pgid, &new->cmb_q_base_pa,
678 					    new->cmb_order, 0, NULL);
679 			if (err) {
680 				netdev_err(lif->netdev,
681 					   "Cannot allocate queue order %d from cmb: err %d\n",
682 					   new->cmb_order, err);
683 				goto err_out_free_q;
684 			}
685 
686 			new->cmb_q_base = ioremap_wc(new->cmb_q_base_pa, new->cmb_q_size);
687 			if (!new->cmb_q_base) {
688 				netdev_err(lif->netdev, "Cannot map queue from cmb\n");
689 				ionic_put_cmb(lif, new->cmb_pgid, new->cmb_order);
690 				err = -ENOMEM;
691 				goto err_out_free_q;
692 			}
693 
694 			new->cmb_q_base_pa -= idev->phy_cmb_pages;
695 			new->q.cmb_base = new->cmb_q_base;
696 			new->q.cmb_base_pa = new->cmb_q_base_pa;
697 		}
698 
699 		/* cq DMA descriptors */
700 		new->cq_size = PAGE_SIZE + (num_descs * cq_desc_size);
701 		new->cq_base = dma_alloc_coherent(dev, new->cq_size, &new->cq_base_pa,
702 						  GFP_KERNEL);
703 		if (!new->cq_base) {
704 			netdev_err(lif->netdev, "Cannot allocate cq DMA memory\n");
705 			err = -ENOMEM;
706 			goto err_out_free_q;
707 		}
708 		new->cq.base = PTR_ALIGN(new->cq_base, PAGE_SIZE);
709 		new->cq.base_pa = ALIGN(new->cq_base_pa, PAGE_SIZE);
710 		new->cq.bound_q = &new->q;
711 	}
712 
713 	if (flags & IONIC_QCQ_F_SG) {
714 		new->sg_size = PAGE_SIZE + (num_descs * sg_desc_size);
715 		new->sg_base = dma_alloc_coherent(dev, new->sg_size, &new->sg_base_pa,
716 						  GFP_KERNEL);
717 		if (!new->sg_base) {
718 			netdev_err(lif->netdev, "Cannot allocate sg DMA memory\n");
719 			err = -ENOMEM;
720 			goto err_out_free_cq;
721 		}
722 		new->q.sg_base = PTR_ALIGN(new->sg_base, PAGE_SIZE);
723 		new->q.sg_base_pa = ALIGN(new->sg_base_pa, PAGE_SIZE);
724 	}
725 
726 	INIT_WORK(&new->dim.work, ionic_dim_work);
727 	new->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_CQE;
728 	if (lif->doorbell_wa)
729 		INIT_WORK(&new->doorbell_napi_work, ionic_doorbell_napi_work);
730 
731 	*qcq = new;
732 
733 	return 0;
734 
735 err_out_free_cq:
736 	dma_free_coherent(dev, new->cq_size, new->cq_base, new->cq_base_pa);
737 err_out_free_q:
738 	if (new->cmb_q_base) {
739 		iounmap(new->cmb_q_base);
740 		ionic_put_cmb(lif, new->cmb_pgid, new->cmb_order);
741 	}
742 	dma_free_coherent(dev, new->q_size, new->q_base, new->q_base_pa);
743 err_out_free_irq:
744 	if (flags & IONIC_QCQ_F_INTR) {
745 		devm_free_irq(dev, new->intr.vector, &new->napi);
746 		ionic_intr_free(lif, new->intr.index);
747 	}
748 err_out_free_page_pool:
749 	page_pool_destroy(new->q.page_pool);
750 err_out_free_q_info:
751 	vfree(new->q.info);
752 err_out_free_qcq:
753 	devm_kfree(dev, new);
754 err_out:
755 	dev_err(dev, "qcq alloc of %s%d failed %d\n", name, index, err);
756 	return err;
757 }
758 
759 static int ionic_qcqs_alloc(struct ionic_lif *lif)
760 {
761 	struct device *dev = lif->ionic->dev;
762 	unsigned int flags;
763 	int err;
764 
765 	flags = IONIC_QCQ_F_INTR;
766 	err = ionic_qcq_alloc(lif, IONIC_QTYPE_ADMINQ, 0, "admin", flags,
767 			      IONIC_ADMINQ_LENGTH,
768 			      sizeof(struct ionic_admin_cmd),
769 			      sizeof(struct ionic_admin_comp),
770 			      0,
771 			      sizeof(struct ionic_admin_desc_info),
772 			      lif->kern_pid, NULL, &lif->adminqcq);
773 	if (err)
774 		return err;
775 	ionic_debugfs_add_qcq(lif, lif->adminqcq);
776 
777 	if (lif->ionic->nnqs_per_lif) {
778 		flags = IONIC_QCQ_F_NOTIFYQ;
779 		err = ionic_qcq_alloc(lif, IONIC_QTYPE_NOTIFYQ, 0, "notifyq",
780 				      flags, IONIC_NOTIFYQ_LENGTH,
781 				      sizeof(struct ionic_notifyq_cmd),
782 				      sizeof(union ionic_notifyq_comp),
783 				      0,
784 				      sizeof(struct ionic_admin_desc_info),
785 				      lif->kern_pid, NULL, &lif->notifyqcq);
786 		if (err)
787 			goto err_out;
788 		ionic_debugfs_add_qcq(lif, lif->notifyqcq);
789 
790 		/* Let the notifyq ride on the adminq interrupt */
791 		ionic_link_qcq_interrupts(lif->adminqcq, lif->notifyqcq);
792 	}
793 
794 	err = -ENOMEM;
795 	lif->txqcqs = devm_kcalloc(dev, lif->ionic->ntxqs_per_lif,
796 				   sizeof(*lif->txqcqs), GFP_KERNEL);
797 	if (!lif->txqcqs)
798 		goto err_out;
799 	lif->rxqcqs = devm_kcalloc(dev, lif->ionic->nrxqs_per_lif,
800 				   sizeof(*lif->rxqcqs), GFP_KERNEL);
801 	if (!lif->rxqcqs)
802 		goto err_out;
803 
804 	lif->txqstats = devm_kcalloc(dev, lif->ionic->ntxqs_per_lif + 1,
805 				     sizeof(*lif->txqstats), GFP_KERNEL);
806 	if (!lif->txqstats)
807 		goto err_out;
808 	lif->rxqstats = devm_kcalloc(dev, lif->ionic->nrxqs_per_lif + 1,
809 				     sizeof(*lif->rxqstats), GFP_KERNEL);
810 	if (!lif->rxqstats)
811 		goto err_out;
812 
813 	return 0;
814 
815 err_out:
816 	ionic_qcqs_free(lif);
817 	return err;
818 }
819 
820 static void ionic_qcq_sanitize(struct ionic_qcq *qcq)
821 {
822 	qcq->q.tail_idx = 0;
823 	qcq->q.head_idx = 0;
824 	qcq->cq.tail_idx = 0;
825 	qcq->cq.done_color = 1;
826 	memset(qcq->q_base, 0, qcq->q_size);
827 	if (qcq->cmb_q_base)
828 		memset_io(qcq->cmb_q_base, 0, qcq->cmb_q_size);
829 	memset(qcq->cq_base, 0, qcq->cq_size);
830 	memset(qcq->sg_base, 0, qcq->sg_size);
831 }
832 
833 static int ionic_lif_txq_init(struct ionic_lif *lif, struct ionic_qcq *qcq)
834 {
835 	struct device *dev = lif->ionic->dev;
836 	struct ionic_queue *q = &qcq->q;
837 	struct ionic_cq *cq = &qcq->cq;
838 	struct ionic_admin_ctx ctx = {
839 		.work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
840 		.cmd.q_init = {
841 			.opcode = IONIC_CMD_Q_INIT,
842 			.lif_index = cpu_to_le16(lif->index),
843 			.type = q->type,
844 			.ver = lif->qtype_info[q->type].version,
845 			.index = cpu_to_le32(q->index),
846 			.flags = cpu_to_le16(IONIC_QINIT_F_IRQ |
847 					     IONIC_QINIT_F_SG),
848 			.intr_index = cpu_to_le16(qcq->intr.index),
849 			.pid = cpu_to_le16(q->pid),
850 			.ring_size = ilog2(q->num_descs),
851 			.ring_base = cpu_to_le64(q->base_pa),
852 			.cq_ring_base = cpu_to_le64(cq->base_pa),
853 			.sg_ring_base = cpu_to_le64(q->sg_base_pa),
854 			.features = cpu_to_le64(q->features),
855 		},
856 	};
857 	int err;
858 
859 	if (qcq->flags & IONIC_QCQ_F_CMB_RINGS) {
860 		ctx.cmd.q_init.flags |= cpu_to_le16(IONIC_QINIT_F_CMB);
861 		ctx.cmd.q_init.ring_base = cpu_to_le64(qcq->cmb_q_base_pa);
862 	}
863 
864 	dev_dbg(dev, "txq_init.pid %d\n", ctx.cmd.q_init.pid);
865 	dev_dbg(dev, "txq_init.index %d\n", ctx.cmd.q_init.index);
866 	dev_dbg(dev, "txq_init.ring_base 0x%llx\n", ctx.cmd.q_init.ring_base);
867 	dev_dbg(dev, "txq_init.ring_size %d\n", ctx.cmd.q_init.ring_size);
868 	dev_dbg(dev, "txq_init.cq_ring_base 0x%llx\n", ctx.cmd.q_init.cq_ring_base);
869 	dev_dbg(dev, "txq_init.sg_ring_base 0x%llx\n", ctx.cmd.q_init.sg_ring_base);
870 	dev_dbg(dev, "txq_init.flags 0x%x\n", ctx.cmd.q_init.flags);
871 	dev_dbg(dev, "txq_init.ver %d\n", ctx.cmd.q_init.ver);
872 	dev_dbg(dev, "txq_init.intr_index %d\n", ctx.cmd.q_init.intr_index);
873 
874 	ionic_qcq_sanitize(qcq);
875 
876 	err = ionic_adminq_post_wait(lif, &ctx);
877 	if (err)
878 		return err;
879 
880 	q->hw_type = ctx.comp.q_init.hw_type;
881 	q->hw_index = le32_to_cpu(ctx.comp.q_init.hw_index);
882 	q->dbval = IONIC_DBELL_QID(q->hw_index);
883 
884 	dev_dbg(dev, "txq->hw_type %d\n", q->hw_type);
885 	dev_dbg(dev, "txq->hw_index %d\n", q->hw_index);
886 
887 	q->dbell_deadline = IONIC_TX_DOORBELL_DEADLINE;
888 	q->dbell_jiffies = jiffies;
889 
890 	if (test_bit(IONIC_LIF_F_SPLIT_INTR, lif->state))
891 		netif_napi_add(lif->netdev, &qcq->napi, ionic_tx_napi);
892 
893 	qcq->flags |= IONIC_QCQ_F_INITED;
894 
895 	return 0;
896 }
897 
898 static int ionic_lif_rxq_init(struct ionic_lif *lif, struct ionic_qcq *qcq)
899 {
900 	struct device *dev = lif->ionic->dev;
901 	struct ionic_queue *q = &qcq->q;
902 	struct ionic_cq *cq = &qcq->cq;
903 	struct ionic_admin_ctx ctx = {
904 		.work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
905 		.cmd.q_init = {
906 			.opcode = IONIC_CMD_Q_INIT,
907 			.lif_index = cpu_to_le16(lif->index),
908 			.type = q->type,
909 			.ver = lif->qtype_info[q->type].version,
910 			.index = cpu_to_le32(q->index),
911 			.flags = cpu_to_le16(IONIC_QINIT_F_IRQ),
912 			.intr_index = cpu_to_le16(cq->bound_intr->index),
913 			.pid = cpu_to_le16(q->pid),
914 			.ring_size = ilog2(q->num_descs),
915 			.ring_base = cpu_to_le64(q->base_pa),
916 			.cq_ring_base = cpu_to_le64(cq->base_pa),
917 			.sg_ring_base = cpu_to_le64(q->sg_base_pa),
918 			.features = cpu_to_le64(q->features),
919 		},
920 	};
921 	int err;
922 
923 	q->partner = &lif->txqcqs[q->index]->q;
924 	q->partner->partner = q;
925 
926 	if (!lif->xdp_prog ||
927 	    (lif->xdp_prog->aux && lif->xdp_prog->aux->xdp_has_frags))
928 		ctx.cmd.q_init.flags |= cpu_to_le16(IONIC_QINIT_F_SG);
929 
930 	if (qcq->flags & IONIC_QCQ_F_CMB_RINGS) {
931 		ctx.cmd.q_init.flags |= cpu_to_le16(IONIC_QINIT_F_CMB);
932 		ctx.cmd.q_init.ring_base = cpu_to_le64(qcq->cmb_q_base_pa);
933 	}
934 
935 	dev_dbg(dev, "rxq_init.pid %d\n", ctx.cmd.q_init.pid);
936 	dev_dbg(dev, "rxq_init.index %d\n", ctx.cmd.q_init.index);
937 	dev_dbg(dev, "rxq_init.ring_base 0x%llx\n", ctx.cmd.q_init.ring_base);
938 	dev_dbg(dev, "rxq_init.ring_size %d\n", ctx.cmd.q_init.ring_size);
939 	dev_dbg(dev, "rxq_init.flags 0x%x\n", ctx.cmd.q_init.flags);
940 	dev_dbg(dev, "rxq_init.ver %d\n", ctx.cmd.q_init.ver);
941 	dev_dbg(dev, "rxq_init.intr_index %d\n", ctx.cmd.q_init.intr_index);
942 
943 	ionic_qcq_sanitize(qcq);
944 
945 	err = ionic_adminq_post_wait(lif, &ctx);
946 	if (err)
947 		return err;
948 
949 	q->hw_type = ctx.comp.q_init.hw_type;
950 	q->hw_index = le32_to_cpu(ctx.comp.q_init.hw_index);
951 	q->dbval = IONIC_DBELL_QID(q->hw_index);
952 
953 	dev_dbg(dev, "rxq->hw_type %d\n", q->hw_type);
954 	dev_dbg(dev, "rxq->hw_index %d\n", q->hw_index);
955 
956 	q->dbell_deadline = IONIC_RX_MIN_DOORBELL_DEADLINE;
957 	q->dbell_jiffies = jiffies;
958 
959 	if (test_bit(IONIC_LIF_F_SPLIT_INTR, lif->state))
960 		netif_napi_add(lif->netdev, &qcq->napi, ionic_rx_napi);
961 	else
962 		netif_napi_add(lif->netdev, &qcq->napi, ionic_txrx_napi);
963 	err = ionic_register_rxq_info(q, qcq->napi.napi_id);
964 	if (err) {
965 		netif_napi_del(&qcq->napi);
966 		return err;
967 	}
968 
969 	qcq->flags |= IONIC_QCQ_F_INITED;
970 
971 	return 0;
972 }
973 
974 int ionic_lif_create_hwstamp_txq(struct ionic_lif *lif)
975 {
976 	unsigned int num_desc, desc_sz, comp_sz, sg_desc_sz;
977 	unsigned int txq_i, flags;
978 	struct ionic_qcq *txq;
979 	u64 features;
980 	int err;
981 
982 	if (lif->hwstamp_txq)
983 		return 0;
984 
985 	features = IONIC_Q_F_2X_CQ_DESC | IONIC_TXQ_F_HWSTAMP;
986 
987 	num_desc = IONIC_MIN_TXRX_DESC;
988 	desc_sz = sizeof(struct ionic_txq_desc);
989 	comp_sz = 2 * sizeof(struct ionic_txq_comp);
990 
991 	if (lif->qtype_info[IONIC_QTYPE_TXQ].version >= 1 &&
992 	    lif->qtype_info[IONIC_QTYPE_TXQ].sg_desc_sz == sizeof(struct ionic_txq_sg_desc_v1))
993 		sg_desc_sz = sizeof(struct ionic_txq_sg_desc_v1);
994 	else
995 		sg_desc_sz = sizeof(struct ionic_txq_sg_desc);
996 
997 	txq_i = lif->ionic->ntxqs_per_lif;
998 	flags = IONIC_QCQ_F_TX_STATS | IONIC_QCQ_F_SG;
999 
1000 	err = ionic_qcq_alloc(lif, IONIC_QTYPE_TXQ, txq_i, "hwstamp_tx", flags,
1001 			      num_desc, desc_sz, comp_sz, sg_desc_sz,
1002 			      sizeof(struct ionic_tx_desc_info),
1003 			      lif->kern_pid, NULL, &txq);
1004 	if (err)
1005 		goto err_qcq_alloc;
1006 
1007 	txq->q.features = features;
1008 
1009 	ionic_link_qcq_interrupts(lif->adminqcq, txq);
1010 	ionic_debugfs_add_qcq(lif, txq);
1011 
1012 	lif->hwstamp_txq = txq;
1013 
1014 	if (netif_running(lif->netdev)) {
1015 		err = ionic_lif_txq_init(lif, txq);
1016 		if (err)
1017 			goto err_qcq_init;
1018 
1019 		if (test_bit(IONIC_LIF_F_UP, lif->state)) {
1020 			err = ionic_qcq_enable(txq);
1021 			if (err)
1022 				goto err_qcq_enable;
1023 		}
1024 	}
1025 
1026 	return 0;
1027 
1028 err_qcq_enable:
1029 	ionic_lif_qcq_deinit(lif, txq);
1030 err_qcq_init:
1031 	lif->hwstamp_txq = NULL;
1032 	ionic_debugfs_del_qcq(txq);
1033 	ionic_qcq_free(lif, txq);
1034 	devm_kfree(lif->ionic->dev, txq);
1035 err_qcq_alloc:
1036 	return err;
1037 }
1038 
1039 int ionic_lif_create_hwstamp_rxq(struct ionic_lif *lif)
1040 {
1041 	unsigned int num_desc, desc_sz, comp_sz, sg_desc_sz;
1042 	unsigned int rxq_i, flags;
1043 	struct ionic_qcq *rxq;
1044 	u64 features;
1045 	int err;
1046 
1047 	if (lif->hwstamp_rxq)
1048 		return 0;
1049 
1050 	features = IONIC_Q_F_2X_CQ_DESC | IONIC_RXQ_F_HWSTAMP;
1051 
1052 	num_desc = IONIC_MIN_TXRX_DESC;
1053 	desc_sz = sizeof(struct ionic_rxq_desc);
1054 	comp_sz = 2 * sizeof(struct ionic_rxq_comp);
1055 	sg_desc_sz = sizeof(struct ionic_rxq_sg_desc);
1056 
1057 	rxq_i = lif->ionic->nrxqs_per_lif;
1058 	flags = IONIC_QCQ_F_RX_STATS | IONIC_QCQ_F_SG;
1059 
1060 	err = ionic_qcq_alloc(lif, IONIC_QTYPE_RXQ, rxq_i, "hwstamp_rx", flags,
1061 			      num_desc, desc_sz, comp_sz, sg_desc_sz,
1062 			      sizeof(struct ionic_rx_desc_info),
1063 			      lif->kern_pid, NULL, &rxq);
1064 	if (err)
1065 		goto err_qcq_alloc;
1066 
1067 	rxq->q.features = features;
1068 
1069 	ionic_link_qcq_interrupts(lif->adminqcq, rxq);
1070 	ionic_debugfs_add_qcq(lif, rxq);
1071 
1072 	lif->hwstamp_rxq = rxq;
1073 
1074 	if (netif_running(lif->netdev)) {
1075 		err = ionic_lif_rxq_init(lif, rxq);
1076 		if (err)
1077 			goto err_qcq_init;
1078 
1079 		if (test_bit(IONIC_LIF_F_UP, lif->state)) {
1080 			ionic_rx_fill(&rxq->q, NULL);
1081 			err = ionic_qcq_enable(rxq);
1082 			if (err)
1083 				goto err_qcq_enable;
1084 		}
1085 	}
1086 
1087 	return 0;
1088 
1089 err_qcq_enable:
1090 	ionic_lif_qcq_deinit(lif, rxq);
1091 err_qcq_init:
1092 	lif->hwstamp_rxq = NULL;
1093 	ionic_debugfs_del_qcq(rxq);
1094 	ionic_qcq_free(lif, rxq);
1095 	devm_kfree(lif->ionic->dev, rxq);
1096 err_qcq_alloc:
1097 	return err;
1098 }
1099 
1100 int ionic_lif_config_hwstamp_rxq_all(struct ionic_lif *lif, bool rx_all)
1101 {
1102 	struct ionic_queue_params qparam;
1103 
1104 	ionic_init_queue_params(lif, &qparam);
1105 
1106 	if (rx_all)
1107 		qparam.rxq_features = IONIC_Q_F_2X_CQ_DESC | IONIC_RXQ_F_HWSTAMP;
1108 	else
1109 		qparam.rxq_features = 0;
1110 
1111 	/* if we're not running, just set the values and return */
1112 	if (!netif_running(lif->netdev)) {
1113 		lif->rxq_features = qparam.rxq_features;
1114 		return 0;
1115 	}
1116 
1117 	return ionic_reconfigure_queues(lif, &qparam);
1118 }
1119 
1120 int ionic_lif_set_hwstamp_txmode(struct ionic_lif *lif, u16 txstamp_mode)
1121 {
1122 	struct ionic_admin_ctx ctx = {
1123 		.work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
1124 		.cmd.lif_setattr = {
1125 			.opcode = IONIC_CMD_LIF_SETATTR,
1126 			.index = cpu_to_le16(lif->index),
1127 			.attr = IONIC_LIF_ATTR_TXSTAMP,
1128 			.txstamp_mode = cpu_to_le16(txstamp_mode),
1129 		},
1130 	};
1131 
1132 	return ionic_adminq_post_wait(lif, &ctx);
1133 }
1134 
1135 static void ionic_lif_del_hwstamp_rxfilt(struct ionic_lif *lif)
1136 {
1137 	struct ionic_admin_ctx ctx = {
1138 		.work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
1139 		.cmd.rx_filter_del = {
1140 			.opcode = IONIC_CMD_RX_FILTER_DEL,
1141 			.lif_index = cpu_to_le16(lif->index),
1142 		},
1143 	};
1144 	struct ionic_rx_filter *f;
1145 	u32 filter_id;
1146 	int err;
1147 
1148 	spin_lock_bh(&lif->rx_filters.lock);
1149 
1150 	f = ionic_rx_filter_rxsteer(lif);
1151 	if (!f) {
1152 		spin_unlock_bh(&lif->rx_filters.lock);
1153 		return;
1154 	}
1155 
1156 	filter_id = f->filter_id;
1157 	ionic_rx_filter_free(lif, f);
1158 
1159 	spin_unlock_bh(&lif->rx_filters.lock);
1160 
1161 	netdev_dbg(lif->netdev, "rx_filter del RXSTEER (id %d)\n", filter_id);
1162 
1163 	ctx.cmd.rx_filter_del.filter_id = cpu_to_le32(filter_id);
1164 
1165 	err = ionic_adminq_post_wait(lif, &ctx);
1166 	if (err && err != -EEXIST)
1167 		netdev_dbg(lif->netdev, "failed to delete rx_filter RXSTEER (id %d)\n", filter_id);
1168 }
1169 
1170 static int ionic_lif_add_hwstamp_rxfilt(struct ionic_lif *lif, u64 pkt_class)
1171 {
1172 	struct ionic_admin_ctx ctx = {
1173 		.work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
1174 		.cmd.rx_filter_add = {
1175 			.opcode = IONIC_CMD_RX_FILTER_ADD,
1176 			.lif_index = cpu_to_le16(lif->index),
1177 			.match = cpu_to_le16(IONIC_RX_FILTER_STEER_PKTCLASS),
1178 			.pkt_class = cpu_to_le64(pkt_class),
1179 		},
1180 	};
1181 	u8 qtype;
1182 	u32 qid;
1183 	int err;
1184 
1185 	if (!lif->hwstamp_rxq)
1186 		return -EINVAL;
1187 
1188 	qtype = lif->hwstamp_rxq->q.type;
1189 	ctx.cmd.rx_filter_add.qtype = qtype;
1190 
1191 	qid = lif->hwstamp_rxq->q.index;
1192 	ctx.cmd.rx_filter_add.qid = cpu_to_le32(qid);
1193 
1194 	netdev_dbg(lif->netdev, "rx_filter add RXSTEER\n");
1195 	err = ionic_adminq_post_wait(lif, &ctx);
1196 	if (err && err != -EEXIST)
1197 		return err;
1198 
1199 	spin_lock_bh(&lif->rx_filters.lock);
1200 	err = ionic_rx_filter_save(lif, 0, qid, 0, &ctx, IONIC_FILTER_STATE_SYNCED);
1201 	spin_unlock_bh(&lif->rx_filters.lock);
1202 
1203 	return err;
1204 }
1205 
1206 int ionic_lif_set_hwstamp_rxfilt(struct ionic_lif *lif, u64 pkt_class)
1207 {
1208 	ionic_lif_del_hwstamp_rxfilt(lif);
1209 
1210 	if (!pkt_class)
1211 		return 0;
1212 
1213 	return ionic_lif_add_hwstamp_rxfilt(lif, pkt_class);
1214 }
1215 
1216 static int ionic_adminq_napi(struct napi_struct *napi, int budget)
1217 {
1218 	struct ionic_intr_info *intr = napi_to_cq(napi)->bound_intr;
1219 	struct ionic_lif *lif = napi_to_cq(napi)->lif;
1220 	struct ionic_dev *idev = &lif->ionic->idev;
1221 	unsigned long irqflags;
1222 	unsigned int flags = 0;
1223 	int rx_work = 0;
1224 	int tx_work = 0;
1225 	int n_work = 0;
1226 	int a_work = 0;
1227 	int work_done;
1228 	int credits;
1229 
1230 	if (lif->notifyqcq && lif->notifyqcq->flags & IONIC_QCQ_F_INITED)
1231 		n_work = ionic_cq_service(&lif->notifyqcq->cq, budget,
1232 					  ionic_notifyq_service, NULL, NULL);
1233 
1234 	spin_lock_irqsave(&lif->adminq_lock, irqflags);
1235 	if (lif->adminqcq && lif->adminqcq->flags & IONIC_QCQ_F_INITED)
1236 		a_work = ionic_cq_service(&lif->adminqcq->cq, budget,
1237 					  ionic_adminq_service, NULL, NULL);
1238 
1239 	spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
1240 
1241 	if (lif->hwstamp_rxq)
1242 		rx_work = ionic_cq_service(&lif->hwstamp_rxq->cq, budget,
1243 					   ionic_rx_service, NULL, NULL);
1244 
1245 	if (lif->hwstamp_txq)
1246 		tx_work = ionic_tx_cq_service(&lif->hwstamp_txq->cq, budget, !!budget);
1247 
1248 	work_done = max(max(n_work, a_work), max(rx_work, tx_work));
1249 	if (work_done < budget && napi_complete_done(napi, work_done)) {
1250 		flags |= IONIC_INTR_CRED_UNMASK;
1251 		intr->rearm_count++;
1252 	}
1253 
1254 	if (work_done || flags) {
1255 		flags |= IONIC_INTR_CRED_RESET_COALESCE;
1256 		credits = n_work + a_work + rx_work + tx_work;
1257 		ionic_intr_credits(idev->intr_ctrl, intr->index, credits, flags);
1258 	}
1259 
1260 	if (lif->doorbell_wa) {
1261 		if (!a_work)
1262 			ionic_adminq_poke_doorbell(&lif->adminqcq->q);
1263 		if (lif->hwstamp_rxq && !rx_work)
1264 			ionic_rxq_poke_doorbell(&lif->hwstamp_rxq->q);
1265 		if (lif->hwstamp_txq && !tx_work)
1266 			ionic_txq_poke_doorbell(&lif->hwstamp_txq->q);
1267 	}
1268 
1269 	return work_done;
1270 }
1271 
1272 void ionic_get_stats64(struct net_device *netdev,
1273 		       struct rtnl_link_stats64 *ns)
1274 {
1275 	struct ionic_lif *lif = netdev_priv(netdev);
1276 	struct ionic_lif_stats *ls;
1277 
1278 	memset(ns, 0, sizeof(*ns));
1279 	ls = &lif->info->stats;
1280 
1281 	ns->rx_packets = le64_to_cpu(ls->rx_ucast_packets) +
1282 			 le64_to_cpu(ls->rx_mcast_packets) +
1283 			 le64_to_cpu(ls->rx_bcast_packets);
1284 
1285 	ns->tx_packets = le64_to_cpu(ls->tx_ucast_packets) +
1286 			 le64_to_cpu(ls->tx_mcast_packets) +
1287 			 le64_to_cpu(ls->tx_bcast_packets);
1288 
1289 	ns->rx_bytes = le64_to_cpu(ls->rx_ucast_bytes) +
1290 		       le64_to_cpu(ls->rx_mcast_bytes) +
1291 		       le64_to_cpu(ls->rx_bcast_bytes);
1292 
1293 	ns->tx_bytes = le64_to_cpu(ls->tx_ucast_bytes) +
1294 		       le64_to_cpu(ls->tx_mcast_bytes) +
1295 		       le64_to_cpu(ls->tx_bcast_bytes);
1296 
1297 	ns->rx_dropped = le64_to_cpu(ls->rx_ucast_drop_packets) +
1298 			 le64_to_cpu(ls->rx_mcast_drop_packets) +
1299 			 le64_to_cpu(ls->rx_bcast_drop_packets);
1300 
1301 	ns->tx_dropped = le64_to_cpu(ls->tx_ucast_drop_packets) +
1302 			 le64_to_cpu(ls->tx_mcast_drop_packets) +
1303 			 le64_to_cpu(ls->tx_bcast_drop_packets);
1304 
1305 	ns->multicast = le64_to_cpu(ls->rx_mcast_packets);
1306 
1307 	ns->rx_over_errors = le64_to_cpu(ls->rx_queue_empty);
1308 
1309 	ns->rx_missed_errors = le64_to_cpu(ls->rx_dma_error) +
1310 			       le64_to_cpu(ls->rx_queue_disabled) +
1311 			       le64_to_cpu(ls->rx_desc_fetch_error) +
1312 			       le64_to_cpu(ls->rx_desc_data_error);
1313 
1314 	ns->tx_aborted_errors = le64_to_cpu(ls->tx_dma_error) +
1315 				le64_to_cpu(ls->tx_queue_disabled) +
1316 				le64_to_cpu(ls->tx_desc_fetch_error) +
1317 				le64_to_cpu(ls->tx_desc_data_error);
1318 
1319 	ns->rx_errors = ns->rx_over_errors +
1320 			ns->rx_missed_errors;
1321 
1322 	ns->tx_errors = ns->tx_aborted_errors;
1323 }
1324 
1325 static int ionic_addr_add(struct net_device *netdev, const u8 *addr)
1326 {
1327 	return ionic_lif_list_addr(netdev_priv(netdev), addr, ADD_ADDR);
1328 }
1329 
1330 static int ionic_addr_del(struct net_device *netdev, const u8 *addr)
1331 {
1332 	/* Don't delete our own address from the uc list */
1333 	if (ether_addr_equal(addr, netdev->dev_addr))
1334 		return 0;
1335 
1336 	return ionic_lif_list_addr(netdev_priv(netdev), addr, DEL_ADDR);
1337 }
1338 
1339 void ionic_lif_rx_mode(struct ionic_lif *lif)
1340 {
1341 	struct net_device *netdev = lif->netdev;
1342 	unsigned int nfilters;
1343 	unsigned int nd_flags;
1344 	char buf[128];
1345 	u16 rx_mode;
1346 	int i;
1347 #define REMAIN(__x) (sizeof(buf) - (__x))
1348 
1349 	mutex_lock(&lif->config_lock);
1350 
1351 	/* grab the flags once for local use */
1352 	nd_flags = netdev->flags;
1353 
1354 	rx_mode = IONIC_RX_MODE_F_UNICAST;
1355 	rx_mode |= (nd_flags & IFF_MULTICAST) ? IONIC_RX_MODE_F_MULTICAST : 0;
1356 	rx_mode |= (nd_flags & IFF_BROADCAST) ? IONIC_RX_MODE_F_BROADCAST : 0;
1357 	rx_mode |= (nd_flags & IFF_PROMISC) ? IONIC_RX_MODE_F_PROMISC : 0;
1358 	rx_mode |= (nd_flags & IFF_ALLMULTI) ? IONIC_RX_MODE_F_ALLMULTI : 0;
1359 
1360 	/* sync the filters */
1361 	ionic_rx_filter_sync(lif);
1362 
1363 	/* check for overflow state
1364 	 *    if so, we track that we overflowed and enable NIC PROMISC
1365 	 *    else if the overflow is set and not needed
1366 	 *       we remove our overflow flag and check the netdev flags
1367 	 *       to see if we can disable NIC PROMISC
1368 	 */
1369 	nfilters = le32_to_cpu(lif->identity->eth.max_ucast_filters);
1370 
1371 	if (((lif->nucast + lif->nmcast) >= nfilters) ||
1372 	    (lif->max_vlans && lif->nvlans >= lif->max_vlans)) {
1373 		rx_mode |= IONIC_RX_MODE_F_PROMISC;
1374 		rx_mode |= IONIC_RX_MODE_F_ALLMULTI;
1375 	} else {
1376 		if (!(nd_flags & IFF_PROMISC))
1377 			rx_mode &= ~IONIC_RX_MODE_F_PROMISC;
1378 		if (!(nd_flags & IFF_ALLMULTI))
1379 			rx_mode &= ~IONIC_RX_MODE_F_ALLMULTI;
1380 	}
1381 
1382 	i = scnprintf(buf, sizeof(buf), "rx_mode 0x%04x -> 0x%04x:",
1383 		      lif->rx_mode, rx_mode);
1384 	if (rx_mode & IONIC_RX_MODE_F_UNICAST)
1385 		i += scnprintf(&buf[i], REMAIN(i), " RX_MODE_F_UNICAST");
1386 	if (rx_mode & IONIC_RX_MODE_F_MULTICAST)
1387 		i += scnprintf(&buf[i], REMAIN(i), " RX_MODE_F_MULTICAST");
1388 	if (rx_mode & IONIC_RX_MODE_F_BROADCAST)
1389 		i += scnprintf(&buf[i], REMAIN(i), " RX_MODE_F_BROADCAST");
1390 	if (rx_mode & IONIC_RX_MODE_F_PROMISC)
1391 		i += scnprintf(&buf[i], REMAIN(i), " RX_MODE_F_PROMISC");
1392 	if (rx_mode & IONIC_RX_MODE_F_ALLMULTI)
1393 		i += scnprintf(&buf[i], REMAIN(i), " RX_MODE_F_ALLMULTI");
1394 	if (rx_mode & IONIC_RX_MODE_F_RDMA_SNIFFER)
1395 		i += scnprintf(&buf[i], REMAIN(i), " RX_MODE_F_RDMA_SNIFFER");
1396 	netdev_dbg(netdev, "lif%d %s\n", lif->index, buf);
1397 
1398 	if (lif->rx_mode != rx_mode) {
1399 		struct ionic_admin_ctx ctx = {
1400 			.work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
1401 			.cmd.rx_mode_set = {
1402 				.opcode = IONIC_CMD_RX_MODE_SET,
1403 				.lif_index = cpu_to_le16(lif->index),
1404 			},
1405 		};
1406 		int err;
1407 
1408 		ctx.cmd.rx_mode_set.rx_mode = cpu_to_le16(rx_mode);
1409 		err = ionic_adminq_post_wait(lif, &ctx);
1410 		if (err)
1411 			netdev_warn(netdev, "set rx_mode 0x%04x failed: %d\n",
1412 				    rx_mode, err);
1413 		else
1414 			lif->rx_mode = rx_mode;
1415 	}
1416 
1417 	mutex_unlock(&lif->config_lock);
1418 }
1419 
1420 static void ionic_ndo_set_rx_mode(struct net_device *netdev)
1421 {
1422 	struct ionic_lif *lif = netdev_priv(netdev);
1423 	struct ionic_deferred_work *work;
1424 
1425 	/* Sync the kernel filter list with the driver filter list */
1426 	__dev_uc_sync(netdev, ionic_addr_add, ionic_addr_del);
1427 	__dev_mc_sync(netdev, ionic_addr_add, ionic_addr_del);
1428 
1429 	/* Shove off the rest of the rxmode work to the work task
1430 	 * which will include syncing the filters to the firmware.
1431 	 */
1432 	work = kzalloc_obj(*work, GFP_ATOMIC);
1433 	if (!work) {
1434 		netdev_err(lif->netdev, "rxmode change dropped\n");
1435 		return;
1436 	}
1437 	work->type = IONIC_DW_TYPE_RX_MODE;
1438 	netdev_dbg(lif->netdev, "deferred: rx_mode\n");
1439 	ionic_lif_deferred_enqueue(lif, work);
1440 }
1441 
1442 static __le64 ionic_netdev_features_to_nic(netdev_features_t features)
1443 {
1444 	u64 wanted = 0;
1445 
1446 	if (features & NETIF_F_HW_VLAN_CTAG_TX)
1447 		wanted |= IONIC_ETH_HW_VLAN_TX_TAG;
1448 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
1449 		wanted |= IONIC_ETH_HW_VLAN_RX_STRIP;
1450 	if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
1451 		wanted |= IONIC_ETH_HW_VLAN_RX_FILTER;
1452 	if (features & NETIF_F_RXHASH)
1453 		wanted |= IONIC_ETH_HW_RX_HASH;
1454 	if (features & NETIF_F_RXCSUM)
1455 		wanted |= IONIC_ETH_HW_RX_CSUM;
1456 	if (features & NETIF_F_SG)
1457 		wanted |= IONIC_ETH_HW_TX_SG;
1458 	if (features & NETIF_F_HW_CSUM)
1459 		wanted |= IONIC_ETH_HW_TX_CSUM;
1460 	if (features & NETIF_F_TSO)
1461 		wanted |= IONIC_ETH_HW_TSO;
1462 	if (features & NETIF_F_TSO6)
1463 		wanted |= IONIC_ETH_HW_TSO_IPV6;
1464 	if (features & NETIF_F_TSO_ECN)
1465 		wanted |= IONIC_ETH_HW_TSO_ECN;
1466 	if (features & NETIF_F_GSO_GRE)
1467 		wanted |= IONIC_ETH_HW_TSO_GRE;
1468 	if (features & NETIF_F_GSO_GRE_CSUM)
1469 		wanted |= IONIC_ETH_HW_TSO_GRE_CSUM;
1470 	if (features & NETIF_F_GSO_IPXIP4)
1471 		wanted |= IONIC_ETH_HW_TSO_IPXIP4;
1472 	if (features & NETIF_F_GSO_IPXIP6)
1473 		wanted |= IONIC_ETH_HW_TSO_IPXIP6;
1474 	if (features & NETIF_F_GSO_UDP_TUNNEL)
1475 		wanted |= IONIC_ETH_HW_TSO_UDP;
1476 	if (features & NETIF_F_GSO_UDP_TUNNEL_CSUM)
1477 		wanted |= IONIC_ETH_HW_TSO_UDP_CSUM;
1478 
1479 	return cpu_to_le64(wanted);
1480 }
1481 
1482 static int ionic_set_nic_features(struct ionic_lif *lif,
1483 				  netdev_features_t features)
1484 {
1485 	struct device *dev = lif->ionic->dev;
1486 	struct ionic_admin_ctx ctx = {
1487 		.work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
1488 		.cmd.lif_setattr = {
1489 			.opcode = IONIC_CMD_LIF_SETATTR,
1490 			.index = cpu_to_le16(lif->index),
1491 			.attr = IONIC_LIF_ATTR_FEATURES,
1492 		},
1493 	};
1494 	u64 vlan_flags = IONIC_ETH_HW_VLAN_TX_TAG |
1495 			 IONIC_ETH_HW_VLAN_RX_STRIP |
1496 			 IONIC_ETH_HW_VLAN_RX_FILTER;
1497 	u64 old_hw_features;
1498 	int err;
1499 
1500 	ctx.cmd.lif_setattr.features = ionic_netdev_features_to_nic(features);
1501 
1502 	if (lif->phc)
1503 		ctx.cmd.lif_setattr.features |= cpu_to_le64(IONIC_ETH_HW_TIMESTAMP);
1504 
1505 	err = ionic_adminq_post_wait(lif, &ctx);
1506 	if (err)
1507 		return err;
1508 
1509 	old_hw_features = lif->hw_features;
1510 	lif->hw_features = le64_to_cpu(ctx.cmd.lif_setattr.features &
1511 				       ctx.comp.lif_setattr.features);
1512 
1513 	if ((old_hw_features ^ lif->hw_features) & IONIC_ETH_HW_RX_HASH)
1514 		ionic_lif_rss_config(lif, lif->rss_types, NULL, NULL);
1515 
1516 	if ((vlan_flags & le64_to_cpu(ctx.cmd.lif_setattr.features)) &&
1517 	    !(vlan_flags & le64_to_cpu(ctx.comp.lif_setattr.features)))
1518 		dev_info_once(lif->ionic->dev, "NIC is not supporting vlan offload, likely in SmartNIC mode\n");
1519 
1520 	if (lif->hw_features & IONIC_ETH_HW_VLAN_TX_TAG)
1521 		dev_dbg(dev, "feature ETH_HW_VLAN_TX_TAG\n");
1522 	if (lif->hw_features & IONIC_ETH_HW_VLAN_RX_STRIP)
1523 		dev_dbg(dev, "feature ETH_HW_VLAN_RX_STRIP\n");
1524 	if (lif->hw_features & IONIC_ETH_HW_VLAN_RX_FILTER)
1525 		dev_dbg(dev, "feature ETH_HW_VLAN_RX_FILTER\n");
1526 	if (lif->hw_features & IONIC_ETH_HW_RX_HASH)
1527 		dev_dbg(dev, "feature ETH_HW_RX_HASH\n");
1528 	if (lif->hw_features & IONIC_ETH_HW_TX_SG)
1529 		dev_dbg(dev, "feature ETH_HW_TX_SG\n");
1530 	if (lif->hw_features & IONIC_ETH_HW_TX_CSUM)
1531 		dev_dbg(dev, "feature ETH_HW_TX_CSUM\n");
1532 	if (lif->hw_features & IONIC_ETH_HW_RX_CSUM)
1533 		dev_dbg(dev, "feature ETH_HW_RX_CSUM\n");
1534 	if (lif->hw_features & IONIC_ETH_HW_TSO)
1535 		dev_dbg(dev, "feature ETH_HW_TSO\n");
1536 	if (lif->hw_features & IONIC_ETH_HW_TSO_IPV6)
1537 		dev_dbg(dev, "feature ETH_HW_TSO_IPV6\n");
1538 	if (lif->hw_features & IONIC_ETH_HW_TSO_ECN)
1539 		dev_dbg(dev, "feature ETH_HW_TSO_ECN\n");
1540 	if (lif->hw_features & IONIC_ETH_HW_TSO_GRE)
1541 		dev_dbg(dev, "feature ETH_HW_TSO_GRE\n");
1542 	if (lif->hw_features & IONIC_ETH_HW_TSO_GRE_CSUM)
1543 		dev_dbg(dev, "feature ETH_HW_TSO_GRE_CSUM\n");
1544 	if (lif->hw_features & IONIC_ETH_HW_TSO_IPXIP4)
1545 		dev_dbg(dev, "feature ETH_HW_TSO_IPXIP4\n");
1546 	if (lif->hw_features & IONIC_ETH_HW_TSO_IPXIP6)
1547 		dev_dbg(dev, "feature ETH_HW_TSO_IPXIP6\n");
1548 	if (lif->hw_features & IONIC_ETH_HW_TSO_UDP)
1549 		dev_dbg(dev, "feature ETH_HW_TSO_UDP\n");
1550 	if (lif->hw_features & IONIC_ETH_HW_TSO_UDP_CSUM)
1551 		dev_dbg(dev, "feature ETH_HW_TSO_UDP_CSUM\n");
1552 	if (lif->hw_features & IONIC_ETH_HW_TIMESTAMP)
1553 		dev_dbg(dev, "feature ETH_HW_TIMESTAMP\n");
1554 
1555 	return 0;
1556 }
1557 
1558 static int ionic_init_nic_features(struct ionic_lif *lif)
1559 {
1560 	struct net_device *netdev = lif->netdev;
1561 	netdev_features_t features;
1562 	int err;
1563 
1564 	/* set up what we expect to support by default */
1565 	features = NETIF_F_HW_VLAN_CTAG_TX |
1566 		   NETIF_F_HW_VLAN_CTAG_RX |
1567 		   NETIF_F_HW_VLAN_CTAG_FILTER |
1568 		   NETIF_F_SG |
1569 		   NETIF_F_HW_CSUM |
1570 		   NETIF_F_RXCSUM |
1571 		   NETIF_F_TSO |
1572 		   NETIF_F_TSO6 |
1573 		   NETIF_F_TSO_ECN |
1574 		   NETIF_F_GSO_GRE |
1575 		   NETIF_F_GSO_GRE_CSUM |
1576 		   NETIF_F_GSO_IPXIP4 |
1577 		   NETIF_F_GSO_IPXIP6 |
1578 		   NETIF_F_GSO_UDP_TUNNEL |
1579 		   NETIF_F_GSO_UDP_TUNNEL_CSUM;
1580 
1581 	if (lif->nxqs > 1)
1582 		features |= NETIF_F_RXHASH;
1583 
1584 	err = ionic_set_nic_features(lif, features);
1585 	if (err)
1586 		return err;
1587 
1588 	/* tell the netdev what we actually can support */
1589 	netdev->features |= NETIF_F_HIGHDMA;
1590 
1591 	if (lif->hw_features & IONIC_ETH_HW_VLAN_TX_TAG)
1592 		netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
1593 	if (lif->hw_features & IONIC_ETH_HW_VLAN_RX_STRIP)
1594 		netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
1595 	if (lif->hw_features & IONIC_ETH_HW_VLAN_RX_FILTER)
1596 		netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1597 	if (lif->hw_features & IONIC_ETH_HW_RX_HASH)
1598 		netdev->hw_features |= NETIF_F_RXHASH;
1599 	if (lif->hw_features & IONIC_ETH_HW_TX_SG)
1600 		netdev->hw_features |= NETIF_F_SG;
1601 
1602 	if (lif->hw_features & IONIC_ETH_HW_TX_CSUM)
1603 		netdev->hw_enc_features |= NETIF_F_HW_CSUM;
1604 	if (lif->hw_features & IONIC_ETH_HW_RX_CSUM)
1605 		netdev->hw_enc_features |= NETIF_F_RXCSUM;
1606 	if (lif->hw_features & IONIC_ETH_HW_TSO)
1607 		netdev->hw_enc_features |= NETIF_F_TSO;
1608 	if (lif->hw_features & IONIC_ETH_HW_TSO_IPV6)
1609 		netdev->hw_enc_features |= NETIF_F_TSO6;
1610 	if (lif->hw_features & IONIC_ETH_HW_TSO_ECN)
1611 		netdev->hw_enc_features |= NETIF_F_TSO_ECN;
1612 	if (lif->hw_features & IONIC_ETH_HW_TSO_GRE)
1613 		netdev->hw_enc_features |= NETIF_F_GSO_GRE;
1614 	if (lif->hw_features & IONIC_ETH_HW_TSO_GRE_CSUM)
1615 		netdev->hw_enc_features |= NETIF_F_GSO_GRE_CSUM;
1616 	if (lif->hw_features & IONIC_ETH_HW_TSO_IPXIP4)
1617 		netdev->hw_enc_features |= NETIF_F_GSO_IPXIP4;
1618 	if (lif->hw_features & IONIC_ETH_HW_TSO_IPXIP6)
1619 		netdev->hw_enc_features |= NETIF_F_GSO_IPXIP6;
1620 	if (lif->hw_features & IONIC_ETH_HW_TSO_UDP)
1621 		netdev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL;
1622 	if (lif->hw_features & IONIC_ETH_HW_TSO_UDP_CSUM)
1623 		netdev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
1624 
1625 	netdev->hw_features |= netdev->hw_enc_features;
1626 	netdev->features |= netdev->hw_features;
1627 	netdev->vlan_features |= netdev->features & ~NETIF_F_VLAN_FEATURES;
1628 
1629 	netdev->priv_flags |= IFF_UNICAST_FLT |
1630 			      IFF_LIVE_ADDR_CHANGE;
1631 
1632 	netdev->xdp_features = NETDEV_XDP_ACT_BASIC    |
1633 			       NETDEV_XDP_ACT_REDIRECT |
1634 			       NETDEV_XDP_ACT_RX_SG    |
1635 			       NETDEV_XDP_ACT_NDO_XMIT |
1636 			       NETDEV_XDP_ACT_NDO_XMIT_SG;
1637 
1638 	return 0;
1639 }
1640 
1641 static int ionic_set_features(struct net_device *netdev,
1642 			      netdev_features_t features)
1643 {
1644 	struct ionic_lif *lif = netdev_priv(netdev);
1645 	int err;
1646 
1647 	netdev_dbg(netdev, "%s: lif->features=0x%08llx new_features=0x%08llx\n",
1648 		   __func__, (u64)lif->netdev->features, (u64)features);
1649 
1650 	err = ionic_set_nic_features(lif, features);
1651 
1652 	return err;
1653 }
1654 
1655 static int ionic_set_attr_mac(struct ionic_lif *lif, u8 *mac)
1656 {
1657 	struct ionic_admin_ctx ctx = {
1658 		.work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
1659 		.cmd.lif_setattr = {
1660 			.opcode = IONIC_CMD_LIF_SETATTR,
1661 			.index = cpu_to_le16(lif->index),
1662 			.attr = IONIC_LIF_ATTR_MAC,
1663 		},
1664 	};
1665 
1666 	ether_addr_copy(ctx.cmd.lif_setattr.mac, mac);
1667 	return ionic_adminq_post_wait(lif, &ctx);
1668 }
1669 
1670 static int ionic_get_attr_mac(struct ionic_lif *lif, u8 *mac_addr)
1671 {
1672 	struct ionic_admin_ctx ctx = {
1673 		.work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
1674 		.cmd.lif_getattr = {
1675 			.opcode = IONIC_CMD_LIF_GETATTR,
1676 			.index = cpu_to_le16(lif->index),
1677 			.attr = IONIC_LIF_ATTR_MAC,
1678 		},
1679 	};
1680 	int err;
1681 
1682 	err = ionic_adminq_post_wait(lif, &ctx);
1683 	if (err)
1684 		return err;
1685 
1686 	ether_addr_copy(mac_addr, ctx.comp.lif_getattr.mac);
1687 	return 0;
1688 }
1689 
1690 static int ionic_program_mac(struct ionic_lif *lif, u8 *mac)
1691 {
1692 	u8  get_mac[ETH_ALEN];
1693 	int err;
1694 
1695 	err = ionic_set_attr_mac(lif, mac);
1696 	if (err)
1697 		return err;
1698 
1699 	err = ionic_get_attr_mac(lif, get_mac);
1700 	if (err)
1701 		return err;
1702 
1703 	/* To deal with older firmware that silently ignores the set attr mac:
1704 	 * doesn't actually change the mac and doesn't return an error, so we
1705 	 * do the get attr to verify whether or not the set actually happened
1706 	 */
1707 	if (!ether_addr_equal(get_mac, mac))
1708 		return 1;
1709 
1710 	return 0;
1711 }
1712 
1713 static int ionic_set_mac_address(struct net_device *netdev, void *sa)
1714 {
1715 	struct ionic_lif *lif = netdev_priv(netdev);
1716 	struct sockaddr *addr = sa;
1717 	u8 *mac;
1718 	int err;
1719 
1720 	mac = (u8 *)addr->sa_data;
1721 	if (ether_addr_equal(netdev->dev_addr, mac))
1722 		return 0;
1723 
1724 	/* Only program macs for virtual functions to avoid losing the permanent
1725 	 * Mac across warm reset/reboot.
1726 	 */
1727 	if (lif->ionic->pdev->is_virtfn) {
1728 		err = ionic_program_mac(lif, mac);
1729 		if (err < 0)
1730 			return err;
1731 
1732 		if (err > 0)
1733 			netdev_dbg(netdev, "%s: SET and GET ATTR Mac are not equal-due to old FW running\n",
1734 				   __func__);
1735 	}
1736 
1737 	err = eth_prepare_mac_addr_change(netdev, addr);
1738 	if (err)
1739 		return err;
1740 
1741 	if (!is_zero_ether_addr(netdev->dev_addr)) {
1742 		netdev_info(netdev, "deleting mac addr %pM\n",
1743 			    netdev->dev_addr);
1744 		ionic_lif_addr_del(netdev_priv(netdev), netdev->dev_addr);
1745 	}
1746 
1747 	eth_commit_mac_addr_change(netdev, addr);
1748 	netdev_info(netdev, "updating mac addr %pM\n", mac);
1749 
1750 	return ionic_lif_addr_add(netdev_priv(netdev), mac);
1751 }
1752 
1753 void ionic_stop_queues_reconfig(struct ionic_lif *lif)
1754 {
1755 	/* Stop and clean the queues before reconfiguration */
1756 	netif_device_detach(lif->netdev);
1757 	ionic_stop_queues(lif);
1758 	ionic_txrx_deinit(lif);
1759 }
1760 
1761 static int ionic_start_queues_reconfig(struct ionic_lif *lif)
1762 {
1763 	int err;
1764 
1765 	/* Re-init the queues after reconfiguration */
1766 
1767 	/* The only way txrx_init can fail here is if communication
1768 	 * with FW is suddenly broken.  There's not much we can do
1769 	 * at this point - error messages have already been printed,
1770 	 * so we can continue on and the user can eventually do a
1771 	 * DOWN and UP to try to reset and clear the issue.
1772 	 */
1773 	err = ionic_txrx_init(lif);
1774 	ionic_link_status_check_request(lif, CAN_NOT_SLEEP);
1775 	netif_device_attach(lif->netdev);
1776 
1777 	return err;
1778 }
1779 
1780 static bool ionic_xdp_is_valid_mtu(struct ionic_lif *lif, u32 mtu,
1781 				   struct bpf_prog *xdp_prog)
1782 {
1783 	if (!xdp_prog)
1784 		return true;
1785 
1786 	if (mtu <= IONIC_XDP_MAX_LINEAR_MTU)
1787 		return true;
1788 
1789 	if (xdp_prog->aux && xdp_prog->aux->xdp_has_frags)
1790 		return true;
1791 
1792 	return false;
1793 }
1794 
1795 static int ionic_change_mtu(struct net_device *netdev, int new_mtu)
1796 {
1797 	struct ionic_lif *lif = netdev_priv(netdev);
1798 	struct ionic_admin_ctx ctx = {
1799 		.work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
1800 		.cmd.lif_setattr = {
1801 			.opcode = IONIC_CMD_LIF_SETATTR,
1802 			.index = cpu_to_le16(lif->index),
1803 			.attr = IONIC_LIF_ATTR_MTU,
1804 			.mtu = cpu_to_le32(new_mtu),
1805 		},
1806 	};
1807 	struct bpf_prog *xdp_prog;
1808 	int err;
1809 
1810 	xdp_prog = READ_ONCE(lif->xdp_prog);
1811 	if (!ionic_xdp_is_valid_mtu(lif, new_mtu, xdp_prog))
1812 		return -EINVAL;
1813 
1814 	err = ionic_adminq_post_wait(lif, &ctx);
1815 	if (err)
1816 		return err;
1817 
1818 	/* if we're not running, nothing more to do */
1819 	if (!netif_running(netdev)) {
1820 		WRITE_ONCE(netdev->mtu, new_mtu);
1821 		return 0;
1822 	}
1823 
1824 	mutex_lock(&lif->queue_lock);
1825 	ionic_stop_queues_reconfig(lif);
1826 	WRITE_ONCE(netdev->mtu, new_mtu);
1827 	err = ionic_start_queues_reconfig(lif);
1828 	mutex_unlock(&lif->queue_lock);
1829 
1830 	return err;
1831 }
1832 
1833 static void ionic_tx_timeout_work(struct work_struct *ws)
1834 {
1835 	struct ionic_lif *lif = container_of(ws, struct ionic_lif, tx_timeout_work);
1836 	int err;
1837 
1838 	if (test_bit(IONIC_LIF_F_FW_RESET, lif->state))
1839 		return;
1840 
1841 	/* if we were stopped before this scheduled job was launched,
1842 	 * don't bother the queues as they are already stopped.
1843 	 */
1844 	if (!netif_running(lif->netdev))
1845 		return;
1846 
1847 	mutex_lock(&lif->queue_lock);
1848 	ionic_stop_queues_reconfig(lif);
1849 	err = ionic_start_queues_reconfig(lif);
1850 	mutex_unlock(&lif->queue_lock);
1851 
1852 	if (err)
1853 		dev_err(lif->ionic->dev, "%s: Restarting queues failed\n", __func__);
1854 }
1855 
1856 static void ionic_tx_timeout(struct net_device *netdev, unsigned int txqueue)
1857 {
1858 	struct ionic_lif *lif = netdev_priv(netdev);
1859 
1860 	netdev_info(lif->netdev, "Tx Timeout triggered - txq %d\n", txqueue);
1861 	schedule_work(&lif->tx_timeout_work);
1862 }
1863 
1864 static int ionic_vlan_rx_add_vid(struct net_device *netdev, __be16 proto,
1865 				 u16 vid)
1866 {
1867 	struct ionic_lif *lif = netdev_priv(netdev);
1868 	int err;
1869 
1870 	err = ionic_lif_vlan_add(lif, vid);
1871 	if (err)
1872 		return err;
1873 
1874 	ionic_lif_rx_mode(lif);
1875 
1876 	return 0;
1877 }
1878 
1879 static int ionic_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto,
1880 				  u16 vid)
1881 {
1882 	struct ionic_lif *lif = netdev_priv(netdev);
1883 	int err;
1884 
1885 	err = ionic_lif_vlan_del(lif, vid);
1886 	if (err)
1887 		return err;
1888 
1889 	ionic_lif_rx_mode(lif);
1890 
1891 	return 0;
1892 }
1893 
1894 int ionic_lif_rss_config(struct ionic_lif *lif, const u16 types,
1895 			 const u8 *key, const u32 *indir)
1896 {
1897 	struct ionic_admin_ctx ctx = {
1898 		.work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
1899 		.cmd.lif_setattr = {
1900 			.opcode = IONIC_CMD_LIF_SETATTR,
1901 			.attr = IONIC_LIF_ATTR_RSS,
1902 			.rss.addr = cpu_to_le64(lif->rss_ind_tbl_pa),
1903 		},
1904 	};
1905 	unsigned int i, tbl_sz;
1906 
1907 	if (lif->hw_features & IONIC_ETH_HW_RX_HASH) {
1908 		lif->rss_types = types;
1909 		ctx.cmd.lif_setattr.rss.types = cpu_to_le16(types);
1910 	}
1911 
1912 	if (key)
1913 		memcpy(lif->rss_hash_key, key, IONIC_RSS_HASH_KEY_SIZE);
1914 
1915 	if (indir) {
1916 		tbl_sz = le16_to_cpu(lif->ionic->ident.lif.eth.rss_ind_tbl_sz);
1917 		for (i = 0; i < tbl_sz; i++)
1918 			lif->rss_ind_tbl[i] = indir[i];
1919 	}
1920 
1921 	memcpy(ctx.cmd.lif_setattr.rss.key, lif->rss_hash_key,
1922 	       IONIC_RSS_HASH_KEY_SIZE);
1923 
1924 	return ionic_adminq_post_wait(lif, &ctx);
1925 }
1926 
1927 static int ionic_lif_rss_init(struct ionic_lif *lif)
1928 {
1929 	unsigned int tbl_sz;
1930 	unsigned int i;
1931 
1932 	lif->rss_types = IONIC_RSS_TYPE_IPV4     |
1933 			 IONIC_RSS_TYPE_IPV4_TCP |
1934 			 IONIC_RSS_TYPE_IPV4_UDP |
1935 			 IONIC_RSS_TYPE_IPV6     |
1936 			 IONIC_RSS_TYPE_IPV6_TCP |
1937 			 IONIC_RSS_TYPE_IPV6_UDP;
1938 
1939 	/* Fill indirection table with 'default' values */
1940 	tbl_sz = le16_to_cpu(lif->ionic->ident.lif.eth.rss_ind_tbl_sz);
1941 	for (i = 0; i < tbl_sz; i++)
1942 		lif->rss_ind_tbl[i] = ethtool_rxfh_indir_default(i, lif->nxqs);
1943 
1944 	return ionic_lif_rss_config(lif, lif->rss_types, NULL, NULL);
1945 }
1946 
1947 static void ionic_lif_rss_deinit(struct ionic_lif *lif)
1948 {
1949 	int tbl_sz;
1950 
1951 	tbl_sz = le16_to_cpu(lif->ionic->ident.lif.eth.rss_ind_tbl_sz);
1952 	memset(lif->rss_ind_tbl, 0, tbl_sz);
1953 	memset(lif->rss_hash_key, 0, IONIC_RSS_HASH_KEY_SIZE);
1954 
1955 	ionic_lif_rss_config(lif, 0x0, NULL, NULL);
1956 }
1957 
1958 static void ionic_lif_quiesce(struct ionic_lif *lif)
1959 {
1960 	struct ionic_admin_ctx ctx = {
1961 		.work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
1962 		.cmd.lif_setattr = {
1963 			.opcode = IONIC_CMD_LIF_SETATTR,
1964 			.index = cpu_to_le16(lif->index),
1965 			.attr = IONIC_LIF_ATTR_STATE,
1966 			.state = IONIC_LIF_QUIESCE,
1967 		},
1968 	};
1969 	int err;
1970 
1971 	err = ionic_adminq_post_wait(lif, &ctx);
1972 	if (err)
1973 		netdev_dbg(lif->netdev, "lif quiesce failed %d\n", err);
1974 }
1975 
1976 static void ionic_txrx_disable(struct ionic_lif *lif)
1977 {
1978 	unsigned int i;
1979 	int err = 0;
1980 
1981 	if (lif->txqcqs) {
1982 		for (i = 0; i < lif->nxqs; i++)
1983 			err = ionic_qcq_disable(lif, lif->txqcqs[i], err);
1984 	}
1985 
1986 	if (lif->hwstamp_txq)
1987 		err = ionic_qcq_disable(lif, lif->hwstamp_txq, err);
1988 
1989 	if (lif->rxqcqs) {
1990 		for (i = 0; i < lif->nxqs; i++)
1991 			err = ionic_qcq_disable(lif, lif->rxqcqs[i], err);
1992 	}
1993 
1994 	if (lif->hwstamp_rxq)
1995 		err = ionic_qcq_disable(lif, lif->hwstamp_rxq, err);
1996 
1997 	ionic_lif_quiesce(lif);
1998 }
1999 
2000 static void ionic_txrx_deinit(struct ionic_lif *lif)
2001 {
2002 	unsigned int i;
2003 
2004 	if (lif->txqcqs) {
2005 		for (i = 0; i < lif->nxqs && lif->txqcqs[i]; i++) {
2006 			ionic_lif_qcq_deinit(lif, lif->txqcqs[i]);
2007 			ionic_tx_flush(&lif->txqcqs[i]->cq);
2008 			ionic_tx_empty(&lif->txqcqs[i]->q);
2009 		}
2010 	}
2011 
2012 	if (lif->rxqcqs) {
2013 		for (i = 0; i < lif->nxqs && lif->rxqcqs[i]; i++) {
2014 			ionic_lif_qcq_deinit(lif, lif->rxqcqs[i]);
2015 			ionic_rx_empty(&lif->rxqcqs[i]->q);
2016 		}
2017 	}
2018 	lif->rx_mode = 0;
2019 
2020 	if (lif->hwstamp_txq) {
2021 		ionic_lif_qcq_deinit(lif, lif->hwstamp_txq);
2022 		ionic_tx_flush(&lif->hwstamp_txq->cq);
2023 		ionic_tx_empty(&lif->hwstamp_txq->q);
2024 	}
2025 
2026 	if (lif->hwstamp_rxq) {
2027 		ionic_lif_qcq_deinit(lif, lif->hwstamp_rxq);
2028 		ionic_rx_empty(&lif->hwstamp_rxq->q);
2029 	}
2030 }
2031 
2032 void ionic_txrx_free(struct ionic_lif *lif)
2033 {
2034 	unsigned int i;
2035 
2036 	if (lif->txqcqs) {
2037 		for (i = 0; i < lif->ionic->ntxqs_per_lif && lif->txqcqs[i]; i++) {
2038 			ionic_qcq_free(lif, lif->txqcqs[i]);
2039 			devm_kfree(lif->ionic->dev, lif->txqcqs[i]);
2040 			lif->txqcqs[i] = NULL;
2041 		}
2042 	}
2043 
2044 	if (lif->rxqcqs) {
2045 		for (i = 0; i < lif->ionic->nrxqs_per_lif && lif->rxqcqs[i]; i++) {
2046 			ionic_qcq_free(lif, lif->rxqcqs[i]);
2047 			devm_kfree(lif->ionic->dev, lif->rxqcqs[i]);
2048 			lif->rxqcqs[i] = NULL;
2049 		}
2050 	}
2051 
2052 	if (lif->hwstamp_txq) {
2053 		ionic_qcq_free(lif, lif->hwstamp_txq);
2054 		devm_kfree(lif->ionic->dev, lif->hwstamp_txq);
2055 		lif->hwstamp_txq = NULL;
2056 	}
2057 
2058 	if (lif->hwstamp_rxq) {
2059 		ionic_qcq_free(lif, lif->hwstamp_rxq);
2060 		devm_kfree(lif->ionic->dev, lif->hwstamp_rxq);
2061 		lif->hwstamp_rxq = NULL;
2062 	}
2063 }
2064 
2065 static int ionic_txrx_alloc(struct ionic_lif *lif)
2066 {
2067 	unsigned int comp_sz, desc_sz, num_desc, sg_desc_sz;
2068 	unsigned int flags, i;
2069 	int err = 0;
2070 
2071 	num_desc = lif->ntxq_descs;
2072 	desc_sz = sizeof(struct ionic_txq_desc);
2073 	comp_sz = sizeof(struct ionic_txq_comp);
2074 
2075 	if (lif->qtype_info[IONIC_QTYPE_TXQ].version >= 1 &&
2076 	    lif->qtype_info[IONIC_QTYPE_TXQ].sg_desc_sz ==
2077 					  sizeof(struct ionic_txq_sg_desc_v1))
2078 		sg_desc_sz = sizeof(struct ionic_txq_sg_desc_v1);
2079 	else
2080 		sg_desc_sz = sizeof(struct ionic_txq_sg_desc);
2081 
2082 	flags = IONIC_QCQ_F_TX_STATS | IONIC_QCQ_F_SG;
2083 
2084 	if (test_bit(IONIC_LIF_F_CMB_TX_RINGS, lif->state))
2085 		flags |= IONIC_QCQ_F_CMB_RINGS;
2086 
2087 	if (test_bit(IONIC_LIF_F_SPLIT_INTR, lif->state))
2088 		flags |= IONIC_QCQ_F_INTR;
2089 
2090 	for (i = 0; i < lif->nxqs; i++) {
2091 		err = ionic_qcq_alloc(lif, IONIC_QTYPE_TXQ, i, "tx", flags,
2092 				      num_desc, desc_sz, comp_sz, sg_desc_sz,
2093 				      sizeof(struct ionic_tx_desc_info),
2094 				      lif->kern_pid, NULL, &lif->txqcqs[i]);
2095 		if (err)
2096 			goto err_out;
2097 
2098 		if (flags & IONIC_QCQ_F_INTR) {
2099 			ionic_intr_coal_init(lif->ionic->idev.intr_ctrl,
2100 					     lif->txqcqs[i]->intr.index,
2101 					     lif->tx_coalesce_hw);
2102 			if (test_bit(IONIC_LIF_F_TX_DIM_INTR, lif->state))
2103 				lif->txqcqs[i]->intr.dim_coal_hw = lif->tx_coalesce_hw;
2104 		}
2105 
2106 		ionic_debugfs_add_qcq(lif, lif->txqcqs[i]);
2107 	}
2108 
2109 	flags = IONIC_QCQ_F_RX_STATS | IONIC_QCQ_F_SG | IONIC_QCQ_F_INTR;
2110 
2111 	if (test_bit(IONIC_LIF_F_CMB_RX_RINGS, lif->state))
2112 		flags |= IONIC_QCQ_F_CMB_RINGS;
2113 
2114 	num_desc = lif->nrxq_descs;
2115 	desc_sz = sizeof(struct ionic_rxq_desc);
2116 	comp_sz = sizeof(struct ionic_rxq_comp);
2117 	sg_desc_sz = sizeof(struct ionic_rxq_sg_desc);
2118 
2119 	if (lif->rxq_features & IONIC_Q_F_2X_CQ_DESC)
2120 		comp_sz *= 2;
2121 
2122 	for (i = 0; i < lif->nxqs; i++) {
2123 		err = ionic_qcq_alloc(lif, IONIC_QTYPE_RXQ, i, "rx", flags,
2124 				      num_desc, desc_sz, comp_sz, sg_desc_sz,
2125 				      sizeof(struct ionic_rx_desc_info),
2126 				      lif->kern_pid, lif->xdp_prog,
2127 				      &lif->rxqcqs[i]);
2128 		if (err)
2129 			goto err_out;
2130 
2131 		lif->rxqcqs[i]->q.features = lif->rxq_features;
2132 
2133 		ionic_intr_coal_init(lif->ionic->idev.intr_ctrl,
2134 				     lif->rxqcqs[i]->intr.index,
2135 				     lif->rx_coalesce_hw);
2136 		if (test_bit(IONIC_LIF_F_RX_DIM_INTR, lif->state))
2137 			lif->rxqcqs[i]->intr.dim_coal_hw = lif->rx_coalesce_hw;
2138 
2139 		if (!test_bit(IONIC_LIF_F_SPLIT_INTR, lif->state))
2140 			ionic_link_qcq_interrupts(lif->rxqcqs[i],
2141 						  lif->txqcqs[i]);
2142 
2143 		ionic_debugfs_add_qcq(lif, lif->rxqcqs[i]);
2144 	}
2145 
2146 	return 0;
2147 
2148 err_out:
2149 	ionic_txrx_free(lif);
2150 
2151 	return err;
2152 }
2153 
2154 static int ionic_txrx_init(struct ionic_lif *lif)
2155 {
2156 	unsigned int i;
2157 	int err;
2158 
2159 	for (i = 0; i < lif->nxqs; i++) {
2160 		err = ionic_lif_txq_init(lif, lif->txqcqs[i]);
2161 		if (err)
2162 			goto err_out;
2163 
2164 		err = ionic_lif_rxq_init(lif, lif->rxqcqs[i]);
2165 		if (err) {
2166 			ionic_lif_qcq_deinit(lif, lif->txqcqs[i]);
2167 			goto err_out;
2168 		}
2169 	}
2170 
2171 	if (lif->netdev->features & NETIF_F_RXHASH)
2172 		ionic_lif_rss_init(lif);
2173 
2174 	ionic_lif_rx_mode(lif);
2175 
2176 	return 0;
2177 
2178 err_out:
2179 	while (i--) {
2180 		ionic_lif_qcq_deinit(lif, lif->txqcqs[i]);
2181 		ionic_lif_qcq_deinit(lif, lif->rxqcqs[i]);
2182 	}
2183 
2184 	return err;
2185 }
2186 
2187 static int ionic_txrx_enable(struct ionic_lif *lif)
2188 {
2189 	int derr = 0;
2190 	int i, err;
2191 
2192 	ionic_xdp_rxqs_prog_update(lif);
2193 
2194 	for (i = 0; i < lif->nxqs; i++) {
2195 		if (!(lif->rxqcqs[i] && lif->txqcqs[i])) {
2196 			dev_err(lif->ionic->dev, "%s: bad qcq %d\n", __func__, i);
2197 			err = -ENXIO;
2198 			goto err_out;
2199 		}
2200 
2201 		ionic_rx_fill(&lif->rxqcqs[i]->q,
2202 			      READ_ONCE(lif->rxqcqs[i]->q.xdp_prog));
2203 		err = ionic_qcq_enable(lif->rxqcqs[i]);
2204 		if (err)
2205 			goto err_out;
2206 
2207 		err = ionic_qcq_enable(lif->txqcqs[i]);
2208 		if (err) {
2209 			derr = ionic_qcq_disable(lif, lif->rxqcqs[i], err);
2210 			goto err_out;
2211 		}
2212 	}
2213 
2214 	if (lif->hwstamp_rxq) {
2215 		ionic_rx_fill(&lif->hwstamp_rxq->q, NULL);
2216 		err = ionic_qcq_enable(lif->hwstamp_rxq);
2217 		if (err)
2218 			goto err_out_hwstamp_rx;
2219 	}
2220 
2221 	if (lif->hwstamp_txq) {
2222 		err = ionic_qcq_enable(lif->hwstamp_txq);
2223 		if (err)
2224 			goto err_out_hwstamp_tx;
2225 	}
2226 
2227 	return 0;
2228 
2229 err_out_hwstamp_tx:
2230 	if (lif->hwstamp_rxq)
2231 		derr = ionic_qcq_disable(lif, lif->hwstamp_rxq, derr);
2232 err_out_hwstamp_rx:
2233 	i = lif->nxqs;
2234 err_out:
2235 	while (i--) {
2236 		derr = ionic_qcq_disable(lif, lif->txqcqs[i], derr);
2237 		derr = ionic_qcq_disable(lif, lif->rxqcqs[i], derr);
2238 	}
2239 
2240 	ionic_xdp_rxqs_prog_update(lif);
2241 
2242 	return err;
2243 }
2244 
2245 static int ionic_start_queues(struct ionic_lif *lif)
2246 {
2247 	int err;
2248 
2249 	if (test_bit(IONIC_LIF_F_BROKEN, lif->state))
2250 		return -EIO;
2251 
2252 	if (test_bit(IONIC_LIF_F_FW_RESET, lif->state))
2253 		return -EBUSY;
2254 
2255 	if (test_and_set_bit(IONIC_LIF_F_UP, lif->state))
2256 		return 0;
2257 
2258 	err = ionic_txrx_enable(lif);
2259 	if (err) {
2260 		clear_bit(IONIC_LIF_F_UP, lif->state);
2261 		return err;
2262 	}
2263 	netif_tx_wake_all_queues(lif->netdev);
2264 
2265 	return 0;
2266 }
2267 
2268 static int ionic_open(struct net_device *netdev)
2269 {
2270 	struct ionic_lif *lif = netdev_priv(netdev);
2271 	int err;
2272 
2273 	/* If recovering from a broken state, clear the bit and we'll try again */
2274 	if (test_and_clear_bit(IONIC_LIF_F_BROKEN, lif->state))
2275 		netdev_info(netdev, "clearing broken state\n");
2276 
2277 	mutex_lock(&lif->queue_lock);
2278 
2279 	err = ionic_txrx_alloc(lif);
2280 	if (err)
2281 		goto err_unlock;
2282 
2283 	err = ionic_txrx_init(lif);
2284 	if (err)
2285 		goto err_txrx_free;
2286 
2287 	err = netif_set_real_num_tx_queues(netdev, lif->nxqs);
2288 	if (err)
2289 		goto err_txrx_deinit;
2290 
2291 	err = netif_set_real_num_rx_queues(netdev, lif->nxqs);
2292 	if (err)
2293 		goto err_txrx_deinit;
2294 
2295 	/* don't start the queues until we have link */
2296 	if (netif_carrier_ok(netdev)) {
2297 		err = ionic_start_queues(lif);
2298 		if (err)
2299 			goto err_txrx_deinit;
2300 	}
2301 
2302 	/* If hardware timestamping is enabled, but the queues were freed by
2303 	 * ionic_stop, those need to be reallocated and initialized, too.
2304 	 */
2305 	ionic_lif_hwstamp_recreate_queues(lif);
2306 
2307 	mutex_unlock(&lif->queue_lock);
2308 
2309 	return 0;
2310 
2311 err_txrx_deinit:
2312 	ionic_txrx_deinit(lif);
2313 err_txrx_free:
2314 	ionic_txrx_free(lif);
2315 err_unlock:
2316 	mutex_unlock(&lif->queue_lock);
2317 	return err;
2318 }
2319 
2320 static void ionic_stop_queues(struct ionic_lif *lif)
2321 {
2322 	if (!test_and_clear_bit(IONIC_LIF_F_UP, lif->state))
2323 		return;
2324 
2325 	netif_tx_disable(lif->netdev);
2326 	ionic_txrx_disable(lif);
2327 }
2328 
2329 static int ionic_stop(struct net_device *netdev)
2330 {
2331 	struct ionic_lif *lif = netdev_priv(netdev);
2332 
2333 	if (test_bit(IONIC_LIF_F_FW_RESET, lif->state))
2334 		return 0;
2335 
2336 	mutex_lock(&lif->queue_lock);
2337 	ionic_stop_queues(lif);
2338 	ionic_txrx_deinit(lif);
2339 	ionic_txrx_free(lif);
2340 	mutex_unlock(&lif->queue_lock);
2341 
2342 	return 0;
2343 }
2344 
2345 static int ionic_get_vf_config(struct net_device *netdev,
2346 			       int vf, struct ifla_vf_info *ivf)
2347 {
2348 	struct ionic_lif *lif = netdev_priv(netdev);
2349 	struct ionic *ionic = lif->ionic;
2350 	int ret = 0;
2351 
2352 	if (!netif_device_present(netdev))
2353 		return -EBUSY;
2354 
2355 	down_read(&ionic->vf_op_lock);
2356 
2357 	if (vf >= pci_num_vf(ionic->pdev) || !ionic->vfs) {
2358 		ret = -EINVAL;
2359 	} else {
2360 		struct ionic_vf *vfdata = &ionic->vfs[vf];
2361 
2362 		ivf->vf		  = vf;
2363 		ivf->qos	  = 0;
2364 		ivf->vlan         = le16_to_cpu(vfdata->vlanid);
2365 		ivf->spoofchk     = vfdata->spoofchk;
2366 		ivf->linkstate    = vfdata->linkstate;
2367 		ivf->max_tx_rate  = le32_to_cpu(vfdata->maxrate);
2368 		ivf->trusted      = vfdata->trusted;
2369 		ether_addr_copy(ivf->mac, vfdata->macaddr);
2370 	}
2371 
2372 	up_read(&ionic->vf_op_lock);
2373 	return ret;
2374 }
2375 
2376 static int ionic_get_vf_stats(struct net_device *netdev, int vf,
2377 			      struct ifla_vf_stats *vf_stats)
2378 {
2379 	struct ionic_lif *lif = netdev_priv(netdev);
2380 	struct ionic *ionic = lif->ionic;
2381 	struct ionic_lif_stats *vs;
2382 	int ret = 0;
2383 
2384 	if (!netif_device_present(netdev))
2385 		return -EBUSY;
2386 
2387 	down_read(&ionic->vf_op_lock);
2388 
2389 	if (vf >= pci_num_vf(ionic->pdev) || !ionic->vfs) {
2390 		ret = -EINVAL;
2391 	} else {
2392 		memset(vf_stats, 0, sizeof(*vf_stats));
2393 		vs = &ionic->vfs[vf].stats;
2394 
2395 		vf_stats->rx_packets = le64_to_cpu(vs->rx_ucast_packets);
2396 		vf_stats->tx_packets = le64_to_cpu(vs->tx_ucast_packets);
2397 		vf_stats->rx_bytes   = le64_to_cpu(vs->rx_ucast_bytes);
2398 		vf_stats->tx_bytes   = le64_to_cpu(vs->tx_ucast_bytes);
2399 		vf_stats->broadcast  = le64_to_cpu(vs->rx_bcast_packets);
2400 		vf_stats->multicast  = le64_to_cpu(vs->rx_mcast_packets);
2401 		vf_stats->rx_dropped = le64_to_cpu(vs->rx_ucast_drop_packets) +
2402 				       le64_to_cpu(vs->rx_mcast_drop_packets) +
2403 				       le64_to_cpu(vs->rx_bcast_drop_packets);
2404 		vf_stats->tx_dropped = le64_to_cpu(vs->tx_ucast_drop_packets) +
2405 				       le64_to_cpu(vs->tx_mcast_drop_packets) +
2406 				       le64_to_cpu(vs->tx_bcast_drop_packets);
2407 	}
2408 
2409 	up_read(&ionic->vf_op_lock);
2410 	return ret;
2411 }
2412 
2413 static int ionic_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
2414 {
2415 	struct ionic_vf_setattr_cmd vfc = { .attr = IONIC_VF_ATTR_MAC };
2416 	struct ionic_lif *lif = netdev_priv(netdev);
2417 	struct ionic *ionic = lif->ionic;
2418 	int ret;
2419 
2420 	if (!(is_zero_ether_addr(mac) || is_valid_ether_addr(mac)))
2421 		return -EINVAL;
2422 
2423 	if (!netif_device_present(netdev))
2424 		return -EBUSY;
2425 
2426 	down_write(&ionic->vf_op_lock);
2427 
2428 	if (vf >= pci_num_vf(ionic->pdev) || !ionic->vfs) {
2429 		ret = -EINVAL;
2430 	} else {
2431 		ether_addr_copy(vfc.macaddr, mac);
2432 		dev_dbg(ionic->dev, "%s: vf %d macaddr %pM\n",
2433 			__func__, vf, vfc.macaddr);
2434 
2435 		ret = ionic_set_vf_config(ionic, vf, &vfc);
2436 		if (!ret)
2437 			ether_addr_copy(ionic->vfs[vf].macaddr, mac);
2438 	}
2439 
2440 	up_write(&ionic->vf_op_lock);
2441 	return ret;
2442 }
2443 
2444 static int ionic_set_vf_vlan(struct net_device *netdev, int vf, u16 vlan,
2445 			     u8 qos, __be16 proto)
2446 {
2447 	struct ionic_vf_setattr_cmd vfc = { .attr = IONIC_VF_ATTR_VLAN };
2448 	struct ionic_lif *lif = netdev_priv(netdev);
2449 	struct ionic *ionic = lif->ionic;
2450 	int ret;
2451 
2452 	/* until someday when we support qos */
2453 	if (qos)
2454 		return -EINVAL;
2455 
2456 	if (vlan > 4095)
2457 		return -EINVAL;
2458 
2459 	if (proto != htons(ETH_P_8021Q))
2460 		return -EPROTONOSUPPORT;
2461 
2462 	if (!netif_device_present(netdev))
2463 		return -EBUSY;
2464 
2465 	down_write(&ionic->vf_op_lock);
2466 
2467 	if (vf >= pci_num_vf(ionic->pdev) || !ionic->vfs) {
2468 		ret = -EINVAL;
2469 	} else {
2470 		vfc.vlanid = cpu_to_le16(vlan);
2471 		dev_dbg(ionic->dev, "%s: vf %d vlan %d\n",
2472 			__func__, vf, le16_to_cpu(vfc.vlanid));
2473 
2474 		ret = ionic_set_vf_config(ionic, vf, &vfc);
2475 		if (!ret)
2476 			ionic->vfs[vf].vlanid = cpu_to_le16(vlan);
2477 	}
2478 
2479 	up_write(&ionic->vf_op_lock);
2480 	return ret;
2481 }
2482 
2483 static int ionic_set_vf_rate(struct net_device *netdev, int vf,
2484 			     int tx_min, int tx_max)
2485 {
2486 	struct ionic_vf_setattr_cmd vfc = { .attr = IONIC_VF_ATTR_RATE };
2487 	struct ionic_lif *lif = netdev_priv(netdev);
2488 	struct ionic *ionic = lif->ionic;
2489 	int ret;
2490 
2491 	/* setting the min just seems silly */
2492 	if (tx_min)
2493 		return -EINVAL;
2494 
2495 	if (!netif_device_present(netdev))
2496 		return -EBUSY;
2497 
2498 	down_write(&ionic->vf_op_lock);
2499 
2500 	if (vf >= pci_num_vf(ionic->pdev) || !ionic->vfs) {
2501 		ret = -EINVAL;
2502 	} else {
2503 		vfc.maxrate = cpu_to_le32(tx_max);
2504 		dev_dbg(ionic->dev, "%s: vf %d maxrate %d\n",
2505 			__func__, vf, le32_to_cpu(vfc.maxrate));
2506 
2507 		ret = ionic_set_vf_config(ionic, vf, &vfc);
2508 		if (!ret)
2509 			ionic->vfs[vf].maxrate = cpu_to_le32(tx_max);
2510 	}
2511 
2512 	up_write(&ionic->vf_op_lock);
2513 	return ret;
2514 }
2515 
2516 static int ionic_set_vf_spoofchk(struct net_device *netdev, int vf, bool set)
2517 {
2518 	struct ionic_vf_setattr_cmd vfc = { .attr = IONIC_VF_ATTR_SPOOFCHK };
2519 	struct ionic_lif *lif = netdev_priv(netdev);
2520 	struct ionic *ionic = lif->ionic;
2521 	int ret;
2522 
2523 	if (!netif_device_present(netdev))
2524 		return -EBUSY;
2525 
2526 	down_write(&ionic->vf_op_lock);
2527 
2528 	if (vf >= pci_num_vf(ionic->pdev) || !ionic->vfs) {
2529 		ret = -EINVAL;
2530 	} else {
2531 		vfc.spoofchk = set;
2532 		dev_dbg(ionic->dev, "%s: vf %d spoof %d\n",
2533 			__func__, vf, vfc.spoofchk);
2534 
2535 		ret = ionic_set_vf_config(ionic, vf, &vfc);
2536 		if (!ret)
2537 			ionic->vfs[vf].spoofchk = set;
2538 	}
2539 
2540 	up_write(&ionic->vf_op_lock);
2541 	return ret;
2542 }
2543 
2544 static int ionic_set_vf_trust(struct net_device *netdev, int vf, bool set)
2545 {
2546 	struct ionic_vf_setattr_cmd vfc = { .attr = IONIC_VF_ATTR_TRUST };
2547 	struct ionic_lif *lif = netdev_priv(netdev);
2548 	struct ionic *ionic = lif->ionic;
2549 	int ret;
2550 
2551 	if (!netif_device_present(netdev))
2552 		return -EBUSY;
2553 
2554 	down_write(&ionic->vf_op_lock);
2555 
2556 	if (vf >= pci_num_vf(ionic->pdev) || !ionic->vfs) {
2557 		ret = -EINVAL;
2558 	} else {
2559 		vfc.trust = set;
2560 		dev_dbg(ionic->dev, "%s: vf %d trust %d\n",
2561 			__func__, vf, vfc.trust);
2562 
2563 		ret = ionic_set_vf_config(ionic, vf, &vfc);
2564 		if (!ret)
2565 			ionic->vfs[vf].trusted = set;
2566 	}
2567 
2568 	up_write(&ionic->vf_op_lock);
2569 	return ret;
2570 }
2571 
2572 static int ionic_set_vf_link_state(struct net_device *netdev, int vf, int set)
2573 {
2574 	struct ionic_vf_setattr_cmd vfc = { .attr = IONIC_VF_ATTR_LINKSTATE };
2575 	struct ionic_lif *lif = netdev_priv(netdev);
2576 	struct ionic *ionic = lif->ionic;
2577 	u8 vfls;
2578 	int ret;
2579 
2580 	switch (set) {
2581 	case IFLA_VF_LINK_STATE_ENABLE:
2582 		vfls = IONIC_VF_LINK_STATUS_UP;
2583 		break;
2584 	case IFLA_VF_LINK_STATE_DISABLE:
2585 		vfls = IONIC_VF_LINK_STATUS_DOWN;
2586 		break;
2587 	case IFLA_VF_LINK_STATE_AUTO:
2588 		vfls = IONIC_VF_LINK_STATUS_AUTO;
2589 		break;
2590 	default:
2591 		return -EINVAL;
2592 	}
2593 
2594 	if (!netif_device_present(netdev))
2595 		return -EBUSY;
2596 
2597 	down_write(&ionic->vf_op_lock);
2598 
2599 	if (vf >= pci_num_vf(ionic->pdev) || !ionic->vfs) {
2600 		ret = -EINVAL;
2601 	} else {
2602 		vfc.linkstate = vfls;
2603 		dev_dbg(ionic->dev, "%s: vf %d linkstate %d\n",
2604 			__func__, vf, vfc.linkstate);
2605 
2606 		ret = ionic_set_vf_config(ionic, vf, &vfc);
2607 		if (!ret)
2608 			ionic->vfs[vf].linkstate = set;
2609 	}
2610 
2611 	up_write(&ionic->vf_op_lock);
2612 	return ret;
2613 }
2614 
2615 static void ionic_vf_attr_replay(struct ionic_lif *lif)
2616 {
2617 	struct ionic_vf_setattr_cmd vfc = { };
2618 	struct ionic *ionic = lif->ionic;
2619 	struct ionic_vf *v;
2620 	int i;
2621 
2622 	if (!ionic->vfs)
2623 		return;
2624 
2625 	down_read(&ionic->vf_op_lock);
2626 
2627 	for (i = 0; i < ionic->num_vfs; i++) {
2628 		v = &ionic->vfs[i];
2629 
2630 		if (v->stats_pa) {
2631 			vfc.attr = IONIC_VF_ATTR_STATSADDR;
2632 			vfc.stats_pa = cpu_to_le64(v->stats_pa);
2633 			ionic_set_vf_config(ionic, i, &vfc);
2634 			vfc.stats_pa = 0;
2635 		}
2636 
2637 		if (!is_zero_ether_addr(v->macaddr)) {
2638 			vfc.attr = IONIC_VF_ATTR_MAC;
2639 			ether_addr_copy(vfc.macaddr, v->macaddr);
2640 			ionic_set_vf_config(ionic, i, &vfc);
2641 			eth_zero_addr(vfc.macaddr);
2642 		}
2643 
2644 		if (v->vlanid) {
2645 			vfc.attr = IONIC_VF_ATTR_VLAN;
2646 			vfc.vlanid = v->vlanid;
2647 			ionic_set_vf_config(ionic, i, &vfc);
2648 			vfc.vlanid = 0;
2649 		}
2650 
2651 		if (v->maxrate) {
2652 			vfc.attr = IONIC_VF_ATTR_RATE;
2653 			vfc.maxrate = v->maxrate;
2654 			ionic_set_vf_config(ionic, i, &vfc);
2655 			vfc.maxrate = 0;
2656 		}
2657 
2658 		if (v->spoofchk) {
2659 			vfc.attr = IONIC_VF_ATTR_SPOOFCHK;
2660 			vfc.spoofchk = v->spoofchk;
2661 			ionic_set_vf_config(ionic, i, &vfc);
2662 			vfc.spoofchk = 0;
2663 		}
2664 
2665 		if (v->trusted) {
2666 			vfc.attr = IONIC_VF_ATTR_TRUST;
2667 			vfc.trust = v->trusted;
2668 			ionic_set_vf_config(ionic, i, &vfc);
2669 			vfc.trust = 0;
2670 		}
2671 
2672 		if (v->linkstate) {
2673 			vfc.attr = IONIC_VF_ATTR_LINKSTATE;
2674 			vfc.linkstate = v->linkstate;
2675 			ionic_set_vf_config(ionic, i, &vfc);
2676 			vfc.linkstate = 0;
2677 		}
2678 	}
2679 
2680 	up_read(&ionic->vf_op_lock);
2681 
2682 	ionic_vf_start(ionic);
2683 }
2684 
2685 static void ionic_unregister_rxq_info(struct ionic_queue *q)
2686 {
2687 	struct xdp_rxq_info *xi;
2688 
2689 	if (!q->xdp_rxq_info)
2690 		return;
2691 
2692 	xi = q->xdp_rxq_info;
2693 	q->xdp_rxq_info = NULL;
2694 
2695 	xdp_rxq_info_unreg(xi);
2696 	kfree(xi);
2697 }
2698 
2699 static int ionic_register_rxq_info(struct ionic_queue *q, unsigned int napi_id)
2700 {
2701 	struct xdp_rxq_info *rxq_info;
2702 	int err;
2703 
2704 	rxq_info = kzalloc_obj(*rxq_info);
2705 	if (!rxq_info)
2706 		return -ENOMEM;
2707 
2708 	err = xdp_rxq_info_reg(rxq_info, q->lif->netdev, q->index, napi_id);
2709 	if (err) {
2710 		netdev_err(q->lif->netdev, "q%d xdp_rxq_info_reg failed, err %d\n",
2711 			   q->index, err);
2712 		goto err_out;
2713 	}
2714 
2715 	err = xdp_rxq_info_reg_mem_model(rxq_info, MEM_TYPE_PAGE_POOL, q->page_pool);
2716 	if (err) {
2717 		netdev_err(q->lif->netdev, "q%d xdp_rxq_info_reg_mem_model failed, err %d\n",
2718 			   q->index, err);
2719 		xdp_rxq_info_unreg(rxq_info);
2720 		goto err_out;
2721 	}
2722 
2723 	q->xdp_rxq_info = rxq_info;
2724 
2725 	return 0;
2726 
2727 err_out:
2728 	kfree(rxq_info);
2729 	return err;
2730 }
2731 
2732 static void ionic_xdp_rxqs_prog_update(struct ionic_lif *lif)
2733 {
2734 	struct bpf_prog *xdp_prog;
2735 	unsigned int i;
2736 
2737 	if (!lif->rxqcqs)
2738 		return;
2739 
2740 	xdp_prog = READ_ONCE(lif->xdp_prog);
2741 	for (i = 0; i < lif->ionic->nrxqs_per_lif && lif->rxqcqs[i]; i++) {
2742 		struct ionic_queue *q = &lif->rxqcqs[i]->q;
2743 
2744 		WRITE_ONCE(q->xdp_prog, xdp_prog);
2745 	}
2746 }
2747 
2748 static int ionic_xdp_config(struct net_device *netdev, struct netdev_bpf *bpf)
2749 {
2750 	struct ionic_lif *lif = netdev_priv(netdev);
2751 	struct bpf_prog *old_prog;
2752 	u32 maxfs;
2753 
2754 	if (test_bit(IONIC_LIF_F_SPLIT_INTR, lif->state)) {
2755 #define XDP_ERR_SPLIT "XDP not available with split Tx/Rx interrupts"
2756 		NL_SET_ERR_MSG_MOD(bpf->extack, XDP_ERR_SPLIT);
2757 		netdev_info(lif->netdev, XDP_ERR_SPLIT);
2758 		return -EOPNOTSUPP;
2759 	}
2760 
2761 	if (!ionic_xdp_is_valid_mtu(lif, netdev->mtu, bpf->prog)) {
2762 #define XDP_ERR_MTU "MTU is too large for XDP without frags support"
2763 		NL_SET_ERR_MSG_MOD(bpf->extack, XDP_ERR_MTU);
2764 		netdev_info(lif->netdev, XDP_ERR_MTU);
2765 		return -EINVAL;
2766 	}
2767 
2768 	maxfs = __le32_to_cpu(lif->identity->eth.max_frame_size) - VLAN_ETH_HLEN;
2769 	if (bpf->prog && !(bpf->prog->aux && bpf->prog->aux->xdp_has_frags))
2770 		maxfs = min_t(u32, maxfs, IONIC_XDP_MAX_LINEAR_MTU);
2771 	netdev->max_mtu = maxfs;
2772 
2773 	if (!netif_running(netdev)) {
2774 		old_prog = xchg(&lif->xdp_prog, bpf->prog);
2775 	} else if (lif->xdp_prog && bpf->prog) {
2776 		old_prog = xchg(&lif->xdp_prog, bpf->prog);
2777 		ionic_xdp_rxqs_prog_update(lif);
2778 	} else {
2779 		struct ionic_queue_params qparams;
2780 
2781 		ionic_init_queue_params(lif, &qparams);
2782 		qparams.xdp_prog = bpf->prog;
2783 		mutex_lock(&lif->queue_lock);
2784 		ionic_reconfigure_queues(lif, &qparams);
2785 		old_prog = xchg(&lif->xdp_prog, bpf->prog);
2786 		mutex_unlock(&lif->queue_lock);
2787 	}
2788 
2789 	if (old_prog)
2790 		bpf_prog_put(old_prog);
2791 
2792 	return 0;
2793 }
2794 
2795 static int ionic_xdp(struct net_device *netdev, struct netdev_bpf *bpf)
2796 {
2797 	switch (bpf->command) {
2798 	case XDP_SETUP_PROG:
2799 		return ionic_xdp_config(netdev, bpf);
2800 	default:
2801 		return -EINVAL;
2802 	}
2803 }
2804 
2805 static const struct net_device_ops ionic_netdev_ops = {
2806 	.ndo_open               = ionic_open,
2807 	.ndo_stop               = ionic_stop,
2808 	.ndo_start_xmit		= ionic_start_xmit,
2809 	.ndo_bpf		= ionic_xdp,
2810 	.ndo_xdp_xmit		= ionic_xdp_xmit,
2811 	.ndo_get_stats64	= ionic_get_stats64,
2812 	.ndo_set_rx_mode	= ionic_ndo_set_rx_mode,
2813 	.ndo_set_features	= ionic_set_features,
2814 	.ndo_set_mac_address	= ionic_set_mac_address,
2815 	.ndo_validate_addr	= eth_validate_addr,
2816 	.ndo_tx_timeout         = ionic_tx_timeout,
2817 	.ndo_change_mtu         = ionic_change_mtu,
2818 	.ndo_vlan_rx_add_vid    = ionic_vlan_rx_add_vid,
2819 	.ndo_vlan_rx_kill_vid   = ionic_vlan_rx_kill_vid,
2820 	.ndo_set_vf_vlan	= ionic_set_vf_vlan,
2821 	.ndo_set_vf_trust	= ionic_set_vf_trust,
2822 	.ndo_set_vf_mac		= ionic_set_vf_mac,
2823 	.ndo_set_vf_rate	= ionic_set_vf_rate,
2824 	.ndo_set_vf_spoofchk	= ionic_set_vf_spoofchk,
2825 	.ndo_get_vf_config	= ionic_get_vf_config,
2826 	.ndo_set_vf_link_state	= ionic_set_vf_link_state,
2827 	.ndo_get_vf_stats       = ionic_get_vf_stats,
2828 	.ndo_hwtstamp_get	= ionic_hwstamp_get,
2829 	.ndo_hwtstamp_set	= ionic_hwstamp_set,
2830 };
2831 
2832 static int ionic_cmb_reconfig(struct ionic_lif *lif,
2833 			      struct ionic_queue_params *qparam)
2834 {
2835 	struct ionic_queue_params start_qparams;
2836 	int err = 0;
2837 
2838 	/* When changing CMB queue parameters, we're using limited
2839 	 * on-device memory and don't have extra memory to use for
2840 	 * duplicate allocations, so we free it all first then
2841 	 * re-allocate with the new parameters.
2842 	 */
2843 
2844 	/* Checkpoint for possible unwind */
2845 	ionic_init_queue_params(lif, &start_qparams);
2846 
2847 	/* Stop and free the queues */
2848 	ionic_stop_queues_reconfig(lif);
2849 	ionic_txrx_free(lif);
2850 
2851 	/* Set up new qparams */
2852 	ionic_set_queue_params(lif, qparam);
2853 
2854 	if (netif_running(lif->netdev)) {
2855 		/* Alloc and start the new configuration */
2856 		err = ionic_txrx_alloc(lif);
2857 		if (err) {
2858 			dev_warn(lif->ionic->dev,
2859 				 "CMB reconfig failed, restoring values: %d\n", err);
2860 
2861 			/* Back out the changes */
2862 			ionic_set_queue_params(lif, &start_qparams);
2863 			err = ionic_txrx_alloc(lif);
2864 			if (err) {
2865 				dev_err(lif->ionic->dev,
2866 					"CMB restore failed: %d\n", err);
2867 				goto err_out;
2868 			}
2869 		}
2870 
2871 		err = ionic_start_queues_reconfig(lif);
2872 		if (err) {
2873 			dev_err(lif->ionic->dev,
2874 				"CMB reconfig failed: %d\n", err);
2875 			goto err_out;
2876 		}
2877 	}
2878 
2879 err_out:
2880 	/* This was detached in ionic_stop_queues_reconfig() */
2881 	netif_device_attach(lif->netdev);
2882 
2883 	return err;
2884 }
2885 
2886 static void ionic_swap_queues(struct ionic_qcq *a, struct ionic_qcq *b)
2887 {
2888 	/* only swapping the queues and napi, not flags or other stuff */
2889 	swap(a->napi,         b->napi);
2890 
2891 	if (a->q.type == IONIC_QTYPE_RXQ) {
2892 		swap(a->q.page_pool, b->q.page_pool);
2893 		a->q.page_pool->p.napi = &a->napi;
2894 		if (b->q.page_pool)  /* is NULL when increasing queue count */
2895 			b->q.page_pool->p.napi = &b->napi;
2896 	}
2897 
2898 	swap(a->q.features,   b->q.features);
2899 	swap(a->q.num_descs,  b->q.num_descs);
2900 	swap(a->q.desc_size,  b->q.desc_size);
2901 	swap(a->q.base,       b->q.base);
2902 	swap(a->q.base_pa,    b->q.base_pa);
2903 	swap(a->q.info,       b->q.info);
2904 	swap(a->q.xdp_prog,   b->q.xdp_prog);
2905 	swap(a->q.xdp_rxq_info, b->q.xdp_rxq_info);
2906 	swap(a->q.partner,    b->q.partner);
2907 	swap(a->q_base,       b->q_base);
2908 	swap(a->q_base_pa,    b->q_base_pa);
2909 	swap(a->q_size,       b->q_size);
2910 
2911 	swap(a->q.sg_desc_size, b->q.sg_desc_size);
2912 	swap(a->q.sg_base,    b->q.sg_base);
2913 	swap(a->q.sg_base_pa, b->q.sg_base_pa);
2914 	swap(a->sg_base,      b->sg_base);
2915 	swap(a->sg_base_pa,   b->sg_base_pa);
2916 	swap(a->sg_size,      b->sg_size);
2917 
2918 	swap(a->cq.num_descs, b->cq.num_descs);
2919 	swap(a->cq.desc_size, b->cq.desc_size);
2920 	swap(a->cq.base,      b->cq.base);
2921 	swap(a->cq.base_pa,   b->cq.base_pa);
2922 	swap(a->cq_base,      b->cq_base);
2923 	swap(a->cq_base_pa,   b->cq_base_pa);
2924 	swap(a->cq_size,      b->cq_size);
2925 
2926 	ionic_debugfs_del_qcq(a);
2927 	ionic_debugfs_add_qcq(a->q.lif, a);
2928 }
2929 
2930 int ionic_reconfigure_queues(struct ionic_lif *lif,
2931 			     struct ionic_queue_params *qparam)
2932 {
2933 	unsigned int comp_sz, desc_sz, num_desc, sg_desc_sz;
2934 	struct ionic_qcq **tx_qcqs = NULL;
2935 	struct ionic_qcq **rx_qcqs = NULL;
2936 	unsigned int flags, i;
2937 	int err = 0;
2938 
2939 	/* Are we changing q params while CMB is on */
2940 	if ((test_bit(IONIC_LIF_F_CMB_TX_RINGS, lif->state) && qparam->cmb_tx) ||
2941 	    (test_bit(IONIC_LIF_F_CMB_RX_RINGS, lif->state) && qparam->cmb_rx))
2942 		return ionic_cmb_reconfig(lif, qparam);
2943 
2944 	/* allocate temporary qcq arrays to hold new queue structs */
2945 	if (qparam->nxqs != lif->nxqs || qparam->ntxq_descs != lif->ntxq_descs) {
2946 		tx_qcqs = devm_kcalloc(lif->ionic->dev, lif->ionic->ntxqs_per_lif,
2947 				       sizeof(struct ionic_qcq *), GFP_KERNEL);
2948 		if (!tx_qcqs) {
2949 			err = -ENOMEM;
2950 			goto err_out;
2951 		}
2952 	}
2953 	if (qparam->nxqs != lif->nxqs ||
2954 	    qparam->nrxq_descs != lif->nrxq_descs ||
2955 	    qparam->rxq_features != lif->rxq_features ||
2956 	    qparam->xdp_prog != lif->xdp_prog) {
2957 		rx_qcqs = devm_kcalloc(lif->ionic->dev, lif->ionic->nrxqs_per_lif,
2958 				       sizeof(struct ionic_qcq *), GFP_KERNEL);
2959 		if (!rx_qcqs) {
2960 			err = -ENOMEM;
2961 			goto err_out;
2962 		}
2963 	}
2964 
2965 	/* allocate new desc_info and rings, but leave the interrupt setup
2966 	 * until later so as to not mess with the still-running queues
2967 	 */
2968 	if (tx_qcqs) {
2969 		num_desc = qparam->ntxq_descs;
2970 		desc_sz = sizeof(struct ionic_txq_desc);
2971 		comp_sz = sizeof(struct ionic_txq_comp);
2972 
2973 		if (lif->qtype_info[IONIC_QTYPE_TXQ].version >= 1 &&
2974 		    lif->qtype_info[IONIC_QTYPE_TXQ].sg_desc_sz ==
2975 		    sizeof(struct ionic_txq_sg_desc_v1))
2976 			sg_desc_sz = sizeof(struct ionic_txq_sg_desc_v1);
2977 		else
2978 			sg_desc_sz = sizeof(struct ionic_txq_sg_desc);
2979 
2980 		for (i = 0; i < qparam->nxqs; i++) {
2981 			/* If missing, short placeholder qcq needed for swap */
2982 			if (!lif->txqcqs[i]) {
2983 				flags = IONIC_QCQ_F_TX_STATS | IONIC_QCQ_F_SG;
2984 				err = ionic_qcq_alloc(lif, IONIC_QTYPE_TXQ, i, "tx", flags,
2985 						      4, desc_sz, comp_sz, sg_desc_sz,
2986 						      sizeof(struct ionic_tx_desc_info),
2987 						      lif->kern_pid, NULL, &lif->txqcqs[i]);
2988 				if (err)
2989 					goto err_out;
2990 			}
2991 
2992 			flags = lif->txqcqs[i]->flags & ~IONIC_QCQ_F_INTR;
2993 			err = ionic_qcq_alloc(lif, IONIC_QTYPE_TXQ, i, "tx", flags,
2994 					      num_desc, desc_sz, comp_sz, sg_desc_sz,
2995 					      sizeof(struct ionic_tx_desc_info),
2996 					      lif->kern_pid, NULL, &tx_qcqs[i]);
2997 			if (err)
2998 				goto err_out;
2999 		}
3000 	}
3001 
3002 	if (rx_qcqs) {
3003 		num_desc = qparam->nrxq_descs;
3004 		desc_sz = sizeof(struct ionic_rxq_desc);
3005 		comp_sz = sizeof(struct ionic_rxq_comp);
3006 		sg_desc_sz = sizeof(struct ionic_rxq_sg_desc);
3007 
3008 		if (qparam->rxq_features & IONIC_Q_F_2X_CQ_DESC)
3009 			comp_sz *= 2;
3010 
3011 		for (i = 0; i < qparam->nxqs; i++) {
3012 			/* If missing, short placeholder qcq needed for swap */
3013 			if (!lif->rxqcqs[i]) {
3014 				flags = IONIC_QCQ_F_RX_STATS | IONIC_QCQ_F_SG;
3015 				err = ionic_qcq_alloc(lif, IONIC_QTYPE_RXQ, i, "rx", flags,
3016 						      4, desc_sz, comp_sz, sg_desc_sz,
3017 						      sizeof(struct ionic_rx_desc_info),
3018 						      lif->kern_pid, NULL, &lif->rxqcqs[i]);
3019 				if (err)
3020 					goto err_out;
3021 			}
3022 
3023 			flags = lif->rxqcqs[i]->flags & ~IONIC_QCQ_F_INTR;
3024 			err = ionic_qcq_alloc(lif, IONIC_QTYPE_RXQ, i, "rx", flags,
3025 					      num_desc, desc_sz, comp_sz, sg_desc_sz,
3026 					      sizeof(struct ionic_rx_desc_info),
3027 					      lif->kern_pid, qparam->xdp_prog, &rx_qcqs[i]);
3028 			if (err)
3029 				goto err_out;
3030 
3031 			rx_qcqs[i]->q.features = qparam->rxq_features;
3032 			rx_qcqs[i]->q.xdp_prog = qparam->xdp_prog;
3033 		}
3034 	}
3035 
3036 	/* stop and clean the queues */
3037 	ionic_stop_queues_reconfig(lif);
3038 
3039 	if (qparam->nxqs != lif->nxqs) {
3040 		err = netif_set_real_num_tx_queues(lif->netdev, qparam->nxqs);
3041 		if (err)
3042 			goto err_out_reinit_unlock;
3043 		err = netif_set_real_num_rx_queues(lif->netdev, qparam->nxqs);
3044 		if (err) {
3045 			netif_set_real_num_tx_queues(lif->netdev, lif->nxqs);
3046 			goto err_out_reinit_unlock;
3047 		}
3048 	}
3049 
3050 	/* swap new desc_info and rings, keeping existing interrupt config */
3051 	if (tx_qcqs) {
3052 		lif->ntxq_descs = qparam->ntxq_descs;
3053 		for (i = 0; i < qparam->nxqs; i++)
3054 			ionic_swap_queues(lif->txqcqs[i], tx_qcqs[i]);
3055 	}
3056 
3057 	if (rx_qcqs) {
3058 		lif->nrxq_descs = qparam->nrxq_descs;
3059 		for (i = 0; i < qparam->nxqs; i++)
3060 			ionic_swap_queues(lif->rxqcqs[i], rx_qcqs[i]);
3061 	}
3062 
3063 	/* if we need to change the interrupt layout, this is the time */
3064 	if (qparam->intr_split != test_bit(IONIC_LIF_F_SPLIT_INTR, lif->state) ||
3065 	    qparam->nxqs != lif->nxqs) {
3066 		if (qparam->intr_split) {
3067 			set_bit(IONIC_LIF_F_SPLIT_INTR, lif->state);
3068 		} else {
3069 			clear_bit(IONIC_LIF_F_SPLIT_INTR, lif->state);
3070 			lif->tx_coalesce_usecs = lif->rx_coalesce_usecs;
3071 			lif->tx_coalesce_hw = lif->rx_coalesce_hw;
3072 		}
3073 
3074 		/* Clear existing interrupt assignments.  We check for NULL here
3075 		 * because we're checking the whole array for potential qcqs, not
3076 		 * just those qcqs that have just been set up.
3077 		 */
3078 		for (i = 0; i < lif->ionic->ntxqs_per_lif; i++) {
3079 			if (lif->txqcqs[i])
3080 				ionic_qcq_intr_free(lif, lif->txqcqs[i]);
3081 			if (lif->rxqcqs[i])
3082 				ionic_qcq_intr_free(lif, lif->rxqcqs[i]);
3083 		}
3084 
3085 		/* re-assign the interrupts */
3086 		for (i = 0; i < qparam->nxqs; i++) {
3087 			lif->rxqcqs[i]->flags |= IONIC_QCQ_F_INTR;
3088 			err = ionic_alloc_qcq_interrupt(lif, lif->rxqcqs[i]);
3089 			ionic_intr_coal_init(lif->ionic->idev.intr_ctrl,
3090 					     lif->rxqcqs[i]->intr.index,
3091 					     lif->rx_coalesce_hw);
3092 
3093 			if (qparam->intr_split) {
3094 				lif->txqcqs[i]->flags |= IONIC_QCQ_F_INTR;
3095 				err = ionic_alloc_qcq_interrupt(lif, lif->txqcqs[i]);
3096 				ionic_intr_coal_init(lif->ionic->idev.intr_ctrl,
3097 						     lif->txqcqs[i]->intr.index,
3098 						     lif->tx_coalesce_hw);
3099 				if (test_bit(IONIC_LIF_F_TX_DIM_INTR, lif->state))
3100 					lif->txqcqs[i]->intr.dim_coal_hw = lif->tx_coalesce_hw;
3101 			} else {
3102 				lif->txqcqs[i]->flags &= ~IONIC_QCQ_F_INTR;
3103 				ionic_link_qcq_interrupts(lif->rxqcqs[i], lif->txqcqs[i]);
3104 			}
3105 		}
3106 	}
3107 
3108 	/* now we can rework the debugfs mappings */
3109 	if (tx_qcqs) {
3110 		for (i = 0; i < qparam->nxqs; i++) {
3111 			ionic_debugfs_del_qcq(lif->txqcqs[i]);
3112 			ionic_debugfs_add_qcq(lif, lif->txqcqs[i]);
3113 		}
3114 	}
3115 
3116 	if (rx_qcqs) {
3117 		for (i = 0; i < qparam->nxqs; i++) {
3118 			ionic_debugfs_del_qcq(lif->rxqcqs[i]);
3119 			ionic_debugfs_add_qcq(lif, lif->rxqcqs[i]);
3120 		}
3121 	}
3122 
3123 	swap(lif->nxqs, qparam->nxqs);
3124 	swap(lif->rxq_features, qparam->rxq_features);
3125 
3126 err_out_reinit_unlock:
3127 	/* re-init the queues, but don't lose an error code */
3128 	if (err)
3129 		ionic_start_queues_reconfig(lif);
3130 	else
3131 		err = ionic_start_queues_reconfig(lif);
3132 
3133 err_out:
3134 	/* free old allocs without cleaning intr */
3135 	for (i = 0; i < qparam->nxqs; i++) {
3136 		if (tx_qcqs && tx_qcqs[i]) {
3137 			tx_qcqs[i]->flags &= ~IONIC_QCQ_F_INTR;
3138 			ionic_qcq_free(lif, tx_qcqs[i]);
3139 			devm_kfree(lif->ionic->dev, tx_qcqs[i]);
3140 			tx_qcqs[i] = NULL;
3141 		}
3142 		if (rx_qcqs && rx_qcqs[i]) {
3143 			rx_qcqs[i]->flags &= ~IONIC_QCQ_F_INTR;
3144 			ionic_qcq_free(lif, rx_qcqs[i]);
3145 			devm_kfree(lif->ionic->dev, rx_qcqs[i]);
3146 			rx_qcqs[i] = NULL;
3147 		}
3148 	}
3149 
3150 	/* free q array */
3151 	if (rx_qcqs) {
3152 		devm_kfree(lif->ionic->dev, rx_qcqs);
3153 		rx_qcqs = NULL;
3154 	}
3155 	if (tx_qcqs) {
3156 		devm_kfree(lif->ionic->dev, tx_qcqs);
3157 		tx_qcqs = NULL;
3158 	}
3159 
3160 	/* clean the unused dma and info allocations when new set is smaller
3161 	 * than the full array, but leave the qcq shells in place
3162 	 */
3163 	for (i = lif->nxqs; i < lif->ionic->ntxqs_per_lif; i++) {
3164 		if (lif->txqcqs && lif->txqcqs[i]) {
3165 			lif->txqcqs[i]->flags &= ~IONIC_QCQ_F_INTR;
3166 			ionic_qcq_free(lif, lif->txqcqs[i]);
3167 		}
3168 
3169 		if (lif->rxqcqs && lif->rxqcqs[i]) {
3170 			lif->rxqcqs[i]->flags &= ~IONIC_QCQ_F_INTR;
3171 			ionic_qcq_free(lif, lif->rxqcqs[i]);
3172 		}
3173 	}
3174 
3175 	if (err)
3176 		netdev_info(lif->netdev, "%s: failed %d\n", __func__, err);
3177 
3178 	return err;
3179 }
3180 
3181 static int ionic_affinity_masks_alloc(struct ionic *ionic)
3182 {
3183 	cpumask_var_t *affinity_masks;
3184 	int nintrs = ionic->nintrs;
3185 	int i;
3186 
3187 	affinity_masks = kzalloc_objs(cpumask_var_t, nintrs);
3188 	if (!affinity_masks)
3189 		return -ENOMEM;
3190 
3191 	for (i = 0; i < nintrs; i++) {
3192 		if (!zalloc_cpumask_var_node(&affinity_masks[i], GFP_KERNEL,
3193 					     dev_to_node(ionic->dev)))
3194 			goto err_out;
3195 	}
3196 
3197 	ionic->affinity_masks = affinity_masks;
3198 
3199 	return 0;
3200 
3201 err_out:
3202 	for (--i; i >= 0; i--)
3203 		free_cpumask_var(affinity_masks[i]);
3204 	kfree(affinity_masks);
3205 
3206 	return -ENOMEM;
3207 }
3208 
3209 static void ionic_affinity_masks_free(struct ionic *ionic)
3210 {
3211 	int i;
3212 
3213 	for (i = 0; i < ionic->nintrs; i++)
3214 		free_cpumask_var(ionic->affinity_masks[i]);
3215 	kfree(ionic->affinity_masks);
3216 	ionic->affinity_masks = NULL;
3217 }
3218 
3219 int ionic_lif_alloc(struct ionic *ionic)
3220 {
3221 	struct device *dev = ionic->dev;
3222 	union ionic_lif_identity *lid;
3223 	struct net_device *netdev;
3224 	struct ionic_lif *lif;
3225 	int tbl_sz;
3226 	int err;
3227 
3228 	lid = kzalloc_obj(*lid);
3229 	if (!lid)
3230 		return -ENOMEM;
3231 
3232 	netdev = alloc_etherdev_mqs(sizeof(*lif),
3233 				    ionic->ntxqs_per_lif, ionic->ntxqs_per_lif);
3234 	if (!netdev) {
3235 		dev_err(dev, "Cannot allocate netdev, aborting\n");
3236 		err = -ENOMEM;
3237 		goto err_out_free_lid;
3238 	}
3239 
3240 	SET_NETDEV_DEV(netdev, dev);
3241 
3242 	lif = netdev_priv(netdev);
3243 	lif->netdev = netdev;
3244 	ionic->lif = lif;
3245 	lif->ionic = ionic;
3246 	netdev->netdev_ops = &ionic_netdev_ops;
3247 	ionic_ethtool_set_ops(netdev);
3248 
3249 	netdev->watchdog_timeo = 5 * HZ;
3250 	netif_carrier_off(netdev);
3251 
3252 	lif->identity = lid;
3253 	lif->lif_type = IONIC_LIF_TYPE_CLASSIC;
3254 	err = ionic_lif_identify(ionic, lif->lif_type, lif->identity);
3255 	if (err) {
3256 		dev_err(ionic->dev, "Cannot identify type %d: %d\n",
3257 			lif->lif_type, err);
3258 		goto err_out_free_netdev;
3259 	}
3260 	lif->netdev->min_mtu = max_t(unsigned int, ETH_MIN_MTU,
3261 				     le32_to_cpu(lif->identity->eth.min_frame_size));
3262 	lif->netdev->max_mtu =
3263 		le32_to_cpu(lif->identity->eth.max_frame_size) - VLAN_ETH_HLEN;
3264 
3265 	lif->neqs = ionic->neqs_per_lif;
3266 	lif->nxqs = ionic->ntxqs_per_lif;
3267 
3268 	lif->index = 0;
3269 
3270 	if (is_kdump_kernel()) {
3271 		lif->ntxq_descs = IONIC_MIN_TXRX_DESC;
3272 		lif->nrxq_descs = IONIC_MIN_TXRX_DESC;
3273 	} else {
3274 		lif->ntxq_descs = IONIC_DEF_TXRX_DESC;
3275 		lif->nrxq_descs = IONIC_DEF_TXRX_DESC;
3276 	}
3277 
3278 	/* Convert the default coalesce value to actual hw resolution */
3279 	lif->rx_coalesce_usecs = IONIC_ITR_COAL_USEC_DEFAULT;
3280 	lif->rx_coalesce_hw = ionic_coal_usec_to_hw(lif->ionic,
3281 						    lif->rx_coalesce_usecs);
3282 	lif->tx_coalesce_usecs = lif->rx_coalesce_usecs;
3283 	lif->tx_coalesce_hw = lif->rx_coalesce_hw;
3284 	set_bit(IONIC_LIF_F_RX_DIM_INTR, lif->state);
3285 	set_bit(IONIC_LIF_F_TX_DIM_INTR, lif->state);
3286 
3287 	snprintf(lif->name, sizeof(lif->name), "lif%u", lif->index);
3288 
3289 	mutex_init(&lif->queue_lock);
3290 	mutex_init(&lif->config_lock);
3291 	mutex_init(&lif->adev_lock);
3292 
3293 	spin_lock_init(&lif->adminq_lock);
3294 
3295 	spin_lock_init(&lif->deferred.lock);
3296 	INIT_LIST_HEAD(&lif->deferred.list);
3297 	INIT_WORK(&lif->deferred.work, ionic_lif_deferred_work);
3298 
3299 	/* allocate lif info */
3300 	lif->info_sz = ALIGN(sizeof(*lif->info), PAGE_SIZE);
3301 	lif->info = dma_alloc_coherent(dev, lif->info_sz,
3302 				       &lif->info_pa, GFP_KERNEL);
3303 	if (!lif->info) {
3304 		dev_err(dev, "Failed to allocate lif info, aborting\n");
3305 		err = -ENOMEM;
3306 		goto err_out_free_mutex;
3307 	}
3308 
3309 	ionic_debugfs_add_lif(lif);
3310 
3311 	err = ionic_affinity_masks_alloc(ionic);
3312 	if (err)
3313 		goto err_out_free_lif_info;
3314 
3315 	/* allocate control queues and txrx queue arrays */
3316 	ionic_lif_queue_identify(lif);
3317 	err = ionic_qcqs_alloc(lif);
3318 	if (err)
3319 		goto err_out_free_affinity_masks;
3320 
3321 	/* allocate rss indirection table */
3322 	tbl_sz = le16_to_cpu(lif->ionic->ident.lif.eth.rss_ind_tbl_sz);
3323 	lif->rss_ind_tbl_sz = sizeof(*lif->rss_ind_tbl) * tbl_sz;
3324 	lif->rss_ind_tbl = dma_alloc_coherent(dev, lif->rss_ind_tbl_sz,
3325 					      &lif->rss_ind_tbl_pa,
3326 					      GFP_KERNEL);
3327 
3328 	if (!lif->rss_ind_tbl) {
3329 		err = -ENOMEM;
3330 		dev_err(dev, "Failed to allocate rss indirection table, aborting\n");
3331 		goto err_out_free_qcqs;
3332 	}
3333 	netdev_rss_key_fill(lif->rss_hash_key, IONIC_RSS_HASH_KEY_SIZE);
3334 
3335 	ionic_lif_alloc_phc(lif);
3336 
3337 	return 0;
3338 
3339 err_out_free_qcqs:
3340 	ionic_qcqs_free(lif);
3341 err_out_free_affinity_masks:
3342 	ionic_affinity_masks_free(lif->ionic);
3343 err_out_free_lif_info:
3344 	dma_free_coherent(dev, lif->info_sz, lif->info, lif->info_pa);
3345 	lif->info = NULL;
3346 	lif->info_pa = 0;
3347 err_out_free_mutex:
3348 	mutex_destroy(&lif->adev_lock);
3349 	mutex_destroy(&lif->config_lock);
3350 	mutex_destroy(&lif->queue_lock);
3351 err_out_free_netdev:
3352 	free_netdev(lif->netdev);
3353 	lif = NULL;
3354 err_out_free_lid:
3355 	kfree(lid);
3356 
3357 	return err;
3358 }
3359 
3360 static void ionic_lif_reset(struct ionic_lif *lif)
3361 {
3362 	struct ionic_dev *idev = &lif->ionic->idev;
3363 
3364 	if (!ionic_is_fw_running(idev))
3365 		return;
3366 
3367 	mutex_lock(&lif->ionic->dev_cmd_lock);
3368 	ionic_dev_cmd_lif_reset(idev, lif->index);
3369 	ionic_dev_cmd_wait(lif->ionic, DEVCMD_TIMEOUT);
3370 	mutex_unlock(&lif->ionic->dev_cmd_lock);
3371 }
3372 
3373 static void ionic_lif_handle_fw_down(struct ionic_lif *lif)
3374 {
3375 	struct ionic *ionic = lif->ionic;
3376 
3377 	if (test_and_set_bit(IONIC_LIF_F_FW_RESET, lif->state))
3378 		return;
3379 
3380 	dev_info(ionic->dev, "FW Down: Stopping LIFs\n");
3381 
3382 	netif_device_detach(lif->netdev);
3383 
3384 	ionic_auxbus_unregister(ionic->lif);
3385 	mutex_lock(&lif->queue_lock);
3386 	if (test_bit(IONIC_LIF_F_UP, lif->state)) {
3387 		dev_info(ionic->dev, "Surprise FW stop, stopping queues\n");
3388 		ionic_stop_queues(lif);
3389 	}
3390 
3391 	if (netif_running(lif->netdev)) {
3392 		ionic_txrx_deinit(lif);
3393 		ionic_txrx_free(lif);
3394 	}
3395 	ionic_lif_deinit(lif);
3396 	ionic_reset(ionic);
3397 	ionic_qcqs_free(lif);
3398 
3399 	mutex_unlock(&lif->queue_lock);
3400 
3401 	clear_bit(IONIC_LIF_F_FW_STOPPING, lif->state);
3402 	dev_info(ionic->dev, "FW Down: LIFs stopped\n");
3403 }
3404 
3405 int ionic_restart_lif(struct ionic_lif *lif)
3406 {
3407 	struct ionic *ionic = lif->ionic;
3408 	int err;
3409 
3410 	mutex_lock(&lif->queue_lock);
3411 
3412 	if (test_and_clear_bit(IONIC_LIF_F_BROKEN, lif->state))
3413 		dev_info(ionic->dev, "FW Up: clearing broken state\n");
3414 
3415 	err = ionic_qcqs_alloc(lif);
3416 	if (err)
3417 		goto err_unlock;
3418 
3419 	err = ionic_lif_init(lif);
3420 	if (err)
3421 		goto err_qcqs_free;
3422 
3423 	ionic_vf_attr_replay(lif);
3424 
3425 	if (lif->registered)
3426 		ionic_lif_set_netdev_info(lif);
3427 
3428 	ionic_rx_filter_replay(lif);
3429 
3430 	if (netif_running(lif->netdev)) {
3431 		err = ionic_txrx_alloc(lif);
3432 		if (err)
3433 			goto err_lifs_deinit;
3434 
3435 		err = ionic_txrx_init(lif);
3436 		if (err)
3437 			goto err_txrx_free;
3438 	}
3439 
3440 	mutex_unlock(&lif->queue_lock);
3441 
3442 	clear_bit(IONIC_LIF_F_FW_RESET, lif->state);
3443 	ionic_link_status_check_request(lif, CAN_SLEEP);
3444 	netif_device_attach(lif->netdev);
3445 	ionic_queue_doorbell_check(ionic, IONIC_NAPI_DEADLINE);
3446 
3447 	ionic_auxbus_register(ionic->lif);
3448 
3449 	return 0;
3450 
3451 err_txrx_free:
3452 	ionic_txrx_free(lif);
3453 err_lifs_deinit:
3454 	ionic_lif_deinit(lif);
3455 err_qcqs_free:
3456 	ionic_qcqs_free(lif);
3457 err_unlock:
3458 	mutex_unlock(&lif->queue_lock);
3459 
3460 	return err;
3461 }
3462 
3463 static void ionic_lif_handle_fw_up(struct ionic_lif *lif)
3464 {
3465 	struct ionic *ionic = lif->ionic;
3466 	int err;
3467 
3468 	if (!test_bit(IONIC_LIF_F_FW_RESET, lif->state))
3469 		return;
3470 
3471 	dev_info(ionic->dev, "FW Up: restarting LIFs\n");
3472 
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.
3476 	 */
3477 	ionic_init_devinfo(ionic);
3478 	ionic_reset(ionic);
3479 	err = ionic_identify(ionic);
3480 	if (err)
3481 		goto err_out;
3482 	err = ionic_port_identify(ionic);
3483 	if (err)
3484 		goto err_out;
3485 	err = ionic_port_init(ionic);
3486 	if (err)
3487 		goto err_out;
3488 
3489 	err = ionic_restart_lif(lif);
3490 	if (err)
3491 		goto err_out;
3492 
3493 	dev_info(ionic->dev, "FW Up: LIFs restarted\n");
3494 
3495 	/* restore the hardware timestamping queues */
3496 	ionic_lif_hwstamp_replay(lif);
3497 
3498 	return;
3499 
3500 err_out:
3501 	dev_err(ionic->dev, "FW Up: LIFs restart failed - err %d\n", err);
3502 }
3503 
3504 void ionic_lif_free(struct ionic_lif *lif)
3505 {
3506 	struct device *dev = lif->ionic->dev;
3507 
3508 	ionic_lif_free_phc(lif);
3509 
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;
3515 
3516 	/* free queues */
3517 	ionic_qcqs_free(lif);
3518 	if (!test_bit(IONIC_LIF_F_FW_RESET, lif->state))
3519 		ionic_lif_reset(lif);
3520 
3521 	ionic_affinity_masks_free(lif->ionic);
3522 
3523 	/* free lif info */
3524 	kfree(lif->identity);
3525 	dma_free_coherent(dev, lif->info_sz, lif->info, lif->info_pa);
3526 	lif->info = NULL;
3527 	lif->info_pa = 0;
3528 
3529 	mutex_destroy(&lif->config_lock);
3530 	mutex_destroy(&lif->queue_lock);
3531 	mutex_destroy(&lif->adev_lock);
3532 
3533 	/* free netdev & lif */
3534 	ionic_debugfs_del_lif(lif);
3535 	free_netdev(lif->netdev);
3536 }
3537 
3538 void ionic_lif_deinit(struct ionic_lif *lif)
3539 {
3540 	if (!test_and_clear_bit(IONIC_LIF_F_INITED, lif->state))
3541 		return;
3542 
3543 	if (!test_bit(IONIC_LIF_F_FW_RESET, lif->state)) {
3544 		cancel_work_sync(&lif->deferred.work);
3545 		cancel_work_sync(&lif->tx_timeout_work);
3546 		ionic_rx_filters_deinit(lif);
3547 		if (lif->netdev->features & NETIF_F_RXHASH)
3548 			ionic_lif_rss_deinit(lif);
3549 	}
3550 
3551 	napi_disable(&lif->adminqcq->napi);
3552 	ionic_lif_qcq_deinit(lif, lif->notifyqcq);
3553 	ionic_lif_qcq_deinit(lif, lif->adminqcq);
3554 
3555 	ionic_bus_unmap_dbpage(lif->ionic, lif->kern_dbpage);
3556 	lif->kern_dbpage = NULL;
3557 
3558 	ionic_lif_reset(lif);
3559 }
3560 
3561 static int ionic_lif_adminq_init(struct ionic_lif *lif)
3562 {
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;
3568 	int err;
3569 
3570 	idev = &lif->ionic->idev;
3571 	qcq = lif->adminqcq;
3572 	q = &qcq->q;
3573 
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);
3579 	if (err) {
3580 		netdev_err(lif->netdev, "adminq init failed %d\n", err);
3581 		return err;
3582 	}
3583 
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);
3587 
3588 	dev_dbg(dev, "adminq->hw_type %d\n", q->hw_type);
3589 	dev_dbg(dev, "adminq->hw_index %d\n", q->hw_index);
3590 
3591 	q->dbell_deadline = IONIC_ADMIN_DOORBELL_DEADLINE;
3592 	q->dbell_jiffies = jiffies;
3593 
3594 	netif_napi_add(lif->netdev, &qcq->napi, ionic_adminq_napi);
3595 
3596 	napi_enable(&qcq->napi);
3597 
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);
3603 	}
3604 
3605 	qcq->flags |= IONIC_QCQ_F_INITED;
3606 
3607 	return 0;
3608 }
3609 
3610 static int ionic_lif_notifyq_init(struct ionic_lif *lif)
3611 {
3612 	struct ionic_qcq *qcq = lif->notifyqcq;
3613 	struct device *dev = lif->ionic->dev;
3614 	struct ionic_queue *q = &qcq->q;
3615 	int err;
3616 
3617 	struct ionic_admin_ctx ctx = {
3618 		.work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
3619 		.cmd.q_init = {
3620 			.opcode = IONIC_CMD_Q_INIT,
3621 			.lif_index = cpu_to_le16(lif->index),
3622 			.type = q->type,
3623 			.ver = lif->qtype_info[q->type].version,
3624 			.index = cpu_to_le32(q->index),
3625 			.flags = cpu_to_le16(IONIC_QINIT_F_IRQ |
3626 					     IONIC_QINIT_F_ENA),
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),
3631 		}
3632 	};
3633 
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);
3638 
3639 	err = ionic_adminq_post_wait(lif, &ctx);
3640 	if (err)
3641 		return err;
3642 
3643 	lif->last_eid = 0;
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);
3647 
3648 	dev_dbg(dev, "notifyq->hw_type %d\n", q->hw_type);
3649 	dev_dbg(dev, "notifyq->hw_index %d\n", q->hw_index);
3650 
3651 	/* preset the callback info */
3652 	q->admin_info[0].ctx = lif;
3653 
3654 	qcq->flags |= IONIC_QCQ_F_INITED;
3655 
3656 	return 0;
3657 }
3658 
3659 static int ionic_station_set(struct ionic_lif *lif)
3660 {
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,
3668 		},
3669 	};
3670 	u8 mac_address[ETH_ALEN];
3671 	struct sockaddr addr;
3672 	int err;
3673 
3674 	err = ionic_adminq_post_wait(lif, &ctx);
3675 	if (err)
3676 		return err;
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);
3680 
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);
3685 
3686 		err = ionic_program_mac(lif, mac_address);
3687 		if (err < 0)
3688 			return err;
3689 
3690 		if (err > 0) {
3691 			netdev_dbg(netdev, "%s:SET/GET ATTR Mac are not same-due to old FW running\n",
3692 				   __func__);
3693 			return 0;
3694 		}
3695 	}
3696 
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.
3702 		 */
3703 		if (!ether_addr_equal(mac_address, netdev->dev_addr))
3704 			ionic_lif_addr_add(lif, netdev->dev_addr);
3705 	} else {
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);
3710 		if (err) {
3711 			netdev_warn(lif->netdev, "ignoring bad MAC addr from NIC %pM - err %d\n",
3712 				    addr.sa_data, err);
3713 			return 0;
3714 		}
3715 
3716 		eth_commit_mac_addr_change(netdev, &addr);
3717 	}
3718 
3719 	netdev_dbg(lif->netdev, "adding station MAC addr %pM\n",
3720 		   netdev->dev_addr);
3721 	ionic_lif_addr_add(lif, netdev->dev_addr);
3722 
3723 	return 0;
3724 }
3725 
3726 int ionic_lif_init(struct ionic_lif *lif)
3727 {
3728 	struct ionic_dev *idev = &lif->ionic->idev;
3729 	struct device *dev = lif->ionic->dev;
3730 	struct ionic_lif_init_comp comp;
3731 	int dbpage_num;
3732 	int err;
3733 
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);
3739 	if (err)
3740 		return err;
3741 
3742 	lif->hw_index = le16_to_cpu(comp.hw_index);
3743 
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");
3748 		return -EINVAL;
3749 	}
3750 
3751 	lif->kern_pid = 0;
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");
3756 		return -ENOMEM;
3757 	}
3758 
3759 	err = ionic_lif_adminq_init(lif);
3760 	if (err)
3761 		goto err_out_adminq_deinit;
3762 
3763 	if (lif->ionic->nnqs_per_lif) {
3764 		err = ionic_lif_notifyq_init(lif);
3765 		if (err)
3766 			goto err_out_notifyq_deinit;
3767 	}
3768 
3769 	if (test_bit(IONIC_LIF_F_FW_RESET, lif->state))
3770 		err = ionic_set_nic_features(lif, lif->netdev->features);
3771 	else
3772 		err = ionic_init_nic_features(lif);
3773 	if (err)
3774 		goto err_out_notifyq_deinit;
3775 
3776 	if (!test_bit(IONIC_LIF_F_FW_RESET, lif->state)) {
3777 		err = ionic_rx_filters_init(lif);
3778 		if (err)
3779 			goto err_out_notifyq_deinit;
3780 	}
3781 
3782 	err = ionic_station_set(lif);
3783 	if (err)
3784 		goto err_out_notifyq_deinit;
3785 
3786 	lif->rx_copybreak = IONIC_RX_COPYBREAK_DEFAULT;
3787 	lif->doorbell_wa = ionic_doorbell_wa(lif->ionic);
3788 
3789 	set_bit(IONIC_LIF_F_INITED, lif->state);
3790 
3791 	INIT_WORK(&lif->tx_timeout_work, ionic_tx_timeout_work);
3792 
3793 	return 0;
3794 
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;
3803 
3804 	return err;
3805 }
3806 
3807 static void ionic_lif_set_netdev_info(struct ionic_lif *lif)
3808 {
3809 	struct ionic_admin_ctx ctx = {
3810 		.work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
3811 		.cmd.lif_setattr = {
3812 			.opcode = IONIC_CMD_LIF_SETATTR,
3813 			.index = cpu_to_le16(lif->index),
3814 			.attr = IONIC_LIF_ATTR_NAME,
3815 		},
3816 	};
3817 
3818 	strscpy(ctx.cmd.lif_setattr.name, netdev_name(lif->netdev),
3819 		sizeof(ctx.cmd.lif_setattr.name));
3820 
3821 	ionic_adminq_post_wait(lif, &ctx);
3822 }
3823 
3824 static struct ionic_lif *ionic_netdev_lif(struct net_device *netdev)
3825 {
3826 	if (!netdev || netdev->netdev_ops->ndo_start_xmit != ionic_start_xmit)
3827 		return NULL;
3828 
3829 	return netdev_priv(netdev);
3830 }
3831 
3832 static int ionic_lif_notify(struct notifier_block *nb,
3833 			    unsigned long event, void *info)
3834 {
3835 	struct net_device *ndev = netdev_notifier_info_to_dev(info);
3836 	struct ionic *ionic = container_of(nb, struct ionic, nb);
3837 	struct ionic_lif *lif = ionic_netdev_lif(ndev);
3838 
3839 	if (!lif || lif->ionic != ionic)
3840 		return NOTIFY_DONE;
3841 
3842 	switch (event) {
3843 	case NETDEV_CHANGENAME:
3844 		ionic_lif_set_netdev_info(lif);
3845 		break;
3846 	}
3847 
3848 	return NOTIFY_DONE;
3849 }
3850 
3851 int ionic_lif_register(struct ionic_lif *lif)
3852 {
3853 	int err;
3854 
3855 	ionic_lif_register_phc(lif);
3856 
3857 	lif->ionic->nb.notifier_call = ionic_lif_notify;
3858 
3859 	err = register_netdevice_notifier(&lif->ionic->nb);
3860 	if (err)
3861 		lif->ionic->nb.notifier_call = NULL;
3862 
3863 	/* only register LIF0 for now */
3864 	err = register_netdev(lif->netdev);
3865 	if (err) {
3866 		dev_err(lif->ionic->dev, "Cannot register net device: %d, aborting\n", err);
3867 		ionic_lif_unregister(lif);
3868 		return err;
3869 	}
3870 
3871 	ionic_link_status_check_request(lif, CAN_SLEEP);
3872 	lif->registered = true;
3873 	ionic_lif_set_netdev_info(lif);
3874 
3875 	return 0;
3876 }
3877 
3878 void ionic_lif_unregister(struct ionic_lif *lif)
3879 {
3880 	if (lif->ionic->nb.notifier_call) {
3881 		unregister_netdevice_notifier(&lif->ionic->nb);
3882 		lif->ionic->nb.notifier_call = NULL;
3883 	}
3884 
3885 	if (lif->netdev->reg_state == NETREG_REGISTERED)
3886 		unregister_netdev(lif->netdev);
3887 
3888 	ionic_lif_unregister_phc(lif);
3889 
3890 	lif->registered = false;
3891 }
3892 
3893 static void ionic_lif_queue_identify(struct ionic_lif *lif)
3894 {
3895 	union ionic_q_identity __iomem *q_ident;
3896 	struct ionic *ionic = lif->ionic;
3897 	struct ionic_dev *idev;
3898 	u16 max_frags;
3899 	int qtype;
3900 	int err;
3901 
3902 	idev = &lif->ionic->idev;
3903 	q_ident = (union ionic_q_identity __iomem *)&idev->dev_cmd_regs->data;
3904 
3905 	for (qtype = 0; qtype < ARRAY_SIZE(ionic_qtype_versions); qtype++) {
3906 		struct ionic_qtype_info *qti = &lif->qtype_info[qtype];
3907 
3908 		/* filter out the ones we know about */
3909 		switch (qtype) {
3910 		case IONIC_QTYPE_ADMINQ:
3911 		case IONIC_QTYPE_NOTIFYQ:
3912 		case IONIC_QTYPE_RXQ:
3913 		case IONIC_QTYPE_TXQ:
3914 			break;
3915 		default:
3916 			continue;
3917 		}
3918 
3919 		memset(qti, 0, sizeof(*qti));
3920 
3921 		mutex_lock(&ionic->dev_cmd_lock);
3922 		ionic_dev_cmd_queue_identify(idev, lif->lif_type, qtype,
3923 					     ionic_qtype_versions[qtype]);
3924 		err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
3925 		if (!err) {
3926 			qti->version   = readb(&q_ident->version);
3927 			qti->supported = readb(&q_ident->supported);
3928 			qti->features  = readq(&q_ident->features);
3929 			qti->desc_sz   = readw(&q_ident->desc_sz);
3930 			qti->comp_sz   = readw(&q_ident->comp_sz);
3931 			qti->sg_desc_sz   = readw(&q_ident->sg_desc_sz);
3932 			qti->max_sg_elems = readw(&q_ident->max_sg_elems);
3933 			qti->sg_desc_stride = readw(&q_ident->sg_desc_stride);
3934 		}
3935 		mutex_unlock(&ionic->dev_cmd_lock);
3936 
3937 		if (err == -EINVAL) {
3938 			dev_err(ionic->dev, "qtype %d not supported\n", qtype);
3939 			continue;
3940 		} else if (err == -EIO) {
3941 			dev_err(ionic->dev, "q_ident failed, not supported on older FW\n");
3942 			return;
3943 		} else if (err) {
3944 			dev_err(ionic->dev, "q_ident failed, qtype %d: %d\n",
3945 				qtype, err);
3946 			return;
3947 		}
3948 
3949 		dev_dbg(ionic->dev, " qtype[%d].version = %d\n",
3950 			qtype, qti->version);
3951 		dev_dbg(ionic->dev, " qtype[%d].supported = 0x%02x\n",
3952 			qtype, qti->supported);
3953 		dev_dbg(ionic->dev, " qtype[%d].features = 0x%04llx\n",
3954 			qtype, qti->features);
3955 		dev_dbg(ionic->dev, " qtype[%d].desc_sz = %d\n",
3956 			qtype, qti->desc_sz);
3957 		dev_dbg(ionic->dev, " qtype[%d].comp_sz = %d\n",
3958 			qtype, qti->comp_sz);
3959 		dev_dbg(ionic->dev, " qtype[%d].sg_desc_sz = %d\n",
3960 			qtype, qti->sg_desc_sz);
3961 		dev_dbg(ionic->dev, " qtype[%d].max_sg_elems = %d\n",
3962 			qtype, qti->max_sg_elems);
3963 		dev_dbg(ionic->dev, " qtype[%d].sg_desc_stride = %d\n",
3964 			qtype, qti->sg_desc_stride);
3965 
3966 		if (qtype == IONIC_QTYPE_TXQ)
3967 			max_frags = IONIC_TX_MAX_FRAGS;
3968 		else if (qtype == IONIC_QTYPE_RXQ)
3969 			max_frags = IONIC_RX_MAX_FRAGS;
3970 		else
3971 			max_frags = 1;
3972 
3973 		qti->max_sg_elems = min_t(u16, max_frags - 1, MAX_SKB_FRAGS);
3974 		dev_dbg(ionic->dev, "qtype %d max_sg_elems %d\n",
3975 			qtype, qti->max_sg_elems);
3976 	}
3977 }
3978 
3979 int ionic_lif_identify(struct ionic *ionic, u8 lif_type,
3980 		       union ionic_lif_identity *lid)
3981 {
3982 	struct ionic_dev *idev = &ionic->idev;
3983 	size_t sz;
3984 	int err;
3985 
3986 	sz = min(sizeof(*lid), sizeof(idev->dev_cmd_regs->data));
3987 
3988 	mutex_lock(&ionic->dev_cmd_lock);
3989 	ionic_dev_cmd_lif_identify(idev, lif_type, IONIC_IDENTITY_VERSION_1);
3990 	err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
3991 	memcpy_fromio(lid, &idev->dev_cmd_regs->data, sz);
3992 	mutex_unlock(&ionic->dev_cmd_lock);
3993 	if (err)
3994 		return (err);
3995 
3996 	dev_dbg(ionic->dev, "capabilities 0x%llx\n",
3997 		le64_to_cpu(lid->capabilities));
3998 
3999 	dev_dbg(ionic->dev, "eth.max_ucast_filters %d\n",
4000 		le32_to_cpu(lid->eth.max_ucast_filters));
4001 	dev_dbg(ionic->dev, "eth.max_mcast_filters %d\n",
4002 		le32_to_cpu(lid->eth.max_mcast_filters));
4003 	dev_dbg(ionic->dev, "eth.features 0x%llx\n",
4004 		le64_to_cpu(lid->eth.config.features));
4005 	dev_dbg(ionic->dev, "eth.queue_count[IONIC_QTYPE_ADMINQ] %d\n",
4006 		le32_to_cpu(lid->eth.config.queue_count[IONIC_QTYPE_ADMINQ]));
4007 	dev_dbg(ionic->dev, "eth.queue_count[IONIC_QTYPE_NOTIFYQ] %d\n",
4008 		le32_to_cpu(lid->eth.config.queue_count[IONIC_QTYPE_NOTIFYQ]));
4009 	dev_dbg(ionic->dev, "eth.queue_count[IONIC_QTYPE_RXQ] %d\n",
4010 		le32_to_cpu(lid->eth.config.queue_count[IONIC_QTYPE_RXQ]));
4011 	dev_dbg(ionic->dev, "eth.queue_count[IONIC_QTYPE_TXQ] %d\n",
4012 		le32_to_cpu(lid->eth.config.queue_count[IONIC_QTYPE_TXQ]));
4013 	dev_dbg(ionic->dev, "eth.config.name %s\n", lid->eth.config.name);
4014 	dev_dbg(ionic->dev, "eth.config.mac %pM\n", lid->eth.config.mac);
4015 	dev_dbg(ionic->dev, "eth.config.mtu %d\n",
4016 		le32_to_cpu(lid->eth.config.mtu));
4017 
4018 	return 0;
4019 }
4020 
4021 int ionic_lif_size(struct ionic *ionic)
4022 {
4023 	struct ionic_identity *ident = &ionic->ident;
4024 	unsigned int nintrs, dev_nintrs;
4025 	union ionic_lif_config *lc;
4026 	unsigned int ntxqs_per_lif;
4027 	unsigned int nrxqs_per_lif;
4028 	unsigned int neqs_per_lif;
4029 	unsigned int nnqs_per_lif;
4030 	unsigned int nxqs, neqs;
4031 	unsigned int min_intrs;
4032 	int err;
4033 
4034 	/* retrieve basic values from FW */
4035 	lc = &ident->lif.eth.config;
4036 	dev_nintrs = le32_to_cpu(ident->dev.nintrs);
4037 	neqs_per_lif = le32_to_cpu(ident->lif.rdma.eq_qtype.qid_count);
4038 	nnqs_per_lif = le32_to_cpu(lc->queue_count[IONIC_QTYPE_NOTIFYQ]);
4039 	ntxqs_per_lif = le32_to_cpu(lc->queue_count[IONIC_QTYPE_TXQ]);
4040 	nrxqs_per_lif = le32_to_cpu(lc->queue_count[IONIC_QTYPE_RXQ]);
4041 
4042 	/* limit values to play nice with kdump */
4043 	if (is_kdump_kernel()) {
4044 		dev_nintrs = 2;
4045 		neqs_per_lif = 0;
4046 		nnqs_per_lif = 0;
4047 		ntxqs_per_lif = 1;
4048 		nrxqs_per_lif = 1;
4049 	}
4050 
4051 	/* reserve last queue id for hardware timestamping */
4052 	if (lc->features & cpu_to_le64(IONIC_ETH_HW_TIMESTAMP)) {
4053 		if (ntxqs_per_lif <= 1 || nrxqs_per_lif <= 1) {
4054 			lc->features &= cpu_to_le64(~IONIC_ETH_HW_TIMESTAMP);
4055 		} else {
4056 			ntxqs_per_lif -= 1;
4057 			nrxqs_per_lif -= 1;
4058 		}
4059 	}
4060 
4061 	nxqs = min(ntxqs_per_lif, nrxqs_per_lif);
4062 	nxqs = min(nxqs, num_online_cpus());
4063 	neqs = min(neqs_per_lif, num_online_cpus());
4064 
4065 try_again:
4066 	/* interrupt usage:
4067 	 *    1 for master lif adminq/notifyq
4068 	 *    1 for each CPU for master lif TxRx queue pairs
4069 	 *    whatever's left is for RDMA queues
4070 	 */
4071 	nintrs = 1 + nxqs + neqs;
4072 	min_intrs = 2;  /* adminq + 1 TxRx queue pair */
4073 
4074 	if (nintrs > dev_nintrs)
4075 		goto try_fewer;
4076 
4077 	err = ionic_bus_alloc_irq_vectors(ionic, nintrs);
4078 	if (err < 0 && err != -ENOSPC) {
4079 		dev_err(ionic->dev, "Can't get intrs from OS: %d\n", err);
4080 		return err;
4081 	}
4082 	if (err == -ENOSPC)
4083 		goto try_fewer;
4084 
4085 	if (err != nintrs) {
4086 		ionic_bus_free_irq_vectors(ionic);
4087 		goto try_fewer;
4088 	}
4089 
4090 	ionic->nnqs_per_lif = nnqs_per_lif;
4091 	ionic->neqs_per_lif = neqs;
4092 	ionic->ntxqs_per_lif = nxqs;
4093 	ionic->nrxqs_per_lif = nxqs;
4094 	ionic->nintrs = nintrs;
4095 
4096 	ionic_debugfs_add_sizes(ionic);
4097 
4098 	return 0;
4099 
4100 try_fewer:
4101 	if (nnqs_per_lif > 1) {
4102 		nnqs_per_lif >>= 1;
4103 		goto try_again;
4104 	}
4105 	if (neqs > 1) {
4106 		neqs >>= 1;
4107 		goto try_again;
4108 	}
4109 	if (nxqs > 1) {
4110 		nxqs >>= 1;
4111 		goto try_again;
4112 	}
4113 	dev_err(ionic->dev, "Can't get minimum %d intrs from OS\n", min_intrs);
4114 	return -ENOSPC;
4115 }
4116