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