xref: /openbsd/sys/dev/ata/atascsi.h (revision a6445c1d)
1 /*	$OpenBSD: atascsi.h,v 1.48 2013/12/09 11:44:52 dlg Exp $ */
2 
3 /*
4  * Copyright (c) 2007 David Gwynne <dlg@openbsd.org>
5  * Copyright (c) 2010 Conformal Systems LLC <info@conformal.com>
6  * Copyright (c) 2010 Jonathan Matthew <jonathan@d14n.org>
7  *
8  * Permission to use, copy, modify, and distribute this software for any
9  * purpose with or without fee is hereby granted, provided that the above
10  * copyright notice and this permission notice appear in all copies.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19  */
20 
21 #ifndef _DEV_ATA_ATASCSI_H_
22 #define _DEV_ATA_ATASCSI_H_
23 
24 #include <sys/task.h>
25 
26 struct atascsi;
27 struct scsi_link;
28 
29 /*
30  * ATA commands
31  */
32 
33 #define ATA_C_READDMA_EXT	0x25
34 #define ATA_C_READ_LOG_EXT	0x2f
35 #define ATA_C_WRITEDMA_EXT	0x35
36 #define ATA_C_READ_FPDMA	0x60
37 #define ATA_C_WRITE_FPDMA	0x61
38 #define ATA_C_PACKET		0xa0
39 #define ATA_C_IDENTIFY_PACKET	0xa1
40 #define ATA_C_READDMA		0xc8
41 #define ATA_C_WRITEDMA		0xca
42 #define ATA_C_STANDBY_IMMED	0xe0
43 #define ATA_C_READ_PM		0xe4
44 #define ATA_C_WRITE_PM		0xe8
45 #define ATA_C_FLUSH_CACHE	0xe7
46 #define ATA_C_FLUSH_CACHE_EXT	0xea /* lba48 */
47 #define ATA_C_IDENTIFY		0xec
48 #define ATA_C_SET_FEATURES	0xef
49 #define ATA_C_SEC_FREEZE_LOCK	0xf5
50 #define ATA_C_DSM		0x06
51 
52 /*
53  * ATA SET FEATURES subcommands
54  */
55 #define ATA_SF_WRITECACHE_EN	0x02
56 #define ATA_SF_LOOKAHEAD_EN	0xaa
57 
58 struct ata_identify {
59 	u_int16_t	config;		/*   0 */
60 	u_int16_t	ncyls;		/*   1 */
61 	u_int16_t	reserved1;	/*   2 */
62 	u_int16_t	nheads;		/*   3 */
63 	u_int16_t	track_size;	/*   4 */
64 	u_int16_t	sector_size;	/*   5 */
65 	u_int16_t	nsectors;	/*   6 */
66 	u_int16_t	reserved2[3];	/*   7 vendor unique */
67 	u_int8_t	serial[20];	/*  10 */
68 	u_int16_t	buffer_type;	/*  20 */
69 	u_int16_t	buffer_size;	/*  21 */
70 	u_int16_t	ecc;		/*  22 */
71 	u_int8_t	firmware[8];	/*  23 */
72 	u_int8_t	model[40];	/*  27 */
73 	u_int16_t	multi;		/*  47 */
74 	u_int16_t	dwcap;		/*  48 */
75 	u_int16_t	cap;		/*  49 */
76 	u_int16_t	reserved3;	/*  50 */
77 	u_int16_t	piomode;	/*  51 */
78 	u_int16_t	dmamode;	/*  52 */
79 	u_int16_t	validinfo;	/*  53 */
80 	u_int16_t	curcyls;	/*  54 */
81 	u_int16_t	curheads;	/*  55 */
82 	u_int16_t	cursectrk;	/*  56 */
83 	u_int16_t	curseccp[2];	/*  57 */
84 	u_int16_t	mult2;		/*  59 */
85 	u_int16_t	addrsec[2];	/*  60 */
86 	u_int16_t	worddma;	/*  62 */
87 	u_int16_t	dworddma;	/*  63 */
88 	u_int16_t	advpiomode;	/*  64 */
89 	u_int16_t	minmwdma;	/*  65 */
90 	u_int16_t	recmwdma;	/*  66 */
91 	u_int16_t	minpio;		/*  67 */
92 	u_int16_t	minpioflow;	/*  68 */
93 	u_int16_t	add_support;	/*  69 */
94 #define ATA_ID_ADD_SUPPORT_DRT	0x4000
95 	u_int16_t	reserved4;	/*  70 */
96 	u_int16_t	typtime[2];	/*  71 */
97 	u_int16_t	reserved5[2];	/*  73 */
98 	u_int16_t	qdepth;		/*  75 */
99 #define ATA_QDEPTH(_q)		(((_q) & 0x1f) + 1)
100 	u_int16_t	satacap;	/*  76 */
101 #define ATA_SATACAP_GEN1	0x0002
102 #define ATA_SATACAP_GEN2	0x0004
103 #define ATA_SATACAP_NCQ		0x0100
104 #define ATA_SATACAP_PWRMGMT	0x0200
105 	u_int16_t	reserved6;	/*  77 */
106 	u_int16_t	satafsup;	/*  78 */
107 	u_int16_t	satafen;	/*  79 */
108 	u_int16_t	majver;		/*  80 */
109 	u_int16_t	minver;		/*  81 */
110 	u_int16_t	cmdset82;	/*  82 */
111 	u_int16_t	cmdset83;	/*  83 */
112 	u_int16_t	cmdset84;	/*  84 */
113 	u_int16_t	features85;	/*  85 */
114 	u_int16_t	features86;	/*  86 */
115 	u_int16_t	features87;	/*  87 */
116 #define ATA_ID_F87_WWN		(1<<8)
117 	u_int16_t	ultradma;	/*  88 */
118 	u_int16_t	erasetime;	/*  89 */
119 	u_int16_t	erasetimex;	/*  90 */
120 	u_int16_t	apm;		/*  91 */
121 	u_int16_t	masterpw;	/*  92 */
122 	u_int16_t	hwreset;	/*  93 */
123 	u_int16_t	acoustic;	/*  94 */
124 	u_int16_t	stream_min;	/*  95 */
125 	u_int16_t	stream_xfer_d;	/*  96 */
126 	u_int16_t	stream_lat;	/*  97 */
127 	u_int16_t	streamperf[2];	/*  98 */
128 	u_int16_t	addrsecxt[4];	/* 100 */
129 	u_int16_t	stream_xfer_p;	/* 104 */
130 	u_int16_t	padding1;	/* 105 */
131 	u_int16_t	p2l_sect;	/* 106 */
132 #define ATA_ID_P2L_SECT_MASK	0xc000
133 #define ATA_ID_P2L_SECT_VALID	0x4000
134 #define ATA_ID_P2L_SECT_SET	0x2000
135 #define ATA_ID_P2L_SECT_SIZESET	0x1000
136 #define ATA_ID_P2L_SECT_SIZE	0x000f
137 	u_int16_t	seek_delay;	/* 107 */
138 	u_int16_t	naa_ieee_oui;	/* 108 */
139 	u_int16_t	ieee_oui_uid;	/* 109 */
140 	u_int16_t	uid_mid;	/* 110 */
141 	u_int16_t	uid_low;	/* 111 */
142 	u_int16_t	resv_wwn[4];	/* 112 */
143 	u_int16_t	incits;		/* 116 */
144 	u_int16_t	words_lsec[2];	/* 117 */
145 	u_int16_t	cmdset119;	/* 119 */
146 	u_int16_t	features120;	/* 120 */
147 	u_int16_t	padding2[6];
148 	u_int16_t	rmsn;		/* 127 */
149 	u_int16_t	securestatus;	/* 128 */
150 	u_int16_t	vendor[31];	/* 129 */
151 	u_int16_t	padding3[8];	/* 160 */
152 	u_int16_t	form;		/* 168 */
153 #define ATA_ID_FORM_MASK	0x000f
154 	u_int16_t	data_set_mgmt;	/* 169 */
155 #define ATA_ID_DATA_SET_MGMT_TRIM 0x0001
156 	u_int16_t	padding4[6];	/* 170 */
157 	u_int16_t	curmedser[30];	/* 176 */
158 	u_int16_t	sctsupport;	/* 206 */
159 	u_int16_t	rpm;		/* 207 */
160 	u_int16_t	padding5[1];	/* 208 */
161 	u_int16_t	logical_align;	/* 209 */
162 #define ATA_ID_LALIGN_MASK	0xc000
163 #define ATA_ID_LALIGN_VALID	0x4000
164 #define ATA_ID_LALIGN		0x3fff
165 	u_int16_t	padding6[45];	/* 210 */
166 	u_int16_t	integrity;	/* 255 */
167 } __packed;
168 
169 /*
170  * IDENTIFY DEVICE data
171  */
172 #define ATA_IDENTIFY_WRITECACHE		(1 << 5)
173 #define ATA_IDENTIFY_LOOKAHEAD		(1 << 6)
174 
175 /*
176  * ATA DSM (Data Set Management) subcommands
177  */
178 #define ATA_DSM_TRIM		0x01
179 
180 #define ATA_DSM_TRIM_DESC(_lba, _len)	((_lba) | ((u_int64_t)(_len) << 48))
181 #define ATA_DSM_TRIM_MAX_LEN		0xffff
182 
183 /*
184  * Frame Information Structures
185  */
186 
187 #define ATA_FIS_LENGTH		20
188 
189 struct ata_fis_h2d {
190 	u_int8_t		type;
191 #define ATA_FIS_TYPE_H2D		0x27
192 	u_int8_t		flags;
193 #define ATA_H2D_FLAGS_CMD		(1<<7)
194 	u_int8_t		command;
195 	u_int8_t		features;
196 #define ATA_H2D_FEATURES_DMA		(1<<0)
197 #define ATA_H2D_FEATURES_DIR		(1<<2)
198 #define ATA_H2D_FEATURES_DIR_READ	(1<<2)
199 #define ATA_H2D_FEATURES_DIR_WRITE	(0<<2)
200 
201 	u_int8_t		lba_low;
202 	u_int8_t		lba_mid;
203 	u_int8_t		lba_high;
204 	u_int8_t		device;
205 #define ATA_H2D_DEVICE_LBA		0x40
206 
207 	u_int8_t		lba_low_exp;
208 	u_int8_t		lba_mid_exp;
209 	u_int8_t		lba_high_exp;
210 	u_int8_t		features_exp;
211 
212 	u_int8_t		sector_count;
213 	u_int8_t		sector_count_exp;
214 	u_int8_t		reserved0;
215 	u_int8_t		control;
216 #define ATA_FIS_CONTROL_SRST		0x04
217 #define ATA_FIS_CONTROL_4BIT		0x08
218 
219 	u_int8_t		reserved1;
220 	u_int8_t		reserved2;
221 	u_int8_t		reserved3;
222 	u_int8_t		reserved4;
223 } __packed;
224 
225 struct ata_fis_d2h {
226 	u_int8_t		type;
227 #define ATA_FIS_TYPE_D2H		0x34
228 	u_int8_t		flags;
229 #define ATA_D2H_FLAGS_INTR		(1<<6)
230 	u_int8_t		status;
231 	u_int8_t		error;
232 
233 	u_int8_t		lba_low;
234 	u_int8_t		lba_mid;
235 	u_int8_t		lba_high;
236 	u_int8_t		device;
237 
238 	u_int8_t		lba_low_exp;
239 	u_int8_t		lba_mid_exp;
240 	u_int8_t		lba_high_exp;
241 	u_int8_t		reserved0;
242 
243 	u_int8_t		sector_count;
244 	u_int8_t		sector_count_exp;
245 	u_int8_t		reserved1;
246 	u_int8_t		reserved2;
247 
248 	u_int8_t		reserved3;
249 	u_int8_t		reserved4;
250 	u_int8_t		reserved5;
251 	u_int8_t		reserved6;
252 } __packed;
253 
254 /*
255  * SATA log page 10h -
256  * looks like a D2H FIS, with errored tag number in first byte.
257  */
258 struct ata_log_page_10h {
259 	struct ata_fis_d2h	err_regs;
260 #define ATA_LOG_10H_TYPE_NOTQUEUED	0x80
261 #define ATA_LOG_10H_TYPE_TAG_MASK	0x1f
262 	u_int8_t		reserved[256 - sizeof(struct ata_fis_d2h)];
263 	u_int8_t		vendor_specific[255];
264 	u_int8_t		checksum;
265 } __packed;
266 
267 /*
268  * SATA registers
269  */
270 
271 #define SATA_SStatus_DET		0x00f
272 #define SATA_SStatus_DET_NODEV		0x000
273 #define SATA_SStatus_DET_NOPHY		0x001
274 #define SATA_SStatus_DET_DEV		0x003
275 #define SATA_SStatus_DET_OFFLINE	0x008
276 
277 #define SATA_SStatus_SPD		0x0f0
278 #define SATA_SStatus_SPD_NONE		0x000
279 #define SATA_SStatus_SPD_1_5		0x010
280 #define SATA_SStatus_SPD_3_0		0x020
281 
282 #define SATA_SStatus_IPM		0xf00
283 #define SATA_SStatus_IPM_NODEV		0x000
284 #define SATA_SStatus_IPM_ACTIVE		0x100
285 #define SATA_SStatus_IPM_PARTIAL	0x200
286 #define SATA_SStatus_IPM_SLUMBER	0x600
287 
288 #define SATA_SIGNATURE_PORT_MULTIPLIER	0x96690101
289 #define SATA_SIGNATURE_ATAPI		0xeb140101
290 #define SATA_SIGNATURE_DISK		0x00000101
291 
292 /*
293  * ATA interface
294  */
295 
296 struct ata_xfer {
297 	struct ata_fis_h2d	*fis;
298 	struct ata_fis_d2h	rfis;
299 	u_int8_t		*packetcmd;
300 	u_int8_t		tag;
301 
302 	void			*data;
303 	size_t			datalen;
304 	size_t			resid;
305 
306 	void			(*complete)(struct ata_xfer *);
307 	struct task		task;
308 	struct timeout		stimeout;
309 	u_int			timeout;
310 
311 	int			flags;
312 #define ATA_F_READ			(1<<0)
313 #define ATA_F_WRITE			(1<<1)
314 #define ATA_F_NOWAIT			(1<<2)
315 #define ATA_F_POLL			(1<<3)
316 #define ATA_F_PIO			(1<<4)
317 #define ATA_F_PACKET			(1<<5)
318 #define ATA_F_NCQ			(1<<6)
319 #define ATA_F_DONE			(1<<7)
320 #define ATA_F_GET_RFIS			(1<<8)
321 #define ATA_FMT_FLAGS			"\020" "\011GET_RFIS" "\010DONE" \
322 					"\007NCQ" "\006PACKET" "\005PIO" \
323 					"\004POLL" "\003NOWAIT" "\002WRITE" \
324 					"\001READ"
325 
326 	volatile int		state;
327 #define ATA_S_SETUP			0
328 #define ATA_S_PENDING			1
329 #define ATA_S_COMPLETE			2
330 #define ATA_S_ERROR			3
331 #define ATA_S_TIMEOUT			4
332 #define ATA_S_ONCHIP			5
333 #define ATA_S_PUT			6
334 #define ATA_S_DONE			7
335 
336 	void			*atascsi_private;
337 
338 	int			pmp_port;
339 };
340 
341 /*
342  * atascsi
343  */
344 
345 struct atascsi_methods {
346 	int			(*probe)(void *, int, int);
347 	void			(*free)(void *, int, int);
348 	struct ata_xfer *	(*ata_get_xfer)(void *, int);
349 	void			(*ata_put_xfer)(struct ata_xfer *);
350 	void			(*ata_cmd)(struct ata_xfer *);
351 };
352 
353 struct atascsi_attach_args {
354 	void			*aaa_cookie;
355 
356 	struct atascsi_methods	*aaa_methods;
357 	void			(*aaa_minphys)(struct buf *,
358 				    struct scsi_link *);
359 	int			aaa_nports;
360 	int			aaa_ncmds;
361 	int			aaa_capability;
362 #define ASAA_CAP_NCQ		(1 << 0)
363 #define ASAA_CAP_NEEDS_RESERVED	(1 << 1)
364 #define ASAA_CAP_PMP_NCQ	(1 << 2)
365 };
366 
367 #define ATA_PORT_T_NONE		0
368 #define ATA_PORT_T_DISK		1
369 #define ATA_PORT_T_ATAPI	2
370 #define ATA_PORT_T_PM		3
371 
372 struct atascsi	*atascsi_attach(struct device *, struct atascsi_attach_args *);
373 int		atascsi_detach(struct atascsi *, int);
374 
375 int		atascsi_probe_dev(struct atascsi *, int, int);
376 int		atascsi_detach_dev(struct atascsi *, int, int, int);
377 
378 void		ata_complete(struct ata_xfer *);
379 
380 #endif /* _DEV_ATA_ATASCSI_H_ */
381