xref: /freebsd/sys/dev/ata/ata-all.h (revision f0a75d274af375d15b97b830966b99a02b7db911)
1 /*-
2  * Copyright (c) 1998 - 2007 S�ren Schmidt <sos@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification, immediately at the beginning of the file.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 /* ATA register defines */
30 #define ATA_DATA                        0       /* (RW) data */
31 
32 #define ATA_FEATURE                     1       /* (W) feature */
33 #define         ATA_F_DMA               0x01    /* enable DMA */
34 #define         ATA_F_OVL               0x02    /* enable overlap */
35 
36 #define ATA_COUNT                       2       /* (W) sector count */
37 
38 #define ATA_SECTOR                      3       /* (RW) sector # */
39 #define ATA_CYL_LSB                     4       /* (RW) cylinder# LSB */
40 #define ATA_CYL_MSB                     5       /* (RW) cylinder# MSB */
41 #define ATA_DRIVE                       6       /* (W) Sector/Drive/Head */
42 #define         ATA_D_LBA               0x40    /* use LBA addressing */
43 #define         ATA_D_IBM               0xa0    /* 512 byte sectors, ECC */
44 
45 #define ATA_COMMAND                     7       /* (W) command */
46 
47 #define ATA_ERROR                       8       /* (R) error */
48 #define         ATA_E_ILI               0x01    /* illegal length */
49 #define         ATA_E_NM                0x02    /* no media */
50 #define         ATA_E_ABORT             0x04    /* command aborted */
51 #define         ATA_E_MCR               0x08    /* media change request */
52 #define         ATA_E_IDNF              0x10    /* ID not found */
53 #define         ATA_E_MC                0x20    /* media changed */
54 #define         ATA_E_UNC               0x40    /* uncorrectable data */
55 #define         ATA_E_ICRC              0x80    /* UDMA crc error */
56 #define		ATA_E_ATAPI_SENSE_MASK	0xf0	/* ATAPI sense key mask */
57 
58 #define ATA_IREASON                     9       /* (R) interrupt reason */
59 #define         ATA_I_CMD               0x01    /* cmd (1) | data (0) */
60 #define         ATA_I_IN                0x02    /* read (1) | write (0) */
61 #define         ATA_I_RELEASE           0x04    /* released bus (1) */
62 #define         ATA_I_TAGMASK           0xf8    /* tag mask */
63 
64 #define ATA_STATUS                      10      /* (R) status */
65 #define ATA_ALTSTAT                     11      /* (R) alternate status */
66 #define         ATA_S_ERROR             0x01    /* error */
67 #define         ATA_S_INDEX             0x02    /* index */
68 #define         ATA_S_CORR              0x04    /* data corrected */
69 #define         ATA_S_DRQ               0x08    /* data request */
70 #define         ATA_S_DSC               0x10    /* drive seek completed */
71 #define         ATA_S_SERVICE           0x10    /* drive needs service */
72 #define         ATA_S_DWF               0x20    /* drive write fault */
73 #define         ATA_S_DMA               0x20    /* DMA ready */
74 #define         ATA_S_READY             0x40    /* drive ready */
75 #define         ATA_S_BUSY              0x80    /* busy */
76 
77 #define ATA_CONTROL                     12      /* (W) control */
78 
79 #define ATA_CTLOFFSET                   0x206   /* control register offset */
80 #define ATA_PCCARD_CTLOFFSET            0x0e    /* do for PCCARD devices */
81 #define ATA_PC98_CTLOFFSET              0x10c   /* do for PC98 devices */
82 #define         ATA_A_IDS               0x02    /* disable interrupts */
83 #define         ATA_A_RESET             0x04    /* RESET controller */
84 #define         ATA_A_4BIT              0x08    /* 4 head bits */
85 #define         ATA_A_HOB               0x80    /* High Order Byte enable */
86 
87 /* SATA register defines */
88 #define ATA_SSTATUS                     13
89 #define         ATA_SS_DET_MASK         0x0000000f
90 #define         ATA_SS_DET_NO_DEVICE    0x00000000
91 #define         ATA_SS_DET_DEV_PRESENT  0x00000001
92 #define         ATA_SS_DET_PHY_ONLINE   0x00000003
93 #define         ATA_SS_DET_PHY_OFFLINE  0x00000004
94 
95 #define         ATA_SS_SPD_MASK         0x000000f0
96 #define         ATA_SS_SPD_NO_SPEED     0x00000000
97 #define         ATA_SS_SPD_GEN1         0x00000010
98 #define         ATA_SS_SPD_GEN2         0x00000020
99 
100 #define         ATA_SS_IPM_MASK         0x00000f00
101 #define         ATA_SS_IPM_NO_DEVICE    0x00000000
102 #define         ATA_SS_IPM_ACTIVE       0x00000100
103 #define         ATA_SS_IPM_PARTIAL      0x00000200
104 #define         ATA_SS_IPM_SLUMBER      0x00000600
105 
106 #define         ATA_SS_CONWELL_MASK \
107 		    (ATA_SS_DET_MASK|ATA_SS_SPD_MASK|ATA_SS_IPM_MASK)
108 #define         ATA_SS_CONWELL_GEN1 \
109 		    (ATA_SS_DET_PHY_ONLINE|ATA_SS_SPD_GEN1|ATA_SS_IPM_ACTIVE)
110 #define         ATA_SS_CONWELL_GEN2 \
111 		    (ATA_SS_DET_PHY_ONLINE|ATA_SS_SPD_GEN2|ATA_SS_IPM_ACTIVE)
112 
113 #define ATA_SERROR                      14
114 #define         ATA_SE_DATA_CORRECTED   0x00000001
115 #define         ATA_SE_COMM_CORRECTED   0x00000002
116 #define         ATA_SE_DATA_ERR         0x00000100
117 #define         ATA_SE_COMM_ERR         0x00000200
118 #define         ATA_SE_PROT_ERR         0x00000400
119 #define         ATA_SE_HOST_ERR         0x00000800
120 #define         ATA_SE_PHY_CHANGED      0x00010000
121 #define         ATA_SE_PHY_IERROR       0x00020000
122 #define         ATA_SE_COMM_WAKE        0x00040000
123 #define         ATA_SE_DECODE_ERR       0x00080000
124 #define         ATA_SE_PARITY_ERR       0x00100000
125 #define         ATA_SE_CRC_ERR          0x00200000
126 #define         ATA_SE_HANDSHAKE_ERR    0x00400000
127 #define         ATA_SE_LINKSEQ_ERR      0x00800000
128 #define         ATA_SE_TRANSPORT_ERR    0x01000000
129 #define         ATA_SE_UNKNOWN_FIS      0x02000000
130 
131 #define ATA_SCONTROL                    15
132 #define         ATA_SC_DET_MASK         0x0000000f
133 #define         ATA_SC_DET_IDLE         0x00000000
134 #define         ATA_SC_DET_RESET        0x00000001
135 #define         ATA_SC_DET_DISABLE      0x00000004
136 
137 #define         ATA_SC_SPD_MASK         0x000000f0
138 #define         ATA_SC_SPD_NO_SPEED     0x00000000
139 #define         ATA_SC_SPD_SPEED_GEN1   0x00000010
140 #define         ATA_SC_SPD_SPEED_GEN2   0x00000020
141 
142 #define         ATA_SC_IPM_MASK         0x00000f00
143 #define         ATA_SC_IPM_NONE         0x00000000
144 #define         ATA_SC_IPM_DIS_PARTIAL  0x00000100
145 #define         ATA_SC_IPM_DIS_SLUMBER  0x00000200
146 
147 #define ATA_SACTIVE                     16
148 
149 /* SATA AHCI v1.0 register defines */
150 #define ATA_AHCI_CAP                    0x00
151 #define         ATA_AHCI_NPMASK         0x1f
152 #define		ATA_AHCI_CAP_CLO	0x01000000
153 #define		ATA_AHCI_CAP_64BIT	0x80000000
154 
155 #define ATA_AHCI_GHC                    0x04
156 #define         ATA_AHCI_GHC_AE         0x80000000
157 #define         ATA_AHCI_GHC_IE         0x00000002
158 #define         ATA_AHCI_GHC_HR         0x80000001
159 
160 #define ATA_AHCI_IS                     0x08
161 #define ATA_AHCI_PI                     0x0c
162 #define ATA_AHCI_VS                     0x10
163 
164 #define ATA_AHCI_OFFSET                 0x80
165 
166 #define ATA_AHCI_P_CLB                  0x100
167 #define ATA_AHCI_P_CLBU                 0x104
168 #define ATA_AHCI_P_FB                   0x108
169 #define ATA_AHCI_P_FBU                  0x10c
170 #define ATA_AHCI_P_IS                   0x110
171 #define ATA_AHCI_P_IE                   0x114
172 #define         ATA_AHCI_P_IX_DHR       0x00000001
173 #define         ATA_AHCI_P_IX_PS        0x00000002
174 #define         ATA_AHCI_P_IX_DS        0x00000004
175 #define         ATA_AHCI_P_IX_SDB       0x00000008
176 #define         ATA_AHCI_P_IX_UF        0x00000010
177 #define         ATA_AHCI_P_IX_DP        0x00000020
178 #define         ATA_AHCI_P_IX_PC        0x00000040
179 #define         ATA_AHCI_P_IX_DI        0x00000080
180 
181 #define         ATA_AHCI_P_IX_PRC       0x00400000
182 #define         ATA_AHCI_P_IX_IPM       0x00800000
183 #define         ATA_AHCI_P_IX_OF        0x01000000
184 #define         ATA_AHCI_P_IX_INF       0x04000000
185 #define         ATA_AHCI_P_IX_IF        0x08000000
186 #define         ATA_AHCI_P_IX_HBD       0x10000000
187 #define         ATA_AHCI_P_IX_HBF       0x20000000
188 #define         ATA_AHCI_P_IX_TFE       0x40000000
189 #define         ATA_AHCI_P_IX_CPD       0x80000000
190 
191 #define ATA_AHCI_P_CMD                  0x118
192 #define         ATA_AHCI_P_CMD_ST       0x00000001
193 #define         ATA_AHCI_P_CMD_SUD      0x00000002
194 #define         ATA_AHCI_P_CMD_POD      0x00000004
195 #define         ATA_AHCI_P_CMD_CLO      0x00000008
196 #define         ATA_AHCI_P_CMD_FRE      0x00000010
197 #define         ATA_AHCI_P_CMD_CCS_MASK 0x00001f00
198 #define         ATA_AHCI_P_CMD_ISS      0x00002000
199 #define         ATA_AHCI_P_CMD_FR       0x00004000
200 #define         ATA_AHCI_P_CMD_CR       0x00008000
201 #define         ATA_AHCI_P_CMD_CPS      0x00010000
202 #define         ATA_AHCI_P_CMD_PMA      0x00020000
203 #define         ATA_AHCI_P_CMD_HPCP     0x00040000
204 #define         ATA_AHCI_P_CMD_ISP      0x00080000
205 #define         ATA_AHCI_P_CMD_CPD      0x00100000
206 #define         ATA_AHCI_P_CMD_ATAPI    0x01000000
207 #define         ATA_AHCI_P_CMD_DLAE     0x02000000
208 #define         ATA_AHCI_P_CMD_ALPE     0x04000000
209 #define         ATA_AHCI_P_CMD_ASP      0x08000000
210 #define         ATA_AHCI_P_CMD_ICC_MASK 0xf0000000
211 #define         ATA_AHCI_P_CMD_NOOP     0x00000000
212 #define         ATA_AHCI_P_CMD_ACTIVE   0x10000000
213 #define         ATA_AHCI_P_CMD_PARTIAL  0x20000000
214 #define         ATA_AHCI_P_CMD_SLUMPER  0x60000000
215 
216 #define ATA_AHCI_P_TFD                  0x120
217 #define ATA_AHCI_P_SIG                  0x124
218 #define ATA_AHCI_P_SSTS                 0x128
219 #define ATA_AHCI_P_SCTL                 0x12c
220 #define ATA_AHCI_P_SERR                 0x130
221 #define ATA_AHCI_P_SACT                 0x134
222 #define ATA_AHCI_P_CI                   0x138
223 
224 #define ATA_AHCI_CL_SIZE                32
225 #define ATA_AHCI_CL_OFFSET              0
226 #define ATA_AHCI_FB_OFFSET              1024
227 #define ATA_AHCI_CT_OFFSET              1024+256
228 #define ATA_AHCI_CT_SG_OFFSET           128
229 #define ATA_AHCI_CT_SIZE                256
230 
231 struct ata_ahci_dma_prd {
232     u_int64_t                   dba;
233     u_int32_t                   reserved;
234     u_int32_t                   dbc;            /* 0 based */
235 #define ATA_AHCI_PRD_MASK       0x003fffff      /* max 4MB */
236 #define ATA_AHCI_PRD_IPC        (1<<31)
237 } __packed;
238 
239 struct ata_ahci_cmd_tab {
240     u_int8_t                    cfis[64];
241     u_int8_t                    acmd[32];
242     u_int8_t                    reserved[32];
243     struct ata_ahci_dma_prd     prd_tab[16];
244 } __packed;
245 
246 struct ata_ahci_cmd_list {
247     u_int16_t                   cmd_flags;
248     u_int16_t                   prd_length;     /* PRD entries */
249     u_int32_t                   bytecount;
250     u_int64_t                   cmd_table_phys; /* 128byte aligned */
251 } __packed;
252 
253 
254 /* DMA register defines */
255 #define ATA_DMA_ENTRIES                 256
256 #define ATA_DMA_EOT                     0x80000000
257 
258 #define ATA_BMCMD_PORT                  17
259 #define         ATA_BMCMD_START_STOP    0x01
260 #define         ATA_BMCMD_WRITE_READ    0x08
261 
262 #define ATA_BMDEVSPEC_0                 18
263 #define ATA_BMSTAT_PORT                 19
264 #define         ATA_BMSTAT_ACTIVE       0x01
265 #define         ATA_BMSTAT_ERROR        0x02
266 #define         ATA_BMSTAT_INTERRUPT    0x04
267 #define         ATA_BMSTAT_MASK         0x07
268 #define         ATA_BMSTAT_DMA_MASTER   0x20
269 #define         ATA_BMSTAT_DMA_SLAVE    0x40
270 #define         ATA_BMSTAT_DMA_SIMPLEX  0x80
271 
272 #define ATA_BMDEVSPEC_1                 20
273 #define ATA_BMDTP_PORT                  21
274 
275 #define ATA_IDX_ADDR                    22
276 #define ATA_IDX_DATA                    23
277 #define ATA_MAX_RES                     24
278 
279 /* misc defines */
280 #define ATA_PRIMARY                     0x1f0
281 #define ATA_SECONDARY                   0x170
282 #define ATA_PC98_BANK                   0x432
283 #define ATA_IOSIZE                      0x08
284 #define ATA_PC98_IOSIZE                 0x10
285 #define ATA_CTLIOSIZE                   0x01
286 #define ATA_BMIOSIZE                    0x08
287 #define ATA_PC98_BANKIOSIZE             0x01
288 #define ATA_IOADDR_RID                  0
289 #define ATA_CTLADDR_RID                 1
290 #define ATA_BMADDR_RID                  0x20
291 #define ATA_PC98_CTLADDR_RID            8
292 #define ATA_PC98_BANKADDR_RID           9
293 #define ATA_IRQ_RID                     0
294 #define ATA_DEV(device)                 ((device == ATA_MASTER) ? 0 : 1)
295 #define ATA_CFA_MAGIC1                  0x844A
296 #define ATA_CFA_MAGIC2                  0x848A
297 #define ATAPI_MAGIC_LSB                 0x14
298 #define ATAPI_MAGIC_MSB                 0xeb
299 #define ATAPI_P_READ                    (ATA_S_DRQ | ATA_I_IN)
300 #define ATAPI_P_WRITE                   (ATA_S_DRQ)
301 #define ATAPI_P_CMDOUT                  (ATA_S_DRQ | ATA_I_CMD)
302 #define ATAPI_P_DONEDRQ                 (ATA_S_DRQ | ATA_I_CMD | ATA_I_IN)
303 #define ATAPI_P_DONE                    (ATA_I_CMD | ATA_I_IN)
304 #define ATAPI_P_ABORT                   0
305 #define ATA_INTR_FLAGS                  (INTR_MPSAFE|INTR_TYPE_BIO|INTR_ENTROPY)
306 #define ATA_OP_CONTINUES                0
307 #define ATA_OP_FINISHED                 1
308 #define ATA_MAX_28BIT_LBA               268435455UL
309 
310 /* structure used for composite atomic operations */
311 #define MAX_COMPOSITES          32              /* u_int32_t bits */
312 struct ata_composite {
313     struct mtx          lock;                   /* control lock */
314     u_int32_t           rd_needed;              /* needed read subdisks */
315     u_int32_t           rd_done;                /* done read subdisks */
316     u_int32_t           wr_needed;              /* needed write subdisks */
317     u_int32_t           wr_depend;              /* write depends on subdisks */
318     u_int32_t           wr_done;                /* done write subdisks */
319     struct ata_request  *request[MAX_COMPOSITES];
320     u_int32_t           residual;               /* bytes still to transfer */
321     caddr_t             data_1;
322     caddr_t             data_2;
323 };
324 
325 /* structure used to queue an ATA/ATAPI request */
326 struct ata_request {
327     device_t                    dev;            /* device handle */
328     device_t                    parent;         /* channel handle */
329     union {
330 	struct {
331 	    u_int8_t            command;        /* command reg */
332 	    u_int16_t           feature;        /* feature reg */
333 	    u_int16_t           count;          /* count reg */
334 	    u_int64_t           lba;            /* lba reg */
335 	} ata;
336 	struct {
337 	    u_int8_t            ccb[16];        /* ATAPI command block */
338 	    struct atapi_sense  sense;          /* ATAPI request sense data */
339 	    u_int8_t            saved_cmd;      /* ATAPI saved command */
340 	} atapi;
341     } u;
342     u_int32_t                   bytecount;      /* bytes to transfer */
343     u_int32_t                   transfersize;   /* bytes pr transfer */
344     caddr_t                     data;           /* pointer to data buf */
345     int                         flags;
346 #define         ATA_R_CONTROL           0x00000001
347 #define         ATA_R_READ              0x00000002
348 #define         ATA_R_WRITE             0x00000004
349 #define         ATA_R_ATAPI             0x00000008
350 #define         ATA_R_DMA               0x00000010
351 #define         ATA_R_QUIET             0x00000020
352 #define         ATA_R_TIMEOUT           0x00000040
353 
354 #define         ATA_R_ORDERED           0x00000100
355 #define         ATA_R_AT_HEAD           0x00000200
356 #define         ATA_R_REQUEUE           0x00000400
357 #define         ATA_R_THREAD            0x00000800
358 #define         ATA_R_DIRECT            0x00001000
359 
360 #define         ATA_R_DEBUG             0x10000000
361 #define         ATA_R_DANGER1           0x20000000
362 #define         ATA_R_DANGER2           0x40000000
363 
364     u_int8_t                    status;         /* ATA status */
365     u_int8_t                    error;          /* ATA error */
366     u_int8_t                    dmastat;        /* DMA status */
367     u_int32_t                   donecount;      /* bytes transferred */
368     int                         result;         /* result error code */
369     void                        (*callback)(struct ata_request *request);
370     struct sema                 done;           /* request done sema */
371     int                         retries;        /* retry count */
372     int                         timeout;        /* timeout for this cmd */
373     struct callout              callout;        /* callout management */
374     struct task                 task;           /* task management */
375     struct bio                  *bio;           /* bio for this request */
376     int                         this;           /* this request ID */
377     struct ata_composite        *composite;     /* for composite atomic ops */
378     void                        *driver;        /* driver specific */
379     TAILQ_ENTRY(ata_request)    chain;          /* list management */
380 };
381 
382 /* define this for debugging request processing */
383 #if 0
384 #define ATA_DEBUG_RQ(request, string) \
385     { \
386     if (request->flags & ATA_R_DEBUG) \
387 	device_printf(request->dev, "req=%p %s " string "\n", \
388 		      request, ata_cmd2str(request)); \
389     }
390 #else
391 #define ATA_DEBUG_RQ(request, string)
392 #endif
393 
394 
395 /* structure describing an ATA/ATAPI device */
396 struct ata_device {
397     device_t                    dev;            /* device handle */
398     int                         unit;           /* physical unit */
399 #define         ATA_MASTER              0x00
400 #define         ATA_SLAVE               0x10
401 
402     struct ata_params           param;          /* ata param structure */
403     int                         mode;           /* current transfermode */
404     u_int32_t                   max_iosize;     /* max IO size */
405     int                         flags;
406 #define         ATA_D_USE_CHS           0x0001
407 #define         ATA_D_MEDIA_CHANGED     0x0002
408 #define         ATA_D_ENC_PRESENT       0x0004
409 #define         ATA_D_48BIT_ACTIVE      0x0008
410 };
411 
412 /* structure for holding DMA Physical Region Descriptors (PRD) entries */
413 struct ata_dma_prdentry {
414     u_int32_t addr;
415     u_int32_t count;
416 };
417 
418 /* structure used by the setprd function */
419 struct ata_dmasetprd_args {
420     void *dmatab;
421     int nsegs;
422     int error;
423 };
424 
425 /* structure holding DMA related information */
426 struct ata_dma {
427     bus_dma_tag_t               dmatag;         /* parent DMA tag */
428     bus_dma_tag_t               sg_tag;         /* SG list DMA tag */
429     bus_dmamap_t                sg_map;         /* SG list DMA map */
430     void                        *sg;            /* DMA transfer table */
431     bus_addr_t                  sg_bus;         /* bus address of dmatab */
432     bus_dma_tag_t               data_tag;       /* data DMA tag */
433     bus_dmamap_t                data_map;       /* data DMA map */
434     bus_dma_tag_t               work_tag;       /* workspace DMA tag */
435     bus_dmamap_t                work_map;       /* workspace DMA map */
436     u_int8_t                    *work;          /* workspace */
437     bus_addr_t                  work_bus;       /* bus address of dmatab */
438 
439     u_int32_t                   alignment;      /* DMA SG list alignment */
440     u_int32_t                   boundary;       /* DMA SG list boundary */
441     u_int32_t                   segsize;        /* DMA SG list segment size */
442     u_int32_t                   max_iosize;     /* DMA data max IO size */
443     u_int32_t                   cur_iosize;     /* DMA data current IO size */
444     int                         flags;
445 #define ATA_DMA_READ                    0x01    /* transaction is a read */
446 #define ATA_DMA_LOADED                  0x02    /* DMA tables etc loaded */
447 #define ATA_DMA_ACTIVE                  0x04    /* DMA transfer in progress */
448 
449     void (*alloc)(device_t dev);
450     void (*free)(device_t dev);
451     void (*setprd)(void *xsc, bus_dma_segment_t *segs, int nsegs, int error);
452     int (*load)(device_t dev, caddr_t data, int32_t count, int dir, void *addr, int *nsegs);
453     int (*unload)(device_t dev);
454     int (*start)(device_t dev);
455     int (*stop)(device_t dev);
456     void (*reset)(device_t dev);
457 };
458 
459 /* structure holding lowlevel functions */
460 struct ata_lowlevel {
461     int (*status)(device_t dev);
462     int (*begin_transaction)(struct ata_request *request);
463     int (*end_transaction)(struct ata_request *request);
464     int (*command)(struct ata_request *request);
465 };
466 
467 /* structure holding resources for an ATA channel */
468 struct ata_resource {
469     struct resource             *res;
470     int                         offset;
471 };
472 
473 /* structure describing an ATA channel */
474 struct ata_channel {
475     device_t                    dev;            /* device handle */
476     int                         unit;           /* physical channel */
477     struct ata_resource         r_io[ATA_MAX_RES];/* I/O resources */
478     struct resource             *r_irq;         /* interrupt of this channel */
479     void                        *ih;            /* interrupt handle */
480     struct ata_lowlevel         hw;             /* lowlevel HW functions */
481     struct ata_dma              *dma;           /* DMA data / functions */
482     int                         flags;          /* channel flags */
483 #define         ATA_NO_SLAVE            0x01
484 #define         ATA_USE_16BIT           0x02
485 #define         ATA_ATAPI_DMA_RO        0x04
486 #define         ATA_NO_48BIT_DMA        0x08
487 #define         ATA_ALWAYS_DMASTAT      0x10
488 
489     int                         devices;        /* what is present */
490 #define         ATA_ATA_MASTER          0x01
491 #define         ATA_ATA_SLAVE           0x02
492 #define         ATA_ATAPI_MASTER        0x04
493 #define         ATA_ATAPI_SLAVE         0x08
494 #define         ATA_PORTMULTIPLIER      0x10
495 
496     struct mtx                  state_mtx;      /* state lock */
497     int                         state;          /* ATA channel state */
498 #define         ATA_IDLE                0x0000
499 #define         ATA_ACTIVE              0x0001
500 #define         ATA_STALL_QUEUE         0x0002
501 
502     struct mtx                  queue_mtx;      /* queue lock */
503     TAILQ_HEAD(, ata_request)   ata_queue;      /* head of ATA queue */
504     struct ata_request          *freezepoint;   /* composite freezepoint */
505     struct ata_request          *running;       /* currently running request */
506 };
507 
508 /* disk bay/enclosure related */
509 #define         ATA_LED_OFF             0x00
510 #define         ATA_LED_RED             0x01
511 #define         ATA_LED_GREEN           0x02
512 #define         ATA_LED_ORANGE          0x03
513 #define         ATA_LED_MASK            0x03
514 
515 /* externs */
516 extern int (*ata_raid_ioctl_func)(u_long cmd, caddr_t data);
517 extern struct intr_config_hook *ata_delayed_attach;
518 extern devclass_t ata_devclass;
519 extern int ata_wc;
520 
521 /* public prototypes */
522 /* ata-all.c: */
523 int ata_probe(device_t dev);
524 int ata_attach(device_t dev);
525 int ata_detach(device_t dev);
526 int ata_reinit(device_t dev);
527 int ata_suspend(device_t dev);
528 int ata_resume(device_t dev);
529 int ata_interrupt(void *data);
530 int ata_device_ioctl(device_t dev, u_long cmd, caddr_t data);
531 int ata_identify(device_t dev);
532 void ata_default_registers(device_t dev);
533 void ata_modify_if_48bit(struct ata_request *request);
534 void ata_udelay(int interval);
535 char *ata_mode2str(int mode);
536 int ata_pmode(struct ata_params *ap);
537 int ata_wmode(struct ata_params *ap);
538 int ata_umode(struct ata_params *ap);
539 int ata_limit_mode(device_t dev, int mode, int maxmode);
540 
541 /* ata-queue.c: */
542 int ata_controlcmd(device_t dev, u_int8_t command, u_int16_t feature, u_int64_t lba, u_int16_t count);
543 int ata_atapicmd(device_t dev, u_int8_t *ccb, caddr_t data, int count, int flags, int timeout);
544 void ata_queue_request(struct ata_request *request);
545 void ata_start(device_t dev);
546 void ata_finish(struct ata_request *request);
547 void ata_timeout(struct ata_request *);
548 void ata_catch_inflight(device_t dev);
549 void ata_fail_requests(device_t dev);
550 char *ata_cmd2str(struct ata_request *request);
551 
552 /* ata-lowlevel.c: */
553 void ata_generic_hw(device_t dev);
554 int ata_begin_transaction(struct ata_request *);
555 int ata_end_transaction(struct ata_request *);
556 void ata_generic_reset(device_t dev);
557 int ata_generic_command(struct ata_request *request);
558 
559 /* macros for alloc/free of struct ata_request */
560 extern uma_zone_t ata_request_zone;
561 #define ata_alloc_request() uma_zalloc(ata_request_zone, M_NOWAIT | M_ZERO)
562 #define ata_free_request(request) { \
563 	if (!(request->flags & ATA_R_DANGER2)) \
564 	    uma_zfree(ata_request_zone, request); \
565 	}
566 /* macros for alloc/free of struct ata_composite */
567 extern uma_zone_t ata_composite_zone;
568 #define ata_alloc_composite() uma_zalloc(ata_composite_zone, M_NOWAIT | M_ZERO)
569 #define ata_free_composite(composite) uma_zfree(ata_composite_zone, composite)
570 
571 MALLOC_DECLARE(M_ATA);
572 
573 /* misc newbus defines */
574 #define GRANDPARENT(dev)        device_get_parent(device_get_parent(dev))
575 
576 /* macros to hide busspace uglyness */
577 #define ATA_INB(res, offset) \
578 	bus_space_read_1(rman_get_bustag((res)), \
579 			 rman_get_bushandle((res)), (offset))
580 
581 #define ATA_INW(res, offset) \
582 	bus_space_read_2(rman_get_bustag((res)), \
583 			 rman_get_bushandle((res)), (offset))
584 #define ATA_INL(res, offset) \
585 	bus_space_read_4(rman_get_bustag((res)), \
586 			 rman_get_bushandle((res)), (offset))
587 #define ATA_INSW(res, offset, addr, count) \
588 	bus_space_read_multi_2(rman_get_bustag((res)), \
589 			       rman_get_bushandle((res)), \
590 			       (offset), (addr), (count))
591 #define ATA_INSW_STRM(res, offset, addr, count) \
592 	bus_space_read_multi_stream_2(rman_get_bustag((res)), \
593 				      rman_get_bushandle((res)), \
594 				      (offset), (addr), (count))
595 #define ATA_INSL(res, offset, addr, count) \
596 	bus_space_read_multi_4(rman_get_bustag((res)), \
597 			       rman_get_bushandle((res)), \
598 			       (offset), (addr), (count))
599 #define ATA_INSL_STRM(res, offset, addr, count) \
600 	bus_space_read_multi_stream_4(rman_get_bustag((res)), \
601 				      rman_get_bushandle((res)), \
602 				      (offset), (addr), (count))
603 #define ATA_OUTB(res, offset, value) \
604 	bus_space_write_1(rman_get_bustag((res)), \
605 			  rman_get_bushandle((res)), (offset), (value))
606 #define ATA_OUTW(res, offset, value) \
607 	bus_space_write_2(rman_get_bustag((res)), \
608 			  rman_get_bushandle((res)), (offset), (value))
609 #define ATA_OUTL(res, offset, value) \
610 	bus_space_write_4(rman_get_bustag((res)), \
611 			  rman_get_bushandle((res)), (offset), (value))
612 #define ATA_OUTSW(res, offset, addr, count) \
613 	bus_space_write_multi_2(rman_get_bustag((res)), \
614 				rman_get_bushandle((res)), \
615 				(offset), (addr), (count))
616 #define ATA_OUTSW_STRM(res, offset, addr, count) \
617 	bus_space_write_multi_stream_2(rman_get_bustag((res)), \
618 				       rman_get_bushandle((res)), \
619 				       (offset), (addr), (count))
620 #define ATA_OUTSL(res, offset, addr, count) \
621 	bus_space_write_multi_4(rman_get_bustag((res)), \
622 				rman_get_bushandle((res)), \
623 				(offset), (addr), (count))
624 #define ATA_OUTSL_STRM(res, offset, addr, count) \
625 	bus_space_write_multi_stream_4(rman_get_bustag((res)), \
626 				       rman_get_bushandle((res)), \
627 				       (offset), (addr), (count))
628 
629 #define ATA_IDX_INB(ch, idx) \
630 	ATA_INB(ch->r_io[idx].res, ch->r_io[idx].offset)
631 
632 #define ATA_IDX_INW(ch, idx) \
633 	ATA_INW(ch->r_io[idx].res, ch->r_io[idx].offset)
634 
635 #define ATA_IDX_INL(ch, idx) \
636 	ATA_INL(ch->r_io[idx].res, ch->r_io[idx].offset)
637 
638 #define ATA_IDX_INSW(ch, idx, addr, count) \
639 	ATA_INSW(ch->r_io[idx].res, ch->r_io[idx].offset, addr, count)
640 
641 #define ATA_IDX_INSW_STRM(ch, idx, addr, count) \
642 	ATA_INSW_STRM(ch->r_io[idx].res, ch->r_io[idx].offset, addr, count)
643 
644 #define ATA_IDX_INSL(ch, idx, addr, count) \
645 	ATA_INSL(ch->r_io[idx].res, ch->r_io[idx].offset, addr, count)
646 
647 #define ATA_IDX_INSL_STRM(ch, idx, addr, count) \
648 	ATA_INSL_STRM(ch->r_io[idx].res, ch->r_io[idx].offset, addr, count)
649 
650 #define ATA_IDX_OUTB(ch, idx, value) \
651 	ATA_OUTB(ch->r_io[idx].res, ch->r_io[idx].offset, value)
652 
653 #define ATA_IDX_OUTW(ch, idx, value) \
654 	ATA_OUTW(ch->r_io[idx].res, ch->r_io[idx].offset, value)
655 
656 #define ATA_IDX_OUTL(ch, idx, value) \
657 	ATA_OUTL(ch->r_io[idx].res, ch->r_io[idx].offset, value)
658 
659 #define ATA_IDX_OUTSW(ch, idx, addr, count) \
660 	ATA_OUTSW(ch->r_io[idx].res, ch->r_io[idx].offset, addr, count)
661 
662 #define ATA_IDX_OUTSW_STRM(ch, idx, addr, count) \
663 	ATA_OUTSW_STRM(ch->r_io[idx].res, ch->r_io[idx].offset, addr, count)
664 
665 #define ATA_IDX_OUTSL(ch, idx, addr, count) \
666 	ATA_OUTSL(ch->r_io[idx].res, ch->r_io[idx].offset, addr, count)
667 
668 #define ATA_IDX_OUTSL_STRM(ch, idx, addr, count) \
669 	ATA_OUTSL_STRM(ch->r_io[idx].res, ch->r_io[idx].offset, addr, count)
670