xref: /titanic_52/usr/src/uts/common/sys/sata/impl/sata.h (revision d92a527c473246e5c750ae6c33ecd2f242a5bf1f)
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 2008 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_ATAPINONCD
157 						 * SATA_DTYPE_PMULT
158 						 * SATA_DTYPE_UNKNOWN
159 						 */
160 	uint32_t		cport_dev_type;
161 	union {
162 	    struct sata_drive_info *cport_sata_drive; /* Attached drive info */
163 	    struct sata_pmult_info *cport_sata_pmult; /* Attached Port Mult */
164 	} 			cport_devp;
165 						/* lbolt value at link lost */
166 	clock_t			cport_link_lost_time;
167 						/* lbolt value @ dev attached */
168 	clock_t			cport_dev_attach_time;
169 
170 	struct sata_port_stats	cport_stats;	/* Port statistics */
171 
172 	boolean_t		cport_tgtnode_clean; /* Target node usable */
173 };
174 
175 typedef struct sata_cport_info sata_cport_info_t;
176 
177 /*
178  * Attached SATA drive info and state.
179  * This structure is pointed to by sata_cport_info's cport_sata_drive field
180  * when a drive is attached directly to a controller device port.
181  */
182 struct sata_drive_info {
183 	sata_address_t	satadrv_addr;		/* this drive SATA address */
184 
185 						/*
186 						 * Drive state flags
187 						 * SATA_STATE_UNKNOWN
188 						 * SATA_STATE_PROBING
189 						 * SATA_STATE_PROBED
190 						 * SATA_STATE_READY
191 						 * SATA_DSTATE_PWR_ACTIVE
192 						 * SATA_DSTATE_PWR_IDLE
193 						 * SATA_DSTATE_RESET
194 						 * SATA_DSTATE_FAILED
195 						 */
196 	uint32_t	satadrv_state;
197 
198 						/*
199 						 * drive event flags:
200 						 * SATA_EVNT_DRIVE_RESET
201 						 */
202 	uint32_t	satadrv_event_flags;
203 						/*
204 						 * lbolt value @ start of
205 						 * device reset processing
206 						 */
207 	clock_t		satadrv_reset_time;
208 						/*
209 						 * Attached device type:
210 						 * SATA_DTYPE_ATADISK
211 						 * SATA_DTYPE_ATAPICD
212 						 * SATA_DTYPE_ATAPINONCD
213 						 */
214 	uint32_t	satadrv_type;
215 
216 	uint32_t	satadrv_status_reg;	/* drive status reg */
217 	uint32_t	satadrv_error_reg;	/* drive error reg */
218 	uint16_t	satadrv_features_support; /* drive features support */
219 	uint16_t	satadrv_queue_depth;    /* drive queue depth */
220 	uint16_t	satadrv_atapi_cdb_len;	/* atapi supported cdb length */
221 	uint16_t	satadrv_atapi_trans_ver; /* atapi transport version */
222 	uint16_t	satadrv_settings;	/* drive settings flags */
223 	uint16_t	satadrv_features_enabled; /* drive features enabled */
224 	uint64_t	satadrv_capacity;	/* drive capacity */
225 	uint64_t	satadrv_max_queue_depth; /* maximum queue depth */
226 	sata_id_t	satadrv_id;		/* Device Identify Data */
227 	struct sata_drive_stats satadrv_stats;	/* drive statistics */
228 };
229 
230 typedef struct sata_drive_info sata_drive_info_t;
231 
232 _NOTE(SCHEME_PROTECTS_DATA("unshared data", sata_drive_info))
233 
234 
235 /* Port Multiplier & host port info and state */
236 struct sata_pmult_info {
237 	sata_address_t	pmult_addr;		/* this PMult SATA Address */
238 	kmutex_t	pmult_mutex;		/* pmult (host port) mutex */
239 
240 						/*
241 						 * PMult state flags
242 						 * SATA_STATE_UNKNOWN
243 						 * SATA_STATE_PROBING
244 						 * SATA_STATE_PROBED
245 						 * SATA_STATE_READY
246 						 * SATA_PSTATE_FAILED
247 						 */
248 	uint32_t	pmult_state;
249 	uint32_t	pmult_event_flags;	/* Undefined for now */
250 	struct sata_port_scr pmult_scr;		/* Host port SCR block */
251 	uint32_t	pmult_num_dev_ports; 	/* Number of data ports */
252 	struct sata_pmport_info	*pmult_dev_port[SATA_MAX_PMPORTS - 1];
253 };
254 
255 typedef	struct sata_pmult_info sata_pmult_info_t;
256 
257 /* Port Multiplier's device port info & state */
258 struct sata_pmport_info {
259 	sata_address_t	pmport_addr;		/* this SATA port address */
260 	kmutex_t	pmport_mutex;		/* pmult device port mutex */
261 
262 						/*
263 						 * Port state flags
264 						 * SATA_STATE_UNKNOWN
265 						 * SATA_STATE_PROBING
266 						 * SATA_STATE_PROBED
267 						 * SATA_STATE_READY
268 						 * SATA_PSTATE_PWRON
269 						 * SATA_PSTATE_PWROFF
270 						 * SATA_PSTATE_SHUTDOWN
271 						 * SATA_PSTATE_FAILED
272 						 */
273 	uint32_t	pmport_state;
274 
275 						/*
276 						 * Port event flags:
277 						 * SATA_EVNT_DEVICE_ATTACHED
278 						 * SATA_EVNT_DEVICE_DETACHED
279 						 * SATA_EVNT_LINK_LOST
280 						 * SATA_EVNT_LINK_ESTABLISHED
281 						 * SATA_EVNT_PORT_FAILED
282 						 * SATA_EVNT_PWR_LEVEL_CHANGED
283 						 */
284 	uint32_t	pmport_event_flags;
285 
286 	struct sata_port_scr pmport_scr;	/* PMult device port scr */
287 
288 						/*
289 						 * Attached device type:
290 						 * SATA_DTYPE_NONE
291 						 * SATA_DTYPE_ATADISK
292 						 * SATA_DTYPE_ATAPICD
293 						 * SATA_DTYPE_ATAPINONCD
294 						 * SATA_DTYPE_UNKNOWN
295 						 */
296 	uint32_t	pmport_dev_type;
297 
298 	struct sata_drive_info	*pmport_sata_drive; /* Attached drive info */
299 
300 						/* lbolt value at link lost */
301 	clock_t		pmport_link_lost_time;
302 						/* lbolt value @ dev attached */
303 	clock_t		pmport_dev_attach_time;
304 
305 	struct sata_port_stats	pmport_stats;	/* Port statistics */
306 
307 	boolean_t	pmport_tgtnode_clean;	/* Target node usable */
308 };
309 
310 typedef	struct sata_pmport_info sata_pmport_info_t;
311 
312 /*
313  * Port SSTATUS register (sata_port_scr sport_sstatus field).
314  * Link bits are valid only in port active state.
315  */
316 #define	SATA_PORT_DEVLINK_UP	0x00000103	/* Link with dev established */
317 #define	SATA_PORT_DEVLINK_UP_MASK 0x0000010F	/* Mask for link bits */
318 
319 /*
320  * Port state clear mask (cport_state and pmport_state fields).
321  * SATA_PSTATE_SHUTDOWN and power state are preserved.
322  */
323 #define	SATA_PORT_STATE_CLEAR_MASK	(~(SATA_PSTATE_SHUTDOWN))
324 
325 /*
326  * Valid i.e.supported device types mask (cport_dev_type, satadrv_type,
327  * pmult_dev_type fields).
328  * ATA disks and ATAPI CD/DVD now.
329  */
330 #define	SATA_VALID_DEV_TYPE	(SATA_DTYPE_ATADISK | \
331 				SATA_DTYPE_ATAPICD)
332 
333 /*
334  * Device feature_support (satadrv_features_support)
335  */
336 #define	SATA_DEV_F_DMA			0x01
337 #define	SATA_DEV_F_LBA28		0x02
338 #define	SATA_DEV_F_LBA48		0x04
339 #define	SATA_DEV_F_NCQ			0x08
340 #define	SATA_DEV_F_SATA1		0x10
341 #define	SATA_DEV_F_SATA2		0x20
342 #define	SATA_DEV_F_TCQ			0x40	/* Non NCQ tagged queuing */
343 
344 /*
345  * Device features enabled (satadrv_features_enabled)
346  */
347 #define	SATA_DEV_F_E_TAGGED_QING	0x01	/* Tagged queuing enabled */
348 #define	SATA_DEV_F_E_UNTAGGED_QING	0x02	/* Untagged queuing enabled */
349 
350 /*
351  * Drive settings flags (satdrv_settings)
352  */
353 #define	SATA_DEV_READ_AHEAD		0x0001	/* Read Ahead enabled */
354 #define	SATA_DEV_WRITE_CACHE		0x0002	/* Write cache ON */
355 #define	SATA_DEV_DMA			0x0004	/* DMA selected */
356 #define	SATA_DEV_SERIAL_FEATURES 	0x8000	/* Serial ATA feat. enabled */
357 #define	SATA_DEV_ASYNCH_NOTIFY		0x2000	/* Asynch-event enabled */
358 #define	SATA_DEV_RMSN			0x0100	/* Rem Media Stat Notfc enbl */
359 
360 /*
361  * Internal event and flags.
362  * These flags are set in the *_event_flags fields of various structures.
363  * Events and lock flags defined below are used internally by the
364  * SATA framework (they are not reported by SATA HBA drivers).
365  */
366 #define	SATA_EVNT_MAIN			0x80000000
367 #define	SATA_EVNT_SKIP			0x40000000
368 #define	SATA_EVNT_INPROC_DEVICE_RESET	0x08000000
369 #define	SATA_EVNT_CLEAR_DEVICE_RESET	0x04000000
370 #define	SATA_EVNT_TARGET_NODE_CLEANUP	0x00000100
371 #define	SATA_EVNT_AUTOONLINE_DEVICE	0x00000200
372 
373 /*
374  * Lock flags - used to serialize configuration operations
375  * on ports and devices.
376  * SATA_EVNT_LOCK_PORT_BUSY is set by event daemon to prevent
377  * simultaneous cfgadm operations.
378  * SATA_APCTL_LOCK_PORT_BUSY is set by cfgadm ioctls to prevent
379  * simultaneous event processing.
380  */
381 #define	SATA_EVNT_LOCK_PORT_BUSY	0x00800000
382 #define	SATA_APCTL_LOCK_PORT_BUSY	0x00400000
383 
384 /* Mask for port events */
385 #define	SATA_EVNT_PORT_EVENTS		(SATA_EVNT_DEVICE_ATTACHED | \
386 					SATA_EVNT_DEVICE_DETACHED | \
387 					SATA_EVNT_LINK_LOST | \
388 					SATA_EVNT_LINK_ESTABLISHED | \
389 					SATA_EVNT_PORT_FAILED | \
390 					SATA_EVNT_TARGET_NODE_CLEANUP | \
391 					SATA_EVNT_AUTOONLINE_DEVICE)
392 /* Mask for drive events */
393 #define	SATA_EVNT_DRIVE_EVENTS		(SATA_EVNT_DEVICE_RESET | \
394 					SATA_EVNT_INPROC_DEVICE_RESET)
395 #define	SATA_EVNT_CONTROLLER_EVENTS	SATA_EVNT_PWR_LEVEL_CHANGED
396 
397 /* Delays and timeout duration definitions */
398 #define	SATA_EVNT_DAEMON_SLEEP_TIME	50000	/* 50 ms */
399 #define	SATA_EVNT_DAEMON_TERM_TIMEOUT	100000	/* 100 ms */
400 #define	SATA_EVNT_DAEMON_TERM_WAIT	60000000 /* 60 s */
401 #define	SATA_EVNT_LINK_LOST_TIMEOUT	1000000	/* 1 s */
402 
403 #define	SATA_DEV_IDENTIFY_TIMEOUT	60000000 /* 60 s, device enumeration */
404 #define	SATA_DEV_REPROBE_TIMEOUT	30000000  /* 30 s, dev resp after rst */
405 #define	SATA_DEV_RETRY_DLY		10000	/* 10 ms */
406 
407 /* DEVICE IDENTIFY and device initialization retry delay */
408 #define	SATA_DEV_IDENTIFY_RETRY		1
409 #define	SATA_DEV_IDENTIFY_NORETRY	0
410 
411 /*
412  * sata_scsi's hba_open_flag: field indicating open devctl instance.
413  *	0 = closed, 1 = shared open, 2 = exclusive open.
414  */
415 #define	SATA_DEVCTL_CLOSED	0
416 #define	SATA_DEVCTL_SOPENED	1
417 #define	SATA_DEVCTL_EXOPENED	2
418 
419 /*
420  * sata_pkt_txlate structure contains info about resources allocated
421  * for the packet
422  * Address of this structure is stored in scsi_pkt.pkt_ha_private and
423  * in sata_pkt.sata_hba_private fields, so all three strucures are
424  * cross-linked, with sata_pkt_txlate as a centerpiece.
425  */
426 
427 typedef struct sata_pkt_txlate {
428 	struct sata_hba_inst	*txlt_sata_hba_inst;
429 	struct scsi_pkt		*txlt_scsi_pkt;
430 	struct sata_pkt		*txlt_sata_pkt;
431 	ddi_dma_handle_t	txlt_buf_dma_handle;
432 	uint_t			txlt_flags;	/* data-in / data-out */
433 	uint_t			txlt_num_dma_win; /* number of DMA windows */
434 	uint_t			txlt_cur_dma_win; /* current DMA window */
435 
436 				/* cookies in the current DMA window */
437 	uint_t			txlt_curwin_num_dma_cookies;
438 
439 				/* processed dma cookies in current DMA win */
440 	uint_t			txlt_curwin_processed_dma_cookies;
441 	size_t			txlt_total_residue;
442 	ddi_dma_cookie_t	txlt_dma_cookie; /* default dma cookie */
443 	int			txlt_dma_cookie_list_len; /* alloc list len */
444 	ddi_dma_cookie_t 	*txlt_dma_cookie_list; /* dma cookie list */
445 	int			txlt_num_dma_cookies; /* dma cookies in list */
446 
447 				/* temporary buffer access handle */
448 	ddi_acc_handle_t	txlt_tmp_buf_handle;
449 	caddr_t			txlt_tmp_buf;	/* temp buffer address */
450 } sata_pkt_txlate_t;
451 
452 _NOTE(SCHEME_PROTECTS_DATA("unshared data", sata_pkt_txlate))
453 _NOTE(SCHEME_PROTECTS_DATA("unshared data", scsi_pkt))
454 
455 
456 /*
457  * Additional scsi sense code definitions.
458  * These definition should eventually be moved to scsi header file
459  * usr/src/uts/common/sys/scsi/generic/sense.h
460  */
461 #define	SD_SCSI_ASC_NO_ADD_SENSE			0x00
462 #define	SD_SCSI_ASC_LU_NOT_READY			0x04
463 #define	SD_SCSI_ASC_WRITE_ERR				0x0c
464 #define	SD_SCSI_ASC_UNREC_READ_ERR			0x11
465 #define	SD_SCSI_ASC_INVALID_COMMAND_CODE		0x20
466 #define	SD_SCSI_ASC_LBA_OUT_OF_RANGE			0x21
467 #define	SD_SCSI_ASC_INVALID_FIELD_IN_CDB		0x24
468 #define	SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST 	0x26
469 #define	SD_SCSI_ASC_RESET				0x29
470 #define	SD_SCSI_ASC_SAVING_PARAMS_NOT_SUPPORTED		0x39
471 
472 
473 /* SCSI defs missing from scsi headers */
474 /* Missing from sys/scsi/generic/commands.h */
475 #define	SCMD_SYNCHRONIZE_CACHE_G1		0x91
476 /*
477  * Missing from sys/scsi/impl/mode.h, although defined
478  * in sys/scsi/targets/sddefs.h as MODEPAGE_ERR_RECOV
479  */
480 #define	MODEPAGE_RW_ERRRECOV			0x01 /* read/write recovery */
481 
482 /*
483  * Macros for accessing various structure fields
484  */
485 
486 #define	SATA_TRAN(sata_hba_inst) \
487 	sata_hba_inst->satahba_tran
488 
489 #define	SATA_DIP(sata_hba_inst) \
490 	sata_hba_inst->satahba_dip
491 
492 #define	SATA_NUM_CPORTS(sata_hba_inst) \
493 	sata_hba_inst->satahba_tran->sata_tran_hba_num_cports
494 
495 #define	SATA_QDEPTH(sata_hba_inst) \
496 	sata_hba_inst->satahba_tran->sata_tran_hba_qdepth
497 
498 #define	SATA_FEATURES(sata_hba_inst) \
499 	sata_hba_inst->satahba_tran->sata_tran_hba_features_support
500 
501 #define	SATA_DMA_ATTR(sata_hba_inst) \
502 	sata_hba_inst->satahba_tran->sata_tran_hba_dma_attr
503 
504 #define	SATA_START_FUNC(sata_hba_inst) \
505 	sata_hba_inst->satahba_tran->sata_tran_start
506 
507 #define	SATA_ABORT_FUNC(sata_hba_inst) \
508 	sata_hba_inst->satahba_tran->sata_tran_abort
509 
510 #define	SATA_RESET_DPORT_FUNC(sata_hba_inst) \
511 	sata_hba_inst->satahba_tran->sata_tran_reset_dport
512 
513 #define	SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) \
514 	(sata_hba_inst->satahba_tran->sata_tran_hotplug_ops == NULL ? \
515 	NULL : \
516 	sata_hba_inst->satahba_tran->sata_tran_hotplug_ops->\
517 	sata_tran_port_deactivate)
518 
519 #define	SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) \
520 	(sata_hba_inst->satahba_tran->sata_tran_hotplug_ops == NULL ? \
521 	NULL : \
522 	sata_hba_inst->satahba_tran->sata_tran_hotplug_ops->\
523 	sata_tran_port_activate)
524 
525 #define	SATA_PROBE_PORT_FUNC(sata_hba_inst) \
526 	sata_hba_inst->satahba_tran->sata_tran_probe_port
527 
528 #define	SATA_SELFTEST_FUNC(sata_hba_inst) \
529 	sata_hba_inst->satahba_tran->sata_tran_selftest
530 
531 #define	SATA_CPORT_MUTEX(sata_hba_inst, cport) \
532 	sata_hba_inst->satahba_dev_port[cport]->cport_mutex
533 
534 #define	SATA_CPORT_INFO(sata_hba_inst, cport) \
535 	sata_hba_inst->satahba_dev_port[cport]
536 
537 #define	SATA_CPORT_STATE(sata_hba_inst, cport) \
538 	sata_hba_inst->satahba_dev_port[cport]->cport_state
539 
540 #define	SATA_CPORT_EVENT_FLAGS(sata_hba_inst, cport) \
541 	sata_hba_inst->satahba_dev_port[cport]->cport_event_flags
542 
543 #define	SATA_CPORT_SCR(sata_hba_inst, cport) \
544 	sata_hba_inst->satahba_dev_port[cport]->cport_scr
545 
546 #define	SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) \
547 	sata_hba_inst->satahba_dev_port[cport]->cport_dev_type
548 
549 #define	SATA_CPORT_DRV_INFO(sata_hba_inst, cport) \
550 	sata_hba_inst->satahba_dev_port[cport]->cport_devp.cport_sata_drive
551 
552 #define	SATA_CPORTINFO_DRV_TYPE(cportinfo) \
553 	cportinfo->cport_dev_type
554 
555 #define	SATA_CPORTINFO_DRV_INFO(cportinfo) \
556 	cportinfo->cport_devp.cport_sata_drive
557 
558 #define	SATA_CPORTINFO_PMULT_INFO(cportinfo) \
559 	cportinfo->cport_devp.cport_sata_pmult
560 
561 #define	SATA_PMULT_INFO(sata_hba_inst, cport) \
562 	sata_hba_inst->satahba_dev_port[cport]->cport_devp.cport_sata_pmult
563 
564 #define	SATA_NUM_PMPORTS(sata_hba_inst, cport) \
565 	sata_hba_inst->satahba_dev_port[cport]->\
566 	cport_devp.cport_sata_pmult->pmult_num_dev_ports
567 
568 #define	SATA_PMPORT_INFO(sata_hba_inst, cport, pmport) \
569 	sata_hba_inst->satahba_dev_port[cport]->\
570 	cport_devp.cport_sata_pmult->pmult_dev_port[pmport]
571 
572 #define	SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport) \
573 	sata_hba_inst->satahba_dev_port[cport]->\
574 	cport_devp.cport_sata_pmult->pmult_dev_port[pmport]->\
575 	pmport_sata_drive
576 
577 #define	SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) \
578 	sata_hba_inst->satahba_dev_port[cport]->\
579 	cport_devp.cport_sata_pmult->pmult_dev_port[pmport]->pmport_state
580 
581 #define	SATA_PMPORT_SCR(sata_hba_inst, cport, pmport) \
582 	sata_hba_inst->satahba_dev_port[cport]->\
583 	cport_devp.cport_sata_pmult->pmult_dev_port[pmport]->pmport_scr
584 
585 #define	SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, pmport) \
586 	sata_hba_inst->satahba_dev_port[cport]->\
587 	cport_devp.cport_sata_pmult->pmult_dev_port[pmport]->pmport_dev_type
588 
589 #define	SATA_PMPORTINFO_DRV_TYPE(pmportinfo) \
590 	pmportinfo->pmport_dev_type
591 
592 #define	SATA_PMPORTINFO_DRV_INFO(pmportinfo) \
593 	pmportinfo->pmport_sata_drive
594 
595 #define	SATA_TXLT_HBA_INST(spx) \
596 	spx->txlt_sata_hba_inst
597 
598 #define	SATA_TXLT_CPORT(spx) \
599 	spx->txlt_sata_pkt->satapkt_device.satadev_addr.cport
600 
601 #define	SATA_TXLT_CPORT_MUTEX(spx) \
602 	spx->txlt_sata_hba_inst->\
603 	satahba_dev_port[spx->txlt_sata_pkt->\
604 	satapkt_device.satadev_addr.cport]->cport_mutex
605 
606 #define	SATA_TXLT_TASKQ(spx) \
607 	spx->txlt_sata_hba_inst->\
608 	satahba_taskq
609 
610 /*
611  * Minor number construction for devctl and attachment point nodes.
612  * All necessary information has to be encoded in NBITSMINOR32 bits.
613  *
614  * Devctl node minor number:
615  * ((controller_instance << SATA_CNTRL_INSTANCE_SHIFT) | SATA_DEVCTL_NODE)
616  *
617  * Attachment point node minor number has to include controller
618  * instance (7 bits), controller port number (5 bits) and port multiplier
619  * device port number (4 bits) and port multiplier device port
620  * indicator (1 bit).  Additionally, a single bit is used to
621  * differentiate between attachment point node and device control node.
622  *
623  * Attachment point minor number:
624  * ((controller_instance << SATA_CNTRL_INSTANCE_SHIFT) | SATA_AP_NODE |
625  * [(port_multiplier_device_port << SATA_PMULT_PORT_SHIFT) | SATA_PMULT_AP] |
626  * (controller_port))
627  *
628  * 17 bits are used (if 64 instances of controllers are expected)
629  * bit 18 is reserved for future use.
630  *
631  *   --------------------------------------------------------
632  *   |17|16|15|14|13|12|11|10 |09|08|07|06|05|04|03|02|01|00|
633  *   --------------------------------------------------------
634  *   | R| c| c| c| c| c| c|a/d|pm|pp|pp|pp|pp|cp|cp|cp|cp|cp|
635  *   --------------------------------------------------------
636  * Where:
637  * cp  - device port number on the HBA SATA controller
638  * pp  - device port number on the port multiplier
639  * pm  - 0 - target attached to controller device port
640  *       1 - target attached to port multiplier's device port
641  * a/d - 0 - devctl node
642  *       1 - attachment point node
643  * c   - controller number
644  * R   - reserved bit
645  */
646 
647 #define	SATA_AP_NODE		0x400		/* Attachment Point node */
648 #define	SATA_DEVCTL_NODE	0x000		/* DEVCTL node */
649 #define	SATA_PMULT_AP		0x200		/* device on PMult port */
650 #define	SATA_PMULT_PORT_SHIFT	5
651 #define	SATA_CNTRL_INSTANCE_SHIFT 11
652 #define	SATA_CPORT_MASK		0x1f		/* 32 device ports */
653 #define	SATA_PMULT_PORT_MASK	0xf		/* 15 device ports */
654 #define	SATA_CNTRL_INSTANCE_MASK 0x03F		/* 64 controllers */
655 
656 /* Macro for creating devctl node minor number */
657 #define	SATA_MAKE_DEVCTL_MINOR(controller_instance) \
658 	((controller_instance << SATA_CNTRL_INSTANCE_SHIFT) | \
659 	SATA_DEVCTL_NODE)
660 
661 /* Macro for creating an attachment point node minor number */
662 #define	SATA_MAKE_AP_MINOR(cntrl_instance, cport, pmport, qual) \
663 	(qual & (SATA_ADDR_PMPORT | SATA_ADDR_DPMPORT) ? \
664 	(((cntrl_instance) << SATA_CNTRL_INSTANCE_SHIFT) | \
665 	SATA_AP_NODE | SATA_PMULT_AP | \
666 	(pmport << SATA_PMULT_PORT_SHIFT) | cport) : \
667 	(((cntrl_instance) << SATA_CNTRL_INSTANCE_SHIFT) | \
668 	SATA_AP_NODE | cport))
669 
670 /* Macro retrieving controller number from a minor number */
671 #define	SATA_MINOR2INSTANCE(minor) \
672 	((minor >> SATA_CNTRL_INSTANCE_SHIFT) & SATA_CNTRL_INSTANCE_MASK)
673 
674 /*
675  * Macro for creating an attachment point number from sata address.
676  * Address qualifier has to be one of:
677  * SATA_ADDR_DCPORT, SATA_ADDR_DPMPORT, SATA_ADDR_CPORT or SATA_ADDR_PMPORT
678  */
679 #define	SATA_MAKE_AP_NUMBER(cport, pmport, qual) \
680 	((qual & (SATA_ADDR_PMPORT | SATA_ADDR_DPMPORT)) ? \
681 	(SATA_PMULT_AP | (pmport << SATA_PMULT_PORT_SHIFT) | cport) : \
682 	(cport))
683 
684 /*
685  * SCSI target number format
686  *
687  *   -------------------------------
688  *   | 9| 8| 7| 6| 5| 4| 3| 2| 1| 0|    Bit number
689  *   -------------------------------
690  *   |pm|pp|pp|pp|pp|cp|cp|cp|cp|cp|
691  *   -------------------------------
692  * Where:
693  * cp  - device port number on the HBA SATA controller
694  * pp  - device port number on the port multiplier
695  * pm  - 0 - target attached to controller device port
696  *       1 - target attached to port multiplier's device port
697  */
698 
699 /* SATA ports to SCSI target number translation */
700 
701 #define	SATA_TO_SCSI_TARGET(cport, pmport, qual) \
702 	(qual == SATA_ADDR_DCPORT ? cport : \
703 	(cport | (pmport << SATA_PMULT_PORT_SHIFT) | SATA_PMULT_AP))
704 
705 /* SCSI target number to SATA cntrl/pmport/cport translations */
706 #define	SCSI_TO_SATA_CPORT(scsi_target) \
707 	(scsi_target & SATA_CPORT_MASK)
708 
709 #define	SCSI_TO_SATA_PMPORT(scsi_target) \
710 	((scsi_target >> SATA_PMULT_PORT_SHIFT) & SATA_PMULT_PORT_MASK)
711 
712 #define	SCSI_TO_SATA_ADDR_QUAL(scsi_target) \
713 	((scsi_target & SATA_PMULT_AP) ? SATA_ADDR_DPMPORT : \
714 	SATA_ADDR_DCPORT)
715 
716 
717 /* Debug flags */
718 #if	DEBUG
719 
720 #define	SATA_DEBUG
721 #define	SATA_DBG_SCSI_IF	1
722 #define	SATA_DBG_HBA_IF		2
723 #define	SATA_DBG_NODES		4
724 #define	SATA_DBG_IOCTL_IF	8
725 #define	SATA_DBG_EVENTS		0x10
726 #define	SATA_DBG_EVENTS_PROC	0x20
727 #define	SATA_DBG_EVENTS_PROCPST	0x40
728 #define	SATA_DBG_EVENTS_CNTRL	0x80
729 #define	SATA_DBG_EVENTS_DAEMON	0x100
730 #define	SATA_DBG_DMA_SETUP	0x400
731 #define	SATA_DBG_DEV_SETTINGS	0x800
732 #define	SATA_DBG_ATAPI		0x1000
733 #define	SATA_DBG_ATAPI_PACKET	0x8000
734 #define	SATA_DBG_INTR_CTX	0x10000
735 
736 typedef struct sata_atapi_cmd {
737 	uint8_t acdb[SATA_ATAPI_MAX_CDB_LEN];
738 	uint8_t arqs[SATA_ATAPI_RQSENSE_LEN];
739 	uint_t sata_pkt_reason;
740 	uint_t scsi_pkt_reason;
741 } sata_atapi_cmd_t;
742 
743 /* Debug macros */
744 #define	SATADBG1(flag, sata, format, arg1) \
745 	if (sata_debug_flags & (flag)) { \
746 		sata_log(sata, CE_CONT, format, arg1); \
747 	}
748 
749 #define	SATADBG2(flag, sata, format, arg1, arg2) \
750 	if (sata_debug_flags & (flag)) { \
751 		sata_log(sata, CE_CONT, format, arg1, arg2); \
752 	}
753 
754 #define	SATADBG3(flag, sata, format, arg1, arg2, arg3) \
755 	if (sata_debug_flags & (flag)) { \
756 		sata_log(sata, CE_CONT, format, arg1, arg2, arg3); \
757 	}
758 #else
759 
760 #define	SATADBG1(flag, dip, frmt, arg1)
761 #define	SATADBG2(flag, dip, frmt, arg1, arg2)
762 #define	SATADBG3(flag, dip, frmt, arg1, arg2, arg3)
763 
764 #endif
765 
766 /* sata_rev_tag 1.38 */
767 
768 #ifdef	__cplusplus
769 }
770 #endif
771 
772 #endif /* _SATA_H */
773