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