xref: /titanic_51/usr/src/uts/common/sys/sata/sata_hba.h (revision 602ca9ea8f9ce0933f0944601cc5d230e91a950d)
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_HBA_H
28 #define	_SATA_HBA_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #ifdef	__cplusplus
33 extern "C" {
34 #endif
35 
36 #include <sys/sata/sata_defs.h>
37 
38 /*
39  * SATA Host Bus Adapter (HBA) driver transport definitions
40  */
41 
42 #include <sys/types.h>
43 
44 #ifndef	TRUE
45 #define	TRUE	1
46 #define	FALSE	0
47 #endif
48 
49 #define	SATA_SUCCESS	0
50 #define	SATA_RETRY	1
51 #define	SATA_FAILURE	-1
52 
53 
54 /* SATA Framework definitions */
55 
56 #define	SATA_MAX_CPORTS		32	/* Max number of controller ports */
57 
58 					/* Multiplier (PMult) */
59 #define	SATA_MAX_PMPORTS	16	/* Maximum number of ports on PMult */
60 #define	SATA_PMULT_HOSTPORT	0xf	/* Port Multiplier host port number */
61 
62 
63 /*
64  * SATA device address
65  * Address qualifier flags are used to specify what is addressed (device
66  * or port) and where (controller or port multiplier data port).
67  */
68 struct sata_address {
69 	uint8_t		cport;		/* Controller's SATA port number */
70 	uint8_t 	pmport;		/* Port Multiplier SATA port number */
71 	uint8_t		qual;		/* Address Qualifier flags */
72 	uint8_t		pad;		/* Reserved */
73 };
74 
75 typedef struct sata_address sata_address_t;
76 
77 /*
78  * SATA address Qualifier flags (in qual field of sata_address struct).
79  * They are mutually exclusive.
80  */
81 
82 #define	SATA_ADDR_NULL		0x00	/* No address */
83 #define	SATA_ADDR_DCPORT	0x01	/* Device attched to controller port */
84 #define	SATA_ADDR_DPMPORT	0x02	/* Device attched to PM device port */
85 #define	SATA_ADDR_CPORT		0x04	/* Controller's device port */
86 #define	SATA_ADDR_PMPORT	0x08	/* Port Multiplier's device port */
87 #define	SATA_ADDR_CNTRL		0x10	/* Controller */
88 #define	SATA_ADDR_PMULT		0x20	/* Port Multiplier */
89 
90 /*
91  * SATA port status and control register block.
92  * The sstatus, serror, scontrol, sactive and snotific
93  * are the copies of the SATA port status and control registers.
94  * (Port SStatus, SError, SControl, SActive and SNotification are
95  * defined by Serial ATA r1.0a sepc and Serial ATA II spec.
96  */
97 
98 struct sata_port_scr
99 {
100 	uint32_t	sstatus;	/* Port SStatus register */
101 	uint32_t	serror;		/* Port SError register */
102 	uint32_t	scontrol;	/* Port SControl register */
103 	uint32_t	sactive;	/* Port SActive register */
104 	uint32_t	snotific; 	/* Port SNotification register */
105 };
106 
107 typedef struct sata_port_scr sata_port_scr_t;
108 
109 /*
110  * SATA Device Structure (rev 1)
111  * Used to request/return state of the controller, port, port multiplier
112  * or an attached drive:
113  *  	The satadev_addr.cport, satadev_addr.pmport and satadev_addr.qual
114  *  	fields are used to specify SATA address (see sata_address structure
115  *  	description).
116  * 	The satadev_scr structure is used to pass the content of a port
117  *	status and control registers.
118  *	The satadev_add_info field is used by SATA HBA driver to return an
119  *	additional information, which type depends on the function using
120  *	sata_device as argument. For example:
121  *	- in case of sata_tran_probe_port() this field should contain
122  *	a number of available Port Multiplier device ports;
123  *	- in case of sata_hba_event_notify() this field may contain
124  *	a value specific for a reported event.
125  */
126 #define	SATA_DEVICE_REV_1	1
127 #define	SATA_DEVICE_REV		SATA_DEVICE_REV_1
128 
129 struct sata_device
130 {
131 	int		satadev_rev;		/* structure  version */
132 	struct sata_address satadev_addr;	/* sata port/device address */
133 	uint32_t	satadev_state;		/* Port or device state */
134 	uint32_t	satadev_type;		/* Attached device type */
135 	struct sata_port_scr satadev_scr; 	/* Port status and ctrl regs */
136 	uint32_t	satadev_add_info;	/* additional information, */
137 						/* function specific */
138 };
139 
140 typedef struct sata_device sata_device_t;
141 
142 _NOTE(SCHEME_PROTECTS_DATA("unshared data", sata_device))
143 
144 
145 /*
146  * satadev_state field of sata_device structure.
147  * Common flags specifying current state of a port or an attached drive.
148  * These states are mutually exclusive, obviously
149  */
150 #define	SATA_STATE_UNKNOWN		0x000000
151 #define	SATA_STATE_READY		0x000010
152 
153 /*
154  * Attached drive specific states (satadev_state field of the sata_device
155  * structure).
156  * SATA_DSTATE_PWR_ACTIVE, SATA_DSTATE_PWR_IDLE and SATA_DSTATE_PWR_STANDBY
157  * are mutually exclusive. All other states may be combined with each other
158  * and with one of the power states.
159  * These flags may be used only if the address qualifier (satadev_addr.qual) is
160  * set to SATA_ADDR_DCPORT or SATA_ADDR_DPMPORT value.
161  */
162 
163 #define	SATA_DSTATE_PWR_ACTIVE		0x000100
164 #define	SATA_DSTATE_PWR_IDLE		0x000200
165 #define	SATA_DSTATE_PWR_STANDBY		0x000400
166 #define	SATA_DSTATE_RESET		0x001000
167 #define	SATA_DSTATE_FAILED		0x008000
168 
169 /* Mask for drive power states */
170 #define	SATA_DSTATE_PWR			(SATA_DSTATE_PWR_ACTIVE | \
171 					SATA_DSTATE_PWR_IDLE | \
172 					SATA_DSTATE_PWR_STANDBY)
173 /*
174  * SATA Port specific states (satadev_state field of sata_device structure).
175  * SATA_PSTATE_PWRON and SATA_PSTATE_PWROFF are mutually exclusive.
176  * All other states may be combined with each other and with one of the power
177  * level state.
178  * These flags may be used only if the address qualifier (satadev_addr.qual) is
179  * set to SATA_ADDR_CPORT or SATA_ADDR_PMPORT value.
180  */
181 
182 #define	SATA_PSTATE_PWRON		0x010000
183 #define	SATA_PSTATE_PWROFF		0X020000
184 #define	SATA_PSTATE_SHUTDOWN		0x040000
185 #define	SATA_PSTATE_FAILED		0x080000
186 
187 /* Mask for the valid port-specific state flags */
188 #define	SATA_PSTATE_VALID		(SATA_PSTATE_PWRON | \
189 					SATA_PSTATE_PWROFF | \
190 					SATA_PSTATE_SHUTDOWN | \
191 					SATA_PSTATE_FAILED)
192 
193 /* Mask for a port power states */
194 #define	SATA_PSTATE_PWR			(SATA_PSTATE_PWRON | \
195 					SATA_PSTATE_PWROFF)
196 /*
197  * Device type (in satadev_type field of sata_device structure).
198  * More device types may be added in the future.
199  */
200 
201 #define	SATA_DTYPE_NONE			0x00	/* No device attached */
202 #define	SATA_DTYPE_ATADISK		0x01	/* ATA Disk */
203 #define	SATA_DTYPE_ATAPICD		0x02	/* Atapi CD/DVD device */
204 #define	SATA_DTYPE_ATAPINONCD		0x03	/* Atapi non-CD/DVD device */
205 #define	SATA_DTYPE_PMULT		0x10	/* Port Multiplier */
206 #define	SATA_DTYPE_UNKNOWN		0x20	/* Device attached, unkown */
207 
208 
209 /*
210  * SATA cmd structure  (rev 1)
211  *
212  * SATA HBA framework always sets all fields except status_reg and error_reg.
213  * SATA HBA driver action depends on the addressing type specified by
214  * addr_type field:
215  * If LBA48 addressing is indicated, SATA HBA driver has to load values from
216  * satacmd_sec_count_msb_reg, satacmd_lba_low_msb_reg,
217  * satacmd_lba_mid_msb_reg and satacmd_lba_hi_msb_reg
218  * to appropriate registers prior to loading other registers.
219  * For other addressing modes, SATA HBA driver should skip loading values
220  * from satacmd_sec_count_msb_reg, satacmd_lba_low_msb_reg,
221  * satacmd_lba_mid_msb_reg and satacmd_lba_hi_msb_reg
222  * fields and load only remaining field values to corresponding registers.
223  *
224  * satacmd_sec_count_msb and satamcd_sec_count_lsb values are loaded into
225  * sec_count register, satacmd_sec_count_msb loaded first (if LBA48
226  * addressing is used).
227  * satacmd_lba_low_msb and satacmd_lba_low_lsb values are loaded into the
228  * lba_low register, satacmd_lba_low_msb loaded first (if LBA48 addressing
229  * is used). The lba_low register is the newer name for the old
230  * sector_number register.
231  * satacmd_lba_mid_msb and satacmd_lba_mid_lsb values are loaded into lba_mid
232  * register, satacmd_lba_mid_msb loaded first (if LBA48 addressing is used).
233  * The lba_mid register is the newer name for the old cylinder_low register.
234  * satacmd_lba_high_msb and satacmd_lba_high_lsb values are loaded into
235  * the lba_high regster, satacmd_lba_high_msb loaded first (if LBA48
236  * addressing is used). The lba_high register  is a newer name for the old
237  * cylinder_high register.
238  *
239  * No addressing mode is selected when an ata command does not involve actual
240  * reading/writing data from/to the media (for example IDENTIFY DEVICE or
241  * SET FEATURE command), or the ATAPI PACKET command is sent.
242  * If ATAPI PACKET command is sent and tagged commands are used,
243  * SATA HBA driver has to provide and manage a tag value and
244  * set it into the sector_count register.
245  *
246  * Device Control register is not specified in sata_cmd structure - SATA HBA
247  * driver shall set it accordingly to current mode of operation (interrupt
248  * enable/disable).
249  *
250  * Buffer structure's b_flags should be used to determine the
251  * address type of b_un.b_addr. However, there is no need to allocate DMA
252  * resources for the buffer in SATA HBA driver.
253  * DMA resources for a buffer structure are allocated by the SATA HBA
254  * framework. Scatter/gather list is to be used only for DMA transfers
255  * and it should be based on the DMA cookies list.
256  *
257  * Upon completion of a command, SATA HBA driver has to update
258  * satacmd_status_reg and satacmd_error_reg to reflect the contents of
259  * the corresponding device status and error registers.
260  * If the command completed successfully, satacmd_flags.sata_copy_xxx flags
261  * specify what register fields should be updated in sata_cmd structure.
262  * If the command completed with error, SATA HBA driver has to update
263  * satacmd_sec_count_msb, satacmd_sec_count_lsb, satacmd_lba_low_msb,
264  * satacmd_lba_low_lsb, satacmd_lba_mid_msb, satacmd_lba_mid_lsb,
265  * satacmd_lba_high_msb and satacmd_lba_high_lsb to values read from the
266  * corresponding device registers.
267  * If an operation could not complete because of the port error, the
268  * sata_pkt.satapkt_device.satadev_scr structure has to be updated.
269  *
270  * If ATAPI PACKET command was sent and command completed with error,
271  * rqsense structure has to be filed by SATA HBA driver. The satacmd_arq_cdb
272  * points to pre-set request sense cdb that may be used for issuing request
273  * sense data from the device.
274  *
275  * The sata_max_queue_depth field specifies the maximum allowable queue depth
276  * minus one, i.e. for maximum queue depth of 32, sata_max_queue_depth would
277  * be set to value 0x1f.
278  * If FPDMA-type command was sent and command completed with error, the HBA
279  * driver may use pre-set command READ LOG EXTENDED command pointed to
280  * by satacmd_rle_sata_cmd field to retrieve error data from a device.
281  * Only ATA register fields of the sata_cmd are set-up for that purpose.
282  *
283  * If the READ MULTIPLIER command was specified in cmd_reg (command directed
284  * to a port multiplier host port rather then to an attached device),
285  * upon the command completion SATA HBA driver has to update_sector count
286  * and lba fields of the sata_cmd structure to values returned via
287  * command block registers (task file registers).
288  */
289 #define	SATA_CMD_REV_1	1
290 #define	SATA_CMD_REV_2	2
291 #define	SATA_CMD_REV_3	3
292 #define	SATA_CMD_REV	SATA_CMD_REV_3
293 
294 #define	SATA_ATAPI_MAX_CDB_LEN	16	/* Covers both 12 and 16 byte cdbs */
295 #define	SATA_ATAPI_RQSENSE_LEN	24	/* Allocated Request Sense data */
296 #define	SATA_ATAPI_MIN_RQSENSE_LEN 18	/* Min Fixed size Request Sense data */
297 #define	SATA_ATAPI_RQSENSE_CDB_LEN 6	/* Request Sense CDB length */
298 
299 #define	SATA_MAX_QUEUE_DEPTH	32	/* Default max queue depth */
300 
301 struct sata_cmd {
302 	int		satacmd_rev;		/* version */
303 	struct buf	*satacmd_bp;		/* ptr to buffer structure */
304 	struct sata_cmd_flags {
305 		uint32_t	sata_data_direction : 3;	 /* 0-2 */
306 		uint32_t	: 1;		/* reserved */	 /* 3 */
307 		uint32_t	sata_queue_stag : 1;		 /* 4 */
308 		uint32_t	sata_queue_otag : 1;		 /* 5 */
309 		uint32_t	: 2;		/* reserved */	 /* 6-7 */
310 		uint32_t	sata_queued : 1;		 /* 8 */
311 		uint32_t	: 3;		/* reserved */	 /* 9-11 */
312 		uint32_t	sata_ignore_dev_reset : 1;	 /* 12 */
313 		uint32_t	sata_clear_dev_reset : 1;	 /* 13 */
314 		uint32_t	: 2;		/* reserved */	 /* 14-15 */
315 		uint32_t	sata_special_regs : 1;		 /* 16 */
316 		uint32_t	sata_copy_out_sec_count_msb : 1; /* 17 */
317 		uint32_t	sata_copy_out_lba_low_msb : 1;	 /* 18 */
318 		uint32_t	sata_copy_out_lba_mid_msb : 1;	 /* 19 */
319 		uint32_t	sata_copy_out_lba_high_msb : 1;	 /* 20 */
320 		uint32_t	sata_copy_out_sec_count_lsb : 1; /* 21 */
321 		uint32_t	sata_copy_out_lba_low_lsb : 1;	 /* 22 */
322 		uint32_t	sata_copy_out_lba_mid_lsb : 1;	 /* 23 */
323 		uint32_t	sata_copy_out_lba_high_lsb : 1;	 /* 24 */
324 		uint32_t	sata_copy_out_device_reg : 1;	 /* 25 */
325 		uint32_t	sata_copy_out_error_reg : 1;	 /* 26 */
326 		uint32_t	sata_max_queue_depth: 5;	 /* 27-31 */
327 	} satacmd_flags;
328 	uint8_t 	satacmd_addr_type; 	/* addr type: LBA28, LBA48 */
329 	uint8_t		satacmd_features_reg_ext; /* features reg extended */
330 	uint8_t		satacmd_sec_count_msb;	/* sector count MSB (LBA48) */
331 	uint8_t		satacmd_lba_low_msb; 	/* LBA Low MSB (LBA48) */
332 	uint8_t		satacmd_lba_mid_msb;	/* LBA Mid MSB (LBA48) */
333 	uint8_t		satacmd_lba_high_msb;	/* LBA High MSB (LBA48) */
334 	uint8_t		satacmd_sec_count_lsb;	/* sector count LSB */
335 	uint8_t		satacmd_lba_low_lsb;	/* LBA Low LSB */
336 	uint8_t		satacmd_lba_mid_lsb;	/* LBA Mid LSB */
337 	uint8_t		satacmd_lba_high_lsb;	/* LBA High LSB */
338 	uint8_t		satacmd_device_reg;	/* ATA dev reg & LBA28 MSB */
339 	uint8_t		satacmd_cmd_reg;	/* ata command code */
340 	uint8_t		satacmd_features_reg;	/* ATA features register */
341 	uint8_t		satacmd_status_reg;	/* ATA status register */
342 	uint8_t		satacmd_error_reg;	/* ATA error register  */
343 	uint8_t		satacmd_acdb_len;	/* ATAPI cdb length */
344 	uint8_t		satacmd_acdb[SATA_ATAPI_MAX_CDB_LEN]; /* ATAPI cdb */
345 
346 						/* kept for binary compat. */
347 	uint8_t		*pad1;			/* unused */
348 
349 	uint8_t 	satacmd_rqsense[SATA_ATAPI_RQSENSE_LEN];
350 						/*
351 						 * Error retrieval buffer
352 						 * dma handle pointer
353 						 * (for buffer DMA syncing)
354 						 * Valid only in error
355 						 * retrieval packet!
356 						 */
357 	ddi_dma_handle_t *satacmd_err_ret_buf_handle;
358 
359 	int		satacmd_num_dma_cookies; /* number of dma cookies */
360 						/* ptr to dma cookie list */
361 	ddi_dma_cookie_t *satacmd_dma_cookie_list;
362 };
363 
364 typedef struct sata_cmd sata_cmd_t;
365 
366 _NOTE(SCHEME_PROTECTS_DATA("unshared data", sata_cmd))
367 
368 
369 /* ATA address type (in satacmd_addr_type field */
370 #define	ATA_ADDR_LBA	0x1
371 #define	ATA_ADDR_LBA28	0x2
372 #define	ATA_ADDR_LBA48	0x4
373 
374 /*
375  * satacmd_flags : contain data transfer direction flags,
376  * tagged queuing type flags, queued command flag, and reset state handling
377  * flag.
378  */
379 
380 /*
381  * Data transfer direction flags (satacmd_flags.sata_data_direction)
382  * Direction flags are mutually exclusive.
383  */
384 #define	SATA_DIR_NODATA_XFER	0x0001	/* No data transfer */
385 #define	SATA_DIR_READ		0x0002	/* Reading data from a device */
386 #define	SATA_DIR_WRITE		0x0004	/* Writing data to a device */
387 
388 /*
389  * Tagged Queuing type flags
390  * 	satacmd_flags.sata_queue_stag
391  * 	satacmd_flags.sata_queue_otag
392  *
393  * These flags indicate how the SATA command should be queued.
394  *
395  * sata_queue_stag
396  * Simple-queue-tagged command. It may be executed out-of-order in respect
397  * to other queued commands.
398  * sata_queue_otag
399  * Ordered-queue-tagged command. It cannot be executed out-of-order in
400  * respect to other commands, i.e. it should be executed in the order of
401  * being transported to the HBA.
402  *
403  * Translated head-of-queue-tagged scsi commands and commands that are
404  * to be put at the head of the queue are treated as sata_queue_otag
405  * tagged commands.
406  */
407 
408 
409 /*
410  * Queuing command set-up flag (satacmd_flags.sata_queued).
411  * This flag indicates that sata_cmd was set-up for DMA Queued command
412  * (either READ_DMA_QUEUED, READ_DMA_QUEUED_EXT, WRITE_DMA_QUEUED or
413  * WRITE_DMA_QUEUED_EXT command) or one of the Native Command Queuing commands
414  * (either READ_FPDMA_QUEUED or WRITE_FPDMA_QUEUED).
415  * This flag will be used only if sata_tran_hba_flags indicates controller
416  * support for queuing and the device for which sata_cmd is prepared supports
417  * either legacy queuing (indicated by Device Identify data word 83 bit 2)
418  * or NCQ (indicated by  word 76 of Device Identify data).
419  */
420 
421 /*
422  * Reset state handling
423  *	satacmd_flags.sata_ignore_dev_reset
424  *	satacmd_flags.sata_clear_dev_reset
425  *
426  * SATA HBA device enters reset state if the device was subjected to
427  * the Device Reset (may also enter this state if the device was reset
428  * as a side effect of port reset). SATA HBA driver sets this state.
429  * Device stays in this condition until explicit request from SATA HBA
430  * framework to clear the state.
431  */
432 
433 /*
434  * SATA Packet structure (rev 1)
435  * hba_driver_private is for a private use of the SATA HBA driver;
436  * satapkt_framework_private is used only by SATA HBA framework;
437  * satapkt_comp is a callback function to be called when packet
438  * execution is completed (for any reason) if mode of operation is not
439  * synchronous (SATA_OPMODE_SYNCH);
440  * satapkt_reason specifies why the packet operation was completed
441  *
442  * NOTE: after the packet completion callback SATA HBA driver should not
443  * attempt to access any sata_pkt fields because sata_pkt is not valid anymore
444  * (it could have been destroyed).
445  * Since satapkt_hba_driver_private field cannot be retrieved, any hba private
446  * data respources allocated per packet and accessed via this pointer should
447  * either be freed before the completion callback is done, or the pointer has
448  * to be saved by the HBA driver before the completion callback.
449  */
450 #define	SATA_PKT_REV_1	1
451 #define	SATA_PKT_REV	SATA_PKT_REV_1
452 
453 struct sata_pkt {
454 	int		satapkt_rev;		/* version */
455 	struct sata_device satapkt_device;	/* Device address/type */
456 
457 						/* HBA driver private data */
458 	void		*satapkt_hba_driver_private;
459 
460 						/* SATA framework priv data */
461 	void		*satapkt_framework_private;
462 
463 						/* Rqsted mode of operation */
464 	uint32_t	satapkt_op_mode;
465 
466 	struct sata_cmd	satapkt_cmd;		/* composite sata command */
467 	int		satapkt_time;		/* time allotted to command */
468 	void		(*satapkt_comp)(struct sata_pkt *); /* callback */
469 	int		satapkt_reason; 	/* completion reason */
470 };
471 
472 typedef struct sata_pkt sata_pkt_t;
473 
474 _NOTE(SCHEME_PROTECTS_DATA("unshared data", sata_pkt))
475 
476 
477 /*
478  * Operation mode flags (in satapkt_op_mode field of sata_pkt structure).
479  * Use to specify what should be a mode of operation for specified command.
480  * Default (000b) means use Interrupt and Asynchronous mode to
481  * perform an operation.
482  * Synchronous operation menas that the packet operation has to be completed
483  * before the function called to initiate the operation returns.
484  */
485 #define	SATA_OPMODE_INTERRUPTS	0 /* Use interrupts (hint) */
486 #define	SATA_OPMODE_POLLING	1 /* Use polling instead of interrupts */
487 #define	SATA_OPMODE_ASYNCH	0 /* Return immediately after accepting pkt */
488 #define	SATA_OPMODE_SYNCH	4 /* Perform synchronous operation */
489 
490 /*
491  * satapkt_reason values:
492  *
493  * SATA_PKT_QUEUE_FULL - cmd not sent because of queue full (detected
494  * 	by the controller). If a device reject command for this reason, it
495  * 	should be reported as SATA_PKT_DEV_ERROR
496  *
497  * SATA_PKT_CMD_NOT_SUPPORTED - command not supported by a controller
498  *	Controller is unable to send such command to a device.
499  *	If device rejects a command, it should be reported as
500  *	SATA_PKT_DEV_ERROR.
501  *
502  * SATA_PKT_DEV_ERROR - cmd failed because of device reported an error.
503  *	The content of status_reg (ERROR bit has to be set) and error_reg
504  *	fields of the sata_cmd structure have to be set and will be used
505  *	by SATA HBA Framework to determine the error cause.
506  *
507  * SATA_PKT_PORT_ERROR - cmd failed because of a link or a port error.
508  *	Link failed / no communication with a device / communication error
509  *	or other port related error was detected by a controller.
510  *	sata_pkt.satapkt_device.satadev_scr.sXXXXXXX words have to be set.
511  *
512  * SATA_PKT_ABORTED - cmd execution was aborted by the request from the
513  *	framework. Abort mechanism is HBA driver specific.
514  *
515  * SATA_PKT_TIMEOUT - cmd execution has timed-out. Timeout specified by
516  *	 pkt_time was exceeded. The command was terminated by the SATA HBA
517  *	driver.
518  *
519  * SATA_PKT_COMPLETED - this is a value returned when an operation
520  *	completes without errors.
521  *
522  * SATA_PKT_BUSY - packet was not accepted for execution because the
523  *      driver was busy performing some other operation(s).
524  *
525  * SATA_PKT_RESET - packet execution was aborted because of device
526  * reset originated by either the HBA driver or the SATA framework.
527  *
528  */
529 
530 #define	SATA_PKT_BUSY			-1	/* Not completed, busy */
531 #define	SATA_PKT_COMPLETED		0	/* No error */
532 #define	SATA_PKT_DEV_ERROR		1	/* Device reported error */
533 #define	SATA_PKT_QUEUE_FULL		2	/* Not accepted, queue full */
534 #define	SATA_PKT_PORT_ERROR		3	/* Not completed, port error */
535 #define	SATA_PKT_CMD_UNSUPPORTED	4	/* Cmd unsupported */
536 #define	SATA_PKT_ABORTED		5	/* Aborted by request */
537 #define	SATA_PKT_TIMEOUT		6	/* Operation timeut */
538 #define	SATA_PKT_RESET			7	/* Aborted by reset request */
539 
540 /*
541  * Error retrieval sata packet types
542  */
543 #define	SATA_ERR_RETR_PKT_TYPE_NCQ	1
544 #define	SATA_ERR_RETR_PKT_TYPE_ATAPI	2
545 
546 /*
547  * Hoplug functions vector structure (rev 1)
548  */
549 #define	SATA_TRAN_HOTPLUG_OPS_REV_1	1
550 
551 struct sata_tran_hotplug_ops {
552 	int	sata_tran_hotplug_ops_rev; /* version */
553 	int	(*sata_tran_port_activate)(dev_info_t  *, sata_device_t *);
554 	int	(*sata_tran_port_deactivate)(dev_info_t  *, sata_device_t *);
555 };
556 
557 typedef struct sata_tran_hotplug_ops sata_tran_hotplug_ops_t;
558 
559 
560 /*
561  * Power management functions vector structure (rev 1)
562  * The embedded function returns information about the controller's
563  * power level.
564  * Additional functions may be added in the future without changes to
565  * sata_tran structure.
566  */
567 #define	SATA_TRAN_PWRMGT_OPS_REV_1	1
568 
569 struct sata_tran_pwrmgt_ops {
570 	int	sata_tran_pwrmgt_ops_rev; /* version */
571 	int	(*sata_tran_get_pwr_level)(dev_info_t  *, sata_device_t *);
572 };
573 
574 typedef struct sata_tran_pwrmgt_ops sata_tran_pwrmgt_ops_t;
575 
576 
577 /*
578  * SATA port PHY Power Level
579  * These states correspond to the interface power management state as defined
580  * in Serial ATA spec.
581  */
582 #define	SATA_TRAN_PORTPWR_LEVEL1	1 /* Interface in active PM state */
583 #define	SATA_TRAN_PORTPWR_LEVEL2	2 /* Interface in PARTIAL PM state */
584 #define	SATA_TRAN_PORTPWR_LEVEL3	3 /* Interface in SLUMBER PM state */
585 
586 /*
587  * SATA HBA Tran structure (rev 1)
588  * Registered with SATA Framework
589  *
590  * dma_attr is a pointer to data (buffer) dma attibutes of the controller
591  * DMA engine.
592  *
593  * The qdepth field specifies number of commands that may be accepted by
594  * the controller. Value range 1-32. A value greater than 1 indicates that
595  * the controller supports queuing. Support for Native Command Queuing
596  * indicated by SATA_CTLF_NCQ flag also requires qdepth set to a value
597  * greater then 1.
598  *
599  */
600 #define	SATA_TRAN_HBA_REV_1	1
601 #define	SATA_TRAN_HBA_REV_2	2
602 #define	SATA_TRAN_HBA_REV	SATA_TRAN_HBA_REV_2
603 
604 struct sata_hba_tran {
605 	int		sata_tran_hba_rev;	/* version */
606 	dev_info_t	*sata_tran_hba_dip;	/* Controler dev info */
607 	ddi_dma_attr_t	*sata_tran_hba_dma_attr; /* DMA attributes */
608 	int		sata_tran_hba_num_cports; /* Num of HBA device ports */
609 	uint16_t	sata_tran_hba_features_support; /* HBA features */
610 	uint16_t	sata_tran_hba_qdepth;	/* HBA-supported queue depth */
611 
612 	int		(*sata_tran_probe_port)(dev_info_t *, sata_device_t *);
613 	int		(*sata_tran_start)(dev_info_t *, sata_pkt_t *);
614 	int		(*sata_tran_abort)(dev_info_t *, sata_pkt_t *, int);
615 	int		(*sata_tran_reset_dport)(dev_info_t *,
616 					sata_device_t *);
617 	int		(*sata_tran_selftest)(dev_info_t *, sata_device_t *);
618 
619 						/* Hotplug vector */
620 	struct sata_tran_hotplug_ops *sata_tran_hotplug_ops;
621 
622 						/* Power mgt vector */
623 	struct sata_tran_pwrmgt_ops *sata_tran_pwrmgt_ops;
624 
625 	int		(*sata_tran_ioctl)(dev_info_t *, int, intptr_t);
626 };
627 
628 typedef struct sata_hba_tran sata_hba_tran_t;
629 
630 
631 /*
632  * Controller's features support flags (sata_tran_hba_features_support).
633  * Note: SATA_CTLF_NCQ indicates that SATA controller supports NCQ in addition
634  * to legacy queuing commands, indicated by SATA_CTLF_QCMD flag.
635  */
636 
637 #define	SATA_CTLF_ATAPI			0x001 /* ATAPI support */
638 #define	SATA_CTLF_PORT_MULTIPLIER 	0x010 /* Port Multiplier suport */
639 #define	SATA_CTLF_HOTPLUG		0x020 /* Hotplug support */
640 #define	SATA_CTLF_ASN			0x040 /* Asynchronous Event Support */
641 #define	SATA_CTLF_QCMD			0x080 /* Queued commands support */
642 #define	SATA_CTLF_NCQ			0x100 /* NCQ support */
643 
644 /*
645  * sata_tran_start() return values.
646  * When pkt is not accepted, the satapkt_reason has to be updated
647  * before function returns - it should reflect the same reason for not being
648  * executed as the return status of above functions.
649  * If pkt was accepted and executed synchronously,
650  * satapk_reason should indicate a completion status.
651  */
652 #define	SATA_TRAN_ACCEPTED		0 /* accepted */
653 #define	SATA_TRAN_QUEUE_FULL		1 /* not accepted, queue full */
654 #define	SATA_TRAN_PORT_ERROR		2 /* not accepted, port error */
655 #define	SATA_TRAN_CMD_UNSUPPORTED	3 /* not accepted, cmd not supported */
656 #define	SATA_TRAN_BUSY			4 /* not accepted, busy */
657 
658 
659 /*
660  * sata_tran_abort() abort type flag
661  */
662 #define	SATA_ABORT_PACKET		0
663 #define	SATA_ABORT_ALL_PACKETS		1
664 
665 
666 /*
667  * Events handled by SATA HBA Framework
668  * More then one event may be reported at the same time
669  *
670  * SATA_EVNT__DEVICE_ATTACHED
671  * HBA detected the presence of a device ( electrical connection with
672  * a device was detected ).
673  *
674  * SATA_EVNT_DEVICE_DETACHED
675  * HBA detected the detachment of a device (electrical connection with
676  * a device was broken)
677  *
678  * SATA_EVNT_LINK_LOST
679  * HBA lost link with an attached device
680  *
681  * SATA_EVNT_LINK_ESTABLISHED
682  * HBA established a link with an attached device
683  *
684  * SATA_EVNT_PORT_FAILED
685  * HBA has determined that the port failed and is unuseable
686  *
687  * SATA_EVENT_DEVICE_RESET
688  * SATA device was reset, causing loss of the device setting
689  *
690  * SATA_EVNT_PWR_LEVEL_CHANGED
691  * A port or entire SATA controller power level has changed
692  *
693  */
694 #define	SATA_EVNT_DEVICE_ATTACHED	0x01
695 #define	SATA_EVNT_DEVICE_DETACHED	0x02
696 #define	SATA_EVNT_LINK_LOST		0x04
697 #define	SATA_EVNT_LINK_ESTABLISHED	0x08
698 #define	SATA_EVNT_PORT_FAILED		0x10
699 #define	SATA_EVNT_DEVICE_RESET		0x20
700 #define	SATA_EVNT_PWR_LEVEL_CHANGED	0x40
701 
702 /*
703  * SATA Framework interface entry points
704  */
705 int 	sata_hba_init(struct modlinkage *);
706 int 	sata_hba_attach(dev_info_t *, sata_hba_tran_t *, ddi_attach_cmd_t);
707 int 	sata_hba_detach(dev_info_t *, ddi_detach_cmd_t);
708 void 	sata_hba_fini(struct modlinkage *);
709 void 	sata_hba_event_notify(dev_info_t *, sata_device_t *, int);
710 sata_pkt_t *sata_get_error_retrieval_pkt(dev_info_t *, sata_device_t *, int);
711 void	sata_free_error_retrieval_pkt(sata_pkt_t *);
712 
713 
714 #ifdef	__cplusplus
715 }
716 #endif
717 
718 #endif /* _SATA_HBA_H */
719