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