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