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