net/mlx4: remove scatter mode compilation option
[dpdk.git] / drivers / net / mlx4 / mlx4.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2012 6WIND S.A.
5  *   Copyright 2012 Mellanox
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 6WIND S.A. 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 RTE_PMD_MLX4_H_
35 #define RTE_PMD_MLX4_H_
36
37 #include <stdint.h>
38 #include <limits.h>
39
40 /*
41  * Runtime logging through RTE_LOG() is enabled when not in debugging mode.
42  * Intermediate LOG_*() macros add the required end-of-line characters.
43  */
44 #ifndef NDEBUG
45 #define INFO(...) DEBUG(__VA_ARGS__)
46 #define WARN(...) DEBUG(__VA_ARGS__)
47 #define ERROR(...) DEBUG(__VA_ARGS__)
48 #else
49 #define LOG__(level, m, ...) \
50         RTE_LOG(level, PMD, MLX4_DRIVER_NAME ": " m "%c", __VA_ARGS__)
51 #define LOG_(level, ...) LOG__(level, __VA_ARGS__, '\n')
52 #define INFO(...) LOG_(INFO, __VA_ARGS__)
53 #define WARN(...) LOG_(WARNING, __VA_ARGS__)
54 #define ERROR(...) LOG_(ERR, __VA_ARGS__)
55 #endif
56
57 /* Verbs header. */
58 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
59 #ifdef PEDANTIC
60 #pragma GCC diagnostic ignored "-Wpedantic"
61 #endif
62 #include <infiniband/verbs.h>
63 #ifdef PEDANTIC
64 #pragma GCC diagnostic error "-Wpedantic"
65 #endif
66
67 /*
68  * Maximum number of simultaneous MAC addresses supported.
69  *
70  * According to ConnectX's Programmer Reference Manual:
71  *   The L2 Address Match is implemented by comparing a MAC/VLAN combination
72  *   of 128 MAC addresses and 127 VLAN values, comprising 128x127 possible
73  *   L2 addresses.
74  */
75 #define MLX4_MAX_MAC_ADDRESSES 128
76
77 /* Maximum number of simultaneous VLAN filters supported. See above. */
78 #define MLX4_MAX_VLAN_IDS 127
79
80 /* Request send completion once in every 64 sends, might be less. */
81 #define MLX4_PMD_TX_PER_COMP_REQ 64
82
83 /* Maximum number of Scatter/Gather Elements per Work Request. */
84 #define MLX4_PMD_SGE_WR_N 4
85
86 /* Maximum size for inline data. */
87 #ifndef MLX4_PMD_MAX_INLINE
88 #define MLX4_PMD_MAX_INLINE 0
89 #endif
90
91 /*
92  * Maximum number of cached Memory Pools (MPs) per TX queue. Each RTE MP
93  * from which buffers are to be transmitted will have to be mapped by this
94  * driver to their own Memory Region (MR). This is a slow operation.
95  *
96  * This value is always 1 for RX queues.
97  */
98 #ifndef MLX4_PMD_TX_MP_CACHE
99 #define MLX4_PMD_TX_MP_CACHE 8
100 #endif
101
102 /* Alarm timeout. */
103 #define MLX4_ALARM_TIMEOUT_US 100000
104
105 /* Port parameter. */
106 #define MLX4_PMD_PORT_KVARG "port"
107
108 enum {
109         PCI_VENDOR_ID_MELLANOX = 0x15b3,
110 };
111
112 enum {
113         PCI_DEVICE_ID_MELLANOX_CONNECTX3 = 0x1003,
114         PCI_DEVICE_ID_MELLANOX_CONNECTX3VF = 0x1004,
115         PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO = 0x1007,
116 };
117
118 #define MLX4_DRIVER_NAME "net_mlx4"
119
120 /* Bit-field manipulation. */
121 #define BITFIELD_DECLARE(bf, type, size)                                \
122         type bf[(((size_t)(size) / (sizeof(type) * CHAR_BIT)) +         \
123                  !!((size_t)(size) % (sizeof(type) * CHAR_BIT)))]
124 #define BITFIELD_DEFINE(bf, type, size)                                 \
125         BITFIELD_DECLARE((bf), type, (size)) = { 0 }
126 #define BITFIELD_SET(bf, b)                                             \
127         (assert((size_t)(b) < (sizeof(bf) * CHAR_BIT)),                 \
128          (void)((bf)[((b) / (sizeof((bf)[0]) * CHAR_BIT))] |=           \
129                 ((size_t)1 << ((b) % (sizeof((bf)[0]) * CHAR_BIT)))))
130 #define BITFIELD_RESET(bf, b)                                           \
131         (assert((size_t)(b) < (sizeof(bf) * CHAR_BIT)),                 \
132          (void)((bf)[((b) / (sizeof((bf)[0]) * CHAR_BIT))] &=           \
133                 ~((size_t)1 << ((b) % (sizeof((bf)[0]) * CHAR_BIT)))))
134 #define BITFIELD_ISSET(bf, b)                                           \
135         (assert((size_t)(b) < (sizeof(bf) * CHAR_BIT)),                 \
136          !!(((bf)[((b) / (sizeof((bf)[0]) * CHAR_BIT))] &               \
137              ((size_t)1 << ((b) % (sizeof((bf)[0]) * CHAR_BIT))))))
138
139 /* Number of elements in array. */
140 #define elemof(a) (sizeof(a) / sizeof((a)[0]))
141
142 /* Debugging */
143 #ifndef NDEBUG
144 #include <stdio.h>
145 #define DEBUG__(m, ...)                                         \
146         (fprintf(stderr, "%s:%d: %s(): " m "%c",                \
147                  __FILE__, __LINE__, __func__, __VA_ARGS__),    \
148          fflush(stderr),                                        \
149          (void)0)
150 /*
151  * Save/restore errno around DEBUG__().
152  * XXX somewhat undefined behavior, but works.
153  */
154 #define DEBUG_(...)                             \
155         (errno = ((int []){                     \
156                 *(volatile int *)&errno,        \
157                 (DEBUG__(__VA_ARGS__), 0)       \
158         })[0])
159 #define DEBUG(...) DEBUG_(__VA_ARGS__, '\n')
160 #ifndef MLX4_PMD_DEBUG_BROKEN_VERBS
161 #define claim_zero(...) assert((__VA_ARGS__) == 0)
162 #else /* MLX4_PMD_DEBUG_BROKEN_VERBS */
163 #define claim_zero(...) \
164         (void)(((__VA_ARGS__) == 0) || \
165                 DEBUG("Assertion `(" # __VA_ARGS__ ") == 0' failed (IGNORED)."))
166 #endif /* MLX4_PMD_DEBUG_BROKEN_VERBS */
167 #define claim_nonzero(...) assert((__VA_ARGS__) != 0)
168 #define claim_positive(...) assert((__VA_ARGS__) >= 0)
169 #else /* NDEBUG */
170 /* No-ops. */
171 #define DEBUG(...) (void)0
172 #define claim_zero(...) (__VA_ARGS__)
173 #define claim_nonzero(...) (__VA_ARGS__)
174 #define claim_positive(...) (__VA_ARGS__)
175 #endif /* NDEBUG */
176
177 struct mlx4_rxq_stats {
178         unsigned int idx; /**< Mapping index. */
179         uint64_t ipackets; /**< Total of successfully received packets. */
180         uint64_t ibytes; /**< Total of successfully received bytes. */
181         uint64_t idropped; /**< Total of packets dropped when RX ring full. */
182         uint64_t rx_nombuf; /**< Total of RX mbuf allocation failures. */
183 };
184
185 /* RX element (scattered packets). */
186 struct rxq_elt_sp {
187         struct ibv_recv_wr wr; /* Work Request. */
188         struct ibv_sge sges[MLX4_PMD_SGE_WR_N]; /* Scatter/Gather Elements. */
189         struct rte_mbuf *bufs[MLX4_PMD_SGE_WR_N]; /* SGEs buffers. */
190 };
191
192 /* RX element. */
193 struct rxq_elt {
194         struct ibv_recv_wr wr; /* Work Request. */
195         struct ibv_sge sge; /* Scatter/Gather Element. */
196         /* mbuf pointer is derived from WR_ID(wr.wr_id).offset. */
197 };
198
199 /* RX queue descriptor. */
200 struct rxq {
201         LIST_ENTRY(rxq) next; /* Used by parent queue only */
202         struct priv *priv; /* Back pointer to private data. */
203         struct rte_mempool *mp; /* Memory Pool for allocations. */
204         struct ibv_mr *mr; /* Memory Region (for mp). */
205         struct ibv_cq *cq; /* Completion Queue. */
206         struct ibv_qp *qp; /* Queue Pair. */
207         struct ibv_exp_qp_burst_family *if_qp; /* QP burst interface. */
208         struct ibv_exp_cq_family *if_cq; /* CQ interface. */
209         struct ibv_comp_channel *channel;
210         /*
211          * Each VLAN ID requires a separate flow steering rule.
212          */
213         BITFIELD_DECLARE(mac_configured, uint32_t, MLX4_MAX_MAC_ADDRESSES);
214         struct ibv_flow *mac_flow[MLX4_MAX_MAC_ADDRESSES][MLX4_MAX_VLAN_IDS];
215         struct ibv_flow *promisc_flow; /* Promiscuous flow. */
216         struct ibv_flow *allmulti_flow; /* Multicast flow. */
217         unsigned int port_id; /* Port ID for incoming packets. */
218         unsigned int elts_n; /* (*elts)[] length. */
219         unsigned int elts_head; /* Current index in (*elts)[]. */
220         union {
221                 struct rxq_elt_sp (*sp)[]; /* Scattered RX elements. */
222                 struct rxq_elt (*no_sp)[]; /* RX elements. */
223         } elts;
224         unsigned int sp:1; /* Use scattered RX elements. */
225         unsigned int csum:1; /* Enable checksum offloading. */
226         unsigned int csum_l2tun:1; /* Same for L2 tunnels. */
227         struct mlx4_rxq_stats stats; /* RX queue counters. */
228         unsigned int socket; /* CPU socket ID for allocations. */
229         struct ibv_exp_res_domain *rd; /* Resource Domain. */
230         struct {
231                 uint16_t queues_n;
232                 uint16_t queues[RTE_MAX_QUEUES_PER_PORT];
233         } rss;
234 };
235
236 /* TX element. */
237 struct txq_elt {
238         struct rte_mbuf *buf;
239 };
240
241 struct mlx4_txq_stats {
242         unsigned int idx; /**< Mapping index. */
243         uint64_t opackets; /**< Total of successfully sent packets. */
244         uint64_t obytes;   /**< Total of successfully sent bytes. */
245         uint64_t odropped; /**< Total of packets not sent when TX ring full. */
246 };
247
248 /*
249  * Linear buffer type. It is used when transmitting buffers with too many
250  * segments that do not fit the hardware queue (see max_send_sge).
251  * Extra segments are copied (linearized) in such buffers, replacing the
252  * last SGE during TX.
253  * The size is arbitrary but large enough to hold a jumbo frame with
254  * 8 segments considering mbuf.buf_len is about 2048 bytes.
255  */
256 typedef uint8_t linear_t[16384];
257
258 /* TX queue descriptor. */
259 struct txq {
260         struct priv *priv; /* Back pointer to private data. */
261         struct {
262                 const struct rte_mempool *mp; /* Cached Memory Pool. */
263                 struct ibv_mr *mr; /* Memory Region (for mp). */
264                 uint32_t lkey; /* mr->lkey */
265         } mp2mr[MLX4_PMD_TX_MP_CACHE]; /* MP to MR translation table. */
266         struct ibv_cq *cq; /* Completion Queue. */
267         struct ibv_qp *qp; /* Queue Pair. */
268         struct ibv_exp_qp_burst_family *if_qp; /* QP burst interface. */
269         struct ibv_exp_cq_family *if_cq; /* CQ interface. */
270 #if MLX4_PMD_MAX_INLINE > 0
271         uint32_t max_inline; /* Max inline send size <= MLX4_PMD_MAX_INLINE. */
272 #endif
273         unsigned int elts_n; /* (*elts)[] length. */
274         struct txq_elt (*elts)[]; /* TX elements. */
275         unsigned int elts_head; /* Current index in (*elts)[]. */
276         unsigned int elts_tail; /* First element awaiting completion. */
277         unsigned int elts_comp; /* Number of completion requests. */
278         unsigned int elts_comp_cd; /* Countdown for next completion request. */
279         unsigned int elts_comp_cd_init; /* Initial value for countdown. */
280         struct mlx4_txq_stats stats; /* TX queue counters. */
281         linear_t (*elts_linear)[]; /* Linearized buffers. */
282         struct ibv_mr *mr_linear; /* Memory Region for linearized buffers. */
283         unsigned int socket; /* CPU socket ID for allocations. */
284         struct ibv_exp_res_domain *rd; /* Resource Domain. */
285 };
286
287 struct rte_flow;
288
289 struct priv {
290         struct rte_eth_dev *dev; /* Ethernet device. */
291         struct ibv_context *ctx; /* Verbs context. */
292         struct ibv_device_attr device_attr; /* Device properties. */
293         struct ibv_pd *pd; /* Protection Domain. */
294         /*
295          * MAC addresses array and configuration bit-field.
296          * An extra entry that cannot be modified by the DPDK is reserved
297          * for broadcast frames (destination MAC address ff:ff:ff:ff:ff:ff).
298          */
299         struct ether_addr mac[MLX4_MAX_MAC_ADDRESSES];
300         BITFIELD_DECLARE(mac_configured, uint32_t, MLX4_MAX_MAC_ADDRESSES);
301         /* VLAN filters. */
302         struct {
303                 unsigned int enabled:1; /* If enabled. */
304                 unsigned int id:12; /* VLAN ID (0-4095). */
305         } vlan_filter[MLX4_MAX_VLAN_IDS]; /* VLAN filters table. */
306         /* Device properties. */
307         uint16_t mtu; /* Configured MTU. */
308         uint8_t port; /* Physical port number. */
309         unsigned int started:1; /* Device started, flows enabled. */
310         unsigned int promisc:1; /* Device in promiscuous mode. */
311         unsigned int allmulti:1; /* Device receives all multicast packets. */
312         unsigned int hw_qpg:1; /* QP groups are supported. */
313         unsigned int hw_tss:1; /* TSS is supported. */
314         unsigned int hw_rss:1; /* RSS is supported. */
315         unsigned int hw_csum:1; /* Checksum offload is supported. */
316         unsigned int hw_csum_l2tun:1; /* Same for L2 tunnels. */
317         unsigned int rss:1; /* RSS is enabled. */
318         unsigned int vf:1; /* This is a VF device. */
319         unsigned int pending_alarm:1; /* An alarm is pending. */
320         unsigned int isolated:1; /* Toggle isolated mode. */
321         unsigned int inl_recv_size; /* Inline recv size */
322         unsigned int max_rss_tbl_sz; /* Maximum number of RSS queues. */
323         /* RX/TX queues. */
324         unsigned int rxqs_n; /* RX queues array size. */
325         unsigned int txqs_n; /* TX queues array size. */
326         struct rxq *(*rxqs)[]; /* RX queues. */
327         struct txq *(*txqs)[]; /* TX queues. */
328         struct rte_intr_handle intr_handle_dev; /* Device interrupt handler. */
329         struct rte_intr_handle intr_handle; /* Interrupt handler. */
330         struct rte_flow_drop *flow_drop_queue; /* Flow drop queue. */
331         LIST_HEAD(mlx4_flows, rte_flow) flows;
332         struct rte_intr_conf intr_conf; /* Active interrupt configuration. */
333         LIST_HEAD(mlx4_parents, rxq) parents;
334         rte_spinlock_t lock; /* Lock for control functions. */
335 };
336
337 void priv_lock(struct priv *priv);
338 void priv_unlock(struct priv *priv);
339
340 int
341 rxq_create_qp(struct rxq *rxq,
342               uint16_t desc,
343               int inactive,
344               int children_n,
345               struct rxq *rxq_parent);
346
347 void
348 rxq_parent_cleanup(struct rxq *parent);
349
350 struct rxq *
351 priv_parent_create(struct priv *priv,
352                    uint16_t queues[],
353                    uint16_t children_n);
354
355 #endif /* RTE_PMD_MLX4_H_ */