GNU Linux-libre 4.9.317-gnu1
[releases.git] / drivers / net / ethernet / amazon / ena / ena_netdev.c
1 /*
2  * Copyright 2015 Amazon.com, Inc. or its affiliates.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32
33 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
34
35 #ifdef CONFIG_RFS_ACCEL
36 #include <linux/cpu_rmap.h>
37 #endif /* CONFIG_RFS_ACCEL */
38 #include <linux/ethtool.h>
39 #include <linux/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
53 static char version[] = DEVICE_NAME " v" DRV_MODULE_VERSION "\n";
54
55 MODULE_AUTHOR("Amazon.com, Inc. or its affiliates");
56 MODULE_DESCRIPTION(DEVICE_NAME);
57 MODULE_LICENSE("GPL");
58 MODULE_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)
67 static int debug = -1;
68 module_param(debug, int, 0);
69 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
70
71 static struct ena_aenq_handlers aenq_handlers;
72
73 static struct workqueue_struct *ena_wq;
74
75 MODULE_DEVICE_TABLE(pci, ena_pci_tbl);
76
77 static int ena_rss_init_default(struct ena_adapter *adapter);
78
79 static void ena_tx_timeout(struct net_device *dev)
80 {
81         struct ena_adapter *adapter = netdev_priv(dev);
82
83         u64_stats_update_begin(&adapter->syncp);
84         adapter->dev_stats.tx_timeout++;
85         u64_stats_update_end(&adapter->syncp);
86
87         netif_err(adapter, tx_err, dev, "Transmit time out\n");
88
89         /* Change the state of the device to trigger reset */
90         set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags);
91 }
92
93 static void update_rx_ring_mtu(struct ena_adapter *adapter, int mtu)
94 {
95         int i;
96
97         for (i = 0; i < adapter->num_queues; i++)
98                 adapter->rx_ring[i].mtu = mtu;
99 }
100
101 static int ena_change_mtu(struct net_device *dev, int new_mtu)
102 {
103         struct ena_adapter *adapter = netdev_priv(dev);
104         int ret;
105
106         if ((new_mtu > adapter->max_mtu) || (new_mtu < ENA_MIN_MTU)) {
107                 netif_err(adapter, drv, dev,
108                           "Invalid MTU setting. new_mtu: %d\n", new_mtu);
109
110                 return -EINVAL;
111         }
112
113         ret = ena_com_set_dev_mtu(adapter->ena_dev, new_mtu);
114         if (!ret) {
115                 netif_dbg(adapter, drv, dev, "set MTU to %d\n", new_mtu);
116                 update_rx_ring_mtu(adapter, new_mtu);
117                 dev->mtu = new_mtu;
118         } else {
119                 netif_err(adapter, drv, dev, "Failed to set MTU to %d\n",
120                           new_mtu);
121         }
122
123         return ret;
124 }
125
126 static int ena_init_rx_cpu_rmap(struct ena_adapter *adapter)
127 {
128 #ifdef CONFIG_RFS_ACCEL
129         u32 i;
130         int rc;
131
132         adapter->netdev->rx_cpu_rmap = alloc_irq_cpu_rmap(adapter->num_queues);
133         if (!adapter->netdev->rx_cpu_rmap)
134                 return -ENOMEM;
135         for (i = 0; i < adapter->num_queues; i++) {
136                 int irq_idx = ENA_IO_IRQ_IDX(i);
137
138                 rc = irq_cpu_rmap_add(adapter->netdev->rx_cpu_rmap,
139                                       adapter->msix_entries[irq_idx].vector);
140                 if (rc) {
141                         free_irq_cpu_rmap(adapter->netdev->rx_cpu_rmap);
142                         adapter->netdev->rx_cpu_rmap = NULL;
143                         return rc;
144                 }
145         }
146 #endif /* CONFIG_RFS_ACCEL */
147         return 0;
148 }
149
150 static void ena_init_io_rings_common(struct ena_adapter *adapter,
151                                      struct ena_ring *ring, u16 qid)
152 {
153         ring->qid = qid;
154         ring->pdev = adapter->pdev;
155         ring->dev = &adapter->pdev->dev;
156         ring->netdev = adapter->netdev;
157         ring->napi = &adapter->ena_napi[qid].napi;
158         ring->adapter = adapter;
159         ring->ena_dev = adapter->ena_dev;
160         ring->per_napi_packets = 0;
161         ring->per_napi_bytes = 0;
162         ring->cpu = 0;
163         u64_stats_init(&ring->syncp);
164 }
165
166 static 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);
196         }
197 }
198
199 /* ena_setup_tx_resources - allocate I/O Tx resources (Descriptors)
200  * @adapter: network interface device structure
201  * @qid: queue index
202  *
203  * Return 0 on success, negative on failure
204  */
205 static int ena_setup_tx_resources(struct ena_adapter *adapter, int qid)
206 {
207         struct ena_ring *tx_ring = &adapter->tx_ring[qid];
208         struct ena_irq *ena_irq = &adapter->irq_tbl[ENA_IO_IRQ_IDX(qid)];
209         int size, i, node;
210
211         if (tx_ring->tx_buffer_info) {
212                 netif_err(adapter, ifup,
213                           adapter->netdev, "tx_buffer_info info is not NULL");
214                 return -EEXIST;
215         }
216
217         size = sizeof(struct ena_tx_buffer) * tx_ring->ring_size;
218         node = cpu_to_node(ena_irq->cpu);
219
220         tx_ring->tx_buffer_info = vzalloc_node(size, node);
221         if (!tx_ring->tx_buffer_info) {
222                 tx_ring->tx_buffer_info = vzalloc(size);
223                 if (!tx_ring->tx_buffer_info)
224                         return -ENOMEM;
225         }
226
227         size = sizeof(u16) * tx_ring->ring_size;
228         tx_ring->free_tx_ids = vzalloc_node(size, node);
229         if (!tx_ring->free_tx_ids) {
230                 tx_ring->free_tx_ids = vzalloc(size);
231                 if (!tx_ring->free_tx_ids) {
232                         vfree(tx_ring->tx_buffer_info);
233                         return -ENOMEM;
234                 }
235         }
236
237         /* Req id ring for TX out of order completions */
238         for (i = 0; i < tx_ring->ring_size; i++)
239                 tx_ring->free_tx_ids[i] = i;
240
241         /* Reset tx statistics */
242         memset(&tx_ring->tx_stats, 0x0, sizeof(tx_ring->tx_stats));
243
244         tx_ring->next_to_use = 0;
245         tx_ring->next_to_clean = 0;
246         tx_ring->cpu = ena_irq->cpu;
247         return 0;
248 }
249
250 /* ena_free_tx_resources - Free I/O Tx Resources per Queue
251  * @adapter: network interface device structure
252  * @qid: queue index
253  *
254  * Free all transmit software resources
255  */
256 static void ena_free_tx_resources(struct ena_adapter *adapter, int qid)
257 {
258         struct ena_ring *tx_ring = &adapter->tx_ring[qid];
259
260         vfree(tx_ring->tx_buffer_info);
261         tx_ring->tx_buffer_info = NULL;
262
263         vfree(tx_ring->free_tx_ids);
264         tx_ring->free_tx_ids = NULL;
265 }
266
267 /* ena_setup_all_tx_resources - allocate I/O Tx queues resources for All queues
268  * @adapter: private structure
269  *
270  * Return 0 on success, negative on failure
271  */
272 static int ena_setup_all_tx_resources(struct ena_adapter *adapter)
273 {
274         int i, rc = 0;
275
276         for (i = 0; i < adapter->num_queues; i++) {
277                 rc = ena_setup_tx_resources(adapter, i);
278                 if (rc)
279                         goto err_setup_tx;
280         }
281
282         return 0;
283
284 err_setup_tx:
285
286         netif_err(adapter, ifup, adapter->netdev,
287                   "Tx queue %d: allocation failed\n", i);
288
289         /* rewind the index freeing the rings as we go */
290         while (i--)
291                 ena_free_tx_resources(adapter, i);
292         return rc;
293 }
294
295 /* ena_free_all_io_tx_resources - Free I/O Tx Resources for All Queues
296  * @adapter: board private structure
297  *
298  * Free all transmit software resources
299  */
300 static void ena_free_all_io_tx_resources(struct ena_adapter *adapter)
301 {
302         int i;
303
304         for (i = 0; i < adapter->num_queues; i++)
305                 ena_free_tx_resources(adapter, i);
306 }
307
308 /* ena_setup_rx_resources - allocate I/O Rx resources (Descriptors)
309  * @adapter: network interface device structure
310  * @qid: queue index
311  *
312  * Returns 0 on success, negative on failure
313  */
314 static int ena_setup_rx_resources(struct ena_adapter *adapter,
315                                   u32 qid)
316 {
317         struct ena_ring *rx_ring = &adapter->rx_ring[qid];
318         struct ena_irq *ena_irq = &adapter->irq_tbl[ENA_IO_IRQ_IDX(qid)];
319         int size, node;
320
321         if (rx_ring->rx_buffer_info) {
322                 netif_err(adapter, ifup, adapter->netdev,
323                           "rx_buffer_info is not NULL");
324                 return -EEXIST;
325         }
326
327         /* alloc extra element so in rx path
328          * we can always prefetch rx_info + 1
329          */
330         size = sizeof(struct ena_rx_buffer) * (rx_ring->ring_size + 1);
331         node = cpu_to_node(ena_irq->cpu);
332
333         rx_ring->rx_buffer_info = vzalloc_node(size, node);
334         if (!rx_ring->rx_buffer_info) {
335                 rx_ring->rx_buffer_info = vzalloc(size);
336                 if (!rx_ring->rx_buffer_info)
337                         return -ENOMEM;
338         }
339
340         /* Reset rx statistics */
341         memset(&rx_ring->rx_stats, 0x0, sizeof(rx_ring->rx_stats));
342
343         rx_ring->next_to_clean = 0;
344         rx_ring->next_to_use = 0;
345         rx_ring->cpu = ena_irq->cpu;
346
347         return 0;
348 }
349
350 /* ena_free_rx_resources - Free I/O Rx Resources
351  * @adapter: network interface device structure
352  * @qid: queue index
353  *
354  * Free all receive software resources
355  */
356 static void ena_free_rx_resources(struct ena_adapter *adapter,
357                                   u32 qid)
358 {
359         struct ena_ring *rx_ring = &adapter->rx_ring[qid];
360
361         vfree(rx_ring->rx_buffer_info);
362         rx_ring->rx_buffer_info = NULL;
363 }
364
365 /* ena_setup_all_rx_resources - allocate I/O Rx queues resources for all queues
366  * @adapter: board private structure
367  *
368  * Return 0 on success, negative on failure
369  */
370 static int ena_setup_all_rx_resources(struct ena_adapter *adapter)
371 {
372         int i, rc = 0;
373
374         for (i = 0; i < adapter->num_queues; i++) {
375                 rc = ena_setup_rx_resources(adapter, i);
376                 if (rc)
377                         goto err_setup_rx;
378         }
379
380         return 0;
381
382 err_setup_rx:
383
384         netif_err(adapter, ifup, adapter->netdev,
385                   "Rx queue %d: allocation failed\n", i);
386
387         /* rewind the index freeing the rings as we go */
388         while (i--)
389                 ena_free_rx_resources(adapter, i);
390         return rc;
391 }
392
393 /* ena_free_all_io_rx_resources - Free I/O Rx Resources for All Queues
394  * @adapter: board private structure
395  *
396  * Free all receive software resources
397  */
398 static void ena_free_all_io_rx_resources(struct ena_adapter *adapter)
399 {
400         int i;
401
402         for (i = 0; i < adapter->num_queues; i++)
403                 ena_free_rx_resources(adapter, i);
404 }
405
406 static inline int ena_alloc_rx_page(struct ena_ring *rx_ring,
407                                     struct ena_rx_buffer *rx_info, gfp_t gfp)
408 {
409         struct ena_com_buf *ena_buf;
410         struct page *page;
411         dma_addr_t dma;
412
413         /* if previous allocated page is not used */
414         if (unlikely(rx_info->page))
415                 return 0;
416
417         page = alloc_page(gfp);
418         if (unlikely(!page)) {
419                 u64_stats_update_begin(&rx_ring->syncp);
420                 rx_ring->rx_stats.page_alloc_fail++;
421                 u64_stats_update_end(&rx_ring->syncp);
422                 return -ENOMEM;
423         }
424
425         dma = dma_map_page(rx_ring->dev, page, 0, ENA_PAGE_SIZE,
426                            DMA_FROM_DEVICE);
427         if (unlikely(dma_mapping_error(rx_ring->dev, dma))) {
428                 u64_stats_update_begin(&rx_ring->syncp);
429                 rx_ring->rx_stats.dma_mapping_err++;
430                 u64_stats_update_end(&rx_ring->syncp);
431
432                 __free_page(page);
433                 return -EIO;
434         }
435         netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev,
436                   "alloc page %p, rx_info %p\n", page, rx_info);
437
438         rx_info->page = page;
439         rx_info->page_offset = 0;
440         ena_buf = &rx_info->ena_buf;
441         ena_buf->paddr = dma;
442         ena_buf->len = ENA_PAGE_SIZE;
443
444         return 0;
445 }
446
447 static void ena_free_rx_page(struct ena_ring *rx_ring,
448                              struct ena_rx_buffer *rx_info)
449 {
450         struct page *page = rx_info->page;
451         struct ena_com_buf *ena_buf = &rx_info->ena_buf;
452
453         if (unlikely(!page)) {
454                 netif_warn(rx_ring->adapter, rx_err, rx_ring->netdev,
455                            "Trying to free unallocated buffer\n");
456                 return;
457         }
458
459         dma_unmap_page(rx_ring->dev, ena_buf->paddr, ENA_PAGE_SIZE,
460                        DMA_FROM_DEVICE);
461
462         __free_page(page);
463         rx_info->page = NULL;
464 }
465
466 static int ena_refill_rx_bufs(struct ena_ring *rx_ring, u32 num)
467 {
468         u16 next_to_use;
469         u32 i;
470         int rc;
471
472         next_to_use = rx_ring->next_to_use;
473
474         for (i = 0; i < num; i++) {
475                 struct ena_rx_buffer *rx_info =
476                         &rx_ring->rx_buffer_info[next_to_use];
477
478                 rc = ena_alloc_rx_page(rx_ring, rx_info,
479                                        __GFP_COLD | GFP_ATOMIC | __GFP_COMP);
480                 if (unlikely(rc < 0)) {
481                         netif_warn(rx_ring->adapter, rx_err, rx_ring->netdev,
482                                    "failed to alloc buffer for rx queue %d\n",
483                                    rx_ring->qid);
484                         break;
485                 }
486                 rc = ena_com_add_single_rx_desc(rx_ring->ena_com_io_sq,
487                                                 &rx_info->ena_buf,
488                                                 next_to_use);
489                 if (unlikely(rc)) {
490                         netif_warn(rx_ring->adapter, rx_status, rx_ring->netdev,
491                                    "failed to add buffer for rx queue %d\n",
492                                    rx_ring->qid);
493                         break;
494                 }
495                 next_to_use = ENA_RX_RING_IDX_NEXT(next_to_use,
496                                                    rx_ring->ring_size);
497         }
498
499         if (unlikely(i < num)) {
500                 u64_stats_update_begin(&rx_ring->syncp);
501                 rx_ring->rx_stats.refil_partial++;
502                 u64_stats_update_end(&rx_ring->syncp);
503                 netdev_warn(rx_ring->netdev,
504                             "refilled rx qid %d with only %d buffers (from %d)\n",
505                             rx_ring->qid, i, num);
506         }
507
508         if (likely(i)) {
509                 /* Add memory barrier to make sure the desc were written before
510                  * issue a doorbell
511                  */
512                 wmb();
513                 ena_com_write_sq_doorbell(rx_ring->ena_com_io_sq);
514         }
515
516         rx_ring->next_to_use = next_to_use;
517
518         return i;
519 }
520
521 static void ena_free_rx_bufs(struct ena_adapter *adapter,
522                              u32 qid)
523 {
524         struct ena_ring *rx_ring = &adapter->rx_ring[qid];
525         u32 i;
526
527         for (i = 0; i < rx_ring->ring_size; i++) {
528                 struct ena_rx_buffer *rx_info = &rx_ring->rx_buffer_info[i];
529
530                 if (rx_info->page)
531                         ena_free_rx_page(rx_ring, rx_info);
532         }
533 }
534
535 /* ena_refill_all_rx_bufs - allocate all queues Rx buffers
536  * @adapter: board private structure
537  *
538  */
539 static void ena_refill_all_rx_bufs(struct ena_adapter *adapter)
540 {
541         struct ena_ring *rx_ring;
542         int i, rc, bufs_num;
543
544         for (i = 0; i < adapter->num_queues; i++) {
545                 rx_ring = &adapter->rx_ring[i];
546                 bufs_num = rx_ring->ring_size - 1;
547                 rc = ena_refill_rx_bufs(rx_ring, bufs_num);
548
549                 if (unlikely(rc != bufs_num))
550                         netif_warn(rx_ring->adapter, rx_status, rx_ring->netdev,
551                                    "refilling Queue %d failed. allocated %d buffers from: %d\n",
552                                    i, rc, bufs_num);
553         }
554 }
555
556 static void ena_free_all_rx_bufs(struct ena_adapter *adapter)
557 {
558         int i;
559
560         for (i = 0; i < adapter->num_queues; i++)
561                 ena_free_rx_bufs(adapter, i);
562 }
563
564 /* ena_free_tx_bufs - Free Tx Buffers per Queue
565  * @tx_ring: TX ring for which buffers be freed
566  */
567 static void ena_free_tx_bufs(struct ena_ring *tx_ring)
568 {
569         u32 i;
570
571         for (i = 0; i < tx_ring->ring_size; i++) {
572                 struct ena_tx_buffer *tx_info = &tx_ring->tx_buffer_info[i];
573                 struct ena_com_buf *ena_buf;
574                 int nr_frags;
575                 int j;
576
577                 if (!tx_info->skb)
578                         continue;
579
580                 netdev_notice(tx_ring->netdev,
581                               "free uncompleted tx skb qid %d idx 0x%x\n",
582                               tx_ring->qid, i);
583
584                 ena_buf = tx_info->bufs;
585                 dma_unmap_single(tx_ring->dev,
586                                  ena_buf->paddr,
587                                  ena_buf->len,
588                                  DMA_TO_DEVICE);
589
590                 /* unmap remaining mapped pages */
591                 nr_frags = tx_info->num_of_bufs - 1;
592                 for (j = 0; j < nr_frags; j++) {
593                         ena_buf++;
594                         dma_unmap_page(tx_ring->dev,
595                                        ena_buf->paddr,
596                                        ena_buf->len,
597                                        DMA_TO_DEVICE);
598                 }
599
600                 dev_kfree_skb_any(tx_info->skb);
601         }
602         netdev_tx_reset_queue(netdev_get_tx_queue(tx_ring->netdev,
603                                                   tx_ring->qid));
604 }
605
606 static void ena_free_all_tx_bufs(struct ena_adapter *adapter)
607 {
608         struct ena_ring *tx_ring;
609         int i;
610
611         for (i = 0; i < adapter->num_queues; i++) {
612                 tx_ring = &adapter->tx_ring[i];
613                 ena_free_tx_bufs(tx_ring);
614         }
615 }
616
617 static void ena_destroy_all_tx_queues(struct ena_adapter *adapter)
618 {
619         u16 ena_qid;
620         int i;
621
622         for (i = 0; i < adapter->num_queues; i++) {
623                 ena_qid = ENA_IO_TXQ_IDX(i);
624                 ena_com_destroy_io_queue(adapter->ena_dev, ena_qid);
625         }
626 }
627
628 static void ena_destroy_all_rx_queues(struct ena_adapter *adapter)
629 {
630         u16 ena_qid;
631         int i;
632
633         for (i = 0; i < adapter->num_queues; i++) {
634                 ena_qid = ENA_IO_RXQ_IDX(i);
635                 ena_com_destroy_io_queue(adapter->ena_dev, ena_qid);
636         }
637 }
638
639 static void ena_destroy_all_io_queues(struct ena_adapter *adapter)
640 {
641         ena_destroy_all_tx_queues(adapter);
642         ena_destroy_all_rx_queues(adapter);
643 }
644
645 static int validate_tx_req_id(struct ena_ring *tx_ring, u16 req_id)
646 {
647         struct ena_tx_buffer *tx_info = NULL;
648
649         if (likely(req_id < tx_ring->ring_size)) {
650                 tx_info = &tx_ring->tx_buffer_info[req_id];
651                 if (likely(tx_info->skb))
652                         return 0;
653         }
654
655         if (tx_info)
656                 netif_err(tx_ring->adapter, tx_done, tx_ring->netdev,
657                           "tx_info doesn't have valid skb\n");
658         else
659                 netif_err(tx_ring->adapter, tx_done, tx_ring->netdev,
660                           "Invalid req_id: %hu\n", req_id);
661
662         u64_stats_update_begin(&tx_ring->syncp);
663         tx_ring->tx_stats.bad_req_id++;
664         u64_stats_update_end(&tx_ring->syncp);
665
666         /* Trigger device reset */
667         set_bit(ENA_FLAG_TRIGGER_RESET, &tx_ring->adapter->flags);
668         return -EFAULT;
669 }
670
671 static int ena_clean_tx_irq(struct ena_ring *tx_ring, u32 budget)
672 {
673         struct netdev_queue *txq;
674         bool above_thresh;
675         u32 tx_bytes = 0;
676         u32 total_done = 0;
677         u16 next_to_clean;
678         u16 req_id;
679         int tx_pkts = 0;
680         int rc;
681
682         next_to_clean = tx_ring->next_to_clean;
683         txq = netdev_get_tx_queue(tx_ring->netdev, tx_ring->qid);
684
685         while (tx_pkts < budget) {
686                 struct ena_tx_buffer *tx_info;
687                 struct sk_buff *skb;
688                 struct ena_com_buf *ena_buf;
689                 int i, nr_frags;
690
691                 rc = ena_com_tx_comp_req_id_get(tx_ring->ena_com_io_cq,
692                                                 &req_id);
693                 if (rc)
694                         break;
695
696                 rc = validate_tx_req_id(tx_ring, req_id);
697                 if (rc)
698                         break;
699
700                 tx_info = &tx_ring->tx_buffer_info[req_id];
701                 skb = tx_info->skb;
702
703                 /* prefetch skb_end_pointer() to speedup skb_shinfo(skb) */
704                 prefetch(&skb->end);
705
706                 tx_info->skb = NULL;
707                 tx_info->last_jiffies = 0;
708
709                 if (likely(tx_info->num_of_bufs != 0)) {
710                         ena_buf = tx_info->bufs;
711
712                         dma_unmap_single(tx_ring->dev,
713                                          dma_unmap_addr(ena_buf, paddr),
714                                          dma_unmap_len(ena_buf, len),
715                                          DMA_TO_DEVICE);
716
717                         /* unmap remaining mapped pages */
718                         nr_frags = tx_info->num_of_bufs - 1;
719                         for (i = 0; i < nr_frags; i++) {
720                                 ena_buf++;
721                                 dma_unmap_page(tx_ring->dev,
722                                                dma_unmap_addr(ena_buf, paddr),
723                                                dma_unmap_len(ena_buf, len),
724                                                DMA_TO_DEVICE);
725                         }
726                 }
727
728                 netif_dbg(tx_ring->adapter, tx_done, tx_ring->netdev,
729                           "tx_poll: q %d skb %p completed\n", tx_ring->qid,
730                           skb);
731
732                 tx_bytes += skb->len;
733                 dev_kfree_skb(skb);
734                 tx_pkts++;
735                 total_done += tx_info->tx_descs;
736
737                 tx_ring->free_tx_ids[next_to_clean] = req_id;
738                 next_to_clean = ENA_TX_RING_IDX_NEXT(next_to_clean,
739                                                      tx_ring->ring_size);
740         }
741
742         tx_ring->next_to_clean = next_to_clean;
743         ena_com_comp_ack(tx_ring->ena_com_io_sq, total_done);
744         ena_com_update_dev_comp_head(tx_ring->ena_com_io_cq);
745
746         netdev_tx_completed_queue(txq, tx_pkts, tx_bytes);
747
748         netif_dbg(tx_ring->adapter, tx_done, tx_ring->netdev,
749                   "tx_poll: q %d done. total pkts: %d\n",
750                   tx_ring->qid, tx_pkts);
751
752         /* need to make the rings circular update visible to
753          * ena_start_xmit() before checking for netif_queue_stopped().
754          */
755         smp_mb();
756
757         above_thresh = ena_com_sq_empty_space(tx_ring->ena_com_io_sq) >
758                 ENA_TX_WAKEUP_THRESH;
759         if (unlikely(netif_tx_queue_stopped(txq) && above_thresh)) {
760                 __netif_tx_lock(txq, smp_processor_id());
761                 above_thresh = ena_com_sq_empty_space(tx_ring->ena_com_io_sq) >
762                         ENA_TX_WAKEUP_THRESH;
763                 if (netif_tx_queue_stopped(txq) && above_thresh) {
764                         netif_tx_wake_queue(txq);
765                         u64_stats_update_begin(&tx_ring->syncp);
766                         tx_ring->tx_stats.queue_wakeup++;
767                         u64_stats_update_end(&tx_ring->syncp);
768                 }
769                 __netif_tx_unlock(txq);
770         }
771
772         tx_ring->per_napi_bytes += tx_bytes;
773         tx_ring->per_napi_packets += tx_pkts;
774
775         return tx_pkts;
776 }
777
778 static struct sk_buff *ena_rx_skb(struct ena_ring *rx_ring,
779                                   struct ena_com_rx_buf_info *ena_bufs,
780                                   u32 descs,
781                                   u16 *next_to_clean)
782 {
783         struct sk_buff *skb;
784         struct ena_rx_buffer *rx_info =
785                 &rx_ring->rx_buffer_info[*next_to_clean];
786         u32 len;
787         u32 buf = 0;
788         void *va;
789
790         len = ena_bufs[0].len;
791         if (unlikely(!rx_info->page)) {
792                 netif_err(rx_ring->adapter, rx_err, rx_ring->netdev,
793                           "Page is NULL\n");
794                 return NULL;
795         }
796
797         netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev,
798                   "rx_info %p page %p\n",
799                   rx_info, rx_info->page);
800
801         /* save virt address of first buffer */
802         va = page_address(rx_info->page) + rx_info->page_offset;
803         prefetch(va + NET_IP_ALIGN);
804
805         if (len <= rx_ring->rx_copybreak) {
806                 skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
807                                                 rx_ring->rx_copybreak);
808                 if (unlikely(!skb)) {
809                         u64_stats_update_begin(&rx_ring->syncp);
810                         rx_ring->rx_stats.skb_alloc_fail++;
811                         u64_stats_update_end(&rx_ring->syncp);
812                         netif_err(rx_ring->adapter, rx_err, rx_ring->netdev,
813                                   "Failed to allocate skb\n");
814                         return NULL;
815                 }
816
817                 netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev,
818                           "rx allocated small packet. len %d. data_len %d\n",
819                           skb->len, skb->data_len);
820
821                 /* sync this buffer for CPU use */
822                 dma_sync_single_for_cpu(rx_ring->dev,
823                                         dma_unmap_addr(&rx_info->ena_buf, paddr),
824                                         len,
825                                         DMA_FROM_DEVICE);
826                 skb_copy_to_linear_data(skb, va, len);
827                 dma_sync_single_for_device(rx_ring->dev,
828                                            dma_unmap_addr(&rx_info->ena_buf, paddr),
829                                            len,
830                                            DMA_FROM_DEVICE);
831
832                 skb_put(skb, len);
833                 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
834                 *next_to_clean = ENA_RX_RING_IDX_ADD(*next_to_clean, descs,
835                                                      rx_ring->ring_size);
836                 return skb;
837         }
838
839         skb = napi_get_frags(rx_ring->napi);
840         if (unlikely(!skb)) {
841                 netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev,
842                           "Failed allocating skb\n");
843                 u64_stats_update_begin(&rx_ring->syncp);
844                 rx_ring->rx_stats.skb_alloc_fail++;
845                 u64_stats_update_end(&rx_ring->syncp);
846                 return NULL;
847         }
848
849         do {
850                 dma_unmap_page(rx_ring->dev,
851                                dma_unmap_addr(&rx_info->ena_buf, paddr),
852                                ENA_PAGE_SIZE, DMA_FROM_DEVICE);
853
854                 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_info->page,
855                                 rx_info->page_offset, len, ENA_PAGE_SIZE);
856
857                 netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev,
858                           "rx skb updated. len %d. data_len %d\n",
859                           skb->len, skb->data_len);
860
861                 rx_info->page = NULL;
862                 *next_to_clean =
863                         ENA_RX_RING_IDX_NEXT(*next_to_clean,
864                                              rx_ring->ring_size);
865                 if (likely(--descs == 0))
866                         break;
867                 rx_info = &rx_ring->rx_buffer_info[*next_to_clean];
868                 len = ena_bufs[++buf].len;
869         } while (1);
870
871         return skb;
872 }
873
874 /* ena_rx_checksum - indicate in skb if hw indicated a good cksum
875  * @adapter: structure containing adapter specific data
876  * @ena_rx_ctx: received packet context/metadata
877  * @skb: skb currently being received and modified
878  */
879 static inline void ena_rx_checksum(struct ena_ring *rx_ring,
880                                    struct ena_com_rx_ctx *ena_rx_ctx,
881                                    struct sk_buff *skb)
882 {
883         /* Rx csum disabled */
884         if (unlikely(!(rx_ring->netdev->features & NETIF_F_RXCSUM))) {
885                 skb->ip_summed = CHECKSUM_NONE;
886                 return;
887         }
888
889         /* For fragmented packets the checksum isn't valid */
890         if (ena_rx_ctx->frag) {
891                 skb->ip_summed = CHECKSUM_NONE;
892                 return;
893         }
894
895         /* if IP and error */
896         if (unlikely((ena_rx_ctx->l3_proto == ENA_ETH_IO_L3_PROTO_IPV4) &&
897                      (ena_rx_ctx->l3_csum_err))) {
898                 /* ipv4 checksum error */
899                 skb->ip_summed = CHECKSUM_NONE;
900                 u64_stats_update_begin(&rx_ring->syncp);
901                 rx_ring->rx_stats.bad_csum++;
902                 u64_stats_update_end(&rx_ring->syncp);
903                 netif_err(rx_ring->adapter, rx_err, rx_ring->netdev,
904                           "RX IPv4 header checksum error\n");
905                 return;
906         }
907
908         /* if TCP/UDP */
909         if (likely((ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_TCP) ||
910                    (ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_UDP))) {
911                 if (unlikely(ena_rx_ctx->l4_csum_err)) {
912                         /* TCP/UDP checksum error */
913                         u64_stats_update_begin(&rx_ring->syncp);
914                         rx_ring->rx_stats.bad_csum++;
915                         u64_stats_update_end(&rx_ring->syncp);
916                         netif_err(rx_ring->adapter, rx_err, rx_ring->netdev,
917                                   "RX L4 checksum error\n");
918                         skb->ip_summed = CHECKSUM_NONE;
919                         return;
920                 }
921
922                 skb->ip_summed = CHECKSUM_UNNECESSARY;
923         }
924 }
925
926 static void ena_set_rx_hash(struct ena_ring *rx_ring,
927                             struct ena_com_rx_ctx *ena_rx_ctx,
928                             struct sk_buff *skb)
929 {
930         enum pkt_hash_types hash_type;
931
932         if (likely(rx_ring->netdev->features & NETIF_F_RXHASH)) {
933                 if (likely((ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_TCP) ||
934                            (ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_UDP)))
935
936                         hash_type = PKT_HASH_TYPE_L4;
937                 else
938                         hash_type = PKT_HASH_TYPE_NONE;
939
940                 /* Override hash type if the packet is fragmented */
941                 if (ena_rx_ctx->frag)
942                         hash_type = PKT_HASH_TYPE_NONE;
943
944                 skb_set_hash(skb, ena_rx_ctx->hash, hash_type);
945         }
946 }
947
948 /* ena_clean_rx_irq - Cleanup RX irq
949  * @rx_ring: RX ring to clean
950  * @napi: napi handler
951  * @budget: how many packets driver is allowed to clean
952  *
953  * Returns the number of cleaned buffers.
954  */
955 static int ena_clean_rx_irq(struct ena_ring *rx_ring, struct napi_struct *napi,
956                             u32 budget)
957 {
958         u16 next_to_clean = rx_ring->next_to_clean;
959         u32 res_budget, work_done;
960
961         struct ena_com_rx_ctx ena_rx_ctx;
962         struct ena_adapter *adapter;
963         struct sk_buff *skb;
964         int refill_required;
965         int refill_threshold;
966         int rc = 0;
967         int total_len = 0;
968         int rx_copybreak_pkt = 0;
969
970         netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev,
971                   "%s qid %d\n", __func__, rx_ring->qid);
972         res_budget = budget;
973
974         do {
975                 ena_rx_ctx.ena_bufs = rx_ring->ena_bufs;
976                 ena_rx_ctx.max_bufs = rx_ring->sgl_size;
977                 ena_rx_ctx.descs = 0;
978                 rc = ena_com_rx_pkt(rx_ring->ena_com_io_cq,
979                                     rx_ring->ena_com_io_sq,
980                                     &ena_rx_ctx);
981                 if (unlikely(rc))
982                         goto error;
983
984                 if (unlikely(ena_rx_ctx.descs == 0))
985                         break;
986
987                 netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev,
988                           "rx_poll: q %d got packet from ena. descs #: %d l3 proto %d l4 proto %d hash: %x\n",
989                           rx_ring->qid, ena_rx_ctx.descs, ena_rx_ctx.l3_proto,
990                           ena_rx_ctx.l4_proto, ena_rx_ctx.hash);
991
992                 /* allocate skb and fill it */
993                 skb = ena_rx_skb(rx_ring, rx_ring->ena_bufs, ena_rx_ctx.descs,
994                                  &next_to_clean);
995
996                 /* exit if we failed to retrieve a buffer */
997                 if (unlikely(!skb)) {
998                         next_to_clean = ENA_RX_RING_IDX_ADD(next_to_clean,
999                                                             ena_rx_ctx.descs,
1000                                                             rx_ring->ring_size);
1001                         break;
1002                 }
1003
1004                 ena_rx_checksum(rx_ring, &ena_rx_ctx, skb);
1005
1006                 ena_set_rx_hash(rx_ring, &ena_rx_ctx, skb);
1007
1008                 skb_record_rx_queue(skb, rx_ring->qid);
1009
1010                 if (rx_ring->ena_bufs[0].len <= rx_ring->rx_copybreak) {
1011                         total_len += rx_ring->ena_bufs[0].len;
1012                         rx_copybreak_pkt++;
1013                         napi_gro_receive(napi, skb);
1014                 } else {
1015                         total_len += skb->len;
1016                         napi_gro_frags(napi);
1017                 }
1018
1019                 res_budget--;
1020         } while (likely(res_budget));
1021
1022         work_done = budget - res_budget;
1023         rx_ring->per_napi_bytes += total_len;
1024         rx_ring->per_napi_packets += work_done;
1025         u64_stats_update_begin(&rx_ring->syncp);
1026         rx_ring->rx_stats.bytes += total_len;
1027         rx_ring->rx_stats.cnt += work_done;
1028         rx_ring->rx_stats.rx_copybreak_pkt += rx_copybreak_pkt;
1029         u64_stats_update_end(&rx_ring->syncp);
1030
1031         rx_ring->next_to_clean = next_to_clean;
1032
1033         refill_required = ena_com_sq_empty_space(rx_ring->ena_com_io_sq);
1034         refill_threshold = rx_ring->ring_size / ENA_RX_REFILL_THRESH_DIVIDER;
1035
1036         /* Optimization, try to batch new rx buffers */
1037         if (refill_required > refill_threshold) {
1038                 ena_com_update_dev_comp_head(rx_ring->ena_com_io_cq);
1039                 ena_refill_rx_bufs(rx_ring, refill_required);
1040         }
1041
1042         return work_done;
1043
1044 error:
1045         adapter = netdev_priv(rx_ring->netdev);
1046
1047         u64_stats_update_begin(&rx_ring->syncp);
1048         rx_ring->rx_stats.bad_desc_num++;
1049         u64_stats_update_end(&rx_ring->syncp);
1050
1051         /* Too many desc from the device. Trigger reset */
1052         set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags);
1053
1054         return 0;
1055 }
1056
1057 inline void ena_adjust_intr_moderation(struct ena_ring *rx_ring,
1058                                        struct ena_ring *tx_ring)
1059 {
1060         /* We apply adaptive moderation on Rx path only.
1061          * Tx uses static interrupt moderation.
1062          */
1063         ena_com_calculate_interrupt_delay(rx_ring->ena_dev,
1064                                           rx_ring->per_napi_packets,
1065                                           rx_ring->per_napi_bytes,
1066                                           &rx_ring->smoothed_interval,
1067                                           &rx_ring->moder_tbl_idx);
1068
1069         /* Reset per napi packets/bytes */
1070         tx_ring->per_napi_packets = 0;
1071         tx_ring->per_napi_bytes = 0;
1072         rx_ring->per_napi_packets = 0;
1073         rx_ring->per_napi_bytes = 0;
1074 }
1075
1076 static inline void ena_update_ring_numa_node(struct ena_ring *tx_ring,
1077                                              struct ena_ring *rx_ring)
1078 {
1079         int cpu = get_cpu();
1080         int numa_node;
1081
1082         /* Check only one ring since the 2 rings are running on the same cpu */
1083         if (likely(tx_ring->cpu == cpu))
1084                 goto out;
1085
1086         numa_node = cpu_to_node(cpu);
1087         put_cpu();
1088
1089         if (numa_node != NUMA_NO_NODE) {
1090                 ena_com_update_numa_node(tx_ring->ena_com_io_cq, numa_node);
1091                 ena_com_update_numa_node(rx_ring->ena_com_io_cq, numa_node);
1092         }
1093
1094         tx_ring->cpu = cpu;
1095         rx_ring->cpu = cpu;
1096
1097         return;
1098 out:
1099         put_cpu();
1100 }
1101
1102 static int ena_io_poll(struct napi_struct *napi, int budget)
1103 {
1104         struct ena_napi *ena_napi = container_of(napi, struct ena_napi, napi);
1105         struct ena_ring *tx_ring, *rx_ring;
1106         struct ena_eth_io_intr_reg intr_reg;
1107
1108         int tx_work_done;
1109         int rx_work_done = 0;
1110         int tx_budget;
1111         int napi_comp_call = 0;
1112         int ret;
1113
1114         tx_ring = ena_napi->tx_ring;
1115         rx_ring = ena_napi->rx_ring;
1116
1117         tx_budget = tx_ring->ring_size / ENA_TX_POLL_BUDGET_DIVIDER;
1118
1119         if (!test_bit(ENA_FLAG_DEV_UP, &tx_ring->adapter->flags)) {
1120                 napi_complete_done(napi, 0);
1121                 return 0;
1122         }
1123
1124         tx_work_done = ena_clean_tx_irq(tx_ring, tx_budget);
1125         /* On netpoll the budget is zero and the handler should only clean the
1126          * tx completions.
1127          */
1128         if (likely(budget))
1129                 rx_work_done = ena_clean_rx_irq(rx_ring, napi, budget);
1130
1131         if ((budget > rx_work_done) && (tx_budget > tx_work_done)) {
1132                 napi_complete_done(napi, rx_work_done);
1133
1134                 napi_comp_call = 1;
1135                 /* Tx and Rx share the same interrupt vector */
1136                 if (ena_com_get_adaptive_moderation_enabled(rx_ring->ena_dev))
1137                         ena_adjust_intr_moderation(rx_ring, tx_ring);
1138
1139                 /* Update intr register: rx intr delay, tx intr delay and
1140                  * interrupt unmask
1141                  */
1142                 ena_com_update_intr_reg(&intr_reg,
1143                                         rx_ring->smoothed_interval,
1144                                         tx_ring->smoothed_interval,
1145                                         true);
1146
1147                 /* It is a shared MSI-X. Tx and Rx CQ have pointer to it.
1148                  * So we use one of them to reach the intr reg
1149                  */
1150                 ena_com_unmask_intr(rx_ring->ena_com_io_cq, &intr_reg);
1151
1152                 ena_update_ring_numa_node(tx_ring, rx_ring);
1153
1154                 ret = rx_work_done;
1155         } else {
1156                 ret = budget;
1157         }
1158
1159         u64_stats_update_begin(&tx_ring->syncp);
1160         tx_ring->tx_stats.napi_comp += napi_comp_call;
1161         tx_ring->tx_stats.tx_poll++;
1162         u64_stats_update_end(&tx_ring->syncp);
1163
1164         return ret;
1165 }
1166
1167 static irqreturn_t ena_intr_msix_mgmnt(int irq, void *data)
1168 {
1169         struct ena_adapter *adapter = (struct ena_adapter *)data;
1170
1171         ena_com_admin_q_comp_intr_handler(adapter->ena_dev);
1172
1173         /* Don't call the aenq handler before probe is done */
1174         if (likely(test_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags)))
1175                 ena_com_aenq_intr_handler(adapter->ena_dev, data);
1176
1177         return IRQ_HANDLED;
1178 }
1179
1180 /* ena_intr_msix_io - MSI-X Interrupt Handler for Tx/Rx
1181  * @irq: interrupt number
1182  * @data: pointer to a network interface private napi device structure
1183  */
1184 static irqreturn_t ena_intr_msix_io(int irq, void *data)
1185 {
1186         struct ena_napi *ena_napi = data;
1187
1188         napi_schedule(&ena_napi->napi);
1189
1190         return IRQ_HANDLED;
1191 }
1192
1193 static int ena_enable_msix(struct ena_adapter *adapter, int num_queues)
1194 {
1195         int i, msix_vecs, rc;
1196
1197         if (test_bit(ENA_FLAG_MSIX_ENABLED, &adapter->flags)) {
1198                 netif_err(adapter, probe, adapter->netdev,
1199                           "Error, MSI-X is already enabled\n");
1200                 return -EPERM;
1201         }
1202
1203         /* Reserved the max msix vectors we might need */
1204         msix_vecs = ENA_MAX_MSIX_VEC(num_queues);
1205
1206         netif_dbg(adapter, probe, adapter->netdev,
1207                   "trying to enable MSI-X, vectors %d\n", msix_vecs);
1208
1209         adapter->msix_entries = vzalloc(msix_vecs * sizeof(struct msix_entry));
1210
1211         if (!adapter->msix_entries)
1212                 return -ENOMEM;
1213
1214         for (i = 0; i < msix_vecs; i++)
1215                 adapter->msix_entries[i].entry = i;
1216
1217         rc = pci_enable_msix(adapter->pdev, adapter->msix_entries, msix_vecs);
1218         if (rc != 0) {
1219                 netif_err(adapter, probe, adapter->netdev,
1220                           "Failed to enable MSI-X, vectors %d rc %d\n",
1221                           msix_vecs, rc);
1222                 return -ENOSPC;
1223         }
1224
1225         netif_dbg(adapter, probe, adapter->netdev, "enable MSI-X, vectors %d\n",
1226                   msix_vecs);
1227
1228         if (msix_vecs >= 1) {
1229                 if (ena_init_rx_cpu_rmap(adapter))
1230                         netif_warn(adapter, probe, adapter->netdev,
1231                                    "Failed to map IRQs to CPUs\n");
1232         }
1233
1234         adapter->msix_vecs = msix_vecs;
1235         set_bit(ENA_FLAG_MSIX_ENABLED, &adapter->flags);
1236
1237         return 0;
1238 }
1239
1240 static void ena_setup_mgmnt_intr(struct ena_adapter *adapter)
1241 {
1242         u32 cpu;
1243
1244         snprintf(adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].name,
1245                  ENA_IRQNAME_SIZE, "ena-mgmnt@pci:%s",
1246                  pci_name(adapter->pdev));
1247         adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].handler =
1248                 ena_intr_msix_mgmnt;
1249         adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].data = adapter;
1250         adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].vector =
1251                 adapter->msix_entries[ENA_MGMNT_IRQ_IDX].vector;
1252         cpu = cpumask_first(cpu_online_mask);
1253         adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].cpu = cpu;
1254         cpumask_set_cpu(cpu,
1255                         &adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].affinity_hint_mask);
1256 }
1257
1258 static void ena_setup_io_intr(struct ena_adapter *adapter)
1259 {
1260         struct net_device *netdev;
1261         int irq_idx, i, cpu;
1262
1263         netdev = adapter->netdev;
1264
1265         for (i = 0; i < adapter->num_queues; i++) {
1266                 irq_idx = ENA_IO_IRQ_IDX(i);
1267                 cpu = i % num_online_cpus();
1268
1269                 snprintf(adapter->irq_tbl[irq_idx].name, ENA_IRQNAME_SIZE,
1270                          "%s-Tx-Rx-%d", netdev->name, i);
1271                 adapter->irq_tbl[irq_idx].handler = ena_intr_msix_io;
1272                 adapter->irq_tbl[irq_idx].data = &adapter->ena_napi[i];
1273                 adapter->irq_tbl[irq_idx].vector =
1274                         adapter->msix_entries[irq_idx].vector;
1275                 adapter->irq_tbl[irq_idx].cpu = cpu;
1276
1277                 cpumask_set_cpu(cpu,
1278                                 &adapter->irq_tbl[irq_idx].affinity_hint_mask);
1279         }
1280 }
1281
1282 static int ena_request_mgmnt_irq(struct ena_adapter *adapter)
1283 {
1284         unsigned long flags = 0;
1285         struct ena_irq *irq;
1286         int rc;
1287
1288         irq = &adapter->irq_tbl[ENA_MGMNT_IRQ_IDX];
1289         rc = request_irq(irq->vector, irq->handler, flags, irq->name,
1290                          irq->data);
1291         if (rc) {
1292                 netif_err(adapter, probe, adapter->netdev,
1293                           "failed to request admin irq\n");
1294                 return rc;
1295         }
1296
1297         netif_dbg(adapter, probe, adapter->netdev,
1298                   "set affinity hint of mgmnt irq.to 0x%lx (irq vector: %d)\n",
1299                   irq->affinity_hint_mask.bits[0], irq->vector);
1300
1301         irq_set_affinity_hint(irq->vector, &irq->affinity_hint_mask);
1302
1303         return rc;
1304 }
1305
1306 static int ena_request_io_irq(struct ena_adapter *adapter)
1307 {
1308         unsigned long flags = 0;
1309         struct ena_irq *irq;
1310         int rc = 0, i, k;
1311
1312         if (!test_bit(ENA_FLAG_MSIX_ENABLED, &adapter->flags)) {
1313                 netif_err(adapter, ifup, adapter->netdev,
1314                           "Failed to request I/O IRQ: MSI-X is not enabled\n");
1315                 return -EINVAL;
1316         }
1317
1318         for (i = ENA_IO_IRQ_FIRST_IDX; i < adapter->msix_vecs; i++) {
1319                 irq = &adapter->irq_tbl[i];
1320                 rc = request_irq(irq->vector, irq->handler, flags, irq->name,
1321                                  irq->data);
1322                 if (rc) {
1323                         netif_err(adapter, ifup, adapter->netdev,
1324                                   "Failed to request I/O IRQ. index %d rc %d\n",
1325                                    i, rc);
1326                         goto err;
1327                 }
1328
1329                 netif_dbg(adapter, ifup, adapter->netdev,
1330                           "set affinity hint of irq. index %d to 0x%lx (irq vector: %d)\n",
1331                           i, irq->affinity_hint_mask.bits[0], irq->vector);
1332
1333                 irq_set_affinity_hint(irq->vector, &irq->affinity_hint_mask);
1334         }
1335
1336         return rc;
1337
1338 err:
1339         for (k = ENA_IO_IRQ_FIRST_IDX; k < i; k++) {
1340                 irq = &adapter->irq_tbl[k];
1341                 free_irq(irq->vector, irq->data);
1342         }
1343
1344         return rc;
1345 }
1346
1347 static void ena_free_mgmnt_irq(struct ena_adapter *adapter)
1348 {
1349         struct ena_irq *irq;
1350
1351         irq = &adapter->irq_tbl[ENA_MGMNT_IRQ_IDX];
1352         synchronize_irq(irq->vector);
1353         irq_set_affinity_hint(irq->vector, NULL);
1354         free_irq(irq->vector, irq->data);
1355 }
1356
1357 static void ena_free_io_irq(struct ena_adapter *adapter)
1358 {
1359         struct ena_irq *irq;
1360         int i;
1361
1362 #ifdef CONFIG_RFS_ACCEL
1363         if (adapter->msix_vecs >= 1) {
1364                 free_irq_cpu_rmap(adapter->netdev->rx_cpu_rmap);
1365                 adapter->netdev->rx_cpu_rmap = NULL;
1366         }
1367 #endif /* CONFIG_RFS_ACCEL */
1368
1369         for (i = ENA_IO_IRQ_FIRST_IDX; i < adapter->msix_vecs; i++) {
1370                 irq = &adapter->irq_tbl[i];
1371                 irq_set_affinity_hint(irq->vector, NULL);
1372                 free_irq(irq->vector, irq->data);
1373         }
1374 }
1375
1376 static void ena_disable_msix(struct ena_adapter *adapter)
1377 {
1378         if (test_and_clear_bit(ENA_FLAG_MSIX_ENABLED, &adapter->flags))
1379                 pci_disable_msix(adapter->pdev);
1380
1381         if (adapter->msix_entries)
1382                 vfree(adapter->msix_entries);
1383         adapter->msix_entries = NULL;
1384 }
1385
1386 static void ena_disable_io_intr_sync(struct ena_adapter *adapter)
1387 {
1388         int i;
1389
1390         if (!netif_running(adapter->netdev))
1391                 return;
1392
1393         for (i = ENA_IO_IRQ_FIRST_IDX; i < adapter->msix_vecs; i++)
1394                 synchronize_irq(adapter->irq_tbl[i].vector);
1395 }
1396
1397 static void ena_del_napi(struct ena_adapter *adapter)
1398 {
1399         int i;
1400
1401         for (i = 0; i < adapter->num_queues; i++)
1402                 netif_napi_del(&adapter->ena_napi[i].napi);
1403 }
1404
1405 static void ena_init_napi(struct ena_adapter *adapter)
1406 {
1407         struct ena_napi *napi;
1408         int i;
1409
1410         for (i = 0; i < adapter->num_queues; i++) {
1411                 napi = &adapter->ena_napi[i];
1412
1413                 netif_napi_add(adapter->netdev,
1414                                &adapter->ena_napi[i].napi,
1415                                ena_io_poll,
1416                                ENA_NAPI_BUDGET);
1417                 napi->rx_ring = &adapter->rx_ring[i];
1418                 napi->tx_ring = &adapter->tx_ring[i];
1419                 napi->qid = i;
1420         }
1421 }
1422
1423 static void ena_napi_disable_all(struct ena_adapter *adapter)
1424 {
1425         int i;
1426
1427         for (i = 0; i < adapter->num_queues; i++)
1428                 napi_disable(&adapter->ena_napi[i].napi);
1429 }
1430
1431 static void ena_napi_enable_all(struct ena_adapter *adapter)
1432 {
1433         int i;
1434
1435         for (i = 0; i < adapter->num_queues; i++)
1436                 napi_enable(&adapter->ena_napi[i].napi);
1437 }
1438
1439 static void ena_restore_ethtool_params(struct ena_adapter *adapter)
1440 {
1441         adapter->tx_usecs = 0;
1442         adapter->rx_usecs = 0;
1443         adapter->tx_frames = 1;
1444         adapter->rx_frames = 1;
1445 }
1446
1447 /* Configure the Rx forwarding */
1448 static int ena_rss_configure(struct ena_adapter *adapter)
1449 {
1450         struct ena_com_dev *ena_dev = adapter->ena_dev;
1451         int rc;
1452
1453         /* In case the RSS table wasn't initialized by probe */
1454         if (!ena_dev->rss.tbl_log_size) {
1455                 rc = ena_rss_init_default(adapter);
1456                 if (rc && (rc != -EPERM)) {
1457                         netif_err(adapter, ifup, adapter->netdev,
1458                                   "Failed to init RSS rc: %d\n", rc);
1459                         return rc;
1460                 }
1461         }
1462
1463         /* Set indirect table */
1464         rc = ena_com_indirect_table_set(ena_dev);
1465         if (unlikely(rc && rc != -EPERM))
1466                 return rc;
1467
1468         /* Configure hash function (if supported) */
1469         rc = ena_com_set_hash_function(ena_dev);
1470         if (unlikely(rc && (rc != -EPERM)))
1471                 return rc;
1472
1473         /* Configure hash inputs (if supported) */
1474         rc = ena_com_set_hash_ctrl(ena_dev);
1475         if (unlikely(rc && (rc != -EPERM)))
1476                 return rc;
1477
1478         return 0;
1479 }
1480
1481 static int ena_up_complete(struct ena_adapter *adapter)
1482 {
1483         int rc, i;
1484
1485         rc = ena_rss_configure(adapter);
1486         if (rc)
1487                 return rc;
1488
1489         ena_change_mtu(adapter->netdev, adapter->netdev->mtu);
1490
1491         ena_refill_all_rx_bufs(adapter);
1492
1493         /* enable transmits */
1494         netif_tx_start_all_queues(adapter->netdev);
1495
1496         ena_restore_ethtool_params(adapter);
1497
1498         ena_napi_enable_all(adapter);
1499
1500         /* schedule napi in case we had pending packets
1501          * from the last time we disable napi
1502          */
1503         for (i = 0; i < adapter->num_queues; i++)
1504                 napi_schedule(&adapter->ena_napi[i].napi);
1505
1506         return 0;
1507 }
1508
1509 static int ena_create_io_tx_queue(struct ena_adapter *adapter, int qid)
1510 {
1511         struct ena_com_create_io_ctx ctx = { 0 };
1512         struct ena_com_dev *ena_dev;
1513         struct ena_ring *tx_ring;
1514         u32 msix_vector;
1515         u16 ena_qid;
1516         int rc;
1517
1518         ena_dev = adapter->ena_dev;
1519
1520         tx_ring = &adapter->tx_ring[qid];
1521         msix_vector = ENA_IO_IRQ_IDX(qid);
1522         ena_qid = ENA_IO_TXQ_IDX(qid);
1523
1524         ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_TX;
1525         ctx.qid = ena_qid;
1526         ctx.mem_queue_type = ena_dev->tx_mem_queue_type;
1527         ctx.msix_vector = msix_vector;
1528         ctx.queue_size = adapter->tx_ring_size;
1529         ctx.numa_node = cpu_to_node(tx_ring->cpu);
1530
1531         rc = ena_com_create_io_queue(ena_dev, &ctx);
1532         if (rc) {
1533                 netif_err(adapter, ifup, adapter->netdev,
1534                           "Failed to create I/O TX queue num %d rc: %d\n",
1535                           qid, rc);
1536                 return rc;
1537         }
1538
1539         rc = ena_com_get_io_handlers(ena_dev, ena_qid,
1540                                      &tx_ring->ena_com_io_sq,
1541                                      &tx_ring->ena_com_io_cq);
1542         if (rc) {
1543                 netif_err(adapter, ifup, adapter->netdev,
1544                           "Failed to get TX queue handlers. TX queue num %d rc: %d\n",
1545                           qid, rc);
1546                 ena_com_destroy_io_queue(ena_dev, ena_qid);
1547                 return rc;
1548         }
1549
1550         ena_com_update_numa_node(tx_ring->ena_com_io_cq, ctx.numa_node);
1551         return rc;
1552 }
1553
1554 static int ena_create_all_io_tx_queues(struct ena_adapter *adapter)
1555 {
1556         struct ena_com_dev *ena_dev = adapter->ena_dev;
1557         int rc, i;
1558
1559         for (i = 0; i < adapter->num_queues; i++) {
1560                 rc = ena_create_io_tx_queue(adapter, i);
1561                 if (rc)
1562                         goto create_err;
1563         }
1564
1565         return 0;
1566
1567 create_err:
1568         while (i--)
1569                 ena_com_destroy_io_queue(ena_dev, ENA_IO_TXQ_IDX(i));
1570
1571         return rc;
1572 }
1573
1574 static int ena_create_io_rx_queue(struct ena_adapter *adapter, int qid)
1575 {
1576         struct ena_com_dev *ena_dev;
1577         struct ena_com_create_io_ctx ctx = { 0 };
1578         struct ena_ring *rx_ring;
1579         u32 msix_vector;
1580         u16 ena_qid;
1581         int rc;
1582
1583         ena_dev = adapter->ena_dev;
1584
1585         rx_ring = &adapter->rx_ring[qid];
1586         msix_vector = ENA_IO_IRQ_IDX(qid);
1587         ena_qid = ENA_IO_RXQ_IDX(qid);
1588
1589         ctx.qid = ena_qid;
1590         ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_RX;
1591         ctx.mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
1592         ctx.msix_vector = msix_vector;
1593         ctx.queue_size = adapter->rx_ring_size;
1594         ctx.numa_node = cpu_to_node(rx_ring->cpu);
1595
1596         rc = ena_com_create_io_queue(ena_dev, &ctx);
1597         if (rc) {
1598                 netif_err(adapter, ifup, adapter->netdev,
1599                           "Failed to create I/O RX queue num %d rc: %d\n",
1600                           qid, rc);
1601                 return rc;
1602         }
1603
1604         rc = ena_com_get_io_handlers(ena_dev, ena_qid,
1605                                      &rx_ring->ena_com_io_sq,
1606                                      &rx_ring->ena_com_io_cq);
1607         if (rc) {
1608                 netif_err(adapter, ifup, adapter->netdev,
1609                           "Failed to get RX queue handlers. RX queue num %d rc: %d\n",
1610                           qid, rc);
1611                 ena_com_destroy_io_queue(ena_dev, ena_qid);
1612                 return rc;
1613         }
1614
1615         ena_com_update_numa_node(rx_ring->ena_com_io_cq, ctx.numa_node);
1616
1617         return rc;
1618 }
1619
1620 static int ena_create_all_io_rx_queues(struct ena_adapter *adapter)
1621 {
1622         struct ena_com_dev *ena_dev = adapter->ena_dev;
1623         int rc, i;
1624
1625         for (i = 0; i < adapter->num_queues; i++) {
1626                 rc = ena_create_io_rx_queue(adapter, i);
1627                 if (rc)
1628                         goto create_err;
1629         }
1630
1631         return 0;
1632
1633 create_err:
1634         while (i--)
1635                 ena_com_destroy_io_queue(ena_dev, ENA_IO_RXQ_IDX(i));
1636
1637         return rc;
1638 }
1639
1640 static int ena_up(struct ena_adapter *adapter)
1641 {
1642         int rc;
1643
1644         netdev_dbg(adapter->netdev, "%s\n", __func__);
1645
1646         ena_setup_io_intr(adapter);
1647
1648         /* napi poll functions should be initialized before running
1649          * request_irq(), to handle a rare condition where there is a pending
1650          * interrupt, causing the ISR to fire immediately while the poll
1651          * function wasn't set yet, causing a null dereference
1652          */
1653         ena_init_napi(adapter);
1654
1655         rc = ena_request_io_irq(adapter);
1656         if (rc)
1657                 goto err_req_irq;
1658
1659         /* allocate transmit descriptors */
1660         rc = ena_setup_all_tx_resources(adapter);
1661         if (rc)
1662                 goto err_setup_tx;
1663
1664         /* allocate receive descriptors */
1665         rc = ena_setup_all_rx_resources(adapter);
1666         if (rc)
1667                 goto err_setup_rx;
1668
1669         /* Create TX queues */
1670         rc = ena_create_all_io_tx_queues(adapter);
1671         if (rc)
1672                 goto err_create_tx_queues;
1673
1674         /* Create RX queues */
1675         rc = ena_create_all_io_rx_queues(adapter);
1676         if (rc)
1677                 goto err_create_rx_queues;
1678
1679         rc = ena_up_complete(adapter);
1680         if (rc)
1681                 goto err_up;
1682
1683         if (test_bit(ENA_FLAG_LINK_UP, &adapter->flags))
1684                 netif_carrier_on(adapter->netdev);
1685
1686         u64_stats_update_begin(&adapter->syncp);
1687         adapter->dev_stats.interface_up++;
1688         u64_stats_update_end(&adapter->syncp);
1689
1690         set_bit(ENA_FLAG_DEV_UP, &adapter->flags);
1691
1692         return rc;
1693
1694 err_up:
1695         ena_destroy_all_rx_queues(adapter);
1696 err_create_rx_queues:
1697         ena_destroy_all_tx_queues(adapter);
1698 err_create_tx_queues:
1699         ena_free_all_io_rx_resources(adapter);
1700 err_setup_rx:
1701         ena_free_all_io_tx_resources(adapter);
1702 err_setup_tx:
1703         ena_free_io_irq(adapter);
1704 err_req_irq:
1705         ena_del_napi(adapter);
1706
1707         return rc;
1708 }
1709
1710 static void ena_down(struct ena_adapter *adapter)
1711 {
1712         netif_info(adapter, ifdown, adapter->netdev, "%s\n", __func__);
1713
1714         clear_bit(ENA_FLAG_DEV_UP, &adapter->flags);
1715
1716         u64_stats_update_begin(&adapter->syncp);
1717         adapter->dev_stats.interface_down++;
1718         u64_stats_update_end(&adapter->syncp);
1719
1720         /* After this point the napi handler won't enable the tx queue */
1721         ena_napi_disable_all(adapter);
1722         netif_carrier_off(adapter->netdev);
1723         netif_tx_disable(adapter->netdev);
1724
1725         /* After destroy the queue there won't be any new interrupts */
1726         ena_destroy_all_io_queues(adapter);
1727
1728         ena_disable_io_intr_sync(adapter);
1729         ena_free_io_irq(adapter);
1730         ena_del_napi(adapter);
1731
1732         ena_free_all_tx_bufs(adapter);
1733         ena_free_all_rx_bufs(adapter);
1734         ena_free_all_io_tx_resources(adapter);
1735         ena_free_all_io_rx_resources(adapter);
1736 }
1737
1738 /* ena_open - Called when a network interface is made active
1739  * @netdev: network interface device structure
1740  *
1741  * Returns 0 on success, negative value on failure
1742  *
1743  * The open entry point is called when a network interface is made
1744  * active by the system (IFF_UP).  At this point all resources needed
1745  * for transmit and receive operations are allocated, the interrupt
1746  * handler is registered with the OS, the watchdog timer is started,
1747  * and the stack is notified that the interface is ready.
1748  */
1749 static int ena_open(struct net_device *netdev)
1750 {
1751         struct ena_adapter *adapter = netdev_priv(netdev);
1752         int rc;
1753
1754         /* Notify the stack of the actual queue counts. */
1755         rc = netif_set_real_num_tx_queues(netdev, adapter->num_queues);
1756         if (rc) {
1757                 netif_err(adapter, ifup, netdev, "Can't set num tx queues\n");
1758                 return rc;
1759         }
1760
1761         rc = netif_set_real_num_rx_queues(netdev, adapter->num_queues);
1762         if (rc) {
1763                 netif_err(adapter, ifup, netdev, "Can't set num rx queues\n");
1764                 return rc;
1765         }
1766
1767         rc = ena_up(adapter);
1768         if (rc)
1769                 return rc;
1770
1771         return rc;
1772 }
1773
1774 /* ena_close - Disables a network interface
1775  * @netdev: network interface device structure
1776  *
1777  * Returns 0, this is not allowed to fail
1778  *
1779  * The close entry point is called when an interface is de-activated
1780  * by the OS.  The hardware is still under the drivers control, but
1781  * needs to be disabled.  A global MAC reset is issued to stop the
1782  * hardware, and all transmit and receive resources are freed.
1783  */
1784 static int ena_close(struct net_device *netdev)
1785 {
1786         struct ena_adapter *adapter = netdev_priv(netdev);
1787
1788         netif_dbg(adapter, ifdown, netdev, "%s\n", __func__);
1789
1790         if (test_bit(ENA_FLAG_DEV_UP, &adapter->flags))
1791                 ena_down(adapter);
1792
1793         return 0;
1794 }
1795
1796 static void ena_tx_csum(struct ena_com_tx_ctx *ena_tx_ctx, struct sk_buff *skb)
1797 {
1798         u32 mss = skb_shinfo(skb)->gso_size;
1799         struct ena_com_tx_meta *ena_meta = &ena_tx_ctx->ena_meta;
1800         u8 l4_protocol = 0;
1801
1802         if ((skb->ip_summed == CHECKSUM_PARTIAL) || mss) {
1803                 ena_tx_ctx->l4_csum_enable = 1;
1804                 if (mss) {
1805                         ena_tx_ctx->tso_enable = 1;
1806                         ena_meta->l4_hdr_len = tcp_hdr(skb)->doff;
1807                         ena_tx_ctx->l4_csum_partial = 0;
1808                 } else {
1809                         ena_tx_ctx->tso_enable = 0;
1810                         ena_meta->l4_hdr_len = 0;
1811                         ena_tx_ctx->l4_csum_partial = 1;
1812                 }
1813
1814                 switch (ip_hdr(skb)->version) {
1815                 case IPVERSION:
1816                         ena_tx_ctx->l3_proto = ENA_ETH_IO_L3_PROTO_IPV4;
1817                         if (ip_hdr(skb)->frag_off & htons(IP_DF))
1818                                 ena_tx_ctx->df = 1;
1819                         if (mss)
1820                                 ena_tx_ctx->l3_csum_enable = 1;
1821                         l4_protocol = ip_hdr(skb)->protocol;
1822                         break;
1823                 case 6:
1824                         ena_tx_ctx->l3_proto = ENA_ETH_IO_L3_PROTO_IPV6;
1825                         l4_protocol = ipv6_hdr(skb)->nexthdr;
1826                         break;
1827                 default:
1828                         break;
1829                 }
1830
1831                 if (l4_protocol == IPPROTO_TCP)
1832                         ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_TCP;
1833                 else
1834                         ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_UDP;
1835
1836                 ena_meta->mss = mss;
1837                 ena_meta->l3_hdr_len = skb_network_header_len(skb);
1838                 ena_meta->l3_hdr_offset = skb_network_offset(skb);
1839                 ena_tx_ctx->meta_valid = 1;
1840
1841         } else {
1842                 ena_tx_ctx->meta_valid = 0;
1843         }
1844 }
1845
1846 static int ena_check_and_linearize_skb(struct ena_ring *tx_ring,
1847                                        struct sk_buff *skb)
1848 {
1849         int num_frags, header_len, rc;
1850
1851         num_frags = skb_shinfo(skb)->nr_frags;
1852         header_len = skb_headlen(skb);
1853
1854         if (num_frags < tx_ring->sgl_size)
1855                 return 0;
1856
1857         if ((num_frags == tx_ring->sgl_size) &&
1858             (header_len < tx_ring->tx_max_header_size))
1859                 return 0;
1860
1861         u64_stats_update_begin(&tx_ring->syncp);
1862         tx_ring->tx_stats.linearize++;
1863         u64_stats_update_end(&tx_ring->syncp);
1864
1865         rc = skb_linearize(skb);
1866         if (unlikely(rc)) {
1867                 u64_stats_update_begin(&tx_ring->syncp);
1868                 tx_ring->tx_stats.linearize_failed++;
1869                 u64_stats_update_end(&tx_ring->syncp);
1870         }
1871
1872         return rc;
1873 }
1874
1875 /* Called with netif_tx_lock. */
1876 static netdev_tx_t ena_start_xmit(struct sk_buff *skb, struct net_device *dev)
1877 {
1878         struct ena_adapter *adapter = netdev_priv(dev);
1879         struct ena_tx_buffer *tx_info;
1880         struct ena_com_tx_ctx ena_tx_ctx;
1881         struct ena_ring *tx_ring;
1882         struct netdev_queue *txq;
1883         struct ena_com_buf *ena_buf;
1884         void *push_hdr;
1885         u32 len, last_frag;
1886         u16 next_to_use;
1887         u16 req_id;
1888         u16 push_len;
1889         u16 header_len;
1890         dma_addr_t dma;
1891         int qid, rc, nb_hw_desc;
1892         int i = -1;
1893
1894         netif_dbg(adapter, tx_queued, dev, "%s skb %p\n", __func__, skb);
1895         /*  Determine which tx ring we will be placed on */
1896         qid = skb_get_queue_mapping(skb);
1897         tx_ring = &adapter->tx_ring[qid];
1898         txq = netdev_get_tx_queue(dev, qid);
1899
1900         rc = ena_check_and_linearize_skb(tx_ring, skb);
1901         if (unlikely(rc))
1902                 goto error_drop_packet;
1903
1904         skb_tx_timestamp(skb);
1905         len = skb_headlen(skb);
1906
1907         next_to_use = tx_ring->next_to_use;
1908         req_id = tx_ring->free_tx_ids[next_to_use];
1909         tx_info = &tx_ring->tx_buffer_info[req_id];
1910         tx_info->num_of_bufs = 0;
1911
1912         WARN(tx_info->skb, "SKB isn't NULL req_id %d\n", req_id);
1913         ena_buf = tx_info->bufs;
1914         tx_info->skb = skb;
1915
1916         if (tx_ring->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV) {
1917                 /* prepared the push buffer */
1918                 push_len = min_t(u32, len, tx_ring->tx_max_header_size);
1919                 header_len = push_len;
1920                 push_hdr = skb->data;
1921         } else {
1922                 push_len = 0;
1923                 header_len = min_t(u32, len, tx_ring->tx_max_header_size);
1924                 push_hdr = NULL;
1925         }
1926
1927         netif_dbg(adapter, tx_queued, dev,
1928                   "skb: %p header_buf->vaddr: %p push_len: %d\n", skb,
1929                   push_hdr, push_len);
1930
1931         if (len > push_len) {
1932                 dma = dma_map_single(tx_ring->dev, skb->data + push_len,
1933                                      len - push_len, DMA_TO_DEVICE);
1934                 if (dma_mapping_error(tx_ring->dev, dma))
1935                         goto error_report_dma_error;
1936
1937                 ena_buf->paddr = dma;
1938                 ena_buf->len = len - push_len;
1939
1940                 ena_buf++;
1941                 tx_info->num_of_bufs++;
1942         }
1943
1944         last_frag = skb_shinfo(skb)->nr_frags;
1945
1946         for (i = 0; i < last_frag; i++) {
1947                 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1948
1949                 len = skb_frag_size(frag);
1950                 dma = skb_frag_dma_map(tx_ring->dev, frag, 0, len,
1951                                        DMA_TO_DEVICE);
1952                 if (dma_mapping_error(tx_ring->dev, dma))
1953                         goto error_report_dma_error;
1954
1955                 ena_buf->paddr = dma;
1956                 ena_buf->len = len;
1957                 ena_buf++;
1958         }
1959
1960         tx_info->num_of_bufs += last_frag;
1961
1962         memset(&ena_tx_ctx, 0x0, sizeof(struct ena_com_tx_ctx));
1963         ena_tx_ctx.ena_bufs = tx_info->bufs;
1964         ena_tx_ctx.push_header = push_hdr;
1965         ena_tx_ctx.num_bufs = tx_info->num_of_bufs;
1966         ena_tx_ctx.req_id = req_id;
1967         ena_tx_ctx.header_len = header_len;
1968
1969         /* set flags and meta data */
1970         ena_tx_csum(&ena_tx_ctx, skb);
1971
1972         /* prepare the packet's descriptors to dma engine */
1973         rc = ena_com_prepare_tx(tx_ring->ena_com_io_sq, &ena_tx_ctx,
1974                                 &nb_hw_desc);
1975
1976         if (unlikely(rc)) {
1977                 netif_err(adapter, tx_queued, dev,
1978                           "failed to prepare tx bufs\n");
1979                 u64_stats_update_begin(&tx_ring->syncp);
1980                 tx_ring->tx_stats.queue_stop++;
1981                 tx_ring->tx_stats.prepare_ctx_err++;
1982                 u64_stats_update_end(&tx_ring->syncp);
1983                 netif_tx_stop_queue(txq);
1984                 goto error_unmap_dma;
1985         }
1986
1987         netdev_tx_sent_queue(txq, skb->len);
1988
1989         u64_stats_update_begin(&tx_ring->syncp);
1990         tx_ring->tx_stats.cnt++;
1991         tx_ring->tx_stats.bytes += skb->len;
1992         u64_stats_update_end(&tx_ring->syncp);
1993
1994         tx_info->tx_descs = nb_hw_desc;
1995         tx_info->last_jiffies = jiffies;
1996
1997         tx_ring->next_to_use = ENA_TX_RING_IDX_NEXT(next_to_use,
1998                 tx_ring->ring_size);
1999
2000         /* This WMB is aimed to:
2001          * 1 - perform smp barrier before reading next_to_completion
2002          * 2 - make sure the desc were written before trigger DB
2003          */
2004         wmb();
2005
2006         /* stop the queue when no more space available, the packet can have up
2007          * to sgl_size + 2. one for the meta descriptor and one for header
2008          * (if the header is larger than tx_max_header_size).
2009          */
2010         if (unlikely(ena_com_sq_empty_space(tx_ring->ena_com_io_sq) <
2011                      (tx_ring->sgl_size + 2))) {
2012                 netif_dbg(adapter, tx_queued, dev, "%s stop queue %d\n",
2013                           __func__, qid);
2014
2015                 netif_tx_stop_queue(txq);
2016                 u64_stats_update_begin(&tx_ring->syncp);
2017                 tx_ring->tx_stats.queue_stop++;
2018                 u64_stats_update_end(&tx_ring->syncp);
2019
2020                 /* There is a rare condition where this function decide to
2021                  * stop the queue but meanwhile clean_tx_irq updates
2022                  * next_to_completion and terminates.
2023                  * The queue will remain stopped forever.
2024                  * To solve this issue this function perform rmb, check
2025                  * the wakeup condition and wake up the queue if needed.
2026                  */
2027                 smp_rmb();
2028
2029                 if (ena_com_sq_empty_space(tx_ring->ena_com_io_sq)
2030                                 > ENA_TX_WAKEUP_THRESH) {
2031                         netif_tx_wake_queue(txq);
2032                         u64_stats_update_begin(&tx_ring->syncp);
2033                         tx_ring->tx_stats.queue_wakeup++;
2034                         u64_stats_update_end(&tx_ring->syncp);
2035                 }
2036         }
2037
2038         if (netif_xmit_stopped(txq) || !skb->xmit_more) {
2039                 /* trigger the dma engine */
2040                 ena_com_write_sq_doorbell(tx_ring->ena_com_io_sq);
2041                 u64_stats_update_begin(&tx_ring->syncp);
2042                 tx_ring->tx_stats.doorbells++;
2043                 u64_stats_update_end(&tx_ring->syncp);
2044         }
2045
2046         return NETDEV_TX_OK;
2047
2048 error_report_dma_error:
2049         u64_stats_update_begin(&tx_ring->syncp);
2050         tx_ring->tx_stats.dma_mapping_err++;
2051         u64_stats_update_end(&tx_ring->syncp);
2052         netdev_warn(adapter->netdev, "failed to map skb\n");
2053
2054         tx_info->skb = NULL;
2055
2056 error_unmap_dma:
2057         if (i >= 0) {
2058                 /* save value of frag that failed */
2059                 last_frag = i;
2060
2061                 /* start back at beginning and unmap skb */
2062                 tx_info->skb = NULL;
2063                 ena_buf = tx_info->bufs;
2064                 dma_unmap_single(tx_ring->dev, dma_unmap_addr(ena_buf, paddr),
2065                                  dma_unmap_len(ena_buf, len), DMA_TO_DEVICE);
2066
2067                 /* unmap remaining mapped pages */
2068                 for (i = 0; i < last_frag; i++) {
2069                         ena_buf++;
2070                         dma_unmap_page(tx_ring->dev, dma_unmap_addr(ena_buf, paddr),
2071                                        dma_unmap_len(ena_buf, len), DMA_TO_DEVICE);
2072                 }
2073         }
2074
2075 error_drop_packet:
2076
2077         dev_kfree_skb(skb);
2078         return NETDEV_TX_OK;
2079 }
2080
2081 #ifdef CONFIG_NET_POLL_CONTROLLER
2082 static void ena_netpoll(struct net_device *netdev)
2083 {
2084         struct ena_adapter *adapter = netdev_priv(netdev);
2085         int i;
2086
2087         for (i = 0; i < adapter->num_queues; i++)
2088                 napi_schedule(&adapter->ena_napi[i].napi);
2089 }
2090 #endif /* CONFIG_NET_POLL_CONTROLLER */
2091
2092 static u16 ena_select_queue(struct net_device *dev, struct sk_buff *skb,
2093                             void *accel_priv, select_queue_fallback_t fallback)
2094 {
2095         u16 qid;
2096         /* we suspect that this is good for in--kernel network services that
2097          * want to loop incoming skb rx to tx in normal user generated traffic,
2098          * most probably we will not get to this
2099          */
2100         if (skb_rx_queue_recorded(skb))
2101                 qid = skb_get_rx_queue(skb);
2102         else
2103                 qid = fallback(dev, skb);
2104
2105         return qid;
2106 }
2107
2108 static void ena_config_host_info(struct ena_com_dev *ena_dev)
2109 {
2110         struct ena_admin_host_info *host_info;
2111         int rc;
2112
2113         /* Allocate only the host info */
2114         rc = ena_com_allocate_host_info(ena_dev);
2115         if (rc) {
2116                 pr_err("Cannot allocate host info\n");
2117                 return;
2118         }
2119
2120         host_info = ena_dev->host_attr.host_info;
2121
2122         host_info->os_type = ENA_ADMIN_OS_LINUX;
2123         host_info->kernel_ver = LINUX_VERSION_CODE;
2124         strlcpy(host_info->kernel_ver_str, utsname()->version,
2125                 sizeof(host_info->kernel_ver_str) - 1);
2126         host_info->os_dist = 0;
2127         strncpy(host_info->os_dist_str, utsname()->release,
2128                 sizeof(host_info->os_dist_str) - 1);
2129         host_info->driver_version =
2130                 (DRV_MODULE_VER_MAJOR) |
2131                 (DRV_MODULE_VER_MINOR << ENA_ADMIN_HOST_INFO_MINOR_SHIFT) |
2132                 (DRV_MODULE_VER_SUBMINOR << ENA_ADMIN_HOST_INFO_SUB_MINOR_SHIFT);
2133
2134         rc = ena_com_set_host_attributes(ena_dev);
2135         if (rc) {
2136                 if (rc == -EPERM)
2137                         pr_warn("Cannot set host attributes\n");
2138                 else
2139                         pr_err("Cannot set host attributes\n");
2140
2141                 goto err;
2142         }
2143
2144         return;
2145
2146 err:
2147         ena_com_delete_host_info(ena_dev);
2148 }
2149
2150 static void ena_config_debug_area(struct ena_adapter *adapter)
2151 {
2152         u32 debug_area_size;
2153         int rc, ss_count;
2154
2155         ss_count = ena_get_sset_count(adapter->netdev, ETH_SS_STATS);
2156         if (ss_count <= 0) {
2157                 netif_err(adapter, drv, adapter->netdev,
2158                           "SS count is negative\n");
2159                 return;
2160         }
2161
2162         /* allocate 32 bytes for each string and 64bit for the value */
2163         debug_area_size = ss_count * ETH_GSTRING_LEN + sizeof(u64) * ss_count;
2164
2165         rc = ena_com_allocate_debug_area(adapter->ena_dev, debug_area_size);
2166         if (rc) {
2167                 pr_err("Cannot allocate debug area\n");
2168                 return;
2169         }
2170
2171         rc = ena_com_set_host_attributes(adapter->ena_dev);
2172         if (rc) {
2173                 if (rc == -EPERM)
2174                         netif_warn(adapter, drv, adapter->netdev,
2175                                    "Cannot set host attributes\n");
2176                 else
2177                         netif_err(adapter, drv, adapter->netdev,
2178                                   "Cannot set host attributes\n");
2179                 goto err;
2180         }
2181
2182         return;
2183 err:
2184         ena_com_delete_debug_area(adapter->ena_dev);
2185 }
2186
2187 static struct rtnl_link_stats64 *ena_get_stats64(struct net_device *netdev,
2188                                                  struct rtnl_link_stats64 *stats)
2189 {
2190         struct ena_adapter *adapter = netdev_priv(netdev);
2191         struct ena_admin_basic_stats ena_stats;
2192         int rc;
2193
2194         if (!test_bit(ENA_FLAG_DEV_UP, &adapter->flags))
2195                 return NULL;
2196
2197         rc = ena_com_get_dev_basic_stats(adapter->ena_dev, &ena_stats);
2198         if (rc)
2199                 return NULL;
2200
2201         stats->tx_bytes = ((u64)ena_stats.tx_bytes_high << 32) |
2202                 ena_stats.tx_bytes_low;
2203         stats->rx_bytes = ((u64)ena_stats.rx_bytes_high << 32) |
2204                 ena_stats.rx_bytes_low;
2205
2206         stats->rx_packets = ((u64)ena_stats.rx_pkts_high << 32) |
2207                 ena_stats.rx_pkts_low;
2208         stats->tx_packets = ((u64)ena_stats.tx_pkts_high << 32) |
2209                 ena_stats.tx_pkts_low;
2210
2211         stats->rx_dropped = ((u64)ena_stats.rx_drops_high << 32) |
2212                 ena_stats.rx_drops_low;
2213
2214         stats->multicast = 0;
2215         stats->collisions = 0;
2216
2217         stats->rx_length_errors = 0;
2218         stats->rx_crc_errors = 0;
2219         stats->rx_frame_errors = 0;
2220         stats->rx_fifo_errors = 0;
2221         stats->rx_missed_errors = 0;
2222         stats->tx_window_errors = 0;
2223
2224         stats->rx_errors = 0;
2225         stats->tx_errors = 0;
2226
2227         return stats;
2228 }
2229
2230 static const struct net_device_ops ena_netdev_ops = {
2231         .ndo_open               = ena_open,
2232         .ndo_stop               = ena_close,
2233         .ndo_start_xmit         = ena_start_xmit,
2234         .ndo_select_queue       = ena_select_queue,
2235         .ndo_get_stats64        = ena_get_stats64,
2236         .ndo_tx_timeout         = ena_tx_timeout,
2237         .ndo_change_mtu         = ena_change_mtu,
2238         .ndo_set_mac_address    = NULL,
2239         .ndo_validate_addr      = eth_validate_addr,
2240 #ifdef CONFIG_NET_POLL_CONTROLLER
2241         .ndo_poll_controller    = ena_netpoll,
2242 #endif /* CONFIG_NET_POLL_CONTROLLER */
2243 };
2244
2245 static void ena_device_io_suspend(struct work_struct *work)
2246 {
2247         struct ena_adapter *adapter =
2248                 container_of(work, struct ena_adapter, suspend_io_task);
2249         struct net_device *netdev = adapter->netdev;
2250
2251         /* ena_napi_disable_all disables only the IO handling.
2252          * We are still subject to AENQ keep alive watchdog.
2253          */
2254         u64_stats_update_begin(&adapter->syncp);
2255         adapter->dev_stats.io_suspend++;
2256         u64_stats_update_begin(&adapter->syncp);
2257         ena_napi_disable_all(adapter);
2258         netif_tx_lock(netdev);
2259         netif_device_detach(netdev);
2260         netif_tx_unlock(netdev);
2261 }
2262
2263 static void ena_device_io_resume(struct work_struct *work)
2264 {
2265         struct ena_adapter *adapter =
2266                 container_of(work, struct ena_adapter, resume_io_task);
2267         struct net_device *netdev = adapter->netdev;
2268
2269         u64_stats_update_begin(&adapter->syncp);
2270         adapter->dev_stats.io_resume++;
2271         u64_stats_update_end(&adapter->syncp);
2272
2273         netif_device_attach(netdev);
2274         ena_napi_enable_all(adapter);
2275 }
2276
2277 static int ena_device_validate_params(struct ena_adapter *adapter,
2278                                       struct ena_com_dev_get_features_ctx *get_feat_ctx)
2279 {
2280         struct net_device *netdev = adapter->netdev;
2281         int rc;
2282
2283         rc = ether_addr_equal(get_feat_ctx->dev_attr.mac_addr,
2284                               adapter->mac_addr);
2285         if (!rc) {
2286                 netif_err(adapter, drv, netdev,
2287                           "Error, mac address are different\n");
2288                 return -EINVAL;
2289         }
2290
2291         if ((get_feat_ctx->max_queues.max_cq_num < adapter->num_queues) ||
2292             (get_feat_ctx->max_queues.max_sq_num < adapter->num_queues)) {
2293                 netif_err(adapter, drv, netdev,
2294                           "Error, device doesn't support enough queues\n");
2295                 return -EINVAL;
2296         }
2297
2298         if (get_feat_ctx->dev_attr.max_mtu < netdev->mtu) {
2299                 netif_err(adapter, drv, netdev,
2300                           "Error, device max mtu is smaller than netdev MTU\n");
2301                 return -EINVAL;
2302         }
2303
2304         return 0;
2305 }
2306
2307 static int ena_device_init(struct ena_com_dev *ena_dev, struct pci_dev *pdev,
2308                            struct ena_com_dev_get_features_ctx *get_feat_ctx,
2309                            bool *wd_state)
2310 {
2311         struct device *dev = &pdev->dev;
2312         bool readless_supported;
2313         u32 aenq_groups;
2314         int dma_width;
2315         int rc;
2316
2317         rc = ena_com_mmio_reg_read_request_init(ena_dev);
2318         if (rc) {
2319                 dev_err(dev, "failed to init mmio read less\n");
2320                 return rc;
2321         }
2322
2323         /* The PCIe configuration space revision id indicate if mmio reg
2324          * read is disabled
2325          */
2326         readless_supported = !(pdev->revision & ENA_MMIO_DISABLE_REG_READ);
2327         ena_com_set_mmio_read_mode(ena_dev, readless_supported);
2328
2329         rc = ena_com_dev_reset(ena_dev);
2330         if (rc) {
2331                 dev_err(dev, "Can not reset device\n");
2332                 goto err_mmio_read_less;
2333         }
2334
2335         rc = ena_com_validate_version(ena_dev);
2336         if (rc) {
2337                 dev_err(dev, "device version is too low\n");
2338                 goto err_mmio_read_less;
2339         }
2340
2341         dma_width = ena_com_get_dma_width(ena_dev);
2342         if (dma_width < 0) {
2343                 dev_err(dev, "Invalid dma width value %d", dma_width);
2344                 rc = dma_width;
2345                 goto err_mmio_read_less;
2346         }
2347
2348         rc = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(dma_width));
2349         if (rc) {
2350                 dev_err(dev, "dma_set_mask_and_coherent failed %d\n", rc);
2351                 goto err_mmio_read_less;
2352         }
2353
2354         /* ENA admin level init */
2355         rc = ena_com_admin_init(ena_dev, &aenq_handlers, true);
2356         if (rc) {
2357                 dev_err(dev,
2358                         "Can not initialize ena admin queue with device\n");
2359                 goto err_mmio_read_less;
2360         }
2361
2362         /* To enable the msix interrupts the driver needs to know the number
2363          * of queues. So the driver uses polling mode to retrieve this
2364          * information
2365          */
2366         ena_com_set_admin_polling_mode(ena_dev, true);
2367
2368         /* Get Device Attributes*/
2369         rc = ena_com_get_dev_attr_feat(ena_dev, get_feat_ctx);
2370         if (rc) {
2371                 dev_err(dev, "Cannot get attribute for ena device rc=%d\n", rc);
2372                 goto err_admin_init;
2373         }
2374
2375         /* Try to turn all the available aenq groups */
2376         aenq_groups = BIT(ENA_ADMIN_LINK_CHANGE) |
2377                 BIT(ENA_ADMIN_FATAL_ERROR) |
2378                 BIT(ENA_ADMIN_WARNING) |
2379                 BIT(ENA_ADMIN_NOTIFICATION) |
2380                 BIT(ENA_ADMIN_KEEP_ALIVE);
2381
2382         aenq_groups &= get_feat_ctx->aenq.supported_groups;
2383
2384         rc = ena_com_set_aenq_config(ena_dev, aenq_groups);
2385         if (rc) {
2386                 dev_err(dev, "Cannot configure aenq groups rc= %d\n", rc);
2387                 goto err_admin_init;
2388         }
2389
2390         *wd_state = !!(aenq_groups & BIT(ENA_ADMIN_KEEP_ALIVE));
2391
2392         ena_config_host_info(ena_dev);
2393
2394         return 0;
2395
2396 err_admin_init:
2397         ena_com_admin_destroy(ena_dev);
2398 err_mmio_read_less:
2399         ena_com_mmio_reg_read_request_destroy(ena_dev);
2400
2401         return rc;
2402 }
2403
2404 static int ena_enable_msix_and_set_admin_interrupts(struct ena_adapter *adapter,
2405                                                     int io_vectors)
2406 {
2407         struct ena_com_dev *ena_dev = adapter->ena_dev;
2408         struct device *dev = &adapter->pdev->dev;
2409         int rc;
2410
2411         rc = ena_enable_msix(adapter, io_vectors);
2412         if (rc) {
2413                 dev_err(dev, "Can not reserve msix vectors\n");
2414                 return rc;
2415         }
2416
2417         ena_setup_mgmnt_intr(adapter);
2418
2419         rc = ena_request_mgmnt_irq(adapter);
2420         if (rc) {
2421                 dev_err(dev, "Can not setup management interrupts\n");
2422                 goto err_disable_msix;
2423         }
2424
2425         ena_com_set_admin_polling_mode(ena_dev, false);
2426
2427         ena_com_admin_aenq_enable(ena_dev);
2428
2429         return 0;
2430
2431 err_disable_msix:
2432         ena_disable_msix(adapter);
2433
2434         return rc;
2435 }
2436
2437 static void ena_fw_reset_device(struct work_struct *work)
2438 {
2439         struct ena_com_dev_get_features_ctx get_feat_ctx;
2440         struct ena_adapter *adapter =
2441                 container_of(work, struct ena_adapter, reset_task);
2442         struct net_device *netdev = adapter->netdev;
2443         struct ena_com_dev *ena_dev = adapter->ena_dev;
2444         struct pci_dev *pdev = adapter->pdev;
2445         bool dev_up, wd_state;
2446         int rc;
2447
2448         del_timer_sync(&adapter->timer_service);
2449
2450         rtnl_lock();
2451
2452         dev_up = test_bit(ENA_FLAG_DEV_UP, &adapter->flags);
2453         ena_com_set_admin_running_state(ena_dev, false);
2454
2455         /* After calling ena_close the tx queues and the napi
2456          * are disabled so no one can interfere or touch the
2457          * data structures
2458          */
2459         ena_close(netdev);
2460
2461         rc = ena_com_dev_reset(ena_dev);
2462         if (rc) {
2463                 dev_err(&pdev->dev, "Device reset failed\n");
2464                 goto err;
2465         }
2466
2467         ena_free_mgmnt_irq(adapter);
2468
2469         ena_disable_msix(adapter);
2470
2471         ena_com_abort_admin_commands(ena_dev);
2472
2473         ena_com_wait_for_abort_completion(ena_dev);
2474
2475         ena_com_admin_destroy(ena_dev);
2476
2477         ena_com_mmio_reg_read_request_destroy(ena_dev);
2478
2479         /* Finish with the destroy part. Start the init part */
2480
2481         rc = ena_device_init(ena_dev, adapter->pdev, &get_feat_ctx, &wd_state);
2482         if (rc) {
2483                 dev_err(&pdev->dev, "Can not initialize device\n");
2484                 goto err;
2485         }
2486         adapter->wd_state = wd_state;
2487
2488         rc = ena_device_validate_params(adapter, &get_feat_ctx);
2489         if (rc) {
2490                 dev_err(&pdev->dev, "Validation of device parameters failed\n");
2491                 goto err_device_destroy;
2492         }
2493
2494         rc = ena_enable_msix_and_set_admin_interrupts(adapter,
2495                                                       adapter->num_queues);
2496         if (rc) {
2497                 dev_err(&pdev->dev, "Enable MSI-X failed\n");
2498                 goto err_device_destroy;
2499         }
2500         /* If the interface was up before the reset bring it up */
2501         if (dev_up) {
2502                 rc = ena_up(adapter);
2503                 if (rc) {
2504                         dev_err(&pdev->dev, "Failed to create I/O queues\n");
2505                         goto err_disable_msix;
2506                 }
2507         }
2508
2509         mod_timer(&adapter->timer_service, round_jiffies(jiffies + HZ));
2510
2511         rtnl_unlock();
2512
2513         dev_err(&pdev->dev, "Device reset completed successfully\n");
2514
2515         return;
2516 err_disable_msix:
2517         ena_free_mgmnt_irq(adapter);
2518         ena_disable_msix(adapter);
2519 err_device_destroy:
2520         ena_com_admin_destroy(ena_dev);
2521 err:
2522         rtnl_unlock();
2523
2524         dev_err(&pdev->dev,
2525                 "Reset attempt failed. Can not reset the device\n");
2526 }
2527
2528 static void check_for_missing_tx_completions(struct ena_adapter *adapter)
2529 {
2530         struct ena_tx_buffer *tx_buf;
2531         unsigned long last_jiffies;
2532         struct ena_ring *tx_ring;
2533         int i, j, budget;
2534         u32 missed_tx;
2535
2536         /* Make sure the driver doesn't turn the device in other process */
2537         smp_rmb();
2538
2539         if (!test_bit(ENA_FLAG_DEV_UP, &adapter->flags))
2540                 return;
2541
2542         budget = ENA_MONITORED_TX_QUEUES;
2543
2544         for (i = adapter->last_monitored_tx_qid; i < adapter->num_queues; i++) {
2545                 tx_ring = &adapter->tx_ring[i];
2546
2547                 for (j = 0; j < tx_ring->ring_size; j++) {
2548                         tx_buf = &tx_ring->tx_buffer_info[j];
2549                         last_jiffies = tx_buf->last_jiffies;
2550                         if (unlikely(last_jiffies && time_is_before_jiffies(last_jiffies + TX_TIMEOUT))) {
2551                                 netif_notice(adapter, tx_err, adapter->netdev,
2552                                              "Found a Tx that wasn't completed on time, qid %d, index %d.\n",
2553                                              tx_ring->qid, j);
2554
2555                                 u64_stats_update_begin(&tx_ring->syncp);
2556                                 missed_tx = tx_ring->tx_stats.missing_tx_comp++;
2557                                 u64_stats_update_end(&tx_ring->syncp);
2558
2559                                 /* Clear last jiffies so the lost buffer won't
2560                                  * be counted twice.
2561                                  */
2562                                 tx_buf->last_jiffies = 0;
2563
2564                                 if (unlikely(missed_tx > MAX_NUM_OF_TIMEOUTED_PACKETS)) {
2565                                         netif_err(adapter, tx_err, adapter->netdev,
2566                                                   "The number of lost tx completion is above the threshold (%d > %d). Reset the device\n",
2567                                                   missed_tx, MAX_NUM_OF_TIMEOUTED_PACKETS);
2568                                         set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags);
2569                                 }
2570                         }
2571                 }
2572
2573                 budget--;
2574                 if (!budget)
2575                         break;
2576         }
2577
2578         adapter->last_monitored_tx_qid = i % adapter->num_queues;
2579 }
2580
2581 /* Check for keep alive expiration */
2582 static void check_for_missing_keep_alive(struct ena_adapter *adapter)
2583 {
2584         unsigned long keep_alive_expired;
2585
2586         if (!adapter->wd_state)
2587                 return;
2588
2589         keep_alive_expired = round_jiffies(adapter->last_keep_alive_jiffies
2590                                            + ENA_DEVICE_KALIVE_TIMEOUT);
2591         if (unlikely(time_is_before_jiffies(keep_alive_expired))) {
2592                 netif_err(adapter, drv, adapter->netdev,
2593                           "Keep alive watchdog timeout.\n");
2594                 u64_stats_update_begin(&adapter->syncp);
2595                 adapter->dev_stats.wd_expired++;
2596                 u64_stats_update_end(&adapter->syncp);
2597                 set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags);
2598         }
2599 }
2600
2601 static void check_for_admin_com_state(struct ena_adapter *adapter)
2602 {
2603         if (unlikely(!ena_com_get_admin_running_state(adapter->ena_dev))) {
2604                 netif_err(adapter, drv, adapter->netdev,
2605                           "ENA admin queue is not in running state!\n");
2606                 u64_stats_update_begin(&adapter->syncp);
2607                 adapter->dev_stats.admin_q_pause++;
2608                 u64_stats_update_end(&adapter->syncp);
2609                 set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags);
2610         }
2611 }
2612
2613 static void ena_update_host_info(struct ena_admin_host_info *host_info,
2614                                  struct net_device *netdev)
2615 {
2616         host_info->supported_network_features[0] =
2617                 netdev->features & GENMASK_ULL(31, 0);
2618         host_info->supported_network_features[1] =
2619                 (netdev->features & GENMASK_ULL(63, 32)) >> 32;
2620 }
2621
2622 static void ena_timer_service(unsigned long data)
2623 {
2624         struct ena_adapter *adapter = (struct ena_adapter *)data;
2625         u8 *debug_area = adapter->ena_dev->host_attr.debug_area_virt_addr;
2626         struct ena_admin_host_info *host_info =
2627                 adapter->ena_dev->host_attr.host_info;
2628
2629         check_for_missing_keep_alive(adapter);
2630
2631         check_for_admin_com_state(adapter);
2632
2633         check_for_missing_tx_completions(adapter);
2634
2635         if (debug_area)
2636                 ena_dump_stats_to_buf(adapter, debug_area);
2637
2638         if (host_info)
2639                 ena_update_host_info(host_info, adapter->netdev);
2640
2641         if (unlikely(test_and_clear_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags))) {
2642                 netif_err(adapter, drv, adapter->netdev,
2643                           "Trigger reset is on\n");
2644                 ena_dump_stats_to_dmesg(adapter);
2645                 queue_work(ena_wq, &adapter->reset_task);
2646                 return;
2647         }
2648
2649         /* Reset the timer */
2650         mod_timer(&adapter->timer_service, jiffies + HZ);
2651 }
2652
2653 static int ena_calc_io_queue_num(struct pci_dev *pdev,
2654                                  struct ena_com_dev *ena_dev,
2655                                  struct ena_com_dev_get_features_ctx *get_feat_ctx)
2656 {
2657         int io_sq_num, io_queue_num;
2658
2659         /* In case of LLQ use the llq number in the get feature cmd */
2660         if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV) {
2661                 io_sq_num = get_feat_ctx->max_queues.max_llq_num;
2662
2663                 if (io_sq_num == 0) {
2664                         dev_err(&pdev->dev,
2665                                 "Trying to use LLQ but llq_num is 0. Fall back into regular queues\n");
2666
2667                         ena_dev->tx_mem_queue_type =
2668                                 ENA_ADMIN_PLACEMENT_POLICY_HOST;
2669                         io_sq_num = get_feat_ctx->max_queues.max_sq_num;
2670                 }
2671         } else {
2672                 io_sq_num = get_feat_ctx->max_queues.max_sq_num;
2673         }
2674
2675         io_queue_num = min_t(int, num_possible_cpus(), ENA_MAX_NUM_IO_QUEUES);
2676         io_queue_num = min_t(int, io_queue_num, io_sq_num);
2677         io_queue_num = min_t(int, io_queue_num,
2678                              get_feat_ctx->max_queues.max_cq_num);
2679         /* 1 IRQ for for mgmnt and 1 IRQs for each IO direction */
2680         io_queue_num = min_t(int, io_queue_num, pci_msix_vec_count(pdev) - 1);
2681         if (unlikely(!io_queue_num)) {
2682                 dev_err(&pdev->dev, "The device doesn't have io queues\n");
2683                 return -EFAULT;
2684         }
2685
2686         return io_queue_num;
2687 }
2688
2689 static void ena_set_push_mode(struct pci_dev *pdev, struct ena_com_dev *ena_dev,
2690                               struct ena_com_dev_get_features_ctx *get_feat_ctx)
2691 {
2692         bool has_mem_bar;
2693
2694         has_mem_bar = pci_select_bars(pdev, IORESOURCE_MEM) & BIT(ENA_MEM_BAR);
2695
2696         /* Enable push mode if device supports LLQ */
2697         if (has_mem_bar && (get_feat_ctx->max_queues.max_llq_num > 0))
2698                 ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_DEV;
2699         else
2700                 ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
2701 }
2702
2703 static void ena_set_dev_offloads(struct ena_com_dev_get_features_ctx *feat,
2704                                  struct net_device *netdev)
2705 {
2706         netdev_features_t dev_features = 0;
2707
2708         /* Set offload features */
2709         if (feat->offload.tx &
2710                 ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_PART_MASK)
2711                 dev_features |= NETIF_F_IP_CSUM;
2712
2713         if (feat->offload.tx &
2714                 ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV6_CSUM_PART_MASK)
2715                 dev_features |= NETIF_F_IPV6_CSUM;
2716
2717         if (feat->offload.tx & ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV4_MASK)
2718                 dev_features |= NETIF_F_TSO;
2719
2720         if (feat->offload.tx & ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV6_MASK)
2721                 dev_features |= NETIF_F_TSO6;
2722
2723         if (feat->offload.tx & ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_ECN_MASK)
2724                 dev_features |= NETIF_F_TSO_ECN;
2725
2726         if (feat->offload.rx_supported &
2727                 ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV4_CSUM_MASK)
2728                 dev_features |= NETIF_F_RXCSUM;
2729
2730         if (feat->offload.rx_supported &
2731                 ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV6_CSUM_MASK)
2732                 dev_features |= NETIF_F_RXCSUM;
2733
2734         netdev->features =
2735                 dev_features |
2736                 NETIF_F_SG |
2737                 NETIF_F_NTUPLE |
2738                 NETIF_F_RXHASH |
2739                 NETIF_F_HIGHDMA;
2740
2741         netdev->hw_features |= netdev->features;
2742         netdev->vlan_features |= netdev->features;
2743 }
2744
2745 static void ena_set_conf_feat_params(struct ena_adapter *adapter,
2746                                      struct ena_com_dev_get_features_ctx *feat)
2747 {
2748         struct net_device *netdev = adapter->netdev;
2749
2750         /* Copy mac address */
2751         if (!is_valid_ether_addr(feat->dev_attr.mac_addr)) {
2752                 eth_hw_addr_random(netdev);
2753                 ether_addr_copy(adapter->mac_addr, netdev->dev_addr);
2754         } else {
2755                 ether_addr_copy(adapter->mac_addr, feat->dev_attr.mac_addr);
2756                 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
2757         }
2758
2759         /* Set offload features */
2760         ena_set_dev_offloads(feat, netdev);
2761
2762         adapter->max_mtu = feat->dev_attr.max_mtu;
2763 }
2764
2765 static int ena_rss_init_default(struct ena_adapter *adapter)
2766 {
2767         struct ena_com_dev *ena_dev = adapter->ena_dev;
2768         struct device *dev = &adapter->pdev->dev;
2769         int rc, i;
2770         u32 val;
2771
2772         rc = ena_com_rss_init(ena_dev, ENA_RX_RSS_TABLE_LOG_SIZE);
2773         if (unlikely(rc)) {
2774                 dev_err(dev, "Cannot init indirect table\n");
2775                 goto err_rss_init;
2776         }
2777
2778         for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++) {
2779                 val = ethtool_rxfh_indir_default(i, adapter->num_queues);
2780                 rc = ena_com_indirect_table_fill_entry(ena_dev, i,
2781                                                        ENA_IO_RXQ_IDX(val));
2782                 if (unlikely(rc && (rc != -EPERM))) {
2783                         dev_err(dev, "Cannot fill indirect table\n");
2784                         goto err_fill_indir;
2785                 }
2786         }
2787
2788         rc = ena_com_fill_hash_function(ena_dev, ENA_ADMIN_CRC32, NULL,
2789                                         ENA_HASH_KEY_SIZE, 0xFFFFFFFF);
2790         if (unlikely(rc && (rc != -EPERM))) {
2791                 dev_err(dev, "Cannot fill hash function\n");
2792                 goto err_fill_indir;
2793         }
2794
2795         rc = ena_com_set_default_hash_ctrl(ena_dev);
2796         if (unlikely(rc && (rc != -EPERM))) {
2797                 dev_err(dev, "Cannot fill hash control\n");
2798                 goto err_fill_indir;
2799         }
2800
2801         return 0;
2802
2803 err_fill_indir:
2804         ena_com_rss_destroy(ena_dev);
2805 err_rss_init:
2806
2807         return rc;
2808 }
2809
2810 static void ena_release_bars(struct ena_com_dev *ena_dev, struct pci_dev *pdev)
2811 {
2812         int release_bars;
2813
2814         if (ena_dev->mem_bar)
2815                 devm_iounmap(&pdev->dev, ena_dev->mem_bar);
2816
2817         devm_iounmap(&pdev->dev, ena_dev->reg_bar);
2818
2819         release_bars = pci_select_bars(pdev, IORESOURCE_MEM) & ENA_BAR_MASK;
2820         pci_release_selected_regions(pdev, release_bars);
2821 }
2822
2823 static int ena_calc_queue_size(struct pci_dev *pdev,
2824                                struct ena_com_dev *ena_dev,
2825                                u16 *max_tx_sgl_size,
2826                                u16 *max_rx_sgl_size,
2827                                struct ena_com_dev_get_features_ctx *get_feat_ctx)
2828 {
2829         u32 queue_size = ENA_DEFAULT_RING_SIZE;
2830
2831         queue_size = min_t(u32, queue_size,
2832                            get_feat_ctx->max_queues.max_cq_depth);
2833         queue_size = min_t(u32, queue_size,
2834                            get_feat_ctx->max_queues.max_sq_depth);
2835
2836         if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV)
2837                 queue_size = min_t(u32, queue_size,
2838                                    get_feat_ctx->max_queues.max_llq_depth);
2839
2840         queue_size = rounddown_pow_of_two(queue_size);
2841
2842         if (unlikely(!queue_size)) {
2843                 dev_err(&pdev->dev, "Invalid queue size\n");
2844                 return -EFAULT;
2845         }
2846
2847         *max_tx_sgl_size = min_t(u16, ENA_PKT_MAX_BUFS,
2848                                  get_feat_ctx->max_queues.max_packet_tx_descs);
2849         *max_rx_sgl_size = min_t(u16, ENA_PKT_MAX_BUFS,
2850                                  get_feat_ctx->max_queues.max_packet_rx_descs);
2851
2852         return queue_size;
2853 }
2854
2855 /* ena_probe - Device Initialization Routine
2856  * @pdev: PCI device information struct
2857  * @ent: entry in ena_pci_tbl
2858  *
2859  * Returns 0 on success, negative on failure
2860  *
2861  * ena_probe initializes an adapter identified by a pci_dev structure.
2862  * The OS initialization, configuring of the adapter private structure,
2863  * and a hardware reset occur.
2864  */
2865 static int ena_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2866 {
2867         struct ena_com_dev_get_features_ctx get_feat_ctx;
2868         static int version_printed;
2869         struct net_device *netdev;
2870         struct ena_adapter *adapter;
2871         struct ena_com_dev *ena_dev = NULL;
2872         static int adapters_found;
2873         int io_queue_num, bars, rc;
2874         int queue_size;
2875         u16 tx_sgl_size = 0;
2876         u16 rx_sgl_size = 0;
2877         bool wd_state;
2878
2879         dev_dbg(&pdev->dev, "%s\n", __func__);
2880
2881         if (version_printed++ == 0)
2882                 dev_info(&pdev->dev, "%s", version);
2883
2884         rc = pci_enable_device_mem(pdev);
2885         if (rc) {
2886                 dev_err(&pdev->dev, "pci_enable_device_mem() failed!\n");
2887                 return rc;
2888         }
2889
2890         rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(ENA_MAX_PHYS_ADDR_SIZE_BITS));
2891         if (rc) {
2892                 dev_err(&pdev->dev, "dma_set_mask_and_coherent failed %d\n", rc);
2893                 goto err_disable_device;
2894         }
2895
2896         pci_set_master(pdev);
2897
2898         ena_dev = vzalloc(sizeof(*ena_dev));
2899         if (!ena_dev) {
2900                 rc = -ENOMEM;
2901                 goto err_disable_device;
2902         }
2903
2904         bars = pci_select_bars(pdev, IORESOURCE_MEM) & ENA_BAR_MASK;
2905         rc = pci_request_selected_regions(pdev, bars, DRV_MODULE_NAME);
2906         if (rc) {
2907                 dev_err(&pdev->dev, "pci_request_selected_regions failed %d\n",
2908                         rc);
2909                 goto err_free_ena_dev;
2910         }
2911
2912         ena_dev->reg_bar = devm_ioremap(&pdev->dev,
2913                                         pci_resource_start(pdev, ENA_REG_BAR),
2914                                         pci_resource_len(pdev, ENA_REG_BAR));
2915         if (!ena_dev->reg_bar) {
2916                 dev_err(&pdev->dev, "failed to remap regs bar\n");
2917                 rc = -EFAULT;
2918                 goto err_free_region;
2919         }
2920
2921         ena_dev->dmadev = &pdev->dev;
2922
2923         rc = ena_device_init(ena_dev, pdev, &get_feat_ctx, &wd_state);
2924         if (rc) {
2925                 dev_err(&pdev->dev, "ena device init failed\n");
2926                 if (rc == -ETIME)
2927                         rc = -EPROBE_DEFER;
2928                 goto err_free_region;
2929         }
2930
2931         ena_set_push_mode(pdev, ena_dev, &get_feat_ctx);
2932
2933         if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV) {
2934                 ena_dev->mem_bar = devm_ioremap_wc(&pdev->dev,
2935                                                    pci_resource_start(pdev, ENA_MEM_BAR),
2936                                                    pci_resource_len(pdev, ENA_MEM_BAR));
2937                 if (!ena_dev->mem_bar) {
2938                         rc = -EFAULT;
2939                         goto err_device_destroy;
2940                 }
2941         }
2942
2943         /* initial Tx interrupt delay, Assumes 1 usec granularity.
2944         * Updated during device initialization with the real granularity
2945         */
2946         ena_dev->intr_moder_tx_interval = ENA_INTR_INITIAL_TX_INTERVAL_USECS;
2947         io_queue_num = ena_calc_io_queue_num(pdev, ena_dev, &get_feat_ctx);
2948         queue_size = ena_calc_queue_size(pdev, ena_dev, &tx_sgl_size,
2949                                          &rx_sgl_size, &get_feat_ctx);
2950         if ((queue_size <= 0) || (io_queue_num <= 0)) {
2951                 rc = -EFAULT;
2952                 goto err_device_destroy;
2953         }
2954
2955         dev_info(&pdev->dev, "creating %d io queues. queue size: %d\n",
2956                  io_queue_num, queue_size);
2957
2958         /* dev zeroed in init_etherdev */
2959         netdev = alloc_etherdev_mq(sizeof(struct ena_adapter), io_queue_num);
2960         if (!netdev) {
2961                 dev_err(&pdev->dev, "alloc_etherdev_mq failed\n");
2962                 rc = -ENOMEM;
2963                 goto err_device_destroy;
2964         }
2965
2966         SET_NETDEV_DEV(netdev, &pdev->dev);
2967
2968         adapter = netdev_priv(netdev);
2969         pci_set_drvdata(pdev, adapter);
2970
2971         adapter->ena_dev = ena_dev;
2972         adapter->netdev = netdev;
2973         adapter->pdev = pdev;
2974
2975         ena_set_conf_feat_params(adapter, &get_feat_ctx);
2976
2977         adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
2978
2979         adapter->tx_ring_size = queue_size;
2980         adapter->rx_ring_size = queue_size;
2981
2982         adapter->max_tx_sgl_size = tx_sgl_size;
2983         adapter->max_rx_sgl_size = rx_sgl_size;
2984
2985         adapter->num_queues = io_queue_num;
2986         adapter->last_monitored_tx_qid = 0;
2987
2988         adapter->rx_copybreak = ENA_DEFAULT_RX_COPYBREAK;
2989         adapter->wd_state = wd_state;
2990
2991         snprintf(adapter->name, ENA_NAME_MAX_LEN, "ena_%d", adapters_found);
2992
2993         rc = ena_com_init_interrupt_moderation(adapter->ena_dev);
2994         if (rc) {
2995                 dev_err(&pdev->dev,
2996                         "Failed to query interrupt moderation feature\n");
2997                 goto err_netdev_destroy;
2998         }
2999         ena_init_io_rings(adapter);
3000
3001         netdev->netdev_ops = &ena_netdev_ops;
3002         netdev->watchdog_timeo = TX_TIMEOUT;
3003         ena_set_ethtool_ops(netdev);
3004
3005         netdev->priv_flags |= IFF_UNICAST_FLT;
3006
3007         u64_stats_init(&adapter->syncp);
3008
3009         rc = ena_enable_msix_and_set_admin_interrupts(adapter, io_queue_num);
3010         if (rc) {
3011                 dev_err(&pdev->dev,
3012                         "Failed to enable and set the admin interrupts\n");
3013                 goto err_worker_destroy;
3014         }
3015         rc = ena_rss_init_default(adapter);
3016         if (rc && (rc != -EPERM)) {
3017                 dev_err(&pdev->dev, "Cannot init RSS rc: %d\n", rc);
3018                 goto err_free_msix;
3019         }
3020
3021         ena_config_debug_area(adapter);
3022
3023         memcpy(adapter->netdev->perm_addr, adapter->mac_addr, netdev->addr_len);
3024
3025         netif_carrier_off(netdev);
3026
3027         rc = register_netdev(netdev);
3028         if (rc) {
3029                 dev_err(&pdev->dev, "Cannot register net device\n");
3030                 goto err_rss;
3031         }
3032
3033         INIT_WORK(&adapter->suspend_io_task, ena_device_io_suspend);
3034         INIT_WORK(&adapter->resume_io_task, ena_device_io_resume);
3035         INIT_WORK(&adapter->reset_task, ena_fw_reset_device);
3036
3037         adapter->last_keep_alive_jiffies = jiffies;
3038
3039         init_timer(&adapter->timer_service);
3040         adapter->timer_service.expires = round_jiffies(jiffies + HZ);
3041         adapter->timer_service.function = ena_timer_service;
3042         adapter->timer_service.data = (unsigned long)adapter;
3043
3044         add_timer(&adapter->timer_service);
3045
3046         dev_info(&pdev->dev, "%s found at mem %lx, mac addr %pM Queues %d\n",
3047                  DEVICE_NAME, (long)pci_resource_start(pdev, 0),
3048                  netdev->dev_addr, io_queue_num);
3049
3050         set_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags);
3051
3052         adapters_found++;
3053
3054         return 0;
3055
3056 err_rss:
3057         ena_com_delete_debug_area(ena_dev);
3058         ena_com_rss_destroy(ena_dev);
3059 err_free_msix:
3060         ena_com_dev_reset(ena_dev);
3061         ena_free_mgmnt_irq(adapter);
3062         ena_disable_msix(adapter);
3063 err_worker_destroy:
3064         ena_com_destroy_interrupt_moderation(ena_dev);
3065         del_timer(&adapter->timer_service);
3066         cancel_work_sync(&adapter->suspend_io_task);
3067         cancel_work_sync(&adapter->resume_io_task);
3068 err_netdev_destroy:
3069         free_netdev(netdev);
3070 err_device_destroy:
3071         ena_com_delete_host_info(ena_dev);
3072         ena_com_admin_destroy(ena_dev);
3073 err_free_region:
3074         ena_release_bars(ena_dev, pdev);
3075 err_free_ena_dev:
3076         vfree(ena_dev);
3077 err_disable_device:
3078         pci_disable_device(pdev);
3079         return rc;
3080 }
3081
3082 /*****************************************************************************/
3083 static int ena_sriov_configure(struct pci_dev *dev, int numvfs)
3084 {
3085         int rc;
3086
3087         if (numvfs > 0) {
3088                 rc = pci_enable_sriov(dev, numvfs);
3089                 if (rc != 0) {
3090                         dev_err(&dev->dev,
3091                                 "pci_enable_sriov failed to enable: %d vfs with the error: %d\n",
3092                                 numvfs, rc);
3093                         return rc;
3094                 }
3095
3096                 return numvfs;
3097         }
3098
3099         if (numvfs == 0) {
3100                 pci_disable_sriov(dev);
3101                 return 0;
3102         }
3103
3104         return -EINVAL;
3105 }
3106
3107 /*****************************************************************************/
3108 /*****************************************************************************/
3109
3110 /* ena_remove - Device Removal Routine
3111  * @pdev: PCI device information struct
3112  *
3113  * ena_remove is called by the PCI subsystem to alert the driver
3114  * that it should release a PCI device.
3115  */
3116 static void ena_remove(struct pci_dev *pdev)
3117 {
3118         struct ena_adapter *adapter = pci_get_drvdata(pdev);
3119         struct ena_com_dev *ena_dev;
3120         struct net_device *netdev;
3121
3122         if (!adapter)
3123                 /* This device didn't load properly and it's resources
3124                  * already released, nothing to do
3125                  */
3126                 return;
3127
3128         ena_dev = adapter->ena_dev;
3129         netdev = adapter->netdev;
3130
3131 #ifdef CONFIG_RFS_ACCEL
3132         if ((adapter->msix_vecs >= 1) && (netdev->rx_cpu_rmap)) {
3133                 free_irq_cpu_rmap(netdev->rx_cpu_rmap);
3134                 netdev->rx_cpu_rmap = NULL;
3135         }
3136 #endif /* CONFIG_RFS_ACCEL */
3137
3138         unregister_netdev(netdev);
3139         del_timer_sync(&adapter->timer_service);
3140
3141         cancel_work_sync(&adapter->reset_task);
3142
3143         cancel_work_sync(&adapter->suspend_io_task);
3144
3145         cancel_work_sync(&adapter->resume_io_task);
3146
3147         ena_com_dev_reset(ena_dev);
3148
3149         ena_free_mgmnt_irq(adapter);
3150
3151         ena_disable_msix(adapter);
3152
3153         free_netdev(netdev);
3154
3155         ena_com_mmio_reg_read_request_destroy(ena_dev);
3156
3157         ena_com_abort_admin_commands(ena_dev);
3158
3159         ena_com_wait_for_abort_completion(ena_dev);
3160
3161         ena_com_admin_destroy(ena_dev);
3162
3163         ena_com_rss_destroy(ena_dev);
3164
3165         ena_com_delete_debug_area(ena_dev);
3166
3167         ena_com_delete_host_info(ena_dev);
3168
3169         ena_release_bars(ena_dev, pdev);
3170
3171         pci_disable_device(pdev);
3172
3173         ena_com_destroy_interrupt_moderation(ena_dev);
3174
3175         vfree(ena_dev);
3176 }
3177
3178 static struct pci_driver ena_pci_driver = {
3179         .name           = DRV_MODULE_NAME,
3180         .id_table       = ena_pci_tbl,
3181         .probe          = ena_probe,
3182         .remove         = ena_remove,
3183         .sriov_configure = ena_sriov_configure,
3184 };
3185
3186 static int __init ena_init(void)
3187 {
3188         pr_info("%s", version);
3189
3190         ena_wq = create_singlethread_workqueue(DRV_MODULE_NAME);
3191         if (!ena_wq) {
3192                 pr_err("Failed to create workqueue\n");
3193                 return -ENOMEM;
3194         }
3195
3196         return pci_register_driver(&ena_pci_driver);
3197 }
3198
3199 static void __exit ena_cleanup(void)
3200 {
3201         pci_unregister_driver(&ena_pci_driver);
3202
3203         if (ena_wq) {
3204                 destroy_workqueue(ena_wq);
3205                 ena_wq = NULL;
3206         }
3207 }
3208
3209 /******************************************************************************
3210  ******************************** AENQ Handlers *******************************
3211  *****************************************************************************/
3212 /* ena_update_on_link_change:
3213  * Notify the network interface about the change in link status
3214  */
3215 static void ena_update_on_link_change(void *adapter_data,
3216                                       struct ena_admin_aenq_entry *aenq_e)
3217 {
3218         struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
3219         struct ena_admin_aenq_link_change_desc *aenq_desc =
3220                 (struct ena_admin_aenq_link_change_desc *)aenq_e;
3221         int status = aenq_desc->flags &
3222                 ENA_ADMIN_AENQ_LINK_CHANGE_DESC_LINK_STATUS_MASK;
3223
3224         if (status) {
3225                 netdev_dbg(adapter->netdev, "%s\n", __func__);
3226                 set_bit(ENA_FLAG_LINK_UP, &adapter->flags);
3227                 netif_carrier_on(adapter->netdev);
3228         } else {
3229                 clear_bit(ENA_FLAG_LINK_UP, &adapter->flags);
3230                 netif_carrier_off(adapter->netdev);
3231         }
3232 }
3233
3234 static void ena_keep_alive_wd(void *adapter_data,
3235                               struct ena_admin_aenq_entry *aenq_e)
3236 {
3237         struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
3238
3239         adapter->last_keep_alive_jiffies = jiffies;
3240 }
3241
3242 static void ena_notification(void *adapter_data,
3243                              struct ena_admin_aenq_entry *aenq_e)
3244 {
3245         struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
3246
3247         WARN(aenq_e->aenq_common_desc.group != ENA_ADMIN_NOTIFICATION,
3248              "Invalid group(%x) expected %x\n",
3249              aenq_e->aenq_common_desc.group,
3250              ENA_ADMIN_NOTIFICATION);
3251
3252         switch (aenq_e->aenq_common_desc.syndrom) {
3253         case ENA_ADMIN_SUSPEND:
3254                 /* Suspend just the IO queues.
3255                  * We deliberately don't suspend admin so the timer and
3256                  * the keep_alive events should remain.
3257                  */
3258                 queue_work(ena_wq, &adapter->suspend_io_task);
3259                 break;
3260         case ENA_ADMIN_RESUME:
3261                 queue_work(ena_wq, &adapter->resume_io_task);
3262                 break;
3263         default:
3264                 netif_err(adapter, drv, adapter->netdev,
3265                           "Invalid aenq notification link state %d\n",
3266                           aenq_e->aenq_common_desc.syndrom);
3267         }
3268 }
3269
3270 /* This handler will called for unknown event group or unimplemented handlers*/
3271 static void unimplemented_aenq_handler(void *data,
3272                                        struct ena_admin_aenq_entry *aenq_e)
3273 {
3274         struct ena_adapter *adapter = (struct ena_adapter *)data;
3275
3276         netif_err(adapter, drv, adapter->netdev,
3277                   "Unknown event was received or event with unimplemented handler\n");
3278 }
3279
3280 static struct ena_aenq_handlers aenq_handlers = {
3281         .handlers = {
3282                 [ENA_ADMIN_LINK_CHANGE] = ena_update_on_link_change,
3283                 [ENA_ADMIN_NOTIFICATION] = ena_notification,
3284                 [ENA_ADMIN_KEEP_ALIVE] = ena_keep_alive_wd,
3285         },
3286         .unimplemented_handler = unimplemented_aenq_handler
3287 };
3288
3289 module_init(ena_init);
3290 module_exit(ena_cleanup);