1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2010-2016 Solarflare Communications Inc.
5 * All rights reserved.
6 *
7 * This software was developed in part by Philip Paeps under contract for
8 * Solarflare Communications, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright notice,
14 * this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 * The views and conclusions contained in the software and documentation are
32 * those of the authors and should not be interpreted as representing official
33 * policies, either expressed or implied, of the FreeBSD Project.
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_var.h>
49 #include <net/if_media.h>
50 #include <net/if_types.h>
51
52 #include "sfxge_ioc.h"
53
54 /*
55 * Debugging
56 */
57 #if 0
58 #define DBGPRINT(dev, fmt, args...) \
59 device_printf(dev, "%s: " fmt "\n", __func__, ## args)
60 #else
61 #define DBGPRINT(dev, fmt, args...)
62 #endif
63
64 /*
65 * Backward-compatibility
66 */
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.
70 */
71 #define CACHE_LINE_SIZE 128
72 #endif
73
74 #ifndef IFCAP_LINKSTATE
75 #define IFCAP_LINKSTATE 0
76 #endif
77
78 #ifndef IFCAP_VLAN_HWTSO
79 #define IFCAP_VLAN_HWTSO 0
80 #endif
81
82 #ifndef IFM_10G_T
83 #define IFM_10G_T IFM_UNKNOWN
84 #endif
85
86 #ifndef IFM_10G_KX4
87 #define IFM_10G_KX4 IFM_10G_CX4
88 #endif
89
90 #ifndef IFM_40G_CR4
91 #define IFM_40G_CR4 IFM_UNKNOWN
92 #endif
93
94 #ifdef IFM_ETH_RXPAUSE
95 #define SFXGE_HAVE_PAUSE_MEDIAOPTS
96 #endif
97
98 #ifndef CTLTYPE_U64
99 #define CTLTYPE_U64 CTLTYPE_QUAD
100 #endif
101
102 #include "sfxge_rx.h"
103 #include "sfxge_tx.h"
104
105 #define ROUNDUP_POW_OF_TWO(_n) (1ULL << flsl((_n) - 1))
106
107 #define SFXGE_IP_ALIGN 2
108
109 #define SFXGE_ETHERTYPE_LOOPBACK 0x9000 /* Xerox loopback */
110
111 #define SFXGE_MAGIC_RESERVED 0x8000
112
113 #define SFXGE_MAGIC_DMAQ_LABEL_WIDTH 6
114 #define SFXGE_MAGIC_DMAQ_LABEL_MASK \
115 ((1 << SFXGE_MAGIC_DMAQ_LABEL_WIDTH) - 1)
116
117 enum sfxge_sw_ev {
118 SFXGE_SW_EV_RX_QFLUSH_DONE = 1,
119 SFXGE_SW_EV_RX_QFLUSH_FAILED,
120 SFXGE_SW_EV_RX_QREFILL,
121 SFXGE_SW_EV_TX_QFLUSH_DONE,
122 };
123
124 #define SFXGE_SW_EV_MAGIC(_sw_ev) \
125 (SFXGE_MAGIC_RESERVED | ((_sw_ev) << SFXGE_MAGIC_DMAQ_LABEL_WIDTH))
126
127 static inline uint16_t
sfxge_sw_ev_mk_magic(enum sfxge_sw_ev sw_ev,unsigned int label)128 sfxge_sw_ev_mk_magic(enum sfxge_sw_ev sw_ev, unsigned int label)
129 {
130 KASSERT((label & SFXGE_MAGIC_DMAQ_LABEL_MASK) == label,
131 ("(label & SFXGE_MAGIC_DMAQ_LABEL_MASK) != label"));
132 return SFXGE_SW_EV_MAGIC(sw_ev) | label;
133 }
134
135 static inline uint16_t
sfxge_sw_ev_rxq_magic(enum sfxge_sw_ev sw_ev,struct sfxge_rxq * rxq)136 sfxge_sw_ev_rxq_magic(enum sfxge_sw_ev sw_ev, struct sfxge_rxq *rxq)
137 {
138 return sfxge_sw_ev_mk_magic(sw_ev, 0);
139 }
140
141 static inline uint16_t
sfxge_sw_ev_txq_magic(enum sfxge_sw_ev sw_ev,struct sfxge_txq * txq)142 sfxge_sw_ev_txq_magic(enum sfxge_sw_ev sw_ev, struct sfxge_txq *txq)
143 {
144 return sfxge_sw_ev_mk_magic(sw_ev, txq->type);
145 }
146
147 enum sfxge_evq_state {
148 SFXGE_EVQ_UNINITIALIZED = 0,
149 SFXGE_EVQ_INITIALIZED,
150 SFXGE_EVQ_STARTING,
151 SFXGE_EVQ_STARTED
152 };
153
154 #define SFXGE_EV_BATCH 16384
155
156 #define SFXGE_STATS_UPDATE_PERIOD_MS 1000
157
158 struct sfxge_evq {
159 /* Structure members below are sorted by usage order */
160 struct sfxge_softc *sc;
161 struct mtx lock;
162 unsigned int index;
163 enum sfxge_evq_state init_state;
164 efsys_mem_t mem;
165 efx_evq_t *common;
166 unsigned int read_ptr;
167 boolean_t exception;
168 unsigned int rx_done;
169 unsigned int tx_done;
170
171 /* Linked list of TX queues with completions to process */
172 struct sfxge_txq *txq;
173 struct sfxge_txq **txqs;
174
175 /* Structure members not used on event processing path */
176 unsigned int buf_base_id;
177 unsigned int entries;
178 char lock_name[SFXGE_LOCK_NAME_MAX];
179 #if EFSYS_OPT_QSTATS
180 clock_t stats_update_time;
181 uint64_t stats[EV_NQSTATS];
182 #endif
183 } __aligned(CACHE_LINE_SIZE);
184
185 #define SFXGE_NDESCS 1024
186 #define SFXGE_MODERATION 30
187
188 enum sfxge_intr_state {
189 SFXGE_INTR_UNINITIALIZED = 0,
190 SFXGE_INTR_INITIALIZED,
191 SFXGE_INTR_TESTING,
192 SFXGE_INTR_STARTED
193 };
194
195 struct sfxge_intr_hdl {
196 int eih_rid;
197 void *eih_tag;
198 struct resource *eih_res;
199 };
200
201 struct sfxge_intr {
202 enum sfxge_intr_state state;
203 struct resource *msix_res;
204 struct sfxge_intr_hdl *table;
205 int n_alloc;
206 int type;
207 efsys_mem_t status;
208 uint32_t zero_count;
209 };
210
211 enum sfxge_mcdi_state {
212 SFXGE_MCDI_UNINITIALIZED = 0,
213 SFXGE_MCDI_INITIALIZED,
214 SFXGE_MCDI_BUSY,
215 SFXGE_MCDI_COMPLETED
216 };
217
218 struct sfxge_mcdi {
219 struct mtx lock;
220 efsys_mem_t mem;
221 enum sfxge_mcdi_state state;
222 efx_mcdi_transport_t transport;
223
224 /* Only used in debugging output */
225 char lock_name[SFXGE_LOCK_NAME_MAX];
226 };
227
228 struct sfxge_hw_stats {
229 clock_t update_time;
230 efsys_mem_t dma_buf;
231 void *decode_buf;
232 };
233
234 enum sfxge_port_state {
235 SFXGE_PORT_UNINITIALIZED = 0,
236 SFXGE_PORT_INITIALIZED,
237 SFXGE_PORT_STARTED
238 };
239
240 struct sfxge_port {
241 struct sfxge_softc *sc;
242 struct mtx lock;
243 enum sfxge_port_state init_state;
244 #ifndef SFXGE_HAVE_PAUSE_MEDIAOPTS
245 unsigned int wanted_fc;
246 #endif
247 struct sfxge_hw_stats phy_stats;
248 struct sfxge_hw_stats mac_stats;
249 uint16_t stats_update_period_ms;
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 if_t ifnet;
272 unsigned int if_flags;
273 struct sysctl_oid *stats_node;
274 #if EFSYS_OPT_QSTATS
275 struct sysctl_oid *evqs_stats_node;
276 #endif
277 struct sysctl_oid *txqs_node;
278
279 struct task task_reset;
280
281 efx_family_t family;
282 unsigned int mem_bar;
283
284 caddr_t vpd_data;
285 size_t vpd_size;
286 efx_nic_t *enp;
287 efsys_lock_t enp_lock;
288
289 boolean_t txq_dynamic_cksum_toggle_supported;
290
291 unsigned int rxq_entries;
292 unsigned int txq_entries;
293
294 bus_dma_tag_t parent_dma_tag;
295 efsys_bar_t bar;
296
297 struct sfxge_intr intr;
298 struct sfxge_mcdi mcdi;
299 struct sfxge_port port;
300 uint32_t buffer_table_next;
301
302 struct sfxge_evq *evq[SFXGE_RX_SCALE_MAX];
303 unsigned int ev_moderation;
304 #if EFSYS_OPT_QSTATS
305 clock_t ev_stats_update_time;
306 uint64_t ev_stats[EV_NQSTATS];
307 #endif
308
309 unsigned int max_rss_channels;
310 struct sfxge_rxq *rxq[SFXGE_RX_SCALE_MAX];
311 unsigned int rx_indir_table[EFX_RSS_TBL_SIZE];
312
313 struct sfxge_txq *txq[SFXGE_TXQ_NTYPES + SFXGE_RX_SCALE_MAX];
314
315 struct ifmedia media;
316
317 size_t rx_prefix_size;
318 size_t rx_buffer_size;
319 size_t rx_buffer_align;
320 int rx_cluster_size;
321
322 unsigned int evq_max;
323 unsigned int evq_count;
324 unsigned int rxq_count;
325 unsigned int txq_count;
326
327 unsigned int tso_fw_assisted;
328 #define SFXGE_FATSOV1 (1 << 0)
329 #define SFXGE_FATSOV2 (1 << 1)
330
331 #if EFSYS_OPT_MCDI_LOGGING
332 int mcdi_logging;
333 #endif
334 };
335
336 #define SFXGE_LINK_UP(sc) \
337 ((sc)->port.link_mode != EFX_LINK_DOWN && \
338 (sc)->port.link_mode != EFX_LINK_UNKNOWN)
339 #define SFXGE_RUNNING(sc) (if_getdrvflags((sc)->ifnet) & IFF_DRV_RUNNING)
340
341 #define SFXGE_PARAM(_name) "hw.sfxge." #_name
342
343 SYSCTL_DECL(_hw_sfxge);
344
345 /*
346 * From sfxge.c.
347 */
348 extern void sfxge_schedule_reset(struct sfxge_softc *sc);
349 extern void sfxge_sram_buf_tbl_alloc(struct sfxge_softc *sc, size_t n,
350 uint32_t *idp);
351
352 /*
353 * From sfxge_dma.c.
354 */
355 extern int sfxge_dma_init(struct sfxge_softc *sc);
356 extern void sfxge_dma_fini(struct sfxge_softc *sc);
357 extern int sfxge_dma_alloc(struct sfxge_softc *sc, bus_size_t len,
358 efsys_mem_t *esmp);
359 extern void sfxge_dma_free(efsys_mem_t *esmp);
360 extern int sfxge_dma_map_sg_collapse(bus_dma_tag_t tag, bus_dmamap_t map,
361 struct mbuf **mp,
362 bus_dma_segment_t *segs,
363 int *nsegs, int maxsegs);
364
365 /*
366 * From sfxge_ev.c.
367 */
368 extern int sfxge_ev_init(struct sfxge_softc *sc);
369 extern void sfxge_ev_fini(struct sfxge_softc *sc);
370 extern int sfxge_ev_start(struct sfxge_softc *sc);
371 extern void sfxge_ev_stop(struct sfxge_softc *sc);
372 extern int sfxge_ev_qpoll(struct sfxge_evq *evq);
373
374 /*
375 * From sfxge_intr.c.
376 */
377 extern int sfxge_intr_init(struct sfxge_softc *sc);
378 extern void sfxge_intr_fini(struct sfxge_softc *sc);
379 extern int sfxge_intr_start(struct sfxge_softc *sc);
380 extern void sfxge_intr_stop(struct sfxge_softc *sc);
381
382 /*
383 * From sfxge_mcdi.c.
384 */
385 extern int sfxge_mcdi_init(struct sfxge_softc *sc);
386 extern void sfxge_mcdi_fini(struct sfxge_softc *sc);
387 extern int sfxge_mcdi_ioctl(struct sfxge_softc *sc, sfxge_ioc_t *ip);
388
389 /*
390 * From sfxge_nvram.c.
391 */
392 extern int sfxge_nvram_ioctl(struct sfxge_softc *sc, sfxge_ioc_t *ip);
393
394 /*
395 * From sfxge_port.c.
396 */
397 extern int sfxge_port_init(struct sfxge_softc *sc);
398 extern void sfxge_port_fini(struct sfxge_softc *sc);
399 extern int sfxge_port_start(struct sfxge_softc *sc);
400 extern void sfxge_port_stop(struct sfxge_softc *sc);
401 extern void sfxge_mac_link_update(struct sfxge_softc *sc,
402 efx_link_mode_t mode);
403 extern int sfxge_mac_filter_set(struct sfxge_softc *sc);
404 extern int sfxge_port_ifmedia_init(struct sfxge_softc *sc);
405 extern uint64_t sfxge_get_counter(if_t ifp, ift_counter c);
406
407 #define SFXGE_MAX_MTU (9 * 1024)
408
409 #define SFXGE_ADAPTER_LOCK_INIT(_sc, _ifname) \
410 do { \
411 struct sfxge_softc *__sc = (_sc); \
412 \
413 snprintf((__sc)->softc_lock_name, \
414 sizeof((__sc)->softc_lock_name), \
415 "%s:softc", (_ifname)); \
416 sx_init(&(__sc)->softc_lock, (__sc)->softc_lock_name); \
417 } while (B_FALSE)
418 #define SFXGE_ADAPTER_LOCK_DESTROY(_sc) \
419 sx_destroy(&(_sc)->softc_lock)
420 #define SFXGE_ADAPTER_LOCK(_sc) \
421 sx_xlock(&(_sc)->softc_lock)
422 #define SFXGE_ADAPTER_UNLOCK(_sc) \
423 sx_xunlock(&(_sc)->softc_lock)
424 #define SFXGE_ADAPTER_LOCK_ASSERT_OWNED(_sc) \
425 sx_assert(&(_sc)->softc_lock, LA_XLOCKED)
426
427 #define SFXGE_PORT_LOCK_INIT(_port, _ifname) \
428 do { \
429 struct sfxge_port *__port = (_port); \
430 \
431 snprintf((__port)->lock_name, \
432 sizeof((__port)->lock_name), \
433 "%s:port", (_ifname)); \
434 mtx_init(&(__port)->lock, (__port)->lock_name, \
435 NULL, MTX_DEF); \
436 } while (B_FALSE)
437 #define SFXGE_PORT_LOCK_DESTROY(_port) \
438 mtx_destroy(&(_port)->lock)
439 #define SFXGE_PORT_LOCK(_port) \
440 mtx_lock(&(_port)->lock)
441 #define SFXGE_PORT_UNLOCK(_port) \
442 mtx_unlock(&(_port)->lock)
443 #define SFXGE_PORT_LOCK_ASSERT_OWNED(_port) \
444 mtx_assert(&(_port)->lock, MA_OWNED)
445
446 #define SFXGE_MCDI_LOCK_INIT(_mcdi, _ifname) \
447 do { \
448 struct sfxge_mcdi *__mcdi = (_mcdi); \
449 \
450 snprintf((__mcdi)->lock_name, \
451 sizeof((__mcdi)->lock_name), \
452 "%s:mcdi", (_ifname)); \
453 mtx_init(&(__mcdi)->lock, (__mcdi)->lock_name, \
454 NULL, MTX_DEF); \
455 } while (B_FALSE)
456 #define SFXGE_MCDI_LOCK_DESTROY(_mcdi) \
457 mtx_destroy(&(_mcdi)->lock)
458 #define SFXGE_MCDI_LOCK(_mcdi) \
459 mtx_lock(&(_mcdi)->lock)
460 #define SFXGE_MCDI_UNLOCK(_mcdi) \
461 mtx_unlock(&(_mcdi)->lock)
462 #define SFXGE_MCDI_LOCK_ASSERT_OWNED(_mcdi) \
463 mtx_assert(&(_mcdi)->lock, MA_OWNED)
464
465 #define SFXGE_EVQ_LOCK_INIT(_evq, _ifname, _evq_index) \
466 do { \
467 struct sfxge_evq *__evq = (_evq); \
468 \
469 snprintf((__evq)->lock_name, \
470 sizeof((__evq)->lock_name), \
471 "%s:evq%u", (_ifname), (_evq_index)); \
472 mtx_init(&(__evq)->lock, (__evq)->lock_name, \
473 NULL, MTX_DEF); \
474 } while (B_FALSE)
475 #define SFXGE_EVQ_LOCK_DESTROY(_evq) \
476 mtx_destroy(&(_evq)->lock)
477 #define SFXGE_EVQ_LOCK(_evq) \
478 mtx_lock(&(_evq)->lock)
479 #define SFXGE_EVQ_UNLOCK(_evq) \
480 mtx_unlock(&(_evq)->lock)
481 #define SFXGE_EVQ_LOCK_ASSERT_OWNED(_evq) \
482 mtx_assert(&(_evq)->lock, MA_OWNED)
483
484 #endif /* _SFXGE_H */
485