GNU Linux-libre 4.9.309-gnu1
[releases.git] / drivers / net / ethernet / amazon / ena / ena_netdev.h
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 #ifndef ENA_H
34 #define ENA_H
35
36 #include <linux/bitops.h>
37 #include <linux/etherdevice.h>
38 #include <linux/inetdevice.h>
39 #include <linux/interrupt.h>
40 #include <linux/netdevice.h>
41 #include <linux/skbuff.h>
42
43 #include "ena_com.h"
44 #include "ena_eth_com.h"
45
46 #define DRV_MODULE_VER_MAJOR    1
47 #define DRV_MODULE_VER_MINOR    0
48 #define DRV_MODULE_VER_SUBMINOR 2
49
50 #define DRV_MODULE_NAME         "ena"
51 #ifndef DRV_MODULE_VERSION
52 #define DRV_MODULE_VERSION \
53         __stringify(DRV_MODULE_VER_MAJOR) "."   \
54         __stringify(DRV_MODULE_VER_MINOR) "."   \
55         __stringify(DRV_MODULE_VER_SUBMINOR)
56 #endif
57
58 #define DEVICE_NAME     "Elastic Network Adapter (ENA)"
59
60 /* 1 for AENQ + ADMIN */
61 #define ENA_MAX_MSIX_VEC(io_queues)     (1 + (io_queues))
62
63 #define ENA_REG_BAR                     0
64 #define ENA_MEM_BAR                     2
65 #define ENA_BAR_MASK (BIT(ENA_REG_BAR) | BIT(ENA_MEM_BAR))
66
67 #define ENA_DEFAULT_RING_SIZE   (1024)
68
69 #define ENA_TX_WAKEUP_THRESH            (MAX_SKB_FRAGS + 2)
70 #define ENA_DEFAULT_RX_COPYBREAK        (128 - NET_IP_ALIGN)
71
72 /* limit the buffer size to 600 bytes to handle MTU changes from very
73  * small to very large, in which case the number of buffers per packet
74  * could exceed ENA_PKT_MAX_BUFS
75  */
76 #define ENA_DEFAULT_MIN_RX_BUFF_ALLOC_SIZE 600
77
78 #define ENA_MIN_MTU             128
79
80 #define ENA_NAME_MAX_LEN        20
81 #define ENA_IRQNAME_SIZE        40
82
83 #define ENA_PKT_MAX_BUFS        19
84
85 #define ENA_RX_RSS_TABLE_LOG_SIZE  7
86 #define ENA_RX_RSS_TABLE_SIZE   (1 << ENA_RX_RSS_TABLE_LOG_SIZE)
87
88 #define ENA_HASH_KEY_SIZE       40
89
90 /* The number of tx packet completions that will be handled each NAPI poll
91  * cycle is ring_size / ENA_TX_POLL_BUDGET_DIVIDER.
92  */
93 #define ENA_TX_POLL_BUDGET_DIVIDER      4
94
95 /* Refill Rx queue when number of available descriptors is below
96  * QUEUE_SIZE / ENA_RX_REFILL_THRESH_DIVIDER
97  */
98 #define ENA_RX_REFILL_THRESH_DIVIDER    8
99
100 /* Number of queues to check for missing queues per timer service */
101 #define ENA_MONITORED_TX_QUEUES 4
102 /* Max timeout packets before device reset */
103 #define MAX_NUM_OF_TIMEOUTED_PACKETS 128
104
105 #define ENA_TX_RING_IDX_NEXT(idx, ring_size) (((idx) + 1) & ((ring_size) - 1))
106
107 #define ENA_RX_RING_IDX_NEXT(idx, ring_size) (((idx) + 1) & ((ring_size) - 1))
108 #define ENA_RX_RING_IDX_ADD(idx, n, ring_size) \
109         (((idx) + (n)) & ((ring_size) - 1))
110
111 #define ENA_IO_TXQ_IDX(q)       (2 * (q))
112 #define ENA_IO_RXQ_IDX(q)       (2 * (q) + 1)
113 #define ENA_IO_TXQ_IDX_TO_COMBINED_IDX(q)       ((q) / 2)
114 #define ENA_IO_RXQ_IDX_TO_COMBINED_IDX(q)       (((q) - 1) / 2)
115
116 #define ENA_MGMNT_IRQ_IDX               0
117 #define ENA_IO_IRQ_FIRST_IDX            1
118 #define ENA_IO_IRQ_IDX(q)               (ENA_IO_IRQ_FIRST_IDX + (q))
119
120 /* ENA device should send keep alive msg every 1 sec.
121  * We wait for 6 sec just to be on the safe side.
122  */
123 #define ENA_DEVICE_KALIVE_TIMEOUT       (6 * HZ)
124
125 #define ENA_MMIO_DISABLE_REG_READ       BIT(0)
126
127 struct ena_irq {
128         irq_handler_t handler;
129         void *data;
130         int cpu;
131         u32 vector;
132         cpumask_t affinity_hint_mask;
133         char name[ENA_IRQNAME_SIZE];
134 };
135
136 struct ena_napi {
137         struct napi_struct napi ____cacheline_aligned;
138         struct ena_ring *tx_ring;
139         struct ena_ring *rx_ring;
140         u32 qid;
141 };
142
143 struct ena_tx_buffer {
144         struct sk_buff *skb;
145         /* num of ena desc for this specific skb
146          * (includes data desc and metadata desc)
147          */
148         u32 tx_descs;
149         /* num of buffers used by this skb */
150         u32 num_of_bufs;
151         /* Save the last jiffies to detect missing tx packets */
152         unsigned long last_jiffies;
153         struct ena_com_buf bufs[ENA_PKT_MAX_BUFS];
154 } ____cacheline_aligned;
155
156 struct ena_rx_buffer {
157         struct sk_buff *skb;
158         struct page *page;
159         u32 page_offset;
160         struct ena_com_buf ena_buf;
161 } ____cacheline_aligned;
162
163 struct ena_stats_tx {
164         u64 cnt;
165         u64 bytes;
166         u64 queue_stop;
167         u64 prepare_ctx_err;
168         u64 queue_wakeup;
169         u64 dma_mapping_err;
170         u64 linearize;
171         u64 linearize_failed;
172         u64 napi_comp;
173         u64 tx_poll;
174         u64 doorbells;
175         u64 missing_tx_comp;
176         u64 bad_req_id;
177 };
178
179 struct ena_stats_rx {
180         u64 cnt;
181         u64 bytes;
182         u64 refil_partial;
183         u64 bad_csum;
184         u64 page_alloc_fail;
185         u64 skb_alloc_fail;
186         u64 dma_mapping_err;
187         u64 bad_desc_num;
188         u64 rx_copybreak_pkt;
189 };
190
191 struct ena_ring {
192         /* Holds the empty requests for TX out of order completions */
193         u16 *free_tx_ids;
194         union {
195                 struct ena_tx_buffer *tx_buffer_info;
196                 struct ena_rx_buffer *rx_buffer_info;
197         };
198
199         /* cache ptr to avoid using the adapter */
200         struct device *dev;
201         struct pci_dev *pdev;
202         struct napi_struct *napi;
203         struct net_device *netdev;
204         struct ena_com_dev *ena_dev;
205         struct ena_adapter *adapter;
206         struct ena_com_io_cq *ena_com_io_cq;
207         struct ena_com_io_sq *ena_com_io_sq;
208
209         u16 next_to_use;
210         u16 next_to_clean;
211         u16 rx_copybreak;
212         u16 qid;
213         u16 mtu;
214         u16 sgl_size;
215
216         /* The maximum header length the device can handle */
217         u8 tx_max_header_size;
218
219         /* cpu for TPH */
220         int cpu;
221          /* number of tx/rx_buffer_info's entries */
222         int ring_size;
223
224         enum ena_admin_placement_policy_type tx_mem_queue_type;
225
226         struct ena_com_rx_buf_info ena_bufs[ENA_PKT_MAX_BUFS];
227         u32  smoothed_interval;
228         u32  per_napi_packets;
229         u32  per_napi_bytes;
230         enum ena_intr_moder_level moder_tbl_idx;
231         struct u64_stats_sync syncp;
232         union {
233                 struct ena_stats_tx tx_stats;
234                 struct ena_stats_rx rx_stats;
235         };
236 } ____cacheline_aligned;
237
238 struct ena_stats_dev {
239         u64 tx_timeout;
240         u64 io_suspend;
241         u64 io_resume;
242         u64 wd_expired;
243         u64 interface_up;
244         u64 interface_down;
245         u64 admin_q_pause;
246 };
247
248 enum ena_flags_t {
249         ENA_FLAG_DEVICE_RUNNING,
250         ENA_FLAG_DEV_UP,
251         ENA_FLAG_LINK_UP,
252         ENA_FLAG_MSIX_ENABLED,
253         ENA_FLAG_TRIGGER_RESET
254 };
255
256 /* adapter specific private data structure */
257 struct ena_adapter {
258         struct ena_com_dev *ena_dev;
259         /* OS defined structs */
260         struct net_device *netdev;
261         struct pci_dev *pdev;
262
263         /* rx packets that shorter that this len will be copied to the skb
264          * header
265          */
266         u32 rx_copybreak;
267         u32 max_mtu;
268
269         int num_queues;
270
271         struct msix_entry *msix_entries;
272         int msix_vecs;
273
274         u32 tx_usecs, rx_usecs; /* interrupt moderation */
275         u32 tx_frames, rx_frames; /* interrupt moderation */
276
277         u32 tx_ring_size;
278         u32 rx_ring_size;
279
280         u32 msg_enable;
281
282         u16 max_tx_sgl_size;
283         u16 max_rx_sgl_size;
284
285         u8 mac_addr[ETH_ALEN];
286
287         char name[ENA_NAME_MAX_LEN];
288
289         unsigned long flags;
290         /* TX */
291         struct ena_ring tx_ring[ENA_MAX_NUM_IO_QUEUES]
292                 ____cacheline_aligned_in_smp;
293
294         /* RX */
295         struct ena_ring rx_ring[ENA_MAX_NUM_IO_QUEUES]
296                 ____cacheline_aligned_in_smp;
297
298         struct ena_napi ena_napi[ENA_MAX_NUM_IO_QUEUES];
299
300         struct ena_irq irq_tbl[ENA_MAX_MSIX_VEC(ENA_MAX_NUM_IO_QUEUES)];
301
302         /* timer service */
303         struct work_struct reset_task;
304         struct work_struct suspend_io_task;
305         struct work_struct resume_io_task;
306         struct timer_list timer_service;
307
308         bool wd_state;
309         unsigned long last_keep_alive_jiffies;
310
311         struct u64_stats_sync syncp;
312         struct ena_stats_dev dev_stats;
313
314         /* last queue index that was checked for uncompleted tx packets */
315         u32 last_monitored_tx_qid;
316 };
317
318 void ena_set_ethtool_ops(struct net_device *netdev);
319
320 void ena_dump_stats_to_dmesg(struct ena_adapter *adapter);
321
322 void ena_dump_stats_to_buf(struct ena_adapter *adapter, u8 *buf);
323
324 int ena_get_sset_count(struct net_device *netdev, int sset);
325
326 /* The ENA buffer length fields is 16 bit long. So when PAGE_SIZE == 64kB the
327  * driver passas 0.
328  * Since the max packet size the ENA handles is ~9kB limit the buffer length to
329  * 16kB.
330  */
331 #if PAGE_SIZE > SZ_16K
332 #define ENA_PAGE_SIZE SZ_16K
333 #else
334 #define ENA_PAGE_SIZE PAGE_SIZE
335 #endif
336
337 #endif /* !(ENA_H) */