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