1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source. A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * This file is part of the Chelsio T4 support code.
14  *
15  * Copyright (C) 2011-2013 Chelsio Communications.  All rights reserved.
16  *
17  * This program is distributed in the hope that it will be useful, but WITHOUT
18  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19  * FITNESS FOR A PARTICULAR PURPOSE.  See the LICENSE file included in this
20  * release for licensing terms and conditions.
21  */
22 
23 #ifndef __CXGBE_ADAPTER_H
24 #define	__CXGBE_ADAPTER_H
25 
26 #include <sys/ddi.h>
27 #include <sys/mac_provider.h>
28 #include <sys/ethernet.h>
29 #include <sys/queue.h>
30 
31 #include "offload.h"
32 #include "firmware/t4fw_interface.h"
33 #include "shared.h"
34 
35 struct adapter;
36 typedef struct adapter adapter_t;
37 
38 enum {
39 	FW_IQ_QSIZE = 256,
40 	FW_IQ_ESIZE = 64,	/* At least 64 mandated by the firmware spec */
41 
42 	RX_IQ_QSIZE = 1024,
43 	RX_IQ_ESIZE = 64,	/* At least 64 so CPL_RX_PKT will fit */
44 
45 	EQ_ESIZE = 64,		/* All egres queues use this entry size */
46 
47 	RX_FL_ESIZE = 64,	/* 8 64bit addresses */
48 
49 	FL_BUF_SIZES = 4,
50 
51 	CTRL_EQ_QSIZE = 128,
52 
53 	TX_EQ_QSIZE = 1024,
54 	TX_SGL_SEGS = 36,
55 	TX_WR_FLITS = SGE_MAX_WR_LEN / 8
56 };
57 
58 enum {
59 	/* adapter flags */
60 	FULL_INIT_DONE	= (1 << 0),
61 	FW_OK		= (1 << 1),
62 	INTR_FWD	= (1 << 2),
63 	INTR_ALLOCATED	= (1 << 3),
64 	MASTER_PF	= (1 << 4),
65 
66 	CXGBE_BUSY	= (1 << 9),
67 
68 	/* port flags */
69 	DOOMED		= (1 << 0),
70 	PORT_INIT_DONE	= (1 << 1),
71 };
72 
73 enum {
74 	/* Features */
75 	CXGBE_HW_LSO	= (1 << 0),
76 	CXGBE_HW_CSUM	= (1 << 1),
77 };
78 
79 enum {
80 	UDBS_SEG_SHIFT	= 7,	/* log2(UDBS_SEG_SIZE) */
81 	UDBS_DB_OFFSET	= 8,	/* offset of the 4B doorbell in a segment */
82 	UDBS_WR_OFFSET	= 64,	/* offset of the work request in a segment */
83 };
84 
85 #define	IS_DOOMED(pi)	(pi->flags & DOOMED)
86 #define	SET_DOOMED(pi)	do { pi->flags |= DOOMED; } while (0)
87 #define	IS_BUSY(sc)	(sc->flags & CXGBE_BUSY)
88 #define	SET_BUSY(sc)	do { sc->flags |= CXGBE_BUSY; } while (0)
89 #define	CLR_BUSY(sc)	do { sc->flags &= ~CXGBE_BUSY; } while (0)
90 
91 struct port_info {
92 	PORT_INFO_HDR;
93 
94 	kmutex_t lock;
95 	struct adapter *adapter;
96 
97 #ifdef TCP_OFFLOAD_ENABLE
98 	void *tdev;
99 #endif
100 
101 	unsigned int flags;
102 
103 	uint16_t viid;
104 	int16_t  xact_addr_filt; /* index of exact MAC address filter */
105 	uint16_t rss_size;	/* size of VI's RSS table slice */
106 	uint16_t ntxq;		/* # of tx queues */
107 	uint16_t first_txq;	/* index of first tx queue */
108 	uint16_t nrxq;		/* # of rx queues */
109 	uint16_t first_rxq;	/* index of first rx queue */
110 #ifdef TCP_OFFLOAD_ENABLE
111 	uint16_t nofldtxq;		/* # of offload tx queues */
112 	uint16_t first_ofld_txq;	/* index of first offload tx queue */
113 	uint16_t nofldrxq;		/* # of offload rx queues */
114 	uint16_t first_ofld_rxq;	/* index of first offload rx queue */
115 #endif
116 	uint8_t  lport;		/* associated offload logical port */
117 	int8_t   mdio_addr;
118 	uint8_t  port_type;
119 	uint8_t  mod_type;
120 	uint8_t  port_id;
121 	uint8_t  tx_chan;
122 	uint8_t  rx_chan;
123 	uint8_t instance; /* Associated adapter instance */
124 	uint8_t child_inst; /* Associated child instance */
125 	uint8_t	tmr_idx;
126 	int8_t	pktc_idx;
127 	struct link_config link_cfg;
128 	struct port_stats stats;
129 	uint32_t features;
130 	uint8_t macaddr_cnt;
131 	u8 rss_mode;
132 	u16 viid_mirror;
133 	kstat_t *ksp_config;
134 	kstat_t *ksp_info;
135 };
136 
137 struct fl_sdesc {
138 	struct rxbuf *rxb;
139 };
140 
141 struct tx_desc {
142 	__be64 flit[8];
143 };
144 
145 /* DMA maps used for tx */
146 struct tx_maps {
147 	ddi_dma_handle_t *map;
148 	uint32_t map_total;	/* # of DMA maps */
149 	uint32_t map_pidx;	/* next map to be used */
150 	uint32_t map_cidx;	/* reclaimed up to this index */
151 	uint32_t map_avail;	/* # of available maps */
152 };
153 
154 struct tx_sdesc {
155 	mblk_t *m;
156 	uint32_t txb_used;	/* # of bytes of tx copy buffer used */
157 	uint16_t hdls_used;	/* # of dma handles used */
158 	uint16_t desc_used;	/* # of hardware descriptors used */
159 };
160 
161 enum {
162 	/* iq flags */
163 	IQ_ALLOCATED	= (1 << 0),	/* firmware resources allocated */
164 	IQ_INTR		= (1 << 1),	/* iq takes direct interrupt */
165 	IQ_HAS_FL	= (1 << 2),	/* iq has fl */
166 
167 	/* iq state */
168 	IQS_DISABLED	= 0,
169 	IQS_BUSY	= 1,
170 	IQS_IDLE	= 2,
171 };
172 
173 /*
174  * Ingress Queue: T4 is producer, driver is consumer.
175  */
176 struct sge_iq {
177 	unsigned int flags;
178 	ddi_dma_handle_t dhdl;
179 	ddi_acc_handle_t ahdl;
180 
181 	volatile uint_t state;
182 	__be64 *desc;		/* KVA of descriptor ring */
183 	uint64_t ba;		/* bus address of descriptor ring */
184 	const __be64 *cdesc;	/* current descriptor */
185 	struct adapter *adapter; /* associated  adapter */
186 	uint8_t  gen;		/* generation bit */
187 	uint8_t  intr_params;	/* interrupt holdoff parameters */
188 	int8_t   intr_pktc_idx;	/* packet count threshold index */
189 	uint8_t  intr_next;	/* holdoff for next interrupt */
190 	uint8_t  esize;		/* size (bytes) of each entry in the queue */
191 	uint16_t qsize;		/* size (# of entries) of the queue */
192 	uint16_t cidx;		/* consumer index */
193 	uint16_t pending;	/* # of descs processed since last doorbell */
194 	uint16_t cntxt_id;	/* SGE context id  for the iq */
195 	uint16_t abs_id;	/* absolute SGE id for the iq */
196 	kmutex_t lock;		/* Rx access lock */
197 	uint8_t polling;
198 
199 	STAILQ_ENTRY(sge_iq) link;
200 };
201 
202 enum {
203 	EQ_CTRL		= 1,
204 	EQ_ETH		= 2,
205 #ifdef TCP_OFFLOAD_ENABLE
206 	EQ_OFLD		= 3,
207 #endif
208 
209 	/* eq flags */
210 	EQ_TYPEMASK	= 7,		/* 3 lsbits hold the type */
211 	EQ_ALLOCATED	= (1 << 3),	/* firmware resources allocated */
212 	EQ_DOOMED	= (1 << 4),	/* about to be destroyed */
213 	EQ_CRFLUSHED	= (1 << 5),	/* expecting an update from SGE */
214 	EQ_STALLED	= (1 << 6),	/* out of hw descriptors or dmamaps */
215 	EQ_MTX		= (1 << 7),	/* mutex has been initialized */
216 	EQ_STARTED	= (1 << 8),	/* started */
217 };
218 
219 /* Listed in order of preference.  Update t4_sysctls too if you change these */
220 enum {DOORBELL_UDB=0x1 , DOORBELL_WCWR=0x2, DOORBELL_UDBWC=0x4, DOORBELL_KDB=0x8};
221 
222 /*
223  * Egress Queue: driver is producer, T4 is consumer.
224  *
225  * Note: A free list is an egress queue (driver produces the buffers and T4
226  * consumes them) but it's special enough to have its own struct (see sge_fl).
227  */
228 struct sge_eq {
229 	ddi_dma_handle_t desc_dhdl;
230 	ddi_acc_handle_t desc_ahdl;
231 	unsigned int flags;
232 	kmutex_t lock;
233 
234 	struct tx_desc *desc;	/* KVA of descriptor ring */
235 	uint64_t ba;		/* bus address of descriptor ring */
236 	struct sge_qstat *spg;	/* status page, for convenience */
237 	int doorbells;
238 	volatile uint32_t *udb; /* KVA of doorbell (lies within BAR2) */
239 	u_int udb_qid;		/* relative qid within the doorbell page */
240 	uint16_t cap;		/* max # of desc, for convenience */
241 	uint16_t avail;		/* available descriptors, for convenience */
242 	uint16_t qsize;		/* size (# of entries) of the queue */
243 	uint16_t cidx;		/* consumer idx (desc idx) */
244 	uint16_t pidx;		/* producer idx (desc idx) */
245 	uint16_t pending;	/* # of descriptors used since last doorbell */
246 	uint16_t iqid;		/* iq that gets egr_update for the eq */
247 	uint8_t tx_chan;	/* tx channel used by the eq */
248 	uint32_t cntxt_id;	/* SGE context id for the eq */
249 };
250 
251 enum {
252 	/* fl flags */
253 	FL_MTX		= (1 << 0),	/* mutex has been initialized */
254 	FL_STARVING	= (1 << 1),	/* on the list of starving fl's */
255 	FL_DOOMED	= (1 << 2),	/* about to be destroyed */
256 };
257 
258 #define	FL_RUNNING_LOW(fl)	(fl->cap - fl->needed <= fl->lowat)
259 #define	FL_NOT_RUNNING_LOW(fl)	(fl->cap - fl->needed >= 2 * fl->lowat)
260 
261 struct sge_fl {
262 	unsigned int flags;
263 	kmutex_t lock;
264 	ddi_dma_handle_t dhdl;
265 	ddi_acc_handle_t ahdl;
266 
267 	__be64 *desc;		/* KVA of descriptor ring, ptr to addresses */
268 	uint64_t ba;		/* bus address of descriptor ring */
269 	struct fl_sdesc *sdesc;	/* KVA of software descriptor ring */
270 	uint32_t cap;		/* max # of buffers, for convenience */
271 	uint16_t qsize;		/* size (# of entries) of the queue */
272 	uint16_t cntxt_id;	/* SGE context id for the freelist */
273 	uint32_t cidx;		/* consumer idx (buffer idx, NOT hw desc idx) */
274 	uint32_t pidx;		/* producer idx (buffer idx, NOT hw desc idx) */
275 	uint32_t needed;	/* # of buffers needed to fill up fl. */
276 	uint32_t lowat;		/* # of buffers <= this means fl needs help */
277 	uint32_t pending;	/* # of bufs allocated since last doorbell */
278 	uint32_t offset;	/* current packet within the larger buffer */
279 	uint16_t copy_threshold; /* anything this size or less is copied up */
280 
281 	uint64_t copied_up;	/* # of frames copied into mblk and handed up */
282 	uint64_t passed_up;	/* # of frames wrapped in mblk and handed up */
283 
284 	TAILQ_ENTRY(sge_fl) link; /* All starving freelists */
285 };
286 
287 /* txq: SGE egress queue + miscellaneous items */
288 struct sge_txq {
289 	struct sge_eq eq;	/* MUST be first */
290 
291 	struct port_info *port;	/* the port this txq belongs to */
292 	struct tx_sdesc *sdesc;	/* KVA of software descriptor ring */
293 	mac_ring_handle_t ring_handle;
294 
295 	/* DMA handles used for tx */
296 	ddi_dma_handle_t *tx_dhdl;
297 	uint32_t tx_dhdl_total;	/* Total # of handles */
298 	uint32_t tx_dhdl_pidx;	/* next handle to be used */
299 	uint32_t tx_dhdl_cidx;	/* reclaimed up to this index */
300 	uint32_t tx_dhdl_avail;	/* # of available handles */
301 
302 	/* Copy buffers for tx */
303 	ddi_dma_handle_t txb_dhdl;
304 	ddi_acc_handle_t txb_ahdl;
305 	caddr_t txb_va;		/* KVA of copy buffers area */
306 	uint64_t txb_ba;	/* bus address of copy buffers area */
307 	uint32_t txb_size;	/* total size */
308 	uint32_t txb_next;	/* offset of next useable area in the buffer */
309 	uint32_t txb_avail;	/* # of bytes available */
310 	uint16_t copy_threshold; /* anything this size or less is copied up */
311 
312 	uint64_t txpkts;	/* # of ethernet packets */
313 	uint64_t txbytes;	/* # of ethernet bytes */
314 	kstat_t *ksp;
315 
316 	/* stats for common events first */
317 
318 	uint64_t txcsum;	/* # of times hardware assisted with checksum */
319 	uint64_t tso_wrs;	/* # of IPv4 TSO work requests */
320 	uint64_t imm_wrs;	/* # of work requests with immediate data */
321 	uint64_t sgl_wrs;	/* # of work requests with direct SGL */
322 	uint64_t txpkt_wrs;	/* # of txpkt work requests (not coalesced) */
323 	uint64_t txpkts_wrs;	/* # of coalesced tx work requests */
324 	uint64_t txpkts_pkts;	/* # of frames in coalesced tx work requests */
325 	uint64_t txb_used;	/* # of tx copy buffers used (64 byte each) */
326 	uint64_t hdl_used;	/* # of DMA handles used */
327 
328 	/* stats for not-that-common events */
329 
330 	uint32_t txb_full;	/* txb ran out of space */
331 	uint32_t dma_hdl_failed; /* couldn't obtain DMA handle */
332 	uint32_t dma_map_failed; /* couldn't obtain DMA mapping */
333 	uint32_t qfull;		/* out of hardware descriptors */
334 	uint32_t qflush;	/* # of SGE_EGR_UPDATE notifications for txq */
335 	uint32_t pullup_early;	/* # of pullups before starting frame's SGL */
336 	uint32_t pullup_late;	/* # of pullups while building frame's SGL */
337 	uint32_t pullup_failed;	/* # of failed pullups */
338 };
339 
340 /* rxq: SGE ingress queue + SGE free list + miscellaneous items */
341 struct sge_rxq {
342 	struct sge_iq iq;	/* MUST be first */
343 	struct sge_fl fl;
344 
345 	struct port_info *port;	/* the port this rxq belongs to */
346 	kstat_t *ksp;
347 
348 	mac_ring_handle_t ring_handle;
349 	uint64_t ring_gen_num;
350 
351 	/* stats for common events first */
352 
353 	uint64_t rxcsum;	/* # of times hardware assisted with checksum */
354 	uint64_t rxpkts;	/* # of ethernet packets */
355 	uint64_t rxbytes;	/* # of ethernet bytes */
356 
357 	/* stats for not-that-common events */
358 
359 	uint32_t nomem;		/* mblk allocation during rx failed */
360 };
361 
362 #ifdef TCP_OFFLOAD_ENABLE
363 /* ofld_rxq: SGE ingress queue + SGE free list + miscellaneous items */
364 struct sge_ofld_rxq {
365 	struct sge_iq iq;	/* MUST be first */
366 	struct sge_fl fl;
367 };
368 
369 /*
370  * wrq: SGE egress queue that is given prebuilt work requests.  Both the control
371  * and offload tx queues are of this type.
372  */
373 struct sge_wrq {
374 	struct sge_eq eq;	/* MUST be first */
375 
376 	struct adapter *adapter;
377 
378 	/* List of WRs held up due to lack of tx descriptors */
379 	struct mblk_pair wr_list;
380 
381 	/* stats for common events first */
382 
383 	uint64_t tx_wrs;	/* # of tx work requests */
384 
385 	/* stats for not-that-common events */
386 
387 	uint32_t no_desc;	/* out of hardware descriptors */
388 };
389 #endif
390 
391 struct sge {
392 	int fl_starve_threshold;
393 	int s_qpp;
394 
395 	int nrxq;	/* total rx queues (all ports and the rest) */
396 	int ntxq;	/* total tx queues (all ports and the rest) */
397 #ifdef TCP_OFFLOAD_ENABLE
398 	int nofldrxq;	/* total # of TOE rx queues */
399 	int nofldtxq;	/* total # of TOE tx queues */
400 #endif
401 	int niq;	/* total ingress queues */
402 	int neq;	/* total egress queues */
403 	int stat_len;	/* length of status page at ring end */
404 	int pktshift;	/* padding between CPL & packet data */
405 	int fl_align;	/* response queue message alignment */
406 
407 	struct sge_iq fwq;	/* Firmware event queue */
408 #ifdef TCP_OFFLOAD_ENABLE
409 	struct sge_wrq mgmtq;	/* Management queue (Control queue) */
410 #endif
411 	struct sge_txq *txq;	/* NIC tx queues */
412 	struct sge_rxq *rxq;	/* NIC rx queues */
413 #ifdef TCP_OFFLOAD_ENABLE
414 	struct sge_wrq *ctrlq;	/* Control queues */
415 	struct sge_wrq *ofld_txq;	/* TOE tx queues */
416 	struct sge_ofld_rxq *ofld_rxq;	/* TOE rx queues */
417 #endif
418 
419 	uint16_t iq_start;
420 	int eq_start;
421 	struct sge_iq **iqmap;	/* iq->cntxt_id to iq mapping */
422 	struct sge_eq **eqmap;	/* eq->cntxt_id to eq mapping */
423 
424 	/* Device access and DMA attributes for all the descriptor rings */
425 	ddi_device_acc_attr_t acc_attr_desc;
426 	ddi_dma_attr_t	dma_attr_desc;
427 
428 	/* Device access and DMA attributes for tx buffers */
429 	ddi_device_acc_attr_t acc_attr_tx;
430 	ddi_dma_attr_t	dma_attr_tx;
431 
432 	/* Device access and DMA attributes for rx buffers are in rxb_params */
433 	kmem_cache_t *rxbuf_cache;
434 	struct rxbuf_cache_params rxb_params;
435 };
436 
437 struct driver_properties {
438 	/* There is a driver.conf variable for each of these */
439 	int max_ntxq_10g;
440 	int max_nrxq_10g;
441 	int max_ntxq_1g;
442 	int max_nrxq_1g;
443 #ifdef TCP_OFFLOAD_ENABLE
444 	int max_nofldtxq_10g;
445 	int max_nofldrxq_10g;
446 	int max_nofldtxq_1g;
447 	int max_nofldrxq_1g;
448 #endif
449 	int intr_types;
450 	int tmr_idx_10g;
451 	int pktc_idx_10g;
452 	int tmr_idx_1g;
453 	int pktc_idx_1g;
454 	int qsize_txq;
455 	int qsize_rxq;
456 
457 	int timer_val[SGE_NTIMERS];
458 	int counter_val[SGE_NCOUNTERS];
459 
460 	int wc;
461 
462 	int multi_rings;
463 	int t4_fw_install;
464 };
465 
466 struct rss_header;
467 typedef int (*cpl_handler_t)(struct sge_iq *, const struct rss_header *,
468     mblk_t *);
469 typedef int (*fw_msg_handler_t)(struct adapter *, const __be64 *);
470 
471 struct adapter {
472 	SLIST_ENTRY(adapter) link;
473 	dev_info_t *dip;
474 	dev_t dev;
475 
476 	unsigned int pf;
477 	unsigned int mbox;
478 
479 	unsigned int vpd_busy;
480 	unsigned int vpd_flag;
481 
482 	u32 t4_bar0;
483 
484 	uint_t open;	/* character device is open */
485 
486 	/* PCI config space access handle */
487 	ddi_acc_handle_t pci_regh;
488 
489 	/* MMIO register access handle */
490 	ddi_acc_handle_t regh;
491 	caddr_t regp;
492 	/* BAR1 register access handle */
493 	ddi_acc_handle_t reg1h;
494 	caddr_t reg1p;
495 
496 	/* Interrupt information */
497 	int intr_type;
498 	int intr_count;
499 	int intr_cap;
500 	uint_t intr_pri;
501 	ddi_intr_handle_t *intr_handle;
502 
503 	struct driver_properties props;
504 	kstat_t *ksp;
505 	kstat_t *ksp_stat;
506 
507 	struct sge sge;
508 
509 	struct port_info *port[MAX_NPORTS];
510 	ddi_taskq_t *tq[NCHAN];
511 	uint8_t chan_map[NCHAN];
512 	uint32_t filter_mode;
513 
514 	struct l2t_data *l2t;	/* L2 table */
515 	struct tid_info tids;
516 
517 	int doorbells;
518 	int registered_device_map;
519 	int open_device_map;
520 	int flags;
521 
522 	unsigned int cfcsum;
523 	struct adapter_params params;
524 	struct t4_virt_res vres;
525 
526 #ifdef TCP_OFFLOAD_ENABLE
527 	struct uld_softc tom;
528 	struct tom_tunables tt;
529 #endif
530 
531 #ifdef TCP_OFFLOAD_ENABLE
532 	int offload_map;
533 #endif
534 	uint16_t linkcaps;
535 	uint16_t niccaps;
536 	uint16_t toecaps;
537 	uint16_t rdmacaps;
538 	uint16_t iscsicaps;
539 	uint16_t fcoecaps;
540 
541 	fw_msg_handler_t fw_msg_handler[5]; /* NUM_FW6_TYPES */
542 	cpl_handler_t cpl_handler[0xef]; /* NUM_CPL_CMDS */
543 
544 	kmutex_t lock;
545 	kcondvar_t cv;
546 
547 	/* Starving free lists */
548 	kmutex_t sfl_lock;	/* same cache-line as sc_lock? but that's ok */
549 	TAILQ_HEAD(, sge_fl) sfl;
550 	timeout_id_t sfl_timer;
551 };
552 
553 enum {
554 	NIC_H = 0,
555 	TOM_H,
556 	IW_H,
557 	ISCSI_H
558 };
559 
560 struct memwin {
561 	uint32_t base;
562 	uint32_t aperture;
563 };
564 
565 #define	ADAPTER_LOCK(sc)		mutex_enter(&(sc)->lock)
566 #define	ADAPTER_UNLOCK(sc)		mutex_exit(&(sc)->lock)
567 #define	ADAPTER_LOCK_ASSERT_OWNED(sc)	ASSERT(mutex_owned(&(sc)->lock))
568 #define	ADAPTER_LOCK_ASSERT_NOTOWNED(sc) ASSERT(!mutex_owned(&(sc)->lock))
569 
570 #define	PORT_LOCK(pi)			mutex_enter(&(pi)->lock)
571 #define	PORT_UNLOCK(pi)			mutex_exit(&(pi)->lock)
572 #define	PORT_LOCK_ASSERT_OWNED(pi)	ASSERT(mutex_owned(&(pi)->lock))
573 #define	PORT_LOCK_ASSERT_NOTOWNED(pi)	ASSERT(!mutex_owned(&(pi)->lock))
574 
575 #define	IQ_LOCK(iq)			mutex_enter(&(iq)->lock)
576 #define	IQ_UNLOCK(iq)			mutex_exit(&(iq)->lock)
577 #define	IQ_LOCK_ASSERT_OWNED(iq)	ASSERT(mutex_owned(&(iq)->lock))
578 #define	IQ_LOCK_ASSERT_NOTOWNED(iq)	ASSERT(!mutex_owned(&(iq)->lock))
579 
580 #define	FL_LOCK(fl)			mutex_enter(&(fl)->lock)
581 #define	FL_UNLOCK(fl)			mutex_exit(&(fl)->lock)
582 #define	FL_LOCK_ASSERT_OWNED(fl)	ASSERT(mutex_owned(&(fl)->lock))
583 #define	FL_LOCK_ASSERT_NOTOWNED(fl)	ASSERT(!mutex_owned(&(fl)->lock))
584 
585 #define	RXQ_LOCK(rxq)			IQ_LOCK(&(rxq)->iq)
586 #define	RXQ_UNLOCK(rxq)			IQ_UNLOCK(&(rxq)->iq)
587 #define	RXQ_LOCK_ASSERT_OWNED(rxq)	IQ_LOCK_ASSERT_OWNED(&(rxq)->iq)
588 #define	RXQ_LOCK_ASSERT_NOTOWNED(rxq)	IQ_LOCK_ASSERT_NOTOWNED(&(rxq)->iq)
589 
590 #define	RXQ_FL_LOCK(rxq)		FL_LOCK(&(rxq)->fl)
591 #define	RXQ_FL_UNLOCK(rxq)		FL_UNLOCK(&(rxq)->fl)
592 #define	RXQ_FL_LOCK_ASSERT_OWNED(rxq)	FL_LOCK_ASSERT_OWNED(&(rxq)->fl)
593 #define	RXQ_FL_LOCK_ASSERT_NOTOWNED(rxq) FL_LOCK_ASSERT_NOTOWNED(&(rxq)->fl)
594 
595 #define	EQ_LOCK(eq)			mutex_enter(&(eq)->lock)
596 #define	EQ_UNLOCK(eq)			mutex_exit(&(eq)->lock)
597 #define	EQ_LOCK_ASSERT_OWNED(eq)	ASSERT(mutex_owned(&(eq)->lock))
598 #define	EQ_LOCK_ASSERT_NOTOWNED(eq)	ASSERT(!mutex_owned(&(eq)->lock))
599 
600 #define	TXQ_LOCK(txq)			EQ_LOCK(&(txq)->eq)
601 #define	TXQ_UNLOCK(txq)			EQ_UNLOCK(&(txq)->eq)
602 #define	TXQ_LOCK_ASSERT_OWNED(txq)	EQ_LOCK_ASSERT_OWNED(&(txq)->eq)
603 #define	TXQ_LOCK_ASSERT_NOTOWNED(txq)	EQ_LOCK_ASSERT_NOTOWNED(&(txq)->eq)
604 
605 #define	for_each_txq(pi, iter, txq) \
606 	txq = &pi->adapter->sge.txq[pi->first_txq]; \
607 	for (iter = 0; iter < pi->ntxq; ++iter, ++txq)
608 #define	for_each_rxq(pi, iter, rxq) \
609 	rxq = &pi->adapter->sge.rxq[pi->first_rxq]; \
610 	for (iter = 0; iter < pi->nrxq; ++iter, ++rxq)
611 #define	for_each_ofld_txq(pi, iter, ofld_txq) \
612 	ofld_txq = &pi->adapter->sge.ofld_txq[pi->first_ofld_txq]; \
613 	for (iter = 0; iter < pi->nofldtxq; ++iter, ++ofld_txq)
614 #define	for_each_ofld_rxq(pi, iter, ofld_rxq) \
615 	ofld_rxq = &pi->adapter->sge.ofld_rxq[pi->first_ofld_rxq]; \
616 	for (iter = 0; iter < pi->nofldrxq; ++iter, ++ofld_rxq)
617 
618 #define	NFIQ(sc) ((sc)->intr_count > 1 ? (sc)->intr_count - 1 : 1)
619 
620 /* One for errors, one for firmware events */
621 #define	T4_EXTRA_INTR 2
622 
623 /* Presently disabling locking around  mbox access
624  * We may need to reenable it later
625  */
626 typedef int t4_os_lock_t;
627 static inline void t4_os_lock(t4_os_lock_t *lock)
628 {
629 
630 }
631 static inline void t4_os_unlock(t4_os_lock_t *lock)
632 {
633 
634 }
635 
636 static inline uint32_t
637 t4_read_reg(struct adapter *sc, uint32_t reg)
638 {
639 	/* LINTED: E_BAD_PTR_CAST_ALIGN */
640 	return (ddi_get32(sc->regh, (uint32_t *)(sc->regp + reg)));
641 }
642 
643 static inline void
644 t4_write_reg(struct adapter *sc, uint32_t reg, uint32_t val)
645 {
646 	/* LINTED: E_BAD_PTR_CAST_ALIGN */
647 	ddi_put32(sc->regh, (uint32_t *)(sc->regp + reg), val);
648 }
649 
650 static inline void
651 t4_os_pci_read_cfg1(struct adapter *sc, int reg, uint8_t *val)
652 {
653 	*val = pci_config_get8(sc->pci_regh, reg);
654 }
655 
656 static inline void
657 t4_os_pci_write_cfg1(struct adapter *sc, int reg, uint8_t val)
658 {
659 	pci_config_put8(sc->pci_regh, reg, val);
660 }
661 
662 static inline void
663 t4_os_pci_read_cfg2(struct adapter *sc, int reg, uint16_t *val)
664 {
665 	*val = pci_config_get16(sc->pci_regh, reg);
666 }
667 
668 static inline void
669 t4_os_pci_write_cfg2(struct adapter *sc, int reg, uint16_t val)
670 {
671 	pci_config_put16(sc->pci_regh, reg, val);
672 }
673 
674 static inline void
675 t4_os_pci_read_cfg4(struct adapter *sc, int reg, uint32_t *val)
676 {
677 	*val = pci_config_get32(sc->pci_regh, reg);
678 }
679 
680 static inline void
681 t4_os_pci_write_cfg4(struct adapter *sc, int reg, uint32_t val)
682 {
683 	pci_config_put32(sc->pci_regh, reg, val);
684 }
685 
686 static inline uint64_t
687 t4_read_reg64(struct adapter *sc, uint32_t reg)
688 {
689 	/* LINTED: E_BAD_PTR_CAST_ALIGN */
690 	return (ddi_get64(sc->regh, (uint64_t *)(sc->regp + reg)));
691 }
692 
693 static inline void
694 t4_write_reg64(struct adapter *sc, uint32_t reg, uint64_t val)
695 {
696 	/* LINTED: E_BAD_PTR_CAST_ALIGN */
697 	ddi_put64(sc->regh, (uint64_t *)(sc->regp + reg), val);
698 }
699 
700 static inline struct port_info *
701 adap2pinfo(struct adapter *sc, int idx)
702 {
703 	return (sc->port[idx]);
704 }
705 
706 static inline void
707 t4_os_set_hw_addr(struct adapter *sc, int idx, uint8_t hw_addr[])
708 {
709 	bcopy(hw_addr, sc->port[idx]->hw_addr, ETHERADDRL);
710 }
711 
712 static inline bool
713 is_10G_port(const struct port_info *pi)
714 {
715 	return ((pi->link_cfg.supported & FW_PORT_CAP_SPEED_10G) != 0);
716 }
717 
718 static inline struct sge_rxq *
719 iq_to_rxq(struct sge_iq *iq)
720 {
721 	return (container_of(iq, struct sge_rxq, iq));
722 }
723 
724 static inline bool
725 is_25G_port(const struct port_info *pi)
726 {
727 	return ((pi->link_cfg.supported & FW_PORT_CAP_SPEED_25G) != 0);
728 }
729 
730 static inline bool
731 is_40G_port(const struct port_info *pi)
732 {
733 	return ((pi->link_cfg.supported & FW_PORT_CAP_SPEED_40G) != 0);
734 }
735 
736 static inline bool
737 is_100G_port(const struct port_info *pi)
738 {
739 	return ((pi->link_cfg.supported & FW_PORT_CAP_SPEED_100G) != 0);
740 }
741 
742 static inline bool
743 is_10XG_port(const struct port_info *pi)
744 {
745 	return (is_10G_port(pi) || is_40G_port(pi) ||
746 		is_25G_port(pi) || is_100G_port(pi));
747 }
748 
749 static inline char *
750 print_port_speed(const struct port_info *pi)
751 {
752 	if (!pi)
753 		return "-";
754 
755 	if (is_100G_port(pi))
756 		return "100G";
757 	else if (is_40G_port(pi))
758 		return "40G";
759 	else if (is_25G_port(pi))
760 		return "25G";
761 	else if (is_10G_port(pi))
762 		return "10G";
763 	else
764 		return "1G";
765 }
766 
767 #ifdef TCP_OFFLOAD_ENABLE
768 int t4_wrq_tx_locked(struct adapter *sc, struct sge_wrq *wrq, mblk_t *m0);
769 
770 static inline int
771 t4_wrq_tx(struct adapter *sc, struct sge_wrq *wrq, mblk_t *m)
772 {
773 	int rc;
774 
775 	TXQ_LOCK(wrq);
776 	rc = t4_wrq_tx_locked(sc, wrq, m);
777 	TXQ_UNLOCK(wrq);
778 	return (rc);
779 }
780 #endif
781 
782 /**
783  * t4_os_pci_read_seeprom - read four bytes of SEEPROM/VPD contents
784  * @adapter: the adapter
785  * @addr: SEEPROM/VPD Address to read
786  * @valp: where to store the value read
787  *
788  * Read a 32-bit value from the given address in the SEEPROM/VPD.  The address
789  * must be four-byte aligned.  Returns 0 on success, a negative erro number
790  * on failure.
791  */
792 static inline int t4_os_pci_read_seeprom(adapter_t *adapter,
793 					 int addr, u32 *valp)
794 {
795 	int t4_seeprom_read(struct adapter *adapter, u32 addr, u32 *data);
796 	int ret;
797 
798 	ret = t4_seeprom_read(adapter, addr, valp);
799 
800 	return ret >= 0 ? 0 : ret;
801 }
802 
803 /**
804  * t4_os_pci_write_seeprom - write four bytes of SEEPROM/VPD contents
805  * @adapter: the adapter
806  * @addr: SEEPROM/VPD Address to write
807  * @val: the value write
808  *
809  * Write a 32-bit value to the given address in the SEEPROM/VPD.  The address
810  * must be four-byte aligned.  Returns 0 on success, a negative erro number
811  * on failure.
812  */
813 static inline int t4_os_pci_write_seeprom(adapter_t *adapter,
814 					  int addr, u32 val)
815 {
816 	int t4_seeprom_write(struct adapter *adapter, u32 addr, u32 data);
817 	int ret;
818 
819 	ret = t4_seeprom_write(adapter, addr, val);
820 
821 	return ret >= 0 ? 0 : ret;
822 }
823 
824 static inline int t4_os_pci_set_vpd_size(struct adapter *adapter, size_t len)
825 {
826 	return 0;
827 }
828 
829 static inline unsigned int t4_use_ldst(struct adapter *adap)
830 {
831 	return (adap->flags & FW_OK);
832 }
833 #define t4_os_alloc(_size)	kmem_alloc(_size, KM_SLEEP)
834 
835 static inline void t4_db_full(struct adapter *adap) {}
836 static inline void t4_db_dropped(struct adapter *adap) {}
837 
838 /* t4_nexus.c */
839 int t4_os_find_pci_capability(struct adapter *sc, int cap);
840 void t4_os_portmod_changed(const struct adapter *sc, int idx);
841 int adapter_full_init(struct adapter *sc);
842 int adapter_full_uninit(struct adapter *sc);
843 int port_full_init(struct port_info *pi);
844 int port_full_uninit(struct port_info *pi);
845 void enable_port_queues(struct port_info *pi);
846 void disable_port_queues(struct port_info *pi);
847 int t4_register_cpl_handler(struct adapter *sc, int opcode, cpl_handler_t h);
848 int t4_register_fw_msg_handler(struct adapter *, int, fw_msg_handler_t);
849 void t4_iterate(void (*func)(int, void *), void *arg);
850 
851 /* t4_sge.c */
852 void t4_sge_init(struct adapter *sc);
853 int t4_setup_adapter_queues(struct adapter *sc);
854 int t4_teardown_adapter_queues(struct adapter *sc);
855 int t4_setup_port_queues(struct port_info *pi);
856 int t4_teardown_port_queues(struct port_info *pi);
857 uint_t t4_intr_all(caddr_t arg1, caddr_t arg2);
858 uint_t t4_intr(caddr_t arg1, caddr_t arg2);
859 uint_t t4_intr_err(caddr_t arg1, caddr_t arg2);
860 int t4_mgmt_tx(struct adapter *sc, mblk_t *m);
861 void memwin_info(struct adapter *, int, uint32_t *, uint32_t *);
862 uint32_t position_memwin(struct adapter *, int, uint32_t);
863 
864 mblk_t *t4_eth_tx(void *, mblk_t *);
865 mblk_t *t4_mc_tx(void *arg, mblk_t *m);
866 mblk_t *t4_ring_rx(struct sge_rxq *rxq, int poll_bytes);
867 int t4_alloc_tx_maps(struct adapter *sc, struct tx_maps *txmaps,  int count,
868     int flags);
869 
870 /* t4_mac.c */
871 void t4_mc_init(struct port_info *pi);
872 void t4_mc_cb_init(struct port_info *);
873 void t4_os_link_changed(struct adapter *sc, int idx, int link_stat);
874 void t4_mac_rx(struct port_info *pi, struct sge_rxq *rxq, mblk_t *m);
875 void t4_mac_tx_update(struct port_info *pi, struct sge_txq *txq);
876 int t4_addmac(void *arg, const uint8_t *ucaddr);
877 
878 /* t4_ioctl.c */
879 int t4_ioctl(struct adapter *sc, int cmd, void *data, int mode);
880 
881 struct l2t_data *t4_init_l2t(struct adapter *sc);
882 #endif /* __CXGBE_ADAPTER_H */
883