xref: /openbsd/sys/dev/ata/atascsi.h (revision 5af055cd)
1 /*	$OpenBSD: atascsi.h,v 1.50 2015/12/29 09:44:46 kettenis 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_XFERMODE		0x03
57 #define  ATA_SF_XFERMODE_UDMA	0x40
58 #define ATA_SF_LOOKAHEAD_EN	0xaa
59 
60 struct ata_identify {
61 	u_int16_t	config;		/*   0 */
62 	u_int16_t	ncyls;		/*   1 */
63 	u_int16_t	reserved1;	/*   2 */
64 	u_int16_t	nheads;		/*   3 */
65 	u_int16_t	track_size;	/*   4 */
66 	u_int16_t	sector_size;	/*   5 */
67 	u_int16_t	nsectors;	/*   6 */
68 	u_int16_t	reserved2[3];	/*   7 vendor unique */
69 	u_int8_t	serial[20];	/*  10 */
70 	u_int16_t	buffer_type;	/*  20 */
71 	u_int16_t	buffer_size;	/*  21 */
72 	u_int16_t	ecc;		/*  22 */
73 	u_int8_t	firmware[8];	/*  23 */
74 	u_int8_t	model[40];	/*  27 */
75 	u_int16_t	multi;		/*  47 */
76 	u_int16_t	dwcap;		/*  48 */
77 	u_int16_t	cap;		/*  49 */
78 	u_int16_t	reserved3;	/*  50 */
79 	u_int16_t	piomode;	/*  51 */
80 	u_int16_t	dmamode;	/*  52 */
81 	u_int16_t	validinfo;	/*  53 */
82 #define ATA_ID_VALIDINFO_ULTRADMA	0x0004
83 	u_int16_t	curcyls;	/*  54 */
84 	u_int16_t	curheads;	/*  55 */
85 	u_int16_t	cursectrk;	/*  56 */
86 	u_int16_t	curseccp[2];	/*  57 */
87 	u_int16_t	mult2;		/*  59 */
88 	u_int16_t	addrsec[2];	/*  60 */
89 	u_int16_t	worddma;	/*  62 */
90 	u_int16_t	dworddma;	/*  63 */
91 	u_int16_t	advpiomode;	/*  64 */
92 	u_int16_t	minmwdma;	/*  65 */
93 	u_int16_t	recmwdma;	/*  66 */
94 	u_int16_t	minpio;		/*  67 */
95 	u_int16_t	minpioflow;	/*  68 */
96 	u_int16_t	add_support;	/*  69 */
97 #define ATA_ID_ADD_SUPPORT_DRT	0x4000
98 	u_int16_t	reserved4;	/*  70 */
99 	u_int16_t	typtime[2];	/*  71 */
100 	u_int16_t	reserved5[2];	/*  73 */
101 	u_int16_t	qdepth;		/*  75 */
102 #define ATA_QDEPTH(_q)		(((_q) & 0x1f) + 1)
103 	u_int16_t	satacap;	/*  76 */
104 #define ATA_SATACAP_GEN1	0x0002
105 #define ATA_SATACAP_GEN2	0x0004
106 #define ATA_SATACAP_NCQ		0x0100
107 #define ATA_SATACAP_PWRMGMT	0x0200
108 	u_int16_t	reserved6;	/*  77 */
109 	u_int16_t	satafsup;	/*  78 */
110 	u_int16_t	satafen;	/*  79 */
111 	u_int16_t	majver;		/*  80 */
112 	u_int16_t	minver;		/*  81 */
113 	u_int16_t	cmdset82;	/*  82 */
114 	u_int16_t	cmdset83;	/*  83 */
115 	u_int16_t	cmdset84;	/*  84 */
116 	u_int16_t	features85;	/*  85 */
117 	u_int16_t	features86;	/*  86 */
118 	u_int16_t	features87;	/*  87 */
119 #define ATA_ID_F87_WWN		(1<<8)
120 	u_int16_t	ultradma;	/*  88 */
121 	u_int16_t	erasetime;	/*  89 */
122 	u_int16_t	erasetimex;	/*  90 */
123 	u_int16_t	apm;		/*  91 */
124 	u_int16_t	masterpw;	/*  92 */
125 	u_int16_t	hwreset;	/*  93 */
126 	u_int16_t	acoustic;	/*  94 */
127 	u_int16_t	stream_min;	/*  95 */
128 	u_int16_t	stream_xfer_d;	/*  96 */
129 	u_int16_t	stream_lat;	/*  97 */
130 	u_int16_t	streamperf[2];	/*  98 */
131 	u_int16_t	addrsecxt[4];	/* 100 */
132 	u_int16_t	stream_xfer_p;	/* 104 */
133 	u_int16_t	padding1;	/* 105 */
134 	u_int16_t	p2l_sect;	/* 106 */
135 #define ATA_ID_P2L_SECT_MASK	0xc000
136 #define ATA_ID_P2L_SECT_VALID	0x4000
137 #define ATA_ID_P2L_SECT_SET	0x2000
138 #define ATA_ID_P2L_SECT_SIZESET	0x1000
139 #define ATA_ID_P2L_SECT_SIZE	0x000f
140 	u_int16_t	seek_delay;	/* 107 */
141 	u_int16_t	naa_ieee_oui;	/* 108 */
142 	u_int16_t	ieee_oui_uid;	/* 109 */
143 	u_int16_t	uid_mid;	/* 110 */
144 	u_int16_t	uid_low;	/* 111 */
145 	u_int16_t	resv_wwn[4];	/* 112 */
146 	u_int16_t	incits;		/* 116 */
147 	u_int16_t	words_lsec[2];	/* 117 */
148 	u_int16_t	cmdset119;	/* 119 */
149 	u_int16_t	features120;	/* 120 */
150 	u_int16_t	padding2[6];
151 	u_int16_t	rmsn;		/* 127 */
152 	u_int16_t	securestatus;	/* 128 */
153 	u_int16_t	vendor[31];	/* 129 */
154 	u_int16_t	padding3[8];	/* 160 */
155 	u_int16_t	form;		/* 168 */
156 #define ATA_ID_FORM_MASK	0x000f
157 	u_int16_t	data_set_mgmt;	/* 169 */
158 #define ATA_ID_DATA_SET_MGMT_TRIM 0x0001
159 	u_int16_t	padding4[6];	/* 170 */
160 	u_int16_t	curmedser[30];	/* 176 */
161 	u_int16_t	sctsupport;	/* 206 */
162 	u_int16_t	rpm;		/* 207 */
163 	u_int16_t	padding5[1];	/* 208 */
164 	u_int16_t	logical_align;	/* 209 */
165 #define ATA_ID_LALIGN_MASK	0xc000
166 #define ATA_ID_LALIGN_VALID	0x4000
167 #define ATA_ID_LALIGN		0x3fff
168 	u_int16_t	padding6[45];	/* 210 */
169 	u_int16_t	integrity;	/* 255 */
170 } __packed;
171 
172 /*
173  * IDENTIFY DEVICE data
174  */
175 #define ATA_IDENTIFY_WRITECACHE		(1 << 5)
176 #define ATA_IDENTIFY_LOOKAHEAD		(1 << 6)
177 
178 /*
179  * ATA DSM (Data Set Management) subcommands
180  */
181 #define ATA_DSM_TRIM		0x01
182 
183 #define ATA_DSM_TRIM_DESC(_lba, _len)	((_lba) | ((u_int64_t)(_len) << 48))
184 #define ATA_DSM_TRIM_MAX_LEN		0xffff
185 
186 /*
187  * Frame Information Structures
188  */
189 
190 #define ATA_FIS_LENGTH		20
191 
192 struct ata_fis_h2d {
193 	u_int8_t		type;
194 #define ATA_FIS_TYPE_H2D		0x27
195 	u_int8_t		flags;
196 #define ATA_H2D_FLAGS_CMD		(1<<7)
197 	u_int8_t		command;
198 	u_int8_t		features;
199 #define ATA_H2D_FEATURES_DMA		(1<<0)
200 #define ATA_H2D_FEATURES_DIR		(1<<2)
201 #define ATA_H2D_FEATURES_DIR_READ	(1<<2)
202 #define ATA_H2D_FEATURES_DIR_WRITE	(0<<2)
203 
204 	u_int8_t		lba_low;
205 	u_int8_t		lba_mid;
206 	u_int8_t		lba_high;
207 	u_int8_t		device;
208 #define ATA_H2D_DEVICE_LBA		0x40
209 
210 	u_int8_t		lba_low_exp;
211 	u_int8_t		lba_mid_exp;
212 	u_int8_t		lba_high_exp;
213 	u_int8_t		features_exp;
214 
215 	u_int8_t		sector_count;
216 	u_int8_t		sector_count_exp;
217 	u_int8_t		reserved0;
218 	u_int8_t		control;
219 #define ATA_FIS_CONTROL_SRST		0x04
220 #define ATA_FIS_CONTROL_4BIT		0x08
221 
222 	u_int8_t		reserved1;
223 	u_int8_t		reserved2;
224 	u_int8_t		reserved3;
225 	u_int8_t		reserved4;
226 } __packed;
227 
228 struct ata_fis_d2h {
229 	u_int8_t		type;
230 #define ATA_FIS_TYPE_D2H		0x34
231 	u_int8_t		flags;
232 #define ATA_D2H_FLAGS_INTR		(1<<6)
233 	u_int8_t		status;
234 	u_int8_t		error;
235 
236 	u_int8_t		lba_low;
237 	u_int8_t		lba_mid;
238 	u_int8_t		lba_high;
239 	u_int8_t		device;
240 
241 	u_int8_t		lba_low_exp;
242 	u_int8_t		lba_mid_exp;
243 	u_int8_t		lba_high_exp;
244 	u_int8_t		reserved0;
245 
246 	u_int8_t		sector_count;
247 	u_int8_t		sector_count_exp;
248 	u_int8_t		reserved1;
249 	u_int8_t		reserved2;
250 
251 	u_int8_t		reserved3;
252 	u_int8_t		reserved4;
253 	u_int8_t		reserved5;
254 	u_int8_t		reserved6;
255 } __packed;
256 
257 /*
258  * SATA log page 10h -
259  * looks like a D2H FIS, with errored tag number in first byte.
260  */
261 struct ata_log_page_10h {
262 	struct ata_fis_d2h	err_regs;
263 #define ATA_LOG_10H_TYPE_NOTQUEUED	0x80
264 #define ATA_LOG_10H_TYPE_TAG_MASK	0x1f
265 	u_int8_t		reserved[256 - sizeof(struct ata_fis_d2h)];
266 	u_int8_t		vendor_specific[255];
267 	u_int8_t		checksum;
268 } __packed;
269 
270 /*
271  * SATA registers
272  */
273 
274 #define SATA_SStatus_DET		0x00f
275 #define SATA_SStatus_DET_NODEV		0x000
276 #define SATA_SStatus_DET_NOPHY		0x001
277 #define SATA_SStatus_DET_DEV		0x003
278 #define SATA_SStatus_DET_OFFLINE	0x008
279 
280 #define SATA_SStatus_SPD		0x0f0
281 #define SATA_SStatus_SPD_NONE		0x000
282 #define SATA_SStatus_SPD_1_5		0x010
283 #define SATA_SStatus_SPD_3_0		0x020
284 
285 #define SATA_SStatus_IPM		0xf00
286 #define SATA_SStatus_IPM_NODEV		0x000
287 #define SATA_SStatus_IPM_ACTIVE		0x100
288 #define SATA_SStatus_IPM_PARTIAL	0x200
289 #define SATA_SStatus_IPM_SLUMBER	0x600
290 
291 #define SATA_SIGNATURE_PORT_MULTIPLIER	0x96690101
292 #define SATA_SIGNATURE_ATAPI		0xeb140101
293 #define SATA_SIGNATURE_DISK		0x00000101
294 
295 /*
296  * ATA interface
297  */
298 
299 struct ata_xfer {
300 	struct ata_fis_h2d	*fis;
301 	struct ata_fis_d2h	rfis;
302 	u_int8_t		*packetcmd;
303 	u_int8_t		tag;
304 
305 	void			*data;
306 	size_t			datalen;
307 	size_t			resid;
308 
309 	void			(*complete)(struct ata_xfer *);
310 	struct task		task;
311 	struct timeout		stimeout;
312 	u_int			timeout;
313 
314 	int			flags;
315 #define ATA_F_READ			(1<<0)
316 #define ATA_F_WRITE			(1<<1)
317 #define ATA_F_NOWAIT			(1<<2)
318 #define ATA_F_POLL			(1<<3)
319 #define ATA_F_PIO			(1<<4)
320 #define ATA_F_PACKET			(1<<5)
321 #define ATA_F_NCQ			(1<<6)
322 #define ATA_F_DONE			(1<<7)
323 #define ATA_F_GET_RFIS			(1<<8)
324 #define ATA_FMT_FLAGS			"\020" "\011GET_RFIS" "\010DONE" \
325 					"\007NCQ" "\006PACKET" "\005PIO" \
326 					"\004POLL" "\003NOWAIT" "\002WRITE" \
327 					"\001READ"
328 
329 	volatile int		state;
330 #define ATA_S_SETUP			0
331 #define ATA_S_PENDING			1
332 #define ATA_S_COMPLETE			2
333 #define ATA_S_ERROR			3
334 #define ATA_S_TIMEOUT			4
335 #define ATA_S_ONCHIP			5
336 #define ATA_S_PUT			6
337 #define ATA_S_DONE			7
338 
339 	void			*atascsi_private;
340 
341 	int			pmp_port;
342 };
343 
344 /*
345  * atascsi
346  */
347 
348 struct atascsi_methods {
349 	int			(*ata_probe)(void *, int, int);
350 	void			(*ata_free)(void *, int, int);
351 	struct ata_xfer *	(*ata_get_xfer)(void *, int);
352 	void			(*ata_put_xfer)(struct ata_xfer *);
353 	void			(*ata_cmd)(struct ata_xfer *);
354 };
355 
356 struct atascsi_attach_args {
357 	void			*aaa_cookie;
358 
359 	struct atascsi_methods	*aaa_methods;
360 	void			(*aaa_minphys)(struct buf *,
361 				    struct scsi_link *);
362 	int			aaa_nports;
363 	int			aaa_ncmds;
364 	int			aaa_capability;
365 #define ASAA_CAP_NCQ		(1 << 0)
366 #define ASAA_CAP_NEEDS_RESERVED	(1 << 1)
367 #define ASAA_CAP_PMP_NCQ	(1 << 2)
368 };
369 
370 #define ATA_PORT_T_NONE		0
371 #define ATA_PORT_T_DISK		1
372 #define ATA_PORT_T_ATAPI	2
373 #define ATA_PORT_T_PM		3
374 
375 struct atascsi	*atascsi_attach(struct device *, struct atascsi_attach_args *);
376 int		atascsi_detach(struct atascsi *, int);
377 
378 int		atascsi_probe_dev(struct atascsi *, int, int);
379 int		atascsi_detach_dev(struct atascsi *, int, int, int);
380 
381 void		ata_complete(struct ata_xfer *);
382 
383 #endif /* _DEV_ATA_ATASCSI_H_ */
384