xref: /titanic_50/usr/src/uts/common/sys/sata/impl/sata.h (revision 3c112a2b34403220c06c3e2fcac403358cfba168)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef _SATA_H
28 #define	_SATA_H
29 
30 #ifdef	__cplusplus
31 extern "C" {
32 #endif
33 
34 /*
35  * Generic SATA Host Adapter Implementation
36  */
37 
38 #include <sys/types.h>
39 #include <sys/scsi/scsi.h>
40 #include <sys/scsi/impl/services.h>
41 #include <sys/sata/sata_defs.h>
42 #include <sys/sata/sata_hba.h>
43 
44 /* Common flags specifying current state of a port or an attached drive. */
45 #define	SATA_STATE_PROBING		0x000001
46 #define	SATA_STATE_PROBED		0x000002
47 
48 /* Statistics counters */
49 struct sata_port_stats {
50 	uint64_t	link_lost;		/* event counter */
51 	uint64_t	link_established;	/* event counter */
52 	uint64_t	device_attached;	/* event counter */
53 	uint64_t	device_detached;	/* event counter */
54 	uint64_t	port_reset;		/* event counter */
55 	uint64_t	port_pwr_changed;	/* event counter */
56 };
57 
58 typedef struct sata_port_stats sata_port_stats_t;
59 
60 struct sata_drive_stats {
61 	uint64_t	media_error;		/* available ??? */
62 	uint64_t	drive_reset;		/* event counter */
63 } sata_drv_stats_t;
64 
65 typedef struct sata_drive_stats sata_drive_stats_t;
66 
67 struct sata_ctrl_stats {
68 	uint64_t	ctrl_reset;		/* event counter */
69 	uint64_t	ctrl_pwr_change;	/* event counter */
70 };
71 
72 typedef struct sata_ctrl_stats sata_ctrl_stats_t;
73 
74 
75 /*
76  * SATA HBA instance info structure
77  */
78 struct sata_hba_inst {
79 	dev_info_t		*satahba_dip;	/* this HBA instance devinfo */
80 	struct sata_hba_inst	*satahba_next;	/* ptr to next sata_hba_inst */
81 	struct sata_hba_inst	*satahba_prev;	/* ptr to prev sata_hba_inst */
82 	struct scsi_hba_tran	*satahba_scsi_tran; /* scsi_hba_tran */
83 	struct sata_hba_tran	*satahba_tran;	/* sata_hba_tran */
84 	kmutex_t		satahba_mutex;	/* sata hba cntrl mutex */
85 	struct taskq		*satahba_taskq;	/* cmd completion task queue */
86 
87 						/*
88 						 * HBA event flags:
89 						 * SATA_EVNT_MAIN
90 						 * SATA_EVNT_PWR_LEVEL_CHANGED
91 						 * SATA_EVNT_SKIP
92 						 */
93 	uint_t			satahba_event_flags;
94 
95 	struct sata_cport_info	*satahba_dev_port[SATA_MAX_CPORTS];
96 
97 						/*
98 						 * DEVCTL open flag:
99 						 * SATA_DEVCTL_SOPENED
100 						 * SATA_DEVCTL_EXOPENED
101 						 */
102 	uint_t			satahba_open_flag; /* shared open flag */
103 	struct sata_ctrl_stats	satahba_stats;	/* HBA cntrl statistics */
104 
105 	uint_t			satahba_attached; /* HBA attaching: */
106 						/* 0 - not completed */
107 						/* 1 - completed */
108 };
109 
110 typedef struct sata_hba_inst	sata_hba_inst_t;
111 
112 /*
113  * SATA controller's device port info and state.
114  * This structure is pointed to by the sata_hba_inst.satahba_dev_port[x]
115  * where x is a device port number.
116  * cport_state holds port state flags, defined in sata_hba.h file.
117  * cport_event_flags holds SATA_EVNT_* flags defined in this file and in
118  * sata_hba.h file.
119  * cport_dev_type holds SATA_DTYPE_* types defined in sata_hba.h file.
120  */
121 struct sata_cport_info {
122 	sata_address_t		cport_addr;	/* this port SATA address */
123 	kmutex_t		cport_mutex;	/* port mutex */
124 
125 						/*
126 						 * Port state flags
127 						 * SATA_STATE_UNKNOWN
128 						 * SATA_STATE_PROBING
129 						 * SATA_STATE_PROBED
130 						 * SATA_STATE_READY
131 						 * SATA_PSTATE_PWRON
132 						 * SATA_PSTATE_PWROFF
133 						 * SATA_PSTATE_SHUTDOWN
134 						 * SATA_PSTATE_FAILED
135 						 */
136 	uint32_t		cport_state;
137 
138 						/*
139 						 * Port event flags:
140 						 * SATA_EVNT_DEVICE_ATTACHED
141 						 * SATA_EVNT_DEVICE_DETACHED
142 						 * SATA_EVNT_LINK_LOST
143 						 * SATA_EVNT_LINK_ESTABLISHED
144 						 * SATA_EVNT_PORT_FAILED
145 						 * SATA_EVNT_PWR_LEVEL_CHANGED
146 						 */
147 	uint32_t		cport_event_flags;
148 
149 	struct sata_port_scr	cport_scr;	/* Port status and ctrl regs */
150 
151 						/*
152 						 * Attached device type:
153 						 * SATA_DTYPE_NONE
154 						 * SATA_DTYPE_ATADISK
155 						 * SATA_DTYPE_ATAPICD
156 						 * SATA_DTYPE_ATAPITAPE
157 						 * SATA_DTYPE_ATAPIDISK
158 						 * SATA_DTYPE_PMULT
159 						 * SATA_DTYPE_UNKNOWN
160 						 */
161 	uint32_t		cport_dev_type;
162 	union {
163 	    struct sata_drive_info *cport_sata_drive; /* Attached drive info */
164 	    struct sata_pmult_info *cport_sata_pmult; /* Attached Port Mult */
165 	} 			cport_devp;
166 						/* lbolt value at link lost */
167 	clock_t			cport_link_lost_time;
168 						/* lbolt value @ dev attached */
169 	clock_t			cport_dev_attach_time;
170 
171 	struct sata_port_stats	cport_stats;	/* Port statistics */
172 
173 	boolean_t		cport_tgtnode_clean; /* Target node usable */
174 };
175 
176 typedef struct sata_cport_info sata_cport_info_t;
177 
178 /*
179  * Attached SATA drive info and state.
180  * This structure is pointed to by sata_cport_info's cport_sata_drive field
181  * when a drive is attached directly to a controller device port.
182  */
183 struct sata_drive_info {
184 	sata_address_t	satadrv_addr;		/* this drive SATA address */
185 
186 						/*
187 						 * Drive state flags
188 						 * SATA_STATE_UNKNOWN
189 						 * SATA_STATE_PROBING
190 						 * SATA_STATE_PROBED
191 						 * SATA_STATE_READY
192 						 * SATA_DSTATE_PWR_ACTIVE
193 						 * SATA_DSTATE_PWR_IDLE
194 						 * SATA_DSTATE_RESET
195 						 * SATA_DSTATE_FAILED
196 						 */
197 	uint32_t	satadrv_state;
198 
199 						/*
200 						 * drive event flags:
201 						 * SATA_EVNT_DRIVE_RESET
202 						 */
203 	uint32_t	satadrv_event_flags;
204 						/*
205 						 * lbolt value @ start of
206 						 * device reset processing
207 						 */
208 	clock_t		satadrv_reset_time;
209 						/*
210 						 * Attached device type:
211 						 * SATA_DTYPE_ATADISK
212 						 * SATA_DTYPE_ATAPICD
213 						 * SATA_DTYPE_ATAPITAPE
214 						 * SATA_DTYPE_ATAPIDISK
215 						 */
216 	uint32_t	satadrv_type;
217 
218 	uint32_t	satadrv_status_reg;	/* drive status reg */
219 	uint32_t	satadrv_error_reg;	/* drive error reg */
220 	uint16_t	satadrv_features_support; /* drive features support */
221 	uint16_t	satadrv_queue_depth;    /* drive queue depth */
222 	uint16_t	satadrv_atapi_cdb_len;	/* atapi supported cdb length */
223 	uint16_t	satadrv_atapi_trans_ver; /* atapi transport version */
224 	uint16_t	satadrv_settings;	/* drive settings flags */
225 	uint16_t	satadrv_features_enabled; /* drive features enabled */
226 	uint64_t	satadrv_capacity;	/* drive capacity */
227 	uint64_t	satadrv_max_queue_depth; /* maximum queue depth */
228 	sata_id_t	satadrv_id;		/* Device Identify Data */
229 	struct sata_drive_stats satadrv_stats;	/* drive statistics */
230 
231 	/*
232 	 * saved standby timer
233 	 * [0] - [3] = high - low
234 	 */
235 	uint8_t		satadrv_standby_timer[4];
236 	uint8_t		satadrv_power_level; /* saved power level */
237 };
238 
239 typedef struct sata_drive_info sata_drive_info_t;
240 
241 _NOTE(SCHEME_PROTECTS_DATA("unshared data", sata_drive_info))
242 
243 
244 /* Port Multiplier & host port info and state */
245 struct sata_pmult_info {
246 	sata_address_t	pmult_addr;		/* this PMult SATA Address */
247 						/*
248 						 * PMult state flags
249 						 * SATA_STATE_UNKNOWN
250 						 * SATA_STATE_PROBING
251 						 * SATA_STATE_PROBED
252 						 * SATA_STATE_READY
253 						 * SATA_PSTATE_FAILED
254 						 */
255 	uint32_t	pmult_state;
256 	uint32_t	pmult_event_flags;	/* Undefined for now */
257 	struct sata_pmult_gscr pmult_gscr;	/* PMult GSCR block */
258 	uint32_t	pmult_num_dev_ports; 	/* Number of data ports */
259 	struct sata_pmport_info	*pmult_dev_port[SATA_MAX_PMPORTS - 1];
260 };
261 
262 typedef	struct sata_pmult_info sata_pmult_info_t;
263 
264 _NOTE(SCHEME_PROTECTS_DATA("unshared data", sata_pmult_info))
265 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
266     sata_pmult_info::pmult_dev_port))
267 
268 /* Port Multiplier's device port info & state */
269 struct sata_pmport_info {
270 	sata_address_t	pmport_addr;		/* this SATA port address */
271 	kmutex_t	pmport_mutex;		/* pmult device port mutex */
272 
273 						/*
274 						 * Port state flags
275 						 * SATA_STATE_UNKNOWN
276 						 * SATA_STATE_PROBING
277 						 * SATA_STATE_PROBED
278 						 * SATA_STATE_READY
279 						 * SATA_PSTATE_PWRON
280 						 * SATA_PSTATE_PWROFF
281 						 * SATA_PSTATE_SHUTDOWN
282 						 * SATA_PSTATE_FAILED
283 						 */
284 	uint32_t	pmport_state;
285 
286 						/*
287 						 * Port event flags:
288 						 * SATA_EVNT_DEVICE_ATTACHED
289 						 * SATA_EVNT_DEVICE_DETACHED
290 						 * SATA_EVNT_LINK_LOST
291 						 * SATA_EVNT_LINK_ESTABLISHED
292 						 * SATA_EVNT_PORT_FAILED
293 						 * SATA_EVNT_PWR_LEVEL_CHANGED
294 						 */
295 	uint32_t	pmport_event_flags;
296 
297 	struct sata_port_scr pmport_scr;	/* PMult device port scr */
298 
299 						/*
300 						 * Attached device type:
301 						 * SATA_DTYPE_NONE
302 						 * SATA_DTYPE_ATADISK
303 						 * SATA_DTYPE_ATAPICD
304 						 * SATA_DTYPE_ATAPITAPE
305 						 * SATA_DTYPE_ATAPIDISK
306 						 * SATA_DTYPE_UNKNOWN
307 						 */
308 	uint32_t	pmport_dev_type;
309 
310 	struct sata_drive_info	*pmport_sata_drive; /* Attached drive info */
311 
312 						/* lbolt value at link lost */
313 	clock_t		pmport_link_lost_time;
314 						/* lbolt value @ dev attached */
315 	clock_t		pmport_dev_attach_time;
316 
317 	struct sata_port_stats	pmport_stats;	/* Port statistics */
318 
319 	boolean_t	pmport_tgtnode_clean;	/* Target node usable */
320 };
321 
322 typedef	struct sata_pmport_info sata_pmport_info_t;
323 
324 /*
325  * sata drive's power level
326  * default value is active
327  */
328 #define	SATA_POWER_ACTIVE	0x00
329 #define	SATA_POWER_IDLE		0x01
330 #define	SATA_POWER_STANDBY	0x02
331 #define	SATA_POWER_STOPPED	0x03
332 
333 /*
334  * pm-capable value definition according to PSARC 2009/310
335  */
336 #define	SATA_CAP_POWER_CONDITON	PM_CAPABLE_SPC4
337 #define	SATA_CAP_SMART_PAGE	PM_CAPABLE_SMART_LOG
338 #define	SATA_CAP_LOG_SENSE	PM_CAPABLE_LOG_SUPPORTED
339 
340 /*
341  * Port SSTATUS register (sata_port_scr sport_sstatus field).
342  * Link bits are valid only in port active state.
343  */
344 #define	SATA_PORT_DEVLINK_UP	0x00000103	/* Link with dev established */
345 #define	SATA_PORT_DEVLINK_UP_MASK 0x0000010F	/* Mask for link bits */
346 
347 /*
348  * Port state clear mask (cport_state and pmport_state fields).
349  * SATA_PSTATE_SHUTDOWN and power state are preserved.
350  */
351 #define	SATA_PORT_STATE_CLEAR_MASK	(~(SATA_PSTATE_SHUTDOWN))
352 
353 /*
354  * Valid i.e.supported device types mask (cport_dev_type, satadrv_type,
355  * pmult_dev_type fields).
356  * ATA disks and ATAPI CD/DVD now.
357  */
358 #define	SATA_VALID_DEV_TYPE	(SATA_DTYPE_ATADISK | \
359 				SATA_DTYPE_ATAPICD | \
360 				SATA_DTYPE_ATAPITAPE | \
361 				SATA_DTYPE_ATAPIDISK)
362 
363 /*
364  * Device feature_support (satadrv_features_support)
365  */
366 #define	SATA_DEV_F_DMA			0x01
367 #define	SATA_DEV_F_LBA28		0x02
368 #define	SATA_DEV_F_LBA48		0x04
369 #define	SATA_DEV_F_NCQ			0x08
370 #define	SATA_DEV_F_SATA1		0x10
371 #define	SATA_DEV_F_SATA2		0x20
372 #define	SATA_DEV_F_TCQ			0x40	/* Non NCQ tagged queuing */
373 
374 /*
375  * Device features enabled (satadrv_features_enabled)
376  */
377 #define	SATA_DEV_F_E_TAGGED_QING	0x01	/* Tagged queuing enabled */
378 #define	SATA_DEV_F_E_UNTAGGED_QING	0x02	/* Untagged queuing enabled */
379 
380 /*
381  * Drive settings flags (satdrv_settings)
382  */
383 #define	SATA_DEV_READ_AHEAD		0x0001	/* Read Ahead enabled */
384 #define	SATA_DEV_WRITE_CACHE		0x0002	/* Write cache ON */
385 #define	SATA_DEV_DMA			0x0004	/* DMA selected */
386 #define	SATA_DEV_SERIAL_FEATURES 	0x8000	/* Serial ATA feat. enabled */
387 #define	SATA_DEV_ASYNCH_NOTIFY		0x2000	/* Asynch-event enabled */
388 #define	SATA_DEV_RMSN			0x0100	/* Rem Media Stat Notfc enbl */
389 
390 /*
391  * Internal event and flags.
392  * These flags are set in the *_event_flags fields of various structures.
393  * Events and lock flags defined below are used internally by the
394  * SATA framework (they are not reported by SATA HBA drivers).
395  */
396 #define	SATA_EVNT_MAIN			0x80000000
397 #define	SATA_EVNT_SKIP			0x40000000
398 #define	SATA_EVNT_INPROC_DEVICE_RESET	0x08000000
399 #define	SATA_EVNT_CLEAR_DEVICE_RESET	0x04000000
400 #define	SATA_EVNT_TARGET_NODE_CLEANUP	0x00000100
401 #define	SATA_EVNT_AUTOONLINE_DEVICE	0x00000200
402 
403 /*
404  * Lock flags - used to serialize configuration operations
405  * on ports and devices.
406  * SATA_EVNT_LOCK_PORT_BUSY is set by event daemon to prevent
407  * simultaneous cfgadm operations.
408  * SATA_APCTL_LOCK_PORT_BUSY is set by cfgadm ioctls to prevent
409  * simultaneous event processing.
410  */
411 #define	SATA_EVNT_LOCK_PORT_BUSY	0x00800000
412 #define	SATA_APCTL_LOCK_PORT_BUSY	0x00400000
413 
414 /* Mask for port events */
415 #define	SATA_EVNT_PORT_EVENTS		(SATA_EVNT_DEVICE_ATTACHED | \
416 					SATA_EVNT_DEVICE_DETACHED | \
417 					SATA_EVNT_LINK_LOST | \
418 					SATA_EVNT_LINK_ESTABLISHED | \
419 					SATA_EVNT_PMULT_LINK_CHANGED | \
420 					SATA_EVNT_PORT_FAILED | \
421 					SATA_EVNT_TARGET_NODE_CLEANUP | \
422 					SATA_EVNT_AUTOONLINE_DEVICE)
423 /* Mask for drive events */
424 #define	SATA_EVNT_DRIVE_EVENTS		(SATA_EVNT_DEVICE_RESET | \
425 					SATA_EVNT_INPROC_DEVICE_RESET)
426 #define	SATA_EVNT_CONTROLLER_EVENTS	SATA_EVNT_PWR_LEVEL_CHANGED
427 
428 /* Delays and timeout duration definitions */
429 #define	SATA_EVNT_DAEMON_SLEEP_TIME	50000	/* 50 ms */
430 #define	SATA_EVNT_DAEMON_TERM_TIMEOUT	100000	/* 100 ms */
431 #define	SATA_EVNT_DAEMON_TERM_WAIT	60000000 /* 60 s */
432 #define	SATA_EVNT_LINK_LOST_TIMEOUT	1000000	/* 1 s */
433 
434 #define	SATA_DEV_IDENTIFY_TIMEOUT	60000000 /* 60 s, device enumeration */
435 #define	SATA_DEV_REPROBE_TIMEOUT	30000000  /* 30 s, dev resp after rst */
436 #define	SATA_DEV_RETRY_DLY		10000	/* 10 ms */
437 
438 /* DEVICE IDENTIFY and device initialization retry delay */
439 #define	SATA_DEV_IDENTIFY_RETRY		1
440 #define	SATA_DEV_IDENTIFY_NORETRY	0
441 
442 /*
443  * sata_scsi's hba_open_flag: field indicating open devctl instance.
444  *	0 = closed, 1 = shared open, 2 = exclusive open.
445  */
446 #define	SATA_DEVCTL_CLOSED	0
447 #define	SATA_DEVCTL_SOPENED	1
448 #define	SATA_DEVCTL_EXOPENED	2
449 
450 /*
451  * sata_pkt_txlate structure contains info about resources allocated
452  * for the packet
453  * Address of this structure is stored in scsi_pkt.pkt_ha_private and
454  * in sata_pkt.sata_hba_private fields, so all three strucures are
455  * cross-linked, with sata_pkt_txlate as a centerpiece.
456  */
457 
458 typedef struct sata_pkt_txlate {
459 	struct sata_hba_inst	*txlt_sata_hba_inst;
460 	struct scsi_pkt		*txlt_scsi_pkt;
461 	struct sata_pkt		*txlt_sata_pkt;
462 	ddi_dma_handle_t	txlt_buf_dma_handle;
463 	uint_t			txlt_flags;	/* data-in / data-out */
464 	uint_t			txlt_num_dma_win; /* number of DMA windows */
465 	uint_t			txlt_cur_dma_win; /* current DMA window */
466 
467 				/* cookies in the current DMA window */
468 	uint_t			txlt_curwin_num_dma_cookies;
469 
470 				/* processed dma cookies in current DMA win */
471 	uint_t			txlt_curwin_processed_dma_cookies;
472 	size_t			txlt_total_residue;
473 	ddi_dma_cookie_t	txlt_dma_cookie; /* default dma cookie */
474 	int			txlt_dma_cookie_list_len; /* alloc list len */
475 	ddi_dma_cookie_t 	*txlt_dma_cookie_list; /* dma cookie list */
476 	int			txlt_num_dma_cookies; /* dma cookies in list */
477 
478 				/* temporary buffer access handle */
479 	ddi_acc_handle_t	txlt_tmp_buf_handle;
480 	caddr_t			txlt_tmp_buf;	/* temp buffer address */
481 } sata_pkt_txlate_t;
482 
483 _NOTE(SCHEME_PROTECTS_DATA("unshared data", sata_pkt_txlate))
484 _NOTE(SCHEME_PROTECTS_DATA("unshared data", scsi_pkt))
485 
486 /* Length of largest sense buffer used by sata */
487 #define	SATA_MAX_SENSE_LEN	MAX(sizeof (struct scsi_arq_status), \
488     sizeof (struct scsi_arq_status) - sizeof (struct scsi_extended_sense) + \
489     sizeof (struct scsi_descr_sense_hdr) + \
490     MAX(sizeof (struct scsi_cmd_specific_sense_descr), \
491     sizeof (struct scsi_ata_status_ret_sense_descr)))
492 
493 /*
494  * Sense Data structure for ATA Pass Through
495  * This is the entire sense data block passed back up to scsi.  It is
496  * effectively the scsi_arq_status structure for ATA Sense Return descriptor
497  * format sense data.
498  */
499 struct sata_apt_sense_data {
500 	struct scsi_status				apt_status;
501 	struct scsi_status				apt_rqpkt_status;
502 	uchar_t						apt_rqpkt_reason;
503 	uchar_t						apt_rqpkt_resid;
504 	uint_t						apt_rqpkt_state;
505 	uint_t						apt_rqpkt_statistics;
506 	struct scsi_descr_sense_hdr			apt_sd_hdr;
507 	struct scsi_ata_status_ret_sense_descr		apt_sd_sense;
508 };
509 
510 
511 /*
512  * Additional scsi sense code definitions.
513  * These definition should eventually be moved to scsi header file
514  * usr/src/uts/common/sys/scsi/generic/sense.h
515  */
516 #define	SD_SCSI_ASC_NO_ADD_SENSE			0x00
517 #define	SD_SCSI_ASC_ATP_INFO_AVAIL			0x00
518 #define	SD_SCSI_ASC_LU_NOT_READY			0x04
519 #define	SD_SCSI_ASC_LU_NOT_RESPONSE			0x05
520 #define	SD_SCSI_ASC_WRITE_ERR				0x0c
521 #define	SD_SCSI_ASC_UNREC_READ_ERR			0x11
522 #define	SD_SCSI_ASC_INVALID_COMMAND_CODE		0x20
523 #define	SD_SCSI_ASC_LBA_OUT_OF_RANGE			0x21
524 #define	SD_SCSI_ASC_INVALID_FIELD_IN_CDB		0x24
525 #define	SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST	0x26
526 #define	SD_SCSI_ASC_WRITE_PROTECTED			0x27
527 #define	SD_SCSI_ASC_MEDIUM_MAY_HAVE_CHANGED		0x28
528 #define	SD_SCSI_ASC_RESET				0x29
529 #define	SD_SCSI_ASC_CMD_SEQUENCE_ERR			0x2c
530 #define	SD_SCSI_ASC_MEDIUM_NOT_PRESENT			0x3a
531 #define	SD_SCSI_ASC_SAVING_PARAMS_NOT_SUPPORTED		0x39
532 #define	SD_SCSI_ASC_INTERNAL_TARGET_FAILURE		0x44
533 #define	SD_SCSI_ASC_INFO_UNIT_IUCRC_ERR			0x47
534 #define	SD_SCSI_ASC_OP_MEDIUM_REM_REQ			0x5a
535 #define	SD_SCSI_ASC_LOW_POWER_CONDITION_ON		0x5e
536 
537 
538 /* SCSI defs missing from scsi headers */
539 /* Missing from sys/scsi/generic/commands.h */
540 #define	SCMD_SYNCHRONIZE_CACHE_G1		0x91
541 /*
542  * Missing from sys/scsi/impl/mode.h, although defined
543  * in sys/scsi/targets/sddefs.h as MODEPAGE_ERR_RECOV
544  */
545 #define	MODEPAGE_RW_ERRRECOV			0x01 /* read/write recovery */
546 /* Missing from sys/scsi/impl/commands.h */
547 #define	SCSI_READ_CAPACITY16_MAX_LBA		0xfffffffffffffffe
548 
549 /*
550  * medium access command
551  */
552 #define	SATA_IS_MEDIUM_ACCESS_CMD(cmd) \
553 	(((cmd) == SCMD_READ) || ((cmd) == SCMD_WRITE) || \
554 	((cmd) == SCMD_READ_G1) || ((cmd) == SCMD_WRITE_G1) || \
555 	((cmd) == SCMD_READ_G4) || ((cmd) == SCMD_WRITE_G4) || \
556 	((cmd) == SCMD_READ_G5) || ((cmd) == SCMD_WRITE_G5) || \
557 	((cmd) == SCMD_VERIFY) || ((cmd) == SCMD_VERIFY_G4) || \
558 	((cmd) == SCMD_VERIFY_G5) || ((cmd) == 0x7f) /* VERIFY(32) */|| \
559 	((cmd) == SCMD_SYNCHRONIZE_CACHE) || ((cmd) == SCMD_SPACE_G4) || \
560 	((cmd) == SCMD_READ_POSITION) || \
561 	((cmd) == 0x90) /* PRE-FETCH(16) */ || \
562 	((cmd) == SCMD_READ_DEFECT_LIST) || \
563 	((cmd) == 0xb7) /* READ DEFECT DATA */ || \
564 	((cmd) == SCMD_READ_LONG) || ((cmd) == SCMD_SVC_ACTION_IN_G4) || \
565 	((cmd) == SCMD_WRITE_LONG) || ((cmd) == SCMD_SVC_ACTION_OUT_G4) || \
566 	((cmd) == 0x41) || ((cmd) == 0x93) || /* WRITE SAME */ \
567 	((cmd) == 0x52) || ((cmd) == 0x50) || /* XDREAD & XDWRITE */ \
568 	((cmd) == 0x53) || ((cmd) == 0x51) || /* XDWRITEREAD & XPWRITE */ \
569 	((cmd) == 0x7f))
570 
571 /*
572  * Macros for accessing various structure fields
573  */
574 
575 #define	SATA_TRAN(sata_hba_inst) \
576 	sata_hba_inst->satahba_tran
577 
578 #define	SATA_DIP(sata_hba_inst) \
579 	sata_hba_inst->satahba_dip
580 
581 #define	SATA_NUM_CPORTS(sata_hba_inst) \
582 	sata_hba_inst->satahba_tran->sata_tran_hba_num_cports
583 
584 #define	SATA_QDEPTH(sata_hba_inst) \
585 	sata_hba_inst->satahba_tran->sata_tran_hba_qdepth
586 
587 #define	SATA_FEATURES(sata_hba_inst) \
588 	sata_hba_inst->satahba_tran->sata_tran_hba_features_support
589 
590 #define	SATA_DMA_ATTR(sata_hba_inst) \
591 	sata_hba_inst->satahba_tran->sata_tran_hba_dma_attr
592 
593 #define	SATA_START_FUNC(sata_hba_inst) \
594 	sata_hba_inst->satahba_tran->sata_tran_start
595 
596 #define	SATA_ABORT_FUNC(sata_hba_inst) \
597 	sata_hba_inst->satahba_tran->sata_tran_abort
598 
599 #define	SATA_RESET_DPORT_FUNC(sata_hba_inst) \
600 	sata_hba_inst->satahba_tran->sata_tran_reset_dport
601 
602 #define	SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) \
603 	(sata_hba_inst->satahba_tran->sata_tran_hotplug_ops == NULL ? \
604 	NULL : \
605 	sata_hba_inst->satahba_tran->sata_tran_hotplug_ops->\
606 	sata_tran_port_deactivate)
607 
608 #define	SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) \
609 	(sata_hba_inst->satahba_tran->sata_tran_hotplug_ops == NULL ? \
610 	NULL : \
611 	sata_hba_inst->satahba_tran->sata_tran_hotplug_ops->\
612 	sata_tran_port_activate)
613 
614 #define	SATA_PROBE_PORT_FUNC(sata_hba_inst) \
615 	sata_hba_inst->satahba_tran->sata_tran_probe_port
616 
617 #define	SATA_SELFTEST_FUNC(sata_hba_inst) \
618 	sata_hba_inst->satahba_tran->sata_tran_selftest
619 
620 #define	SATA_CPORT_MUTEX(sata_hba_inst, cport) \
621 	sata_hba_inst->satahba_dev_port[cport]->cport_mutex
622 
623 #define	SATA_CPORT_INFO(sata_hba_inst, cport) \
624 	sata_hba_inst->satahba_dev_port[cport]
625 
626 #define	SATA_CPORT_STATE(sata_hba_inst, cport) \
627 	sata_hba_inst->satahba_dev_port[cport]->cport_state
628 
629 #define	SATA_CPORT_EVENT_FLAGS(sata_hba_inst, cport) \
630 	sata_hba_inst->satahba_dev_port[cport]->cport_event_flags
631 
632 #define	SATA_CPORT_SCR(sata_hba_inst, cport) \
633 	sata_hba_inst->satahba_dev_port[cport]->cport_scr
634 
635 #define	SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) \
636 	sata_hba_inst->satahba_dev_port[cport]->cport_dev_type
637 
638 #define	SATA_CPORT_DRV_INFO(sata_hba_inst, cport) \
639 	sata_hba_inst->satahba_dev_port[cport]->cport_devp.cport_sata_drive
640 
641 #define	SATA_CPORTINFO_DRV_TYPE(cportinfo) \
642 	cportinfo->cport_dev_type
643 
644 #define	SATA_CPORTINFO_DRV_INFO(cportinfo) \
645 	cportinfo->cport_devp.cport_sata_drive
646 
647 #define	SATA_CPORTINFO_PMULT_INFO(cportinfo) \
648 	cportinfo->cport_devp.cport_sata_pmult
649 
650 #define	SATA_PMULT_INFO(sata_hba_inst, cport) \
651 	sata_hba_inst->satahba_dev_port[cport]->cport_devp.cport_sata_pmult
652 
653 #define	SATA_NUM_PMPORTS(sata_hba_inst, cport) \
654 	sata_hba_inst->satahba_dev_port[cport]->\
655 	cport_devp.cport_sata_pmult->pmult_num_dev_ports
656 
657 #define	SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport) \
658 	sata_hba_inst->satahba_dev_port[cport]->\
659 	cport_devp.cport_sata_pmult->pmult_dev_port[pmport]->pmport_mutex
660 
661 #define	SATA_PMPORT_INFO(sata_hba_inst, cport, pmport) \
662 	sata_hba_inst->satahba_dev_port[cport]->\
663 	cport_devp.cport_sata_pmult->pmult_dev_port[pmport]
664 
665 #define	SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport) \
666 	sata_hba_inst->satahba_dev_port[cport]->\
667 	cport_devp.cport_sata_pmult->pmult_dev_port[pmport]->\
668 	pmport_sata_drive
669 
670 #define	SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) \
671 	sata_hba_inst->satahba_dev_port[cport]->\
672 	cport_devp.cport_sata_pmult->pmult_dev_port[pmport]->pmport_state
673 
674 #define	SATA_PMPORT_SCR(sata_hba_inst, cport, pmport) \
675 	sata_hba_inst->satahba_dev_port[cport]->\
676 	cport_devp.cport_sata_pmult->pmult_dev_port[pmport]->pmport_scr
677 
678 #define	SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, pmport) \
679 	sata_hba_inst->satahba_dev_port[cport]->\
680 	cport_devp.cport_sata_pmult->pmult_dev_port[pmport]->pmport_dev_type
681 
682 #define	SATA_PMPORT_EVENT_FLAGS(sata_hba_inst, cport, pmport) \
683 	sata_hba_inst->satahba_dev_port[cport]->\
684 	cport_devp.cport_sata_pmult->pmult_dev_port[pmport]->\
685 	pmport_event_flags
686 
687 #define	SATA_PMPORTINFO_DRV_TYPE(pmportinfo) \
688 	pmportinfo->pmport_dev_type
689 
690 #define	SATA_PMPORTINFO_DRV_INFO(pmportinfo) \
691 	pmportinfo->pmport_sata_drive
692 
693 #define	SATA_TXLT_HBA_INST(spx) \
694 	spx->txlt_sata_hba_inst
695 
696 #define	SATA_TXLT_CPORT(spx) \
697 	spx->txlt_sata_pkt->satapkt_device.satadev_addr.cport
698 
699 #define	SATA_TXLT_PMPORT(spx) \
700 	spx->txlt_sata_pkt->satapkt_device.satadev_addr.pmport
701 
702 #define	SATA_TXLT_QUAL(spx) \
703 	spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual
704 
705 #define	SATA_TXLT_CPORT_MUTEX(spx) \
706 	spx->txlt_sata_hba_inst->\
707 	satahba_dev_port[spx->txlt_sata_pkt->\
708 	satapkt_device.satadev_addr.cport]->cport_mutex
709 
710 #define	SATA_TXLT_TASKQ(spx) \
711 	spx->txlt_sata_hba_inst->\
712 	satahba_taskq
713 
714 /*
715  * Minor number construction for devctl and attachment point nodes.
716  * All necessary information has to be encoded in NBITSMINOR32 bits.
717  *
718  * Devctl node minor number:
719  * ((controller_instance << SATA_CNTRL_INSTANCE_SHIFT) | SATA_DEVCTL_NODE)
720  *
721  * Attachment point node minor number has to include controller
722  * instance (7 bits), controller port number (5 bits) and port multiplier
723  * device port number (4 bits) and port multiplier device port
724  * indicator (1 bit).  Additionally, a single bit is used to
725  * differentiate between attachment point node and device control node.
726  *
727  * Attachment point minor number:
728  * ((controller_instance << SATA_CNTRL_INSTANCE_SHIFT) | SATA_AP_NODE |
729  * [(port_multiplier_device_port << SATA_PMULT_PORT_SHIFT) | SATA_PMULT_AP] |
730  * (controller_port))
731  *
732  * 17 bits are used (if 64 instances of controllers are expected)
733  * bit 18 is reserved for future use.
734  *
735  *   --------------------------------------------------------
736  *   |17|16|15|14|13|12|11|10 |09|08|07|06|05|04|03|02|01|00|
737  *   --------------------------------------------------------
738  *   | R| c| c| c| c| c| c|a/d|pm|pp|pp|pp|pp|cp|cp|cp|cp|cp|
739  *   --------------------------------------------------------
740  * Where:
741  * cp  - device port number on the HBA SATA controller
742  * pp  - device port number on the port multiplier
743  * pm  - 0 - target attached to controller device port
744  *       1 - target attached to port multiplier's device port
745  * a/d - 0 - devctl node
746  *       1 - attachment point node
747  * c   - controller number
748  * R   - reserved bit
749  */
750 
751 #define	SATA_AP_NODE		0x400		/* Attachment Point node */
752 #define	SATA_DEVCTL_NODE	0x000		/* DEVCTL node */
753 #define	SATA_PMULT_AP		0x200		/* device on PMult port */
754 #define	SATA_PMULT_PORT_SHIFT	5
755 #define	SATA_CNTRL_INSTANCE_SHIFT 11
756 #define	SATA_CPORT_MASK		0x1f		/* 32 device ports */
757 #define	SATA_PMULT_PORT_MASK	0xf		/* 15 device ports */
758 #define	SATA_CNTRL_INSTANCE_MASK 0x03F		/* 64 controllers */
759 
760 /* Macro for creating devctl node minor number */
761 #define	SATA_MAKE_DEVCTL_MINOR(controller_instance) \
762 	((controller_instance << SATA_CNTRL_INSTANCE_SHIFT) | \
763 	SATA_DEVCTL_NODE)
764 
765 /* Macro for creating an attachment point node minor number */
766 #define	SATA_MAKE_AP_MINOR(cntrl_instance, cport, pmport, qual) \
767 	(qual & (SATA_ADDR_PMPORT | SATA_ADDR_DPMPORT) ? \
768 	(((cntrl_instance) << SATA_CNTRL_INSTANCE_SHIFT) | \
769 	SATA_AP_NODE | SATA_PMULT_AP | \
770 	(pmport << SATA_PMULT_PORT_SHIFT) | cport) : \
771 	(((cntrl_instance) << SATA_CNTRL_INSTANCE_SHIFT) | \
772 	SATA_AP_NODE | cport))
773 
774 /* Macro retrieving controller number from a minor number */
775 #define	SATA_MINOR2INSTANCE(minor) \
776 	((minor >> SATA_CNTRL_INSTANCE_SHIFT) & SATA_CNTRL_INSTANCE_MASK)
777 
778 /*
779  * Macro for creating an attachment point number from sata address.
780  * Address qualifier has to be one of:
781  * SATA_ADDR_DCPORT, SATA_ADDR_DPMPORT, SATA_ADDR_CPORT or SATA_ADDR_PMPORT
782  */
783 #define	SATA_MAKE_AP_NUMBER(cport, pmport, qual) \
784 	((qual & (SATA_ADDR_PMPORT | SATA_ADDR_DPMPORT)) ? \
785 	(SATA_PMULT_AP | (pmport << SATA_PMULT_PORT_SHIFT) | cport) : \
786 	(cport))
787 
788 /*
789  * SCSI target number format
790  *
791  *   -------------------------------
792  *   | 9| 8| 7| 6| 5| 4| 3| 2| 1| 0|    Bit number
793  *   -------------------------------
794  *   |pm|pp|pp|pp|pp|cp|cp|cp|cp|cp|
795  *   -------------------------------
796  * Where:
797  * cp  - device port number on the HBA SATA controller
798  * pp  - device port number on the port multiplier
799  * pm  - 0 - target attached to controller device port
800  *       1 - target attached to port multiplier's device port
801  */
802 
803 /* SATA ports to SCSI target number translation */
804 
805 #define	SATA_TO_SCSI_TARGET(cport, pmport, qual) \
806 	(qual == SATA_ADDR_DCPORT ? cport : \
807 	(cport | (pmport << SATA_PMULT_PORT_SHIFT) | SATA_PMULT_AP))
808 
809 /* SCSI target number to SATA cntrl/pmport/cport translations */
810 #define	SCSI_TO_SATA_CPORT(scsi_target) \
811 	(scsi_target & SATA_CPORT_MASK)
812 
813 #define	SCSI_TO_SATA_PMPORT(scsi_target) \
814 	((scsi_target >> SATA_PMULT_PORT_SHIFT) & SATA_PMULT_PORT_MASK)
815 
816 #define	SCSI_TO_SATA_ADDR_QUAL(scsi_target) \
817 	((scsi_target & SATA_PMULT_AP) ? SATA_ADDR_DPMPORT : \
818 	SATA_ADDR_DCPORT)
819 
820 
821 /* Debug flags */
822 #if	DEBUG
823 
824 #define	SATA_DEBUG
825 #define	SATA_DBG_SCSI_IF	1
826 #define	SATA_DBG_HBA_IF		2
827 #define	SATA_DBG_NODES		4
828 #define	SATA_DBG_IOCTL_IF	8
829 #define	SATA_DBG_EVENTS		0x10
830 #define	SATA_DBG_EVENTS_PROC	0x20
831 #define	SATA_DBG_EVENTS_PROCPST	0x40
832 #define	SATA_DBG_EVENTS_CNTRL	0x80
833 #define	SATA_DBG_EVENTS_DAEMON	0x100
834 #define	SATA_DBG_DMA_SETUP	0x400
835 #define	SATA_DBG_DEV_SETTINGS	0x800
836 #define	SATA_DBG_ATAPI		0x1000
837 #define	SATA_DBG_ATAPI_PACKET	0x8000
838 #define	SATA_DBG_INTR_CTX	0x10000
839 #define	SATA_DBG_PMULT		0x20000
840 
841 typedef struct sata_atapi_cmd {
842 	uint8_t acdb[SATA_ATAPI_MAX_CDB_LEN];
843 	uint8_t arqs[SATA_ATAPI_RQSENSE_LEN];
844 	uint_t sata_pkt_reason;
845 	uint_t scsi_pkt_reason;
846 } sata_atapi_cmd_t;
847 
848 /* Debug macros */
849 #define	SATADBG1(flag, sata, format, arg1) \
850 	if (sata_debug_flags & (flag)) { \
851 		sata_log(sata, CE_CONT, format, arg1); \
852 	}
853 
854 #define	SATADBG2(flag, sata, format, arg1, arg2) \
855 	if (sata_debug_flags & (flag)) { \
856 		sata_log(sata, CE_CONT, format, arg1, arg2); \
857 	}
858 
859 #define	SATADBG3(flag, sata, format, arg1, arg2, arg3) \
860 	if (sata_debug_flags & (flag)) { \
861 		sata_log(sata, CE_CONT, format, arg1, arg2, arg3); \
862 	}
863 #else
864 
865 #define	SATADBG1(flag, dip, frmt, arg1)
866 #define	SATADBG2(flag, dip, frmt, arg1, arg2)
867 #define	SATADBG3(flag, dip, frmt, arg1, arg2, arg3)
868 
869 #endif
870 
871 /* sata_rev_tag 1.46 */
872 
873 #ifdef	__cplusplus
874 }
875 #endif
876 
877 #endif /* _SATA_H */
878