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