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