blob: a6f283232cb7a9df601d4f2e464fb55257e43434 [file] [log] [blame]
Netanel Belgazal1738cd32016-08-10 14:03:22 +03001/*
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/if_vlan.h>
40#include <linux/kernel.h>
41#include <linux/module.h>
42#include <linux/moduleparam.h>
43#include <linux/numa.h>
44#include <linux/pci.h>
45#include <linux/utsname.h>
46#include <linux/version.h>
47#include <linux/vmalloc.h>
48#include <net/ip.h>
49
50#include "ena_netdev.h"
51#include "ena_pci_id_tbl.h"
52
53static char version[] = DEVICE_NAME " v" DRV_MODULE_VERSION "\n";
54
55MODULE_AUTHOR("Amazon.com, Inc. or its affiliates");
56MODULE_DESCRIPTION(DEVICE_NAME);
57MODULE_LICENSE("GPL");
58MODULE_VERSION(DRV_MODULE_VERSION);
59
60/* Time in jiffies before concluding the transmitter is hung. */
61#define TX_TIMEOUT (5 * HZ)
62
63#define ENA_NAPI_BUDGET 64
64
65#define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | \
66 NETIF_MSG_TX_DONE | NETIF_MSG_TX_ERR | NETIF_MSG_RX_ERR)
67static int debug = -1;
68module_param(debug, int, 0);
69MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
70
71static struct ena_aenq_handlers aenq_handlers;
72
73static struct workqueue_struct *ena_wq;
74
75MODULE_DEVICE_TABLE(pci, ena_pci_tbl);
76
77static int ena_rss_init_default(struct ena_adapter *adapter);
78
79static void ena_tx_timeout(struct net_device *dev)
80{
81 struct ena_adapter *adapter = netdev_priv(dev);
82
Netanel Belgazal3f6159d2017-02-09 15:21:33 +020083 /* Change the state of the device to trigger reset
84 * Check that we are not in the middle or a trigger already
85 */
86
87 if (test_and_set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags))
88 return;
89
Netanel Belgazale2eed0e2017-06-23 11:21:53 +030090 adapter->reset_reason = ENA_REGS_RESET_OS_NETDEV_WD;
Netanel Belgazal1738cd32016-08-10 14:03:22 +030091 u64_stats_update_begin(&adapter->syncp);
92 adapter->dev_stats.tx_timeout++;
93 u64_stats_update_end(&adapter->syncp);
94
95 netif_err(adapter, tx_err, dev, "Transmit time out\n");
Netanel Belgazal1738cd32016-08-10 14:03:22 +030096}
97
98static void update_rx_ring_mtu(struct ena_adapter *adapter, int mtu)
99{
100 int i;
101
102 for (i = 0; i < adapter->num_queues; i++)
103 adapter->rx_ring[i].mtu = mtu;
104}
105
106static int ena_change_mtu(struct net_device *dev, int new_mtu)
107{
108 struct ena_adapter *adapter = netdev_priv(dev);
109 int ret;
110
Netanel Belgazal1738cd32016-08-10 14:03:22 +0300111 ret = ena_com_set_dev_mtu(adapter->ena_dev, new_mtu);
112 if (!ret) {
113 netif_dbg(adapter, drv, dev, "set MTU to %d\n", new_mtu);
114 update_rx_ring_mtu(adapter, new_mtu);
115 dev->mtu = new_mtu;
116 } else {
117 netif_err(adapter, drv, dev, "Failed to set MTU to %d\n",
118 new_mtu);
119 }
120
121 return ret;
122}
123
124static int ena_init_rx_cpu_rmap(struct ena_adapter *adapter)
125{
126#ifdef CONFIG_RFS_ACCEL
127 u32 i;
128 int rc;
129
130 adapter->netdev->rx_cpu_rmap = alloc_irq_cpu_rmap(adapter->num_queues);
131 if (!adapter->netdev->rx_cpu_rmap)
132 return -ENOMEM;
133 for (i = 0; i < adapter->num_queues; i++) {
134 int irq_idx = ENA_IO_IRQ_IDX(i);
135
136 rc = irq_cpu_rmap_add(adapter->netdev->rx_cpu_rmap,
Christoph Hellwigda6f4cf2017-04-11 13:01:22 +0200137 pci_irq_vector(adapter->pdev, irq_idx));
Netanel Belgazal1738cd32016-08-10 14:03:22 +0300138 if (rc) {
139 free_irq_cpu_rmap(adapter->netdev->rx_cpu_rmap);
140 adapter->netdev->rx_cpu_rmap = NULL;
141 return rc;
142 }
143 }
144#endif /* CONFIG_RFS_ACCEL */
145 return 0;
146}
147
148static void ena_init_io_rings_common(struct ena_adapter *adapter,
149 struct ena_ring *ring, u16 qid)
150{
151 ring->qid = qid;
152 ring->pdev = adapter->pdev;
153 ring->dev = &adapter->pdev->dev;
154 ring->netdev = adapter->netdev;
155 ring->napi = &adapter->ena_napi[qid].napi;
156 ring->adapter = adapter;
157 ring->ena_dev = adapter->ena_dev;
158 ring->per_napi_packets = 0;
159 ring->per_napi_bytes = 0;
160 ring->cpu = 0;
Netanel Belgazal8510e1a2017-12-28 21:31:30 +0000161 ring->first_interrupt = false;
162 ring->no_interrupt_event_cnt = 0;
Netanel Belgazal1738cd32016-08-10 14:03:22 +0300163 u64_stats_init(&ring->syncp);
164}
165
166static void ena_init_io_rings(struct ena_adapter *adapter)
167{
168 struct ena_com_dev *ena_dev;
169 struct ena_ring *txr, *rxr;
170 int i;
171
172 ena_dev = adapter->ena_dev;
173
174 for (i = 0; i < adapter->num_queues; i++) {
175 txr = &adapter->tx_ring[i];
176 rxr = &adapter->rx_ring[i];
177
178 /* TX/RX common ring state */
179 ena_init_io_rings_common(adapter, txr, i);
180 ena_init_io_rings_common(adapter, rxr, i);
181
182 /* TX specific ring state */
183 txr->ring_size = adapter->tx_ring_size;
184 txr->tx_max_header_size = ena_dev->tx_max_header_size;
185 txr->tx_mem_queue_type = ena_dev->tx_mem_queue_type;
186 txr->sgl_size = adapter->max_tx_sgl_size;
187 txr->smoothed_interval =
188 ena_com_get_nonadaptive_moderation_interval_tx(ena_dev);
189
190 /* RX specific ring state */
191 rxr->ring_size = adapter->rx_ring_size;
192 rxr->rx_copybreak = adapter->rx_copybreak;
193 rxr->sgl_size = adapter->max_rx_sgl_size;
194 rxr->smoothed_interval =
195 ena_com_get_nonadaptive_moderation_interval_rx(ena_dev);
Netanel Belgazala3af7c12017-06-11 15:42:48 +0300196 rxr->empty_rx_queue = 0;
Netanel Belgazal1738cd32016-08-10 14:03:22 +0300197 }
198}
199
200/* ena_setup_tx_resources - allocate I/O Tx resources (Descriptors)
201 * @adapter: network interface device structure
202 * @qid: queue index
203 *
204 * Return 0 on success, negative on failure
205 */
206static int ena_setup_tx_resources(struct ena_adapter *adapter, int qid)
207{
208 struct ena_ring *tx_ring = &adapter->tx_ring[qid];
209 struct ena_irq *ena_irq = &adapter->irq_tbl[ENA_IO_IRQ_IDX(qid)];
210 int size, i, node;
211
212 if (tx_ring->tx_buffer_info) {
213 netif_err(adapter, ifup,
214 adapter->netdev, "tx_buffer_info info is not NULL");
215 return -EEXIST;
216 }
217
218 size = sizeof(struct ena_tx_buffer) * tx_ring->ring_size;
219 node = cpu_to_node(ena_irq->cpu);
220
221 tx_ring->tx_buffer_info = vzalloc_node(size, node);
222 if (!tx_ring->tx_buffer_info) {
223 tx_ring->tx_buffer_info = vzalloc(size);
224 if (!tx_ring->tx_buffer_info)
225 return -ENOMEM;
226 }
227
228 size = sizeof(u16) * tx_ring->ring_size;
229 tx_ring->free_tx_ids = vzalloc_node(size, node);
230 if (!tx_ring->free_tx_ids) {
231 tx_ring->free_tx_ids = vzalloc(size);
232 if (!tx_ring->free_tx_ids) {
233 vfree(tx_ring->tx_buffer_info);
234 return -ENOMEM;
235 }
236 }
237
238 /* Req id ring for TX out of order completions */
239 for (i = 0; i < tx_ring->ring_size; i++)
240 tx_ring->free_tx_ids[i] = i;
241
242 /* Reset tx statistics */
243 memset(&tx_ring->tx_stats, 0x0, sizeof(tx_ring->tx_stats));
244
245 tx_ring->next_to_use = 0;
246 tx_ring->next_to_clean = 0;
247 tx_ring->cpu = ena_irq->cpu;
248 return 0;
249}
250
251/* ena_free_tx_resources - Free I/O Tx Resources per Queue
252 * @adapter: network interface device structure
253 * @qid: queue index
254 *
255 * Free all transmit software resources
256 */
257static void ena_free_tx_resources(struct ena_adapter *adapter, int qid)
258{
259 struct ena_ring *tx_ring = &adapter->tx_ring[qid];
260
261 vfree(tx_ring->tx_buffer_info);
262 tx_ring->tx_buffer_info = NULL;
263
264 vfree(tx_ring->free_tx_ids);
265 tx_ring->free_tx_ids = NULL;
266}
267
268/* ena_setup_all_tx_resources - allocate I/O Tx queues resources for All queues
269 * @adapter: private structure
270 *
271 * Return 0 on success, negative on failure
272 */
273static int ena_setup_all_tx_resources(struct ena_adapter *adapter)
274{
275 int i, rc = 0;
276
277 for (i = 0; i < adapter->num_queues; i++) {
278 rc = ena_setup_tx_resources(adapter, i);
279 if (rc)
280 goto err_setup_tx;
281 }
282
283 return 0;
284
285err_setup_tx:
286
287 netif_err(adapter, ifup, adapter->netdev,
288 "Tx queue %d: allocation failed\n", i);
289
290 /* rewind the index freeing the rings as we go */
291 while (i--)
292 ena_free_tx_resources(adapter, i);
293 return rc;
294}
295
296/* ena_free_all_io_tx_resources - Free I/O Tx Resources for All Queues
297 * @adapter: board private structure
298 *
299 * Free all transmit software resources
300 */
301static void ena_free_all_io_tx_resources(struct ena_adapter *adapter)
302{
303 int i;
304
305 for (i = 0; i < adapter->num_queues; i++)
306 ena_free_tx_resources(adapter, i);
307}
308
Netanel Belgazalad974ba2017-06-23 11:21:54 +0300309static inline int validate_rx_req_id(struct ena_ring *rx_ring, u16 req_id)
310{
311 if (likely(req_id < rx_ring->ring_size))
312 return 0;
313
314 netif_err(rx_ring->adapter, rx_err, rx_ring->netdev,
315 "Invalid rx req_id: %hu\n", req_id);
316
317 u64_stats_update_begin(&rx_ring->syncp);
318 rx_ring->rx_stats.bad_req_id++;
319 u64_stats_update_end(&rx_ring->syncp);
320
321 /* Trigger device reset */
322 rx_ring->adapter->reset_reason = ENA_REGS_RESET_INV_RX_REQ_ID;
323 set_bit(ENA_FLAG_TRIGGER_RESET, &rx_ring->adapter->flags);
324 return -EFAULT;
325}
326
Netanel Belgazal1738cd32016-08-10 14:03:22 +0300327/* ena_setup_rx_resources - allocate I/O Rx resources (Descriptors)
328 * @adapter: network interface device structure
329 * @qid: queue index
330 *
331 * Returns 0 on success, negative on failure
332 */
333static int ena_setup_rx_resources(struct ena_adapter *adapter,
334 u32 qid)
335{
336 struct ena_ring *rx_ring = &adapter->rx_ring[qid];
337 struct ena_irq *ena_irq = &adapter->irq_tbl[ENA_IO_IRQ_IDX(qid)];
Netanel Belgazalad974ba2017-06-23 11:21:54 +0300338 int size, node, i;
Netanel Belgazal1738cd32016-08-10 14:03:22 +0300339
340 if (rx_ring->rx_buffer_info) {
341 netif_err(adapter, ifup, adapter->netdev,
342 "rx_buffer_info is not NULL");
343 return -EEXIST;
344 }
345
346 /* alloc extra element so in rx path
347 * we can always prefetch rx_info + 1
348 */
349 size = sizeof(struct ena_rx_buffer) * (rx_ring->ring_size + 1);
350 node = cpu_to_node(ena_irq->cpu);
351
352 rx_ring->rx_buffer_info = vzalloc_node(size, node);
353 if (!rx_ring->rx_buffer_info) {
354 rx_ring->rx_buffer_info = vzalloc(size);
355 if (!rx_ring->rx_buffer_info)
356 return -ENOMEM;
357 }
358
Netanel Belgazalad974ba2017-06-23 11:21:54 +0300359 size = sizeof(u16) * rx_ring->ring_size;
360 rx_ring->free_rx_ids = vzalloc_node(size, node);
361 if (!rx_ring->free_rx_ids) {
362 rx_ring->free_rx_ids = vzalloc(size);
363 if (!rx_ring->free_rx_ids) {
364 vfree(rx_ring->rx_buffer_info);
365 return -ENOMEM;
366 }
367 }
368
369 /* Req id ring for receiving RX pkts out of order */
370 for (i = 0; i < rx_ring->ring_size; i++)
371 rx_ring->free_rx_ids[i] = i;
372
Netanel Belgazal1738cd32016-08-10 14:03:22 +0300373 /* Reset rx statistics */
374 memset(&rx_ring->rx_stats, 0x0, sizeof(rx_ring->rx_stats));
375
376 rx_ring->next_to_clean = 0;
377 rx_ring->next_to_use = 0;
378 rx_ring->cpu = ena_irq->cpu;
379
380 return 0;
381}
382
383/* ena_free_rx_resources - Free I/O Rx Resources
384 * @adapter: network interface device structure
385 * @qid: queue index
386 *
387 * Free all receive software resources
388 */
389static void ena_free_rx_resources(struct ena_adapter *adapter,
390 u32 qid)
391{
392 struct ena_ring *rx_ring = &adapter->rx_ring[qid];
393
394 vfree(rx_ring->rx_buffer_info);
395 rx_ring->rx_buffer_info = NULL;
Netanel Belgazalad974ba2017-06-23 11:21:54 +0300396
397 vfree(rx_ring->free_rx_ids);
398 rx_ring->free_rx_ids = NULL;
Netanel Belgazal1738cd32016-08-10 14:03:22 +0300399}
400
401/* ena_setup_all_rx_resources - allocate I/O Rx queues resources for all queues
402 * @adapter: board private structure
403 *
404 * Return 0 on success, negative on failure
405 */
406static int ena_setup_all_rx_resources(struct ena_adapter *adapter)
407{
408 int i, rc = 0;
409
410 for (i = 0; i < adapter->num_queues; i++) {
411 rc = ena_setup_rx_resources(adapter, i);
412 if (rc)
413 goto err_setup_rx;
414 }
415
416 return 0;
417
418err_setup_rx:
419
420 netif_err(adapter, ifup, adapter->netdev,
421 "Rx queue %d: allocation failed\n", i);
422
423 /* rewind the index freeing the rings as we go */
424 while (i--)
425 ena_free_rx_resources(adapter, i);
426 return rc;
427}
428
429/* ena_free_all_io_rx_resources - Free I/O Rx Resources for All Queues
430 * @adapter: board private structure
431 *
432 * Free all receive software resources
433 */
434static void ena_free_all_io_rx_resources(struct ena_adapter *adapter)
435{
436 int i;
437
438 for (i = 0; i < adapter->num_queues; i++)
439 ena_free_rx_resources(adapter, i);
440}
441
442static inline int ena_alloc_rx_page(struct ena_ring *rx_ring,
443 struct ena_rx_buffer *rx_info, gfp_t gfp)
444{
445 struct ena_com_buf *ena_buf;
446 struct page *page;
447 dma_addr_t dma;
448
449 /* if previous allocated page is not used */
450 if (unlikely(rx_info->page))
451 return 0;
452
453 page = alloc_page(gfp);
454 if (unlikely(!page)) {
455 u64_stats_update_begin(&rx_ring->syncp);
456 rx_ring->rx_stats.page_alloc_fail++;
457 u64_stats_update_end(&rx_ring->syncp);
458 return -ENOMEM;
459 }
460
461 dma = dma_map_page(rx_ring->dev, page, 0, PAGE_SIZE,
462 DMA_FROM_DEVICE);
463 if (unlikely(dma_mapping_error(rx_ring->dev, dma))) {
464 u64_stats_update_begin(&rx_ring->syncp);
465 rx_ring->rx_stats.dma_mapping_err++;
466 u64_stats_update_end(&rx_ring->syncp);
467
468 __free_page(page);
469 return -EIO;
470 }
471 netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev,
472 "alloc page %p, rx_info %p\n", page, rx_info);
473
474 rx_info->page = page;
475 rx_info->page_offset = 0;
476 ena_buf = &rx_info->ena_buf;
477 ena_buf->paddr = dma;
478 ena_buf->len = PAGE_SIZE;
479
480 return 0;
481}
482
483static void ena_free_rx_page(struct ena_ring *rx_ring,
484 struct ena_rx_buffer *rx_info)
485{
486 struct page *page = rx_info->page;
487 struct ena_com_buf *ena_buf = &rx_info->ena_buf;
488
489 if (unlikely(!page)) {
490 netif_warn(rx_ring->adapter, rx_err, rx_ring->netdev,
491 "Trying to free unallocated buffer\n");
492 return;
493 }
494
495 dma_unmap_page(rx_ring->dev, ena_buf->paddr, PAGE_SIZE,
496 DMA_FROM_DEVICE);
497
498 __free_page(page);
499 rx_info->page = NULL;
500}
501
502static int ena_refill_rx_bufs(struct ena_ring *rx_ring, u32 num)
503{
Netanel Belgazalad974ba2017-06-23 11:21:54 +0300504 u16 next_to_use, req_id;
Netanel Belgazal1738cd32016-08-10 14:03:22 +0300505 u32 i;
506 int rc;
507
508 next_to_use = rx_ring->next_to_use;
509
510 for (i = 0; i < num; i++) {
Netanel Belgazalad974ba2017-06-23 11:21:54 +0300511 struct ena_rx_buffer *rx_info;
512
513 req_id = rx_ring->free_rx_ids[next_to_use];
514 rc = validate_rx_req_id(rx_ring, req_id);
515 if (unlikely(rc < 0))
516 break;
517
518 rx_info = &rx_ring->rx_buffer_info[req_id];
519
Netanel Belgazal1738cd32016-08-10 14:03:22 +0300520
521 rc = ena_alloc_rx_page(rx_ring, rx_info,
Mel Gorman453f85d2017-11-15 17:38:03 -0800522 GFP_ATOMIC | __GFP_COMP);
Netanel Belgazal1738cd32016-08-10 14:03:22 +0300523 if (unlikely(rc < 0)) {
524 netif_warn(rx_ring->adapter, rx_err, rx_ring->netdev,
525 "failed to alloc buffer for rx queue %d\n",
526 rx_ring->qid);
527 break;
528 }
529 rc = ena_com_add_single_rx_desc(rx_ring->ena_com_io_sq,
530 &rx_info->ena_buf,
Netanel Belgazalad974ba2017-06-23 11:21:54 +0300531 req_id);
Netanel Belgazal1738cd32016-08-10 14:03:22 +0300532 if (unlikely(rc)) {
533 netif_warn(rx_ring->adapter, rx_status, rx_ring->netdev,
534 "failed to add buffer for rx queue %d\n",
535 rx_ring->qid);
536 break;
537 }
538 next_to_use = ENA_RX_RING_IDX_NEXT(next_to_use,
539 rx_ring->ring_size);
540 }
541
542 if (unlikely(i < num)) {
543 u64_stats_update_begin(&rx_ring->syncp);
544 rx_ring->rx_stats.refil_partial++;
545 u64_stats_update_end(&rx_ring->syncp);
546 netdev_warn(rx_ring->netdev,
547 "refilled rx qid %d with only %d buffers (from %d)\n",
548 rx_ring->qid, i, num);
549 }
550
551 if (likely(i)) {
552 /* Add memory barrier to make sure the desc were written before
553 * issue a doorbell
554 */
555 wmb();
556 ena_com_write_sq_doorbell(rx_ring->ena_com_io_sq);
557 }
558
559 rx_ring->next_to_use = next_to_use;
560
561 return i;
562}
563
564static void ena_free_rx_bufs(struct ena_adapter *adapter,
565 u32 qid)
566{
567 struct ena_ring *rx_ring = &adapter->rx_ring[qid];
568 u32 i;
569
570 for (i = 0; i < rx_ring->ring_size; i++) {
571 struct ena_rx_buffer *rx_info = &rx_ring->rx_buffer_info[i];
572
573 if (rx_info->page)
574 ena_free_rx_page(rx_ring, rx_info);
575 }
576}
577
578/* ena_refill_all_rx_bufs - allocate all queues Rx buffers
579 * @adapter: board private structure
580 *
581 */
582static void ena_refill_all_rx_bufs(struct ena_adapter *adapter)
583{
584 struct ena_ring *rx_ring;
585 int i, rc, bufs_num;
586
587 for (i = 0; i < adapter->num_queues; i++) {
588 rx_ring = &adapter->rx_ring[i];
589 bufs_num = rx_ring->ring_size - 1;
590 rc = ena_refill_rx_bufs(rx_ring, bufs_num);
591
592 if (unlikely(rc != bufs_num))
593 netif_warn(rx_ring->adapter, rx_status, rx_ring->netdev,
594 "refilling Queue %d failed. allocated %d buffers from: %d\n",
595 i, rc, bufs_num);
596 }
597}
598
599static void ena_free_all_rx_bufs(struct ena_adapter *adapter)
600{
601 int i;
602
603 for (i = 0; i < adapter->num_queues; i++)
604 ena_free_rx_bufs(adapter, i);
605}
606
607/* ena_free_tx_bufs - Free Tx Buffers per Queue
608 * @tx_ring: TX ring for which buffers be freed
609 */
610static void ena_free_tx_bufs(struct ena_ring *tx_ring)
611{
Netanel Belgazal5add6e42017-02-09 15:21:36 +0200612 bool print_once = true;
Netanel Belgazal1738cd32016-08-10 14:03:22 +0300613 u32 i;
614
615 for (i = 0; i < tx_ring->ring_size; i++) {
616 struct ena_tx_buffer *tx_info = &tx_ring->tx_buffer_info[i];
617 struct ena_com_buf *ena_buf;
618 int nr_frags;
619 int j;
620
621 if (!tx_info->skb)
622 continue;
623
Netanel Belgazal5add6e42017-02-09 15:21:36 +0200624 if (print_once) {
625 netdev_notice(tx_ring->netdev,
626 "free uncompleted tx skb qid %d idx 0x%x\n",
627 tx_ring->qid, i);
628 print_once = false;
629 } else {
630 netdev_dbg(tx_ring->netdev,
631 "free uncompleted tx skb qid %d idx 0x%x\n",
632 tx_ring->qid, i);
633 }
Netanel Belgazal1738cd32016-08-10 14:03:22 +0300634
635 ena_buf = tx_info->bufs;
636 dma_unmap_single(tx_ring->dev,
637 ena_buf->paddr,
638 ena_buf->len,
639 DMA_TO_DEVICE);
640
641 /* unmap remaining mapped pages */
642 nr_frags = tx_info->num_of_bufs - 1;
643 for (j = 0; j < nr_frags; j++) {
644 ena_buf++;
645 dma_unmap_page(tx_ring->dev,
646 ena_buf->paddr,
647 ena_buf->len,
648 DMA_TO_DEVICE);
649 }
650
651 dev_kfree_skb_any(tx_info->skb);
652 }
653 netdev_tx_reset_queue(netdev_get_tx_queue(tx_ring->netdev,
654 tx_ring->qid));
655}
656
657static void ena_free_all_tx_bufs(struct ena_adapter *adapter)
658{
659 struct ena_ring *tx_ring;
660 int i;
661
662 for (i = 0; i < adapter->num_queues; i++) {
663 tx_ring = &adapter->tx_ring[i];
664 ena_free_tx_bufs(tx_ring);
665 }
666}
667
668static void ena_destroy_all_tx_queues(struct ena_adapter *adapter)
669{
670 u16 ena_qid;
671 int i;
672
673 for (i = 0; i < adapter->num_queues; i++) {
674 ena_qid = ENA_IO_TXQ_IDX(i);
675 ena_com_destroy_io_queue(adapter->ena_dev, ena_qid);
676 }
677}
678
679static void ena_destroy_all_rx_queues(struct ena_adapter *adapter)
680{
681 u16 ena_qid;
682 int i;
683
684 for (i = 0; i < adapter->num_queues; i++) {
685 ena_qid = ENA_IO_RXQ_IDX(i);
686 ena_com_destroy_io_queue(adapter->ena_dev, ena_qid);
687 }
688}
689
690static void ena_destroy_all_io_queues(struct ena_adapter *adapter)
691{
692 ena_destroy_all_tx_queues(adapter);
693 ena_destroy_all_rx_queues(adapter);
694}
695
696static int validate_tx_req_id(struct ena_ring *tx_ring, u16 req_id)
697{
698 struct ena_tx_buffer *tx_info = NULL;
699
700 if (likely(req_id < tx_ring->ring_size)) {
701 tx_info = &tx_ring->tx_buffer_info[req_id];
702 if (likely(tx_info->skb))
703 return 0;
704 }
705
706 if (tx_info)
707 netif_err(tx_ring->adapter, tx_done, tx_ring->netdev,
708 "tx_info doesn't have valid skb\n");
709 else
710 netif_err(tx_ring->adapter, tx_done, tx_ring->netdev,
711 "Invalid req_id: %hu\n", req_id);
712
713 u64_stats_update_begin(&tx_ring->syncp);
714 tx_ring->tx_stats.bad_req_id++;
715 u64_stats_update_end(&tx_ring->syncp);
716
717 /* Trigger device reset */
Netanel Belgazale2eed0e2017-06-23 11:21:53 +0300718 tx_ring->adapter->reset_reason = ENA_REGS_RESET_INV_TX_REQ_ID;
Netanel Belgazal1738cd32016-08-10 14:03:22 +0300719 set_bit(ENA_FLAG_TRIGGER_RESET, &tx_ring->adapter->flags);
720 return -EFAULT;
721}
722
723static int ena_clean_tx_irq(struct ena_ring *tx_ring, u32 budget)
724{
725 struct netdev_queue *txq;
726 bool above_thresh;
727 u32 tx_bytes = 0;
728 u32 total_done = 0;
729 u16 next_to_clean;
730 u16 req_id;
731 int tx_pkts = 0;
732 int rc;
733
734 next_to_clean = tx_ring->next_to_clean;
735 txq = netdev_get_tx_queue(tx_ring->netdev, tx_ring->qid);
736
737 while (tx_pkts < budget) {
738 struct ena_tx_buffer *tx_info;
739 struct sk_buff *skb;
740 struct ena_com_buf *ena_buf;
741 int i, nr_frags;
742
743 rc = ena_com_tx_comp_req_id_get(tx_ring->ena_com_io_cq,
744 &req_id);
745 if (rc)
746 break;
747
748 rc = validate_tx_req_id(tx_ring, req_id);
749 if (rc)
750 break;
751
752 tx_info = &tx_ring->tx_buffer_info[req_id];
753 skb = tx_info->skb;
754
755 /* prefetch skb_end_pointer() to speedup skb_shinfo(skb) */
756 prefetch(&skb->end);
757
758 tx_info->skb = NULL;
759 tx_info->last_jiffies = 0;
760
761 if (likely(tx_info->num_of_bufs != 0)) {
762 ena_buf = tx_info->bufs;
763
764 dma_unmap_single(tx_ring->dev,
765 dma_unmap_addr(ena_buf, paddr),
766 dma_unmap_len(ena_buf, len),
767 DMA_TO_DEVICE);
768
769 /* unmap remaining mapped pages */
770 nr_frags = tx_info->num_of_bufs - 1;
771 for (i = 0; i < nr_frags; i++) {
772 ena_buf++;
773 dma_unmap_page(tx_ring->dev,
774 dma_unmap_addr(ena_buf, paddr),
775 dma_unmap_len(ena_buf, len),
776 DMA_TO_DEVICE);
777 }
778 }
779
780 netif_dbg(tx_ring->adapter, tx_done, tx_ring->netdev,
781 "tx_poll: q %d skb %p completed\n", tx_ring->qid,
782 skb);
783
784 tx_bytes += skb->len;
785 dev_kfree_skb(skb);
786 tx_pkts++;
787 total_done += tx_info->tx_descs;
788
789 tx_ring->free_tx_ids[next_to_clean] = req_id;
790 next_to_clean = ENA_TX_RING_IDX_NEXT(next_to_clean,
791 tx_ring->ring_size);
792 }
793
794 tx_ring->next_to_clean = next_to_clean;
795 ena_com_comp_ack(tx_ring->ena_com_io_sq, total_done);
796 ena_com_update_dev_comp_head(tx_ring->ena_com_io_cq);
797
798 netdev_tx_completed_queue(txq, tx_pkts, tx_bytes);
799
800 netif_dbg(tx_ring->adapter, tx_done, tx_ring->netdev,
801 "tx_poll: q %d done. total pkts: %d\n",
802 tx_ring->qid, tx_pkts);
803
804 /* need to make the rings circular update visible to
805 * ena_start_xmit() before checking for netif_queue_stopped().
806 */
807 smp_mb();
808
809 above_thresh = ena_com_sq_empty_space(tx_ring->ena_com_io_sq) >
810 ENA_TX_WAKEUP_THRESH;
811 if (unlikely(netif_tx_queue_stopped(txq) && above_thresh)) {
812 __netif_tx_lock(txq, smp_processor_id());
813 above_thresh = ena_com_sq_empty_space(tx_ring->ena_com_io_sq) >
814 ENA_TX_WAKEUP_THRESH;
815 if (netif_tx_queue_stopped(txq) && above_thresh) {
816 netif_tx_wake_queue(txq);
817 u64_stats_update_begin(&tx_ring->syncp);
818 tx_ring->tx_stats.queue_wakeup++;
819 u64_stats_update_end(&tx_ring->syncp);
820 }
821 __netif_tx_unlock(txq);
822 }
823
824 tx_ring->per_napi_bytes += tx_bytes;
825 tx_ring->per_napi_packets += tx_pkts;
826
827 return tx_pkts;
828}
829
Netanel Belgazal42651142017-06-23 11:21:57 +0300830static struct sk_buff *ena_alloc_skb(struct ena_ring *rx_ring, bool frags)
831{
832 struct sk_buff *skb;
833
834 if (frags)
835 skb = napi_get_frags(rx_ring->napi);
836 else
837 skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
838 rx_ring->rx_copybreak);
839
840 if (unlikely(!skb)) {
841 u64_stats_update_begin(&rx_ring->syncp);
842 rx_ring->rx_stats.skb_alloc_fail++;
843 u64_stats_update_end(&rx_ring->syncp);
844 netif_dbg(rx_ring->adapter, rx_err, rx_ring->netdev,
845 "Failed to allocate skb. frags: %d\n", frags);
846 return NULL;
847 }
848
849 return skb;
850}
851
Netanel Belgazal1738cd32016-08-10 14:03:22 +0300852static struct sk_buff *ena_rx_skb(struct ena_ring *rx_ring,
853 struct ena_com_rx_buf_info *ena_bufs,
854 u32 descs,
855 u16 *next_to_clean)
856{
857 struct sk_buff *skb;
Netanel Belgazalad974ba2017-06-23 11:21:54 +0300858 struct ena_rx_buffer *rx_info;
859 u16 len, req_id, buf = 0;
Netanel Belgazal1738cd32016-08-10 14:03:22 +0300860 void *va;
861
Netanel Belgazalad974ba2017-06-23 11:21:54 +0300862 len = ena_bufs[buf].len;
863 req_id = ena_bufs[buf].req_id;
864 rx_info = &rx_ring->rx_buffer_info[req_id];
865
Netanel Belgazal1738cd32016-08-10 14:03:22 +0300866 if (unlikely(!rx_info->page)) {
867 netif_err(rx_ring->adapter, rx_err, rx_ring->netdev,
868 "Page is NULL\n");
869 return NULL;
870 }
871
872 netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev,
873 "rx_info %p page %p\n",
874 rx_info, rx_info->page);
875
876 /* save virt address of first buffer */
877 va = page_address(rx_info->page) + rx_info->page_offset;
878 prefetch(va + NET_IP_ALIGN);
879
880 if (len <= rx_ring->rx_copybreak) {
Netanel Belgazal42651142017-06-23 11:21:57 +0300881 skb = ena_alloc_skb(rx_ring, false);
882 if (unlikely(!skb))
Netanel Belgazal1738cd32016-08-10 14:03:22 +0300883 return NULL;
Netanel Belgazal1738cd32016-08-10 14:03:22 +0300884
885 netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev,
886 "rx allocated small packet. len %d. data_len %d\n",
887 skb->len, skb->data_len);
888
889 /* sync this buffer for CPU use */
890 dma_sync_single_for_cpu(rx_ring->dev,
891 dma_unmap_addr(&rx_info->ena_buf, paddr),
892 len,
893 DMA_FROM_DEVICE);
894 skb_copy_to_linear_data(skb, va, len);
895 dma_sync_single_for_device(rx_ring->dev,
896 dma_unmap_addr(&rx_info->ena_buf, paddr),
897 len,
898 DMA_FROM_DEVICE);
899
900 skb_put(skb, len);
901 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
Netanel Belgazal42651142017-06-23 11:21:57 +0300902 rx_ring->free_rx_ids[*next_to_clean] = req_id;
Netanel Belgazal1738cd32016-08-10 14:03:22 +0300903 *next_to_clean = ENA_RX_RING_IDX_ADD(*next_to_clean, descs,
904 rx_ring->ring_size);
905 return skb;
906 }
907
Netanel Belgazal42651142017-06-23 11:21:57 +0300908 skb = ena_alloc_skb(rx_ring, true);
909 if (unlikely(!skb))
Netanel Belgazal1738cd32016-08-10 14:03:22 +0300910 return NULL;
Netanel Belgazal1738cd32016-08-10 14:03:22 +0300911
912 do {
913 dma_unmap_page(rx_ring->dev,
914 dma_unmap_addr(&rx_info->ena_buf, paddr),
915 PAGE_SIZE, DMA_FROM_DEVICE);
916
917 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_info->page,
918 rx_info->page_offset, len, PAGE_SIZE);
919
920 netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev,
921 "rx skb updated. len %d. data_len %d\n",
922 skb->len, skb->data_len);
923
924 rx_info->page = NULL;
Netanel Belgazalad974ba2017-06-23 11:21:54 +0300925
926 rx_ring->free_rx_ids[*next_to_clean] = req_id;
Netanel Belgazal1738cd32016-08-10 14:03:22 +0300927 *next_to_clean =
928 ENA_RX_RING_IDX_NEXT(*next_to_clean,
929 rx_ring->ring_size);
930 if (likely(--descs == 0))
931 break;
Netanel Belgazalad974ba2017-06-23 11:21:54 +0300932
933 buf++;
934 len = ena_bufs[buf].len;
935 req_id = ena_bufs[buf].req_id;
936 rx_info = &rx_ring->rx_buffer_info[req_id];
Netanel Belgazal1738cd32016-08-10 14:03:22 +0300937 } while (1);
938
939 return skb;
940}
941
942/* ena_rx_checksum - indicate in skb if hw indicated a good cksum
943 * @adapter: structure containing adapter specific data
944 * @ena_rx_ctx: received packet context/metadata
945 * @skb: skb currently being received and modified
946 */
947static inline void ena_rx_checksum(struct ena_ring *rx_ring,
948 struct ena_com_rx_ctx *ena_rx_ctx,
949 struct sk_buff *skb)
950{
951 /* Rx csum disabled */
952 if (unlikely(!(rx_ring->netdev->features & NETIF_F_RXCSUM))) {
953 skb->ip_summed = CHECKSUM_NONE;
954 return;
955 }
956
957 /* For fragmented packets the checksum isn't valid */
958 if (ena_rx_ctx->frag) {
959 skb->ip_summed = CHECKSUM_NONE;
960 return;
961 }
962
963 /* if IP and error */
964 if (unlikely((ena_rx_ctx->l3_proto == ENA_ETH_IO_L3_PROTO_IPV4) &&
965 (ena_rx_ctx->l3_csum_err))) {
966 /* ipv4 checksum error */
967 skb->ip_summed = CHECKSUM_NONE;
968 u64_stats_update_begin(&rx_ring->syncp);
969 rx_ring->rx_stats.bad_csum++;
970 u64_stats_update_end(&rx_ring->syncp);
Netanel Belgazalcd7aea12017-10-17 07:33:03 +0000971 netif_dbg(rx_ring->adapter, rx_err, rx_ring->netdev,
Netanel Belgazal1738cd32016-08-10 14:03:22 +0300972 "RX IPv4 header checksum error\n");
973 return;
974 }
975
976 /* if TCP/UDP */
977 if (likely((ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_TCP) ||
978 (ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_UDP))) {
979 if (unlikely(ena_rx_ctx->l4_csum_err)) {
980 /* TCP/UDP checksum error */
981 u64_stats_update_begin(&rx_ring->syncp);
982 rx_ring->rx_stats.bad_csum++;
983 u64_stats_update_end(&rx_ring->syncp);
Netanel Belgazalcd7aea12017-10-17 07:33:03 +0000984 netif_dbg(rx_ring->adapter, rx_err, rx_ring->netdev,
Netanel Belgazal1738cd32016-08-10 14:03:22 +0300985 "RX L4 checksum error\n");
986 skb->ip_summed = CHECKSUM_NONE;
987 return;
988 }
989
990 skb->ip_summed = CHECKSUM_UNNECESSARY;
991 }
992}
993
994static void ena_set_rx_hash(struct ena_ring *rx_ring,
995 struct ena_com_rx_ctx *ena_rx_ctx,
996 struct sk_buff *skb)
997{
998 enum pkt_hash_types hash_type;
999
1000 if (likely(rx_ring->netdev->features & NETIF_F_RXHASH)) {
1001 if (likely((ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_TCP) ||
1002 (ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_UDP)))
1003
1004 hash_type = PKT_HASH_TYPE_L4;
1005 else
1006 hash_type = PKT_HASH_TYPE_NONE;
1007
1008 /* Override hash type if the packet is fragmented */
1009 if (ena_rx_ctx->frag)
1010 hash_type = PKT_HASH_TYPE_NONE;
1011
1012 skb_set_hash(skb, ena_rx_ctx->hash, hash_type);
1013 }
1014}
1015
1016/* ena_clean_rx_irq - Cleanup RX irq
1017 * @rx_ring: RX ring to clean
1018 * @napi: napi handler
1019 * @budget: how many packets driver is allowed to clean
1020 *
1021 * Returns the number of cleaned buffers.
1022 */
1023static int ena_clean_rx_irq(struct ena_ring *rx_ring, struct napi_struct *napi,
1024 u32 budget)
1025{
1026 u16 next_to_clean = rx_ring->next_to_clean;
1027 u32 res_budget, work_done;
1028
1029 struct ena_com_rx_ctx ena_rx_ctx;
1030 struct ena_adapter *adapter;
1031 struct sk_buff *skb;
1032 int refill_required;
1033 int refill_threshold;
1034 int rc = 0;
1035 int total_len = 0;
1036 int rx_copybreak_pkt = 0;
Netanel Belgazalad974ba2017-06-23 11:21:54 +03001037 int i;
Netanel Belgazal1738cd32016-08-10 14:03:22 +03001038
1039 netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev,
1040 "%s qid %d\n", __func__, rx_ring->qid);
1041 res_budget = budget;
1042
1043 do {
1044 ena_rx_ctx.ena_bufs = rx_ring->ena_bufs;
1045 ena_rx_ctx.max_bufs = rx_ring->sgl_size;
1046 ena_rx_ctx.descs = 0;
1047 rc = ena_com_rx_pkt(rx_ring->ena_com_io_cq,
1048 rx_ring->ena_com_io_sq,
1049 &ena_rx_ctx);
1050 if (unlikely(rc))
1051 goto error;
1052
1053 if (unlikely(ena_rx_ctx.descs == 0))
1054 break;
1055
1056 netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev,
1057 "rx_poll: q %d got packet from ena. descs #: %d l3 proto %d l4 proto %d hash: %x\n",
1058 rx_ring->qid, ena_rx_ctx.descs, ena_rx_ctx.l3_proto,
1059 ena_rx_ctx.l4_proto, ena_rx_ctx.hash);
1060
1061 /* allocate skb and fill it */
1062 skb = ena_rx_skb(rx_ring, rx_ring->ena_bufs, ena_rx_ctx.descs,
1063 &next_to_clean);
1064
1065 /* exit if we failed to retrieve a buffer */
1066 if (unlikely(!skb)) {
Netanel Belgazalad974ba2017-06-23 11:21:54 +03001067 for (i = 0; i < ena_rx_ctx.descs; i++) {
1068 rx_ring->free_tx_ids[next_to_clean] =
1069 rx_ring->ena_bufs[i].req_id;
1070 next_to_clean =
1071 ENA_RX_RING_IDX_NEXT(next_to_clean,
1072 rx_ring->ring_size);
1073 }
Netanel Belgazal1738cd32016-08-10 14:03:22 +03001074 break;
1075 }
1076
1077 ena_rx_checksum(rx_ring, &ena_rx_ctx, skb);
1078
1079 ena_set_rx_hash(rx_ring, &ena_rx_ctx, skb);
1080
1081 skb_record_rx_queue(skb, rx_ring->qid);
1082
1083 if (rx_ring->ena_bufs[0].len <= rx_ring->rx_copybreak) {
1084 total_len += rx_ring->ena_bufs[0].len;
1085 rx_copybreak_pkt++;
1086 napi_gro_receive(napi, skb);
1087 } else {
1088 total_len += skb->len;
1089 napi_gro_frags(napi);
1090 }
1091
1092 res_budget--;
1093 } while (likely(res_budget));
1094
1095 work_done = budget - res_budget;
1096 rx_ring->per_napi_bytes += total_len;
1097 rx_ring->per_napi_packets += work_done;
1098 u64_stats_update_begin(&rx_ring->syncp);
1099 rx_ring->rx_stats.bytes += total_len;
1100 rx_ring->rx_stats.cnt += work_done;
1101 rx_ring->rx_stats.rx_copybreak_pkt += rx_copybreak_pkt;
1102 u64_stats_update_end(&rx_ring->syncp);
1103
1104 rx_ring->next_to_clean = next_to_clean;
1105
1106 refill_required = ena_com_sq_empty_space(rx_ring->ena_com_io_sq);
1107 refill_threshold = rx_ring->ring_size / ENA_RX_REFILL_THRESH_DIVIDER;
1108
1109 /* Optimization, try to batch new rx buffers */
1110 if (refill_required > refill_threshold) {
1111 ena_com_update_dev_comp_head(rx_ring->ena_com_io_cq);
1112 ena_refill_rx_bufs(rx_ring, refill_required);
1113 }
1114
1115 return work_done;
1116
1117error:
1118 adapter = netdev_priv(rx_ring->netdev);
1119
1120 u64_stats_update_begin(&rx_ring->syncp);
1121 rx_ring->rx_stats.bad_desc_num++;
1122 u64_stats_update_end(&rx_ring->syncp);
1123
1124 /* Too many desc from the device. Trigger reset */
Netanel Belgazale2eed0e2017-06-23 11:21:53 +03001125 adapter->reset_reason = ENA_REGS_RESET_TOO_MANY_RX_DESCS;
Netanel Belgazal1738cd32016-08-10 14:03:22 +03001126 set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags);
1127
1128 return 0;
1129}
1130
1131inline void ena_adjust_intr_moderation(struct ena_ring *rx_ring,
1132 struct ena_ring *tx_ring)
1133{
1134 /* We apply adaptive moderation on Rx path only.
1135 * Tx uses static interrupt moderation.
1136 */
1137 ena_com_calculate_interrupt_delay(rx_ring->ena_dev,
1138 rx_ring->per_napi_packets,
1139 rx_ring->per_napi_bytes,
1140 &rx_ring->smoothed_interval,
1141 &rx_ring->moder_tbl_idx);
1142
1143 /* Reset per napi packets/bytes */
1144 tx_ring->per_napi_packets = 0;
1145 tx_ring->per_napi_bytes = 0;
1146 rx_ring->per_napi_packets = 0;
1147 rx_ring->per_napi_bytes = 0;
1148}
1149
Netanel Belgazal418df302017-06-11 15:42:44 +03001150static inline void ena_unmask_interrupt(struct ena_ring *tx_ring,
1151 struct ena_ring *rx_ring)
1152{
1153 struct ena_eth_io_intr_reg intr_reg;
1154
1155 /* Update intr register: rx intr delay,
1156 * tx intr delay and interrupt unmask
1157 */
1158 ena_com_update_intr_reg(&intr_reg,
1159 rx_ring->smoothed_interval,
1160 tx_ring->smoothed_interval,
1161 true);
1162
1163 /* It is a shared MSI-X.
1164 * Tx and Rx CQ have pointer to it.
1165 * So we use one of them to reach the intr reg
1166 */
1167 ena_com_unmask_intr(rx_ring->ena_com_io_cq, &intr_reg);
1168}
1169
Netanel Belgazal1738cd32016-08-10 14:03:22 +03001170static inline void ena_update_ring_numa_node(struct ena_ring *tx_ring,
1171 struct ena_ring *rx_ring)
1172{
1173 int cpu = get_cpu();
1174 int numa_node;
1175
1176 /* Check only one ring since the 2 rings are running on the same cpu */
1177 if (likely(tx_ring->cpu == cpu))
1178 goto out;
1179
1180 numa_node = cpu_to_node(cpu);
1181 put_cpu();
1182
1183 if (numa_node != NUMA_NO_NODE) {
1184 ena_com_update_numa_node(tx_ring->ena_com_io_cq, numa_node);
1185 ena_com_update_numa_node(rx_ring->ena_com_io_cq, numa_node);
1186 }
1187
1188 tx_ring->cpu = cpu;
1189 rx_ring->cpu = cpu;
1190
1191 return;
1192out:
1193 put_cpu();
1194}
1195
1196static int ena_io_poll(struct napi_struct *napi, int budget)
1197{
1198 struct ena_napi *ena_napi = container_of(napi, struct ena_napi, napi);
1199 struct ena_ring *tx_ring, *rx_ring;
Netanel Belgazal1738cd32016-08-10 14:03:22 +03001200
1201 u32 tx_work_done;
1202 u32 rx_work_done;
1203 int tx_budget;
1204 int napi_comp_call = 0;
1205 int ret;
1206
1207 tx_ring = ena_napi->tx_ring;
1208 rx_ring = ena_napi->rx_ring;
1209
1210 tx_budget = tx_ring->ring_size / ENA_TX_POLL_BUDGET_DIVIDER;
1211
Netanel Belgazal3f6159d2017-02-09 15:21:33 +02001212 if (!test_bit(ENA_FLAG_DEV_UP, &tx_ring->adapter->flags) ||
1213 test_bit(ENA_FLAG_TRIGGER_RESET, &tx_ring->adapter->flags)) {
Netanel Belgazal1738cd32016-08-10 14:03:22 +03001214 napi_complete_done(napi, 0);
1215 return 0;
1216 }
1217
1218 tx_work_done = ena_clean_tx_irq(tx_ring, tx_budget);
1219 rx_work_done = ena_clean_rx_irq(rx_ring, napi, budget);
1220
Netanel Belgazalb1669c92017-02-09 15:21:34 +02001221 /* If the device is about to reset or down, avoid unmask
1222 * the interrupt and return 0 so NAPI won't reschedule
1223 */
1224 if (unlikely(!test_bit(ENA_FLAG_DEV_UP, &tx_ring->adapter->flags) ||
1225 test_bit(ENA_FLAG_TRIGGER_RESET, &tx_ring->adapter->flags))) {
1226 napi_complete_done(napi, 0);
1227 ret = 0;
Netanel Belgazal1738cd32016-08-10 14:03:22 +03001228
Netanel Belgazalb1669c92017-02-09 15:21:34 +02001229 } else if ((budget > rx_work_done) && (tx_budget > tx_work_done)) {
Netanel Belgazal1738cd32016-08-10 14:03:22 +03001230 napi_comp_call = 1;
Netanel Belgazal1738cd32016-08-10 14:03:22 +03001231
Netanel Belgazalb1669c92017-02-09 15:21:34 +02001232 /* Update numa and unmask the interrupt only when schedule
1233 * from the interrupt context (vs from sk_busy_loop)
Netanel Belgazal1738cd32016-08-10 14:03:22 +03001234 */
Netanel Belgazalb1669c92017-02-09 15:21:34 +02001235 if (napi_complete_done(napi, rx_work_done)) {
1236 /* Tx and Rx share the same interrupt vector */
1237 if (ena_com_get_adaptive_moderation_enabled(rx_ring->ena_dev))
1238 ena_adjust_intr_moderation(rx_ring, tx_ring);
Netanel Belgazal1738cd32016-08-10 14:03:22 +03001239
Netanel Belgazal418df302017-06-11 15:42:44 +03001240 ena_unmask_interrupt(tx_ring, rx_ring);
Netanel Belgazalb1669c92017-02-09 15:21:34 +02001241 }
1242
Netanel Belgazal1738cd32016-08-10 14:03:22 +03001243 ena_update_ring_numa_node(tx_ring, rx_ring);
1244
1245 ret = rx_work_done;
1246 } else {
1247 ret = budget;
1248 }
1249
1250 u64_stats_update_begin(&tx_ring->syncp);
1251 tx_ring->tx_stats.napi_comp += napi_comp_call;
1252 tx_ring->tx_stats.tx_poll++;
1253 u64_stats_update_end(&tx_ring->syncp);
1254
1255 return ret;
1256}
1257
1258static irqreturn_t ena_intr_msix_mgmnt(int irq, void *data)
1259{
1260 struct ena_adapter *adapter = (struct ena_adapter *)data;
1261
1262 ena_com_admin_q_comp_intr_handler(adapter->ena_dev);
1263
1264 /* Don't call the aenq handler before probe is done */
1265 if (likely(test_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags)))
1266 ena_com_aenq_intr_handler(adapter->ena_dev, data);
1267
1268 return IRQ_HANDLED;
1269}
1270
1271/* ena_intr_msix_io - MSI-X Interrupt Handler for Tx/Rx
1272 * @irq: interrupt number
1273 * @data: pointer to a network interface private napi device structure
1274 */
1275static irqreturn_t ena_intr_msix_io(int irq, void *data)
1276{
1277 struct ena_napi *ena_napi = data;
1278
Netanel Belgazal8510e1a2017-12-28 21:31:30 +00001279 ena_napi->tx_ring->first_interrupt = true;
1280 ena_napi->rx_ring->first_interrupt = true;
1281
Netanel Belgazale745daf2017-06-23 11:21:56 +03001282 napi_schedule_irqoff(&ena_napi->napi);
Netanel Belgazal1738cd32016-08-10 14:03:22 +03001283
1284 return IRQ_HANDLED;
1285}
1286
Netanel Belgazal06443682017-06-23 11:21:55 +03001287/* Reserve a single MSI-X vector for management (admin + aenq).
1288 * plus reserve one vector for each potential io queue.
1289 * the number of potential io queues is the minimum of what the device
1290 * supports and the number of vCPUs.
1291 */
Netanel Belgazal1738cd32016-08-10 14:03:22 +03001292static int ena_enable_msix(struct ena_adapter *adapter, int num_queues)
1293{
Netanel Belgazal06443682017-06-23 11:21:55 +03001294 int msix_vecs, irq_cnt;
1295
1296 if (test_bit(ENA_FLAG_MSIX_ENABLED, &adapter->flags)) {
1297 netif_err(adapter, probe, adapter->netdev,
1298 "Error, MSI-X is already enabled\n");
1299 return -EPERM;
1300 }
Netanel Belgazal1738cd32016-08-10 14:03:22 +03001301
1302 /* Reserved the max msix vectors we might need */
1303 msix_vecs = ENA_MAX_MSIX_VEC(num_queues);
1304
1305 netif_dbg(adapter, probe, adapter->netdev,
1306 "trying to enable MSI-X, vectors %d\n", msix_vecs);
1307
Netanel Belgazal06443682017-06-23 11:21:55 +03001308 irq_cnt = pci_alloc_irq_vectors(adapter->pdev, ENA_MIN_MSIX_VEC,
1309 msix_vecs, PCI_IRQ_MSIX);
1310
1311 if (irq_cnt < 0) {
Netanel Belgazal1738cd32016-08-10 14:03:22 +03001312 netif_err(adapter, probe, adapter->netdev,
Netanel Belgazal06443682017-06-23 11:21:55 +03001313 "Failed to enable MSI-X. irq_cnt %d\n", irq_cnt);
Netanel Belgazal1738cd32016-08-10 14:03:22 +03001314 return -ENOSPC;
1315 }
1316
Netanel Belgazal06443682017-06-23 11:21:55 +03001317 if (irq_cnt != msix_vecs) {
1318 netif_notice(adapter, probe, adapter->netdev,
1319 "enable only %d MSI-X (out of %d), reduce the number of queues\n",
1320 irq_cnt, msix_vecs);
1321 adapter->num_queues = irq_cnt - ENA_ADMIN_MSIX_VEC;
Netanel Belgazal1738cd32016-08-10 14:03:22 +03001322 }
1323
Netanel Belgazal06443682017-06-23 11:21:55 +03001324 if (ena_init_rx_cpu_rmap(adapter))
1325 netif_warn(adapter, probe, adapter->netdev,
1326 "Failed to map IRQs to CPUs\n");
1327
1328 adapter->msix_vecs = irq_cnt;
1329 set_bit(ENA_FLAG_MSIX_ENABLED, &adapter->flags);
Netanel Belgazal1738cd32016-08-10 14:03:22 +03001330
1331 return 0;
1332}
1333
1334static void ena_setup_mgmnt_intr(struct ena_adapter *adapter)
1335{
1336 u32 cpu;
1337
1338 snprintf(adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].name,
1339 ENA_IRQNAME_SIZE, "ena-mgmnt@pci:%s",
1340 pci_name(adapter->pdev));
1341 adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].handler =
1342 ena_intr_msix_mgmnt;
1343 adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].data = adapter;
1344 adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].vector =
Christoph Hellwigda6f4cf2017-04-11 13:01:22 +02001345 pci_irq_vector(adapter->pdev, ENA_MGMNT_IRQ_IDX);
Netanel Belgazal1738cd32016-08-10 14:03:22 +03001346 cpu = cpumask_first(cpu_online_mask);
1347 adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].cpu = cpu;
1348 cpumask_set_cpu(cpu,
1349 &adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].affinity_hint_mask);
1350}
1351
1352static void ena_setup_io_intr(struct ena_adapter *adapter)
1353{
1354 struct net_device *netdev;
1355 int irq_idx, i, cpu;
1356
1357 netdev = adapter->netdev;
1358
1359 for (i = 0; i < adapter->num_queues; i++) {
1360 irq_idx = ENA_IO_IRQ_IDX(i);
1361 cpu = i % num_online_cpus();
1362
1363 snprintf(adapter->irq_tbl[irq_idx].name, ENA_IRQNAME_SIZE,
1364 "%s-Tx-Rx-%d", netdev->name, i);
1365 adapter->irq_tbl[irq_idx].handler = ena_intr_msix_io;
1366 adapter->irq_tbl[irq_idx].data = &adapter->ena_napi[i];
1367 adapter->irq_tbl[irq_idx].vector =
Christoph Hellwigda6f4cf2017-04-11 13:01:22 +02001368 pci_irq_vector(adapter->pdev, irq_idx);
Netanel Belgazal1738cd32016-08-10 14:03:22 +03001369 adapter->irq_tbl[irq_idx].cpu = cpu;
1370
1371 cpumask_set_cpu(cpu,
1372 &adapter->irq_tbl[irq_idx].affinity_hint_mask);
1373 }
1374}
1375
1376static int ena_request_mgmnt_irq(struct ena_adapter *adapter)
1377{
1378 unsigned long flags = 0;
1379 struct ena_irq *irq;
1380 int rc;
1381
1382 irq = &adapter->irq_tbl[ENA_MGMNT_IRQ_IDX];
1383 rc = request_irq(irq->vector, irq->handler, flags, irq->name,
1384 irq->data);
1385 if (rc) {
1386 netif_err(adapter, probe, adapter->netdev,
1387 "failed to request admin irq\n");
1388 return rc;
1389 }
1390
1391 netif_dbg(adapter, probe, adapter->netdev,
1392 "set affinity hint of mgmnt irq.to 0x%lx (irq vector: %d)\n",
1393 irq->affinity_hint_mask.bits[0], irq->vector);
1394
1395 irq_set_affinity_hint(irq->vector, &irq->affinity_hint_mask);
1396
1397 return rc;
1398}
1399
1400static int ena_request_io_irq(struct ena_adapter *adapter)
1401{
1402 unsigned long flags = 0;
1403 struct ena_irq *irq;
1404 int rc = 0, i, k;
1405
Netanel Belgazal06443682017-06-23 11:21:55 +03001406 if (!test_bit(ENA_FLAG_MSIX_ENABLED, &adapter->flags)) {
1407 netif_err(adapter, ifup, adapter->netdev,
1408 "Failed to request I/O IRQ: MSI-X is not enabled\n");
1409 return -EINVAL;
1410 }
1411
Netanel Belgazal1738cd32016-08-10 14:03:22 +03001412 for (i = ENA_IO_IRQ_FIRST_IDX; i < adapter->msix_vecs; i++) {
1413 irq = &adapter->irq_tbl[i];
1414 rc = request_irq(irq->vector, irq->handler, flags, irq->name,
1415 irq->data);
1416 if (rc) {
1417 netif_err(adapter, ifup, adapter->netdev,
1418 "Failed to request I/O IRQ. index %d rc %d\n",
1419 i, rc);
1420 goto err;
1421 }
1422
1423 netif_dbg(adapter, ifup, adapter->netdev,
1424 "set affinity hint of irq. index %d to 0x%lx (irq vector: %d)\n",
1425 i, irq->affinity_hint_mask.bits[0], irq->vector);
1426
1427 irq_set_affinity_hint(irq->vector, &irq->affinity_hint_mask);
1428 }
1429
1430 return rc;
1431
1432err:
1433 for (k = ENA_IO_IRQ_FIRST_IDX; k < i; k++) {
1434 irq = &adapter->irq_tbl[k];
1435 free_irq(irq->vector, irq->data);
1436 }
1437
1438 return rc;
1439}
1440
1441static void ena_free_mgmnt_irq(struct ena_adapter *adapter)
1442{
1443 struct ena_irq *irq;
1444
1445 irq = &adapter->irq_tbl[ENA_MGMNT_IRQ_IDX];
1446 synchronize_irq(irq->vector);
1447 irq_set_affinity_hint(irq->vector, NULL);
1448 free_irq(irq->vector, irq->data);
1449}
1450
1451static void ena_free_io_irq(struct ena_adapter *adapter)
1452{
1453 struct ena_irq *irq;
1454 int i;
1455
1456#ifdef CONFIG_RFS_ACCEL
1457 if (adapter->msix_vecs >= 1) {
1458 free_irq_cpu_rmap(adapter->netdev->rx_cpu_rmap);
1459 adapter->netdev->rx_cpu_rmap = NULL;
1460 }
1461#endif /* CONFIG_RFS_ACCEL */
1462
1463 for (i = ENA_IO_IRQ_FIRST_IDX; i < adapter->msix_vecs; i++) {
1464 irq = &adapter->irq_tbl[i];
1465 irq_set_affinity_hint(irq->vector, NULL);
1466 free_irq(irq->vector, irq->data);
1467 }
1468}
1469
Netanel Belgazal06443682017-06-23 11:21:55 +03001470static void ena_disable_msix(struct ena_adapter *adapter)
1471{
1472 if (test_and_clear_bit(ENA_FLAG_MSIX_ENABLED, &adapter->flags))
1473 pci_free_irq_vectors(adapter->pdev);
1474}
1475
Netanel Belgazal1738cd32016-08-10 14:03:22 +03001476static void ena_disable_io_intr_sync(struct ena_adapter *adapter)
1477{
1478 int i;
1479
1480 if (!netif_running(adapter->netdev))
1481 return;
1482
1483 for (i = ENA_IO_IRQ_FIRST_IDX; i < adapter->msix_vecs; i++)
1484 synchronize_irq(adapter->irq_tbl[i].vector);
1485}
1486
1487static void ena_del_napi(struct ena_adapter *adapter)
1488{
1489 int i;
1490
1491 for (i = 0; i < adapter->num_queues; i++)
1492 netif_napi_del(&adapter->ena_napi[i].napi);
1493}
1494
1495static void ena_init_napi(struct ena_adapter *adapter)
1496{
1497 struct ena_napi *napi;
1498 int i;
1499
1500 for (i = 0; i < adapter->num_queues; i++) {
1501 napi = &adapter->ena_napi[i];
1502
1503 netif_napi_add(adapter->netdev,
1504 &adapter->ena_napi[i].napi,
1505 ena_io_poll,
1506 ENA_NAPI_BUDGET);
1507 napi->rx_ring = &adapter->rx_ring[i];
1508 napi->tx_ring = &adapter->tx_ring[i];
1509 napi->qid = i;
1510 }
1511}
1512
1513static void ena_napi_disable_all(struct ena_adapter *adapter)
1514{
1515 int i;
1516
1517 for (i = 0; i < adapter->num_queues; i++)
1518 napi_disable(&adapter->ena_napi[i].napi);
1519}
1520
1521static void ena_napi_enable_all(struct ena_adapter *adapter)
1522{
1523 int i;
1524
1525 for (i = 0; i < adapter->num_queues; i++)
1526 napi_enable(&adapter->ena_napi[i].napi);
1527}
1528
1529static void ena_restore_ethtool_params(struct ena_adapter *adapter)
1530{
1531 adapter->tx_usecs = 0;
1532 adapter->rx_usecs = 0;
1533 adapter->tx_frames = 1;
1534 adapter->rx_frames = 1;
1535}
1536
1537/* Configure the Rx forwarding */
1538static int ena_rss_configure(struct ena_adapter *adapter)
1539{
1540 struct ena_com_dev *ena_dev = adapter->ena_dev;
1541 int rc;
1542
1543 /* In case the RSS table wasn't initialized by probe */
1544 if (!ena_dev->rss.tbl_log_size) {
1545 rc = ena_rss_init_default(adapter);
Netanel Belgazald1497632017-06-23 11:21:50 +03001546 if (rc && (rc != -EOPNOTSUPP)) {
Netanel Belgazal1738cd32016-08-10 14:03:22 +03001547 netif_err(adapter, ifup, adapter->netdev,
1548 "Failed to init RSS rc: %d\n", rc);
1549 return rc;
1550 }
1551 }
1552
1553 /* Set indirect table */
1554 rc = ena_com_indirect_table_set(ena_dev);
Netanel Belgazald1497632017-06-23 11:21:50 +03001555 if (unlikely(rc && rc != -EOPNOTSUPP))
Netanel Belgazal1738cd32016-08-10 14:03:22 +03001556 return rc;
1557
1558 /* Configure hash function (if supported) */
1559 rc = ena_com_set_hash_function(ena_dev);
Netanel Belgazald1497632017-06-23 11:21:50 +03001560 if (unlikely(rc && (rc != -EOPNOTSUPP)))
Netanel Belgazal1738cd32016-08-10 14:03:22 +03001561 return rc;
1562
1563 /* Configure hash inputs (if supported) */
1564 rc = ena_com_set_hash_ctrl(ena_dev);
Netanel Belgazald1497632017-06-23 11:21:50 +03001565 if (unlikely(rc && (rc != -EOPNOTSUPP)))
Netanel Belgazal1738cd32016-08-10 14:03:22 +03001566 return rc;
1567
1568 return 0;
1569}
1570
1571static int ena_up_complete(struct ena_adapter *adapter)
1572{
1573 int rc, i;
1574
1575 rc = ena_rss_configure(adapter);
1576 if (rc)
1577 return rc;
1578
1579 ena_init_napi(adapter);
1580
1581 ena_change_mtu(adapter->netdev, adapter->netdev->mtu);
1582
1583 ena_refill_all_rx_bufs(adapter);
1584
1585 /* enable transmits */
1586 netif_tx_start_all_queues(adapter->netdev);
1587
1588 ena_restore_ethtool_params(adapter);
1589
1590 ena_napi_enable_all(adapter);
1591
Netanel Belgazal418df302017-06-11 15:42:44 +03001592 /* Enable completion queues interrupt */
1593 for (i = 0; i < adapter->num_queues; i++)
1594 ena_unmask_interrupt(&adapter->tx_ring[i],
1595 &adapter->rx_ring[i]);
1596
Netanel Belgazal1738cd32016-08-10 14:03:22 +03001597 /* schedule napi in case we had pending packets
1598 * from the last time we disable napi
1599 */
1600 for (i = 0; i < adapter->num_queues; i++)
1601 napi_schedule(&adapter->ena_napi[i].napi);
1602
1603 return 0;
1604}
1605
1606static int ena_create_io_tx_queue(struct ena_adapter *adapter, int qid)
1607{
1608 struct ena_com_create_io_ctx ctx = { 0 };
1609 struct ena_com_dev *ena_dev;
1610 struct ena_ring *tx_ring;
1611 u32 msix_vector;
1612 u16 ena_qid;
1613 int rc;
1614
1615 ena_dev = adapter->ena_dev;
1616
1617 tx_ring = &adapter->tx_ring[qid];
1618 msix_vector = ENA_IO_IRQ_IDX(qid);
1619 ena_qid = ENA_IO_TXQ_IDX(qid);
1620
1621 ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_TX;
1622 ctx.qid = ena_qid;
1623 ctx.mem_queue_type = ena_dev->tx_mem_queue_type;
1624 ctx.msix_vector = msix_vector;
1625 ctx.queue_size = adapter->tx_ring_size;
1626 ctx.numa_node = cpu_to_node(tx_ring->cpu);
1627
1628 rc = ena_com_create_io_queue(ena_dev, &ctx);
1629 if (rc) {
1630 netif_err(adapter, ifup, adapter->netdev,
1631 "Failed to create I/O TX queue num %d rc: %d\n",
1632 qid, rc);
1633 return rc;
1634 }
1635
1636 rc = ena_com_get_io_handlers(ena_dev, ena_qid,
1637 &tx_ring->ena_com_io_sq,
1638 &tx_ring->ena_com_io_cq);
1639 if (rc) {
1640 netif_err(adapter, ifup, adapter->netdev,
1641 "Failed to get TX queue handlers. TX queue num %d rc: %d\n",
1642 qid, rc);
1643 ena_com_destroy_io_queue(ena_dev, ena_qid);
Netanel Belgazal2d2c6002017-06-11 15:42:45 +03001644 return rc;
Netanel Belgazal1738cd32016-08-10 14:03:22 +03001645 }
1646
1647 ena_com_update_numa_node(tx_ring->ena_com_io_cq, ctx.numa_node);
1648 return rc;
1649}
1650
1651static int ena_create_all_io_tx_queues(struct ena_adapter *adapter)
1652{
1653 struct ena_com_dev *ena_dev = adapter->ena_dev;
1654 int rc, i;
1655
1656 for (i = 0; i < adapter->num_queues; i++) {
1657 rc = ena_create_io_tx_queue(adapter, i);
1658 if (rc)
1659 goto create_err;
1660 }
1661
1662 return 0;
1663
1664create_err:
1665 while (i--)
1666 ena_com_destroy_io_queue(ena_dev, ENA_IO_TXQ_IDX(i));
1667
1668 return rc;
1669}
1670
1671static int ena_create_io_rx_queue(struct ena_adapter *adapter, int qid)
1672{
1673 struct ena_com_dev *ena_dev;
1674 struct ena_com_create_io_ctx ctx = { 0 };
1675 struct ena_ring *rx_ring;
1676 u32 msix_vector;
1677 u16 ena_qid;
1678 int rc;
1679
1680 ena_dev = adapter->ena_dev;
1681
1682 rx_ring = &adapter->rx_ring[qid];
1683 msix_vector = ENA_IO_IRQ_IDX(qid);
1684 ena_qid = ENA_IO_RXQ_IDX(qid);
1685
1686 ctx.qid = ena_qid;
1687 ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_RX;
1688 ctx.mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
1689 ctx.msix_vector = msix_vector;
1690 ctx.queue_size = adapter->rx_ring_size;
1691 ctx.numa_node = cpu_to_node(rx_ring->cpu);
1692
1693 rc = ena_com_create_io_queue(ena_dev, &ctx);
1694 if (rc) {
1695 netif_err(adapter, ifup, adapter->netdev,
1696 "Failed to create I/O RX queue num %d rc: %d\n",
1697 qid, rc);
1698 return rc;
1699 }
1700
1701 rc = ena_com_get_io_handlers(ena_dev, ena_qid,
1702 &rx_ring->ena_com_io_sq,
1703 &rx_ring->ena_com_io_cq);
1704 if (rc) {
1705 netif_err(adapter, ifup, adapter->netdev,
1706 "Failed to get RX queue handlers. RX queue num %d rc: %d\n",
1707 qid, rc);
1708 ena_com_destroy_io_queue(ena_dev, ena_qid);
Netanel Belgazal2d2c6002017-06-11 15:42:45 +03001709 return rc;
Netanel Belgazal1738cd32016-08-10 14:03:22 +03001710 }
1711
1712 ena_com_update_numa_node(rx_ring->ena_com_io_cq, ctx.numa_node);
1713
1714 return rc;
1715}
1716
1717static int ena_create_all_io_rx_queues(struct ena_adapter *adapter)
1718{
1719 struct ena_com_dev *ena_dev = adapter->ena_dev;
1720 int rc, i;
1721
1722 for (i = 0; i < adapter->num_queues; i++) {
1723 rc = ena_create_io_rx_queue(adapter, i);
1724 if (rc)
1725 goto create_err;
1726 }
1727
1728 return 0;
1729
1730create_err:
1731 while (i--)
1732 ena_com_destroy_io_queue(ena_dev, ENA_IO_RXQ_IDX(i));
1733
1734 return rc;
1735}
1736
1737static int ena_up(struct ena_adapter *adapter)
1738{
1739 int rc;
1740
1741 netdev_dbg(adapter->netdev, "%s\n", __func__);
1742
1743 ena_setup_io_intr(adapter);
1744
1745 rc = ena_request_io_irq(adapter);
1746 if (rc)
1747 goto err_req_irq;
1748
1749 /* allocate transmit descriptors */
1750 rc = ena_setup_all_tx_resources(adapter);
1751 if (rc)
1752 goto err_setup_tx;
1753
1754 /* allocate receive descriptors */
1755 rc = ena_setup_all_rx_resources(adapter);
1756 if (rc)
1757 goto err_setup_rx;
1758
1759 /* Create TX queues */
1760 rc = ena_create_all_io_tx_queues(adapter);
1761 if (rc)
1762 goto err_create_tx_queues;
1763
1764 /* Create RX queues */
1765 rc = ena_create_all_io_rx_queues(adapter);
1766 if (rc)
1767 goto err_create_rx_queues;
1768
1769 rc = ena_up_complete(adapter);
1770 if (rc)
1771 goto err_up;
1772
1773 if (test_bit(ENA_FLAG_LINK_UP, &adapter->flags))
1774 netif_carrier_on(adapter->netdev);
1775
1776 u64_stats_update_begin(&adapter->syncp);
1777 adapter->dev_stats.interface_up++;
1778 u64_stats_update_end(&adapter->syncp);
1779
1780 set_bit(ENA_FLAG_DEV_UP, &adapter->flags);
1781
1782 return rc;
1783
1784err_up:
1785 ena_destroy_all_rx_queues(adapter);
1786err_create_rx_queues:
1787 ena_destroy_all_tx_queues(adapter);
1788err_create_tx_queues:
1789 ena_free_all_io_rx_resources(adapter);
1790err_setup_rx:
1791 ena_free_all_io_tx_resources(adapter);
1792err_setup_tx:
1793 ena_free_io_irq(adapter);
1794err_req_irq:
1795
1796 return rc;
1797}
1798
1799static void ena_down(struct ena_adapter *adapter)
1800{
1801 netif_info(adapter, ifdown, adapter->netdev, "%s\n", __func__);
1802
1803 clear_bit(ENA_FLAG_DEV_UP, &adapter->flags);
1804
1805 u64_stats_update_begin(&adapter->syncp);
1806 adapter->dev_stats.interface_down++;
1807 u64_stats_update_end(&adapter->syncp);
1808
Netanel Belgazal1738cd32016-08-10 14:03:22 +03001809 netif_carrier_off(adapter->netdev);
1810 netif_tx_disable(adapter->netdev);
1811
Netanel Belgazal3f6159d2017-02-09 15:21:33 +02001812 /* After this point the napi handler won't enable the tx queue */
1813 ena_napi_disable_all(adapter);
1814
Netanel Belgazal1738cd32016-08-10 14:03:22 +03001815 /* After destroy the queue there won't be any new interrupts */
Netanel Belgazal3f6159d2017-02-09 15:21:33 +02001816
1817 if (test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags)) {
1818 int rc;
1819
Netanel Belgazale2eed0e2017-06-23 11:21:53 +03001820 rc = ena_com_dev_reset(adapter->ena_dev, adapter->reset_reason);
Netanel Belgazal3f6159d2017-02-09 15:21:33 +02001821 if (rc)
1822 dev_err(&adapter->pdev->dev, "Device reset failed\n");
1823 }
1824
Netanel Belgazal1738cd32016-08-10 14:03:22 +03001825 ena_destroy_all_io_queues(adapter);
1826
1827 ena_disable_io_intr_sync(adapter);
1828 ena_free_io_irq(adapter);
1829 ena_del_napi(adapter);
1830
1831 ena_free_all_tx_bufs(adapter);
1832 ena_free_all_rx_bufs(adapter);
1833 ena_free_all_io_tx_resources(adapter);
1834 ena_free_all_io_rx_resources(adapter);
1835}
1836
1837/* ena_open - Called when a network interface is made active
1838 * @netdev: network interface device structure
1839 *
1840 * Returns 0 on success, negative value on failure
1841 *
1842 * The open entry point is called when a network interface is made
1843 * active by the system (IFF_UP). At this point all resources needed
1844 * for transmit and receive operations are allocated, the interrupt
1845 * handler is registered with the OS, the watchdog timer is started,
1846 * and the stack is notified that the interface is ready.
1847 */
1848static int ena_open(struct net_device *netdev)
1849{
1850 struct ena_adapter *adapter = netdev_priv(netdev);
1851 int rc;
1852
1853 /* Notify the stack of the actual queue counts. */
1854 rc = netif_set_real_num_tx_queues(netdev, adapter->num_queues);
1855 if (rc) {
1856 netif_err(adapter, ifup, netdev, "Can't set num tx queues\n");
1857 return rc;
1858 }
1859
1860 rc = netif_set_real_num_rx_queues(netdev, adapter->num_queues);
1861 if (rc) {
1862 netif_err(adapter, ifup, netdev, "Can't set num rx queues\n");
1863 return rc;
1864 }
1865
1866 rc = ena_up(adapter);
1867 if (rc)
1868 return rc;
1869
1870 return rc;
1871}
1872
1873/* ena_close - Disables a network interface
1874 * @netdev: network interface device structure
1875 *
1876 * Returns 0, this is not allowed to fail
1877 *
1878 * The close entry point is called when an interface is de-activated
1879 * by the OS. The hardware is still under the drivers control, but
1880 * needs to be disabled. A global MAC reset is issued to stop the
1881 * hardware, and all transmit and receive resources are freed.
1882 */
1883static int ena_close(struct net_device *netdev)
1884{
1885 struct ena_adapter *adapter = netdev_priv(netdev);
1886
1887 netif_dbg(adapter, ifdown, netdev, "%s\n", __func__);
1888
1889 if (test_bit(ENA_FLAG_DEV_UP, &adapter->flags))
1890 ena_down(adapter);
1891
1892 return 0;
1893}
1894
1895static void ena_tx_csum(struct ena_com_tx_ctx *ena_tx_ctx, struct sk_buff *skb)
1896{
1897 u32 mss = skb_shinfo(skb)->gso_size;
1898 struct ena_com_tx_meta *ena_meta = &ena_tx_ctx->ena_meta;
1899 u8 l4_protocol = 0;
1900
1901 if ((skb->ip_summed == CHECKSUM_PARTIAL) || mss) {
1902 ena_tx_ctx->l4_csum_enable = 1;
1903 if (mss) {
1904 ena_tx_ctx->tso_enable = 1;
1905 ena_meta->l4_hdr_len = tcp_hdr(skb)->doff;
1906 ena_tx_ctx->l4_csum_partial = 0;
1907 } else {
1908 ena_tx_ctx->tso_enable = 0;
1909 ena_meta->l4_hdr_len = 0;
1910 ena_tx_ctx->l4_csum_partial = 1;
1911 }
1912
1913 switch (ip_hdr(skb)->version) {
1914 case IPVERSION:
1915 ena_tx_ctx->l3_proto = ENA_ETH_IO_L3_PROTO_IPV4;
1916 if (ip_hdr(skb)->frag_off & htons(IP_DF))
1917 ena_tx_ctx->df = 1;
1918 if (mss)
1919 ena_tx_ctx->l3_csum_enable = 1;
1920 l4_protocol = ip_hdr(skb)->protocol;
1921 break;
1922 case 6:
1923 ena_tx_ctx->l3_proto = ENA_ETH_IO_L3_PROTO_IPV6;
1924 l4_protocol = ipv6_hdr(skb)->nexthdr;
1925 break;
1926 default:
1927 break;
1928 }
1929
1930 if (l4_protocol == IPPROTO_TCP)
1931 ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_TCP;
1932 else
1933 ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_UDP;
1934
1935 ena_meta->mss = mss;
1936 ena_meta->l3_hdr_len = skb_network_header_len(skb);
1937 ena_meta->l3_hdr_offset = skb_network_offset(skb);
1938 ena_tx_ctx->meta_valid = 1;
1939
1940 } else {
1941 ena_tx_ctx->meta_valid = 0;
1942 }
1943}
1944
1945static int ena_check_and_linearize_skb(struct ena_ring *tx_ring,
1946 struct sk_buff *skb)
1947{
1948 int num_frags, header_len, rc;
1949
1950 num_frags = skb_shinfo(skb)->nr_frags;
1951 header_len = skb_headlen(skb);
1952
1953 if (num_frags < tx_ring->sgl_size)
1954 return 0;
1955
1956 if ((num_frags == tx_ring->sgl_size) &&
1957 (header_len < tx_ring->tx_max_header_size))
1958 return 0;
1959
1960 u64_stats_update_begin(&tx_ring->syncp);
1961 tx_ring->tx_stats.linearize++;
1962 u64_stats_update_end(&tx_ring->syncp);
1963
1964 rc = skb_linearize(skb);
1965 if (unlikely(rc)) {
1966 u64_stats_update_begin(&tx_ring->syncp);
1967 tx_ring->tx_stats.linearize_failed++;
1968 u64_stats_update_end(&tx_ring->syncp);
1969 }
1970
1971 return rc;
1972}
1973
1974/* Called with netif_tx_lock. */
1975static netdev_tx_t ena_start_xmit(struct sk_buff *skb, struct net_device *dev)
1976{
1977 struct ena_adapter *adapter = netdev_priv(dev);
1978 struct ena_tx_buffer *tx_info;
1979 struct ena_com_tx_ctx ena_tx_ctx;
1980 struct ena_ring *tx_ring;
1981 struct netdev_queue *txq;
1982 struct ena_com_buf *ena_buf;
1983 void *push_hdr;
1984 u32 len, last_frag;
1985 u16 next_to_use;
1986 u16 req_id;
1987 u16 push_len;
1988 u16 header_len;
1989 dma_addr_t dma;
1990 int qid, rc, nb_hw_desc;
1991 int i = -1;
1992
1993 netif_dbg(adapter, tx_queued, dev, "%s skb %p\n", __func__, skb);
1994 /* Determine which tx ring we will be placed on */
1995 qid = skb_get_queue_mapping(skb);
1996 tx_ring = &adapter->tx_ring[qid];
1997 txq = netdev_get_tx_queue(dev, qid);
1998
1999 rc = ena_check_and_linearize_skb(tx_ring, skb);
2000 if (unlikely(rc))
2001 goto error_drop_packet;
2002
2003 skb_tx_timestamp(skb);
2004 len = skb_headlen(skb);
2005
2006 next_to_use = tx_ring->next_to_use;
2007 req_id = tx_ring->free_tx_ids[next_to_use];
2008 tx_info = &tx_ring->tx_buffer_info[req_id];
2009 tx_info->num_of_bufs = 0;
2010
2011 WARN(tx_info->skb, "SKB isn't NULL req_id %d\n", req_id);
2012 ena_buf = tx_info->bufs;
2013 tx_info->skb = skb;
2014
2015 if (tx_ring->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV) {
2016 /* prepared the push buffer */
2017 push_len = min_t(u32, len, tx_ring->tx_max_header_size);
2018 header_len = push_len;
2019 push_hdr = skb->data;
2020 } else {
2021 push_len = 0;
2022 header_len = min_t(u32, len, tx_ring->tx_max_header_size);
2023 push_hdr = NULL;
2024 }
2025
2026 netif_dbg(adapter, tx_queued, dev,
2027 "skb: %p header_buf->vaddr: %p push_len: %d\n", skb,
2028 push_hdr, push_len);
2029
2030 if (len > push_len) {
2031 dma = dma_map_single(tx_ring->dev, skb->data + push_len,
2032 len - push_len, DMA_TO_DEVICE);
2033 if (dma_mapping_error(tx_ring->dev, dma))
2034 goto error_report_dma_error;
2035
2036 ena_buf->paddr = dma;
2037 ena_buf->len = len - push_len;
2038
2039 ena_buf++;
2040 tx_info->num_of_bufs++;
2041 }
2042
2043 last_frag = skb_shinfo(skb)->nr_frags;
2044
2045 for (i = 0; i < last_frag; i++) {
2046 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2047
2048 len = skb_frag_size(frag);
2049 dma = skb_frag_dma_map(tx_ring->dev, frag, 0, len,
2050 DMA_TO_DEVICE);
2051 if (dma_mapping_error(tx_ring->dev, dma))
2052 goto error_report_dma_error;
2053
2054 ena_buf->paddr = dma;
2055 ena_buf->len = len;
2056 ena_buf++;
2057 }
2058
2059 tx_info->num_of_bufs += last_frag;
2060
2061 memset(&ena_tx_ctx, 0x0, sizeof(struct ena_com_tx_ctx));
2062 ena_tx_ctx.ena_bufs = tx_info->bufs;
2063 ena_tx_ctx.push_header = push_hdr;
2064 ena_tx_ctx.num_bufs = tx_info->num_of_bufs;
2065 ena_tx_ctx.req_id = req_id;
2066 ena_tx_ctx.header_len = header_len;
2067
2068 /* set flags and meta data */
2069 ena_tx_csum(&ena_tx_ctx, skb);
2070
2071 /* prepare the packet's descriptors to dma engine */
2072 rc = ena_com_prepare_tx(tx_ring->ena_com_io_sq, &ena_tx_ctx,
2073 &nb_hw_desc);
2074
2075 if (unlikely(rc)) {
2076 netif_err(adapter, tx_queued, dev,
2077 "failed to prepare tx bufs\n");
2078 u64_stats_update_begin(&tx_ring->syncp);
2079 tx_ring->tx_stats.queue_stop++;
2080 tx_ring->tx_stats.prepare_ctx_err++;
2081 u64_stats_update_end(&tx_ring->syncp);
2082 netif_tx_stop_queue(txq);
2083 goto error_unmap_dma;
2084 }
2085
2086 netdev_tx_sent_queue(txq, skb->len);
2087
2088 u64_stats_update_begin(&tx_ring->syncp);
2089 tx_ring->tx_stats.cnt++;
2090 tx_ring->tx_stats.bytes += skb->len;
2091 u64_stats_update_end(&tx_ring->syncp);
2092
2093 tx_info->tx_descs = nb_hw_desc;
2094 tx_info->last_jiffies = jiffies;
Netanel Belgazal800c55c2017-06-11 15:42:50 +03002095 tx_info->print_once = 0;
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002096
2097 tx_ring->next_to_use = ENA_TX_RING_IDX_NEXT(next_to_use,
2098 tx_ring->ring_size);
2099
2100 /* This WMB is aimed to:
2101 * 1 - perform smp barrier before reading next_to_completion
2102 * 2 - make sure the desc were written before trigger DB
2103 */
2104 wmb();
2105
2106 /* stop the queue when no more space available, the packet can have up
2107 * to sgl_size + 2. one for the meta descriptor and one for header
2108 * (if the header is larger than tx_max_header_size).
2109 */
2110 if (unlikely(ena_com_sq_empty_space(tx_ring->ena_com_io_sq) <
2111 (tx_ring->sgl_size + 2))) {
2112 netif_dbg(adapter, tx_queued, dev, "%s stop queue %d\n",
2113 __func__, qid);
2114
2115 netif_tx_stop_queue(txq);
2116 u64_stats_update_begin(&tx_ring->syncp);
2117 tx_ring->tx_stats.queue_stop++;
2118 u64_stats_update_end(&tx_ring->syncp);
2119
2120 /* There is a rare condition where this function decide to
2121 * stop the queue but meanwhile clean_tx_irq updates
2122 * next_to_completion and terminates.
2123 * The queue will remain stopped forever.
2124 * To solve this issue this function perform rmb, check
2125 * the wakeup condition and wake up the queue if needed.
2126 */
2127 smp_rmb();
2128
2129 if (ena_com_sq_empty_space(tx_ring->ena_com_io_sq)
2130 > ENA_TX_WAKEUP_THRESH) {
2131 netif_tx_wake_queue(txq);
2132 u64_stats_update_begin(&tx_ring->syncp);
2133 tx_ring->tx_stats.queue_wakeup++;
2134 u64_stats_update_end(&tx_ring->syncp);
2135 }
2136 }
2137
2138 if (netif_xmit_stopped(txq) || !skb->xmit_more) {
2139 /* trigger the dma engine */
2140 ena_com_write_sq_doorbell(tx_ring->ena_com_io_sq);
2141 u64_stats_update_begin(&tx_ring->syncp);
2142 tx_ring->tx_stats.doorbells++;
2143 u64_stats_update_end(&tx_ring->syncp);
2144 }
2145
2146 return NETDEV_TX_OK;
2147
2148error_report_dma_error:
2149 u64_stats_update_begin(&tx_ring->syncp);
2150 tx_ring->tx_stats.dma_mapping_err++;
2151 u64_stats_update_end(&tx_ring->syncp);
2152 netdev_warn(adapter->netdev, "failed to map skb\n");
2153
2154 tx_info->skb = NULL;
2155
2156error_unmap_dma:
2157 if (i >= 0) {
2158 /* save value of frag that failed */
2159 last_frag = i;
2160
2161 /* start back at beginning and unmap skb */
2162 tx_info->skb = NULL;
2163 ena_buf = tx_info->bufs;
2164 dma_unmap_single(tx_ring->dev, dma_unmap_addr(ena_buf, paddr),
2165 dma_unmap_len(ena_buf, len), DMA_TO_DEVICE);
2166
2167 /* unmap remaining mapped pages */
2168 for (i = 0; i < last_frag; i++) {
2169 ena_buf++;
2170 dma_unmap_page(tx_ring->dev, dma_unmap_addr(ena_buf, paddr),
2171 dma_unmap_len(ena_buf, len), DMA_TO_DEVICE);
2172 }
2173 }
2174
2175error_drop_packet:
2176
2177 dev_kfree_skb(skb);
2178 return NETDEV_TX_OK;
2179}
2180
2181#ifdef CONFIG_NET_POLL_CONTROLLER
2182static void ena_netpoll(struct net_device *netdev)
2183{
2184 struct ena_adapter *adapter = netdev_priv(netdev);
2185 int i;
2186
Netanel Belgazal3f6159d2017-02-09 15:21:33 +02002187 /* Dont schedule NAPI if the driver is in the middle of reset
2188 * or netdev is down.
2189 */
2190
2191 if (!test_bit(ENA_FLAG_DEV_UP, &adapter->flags) ||
2192 test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags))
2193 return;
2194
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002195 for (i = 0; i < adapter->num_queues; i++)
2196 napi_schedule(&adapter->ena_napi[i].napi);
2197}
2198#endif /* CONFIG_NET_POLL_CONTROLLER */
2199
2200static u16 ena_select_queue(struct net_device *dev, struct sk_buff *skb,
2201 void *accel_priv, select_queue_fallback_t fallback)
2202{
2203 u16 qid;
2204 /* we suspect that this is good for in--kernel network services that
2205 * want to loop incoming skb rx to tx in normal user generated traffic,
2206 * most probably we will not get to this
2207 */
2208 if (skb_rx_queue_recorded(skb))
2209 qid = skb_get_rx_queue(skb);
2210 else
2211 qid = fallback(dev, skb);
2212
2213 return qid;
2214}
2215
2216static void ena_config_host_info(struct ena_com_dev *ena_dev)
2217{
2218 struct ena_admin_host_info *host_info;
2219 int rc;
2220
2221 /* Allocate only the host info */
2222 rc = ena_com_allocate_host_info(ena_dev);
2223 if (rc) {
2224 pr_err("Cannot allocate host info\n");
2225 return;
2226 }
2227
2228 host_info = ena_dev->host_attr.host_info;
2229
2230 host_info->os_type = ENA_ADMIN_OS_LINUX;
2231 host_info->kernel_ver = LINUX_VERSION_CODE;
2232 strncpy(host_info->kernel_ver_str, utsname()->version,
2233 sizeof(host_info->kernel_ver_str) - 1);
2234 host_info->os_dist = 0;
2235 strncpy(host_info->os_dist_str, utsname()->release,
2236 sizeof(host_info->os_dist_str) - 1);
2237 host_info->driver_version =
2238 (DRV_MODULE_VER_MAJOR) |
2239 (DRV_MODULE_VER_MINOR << ENA_ADMIN_HOST_INFO_MINOR_SHIFT) |
2240 (DRV_MODULE_VER_SUBMINOR << ENA_ADMIN_HOST_INFO_SUB_MINOR_SHIFT);
2241
2242 rc = ena_com_set_host_attributes(ena_dev);
2243 if (rc) {
Netanel Belgazald1497632017-06-23 11:21:50 +03002244 if (rc == -EOPNOTSUPP)
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002245 pr_warn("Cannot set host attributes\n");
2246 else
2247 pr_err("Cannot set host attributes\n");
2248
2249 goto err;
2250 }
2251
2252 return;
2253
2254err:
2255 ena_com_delete_host_info(ena_dev);
2256}
2257
2258static void ena_config_debug_area(struct ena_adapter *adapter)
2259{
2260 u32 debug_area_size;
2261 int rc, ss_count;
2262
2263 ss_count = ena_get_sset_count(adapter->netdev, ETH_SS_STATS);
2264 if (ss_count <= 0) {
2265 netif_err(adapter, drv, adapter->netdev,
2266 "SS count is negative\n");
2267 return;
2268 }
2269
2270 /* allocate 32 bytes for each string and 64bit for the value */
2271 debug_area_size = ss_count * ETH_GSTRING_LEN + sizeof(u64) * ss_count;
2272
2273 rc = ena_com_allocate_debug_area(adapter->ena_dev, debug_area_size);
2274 if (rc) {
2275 pr_err("Cannot allocate debug area\n");
2276 return;
2277 }
2278
2279 rc = ena_com_set_host_attributes(adapter->ena_dev);
2280 if (rc) {
Netanel Belgazald1497632017-06-23 11:21:50 +03002281 if (rc == -EOPNOTSUPP)
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002282 netif_warn(adapter, drv, adapter->netdev,
2283 "Cannot set host attributes\n");
2284 else
2285 netif_err(adapter, drv, adapter->netdev,
2286 "Cannot set host attributes\n");
2287 goto err;
2288 }
2289
2290 return;
2291err:
2292 ena_com_delete_debug_area(adapter->ena_dev);
2293}
2294
stephen hemmingerbc1f4472017-01-06 19:12:52 -08002295static void ena_get_stats64(struct net_device *netdev,
2296 struct rtnl_link_stats64 *stats)
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002297{
2298 struct ena_adapter *adapter = netdev_priv(netdev);
Netanel Belgazald81db2402017-02-09 15:21:32 +02002299 struct ena_ring *rx_ring, *tx_ring;
2300 unsigned int start;
2301 u64 rx_drops;
2302 int i;
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002303
2304 if (!test_bit(ENA_FLAG_DEV_UP, &adapter->flags))
stephen hemmingerbc1f4472017-01-06 19:12:52 -08002305 return;
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002306
Netanel Belgazald81db2402017-02-09 15:21:32 +02002307 for (i = 0; i < adapter->num_queues; i++) {
2308 u64 bytes, packets;
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002309
Netanel Belgazald81db2402017-02-09 15:21:32 +02002310 tx_ring = &adapter->tx_ring[i];
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002311
Netanel Belgazald81db2402017-02-09 15:21:32 +02002312 do {
2313 start = u64_stats_fetch_begin_irq(&tx_ring->syncp);
2314 packets = tx_ring->tx_stats.cnt;
2315 bytes = tx_ring->tx_stats.bytes;
2316 } while (u64_stats_fetch_retry_irq(&tx_ring->syncp, start));
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002317
Netanel Belgazald81db2402017-02-09 15:21:32 +02002318 stats->tx_packets += packets;
2319 stats->tx_bytes += bytes;
2320
2321 rx_ring = &adapter->rx_ring[i];
2322
2323 do {
2324 start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
2325 packets = rx_ring->rx_stats.cnt;
2326 bytes = rx_ring->rx_stats.bytes;
2327 } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
2328
2329 stats->rx_packets += packets;
2330 stats->rx_bytes += bytes;
2331 }
2332
2333 do {
2334 start = u64_stats_fetch_begin_irq(&adapter->syncp);
2335 rx_drops = adapter->dev_stats.rx_drops;
2336 } while (u64_stats_fetch_retry_irq(&adapter->syncp, start));
2337
2338 stats->rx_dropped = rx_drops;
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002339
2340 stats->multicast = 0;
2341 stats->collisions = 0;
2342
2343 stats->rx_length_errors = 0;
2344 stats->rx_crc_errors = 0;
2345 stats->rx_frame_errors = 0;
2346 stats->rx_fifo_errors = 0;
2347 stats->rx_missed_errors = 0;
2348 stats->tx_window_errors = 0;
2349
2350 stats->rx_errors = 0;
2351 stats->tx_errors = 0;
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002352}
2353
2354static const struct net_device_ops ena_netdev_ops = {
2355 .ndo_open = ena_open,
2356 .ndo_stop = ena_close,
2357 .ndo_start_xmit = ena_start_xmit,
2358 .ndo_select_queue = ena_select_queue,
2359 .ndo_get_stats64 = ena_get_stats64,
2360 .ndo_tx_timeout = ena_tx_timeout,
2361 .ndo_change_mtu = ena_change_mtu,
2362 .ndo_set_mac_address = NULL,
2363 .ndo_validate_addr = eth_validate_addr,
2364#ifdef CONFIG_NET_POLL_CONTROLLER
2365 .ndo_poll_controller = ena_netpoll,
2366#endif /* CONFIG_NET_POLL_CONTROLLER */
2367};
2368
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002369static int ena_device_validate_params(struct ena_adapter *adapter,
2370 struct ena_com_dev_get_features_ctx *get_feat_ctx)
2371{
2372 struct net_device *netdev = adapter->netdev;
2373 int rc;
2374
2375 rc = ether_addr_equal(get_feat_ctx->dev_attr.mac_addr,
2376 adapter->mac_addr);
2377 if (!rc) {
2378 netif_err(adapter, drv, netdev,
2379 "Error, mac address are different\n");
2380 return -EINVAL;
2381 }
2382
2383 if ((get_feat_ctx->max_queues.max_cq_num < adapter->num_queues) ||
2384 (get_feat_ctx->max_queues.max_sq_num < adapter->num_queues)) {
2385 netif_err(adapter, drv, netdev,
2386 "Error, device doesn't support enough queues\n");
2387 return -EINVAL;
2388 }
2389
2390 if (get_feat_ctx->dev_attr.max_mtu < netdev->mtu) {
2391 netif_err(adapter, drv, netdev,
2392 "Error, device max mtu is smaller than netdev MTU\n");
2393 return -EINVAL;
2394 }
2395
2396 return 0;
2397}
2398
2399static int ena_device_init(struct ena_com_dev *ena_dev, struct pci_dev *pdev,
2400 struct ena_com_dev_get_features_ctx *get_feat_ctx,
2401 bool *wd_state)
2402{
2403 struct device *dev = &pdev->dev;
2404 bool readless_supported;
2405 u32 aenq_groups;
2406 int dma_width;
2407 int rc;
2408
2409 rc = ena_com_mmio_reg_read_request_init(ena_dev);
2410 if (rc) {
2411 dev_err(dev, "failed to init mmio read less\n");
2412 return rc;
2413 }
2414
2415 /* The PCIe configuration space revision id indicate if mmio reg
2416 * read is disabled
2417 */
2418 readless_supported = !(pdev->revision & ENA_MMIO_DISABLE_REG_READ);
2419 ena_com_set_mmio_read_mode(ena_dev, readless_supported);
2420
Netanel Belgazale2eed0e2017-06-23 11:21:53 +03002421 rc = ena_com_dev_reset(ena_dev, ENA_REGS_RESET_NORMAL);
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002422 if (rc) {
2423 dev_err(dev, "Can not reset device\n");
2424 goto err_mmio_read_less;
2425 }
2426
2427 rc = ena_com_validate_version(ena_dev);
2428 if (rc) {
2429 dev_err(dev, "device version is too low\n");
2430 goto err_mmio_read_less;
2431 }
2432
2433 dma_width = ena_com_get_dma_width(ena_dev);
2434 if (dma_width < 0) {
2435 dev_err(dev, "Invalid dma width value %d", dma_width);
Wei Yongjun6e22066f2016-08-15 22:51:04 +00002436 rc = dma_width;
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002437 goto err_mmio_read_less;
2438 }
2439
2440 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(dma_width));
2441 if (rc) {
2442 dev_err(dev, "pci_set_dma_mask failed 0x%x\n", rc);
2443 goto err_mmio_read_less;
2444 }
2445
2446 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(dma_width));
2447 if (rc) {
2448 dev_err(dev, "err_pci_set_consistent_dma_mask failed 0x%x\n",
2449 rc);
2450 goto err_mmio_read_less;
2451 }
2452
2453 /* ENA admin level init */
2454 rc = ena_com_admin_init(ena_dev, &aenq_handlers, true);
2455 if (rc) {
2456 dev_err(dev,
2457 "Can not initialize ena admin queue with device\n");
2458 goto err_mmio_read_less;
2459 }
2460
2461 /* To enable the msix interrupts the driver needs to know the number
2462 * of queues. So the driver uses polling mode to retrieve this
2463 * information
2464 */
2465 ena_com_set_admin_polling_mode(ena_dev, true);
2466
Netanel Belgazaldd8427a2017-02-09 15:21:38 +02002467 ena_config_host_info(ena_dev);
2468
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002469 /* Get Device Attributes*/
2470 rc = ena_com_get_dev_attr_feat(ena_dev, get_feat_ctx);
2471 if (rc) {
2472 dev_err(dev, "Cannot get attribute for ena device rc=%d\n", rc);
2473 goto err_admin_init;
2474 }
2475
2476 /* Try to turn all the available aenq groups */
2477 aenq_groups = BIT(ENA_ADMIN_LINK_CHANGE) |
2478 BIT(ENA_ADMIN_FATAL_ERROR) |
2479 BIT(ENA_ADMIN_WARNING) |
2480 BIT(ENA_ADMIN_NOTIFICATION) |
2481 BIT(ENA_ADMIN_KEEP_ALIVE);
2482
2483 aenq_groups &= get_feat_ctx->aenq.supported_groups;
2484
2485 rc = ena_com_set_aenq_config(ena_dev, aenq_groups);
2486 if (rc) {
2487 dev_err(dev, "Cannot configure aenq groups rc= %d\n", rc);
2488 goto err_admin_init;
2489 }
2490
2491 *wd_state = !!(aenq_groups & BIT(ENA_ADMIN_KEEP_ALIVE));
2492
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002493 return 0;
2494
2495err_admin_init:
Netanel Belgazaldd8427a2017-02-09 15:21:38 +02002496 ena_com_delete_host_info(ena_dev);
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002497 ena_com_admin_destroy(ena_dev);
2498err_mmio_read_less:
2499 ena_com_mmio_reg_read_request_destroy(ena_dev);
2500
2501 return rc;
2502}
2503
2504static int ena_enable_msix_and_set_admin_interrupts(struct ena_adapter *adapter,
2505 int io_vectors)
2506{
2507 struct ena_com_dev *ena_dev = adapter->ena_dev;
2508 struct device *dev = &adapter->pdev->dev;
2509 int rc;
2510
2511 rc = ena_enable_msix(adapter, io_vectors);
2512 if (rc) {
2513 dev_err(dev, "Can not reserve msix vectors\n");
2514 return rc;
2515 }
2516
2517 ena_setup_mgmnt_intr(adapter);
2518
2519 rc = ena_request_mgmnt_irq(adapter);
2520 if (rc) {
2521 dev_err(dev, "Can not setup management interrupts\n");
2522 goto err_disable_msix;
2523 }
2524
2525 ena_com_set_admin_polling_mode(ena_dev, false);
2526
2527 ena_com_admin_aenq_enable(ena_dev);
2528
2529 return 0;
2530
2531err_disable_msix:
Netanel Belgazal06443682017-06-23 11:21:55 +03002532 ena_disable_msix(adapter);
2533
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002534 return rc;
2535}
2536
Netanel Belgazal8c5c7ab2017-10-17 07:33:58 +00002537static void ena_destroy_device(struct ena_adapter *adapter)
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002538{
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002539 struct net_device *netdev = adapter->netdev;
2540 struct ena_com_dev *ena_dev = adapter->ena_dev;
Netanel Belgazal8c5c7ab2017-10-17 07:33:58 +00002541 bool dev_up;
Netanel Belgazal3f6159d2017-02-09 15:21:33 +02002542
2543 netif_carrier_off(netdev);
2544
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002545 del_timer_sync(&adapter->timer_service);
2546
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002547 dev_up = test_bit(ENA_FLAG_DEV_UP, &adapter->flags);
Netanel Belgazal8c5c7ab2017-10-17 07:33:58 +00002548 adapter->dev_up_before_reset = dev_up;
2549
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002550 ena_com_set_admin_running_state(ena_dev, false);
2551
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002552 ena_close(netdev);
2553
Netanel Belgazal8c5c7ab2017-10-17 07:33:58 +00002554 /* Before releasing the ENA resources, a device reset is required.
2555 * (to prevent the device from accessing them).
2556 * In case the reset flag is set and the device is up, ena_close
2557 * already perform the reset, so it can be skipped.
2558 */
2559 if (!(test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags) && dev_up))
2560 ena_com_dev_reset(adapter->ena_dev, adapter->reset_reason);
2561
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002562 ena_free_mgmnt_irq(adapter);
2563
Netanel Belgazal06443682017-06-23 11:21:55 +03002564 ena_disable_msix(adapter);
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002565
2566 ena_com_abort_admin_commands(ena_dev);
2567
2568 ena_com_wait_for_abort_completion(ena_dev);
2569
2570 ena_com_admin_destroy(ena_dev);
2571
2572 ena_com_mmio_reg_read_request_destroy(ena_dev);
2573
Netanel Belgazale2eed0e2017-06-23 11:21:53 +03002574 adapter->reset_reason = ENA_REGS_RESET_NORMAL;
Netanel Belgazal3f6159d2017-02-09 15:21:33 +02002575
Netanel Belgazal8c5c7ab2017-10-17 07:33:58 +00002576 clear_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags);
2577}
2578
2579static int ena_restore_device(struct ena_adapter *adapter)
2580{
2581 struct ena_com_dev_get_features_ctx get_feat_ctx;
2582 struct ena_com_dev *ena_dev = adapter->ena_dev;
2583 struct pci_dev *pdev = adapter->pdev;
2584 bool wd_state;
2585 int rc;
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002586
Netanel Belgazald18e4f62017-11-19 18:03:40 +00002587 set_bit(ENA_FLAG_ONGOING_RESET, &adapter->flags);
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002588 rc = ena_device_init(ena_dev, adapter->pdev, &get_feat_ctx, &wd_state);
2589 if (rc) {
2590 dev_err(&pdev->dev, "Can not initialize device\n");
2591 goto err;
2592 }
2593 adapter->wd_state = wd_state;
2594
2595 rc = ena_device_validate_params(adapter, &get_feat_ctx);
2596 if (rc) {
2597 dev_err(&pdev->dev, "Validation of device parameters failed\n");
2598 goto err_device_destroy;
2599 }
2600
Netanel Belgazald18e4f62017-11-19 18:03:40 +00002601 clear_bit(ENA_FLAG_ONGOING_RESET, &adapter->flags);
2602 /* Make sure we don't have a race with AENQ Links state handler */
2603 if (test_bit(ENA_FLAG_LINK_UP, &adapter->flags))
2604 netif_carrier_on(adapter->netdev);
2605
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002606 rc = ena_enable_msix_and_set_admin_interrupts(adapter,
2607 adapter->num_queues);
2608 if (rc) {
2609 dev_err(&pdev->dev, "Enable MSI-X failed\n");
2610 goto err_device_destroy;
2611 }
2612 /* If the interface was up before the reset bring it up */
Netanel Belgazal8c5c7ab2017-10-17 07:33:58 +00002613 if (adapter->dev_up_before_reset) {
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002614 rc = ena_up(adapter);
2615 if (rc) {
2616 dev_err(&pdev->dev, "Failed to create I/O queues\n");
2617 goto err_disable_msix;
2618 }
2619 }
2620
2621 mod_timer(&adapter->timer_service, round_jiffies(jiffies + HZ));
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002622 dev_err(&pdev->dev, "Device reset completed successfully\n");
2623
Netanel Belgazal8c5c7ab2017-10-17 07:33:58 +00002624 return rc;
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002625err_disable_msix:
2626 ena_free_mgmnt_irq(adapter);
Netanel Belgazal06443682017-06-23 11:21:55 +03002627 ena_disable_msix(adapter);
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002628err_device_destroy:
2629 ena_com_admin_destroy(ena_dev);
2630err:
Netanel Belgazal22b331c2017-02-09 15:21:31 +02002631 clear_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags);
Netanel Belgazald18e4f62017-11-19 18:03:40 +00002632 clear_bit(ENA_FLAG_ONGOING_RESET, &adapter->flags);
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002633 dev_err(&pdev->dev,
2634 "Reset attempt failed. Can not reset the device\n");
Netanel Belgazal8c5c7ab2017-10-17 07:33:58 +00002635
2636 return rc;
2637}
2638
2639static void ena_fw_reset_device(struct work_struct *work)
2640{
2641 struct ena_adapter *adapter =
2642 container_of(work, struct ena_adapter, reset_task);
2643 struct pci_dev *pdev = adapter->pdev;
2644
2645 if (unlikely(!test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags))) {
2646 dev_err(&pdev->dev,
2647 "device reset schedule while reset bit is off\n");
2648 return;
2649 }
2650 rtnl_lock();
2651 ena_destroy_device(adapter);
2652 ena_restore_device(adapter);
2653 rtnl_unlock();
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002654}
2655
Netanel Belgazal8510e1a2017-12-28 21:31:30 +00002656static int check_for_rx_interrupt_queue(struct ena_adapter *adapter,
2657 struct ena_ring *rx_ring)
2658{
2659 if (likely(rx_ring->first_interrupt))
2660 return 0;
2661
2662 if (ena_com_cq_empty(rx_ring->ena_com_io_cq))
2663 return 0;
2664
2665 rx_ring->no_interrupt_event_cnt++;
2666
2667 if (rx_ring->no_interrupt_event_cnt == ENA_MAX_NO_INTERRUPT_ITERATIONS) {
2668 netif_err(adapter, rx_err, adapter->netdev,
2669 "Potential MSIX issue on Rx side Queue = %d. Reset the device\n",
2670 rx_ring->qid);
2671 adapter->reset_reason = ENA_REGS_RESET_MISS_INTERRUPT;
2672 smp_mb__before_atomic();
2673 set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags);
2674 return -EIO;
2675 }
2676
2677 return 0;
2678}
2679
2680static int check_missing_comp_in_tx_queue(struct ena_adapter *adapter,
2681 struct ena_ring *tx_ring)
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002682{
2683 struct ena_tx_buffer *tx_buf;
2684 unsigned long last_jiffies;
Netanel Belgazal800c55c2017-06-11 15:42:50 +03002685 u32 missed_tx = 0;
Netanel Belgazal11095fd2017-10-17 07:33:59 +00002686 int i, rc = 0;
Netanel Belgazal800c55c2017-06-11 15:42:50 +03002687
2688 for (i = 0; i < tx_ring->ring_size; i++) {
2689 tx_buf = &tx_ring->tx_buffer_info[i];
2690 last_jiffies = tx_buf->last_jiffies;
Netanel Belgazal8510e1a2017-12-28 21:31:30 +00002691
2692 if (last_jiffies == 0)
2693 /* no pending Tx at this location */
2694 continue;
2695
2696 if (unlikely(!tx_ring->first_interrupt && time_is_before_jiffies(last_jiffies +
2697 2 * adapter->missing_tx_completion_to))) {
2698 /* If after graceful period interrupt is still not
2699 * received, we schedule a reset
2700 */
2701 netif_err(adapter, tx_err, adapter->netdev,
2702 "Potential MSIX issue on Tx side Queue = %d. Reset the device\n",
2703 tx_ring->qid);
2704 adapter->reset_reason = ENA_REGS_RESET_MISS_INTERRUPT;
2705 smp_mb__before_atomic();
2706 set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags);
2707 return -EIO;
2708 }
2709
2710 if (unlikely(time_is_before_jiffies(last_jiffies +
2711 adapter->missing_tx_completion_to))) {
Netanel Belgazal800c55c2017-06-11 15:42:50 +03002712 if (!tx_buf->print_once)
2713 netif_notice(adapter, tx_err, adapter->netdev,
2714 "Found a Tx that wasn't completed on time, qid %d, index %d.\n",
2715 tx_ring->qid, i);
2716
2717 tx_buf->print_once = 1;
2718 missed_tx++;
Netanel Belgazal800c55c2017-06-11 15:42:50 +03002719 }
2720 }
2721
Netanel Belgazal11095fd2017-10-17 07:33:59 +00002722 if (unlikely(missed_tx > adapter->missing_tx_completion_threshold)) {
2723 netif_err(adapter, tx_err, adapter->netdev,
2724 "The number of lost tx completions is above the threshold (%d > %d). Reset the device\n",
2725 missed_tx,
2726 adapter->missing_tx_completion_threshold);
2727 adapter->reset_reason =
2728 ENA_REGS_RESET_MISS_TX_CMPL;
2729 set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags);
2730 rc = -EIO;
2731 }
2732
2733 u64_stats_update_begin(&tx_ring->syncp);
2734 tx_ring->tx_stats.missed_tx = missed_tx;
2735 u64_stats_update_end(&tx_ring->syncp);
2736
2737 return rc;
Netanel Belgazal800c55c2017-06-11 15:42:50 +03002738}
2739
Netanel Belgazal8510e1a2017-12-28 21:31:30 +00002740static void check_for_missing_completions(struct ena_adapter *adapter)
Netanel Belgazal800c55c2017-06-11 15:42:50 +03002741{
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002742 struct ena_ring *tx_ring;
Netanel Belgazal8510e1a2017-12-28 21:31:30 +00002743 struct ena_ring *rx_ring;
Netanel Belgazal800c55c2017-06-11 15:42:50 +03002744 int i, budget, rc;
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002745
2746 /* Make sure the driver doesn't turn the device in other process */
2747 smp_rmb();
2748
2749 if (!test_bit(ENA_FLAG_DEV_UP, &adapter->flags))
2750 return;
2751
Netanel Belgazal3f6159d2017-02-09 15:21:33 +02002752 if (test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags))
2753 return;
2754
Netanel Belgazal82ef30f2017-06-23 11:21:51 +03002755 if (adapter->missing_tx_completion_to == ENA_HW_HINTS_NO_TIMEOUT)
2756 return;
2757
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002758 budget = ENA_MONITORED_TX_QUEUES;
2759
2760 for (i = adapter->last_monitored_tx_qid; i < adapter->num_queues; i++) {
2761 tx_ring = &adapter->tx_ring[i];
Netanel Belgazal8510e1a2017-12-28 21:31:30 +00002762 rx_ring = &adapter->rx_ring[i];
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002763
Netanel Belgazal8510e1a2017-12-28 21:31:30 +00002764 rc = check_missing_comp_in_tx_queue(adapter, tx_ring);
2765 if (unlikely(rc))
2766 return;
2767
2768 rc = check_for_rx_interrupt_queue(adapter, rx_ring);
Netanel Belgazal800c55c2017-06-11 15:42:50 +03002769 if (unlikely(rc))
2770 return;
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002771
2772 budget--;
2773 if (!budget)
2774 break;
2775 }
2776
2777 adapter->last_monitored_tx_qid = i % adapter->num_queues;
2778}
2779
Netanel Belgazala3af7c12017-06-11 15:42:48 +03002780/* trigger napi schedule after 2 consecutive detections */
2781#define EMPTY_RX_REFILL 2
2782/* For the rare case where the device runs out of Rx descriptors and the
2783 * napi handler failed to refill new Rx descriptors (due to a lack of memory
2784 * for example).
2785 * This case will lead to a deadlock:
2786 * The device won't send interrupts since all the new Rx packets will be dropped
2787 * The napi handler won't allocate new Rx descriptors so the device will be
2788 * able to send new packets.
2789 *
2790 * This scenario can happen when the kernel's vm.min_free_kbytes is too small.
2791 * It is recommended to have at least 512MB, with a minimum of 128MB for
2792 * constrained environment).
2793 *
2794 * When such a situation is detected - Reschedule napi
2795 */
2796static void check_for_empty_rx_ring(struct ena_adapter *adapter)
2797{
2798 struct ena_ring *rx_ring;
2799 int i, refill_required;
2800
2801 if (!test_bit(ENA_FLAG_DEV_UP, &adapter->flags))
2802 return;
2803
2804 if (test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags))
2805 return;
2806
2807 for (i = 0; i < adapter->num_queues; i++) {
2808 rx_ring = &adapter->rx_ring[i];
2809
2810 refill_required =
2811 ena_com_sq_empty_space(rx_ring->ena_com_io_sq);
2812 if (unlikely(refill_required == (rx_ring->ring_size - 1))) {
2813 rx_ring->empty_rx_queue++;
2814
2815 if (rx_ring->empty_rx_queue >= EMPTY_RX_REFILL) {
2816 u64_stats_update_begin(&rx_ring->syncp);
2817 rx_ring->rx_stats.empty_rx_ring++;
2818 u64_stats_update_end(&rx_ring->syncp);
2819
2820 netif_err(adapter, drv, adapter->netdev,
2821 "trigger refill for ring %d\n", i);
2822
2823 napi_schedule(rx_ring->napi);
2824 rx_ring->empty_rx_queue = 0;
2825 }
2826 } else {
2827 rx_ring->empty_rx_queue = 0;
2828 }
2829 }
2830}
2831
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002832/* Check for keep alive expiration */
2833static void check_for_missing_keep_alive(struct ena_adapter *adapter)
2834{
2835 unsigned long keep_alive_expired;
2836
2837 if (!adapter->wd_state)
2838 return;
2839
Netanel Belgazal82ef30f2017-06-23 11:21:51 +03002840 if (adapter->keep_alive_timeout == ENA_HW_HINTS_NO_TIMEOUT)
2841 return;
2842
2843 keep_alive_expired = round_jiffies(adapter->last_keep_alive_jiffies +
2844 adapter->keep_alive_timeout);
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002845 if (unlikely(time_is_before_jiffies(keep_alive_expired))) {
2846 netif_err(adapter, drv, adapter->netdev,
2847 "Keep alive watchdog timeout.\n");
2848 u64_stats_update_begin(&adapter->syncp);
2849 adapter->dev_stats.wd_expired++;
2850 u64_stats_update_end(&adapter->syncp);
Netanel Belgazale2eed0e2017-06-23 11:21:53 +03002851 adapter->reset_reason = ENA_REGS_RESET_KEEP_ALIVE_TO;
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002852 set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags);
2853 }
2854}
2855
2856static void check_for_admin_com_state(struct ena_adapter *adapter)
2857{
2858 if (unlikely(!ena_com_get_admin_running_state(adapter->ena_dev))) {
2859 netif_err(adapter, drv, adapter->netdev,
2860 "ENA admin queue is not in running state!\n");
2861 u64_stats_update_begin(&adapter->syncp);
2862 adapter->dev_stats.admin_q_pause++;
2863 u64_stats_update_end(&adapter->syncp);
Netanel Belgazale2eed0e2017-06-23 11:21:53 +03002864 adapter->reset_reason = ENA_REGS_RESET_ADMIN_TO;
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002865 set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags);
2866 }
2867}
2868
Netanel Belgazal82ef30f2017-06-23 11:21:51 +03002869static void ena_update_hints(struct ena_adapter *adapter,
2870 struct ena_admin_ena_hw_hints *hints)
2871{
2872 struct net_device *netdev = adapter->netdev;
2873
2874 if (hints->admin_completion_tx_timeout)
2875 adapter->ena_dev->admin_queue.completion_timeout =
2876 hints->admin_completion_tx_timeout * 1000;
2877
2878 if (hints->mmio_read_timeout)
2879 /* convert to usec */
2880 adapter->ena_dev->mmio_read.reg_read_to =
2881 hints->mmio_read_timeout * 1000;
2882
2883 if (hints->missed_tx_completion_count_threshold_to_reset)
2884 adapter->missing_tx_completion_threshold =
2885 hints->missed_tx_completion_count_threshold_to_reset;
2886
2887 if (hints->missing_tx_completion_timeout) {
2888 if (hints->missing_tx_completion_timeout == ENA_HW_HINTS_NO_TIMEOUT)
2889 adapter->missing_tx_completion_to = ENA_HW_HINTS_NO_TIMEOUT;
2890 else
2891 adapter->missing_tx_completion_to =
2892 msecs_to_jiffies(hints->missing_tx_completion_timeout);
2893 }
2894
2895 if (hints->netdev_wd_timeout)
2896 netdev->watchdog_timeo = msecs_to_jiffies(hints->netdev_wd_timeout);
2897
2898 if (hints->driver_watchdog_timeout) {
2899 if (hints->driver_watchdog_timeout == ENA_HW_HINTS_NO_TIMEOUT)
2900 adapter->keep_alive_timeout = ENA_HW_HINTS_NO_TIMEOUT;
2901 else
2902 adapter->keep_alive_timeout =
2903 msecs_to_jiffies(hints->driver_watchdog_timeout);
2904 }
2905}
2906
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002907static void ena_update_host_info(struct ena_admin_host_info *host_info,
2908 struct net_device *netdev)
2909{
2910 host_info->supported_network_features[0] =
2911 netdev->features & GENMASK_ULL(31, 0);
2912 host_info->supported_network_features[1] =
2913 (netdev->features & GENMASK_ULL(63, 32)) >> 32;
2914}
2915
Kees Cooke99e88a2017-10-16 14:43:17 -07002916static void ena_timer_service(struct timer_list *t)
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002917{
Kees Cooke99e88a2017-10-16 14:43:17 -07002918 struct ena_adapter *adapter = from_timer(adapter, t, timer_service);
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002919 u8 *debug_area = adapter->ena_dev->host_attr.debug_area_virt_addr;
2920 struct ena_admin_host_info *host_info =
2921 adapter->ena_dev->host_attr.host_info;
2922
2923 check_for_missing_keep_alive(adapter);
2924
2925 check_for_admin_com_state(adapter);
2926
Netanel Belgazal8510e1a2017-12-28 21:31:30 +00002927 check_for_missing_completions(adapter);
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002928
Netanel Belgazala3af7c12017-06-11 15:42:48 +03002929 check_for_empty_rx_ring(adapter);
2930
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002931 if (debug_area)
2932 ena_dump_stats_to_buf(adapter, debug_area);
2933
2934 if (host_info)
2935 ena_update_host_info(host_info, adapter->netdev);
2936
Netanel Belgazal3f6159d2017-02-09 15:21:33 +02002937 if (unlikely(test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags))) {
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002938 netif_err(adapter, drv, adapter->netdev,
2939 "Trigger reset is on\n");
2940 ena_dump_stats_to_dmesg(adapter);
2941 queue_work(ena_wq, &adapter->reset_task);
2942 return;
2943 }
2944
2945 /* Reset the timer */
2946 mod_timer(&adapter->timer_service, jiffies + HZ);
2947}
2948
2949static int ena_calc_io_queue_num(struct pci_dev *pdev,
2950 struct ena_com_dev *ena_dev,
2951 struct ena_com_dev_get_features_ctx *get_feat_ctx)
2952{
2953 int io_sq_num, io_queue_num;
2954
2955 /* In case of LLQ use the llq number in the get feature cmd */
2956 if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV) {
2957 io_sq_num = get_feat_ctx->max_queues.max_llq_num;
2958
2959 if (io_sq_num == 0) {
2960 dev_err(&pdev->dev,
2961 "Trying to use LLQ but llq_num is 0. Fall back into regular queues\n");
2962
2963 ena_dev->tx_mem_queue_type =
2964 ENA_ADMIN_PLACEMENT_POLICY_HOST;
2965 io_sq_num = get_feat_ctx->max_queues.max_sq_num;
2966 }
2967 } else {
2968 io_sq_num = get_feat_ctx->max_queues.max_sq_num;
2969 }
2970
Netanel Belgazal6a1ce2f2017-02-09 15:21:28 +02002971 io_queue_num = min_t(int, num_online_cpus(), ENA_MAX_NUM_IO_QUEUES);
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002972 io_queue_num = min_t(int, io_queue_num, io_sq_num);
2973 io_queue_num = min_t(int, io_queue_num,
2974 get_feat_ctx->max_queues.max_cq_num);
2975 /* 1 IRQ for for mgmnt and 1 IRQs for each IO direction */
2976 io_queue_num = min_t(int, io_queue_num, pci_msix_vec_count(pdev) - 1);
2977 if (unlikely(!io_queue_num)) {
2978 dev_err(&pdev->dev, "The device doesn't have io queues\n");
2979 return -EFAULT;
2980 }
2981
2982 return io_queue_num;
2983}
2984
Rami Rosen184b49c2016-08-23 20:20:17 +03002985static void ena_set_push_mode(struct pci_dev *pdev, struct ena_com_dev *ena_dev,
2986 struct ena_com_dev_get_features_ctx *get_feat_ctx)
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002987{
2988 bool has_mem_bar;
2989
2990 has_mem_bar = pci_select_bars(pdev, IORESOURCE_MEM) & BIT(ENA_MEM_BAR);
2991
2992 /* Enable push mode if device supports LLQ */
2993 if (has_mem_bar && (get_feat_ctx->max_queues.max_llq_num > 0))
2994 ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_DEV;
2995 else
2996 ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
Netanel Belgazal1738cd32016-08-10 14:03:22 +03002997}
2998
2999static void ena_set_dev_offloads(struct ena_com_dev_get_features_ctx *feat,
3000 struct net_device *netdev)
3001{
3002 netdev_features_t dev_features = 0;
3003
3004 /* Set offload features */
3005 if (feat->offload.tx &
3006 ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_PART_MASK)
3007 dev_features |= NETIF_F_IP_CSUM;
3008
3009 if (feat->offload.tx &
3010 ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV6_CSUM_PART_MASK)
3011 dev_features |= NETIF_F_IPV6_CSUM;
3012
3013 if (feat->offload.tx & ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV4_MASK)
3014 dev_features |= NETIF_F_TSO;
3015
3016 if (feat->offload.tx & ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV6_MASK)
3017 dev_features |= NETIF_F_TSO6;
3018
3019 if (feat->offload.tx & ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_ECN_MASK)
3020 dev_features |= NETIF_F_TSO_ECN;
3021
3022 if (feat->offload.rx_supported &
3023 ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV4_CSUM_MASK)
3024 dev_features |= NETIF_F_RXCSUM;
3025
3026 if (feat->offload.rx_supported &
3027 ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV6_CSUM_MASK)
3028 dev_features |= NETIF_F_RXCSUM;
3029
3030 netdev->features =
3031 dev_features |
3032 NETIF_F_SG |
Netanel Belgazal1738cd32016-08-10 14:03:22 +03003033 NETIF_F_RXHASH |
3034 NETIF_F_HIGHDMA;
3035
3036 netdev->hw_features |= netdev->features;
3037 netdev->vlan_features |= netdev->features;
3038}
3039
3040static void ena_set_conf_feat_params(struct ena_adapter *adapter,
3041 struct ena_com_dev_get_features_ctx *feat)
3042{
3043 struct net_device *netdev = adapter->netdev;
3044
3045 /* Copy mac address */
3046 if (!is_valid_ether_addr(feat->dev_attr.mac_addr)) {
3047 eth_hw_addr_random(netdev);
3048 ether_addr_copy(adapter->mac_addr, netdev->dev_addr);
3049 } else {
3050 ether_addr_copy(adapter->mac_addr, feat->dev_attr.mac_addr);
3051 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
3052 }
3053
3054 /* Set offload features */
3055 ena_set_dev_offloads(feat, netdev);
3056
3057 adapter->max_mtu = feat->dev_attr.max_mtu;
Jarod Wilsond894be52016-10-20 13:55:16 -04003058 netdev->max_mtu = adapter->max_mtu;
3059 netdev->min_mtu = ENA_MIN_MTU;
Netanel Belgazal1738cd32016-08-10 14:03:22 +03003060}
3061
3062static int ena_rss_init_default(struct ena_adapter *adapter)
3063{
3064 struct ena_com_dev *ena_dev = adapter->ena_dev;
3065 struct device *dev = &adapter->pdev->dev;
3066 int rc, i;
3067 u32 val;
3068
3069 rc = ena_com_rss_init(ena_dev, ENA_RX_RSS_TABLE_LOG_SIZE);
3070 if (unlikely(rc)) {
3071 dev_err(dev, "Cannot init indirect table\n");
3072 goto err_rss_init;
3073 }
3074
3075 for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++) {
3076 val = ethtool_rxfh_indir_default(i, adapter->num_queues);
3077 rc = ena_com_indirect_table_fill_entry(ena_dev, i,
3078 ENA_IO_RXQ_IDX(val));
Netanel Belgazald1497632017-06-23 11:21:50 +03003079 if (unlikely(rc && (rc != -EOPNOTSUPP))) {
Netanel Belgazal1738cd32016-08-10 14:03:22 +03003080 dev_err(dev, "Cannot fill indirect table\n");
3081 goto err_fill_indir;
3082 }
3083 }
3084
3085 rc = ena_com_fill_hash_function(ena_dev, ENA_ADMIN_CRC32, NULL,
3086 ENA_HASH_KEY_SIZE, 0xFFFFFFFF);
Netanel Belgazald1497632017-06-23 11:21:50 +03003087 if (unlikely(rc && (rc != -EOPNOTSUPP))) {
Netanel Belgazal1738cd32016-08-10 14:03:22 +03003088 dev_err(dev, "Cannot fill hash function\n");
3089 goto err_fill_indir;
3090 }
3091
3092 rc = ena_com_set_default_hash_ctrl(ena_dev);
Netanel Belgazald1497632017-06-23 11:21:50 +03003093 if (unlikely(rc && (rc != -EOPNOTSUPP))) {
Netanel Belgazal1738cd32016-08-10 14:03:22 +03003094 dev_err(dev, "Cannot fill hash control\n");
3095 goto err_fill_indir;
3096 }
3097
3098 return 0;
3099
3100err_fill_indir:
3101 ena_com_rss_destroy(ena_dev);
3102err_rss_init:
3103
3104 return rc;
3105}
3106
3107static void ena_release_bars(struct ena_com_dev *ena_dev, struct pci_dev *pdev)
3108{
3109 int release_bars;
3110
Netanel Belgazal0857d922017-06-11 15:42:47 +03003111 if (ena_dev->mem_bar)
3112 devm_iounmap(&pdev->dev, ena_dev->mem_bar);
3113
Netanel Belgazal411838e2017-10-17 07:33:04 +00003114 if (ena_dev->reg_bar)
3115 devm_iounmap(&pdev->dev, ena_dev->reg_bar);
Netanel Belgazal0857d922017-06-11 15:42:47 +03003116
Netanel Belgazal1738cd32016-08-10 14:03:22 +03003117 release_bars = pci_select_bars(pdev, IORESOURCE_MEM) & ENA_BAR_MASK;
3118 pci_release_selected_regions(pdev, release_bars);
3119}
3120
3121static int ena_calc_queue_size(struct pci_dev *pdev,
3122 struct ena_com_dev *ena_dev,
3123 u16 *max_tx_sgl_size,
3124 u16 *max_rx_sgl_size,
3125 struct ena_com_dev_get_features_ctx *get_feat_ctx)
3126{
3127 u32 queue_size = ENA_DEFAULT_RING_SIZE;
3128
3129 queue_size = min_t(u32, queue_size,
3130 get_feat_ctx->max_queues.max_cq_depth);
3131 queue_size = min_t(u32, queue_size,
3132 get_feat_ctx->max_queues.max_sq_depth);
3133
3134 if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV)
3135 queue_size = min_t(u32, queue_size,
3136 get_feat_ctx->max_queues.max_llq_depth);
3137
3138 queue_size = rounddown_pow_of_two(queue_size);
3139
3140 if (unlikely(!queue_size)) {
3141 dev_err(&pdev->dev, "Invalid queue size\n");
3142 return -EFAULT;
3143 }
3144
3145 *max_tx_sgl_size = min_t(u16, ENA_PKT_MAX_BUFS,
3146 get_feat_ctx->max_queues.max_packet_tx_descs);
3147 *max_rx_sgl_size = min_t(u16, ENA_PKT_MAX_BUFS,
3148 get_feat_ctx->max_queues.max_packet_rx_descs);
3149
3150 return queue_size;
3151}
3152
3153/* ena_probe - Device Initialization Routine
3154 * @pdev: PCI device information struct
3155 * @ent: entry in ena_pci_tbl
3156 *
3157 * Returns 0 on success, negative on failure
3158 *
3159 * ena_probe initializes an adapter identified by a pci_dev structure.
3160 * The OS initialization, configuring of the adapter private structure,
3161 * and a hardware reset occur.
3162 */
3163static int ena_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
3164{
3165 struct ena_com_dev_get_features_ctx get_feat_ctx;
3166 static int version_printed;
3167 struct net_device *netdev;
3168 struct ena_adapter *adapter;
3169 struct ena_com_dev *ena_dev = NULL;
3170 static int adapters_found;
3171 int io_queue_num, bars, rc;
3172 int queue_size;
3173 u16 tx_sgl_size = 0;
3174 u16 rx_sgl_size = 0;
3175 bool wd_state;
3176
3177 dev_dbg(&pdev->dev, "%s\n", __func__);
3178
3179 if (version_printed++ == 0)
3180 dev_info(&pdev->dev, "%s", version);
3181
3182 rc = pci_enable_device_mem(pdev);
3183 if (rc) {
3184 dev_err(&pdev->dev, "pci_enable_device_mem() failed!\n");
3185 return rc;
3186 }
3187
3188 pci_set_master(pdev);
3189
3190 ena_dev = vzalloc(sizeof(*ena_dev));
3191 if (!ena_dev) {
3192 rc = -ENOMEM;
3193 goto err_disable_device;
3194 }
3195
3196 bars = pci_select_bars(pdev, IORESOURCE_MEM) & ENA_BAR_MASK;
3197 rc = pci_request_selected_regions(pdev, bars, DRV_MODULE_NAME);
3198 if (rc) {
3199 dev_err(&pdev->dev, "pci_request_selected_regions failed %d\n",
3200 rc);
3201 goto err_free_ena_dev;
3202 }
3203
Netanel Belgazal0857d922017-06-11 15:42:47 +03003204 ena_dev->reg_bar = devm_ioremap(&pdev->dev,
3205 pci_resource_start(pdev, ENA_REG_BAR),
3206 pci_resource_len(pdev, ENA_REG_BAR));
Netanel Belgazal1738cd32016-08-10 14:03:22 +03003207 if (!ena_dev->reg_bar) {
3208 dev_err(&pdev->dev, "failed to remap regs bar\n");
3209 rc = -EFAULT;
3210 goto err_free_region;
3211 }
3212
3213 ena_dev->dmadev = &pdev->dev;
3214
3215 rc = ena_device_init(ena_dev, pdev, &get_feat_ctx, &wd_state);
3216 if (rc) {
3217 dev_err(&pdev->dev, "ena device init failed\n");
3218 if (rc == -ETIME)
3219 rc = -EPROBE_DEFER;
3220 goto err_free_region;
3221 }
3222
Rami Rosen184b49c2016-08-23 20:20:17 +03003223 ena_set_push_mode(pdev, ena_dev, &get_feat_ctx);
Netanel Belgazal1738cd32016-08-10 14:03:22 +03003224
3225 if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV) {
Netanel Belgazal0857d922017-06-11 15:42:47 +03003226 ena_dev->mem_bar = devm_ioremap_wc(&pdev->dev,
3227 pci_resource_start(pdev, ENA_MEM_BAR),
3228 pci_resource_len(pdev, ENA_MEM_BAR));
Netanel Belgazal1738cd32016-08-10 14:03:22 +03003229 if (!ena_dev->mem_bar) {
3230 rc = -EFAULT;
3231 goto err_device_destroy;
3232 }
3233 }
3234
3235 /* initial Tx interrupt delay, Assumes 1 usec granularity.
3236 * Updated during device initialization with the real granularity
3237 */
3238 ena_dev->intr_moder_tx_interval = ENA_INTR_INITIAL_TX_INTERVAL_USECS;
3239 io_queue_num = ena_calc_io_queue_num(pdev, ena_dev, &get_feat_ctx);
3240 queue_size = ena_calc_queue_size(pdev, ena_dev, &tx_sgl_size,
3241 &rx_sgl_size, &get_feat_ctx);
3242 if ((queue_size <= 0) || (io_queue_num <= 0)) {
3243 rc = -EFAULT;
3244 goto err_device_destroy;
3245 }
3246
3247 dev_info(&pdev->dev, "creating %d io queues. queue size: %d\n",
3248 io_queue_num, queue_size);
3249
3250 /* dev zeroed in init_etherdev */
3251 netdev = alloc_etherdev_mq(sizeof(struct ena_adapter), io_queue_num);
3252 if (!netdev) {
3253 dev_err(&pdev->dev, "alloc_etherdev_mq failed\n");
3254 rc = -ENOMEM;
3255 goto err_device_destroy;
3256 }
3257
3258 SET_NETDEV_DEV(netdev, &pdev->dev);
3259
3260 adapter = netdev_priv(netdev);
3261 pci_set_drvdata(pdev, adapter);
3262
3263 adapter->ena_dev = ena_dev;
3264 adapter->netdev = netdev;
3265 adapter->pdev = pdev;
3266
3267 ena_set_conf_feat_params(adapter, &get_feat_ctx);
3268
3269 adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
Netanel Belgazale2eed0e2017-06-23 11:21:53 +03003270 adapter->reset_reason = ENA_REGS_RESET_NORMAL;
Netanel Belgazal1738cd32016-08-10 14:03:22 +03003271
3272 adapter->tx_ring_size = queue_size;
3273 adapter->rx_ring_size = queue_size;
3274
3275 adapter->max_tx_sgl_size = tx_sgl_size;
3276 adapter->max_rx_sgl_size = rx_sgl_size;
3277
3278 adapter->num_queues = io_queue_num;
3279 adapter->last_monitored_tx_qid = 0;
3280
3281 adapter->rx_copybreak = ENA_DEFAULT_RX_COPYBREAK;
3282 adapter->wd_state = wd_state;
3283
3284 snprintf(adapter->name, ENA_NAME_MAX_LEN, "ena_%d", adapters_found);
3285
3286 rc = ena_com_init_interrupt_moderation(adapter->ena_dev);
3287 if (rc) {
3288 dev_err(&pdev->dev,
3289 "Failed to query interrupt moderation feature\n");
3290 goto err_netdev_destroy;
3291 }
3292 ena_init_io_rings(adapter);
3293
3294 netdev->netdev_ops = &ena_netdev_ops;
3295 netdev->watchdog_timeo = TX_TIMEOUT;
3296 ena_set_ethtool_ops(netdev);
3297
3298 netdev->priv_flags |= IFF_UNICAST_FLT;
3299
3300 u64_stats_init(&adapter->syncp);
3301
3302 rc = ena_enable_msix_and_set_admin_interrupts(adapter, io_queue_num);
3303 if (rc) {
3304 dev_err(&pdev->dev,
3305 "Failed to enable and set the admin interrupts\n");
3306 goto err_worker_destroy;
3307 }
3308 rc = ena_rss_init_default(adapter);
Netanel Belgazald1497632017-06-23 11:21:50 +03003309 if (rc && (rc != -EOPNOTSUPP)) {
Netanel Belgazal1738cd32016-08-10 14:03:22 +03003310 dev_err(&pdev->dev, "Cannot init RSS rc: %d\n", rc);
3311 goto err_free_msix;
3312 }
3313
3314 ena_config_debug_area(adapter);
3315
3316 memcpy(adapter->netdev->perm_addr, adapter->mac_addr, netdev->addr_len);
3317
3318 netif_carrier_off(netdev);
3319
3320 rc = register_netdev(netdev);
3321 if (rc) {
3322 dev_err(&pdev->dev, "Cannot register net device\n");
3323 goto err_rss;
3324 }
3325
Netanel Belgazal1738cd32016-08-10 14:03:22 +03003326 INIT_WORK(&adapter->reset_task, ena_fw_reset_device);
3327
3328 adapter->last_keep_alive_jiffies = jiffies;
Netanel Belgazal82ef30f2017-06-23 11:21:51 +03003329 adapter->keep_alive_timeout = ENA_DEVICE_KALIVE_TIMEOUT;
3330 adapter->missing_tx_completion_to = TX_TIMEOUT;
3331 adapter->missing_tx_completion_threshold = MAX_NUM_OF_TIMEOUTED_PACKETS;
3332
3333 ena_update_hints(adapter, &get_feat_ctx.hw_hints);
Netanel Belgazal1738cd32016-08-10 14:03:22 +03003334
Kees Cooke99e88a2017-10-16 14:43:17 -07003335 timer_setup(&adapter->timer_service, ena_timer_service, 0);
Wei Yongjunf850b4a2016-10-22 14:36:36 +00003336 mod_timer(&adapter->timer_service, round_jiffies(jiffies + HZ));
Netanel Belgazal1738cd32016-08-10 14:03:22 +03003337
3338 dev_info(&pdev->dev, "%s found at mem %lx, mac addr %pM Queues %d\n",
3339 DEVICE_NAME, (long)pci_resource_start(pdev, 0),
3340 netdev->dev_addr, io_queue_num);
3341
3342 set_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags);
3343
3344 adapters_found++;
3345
3346 return 0;
3347
3348err_rss:
3349 ena_com_delete_debug_area(ena_dev);
3350 ena_com_rss_destroy(ena_dev);
3351err_free_msix:
Netanel Belgazale2eed0e2017-06-23 11:21:53 +03003352 ena_com_dev_reset(ena_dev, ENA_REGS_RESET_INIT_ERR);
Netanel Belgazal1738cd32016-08-10 14:03:22 +03003353 ena_free_mgmnt_irq(adapter);
Netanel Belgazal06443682017-06-23 11:21:55 +03003354 ena_disable_msix(adapter);
Netanel Belgazal1738cd32016-08-10 14:03:22 +03003355err_worker_destroy:
3356 ena_com_destroy_interrupt_moderation(ena_dev);
3357 del_timer(&adapter->timer_service);
Netanel Belgazal1738cd32016-08-10 14:03:22 +03003358err_netdev_destroy:
3359 free_netdev(netdev);
3360err_device_destroy:
3361 ena_com_delete_host_info(ena_dev);
3362 ena_com_admin_destroy(ena_dev);
3363err_free_region:
3364 ena_release_bars(ena_dev, pdev);
3365err_free_ena_dev:
Netanel Belgazal1738cd32016-08-10 14:03:22 +03003366 vfree(ena_dev);
3367err_disable_device:
3368 pci_disable_device(pdev);
3369 return rc;
3370}
3371
3372/*****************************************************************************/
3373static int ena_sriov_configure(struct pci_dev *dev, int numvfs)
3374{
3375 int rc;
3376
3377 if (numvfs > 0) {
3378 rc = pci_enable_sriov(dev, numvfs);
3379 if (rc != 0) {
3380 dev_err(&dev->dev,
3381 "pci_enable_sriov failed to enable: %d vfs with the error: %d\n",
3382 numvfs, rc);
3383 return rc;
3384 }
3385
3386 return numvfs;
3387 }
3388
3389 if (numvfs == 0) {
3390 pci_disable_sriov(dev);
3391 return 0;
3392 }
3393
3394 return -EINVAL;
3395}
3396
3397/*****************************************************************************/
3398/*****************************************************************************/
3399
3400/* ena_remove - Device Removal Routine
3401 * @pdev: PCI device information struct
3402 *
3403 * ena_remove is called by the PCI subsystem to alert the driver
3404 * that it should release a PCI device.
3405 */
3406static void ena_remove(struct pci_dev *pdev)
3407{
3408 struct ena_adapter *adapter = pci_get_drvdata(pdev);
3409 struct ena_com_dev *ena_dev;
3410 struct net_device *netdev;
3411
Netanel Belgazal1738cd32016-08-10 14:03:22 +03003412 ena_dev = adapter->ena_dev;
3413 netdev = adapter->netdev;
3414
3415#ifdef CONFIG_RFS_ACCEL
3416 if ((adapter->msix_vecs >= 1) && (netdev->rx_cpu_rmap)) {
3417 free_irq_cpu_rmap(netdev->rx_cpu_rmap);
3418 netdev->rx_cpu_rmap = NULL;
3419 }
3420#endif /* CONFIG_RFS_ACCEL */
3421
3422 unregister_netdev(netdev);
3423 del_timer_sync(&adapter->timer_service);
3424
3425 cancel_work_sync(&adapter->reset_task);
3426
Netanel Belgazal22b331c2017-02-09 15:21:31 +02003427 /* Reset the device only if the device is running. */
3428 if (test_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags))
Netanel Belgazale2eed0e2017-06-23 11:21:53 +03003429 ena_com_dev_reset(ena_dev, adapter->reset_reason);
Netanel Belgazal1738cd32016-08-10 14:03:22 +03003430
3431 ena_free_mgmnt_irq(adapter);
3432
Netanel Belgazal06443682017-06-23 11:21:55 +03003433 ena_disable_msix(adapter);
Netanel Belgazal1738cd32016-08-10 14:03:22 +03003434
3435 free_netdev(netdev);
3436
3437 ena_com_mmio_reg_read_request_destroy(ena_dev);
3438
3439 ena_com_abort_admin_commands(ena_dev);
3440
3441 ena_com_wait_for_abort_completion(ena_dev);
3442
3443 ena_com_admin_destroy(ena_dev);
3444
3445 ena_com_rss_destroy(ena_dev);
3446
3447 ena_com_delete_debug_area(ena_dev);
3448
3449 ena_com_delete_host_info(ena_dev);
3450
3451 ena_release_bars(ena_dev, pdev);
3452
Netanel Belgazal1738cd32016-08-10 14:03:22 +03003453 pci_disable_device(pdev);
3454
3455 ena_com_destroy_interrupt_moderation(ena_dev);
3456
3457 vfree(ena_dev);
3458}
3459
Netanel Belgazal8c5c7ab2017-10-17 07:33:58 +00003460#ifdef CONFIG_PM
3461/* ena_suspend - PM suspend callback
3462 * @pdev: PCI device information struct
3463 * @state:power state
3464 */
3465static int ena_suspend(struct pci_dev *pdev, pm_message_t state)
3466{
3467 struct ena_adapter *adapter = pci_get_drvdata(pdev);
3468
3469 u64_stats_update_begin(&adapter->syncp);
3470 adapter->dev_stats.suspend++;
3471 u64_stats_update_end(&adapter->syncp);
3472
3473 rtnl_lock();
3474 if (unlikely(test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags))) {
3475 dev_err(&pdev->dev,
3476 "ignoring device reset request as the device is being suspended\n");
3477 clear_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags);
3478 }
3479 ena_destroy_device(adapter);
3480 rtnl_unlock();
3481 return 0;
3482}
3483
3484/* ena_resume - PM resume callback
3485 * @pdev: PCI device information struct
3486 *
3487 */
3488static int ena_resume(struct pci_dev *pdev)
3489{
3490 struct ena_adapter *adapter = pci_get_drvdata(pdev);
3491 int rc;
3492
3493 u64_stats_update_begin(&adapter->syncp);
3494 adapter->dev_stats.resume++;
3495 u64_stats_update_end(&adapter->syncp);
3496
3497 rtnl_lock();
3498 rc = ena_restore_device(adapter);
3499 rtnl_unlock();
3500 return rc;
3501}
3502#endif
3503
Netanel Belgazal1738cd32016-08-10 14:03:22 +03003504static struct pci_driver ena_pci_driver = {
3505 .name = DRV_MODULE_NAME,
3506 .id_table = ena_pci_tbl,
3507 .probe = ena_probe,
3508 .remove = ena_remove,
Netanel Belgazal8c5c7ab2017-10-17 07:33:58 +00003509#ifdef CONFIG_PM
3510 .suspend = ena_suspend,
3511 .resume = ena_resume,
3512#endif
Netanel Belgazal1738cd32016-08-10 14:03:22 +03003513 .sriov_configure = ena_sriov_configure,
3514};
3515
3516static int __init ena_init(void)
3517{
3518 pr_info("%s", version);
3519
3520 ena_wq = create_singlethread_workqueue(DRV_MODULE_NAME);
3521 if (!ena_wq) {
3522 pr_err("Failed to create workqueue\n");
3523 return -ENOMEM;
3524 }
3525
3526 return pci_register_driver(&ena_pci_driver);
3527}
3528
3529static void __exit ena_cleanup(void)
3530{
3531 pci_unregister_driver(&ena_pci_driver);
3532
3533 if (ena_wq) {
3534 destroy_workqueue(ena_wq);
3535 ena_wq = NULL;
3536 }
3537}
3538
3539/******************************************************************************
3540 ******************************** AENQ Handlers *******************************
3541 *****************************************************************************/
3542/* ena_update_on_link_change:
3543 * Notify the network interface about the change in link status
3544 */
3545static void ena_update_on_link_change(void *adapter_data,
3546 struct ena_admin_aenq_entry *aenq_e)
3547{
3548 struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
3549 struct ena_admin_aenq_link_change_desc *aenq_desc =
3550 (struct ena_admin_aenq_link_change_desc *)aenq_e;
3551 int status = aenq_desc->flags &
3552 ENA_ADMIN_AENQ_LINK_CHANGE_DESC_LINK_STATUS_MASK;
3553
3554 if (status) {
3555 netdev_dbg(adapter->netdev, "%s\n", __func__);
3556 set_bit(ENA_FLAG_LINK_UP, &adapter->flags);
Netanel Belgazald18e4f62017-11-19 18:03:40 +00003557 if (!test_bit(ENA_FLAG_ONGOING_RESET, &adapter->flags))
3558 netif_carrier_on(adapter->netdev);
Netanel Belgazal1738cd32016-08-10 14:03:22 +03003559 } else {
3560 clear_bit(ENA_FLAG_LINK_UP, &adapter->flags);
3561 netif_carrier_off(adapter->netdev);
3562 }
3563}
3564
3565static void ena_keep_alive_wd(void *adapter_data,
3566 struct ena_admin_aenq_entry *aenq_e)
3567{
3568 struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
Netanel Belgazal11a9a462017-06-23 11:21:59 +03003569 struct ena_admin_aenq_keep_alive_desc *desc;
3570 u64 rx_drops;
Netanel Belgazal1738cd32016-08-10 14:03:22 +03003571
Netanel Belgazal11a9a462017-06-23 11:21:59 +03003572 desc = (struct ena_admin_aenq_keep_alive_desc *)aenq_e;
Netanel Belgazal1738cd32016-08-10 14:03:22 +03003573 adapter->last_keep_alive_jiffies = jiffies;
Netanel Belgazal11a9a462017-06-23 11:21:59 +03003574
3575 rx_drops = ((u64)desc->rx_drops_high << 32) | desc->rx_drops_low;
3576
3577 u64_stats_update_begin(&adapter->syncp);
3578 adapter->dev_stats.rx_drops = rx_drops;
3579 u64_stats_update_end(&adapter->syncp);
Netanel Belgazal1738cd32016-08-10 14:03:22 +03003580}
3581
3582static void ena_notification(void *adapter_data,
3583 struct ena_admin_aenq_entry *aenq_e)
3584{
3585 struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
Netanel Belgazal82ef30f2017-06-23 11:21:51 +03003586 struct ena_admin_ena_hw_hints *hints;
Netanel Belgazal1738cd32016-08-10 14:03:22 +03003587
3588 WARN(aenq_e->aenq_common_desc.group != ENA_ADMIN_NOTIFICATION,
3589 "Invalid group(%x) expected %x\n",
3590 aenq_e->aenq_common_desc.group,
3591 ENA_ADMIN_NOTIFICATION);
3592
3593 switch (aenq_e->aenq_common_desc.syndrom) {
Netanel Belgazal82ef30f2017-06-23 11:21:51 +03003594 case ENA_ADMIN_UPDATE_HINTS:
3595 hints = (struct ena_admin_ena_hw_hints *)
3596 (&aenq_e->inline_data_w4);
3597 ena_update_hints(adapter, hints);
3598 break;
Netanel Belgazal1738cd32016-08-10 14:03:22 +03003599 default:
3600 netif_err(adapter, drv, adapter->netdev,
3601 "Invalid aenq notification link state %d\n",
3602 aenq_e->aenq_common_desc.syndrom);
3603 }
3604}
3605
3606/* This handler will called for unknown event group or unimplemented handlers*/
3607static void unimplemented_aenq_handler(void *data,
3608 struct ena_admin_aenq_entry *aenq_e)
3609{
3610 struct ena_adapter *adapter = (struct ena_adapter *)data;
3611
3612 netif_err(adapter, drv, adapter->netdev,
3613 "Unknown event was received or event with unimplemented handler\n");
3614}
3615
3616static struct ena_aenq_handlers aenq_handlers = {
3617 .handlers = {
3618 [ENA_ADMIN_LINK_CHANGE] = ena_update_on_link_change,
3619 [ENA_ADMIN_NOTIFICATION] = ena_notification,
3620 [ENA_ADMIN_KEEP_ALIVE] = ena_keep_alive_wd,
3621 },
3622 .unimplemented_handler = unimplemented_aenq_handler
3623};
3624
3625module_init(ena_init);
3626module_exit(ena_cleanup);