xref: /onnv-gate/usr/src/uts/common/sys/sata/impl/sata.h (revision 1258:9961d7d3ec8c)
1*1258Smlf /*
2*1258Smlf  * CDDL HEADER START
3*1258Smlf  *
4*1258Smlf  * The contents of this file are subject to the terms of the
5*1258Smlf  * Common Development and Distribution License (the "License").
6*1258Smlf  * You may not use this file except in compliance with the License.
7*1258Smlf  *
8*1258Smlf  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*1258Smlf  * or http://www.opensolaris.org/os/licensing.
10*1258Smlf  * See the License for the specific language governing permissions
11*1258Smlf  * and limitations under the License.
12*1258Smlf  *
13*1258Smlf  * When distributing Covered Code, include this CDDL HEADER in each
14*1258Smlf  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*1258Smlf  * If applicable, add the following below this CDDL HEADER, with the
16*1258Smlf  * fields enclosed by brackets "[]" replaced with your own identifying
17*1258Smlf  * information: Portions Copyright [yyyy] [name of copyright owner]
18*1258Smlf  *
19*1258Smlf  * CDDL HEADER END
20*1258Smlf  */
21*1258Smlf 
22*1258Smlf /*
23*1258Smlf  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*1258Smlf  * Use is subject to license terms.
25*1258Smlf  */
26*1258Smlf 
27*1258Smlf #ifndef _SATA_H
28*1258Smlf #define	_SATA_H
29*1258Smlf 
30*1258Smlf #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*1258Smlf 
32*1258Smlf #ifdef	__cplusplus
33*1258Smlf extern "C" {
34*1258Smlf #endif
35*1258Smlf 
36*1258Smlf /*
37*1258Smlf  * Generic SATA Host Adapter Implementation
38*1258Smlf  */
39*1258Smlf 
40*1258Smlf #include <sys/types.h>
41*1258Smlf #include <sys/scsi/scsi.h>
42*1258Smlf #include <sys/scsi/impl/services.h>
43*1258Smlf #include <sys/sata/sata_defs.h>
44*1258Smlf #include <sys/sata/sata_hba.h>
45*1258Smlf 
46*1258Smlf /* Statistics counters */
47*1258Smlf struct sata_port_stats {
48*1258Smlf 	uint64_t	link_lost;		/* event counter */
49*1258Smlf 	uint64_t	link_established;	/* event counter */
50*1258Smlf 	uint64_t	device_attached;	/* event counter */
51*1258Smlf 	uint64_t	device_detached;	/* event counter */
52*1258Smlf 	uint64_t	port_reset;		/* event counter */
53*1258Smlf 	uint64_t	port_pwr_changed;	/* event counter */
54*1258Smlf };
55*1258Smlf 
56*1258Smlf typedef struct sata_port_stats sata_port_stats_t;
57*1258Smlf 
58*1258Smlf struct sata_drive_stats {
59*1258Smlf 	uint64_t	media_error;		/* available ??? */
60*1258Smlf 	uint64_t	drive_reset;		/* event counter */
61*1258Smlf } sata_drv_stats_t;
62*1258Smlf 
63*1258Smlf typedef struct sata_drive_stats sata_drive_stats_t;
64*1258Smlf 
65*1258Smlf struct sata_ctrl_stats {
66*1258Smlf 	uint64_t	ctrl_reset;		/* event counter */
67*1258Smlf 	uint64_t	ctrl_pwr_change;	/* event counter */
68*1258Smlf };
69*1258Smlf 
70*1258Smlf typedef struct sata_ctrl_stats sata_ctrl_stats_t;
71*1258Smlf 
72*1258Smlf 
73*1258Smlf /*
74*1258Smlf  * SATA HBA instance info structure
75*1258Smlf  */
76*1258Smlf struct sata_hba_inst {
77*1258Smlf 	dev_info_t		*satahba_dip;	/* this HBA instance devinfo */
78*1258Smlf 	struct sata_hba_inst	*satahba_next;	/* ptr to next sata_hba_inst */
79*1258Smlf 	struct sata_hba_inst	*satahba_prev;	/* ptr to prev sata_hba_inst */
80*1258Smlf 	struct scsi_hba_tran	*satahba_scsi_tran; /* scsi_hba_tran */
81*1258Smlf 	struct sata_hba_tran	*satahba_tran;	/* sata_hba_tran */
82*1258Smlf 	kmutex_t		satahba_mutex;	/* sata hba cntrl mutex */
83*1258Smlf 
84*1258Smlf 						/*
85*1258Smlf 						 * HBA event flags:
86*1258Smlf 						 * SATA_EVNT_MAIN
87*1258Smlf 						 * SATA_EVNT_PWR_LEVEL_CHANGED
88*1258Smlf 						 * SATA_EVNT_SKIP
89*1258Smlf 						 */
90*1258Smlf 	uint_t			satahba_event_flags;
91*1258Smlf 
92*1258Smlf 	struct sata_cport_info	*satahba_dev_port[SATA_MAX_CPORTS];
93*1258Smlf 
94*1258Smlf 						/*
95*1258Smlf 						 * DEVCTL open flag:
96*1258Smlf 						 * SATA_DEVCTL_SOPENED
97*1258Smlf 						 * SATA_DEVCTL_EXOPENED
98*1258Smlf 						 */
99*1258Smlf 	uint_t			satahba_open_flag; /* shared open flag */
100*1258Smlf 	struct sata_ctrl_stats	satahba_stats;	/* HBA cntrl statistics */
101*1258Smlf 
102*1258Smlf 	uint_t			satahba_attached; /* HBA attaching: */
103*1258Smlf 						/* 0 - not completed */
104*1258Smlf 						/* 1 - completed */
105*1258Smlf };
106*1258Smlf 
107*1258Smlf typedef struct sata_hba_inst	sata_hba_inst_t;
108*1258Smlf 
109*1258Smlf /*
110*1258Smlf  * SATA controller's device port info and state.
111*1258Smlf  * This structure is pointed to by the sata_hba_inst.satahba_dev_port[x]
112*1258Smlf  * where x is a device port number.
113*1258Smlf  * cport_state holds port state flags, defined in sata_hba.h file.
114*1258Smlf  * cport_event_flags holds SATA_EVNT_* flags defined in this file and in
115*1258Smlf  * sata_hba.h file.
116*1258Smlf  * cport_dev_type holds SATA_DTYPE_* types defined in sata_hba.h file.
117*1258Smlf  */
118*1258Smlf struct sata_cport_info {
119*1258Smlf 	sata_address_t		cport_addr;	/* this port SATA address */
120*1258Smlf 	kmutex_t		cport_mutex;	/* port mutex */
121*1258Smlf 
122*1258Smlf 						/*
123*1258Smlf 						 * Port state flags
124*1258Smlf 						 * SATA_STATE_UNKNOWN
125*1258Smlf 						 * SATA_STATE_PROBING
126*1258Smlf 						 * SATA_STATE_PROBED
127*1258Smlf 						 * SATA_STATE_READY
128*1258Smlf 						 * SATA_PSTATE_PWRON
129*1258Smlf 						 * SATA_PSTATE_PWROFF
130*1258Smlf 						 * SATA_PSTATE_SHUTDOWN
131*1258Smlf 						 * SATA_PSTATE_FAILED
132*1258Smlf 						 */
133*1258Smlf 	uint32_t		cport_state;
134*1258Smlf 
135*1258Smlf 						/*
136*1258Smlf 						 * Port event flags:
137*1258Smlf 						 * SATA_EVNT_DEVICE_ATTACHED
138*1258Smlf 						 * SATA_EVNT_DEVICE_DETACHED
139*1258Smlf 						 * SATA_EVNT_LINK_LOST
140*1258Smlf 						 * SATA_EVNT_LINK_ESTABLISHED
141*1258Smlf 						 * SATA_EVNT_PORT_FAILED
142*1258Smlf 						 * SATA_EVNT_PWR_LEVEL_CHANGED
143*1258Smlf 						 */
144*1258Smlf 	uint32_t		cport_event_flags;
145*1258Smlf 
146*1258Smlf 	struct sata_port_scr	cport_scr;	/* Port status and ctrl regs */
147*1258Smlf 
148*1258Smlf 						/*
149*1258Smlf 						 * Attached device type:
150*1258Smlf 						 * SATA_DTYPE_NONE
151*1258Smlf 						 * SATA_DTYPE_ATADISK
152*1258Smlf 						 * SATA_DTYPE_ATAPICD
153*1258Smlf 						 * SATA_DTYPE_ATAPINONCD
154*1258Smlf 						 * SATA_DTYPE_PMULT
155*1258Smlf 						 * SATA_DTYPE_UNKNOWN
156*1258Smlf 						 */
157*1258Smlf 	uint32_t		cport_dev_type;
158*1258Smlf 	union {
159*1258Smlf 	    struct sata_drive_info *cport_sata_drive; /* Attached drive info */
160*1258Smlf 	    struct sata_pmult_info *cport_sata_pmult; /* Attached Port Mult */
161*1258Smlf 	} 			cport_devp;
162*1258Smlf 						/* lbolt value at link lost */
163*1258Smlf 	clock_t			cport_link_lost_time;
164*1258Smlf 
165*1258Smlf 	struct sata_port_stats	cport_stats;	/* Port statistics */
166*1258Smlf };
167*1258Smlf 
168*1258Smlf typedef struct sata_cport_info sata_cport_info_t;
169*1258Smlf 
170*1258Smlf /*
171*1258Smlf  * Attached SATA drive info and state.
172*1258Smlf  * This structure is pointed to by sata_cport_info's cport_sata_drive field
173*1258Smlf  * when a drive is attached directly to a controller device port.
174*1258Smlf  */
175*1258Smlf struct sata_drive_info {
176*1258Smlf 	sata_address_t	satadrv_addr;		/* this drive SATA address */
177*1258Smlf 
178*1258Smlf 						/*
179*1258Smlf 						 * Drive state flags
180*1258Smlf 						 * SATA_STATE_UNKNOWN
181*1258Smlf 						 * SATA_STATE_PROBING
182*1258Smlf 						 * SATA_STATE_PROBED
183*1258Smlf 						 * SATA_STATE_READY
184*1258Smlf 						 * SATA_DSTATE_PWR_ACTIVE
185*1258Smlf 						 * SATA_DSTATE_PWR_IDLE
186*1258Smlf 						 * SATA_DSTATE_RESET
187*1258Smlf 						 * SATA_DSTATE_FAILED
188*1258Smlf 						 */
189*1258Smlf 	uint32_t	satadrv_state;
190*1258Smlf 
191*1258Smlf 						/*
192*1258Smlf 						 * drive event flags:
193*1258Smlf 						 * SATA_EVNT_DRIVE_RESET
194*1258Smlf 						 */
195*1258Smlf 	uint32_t	satadrv_event_flags;
196*1258Smlf 
197*1258Smlf 						/*
198*1258Smlf 						 * Attached device type:
199*1258Smlf 						 * SATA_DTYPE_ATADISK
200*1258Smlf 						 * SATA_DTYPE_ATAPICD
201*1258Smlf 						 * SATA_DTYPE_ATAPINONCD
202*1258Smlf 						 */
203*1258Smlf 	uint32_t	satadrv_type;
204*1258Smlf 
205*1258Smlf 	uint32_t	satadrv_status_reg;	/* drive status reg */
206*1258Smlf 	uint32_t	satadrv_error_reg;	/* drive error reg */
207*1258Smlf 	uint16_t	satadrv_features_support; /* drive features support */
208*1258Smlf 	uint16_t	satadrv_queue_depth;    /* drive queue depth */
209*1258Smlf 	uint16_t	satadrv_settings;	/* drive settings flags */
210*1258Smlf 	uint16_t	satadrv_pad2;		/* struct alignment pad */
211*1258Smlf 	uint64_t	satadrv_capacity;	/* drive capacity */
212*1258Smlf 	sata_id_t	satadrv_id;		/* Device Identify Data */
213*1258Smlf 	struct sata_drive_stats satadrv_stats;	/* drive statistics */
214*1258Smlf };
215*1258Smlf 
216*1258Smlf typedef struct sata_drive_info sata_drive_info_t;
217*1258Smlf 
218*1258Smlf _NOTE(SCHEME_PROTECTS_DATA("unshared data", sata_drive_info))
219*1258Smlf 
220*1258Smlf 
221*1258Smlf /* Port Multiplier & host port info and state */
222*1258Smlf struct sata_pmult_info {
223*1258Smlf 	sata_address_t	pmult_addr;		/* this PMult SATA Address */
224*1258Smlf 	kmutex_t	pmult_mutex;		/* pmult (host port) mutex */
225*1258Smlf 
226*1258Smlf 						/*
227*1258Smlf 						 * PMult state flags
228*1258Smlf 						 * SATA_STATE_UNKNOWN
229*1258Smlf 						 * SATA_STATE_PROBING
230*1258Smlf 						 * SATA_STATE_PROBED
231*1258Smlf 						 * SATA_STATE_READY
232*1258Smlf 						 * SATA_PSTATE_FAILED
233*1258Smlf 						 */
234*1258Smlf 	uint32_t	pmult_state;
235*1258Smlf 	uint32_t	pmult_event_flags;	/* Undefined for now */
236*1258Smlf 	struct sata_port_scr pmult_scr;		/* Host port SCR block */
237*1258Smlf 	uint32_t	pmult_num_dev_ports; 	/* Number of data ports */
238*1258Smlf 	struct sata_pmport_info	*pmult_dev_port[SATA_MAX_PMPORTS - 1];
239*1258Smlf };
240*1258Smlf 
241*1258Smlf typedef	struct sata_pmult_info sata_pmult_info_t;
242*1258Smlf 
243*1258Smlf /* Port Multiplier's device port info & state */
244*1258Smlf struct sata_pmport_info {
245*1258Smlf 	sata_address_t	pmport_addr;		/* this SATA port address */
246*1258Smlf 	kmutex_t	pmport_mutex;		/* pmult device port mutex */
247*1258Smlf 
248*1258Smlf 						/*
249*1258Smlf 						 * Port state flags
250*1258Smlf 						 * SATA_STATE_UNKNOWN
251*1258Smlf 						 * SATA_STATE_PROBING
252*1258Smlf 						 * SATA_STATE_PROBED
253*1258Smlf 						 * SATA_STATE_READY
254*1258Smlf 						 * SATA_PSTATE_PWRON
255*1258Smlf 						 * SATA_PSTATE_PWROFF
256*1258Smlf 						 * SATA_PSTATE_SHUTDOWN
257*1258Smlf 						 * SATA_PSTATE_FAILED
258*1258Smlf 						 */
259*1258Smlf 	uint32_t	pmport_state;
260*1258Smlf 
261*1258Smlf 						/*
262*1258Smlf 						 * Port event flags:
263*1258Smlf 						 * SATA_EVNT_DEVICE_ATTACHED
264*1258Smlf 						 * SATA_EVNT_DEVICE_DETACHED
265*1258Smlf 						 * SATA_EVNT_LINK_LOST
266*1258Smlf 						 * SATA_EVNT_LINK_ESTABLISHED
267*1258Smlf 						 * SATA_EVNT_PORT_FAILED
268*1258Smlf 						 * SATA_EVNT_PWR_LEVEL_CHANGED
269*1258Smlf 						 */
270*1258Smlf 	uint32_t	pmport_event_flags;
271*1258Smlf 
272*1258Smlf 	struct sata_port_scr pmport_scr;	/* PMult device port scr */
273*1258Smlf 
274*1258Smlf 						/*
275*1258Smlf 						 * Attached device type:
276*1258Smlf 						 * SATA_DTYPE_NONE
277*1258Smlf 						 * SATA_DTYPE_ATADISK
278*1258Smlf 						 * SATA_DTYPE_ATAPICD
279*1258Smlf 						 * SATA_DTYPE_ATAPINONCD
280*1258Smlf 						 * SATA_DTYPE_UNKNOWN
281*1258Smlf 						 */
282*1258Smlf 	uint32_t	pmport_dev_type;
283*1258Smlf 
284*1258Smlf 	struct sata_drive_info	*pmport_sata_drive; /* Attached drive info */
285*1258Smlf 
286*1258Smlf 						/* lbolt value at link lost */
287*1258Smlf 	clock_t		pmport_link_lost_time;
288*1258Smlf 
289*1258Smlf 	struct sata_port_stats	pmport_stats;	/* Port statistics */
290*1258Smlf };
291*1258Smlf 
292*1258Smlf typedef	struct sata_pmport_info sata_pmport_info_t;
293*1258Smlf 
294*1258Smlf /*
295*1258Smlf  * Port SSTATUS register (sata_port_scr sport_sstatus field).
296*1258Smlf  * Link bits are valid only in port active state.
297*1258Smlf  */
298*1258Smlf #define	SATA_PORT_DEVLINK_UP	0x00000103	/* Link with dev established */
299*1258Smlf #define	SATA_PORT_DEVLINK_UP_MASK 0x0000010F	/* Mask for link bits */
300*1258Smlf 
301*1258Smlf /*
302*1258Smlf  * Port state clear mask (cport_state and pmport_state fields).
303*1258Smlf  * SATA_PSTATE_SHUTDOWN and power state are preserved.
304*1258Smlf  */
305*1258Smlf #define	SATA_PORT_STATE_CLEAR_MASK	(~(SATA_PSTATE_SHUTDOWN))
306*1258Smlf 
307*1258Smlf /*
308*1258Smlf  * Valid i.e.supported device types mask (cport_dev_type, satadrv_type,
309*1258Smlf  * pmult_dev_type fields).
310*1258Smlf  */
311*1258Smlf #define	SATA_VALID_DEV_TYPE	(SATA_DTYPE_ATADISK)	/* only disks now */
312*1258Smlf 
313*1258Smlf /*
314*1258Smlf  * Device feature_support (satadrv_features_support)
315*1258Smlf  */
316*1258Smlf #define	SATA_DEV_F_DMA			0x01
317*1258Smlf #define	SATA_DEV_F_LBA28		0x02
318*1258Smlf #define	SATA_DEV_F_LBA48		0x04
319*1258Smlf #define	SATA_DEV_F_NCQ			0x08
320*1258Smlf #define	SATA_DEV_F_SATA1		0x10
321*1258Smlf #define	SATA_DEV_F_SATA2		0x20
322*1258Smlf 
323*1258Smlf /*
324*1258Smlf  * Drive settings flags (satdrv_settings)
325*1258Smlf  */
326*1258Smlf #define	SATA_DEV_READ_AHEAD		0x0001	/* Read Ahead enabled */
327*1258Smlf #define	SATA_DEV_WRITE_CACHE		0x0002	/* Write cache ON */
328*1258Smlf #define	SATA_DEV_SERIAL_FEATURES 	0x8000	/* Serial ATA feat. enabled */
329*1258Smlf #define	SATA_DEV_ASYNCH_NOTIFY		0x2000	/* Asynch-event enabled */
330*1258Smlf 
331*1258Smlf 
332*1258Smlf /*
333*1258Smlf  * Internal event and flags.
334*1258Smlf  * These flags are set in the *_event_flags fields of various structures.
335*1258Smlf  * Events and lock flags defined below are used internally by the
336*1258Smlf  * SATA framework (they are not reported by SATA HBA drivers).
337*1258Smlf  */
338*1258Smlf #define	SATA_EVNT_MAIN			0x80000000
339*1258Smlf #define	SATA_EVNT_SKIP			0x40000000
340*1258Smlf #define	SATA_EVNT_INPROC_DEVICE_RESET	0x08000000
341*1258Smlf #define	SATA_EVNT_CLEAR_DEVICE_RESET	0x04000000
342*1258Smlf 
343*1258Smlf /*
344*1258Smlf  * Lock flags - used to serialize configuration operations
345*1258Smlf  * on ports and devices.
346*1258Smlf  * SATA_EVNT_LOCK_PORT_BUSY is set by event daemon to prevent
347*1258Smlf  * simultaneous cfgadm operations.
348*1258Smlf  * SATA_APCTL_LOCK_PORT_BUSY is set by cfgadm ioctls to prevent
349*1258Smlf  * simultaneous event processing.
350*1258Smlf  */
351*1258Smlf #define	SATA_EVNT_LOCK_PORT_BUSY	0x00800000
352*1258Smlf #define	SATA_APCTL_LOCK_PORT_BUSY	0x00400000
353*1258Smlf 
354*1258Smlf /* Mask for port events */
355*1258Smlf #define	SATA_EVNT_PORT_EVENTS		(SATA_EVNT_DEVICE_ATTACHED | \
356*1258Smlf 					SATA_EVNT_DEVICE_DETACHED | \
357*1258Smlf 					SATA_EVNT_LINK_LOST | \
358*1258Smlf 					SATA_EVNT_LINK_ESTABLISHED | \
359*1258Smlf 					SATA_EVNT_PORT_FAILED)
360*1258Smlf /* Mask for drive events */
361*1258Smlf #define	SATA_EVNT_DRIVE_EVENTS		SATA_EVNT_DEVICE_RESET
362*1258Smlf #define	SATA_EVNT_CONTROLLER_EVENTS	SATA_EVNT_PWR_LEVEL_CHANGED
363*1258Smlf 
364*1258Smlf /* Delays and timeounts definitions */
365*1258Smlf #define	SATA_EVNT_DAEMON_SLEEP_TIME	50000	/* 50 ms */
366*1258Smlf #define	SATA_EVNT_DAEMON_TERM_TIMEOUT	100000	/* 100 ms */
367*1258Smlf #define	SATA_EVNT_DAEMON_TERM_WAIT	60000000 /* 60 s */
368*1258Smlf #define	SATA_EVNT_LINK_LOST_TIMEOUT	1000000	/* 1 s */
369*1258Smlf 
370*1258Smlf #define	SATA_DEVICE_IDENTIFY_RETRY	2
371*1258Smlf 
372*1258Smlf /*
373*1258Smlf  * sata_scsi's hba_open_flag: field indicating open devctl instance.
374*1258Smlf  *	0 = closed, 1 = shared open, 2 = exclusive open.
375*1258Smlf  */
376*1258Smlf #define	SATA_DEVCTL_CLOSED	0
377*1258Smlf #define	SATA_DEVCTL_SOPENED	1
378*1258Smlf #define	SATA_DEVCTL_EXOPENED	2
379*1258Smlf 
380*1258Smlf /*
381*1258Smlf  * sata_pkt_txlate structure contains info about resources allocated
382*1258Smlf  * for the packet
383*1258Smlf  * Address of this structure is stored in scsi_pkt.pkt_ha_private and
384*1258Smlf  * in sata_pkt.sata_hba_private fields, so all three strucures are
385*1258Smlf  * cross-linked, with sata_pkt_txlate as a centerpiece.
386*1258Smlf  */
387*1258Smlf 
388*1258Smlf typedef struct sata_pkt_txlate {
389*1258Smlf 	struct sata_hba_inst	*txlt_sata_hba_inst;
390*1258Smlf 	struct scsi_pkt		*txlt_scsi_pkt;
391*1258Smlf 	struct sata_pkt		*txlt_sata_pkt;
392*1258Smlf 	ddi_dma_handle_t	txlt_buf_dma_handle;
393*1258Smlf 	uint_t			txlt_flags;	/* data-in / data-out */
394*1258Smlf 	uint_t			txlt_num_dma_win; /* number of DMA windows */
395*1258Smlf 	uint_t			txlt_cur_dma_win; /* current DMA window */
396*1258Smlf 
397*1258Smlf 				/* cookies in the current DMA window */
398*1258Smlf 	uint_t			txlt_curwin_num_dma_cookies;
399*1258Smlf 
400*1258Smlf 				/* procesed dma cookies in current DMA win */
401*1258Smlf 	uint_t			txlt_curwin_processed_dma_cookies;
402*1258Smlf 	size_t			txlt_total_residue;
403*1258Smlf 	int			txlt_dma_cookie_list_len; /* alloc list len */
404*1258Smlf 	ddi_dma_cookie_t 	*txlt_dma_cookie_list; /* dma cookie list */
405*1258Smlf 	int			txlt_num_dma_cookies; /* dma cookies in list */
406*1258Smlf } sata_pkt_txlate_t;
407*1258Smlf 
408*1258Smlf _NOTE(SCHEME_PROTECTS_DATA("unshared data", sata_pkt_txlate))
409*1258Smlf _NOTE(SCHEME_PROTECTS_DATA("unshared data", scsi_pkt))
410*1258Smlf 
411*1258Smlf 
412*1258Smlf /*
413*1258Smlf  * Additional scsi sense code definitions.
414*1258Smlf  * These definition should eventually be moved to scsi header files.
415*1258Smlf  */
416*1258Smlf #define	SD_SCSI_NO_ADD_SENSE			0x00
417*1258Smlf #define	SD_SCSI_LU_NOT_READY			0x04
418*1258Smlf #define	SD_SCSI_WRITE_ERROR			0x0c
419*1258Smlf #define	SD_SCSI_UNREC_READ_ERROR		0x11
420*1258Smlf #define	SD_SCSI_INVALID_COMMAND_CODE		0x20
421*1258Smlf #define	SD_SCSI_LBA_OUT_OF_RANGE		0x21
422*1258Smlf #define	SD_SCSI_INVALID_FIELD_IN_CDB		0x24
423*1258Smlf #define	SD_SCSI_INVALID_FIELD_IN_PARAMETER_LIST 0x26
424*1258Smlf #define	SD_SCSI_SAVING_PARAMS_NOT_SUP		0x39
425*1258Smlf 
426*1258Smlf 
427*1258Smlf /* SCSI defs missing from scsi headers */
428*1258Smlf /* Missing from sys/scsi/generic/commands.h */
429*1258Smlf #define	SCMD_SYNCHRONIZE_CACHE_G1		0x91
430*1258Smlf /*
431*1258Smlf  * Missing from sys/scsi/impl/mode.h, although defined
432*1258Smlf  * in sys/scsi/targets/sddefs.h as MODEPAGE_ERR_RECOV
433*1258Smlf  */
434*1258Smlf #define	MODEPAGE_RW_ERRRECOV			0x01 /* read/write recovery */
435*1258Smlf 
436*1258Smlf /*
437*1258Smlf  * Macros for accessing various structure fields
438*1258Smlf  *
439*1258Smlf  */
440*1258Smlf 
441*1258Smlf #define	SATA_TRAN(sata_hba_inst) \
442*1258Smlf 	sata_hba_inst->satahba_tran
443*1258Smlf 
444*1258Smlf #define	SATA_DIP(sata_hba_inst) \
445*1258Smlf 	sata_hba_inst->satahba_dip
446*1258Smlf 
447*1258Smlf #define	SATA_NUM_CPORTS(sata_hba_inst) \
448*1258Smlf 	sata_hba_inst->satahba_tran->sata_tran_hba_num_cports
449*1258Smlf 
450*1258Smlf #define	SATA_QDEPTH(sata_hba_inst) \
451*1258Smlf 	sata_hba_inst->satahba_tran->sata_tran_hba_qdepth
452*1258Smlf 
453*1258Smlf #define	SATA_FEATURES(sata_hba_inst) \
454*1258Smlf 	sata_hba_inst->satahba_tran->sata_tran_hba_features_support
455*1258Smlf 
456*1258Smlf #define	SATA_DMA_ATTR(sata_hba_inst) \
457*1258Smlf 	sata_hba_inst->satahba_tran->sata_tran_hba_dma_attr
458*1258Smlf 
459*1258Smlf #define	SATA_START_FUNC(sata_hba_inst) \
460*1258Smlf 	sata_hba_inst->satahba_tran->sata_tran_start
461*1258Smlf 
462*1258Smlf #define	SATA_ABORT_FUNC(sata_hba_inst) \
463*1258Smlf 	sata_hba_inst->satahba_tran->sata_tran_abort
464*1258Smlf 
465*1258Smlf #define	SATA_RESET_DPORT_FUNC(sata_hba_inst) \
466*1258Smlf 	sata_hba_inst->satahba_tran->sata_tran_reset_dport
467*1258Smlf 
468*1258Smlf #define	SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) \
469*1258Smlf 	(sata_hba_inst->satahba_tran->sata_tran_hotplug_ops == NULL ? \
470*1258Smlf 	NULL : \
471*1258Smlf 	sata_hba_inst->satahba_tran->sata_tran_hotplug_ops->\
472*1258Smlf 	sata_tran_port_deactivate)
473*1258Smlf 
474*1258Smlf #define	SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) \
475*1258Smlf 	(sata_hba_inst->satahba_tran->sata_tran_hotplug_ops == NULL ? \
476*1258Smlf 	NULL : \
477*1258Smlf 	sata_hba_inst->satahba_tran->sata_tran_hotplug_ops->\
478*1258Smlf 	sata_tran_port_activate)
479*1258Smlf 
480*1258Smlf #define	SATA_PROBE_PORT_FUNC(sata_hba_inst) \
481*1258Smlf 	sata_hba_inst->satahba_tran->sata_tran_probe_port
482*1258Smlf 
483*1258Smlf #define	SATA_SELFTEST_FUNC(sata_hba_inst) \
484*1258Smlf 	sata_hba_inst->satahba_tran->sata_tran_selftest
485*1258Smlf 
486*1258Smlf #define	SATA_CPORT_MUTEX(sata_hba_inst, cport) \
487*1258Smlf 	sata_hba_inst->satahba_dev_port[cport]->cport_mutex
488*1258Smlf 
489*1258Smlf #define	SATA_CPORT_INFO(sata_hba_inst, cport) \
490*1258Smlf 	sata_hba_inst->satahba_dev_port[cport]
491*1258Smlf 
492*1258Smlf #define	SATA_CPORT_STATE(sata_hba_inst, cport) \
493*1258Smlf 	sata_hba_inst->satahba_dev_port[cport]->cport_state
494*1258Smlf 
495*1258Smlf #define	SATA_CPORT_SCR(sata_hba_inst, cport) \
496*1258Smlf 	sata_hba_inst->satahba_dev_port[cport]->cport_scr
497*1258Smlf 
498*1258Smlf #define	SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) \
499*1258Smlf 	sata_hba_inst->satahba_dev_port[cport]->cport_dev_type
500*1258Smlf 
501*1258Smlf #define	SATA_CPORT_DRV_INFO(sata_hba_inst, cport) \
502*1258Smlf 	sata_hba_inst->satahba_dev_port[cport]->cport_devp.cport_sata_drive
503*1258Smlf 
504*1258Smlf #define	SATA_CPORTINFO_DRV_TYPE(cportinfo) \
505*1258Smlf 	cportinfo->cport_dev_type
506*1258Smlf 
507*1258Smlf #define	SATA_CPORTINFO_DRV_INFO(cportinfo) \
508*1258Smlf 	cportinfo->cport_devp.cport_sata_drive
509*1258Smlf 
510*1258Smlf #define	SATA_CPORTINFO_PMULT_INFO(cportinfo) \
511*1258Smlf 	cportinfo->cport_devp.cport_sata_pmult
512*1258Smlf 
513*1258Smlf #define	SATA_PMULT_INFO(sata_hba_inst, cport) \
514*1258Smlf 	sata_hba_inst->satahba_dev_port[cport]->cport_devp.cport_sata_pmult
515*1258Smlf 
516*1258Smlf #define	SATA_NUM_PMPORTS(sata_hba_inst, cport) \
517*1258Smlf 	sata_hba_inst->satahba_dev_port[cport]->\
518*1258Smlf 	cport_devp.cport_sata_pmult->pmult_num_dev_ports
519*1258Smlf 
520*1258Smlf #define	SATA_PMPORT_INFO(sata_hba_inst, cport, pmport) \
521*1258Smlf 	sata_hba_inst->satahba_dev_port[cport]->\
522*1258Smlf 	cport_devp.cport_sata_pmult->pmult_dev_port[pmport]
523*1258Smlf 
524*1258Smlf #define	SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport) \
525*1258Smlf 	sata_hba_inst->satahba_dev_port[cport]->\
526*1258Smlf 	cport_devp.cport_sata_pmult->pmult_dev_port[pmport]->\
527*1258Smlf 	pmport_sata_drive
528*1258Smlf 
529*1258Smlf #define	SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) \
530*1258Smlf 	sata_hba_inst->satahba_dev_port[cport]->\
531*1258Smlf 	cport_devp.cport_sata_pmult->pmult_dev_port[pmport]->pmport_state
532*1258Smlf 
533*1258Smlf #define	SATA_PMPORT_SCR(sata_hba_inst, cport, pmport) \
534*1258Smlf 	sata_hba_inst->satahba_dev_port[cport]->\
535*1258Smlf 	cport_devp.cport_sata_pmult->pmult_dev_port[pmport]->pmport_scr
536*1258Smlf 
537*1258Smlf #define	SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, pmport) \
538*1258Smlf 	sata_hba_inst->satahba_dev_port[cport]->\
539*1258Smlf 	cport_devp.cport_sata_pmult->pmult_dev_port[pmport]->pmport_dev_type
540*1258Smlf 
541*1258Smlf #define	SATA_TXLT_HBA_INST(spx) \
542*1258Smlf 	spx->txlt_sata_hba_inst
543*1258Smlf 
544*1258Smlf #define	SATA_TXLT_CPORT(spx) \
545*1258Smlf 	spx->txlt_sata_pkt->satapkt_device.satadev_addr.cport
546*1258Smlf 
547*1258Smlf #define	SATA_TXLT_CPORT_MUTEX(spx) \
548*1258Smlf 	spx->txlt_sata_hba_inst->\
549*1258Smlf 	satahba_dev_port[spx->txlt_sata_pkt->\
550*1258Smlf 	satapkt_device.satadev_addr.cport]->cport_mutex
551*1258Smlf 
552*1258Smlf /*
553*1258Smlf  * Minor number construction for devctl and attachment point nodes.
554*1258Smlf  * All necessary information has to be encoded in NBITSMINOR32 bits.
555*1258Smlf  *
556*1258Smlf  * Devctl node minor number:
557*1258Smlf  * ((controller_instance << SATA_CNTRL_INSTANCE_SHIFT) | SATA_DEVCTL_NODE)
558*1258Smlf  *
559*1258Smlf  * Attachment point node minor number has to include controller
560*1258Smlf  * instance (7 bits), controller port number (5 bits) and port multiplier
561*1258Smlf  * device port number (4 bits) and port multiplier device port
562*1258Smlf  * indicator (1 bit).  Additionally, a single bit is used to
563*1258Smlf  * differentiate between attachment point node and device control node.
564*1258Smlf  *
565*1258Smlf  * Attachment point minor number:
566*1258Smlf  * ((controller_instance << SATA_CNTRL_INSTANCE_SHIFT) | SATA_AP_NODE |
567*1258Smlf  * [(port_multiplier_device_port << SATA_PMULT_PORT_SHIFT) | SATA_PMULT_AP] |
568*1258Smlf  * (controller_port))
569*1258Smlf  *
570*1258Smlf  * 17 bits are used (if 64 instances of controllers are expected)
571*1258Smlf  * bit 18 is reserved for future use.
572*1258Smlf  *
573*1258Smlf  *   --------------------------------------------------------
574*1258Smlf  *   |17|16|15|14|13|12|11|10 |09|08|07|06|05|04|03|02|01|00|
575*1258Smlf  *   --------------------------------------------------------
576*1258Smlf  *   | R| c| c| c| c| c| c|a/d|pm|pp|pp|pp|pp|cp|cp|cp|cp|cp|
577*1258Smlf  *   --------------------------------------------------------
578*1258Smlf  * Where:
579*1258Smlf  * cp  - device port number on the HBA SATA controller
580*1258Smlf  * pp  - device port number on the port multiplier
581*1258Smlf  * pm  - 0 - target attached to controller device port
582*1258Smlf  *       1 - target attached to port multiplier's device port
583*1258Smlf  * a/d - 0 - devctl node
584*1258Smlf  *       1 - attachment point node
585*1258Smlf  * c   - controller number
586*1258Smlf  * R   - reserved bit
587*1258Smlf  */
588*1258Smlf 
589*1258Smlf #define	SATA_AP_NODE		0x400		/* Attachment Point node */
590*1258Smlf #define	SATA_DEVCTL_NODE	0x000		/* DEVCTL node */
591*1258Smlf #define	SATA_PMULT_AP		0x200		/* device on PMult port */
592*1258Smlf #define	SATA_PMULT_PORT_SHIFT	5
593*1258Smlf #define	SATA_CNTRL_INSTANCE_SHIFT 11
594*1258Smlf #define	SATA_CPORT_MASK		0x1f		/* 32 device ports */
595*1258Smlf #define	SATA_PMULT_PORT_MASK	0xf		/* 15 device ports */
596*1258Smlf #define	SATA_CNTRL_INSTANCE_MASK 0x03F		/* 64 controllers */
597*1258Smlf 
598*1258Smlf /* Macro for creating devctl node minor number */
599*1258Smlf #define	SATA_MAKE_DEVCTL_MINOR(controller_instance) \
600*1258Smlf 	((controller_instance << SATA_CNTRL_INSTANCE_SHIFT) | \
601*1258Smlf 	SATA_DEVCTL_NODE)
602*1258Smlf 
603*1258Smlf /* Macro for creating an attachment point node minor number */
604*1258Smlf #define	SATA_MAKE_AP_MINOR(cntrl_instance, cport, pmport, qual) \
605*1258Smlf 	(qual & (SATA_ADDR_PMPORT | SATA_ADDR_DPMPORT) ? \
606*1258Smlf 	(((cntrl_instance) << SATA_CNTRL_INSTANCE_SHIFT) | \
607*1258Smlf 	SATA_AP_NODE | SATA_PMULT_AP | \
608*1258Smlf 	(pmport << SATA_PMULT_PORT_SHIFT) | cport) : \
609*1258Smlf 	(((cntrl_instance) << SATA_CNTRL_INSTANCE_SHIFT) | \
610*1258Smlf 	SATA_AP_NODE | cport))
611*1258Smlf 
612*1258Smlf /* Macro retrieving controller number from a minor number */
613*1258Smlf #define	SATA_MINOR2INSTANCE(minor) \
614*1258Smlf 	((minor >> SATA_CNTRL_INSTANCE_SHIFT) & SATA_CNTRL_INSTANCE_MASK)
615*1258Smlf 
616*1258Smlf 
617*1258Smlf /*
618*1258Smlf  * SCSI target number format
619*1258Smlf  *
620*1258Smlf  *   -------------------------------
621*1258Smlf  *   | 9| 8| 7| 6| 5| 4| 3| 2| 1| 0|    Bit number
622*1258Smlf  *   -------------------------------
623*1258Smlf  *   |pm|pp|pp|pp|pp|cp|cp|cp|cp|cp|
624*1258Smlf  *   -------------------------------
625*1258Smlf  * Where:
626*1258Smlf  * cp  - device port number on the HBA SATA controller
627*1258Smlf  * pp  - device port number on the port multiplier
628*1258Smlf  * pm  - 0 - target attached to controller device port
629*1258Smlf  *       1 - target attached to port multiplier's device port
630*1258Smlf  */
631*1258Smlf 
632*1258Smlf /* SATA ports to SCSI target number translation */
633*1258Smlf 
634*1258Smlf #define	SATA_TO_SCSI_TARGET(cport, pmport, qual) \
635*1258Smlf 	(qual == SATA_ADDR_DCPORT ? cport : \
636*1258Smlf 	(cport | (pmport << SATA_PMULT_PORT_SHIFT) | SATA_PMULT_AP))
637*1258Smlf 
638*1258Smlf /* SCSI target number to SATA cntrl/pmport/cport translations */
639*1258Smlf #define	SCSI_TO_SATA_CPORT(scsi_target) \
640*1258Smlf 	(scsi_target & SATA_CPORT_MASK)
641*1258Smlf 
642*1258Smlf #define	SCSI_TO_SATA_PMPORT(scsi_target) \
643*1258Smlf 	((scsi_target >> SATA_PMULT_PORT_SHIFT) & SATA_PMULT_PORT_MASK)
644*1258Smlf 
645*1258Smlf #define	SCSI_TO_SATA_ADDR_QUAL(scsi_target) \
646*1258Smlf 	((scsi_target & SATA_PMULT_AP) ? SATA_ADDR_DPMPORT : \
647*1258Smlf 	SATA_ADDR_DCPORT)
648*1258Smlf 
649*1258Smlf 
650*1258Smlf /* Debug flags */
651*1258Smlf #if	DEBUG
652*1258Smlf 
653*1258Smlf #define	SATA_DEBUG
654*1258Smlf #define	SATA_DBG_SCSI_IF	1
655*1258Smlf #define	SATA_DBG_HBA_IF		2
656*1258Smlf #define	SATA_DBG_NODES		4
657*1258Smlf #define	SATA_DBG_IOCTL_IF	8
658*1258Smlf #define	SATA_DBG_EVENTS		0x10
659*1258Smlf #define	SATA_DBG_EVENTS_PROC	0x20
660*1258Smlf #define	SATA_DBG_EVENTS_PROCPST	0x40
661*1258Smlf #define	SATA_DBG_EVENTS_CNTRL	0x80
662*1258Smlf #define	SATA_DBG_EVENTS_DAEMON	0x100
663*1258Smlf #define	SATA_DBG_DMA_SETUP	0x400
664*1258Smlf 
665*1258Smlf extern int sata_debug_flag;
666*1258Smlf 
667*1258Smlf /* Debug macros */
668*1258Smlf #define	SATADBG1(flag, sata, format, arg1) \
669*1258Smlf 	if (sata_debug_flags & (flag)) { \
670*1258Smlf 		sata_log(sata, CE_CONT, format, arg1); \
671*1258Smlf 	}
672*1258Smlf 
673*1258Smlf #define	SATADBG2(flag, sata, format, arg1, arg2) \
674*1258Smlf 	if (sata_debug_flags & (flag)) { \
675*1258Smlf 		sata_log(sata, CE_CONT, format, arg1, arg2); \
676*1258Smlf 	}
677*1258Smlf 
678*1258Smlf #define	SATADBG3(flag, sata, format, arg1, arg2, arg3) \
679*1258Smlf 	if (sata_debug_flags & (flag)) { \
680*1258Smlf 		sata_log(sata, CE_CONT, format, arg1, arg2, arg3); \
681*1258Smlf 	}
682*1258Smlf #else
683*1258Smlf 
684*1258Smlf #define	SATADBG1(flag, dip, frmt, arg1)
685*1258Smlf #define	SATADBG2(flag, dip, frmt, arg1, arg2)
686*1258Smlf #define	SATADBG3(flag, dip, frmt, arg1, arg2, arg3)
687*1258Smlf 
688*1258Smlf #endif
689*1258Smlf 
690*1258Smlf #ifdef	__cplusplus
691*1258Smlf }
692*1258Smlf #endif
693*1258Smlf 
694*1258Smlf #endif /* _SATA_H */
695