fm10k: allow to disable vector driver
[dpdk.git] / drivers / net / fm10k / fm10k.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef _FM10K_H_
35 #define _FM10K_H_
36
37 #include <stdint.h>
38 #include <rte_mbuf.h>
39 #include <rte_mempool.h>
40 #include <rte_malloc.h>
41 #include <rte_spinlock.h>
42 #include "fm10k_logs.h"
43 #include "base/fm10k_type.h"
44
45 /* descriptor ring base addresses must be aligned to the following */
46 #define FM10K_ALIGN_RX_DESC  128
47 #define FM10K_ALIGN_TX_DESC  128
48
49 /* The maximum packet size that FM10K supports */
50 #define FM10K_MAX_PKT_SIZE  (15 * 1024)
51
52 /* Minimum size of RX buffer FM10K supported */
53 #define FM10K_MIN_RX_BUF_SIZE  256
54
55 /* The maximum of SRIOV VFs per port supported */
56 #define FM10K_MAX_VF_NUM    64
57
58 /* number of descriptors must be a multiple of the following */
59 #define FM10K_MULT_RX_DESC  FM10K_REQ_RX_DESCRIPTOR_MULTIPLE
60 #define FM10K_MULT_TX_DESC  FM10K_REQ_TX_DESCRIPTOR_MULTIPLE
61
62 /* maximum size of descriptor rings */
63 #define FM10K_MAX_RX_RING_SZ  (512 * 1024)
64 #define FM10K_MAX_TX_RING_SZ  (512 * 1024)
65
66 /* minimum and maximum number of descriptors in a ring */
67 #define FM10K_MIN_RX_DESC  32
68 #define FM10K_MIN_TX_DESC  32
69 #define FM10K_MAX_RX_DESC  (FM10K_MAX_RX_RING_SZ / sizeof(union fm10k_rx_desc))
70 #define FM10K_MAX_TX_DESC  (FM10K_MAX_TX_RING_SZ / sizeof(struct fm10k_tx_desc))
71
72 /*
73  * byte aligment for HW RX data buffer
74  * Datasheet requires RX buffer addresses shall either be 512-byte aligned or
75  * be 8-byte aligned but without crossing host memory pages (4KB alignment
76  * boundaries). Satisfy first option.
77  */
78 #define FM10K_RX_DATABUF_ALIGN 512
79
80 /*
81  * threshold default, min, max, and divisor constraints
82  * the configured values must satisfy the following:
83  *   MIN <= value <= MAX
84  *   DIV % value == 0
85  */
86 #define FM10K_RX_FREE_THRESH_DEFAULT(rxq)  32
87 #define FM10K_RX_FREE_THRESH_MIN(rxq)      1
88 #define FM10K_RX_FREE_THRESH_MAX(rxq)      ((rxq)->nb_desc - 1)
89 #define FM10K_RX_FREE_THRESH_DIV(rxq)      ((rxq)->nb_desc)
90
91 #define FM10K_TX_FREE_THRESH_DEFAULT(txq)  32
92 #define FM10K_TX_FREE_THRESH_MIN(txq)      1
93 #define FM10K_TX_FREE_THRESH_MAX(txq)      ((txq)->nb_desc - 3)
94 #define FM10K_TX_FREE_THRESH_DIV(txq)      0
95
96 #define FM10K_DEFAULT_RX_PTHRESH      8
97 #define FM10K_DEFAULT_RX_HTHRESH      8
98 #define FM10K_DEFAULT_RX_WTHRESH      0
99
100 #define FM10K_DEFAULT_TX_PTHRESH      32
101 #define FM10K_DEFAULT_TX_HTHRESH      0
102 #define FM10K_DEFAULT_TX_WTHRESH      0
103
104 #define FM10K_TX_RS_THRESH_DEFAULT(txq)    32
105 #define FM10K_TX_RS_THRESH_MIN(txq)        1
106 #define FM10K_TX_RS_THRESH_MAX(txq)        \
107         RTE_MIN(((txq)->nb_desc - 2), (txq)->free_thresh)
108 #define FM10K_TX_RS_THRESH_DIV(txq)        ((txq)->nb_desc)
109
110 #define FM10K_VLAN_TAG_SIZE 4
111
112 /* Maximum number of MAC addresses per PF/VF */
113 #define FM10K_MAX_MACADDR_NUM       64
114
115 #define FM10K_UINT32_BIT_SIZE      (CHAR_BIT * sizeof(uint32_t))
116 #define FM10K_VFTA_SIZE            (4096 / FM10K_UINT32_BIT_SIZE)
117
118 /* vlan_id is a 12 bit number.
119  * The VFTA array is actually a 4096 bit array, 128 of 32bit elements.
120  * 2^5 = 32. The val of lower 5 bits specifies the bit in the 32bit element.
121  * The higher 7 bit val specifies VFTA array index.
122  */
123 #define FM10K_VFTA_BIT(vlan_id)    (1 << ((vlan_id) & 0x1F))
124 #define FM10K_VFTA_IDX(vlan_id)    ((vlan_id) >> 5)
125
126 #define RTE_FM10K_RXQ_REARM_THRESH      32
127 #define RTE_FM10K_VPMD_TX_BURST         32
128 #define RTE_FM10K_MAX_RX_BURST          RTE_FM10K_RXQ_REARM_THRESH
129 #define RTE_FM10K_TX_MAX_FREE_BUF_SZ    64
130 #define RTE_FM10K_DESCS_PER_LOOP    4
131
132 #define FM10K_SIMPLE_TX_FLAG ((uint32_t)ETH_TXQ_FLAGS_NOMULTSEGS | \
133                                 ETH_TXQ_FLAGS_NOOFFLOADS)
134
135 struct fm10k_macvlan_filter_info {
136         uint16_t vlan_num;       /* Total VLAN number */
137         uint16_t mac_num;        /* Total mac number */
138         uint16_t nb_queue_pools; /* Active queue pools number */
139         /* VMDQ ID for each MAC address */
140         uint8_t  mac_vmdq_id[FM10K_MAX_MACADDR_NUM];
141         uint32_t vfta[FM10K_VFTA_SIZE];        /* VLAN bitmap */
142 };
143
144 struct fm10k_dev_info {
145         volatile uint32_t enable;
146         volatile uint32_t glort;
147         /* Protect the mailbox to avoid race condition */
148         rte_spinlock_t    mbx_lock;
149         struct fm10k_macvlan_filter_info    macvlan;
150         /* Flag to indicate if RX vector conditions satisfied */
151         bool rx_vec_allowed;
152 };
153
154 /*
155  * Structure to store private data for each driver instance.
156  */
157 struct fm10k_adapter {
158         struct fm10k_hw             hw;
159         struct fm10k_hw_stats       stats;
160         struct fm10k_dev_info       info;
161 };
162
163 #define FM10K_DEV_PRIVATE_TO_HW(adapter) \
164         (&((struct fm10k_adapter *)adapter)->hw)
165
166 #define FM10K_DEV_PRIVATE_TO_STATS(adapter) \
167         (&((struct fm10k_adapter *)adapter)->stats)
168
169 #define FM10K_DEV_PRIVATE_TO_INFO(adapter) \
170         (&((struct fm10k_adapter *)adapter)->info)
171
172 #define FM10K_DEV_PRIVATE_TO_MBXLOCK(adapter) \
173         (&(((struct fm10k_adapter *)adapter)->info.mbx_lock))
174
175 #define FM10K_DEV_PRIVATE_TO_MACVLAN(adapter) \
176                 (&(((struct fm10k_adapter *)adapter)->info.macvlan))
177
178 struct fm10k_rx_queue {
179         struct rte_mempool *mp;
180         struct rte_mbuf **sw_ring;
181         volatile union fm10k_rx_desc *hw_ring;
182         struct rte_mbuf *pkt_first_seg; /* First segment of current packet. */
183         struct rte_mbuf *pkt_last_seg;  /* Last segment of current packet. */
184         uint64_t hw_ring_phys_addr;
185         uint64_t mbuf_initializer; /* value to init mbufs */
186         /* need to alloc dummy mbuf, for wraparound when scanning hw ring */
187         struct rte_mbuf fake_mbuf;
188         uint16_t next_dd;
189         uint16_t next_alloc;
190         uint16_t next_trigger;
191         uint16_t alloc_thresh;
192         volatile uint32_t *tail_ptr;
193         uint16_t nb_desc;
194         /* Number of faked desc added at the tail for Vector RX function */
195         uint16_t nb_fake_desc;
196         uint16_t queue_id;
197         /* Below 2 fields only valid in case vPMD is applied. */
198         uint16_t rxrearm_nb;     /* number of remaining to be re-armed */
199         uint16_t rxrearm_start;  /* the idx we start the re-arming from */
200         uint16_t rx_using_sse; /* indicates that vector RX is in use */
201         uint8_t port_id;
202         uint8_t drop_en;
203         uint8_t rx_deferred_start; /* don't start this queue in dev start. */
204 };
205
206 /*
207  * a FIFO is used to track which descriptors have their RS bit set for Tx
208  * queues which are configured to allow multiple descriptors per packet
209  */
210 struct fifo {
211         uint16_t *list;
212         uint16_t *head;
213         uint16_t *tail;
214         uint16_t *endp;
215 };
216
217 struct fm10k_txq_ops;
218
219 struct fm10k_tx_queue {
220         struct rte_mbuf **sw_ring;
221         struct fm10k_tx_desc *hw_ring;
222         uint64_t hw_ring_phys_addr;
223         struct fifo rs_tracker;
224         const struct fm10k_txq_ops *ops; /* txq ops */
225         uint16_t last_free;
226         uint16_t next_free;
227         uint16_t nb_free;
228         uint16_t nb_used;
229         uint16_t free_thresh;
230         uint16_t rs_thresh;
231         /* Below 2 fields only valid in case vPMD is applied. */
232         uint16_t next_rs; /* Next pos to set RS flag */
233         uint16_t next_dd; /* Next pos to check DD flag */
234         volatile uint32_t *tail_ptr;
235         uint32_t txq_flags; /* Holds flags for this TXq */
236         uint16_t nb_desc;
237         uint8_t port_id;
238         uint8_t tx_deferred_start; /** don't start this queue in dev start. */
239         uint16_t queue_id;
240 };
241
242 struct fm10k_txq_ops {
243         void (*reset)(struct fm10k_tx_queue *txq);
244 };
245
246 #define MBUF_DMA_ADDR(mb) \
247         ((uint64_t) ((mb)->buf_physaddr + (mb)->data_off))
248
249 /* enforce 512B alignment on default Rx DMA addresses */
250 #define MBUF_DMA_ADDR_DEFAULT(mb) \
251         ((uint64_t) RTE_ALIGN(((mb)->buf_physaddr + RTE_PKTMBUF_HEADROOM),\
252                         FM10K_RX_DATABUF_ALIGN))
253
254 static inline void fifo_reset(struct fifo *fifo, uint32_t len)
255 {
256         fifo->head = fifo->tail = fifo->list;
257         fifo->endp = fifo->list + len;
258 }
259
260 static inline void fifo_insert(struct fifo *fifo, uint16_t val)
261 {
262         *fifo->head = val;
263         if (++fifo->head == fifo->endp)
264                 fifo->head = fifo->list;
265 }
266
267 /* do not worry about list being empty since we only check it once we know
268  * we have used enough descriptors to set the RS bit at least once */
269 static inline uint16_t fifo_peek(struct fifo *fifo)
270 {
271         return *fifo->tail;
272 }
273
274 static inline uint16_t fifo_remove(struct fifo *fifo)
275 {
276         uint16_t val;
277         val = *fifo->tail;
278         if (++fifo->tail == fifo->endp)
279                 fifo->tail = fifo->list;
280         return val;
281 }
282
283 static inline void
284 fm10k_pktmbuf_reset(struct rte_mbuf *mb, uint8_t in_port)
285 {
286         rte_mbuf_refcnt_set(mb, 1);
287         mb->next = NULL;
288         mb->nb_segs = 1;
289
290         /* enforce 512B alignment on default Rx virtual addresses */
291         mb->data_off = (uint16_t)(RTE_PTR_ALIGN((char *)mb->buf_addr +
292                         RTE_PKTMBUF_HEADROOM, FM10K_RX_DATABUF_ALIGN)
293                         - (char *)mb->buf_addr);
294         mb->port = in_port;
295 }
296
297 /*
298  * Verify Rx packet buffer alignment is valid.
299  *
300  * Hardware requires specific alignment for Rx packet buffers. At
301  * least one of the following two conditions must be satisfied.
302  *  1. Address is 512B aligned
303  *  2. Address is 8B aligned and buffer does not cross 4K boundary.
304  *
305  * Return 1 if buffer alignment satisfies at least one condition,
306  * otherwise return 0.
307  *
308  * Note: Alignment is checked by the driver when the Rx queue is reset. It
309  *       is assumed that if an entire descriptor ring can be filled with
310  *       buffers containing valid alignment, then all buffers in that mempool
311  *       have valid address alignment. It is the responsibility of the user
312  *       to ensure all buffers have valid alignment, as it is the user who
313  *       creates the mempool.
314  * Note: It is assumed the buffer needs only to store a maximum size Ethernet
315  *       frame.
316  */
317 static inline int
318 fm10k_addr_alignment_valid(struct rte_mbuf *mb)
319 {
320         uint64_t addr = MBUF_DMA_ADDR_DEFAULT(mb);
321         uint64_t boundary1, boundary2;
322
323         /* 512B aligned? */
324         if (RTE_ALIGN(addr, FM10K_RX_DATABUF_ALIGN) == addr)
325                 return 1;
326
327         /* 8B aligned, and max Ethernet frame would not cross a 4KB boundary? */
328         if (RTE_ALIGN(addr, 8) == addr) {
329                 boundary1 = RTE_ALIGN_FLOOR(addr, 4096);
330                 boundary2 = RTE_ALIGN_FLOOR(addr + ETHER_MAX_VLAN_FRAME_LEN,
331                                                 4096);
332                 if (boundary1 == boundary2)
333                         return 1;
334         }
335
336         PMD_INIT_LOG(ERR, "Error: Invalid buffer alignment!");
337
338         return 0;
339 }
340
341 /* Rx and Tx prototypes */
342 uint16_t fm10k_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
343         uint16_t nb_pkts);
344
345 uint16_t fm10k_recv_scattered_pkts(void *rx_queue,
346                 struct rte_mbuf **rx_pkts, uint16_t nb_pkts);
347
348 uint16_t fm10k_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
349         uint16_t nb_pkts);
350
351 int fm10k_rxq_vec_setup(struct fm10k_rx_queue *rxq);
352 int fm10k_rx_vec_condition_check(struct rte_eth_dev *);
353 void fm10k_rx_queue_release_mbufs_vec(struct fm10k_rx_queue *rxq);
354 uint16_t fm10k_recv_pkts_vec(void *, struct rte_mbuf **, uint16_t);
355 uint16_t fm10k_recv_scattered_pkts_vec(void *, struct rte_mbuf **,
356                                         uint16_t);
357 uint16_t fm10k_xmit_pkts_vec(void *tx_queue, struct rte_mbuf **tx_pkts,
358                 uint16_t nb_pkts);
359 void fm10k_txq_vec_setup(struct fm10k_tx_queue *txq);
360 int fm10k_tx_vec_condition_check(struct fm10k_tx_queue *txq);
361
362 #endif