xref: /linux/drivers/net/ethernet/amazon/ena/ena_netdev.c (revision ca93bf607a44c1f009283dac4af7df0d9ae5e357)
1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /*
3  * Copyright 2015-2020 Amazon.com, Inc. or its affiliates. All rights reserved.
4  */
5 
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 
8 #ifdef CONFIG_RFS_ACCEL
9 #include <linux/cpu_rmap.h>
10 #endif /* CONFIG_RFS_ACCEL */
11 #include <linux/ethtool.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/numa.h>
15 #include <linux/pci.h>
16 #include <linux/utsname.h>
17 #include <linux/version.h>
18 #include <linux/vmalloc.h>
19 #include <net/ip.h>
20 
21 #include "ena_netdev.h"
22 #include "ena_pci_id_tbl.h"
23 #include "ena_xdp.h"
24 
25 MODULE_AUTHOR("Amazon.com, Inc. or its affiliates");
26 MODULE_DESCRIPTION(DEVICE_NAME);
27 MODULE_LICENSE("GPL");
28 
29 /* Time in jiffies before concluding the transmitter is hung. */
30 #define TX_TIMEOUT  (5 * HZ)
31 
32 #define ENA_MAX_RINGS min_t(unsigned int, ENA_MAX_NUM_IO_QUEUES, num_possible_cpus())
33 
34 #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | \
35 		NETIF_MSG_TX_DONE | NETIF_MSG_TX_ERR | NETIF_MSG_RX_ERR)
36 
37 static struct ena_aenq_handlers aenq_handlers;
38 
39 static struct workqueue_struct *ena_wq;
40 
41 MODULE_DEVICE_TABLE(pci, ena_pci_tbl);
42 
43 static int ena_rss_init_default(struct ena_adapter *adapter);
44 static void check_for_admin_com_state(struct ena_adapter *adapter);
45 static void ena_destroy_device(struct ena_adapter *adapter, bool graceful);
46 static int ena_restore_device(struct ena_adapter *adapter);
47 
48 static void ena_tx_timeout(struct net_device *dev, unsigned int txqueue)
49 {
50 	struct ena_adapter *adapter = netdev_priv(dev);
51 
52 	/* Change the state of the device to trigger reset
53 	 * Check that we are not in the middle or a trigger already
54 	 */
55 
56 	if (test_and_set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags))
57 		return;
58 
59 	ena_reset_device(adapter, ENA_REGS_RESET_OS_NETDEV_WD);
60 	ena_increase_stat(&adapter->dev_stats.tx_timeout, 1, &adapter->syncp);
61 
62 	netif_err(adapter, tx_err, dev, "Transmit time out\n");
63 }
64 
65 static void update_rx_ring_mtu(struct ena_adapter *adapter, int mtu)
66 {
67 	int i;
68 
69 	for (i = 0; i < adapter->num_io_queues; i++)
70 		adapter->rx_ring[i].mtu = mtu;
71 }
72 
73 static int ena_change_mtu(struct net_device *dev, int new_mtu)
74 {
75 	struct ena_adapter *adapter = netdev_priv(dev);
76 	int ret;
77 
78 	ret = ena_com_set_dev_mtu(adapter->ena_dev, new_mtu);
79 	if (!ret) {
80 		netif_dbg(adapter, drv, dev, "Set MTU to %d\n", new_mtu);
81 		update_rx_ring_mtu(adapter, new_mtu);
82 		dev->mtu = new_mtu;
83 	} else {
84 		netif_err(adapter, drv, dev, "Failed to set MTU to %d\n",
85 			  new_mtu);
86 	}
87 
88 	return ret;
89 }
90 
91 int ena_xmit_common(struct ena_adapter *adapter,
92 		    struct ena_ring *ring,
93 		    struct ena_tx_buffer *tx_info,
94 		    struct ena_com_tx_ctx *ena_tx_ctx,
95 		    u16 next_to_use,
96 		    u32 bytes)
97 {
98 	int rc, nb_hw_desc;
99 
100 	if (unlikely(ena_com_is_doorbell_needed(ring->ena_com_io_sq,
101 						ena_tx_ctx))) {
102 		netif_dbg(adapter, tx_queued, adapter->netdev,
103 			  "llq tx max burst size of queue %d achieved, writing doorbell to send burst\n",
104 			  ring->qid);
105 		ena_ring_tx_doorbell(ring);
106 	}
107 
108 	/* prepare the packet's descriptors to dma engine */
109 	rc = ena_com_prepare_tx(ring->ena_com_io_sq, ena_tx_ctx,
110 				&nb_hw_desc);
111 
112 	/* In case there isn't enough space in the queue for the packet,
113 	 * we simply drop it. All other failure reasons of
114 	 * ena_com_prepare_tx() are fatal and therefore require a device reset.
115 	 */
116 	if (unlikely(rc)) {
117 		netif_err(adapter, tx_queued, adapter->netdev,
118 			  "Failed to prepare tx bufs\n");
119 		ena_increase_stat(&ring->tx_stats.prepare_ctx_err, 1,
120 				  &ring->syncp);
121 		if (rc != -ENOMEM)
122 			ena_reset_device(adapter,
123 					 ENA_REGS_RESET_DRIVER_INVALID_STATE);
124 		return rc;
125 	}
126 
127 	u64_stats_update_begin(&ring->syncp);
128 	ring->tx_stats.cnt++;
129 	ring->tx_stats.bytes += bytes;
130 	u64_stats_update_end(&ring->syncp);
131 
132 	tx_info->tx_descs = nb_hw_desc;
133 	tx_info->total_tx_size = bytes;
134 	tx_info->last_jiffies = jiffies;
135 	tx_info->print_once = 0;
136 
137 	ring->next_to_use = ENA_TX_RING_IDX_NEXT(next_to_use,
138 						 ring->ring_size);
139 	return 0;
140 }
141 
142 static int ena_init_rx_cpu_rmap(struct ena_adapter *adapter)
143 {
144 #ifdef CONFIG_RFS_ACCEL
145 	u32 i;
146 	int rc;
147 
148 	adapter->netdev->rx_cpu_rmap = alloc_irq_cpu_rmap(adapter->num_io_queues);
149 	if (!adapter->netdev->rx_cpu_rmap)
150 		return -ENOMEM;
151 	for (i = 0; i < adapter->num_io_queues; i++) {
152 		int irq_idx = ENA_IO_IRQ_IDX(i);
153 
154 		rc = irq_cpu_rmap_add(adapter->netdev->rx_cpu_rmap,
155 				      pci_irq_vector(adapter->pdev, irq_idx));
156 		if (rc) {
157 			free_irq_cpu_rmap(adapter->netdev->rx_cpu_rmap);
158 			adapter->netdev->rx_cpu_rmap = NULL;
159 			return rc;
160 		}
161 	}
162 #endif /* CONFIG_RFS_ACCEL */
163 	return 0;
164 }
165 
166 static void ena_init_io_rings_common(struct ena_adapter *adapter,
167 				     struct ena_ring *ring, u16 qid)
168 {
169 	ring->qid = qid;
170 	ring->pdev = adapter->pdev;
171 	ring->dev = &adapter->pdev->dev;
172 	ring->netdev = adapter->netdev;
173 	ring->napi = &adapter->ena_napi[qid].napi;
174 	ring->adapter = adapter;
175 	ring->ena_dev = adapter->ena_dev;
176 	ring->per_napi_packets = 0;
177 	ring->cpu = 0;
178 	ring->numa_node = 0;
179 	ring->no_interrupt_event_cnt = 0;
180 	u64_stats_init(&ring->syncp);
181 }
182 
183 void ena_init_io_rings(struct ena_adapter *adapter,
184 		       int first_index, int count)
185 {
186 	struct ena_com_dev *ena_dev;
187 	struct ena_ring *txr, *rxr;
188 	int i;
189 
190 	ena_dev = adapter->ena_dev;
191 
192 	for (i = first_index; i < first_index + count; i++) {
193 		txr = &adapter->tx_ring[i];
194 		rxr = &adapter->rx_ring[i];
195 
196 		/* TX common ring state */
197 		ena_init_io_rings_common(adapter, txr, i);
198 
199 		/* TX specific ring state */
200 		txr->ring_size = adapter->requested_tx_ring_size;
201 		txr->tx_max_header_size = ena_dev->tx_max_header_size;
202 		txr->tx_mem_queue_type = ena_dev->tx_mem_queue_type;
203 		txr->sgl_size = adapter->max_tx_sgl_size;
204 		txr->smoothed_interval =
205 			ena_com_get_nonadaptive_moderation_interval_tx(ena_dev);
206 		txr->disable_meta_caching = adapter->disable_meta_caching;
207 		spin_lock_init(&txr->xdp_tx_lock);
208 
209 		/* Don't init RX queues for xdp queues */
210 		if (!ENA_IS_XDP_INDEX(adapter, i)) {
211 			/* RX common ring state */
212 			ena_init_io_rings_common(adapter, rxr, i);
213 
214 			/* RX specific ring state */
215 			rxr->ring_size = adapter->requested_rx_ring_size;
216 			rxr->rx_copybreak = adapter->rx_copybreak;
217 			rxr->sgl_size = adapter->max_rx_sgl_size;
218 			rxr->smoothed_interval =
219 				ena_com_get_nonadaptive_moderation_interval_rx(ena_dev);
220 			rxr->empty_rx_queue = 0;
221 			rxr->rx_headroom = NET_SKB_PAD;
222 			adapter->ena_napi[i].dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
223 			rxr->xdp_ring = &adapter->tx_ring[i + adapter->num_io_queues];
224 		}
225 	}
226 }
227 
228 /* ena_setup_tx_resources - allocate I/O Tx resources (Descriptors)
229  * @adapter: network interface device structure
230  * @qid: queue index
231  *
232  * Return 0 on success, negative on failure
233  */
234 static int ena_setup_tx_resources(struct ena_adapter *adapter, int qid)
235 {
236 	struct ena_ring *tx_ring = &adapter->tx_ring[qid];
237 	struct ena_irq *ena_irq = &adapter->irq_tbl[ENA_IO_IRQ_IDX(qid)];
238 	int size, i, node;
239 
240 	if (tx_ring->tx_buffer_info) {
241 		netif_err(adapter, ifup,
242 			  adapter->netdev, "tx_buffer_info info is not NULL");
243 		return -EEXIST;
244 	}
245 
246 	size = sizeof(struct ena_tx_buffer) * tx_ring->ring_size;
247 	node = cpu_to_node(ena_irq->cpu);
248 
249 	tx_ring->tx_buffer_info = vzalloc_node(size, node);
250 	if (!tx_ring->tx_buffer_info) {
251 		tx_ring->tx_buffer_info = vzalloc(size);
252 		if (!tx_ring->tx_buffer_info)
253 			goto err_tx_buffer_info;
254 	}
255 
256 	size = sizeof(u16) * tx_ring->ring_size;
257 	tx_ring->free_ids = vzalloc_node(size, node);
258 	if (!tx_ring->free_ids) {
259 		tx_ring->free_ids = vzalloc(size);
260 		if (!tx_ring->free_ids)
261 			goto err_tx_free_ids;
262 	}
263 
264 	size = tx_ring->tx_max_header_size;
265 	tx_ring->push_buf_intermediate_buf = vzalloc_node(size, node);
266 	if (!tx_ring->push_buf_intermediate_buf) {
267 		tx_ring->push_buf_intermediate_buf = vzalloc(size);
268 		if (!tx_ring->push_buf_intermediate_buf)
269 			goto err_push_buf_intermediate_buf;
270 	}
271 
272 	/* Req id ring for TX out of order completions */
273 	for (i = 0; i < tx_ring->ring_size; i++)
274 		tx_ring->free_ids[i] = i;
275 
276 	/* Reset tx statistics */
277 	memset(&tx_ring->tx_stats, 0x0, sizeof(tx_ring->tx_stats));
278 
279 	tx_ring->next_to_use = 0;
280 	tx_ring->next_to_clean = 0;
281 	tx_ring->cpu = ena_irq->cpu;
282 	tx_ring->numa_node = node;
283 	return 0;
284 
285 err_push_buf_intermediate_buf:
286 	vfree(tx_ring->free_ids);
287 	tx_ring->free_ids = NULL;
288 err_tx_free_ids:
289 	vfree(tx_ring->tx_buffer_info);
290 	tx_ring->tx_buffer_info = NULL;
291 err_tx_buffer_info:
292 	return -ENOMEM;
293 }
294 
295 /* ena_free_tx_resources - Free I/O Tx Resources per Queue
296  * @adapter: network interface device structure
297  * @qid: queue index
298  *
299  * Free all transmit software resources
300  */
301 static void ena_free_tx_resources(struct ena_adapter *adapter, int qid)
302 {
303 	struct ena_ring *tx_ring = &adapter->tx_ring[qid];
304 
305 	vfree(tx_ring->tx_buffer_info);
306 	tx_ring->tx_buffer_info = NULL;
307 
308 	vfree(tx_ring->free_ids);
309 	tx_ring->free_ids = NULL;
310 
311 	vfree(tx_ring->push_buf_intermediate_buf);
312 	tx_ring->push_buf_intermediate_buf = NULL;
313 }
314 
315 int ena_setup_tx_resources_in_range(struct ena_adapter *adapter,
316 				    int first_index, int count)
317 {
318 	int i, rc = 0;
319 
320 	for (i = first_index; i < first_index + count; i++) {
321 		rc = ena_setup_tx_resources(adapter, i);
322 		if (rc)
323 			goto err_setup_tx;
324 	}
325 
326 	return 0;
327 
328 err_setup_tx:
329 
330 	netif_err(adapter, ifup, adapter->netdev,
331 		  "Tx queue %d: allocation failed\n", i);
332 
333 	/* rewind the index freeing the rings as we go */
334 	while (first_index < i--)
335 		ena_free_tx_resources(adapter, i);
336 	return rc;
337 }
338 
339 void ena_free_all_io_tx_resources_in_range(struct ena_adapter *adapter,
340 					   int first_index, int count)
341 {
342 	int i;
343 
344 	for (i = first_index; i < first_index + count; i++)
345 		ena_free_tx_resources(adapter, i);
346 }
347 
348 /* ena_free_all_io_tx_resources - Free I/O Tx Resources for All Queues
349  * @adapter: board private structure
350  *
351  * Free all transmit software resources
352  */
353 void ena_free_all_io_tx_resources(struct ena_adapter *adapter)
354 {
355 	ena_free_all_io_tx_resources_in_range(adapter,
356 					      0,
357 					      adapter->xdp_num_queues +
358 					      adapter->num_io_queues);
359 }
360 
361 /* ena_setup_rx_resources - allocate I/O Rx resources (Descriptors)
362  * @adapter: network interface device structure
363  * @qid: queue index
364  *
365  * Returns 0 on success, negative on failure
366  */
367 static int ena_setup_rx_resources(struct ena_adapter *adapter,
368 				  u32 qid)
369 {
370 	struct ena_ring *rx_ring = &adapter->rx_ring[qid];
371 	struct ena_irq *ena_irq = &adapter->irq_tbl[ENA_IO_IRQ_IDX(qid)];
372 	int size, node, i;
373 
374 	if (rx_ring->rx_buffer_info) {
375 		netif_err(adapter, ifup, adapter->netdev,
376 			  "rx_buffer_info is not NULL");
377 		return -EEXIST;
378 	}
379 
380 	/* alloc extra element so in rx path
381 	 * we can always prefetch rx_info + 1
382 	 */
383 	size = sizeof(struct ena_rx_buffer) * (rx_ring->ring_size + 1);
384 	node = cpu_to_node(ena_irq->cpu);
385 
386 	rx_ring->rx_buffer_info = vzalloc_node(size, node);
387 	if (!rx_ring->rx_buffer_info) {
388 		rx_ring->rx_buffer_info = vzalloc(size);
389 		if (!rx_ring->rx_buffer_info)
390 			return -ENOMEM;
391 	}
392 
393 	size = sizeof(u16) * rx_ring->ring_size;
394 	rx_ring->free_ids = vzalloc_node(size, node);
395 	if (!rx_ring->free_ids) {
396 		rx_ring->free_ids = vzalloc(size);
397 		if (!rx_ring->free_ids) {
398 			vfree(rx_ring->rx_buffer_info);
399 			rx_ring->rx_buffer_info = NULL;
400 			return -ENOMEM;
401 		}
402 	}
403 
404 	/* Req id ring for receiving RX pkts out of order */
405 	for (i = 0; i < rx_ring->ring_size; i++)
406 		rx_ring->free_ids[i] = i;
407 
408 	/* Reset rx statistics */
409 	memset(&rx_ring->rx_stats, 0x0, sizeof(rx_ring->rx_stats));
410 
411 	rx_ring->next_to_clean = 0;
412 	rx_ring->next_to_use = 0;
413 	rx_ring->cpu = ena_irq->cpu;
414 	rx_ring->numa_node = node;
415 
416 	return 0;
417 }
418 
419 /* ena_free_rx_resources - Free I/O Rx Resources
420  * @adapter: network interface device structure
421  * @qid: queue index
422  *
423  * Free all receive software resources
424  */
425 static void ena_free_rx_resources(struct ena_adapter *adapter,
426 				  u32 qid)
427 {
428 	struct ena_ring *rx_ring = &adapter->rx_ring[qid];
429 
430 	vfree(rx_ring->rx_buffer_info);
431 	rx_ring->rx_buffer_info = NULL;
432 
433 	vfree(rx_ring->free_ids);
434 	rx_ring->free_ids = NULL;
435 }
436 
437 /* ena_setup_all_rx_resources - allocate I/O Rx queues resources for all queues
438  * @adapter: board private structure
439  *
440  * Return 0 on success, negative on failure
441  */
442 static int ena_setup_all_rx_resources(struct ena_adapter *adapter)
443 {
444 	int i, rc = 0;
445 
446 	for (i = 0; i < adapter->num_io_queues; i++) {
447 		rc = ena_setup_rx_resources(adapter, i);
448 		if (rc)
449 			goto err_setup_rx;
450 	}
451 
452 	return 0;
453 
454 err_setup_rx:
455 
456 	netif_err(adapter, ifup, adapter->netdev,
457 		  "Rx queue %d: allocation failed\n", i);
458 
459 	/* rewind the index freeing the rings as we go */
460 	while (i--)
461 		ena_free_rx_resources(adapter, i);
462 	return rc;
463 }
464 
465 /* ena_free_all_io_rx_resources - Free I/O Rx Resources for All Queues
466  * @adapter: board private structure
467  *
468  * Free all receive software resources
469  */
470 static void ena_free_all_io_rx_resources(struct ena_adapter *adapter)
471 {
472 	int i;
473 
474 	for (i = 0; i < adapter->num_io_queues; i++)
475 		ena_free_rx_resources(adapter, i);
476 }
477 
478 static struct page *ena_alloc_map_page(struct ena_ring *rx_ring,
479 				       dma_addr_t *dma)
480 {
481 	struct page *page;
482 
483 	/* This would allocate the page on the same NUMA node the executing code
484 	 * is running on.
485 	 */
486 	page = dev_alloc_page();
487 	if (!page) {
488 		ena_increase_stat(&rx_ring->rx_stats.page_alloc_fail, 1,
489 				  &rx_ring->syncp);
490 		return ERR_PTR(-ENOSPC);
491 	}
492 
493 	/* To enable NIC-side port-mirroring, AKA SPAN port,
494 	 * we make the buffer readable from the nic as well
495 	 */
496 	*dma = dma_map_page(rx_ring->dev, page, 0, ENA_PAGE_SIZE,
497 			    DMA_BIDIRECTIONAL);
498 	if (unlikely(dma_mapping_error(rx_ring->dev, *dma))) {
499 		ena_increase_stat(&rx_ring->rx_stats.dma_mapping_err, 1,
500 				  &rx_ring->syncp);
501 		__free_page(page);
502 		return ERR_PTR(-EIO);
503 	}
504 
505 	return page;
506 }
507 
508 static int ena_alloc_rx_buffer(struct ena_ring *rx_ring,
509 			       struct ena_rx_buffer *rx_info)
510 {
511 	int headroom = rx_ring->rx_headroom;
512 	struct ena_com_buf *ena_buf;
513 	struct page *page;
514 	dma_addr_t dma;
515 	int tailroom;
516 
517 	/* restore page offset value in case it has been changed by device */
518 	rx_info->buf_offset = headroom;
519 
520 	/* if previous allocated page is not used */
521 	if (unlikely(rx_info->page))
522 		return 0;
523 
524 	/* We handle DMA here */
525 	page = ena_alloc_map_page(rx_ring, &dma);
526 	if (unlikely(IS_ERR(page)))
527 		return PTR_ERR(page);
528 
529 	netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev,
530 		  "Allocate page %p, rx_info %p\n", page, rx_info);
531 
532 	tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
533 
534 	rx_info->page = page;
535 	rx_info->dma_addr = dma;
536 	rx_info->page_offset = 0;
537 	ena_buf = &rx_info->ena_buf;
538 	ena_buf->paddr = dma + headroom;
539 	ena_buf->len = ENA_PAGE_SIZE - headroom - tailroom;
540 
541 	return 0;
542 }
543 
544 static void ena_unmap_rx_buff_attrs(struct ena_ring *rx_ring,
545 				    struct ena_rx_buffer *rx_info,
546 				    unsigned long attrs)
547 {
548 	dma_unmap_page_attrs(rx_ring->dev, rx_info->dma_addr, ENA_PAGE_SIZE,
549 			     DMA_BIDIRECTIONAL, attrs);
550 }
551 
552 static void ena_free_rx_page(struct ena_ring *rx_ring,
553 			     struct ena_rx_buffer *rx_info)
554 {
555 	struct page *page = rx_info->page;
556 
557 	if (unlikely(!page)) {
558 		netif_warn(rx_ring->adapter, rx_err, rx_ring->netdev,
559 			   "Trying to free unallocated buffer\n");
560 		return;
561 	}
562 
563 	ena_unmap_rx_buff_attrs(rx_ring, rx_info, 0);
564 
565 	__free_page(page);
566 	rx_info->page = NULL;
567 }
568 
569 static int ena_refill_rx_bufs(struct ena_ring *rx_ring, u32 num)
570 {
571 	u16 next_to_use, req_id;
572 	u32 i;
573 	int rc;
574 
575 	next_to_use = rx_ring->next_to_use;
576 
577 	for (i = 0; i < num; i++) {
578 		struct ena_rx_buffer *rx_info;
579 
580 		req_id = rx_ring->free_ids[next_to_use];
581 
582 		rx_info = &rx_ring->rx_buffer_info[req_id];
583 
584 		rc = ena_alloc_rx_buffer(rx_ring, rx_info);
585 		if (unlikely(rc < 0)) {
586 			netif_warn(rx_ring->adapter, rx_err, rx_ring->netdev,
587 				   "Failed to allocate buffer for rx queue %d\n",
588 				   rx_ring->qid);
589 			break;
590 		}
591 		rc = ena_com_add_single_rx_desc(rx_ring->ena_com_io_sq,
592 						&rx_info->ena_buf,
593 						req_id);
594 		if (unlikely(rc)) {
595 			netif_warn(rx_ring->adapter, rx_status, rx_ring->netdev,
596 				   "Failed to add buffer for rx queue %d\n",
597 				   rx_ring->qid);
598 			break;
599 		}
600 		next_to_use = ENA_RX_RING_IDX_NEXT(next_to_use,
601 						   rx_ring->ring_size);
602 	}
603 
604 	if (unlikely(i < num)) {
605 		ena_increase_stat(&rx_ring->rx_stats.refil_partial, 1,
606 				  &rx_ring->syncp);
607 		netif_warn(rx_ring->adapter, rx_err, rx_ring->netdev,
608 			   "Refilled rx qid %d with only %d buffers (from %d)\n",
609 			   rx_ring->qid, i, num);
610 	}
611 
612 	/* ena_com_write_sq_doorbell issues a wmb() */
613 	if (likely(i))
614 		ena_com_write_sq_doorbell(rx_ring->ena_com_io_sq);
615 
616 	rx_ring->next_to_use = next_to_use;
617 
618 	return i;
619 }
620 
621 static void ena_free_rx_bufs(struct ena_adapter *adapter,
622 			     u32 qid)
623 {
624 	struct ena_ring *rx_ring = &adapter->rx_ring[qid];
625 	u32 i;
626 
627 	for (i = 0; i < rx_ring->ring_size; i++) {
628 		struct ena_rx_buffer *rx_info = &rx_ring->rx_buffer_info[i];
629 
630 		if (rx_info->page)
631 			ena_free_rx_page(rx_ring, rx_info);
632 	}
633 }
634 
635 /* ena_refill_all_rx_bufs - allocate all queues Rx buffers
636  * @adapter: board private structure
637  */
638 static void ena_refill_all_rx_bufs(struct ena_adapter *adapter)
639 {
640 	struct ena_ring *rx_ring;
641 	int i, rc, bufs_num;
642 
643 	for (i = 0; i < adapter->num_io_queues; i++) {
644 		rx_ring = &adapter->rx_ring[i];
645 		bufs_num = rx_ring->ring_size - 1;
646 		rc = ena_refill_rx_bufs(rx_ring, bufs_num);
647 
648 		if (unlikely(rc != bufs_num))
649 			netif_warn(rx_ring->adapter, rx_status, rx_ring->netdev,
650 				   "Refilling Queue %d failed. allocated %d buffers from: %d\n",
651 				   i, rc, bufs_num);
652 	}
653 }
654 
655 static void ena_free_all_rx_bufs(struct ena_adapter *adapter)
656 {
657 	int i;
658 
659 	for (i = 0; i < adapter->num_io_queues; i++)
660 		ena_free_rx_bufs(adapter, i);
661 }
662 
663 void ena_unmap_tx_buff(struct ena_ring *tx_ring,
664 		       struct ena_tx_buffer *tx_info)
665 {
666 	struct ena_com_buf *ena_buf;
667 	u32 cnt;
668 	int i;
669 
670 	ena_buf = tx_info->bufs;
671 	cnt = tx_info->num_of_bufs;
672 
673 	if (unlikely(!cnt))
674 		return;
675 
676 	if (tx_info->map_linear_data) {
677 		dma_unmap_single(tx_ring->dev,
678 				 dma_unmap_addr(ena_buf, paddr),
679 				 dma_unmap_len(ena_buf, len),
680 				 DMA_TO_DEVICE);
681 		ena_buf++;
682 		cnt--;
683 	}
684 
685 	/* unmap remaining mapped pages */
686 	for (i = 0; i < cnt; i++) {
687 		dma_unmap_page(tx_ring->dev, dma_unmap_addr(ena_buf, paddr),
688 			       dma_unmap_len(ena_buf, len), DMA_TO_DEVICE);
689 		ena_buf++;
690 	}
691 }
692 
693 /* ena_free_tx_bufs - Free Tx Buffers per Queue
694  * @tx_ring: TX ring for which buffers be freed
695  */
696 static void ena_free_tx_bufs(struct ena_ring *tx_ring)
697 {
698 	bool print_once = true;
699 	u32 i;
700 
701 	for (i = 0; i < tx_ring->ring_size; i++) {
702 		struct ena_tx_buffer *tx_info = &tx_ring->tx_buffer_info[i];
703 
704 		if (!tx_info->skb)
705 			continue;
706 
707 		if (print_once) {
708 			netif_notice(tx_ring->adapter, ifdown, tx_ring->netdev,
709 				     "Free uncompleted tx skb qid %d idx 0x%x\n",
710 				     tx_ring->qid, i);
711 			print_once = false;
712 		} else {
713 			netif_dbg(tx_ring->adapter, ifdown, tx_ring->netdev,
714 				  "Free uncompleted tx skb qid %d idx 0x%x\n",
715 				  tx_ring->qid, i);
716 		}
717 
718 		ena_unmap_tx_buff(tx_ring, tx_info);
719 
720 		dev_kfree_skb_any(tx_info->skb);
721 	}
722 	netdev_tx_reset_queue(netdev_get_tx_queue(tx_ring->netdev,
723 						  tx_ring->qid));
724 }
725 
726 static void ena_free_all_tx_bufs(struct ena_adapter *adapter)
727 {
728 	struct ena_ring *tx_ring;
729 	int i;
730 
731 	for (i = 0; i < adapter->num_io_queues + adapter->xdp_num_queues; i++) {
732 		tx_ring = &adapter->tx_ring[i];
733 		ena_free_tx_bufs(tx_ring);
734 	}
735 }
736 
737 static void ena_destroy_all_tx_queues(struct ena_adapter *adapter)
738 {
739 	u16 ena_qid;
740 	int i;
741 
742 	for (i = 0; i < adapter->num_io_queues + adapter->xdp_num_queues; i++) {
743 		ena_qid = ENA_IO_TXQ_IDX(i);
744 		ena_com_destroy_io_queue(adapter->ena_dev, ena_qid);
745 	}
746 }
747 
748 static void ena_destroy_all_rx_queues(struct ena_adapter *adapter)
749 {
750 	u16 ena_qid;
751 	int i;
752 
753 	for (i = 0; i < adapter->num_io_queues; i++) {
754 		ena_qid = ENA_IO_RXQ_IDX(i);
755 		cancel_work_sync(&adapter->ena_napi[i].dim.work);
756 		ena_xdp_unregister_rxq_info(&adapter->rx_ring[i]);
757 		ena_com_destroy_io_queue(adapter->ena_dev, ena_qid);
758 	}
759 }
760 
761 static void ena_destroy_all_io_queues(struct ena_adapter *adapter)
762 {
763 	ena_destroy_all_tx_queues(adapter);
764 	ena_destroy_all_rx_queues(adapter);
765 }
766 
767 int handle_invalid_req_id(struct ena_ring *ring, u16 req_id,
768 			  struct ena_tx_buffer *tx_info, bool is_xdp)
769 {
770 	if (tx_info)
771 		netif_err(ring->adapter,
772 			  tx_done,
773 			  ring->netdev,
774 			  "tx_info doesn't have valid %s. qid %u req_id %u",
775 			   is_xdp ? "xdp frame" : "skb", ring->qid, req_id);
776 	else
777 		netif_err(ring->adapter,
778 			  tx_done,
779 			  ring->netdev,
780 			  "Invalid req_id %u in qid %u\n",
781 			  req_id, ring->qid);
782 
783 	ena_increase_stat(&ring->tx_stats.bad_req_id, 1, &ring->syncp);
784 	ena_reset_device(ring->adapter, ENA_REGS_RESET_INV_TX_REQ_ID);
785 
786 	return -EFAULT;
787 }
788 
789 static int validate_tx_req_id(struct ena_ring *tx_ring, u16 req_id)
790 {
791 	struct ena_tx_buffer *tx_info;
792 
793 	tx_info = &tx_ring->tx_buffer_info[req_id];
794 	if (likely(tx_info->skb))
795 		return 0;
796 
797 	return handle_invalid_req_id(tx_ring, req_id, tx_info, false);
798 }
799 
800 static int ena_clean_tx_irq(struct ena_ring *tx_ring, u32 budget)
801 {
802 	struct netdev_queue *txq;
803 	bool above_thresh;
804 	u32 tx_bytes = 0;
805 	u32 total_done = 0;
806 	u16 next_to_clean;
807 	u16 req_id;
808 	int tx_pkts = 0;
809 	int rc;
810 
811 	next_to_clean = tx_ring->next_to_clean;
812 	txq = netdev_get_tx_queue(tx_ring->netdev, tx_ring->qid);
813 
814 	while (tx_pkts < budget) {
815 		struct ena_tx_buffer *tx_info;
816 		struct sk_buff *skb;
817 
818 		rc = ena_com_tx_comp_req_id_get(tx_ring->ena_com_io_cq,
819 						&req_id);
820 		if (rc) {
821 			if (unlikely(rc == -EINVAL))
822 				handle_invalid_req_id(tx_ring, req_id, NULL,
823 						      false);
824 			break;
825 		}
826 
827 		/* validate that the request id points to a valid skb */
828 		rc = validate_tx_req_id(tx_ring, req_id);
829 		if (rc)
830 			break;
831 
832 		tx_info = &tx_ring->tx_buffer_info[req_id];
833 		skb = tx_info->skb;
834 
835 		/* prefetch skb_end_pointer() to speedup skb_shinfo(skb) */
836 		prefetch(&skb->end);
837 
838 		tx_info->skb = NULL;
839 		tx_info->last_jiffies = 0;
840 
841 		ena_unmap_tx_buff(tx_ring, tx_info);
842 
843 		netif_dbg(tx_ring->adapter, tx_done, tx_ring->netdev,
844 			  "tx_poll: q %d skb %p completed\n", tx_ring->qid,
845 			  skb);
846 
847 		tx_bytes += tx_info->total_tx_size;
848 		dev_kfree_skb(skb);
849 		tx_pkts++;
850 		total_done += tx_info->tx_descs;
851 
852 		tx_ring->free_ids[next_to_clean] = req_id;
853 		next_to_clean = ENA_TX_RING_IDX_NEXT(next_to_clean,
854 						     tx_ring->ring_size);
855 	}
856 
857 	tx_ring->next_to_clean = next_to_clean;
858 	ena_com_comp_ack(tx_ring->ena_com_io_sq, total_done);
859 	ena_com_update_dev_comp_head(tx_ring->ena_com_io_cq);
860 
861 	netdev_tx_completed_queue(txq, tx_pkts, tx_bytes);
862 
863 	netif_dbg(tx_ring->adapter, tx_done, tx_ring->netdev,
864 		  "tx_poll: q %d done. total pkts: %d\n",
865 		  tx_ring->qid, tx_pkts);
866 
867 	/* need to make the rings circular update visible to
868 	 * ena_start_xmit() before checking for netif_queue_stopped().
869 	 */
870 	smp_mb();
871 
872 	above_thresh = ena_com_sq_have_enough_space(tx_ring->ena_com_io_sq,
873 						    ENA_TX_WAKEUP_THRESH);
874 	if (unlikely(netif_tx_queue_stopped(txq) && above_thresh)) {
875 		__netif_tx_lock(txq, smp_processor_id());
876 		above_thresh =
877 			ena_com_sq_have_enough_space(tx_ring->ena_com_io_sq,
878 						     ENA_TX_WAKEUP_THRESH);
879 		if (netif_tx_queue_stopped(txq) && above_thresh &&
880 		    test_bit(ENA_FLAG_DEV_UP, &tx_ring->adapter->flags)) {
881 			netif_tx_wake_queue(txq);
882 			ena_increase_stat(&tx_ring->tx_stats.queue_wakeup, 1,
883 					  &tx_ring->syncp);
884 		}
885 		__netif_tx_unlock(txq);
886 	}
887 
888 	return tx_pkts;
889 }
890 
891 static struct sk_buff *ena_alloc_skb(struct ena_ring *rx_ring, void *first_frag, u16 len)
892 {
893 	struct sk_buff *skb;
894 
895 	if (!first_frag)
896 		skb = napi_alloc_skb(rx_ring->napi, len);
897 	else
898 		skb = napi_build_skb(first_frag, len);
899 
900 	if (unlikely(!skb)) {
901 		ena_increase_stat(&rx_ring->rx_stats.skb_alloc_fail, 1,
902 				  &rx_ring->syncp);
903 
904 		netif_dbg(rx_ring->adapter, rx_err, rx_ring->netdev,
905 			  "Failed to allocate skb. first_frag %s\n",
906 			  first_frag ? "provided" : "not provided");
907 	}
908 
909 	return skb;
910 }
911 
912 static bool ena_try_rx_buf_page_reuse(struct ena_rx_buffer *rx_info, u16 buf_len,
913 				      u16 len, int pkt_offset)
914 {
915 	struct ena_com_buf *ena_buf = &rx_info->ena_buf;
916 
917 	/* More than ENA_MIN_RX_BUF_SIZE left in the reused buffer
918 	 * for data + headroom + tailroom.
919 	 */
920 	if (SKB_DATA_ALIGN(len + pkt_offset) + ENA_MIN_RX_BUF_SIZE <= ena_buf->len) {
921 		page_ref_inc(rx_info->page);
922 		rx_info->page_offset += buf_len;
923 		ena_buf->paddr += buf_len;
924 		ena_buf->len -= buf_len;
925 		return true;
926 	}
927 
928 	return false;
929 }
930 
931 static struct sk_buff *ena_rx_skb(struct ena_ring *rx_ring,
932 				  struct ena_com_rx_buf_info *ena_bufs,
933 				  u32 descs,
934 				  u16 *next_to_clean)
935 {
936 	int tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
937 	bool is_xdp_loaded = ena_xdp_present_ring(rx_ring);
938 	struct ena_rx_buffer *rx_info;
939 	struct ena_adapter *adapter;
940 	int page_offset, pkt_offset;
941 	dma_addr_t pre_reuse_paddr;
942 	u16 len, req_id, buf = 0;
943 	bool reuse_rx_buf_page;
944 	struct sk_buff *skb;
945 	void *buf_addr;
946 	int buf_offset;
947 	u16 buf_len;
948 
949 	len = ena_bufs[buf].len;
950 	req_id = ena_bufs[buf].req_id;
951 
952 	rx_info = &rx_ring->rx_buffer_info[req_id];
953 
954 	if (unlikely(!rx_info->page)) {
955 		adapter = rx_ring->adapter;
956 		netif_err(adapter, rx_err, rx_ring->netdev,
957 			  "Page is NULL. qid %u req_id %u\n", rx_ring->qid, req_id);
958 		ena_increase_stat(&rx_ring->rx_stats.bad_req_id, 1, &rx_ring->syncp);
959 		ena_reset_device(adapter, ENA_REGS_RESET_INV_RX_REQ_ID);
960 		return NULL;
961 	}
962 
963 	netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev,
964 		  "rx_info %p page %p\n",
965 		  rx_info, rx_info->page);
966 
967 	buf_offset = rx_info->buf_offset;
968 	pkt_offset = buf_offset - rx_ring->rx_headroom;
969 	page_offset = rx_info->page_offset;
970 	buf_addr = page_address(rx_info->page) + page_offset;
971 
972 	if (len <= rx_ring->rx_copybreak) {
973 		skb = ena_alloc_skb(rx_ring, NULL, len);
974 		if (unlikely(!skb))
975 			return NULL;
976 
977 		skb_copy_to_linear_data(skb, buf_addr + buf_offset, len);
978 		dma_sync_single_for_device(rx_ring->dev,
979 					   dma_unmap_addr(&rx_info->ena_buf, paddr) + pkt_offset,
980 					   len,
981 					   DMA_FROM_DEVICE);
982 
983 		skb_put(skb, len);
984 		netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev,
985 			  "RX allocated small packet. len %d.\n", skb->len);
986 		skb->protocol = eth_type_trans(skb, rx_ring->netdev);
987 		rx_ring->free_ids[*next_to_clean] = req_id;
988 		*next_to_clean = ENA_RX_RING_IDX_ADD(*next_to_clean, descs,
989 						     rx_ring->ring_size);
990 		return skb;
991 	}
992 
993 	buf_len = SKB_DATA_ALIGN(len + buf_offset + tailroom);
994 
995 	/* If XDP isn't loaded try to reuse part of the RX buffer */
996 	reuse_rx_buf_page = !is_xdp_loaded &&
997 			    ena_try_rx_buf_page_reuse(rx_info, buf_len, len, pkt_offset);
998 
999 	if (!reuse_rx_buf_page)
1000 		ena_unmap_rx_buff_attrs(rx_ring, rx_info, DMA_ATTR_SKIP_CPU_SYNC);
1001 
1002 	skb = ena_alloc_skb(rx_ring, buf_addr, buf_len);
1003 	if (unlikely(!skb))
1004 		return NULL;
1005 
1006 	/* Populate skb's linear part */
1007 	skb_reserve(skb, buf_offset);
1008 	skb_put(skb, len);
1009 	skb->protocol = eth_type_trans(skb, rx_ring->netdev);
1010 
1011 	do {
1012 		netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev,
1013 			  "RX skb updated. len %d. data_len %d\n",
1014 			  skb->len, skb->data_len);
1015 
1016 		if (!reuse_rx_buf_page)
1017 			rx_info->page = NULL;
1018 
1019 		rx_ring->free_ids[*next_to_clean] = req_id;
1020 		*next_to_clean =
1021 			ENA_RX_RING_IDX_NEXT(*next_to_clean,
1022 					     rx_ring->ring_size);
1023 		if (likely(--descs == 0))
1024 			break;
1025 
1026 		buf++;
1027 		len = ena_bufs[buf].len;
1028 		req_id = ena_bufs[buf].req_id;
1029 
1030 		rx_info = &rx_ring->rx_buffer_info[req_id];
1031 
1032 		/* rx_info->buf_offset includes rx_ring->rx_headroom */
1033 		buf_offset = rx_info->buf_offset;
1034 		pkt_offset = buf_offset - rx_ring->rx_headroom;
1035 		buf_len = SKB_DATA_ALIGN(len + buf_offset + tailroom);
1036 		page_offset = rx_info->page_offset;
1037 
1038 		pre_reuse_paddr = dma_unmap_addr(&rx_info->ena_buf, paddr);
1039 
1040 		reuse_rx_buf_page = !is_xdp_loaded &&
1041 				    ena_try_rx_buf_page_reuse(rx_info, buf_len, len, pkt_offset);
1042 
1043 		dma_sync_single_for_cpu(rx_ring->dev,
1044 					pre_reuse_paddr + pkt_offset,
1045 					len,
1046 					DMA_FROM_DEVICE);
1047 
1048 		if (!reuse_rx_buf_page)
1049 			ena_unmap_rx_buff_attrs(rx_ring, rx_info,
1050 						DMA_ATTR_SKIP_CPU_SYNC);
1051 
1052 		skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_info->page,
1053 				page_offset + buf_offset, len, buf_len);
1054 
1055 	} while (1);
1056 
1057 	return skb;
1058 }
1059 
1060 /* ena_rx_checksum - indicate in skb if hw indicated a good cksum
1061  * @adapter: structure containing adapter specific data
1062  * @ena_rx_ctx: received packet context/metadata
1063  * @skb: skb currently being received and modified
1064  */
1065 static void ena_rx_checksum(struct ena_ring *rx_ring,
1066 				   struct ena_com_rx_ctx *ena_rx_ctx,
1067 				   struct sk_buff *skb)
1068 {
1069 	/* Rx csum disabled */
1070 	if (unlikely(!(rx_ring->netdev->features & NETIF_F_RXCSUM))) {
1071 		skb->ip_summed = CHECKSUM_NONE;
1072 		return;
1073 	}
1074 
1075 	/* For fragmented packets the checksum isn't valid */
1076 	if (ena_rx_ctx->frag) {
1077 		skb->ip_summed = CHECKSUM_NONE;
1078 		return;
1079 	}
1080 
1081 	/* if IP and error */
1082 	if (unlikely((ena_rx_ctx->l3_proto == ENA_ETH_IO_L3_PROTO_IPV4) &&
1083 		     (ena_rx_ctx->l3_csum_err))) {
1084 		/* ipv4 checksum error */
1085 		skb->ip_summed = CHECKSUM_NONE;
1086 		ena_increase_stat(&rx_ring->rx_stats.csum_bad, 1,
1087 				  &rx_ring->syncp);
1088 		netif_dbg(rx_ring->adapter, rx_err, rx_ring->netdev,
1089 			  "RX IPv4 header checksum error\n");
1090 		return;
1091 	}
1092 
1093 	/* if TCP/UDP */
1094 	if (likely((ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_TCP) ||
1095 		   (ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_UDP))) {
1096 		if (unlikely(ena_rx_ctx->l4_csum_err)) {
1097 			/* TCP/UDP checksum error */
1098 			ena_increase_stat(&rx_ring->rx_stats.csum_bad, 1,
1099 					  &rx_ring->syncp);
1100 			netif_dbg(rx_ring->adapter, rx_err, rx_ring->netdev,
1101 				  "RX L4 checksum error\n");
1102 			skb->ip_summed = CHECKSUM_NONE;
1103 			return;
1104 		}
1105 
1106 		if (likely(ena_rx_ctx->l4_csum_checked)) {
1107 			skb->ip_summed = CHECKSUM_UNNECESSARY;
1108 			ena_increase_stat(&rx_ring->rx_stats.csum_good, 1,
1109 					  &rx_ring->syncp);
1110 		} else {
1111 			ena_increase_stat(&rx_ring->rx_stats.csum_unchecked, 1,
1112 					  &rx_ring->syncp);
1113 			skb->ip_summed = CHECKSUM_NONE;
1114 		}
1115 	} else {
1116 		skb->ip_summed = CHECKSUM_NONE;
1117 		return;
1118 	}
1119 
1120 }
1121 
1122 static void ena_set_rx_hash(struct ena_ring *rx_ring,
1123 			    struct ena_com_rx_ctx *ena_rx_ctx,
1124 			    struct sk_buff *skb)
1125 {
1126 	enum pkt_hash_types hash_type;
1127 
1128 	if (likely(rx_ring->netdev->features & NETIF_F_RXHASH)) {
1129 		if (likely((ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_TCP) ||
1130 			   (ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_UDP)))
1131 
1132 			hash_type = PKT_HASH_TYPE_L4;
1133 		else
1134 			hash_type = PKT_HASH_TYPE_NONE;
1135 
1136 		/* Override hash type if the packet is fragmented */
1137 		if (ena_rx_ctx->frag)
1138 			hash_type = PKT_HASH_TYPE_NONE;
1139 
1140 		skb_set_hash(skb, ena_rx_ctx->hash, hash_type);
1141 	}
1142 }
1143 
1144 static int ena_xdp_handle_buff(struct ena_ring *rx_ring, struct xdp_buff *xdp, u16 num_descs)
1145 {
1146 	struct ena_rx_buffer *rx_info;
1147 	int ret;
1148 
1149 	/* XDP multi-buffer packets not supported */
1150 	if (unlikely(num_descs > 1)) {
1151 		netdev_err_once(rx_ring->adapter->netdev,
1152 				"xdp: dropped unsupported multi-buffer packets\n");
1153 		ena_increase_stat(&rx_ring->rx_stats.xdp_drop, 1, &rx_ring->syncp);
1154 		return ENA_XDP_DROP;
1155 	}
1156 
1157 	rx_info = &rx_ring->rx_buffer_info[rx_ring->ena_bufs[0].req_id];
1158 	xdp_prepare_buff(xdp, page_address(rx_info->page),
1159 			 rx_info->buf_offset,
1160 			 rx_ring->ena_bufs[0].len, false);
1161 
1162 	ret = ena_xdp_execute(rx_ring, xdp);
1163 
1164 	/* The xdp program might expand the headers */
1165 	if (ret == ENA_XDP_PASS) {
1166 		rx_info->buf_offset = xdp->data - xdp->data_hard_start;
1167 		rx_ring->ena_bufs[0].len = xdp->data_end - xdp->data;
1168 	}
1169 
1170 	return ret;
1171 }
1172 
1173 /* ena_clean_rx_irq - Cleanup RX irq
1174  * @rx_ring: RX ring to clean
1175  * @napi: napi handler
1176  * @budget: how many packets driver is allowed to clean
1177  *
1178  * Returns the number of cleaned buffers.
1179  */
1180 static int ena_clean_rx_irq(struct ena_ring *rx_ring, struct napi_struct *napi,
1181 			    u32 budget)
1182 {
1183 	u16 next_to_clean = rx_ring->next_to_clean;
1184 	struct ena_com_rx_ctx ena_rx_ctx;
1185 	struct ena_rx_buffer *rx_info;
1186 	struct ena_adapter *adapter;
1187 	u32 res_budget, work_done;
1188 	int rx_copybreak_pkt = 0;
1189 	int refill_threshold;
1190 	struct sk_buff *skb;
1191 	int refill_required;
1192 	struct xdp_buff xdp;
1193 	int xdp_flags = 0;
1194 	int total_len = 0;
1195 	int xdp_verdict;
1196 	u8 pkt_offset;
1197 	int rc = 0;
1198 	int i;
1199 
1200 	netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev,
1201 		  "%s qid %d\n", __func__, rx_ring->qid);
1202 	res_budget = budget;
1203 	xdp_init_buff(&xdp, ENA_PAGE_SIZE, &rx_ring->xdp_rxq);
1204 
1205 	do {
1206 		xdp_verdict = ENA_XDP_PASS;
1207 		skb = NULL;
1208 		ena_rx_ctx.ena_bufs = rx_ring->ena_bufs;
1209 		ena_rx_ctx.max_bufs = rx_ring->sgl_size;
1210 		ena_rx_ctx.descs = 0;
1211 		ena_rx_ctx.pkt_offset = 0;
1212 		rc = ena_com_rx_pkt(rx_ring->ena_com_io_cq,
1213 				    rx_ring->ena_com_io_sq,
1214 				    &ena_rx_ctx);
1215 		if (unlikely(rc))
1216 			goto error;
1217 
1218 		if (unlikely(ena_rx_ctx.descs == 0))
1219 			break;
1220 
1221 		/* First descriptor might have an offset set by the device */
1222 		rx_info = &rx_ring->rx_buffer_info[rx_ring->ena_bufs[0].req_id];
1223 		pkt_offset = ena_rx_ctx.pkt_offset;
1224 		rx_info->buf_offset += pkt_offset;
1225 
1226 		netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev,
1227 			  "rx_poll: q %d got packet from ena. descs #: %d l3 proto %d l4 proto %d hash: %x\n",
1228 			  rx_ring->qid, ena_rx_ctx.descs, ena_rx_ctx.l3_proto,
1229 			  ena_rx_ctx.l4_proto, ena_rx_ctx.hash);
1230 
1231 		dma_sync_single_for_cpu(rx_ring->dev,
1232 					dma_unmap_addr(&rx_info->ena_buf, paddr) + pkt_offset,
1233 					rx_ring->ena_bufs[0].len,
1234 					DMA_FROM_DEVICE);
1235 
1236 		if (ena_xdp_present_ring(rx_ring))
1237 			xdp_verdict = ena_xdp_handle_buff(rx_ring, &xdp, ena_rx_ctx.descs);
1238 
1239 		/* allocate skb and fill it */
1240 		if (xdp_verdict == ENA_XDP_PASS)
1241 			skb = ena_rx_skb(rx_ring,
1242 					 rx_ring->ena_bufs,
1243 					 ena_rx_ctx.descs,
1244 					 &next_to_clean);
1245 
1246 		if (unlikely(!skb)) {
1247 			for (i = 0; i < ena_rx_ctx.descs; i++) {
1248 				int req_id = rx_ring->ena_bufs[i].req_id;
1249 
1250 				rx_ring->free_ids[next_to_clean] = req_id;
1251 				next_to_clean =
1252 					ENA_RX_RING_IDX_NEXT(next_to_clean,
1253 							     rx_ring->ring_size);
1254 
1255 				/* Packets was passed for transmission, unmap it
1256 				 * from RX side.
1257 				 */
1258 				if (xdp_verdict & ENA_XDP_FORWARDED) {
1259 					ena_unmap_rx_buff_attrs(rx_ring,
1260 								&rx_ring->rx_buffer_info[req_id],
1261 								DMA_ATTR_SKIP_CPU_SYNC);
1262 					rx_ring->rx_buffer_info[req_id].page = NULL;
1263 				}
1264 			}
1265 			if (xdp_verdict != ENA_XDP_PASS) {
1266 				xdp_flags |= xdp_verdict;
1267 				total_len += ena_rx_ctx.ena_bufs[0].len;
1268 				res_budget--;
1269 				continue;
1270 			}
1271 			break;
1272 		}
1273 
1274 		ena_rx_checksum(rx_ring, &ena_rx_ctx, skb);
1275 
1276 		ena_set_rx_hash(rx_ring, &ena_rx_ctx, skb);
1277 
1278 		skb_record_rx_queue(skb, rx_ring->qid);
1279 
1280 		if (rx_ring->ena_bufs[0].len <= rx_ring->rx_copybreak)
1281 			rx_copybreak_pkt++;
1282 
1283 		total_len += skb->len;
1284 
1285 		napi_gro_receive(napi, skb);
1286 
1287 		res_budget--;
1288 	} while (likely(res_budget));
1289 
1290 	work_done = budget - res_budget;
1291 	rx_ring->per_napi_packets += work_done;
1292 	u64_stats_update_begin(&rx_ring->syncp);
1293 	rx_ring->rx_stats.bytes += total_len;
1294 	rx_ring->rx_stats.cnt += work_done;
1295 	rx_ring->rx_stats.rx_copybreak_pkt += rx_copybreak_pkt;
1296 	u64_stats_update_end(&rx_ring->syncp);
1297 
1298 	rx_ring->next_to_clean = next_to_clean;
1299 
1300 	refill_required = ena_com_free_q_entries(rx_ring->ena_com_io_sq);
1301 	refill_threshold =
1302 		min_t(int, rx_ring->ring_size / ENA_RX_REFILL_THRESH_DIVIDER,
1303 		      ENA_RX_REFILL_THRESH_PACKET);
1304 
1305 	/* Optimization, try to batch new rx buffers */
1306 	if (refill_required > refill_threshold) {
1307 		ena_com_update_dev_comp_head(rx_ring->ena_com_io_cq);
1308 		ena_refill_rx_bufs(rx_ring, refill_required);
1309 	}
1310 
1311 	if (xdp_flags & ENA_XDP_REDIRECT)
1312 		xdp_do_flush();
1313 
1314 	return work_done;
1315 
1316 error:
1317 	if (xdp_flags & ENA_XDP_REDIRECT)
1318 		xdp_do_flush();
1319 
1320 	adapter = netdev_priv(rx_ring->netdev);
1321 
1322 	if (rc == -ENOSPC) {
1323 		ena_increase_stat(&rx_ring->rx_stats.bad_desc_num, 1,
1324 				  &rx_ring->syncp);
1325 		ena_reset_device(adapter, ENA_REGS_RESET_TOO_MANY_RX_DESCS);
1326 	} else {
1327 		ena_increase_stat(&rx_ring->rx_stats.bad_req_id, 1,
1328 				  &rx_ring->syncp);
1329 		ena_reset_device(adapter, ENA_REGS_RESET_INV_RX_REQ_ID);
1330 	}
1331 	return 0;
1332 }
1333 
1334 static void ena_dim_work(struct work_struct *w)
1335 {
1336 	struct dim *dim = container_of(w, struct dim, work);
1337 	struct dim_cq_moder cur_moder =
1338 		net_dim_get_rx_moderation(dim->mode, dim->profile_ix);
1339 	struct ena_napi *ena_napi = container_of(dim, struct ena_napi, dim);
1340 
1341 	ena_napi->rx_ring->smoothed_interval = cur_moder.usec;
1342 	dim->state = DIM_START_MEASURE;
1343 }
1344 
1345 static void ena_adjust_adaptive_rx_intr_moderation(struct ena_napi *ena_napi)
1346 {
1347 	struct dim_sample dim_sample;
1348 	struct ena_ring *rx_ring = ena_napi->rx_ring;
1349 
1350 	if (!rx_ring->per_napi_packets)
1351 		return;
1352 
1353 	rx_ring->non_empty_napi_events++;
1354 
1355 	dim_update_sample(rx_ring->non_empty_napi_events,
1356 			  rx_ring->rx_stats.cnt,
1357 			  rx_ring->rx_stats.bytes,
1358 			  &dim_sample);
1359 
1360 	net_dim(&ena_napi->dim, dim_sample);
1361 
1362 	rx_ring->per_napi_packets = 0;
1363 }
1364 
1365 void ena_unmask_interrupt(struct ena_ring *tx_ring,
1366 			  struct ena_ring *rx_ring)
1367 {
1368 	u32 rx_interval = tx_ring->smoothed_interval;
1369 	struct ena_eth_io_intr_reg intr_reg;
1370 
1371 	/* Rx ring can be NULL when for XDP tx queues which don't have an
1372 	 * accompanying rx_ring pair.
1373 	 */
1374 	if (rx_ring)
1375 		rx_interval = ena_com_get_adaptive_moderation_enabled(rx_ring->ena_dev) ?
1376 			rx_ring->smoothed_interval :
1377 			ena_com_get_nonadaptive_moderation_interval_rx(rx_ring->ena_dev);
1378 
1379 	/* Update intr register: rx intr delay,
1380 	 * tx intr delay and interrupt unmask
1381 	 */
1382 	ena_com_update_intr_reg(&intr_reg,
1383 				rx_interval,
1384 				tx_ring->smoothed_interval,
1385 				true);
1386 
1387 	ena_increase_stat(&tx_ring->tx_stats.unmask_interrupt, 1,
1388 			  &tx_ring->syncp);
1389 
1390 	/* It is a shared MSI-X.
1391 	 * Tx and Rx CQ have pointer to it.
1392 	 * So we use one of them to reach the intr reg
1393 	 * The Tx ring is used because the rx_ring is NULL for XDP queues
1394 	 */
1395 	ena_com_unmask_intr(tx_ring->ena_com_io_cq, &intr_reg);
1396 }
1397 
1398 void ena_update_ring_numa_node(struct ena_ring *tx_ring,
1399 			       struct ena_ring *rx_ring)
1400 {
1401 	int cpu = get_cpu();
1402 	int numa_node;
1403 
1404 	/* Check only one ring since the 2 rings are running on the same cpu */
1405 	if (likely(tx_ring->cpu == cpu))
1406 		goto out;
1407 
1408 	tx_ring->cpu = cpu;
1409 	if (rx_ring)
1410 		rx_ring->cpu = cpu;
1411 
1412 	numa_node = cpu_to_node(cpu);
1413 
1414 	if (likely(tx_ring->numa_node == numa_node))
1415 		goto out;
1416 
1417 	put_cpu();
1418 
1419 	if (numa_node != NUMA_NO_NODE) {
1420 		ena_com_update_numa_node(tx_ring->ena_com_io_cq, numa_node);
1421 		tx_ring->numa_node = numa_node;
1422 		if (rx_ring) {
1423 			rx_ring->numa_node = numa_node;
1424 			ena_com_update_numa_node(rx_ring->ena_com_io_cq,
1425 						 numa_node);
1426 		}
1427 	}
1428 
1429 	return;
1430 out:
1431 	put_cpu();
1432 }
1433 
1434 static int ena_io_poll(struct napi_struct *napi, int budget)
1435 {
1436 	struct ena_napi *ena_napi = container_of(napi, struct ena_napi, napi);
1437 	struct ena_ring *tx_ring, *rx_ring;
1438 	int tx_work_done;
1439 	int rx_work_done = 0;
1440 	int tx_budget;
1441 	int napi_comp_call = 0;
1442 	int ret;
1443 
1444 	tx_ring = ena_napi->tx_ring;
1445 	rx_ring = ena_napi->rx_ring;
1446 
1447 	tx_budget = tx_ring->ring_size / ENA_TX_POLL_BUDGET_DIVIDER;
1448 
1449 	if (!test_bit(ENA_FLAG_DEV_UP, &tx_ring->adapter->flags) ||
1450 	    test_bit(ENA_FLAG_TRIGGER_RESET, &tx_ring->adapter->flags)) {
1451 		napi_complete_done(napi, 0);
1452 		return 0;
1453 	}
1454 
1455 	tx_work_done = ena_clean_tx_irq(tx_ring, tx_budget);
1456 	/* On netpoll the budget is zero and the handler should only clean the
1457 	 * tx completions.
1458 	 */
1459 	if (likely(budget))
1460 		rx_work_done = ena_clean_rx_irq(rx_ring, napi, budget);
1461 
1462 	/* If the device is about to reset or down, avoid unmask
1463 	 * the interrupt and return 0 so NAPI won't reschedule
1464 	 */
1465 	if (unlikely(!test_bit(ENA_FLAG_DEV_UP, &tx_ring->adapter->flags) ||
1466 		     test_bit(ENA_FLAG_TRIGGER_RESET, &tx_ring->adapter->flags))) {
1467 		napi_complete_done(napi, 0);
1468 		ret = 0;
1469 
1470 	} else if ((budget > rx_work_done) && (tx_budget > tx_work_done)) {
1471 		napi_comp_call = 1;
1472 
1473 		/* Update numa and unmask the interrupt only when schedule
1474 		 * from the interrupt context (vs from sk_busy_loop)
1475 		 */
1476 		if (napi_complete_done(napi, rx_work_done) &&
1477 		    READ_ONCE(ena_napi->interrupts_masked)) {
1478 			smp_rmb(); /* make sure interrupts_masked is read */
1479 			WRITE_ONCE(ena_napi->interrupts_masked, false);
1480 			/* We apply adaptive moderation on Rx path only.
1481 			 * Tx uses static interrupt moderation.
1482 			 */
1483 			if (ena_com_get_adaptive_moderation_enabled(rx_ring->ena_dev))
1484 				ena_adjust_adaptive_rx_intr_moderation(ena_napi);
1485 
1486 			ena_update_ring_numa_node(tx_ring, rx_ring);
1487 			ena_unmask_interrupt(tx_ring, rx_ring);
1488 		}
1489 
1490 		ret = rx_work_done;
1491 	} else {
1492 		ret = budget;
1493 	}
1494 
1495 	u64_stats_update_begin(&tx_ring->syncp);
1496 	tx_ring->tx_stats.napi_comp += napi_comp_call;
1497 	tx_ring->tx_stats.tx_poll++;
1498 	u64_stats_update_end(&tx_ring->syncp);
1499 
1500 	tx_ring->tx_stats.last_napi_jiffies = jiffies;
1501 
1502 	return ret;
1503 }
1504 
1505 static irqreturn_t ena_intr_msix_mgmnt(int irq, void *data)
1506 {
1507 	struct ena_adapter *adapter = (struct ena_adapter *)data;
1508 
1509 	ena_com_admin_q_comp_intr_handler(adapter->ena_dev);
1510 
1511 	/* Don't call the aenq handler before probe is done */
1512 	if (likely(test_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags)))
1513 		ena_com_aenq_intr_handler(adapter->ena_dev, data);
1514 
1515 	return IRQ_HANDLED;
1516 }
1517 
1518 /* ena_intr_msix_io - MSI-X Interrupt Handler for Tx/Rx
1519  * @irq: interrupt number
1520  * @data: pointer to a network interface private napi device structure
1521  */
1522 static irqreturn_t ena_intr_msix_io(int irq, void *data)
1523 {
1524 	struct ena_napi *ena_napi = data;
1525 
1526 	/* Used to check HW health */
1527 	WRITE_ONCE(ena_napi->first_interrupt, true);
1528 
1529 	WRITE_ONCE(ena_napi->interrupts_masked, true);
1530 	smp_wmb(); /* write interrupts_masked before calling napi */
1531 
1532 	napi_schedule_irqoff(&ena_napi->napi);
1533 
1534 	return IRQ_HANDLED;
1535 }
1536 
1537 /* Reserve a single MSI-X vector for management (admin + aenq).
1538  * plus reserve one vector for each potential io queue.
1539  * the number of potential io queues is the minimum of what the device
1540  * supports and the number of vCPUs.
1541  */
1542 static int ena_enable_msix(struct ena_adapter *adapter)
1543 {
1544 	int msix_vecs, irq_cnt;
1545 
1546 	if (test_bit(ENA_FLAG_MSIX_ENABLED, &adapter->flags)) {
1547 		netif_err(adapter, probe, adapter->netdev,
1548 			  "Error, MSI-X is already enabled\n");
1549 		return -EPERM;
1550 	}
1551 
1552 	/* Reserved the max msix vectors we might need */
1553 	msix_vecs = ENA_MAX_MSIX_VEC(adapter->max_num_io_queues);
1554 	netif_dbg(adapter, probe, adapter->netdev,
1555 		  "Trying to enable MSI-X, vectors %d\n", msix_vecs);
1556 
1557 	irq_cnt = pci_alloc_irq_vectors(adapter->pdev, ENA_MIN_MSIX_VEC,
1558 					msix_vecs, PCI_IRQ_MSIX);
1559 
1560 	if (irq_cnt < 0) {
1561 		netif_err(adapter, probe, adapter->netdev,
1562 			  "Failed to enable MSI-X. irq_cnt %d\n", irq_cnt);
1563 		return -ENOSPC;
1564 	}
1565 
1566 	if (irq_cnt != msix_vecs) {
1567 		netif_notice(adapter, probe, adapter->netdev,
1568 			     "Enable only %d MSI-X (out of %d), reduce the number of queues\n",
1569 			     irq_cnt, msix_vecs);
1570 		adapter->num_io_queues = irq_cnt - ENA_ADMIN_MSIX_VEC;
1571 	}
1572 
1573 	if (ena_init_rx_cpu_rmap(adapter))
1574 		netif_warn(adapter, probe, adapter->netdev,
1575 			   "Failed to map IRQs to CPUs\n");
1576 
1577 	adapter->msix_vecs = irq_cnt;
1578 	set_bit(ENA_FLAG_MSIX_ENABLED, &adapter->flags);
1579 
1580 	return 0;
1581 }
1582 
1583 static void ena_setup_mgmnt_intr(struct ena_adapter *adapter)
1584 {
1585 	u32 cpu;
1586 
1587 	snprintf(adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].name,
1588 		 ENA_IRQNAME_SIZE, "ena-mgmnt@pci:%s",
1589 		 pci_name(adapter->pdev));
1590 	adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].handler =
1591 		ena_intr_msix_mgmnt;
1592 	adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].data = adapter;
1593 	adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].vector =
1594 		pci_irq_vector(adapter->pdev, ENA_MGMNT_IRQ_IDX);
1595 	cpu = cpumask_first(cpu_online_mask);
1596 	adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].cpu = cpu;
1597 	cpumask_set_cpu(cpu,
1598 			&adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].affinity_hint_mask);
1599 }
1600 
1601 static void ena_setup_io_intr(struct ena_adapter *adapter)
1602 {
1603 	struct net_device *netdev;
1604 	int irq_idx, i, cpu;
1605 	int io_queue_count;
1606 
1607 	netdev = adapter->netdev;
1608 	io_queue_count = adapter->num_io_queues + adapter->xdp_num_queues;
1609 
1610 	for (i = 0; i < io_queue_count; i++) {
1611 		irq_idx = ENA_IO_IRQ_IDX(i);
1612 		cpu = i % num_online_cpus();
1613 
1614 		snprintf(adapter->irq_tbl[irq_idx].name, ENA_IRQNAME_SIZE,
1615 			 "%s-Tx-Rx-%d", netdev->name, i);
1616 		adapter->irq_tbl[irq_idx].handler = ena_intr_msix_io;
1617 		adapter->irq_tbl[irq_idx].data = &adapter->ena_napi[i];
1618 		adapter->irq_tbl[irq_idx].vector =
1619 			pci_irq_vector(adapter->pdev, irq_idx);
1620 		adapter->irq_tbl[irq_idx].cpu = cpu;
1621 
1622 		cpumask_set_cpu(cpu,
1623 				&adapter->irq_tbl[irq_idx].affinity_hint_mask);
1624 	}
1625 }
1626 
1627 static int ena_request_mgmnt_irq(struct ena_adapter *adapter)
1628 {
1629 	unsigned long flags = 0;
1630 	struct ena_irq *irq;
1631 	int rc;
1632 
1633 	irq = &adapter->irq_tbl[ENA_MGMNT_IRQ_IDX];
1634 	rc = request_irq(irq->vector, irq->handler, flags, irq->name,
1635 			 irq->data);
1636 	if (rc) {
1637 		netif_err(adapter, probe, adapter->netdev,
1638 			  "Failed to request admin irq\n");
1639 		return rc;
1640 	}
1641 
1642 	netif_dbg(adapter, probe, adapter->netdev,
1643 		  "Set affinity hint of mgmnt irq.to 0x%lx (irq vector: %d)\n",
1644 		  irq->affinity_hint_mask.bits[0], irq->vector);
1645 
1646 	irq_set_affinity_hint(irq->vector, &irq->affinity_hint_mask);
1647 
1648 	return rc;
1649 }
1650 
1651 static int ena_request_io_irq(struct ena_adapter *adapter)
1652 {
1653 	u32 io_queue_count = adapter->num_io_queues + adapter->xdp_num_queues;
1654 	unsigned long flags = 0;
1655 	struct ena_irq *irq;
1656 	int rc = 0, i, k;
1657 
1658 	if (!test_bit(ENA_FLAG_MSIX_ENABLED, &adapter->flags)) {
1659 		netif_err(adapter, ifup, adapter->netdev,
1660 			  "Failed to request I/O IRQ: MSI-X is not enabled\n");
1661 		return -EINVAL;
1662 	}
1663 
1664 	for (i = ENA_IO_IRQ_FIRST_IDX; i < ENA_MAX_MSIX_VEC(io_queue_count); i++) {
1665 		irq = &adapter->irq_tbl[i];
1666 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
1667 				 irq->data);
1668 		if (rc) {
1669 			netif_err(adapter, ifup, adapter->netdev,
1670 				  "Failed to request I/O IRQ. index %d rc %d\n",
1671 				   i, rc);
1672 			goto err;
1673 		}
1674 
1675 		netif_dbg(adapter, ifup, adapter->netdev,
1676 			  "Set affinity hint of irq. index %d to 0x%lx (irq vector: %d)\n",
1677 			  i, irq->affinity_hint_mask.bits[0], irq->vector);
1678 
1679 		irq_set_affinity_hint(irq->vector, &irq->affinity_hint_mask);
1680 	}
1681 
1682 	return rc;
1683 
1684 err:
1685 	for (k = ENA_IO_IRQ_FIRST_IDX; k < i; k++) {
1686 		irq = &adapter->irq_tbl[k];
1687 		free_irq(irq->vector, irq->data);
1688 	}
1689 
1690 	return rc;
1691 }
1692 
1693 static void ena_free_mgmnt_irq(struct ena_adapter *adapter)
1694 {
1695 	struct ena_irq *irq;
1696 
1697 	irq = &adapter->irq_tbl[ENA_MGMNT_IRQ_IDX];
1698 	synchronize_irq(irq->vector);
1699 	irq_set_affinity_hint(irq->vector, NULL);
1700 	free_irq(irq->vector, irq->data);
1701 }
1702 
1703 static void ena_free_io_irq(struct ena_adapter *adapter)
1704 {
1705 	u32 io_queue_count = adapter->num_io_queues + adapter->xdp_num_queues;
1706 	struct ena_irq *irq;
1707 	int i;
1708 
1709 #ifdef CONFIG_RFS_ACCEL
1710 	if (adapter->msix_vecs >= 1) {
1711 		free_irq_cpu_rmap(adapter->netdev->rx_cpu_rmap);
1712 		adapter->netdev->rx_cpu_rmap = NULL;
1713 	}
1714 #endif /* CONFIG_RFS_ACCEL */
1715 
1716 	for (i = ENA_IO_IRQ_FIRST_IDX; i < ENA_MAX_MSIX_VEC(io_queue_count); i++) {
1717 		irq = &adapter->irq_tbl[i];
1718 		irq_set_affinity_hint(irq->vector, NULL);
1719 		free_irq(irq->vector, irq->data);
1720 	}
1721 }
1722 
1723 static void ena_disable_msix(struct ena_adapter *adapter)
1724 {
1725 	if (test_and_clear_bit(ENA_FLAG_MSIX_ENABLED, &adapter->flags))
1726 		pci_free_irq_vectors(adapter->pdev);
1727 }
1728 
1729 static void ena_disable_io_intr_sync(struct ena_adapter *adapter)
1730 {
1731 	u32 io_queue_count = adapter->num_io_queues + adapter->xdp_num_queues;
1732 	int i;
1733 
1734 	if (!netif_running(adapter->netdev))
1735 		return;
1736 
1737 	for (i = ENA_IO_IRQ_FIRST_IDX; i < ENA_MAX_MSIX_VEC(io_queue_count); i++)
1738 		synchronize_irq(adapter->irq_tbl[i].vector);
1739 }
1740 
1741 static void ena_del_napi_in_range(struct ena_adapter *adapter,
1742 				  int first_index,
1743 				  int count)
1744 {
1745 	int i;
1746 
1747 	for (i = first_index; i < first_index + count; i++) {
1748 		netif_napi_del(&adapter->ena_napi[i].napi);
1749 
1750 		WARN_ON(ENA_IS_XDP_INDEX(adapter, i) &&
1751 			adapter->ena_napi[i].rx_ring);
1752 	}
1753 }
1754 
1755 static void ena_init_napi_in_range(struct ena_adapter *adapter,
1756 				   int first_index, int count)
1757 {
1758 	int (*napi_handler)(struct napi_struct *napi, int budget);
1759 	int i;
1760 
1761 	for (i = first_index; i < first_index + count; i++) {
1762 		struct ena_napi *napi = &adapter->ena_napi[i];
1763 		struct ena_ring *rx_ring, *tx_ring;
1764 
1765 		memset(napi, 0, sizeof(*napi));
1766 
1767 		rx_ring = &adapter->rx_ring[i];
1768 		tx_ring = &adapter->tx_ring[i];
1769 
1770 		napi_handler = ena_io_poll;
1771 		if (ENA_IS_XDP_INDEX(adapter, i))
1772 			napi_handler = ena_xdp_io_poll;
1773 
1774 		netif_napi_add(adapter->netdev, &napi->napi, napi_handler);
1775 
1776 		if (!ENA_IS_XDP_INDEX(adapter, i))
1777 			napi->rx_ring = rx_ring;
1778 
1779 		napi->tx_ring = tx_ring;
1780 		napi->qid = i;
1781 	}
1782 }
1783 
1784 static void ena_napi_disable_in_range(struct ena_adapter *adapter,
1785 				      int first_index,
1786 				      int count)
1787 {
1788 	int i;
1789 
1790 	for (i = first_index; i < first_index + count; i++)
1791 		napi_disable(&adapter->ena_napi[i].napi);
1792 }
1793 
1794 static void ena_napi_enable_in_range(struct ena_adapter *adapter,
1795 				     int first_index,
1796 				     int count)
1797 {
1798 	int i;
1799 
1800 	for (i = first_index; i < first_index + count; i++)
1801 		napi_enable(&adapter->ena_napi[i].napi);
1802 }
1803 
1804 /* Configure the Rx forwarding */
1805 static int ena_rss_configure(struct ena_adapter *adapter)
1806 {
1807 	struct ena_com_dev *ena_dev = adapter->ena_dev;
1808 	int rc;
1809 
1810 	/* In case the RSS table wasn't initialized by probe */
1811 	if (!ena_dev->rss.tbl_log_size) {
1812 		rc = ena_rss_init_default(adapter);
1813 		if (rc && (rc != -EOPNOTSUPP)) {
1814 			netif_err(adapter, ifup, adapter->netdev,
1815 				  "Failed to init RSS rc: %d\n", rc);
1816 			return rc;
1817 		}
1818 	}
1819 
1820 	/* Set indirect table */
1821 	rc = ena_com_indirect_table_set(ena_dev);
1822 	if (unlikely(rc && rc != -EOPNOTSUPP))
1823 		return rc;
1824 
1825 	/* Configure hash function (if supported) */
1826 	rc = ena_com_set_hash_function(ena_dev);
1827 	if (unlikely(rc && (rc != -EOPNOTSUPP)))
1828 		return rc;
1829 
1830 	/* Configure hash inputs (if supported) */
1831 	rc = ena_com_set_hash_ctrl(ena_dev);
1832 	if (unlikely(rc && (rc != -EOPNOTSUPP)))
1833 		return rc;
1834 
1835 	return 0;
1836 }
1837 
1838 static int ena_up_complete(struct ena_adapter *adapter)
1839 {
1840 	int rc;
1841 
1842 	rc = ena_rss_configure(adapter);
1843 	if (rc)
1844 		return rc;
1845 
1846 	ena_change_mtu(adapter->netdev, adapter->netdev->mtu);
1847 
1848 	ena_refill_all_rx_bufs(adapter);
1849 
1850 	/* enable transmits */
1851 	netif_tx_start_all_queues(adapter->netdev);
1852 
1853 	ena_napi_enable_in_range(adapter,
1854 				 0,
1855 				 adapter->xdp_num_queues + adapter->num_io_queues);
1856 
1857 	return 0;
1858 }
1859 
1860 static int ena_create_io_tx_queue(struct ena_adapter *adapter, int qid)
1861 {
1862 	struct ena_com_create_io_ctx ctx;
1863 	struct ena_com_dev *ena_dev;
1864 	struct ena_ring *tx_ring;
1865 	u32 msix_vector;
1866 	u16 ena_qid;
1867 	int rc;
1868 
1869 	ena_dev = adapter->ena_dev;
1870 
1871 	tx_ring = &adapter->tx_ring[qid];
1872 	msix_vector = ENA_IO_IRQ_IDX(qid);
1873 	ena_qid = ENA_IO_TXQ_IDX(qid);
1874 
1875 	memset(&ctx, 0x0, sizeof(ctx));
1876 
1877 	ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_TX;
1878 	ctx.qid = ena_qid;
1879 	ctx.mem_queue_type = ena_dev->tx_mem_queue_type;
1880 	ctx.msix_vector = msix_vector;
1881 	ctx.queue_size = tx_ring->ring_size;
1882 	ctx.numa_node = tx_ring->numa_node;
1883 
1884 	rc = ena_com_create_io_queue(ena_dev, &ctx);
1885 	if (rc) {
1886 		netif_err(adapter, ifup, adapter->netdev,
1887 			  "Failed to create I/O TX queue num %d rc: %d\n",
1888 			  qid, rc);
1889 		return rc;
1890 	}
1891 
1892 	rc = ena_com_get_io_handlers(ena_dev, ena_qid,
1893 				     &tx_ring->ena_com_io_sq,
1894 				     &tx_ring->ena_com_io_cq);
1895 	if (rc) {
1896 		netif_err(adapter, ifup, adapter->netdev,
1897 			  "Failed to get TX queue handlers. TX queue num %d rc: %d\n",
1898 			  qid, rc);
1899 		ena_com_destroy_io_queue(ena_dev, ena_qid);
1900 		return rc;
1901 	}
1902 
1903 	ena_com_update_numa_node(tx_ring->ena_com_io_cq, ctx.numa_node);
1904 	return rc;
1905 }
1906 
1907 int ena_create_io_tx_queues_in_range(struct ena_adapter *adapter,
1908 				     int first_index, int count)
1909 {
1910 	struct ena_com_dev *ena_dev = adapter->ena_dev;
1911 	int rc, i;
1912 
1913 	for (i = first_index; i < first_index + count; i++) {
1914 		rc = ena_create_io_tx_queue(adapter, i);
1915 		if (rc)
1916 			goto create_err;
1917 	}
1918 
1919 	return 0;
1920 
1921 create_err:
1922 	while (i-- > first_index)
1923 		ena_com_destroy_io_queue(ena_dev, ENA_IO_TXQ_IDX(i));
1924 
1925 	return rc;
1926 }
1927 
1928 static int ena_create_io_rx_queue(struct ena_adapter *adapter, int qid)
1929 {
1930 	struct ena_com_dev *ena_dev;
1931 	struct ena_com_create_io_ctx ctx;
1932 	struct ena_ring *rx_ring;
1933 	u32 msix_vector;
1934 	u16 ena_qid;
1935 	int rc;
1936 
1937 	ena_dev = adapter->ena_dev;
1938 
1939 	rx_ring = &adapter->rx_ring[qid];
1940 	msix_vector = ENA_IO_IRQ_IDX(qid);
1941 	ena_qid = ENA_IO_RXQ_IDX(qid);
1942 
1943 	memset(&ctx, 0x0, sizeof(ctx));
1944 
1945 	ctx.qid = ena_qid;
1946 	ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_RX;
1947 	ctx.mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
1948 	ctx.msix_vector = msix_vector;
1949 	ctx.queue_size = rx_ring->ring_size;
1950 	ctx.numa_node = rx_ring->numa_node;
1951 
1952 	rc = ena_com_create_io_queue(ena_dev, &ctx);
1953 	if (rc) {
1954 		netif_err(adapter, ifup, adapter->netdev,
1955 			  "Failed to create I/O RX queue num %d rc: %d\n",
1956 			  qid, rc);
1957 		return rc;
1958 	}
1959 
1960 	rc = ena_com_get_io_handlers(ena_dev, ena_qid,
1961 				     &rx_ring->ena_com_io_sq,
1962 				     &rx_ring->ena_com_io_cq);
1963 	if (rc) {
1964 		netif_err(adapter, ifup, adapter->netdev,
1965 			  "Failed to get RX queue handlers. RX queue num %d rc: %d\n",
1966 			  qid, rc);
1967 		goto err;
1968 	}
1969 
1970 	ena_com_update_numa_node(rx_ring->ena_com_io_cq, ctx.numa_node);
1971 
1972 	return rc;
1973 err:
1974 	ena_com_destroy_io_queue(ena_dev, ena_qid);
1975 	return rc;
1976 }
1977 
1978 static int ena_create_all_io_rx_queues(struct ena_adapter *adapter)
1979 {
1980 	struct ena_com_dev *ena_dev = adapter->ena_dev;
1981 	int rc, i;
1982 
1983 	for (i = 0; i < adapter->num_io_queues; i++) {
1984 		rc = ena_create_io_rx_queue(adapter, i);
1985 		if (rc)
1986 			goto create_err;
1987 		INIT_WORK(&adapter->ena_napi[i].dim.work, ena_dim_work);
1988 
1989 		ena_xdp_register_rxq_info(&adapter->rx_ring[i]);
1990 	}
1991 
1992 	return 0;
1993 
1994 create_err:
1995 	while (i--) {
1996 		ena_xdp_unregister_rxq_info(&adapter->rx_ring[i]);
1997 		cancel_work_sync(&adapter->ena_napi[i].dim.work);
1998 		ena_com_destroy_io_queue(ena_dev, ENA_IO_RXQ_IDX(i));
1999 	}
2000 
2001 	return rc;
2002 }
2003 
2004 static void set_io_rings_size(struct ena_adapter *adapter,
2005 			      int new_tx_size,
2006 			      int new_rx_size)
2007 {
2008 	int i;
2009 
2010 	for (i = 0; i < adapter->num_io_queues; i++) {
2011 		adapter->tx_ring[i].ring_size = new_tx_size;
2012 		adapter->rx_ring[i].ring_size = new_rx_size;
2013 	}
2014 }
2015 
2016 /* This function allows queue allocation to backoff when the system is
2017  * low on memory. If there is not enough memory to allocate io queues
2018  * the driver will try to allocate smaller queues.
2019  *
2020  * The backoff algorithm is as follows:
2021  *  1. Try to allocate TX and RX and if successful.
2022  *  1.1. return success
2023  *
2024  *  2. Divide by 2 the size of the larger of RX and TX queues (or both if their size is the same).
2025  *
2026  *  3. If TX or RX is smaller than 256
2027  *  3.1. return failure.
2028  *  4. else
2029  *  4.1. go back to 1.
2030  */
2031 static int create_queues_with_size_backoff(struct ena_adapter *adapter)
2032 {
2033 	int rc, cur_rx_ring_size, cur_tx_ring_size;
2034 	int new_rx_ring_size, new_tx_ring_size;
2035 
2036 	/* current queue sizes might be set to smaller than the requested
2037 	 * ones due to past queue allocation failures.
2038 	 */
2039 	set_io_rings_size(adapter, adapter->requested_tx_ring_size,
2040 			  adapter->requested_rx_ring_size);
2041 
2042 	while (1) {
2043 		if (ena_xdp_present(adapter)) {
2044 			rc = ena_setup_and_create_all_xdp_queues(adapter);
2045 
2046 			if (rc)
2047 				goto err_setup_tx;
2048 		}
2049 		rc = ena_setup_tx_resources_in_range(adapter,
2050 						     0,
2051 						     adapter->num_io_queues);
2052 		if (rc)
2053 			goto err_setup_tx;
2054 
2055 		rc = ena_create_io_tx_queues_in_range(adapter,
2056 						      0,
2057 						      adapter->num_io_queues);
2058 		if (rc)
2059 			goto err_create_tx_queues;
2060 
2061 		rc = ena_setup_all_rx_resources(adapter);
2062 		if (rc)
2063 			goto err_setup_rx;
2064 
2065 		rc = ena_create_all_io_rx_queues(adapter);
2066 		if (rc)
2067 			goto err_create_rx_queues;
2068 
2069 		return 0;
2070 
2071 err_create_rx_queues:
2072 		ena_free_all_io_rx_resources(adapter);
2073 err_setup_rx:
2074 		ena_destroy_all_tx_queues(adapter);
2075 err_create_tx_queues:
2076 		ena_free_all_io_tx_resources(adapter);
2077 err_setup_tx:
2078 		if (rc != -ENOMEM) {
2079 			netif_err(adapter, ifup, adapter->netdev,
2080 				  "Queue creation failed with error code %d\n",
2081 				  rc);
2082 			return rc;
2083 		}
2084 
2085 		cur_tx_ring_size = adapter->tx_ring[0].ring_size;
2086 		cur_rx_ring_size = adapter->rx_ring[0].ring_size;
2087 
2088 		netif_err(adapter, ifup, adapter->netdev,
2089 			  "Not enough memory to create queues with sizes TX=%d, RX=%d\n",
2090 			  cur_tx_ring_size, cur_rx_ring_size);
2091 
2092 		new_tx_ring_size = cur_tx_ring_size;
2093 		new_rx_ring_size = cur_rx_ring_size;
2094 
2095 		/* Decrease the size of the larger queue, or
2096 		 * decrease both if they are the same size.
2097 		 */
2098 		if (cur_rx_ring_size <= cur_tx_ring_size)
2099 			new_tx_ring_size = cur_tx_ring_size / 2;
2100 		if (cur_rx_ring_size >= cur_tx_ring_size)
2101 			new_rx_ring_size = cur_rx_ring_size / 2;
2102 
2103 		if (new_tx_ring_size < ENA_MIN_RING_SIZE ||
2104 		    new_rx_ring_size < ENA_MIN_RING_SIZE) {
2105 			netif_err(adapter, ifup, adapter->netdev,
2106 				  "Queue creation failed with the smallest possible queue size of %d for both queues. Not retrying with smaller queues\n",
2107 				  ENA_MIN_RING_SIZE);
2108 			return rc;
2109 		}
2110 
2111 		netif_err(adapter, ifup, adapter->netdev,
2112 			  "Retrying queue creation with sizes TX=%d, RX=%d\n",
2113 			  new_tx_ring_size,
2114 			  new_rx_ring_size);
2115 
2116 		set_io_rings_size(adapter, new_tx_ring_size,
2117 				  new_rx_ring_size);
2118 	}
2119 }
2120 
2121 int ena_up(struct ena_adapter *adapter)
2122 {
2123 	int io_queue_count, rc, i;
2124 
2125 	netif_dbg(adapter, ifup, adapter->netdev, "%s\n", __func__);
2126 
2127 	io_queue_count = adapter->num_io_queues + adapter->xdp_num_queues;
2128 	ena_setup_io_intr(adapter);
2129 
2130 	/* napi poll functions should be initialized before running
2131 	 * request_irq(), to handle a rare condition where there is a pending
2132 	 * interrupt, causing the ISR to fire immediately while the poll
2133 	 * function wasn't set yet, causing a null dereference
2134 	 */
2135 	ena_init_napi_in_range(adapter, 0, io_queue_count);
2136 
2137 	rc = ena_request_io_irq(adapter);
2138 	if (rc)
2139 		goto err_req_irq;
2140 
2141 	rc = create_queues_with_size_backoff(adapter);
2142 	if (rc)
2143 		goto err_create_queues_with_backoff;
2144 
2145 	rc = ena_up_complete(adapter);
2146 	if (rc)
2147 		goto err_up;
2148 
2149 	if (test_bit(ENA_FLAG_LINK_UP, &adapter->flags))
2150 		netif_carrier_on(adapter->netdev);
2151 
2152 	ena_increase_stat(&adapter->dev_stats.interface_up, 1,
2153 			  &adapter->syncp);
2154 
2155 	set_bit(ENA_FLAG_DEV_UP, &adapter->flags);
2156 
2157 	/* Enable completion queues interrupt */
2158 	for (i = 0; i < adapter->num_io_queues; i++)
2159 		ena_unmask_interrupt(&adapter->tx_ring[i],
2160 				     &adapter->rx_ring[i]);
2161 
2162 	/* schedule napi in case we had pending packets
2163 	 * from the last time we disable napi
2164 	 */
2165 	for (i = 0; i < io_queue_count; i++)
2166 		napi_schedule(&adapter->ena_napi[i].napi);
2167 
2168 	return rc;
2169 
2170 err_up:
2171 	ena_destroy_all_tx_queues(adapter);
2172 	ena_free_all_io_tx_resources(adapter);
2173 	ena_destroy_all_rx_queues(adapter);
2174 	ena_free_all_io_rx_resources(adapter);
2175 err_create_queues_with_backoff:
2176 	ena_free_io_irq(adapter);
2177 err_req_irq:
2178 	ena_del_napi_in_range(adapter, 0, io_queue_count);
2179 
2180 	return rc;
2181 }
2182 
2183 void ena_down(struct ena_adapter *adapter)
2184 {
2185 	int io_queue_count = adapter->num_io_queues + adapter->xdp_num_queues;
2186 
2187 	netif_info(adapter, ifdown, adapter->netdev, "%s\n", __func__);
2188 
2189 	clear_bit(ENA_FLAG_DEV_UP, &adapter->flags);
2190 
2191 	ena_increase_stat(&adapter->dev_stats.interface_down, 1,
2192 			  &adapter->syncp);
2193 
2194 	netif_carrier_off(adapter->netdev);
2195 	netif_tx_disable(adapter->netdev);
2196 
2197 	/* After this point the napi handler won't enable the tx queue */
2198 	ena_napi_disable_in_range(adapter, 0, io_queue_count);
2199 
2200 	/* After destroy the queue there won't be any new interrupts */
2201 
2202 	if (test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags)) {
2203 		int rc;
2204 
2205 		rc = ena_com_dev_reset(adapter->ena_dev, adapter->reset_reason);
2206 		if (rc)
2207 			netif_err(adapter, ifdown, adapter->netdev,
2208 				  "Device reset failed\n");
2209 		/* stop submitting admin commands on a device that was reset */
2210 		ena_com_set_admin_running_state(adapter->ena_dev, false);
2211 	}
2212 
2213 	ena_destroy_all_io_queues(adapter);
2214 
2215 	ena_disable_io_intr_sync(adapter);
2216 	ena_free_io_irq(adapter);
2217 	ena_del_napi_in_range(adapter, 0, io_queue_count);
2218 
2219 	ena_free_all_tx_bufs(adapter);
2220 	ena_free_all_rx_bufs(adapter);
2221 	ena_free_all_io_tx_resources(adapter);
2222 	ena_free_all_io_rx_resources(adapter);
2223 }
2224 
2225 /* ena_open - Called when a network interface is made active
2226  * @netdev: network interface device structure
2227  *
2228  * Returns 0 on success, negative value on failure
2229  *
2230  * The open entry point is called when a network interface is made
2231  * active by the system (IFF_UP).  At this point all resources needed
2232  * for transmit and receive operations are allocated, the interrupt
2233  * handler is registered with the OS, the watchdog timer is started,
2234  * and the stack is notified that the interface is ready.
2235  */
2236 static int ena_open(struct net_device *netdev)
2237 {
2238 	struct ena_adapter *adapter = netdev_priv(netdev);
2239 	int rc;
2240 
2241 	/* Notify the stack of the actual queue counts. */
2242 	rc = netif_set_real_num_tx_queues(netdev, adapter->num_io_queues);
2243 	if (rc) {
2244 		netif_err(adapter, ifup, netdev, "Can't set num tx queues\n");
2245 		return rc;
2246 	}
2247 
2248 	rc = netif_set_real_num_rx_queues(netdev, adapter->num_io_queues);
2249 	if (rc) {
2250 		netif_err(adapter, ifup, netdev, "Can't set num rx queues\n");
2251 		return rc;
2252 	}
2253 
2254 	rc = ena_up(adapter);
2255 	if (rc)
2256 		return rc;
2257 
2258 	return rc;
2259 }
2260 
2261 /* ena_close - Disables a network interface
2262  * @netdev: network interface device structure
2263  *
2264  * Returns 0, this is not allowed to fail
2265  *
2266  * The close entry point is called when an interface is de-activated
2267  * by the OS.  The hardware is still under the drivers control, but
2268  * needs to be disabled.  A global MAC reset is issued to stop the
2269  * hardware, and all transmit and receive resources are freed.
2270  */
2271 static int ena_close(struct net_device *netdev)
2272 {
2273 	struct ena_adapter *adapter = netdev_priv(netdev);
2274 
2275 	netif_dbg(adapter, ifdown, netdev, "%s\n", __func__);
2276 
2277 	if (!test_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags))
2278 		return 0;
2279 
2280 	if (test_bit(ENA_FLAG_DEV_UP, &adapter->flags))
2281 		ena_down(adapter);
2282 
2283 	/* Check for device status and issue reset if needed*/
2284 	check_for_admin_com_state(adapter);
2285 	if (unlikely(test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags))) {
2286 		netif_err(adapter, ifdown, adapter->netdev,
2287 			  "Destroy failure, restarting device\n");
2288 		ena_dump_stats_to_dmesg(adapter);
2289 		/* rtnl lock already obtained in dev_ioctl() layer */
2290 		ena_destroy_device(adapter, false);
2291 		ena_restore_device(adapter);
2292 	}
2293 
2294 	return 0;
2295 }
2296 
2297 int ena_update_queue_params(struct ena_adapter *adapter,
2298 			    u32 new_tx_size,
2299 			    u32 new_rx_size,
2300 			    u32 new_llq_header_len)
2301 {
2302 	bool dev_was_up, large_llq_changed = false;
2303 	int rc = 0;
2304 
2305 	dev_was_up = test_bit(ENA_FLAG_DEV_UP, &adapter->flags);
2306 	ena_close(adapter->netdev);
2307 	adapter->requested_tx_ring_size = new_tx_size;
2308 	adapter->requested_rx_ring_size = new_rx_size;
2309 	ena_init_io_rings(adapter,
2310 			  0,
2311 			  adapter->xdp_num_queues +
2312 			  adapter->num_io_queues);
2313 
2314 	large_llq_changed = adapter->ena_dev->tx_mem_queue_type ==
2315 			    ENA_ADMIN_PLACEMENT_POLICY_DEV;
2316 	large_llq_changed &=
2317 		new_llq_header_len != adapter->ena_dev->tx_max_header_size;
2318 
2319 	/* a check that the configuration is valid is done by caller */
2320 	if (large_llq_changed) {
2321 		adapter->large_llq_header_enabled = !adapter->large_llq_header_enabled;
2322 
2323 		ena_destroy_device(adapter, false);
2324 		rc = ena_restore_device(adapter);
2325 	}
2326 
2327 	return dev_was_up && !rc ? ena_up(adapter) : rc;
2328 }
2329 
2330 int ena_set_rx_copybreak(struct ena_adapter *adapter, u32 rx_copybreak)
2331 {
2332 	struct ena_ring *rx_ring;
2333 	int i;
2334 
2335 	if (rx_copybreak > min_t(u16, adapter->netdev->mtu, ENA_PAGE_SIZE))
2336 		return -EINVAL;
2337 
2338 	adapter->rx_copybreak = rx_copybreak;
2339 
2340 	for (i = 0; i < adapter->num_io_queues; i++) {
2341 		rx_ring = &adapter->rx_ring[i];
2342 		rx_ring->rx_copybreak = rx_copybreak;
2343 	}
2344 
2345 	return 0;
2346 }
2347 
2348 int ena_update_queue_count(struct ena_adapter *adapter, u32 new_channel_count)
2349 {
2350 	struct ena_com_dev *ena_dev = adapter->ena_dev;
2351 	int prev_channel_count;
2352 	bool dev_was_up;
2353 
2354 	dev_was_up = test_bit(ENA_FLAG_DEV_UP, &adapter->flags);
2355 	ena_close(adapter->netdev);
2356 	prev_channel_count = adapter->num_io_queues;
2357 	adapter->num_io_queues = new_channel_count;
2358 	if (ena_xdp_present(adapter) &&
2359 	    ena_xdp_allowed(adapter) == ENA_XDP_ALLOWED) {
2360 		adapter->xdp_first_ring = new_channel_count;
2361 		adapter->xdp_num_queues = new_channel_count;
2362 		if (prev_channel_count > new_channel_count)
2363 			ena_xdp_exchange_program_rx_in_range(adapter,
2364 							     NULL,
2365 							     new_channel_count,
2366 							     prev_channel_count);
2367 		else
2368 			ena_xdp_exchange_program_rx_in_range(adapter,
2369 							     adapter->xdp_bpf_prog,
2370 							     prev_channel_count,
2371 							     new_channel_count);
2372 	}
2373 
2374 	/* We need to destroy the rss table so that the indirection
2375 	 * table will be reinitialized by ena_up()
2376 	 */
2377 	ena_com_rss_destroy(ena_dev);
2378 	ena_init_io_rings(adapter,
2379 			  0,
2380 			  adapter->xdp_num_queues +
2381 			  adapter->num_io_queues);
2382 	return dev_was_up ? ena_open(adapter->netdev) : 0;
2383 }
2384 
2385 static void ena_tx_csum(struct ena_com_tx_ctx *ena_tx_ctx,
2386 			struct sk_buff *skb,
2387 			bool disable_meta_caching)
2388 {
2389 	u32 mss = skb_shinfo(skb)->gso_size;
2390 	struct ena_com_tx_meta *ena_meta = &ena_tx_ctx->ena_meta;
2391 	u8 l4_protocol = 0;
2392 
2393 	if ((skb->ip_summed == CHECKSUM_PARTIAL) || mss) {
2394 		ena_tx_ctx->l4_csum_enable = 1;
2395 		if (mss) {
2396 			ena_tx_ctx->tso_enable = 1;
2397 			ena_meta->l4_hdr_len = tcp_hdr(skb)->doff;
2398 			ena_tx_ctx->l4_csum_partial = 0;
2399 		} else {
2400 			ena_tx_ctx->tso_enable = 0;
2401 			ena_meta->l4_hdr_len = 0;
2402 			ena_tx_ctx->l4_csum_partial = 1;
2403 		}
2404 
2405 		switch (ip_hdr(skb)->version) {
2406 		case IPVERSION:
2407 			ena_tx_ctx->l3_proto = ENA_ETH_IO_L3_PROTO_IPV4;
2408 			if (ip_hdr(skb)->frag_off & htons(IP_DF))
2409 				ena_tx_ctx->df = 1;
2410 			if (mss)
2411 				ena_tx_ctx->l3_csum_enable = 1;
2412 			l4_protocol = ip_hdr(skb)->protocol;
2413 			break;
2414 		case 6:
2415 			ena_tx_ctx->l3_proto = ENA_ETH_IO_L3_PROTO_IPV6;
2416 			l4_protocol = ipv6_hdr(skb)->nexthdr;
2417 			break;
2418 		default:
2419 			break;
2420 		}
2421 
2422 		if (l4_protocol == IPPROTO_TCP)
2423 			ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_TCP;
2424 		else
2425 			ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_UDP;
2426 
2427 		ena_meta->mss = mss;
2428 		ena_meta->l3_hdr_len = skb_network_header_len(skb);
2429 		ena_meta->l3_hdr_offset = skb_network_offset(skb);
2430 		ena_tx_ctx->meta_valid = 1;
2431 	} else if (disable_meta_caching) {
2432 		memset(ena_meta, 0, sizeof(*ena_meta));
2433 		ena_tx_ctx->meta_valid = 1;
2434 	} else {
2435 		ena_tx_ctx->meta_valid = 0;
2436 	}
2437 }
2438 
2439 static int ena_check_and_linearize_skb(struct ena_ring *tx_ring,
2440 				       struct sk_buff *skb)
2441 {
2442 	int num_frags, header_len, rc;
2443 
2444 	num_frags = skb_shinfo(skb)->nr_frags;
2445 	header_len = skb_headlen(skb);
2446 
2447 	if (num_frags < tx_ring->sgl_size)
2448 		return 0;
2449 
2450 	if ((num_frags == tx_ring->sgl_size) &&
2451 	    (header_len < tx_ring->tx_max_header_size))
2452 		return 0;
2453 
2454 	ena_increase_stat(&tx_ring->tx_stats.linearize, 1, &tx_ring->syncp);
2455 
2456 	rc = skb_linearize(skb);
2457 	if (unlikely(rc)) {
2458 		ena_increase_stat(&tx_ring->tx_stats.linearize_failed, 1,
2459 				  &tx_ring->syncp);
2460 	}
2461 
2462 	return rc;
2463 }
2464 
2465 static int ena_tx_map_skb(struct ena_ring *tx_ring,
2466 			  struct ena_tx_buffer *tx_info,
2467 			  struct sk_buff *skb,
2468 			  void **push_hdr,
2469 			  u16 *header_len)
2470 {
2471 	struct ena_adapter *adapter = tx_ring->adapter;
2472 	struct ena_com_buf *ena_buf;
2473 	dma_addr_t dma;
2474 	u32 skb_head_len, frag_len, last_frag;
2475 	u16 push_len = 0;
2476 	u16 delta = 0;
2477 	int i = 0;
2478 
2479 	skb_head_len = skb_headlen(skb);
2480 	tx_info->skb = skb;
2481 	ena_buf = tx_info->bufs;
2482 
2483 	if (tx_ring->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV) {
2484 		/* When the device is LLQ mode, the driver will copy
2485 		 * the header into the device memory space.
2486 		 * the ena_com layer assume the header is in a linear
2487 		 * memory space.
2488 		 * This assumption might be wrong since part of the header
2489 		 * can be in the fragmented buffers.
2490 		 * Use skb_header_pointer to make sure the header is in a
2491 		 * linear memory space.
2492 		 */
2493 
2494 		push_len = min_t(u32, skb->len, tx_ring->tx_max_header_size);
2495 		*push_hdr = skb_header_pointer(skb, 0, push_len,
2496 					       tx_ring->push_buf_intermediate_buf);
2497 		*header_len = push_len;
2498 		if (unlikely(skb->data != *push_hdr)) {
2499 			ena_increase_stat(&tx_ring->tx_stats.llq_buffer_copy, 1,
2500 					  &tx_ring->syncp);
2501 
2502 			delta = push_len - skb_head_len;
2503 		}
2504 	} else {
2505 		*push_hdr = NULL;
2506 		*header_len = min_t(u32, skb_head_len,
2507 				    tx_ring->tx_max_header_size);
2508 	}
2509 
2510 	netif_dbg(adapter, tx_queued, adapter->netdev,
2511 		  "skb: %p header_buf->vaddr: %p push_len: %d\n", skb,
2512 		  *push_hdr, push_len);
2513 
2514 	if (skb_head_len > push_len) {
2515 		dma = dma_map_single(tx_ring->dev, skb->data + push_len,
2516 				     skb_head_len - push_len, DMA_TO_DEVICE);
2517 		if (unlikely(dma_mapping_error(tx_ring->dev, dma)))
2518 			goto error_report_dma_error;
2519 
2520 		ena_buf->paddr = dma;
2521 		ena_buf->len = skb_head_len - push_len;
2522 
2523 		ena_buf++;
2524 		tx_info->num_of_bufs++;
2525 		tx_info->map_linear_data = 1;
2526 	} else {
2527 		tx_info->map_linear_data = 0;
2528 	}
2529 
2530 	last_frag = skb_shinfo(skb)->nr_frags;
2531 
2532 	for (i = 0; i < last_frag; i++) {
2533 		const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2534 
2535 		frag_len = skb_frag_size(frag);
2536 
2537 		if (unlikely(delta >= frag_len)) {
2538 			delta -= frag_len;
2539 			continue;
2540 		}
2541 
2542 		dma = skb_frag_dma_map(tx_ring->dev, frag, delta,
2543 				       frag_len - delta, DMA_TO_DEVICE);
2544 		if (unlikely(dma_mapping_error(tx_ring->dev, dma)))
2545 			goto error_report_dma_error;
2546 
2547 		ena_buf->paddr = dma;
2548 		ena_buf->len = frag_len - delta;
2549 		ena_buf++;
2550 		tx_info->num_of_bufs++;
2551 		delta = 0;
2552 	}
2553 
2554 	return 0;
2555 
2556 error_report_dma_error:
2557 	ena_increase_stat(&tx_ring->tx_stats.dma_mapping_err, 1,
2558 			  &tx_ring->syncp);
2559 	netif_warn(adapter, tx_queued, adapter->netdev, "Failed to map skb\n");
2560 
2561 	tx_info->skb = NULL;
2562 
2563 	tx_info->num_of_bufs += i;
2564 	ena_unmap_tx_buff(tx_ring, tx_info);
2565 
2566 	return -EINVAL;
2567 }
2568 
2569 /* Called with netif_tx_lock. */
2570 static netdev_tx_t ena_start_xmit(struct sk_buff *skb, struct net_device *dev)
2571 {
2572 	struct ena_adapter *adapter = netdev_priv(dev);
2573 	struct ena_tx_buffer *tx_info;
2574 	struct ena_com_tx_ctx ena_tx_ctx;
2575 	struct ena_ring *tx_ring;
2576 	struct netdev_queue *txq;
2577 	void *push_hdr;
2578 	u16 next_to_use, req_id, header_len;
2579 	int qid, rc;
2580 
2581 	netif_dbg(adapter, tx_queued, dev, "%s skb %p\n", __func__, skb);
2582 	/*  Determine which tx ring we will be placed on */
2583 	qid = skb_get_queue_mapping(skb);
2584 	tx_ring = &adapter->tx_ring[qid];
2585 	txq = netdev_get_tx_queue(dev, qid);
2586 
2587 	rc = ena_check_and_linearize_skb(tx_ring, skb);
2588 	if (unlikely(rc))
2589 		goto error_drop_packet;
2590 
2591 	skb_tx_timestamp(skb);
2592 
2593 	next_to_use = tx_ring->next_to_use;
2594 	req_id = tx_ring->free_ids[next_to_use];
2595 	tx_info = &tx_ring->tx_buffer_info[req_id];
2596 	tx_info->num_of_bufs = 0;
2597 
2598 	WARN(tx_info->skb, "SKB isn't NULL req_id %d\n", req_id);
2599 
2600 	rc = ena_tx_map_skb(tx_ring, tx_info, skb, &push_hdr, &header_len);
2601 	if (unlikely(rc))
2602 		goto error_drop_packet;
2603 
2604 	memset(&ena_tx_ctx, 0x0, sizeof(struct ena_com_tx_ctx));
2605 	ena_tx_ctx.ena_bufs = tx_info->bufs;
2606 	ena_tx_ctx.push_header = push_hdr;
2607 	ena_tx_ctx.num_bufs = tx_info->num_of_bufs;
2608 	ena_tx_ctx.req_id = req_id;
2609 	ena_tx_ctx.header_len = header_len;
2610 
2611 	/* set flags and meta data */
2612 	ena_tx_csum(&ena_tx_ctx, skb, tx_ring->disable_meta_caching);
2613 
2614 	rc = ena_xmit_common(adapter,
2615 			     tx_ring,
2616 			     tx_info,
2617 			     &ena_tx_ctx,
2618 			     next_to_use,
2619 			     skb->len);
2620 	if (rc)
2621 		goto error_unmap_dma;
2622 
2623 	netdev_tx_sent_queue(txq, skb->len);
2624 
2625 	/* stop the queue when no more space available, the packet can have up
2626 	 * to sgl_size + 2. one for the meta descriptor and one for header
2627 	 * (if the header is larger than tx_max_header_size).
2628 	 */
2629 	if (unlikely(!ena_com_sq_have_enough_space(tx_ring->ena_com_io_sq,
2630 						   tx_ring->sgl_size + 2))) {
2631 		netif_dbg(adapter, tx_queued, dev, "%s stop queue %d\n",
2632 			  __func__, qid);
2633 
2634 		netif_tx_stop_queue(txq);
2635 		ena_increase_stat(&tx_ring->tx_stats.queue_stop, 1,
2636 				  &tx_ring->syncp);
2637 
2638 		/* There is a rare condition where this function decide to
2639 		 * stop the queue but meanwhile clean_tx_irq updates
2640 		 * next_to_completion and terminates.
2641 		 * The queue will remain stopped forever.
2642 		 * To solve this issue add a mb() to make sure that
2643 		 * netif_tx_stop_queue() write is vissible before checking if
2644 		 * there is additional space in the queue.
2645 		 */
2646 		smp_mb();
2647 
2648 		if (ena_com_sq_have_enough_space(tx_ring->ena_com_io_sq,
2649 						 ENA_TX_WAKEUP_THRESH)) {
2650 			netif_tx_wake_queue(txq);
2651 			ena_increase_stat(&tx_ring->tx_stats.queue_wakeup, 1,
2652 					  &tx_ring->syncp);
2653 		}
2654 	}
2655 
2656 	if (netif_xmit_stopped(txq) || !netdev_xmit_more())
2657 		/* trigger the dma engine. ena_ring_tx_doorbell()
2658 		 * calls a memory barrier inside it.
2659 		 */
2660 		ena_ring_tx_doorbell(tx_ring);
2661 
2662 	return NETDEV_TX_OK;
2663 
2664 error_unmap_dma:
2665 	ena_unmap_tx_buff(tx_ring, tx_info);
2666 	tx_info->skb = NULL;
2667 
2668 error_drop_packet:
2669 	dev_kfree_skb(skb);
2670 	return NETDEV_TX_OK;
2671 }
2672 
2673 static u16 ena_select_queue(struct net_device *dev, struct sk_buff *skb,
2674 			    struct net_device *sb_dev)
2675 {
2676 	u16 qid;
2677 	/* we suspect that this is good for in--kernel network services that
2678 	 * want to loop incoming skb rx to tx in normal user generated traffic,
2679 	 * most probably we will not get to this
2680 	 */
2681 	if (skb_rx_queue_recorded(skb))
2682 		qid = skb_get_rx_queue(skb);
2683 	else
2684 		qid = netdev_pick_tx(dev, skb, NULL);
2685 
2686 	return qid;
2687 }
2688 
2689 static void ena_config_host_info(struct ena_com_dev *ena_dev, struct pci_dev *pdev)
2690 {
2691 	struct device *dev = &pdev->dev;
2692 	struct ena_admin_host_info *host_info;
2693 	int rc;
2694 
2695 	/* Allocate only the host info */
2696 	rc = ena_com_allocate_host_info(ena_dev);
2697 	if (rc) {
2698 		dev_err(dev, "Cannot allocate host info\n");
2699 		return;
2700 	}
2701 
2702 	host_info = ena_dev->host_attr.host_info;
2703 
2704 	host_info->bdf = pci_dev_id(pdev);
2705 	host_info->os_type = ENA_ADMIN_OS_LINUX;
2706 	host_info->kernel_ver = LINUX_VERSION_CODE;
2707 	strscpy(host_info->kernel_ver_str, utsname()->version,
2708 		sizeof(host_info->kernel_ver_str) - 1);
2709 	host_info->os_dist = 0;
2710 	strscpy(host_info->os_dist_str, utsname()->release,
2711 		sizeof(host_info->os_dist_str));
2712 	host_info->driver_version =
2713 		(DRV_MODULE_GEN_MAJOR) |
2714 		(DRV_MODULE_GEN_MINOR << ENA_ADMIN_HOST_INFO_MINOR_SHIFT) |
2715 		(DRV_MODULE_GEN_SUBMINOR << ENA_ADMIN_HOST_INFO_SUB_MINOR_SHIFT) |
2716 		("K"[0] << ENA_ADMIN_HOST_INFO_MODULE_TYPE_SHIFT);
2717 	host_info->num_cpus = num_online_cpus();
2718 
2719 	host_info->driver_supported_features =
2720 		ENA_ADMIN_HOST_INFO_RX_OFFSET_MASK |
2721 		ENA_ADMIN_HOST_INFO_INTERRUPT_MODERATION_MASK |
2722 		ENA_ADMIN_HOST_INFO_RX_BUF_MIRRORING_MASK |
2723 		ENA_ADMIN_HOST_INFO_RSS_CONFIGURABLE_FUNCTION_KEY_MASK |
2724 		ENA_ADMIN_HOST_INFO_RX_PAGE_REUSE_MASK;
2725 
2726 	rc = ena_com_set_host_attributes(ena_dev);
2727 	if (rc) {
2728 		if (rc == -EOPNOTSUPP)
2729 			dev_warn(dev, "Cannot set host attributes\n");
2730 		else
2731 			dev_err(dev, "Cannot set host attributes\n");
2732 
2733 		goto err;
2734 	}
2735 
2736 	return;
2737 
2738 err:
2739 	ena_com_delete_host_info(ena_dev);
2740 }
2741 
2742 static void ena_config_debug_area(struct ena_adapter *adapter)
2743 {
2744 	u32 debug_area_size;
2745 	int rc, ss_count;
2746 
2747 	ss_count = ena_get_sset_count(adapter->netdev, ETH_SS_STATS);
2748 	if (ss_count <= 0) {
2749 		netif_err(adapter, drv, adapter->netdev,
2750 			  "SS count is negative\n");
2751 		return;
2752 	}
2753 
2754 	/* allocate 32 bytes for each string and 64bit for the value */
2755 	debug_area_size = ss_count * ETH_GSTRING_LEN + sizeof(u64) * ss_count;
2756 
2757 	rc = ena_com_allocate_debug_area(adapter->ena_dev, debug_area_size);
2758 	if (rc) {
2759 		netif_err(adapter, drv, adapter->netdev,
2760 			  "Cannot allocate debug area\n");
2761 		return;
2762 	}
2763 
2764 	rc = ena_com_set_host_attributes(adapter->ena_dev);
2765 	if (rc) {
2766 		if (rc == -EOPNOTSUPP)
2767 			netif_warn(adapter, drv, adapter->netdev,
2768 				   "Cannot set host attributes\n");
2769 		else
2770 			netif_err(adapter, drv, adapter->netdev,
2771 				  "Cannot set host attributes\n");
2772 		goto err;
2773 	}
2774 
2775 	return;
2776 err:
2777 	ena_com_delete_debug_area(adapter->ena_dev);
2778 }
2779 
2780 int ena_update_hw_stats(struct ena_adapter *adapter)
2781 {
2782 	int rc;
2783 
2784 	rc = ena_com_get_eni_stats(adapter->ena_dev, &adapter->eni_stats);
2785 	if (rc) {
2786 		netdev_err(adapter->netdev, "Failed to get ENI stats\n");
2787 		return rc;
2788 	}
2789 
2790 	return 0;
2791 }
2792 
2793 static void ena_get_stats64(struct net_device *netdev,
2794 			    struct rtnl_link_stats64 *stats)
2795 {
2796 	struct ena_adapter *adapter = netdev_priv(netdev);
2797 	struct ena_ring *rx_ring, *tx_ring;
2798 	u64 total_xdp_rx_drops = 0;
2799 	unsigned int start;
2800 	u64 rx_drops;
2801 	u64 tx_drops;
2802 	int i;
2803 
2804 	if (!test_bit(ENA_FLAG_DEV_UP, &adapter->flags))
2805 		return;
2806 
2807 	for (i = 0; i < adapter->num_io_queues + adapter->xdp_num_queues; i++) {
2808 		u64 bytes, packets, xdp_rx_drops;
2809 
2810 		tx_ring = &adapter->tx_ring[i];
2811 
2812 		do {
2813 			start = u64_stats_fetch_begin(&tx_ring->syncp);
2814 			packets = tx_ring->tx_stats.cnt;
2815 			bytes = tx_ring->tx_stats.bytes;
2816 		} while (u64_stats_fetch_retry(&tx_ring->syncp, start));
2817 
2818 		stats->tx_packets += packets;
2819 		stats->tx_bytes += bytes;
2820 
2821 		/* In XDP there isn't an RX queue counterpart */
2822 		if (ENA_IS_XDP_INDEX(adapter, i))
2823 			continue;
2824 
2825 		rx_ring = &adapter->rx_ring[i];
2826 
2827 		do {
2828 			start = u64_stats_fetch_begin(&rx_ring->syncp);
2829 			packets = rx_ring->rx_stats.cnt;
2830 			bytes = rx_ring->rx_stats.bytes;
2831 			xdp_rx_drops = rx_ring->rx_stats.xdp_drop;
2832 		} while (u64_stats_fetch_retry(&rx_ring->syncp, start));
2833 
2834 		stats->rx_packets += packets;
2835 		stats->rx_bytes += bytes;
2836 		total_xdp_rx_drops += xdp_rx_drops;
2837 	}
2838 
2839 	do {
2840 		start = u64_stats_fetch_begin(&adapter->syncp);
2841 		rx_drops = adapter->dev_stats.rx_drops;
2842 		tx_drops = adapter->dev_stats.tx_drops;
2843 	} while (u64_stats_fetch_retry(&adapter->syncp, start));
2844 
2845 	stats->rx_dropped = rx_drops + total_xdp_rx_drops;
2846 	stats->tx_dropped = tx_drops;
2847 
2848 	stats->multicast = 0;
2849 	stats->collisions = 0;
2850 
2851 	stats->rx_length_errors = 0;
2852 	stats->rx_crc_errors = 0;
2853 	stats->rx_frame_errors = 0;
2854 	stats->rx_fifo_errors = 0;
2855 	stats->rx_missed_errors = 0;
2856 	stats->tx_window_errors = 0;
2857 
2858 	stats->rx_errors = 0;
2859 	stats->tx_errors = 0;
2860 }
2861 
2862 static const struct net_device_ops ena_netdev_ops = {
2863 	.ndo_open		= ena_open,
2864 	.ndo_stop		= ena_close,
2865 	.ndo_start_xmit		= ena_start_xmit,
2866 	.ndo_select_queue	= ena_select_queue,
2867 	.ndo_get_stats64	= ena_get_stats64,
2868 	.ndo_tx_timeout		= ena_tx_timeout,
2869 	.ndo_change_mtu		= ena_change_mtu,
2870 	.ndo_set_mac_address	= NULL,
2871 	.ndo_validate_addr	= eth_validate_addr,
2872 	.ndo_bpf		= ena_xdp,
2873 	.ndo_xdp_xmit		= ena_xdp_xmit,
2874 };
2875 
2876 static void ena_calc_io_queue_size(struct ena_adapter *adapter,
2877 				   struct ena_com_dev_get_features_ctx *get_feat_ctx)
2878 {
2879 	struct ena_admin_feature_llq_desc *llq = &get_feat_ctx->llq;
2880 	struct ena_com_dev *ena_dev = adapter->ena_dev;
2881 	u32 tx_queue_size = ENA_DEFAULT_RING_SIZE;
2882 	u32 rx_queue_size = ENA_DEFAULT_RING_SIZE;
2883 	u32 max_tx_queue_size;
2884 	u32 max_rx_queue_size;
2885 
2886 	/* If this function is called after driver load, the ring sizes have already
2887 	 * been configured. Take it into account when recalculating ring size.
2888 	 */
2889 	if (adapter->tx_ring->ring_size)
2890 		tx_queue_size = adapter->tx_ring->ring_size;
2891 
2892 	if (adapter->rx_ring->ring_size)
2893 		rx_queue_size = adapter->rx_ring->ring_size;
2894 
2895 	if (ena_dev->supported_features & BIT(ENA_ADMIN_MAX_QUEUES_EXT)) {
2896 		struct ena_admin_queue_ext_feature_fields *max_queue_ext =
2897 			&get_feat_ctx->max_queue_ext.max_queue_ext;
2898 		max_rx_queue_size = min_t(u32, max_queue_ext->max_rx_cq_depth,
2899 					  max_queue_ext->max_rx_sq_depth);
2900 		max_tx_queue_size = max_queue_ext->max_tx_cq_depth;
2901 
2902 		if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV)
2903 			max_tx_queue_size = min_t(u32, max_tx_queue_size,
2904 						  llq->max_llq_depth);
2905 		else
2906 			max_tx_queue_size = min_t(u32, max_tx_queue_size,
2907 						  max_queue_ext->max_tx_sq_depth);
2908 
2909 		adapter->max_tx_sgl_size = min_t(u16, ENA_PKT_MAX_BUFS,
2910 						 max_queue_ext->max_per_packet_tx_descs);
2911 		adapter->max_rx_sgl_size = min_t(u16, ENA_PKT_MAX_BUFS,
2912 						 max_queue_ext->max_per_packet_rx_descs);
2913 	} else {
2914 		struct ena_admin_queue_feature_desc *max_queues =
2915 			&get_feat_ctx->max_queues;
2916 		max_rx_queue_size = min_t(u32, max_queues->max_cq_depth,
2917 					  max_queues->max_sq_depth);
2918 		max_tx_queue_size = max_queues->max_cq_depth;
2919 
2920 		if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV)
2921 			max_tx_queue_size = min_t(u32, max_tx_queue_size,
2922 						  llq->max_llq_depth);
2923 		else
2924 			max_tx_queue_size = min_t(u32, max_tx_queue_size,
2925 						  max_queues->max_sq_depth);
2926 
2927 		adapter->max_tx_sgl_size = min_t(u16, ENA_PKT_MAX_BUFS,
2928 						 max_queues->max_packet_tx_descs);
2929 		adapter->max_rx_sgl_size = min_t(u16, ENA_PKT_MAX_BUFS,
2930 						 max_queues->max_packet_rx_descs);
2931 	}
2932 
2933 	max_tx_queue_size = rounddown_pow_of_two(max_tx_queue_size);
2934 	max_rx_queue_size = rounddown_pow_of_two(max_rx_queue_size);
2935 
2936 	/* When forcing large headers, we multiply the entry size by 2, and therefore divide
2937 	 * the queue size by 2, leaving the amount of memory used by the queues unchanged.
2938 	 */
2939 	if (adapter->large_llq_header_enabled) {
2940 		if ((llq->entry_size_ctrl_supported & ENA_ADMIN_LIST_ENTRY_SIZE_256B) &&
2941 		    ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV) {
2942 			max_tx_queue_size /= 2;
2943 			dev_info(&adapter->pdev->dev,
2944 				 "Forcing large headers and decreasing maximum TX queue size to %d\n",
2945 				 max_tx_queue_size);
2946 		} else {
2947 			dev_err(&adapter->pdev->dev,
2948 				"Forcing large headers failed: LLQ is disabled or device does not support large headers\n");
2949 
2950 			adapter->large_llq_header_enabled = false;
2951 		}
2952 	}
2953 
2954 	tx_queue_size = clamp_val(tx_queue_size, ENA_MIN_RING_SIZE,
2955 				  max_tx_queue_size);
2956 	rx_queue_size = clamp_val(rx_queue_size, ENA_MIN_RING_SIZE,
2957 				  max_rx_queue_size);
2958 
2959 	tx_queue_size = rounddown_pow_of_two(tx_queue_size);
2960 	rx_queue_size = rounddown_pow_of_two(rx_queue_size);
2961 
2962 	adapter->max_tx_ring_size  = max_tx_queue_size;
2963 	adapter->max_rx_ring_size = max_rx_queue_size;
2964 	adapter->requested_tx_ring_size = tx_queue_size;
2965 	adapter->requested_rx_ring_size = rx_queue_size;
2966 }
2967 
2968 static int ena_device_validate_params(struct ena_adapter *adapter,
2969 				      struct ena_com_dev_get_features_ctx *get_feat_ctx)
2970 {
2971 	struct net_device *netdev = adapter->netdev;
2972 	int rc;
2973 
2974 	rc = ether_addr_equal(get_feat_ctx->dev_attr.mac_addr,
2975 			      adapter->mac_addr);
2976 	if (!rc) {
2977 		netif_err(adapter, drv, netdev,
2978 			  "Error, mac address are different\n");
2979 		return -EINVAL;
2980 	}
2981 
2982 	if (get_feat_ctx->dev_attr.max_mtu < netdev->mtu) {
2983 		netif_err(adapter, drv, netdev,
2984 			  "Error, device max mtu is smaller than netdev MTU\n");
2985 		return -EINVAL;
2986 	}
2987 
2988 	return 0;
2989 }
2990 
2991 static void set_default_llq_configurations(struct ena_adapter *adapter,
2992 					   struct ena_llq_configurations *llq_config,
2993 					   struct ena_admin_feature_llq_desc *llq)
2994 {
2995 	struct ena_com_dev *ena_dev = adapter->ena_dev;
2996 
2997 	llq_config->llq_header_location = ENA_ADMIN_INLINE_HEADER;
2998 	llq_config->llq_stride_ctrl = ENA_ADMIN_MULTIPLE_DESCS_PER_ENTRY;
2999 	llq_config->llq_num_decs_before_header = ENA_ADMIN_LLQ_NUM_DESCS_BEFORE_HEADER_2;
3000 
3001 	adapter->large_llq_header_supported =
3002 		!!(ena_dev->supported_features & BIT(ENA_ADMIN_LLQ));
3003 	adapter->large_llq_header_supported &=
3004 		!!(llq->entry_size_ctrl_supported &
3005 			ENA_ADMIN_LIST_ENTRY_SIZE_256B);
3006 
3007 	if ((llq->entry_size_ctrl_supported & ENA_ADMIN_LIST_ENTRY_SIZE_256B) &&
3008 	    adapter->large_llq_header_enabled) {
3009 		llq_config->llq_ring_entry_size = ENA_ADMIN_LIST_ENTRY_SIZE_256B;
3010 		llq_config->llq_ring_entry_size_value = 256;
3011 	} else {
3012 		llq_config->llq_ring_entry_size = ENA_ADMIN_LIST_ENTRY_SIZE_128B;
3013 		llq_config->llq_ring_entry_size_value = 128;
3014 	}
3015 }
3016 
3017 static int ena_set_queues_placement_policy(struct pci_dev *pdev,
3018 					   struct ena_com_dev *ena_dev,
3019 					   struct ena_admin_feature_llq_desc *llq,
3020 					   struct ena_llq_configurations *llq_default_configurations)
3021 {
3022 	int rc;
3023 	u32 llq_feature_mask;
3024 
3025 	llq_feature_mask = 1 << ENA_ADMIN_LLQ;
3026 	if (!(ena_dev->supported_features & llq_feature_mask)) {
3027 		dev_warn(&pdev->dev,
3028 			"LLQ is not supported Fallback to host mode policy.\n");
3029 		ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
3030 		return 0;
3031 	}
3032 
3033 	if (!ena_dev->mem_bar) {
3034 		netdev_err(ena_dev->net_device,
3035 			   "LLQ is advertised as supported but device doesn't expose mem bar\n");
3036 		ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
3037 		return 0;
3038 	}
3039 
3040 	rc = ena_com_config_dev_mode(ena_dev, llq, llq_default_configurations);
3041 	if (unlikely(rc)) {
3042 		dev_err(&pdev->dev,
3043 			"Failed to configure the device mode.  Fallback to host mode policy.\n");
3044 		ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
3045 	}
3046 
3047 	return 0;
3048 }
3049 
3050 static int ena_map_llq_mem_bar(struct pci_dev *pdev, struct ena_com_dev *ena_dev,
3051 			       int bars)
3052 {
3053 	bool has_mem_bar = !!(bars & BIT(ENA_MEM_BAR));
3054 
3055 	if (!has_mem_bar)
3056 		return 0;
3057 
3058 	ena_dev->mem_bar = devm_ioremap_wc(&pdev->dev,
3059 					   pci_resource_start(pdev, ENA_MEM_BAR),
3060 					   pci_resource_len(pdev, ENA_MEM_BAR));
3061 
3062 	if (!ena_dev->mem_bar)
3063 		return -EFAULT;
3064 
3065 	return 0;
3066 }
3067 
3068 static int ena_device_init(struct ena_adapter *adapter, struct pci_dev *pdev,
3069 			   struct ena_com_dev_get_features_ctx *get_feat_ctx,
3070 			   bool *wd_state)
3071 {
3072 	struct ena_com_dev *ena_dev = adapter->ena_dev;
3073 	struct ena_llq_configurations llq_config;
3074 	struct device *dev = &pdev->dev;
3075 	bool readless_supported;
3076 	u32 aenq_groups;
3077 	int dma_width;
3078 	int rc;
3079 
3080 	rc = ena_com_mmio_reg_read_request_init(ena_dev);
3081 	if (rc) {
3082 		dev_err(dev, "Failed to init mmio read less\n");
3083 		return rc;
3084 	}
3085 
3086 	/* The PCIe configuration space revision id indicate if mmio reg
3087 	 * read is disabled
3088 	 */
3089 	readless_supported = !(pdev->revision & ENA_MMIO_DISABLE_REG_READ);
3090 	ena_com_set_mmio_read_mode(ena_dev, readless_supported);
3091 
3092 	rc = ena_com_dev_reset(ena_dev, ENA_REGS_RESET_NORMAL);
3093 	if (rc) {
3094 		dev_err(dev, "Can not reset device\n");
3095 		goto err_mmio_read_less;
3096 	}
3097 
3098 	rc = ena_com_validate_version(ena_dev);
3099 	if (rc) {
3100 		dev_err(dev, "Device version is too low\n");
3101 		goto err_mmio_read_less;
3102 	}
3103 
3104 	dma_width = ena_com_get_dma_width(ena_dev);
3105 	if (dma_width < 0) {
3106 		dev_err(dev, "Invalid dma width value %d", dma_width);
3107 		rc = dma_width;
3108 		goto err_mmio_read_less;
3109 	}
3110 
3111 	rc = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(dma_width));
3112 	if (rc) {
3113 		dev_err(dev, "dma_set_mask_and_coherent failed %d\n", rc);
3114 		goto err_mmio_read_less;
3115 	}
3116 
3117 	/* ENA admin level init */
3118 	rc = ena_com_admin_init(ena_dev, &aenq_handlers);
3119 	if (rc) {
3120 		dev_err(dev,
3121 			"Can not initialize ena admin queue with device\n");
3122 		goto err_mmio_read_less;
3123 	}
3124 
3125 	/* To enable the msix interrupts the driver needs to know the number
3126 	 * of queues. So the driver uses polling mode to retrieve this
3127 	 * information
3128 	 */
3129 	ena_com_set_admin_polling_mode(ena_dev, true);
3130 
3131 	ena_config_host_info(ena_dev, pdev);
3132 
3133 	/* Get Device Attributes*/
3134 	rc = ena_com_get_dev_attr_feat(ena_dev, get_feat_ctx);
3135 	if (rc) {
3136 		dev_err(dev, "Cannot get attribute for ena device rc=%d\n", rc);
3137 		goto err_admin_init;
3138 	}
3139 
3140 	/* Try to turn all the available aenq groups */
3141 	aenq_groups = BIT(ENA_ADMIN_LINK_CHANGE) |
3142 		BIT(ENA_ADMIN_FATAL_ERROR) |
3143 		BIT(ENA_ADMIN_WARNING) |
3144 		BIT(ENA_ADMIN_NOTIFICATION) |
3145 		BIT(ENA_ADMIN_KEEP_ALIVE);
3146 
3147 	aenq_groups &= get_feat_ctx->aenq.supported_groups;
3148 
3149 	rc = ena_com_set_aenq_config(ena_dev, aenq_groups);
3150 	if (rc) {
3151 		dev_err(dev, "Cannot configure aenq groups rc= %d\n", rc);
3152 		goto err_admin_init;
3153 	}
3154 
3155 	*wd_state = !!(aenq_groups & BIT(ENA_ADMIN_KEEP_ALIVE));
3156 
3157 	set_default_llq_configurations(adapter, &llq_config, &get_feat_ctx->llq);
3158 
3159 	rc = ena_set_queues_placement_policy(pdev, ena_dev, &get_feat_ctx->llq,
3160 					     &llq_config);
3161 	if (rc) {
3162 		dev_err(dev, "ENA device init failed\n");
3163 		goto err_admin_init;
3164 	}
3165 
3166 	ena_calc_io_queue_size(adapter, get_feat_ctx);
3167 
3168 	return 0;
3169 
3170 err_admin_init:
3171 	ena_com_delete_host_info(ena_dev);
3172 	ena_com_admin_destroy(ena_dev);
3173 err_mmio_read_less:
3174 	ena_com_mmio_reg_read_request_destroy(ena_dev);
3175 
3176 	return rc;
3177 }
3178 
3179 static int ena_enable_msix_and_set_admin_interrupts(struct ena_adapter *adapter)
3180 {
3181 	struct ena_com_dev *ena_dev = adapter->ena_dev;
3182 	struct device *dev = &adapter->pdev->dev;
3183 	int rc;
3184 
3185 	rc = ena_enable_msix(adapter);
3186 	if (rc) {
3187 		dev_err(dev, "Can not reserve msix vectors\n");
3188 		return rc;
3189 	}
3190 
3191 	ena_setup_mgmnt_intr(adapter);
3192 
3193 	rc = ena_request_mgmnt_irq(adapter);
3194 	if (rc) {
3195 		dev_err(dev, "Can not setup management interrupts\n");
3196 		goto err_disable_msix;
3197 	}
3198 
3199 	ena_com_set_admin_polling_mode(ena_dev, false);
3200 
3201 	ena_com_admin_aenq_enable(ena_dev);
3202 
3203 	return 0;
3204 
3205 err_disable_msix:
3206 	ena_disable_msix(adapter);
3207 
3208 	return rc;
3209 }
3210 
3211 static void ena_destroy_device(struct ena_adapter *adapter, bool graceful)
3212 {
3213 	struct net_device *netdev = adapter->netdev;
3214 	struct ena_com_dev *ena_dev = adapter->ena_dev;
3215 	bool dev_up;
3216 
3217 	if (!test_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags))
3218 		return;
3219 
3220 	netif_carrier_off(netdev);
3221 
3222 	del_timer_sync(&adapter->timer_service);
3223 
3224 	dev_up = test_bit(ENA_FLAG_DEV_UP, &adapter->flags);
3225 	adapter->dev_up_before_reset = dev_up;
3226 	if (!graceful)
3227 		ena_com_set_admin_running_state(ena_dev, false);
3228 
3229 	if (test_bit(ENA_FLAG_DEV_UP, &adapter->flags))
3230 		ena_down(adapter);
3231 
3232 	/* Stop the device from sending AENQ events (in case reset flag is set
3233 	 *  and device is up, ena_down() already reset the device.
3234 	 */
3235 	if (!(test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags) && dev_up))
3236 		ena_com_dev_reset(adapter->ena_dev, adapter->reset_reason);
3237 
3238 	ena_free_mgmnt_irq(adapter);
3239 
3240 	ena_disable_msix(adapter);
3241 
3242 	ena_com_abort_admin_commands(ena_dev);
3243 
3244 	ena_com_wait_for_abort_completion(ena_dev);
3245 
3246 	ena_com_admin_destroy(ena_dev);
3247 
3248 	ena_com_mmio_reg_read_request_destroy(ena_dev);
3249 
3250 	/* return reset reason to default value */
3251 	adapter->reset_reason = ENA_REGS_RESET_NORMAL;
3252 
3253 	clear_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags);
3254 	clear_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags);
3255 }
3256 
3257 static int ena_restore_device(struct ena_adapter *adapter)
3258 {
3259 	struct ena_com_dev_get_features_ctx get_feat_ctx;
3260 	struct ena_com_dev *ena_dev = adapter->ena_dev;
3261 	struct pci_dev *pdev = adapter->pdev;
3262 	struct ena_ring *txr;
3263 	int rc, count, i;
3264 	bool wd_state;
3265 
3266 	set_bit(ENA_FLAG_ONGOING_RESET, &adapter->flags);
3267 	rc = ena_device_init(adapter, adapter->pdev, &get_feat_ctx, &wd_state);
3268 	if (rc) {
3269 		dev_err(&pdev->dev, "Can not initialize device\n");
3270 		goto err;
3271 	}
3272 	adapter->wd_state = wd_state;
3273 
3274 	count =  adapter->xdp_num_queues + adapter->num_io_queues;
3275 	for (i = 0 ; i < count; i++) {
3276 		txr = &adapter->tx_ring[i];
3277 		txr->tx_mem_queue_type = ena_dev->tx_mem_queue_type;
3278 		txr->tx_max_header_size = ena_dev->tx_max_header_size;
3279 	}
3280 
3281 	rc = ena_device_validate_params(adapter, &get_feat_ctx);
3282 	if (rc) {
3283 		dev_err(&pdev->dev, "Validation of device parameters failed\n");
3284 		goto err_device_destroy;
3285 	}
3286 
3287 	rc = ena_enable_msix_and_set_admin_interrupts(adapter);
3288 	if (rc) {
3289 		dev_err(&pdev->dev, "Enable MSI-X failed\n");
3290 		goto err_device_destroy;
3291 	}
3292 	/* If the interface was up before the reset bring it up */
3293 	if (adapter->dev_up_before_reset) {
3294 		rc = ena_up(adapter);
3295 		if (rc) {
3296 			dev_err(&pdev->dev, "Failed to create I/O queues\n");
3297 			goto err_disable_msix;
3298 		}
3299 	}
3300 
3301 	set_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags);
3302 
3303 	clear_bit(ENA_FLAG_ONGOING_RESET, &adapter->flags);
3304 	if (test_bit(ENA_FLAG_LINK_UP, &adapter->flags))
3305 		netif_carrier_on(adapter->netdev);
3306 
3307 	mod_timer(&adapter->timer_service, round_jiffies(jiffies + HZ));
3308 	adapter->last_keep_alive_jiffies = jiffies;
3309 
3310 	return rc;
3311 err_disable_msix:
3312 	ena_free_mgmnt_irq(adapter);
3313 	ena_disable_msix(adapter);
3314 err_device_destroy:
3315 	ena_com_abort_admin_commands(ena_dev);
3316 	ena_com_wait_for_abort_completion(ena_dev);
3317 	ena_com_admin_destroy(ena_dev);
3318 	ena_com_dev_reset(ena_dev, ENA_REGS_RESET_DRIVER_INVALID_STATE);
3319 	ena_com_mmio_reg_read_request_destroy(ena_dev);
3320 err:
3321 	clear_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags);
3322 	clear_bit(ENA_FLAG_ONGOING_RESET, &adapter->flags);
3323 	dev_err(&pdev->dev,
3324 		"Reset attempt failed. Can not reset the device\n");
3325 
3326 	return rc;
3327 }
3328 
3329 static void ena_fw_reset_device(struct work_struct *work)
3330 {
3331 	struct ena_adapter *adapter =
3332 		container_of(work, struct ena_adapter, reset_task);
3333 
3334 	rtnl_lock();
3335 
3336 	if (likely(test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags))) {
3337 		ena_destroy_device(adapter, false);
3338 		ena_restore_device(adapter);
3339 
3340 		dev_err(&adapter->pdev->dev, "Device reset completed successfully\n");
3341 	}
3342 
3343 	rtnl_unlock();
3344 }
3345 
3346 static int check_for_rx_interrupt_queue(struct ena_adapter *adapter,
3347 					struct ena_ring *rx_ring)
3348 {
3349 	struct ena_napi *ena_napi = container_of(rx_ring->napi, struct ena_napi, napi);
3350 
3351 	if (likely(READ_ONCE(ena_napi->first_interrupt)))
3352 		return 0;
3353 
3354 	if (ena_com_cq_empty(rx_ring->ena_com_io_cq))
3355 		return 0;
3356 
3357 	rx_ring->no_interrupt_event_cnt++;
3358 
3359 	if (rx_ring->no_interrupt_event_cnt == ENA_MAX_NO_INTERRUPT_ITERATIONS) {
3360 		netif_err(adapter, rx_err, adapter->netdev,
3361 			  "Potential MSIX issue on Rx side Queue = %d. Reset the device\n",
3362 			  rx_ring->qid);
3363 
3364 		ena_reset_device(adapter, ENA_REGS_RESET_MISS_INTERRUPT);
3365 		return -EIO;
3366 	}
3367 
3368 	return 0;
3369 }
3370 
3371 static int check_missing_comp_in_tx_queue(struct ena_adapter *adapter,
3372 					  struct ena_ring *tx_ring)
3373 {
3374 	struct ena_napi *ena_napi = container_of(tx_ring->napi, struct ena_napi, napi);
3375 	unsigned int time_since_last_napi;
3376 	unsigned int missing_tx_comp_to;
3377 	bool is_tx_comp_time_expired;
3378 	struct ena_tx_buffer *tx_buf;
3379 	unsigned long last_jiffies;
3380 	u32 missed_tx = 0;
3381 	int i, rc = 0;
3382 
3383 	for (i = 0; i < tx_ring->ring_size; i++) {
3384 		tx_buf = &tx_ring->tx_buffer_info[i];
3385 		last_jiffies = tx_buf->last_jiffies;
3386 
3387 		if (last_jiffies == 0)
3388 			/* no pending Tx at this location */
3389 			continue;
3390 
3391 		is_tx_comp_time_expired = time_is_before_jiffies(last_jiffies +
3392 			 2 * adapter->missing_tx_completion_to);
3393 
3394 		if (unlikely(!READ_ONCE(ena_napi->first_interrupt) && is_tx_comp_time_expired)) {
3395 			/* If after graceful period interrupt is still not
3396 			 * received, we schedule a reset
3397 			 */
3398 			netif_err(adapter, tx_err, adapter->netdev,
3399 				  "Potential MSIX issue on Tx side Queue = %d. Reset the device\n",
3400 				  tx_ring->qid);
3401 			ena_reset_device(adapter, ENA_REGS_RESET_MISS_INTERRUPT);
3402 			return -EIO;
3403 		}
3404 
3405 		is_tx_comp_time_expired = time_is_before_jiffies(last_jiffies +
3406 			adapter->missing_tx_completion_to);
3407 
3408 		if (unlikely(is_tx_comp_time_expired)) {
3409 			if (!tx_buf->print_once) {
3410 				time_since_last_napi = jiffies_to_usecs(jiffies - tx_ring->tx_stats.last_napi_jiffies);
3411 				missing_tx_comp_to = jiffies_to_msecs(adapter->missing_tx_completion_to);
3412 				netif_notice(adapter, tx_err, adapter->netdev,
3413 					     "Found a Tx that wasn't completed on time, qid %d, index %d. %u usecs have passed since last napi execution. Missing Tx timeout value %u msecs\n",
3414 					     tx_ring->qid, i, time_since_last_napi, missing_tx_comp_to);
3415 			}
3416 
3417 			tx_buf->print_once = 1;
3418 			missed_tx++;
3419 		}
3420 	}
3421 
3422 	if (unlikely(missed_tx > adapter->missing_tx_completion_threshold)) {
3423 		netif_err(adapter, tx_err, adapter->netdev,
3424 			  "The number of lost tx completions is above the threshold (%d > %d). Reset the device\n",
3425 			  missed_tx,
3426 			  adapter->missing_tx_completion_threshold);
3427 		ena_reset_device(adapter, ENA_REGS_RESET_MISS_TX_CMPL);
3428 		rc = -EIO;
3429 	}
3430 
3431 	ena_increase_stat(&tx_ring->tx_stats.missed_tx, missed_tx,
3432 			  &tx_ring->syncp);
3433 
3434 	return rc;
3435 }
3436 
3437 static void check_for_missing_completions(struct ena_adapter *adapter)
3438 {
3439 	struct ena_ring *tx_ring;
3440 	struct ena_ring *rx_ring;
3441 	int i, budget, rc;
3442 	int io_queue_count;
3443 
3444 	io_queue_count = adapter->xdp_num_queues + adapter->num_io_queues;
3445 	/* Make sure the driver doesn't turn the device in other process */
3446 	smp_rmb();
3447 
3448 	if (!test_bit(ENA_FLAG_DEV_UP, &adapter->flags))
3449 		return;
3450 
3451 	if (test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags))
3452 		return;
3453 
3454 	if (adapter->missing_tx_completion_to == ENA_HW_HINTS_NO_TIMEOUT)
3455 		return;
3456 
3457 	budget = ENA_MONITORED_TX_QUEUES;
3458 
3459 	for (i = adapter->last_monitored_tx_qid; i < io_queue_count; i++) {
3460 		tx_ring = &adapter->tx_ring[i];
3461 		rx_ring = &adapter->rx_ring[i];
3462 
3463 		rc = check_missing_comp_in_tx_queue(adapter, tx_ring);
3464 		if (unlikely(rc))
3465 			return;
3466 
3467 		rc =  !ENA_IS_XDP_INDEX(adapter, i) ?
3468 			check_for_rx_interrupt_queue(adapter, rx_ring) : 0;
3469 		if (unlikely(rc))
3470 			return;
3471 
3472 		budget--;
3473 		if (!budget)
3474 			break;
3475 	}
3476 
3477 	adapter->last_monitored_tx_qid = i % io_queue_count;
3478 }
3479 
3480 /* trigger napi schedule after 2 consecutive detections */
3481 #define EMPTY_RX_REFILL 2
3482 /* For the rare case where the device runs out of Rx descriptors and the
3483  * napi handler failed to refill new Rx descriptors (due to a lack of memory
3484  * for example).
3485  * This case will lead to a deadlock:
3486  * The device won't send interrupts since all the new Rx packets will be dropped
3487  * The napi handler won't allocate new Rx descriptors so the device will be
3488  * able to send new packets.
3489  *
3490  * This scenario can happen when the kernel's vm.min_free_kbytes is too small.
3491  * It is recommended to have at least 512MB, with a minimum of 128MB for
3492  * constrained environment).
3493  *
3494  * When such a situation is detected - Reschedule napi
3495  */
3496 static void check_for_empty_rx_ring(struct ena_adapter *adapter)
3497 {
3498 	struct ena_ring *rx_ring;
3499 	int i, refill_required;
3500 
3501 	if (!test_bit(ENA_FLAG_DEV_UP, &adapter->flags))
3502 		return;
3503 
3504 	if (test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags))
3505 		return;
3506 
3507 	for (i = 0; i < adapter->num_io_queues; i++) {
3508 		rx_ring = &adapter->rx_ring[i];
3509 
3510 		refill_required = ena_com_free_q_entries(rx_ring->ena_com_io_sq);
3511 		if (unlikely(refill_required == (rx_ring->ring_size - 1))) {
3512 			rx_ring->empty_rx_queue++;
3513 
3514 			if (rx_ring->empty_rx_queue >= EMPTY_RX_REFILL) {
3515 				ena_increase_stat(&rx_ring->rx_stats.empty_rx_ring, 1,
3516 						  &rx_ring->syncp);
3517 
3518 				netif_err(adapter, drv, adapter->netdev,
3519 					  "Trigger refill for ring %d\n", i);
3520 
3521 				napi_schedule(rx_ring->napi);
3522 				rx_ring->empty_rx_queue = 0;
3523 			}
3524 		} else {
3525 			rx_ring->empty_rx_queue = 0;
3526 		}
3527 	}
3528 }
3529 
3530 /* Check for keep alive expiration */
3531 static void check_for_missing_keep_alive(struct ena_adapter *adapter)
3532 {
3533 	unsigned long keep_alive_expired;
3534 
3535 	if (!adapter->wd_state)
3536 		return;
3537 
3538 	if (adapter->keep_alive_timeout == ENA_HW_HINTS_NO_TIMEOUT)
3539 		return;
3540 
3541 	keep_alive_expired = adapter->last_keep_alive_jiffies +
3542 			     adapter->keep_alive_timeout;
3543 	if (unlikely(time_is_before_jiffies(keep_alive_expired))) {
3544 		netif_err(adapter, drv, adapter->netdev,
3545 			  "Keep alive watchdog timeout.\n");
3546 		ena_increase_stat(&adapter->dev_stats.wd_expired, 1,
3547 				  &adapter->syncp);
3548 		ena_reset_device(adapter, ENA_REGS_RESET_KEEP_ALIVE_TO);
3549 	}
3550 }
3551 
3552 static void check_for_admin_com_state(struct ena_adapter *adapter)
3553 {
3554 	if (unlikely(!ena_com_get_admin_running_state(adapter->ena_dev))) {
3555 		netif_err(adapter, drv, adapter->netdev,
3556 			  "ENA admin queue is not in running state!\n");
3557 		ena_increase_stat(&adapter->dev_stats.admin_q_pause, 1,
3558 				  &adapter->syncp);
3559 		ena_reset_device(adapter, ENA_REGS_RESET_ADMIN_TO);
3560 	}
3561 }
3562 
3563 static void ena_update_hints(struct ena_adapter *adapter,
3564 			     struct ena_admin_ena_hw_hints *hints)
3565 {
3566 	struct net_device *netdev = adapter->netdev;
3567 
3568 	if (hints->admin_completion_tx_timeout)
3569 		adapter->ena_dev->admin_queue.completion_timeout =
3570 			hints->admin_completion_tx_timeout * 1000;
3571 
3572 	if (hints->mmio_read_timeout)
3573 		/* convert to usec */
3574 		adapter->ena_dev->mmio_read.reg_read_to =
3575 			hints->mmio_read_timeout * 1000;
3576 
3577 	if (hints->missed_tx_completion_count_threshold_to_reset)
3578 		adapter->missing_tx_completion_threshold =
3579 			hints->missed_tx_completion_count_threshold_to_reset;
3580 
3581 	if (hints->missing_tx_completion_timeout) {
3582 		if (hints->missing_tx_completion_timeout == ENA_HW_HINTS_NO_TIMEOUT)
3583 			adapter->missing_tx_completion_to = ENA_HW_HINTS_NO_TIMEOUT;
3584 		else
3585 			adapter->missing_tx_completion_to =
3586 				msecs_to_jiffies(hints->missing_tx_completion_timeout);
3587 	}
3588 
3589 	if (hints->netdev_wd_timeout)
3590 		netdev->watchdog_timeo = msecs_to_jiffies(hints->netdev_wd_timeout);
3591 
3592 	if (hints->driver_watchdog_timeout) {
3593 		if (hints->driver_watchdog_timeout == ENA_HW_HINTS_NO_TIMEOUT)
3594 			adapter->keep_alive_timeout = ENA_HW_HINTS_NO_TIMEOUT;
3595 		else
3596 			adapter->keep_alive_timeout =
3597 				msecs_to_jiffies(hints->driver_watchdog_timeout);
3598 	}
3599 }
3600 
3601 static void ena_update_host_info(struct ena_admin_host_info *host_info,
3602 				 struct net_device *netdev)
3603 {
3604 	host_info->supported_network_features[0] =
3605 		netdev->features & GENMASK_ULL(31, 0);
3606 	host_info->supported_network_features[1] =
3607 		(netdev->features & GENMASK_ULL(63, 32)) >> 32;
3608 }
3609 
3610 static void ena_timer_service(struct timer_list *t)
3611 {
3612 	struct ena_adapter *adapter = from_timer(adapter, t, timer_service);
3613 	u8 *debug_area = adapter->ena_dev->host_attr.debug_area_virt_addr;
3614 	struct ena_admin_host_info *host_info =
3615 		adapter->ena_dev->host_attr.host_info;
3616 
3617 	check_for_missing_keep_alive(adapter);
3618 
3619 	check_for_admin_com_state(adapter);
3620 
3621 	check_for_missing_completions(adapter);
3622 
3623 	check_for_empty_rx_ring(adapter);
3624 
3625 	if (debug_area)
3626 		ena_dump_stats_to_buf(adapter, debug_area);
3627 
3628 	if (host_info)
3629 		ena_update_host_info(host_info, adapter->netdev);
3630 
3631 	if (unlikely(test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags))) {
3632 		netif_err(adapter, drv, adapter->netdev,
3633 			  "Trigger reset is on\n");
3634 		ena_dump_stats_to_dmesg(adapter);
3635 		queue_work(ena_wq, &adapter->reset_task);
3636 		return;
3637 	}
3638 
3639 	/* Reset the timer */
3640 	mod_timer(&adapter->timer_service, round_jiffies(jiffies + HZ));
3641 }
3642 
3643 static u32 ena_calc_max_io_queue_num(struct pci_dev *pdev,
3644 				     struct ena_com_dev *ena_dev,
3645 				     struct ena_com_dev_get_features_ctx *get_feat_ctx)
3646 {
3647 	u32 io_tx_sq_num, io_tx_cq_num, io_rx_num, max_num_io_queues;
3648 
3649 	if (ena_dev->supported_features & BIT(ENA_ADMIN_MAX_QUEUES_EXT)) {
3650 		struct ena_admin_queue_ext_feature_fields *max_queue_ext =
3651 			&get_feat_ctx->max_queue_ext.max_queue_ext;
3652 		io_rx_num = min_t(u32, max_queue_ext->max_rx_sq_num,
3653 				  max_queue_ext->max_rx_cq_num);
3654 
3655 		io_tx_sq_num = max_queue_ext->max_tx_sq_num;
3656 		io_tx_cq_num = max_queue_ext->max_tx_cq_num;
3657 	} else {
3658 		struct ena_admin_queue_feature_desc *max_queues =
3659 			&get_feat_ctx->max_queues;
3660 		io_tx_sq_num = max_queues->max_sq_num;
3661 		io_tx_cq_num = max_queues->max_cq_num;
3662 		io_rx_num = min_t(u32, io_tx_sq_num, io_tx_cq_num);
3663 	}
3664 
3665 	/* In case of LLQ use the llq fields for the tx SQ/CQ */
3666 	if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV)
3667 		io_tx_sq_num = get_feat_ctx->llq.max_llq_num;
3668 
3669 	max_num_io_queues = min_t(u32, num_online_cpus(), ENA_MAX_NUM_IO_QUEUES);
3670 	max_num_io_queues = min_t(u32, max_num_io_queues, io_rx_num);
3671 	max_num_io_queues = min_t(u32, max_num_io_queues, io_tx_sq_num);
3672 	max_num_io_queues = min_t(u32, max_num_io_queues, io_tx_cq_num);
3673 	/* 1 IRQ for mgmnt and 1 IRQs for each IO direction */
3674 	max_num_io_queues = min_t(u32, max_num_io_queues, pci_msix_vec_count(pdev) - 1);
3675 
3676 	return max_num_io_queues;
3677 }
3678 
3679 static void ena_set_dev_offloads(struct ena_com_dev_get_features_ctx *feat,
3680 				 struct net_device *netdev)
3681 {
3682 	netdev_features_t dev_features = 0;
3683 
3684 	/* Set offload features */
3685 	if (feat->offload.tx &
3686 		ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_PART_MASK)
3687 		dev_features |= NETIF_F_IP_CSUM;
3688 
3689 	if (feat->offload.tx &
3690 		ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV6_CSUM_PART_MASK)
3691 		dev_features |= NETIF_F_IPV6_CSUM;
3692 
3693 	if (feat->offload.tx & ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV4_MASK)
3694 		dev_features |= NETIF_F_TSO;
3695 
3696 	if (feat->offload.tx & ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV6_MASK)
3697 		dev_features |= NETIF_F_TSO6;
3698 
3699 	if (feat->offload.tx & ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_ECN_MASK)
3700 		dev_features |= NETIF_F_TSO_ECN;
3701 
3702 	if (feat->offload.rx_supported &
3703 		ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV4_CSUM_MASK)
3704 		dev_features |= NETIF_F_RXCSUM;
3705 
3706 	if (feat->offload.rx_supported &
3707 		ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV6_CSUM_MASK)
3708 		dev_features |= NETIF_F_RXCSUM;
3709 
3710 	netdev->features =
3711 		dev_features |
3712 		NETIF_F_SG |
3713 		NETIF_F_RXHASH |
3714 		NETIF_F_HIGHDMA;
3715 
3716 	netdev->hw_features |= netdev->features;
3717 	netdev->vlan_features |= netdev->features;
3718 }
3719 
3720 static void ena_set_conf_feat_params(struct ena_adapter *adapter,
3721 				     struct ena_com_dev_get_features_ctx *feat)
3722 {
3723 	struct net_device *netdev = adapter->netdev;
3724 
3725 	/* Copy mac address */
3726 	if (!is_valid_ether_addr(feat->dev_attr.mac_addr)) {
3727 		eth_hw_addr_random(netdev);
3728 		ether_addr_copy(adapter->mac_addr, netdev->dev_addr);
3729 	} else {
3730 		ether_addr_copy(adapter->mac_addr, feat->dev_attr.mac_addr);
3731 		eth_hw_addr_set(netdev, adapter->mac_addr);
3732 	}
3733 
3734 	/* Set offload features */
3735 	ena_set_dev_offloads(feat, netdev);
3736 
3737 	adapter->max_mtu = feat->dev_attr.max_mtu;
3738 	netdev->max_mtu = adapter->max_mtu;
3739 	netdev->min_mtu = ENA_MIN_MTU;
3740 }
3741 
3742 static int ena_rss_init_default(struct ena_adapter *adapter)
3743 {
3744 	struct ena_com_dev *ena_dev = adapter->ena_dev;
3745 	struct device *dev = &adapter->pdev->dev;
3746 	int rc, i;
3747 	u32 val;
3748 
3749 	rc = ena_com_rss_init(ena_dev, ENA_RX_RSS_TABLE_LOG_SIZE);
3750 	if (unlikely(rc)) {
3751 		dev_err(dev, "Cannot init indirect table\n");
3752 		goto err_rss_init;
3753 	}
3754 
3755 	for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++) {
3756 		val = ethtool_rxfh_indir_default(i, adapter->num_io_queues);
3757 		rc = ena_com_indirect_table_fill_entry(ena_dev, i,
3758 						       ENA_IO_RXQ_IDX(val));
3759 		if (unlikely(rc)) {
3760 			dev_err(dev, "Cannot fill indirect table\n");
3761 			goto err_fill_indir;
3762 		}
3763 	}
3764 
3765 	rc = ena_com_fill_hash_function(ena_dev, ENA_ADMIN_TOEPLITZ, NULL,
3766 					ENA_HASH_KEY_SIZE, 0xFFFFFFFF);
3767 	if (unlikely(rc && (rc != -EOPNOTSUPP))) {
3768 		dev_err(dev, "Cannot fill hash function\n");
3769 		goto err_fill_indir;
3770 	}
3771 
3772 	rc = ena_com_set_default_hash_ctrl(ena_dev);
3773 	if (unlikely(rc && (rc != -EOPNOTSUPP))) {
3774 		dev_err(dev, "Cannot fill hash control\n");
3775 		goto err_fill_indir;
3776 	}
3777 
3778 	return 0;
3779 
3780 err_fill_indir:
3781 	ena_com_rss_destroy(ena_dev);
3782 err_rss_init:
3783 
3784 	return rc;
3785 }
3786 
3787 static void ena_release_bars(struct ena_com_dev *ena_dev, struct pci_dev *pdev)
3788 {
3789 	int release_bars = pci_select_bars(pdev, IORESOURCE_MEM) & ENA_BAR_MASK;
3790 
3791 	pci_release_selected_regions(pdev, release_bars);
3792 }
3793 
3794 /* ena_probe - Device Initialization Routine
3795  * @pdev: PCI device information struct
3796  * @ent: entry in ena_pci_tbl
3797  *
3798  * Returns 0 on success, negative on failure
3799  *
3800  * ena_probe initializes an adapter identified by a pci_dev structure.
3801  * The OS initialization, configuring of the adapter private structure,
3802  * and a hardware reset occur.
3803  */
3804 static int ena_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
3805 {
3806 	struct ena_com_dev_get_features_ctx get_feat_ctx;
3807 	struct ena_com_dev *ena_dev = NULL;
3808 	struct ena_adapter *adapter;
3809 	struct net_device *netdev;
3810 	static int adapters_found;
3811 	u32 max_num_io_queues;
3812 	bool wd_state;
3813 	int bars, rc;
3814 
3815 	dev_dbg(&pdev->dev, "%s\n", __func__);
3816 
3817 	rc = pci_enable_device_mem(pdev);
3818 	if (rc) {
3819 		dev_err(&pdev->dev, "pci_enable_device_mem() failed!\n");
3820 		return rc;
3821 	}
3822 
3823 	rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(ENA_MAX_PHYS_ADDR_SIZE_BITS));
3824 	if (rc) {
3825 		dev_err(&pdev->dev, "dma_set_mask_and_coherent failed %d\n", rc);
3826 		goto err_disable_device;
3827 	}
3828 
3829 	pci_set_master(pdev);
3830 
3831 	ena_dev = vzalloc(sizeof(*ena_dev));
3832 	if (!ena_dev) {
3833 		rc = -ENOMEM;
3834 		goto err_disable_device;
3835 	}
3836 
3837 	bars = pci_select_bars(pdev, IORESOURCE_MEM) & ENA_BAR_MASK;
3838 	rc = pci_request_selected_regions(pdev, bars, DRV_MODULE_NAME);
3839 	if (rc) {
3840 		dev_err(&pdev->dev, "pci_request_selected_regions failed %d\n",
3841 			rc);
3842 		goto err_free_ena_dev;
3843 	}
3844 
3845 	ena_dev->reg_bar = devm_ioremap(&pdev->dev,
3846 					pci_resource_start(pdev, ENA_REG_BAR),
3847 					pci_resource_len(pdev, ENA_REG_BAR));
3848 	if (!ena_dev->reg_bar) {
3849 		dev_err(&pdev->dev, "Failed to remap regs bar\n");
3850 		rc = -EFAULT;
3851 		goto err_free_region;
3852 	}
3853 
3854 	ena_dev->ena_min_poll_delay_us = ENA_ADMIN_POLL_DELAY_US;
3855 
3856 	ena_dev->dmadev = &pdev->dev;
3857 
3858 	netdev = alloc_etherdev_mq(sizeof(struct ena_adapter), ENA_MAX_RINGS);
3859 	if (!netdev) {
3860 		dev_err(&pdev->dev, "alloc_etherdev_mq failed\n");
3861 		rc = -ENOMEM;
3862 		goto err_free_region;
3863 	}
3864 
3865 	SET_NETDEV_DEV(netdev, &pdev->dev);
3866 	adapter = netdev_priv(netdev);
3867 	adapter->ena_dev = ena_dev;
3868 	adapter->netdev = netdev;
3869 	adapter->pdev = pdev;
3870 	adapter->msg_enable = DEFAULT_MSG_ENABLE;
3871 
3872 	ena_dev->net_device = netdev;
3873 
3874 	pci_set_drvdata(pdev, adapter);
3875 
3876 	rc = ena_map_llq_mem_bar(pdev, ena_dev, bars);
3877 	if (rc) {
3878 		dev_err(&pdev->dev, "ENA LLQ bar mapping failed\n");
3879 		goto err_netdev_destroy;
3880 	}
3881 
3882 	rc = ena_device_init(adapter, pdev, &get_feat_ctx, &wd_state);
3883 	if (rc) {
3884 		dev_err(&pdev->dev, "ENA device init failed\n");
3885 		if (rc == -ETIME)
3886 			rc = -EPROBE_DEFER;
3887 		goto err_netdev_destroy;
3888 	}
3889 
3890 	/* Initial TX and RX interrupt delay. Assumes 1 usec granularity.
3891 	 * Updated during device initialization with the real granularity
3892 	 */
3893 	ena_dev->intr_moder_tx_interval = ENA_INTR_INITIAL_TX_INTERVAL_USECS;
3894 	ena_dev->intr_moder_rx_interval = ENA_INTR_INITIAL_RX_INTERVAL_USECS;
3895 	ena_dev->intr_delay_resolution = ENA_DEFAULT_INTR_DELAY_RESOLUTION;
3896 	max_num_io_queues = ena_calc_max_io_queue_num(pdev, ena_dev, &get_feat_ctx);
3897 	if (unlikely(!max_num_io_queues)) {
3898 		rc = -EFAULT;
3899 		goto err_device_destroy;
3900 	}
3901 
3902 	ena_set_conf_feat_params(adapter, &get_feat_ctx);
3903 
3904 	adapter->reset_reason = ENA_REGS_RESET_NORMAL;
3905 
3906 	adapter->num_io_queues = max_num_io_queues;
3907 	adapter->max_num_io_queues = max_num_io_queues;
3908 	adapter->last_monitored_tx_qid = 0;
3909 
3910 	adapter->xdp_first_ring = 0;
3911 	adapter->xdp_num_queues = 0;
3912 
3913 	adapter->rx_copybreak = ENA_DEFAULT_RX_COPYBREAK;
3914 	if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV)
3915 		adapter->disable_meta_caching =
3916 			!!(get_feat_ctx.llq.accel_mode.u.get.supported_flags &
3917 			   BIT(ENA_ADMIN_DISABLE_META_CACHING));
3918 
3919 	adapter->wd_state = wd_state;
3920 
3921 	snprintf(adapter->name, ENA_NAME_MAX_LEN, "ena_%d", adapters_found);
3922 
3923 	rc = ena_com_init_interrupt_moderation(adapter->ena_dev);
3924 	if (rc) {
3925 		dev_err(&pdev->dev,
3926 			"Failed to query interrupt moderation feature\n");
3927 		goto err_device_destroy;
3928 	}
3929 
3930 	ena_init_io_rings(adapter,
3931 			  0,
3932 			  adapter->xdp_num_queues +
3933 			  adapter->num_io_queues);
3934 
3935 	netdev->netdev_ops = &ena_netdev_ops;
3936 	netdev->watchdog_timeo = TX_TIMEOUT;
3937 	ena_set_ethtool_ops(netdev);
3938 
3939 	netdev->priv_flags |= IFF_UNICAST_FLT;
3940 
3941 	u64_stats_init(&adapter->syncp);
3942 
3943 	rc = ena_enable_msix_and_set_admin_interrupts(adapter);
3944 	if (rc) {
3945 		dev_err(&pdev->dev,
3946 			"Failed to enable and set the admin interrupts\n");
3947 		goto err_worker_destroy;
3948 	}
3949 	rc = ena_rss_init_default(adapter);
3950 	if (rc && (rc != -EOPNOTSUPP)) {
3951 		dev_err(&pdev->dev, "Cannot init RSS rc: %d\n", rc);
3952 		goto err_free_msix;
3953 	}
3954 
3955 	ena_config_debug_area(adapter);
3956 
3957 	if (ena_xdp_legal_queue_count(adapter, adapter->num_io_queues))
3958 		netdev->xdp_features = NETDEV_XDP_ACT_BASIC |
3959 				       NETDEV_XDP_ACT_REDIRECT;
3960 
3961 	memcpy(adapter->netdev->perm_addr, adapter->mac_addr, netdev->addr_len);
3962 
3963 	netif_carrier_off(netdev);
3964 
3965 	rc = register_netdev(netdev);
3966 	if (rc) {
3967 		dev_err(&pdev->dev, "Cannot register net device\n");
3968 		goto err_rss;
3969 	}
3970 
3971 	INIT_WORK(&adapter->reset_task, ena_fw_reset_device);
3972 
3973 	adapter->last_keep_alive_jiffies = jiffies;
3974 	adapter->keep_alive_timeout = ENA_DEVICE_KALIVE_TIMEOUT;
3975 	adapter->missing_tx_completion_to = TX_TIMEOUT;
3976 	adapter->missing_tx_completion_threshold = MAX_NUM_OF_TIMEOUTED_PACKETS;
3977 
3978 	ena_update_hints(adapter, &get_feat_ctx.hw_hints);
3979 
3980 	timer_setup(&adapter->timer_service, ena_timer_service, 0);
3981 	mod_timer(&adapter->timer_service, round_jiffies(jiffies + HZ));
3982 
3983 	dev_info(&pdev->dev,
3984 		 "%s found at mem %lx, mac addr %pM\n",
3985 		 DEVICE_NAME, (long)pci_resource_start(pdev, 0),
3986 		 netdev->dev_addr);
3987 
3988 	set_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags);
3989 
3990 	adapters_found++;
3991 
3992 	return 0;
3993 
3994 err_rss:
3995 	ena_com_delete_debug_area(ena_dev);
3996 	ena_com_rss_destroy(ena_dev);
3997 err_free_msix:
3998 	ena_com_dev_reset(ena_dev, ENA_REGS_RESET_INIT_ERR);
3999 	/* stop submitting admin commands on a device that was reset */
4000 	ena_com_set_admin_running_state(ena_dev, false);
4001 	ena_free_mgmnt_irq(adapter);
4002 	ena_disable_msix(adapter);
4003 err_worker_destroy:
4004 	del_timer(&adapter->timer_service);
4005 err_device_destroy:
4006 	ena_com_delete_host_info(ena_dev);
4007 	ena_com_admin_destroy(ena_dev);
4008 err_netdev_destroy:
4009 	free_netdev(netdev);
4010 err_free_region:
4011 	ena_release_bars(ena_dev, pdev);
4012 err_free_ena_dev:
4013 	vfree(ena_dev);
4014 err_disable_device:
4015 	pci_disable_device(pdev);
4016 	return rc;
4017 }
4018 
4019 /*****************************************************************************/
4020 
4021 /* __ena_shutoff - Helper used in both PCI remove/shutdown routines
4022  * @pdev: PCI device information struct
4023  * @shutdown: Is it a shutdown operation? If false, means it is a removal
4024  *
4025  * __ena_shutoff is a helper routine that does the real work on shutdown and
4026  * removal paths; the difference between those paths is with regards to whether
4027  * dettach or unregister the netdevice.
4028  */
4029 static void __ena_shutoff(struct pci_dev *pdev, bool shutdown)
4030 {
4031 	struct ena_adapter *adapter = pci_get_drvdata(pdev);
4032 	struct ena_com_dev *ena_dev;
4033 	struct net_device *netdev;
4034 
4035 	ena_dev = adapter->ena_dev;
4036 	netdev = adapter->netdev;
4037 
4038 #ifdef CONFIG_RFS_ACCEL
4039 	if ((adapter->msix_vecs >= 1) && (netdev->rx_cpu_rmap)) {
4040 		free_irq_cpu_rmap(netdev->rx_cpu_rmap);
4041 		netdev->rx_cpu_rmap = NULL;
4042 	}
4043 #endif /* CONFIG_RFS_ACCEL */
4044 
4045 	/* Make sure timer and reset routine won't be called after
4046 	 * freeing device resources.
4047 	 */
4048 	del_timer_sync(&adapter->timer_service);
4049 	cancel_work_sync(&adapter->reset_task);
4050 
4051 	rtnl_lock(); /* lock released inside the below if-else block */
4052 	adapter->reset_reason = ENA_REGS_RESET_SHUTDOWN;
4053 	ena_destroy_device(adapter, true);
4054 
4055 	if (shutdown) {
4056 		netif_device_detach(netdev);
4057 		dev_close(netdev);
4058 		rtnl_unlock();
4059 	} else {
4060 		rtnl_unlock();
4061 		unregister_netdev(netdev);
4062 		free_netdev(netdev);
4063 	}
4064 
4065 	ena_com_rss_destroy(ena_dev);
4066 
4067 	ena_com_delete_debug_area(ena_dev);
4068 
4069 	ena_com_delete_host_info(ena_dev);
4070 
4071 	ena_release_bars(ena_dev, pdev);
4072 
4073 	pci_disable_device(pdev);
4074 
4075 	vfree(ena_dev);
4076 }
4077 
4078 /* ena_remove - Device Removal Routine
4079  * @pdev: PCI device information struct
4080  *
4081  * ena_remove is called by the PCI subsystem to alert the driver
4082  * that it should release a PCI device.
4083  */
4084 
4085 static void ena_remove(struct pci_dev *pdev)
4086 {
4087 	__ena_shutoff(pdev, false);
4088 }
4089 
4090 /* ena_shutdown - Device Shutdown Routine
4091  * @pdev: PCI device information struct
4092  *
4093  * ena_shutdown is called by the PCI subsystem to alert the driver that
4094  * a shutdown/reboot (or kexec) is happening and device must be disabled.
4095  */
4096 
4097 static void ena_shutdown(struct pci_dev *pdev)
4098 {
4099 	__ena_shutoff(pdev, true);
4100 }
4101 
4102 /* ena_suspend - PM suspend callback
4103  * @dev_d: Device information struct
4104  */
4105 static int __maybe_unused ena_suspend(struct device *dev_d)
4106 {
4107 	struct pci_dev *pdev = to_pci_dev(dev_d);
4108 	struct ena_adapter *adapter = pci_get_drvdata(pdev);
4109 
4110 	ena_increase_stat(&adapter->dev_stats.suspend, 1, &adapter->syncp);
4111 
4112 	rtnl_lock();
4113 	if (unlikely(test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags))) {
4114 		dev_err(&pdev->dev,
4115 			"Ignoring device reset request as the device is being suspended\n");
4116 		clear_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags);
4117 	}
4118 	ena_destroy_device(adapter, true);
4119 	rtnl_unlock();
4120 	return 0;
4121 }
4122 
4123 /* ena_resume - PM resume callback
4124  * @dev_d: Device information struct
4125  */
4126 static int __maybe_unused ena_resume(struct device *dev_d)
4127 {
4128 	struct ena_adapter *adapter = dev_get_drvdata(dev_d);
4129 	int rc;
4130 
4131 	ena_increase_stat(&adapter->dev_stats.resume, 1, &adapter->syncp);
4132 
4133 	rtnl_lock();
4134 	rc = ena_restore_device(adapter);
4135 	rtnl_unlock();
4136 	return rc;
4137 }
4138 
4139 static SIMPLE_DEV_PM_OPS(ena_pm_ops, ena_suspend, ena_resume);
4140 
4141 static struct pci_driver ena_pci_driver = {
4142 	.name		= DRV_MODULE_NAME,
4143 	.id_table	= ena_pci_tbl,
4144 	.probe		= ena_probe,
4145 	.remove		= ena_remove,
4146 	.shutdown	= ena_shutdown,
4147 	.driver.pm	= &ena_pm_ops,
4148 	.sriov_configure = pci_sriov_configure_simple,
4149 };
4150 
4151 static int __init ena_init(void)
4152 {
4153 	int ret;
4154 
4155 	ena_wq = create_singlethread_workqueue(DRV_MODULE_NAME);
4156 	if (!ena_wq) {
4157 		pr_err("Failed to create workqueue\n");
4158 		return -ENOMEM;
4159 	}
4160 
4161 	ret = pci_register_driver(&ena_pci_driver);
4162 	if (ret)
4163 		destroy_workqueue(ena_wq);
4164 
4165 	return ret;
4166 }
4167 
4168 static void __exit ena_cleanup(void)
4169 {
4170 	pci_unregister_driver(&ena_pci_driver);
4171 
4172 	if (ena_wq) {
4173 		destroy_workqueue(ena_wq);
4174 		ena_wq = NULL;
4175 	}
4176 }
4177 
4178 /******************************************************************************
4179  ******************************** AENQ Handlers *******************************
4180  *****************************************************************************/
4181 /* ena_update_on_link_change:
4182  * Notify the network interface about the change in link status
4183  */
4184 static void ena_update_on_link_change(void *adapter_data,
4185 				      struct ena_admin_aenq_entry *aenq_e)
4186 {
4187 	struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
4188 	struct ena_admin_aenq_link_change_desc *aenq_desc =
4189 		(struct ena_admin_aenq_link_change_desc *)aenq_e;
4190 	int status = aenq_desc->flags &
4191 		ENA_ADMIN_AENQ_LINK_CHANGE_DESC_LINK_STATUS_MASK;
4192 
4193 	if (status) {
4194 		netif_dbg(adapter, ifup, adapter->netdev, "%s\n", __func__);
4195 		set_bit(ENA_FLAG_LINK_UP, &adapter->flags);
4196 		if (!test_bit(ENA_FLAG_ONGOING_RESET, &adapter->flags))
4197 			netif_carrier_on(adapter->netdev);
4198 	} else {
4199 		clear_bit(ENA_FLAG_LINK_UP, &adapter->flags);
4200 		netif_carrier_off(adapter->netdev);
4201 	}
4202 }
4203 
4204 static void ena_keep_alive_wd(void *adapter_data,
4205 			      struct ena_admin_aenq_entry *aenq_e)
4206 {
4207 	struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
4208 	struct ena_admin_aenq_keep_alive_desc *desc;
4209 	u64 rx_drops;
4210 	u64 tx_drops;
4211 
4212 	desc = (struct ena_admin_aenq_keep_alive_desc *)aenq_e;
4213 	adapter->last_keep_alive_jiffies = jiffies;
4214 
4215 	rx_drops = ((u64)desc->rx_drops_high << 32) | desc->rx_drops_low;
4216 	tx_drops = ((u64)desc->tx_drops_high << 32) | desc->tx_drops_low;
4217 
4218 	u64_stats_update_begin(&adapter->syncp);
4219 	/* These stats are accumulated by the device, so the counters indicate
4220 	 * all drops since last reset.
4221 	 */
4222 	adapter->dev_stats.rx_drops = rx_drops;
4223 	adapter->dev_stats.tx_drops = tx_drops;
4224 	u64_stats_update_end(&adapter->syncp);
4225 }
4226 
4227 static void ena_notification(void *adapter_data,
4228 			     struct ena_admin_aenq_entry *aenq_e)
4229 {
4230 	struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
4231 	struct ena_admin_ena_hw_hints *hints;
4232 
4233 	WARN(aenq_e->aenq_common_desc.group != ENA_ADMIN_NOTIFICATION,
4234 	     "Invalid group(%x) expected %x\n",
4235 	     aenq_e->aenq_common_desc.group,
4236 	     ENA_ADMIN_NOTIFICATION);
4237 
4238 	switch (aenq_e->aenq_common_desc.syndrome) {
4239 	case ENA_ADMIN_UPDATE_HINTS:
4240 		hints = (struct ena_admin_ena_hw_hints *)
4241 			(&aenq_e->inline_data_w4);
4242 		ena_update_hints(adapter, hints);
4243 		break;
4244 	default:
4245 		netif_err(adapter, drv, adapter->netdev,
4246 			  "Invalid aenq notification link state %d\n",
4247 			  aenq_e->aenq_common_desc.syndrome);
4248 	}
4249 }
4250 
4251 /* This handler will called for unknown event group or unimplemented handlers*/
4252 static void unimplemented_aenq_handler(void *data,
4253 				       struct ena_admin_aenq_entry *aenq_e)
4254 {
4255 	struct ena_adapter *adapter = (struct ena_adapter *)data;
4256 
4257 	netif_err(adapter, drv, adapter->netdev,
4258 		  "Unknown event was received or event with unimplemented handler\n");
4259 }
4260 
4261 static struct ena_aenq_handlers aenq_handlers = {
4262 	.handlers = {
4263 		[ENA_ADMIN_LINK_CHANGE] = ena_update_on_link_change,
4264 		[ENA_ADMIN_NOTIFICATION] = ena_notification,
4265 		[ENA_ADMIN_KEEP_ALIVE] = ena_keep_alive_wd,
4266 	},
4267 	.unimplemented_handler = unimplemented_aenq_handler
4268 };
4269 
4270 module_init(ena_init);
4271 module_exit(ena_cleanup);
4272