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