aac.h revision 7100:9d60065f44d9
1/*
2 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
3 * Use is subject to license terms.
4 */
5
6/*
7 * Copyright 2005-06 Adaptec, Inc.
8 * Copyright (c) 2005-06 Adaptec Inc., Achim Leubner
9 * Copyright (c) 2000 Michael Smith
10 * Copyright (c) 2001 Scott Long
11 * Copyright (c) 2000 BSDi
12 * All rights reserved.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 *    notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 *    notice, this list of conditions and the following disclaimer in the
21 *    documentation and/or other materials provided with the distribution.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 *    $FreeBSD: src/sys/dev/aac/aacvar.h,v 1.47 2005/10/08 15:55:09 scottl Exp $
36 */
37
38#ifndef	_AAC_H_
39#define	_AAC_H_
40
41#pragma ident	"%Z%%M%	%I%	%E% SMI"
42
43#ifdef	__cplusplus
44extern "C" {
45#endif
46
47#define	AAC_ROUNDUP(x, y)		(((x) + (y) - 1) / (y) * (y))
48
49#define	AAC_TYPE_DEVO			1
50#define	AAC_TYPE_ALPHA			2
51#define	AAC_TYPE_BETA			3
52#define	AAC_TYPE_RELEASE		4
53
54#ifndef	AAC_DRIVER_BUILD
55#define	AAC_DRIVER_BUILD		1
56#endif
57
58#define	AAC_DRIVER_MAJOR_VERSION	2
59#define	AAC_DRIVER_MINOR_VERSION	2
60#define	AAC_DRIVER_BUGFIX_LEVEL		4
61#define	AAC_DRIVER_TYPE			AAC_TYPE_RELEASE
62
63#define	STR(s)				# s
64#define	AAC_VERSION(a, b, c)		STR(a.b.c)
65#define	AAC_DRIVER_VERSION		AAC_VERSION(AAC_DRIVER_MAJOR_VERSION, \
66					AAC_DRIVER_MINOR_VERSION, \
67					AAC_DRIVER_BUGFIX_LEVEL)
68
69#define	AACOK				0
70#define	AACERR				-1
71
72#define	AAC_MAX_ADAPTERS		64
73
74/* Definitions for mode sense */
75#ifndef	SD_MODE_SENSE_PAGE3_CODE
76#define	SD_MODE_SENSE_PAGE3_CODE	0x03
77#endif
78
79#ifndef	SD_MODE_SENSE_PAGE4_CODE
80#define	SD_MODE_SENSE_PAGE4_CODE	0x04
81#endif
82
83#ifndef	SCMD_SYNCHRONIZE_CACHE
84#define	SCMD_SYNCHRONIZE_CACHE		0x35
85#endif
86
87/*
88 * The controller reports status events in AIFs. We hang on to a number of
89 * these in order to pass them out to user-space management tools.
90 */
91#define	AAC_AIFQ_LENGTH			64
92
93#ifdef __x86
94#define	AAC_IMMEDIATE_TIMEOUT		30	/* seconds */
95#else
96#define	AAC_IMMEDIATE_TIMEOUT		60	/* seconds */
97#endif
98#define	AAC_FWUP_TIMEOUT		180	/* wait up to 3 minutes */
99#define	AAC_IOCTL_TIMEOUT		180	/* wait up to 3 minutes */
100
101/* Adapter hardware interface types */
102#define	AAC_HWIF_UNKNOWN		0
103#define	AAC_HWIF_I960RX			1
104#define	AAC_HWIF_RKT			2
105
106#define	AAC_TYPE_UNKNOWN		0
107#define	AAC_TYPE_SCSI			1
108#define	AAC_TYPE_SATA			2
109#define	AAC_TYPE_SAS			3
110
111#define	AAC_LS32(d)			((uint32_t)((d) & 0xffffffffull))
112#define	AAC_MS32(d)			((uint32_t)((d) >> 32))
113#define	AAC_LO32(p64)			((uint32_t *)(p64))
114#define	AAC_HI32(p64)			((uint32_t *)(p64) + 1)
115
116/*
117 * AAC_CMDQ_SYNC should be 0 and AAC_CMDQ_ASYNC be 1 for Sync FIB io
118 * to be served before async FIB io, see aac_start_waiting_io().
119 * So that io requests sent by interactive userland commands get
120 * responded asap.
121 */
122enum aac_cmdq {
123	AAC_CMDQ_SYNC,	/* sync FIB queue */
124	AAC_CMDQ_ASYNC,	/* async FIB queue */
125	AAC_CMDQ_NUM
126};
127
128/*
129 * IO command flags
130 */
131#define	AAC_IOCMD_SYNC		(1 << AAC_CMDQ_SYNC)
132#define	AAC_IOCMD_ASYNC		(1 << AAC_CMDQ_ASYNC)
133#define	AAC_IOCMD_OUTSTANDING	(1 << AAC_CMDQ_NUM)
134#define	AAC_IOCMD_ALL		(AAC_IOCMD_SYNC | AAC_IOCMD_ASYNC | \
135				AAC_IOCMD_OUTSTANDING)
136
137struct aac_cmd_queue {
138	struct aac_cmd *q_head; /* also as the header of aac_cmd */
139	struct aac_cmd *q_tail;
140};
141
142struct aac_card_type {
143	uint16_t vendor;	/* PCI Vendor ID */
144	uint16_t device;	/* PCI Device ID */
145	uint16_t subvendor;	/* PCI Subsystem Vendor ID */
146	uint16_t subsys;	/* PCI Subsystem ID */
147	uint16_t hwif;		/* card chip type: i960 or Rocket */
148	uint16_t quirks;	/* card odd limits */
149	uint16_t type;		/* hard drive type */
150	char *vid;		/* ASCII data for INQUIRY command vendor id */
151	char *desc;		/* ASCII data for INQUIRY command product id */
152};
153
154/* Array description */
155struct aac_container {
156	uint8_t valid;
157	uint32_t cid;		/* container id */
158	uint32_t uid;		/* container uid */
159	uint64_t size;		/* in block */
160	uint8_t locked;
161	uint8_t deleted;
162	uint8_t reset;			/* container is being reseted */
163	int ncmds[AAC_CMDQ_NUM];	/* outstanding cmds of the device */
164	int throttle[AAC_CMDQ_NUM];	/* hold IO cmds for the device */
165};
166
167/*
168 * The firmware can support a lot of outstanding commands. Each aac_slot
169 * is corresponding to one of such commands. It records the command and
170 * associated DMA resource for FIB command.
171 */
172struct aac_slot {
173	struct aac_slot *next;	/* next slot in the free slot list */
174	int index;		/* index of this slot */
175	ddi_acc_handle_t fib_acc_handle;
176	ddi_dma_handle_t fib_dma_handle;
177	uint64_t fib_phyaddr;	/* physical address of FIB memory */
178	struct aac_cmd *acp;	/* command using this slot */
179	struct aac_fib *fibp;	/* virtual address of FIB memory */
180};
181
182/* Flags for attach tracking */
183#define	AAC_ATTACH_SOFTSTATE_ALLOCED	(1 << 0)
184#define	AAC_ATTACH_CARD_DETECTED	(1 << 1)
185#define	AAC_ATTACH_PCI_MEM_MAPPED	(1 << 2)
186#define	AAC_ATTACH_KMUTEX_INITED	(1 << 3)
187#define	AAC_ATTACH_HARD_INTR_SETUP	(1 << 4)
188#define	AAC_ATTACH_SOFT_INTR_SETUP	(1 << 5)
189#define	AAC_ATTACH_SCSI_TRAN_SETUP	(1 << 6)
190#define	AAC_ATTACH_COMM_SPACE_SETUP	(1 << 7)
191#define	AAC_ATTACH_CREATE_DEVCTL	(1 << 8)
192#define	AAC_ATTACH_CREATE_SCSI		(1 << 9)
193
194/* Driver running states */
195#define	AAC_STATE_STOPPED	0
196#define	AAC_STATE_RUN		(1 << 0)
197#define	AAC_STATE_RESET		(1 << 1)
198#define	AAC_STATE_QUIESCED	(1 << 2)
199#define	AAC_STATE_DEAD		(1 << 3)
200
201/*
202 * Flags for aac firmware
203 * Note: Quirks are only valid for the older cards. These cards only supported
204 * old comm. Thus they are not valid for any cards that support new comm.
205 */
206#define	AAC_FLAGS_SG_64BIT	(1 << 0) /* Use 64-bit S/G addresses */
207#define	AAC_FLAGS_4GB_WINDOW	(1 << 1) /* Can access host mem 2-4GB range */
208#define	AAC_FLAGS_NO4GB	(1 << 2)	/* quirk: FIB addresses must reside */
209					/*	  between 0x2000 & 0x7FFFFFFF */
210#define	AAC_FLAGS_256FIBS	(1 << 3) /* quirk: Can only do 256 commands */
211#define	AAC_FLAGS_NEW_COMM	(1 << 4) /* New comm. interface supported */
212#define	AAC_FLAGS_RAW_IO	(1 << 5) /* Raw I/O interface */
213#define	AAC_FLAGS_ARRAY_64BIT	(1 << 6) /* 64-bit array size */
214#define	AAC_FLAGS_LBA_64BIT	(1 << 7) /* 64-bit LBA supported */
215#define	AAC_FLAGS_17SG		(1 << 8) /* quirk: 17 scatter gather maximum */
216#define	AAC_FLAGS_34SG		(1 << 9) /* quirk: 34 scatter gather maximum */
217
218struct aac_softstate;
219struct aac_interface {
220	int (*aif_get_fwstatus)(struct aac_softstate *);
221	int (*aif_get_mailbox)(struct aac_softstate *, int);
222	void (*aif_set_mailbox)(struct aac_softstate *, uint32_t,
223	    uint32_t, uint32_t, uint32_t, uint32_t);
224};
225
226struct aac_fib_context {
227	uint32_t unique;
228	int ctx_idx;
229	int ctx_filled;		/* aifq is full for this fib context */
230	struct aac_fib_context *next, *prev;
231};
232
233typedef void (*aac_cmd_fib_t)(struct aac_softstate *, struct aac_cmd *);
234
235#define	AAC_VENDOR_LEN		8
236#define	AAC_PRODUCT_LEN		16
237
238struct aac_softstate {
239	int card;		/* index to aac_cards */
240	uint16_t hwif;		/* card chip type: i960 or Rocket */
241	uint16_t vendid;	/* vendor id */
242	uint16_t subvendid;	/* sub vendor id */
243	uint16_t devid;		/* device id */
244	uint16_t subsysid;	/* sub system id */
245	char vendor_name[AAC_VENDOR_LEN + 1];
246	char product_name[AAC_PRODUCT_LEN + 1];
247	uint32_t support_opt;	/* firmware features */
248	uint32_t atu_size;	/* actual size of PCI mem space */
249	uint32_t map_size;	/* mapped PCI mem space size */
250	uint32_t map_size_min;	/* minimum size of PCI mem that must be */
251				/* mapped to address the card */
252	int flags;		/* firmware features enabled */
253	int instance;
254	dev_info_t *devinfo_p;
255	int slen;
256
257	/* DMA attributes */
258	ddi_dma_attr_t buf_dma_attr;
259	ddi_dma_attr_t addr_dma_attr;
260
261	/* PCI spaces */
262	ddi_acc_handle_t pci_mem_handle;
263	uint8_t *pci_mem_base_vaddr;
264	uint32_t pci_mem_base_paddr;
265
266	struct aac_interface aac_if;	/* adapter hardware interface */
267
268	struct aac_slot sync_slot;	/* sync FIB */
269
270	/* Communication space */
271	struct aac_comm_space *comm_space;
272	ddi_acc_handle_t comm_space_acc_handle;
273	ddi_dma_handle_t comm_space_dma_handle;
274	uint32_t comm_space_phyaddr;
275
276	/* Old Comm. interface: message queues */
277	struct aac_queue_table *qtablep;
278	struct aac_queue_entry *qentries[AAC_QUEUE_COUNT];
279
280	/* New Comm. interface */
281	uint32_t aac_max_fibs;		/* max. FIB count */
282	uint32_t aac_max_fib_size;	/* max. FIB size */
283	uint32_t aac_sg_tablesize;	/* max. sg count from host */
284	uint32_t aac_max_sectors;	/* max. I/O size from host (blocks) */
285
286	aac_cmd_fib_t aac_cmd_fib;	/* IO cmd FIB construct function */
287	aac_cmd_fib_t aac_cmd_fib_scsi;	/* SRB construct function */
288
289	ddi_softintr_t softint_id;	/* soft intr */
290
291	kmutex_t io_lock;
292	int state;			/* driver state */
293
294	struct aac_container containers[AAC_MAX_LD];
295	int container_count;		/* max container id + 1 */
296
297	/*
298	 * Command queues
299	 * Each aac command flows through wait(or wait_sync) queue,
300	 * busy queue, and complete queue sequentially.
301	 */
302	struct aac_cmd_queue q_wait[AAC_CMDQ_NUM];
303	struct aac_cmd_queue q_busy;	/* outstanding cmd queue */
304	kmutex_t q_comp_mutex;
305	struct aac_cmd_queue q_comp;	/* completed io requests */
306
307	/* I/O slots and FIBs */
308	int total_slots;		/* total slots allocated */
309	int total_fibs;			/* total FIBs allocated */
310	struct aac_slot *io_slot;	/* static list for allocated slots */
311	struct aac_slot *free_io_slot_head;
312
313	timeout_id_t timeout_id;	/* for timeout daemon */
314
315	kcondvar_t event;		/* for ioctl_send_fib() and sync IO */
316
317	int bus_ncmds[AAC_CMDQ_NUM];	/* total outstanding async cmds */
318	int bus_throttle[AAC_CMDQ_NUM];	/* hold IO cmds for the bus */
319	int ndrains;			/* number of draining threads */
320	timeout_id_t drain_timeid;	/* for outstanding cmd drain */
321	kcondvar_t drain_cv;		/* for quiesce drain */
322
323	/* AIF */
324	kmutex_t aifq_mutex;		/* for AIF queue aifq */
325	kcondvar_t aifv;
326	union aac_fib_align aifq[AAC_AIFQ_LENGTH];
327	int aifq_idx;			/* slot for next new AIF */
328	int aifq_wrap;			/* AIF queue has ever been wrapped */
329	struct aac_fib_context *fibctx;
330	int devcfg_wait_on;		/* AIF event waited for rescan */
331
332	int fm_capabilities;
333
334	/* MSI specific fields */
335	ddi_intr_handle_t *htable;	/* For array of interrupts */
336	int intr_type;			/* What type of interrupt */
337	int intr_cnt;			/* # of intrs count returned */
338	uint_t intr_pri;		/* Interrupt priority   */
339	int intr_cap;			/* Interrupt capabilities */
340
341#ifdef DEBUG
342	/* UART trace printf variables */
343	uint32_t debug_flags;		/* debug print flags bitmap */
344	uint32_t debug_fw_flags;	/* FW debug flags */
345	uint32_t debug_buf_offset;	/* offset from DPMEM start */
346	uint32_t debug_buf_size;	/* FW debug buffer size in bytes */
347	uint32_t debug_header_size;	/* size of debug header */
348#endif
349};
350
351/*
352 * The following data are kept stable because they are only written at driver
353 * initialization, and we do not allow them changed otherwise even at driver
354 * re-initialization.
355 */
356_NOTE(SCHEME_PROTECTS_DATA("stable data", aac_softstate::{flags slen \
357    buf_dma_attr pci_mem_handle pci_mem_base_vaddr \
358    comm_space_acc_handle comm_space_dma_handle aac_max_fib_size \
359    aac_sg_tablesize aac_cmd_fib aac_cmd_fib_scsi debug_flags}))
360
361/*
362 * Scatter-gather list structure defined by HBA hardware
363 */
364struct aac_sge {
365	uint32_t bcount;	/* byte count */
366	union {
367		uint32_t ad32;	/* 32 bit address */
368		struct {
369			uint32_t lo;
370			uint32_t hi;
371		} ad64;		/* 64 bit address */
372	} addr;
373};
374
375/* aac_cmd flags */
376#define	AAC_CMD_CONSISTENT		(1 << 0)
377#define	AAC_CMD_DMA_PARTIAL		(1 << 1)
378#define	AAC_CMD_DMA_VALID		(1 << 2)
379#define	AAC_CMD_BUF_READ		(1 << 3)
380#define	AAC_CMD_BUF_WRITE		(1 << 4)
381#define	AAC_CMD_SYNC			(1 << 5) /* use sync FIB */
382#define	AAC_CMD_NO_INTR			(1 << 6) /* poll IO, no intr */
383#define	AAC_CMD_NO_CB			(1 << 7) /* sync IO, no callback */
384#define	AAC_CMD_NTAG			(1 << 8)
385#define	AAC_CMD_CMPLT			(1 << 9) /* cmd exec'ed by driver/fw */
386#define	AAC_CMD_ABORT			(1 << 10)
387#define	AAC_CMD_TIMEOUT			(1 << 11)
388#define	AAC_CMD_ERR			(1 << 12)
389
390struct aac_cmd {
391	/*
392	 * Note: should be the first member for aac_cmd_queue to work
393	 * correctly.
394	 */
395	struct aac_cmd *next;
396	struct aac_cmd *prev;
397
398	struct scsi_pkt *pkt;
399	int cmdlen;
400	int flags;
401	uint32_t timeout; /* time when the cmd should have completed */
402	struct buf *bp;
403	ddi_dma_handle_t buf_dma_handle;
404
405	/* For non-aligned buffer and SRB */
406	caddr_t abp;
407	ddi_acc_handle_t abh;
408
409	/* Data transfer state */
410	ddi_dma_cookie_t cookie;
411	uint_t left_cookien;
412	uint_t cur_win;
413	uint_t total_nwin;
414	size_t total_xfer;
415	uint64_t blkno;
416	uint32_t bcount;	/* buffer size in byte */
417	struct aac_sge *sgt;	/* sg table */
418
419	/* FIB construct function */
420	aac_cmd_fib_t aac_cmd_fib;
421	/* Call back function for completed command */
422	void (*ac_comp)(struct aac_softstate *, struct aac_cmd *);
423
424	struct aac_slot *slotp;	/* slot used by this command */
425	struct aac_container *dvp;	/* target device */
426
427	/* FIB for this IO command */
428	int fib_size; /* size of the FIB xferred to/from the card */
429	struct aac_fib *fibp;
430};
431
432#ifdef DEBUG
433
434#define	AACDB_FLAGS_MASK		0x0000ffff
435#define	AACDB_FLAGS_KERNEL_PRINT	0x00000001
436#define	AACDB_FLAGS_FW_PRINT		0x00000002
437#define	AACDB_FLAGS_NO_HEADERS		0x00000004
438
439#define	AACDB_FLAGS_MISC		0x00000010
440#define	AACDB_FLAGS_FUNC1		0x00000020
441#define	AACDB_FLAGS_FUNC2		0x00000040
442#define	AACDB_FLAGS_SCMD		0x00000080
443#define	AACDB_FLAGS_AIF			0x00000100
444#define	AACDB_FLAGS_FIB			0x00000200
445#define	AACDB_FLAGS_IOCTL		0x00000400
446
447extern uint32_t aac_debug_flags;
448extern int aac_dbflag_on(struct aac_softstate *, int);
449extern void aac_printf(struct aac_softstate *, uint_t, const char *, ...);
450extern void aac_print_fib(struct aac_softstate *, struct aac_fib *);
451
452#define	AACDB_PRINT(s, lev, ...) { \
453	if (aac_dbflag_on((s), AACDB_FLAGS_MISC)) \
454		aac_printf((s), (lev), __VA_ARGS__); }
455
456#define	AACDB_PRINT_IOCTL(s, ...) { \
457	if (aac_dbflag_on((s), AACDB_FLAGS_IOCTL)) \
458		aac_printf((s), CE_NOTE, __VA_ARGS__); }
459
460#define	AACDB_PRINT_TRAN(s, ...) { \
461	if (aac_dbflag_on((s), AACDB_FLAGS_SCMD)) \
462		aac_printf((s), CE_NOTE, __VA_ARGS__); }
463
464#define	DBCALLED(s, n) { \
465	if (aac_dbflag_on((s), AACDB_FLAGS_FUNC ## n)) \
466		aac_printf((s), CE_NOTE, "--- %s() called ---", __func__); }
467
468#define	AACDB_PRINT_SCMD(s, x) { \
469	if (aac_dbflag_on((s), AACDB_FLAGS_SCMD)) aac_print_scmd((s), (x)); }
470
471#define	AACDB_PRINT_AIF(s, x) { \
472	if (aac_dbflag_on((s), AACDB_FLAGS_AIF)) aac_print_aif((s), (x)); }
473
474#define	AACDB_PRINT_FIB(s, x) { \
475	if (aac_dbflag_on((s), AACDB_FLAGS_FIB)) aac_print_fib((s), (x)); }
476
477#else /* DEBUG */
478
479#define	AACDB_PRINT(s, lev, ...)
480#define	AACDB_PRINT_IOCTL(s, ...)
481#define	AACDB_PRINT_TRAN(s, ...)
482#define	AACDB_PRINT_FIB(s, x)
483#define	AACDB_PRINT_SCMD(s, x)
484#define	AACDB_PRINT_AIF(s, x)
485#define	DBCALLED(s, n)
486
487#endif /* DEBUG */
488
489#ifdef	__cplusplus
490}
491#endif
492
493#endif /* _AAC_H_ */
494