net/mlx4: use standard macro to get array size
[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
39 /*
40  * Runtime logging through RTE_LOG() is enabled when not in debugging mode.
41  * Intermediate LOG_*() macros add the required end-of-line characters.
42  */
43 #ifndef NDEBUG
44 #define INFO(...) DEBUG(__VA_ARGS__)
45 #define WARN(...) DEBUG(__VA_ARGS__)
46 #define ERROR(...) DEBUG(__VA_ARGS__)
47 #else
48 #define LOG__(level, m, ...) \
49         RTE_LOG(level, PMD, MLX4_DRIVER_NAME ": " m "%c", __VA_ARGS__)
50 #define LOG_(level, ...) LOG__(level, __VA_ARGS__, '\n')
51 #define INFO(...) LOG_(INFO, __VA_ARGS__)
52 #define WARN(...) LOG_(WARNING, __VA_ARGS__)
53 #define ERROR(...) LOG_(ERR, __VA_ARGS__)
54 #endif
55
56 /* Verbs header. */
57 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
58 #ifdef PEDANTIC
59 #pragma GCC diagnostic ignored "-Wpedantic"
60 #endif
61 #include <infiniband/verbs.h>
62 #ifdef PEDANTIC
63 #pragma GCC diagnostic error "-Wpedantic"
64 #endif
65
66 /* Request send completion once in every 64 sends, might be less. */
67 #define MLX4_PMD_TX_PER_COMP_REQ 64
68
69 /* Maximum size for inline data. */
70 #define MLX4_PMD_MAX_INLINE 0
71
72 /*
73  * Maximum number of cached Memory Pools (MPs) per TX queue. Each RTE MP
74  * from which buffers are to be transmitted will have to be mapped by this
75  * driver to their own Memory Region (MR). This is a slow operation.
76  *
77  * This value is always 1 for RX queues.
78  */
79 #ifndef MLX4_PMD_TX_MP_CACHE
80 #define MLX4_PMD_TX_MP_CACHE 8
81 #endif
82
83 /* Alarm timeout. */
84 #define MLX4_ALARM_TIMEOUT_US 100000
85
86 /* Port parameter. */
87 #define MLX4_PMD_PORT_KVARG "port"
88
89 enum {
90         PCI_VENDOR_ID_MELLANOX = 0x15b3,
91 };
92
93 enum {
94         PCI_DEVICE_ID_MELLANOX_CONNECTX3 = 0x1003,
95         PCI_DEVICE_ID_MELLANOX_CONNECTX3VF = 0x1004,
96         PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO = 0x1007,
97 };
98
99 #define MLX4_DRIVER_NAME "net_mlx4"
100
101 /* Debugging */
102 #ifndef NDEBUG
103 #include <stdio.h>
104 #define DEBUG__(m, ...)                                         \
105         (fprintf(stderr, "%s:%d: %s(): " m "%c",                \
106                  __FILE__, __LINE__, __func__, __VA_ARGS__),    \
107          fflush(stderr),                                        \
108          (void)0)
109 /*
110  * Save/restore errno around DEBUG__().
111  * XXX somewhat undefined behavior, but works.
112  */
113 #define DEBUG_(...)                             \
114         (errno = ((int []){                     \
115                 *(volatile int *)&errno,        \
116                 (DEBUG__(__VA_ARGS__), 0)       \
117         })[0])
118 #define DEBUG(...) DEBUG_(__VA_ARGS__, '\n')
119 #ifndef MLX4_PMD_DEBUG_BROKEN_VERBS
120 #define claim_zero(...) assert((__VA_ARGS__) == 0)
121 #else /* MLX4_PMD_DEBUG_BROKEN_VERBS */
122 #define claim_zero(...) \
123         (void)(((__VA_ARGS__) == 0) || \
124                 DEBUG("Assertion `(" # __VA_ARGS__ ") == 0' failed (IGNORED)."))
125 #endif /* MLX4_PMD_DEBUG_BROKEN_VERBS */
126 #define claim_nonzero(...) assert((__VA_ARGS__) != 0)
127 #define claim_positive(...) assert((__VA_ARGS__) >= 0)
128 #else /* NDEBUG */
129 /* No-ops. */
130 #define DEBUG(...) (void)0
131 #define claim_zero(...) (__VA_ARGS__)
132 #define claim_nonzero(...) (__VA_ARGS__)
133 #define claim_positive(...) (__VA_ARGS__)
134 #endif /* NDEBUG */
135
136 struct mlx4_rxq_stats {
137         unsigned int idx; /**< Mapping index. */
138         uint64_t ipackets; /**< Total of successfully received packets. */
139         uint64_t ibytes; /**< Total of successfully received bytes. */
140         uint64_t idropped; /**< Total of packets dropped when RX ring full. */
141         uint64_t rx_nombuf; /**< Total of RX mbuf allocation failures. */
142 };
143
144 /* RX element. */
145 struct rxq_elt {
146         struct ibv_recv_wr wr; /* Work Request. */
147         struct ibv_sge sge; /* Scatter/Gather Element. */
148         struct rte_mbuf *buf; /**< Buffer. */
149 };
150
151 /* RX queue descriptor. */
152 struct rxq {
153         struct priv *priv; /* Back pointer to private data. */
154         struct rte_mempool *mp; /* Memory Pool for allocations. */
155         struct ibv_mr *mr; /* Memory Region (for mp). */
156         struct ibv_cq *cq; /* Completion Queue. */
157         struct ibv_qp *qp; /* Queue Pair. */
158         struct ibv_comp_channel *channel;
159         unsigned int port_id; /* Port ID for incoming packets. */
160         unsigned int elts_n; /* (*elts)[] length. */
161         unsigned int elts_head; /* Current index in (*elts)[]. */
162         struct rxq_elt (*elts)[]; /* Rx elements. */
163         struct mlx4_rxq_stats stats; /* RX queue counters. */
164         unsigned int socket; /* CPU socket ID for allocations. */
165 };
166
167 /* TX element. */
168 struct txq_elt {
169         struct ibv_send_wr wr; /* Work request. */
170         struct ibv_sge sge; /* Scatter/gather element. */
171         struct rte_mbuf *buf;
172 };
173
174 struct mlx4_txq_stats {
175         unsigned int idx; /**< Mapping index. */
176         uint64_t opackets; /**< Total of successfully sent packets. */
177         uint64_t obytes;   /**< Total of successfully sent bytes. */
178         uint64_t odropped; /**< Total of packets not sent when TX ring full. */
179 };
180
181 /* TX queue descriptor. */
182 struct txq {
183         struct priv *priv; /* Back pointer to private data. */
184         struct {
185                 const struct rte_mempool *mp; /* Cached Memory Pool. */
186                 struct ibv_mr *mr; /* Memory Region (for mp). */
187                 uint32_t lkey; /* mr->lkey */
188         } mp2mr[MLX4_PMD_TX_MP_CACHE]; /* MP to MR translation table. */
189         struct ibv_cq *cq; /* Completion Queue. */
190         struct ibv_qp *qp; /* Queue Pair. */
191         uint32_t max_inline; /* Max inline send size <= MLX4_PMD_MAX_INLINE. */
192         unsigned int elts_n; /* (*elts)[] length. */
193         struct txq_elt (*elts)[]; /* TX elements. */
194         unsigned int elts_head; /* Current index in (*elts)[]. */
195         unsigned int elts_tail; /* First element awaiting completion. */
196         unsigned int elts_comp; /* Number of completion requests. */
197         unsigned int elts_comp_cd; /* Countdown for next completion request. */
198         unsigned int elts_comp_cd_init; /* Initial value for countdown. */
199         struct mlx4_txq_stats stats; /* TX queue counters. */
200         unsigned int socket; /* CPU socket ID for allocations. */
201 };
202
203 struct rte_flow;
204
205 struct priv {
206         struct rte_eth_dev *dev; /* Ethernet device. */
207         struct ibv_context *ctx; /* Verbs context. */
208         struct ibv_device_attr device_attr; /* Device properties. */
209         struct ibv_pd *pd; /* Protection Domain. */
210         struct ether_addr mac; /* MAC address. */
211         struct ibv_flow *mac_flow; /* Flow associated with MAC address. */
212         /* Device properties. */
213         uint16_t mtu; /* Configured MTU. */
214         uint8_t port; /* Physical port number. */
215         unsigned int started:1; /* Device started, flows enabled. */
216         unsigned int vf:1; /* This is a VF device. */
217         unsigned int pending_alarm:1; /* An alarm is pending. */
218         unsigned int isolated:1; /* Toggle isolated mode. */
219         /* RX/TX queues. */
220         unsigned int rxqs_n; /* RX queues array size. */
221         unsigned int txqs_n; /* TX queues array size. */
222         struct rxq *(*rxqs)[]; /* RX queues. */
223         struct txq *(*txqs)[]; /* TX queues. */
224         struct rte_intr_handle intr_handle_dev; /* Device interrupt handler. */
225         struct rte_intr_handle intr_handle; /* Interrupt handler. */
226         struct rte_flow_drop *flow_drop_queue; /* Flow drop queue. */
227         LIST_HEAD(mlx4_flows, rte_flow) flows;
228         struct rte_intr_conf intr_conf; /* Active interrupt configuration. */
229 };
230
231 #endif /* RTE_PMD_MLX4_H_ */