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