2 * Copyright (c) 2010-2016 Solarflare Communications Inc.
5 * This software was developed in part by Philip Paeps under contract for
6 * Solarflare Communications, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
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.
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.
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.
39 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/socket.h>
42 #include <sys/sysctl.h>
46 #include <net/ethernet.h>
48 #include <net/if_var.h>
49 #include <net/if_media.h>
50 #include <net/if_types.h>
52 #include "sfxge_ioc.h"
58 #define DBGPRINT(dev, fmt, args...) \
59 device_printf(dev, "%s: " fmt "\n", __func__, ## args)
61 #define DBGPRINT(dev, fmt, args...)
65 * Backward-compatibility
67 #ifndef CACHE_LINE_SIZE
68 /* This should be right on most machines the driver will be used on, and
69 * we needn't care too much about wasting a few KB per interface.
71 #define CACHE_LINE_SIZE 128
74 #ifndef IFCAP_LINKSTATE
75 #define IFCAP_LINKSTATE 0
78 #ifndef IFCAP_VLAN_HWTSO
79 #define IFCAP_VLAN_HWTSO 0
83 #define IFM_10G_T IFM_UNKNOWN
87 #define IFM_10G_KX4 IFM_10G_CX4
91 #define IFM_40G_CR4 IFM_UNKNOWN
94 #if (__FreeBSD_version >= 800501 && __FreeBSD_version < 900000) || \
95 __FreeBSD_version >= 900003
96 #define SFXGE_HAVE_DESCRIBE_INTR
99 #ifdef IFM_ETH_RXPAUSE
100 #define SFXGE_HAVE_PAUSE_MEDIAOPTS
104 #define CTLTYPE_U64 CTLTYPE_QUAD
107 #include "sfxge_rx.h"
108 #include "sfxge_tx.h"
110 #define ROUNDUP_POW_OF_TWO(_n) (1ULL << flsl((_n) - 1))
112 #define SFXGE_IP_ALIGN 2
114 #define SFXGE_ETHERTYPE_LOOPBACK 0x9000 /* Xerox loopback */
117 #define SFXGE_MAGIC_RESERVED 0x8000
119 #define SFXGE_MAGIC_DMAQ_LABEL_WIDTH 6
120 #define SFXGE_MAGIC_DMAQ_LABEL_MASK \
121 ((1 << SFXGE_MAGIC_DMAQ_LABEL_WIDTH) - 1)
124 SFXGE_SW_EV_RX_QFLUSH_DONE = 1,
125 SFXGE_SW_EV_RX_QFLUSH_FAILED,
126 SFXGE_SW_EV_RX_QREFILL,
127 SFXGE_SW_EV_TX_QFLUSH_DONE,
130 #define SFXGE_SW_EV_MAGIC(_sw_ev) \
131 (SFXGE_MAGIC_RESERVED | ((_sw_ev) << SFXGE_MAGIC_DMAQ_LABEL_WIDTH))
133 static inline uint16_t
134 sfxge_sw_ev_mk_magic(enum sfxge_sw_ev sw_ev, unsigned int label)
136 KASSERT((label & SFXGE_MAGIC_DMAQ_LABEL_MASK) == label,
137 ("(label & SFXGE_MAGIC_DMAQ_LABEL_MASK) != label"));
138 return SFXGE_SW_EV_MAGIC(sw_ev) | label;
141 static inline uint16_t
142 sfxge_sw_ev_rxq_magic(enum sfxge_sw_ev sw_ev, struct sfxge_rxq *rxq)
144 return sfxge_sw_ev_mk_magic(sw_ev, 0);
147 static inline uint16_t
148 sfxge_sw_ev_txq_magic(enum sfxge_sw_ev sw_ev, struct sfxge_txq *txq)
150 return sfxge_sw_ev_mk_magic(sw_ev, txq->type);
153 enum sfxge_evq_state {
154 SFXGE_EVQ_UNINITIALIZED = 0,
155 SFXGE_EVQ_INITIALIZED,
160 #define SFXGE_EV_BATCH 16384
162 #define SFXGE_STATS_UPDATE_PERIOD_MS 1000
165 /* Structure members below are sorted by usage order */
166 struct sfxge_softc *sc;
169 enum sfxge_evq_state init_state;
172 unsigned int read_ptr;
174 unsigned int rx_done;
175 unsigned int tx_done;
177 /* Linked list of TX queues with completions to process */
178 struct sfxge_txq *txq;
179 struct sfxge_txq **txqs;
181 /* Structure members not used on event processing path */
182 unsigned int buf_base_id;
183 unsigned int entries;
184 char lock_name[SFXGE_LOCK_NAME_MAX];
186 clock_t stats_update_time;
187 uint64_t stats[EV_NQSTATS];
189 } __aligned(CACHE_LINE_SIZE);
191 #define SFXGE_NDESCS 1024
192 #define SFXGE_MODERATION 30
194 enum sfxge_intr_state {
195 SFXGE_INTR_UNINITIALIZED = 0,
196 SFXGE_INTR_INITIALIZED,
201 struct sfxge_intr_hdl {
204 struct resource *eih_res;
208 enum sfxge_intr_state state;
209 struct resource *msix_res;
210 struct sfxge_intr_hdl *table;
217 enum sfxge_mcdi_state {
218 SFXGE_MCDI_UNINITIALIZED = 0,
219 SFXGE_MCDI_INITIALIZED,
227 enum sfxge_mcdi_state state;
228 efx_mcdi_transport_t transport;
230 /* Only used in debugging output */
231 char lock_name[SFXGE_LOCK_NAME_MAX];
234 struct sfxge_hw_stats {
240 enum sfxge_port_state {
241 SFXGE_PORT_UNINITIALIZED = 0,
242 SFXGE_PORT_INITIALIZED,
247 struct sfxge_softc *sc;
249 enum sfxge_port_state init_state;
250 #ifndef SFXGE_HAVE_PAUSE_MEDIAOPTS
251 unsigned int wanted_fc;
253 struct sfxge_hw_stats phy_stats;
254 struct sfxge_hw_stats mac_stats;
255 uint16_t stats_update_period_ms;
256 efx_link_mode_t link_mode;
257 uint8_t mcast_addrs[EFX_MAC_MULTICAST_LIST_MAX *
259 unsigned int mcast_count;
261 /* Only used in debugging output */
262 char lock_name[SFXGE_LOCK_NAME_MAX];
265 enum sfxge_softc_state {
266 SFXGE_UNINITIALIZED = 0,
274 struct sx softc_lock;
275 char softc_lock_name[SFXGE_LOCK_NAME_MAX];
276 enum sfxge_softc_state init_state;
278 unsigned int if_flags;
279 struct sysctl_oid *stats_node;
281 struct sysctl_oid *evqs_stats_node;
283 struct sysctl_oid *txqs_node;
285 struct task task_reset;
291 efsys_lock_t enp_lock;
293 boolean_t txq_dynamic_cksum_toggle_supported;
295 unsigned int rxq_entries;
296 unsigned int txq_entries;
298 bus_dma_tag_t parent_dma_tag;
301 struct sfxge_intr intr;
302 struct sfxge_mcdi mcdi;
303 struct sfxge_port port;
304 uint32_t buffer_table_next;
306 struct sfxge_evq *evq[SFXGE_RX_SCALE_MAX];
307 unsigned int ev_moderation;
309 clock_t ev_stats_update_time;
310 uint64_t ev_stats[EV_NQSTATS];
313 unsigned int max_rss_channels;
314 struct sfxge_rxq *rxq[SFXGE_RX_SCALE_MAX];
315 unsigned int rx_indir_table[EFX_RSS_TBL_SIZE];
317 struct sfxge_txq *txq[SFXGE_TXQ_NTYPES + SFXGE_RX_SCALE_MAX];
319 struct ifmedia media;
321 size_t rx_prefix_size;
322 size_t rx_buffer_size;
323 size_t rx_buffer_align;
326 unsigned int evq_max;
327 unsigned int evq_count;
328 unsigned int rxq_count;
329 unsigned int txq_count;
331 unsigned int tso_fw_assisted;
332 #define SFXGE_FATSOV1 (1 << 0)
333 #define SFXGE_FATSOV2 (1 << 1)
335 #if EFSYS_OPT_MCDI_LOGGING
340 #define SFXGE_LINK_UP(sc) \
341 ((sc)->port.link_mode != EFX_LINK_DOWN && \
342 (sc)->port.link_mode != EFX_LINK_UNKNOWN)
343 #define SFXGE_RUNNING(sc) ((sc)->ifnet->if_drv_flags & IFF_DRV_RUNNING)
345 #define SFXGE_PARAM(_name) "hw.sfxge." #_name
347 SYSCTL_DECL(_hw_sfxge);
352 extern void sfxge_schedule_reset(struct sfxge_softc *sc);
353 extern void sfxge_sram_buf_tbl_alloc(struct sfxge_softc *sc, size_t n,
359 extern int sfxge_dma_init(struct sfxge_softc *sc);
360 extern void sfxge_dma_fini(struct sfxge_softc *sc);
361 extern int sfxge_dma_alloc(struct sfxge_softc *sc, bus_size_t len,
363 extern void sfxge_dma_free(efsys_mem_t *esmp);
364 extern int sfxge_dma_map_sg_collapse(bus_dma_tag_t tag, bus_dmamap_t map,
366 bus_dma_segment_t *segs,
367 int *nsegs, int maxsegs);
372 extern int sfxge_ev_init(struct sfxge_softc *sc);
373 extern void sfxge_ev_fini(struct sfxge_softc *sc);
374 extern int sfxge_ev_start(struct sfxge_softc *sc);
375 extern void sfxge_ev_stop(struct sfxge_softc *sc);
376 extern int sfxge_ev_qpoll(struct sfxge_evq *evq);
381 extern int sfxge_intr_init(struct sfxge_softc *sc);
382 extern void sfxge_intr_fini(struct sfxge_softc *sc);
383 extern int sfxge_intr_start(struct sfxge_softc *sc);
384 extern void sfxge_intr_stop(struct sfxge_softc *sc);
389 extern int sfxge_mcdi_init(struct sfxge_softc *sc);
390 extern void sfxge_mcdi_fini(struct sfxge_softc *sc);
391 extern int sfxge_mcdi_ioctl(struct sfxge_softc *sc, sfxge_ioc_t *ip);
394 * From sfxge_nvram.c.
396 extern int sfxge_nvram_ioctl(struct sfxge_softc *sc, sfxge_ioc_t *ip);
401 extern int sfxge_port_init(struct sfxge_softc *sc);
402 extern void sfxge_port_fini(struct sfxge_softc *sc);
403 extern int sfxge_port_start(struct sfxge_softc *sc);
404 extern void sfxge_port_stop(struct sfxge_softc *sc);
405 extern void sfxge_mac_link_update(struct sfxge_softc *sc,
406 efx_link_mode_t mode);
407 extern int sfxge_mac_filter_set(struct sfxge_softc *sc);
408 extern int sfxge_port_ifmedia_init(struct sfxge_softc *sc);
409 extern uint64_t sfxge_get_counter(struct ifnet *ifp, ift_counter c);
411 #define SFXGE_MAX_MTU (9 * 1024)
413 #define SFXGE_ADAPTER_LOCK_INIT(_sc, _ifname) \
415 struct sfxge_softc *__sc = (_sc); \
417 snprintf((__sc)->softc_lock_name, \
418 sizeof((__sc)->softc_lock_name), \
419 "%s:softc", (_ifname)); \
420 sx_init(&(__sc)->softc_lock, (__sc)->softc_lock_name); \
422 #define SFXGE_ADAPTER_LOCK_DESTROY(_sc) \
423 sx_destroy(&(_sc)->softc_lock)
424 #define SFXGE_ADAPTER_LOCK(_sc) \
425 sx_xlock(&(_sc)->softc_lock)
426 #define SFXGE_ADAPTER_UNLOCK(_sc) \
427 sx_xunlock(&(_sc)->softc_lock)
428 #define SFXGE_ADAPTER_LOCK_ASSERT_OWNED(_sc) \
429 sx_assert(&(_sc)->softc_lock, LA_XLOCKED)
431 #define SFXGE_PORT_LOCK_INIT(_port, _ifname) \
433 struct sfxge_port *__port = (_port); \
435 snprintf((__port)->lock_name, \
436 sizeof((__port)->lock_name), \
437 "%s:port", (_ifname)); \
438 mtx_init(&(__port)->lock, (__port)->lock_name, \
441 #define SFXGE_PORT_LOCK_DESTROY(_port) \
442 mtx_destroy(&(_port)->lock)
443 #define SFXGE_PORT_LOCK(_port) \
444 mtx_lock(&(_port)->lock)
445 #define SFXGE_PORT_UNLOCK(_port) \
446 mtx_unlock(&(_port)->lock)
447 #define SFXGE_PORT_LOCK_ASSERT_OWNED(_port) \
448 mtx_assert(&(_port)->lock, MA_OWNED)
450 #define SFXGE_MCDI_LOCK_INIT(_mcdi, _ifname) \
452 struct sfxge_mcdi *__mcdi = (_mcdi); \
454 snprintf((__mcdi)->lock_name, \
455 sizeof((__mcdi)->lock_name), \
456 "%s:mcdi", (_ifname)); \
457 mtx_init(&(__mcdi)->lock, (__mcdi)->lock_name, \
460 #define SFXGE_MCDI_LOCK_DESTROY(_mcdi) \
461 mtx_destroy(&(_mcdi)->lock)
462 #define SFXGE_MCDI_LOCK(_mcdi) \
463 mtx_lock(&(_mcdi)->lock)
464 #define SFXGE_MCDI_UNLOCK(_mcdi) \
465 mtx_unlock(&(_mcdi)->lock)
466 #define SFXGE_MCDI_LOCK_ASSERT_OWNED(_mcdi) \
467 mtx_assert(&(_mcdi)->lock, MA_OWNED)
469 #define SFXGE_EVQ_LOCK_INIT(_evq, _ifname, _evq_index) \
471 struct sfxge_evq *__evq = (_evq); \
473 snprintf((__evq)->lock_name, \
474 sizeof((__evq)->lock_name), \
475 "%s:evq%u", (_ifname), (_evq_index)); \
476 mtx_init(&(__evq)->lock, (__evq)->lock_name, \
479 #define SFXGE_EVQ_LOCK_DESTROY(_evq) \
480 mtx_destroy(&(_evq)->lock)
481 #define SFXGE_EVQ_LOCK(_evq) \
482 mtx_lock(&(_evq)->lock)
483 #define SFXGE_EVQ_UNLOCK(_evq) \
484 mtx_unlock(&(_evq)->lock)
485 #define SFXGE_EVQ_LOCK_ASSERT_OWNED(_evq) \
486 mtx_assert(&(_evq)->lock, MA_OWNED)
488 #endif /* _SFXGE_H */