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