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