]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/sfxge/sfxge.h
MFC r311639
[FreeBSD/stable/10.git] / sys / dev / sfxge / sfxge.h
1 /*-
2  * Copyright (c) 2010-2016 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
116 #define SFXGE_MAGIC_RESERVED            0x8000
117
118 #define SFXGE_MAGIC_DMAQ_LABEL_WIDTH    6
119 #define SFXGE_MAGIC_DMAQ_LABEL_MASK \
120         ((1 << SFXGE_MAGIC_DMAQ_LABEL_WIDTH) - 1)
121
122 enum sfxge_sw_ev {
123         SFXGE_SW_EV_RX_QFLUSH_DONE = 1,
124         SFXGE_SW_EV_RX_QFLUSH_FAILED,
125         SFXGE_SW_EV_RX_QREFILL,
126         SFXGE_SW_EV_TX_QFLUSH_DONE,
127 };
128
129 #define SFXGE_SW_EV_MAGIC(_sw_ev) \
130         (SFXGE_MAGIC_RESERVED | ((_sw_ev) << SFXGE_MAGIC_DMAQ_LABEL_WIDTH))
131
132 static inline uint16_t
133 sfxge_sw_ev_mk_magic(enum sfxge_sw_ev sw_ev, unsigned int label)
134 {
135         KASSERT((label & SFXGE_MAGIC_DMAQ_LABEL_MASK) == label,
136             ("(label & SFXGE_MAGIC_DMAQ_LABEL_MASK) != label"));
137         return SFXGE_SW_EV_MAGIC(sw_ev) | label;
138 }
139
140 static inline uint16_t
141 sfxge_sw_ev_rxq_magic(enum sfxge_sw_ev sw_ev, struct sfxge_rxq *rxq)
142 {
143         return sfxge_sw_ev_mk_magic(sw_ev, 0);
144 }
145
146 static inline uint16_t
147 sfxge_sw_ev_txq_magic(enum sfxge_sw_ev sw_ev, struct sfxge_txq *txq)
148 {
149         return sfxge_sw_ev_mk_magic(sw_ev, txq->type);
150 }
151
152 enum sfxge_evq_state {
153         SFXGE_EVQ_UNINITIALIZED = 0,
154         SFXGE_EVQ_INITIALIZED,
155         SFXGE_EVQ_STARTING,
156         SFXGE_EVQ_STARTED
157 };
158
159 #define SFXGE_EV_BATCH  16384
160
161 #define SFXGE_CALLOUT_TICKS 100
162
163 struct sfxge_evq {
164         /* Structure members below are sorted by usage order */
165         struct sfxge_softc      *sc;
166         struct mtx              lock;
167         unsigned int            index;
168         enum sfxge_evq_state    init_state;
169         efsys_mem_t             mem;
170         efx_evq_t               *common;
171         unsigned int            read_ptr;
172         boolean_t               exception;
173         unsigned int            rx_done;
174         unsigned int            tx_done;
175
176         /* Linked list of TX queues with completions to process */
177         struct sfxge_txq        *txq;
178         struct sfxge_txq        **txqs;
179
180         /* Structure members not used on event processing path */
181         unsigned int            buf_base_id;
182         unsigned int            entries;
183         char                    lock_name[SFXGE_LOCK_NAME_MAX];
184 } __aligned(CACHE_LINE_SIZE);
185
186 #define SFXGE_NDESCS    1024
187 #define SFXGE_MODERATION        30
188
189 enum sfxge_intr_state {
190         SFXGE_INTR_UNINITIALIZED = 0,
191         SFXGE_INTR_INITIALIZED,
192         SFXGE_INTR_TESTING,
193         SFXGE_INTR_STARTED
194 };
195
196 struct sfxge_intr_hdl {
197         int                     eih_rid;
198         void                    *eih_tag;
199         struct resource         *eih_res;
200 };
201
202 struct sfxge_intr {
203         enum sfxge_intr_state   state;
204         struct resource         *msix_res;
205         struct sfxge_intr_hdl   *table;
206         int                     n_alloc;
207         int                     type;
208         efsys_mem_t             status;
209         uint32_t                zero_count;
210 };
211
212 enum sfxge_mcdi_state {
213         SFXGE_MCDI_UNINITIALIZED = 0,
214         SFXGE_MCDI_INITIALIZED,
215         SFXGE_MCDI_BUSY,
216         SFXGE_MCDI_COMPLETED
217 };
218
219 struct sfxge_mcdi {
220         struct mtx              lock;
221         efsys_mem_t             mem;
222         enum sfxge_mcdi_state   state;
223         efx_mcdi_transport_t    transport;
224
225         /* Only used in debugging output */
226         char                    lock_name[SFXGE_LOCK_NAME_MAX];
227 };
228
229 struct sfxge_hw_stats {
230         clock_t                 update_time;
231         efsys_mem_t             dma_buf;
232         void                    *decode_buf;
233 };
234
235 enum sfxge_port_state {
236         SFXGE_PORT_UNINITIALIZED = 0,
237         SFXGE_PORT_INITIALIZED,
238         SFXGE_PORT_STARTED
239 };
240
241 struct sfxge_port {
242         struct sfxge_softc      *sc;
243         struct mtx              lock;
244         enum sfxge_port_state   init_state;
245 #ifndef SFXGE_HAVE_PAUSE_MEDIAOPTS
246         unsigned int            wanted_fc;
247 #endif
248         struct sfxge_hw_stats   phy_stats;
249         struct sfxge_hw_stats   mac_stats;
250         efx_link_mode_t         link_mode;
251         uint8_t                 mcast_addrs[EFX_MAC_MULTICAST_LIST_MAX *
252                                             EFX_MAC_ADDR_LEN];
253         unsigned int            mcast_count;
254
255         /* Only used in debugging output */
256         char                    lock_name[SFXGE_LOCK_NAME_MAX];
257 };
258
259 enum sfxge_softc_state {
260         SFXGE_UNINITIALIZED = 0,
261         SFXGE_INITIALIZED,
262         SFXGE_REGISTERED,
263         SFXGE_STARTED
264 };
265
266 struct sfxge_softc {
267         device_t                        dev;
268         struct sx                       softc_lock;
269         char                            softc_lock_name[SFXGE_LOCK_NAME_MAX];
270         enum sfxge_softc_state          init_state;
271         struct ifnet                    *ifnet;
272         unsigned int                    if_flags;
273         struct sysctl_oid               *stats_node;
274         struct sysctl_oid               *txqs_node;
275
276         struct task                     task_reset;
277
278         efx_family_t                    family;
279         caddr_t                         vpd_data;
280         size_t                          vpd_size;
281         efx_nic_t                       *enp;
282         efsys_lock_t                    enp_lock;
283
284         unsigned int                    rxq_entries;
285         unsigned int                    txq_entries;
286
287         bus_dma_tag_t                   parent_dma_tag;
288         efsys_bar_t                     bar;
289
290         struct sfxge_intr               intr;
291         struct sfxge_mcdi               mcdi;
292         struct sfxge_port               port;
293         uint32_t                        buffer_table_next;
294
295         struct sfxge_evq                *evq[SFXGE_RX_SCALE_MAX];
296         unsigned int                    ev_moderation;
297 #if EFSYS_OPT_QSTATS
298         clock_t                         ev_stats_update_time;
299         uint64_t                        ev_stats[EV_NQSTATS];
300 #endif
301
302         unsigned int                    max_rss_channels;
303         uma_zone_t                      rxq_cache;
304         struct sfxge_rxq                *rxq[SFXGE_RX_SCALE_MAX];
305         unsigned int                    rx_indir_table[EFX_RSS_TBL_SIZE];
306
307         struct sfxge_txq                *txq[SFXGE_TXQ_NTYPES + SFXGE_RX_SCALE_MAX];
308
309         struct ifmedia                  media;
310
311         size_t                          rx_prefix_size;
312         size_t                          rx_buffer_size;
313         size_t                          rx_buffer_align;
314         uma_zone_t                      rx_buffer_zone;
315
316         struct callout                  tick_callout;
317
318         unsigned int                    evq_max;
319         unsigned int                    evq_count;
320         unsigned int                    rxq_count;
321         unsigned int                    txq_count;
322
323         unsigned int                    tso_fw_assisted;
324 #define SFXGE_FATSOV1   (1 << 0)
325 #define SFXGE_FATSOV2   (1 << 1)
326
327 #if EFSYS_OPT_MCDI_LOGGING
328         int                             mcdi_logging;
329 #endif
330 };
331
332 #define SFXGE_LINK_UP(sc) \
333         ((sc)->port.link_mode != EFX_LINK_DOWN && \
334          (sc)->port.link_mode != EFX_LINK_UNKNOWN)
335 #define SFXGE_RUNNING(sc) ((sc)->ifnet->if_drv_flags & IFF_DRV_RUNNING)
336
337 #define SFXGE_PARAM(_name)      "hw.sfxge." #_name
338
339 SYSCTL_DECL(_hw_sfxge);
340
341 /*
342  * From sfxge.c.
343  */
344 extern void sfxge_schedule_reset(struct sfxge_softc *sc);
345 extern void sfxge_sram_buf_tbl_alloc(struct sfxge_softc *sc, size_t n,
346                                      uint32_t *idp);
347
348 /*
349  * From sfxge_dma.c.
350  */
351 extern int sfxge_dma_init(struct sfxge_softc *sc);
352 extern void sfxge_dma_fini(struct sfxge_softc *sc);
353 extern int sfxge_dma_alloc(struct sfxge_softc *sc, bus_size_t len,
354                            efsys_mem_t *esmp);
355 extern void sfxge_dma_free(efsys_mem_t *esmp);
356 extern int sfxge_dma_map_sg_collapse(bus_dma_tag_t tag, bus_dmamap_t map,
357                                      struct mbuf **mp,
358                                      bus_dma_segment_t *segs,
359                                      int *nsegs, int maxsegs);
360
361 /*
362  * From sfxge_ev.c.
363  */
364 extern int sfxge_ev_init(struct sfxge_softc *sc);
365 extern void sfxge_ev_fini(struct sfxge_softc *sc);
366 extern int sfxge_ev_start(struct sfxge_softc *sc);
367 extern void sfxge_ev_stop(struct sfxge_softc *sc);
368 extern int sfxge_ev_qpoll(struct sfxge_evq *evq);
369
370 /*
371  * From sfxge_intr.c.
372  */
373 extern int sfxge_intr_init(struct sfxge_softc *sc);
374 extern void sfxge_intr_fini(struct sfxge_softc *sc);
375 extern int sfxge_intr_start(struct sfxge_softc *sc);
376 extern void sfxge_intr_stop(struct sfxge_softc *sc);
377
378 /*
379  * From sfxge_mcdi.c.
380  */
381 extern int sfxge_mcdi_init(struct sfxge_softc *sc);
382 extern void sfxge_mcdi_fini(struct sfxge_softc *sc);
383 extern int sfxge_mcdi_ioctl(struct sfxge_softc *sc, sfxge_ioc_t *ip);
384
385 /*
386  * From sfxge_nvram.c.
387  */
388 extern int sfxge_nvram_ioctl(struct sfxge_softc *sc, sfxge_ioc_t *ip);
389
390 /*
391  * From sfxge_port.c.
392  */
393 extern int sfxge_port_init(struct sfxge_softc *sc);
394 extern void sfxge_port_fini(struct sfxge_softc *sc);
395 extern int sfxge_port_start(struct sfxge_softc *sc);
396 extern void sfxge_port_stop(struct sfxge_softc *sc);
397 extern void sfxge_mac_link_update(struct sfxge_softc *sc,
398                                   efx_link_mode_t mode);
399 extern int sfxge_mac_filter_set(struct sfxge_softc *sc);
400 extern int sfxge_port_ifmedia_init(struct sfxge_softc *sc);
401 extern void sfxge_port_update_stats(struct sfxge_softc *sc);
402
403 #define SFXGE_MAX_MTU (9 * 1024)
404
405 #define SFXGE_ADAPTER_LOCK_INIT(_sc, _ifname)                           \
406         do {                                                            \
407                 struct sfxge_softc *__sc = (_sc);                       \
408                                                                         \
409                 snprintf((__sc)->softc_lock_name,                       \
410                          sizeof((__sc)->softc_lock_name),               \
411                          "%s:softc", (_ifname));                        \
412                 sx_init(&(__sc)->softc_lock, (__sc)->softc_lock_name);  \
413         } while (B_FALSE)
414 #define SFXGE_ADAPTER_LOCK_DESTROY(_sc)                                 \
415         sx_destroy(&(_sc)->softc_lock)
416 #define SFXGE_ADAPTER_LOCK(_sc)                                         \
417         sx_xlock(&(_sc)->softc_lock)
418 #define SFXGE_ADAPTER_UNLOCK(_sc)                                       \
419         sx_xunlock(&(_sc)->softc_lock)
420 #define SFXGE_ADAPTER_LOCK_ASSERT_OWNED(_sc)                            \
421         sx_assert(&(_sc)->softc_lock, LA_XLOCKED)
422
423 #define SFXGE_PORT_LOCK_INIT(_port, _ifname)                            \
424         do {                                                            \
425                 struct sfxge_port *__port = (_port);                    \
426                                                                         \
427                 snprintf((__port)->lock_name,                           \
428                          sizeof((__port)->lock_name),                   \
429                          "%s:port", (_ifname));                         \
430                 mtx_init(&(__port)->lock, (__port)->lock_name,          \
431                          NULL, MTX_DEF);                                \
432         } while (B_FALSE)
433 #define SFXGE_PORT_LOCK_DESTROY(_port)                                  \
434         mtx_destroy(&(_port)->lock)
435 #define SFXGE_PORT_LOCK(_port)                                          \
436         mtx_lock(&(_port)->lock)
437 #define SFXGE_PORT_UNLOCK(_port)                                        \
438         mtx_unlock(&(_port)->lock)
439 #define SFXGE_PORT_LOCK_ASSERT_OWNED(_port)                             \
440         mtx_assert(&(_port)->lock, MA_OWNED)
441
442 #define SFXGE_MCDI_LOCK_INIT(_mcdi, _ifname)                            \
443         do {                                                            \
444                 struct sfxge_mcdi  *__mcdi = (_mcdi);                   \
445                                                                         \
446                 snprintf((__mcdi)->lock_name,                           \
447                          sizeof((__mcdi)->lock_name),                   \
448                          "%s:mcdi", (_ifname));                         \
449                 mtx_init(&(__mcdi)->lock, (__mcdi)->lock_name,          \
450                          NULL, MTX_DEF);                                \
451         } while (B_FALSE)
452 #define SFXGE_MCDI_LOCK_DESTROY(_mcdi)                                  \
453         mtx_destroy(&(_mcdi)->lock)
454 #define SFXGE_MCDI_LOCK(_mcdi)                                          \
455         mtx_lock(&(_mcdi)->lock)
456 #define SFXGE_MCDI_UNLOCK(_mcdi)                                        \
457         mtx_unlock(&(_mcdi)->lock)
458 #define SFXGE_MCDI_LOCK_ASSERT_OWNED(_mcdi)                             \
459         mtx_assert(&(_mcdi)->lock, MA_OWNED)
460
461 #define SFXGE_EVQ_LOCK_INIT(_evq, _ifname, _evq_index)                  \
462         do {                                                            \
463                 struct sfxge_evq  *__evq = (_evq);                      \
464                                                                         \
465                 snprintf((__evq)->lock_name,                            \
466                          sizeof((__evq)->lock_name),                    \
467                          "%s:evq%u", (_ifname), (_evq_index));          \
468                 mtx_init(&(__evq)->lock, (__evq)->lock_name,            \
469                          NULL, MTX_DEF);                                \
470         } while (B_FALSE)
471 #define SFXGE_EVQ_LOCK_DESTROY(_evq)                                    \
472         mtx_destroy(&(_evq)->lock)
473 #define SFXGE_EVQ_LOCK(_evq)                                            \
474         mtx_lock(&(_evq)->lock)
475 #define SFXGE_EVQ_UNLOCK(_evq)                                          \
476         mtx_unlock(&(_evq)->lock)
477 #define SFXGE_EVQ_LOCK_ASSERT_OWNED(_evq)                               \
478         mtx_assert(&(_evq)->lock, MA_OWNED)
479
480 #endif /* _SFXGE_H */