cee87421d6476f73f44983e4249e8894fa111f75
[dragonfly.git] / sys / dev / disk / ahci / atascsi.h
1 /*
2  * Copyright (c) 2007 David Gwynne <dlg@openbsd.org>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  *
16  * $OpenBSD: atascsi.h,v 1.33 2009/02/16 21:19:06 miod Exp $
17  */
18
19 struct atascsi;
20 struct scsi_link;
21
22 /*
23  * ATA commands
24  */
25
26 #define ATA_C_READDMA_EXT       0x25
27 #define ATA_C_READ_LOG_EXT      0x2f
28 #define ATA_C_WRITEDMA_EXT      0x35
29 #define ATA_C_READ_FPDMA        0x60
30 #define ATA_C_WRITE_FPDMA       0x61
31 #define ATA_C_PACKET            0xa0
32 #define ATA_C_ATAPI_IDENTIFY    0xa1
33 #define ATA_C_READDMA           0xc8
34 #define ATA_C_WRITEDMA          0xca
35 #define ATA_C_FLUSH_CACHE       0xe7
36 #define ATA_C_FLUSH_CACHE_EXT   0xea /* lba48 */
37 #define ATA_C_IDENTIFY          0xec
38 #define ATA_C_SET_FEATURES      0xef
39 #define ATA_C_SEC_FREEZE_LOCK   0xf5
40
41 /*
42  * ATA SET FEATURES subcommands
43  */
44 #define ATA_SF_WRITECACHE_EN    0x02
45 #define ATA_SF_LOOKAHEAD_EN     0xaa
46
47 struct ata_identify {
48         u_int16_t       config;         /*   0 */
49         u_int16_t       ncyls;          /*   1 */
50         u_int16_t       reserved1;      /*   2 */
51         u_int16_t       nheads;         /*   3 */
52         u_int16_t       track_size;     /*   4 */
53         u_int16_t       sector_size;    /*   5 */
54         u_int16_t       nsectors;       /*   6 */
55         u_int16_t       reserved2[3];   /*   7 vendor unique */
56         u_int8_t        serial[20];     /*  10 */
57         u_int16_t       buffer_type;    /*  20 */
58         u_int16_t       buffer_size;    /*  21 */
59         u_int16_t       ecc;            /*  22 */
60         u_int8_t        firmware[8];    /*  23 */
61         u_int8_t        model[40];      /*  27 */
62         u_int16_t       multi;          /*  47 */
63         u_int16_t       dwcap;          /*  48 */
64         u_int16_t       cap;            /*  49 */
65         u_int16_t       reserved3;      /*  50 */
66         u_int16_t       piomode;        /*  51 */
67         u_int16_t       dmamode;        /*  52 */
68         u_int16_t       validinfo;      /*  53 */
69         u_int16_t       curcyls;        /*  54 */
70         u_int16_t       curheads;       /*  55 */
71         u_int16_t       cursectrk;      /*  56 */
72         u_int16_t       curseccp[2];    /*  57 */
73         u_int16_t       mult2;          /*  59 */
74         u_int16_t       addrsec[2];     /*  60 */
75         u_int16_t       worddma;        /*  62 */
76         u_int16_t       dworddma;       /*  63 */
77         u_int16_t       advpiomode;     /*  64 */
78         u_int16_t       minmwdma;       /*  65 */
79         u_int16_t       recmwdma;       /*  66 */
80         u_int16_t       minpio;         /*  67 */
81         u_int16_t       minpioflow;     /*  68 */
82         u_int16_t       reserved4[2];   /*  69 */
83         u_int16_t       typtime[2];     /*  71 */
84         u_int16_t       reserved5[2];   /*  73 */
85         u_int16_t       qdepth;         /*  75 */
86         u_int16_t       satacap;        /*  76 */
87         u_int16_t       reserved6;      /*  77 */
88         u_int16_t       satafsup;       /*  78 */
89         u_int16_t       satafen;        /*  79 */
90         u_int16_t       majver;         /*  80 */
91         u_int16_t       minver;         /*  81 */
92         u_int16_t       cmdset82;       /*  82 */
93         u_int16_t       cmdset83;       /*  83 */
94         u_int16_t       cmdset84;       /*  84 */
95         u_int16_t       features85;     /*  85 */
96         u_int16_t       features86;     /*  86 */
97         u_int16_t       features87;     /*  87 */
98 #define ATA_ID_F87_WWN          (1<<8)
99         u_int16_t       ultradma;       /*  88 */
100         u_int16_t       erasetime;      /*  89 */
101         u_int16_t       erasetimex;     /*  90 */
102         u_int16_t       apm;            /*  91 */
103         u_int16_t       masterpw;       /*  92 */
104         u_int16_t       hwreset;        /*  93 */
105         u_int16_t       acoustic;       /*  94 */
106         u_int16_t       stream_min;     /*  95 */
107         u_int16_t       stream_xfer_d;  /*  96 */
108         u_int16_t       stream_lat;     /*  97 */
109         u_int16_t       streamperf[2];  /*  98 */
110         u_int16_t       addrsecxt[4];   /* 100 */
111         u_int16_t       stream_xfer_p;  /* 104 */
112         u_int16_t       padding1;       /* 105 */
113         u_int16_t       phys_sect_sz;   /* 106 */
114         u_int16_t       seek_delay;     /* 107 */
115         u_int16_t       naa_ieee_oui;   /* 108 */
116         u_int16_t       ieee_oui_uid;   /* 109 */
117         u_int16_t       uid_mid;        /* 110 */
118         u_int16_t       uid_low;        /* 111 */
119         u_int16_t       resv_wwn[4];    /* 112 */
120         u_int16_t       incits;         /* 116 */
121         u_int16_t       words_lsec[2];  /* 117 */
122         u_int16_t       cmdset119;      /* 119 */
123         u_int16_t       features120;    /* 120 */
124         u_int16_t       padding2[6];
125         u_int16_t       rmsn;           /* 127 */
126         u_int16_t       securestatus;   /* 128 */
127 #define ATA_SECURE_LOCKED               (1<<2)
128 #define ATA_SECURE_FROZEN               (1<<3)
129         u_int16_t       vendor[31];     /* 129 */
130         u_int16_t       padding3[16];   /* 160 */
131         u_int16_t       curmedser[30];  /* 176 */
132         u_int16_t       sctsupport;     /* 206 */
133         u_int16_t       padding4[48];   /* 207 */
134         u_int16_t       integrity;      /* 255 */
135 } __packed;
136
137 /*
138  * IDENTIFY DEVICE data
139  */
140 #define ATA_IDENTIFY_SECURITY           (1 << 1)
141 #define ATA_IDENTIFY_WRITECACHE         (1 << 5)
142 #define ATA_IDENTIFY_LOOKAHEAD          (1 << 6)
143
144 /*
145  * Frame Information Structures
146  */
147
148 #define ATA_FIS_LENGTH          20
149
150 struct ata_fis_h2d {
151         u_int8_t                type;
152 #define ATA_FIS_TYPE_H2D                0x27
153         u_int8_t                flags;
154 #define ATA_H2D_FLAGS_CMD               (1<<7)
155         u_int8_t                command;
156         u_int8_t                features;
157 #define ATA_H2D_FEATURES_DMA            (1<<0)
158 #define ATA_H2D_FEATURES_DIR            (1<<2)
159 #define ATA_H2D_FEATURES_DIR_READ       (1<<2)
160 #define ATA_H2D_FEATURES_DIR_WRITE      (0<<2)
161
162         u_int8_t                lba_low;
163         u_int8_t                lba_mid;
164         u_int8_t                lba_high;
165         u_int8_t                device;
166 #define ATA_H2D_DEVICE_LBA              0x40
167
168         u_int8_t                lba_low_exp;
169         u_int8_t                lba_mid_exp;
170         u_int8_t                lba_high_exp;
171         u_int8_t                features_exp;
172
173         u_int8_t                sector_count;
174         u_int8_t                sector_count_exp;
175         u_int8_t                reserved0;
176         u_int8_t                control;
177
178         u_int8_t                reserved1;
179         u_int8_t                reserved2;
180         u_int8_t                reserved3;
181         u_int8_t                reserved4;
182 } __packed;
183
184 struct ata_fis_d2h {
185         u_int8_t                type;
186 #define ATA_FIS_TYPE_D2H                0x34
187         u_int8_t                flags;
188 #define ATA_D2H_FLAGS_INTR              (1<<6)
189         u_int8_t                status;
190         u_int8_t                error;
191
192         u_int8_t                lba_low;
193         u_int8_t                lba_mid;
194         u_int8_t                lba_high;
195         u_int8_t                device;
196
197         u_int8_t                lba_low_exp;
198         u_int8_t                lba_mid_exp;
199         u_int8_t                lba_high_exp;
200         u_int8_t                reserved0;
201
202         u_int8_t                sector_count;
203         u_int8_t                sector_count_exp;
204         u_int8_t                reserved1;
205         u_int8_t                reserved2;
206
207         u_int8_t                reserved3;
208         u_int8_t                reserved4;
209         u_int8_t                reserved5;
210         u_int8_t                reserved6;
211 } __packed;
212
213 /*
214  * SATA log page 10h -
215  * looks like a D2H FIS, with errored tag number in first byte.
216  */
217 struct ata_log_page_10h {
218         struct ata_fis_d2h      err_regs;
219 #define ATA_LOG_10H_TYPE_NOTQUEUED      0x80
220 #define ATA_LOG_10H_TYPE_TAG_MASK       0x1f
221         u_int8_t                reserved[256 - sizeof(struct ata_fis_d2h)];
222         u_int8_t                vendor_specific[255];
223         u_int8_t                checksum;
224 } __packed;
225
226 /*
227  * SATA registers
228  */
229
230 #define SATA_SStatus_DET                0x00f
231 #define SATA_SStatus_DET_NODEV          0x000
232 #define SATA_SStatus_DET_NOPHY          0x001
233 #define SATA_SStatus_DET_DEV            0x003
234 #define SATA_SStatus_DET_OFFLINE        0x008
235
236 #define SATA_SStatus_SPD                0x0f0
237 #define SATA_SStatus_SPD_NONE           0x000
238 #define SATA_SStatus_SPD_1_5            0x010
239 #define SATA_SStatus_SPD_3_0            0x020
240
241 #define SATA_SStatus_IPM                0xf00
242 #define SATA_SStatus_IPM_NODEV          0x000
243 #define SATA_SStatus_IPM_ACTIVE         0x100
244 #define SATA_SStatus_IPM_PARTIAL        0x200
245 #define SATA_SStatus_IPM_SLUMBER        0x600
246
247 #define SATA_SIGNATURE_PORT_MULTIPLIER  0x96690101
248 #define SATA_SIGNATURE_ATAPI            0xeb140101
249 #define SATA_SIGNATURE_DISK             0x00000101
250
251 /*
252  * ATA interface
253  */
254
255 struct ata_port {
256         struct ata_identify     ap_identify;    /* only if ATA_PORT_T_DISK */
257         struct atascsi          *ap_as;
258         int                     ap_type;
259 #define ATA_PORT_T_NONE                 0
260 #define ATA_PORT_T_DISK                 1
261 #define ATA_PORT_T_ATAPI                2
262         int                     ap_features;
263 #define ATA_PORT_F_PROBED               (1 << 0)
264 #define ATA_PORT_F_WCACHE               (1 << 1)
265 #define ATA_PORT_F_RAHEAD               (1 << 2)
266 #define ATA_PORT_F_FRZLCK               (1 << 3)
267         int                     ap_ncqdepth;
268         u_int64_t               ap_capacity;    /* only if ATA_PORT_T_DISK */
269 };
270
271 struct ata_xfer {
272         struct ata_fis_h2d      *fis;
273         struct ata_fis_d2h      rfis;
274         u_int8_t                *packetcmd;
275         u_int8_t                tag;
276
277         void                    *data;
278         size_t                  datalen;
279         size_t                  resid;
280
281         void                    (*complete)(struct ata_xfer *);
282         u_int                   timeout;
283
284         int                     flags;
285 #define ATA_F_READ                      (1<<0)
286 #define ATA_F_WRITE                     (1<<1)
287 #define ATA_F_NOWAIT                    (1<<2)
288 #define ATA_F_POLL                      (1<<3)
289 #define ATA_F_PIO                       (1<<4)
290 #define ATA_F_PACKET                    (1<<5)
291 #define ATA_F_NCQ                       (1<<6)
292 #define ATA_F_TIMEOUT_RUNNING           (1<<7)
293 #define ATA_FMT_FLAGS                   "\020" "\010TRUNNING" \
294                                         "\007NCQ" "\006PACKET" \
295                                         "\005PIO" "\004POLL" "\003NOWAIT" \
296                                         "\002WRITE" "\001READ"
297
298         volatile int            state;
299 #define ATA_S_SETUP                     0
300 #define ATA_S_PENDING                   1
301 #define ATA_S_COMPLETE                  2
302 #define ATA_S_ERROR                     3
303 #define ATA_S_TIMEOUT                   4
304 #define ATA_S_ONCHIP                    5
305 #define ATA_S_PUT                       6
306
307         void                    *atascsi_private;
308
309         void                    (*ata_put_xfer)(struct ata_xfer *);
310 };
311
312 #define ATA_QUEUED              0
313 #define ATA_COMPLETE            1
314 #define ATA_ERROR               2