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