xref: /linux/drivers/net/ethernet/alibaba/eea/eea_net.c (revision 4e88fb3234c864b67338ca8d48ca515cf9992ab6)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Driver for Alibaba Elastic Ethernet Adapter.
4  *
5  * Copyright (C) 2025 Alibaba Inc.
6  */
7 
8 #include <linux/etherdevice.h>
9 #include <linux/module.h>
10 #include <linux/netdevice.h>
11 #include <linux/rtnetlink.h>
12 #include <net/netdev_queues.h>
13 
14 #include "eea_adminq.h"
15 #include "eea_net.h"
16 #include "eea_pci.h"
17 #include "eea_ring.h"
18 
19 #define EEA_SPLIT_HDR_SIZE ALIGN(128, L1_CACHE_BYTES)
20 
21 static irqreturn_t eea_irq_handler(int irq, void *data)
22 {
23 	struct eea_irq_blk *blk = data;
24 
25 	napi_schedule_irqoff(&blk->napi);
26 
27 	return IRQ_HANDLED;
28 }
29 
30 static void eea_free_irq_blk(struct eea_net *enet)
31 {
32 	struct eea_irq_blk *blk;
33 	u32 num;
34 	int i;
35 
36 	if (!enet->irq_blks)
37 		return;
38 
39 	num = enet->edev->rx_num;
40 
41 	for (i = 0; i < num; i++) {
42 		blk = &enet->irq_blks[i];
43 
44 		if (blk->ready)
45 			eea_pci_free_irq(blk);
46 
47 		blk->ready = false;
48 	}
49 
50 	kvfree(enet->irq_blks);
51 	enet->irq_blks = NULL;
52 }
53 
54 /* The driver will always attempt to allocate IRQ blocks based on the maximum
55  * possible queue num.
56  */
57 static int eea_alloc_irq_blks(struct eea_net *enet)
58 {
59 	struct eea_device *edev = enet->edev;
60 	struct eea_irq_blk *blk, *irq_blks;
61 	int i, err, num;
62 
63 	num = enet->edev->rx_num;
64 
65 	irq_blks = kvcalloc(num, sizeof(*blk), GFP_KERNEL);
66 	if (!irq_blks)
67 		return -ENOMEM;
68 
69 	enet->irq_blks = irq_blks;
70 
71 	for (i = 0; i < num; i++) {
72 		blk = &irq_blks[i];
73 		blk->idx = i;
74 
75 		/* vec 0 is for error notify. */
76 		blk->msix_vec = i + 1;
77 
78 		err = eea_pci_request_irq(edev, blk, eea_irq_handler);
79 		if (err)
80 			goto err_free_irq_blk;
81 
82 		blk->ready = true;
83 	}
84 
85 	return 0;
86 
87 err_free_irq_blk:
88 	eea_free_irq_blk(enet);
89 	return err;
90 }
91 
92 static int eea_update_queues(struct eea_net *enet)
93 {
94 	return netif_set_real_num_queues(enet->netdev, enet->cfg.tx_ring_num,
95 					 enet->cfg.rx_ring_num);
96 }
97 
98 void eea_init_ctx(struct eea_net *enet, struct eea_net_init_ctx *ctx)
99 {
100 	memset(ctx, 0, sizeof(*ctx));
101 
102 	ctx->netdev = enet->netdev;
103 	ctx->edev = enet->edev;
104 	ctx->cfg = enet->cfg;
105 }
106 
107 static void eea_bind_q_and_cfg(struct eea_net *enet,
108 			       struct eea_net_init_ctx *ctx)
109 {
110 	struct eea_irq_blk *blk;
111 	struct eea_net_rx *rx;
112 	struct eea_net_tx *tx;
113 	int i;
114 
115 	/* Since 'ndo_get_stats64' is not called in softirq context, there is no
116 	 * need to use 'spin_lock_bh'.
117 	 */
118 	spin_lock(&enet->stats_lock);
119 
120 	enet->cfg = ctx->cfg;
121 	enet->rx = ctx->rx;
122 	enet->tx = ctx->tx;
123 
124 	for (i = 0; i < ctx->cfg.rx_ring_num; i++) {
125 		blk = &enet->irq_blks[i];
126 
127 		rx = ctx->rx[i];
128 		tx = &ctx->tx[i];
129 
130 		rx->enet = enet;
131 		rx->napi = &blk->napi;
132 		rx->ering->msix_vec = blk->msix_vec;
133 
134 		tx->enet = enet;
135 		tx->ering->msix_vec = blk->msix_vec;
136 
137 		blk->rx = rx;
138 	}
139 
140 	spin_unlock(&enet->stats_lock);
141 }
142 
143 static void eea_unbind_q_and_cfg(struct eea_net *enet,
144 				 struct eea_net_init_ctx *ctx)
145 {
146 	struct eea_irq_blk *blk;
147 	struct eea_net_rx *rx;
148 	int i;
149 
150 	spin_lock(&enet->stats_lock);
151 
152 	ctx->cfg = enet->cfg;
153 	ctx->rx = enet->rx;
154 	ctx->tx = enet->tx;
155 
156 	enet->rx = NULL;
157 	enet->tx = NULL;
158 
159 	for (i = 0; i < ctx->cfg.rx_ring_num; i++) {
160 		blk = &enet->irq_blks[i];
161 
162 		rx = ctx->rx[i];
163 
164 		rx->napi = NULL;
165 
166 		blk->rx = NULL;
167 	}
168 
169 	spin_unlock(&enet->stats_lock);
170 }
171 
172 static void eea_free_rxtx_q_mem(struct eea_net_init_ctx *ctx)
173 {
174 	struct eea_net_rx *rx;
175 	struct eea_net_tx *tx;
176 	int i;
177 
178 	for (i = 0; i < ctx->cfg.rx_ring_num; i++) {
179 		rx = ctx->rx[i];
180 		tx = &ctx->tx[i];
181 
182 		eea_free_rx(rx, &ctx->cfg);
183 		eea_free_tx(tx, &ctx->cfg);
184 	}
185 
186 	kvfree(ctx->rx);
187 	kvfree(ctx->tx);
188 }
189 
190 /* alloc tx/rx: struct, ring, meta, pp, napi */
191 static int eea_alloc_rxtx_q_mem(struct eea_net_init_ctx *ctx)
192 {
193 	struct eea_net_rx *rx;
194 	struct eea_net_tx *tx;
195 	int err, i;
196 
197 	ctx->tx = kvcalloc(ctx->cfg.tx_ring_num, sizeof(*ctx->tx), GFP_KERNEL);
198 	if (!ctx->tx)
199 		return -ENOMEM;
200 
201 	ctx->rx = kvcalloc(ctx->cfg.rx_ring_num, sizeof(*ctx->rx), GFP_KERNEL);
202 	if (!ctx->rx)
203 		goto err_free_tx;
204 
205 	ctx->cfg.rx_sq_desc_size = sizeof(struct eea_rx_desc);
206 	ctx->cfg.rx_cq_desc_size = sizeof(struct eea_rx_cdesc);
207 	ctx->cfg.tx_sq_desc_size = sizeof(struct eea_tx_desc);
208 	ctx->cfg.tx_cq_desc_size = sizeof(struct eea_tx_cdesc);
209 
210 	/* ethtool may config this. */
211 	if (!ctx->cfg.split_hdr)
212 		ctx->cfg.rx_sq_desc_size = sizeof(struct eea_rx_desc_no_hdr);
213 
214 	for (i = 0; i < ctx->cfg.rx_ring_num; i++) {
215 		rx = eea_alloc_rx(ctx, i);
216 		if (!rx)
217 			goto err_free;
218 
219 		ctx->rx[i] = rx;
220 
221 		tx = ctx->tx + i;
222 		err = eea_alloc_tx(ctx, tx, i);
223 		if (err)
224 			goto err_free;
225 	}
226 
227 	return 0;
228 
229 err_free:
230 	for (i = 0; i < ctx->cfg.rx_ring_num; i++) {
231 		rx = ctx->rx[i];
232 		tx = ctx->tx + i;
233 
234 		eea_free_rx(rx, &ctx->cfg);
235 		eea_free_tx(tx, &ctx->cfg);
236 	}
237 
238 	kvfree(ctx->rx);
239 
240 err_free_tx:
241 	kvfree(ctx->tx);
242 	return -ENOMEM;
243 }
244 
245 static int eea_hw_active_ring(struct eea_net *enet)
246 {
247 	return eea_adminq_create_q(enet, enet->cfg.rx_ring_num
248 				   + enet->cfg.tx_ring_num, 0);
249 }
250 
251 static int eea_hw_unactive_ring(struct eea_net *enet)
252 {
253 	int err;
254 
255 	err = eea_adminq_destroy_all_q(enet);
256 	if (err)
257 		netdev_warn(enet->netdev, "unactive rxtx ring failed.\n");
258 
259 	return err;
260 }
261 
262 /* stop rx napi, stop tx queue. */
263 static void eea_stop_rxtx(struct net_device *netdev)
264 {
265 	struct eea_net *enet = netdev_priv(netdev);
266 	int i;
267 
268 	netif_tx_disable(netdev);
269 
270 	for (i = 0; i < enet->cfg.rx_ring_num; i++)
271 		enet_rx_stop(enet->rx[i]);
272 
273 	netif_carrier_off(netdev);
274 }
275 
276 static void eea_start_rxtx(struct eea_net *enet)
277 {
278 	int i;
279 
280 	for (i = 0; i < enet->cfg.rx_ring_num; i++)
281 		enet_rx_start(enet->rx[i]);
282 
283 	netif_tx_start_all_queues(enet->netdev);
284 	netif_carrier_on(enet->netdev);
285 
286 	enet->started = true;
287 }
288 
289 static int eea_netdev_stop(struct net_device *netdev)
290 {
291 	struct eea_net *enet = netdev_priv(netdev);
292 	struct eea_net_init_ctx ctx;
293 
294 	/* This function can be called during device anomaly recovery. To
295 	 * prevent duplicate stop operations, the `started` flag is introduced
296 	 * for checking.
297 	 */
298 
299 	if (!enet->started) {
300 		netdev_warn(netdev, "eea netdev stop: but dev is not started.\n");
301 		return 0;
302 	}
303 
304 	eea_init_ctx(enet, &ctx);
305 
306 	eea_stop_rxtx(netdev);
307 	eea_hw_unactive_ring(enet);
308 	eea_unbind_q_and_cfg(enet, &ctx);
309 	eea_free_rxtx_q_mem(&ctx);
310 
311 	enet->started = false;
312 
313 	return 0;
314 }
315 
316 static int eea_netdev_open(struct net_device *netdev)
317 {
318 	struct eea_net *enet = netdev_priv(netdev);
319 	struct eea_net_init_ctx ctx;
320 	int err;
321 
322 	if (enet->link_err) {
323 		netdev_err(netdev, "netdev open err, because link error: %d\n",
324 			   enet->link_err);
325 		return -EBUSY;
326 	}
327 
328 	eea_init_ctx(enet, &ctx);
329 
330 	err = eea_alloc_rxtx_q_mem(&ctx);
331 	if (err)
332 		goto err_done;
333 
334 	eea_bind_q_and_cfg(enet, &ctx);
335 
336 	err = eea_update_queues(enet);
337 	if (err)
338 		goto err_free_q;
339 
340 	err = eea_hw_active_ring(enet);
341 	if (err)
342 		goto err_free_q;
343 
344 	eea_start_rxtx(enet);
345 
346 	return 0;
347 
348 err_free_q:
349 	eea_unbind_q_and_cfg(enet, &ctx);
350 	eea_free_rxtx_q_mem(&ctx);
351 
352 err_done:
353 	return err;
354 }
355 
356 /* Statistics may be reset to zero upon device reset. This is expected behavior
357  * for now and will be addressed in the future.
358  */
359 static void eea_stats(struct net_device *netdev, struct rtnl_link_stats64 *tot)
360 {
361 	struct eea_net *enet = netdev_priv(netdev);
362 	u64 packets, bytes, drop, lerr;
363 	u32 start;
364 	int i;
365 
366 	spin_lock(&enet->stats_lock);
367 
368 	if (enet->rx) {
369 		for (i = 0; i < enet->cfg.rx_ring_num; i++) {
370 			struct eea_net_rx *rx = enet->rx[i];
371 
372 			do {
373 				start = u64_stats_fetch_begin(&rx->stats.syncp);
374 				packets = u64_stats_read(&rx->stats.packets);
375 				bytes = u64_stats_read(&rx->stats.bytes);
376 				drop = u64_stats_read(&rx->stats.drops);
377 				lerr = u64_stats_read(&rx->stats.length_errors);
378 			} while (u64_stats_fetch_retry(&rx->stats.syncp,
379 						       start));
380 
381 			tot->rx_packets       += packets;
382 			tot->rx_bytes         += bytes;
383 			tot->rx_dropped       += drop;
384 			tot->rx_length_errors += lerr;
385 			tot->rx_errors        += lerr;
386 		}
387 	}
388 
389 	if (enet->tx) {
390 		for (i = 0; i < enet->cfg.tx_ring_num; i++) {
391 			struct eea_net_tx *tx = &enet->tx[i];
392 
393 			do {
394 				start = u64_stats_fetch_begin(&tx->stats.syncp);
395 				packets = u64_stats_read(&tx->stats.packets);
396 				bytes = u64_stats_read(&tx->stats.bytes);
397 				drop = u64_stats_read(&tx->stats.drops);
398 			} while (u64_stats_fetch_retry(&tx->stats.syncp,
399 						       start));
400 
401 			tot->tx_packets += packets;
402 			tot->tx_bytes   += bytes;
403 			tot->tx_dropped += drop;
404 		}
405 	}
406 
407 	spin_unlock(&enet->stats_lock);
408 }
409 
410 /* resources: ring, buffers, irq */
411 int eea_reset_hw_resources(struct eea_net *enet, struct eea_net_init_ctx *ctx)
412 {
413 	struct eea_net_init_ctx ctx_old = {0};
414 	int err, error;
415 
416 	if (!netif_running(enet->netdev) || !enet->started) {
417 		spin_lock(&enet->stats_lock);
418 		enet->cfg = ctx->cfg;
419 		spin_unlock(&enet->stats_lock);
420 		return 0;
421 	}
422 
423 	err = eea_alloc_rxtx_q_mem(ctx);
424 	if (err) {
425 		netdev_warn(enet->netdev,
426 			    "eea reset: alloc q failed. stop reset. err %d\n",
427 			    err);
428 		return err;
429 	}
430 
431 	eea_stop_rxtx(enet->netdev);
432 	eea_hw_unactive_ring(enet);
433 
434 	eea_unbind_q_and_cfg(enet, &ctx_old);
435 	eea_bind_q_and_cfg(enet, ctx);
436 
437 	err = eea_update_queues(enet);
438 	if (err) {
439 		netdev_err(enet->netdev,
440 			   "eea reset: set real num queues failed. err %d\n",
441 			   err);
442 		goto err_bind_old;
443 	}
444 
445 	err = eea_hw_active_ring(enet);
446 	if (err) {
447 		netdev_err(enet->netdev, "eea reset: active new ring. err %d\n",
448 			   err);
449 		eea_unbind_q_and_cfg(enet, ctx);
450 		goto err_free_q;
451 	}
452 
453 	eea_start_rxtx(enet);
454 	eea_free_rxtx_q_mem(&ctx_old);
455 	return 0;
456 
457 err_bind_old:
458 	eea_unbind_q_and_cfg(enet, ctx);
459 	eea_bind_q_and_cfg(enet, &ctx_old);
460 	error = eea_hw_active_ring(enet);
461 	if (error) {
462 		netdev_err(enet->netdev, "eea reset: active old ring. err %d\n",
463 			   error);
464 		eea_unbind_q_and_cfg(enet, &ctx_old);
465 		err = error;
466 		goto err_free_q;
467 	}
468 
469 	eea_start_rxtx(enet);
470 	eea_free_rxtx_q_mem(ctx);
471 	return err;
472 
473 err_free_q:
474 
475 	/* An exception occurred at the hardware level, and there's not much we
476 	 * can do about it -- we can only release the resources first.
477 	 */
478 	eea_free_rxtx_q_mem(ctx);
479 	eea_free_rxtx_q_mem(&ctx_old);
480 	enet->started = false;
481 	return err;
482 }
483 
484 int eea_queues_check_and_reset(struct eea_device *edev)
485 {
486 	struct eea_aq_dev_status dstatus = {0};
487 	struct eea_aq_queue_status *qstatus;
488 	struct eea_aq_queue_status *qs;
489 	struct eea_net_init_ctx ctx;
490 	bool need_reset = false;
491 	int i, err = 0;
492 
493 	rtnl_lock();
494 
495 	if (!netif_running(edev->enet->netdev))
496 		goto err_unlock;
497 
498 	/* Maybe stopped by ha. */
499 	if (!edev->enet->started || edev->enet->link_err)
500 		goto err_unlock;
501 
502 	err = eea_adminq_dev_status(edev->enet, &dstatus);
503 	if (err) {
504 		netdev_warn(edev->enet->netdev, "query queue status failed.\n");
505 		goto err_unlock;
506 	}
507 
508 	if (le16_to_cpu(dstatus.status->link_status) == EEA_LINK_DOWN_STATUS) {
509 		/* The device is broken, can not be up. */
510 		eea_netdev_stop(edev->enet->netdev);
511 		edev->enet->link_err = EEA_LINK_ERR_LINK_DOWN;
512 		netdev_warn(edev->enet->netdev, "device link is down. stop device.\n");
513 		goto err_free;
514 	}
515 
516 	qstatus = dstatus.status->q_status;
517 
518 	for (i = 0; i < dstatus.num; ++i) {
519 		qs = &qstatus[i];
520 
521 		if (le16_to_cpu(qs->status) == EEA_QUEUE_STATUS_NEED_RESET) {
522 			netdev_warn(edev->enet->netdev,
523 				    "queue status: queue %u needs to reset\n",
524 				    le16_to_cpu(qs->qidx));
525 			need_reset = true;
526 		}
527 	}
528 
529 	if (need_reset) {
530 		eea_init_ctx(edev->enet, &ctx);
531 		err = eea_reset_hw_resources(edev->enet, &ctx);
532 	}
533 
534 err_free:
535 	kfree(dstatus.status);
536 
537 err_unlock:
538 	rtnl_unlock();
539 	return err;
540 }
541 
542 static int eea_update_cfg(struct eea_net *enet,
543 			  struct eea_device *edev,
544 			  struct eea_aq_cfg *hwcfg)
545 {
546 	u32 rx_max = le32_to_cpu(hwcfg->rx_depth_max);
547 	u32 tx_max = le32_to_cpu(hwcfg->tx_depth_max);
548 	u32 rx_def = le32_to_cpu(hwcfg->rx_depth_def);
549 	u32 tx_def = le32_to_cpu(hwcfg->tx_depth_def);
550 
551 	/* Now, we assert that the rx ring num is equal to the tx ring num. */
552 	if (edev->rx_num != edev->tx_num) {
553 		dev_err(edev->dma_dev, "Inconsistent ring num: RX %u, TX %u\n",
554 			edev->rx_num, edev->tx_num);
555 		return -EINVAL;
556 	}
557 
558 	if (rx_max > EEA_NET_IO_HW_RING_DEPTH_MAX ||
559 	    rx_max < EEA_NET_IO_HW_RING_DEPTH_MIN ||
560 	    tx_max > EEA_NET_IO_HW_RING_DEPTH_MAX ||
561 	    tx_max < EEA_NET_IO_HW_RING_DEPTH_MIN) {
562 		dev_err(edev->dma_dev, "Invalid HW max depth: RX %u, TX %u\n",
563 			rx_max, tx_max);
564 		return -EINVAL;
565 	}
566 
567 	if (rx_def > rx_max ||
568 	    tx_def > tx_max ||
569 	    rx_def < EEA_NET_IO_HW_RING_DEPTH_MIN ||
570 	    tx_def < EEA_NET_IO_HW_RING_DEPTH_MIN) {
571 		dev_err(edev->dma_dev, "Invalid default depth: RX %u (max %u), TX %u (max %u)\n",
572 			rx_def, rx_max, tx_def, tx_max);
573 		return -EINVAL;
574 	}
575 
576 	if (!is_power_of_2(rx_max) || !is_power_of_2(tx_max) ||
577 	    !is_power_of_2(rx_def) || !is_power_of_2(tx_def)) {
578 		dev_err(edev->dma_dev, "Ring depth must be power of 2\n");
579 		return -EINVAL;
580 	}
581 
582 	enet->cfg_hw.rx_ring_depth = rx_max;
583 	enet->cfg_hw.tx_ring_depth = tx_max;
584 	enet->cfg_hw.rx_ring_num = edev->rx_num;
585 	enet->cfg_hw.tx_ring_num = edev->tx_num;
586 	enet->cfg_hw.split_hdr = EEA_SPLIT_HDR_SIZE;
587 
588 	enet->cfg.rx_ring_depth = rx_def;
589 	enet->cfg.tx_ring_depth = tx_def;
590 	enet->cfg.rx_ring_num = edev->rx_num;
591 	enet->cfg.tx_ring_num = edev->tx_num;
592 
593 	return 0;
594 }
595 
596 static int eea_netdev_init_features(struct net_device *netdev,
597 				    struct eea_net *enet,
598 				    struct eea_device *edev)
599 {
600 	struct eea_aq_cfg *cfg;
601 	int err;
602 	u32 mtu;
603 
604 	cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
605 	if (!cfg)
606 		return -ENOMEM;
607 
608 	err = eea_adminq_query_cfg(enet, cfg);
609 	if (err)
610 		goto err_free;
611 
612 	mtu = le16_to_cpu(cfg->mtu);
613 	if (mtu < ETH_MIN_MTU) {
614 		dev_err(edev->dma_dev, "The device gave us an invalid MTU. Here we can only exit the initialization. %u < %u\n",
615 			mtu, ETH_MIN_MTU);
616 		err = -EINVAL;
617 		goto err_free;
618 	}
619 
620 	err = eea_update_cfg(enet, edev, cfg);
621 	if (err)
622 		goto err_free;
623 
624 	netdev->priv_flags |= IFF_UNICAST_FLT;
625 	netdev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
626 
627 	netdev->hw_features |= NETIF_F_HW_CSUM;
628 	netdev->hw_features |= NETIF_F_GRO_HW;
629 	netdev->hw_features |= NETIF_F_SG;
630 	netdev->hw_features |= NETIF_F_TSO;
631 	netdev->hw_features |= NETIF_F_TSO_ECN;
632 	netdev->hw_features |= NETIF_F_TSO6;
633 	netdev->hw_features |= NETIF_F_GSO_UDP_L4;
634 
635 	netdev->features |= NETIF_F_HIGHDMA;
636 	netdev->features |= NETIF_F_HW_CSUM;
637 	netdev->features |= NETIF_F_SG;
638 	netdev->features |= NETIF_F_GSO_ROBUST;
639 	netdev->features |= netdev->hw_features & NETIF_F_ALL_TSO;
640 	netdev->features |= NETIF_F_RXCSUM;
641 	netdev->features |= NETIF_F_GRO_HW;
642 
643 	netdev->vlan_features = netdev->features;
644 
645 	if (!is_valid_ether_addr(cfg->mac)) {
646 		dev_err(edev->dma_dev, "The device gave invalid mac %pM\n",
647 			cfg->mac);
648 		err = -EINVAL;
649 		goto err_free;
650 	}
651 
652 	eth_hw_addr_set(netdev, cfg->mac);
653 
654 	enet->speed = SPEED_UNKNOWN;
655 	enet->duplex = DUPLEX_UNKNOWN;
656 
657 	netdev->min_mtu = ETH_MIN_MTU;
658 
659 	netdev->mtu = mtu;
660 
661 	/* If jumbo frames are already enabled, then the returned MTU will be a
662 	 * jumbo MTU, and the driver will automatically enable jumbo frame
663 	 * support by default.
664 	 */
665 	netdev->max_mtu = mtu;
666 
667 err_free:
668 	kfree(cfg);
669 	return err;
670 }
671 
672 static const struct net_device_ops eea_netdev = {
673 	.ndo_open           = eea_netdev_open,
674 	.ndo_stop           = eea_netdev_stop,
675 	.ndo_start_xmit     = eea_tx_xmit,
676 	.ndo_validate_addr  = eth_validate_addr,
677 	.ndo_get_stats64    = eea_stats,
678 	.ndo_features_check = passthru_features_check,
679 };
680 
681 static struct eea_net *eea_netdev_alloc(struct eea_device *edev, u32 pairs)
682 {
683 	struct net_device *netdev;
684 	struct eea_net *enet;
685 	int err;
686 
687 	netdev = alloc_etherdev_mq(sizeof(struct eea_net), pairs);
688 	if (!netdev) {
689 		dev_err(edev->dma_dev,
690 			"alloc_etherdev_mq failed with pairs %d\n", pairs);
691 		return NULL;
692 	}
693 
694 	netdev->netdev_ops = &eea_netdev;
695 	netdev->ethtool_ops = &eea_ethtool_ops;
696 	SET_NETDEV_DEV(netdev, edev->dma_dev);
697 
698 	enet = netdev_priv(netdev);
699 	enet->netdev = netdev;
700 	enet->edev = edev;
701 	edev->enet = enet;
702 
703 	err = eea_alloc_irq_blks(enet);
704 	if (err) {
705 		dev_err(edev->dma_dev,
706 			"eea_alloc_irq_blks failed with pairs %d\n", pairs);
707 		free_netdev(netdev);
708 		return NULL;
709 	}
710 
711 	spin_lock_init(&enet->stats_lock);
712 
713 	return enet;
714 }
715 
716 static void eea_update_ts_off(struct eea_device *edev, struct eea_net *enet)
717 {
718 	u64 ts;
719 
720 	ts = eea_pci_device_ts(edev);
721 
722 	enet->hw_ts_offset = ktime_get_real() - ts;
723 }
724 
725 static int eea_net_reprobe(struct eea_device *edev)
726 {
727 	struct eea_net *enet = edev->enet;
728 	int err = 0;
729 
730 	enet->edev = edev;
731 
732 	if (!enet->adminq.ring) {
733 		err = eea_create_adminq(enet, edev->rx_num + edev->tx_num);
734 		if (err)
735 			return err;
736 	}
737 
738 	err = eea_alloc_irq_blks(enet);
739 	if (err)
740 		goto err_destroy_aq;
741 
742 	eea_update_ts_off(edev, enet);
743 
744 	rtnl_lock();
745 
746 	enet->link_err = 0;
747 	if (edev->ha_reset_netdev_running &&
748 	    netif_running(edev->enet->netdev)) {
749 		err = eea_netdev_open(enet->netdev);
750 		if (err) {
751 			enet->link_err = EEA_LINK_ERR_HA_RESET_DEV;
752 			rtnl_unlock();
753 			goto err_free_irq_blks;
754 		}
755 	}
756 
757 	rtnl_unlock();
758 
759 	enet->wait_pci_ready = false;
760 	return 0;
761 
762 err_free_irq_blks:
763 	eea_free_irq_blk(enet);
764 
765 err_destroy_aq:
766 	eea_destroy_adminq(enet);
767 
768 	return err;
769 }
770 
771 int eea_net_probe(struct eea_device *edev)
772 {
773 	struct eea_net *enet;
774 	int err = -ENOMEM;
775 
776 	/* If edev->enet is not null, then this is called from ha reset worker.
777 	 * Call eea_net_reprobe() directly.
778 	 */
779 	if (edev->enet)
780 		return eea_net_reprobe(edev);
781 
782 	enet = eea_netdev_alloc(edev, edev->rx_num);
783 	if (!enet)
784 		return -ENOMEM;
785 
786 	err = eea_create_adminq(enet, edev->rx_num + edev->tx_num);
787 	if (err)
788 		goto err_free_netdev;
789 
790 	eea_adminq_config_host_info(enet);
791 
792 	err = eea_netdev_init_features(enet->netdev, enet, edev);
793 	if (err)
794 		goto err_reset_dev;
795 
796 	eea_update_ts_off(edev, enet);
797 
798 	netif_carrier_off(enet->netdev);
799 
800 	err = register_netdev(enet->netdev);
801 	if (err)
802 		goto err_reset_dev;
803 
804 	netdev_dbg(enet->netdev, "eea probe success.\n");
805 
806 	return 0;
807 
808 err_reset_dev:
809 	eea_device_reset(edev);
810 	eea_destroy_adminq(enet);
811 
812 err_free_netdev:
813 	eea_free_irq_blk(enet);
814 	free_netdev(enet->netdev);
815 	return err;
816 }
817 
818 static void eea_net_ha_reset_remove(struct eea_net *enet,
819 				    struct eea_device *edev)
820 {
821 	rtnl_lock();
822 	edev->ha_reset_netdev_running = false;
823 	if (netif_running(enet->netdev)) {
824 		eea_netdev_stop(enet->netdev);
825 		edev->ha_reset_netdev_running = true;
826 	}
827 
828 	/* Prevent that the user set up the net device. */
829 	enet->link_err = EEA_LINK_ERR_HA_RESET_DEV;
830 
831 	rtnl_unlock();
832 
833 	eea_device_reset(edev);
834 	eea_destroy_adminq(enet);
835 	eea_free_irq_blk(enet);
836 
837 	enet->wait_pci_ready = true;
838 }
839 
840 void eea_net_remove(struct eea_device *edev, bool ha)
841 {
842 	struct net_device *netdev;
843 	struct eea_net *enet;
844 
845 	enet = edev->enet;
846 	netdev = enet->netdev;
847 
848 	if (ha) {
849 		if (enet->wait_pci_ready)
850 			return;
851 
852 		eea_net_ha_reset_remove(enet, edev);
853 		return;
854 	}
855 
856 	unregister_netdev(netdev);
857 
858 	if (!enet->wait_pci_ready) {
859 		eea_device_reset(edev);
860 		eea_destroy_adminq(enet);
861 		eea_free_irq_blk(enet);
862 	}
863 
864 	free_netdev(netdev);
865 }
866 
867 void eea_net_shutdown(struct eea_device *edev)
868 {
869 	struct net_device *netdev;
870 	struct eea_net *enet;
871 
872 	enet = edev->enet;
873 	netdev = enet->netdev;
874 
875 	rtnl_lock();
876 
877 	netif_device_detach(netdev);
878 	dev_close(netdev);
879 
880 	if (!enet->wait_pci_ready) {
881 		eea_device_reset(edev);
882 		eea_destroy_adminq(enet);
883 		eea_free_irq_blk(enet);
884 	}
885 
886 	rtnl_unlock();
887 }
888