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