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