fm10k: fix Rx buffer size
[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 struct fm10k_dev_info {
113         volatile uint32_t enable;
114         volatile uint32_t glort;
115         /* Protect the mailbox to avoid race condition */
116         rte_spinlock_t    mbx_lock;
117 };
118
119 /*
120  * Structure to store private data for each driver instance.
121  */
122 struct fm10k_adapter {
123         struct fm10k_hw             hw;
124         struct fm10k_hw_stats       stats;
125         struct fm10k_dev_info       info;
126 };
127
128 #define FM10K_DEV_PRIVATE_TO_HW(adapter) \
129         (&((struct fm10k_adapter *)adapter)->hw)
130
131 #define FM10K_DEV_PRIVATE_TO_STATS(adapter) \
132         (&((struct fm10k_adapter *)adapter)->stats)
133
134 #define FM10K_DEV_PRIVATE_TO_INFO(adapter) \
135         (&((struct fm10k_adapter *)adapter)->info)
136
137 #define FM10K_DEV_PRIVATE_TO_MBXLOCK(adapter) \
138         (&(((struct fm10k_adapter *)adapter)->info.mbx_lock))
139
140 struct fm10k_rx_queue {
141         struct rte_mempool *mp;
142         struct rte_mbuf **sw_ring;
143         volatile union fm10k_rx_desc *hw_ring;
144         struct rte_mbuf *pkt_first_seg; /**< First segment of current packet. */
145         struct rte_mbuf *pkt_last_seg;  /**< Last segment of current packet. */
146         uint64_t hw_ring_phys_addr;
147         uint16_t next_dd;
148         uint16_t next_alloc;
149         uint16_t next_trigger;
150         uint16_t alloc_thresh;
151         volatile uint32_t *tail_ptr;
152         uint16_t nb_desc;
153         uint16_t queue_id;
154         uint8_t port_id;
155         uint8_t drop_en;
156         uint8_t rx_deferred_start; /**< don't start this queue in dev start. */
157 };
158
159 /*
160  * a FIFO is used to track which descriptors have their RS bit set for Tx
161  * queues which are configured to allow multiple descriptors per packet
162  */
163 struct fifo {
164         uint16_t *list;
165         uint16_t *head;
166         uint16_t *tail;
167         uint16_t *endp;
168 };
169
170 struct fm10k_tx_queue {
171         struct rte_mbuf **sw_ring;
172         struct fm10k_tx_desc *hw_ring;
173         uint64_t hw_ring_phys_addr;
174         struct fifo rs_tracker;
175         uint16_t last_free;
176         uint16_t next_free;
177         uint16_t nb_free;
178         uint16_t nb_used;
179         uint16_t free_trigger;
180         uint16_t free_thresh;
181         uint16_t rs_thresh;
182         volatile uint32_t *tail_ptr;
183         uint16_t nb_desc;
184         uint8_t port_id;
185         uint8_t tx_deferred_start; /** < don't start this queue in dev start. */
186         uint16_t queue_id;
187 };
188
189 #define MBUF_DMA_ADDR(mb) \
190         ((uint64_t) ((mb)->buf_physaddr + (mb)->data_off))
191
192 /* enforce 512B alignment on default Rx DMA addresses */
193 #define MBUF_DMA_ADDR_DEFAULT(mb) \
194         ((uint64_t) RTE_ALIGN(((mb)->buf_physaddr + RTE_PKTMBUF_HEADROOM),\
195                         FM10K_RX_DATABUF_ALIGN))
196
197 static inline void fifo_reset(struct fifo *fifo, uint32_t len)
198 {
199         fifo->head = fifo->tail = fifo->list;
200         fifo->endp = fifo->list + len;
201 }
202
203 static inline void fifo_insert(struct fifo *fifo, uint16_t val)
204 {
205         *fifo->head = val;
206         if (++fifo->head == fifo->endp)
207                 fifo->head = fifo->list;
208 }
209
210 /* do not worry about list being empty since we only check it once we know
211  * we have used enough descriptors to set the RS bit at least once */
212 static inline uint16_t fifo_peek(struct fifo *fifo)
213 {
214         return *fifo->tail;
215 }
216
217 static inline uint16_t fifo_remove(struct fifo *fifo)
218 {
219         uint16_t val;
220         val = *fifo->tail;
221         if (++fifo->tail == fifo->endp)
222                 fifo->tail = fifo->list;
223         return val;
224 }
225
226 static inline void
227 fm10k_pktmbuf_reset(struct rte_mbuf *mb, uint8_t in_port)
228 {
229         rte_mbuf_refcnt_set(mb, 1);
230         mb->next = NULL;
231         mb->nb_segs = 1;
232
233         /* enforce 512B alignment on default Rx virtual addresses */
234         mb->data_off = (uint16_t)(RTE_PTR_ALIGN((char *)mb->buf_addr +
235                         RTE_PKTMBUF_HEADROOM, FM10K_RX_DATABUF_ALIGN)
236                         - (char *)mb->buf_addr);
237         mb->port = in_port;
238 }
239
240 /*
241  * Verify Rx packet buffer alignment is valid.
242  *
243  * Hardware requires specific alignment for Rx packet buffers. At
244  * least one of the following two conditions must be satisfied.
245  *  1. Address is 512B aligned
246  *  2. Address is 8B aligned and buffer does not cross 4K boundary.
247  *
248  * Return 1 if buffer alignment satisfies at least one condition,
249  * otherwise return 0.
250  *
251  * Note: Alignment is checked by the driver when the Rx queue is reset. It
252  *       is assumed that if an entire descriptor ring can be filled with
253  *       buffers containing valid alignment, then all buffers in that mempool
254  *       have valid address alignment. It is the responsibility of the user
255  *       to ensure all buffers have valid alignment, as it is the user who
256  *       creates the mempool.
257  * Note: It is assumed the buffer needs only to store a maximum size Ethernet
258  *       frame.
259  */
260 static inline int
261 fm10k_addr_alignment_valid(struct rte_mbuf *mb)
262 {
263         uint64_t addr = MBUF_DMA_ADDR_DEFAULT(mb);
264         uint64_t boundary1, boundary2;
265
266         /* 512B aligned? */
267         if (RTE_ALIGN(addr, FM10K_RX_DATABUF_ALIGN) == addr)
268                 return 1;
269
270         /* 8B aligned, and max Ethernet frame would not cross a 4KB boundary? */
271         if (RTE_ALIGN(addr, 8) == addr) {
272                 boundary1 = RTE_ALIGN_FLOOR(addr, 4096);
273                 boundary2 = RTE_ALIGN_FLOOR(addr + ETHER_MAX_VLAN_FRAME_LEN,
274                                                 4096);
275                 if (boundary1 == boundary2)
276                         return 1;
277         }
278
279         PMD_INIT_LOG(ERR, "Error: Invalid buffer alignment!");
280
281         return 0;
282 }
283
284 /* Rx and Tx prototypes */
285 uint16_t fm10k_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
286         uint16_t nb_pkts);
287
288 uint16_t fm10k_recv_scattered_pkts(void *rx_queue,
289                 struct rte_mbuf **rx_pkts, uint16_t nb_pkts);
290
291 uint16_t fm10k_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
292         uint16_t nb_pkts);
293 #endif