]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/sfxge/sfxge.h
MFC: 278250
[FreeBSD/stable/10.git] / sys / dev / sfxge / sfxge.h
1 /*-
2  * Copyright (c) 2010-2011 Solarflare Communications, Inc.
3  * All rights reserved.
4  *
5  * This software was developed in part by Philip Paeps under contract for
6  * Solarflare Communications, Inc.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31
32 #ifndef _SFXGE_H
33 #define _SFXGE_H
34
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/condvar.h>
38 #include <sys/socket.h>
39 #include <sys/sysctl.h>
40 #include <sys/sx.h>
41 #include <vm/uma.h>
42
43 #include <net/ethernet.h>
44 #include <net/if.h>
45 #include <net/if_media.h>
46 #include <net/if_types.h>
47
48 /*
49  * Backward-compatibility
50  */
51 #ifndef CACHE_LINE_SIZE
52 /* This should be right on most machines the driver will be used on, and
53  * we needn't care too much about wasting a few KB per interface.
54  */
55 #define CACHE_LINE_SIZE 128
56 #endif
57 #ifndef IFCAP_LINKSTATE
58 #define IFCAP_LINKSTATE 0
59 #endif
60 #ifndef IFCAP_VLAN_HWTSO
61 #define IFCAP_VLAN_HWTSO 0
62 #endif
63 #ifndef IFM_10G_T
64 #define IFM_10G_T IFM_UNKNOWN
65 #endif
66 #ifndef IFM_10G_KX4
67 #define IFM_10G_KX4 IFM_10G_CX4
68 #endif
69 #if __FreeBSD_version >= 800054
70 /* Networking core is multiqueue aware. We can manage our own TX
71  * queues and use m_pkthdr.flowid.
72  */
73 #define SFXGE_HAVE_MQ
74 #endif
75 #if (__FreeBSD_version >= 800501 && __FreeBSD_version < 900000) || \
76         __FreeBSD_version >= 900003
77 #define SFXGE_HAVE_DESCRIBE_INTR
78 #endif
79 #ifdef IFM_ETH_RXPAUSE
80 #define SFXGE_HAVE_PAUSE_MEDIAOPTS
81 #endif
82 #ifndef CTLTYPE_U64
83 #define CTLTYPE_U64 CTLTYPE_QUAD
84 #endif
85
86 #include "sfxge_rx.h"
87 #include "sfxge_tx.h"
88
89 #define ROUNDUP_POW_OF_TWO(_n)  (1ULL << flsl((_n) - 1))
90
91 #define SFXGE_IP_ALIGN  2
92
93 #define SFXGE_ETHERTYPE_LOOPBACK        0x9000  /* Xerox loopback */
94
95 enum sfxge_evq_state {
96         SFXGE_EVQ_UNINITIALIZED = 0,
97         SFXGE_EVQ_INITIALIZED,
98         SFXGE_EVQ_STARTING,
99         SFXGE_EVQ_STARTED
100 };
101
102 #define SFXGE_EV_BATCH  16384
103
104 struct sfxge_evq {
105         /* Structure members below are sorted by usage order */
106         struct sfxge_softc      *sc;
107         struct mtx              lock;
108         unsigned int            index;
109         enum sfxge_evq_state    init_state;
110         efsys_mem_t             mem;
111         efx_evq_t               *common;
112         unsigned int            read_ptr;
113         boolean_t               exception;
114         unsigned int            rx_done;
115         unsigned int            tx_done;
116
117         /* Linked list of TX queues with completions to process */
118         struct sfxge_txq        *txq;
119         struct sfxge_txq        **txqs;
120
121         /* Structure members not used on event processing path */
122         unsigned int            buf_base_id;
123         unsigned int            entries;
124         char                    lock_name[SFXGE_LOCK_NAME_MAX];
125 } __aligned(CACHE_LINE_SIZE);
126
127 #define SFXGE_NDESCS    1024
128 #define SFXGE_MODERATION        30
129
130 enum sfxge_intr_state {
131         SFXGE_INTR_UNINITIALIZED = 0,
132         SFXGE_INTR_INITIALIZED,
133         SFXGE_INTR_TESTING,
134         SFXGE_INTR_STARTED
135 };
136
137 struct sfxge_intr_hdl {
138         int                     eih_rid;
139         void                    *eih_tag;
140         struct resource         *eih_res;
141 };
142
143 struct sfxge_intr {
144         enum sfxge_intr_state   state;
145         struct resource         *msix_res;
146         struct sfxge_intr_hdl   *table;
147         int                     n_alloc;
148         int                     type;
149         efsys_mem_t             status;
150         uint32_t                zero_count;
151 };
152
153 enum sfxge_mcdi_state {
154         SFXGE_MCDI_UNINITIALIZED = 0,
155         SFXGE_MCDI_INITIALIZED,
156         SFXGE_MCDI_BUSY,
157         SFXGE_MCDI_COMPLETED
158 };
159
160 struct sfxge_mcdi {
161         struct mtx              lock;
162         struct cv               cv;
163         enum sfxge_mcdi_state   state;
164         efx_mcdi_transport_t    transport;
165
166         /* Only used in debugging output */
167         char                    lock_name[SFXGE_LOCK_NAME_MAX];
168 };
169
170 struct sfxge_hw_stats {
171         clock_t                 update_time;
172         efsys_mem_t             dma_buf;
173         void                    *decode_buf;
174 };
175
176 enum sfxge_port_state {
177         SFXGE_PORT_UNINITIALIZED = 0,
178         SFXGE_PORT_INITIALIZED,
179         SFXGE_PORT_STARTED
180 };
181
182 struct sfxge_port {
183         struct sfxge_softc      *sc;
184         struct mtx              lock;
185         enum sfxge_port_state   init_state;
186 #ifndef SFXGE_HAVE_PAUSE_MEDIAOPTS
187         unsigned int            wanted_fc;
188 #endif
189         struct sfxge_hw_stats   phy_stats;
190         struct sfxge_hw_stats   mac_stats;
191         efx_link_mode_t         link_mode;
192
193         /* Only used in debugging output */
194         char                    lock_name[SFXGE_LOCK_NAME_MAX];
195 };
196
197 enum sfxge_softc_state {
198         SFXGE_UNINITIALIZED = 0,
199         SFXGE_INITIALIZED,
200         SFXGE_REGISTERED,
201         SFXGE_STARTED
202 };
203
204 struct sfxge_softc {
205         device_t                        dev;
206         struct sx                       softc_lock;
207         char                            softc_lock_name[SFXGE_LOCK_NAME_MAX];
208         enum sfxge_softc_state          init_state;
209         struct ifnet                    *ifnet;
210         unsigned int                    if_flags;
211         struct sysctl_oid               *stats_node;
212         struct sysctl_oid               *txqs_node;
213
214         struct task                     task_reset;
215
216         efx_family_t                    family;
217         caddr_t                         vpd_data;
218         size_t                          vpd_size;
219         efx_nic_t                       *enp;
220         efsys_lock_t                    enp_lock;
221
222         unsigned int                    rxq_entries;
223         unsigned int                    txq_entries;
224
225         bus_dma_tag_t                   parent_dma_tag;
226         efsys_bar_t                     bar;
227
228         struct sfxge_intr               intr;
229         struct sfxge_mcdi               mcdi;
230         struct sfxge_port               port;
231         uint32_t                        buffer_table_next;
232
233         struct sfxge_evq                *evq[SFXGE_RX_SCALE_MAX];
234         unsigned int                    ev_moderation;
235 #if EFSYS_OPT_QSTATS
236         clock_t                         ev_stats_update_time;
237         uint64_t                        ev_stats[EV_NQSTATS];
238 #endif
239
240         unsigned int                    max_rss_channels;
241         uma_zone_t                      rxq_cache;
242         struct sfxge_rxq                *rxq[SFXGE_RX_SCALE_MAX];
243         unsigned int                    rx_indir_table[SFXGE_RX_SCALE_MAX];
244
245 #ifdef SFXGE_HAVE_MQ
246         struct sfxge_txq                *txq[SFXGE_TXQ_NTYPES + SFXGE_RX_SCALE_MAX];
247 #else
248         struct sfxge_txq                *txq[SFXGE_TXQ_NTYPES];
249 #endif
250
251         struct ifmedia                  media;
252
253         size_t                          rx_prefix_size;
254         size_t                          rx_buffer_size;
255         uma_zone_t                      rx_buffer_zone;
256
257 #ifndef SFXGE_HAVE_MQ
258         struct mtx                      tx_lock __aligned(CACHE_LINE_SIZE);
259         char                            tx_lock_name[SFXGE_LOCK_NAME_MAX];
260 #endif
261 };
262
263 #define SFXGE_LINK_UP(sc) ((sc)->port.link_mode != EFX_LINK_DOWN)
264 #define SFXGE_RUNNING(sc) ((sc)->ifnet->if_drv_flags & IFF_DRV_RUNNING)
265
266 #define SFXGE_PARAM(_name)      "hw.sfxge." #_name
267
268 SYSCTL_DECL(_hw_sfxge);
269
270 /*
271  * From sfxge.c.
272  */
273 extern void sfxge_schedule_reset(struct sfxge_softc *sc);
274 extern void sfxge_sram_buf_tbl_alloc(struct sfxge_softc *sc, size_t n,
275                                      uint32_t *idp);
276
277 /*
278  * From sfxge_dma.c.
279  */
280 extern int sfxge_dma_init(struct sfxge_softc *sc);
281 extern void sfxge_dma_fini(struct sfxge_softc *sc);
282 extern int sfxge_dma_alloc(struct sfxge_softc *sc, bus_size_t len,
283     efsys_mem_t *esmp);
284 extern void sfxge_dma_free(efsys_mem_t *esmp);
285 extern int sfxge_dma_map_sg_collapse(bus_dma_tag_t tag, bus_dmamap_t map,
286     struct mbuf **mp, bus_dma_segment_t *segs, int *nsegs, int maxsegs);
287
288 /*
289  * From sfxge_ev.c.
290  */
291 extern int sfxge_ev_init(struct sfxge_softc *sc);
292 extern void sfxge_ev_fini(struct sfxge_softc *sc);
293 extern int sfxge_ev_start(struct sfxge_softc *sc);
294 extern void sfxge_ev_stop(struct sfxge_softc *sc);
295 extern int sfxge_ev_qpoll(struct sfxge_evq *evq);
296
297 /*
298  * From sfxge_intr.c.
299  */
300 extern int sfxge_intr_init(struct sfxge_softc *sc);
301 extern void sfxge_intr_fini(struct sfxge_softc *sc);
302 extern int sfxge_intr_start(struct sfxge_softc *sc);
303 extern void sfxge_intr_stop(struct sfxge_softc *sc);
304
305 /*
306  * From sfxge_mcdi.c.
307  */
308 extern int sfxge_mcdi_init(struct sfxge_softc *sc);
309 extern void sfxge_mcdi_fini(struct sfxge_softc *sc);
310
311 /*
312  * From sfxge_port.c.
313  */
314 extern int sfxge_port_init(struct sfxge_softc *sc);
315 extern void sfxge_port_fini(struct sfxge_softc *sc);
316 extern int sfxge_port_start(struct sfxge_softc *sc);
317 extern void sfxge_port_stop(struct sfxge_softc *sc);
318 extern void sfxge_mac_link_update(struct sfxge_softc *sc,
319     efx_link_mode_t mode);
320 extern int sfxge_mac_filter_set(struct sfxge_softc *sc);
321 extern int sfxge_port_ifmedia_init(struct sfxge_softc *sc);
322
323 #define SFXGE_MAX_MTU (9 * 1024)
324
325 #define SFXGE_ADAPTER_LOCK_INIT(_sc, _ifname)                           \
326         do {                                                            \
327                 struct sfxge_softc *__sc = (_sc);                       \
328                                                                         \
329                 snprintf((__sc)->softc_lock_name,                       \
330                          sizeof((__sc)->softc_lock_name),               \
331                          "%s:softc", (_ifname));                        \
332                 sx_init(&(__sc)->softc_lock, (__sc)->softc_lock_name);  \
333         } while (B_FALSE)
334 #define SFXGE_ADAPTER_LOCK_DESTROY(_sc)                                 \
335         sx_destroy(&(_sc)->softc_lock)
336 #define SFXGE_ADAPTER_LOCK(_sc)                                         \
337         sx_xlock(&(_sc)->softc_lock)
338 #define SFXGE_ADAPTER_UNLOCK(_sc)                                       \
339         sx_xunlock(&(_sc)->softc_lock)
340 #define SFXGE_ADAPTER_LOCK_ASSERT_OWNED(_sc)                            \
341         sx_assert(&(_sc)->softc_lock, LA_XLOCKED)
342
343 #define SFXGE_PORT_LOCK_INIT(_port, _ifname)                            \
344         do {                                                            \
345                 struct sfxge_port *__port = (_port);                    \
346                                                                         \
347                 snprintf((__port)->lock_name,                           \
348                          sizeof((__port)->lock_name),                   \
349                          "%s:port", (_ifname));                         \
350                 mtx_init(&(__port)->lock, (__port)->lock_name,          \
351                          NULL, MTX_DEF);                                \
352         } while (B_FALSE)
353 #define SFXGE_PORT_LOCK_DESTROY(_port)                                  \
354         mtx_destroy(&(_port)->lock)
355 #define SFXGE_PORT_LOCK(_port)                                          \
356         mtx_lock(&(_port)->lock)
357 #define SFXGE_PORT_UNLOCK(_port)                                        \
358         mtx_unlock(&(_port)->lock)
359 #define SFXGE_PORT_LOCK_ASSERT_OWNED(_port)                             \
360         mtx_assert(&(_port)->lock, MA_OWNED)
361
362 #define SFXGE_MCDI_LOCK_INIT(_mcdi, _ifname)                            \
363         do {                                                            \
364                 struct sfxge_mcdi  *__mcdi = (_mcdi);                   \
365                                                                         \
366                 snprintf((__mcdi)->lock_name,                           \
367                          sizeof((__mcdi)->lock_name),                   \
368                          "%s:mcdi", (_ifname));                         \
369                 mtx_init(&(__mcdi)->lock, (__mcdi)->lock_name,          \
370                          NULL, MTX_DEF);                                \
371         } while (B_FALSE)
372 #define SFXGE_MCDI_LOCK_DESTROY(_mcdi)                                  \
373         mtx_destroy(&(_mcdi)->lock)
374 #define SFXGE_MCDI_LOCK(_mcdi)                                          \
375         mtx_lock(&(_mcdi)->lock)
376 #define SFXGE_MCDI_UNLOCK(_mcdi)                                        \
377         mtx_unlock(&(_mcdi)->lock)
378 #define SFXGE_MCDI_LOCK_ASSERT_OWNED(_mcdi)                             \
379         mtx_assert(&(_mcdi)->lock, MA_OWNED)
380
381 #define SFXGE_EVQ_LOCK_INIT(_evq, _ifname, _evq_index)                  \
382         do {                                                            \
383                 struct sfxge_evq  *__evq = (_evq);                      \
384                                                                         \
385                 snprintf((__evq)->lock_name,                            \
386                          sizeof((__evq)->lock_name),                    \
387                          "%s:evq%u", (_ifname), (_evq_index));          \
388                 mtx_init(&(__evq)->lock, (__evq)->lock_name,            \
389                          NULL, MTX_DEF);                                \
390         } while (B_FALSE)
391 #define SFXGE_EVQ_LOCK_DESTROY(_evq)                                    \
392         mtx_destroy(&(_evq)->lock)
393 #define SFXGE_EVQ_LOCK(_evq)                                            \
394         mtx_lock(&(_evq)->lock)
395 #define SFXGE_EVQ_UNLOCK(_evq)                                          \
396         mtx_unlock(&(_evq)->lock)
397 #define SFXGE_EVQ_LOCK_ASSERT_OWNED(_evq)                               \
398         mtx_assert(&(_evq)->lock, MA_OWNED)
399
400 #endif /* _SFXGE_H */