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