1 /*- 2 * Copyright (c) 1998 - 2003 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 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * $FreeBSD$ 29 */ 30 31 /* ATA register defines */ 32 #define ATA_DATA 0x00 /* data register */ 33 #define ATA_ERROR 0x01 /* (R) error register */ 34 #define ATA_E_NM 0x02 /* no media */ 35 #define ATA_E_ABORT 0x04 /* command aborted */ 36 #define ATA_E_MCR 0x08 /* media change request */ 37 #define ATA_E_IDNF 0x10 /* ID not found */ 38 #define ATA_E_MC 0x20 /* media changed */ 39 #define ATA_E_UNC 0x40 /* uncorrectable data */ 40 #define ATA_E_ICRC 0x80 /* UDMA crc error */ 41 42 #define ATA_FEATURE 0x01 /* (W) feature register */ 43 #define ATA_F_DMA 0x01 /* enable DMA */ 44 #define ATA_F_OVL 0x02 /* enable overlap */ 45 46 #define ATA_COUNT 0x02 /* (W) sector count */ 47 #define ATA_IREASON 0x02 /* (R) interrupt reason */ 48 #define ATA_I_CMD 0x01 /* cmd (1) | data (0) */ 49 #define ATA_I_IN 0x02 /* read (1) | write (0) */ 50 #define ATA_I_RELEASE 0x04 /* released bus (1) */ 51 #define ATA_I_TAGMASK 0xf8 /* tag mask */ 52 53 #define ATA_SECTOR 0x03 /* sector # */ 54 #define ATA_CYL_LSB 0x04 /* cylinder# LSB */ 55 #define ATA_CYL_MSB 0x05 /* cylinder# MSB */ 56 #define ATA_DRIVE 0x06 /* Sector/Drive/Head register */ 57 #define ATA_D_LBA 0x40 /* use LBA addressing */ 58 #define ATA_D_IBM 0xa0 /* 512 byte sectors, ECC */ 59 60 #define ATA_CMD 0x07 /* command register */ 61 #define ATA_C_NOP 0x00 /* NOP command */ 62 #define ATA_C_F_FLUSHQUEUE 0x00 /* flush queued cmd's */ 63 #define ATA_C_F_AUTOPOLL 0x01 /* start autopoll function */ 64 #define ATA_C_ATAPI_RESET 0x08 /* reset ATAPI device */ 65 #define ATA_C_READ 0x20 /* read command */ 66 #define ATA_C_READ48 0x24 /* read command */ 67 #define ATA_C_READ_DMA48 0x25 /* read w/DMA command */ 68 #define ATA_C_READ_DMA_QUEUED48 0x26 /* read w/DMA QUEUED command */ 69 #define ATA_C_READ_MUL48 0x29 /* read multi command */ 70 #define ATA_C_WRITE 0x30 /* write command */ 71 #define ATA_C_WRITE48 0x34 /* write command */ 72 #define ATA_C_WRITE_DMA48 0x35 /* write w/DMA command */ 73 #define ATA_C_WRITE_DMA_QUEUED48 0x36 /* write w/DMA QUEUED command */ 74 #define ATA_C_WRITE_MUL48 0x39 /* write multi command */ 75 #define ATA_C_PACKET_CMD 0xa0 /* packet command */ 76 #define ATA_C_ATAPI_IDENTIFY 0xa1 /* get ATAPI params*/ 77 #define ATA_C_SERVICE 0xa2 /* service command */ 78 #define ATA_C_READ_MUL 0xc4 /* read multi command */ 79 #define ATA_C_WRITE_MUL 0xc5 /* write multi command */ 80 #define ATA_C_SET_MULTI 0xc6 /* set multi size command */ 81 #define ATA_C_READ_DMA_QUEUED 0xc7 /* read w/DMA QUEUED command */ 82 #define ATA_C_READ_DMA 0xc8 /* read w/DMA command */ 83 #define ATA_C_WRITE_DMA 0xca /* write w/DMA command */ 84 #define ATA_C_WRITE_DMA_QUEUED 0xcc /* write w/DMA QUEUED command */ 85 #define ATA_C_SLEEP 0xe6 /* sleep command */ 86 #define ATA_C_FLUSHCACHE 0xe7 /* flush cache to disk */ 87 #define ATA_C_FLUSHCACHE48 0xea /* flush cache to disk */ 88 #define ATA_C_ATA_IDENTIFY 0xec /* get ATA params */ 89 #define ATA_C_SETFEATURES 0xef /* features command */ 90 #define ATA_C_F_SETXFER 0x03 /* set transfer mode */ 91 #define ATA_C_F_ENAB_WCACHE 0x02 /* enable write cache */ 92 #define ATA_C_F_DIS_WCACHE 0x82 /* disable write cache */ 93 #define ATA_C_F_ENAB_RCACHE 0xaa /* enable readahead cache */ 94 #define ATA_C_F_DIS_RCACHE 0x55 /* disable readahead cache */ 95 #define ATA_C_F_ENAB_RELIRQ 0x5d /* enable release interrupt */ 96 #define ATA_C_F_DIS_RELIRQ 0xdd /* disable release interrupt */ 97 #define ATA_C_F_ENAB_SRVIRQ 0x5e /* enable service interrupt */ 98 #define ATA_C_F_DIS_SRVIRQ 0xde /* disable service interrupt */ 99 100 #define ATA_STATUS 0x07 /* status register */ 101 #define ATA_S_ERROR 0x01 /* error */ 102 #define ATA_S_INDEX 0x02 /* index */ 103 #define ATA_S_CORR 0x04 /* data corrected */ 104 #define ATA_S_DRQ 0x08 /* data request */ 105 #define ATA_S_DSC 0x10 /* drive seek completed */ 106 #define ATA_S_SERVICE 0x10 /* drive needs service */ 107 #define ATA_S_DWF 0x20 /* drive write fault */ 108 #define ATA_S_DMA 0x20 /* DMA ready */ 109 #define ATA_S_READY 0x40 /* drive ready */ 110 #define ATA_S_BUSY 0x80 /* busy */ 111 112 #define ATA_ALTSTAT 0x08 /* alternate status register */ 113 #define ATA_ALTOFFSET 0x206 /* alternate registers offset */ 114 #define ATA_PCCARD_ALTOFFSET 0x0e /* do for PCCARD devices */ 115 #define ATA_PC98_ALTOFFSET 0x10c /* do for PC98 devices */ 116 #define ATA_A_IDS 0x02 /* disable interrupts */ 117 #define ATA_A_RESET 0x04 /* RESET controller */ 118 #define ATA_A_4BIT 0x08 /* 4 head bits */ 119 120 /* misc defines */ 121 #define ATA_PRIMARY 0x1f0 122 #define ATA_SECONDARY 0x170 123 #define ATA_PC98_BANK 0x432 124 #define ATA_IOSIZE 0x08 125 #define ATA_PC98_IOSIZE 0x10 126 #define ATA_ALTIOSIZE 0x01 127 #define ATA_BMIOSIZE 0x08 128 #define ATA_PC98_BANKIOSIZE 0x01 129 #define ATA_OP_FINISHED 0x00 130 #define ATA_OP_CONTINUES 0x01 131 #define ATA_IOADDR_RID 0 132 #define ATA_ALTADDR_RID 1 133 #define ATA_BMADDR_RID 0x20 134 #define ATA_PC98_ALTADDR_RID 8 135 #define ATA_PC98_BANKADDR_RID 9 136 137 #define ATA_IRQ_RID 0 138 #define ATA_DEV(device) ((device == ATA_MASTER) ? 0 : 1) 139 140 /* busmaster DMA related defines */ 141 #define ATA_DMA_ENTRIES 256 142 #define ATA_DMA_EOT 0x80000000 143 144 #define ATA_BMCMD_PORT 0x09 145 #define ATA_BMCMD_START_STOP 0x01 146 #define ATA_BMCMD_WRITE_READ 0x08 147 148 #define ATA_BMDEVSPEC_0 0x0a 149 #define ATA_BMSTAT_PORT 0x0b 150 #define ATA_BMSTAT_ACTIVE 0x01 151 #define ATA_BMSTAT_ERROR 0x02 152 #define ATA_BMSTAT_INTERRUPT 0x04 153 #define ATA_BMSTAT_MASK 0x07 154 #define ATA_BMSTAT_DMA_MASTER 0x20 155 #define ATA_BMSTAT_DMA_SLAVE 0x40 156 #define ATA_BMSTAT_DMA_SIMPLEX 0x80 157 158 #define ATA_BMDEVSPEC_1 0x0c 159 #define ATA_BMDTP_PORT 0x0d 160 161 #define ATA_MAX_RES 0x10 162 163 /* structure for holding DMA address data */ 164 struct ata_dmaentry { 165 u_int32_t base; 166 u_int32_t count; 167 }; 168 169 /* structure describing an ATA/ATAPI device */ 170 struct ata_device { 171 struct ata_channel *channel; 172 int unit; /* unit number */ 173 #define ATA_MASTER 0x00 174 #define ATA_SLAVE 0x10 175 176 char *name; /* device name */ 177 struct ata_params *param; /* ata param structure */ 178 void *driver; /* ptr to driver for device */ 179 int flags; 180 #define ATA_D_USE_CHS 0x0001 181 #define ATA_D_DETACHING 0x0002 182 #define ATA_D_MEDIA_CHANGED 0x0004 183 #define ATA_D_ENC_PRESENT 0x0008 184 185 int cmd; /* last cmd executed */ 186 void *result; /* misc data */ 187 int mode; /* transfermode */ 188 void (*setmode)(struct ata_device *, int); 189 }; 190 191 /* structure holding DMA related information */ 192 struct ata_dma_data { 193 bus_dma_tag_t dmatag; /* parent DMA tag */ 194 bus_dma_tag_t cdmatag; /* control DMA tag */ 195 bus_dmamap_t cdmamap; /* control DMA map */ 196 bus_dma_tag_t ddmatag; /* data DMA tag */ 197 bus_dmamap_t ddmamap; /* data DMA map */ 198 struct ata_dmaentry *dmatab; /* DMA transfer table */ 199 bus_addr_t mdmatab; /* bus address of dmatab */ 200 u_int32_t alignment; /* DMA engine alignment */ 201 int flags; 202 #define ATA_DMA_ACTIVE 0x01 /* DMA transfer in progress */ 203 #define ATA_DMA_READ 0x02 /* transaction is a read */ 204 205 int (*alloc)(struct ata_channel *); 206 void (*free)(struct ata_channel *); 207 int (*setup)(struct ata_device *, caddr_t, int32_t); 208 int (*start)(struct ata_channel *, caddr_t, int32_t, int); 209 int (*stop)(struct ata_channel *); 210 int (*status)(struct ata_channel *); 211 }; 212 213 /* structure holding resources for an ATA channel */ 214 struct ata_resource { 215 struct resource *res; 216 int offset; 217 }; 218 219 /* structure describing an ATA channel */ 220 struct ata_channel { 221 struct device *dev; /* device handle */ 222 int unit; /* channel number */ 223 struct ata_resource r_io[ATA_MAX_RES];/* I/O resources */ 224 struct resource *r_irq; /* interrupt of this channel */ 225 void *ih; /* interrupt handle */ 226 struct ata_dma_data *dma; /* DMA data / functions */ 227 u_int32_t chiptype; /* controller chip PCI id */ 228 int flags; /* channel flags */ 229 #define ATA_NO_SLAVE 0x01 230 #define ATA_USE_16BIT 0x02 231 #define ATA_USE_PC98GEOM 0x04 232 #define ATA_ATAPI_DMA_RO 0x08 233 #define ATA_QUEUED 0x10 234 #define ATA_48BIT_ACTIVE 0x20 235 236 struct ata_device device[2]; /* devices on this channel */ 237 #define MASTER 0x00 238 #define SLAVE 0x01 239 240 int devices; /* what is present */ 241 #define ATA_ATA_MASTER 0x01 242 #define ATA_ATA_SLAVE 0x02 243 #define ATA_ATAPI_MASTER 0x04 244 #define ATA_ATAPI_SLAVE 0x08 245 246 u_int8_t status; /* last controller status */ 247 u_int8_t error; /* last controller error */ 248 int active; /* ATA channel state control */ 249 #define ATA_IDLE 0x0000 250 #define ATA_IMMEDIATE 0x0001 251 #define ATA_WAIT_INTR 0x0002 252 #define ATA_WAIT_READY 0x0004 253 #define ATA_WAIT_MASK 0x0007 254 #define ATA_ACTIVE 0x0010 255 #define ATA_ACTIVE_ATA 0x0020 256 #define ATA_ACTIVE_ATAPI 0x0040 257 #define ATA_CONTROL 0x0080 258 259 void (*locking)(struct ata_channel *, int); 260 #define ATA_LF_LOCK 0x0001 261 #define ATA_LF_UNLOCK 0x0002 262 263 TAILQ_HEAD(, ad_request) ata_queue; /* head of ATA queue */ 264 TAILQ_HEAD(, atapi_request) atapi_queue; /* head of ATAPI queue */ 265 void *running; /* currently running request */ 266 }; 267 268 /* disk bay/enclosure related */ 269 #define ATA_LED_OFF 0x00 270 #define ATA_LED_RED 0x01 271 #define ATA_LED_GREEN 0x02 272 #define ATA_LED_ORANGE 0x03 273 #define ATA_LED_MASK 0x03 274 275 /* externs */ 276 extern devclass_t ata_devclass; 277 extern struct intr_config_hook *ata_delayed_attach; 278 279 /* public prototypes */ 280 int ata_probe(device_t); 281 int ata_attach(device_t); 282 int ata_detach(device_t); 283 int ata_resume(device_t); 284 void ata_start(struct ata_channel *); 285 void ata_reset(struct ata_channel *); 286 int ata_reinit(struct ata_channel *); 287 int ata_wait(struct ata_device *, u_int8_t); 288 int ata_command(struct ata_device *, u_int8_t, u_int64_t, u_int16_t, u_int16_t, int); 289 void ata_enclosure_leds(struct ata_device *, u_int8_t); 290 void ata_enclosure_print(struct ata_device *); 291 int ata_printf(struct ata_channel *, int, const char *, ...) __printflike(3, 4); 292 int ata_prtdev(struct ata_device *, const char *, ...) __printflike(2, 3); 293 void ata_set_name(struct ata_device *, char *, int); 294 void ata_free_name(struct ata_device *); 295 int ata_get_lun(u_int32_t *); 296 int ata_test_lun(u_int32_t *, int); 297 void ata_free_lun(u_int32_t *, int); 298 char *ata_mode2str(int); 299 int ata_pmode(struct ata_params *); 300 int ata_wmode(struct ata_params *); 301 int ata_umode(struct ata_params *); 302 int ata_limit_mode(struct ata_device *, int, int); 303 304 /* macros for locking a channel */ 305 #define ATA_LOCK_CH(ch, value) \ 306 atomic_cmpset_acq_int(&(ch)->active, ATA_IDLE, (value)) 307 308 #define ATA_SLEEPLOCK_CH(ch, value) \ 309 while (!atomic_cmpset_acq_int(&(ch)->active, ATA_IDLE, (value))) \ 310 tsleep((caddr_t)&(ch), PRIBIO, "atalck", 1); 311 312 #define ATA_FORCELOCK_CH(ch, value) atomic_store_rel_int(&(ch)->active, (value)) 313 314 #define ATA_UNLOCK_CH(ch) atomic_store_rel_int(&(ch)->active, ATA_IDLE) 315 316 /* macros to hide busspace uglyness */ 317 #define ATA_INB(res, offset) \ 318 bus_space_read_1(rman_get_bustag((res)), \ 319 rman_get_bushandle((res)), (offset)) 320 321 #define ATA_INW(res, offset) \ 322 bus_space_read_2(rman_get_bustag((res)), \ 323 rman_get_bushandle((res)), (offset)) 324 #define ATA_INL(res, offset) \ 325 bus_space_read_4(rman_get_bustag((res)), \ 326 rman_get_bushandle((res)), (offset)) 327 #define ATA_INSW(res, offset, addr, count) \ 328 bus_space_read_multi_2(rman_get_bustag((res)), \ 329 rman_get_bushandle((res)), \ 330 (offset), (addr), (count)) 331 #define ATA_INSW_STRM(res, offset, addr, count) \ 332 bus_space_read_multi_stream_2(rman_get_bustag((res)), \ 333 rman_get_bushandle((res)), \ 334 (offset), (addr), (count)) 335 #define ATA_INSL(res, offset, addr, count) \ 336 bus_space_read_multi_4(rman_get_bustag((res)), \ 337 rman_get_bushandle((res)), \ 338 (offset), (addr), (count)) 339 #define ATA_INSL_STRM(res, offset, addr, count) \ 340 bus_space_read_multi_stream_4(rman_get_bustag((res)), \ 341 rman_get_bushandle((res)), \ 342 (offset), (addr), (count)) 343 #define ATA_OUTB(res, offset, value) \ 344 bus_space_write_1(rman_get_bustag((res)), \ 345 rman_get_bushandle((res)), (offset), (value)) 346 #define ATA_OUTW(res, offset, value) \ 347 bus_space_write_2(rman_get_bustag((res)), \ 348 rman_get_bushandle((res)), (offset), (value)) 349 #define ATA_OUTL(res, offset, value) \ 350 bus_space_write_4(rman_get_bustag((res)), \ 351 rman_get_bushandle((res)), (offset), (value)) 352 #define ATA_OUTSW(res, offset, addr, count) \ 353 bus_space_write_multi_2(rman_get_bustag((res)), \ 354 rman_get_bushandle((res)), \ 355 (offset), (addr), (count)) 356 #define ATA_OUTSW_STRM(res, offset, addr, count) \ 357 bus_space_write_multi_stream_2(rman_get_bustag((res)), \ 358 rman_get_bushandle((res)), \ 359 (offset), (addr), (count)) 360 #define ATA_OUTSL(res, offset, addr, count) \ 361 bus_space_write_multi_4(rman_get_bustag((res)), \ 362 rman_get_bushandle((res)), \ 363 (offset), (addr), (count)) 364 #define ATA_OUTSL_STRM(res, offset, addr, count) \ 365 bus_space_write_multi_stream_4(rman_get_bustag((res)), \ 366 rman_get_bushandle((res)), \ 367 (offset), (addr), (count)) 368 #define ATA_IDX_INB(ch, idx) \ 369 ATA_INB(ch->r_io[idx].res, ch->r_io[idx].offset) 370 #define ATA_IDX_INW(ch, idx) \ 371 ATA_INW(ch->r_io[idx].res, ch->r_io[idx].offset) 372 #define ATA_IDX_INL(ch, idx) \ 373 ATA_INL(ch->r_io[idx].res, ch->r_io[idx].offset) 374 #define ATA_IDX_INSW(ch, idx, addr, count) \ 375 ATA_INSW(ch->r_io[idx].res, ch->r_io[idx].offset, addr, count) 376 #define ATA_IDX_INSW_STRM(ch, idx, addr, count) \ 377 ATA_INSW_STRM(ch->r_io[idx].res, ch->r_io[idx].offset, addr, count) 378 #define ATA_IDX_INSL(ch, idx, addr, count) \ 379 ATA_INSL(ch->r_io[idx].res, ch->r_io[idx].offset, addr, count) 380 #define ATA_IDX_INSL_STRM(ch, idx, addr, count) \ 381 ATA_INSL_STRM(ch->r_io[idx].res, ch->r_io[idx].offset, addr, count) 382 #define ATA_IDX_OUTB(ch, idx, value) \ 383 ATA_OUTB(ch->r_io[idx].res, ch->r_io[idx].offset, value) 384 #define ATA_IDX_OUTW(ch, idx, value) \ 385 ATA_OUTW(ch->r_io[idx].res, ch->r_io[idx].offset, value) 386 #define ATA_IDX_OUTL(ch, idx, value) \ 387 ATA_OUTL(ch->r_io[idx].res, ch->r_io[idx].offset, value) 388 #define ATA_IDX_OUTSW(ch, idx, addr, count) \ 389 ATA_OUTSW(ch->r_io[idx].res, ch->r_io[idx].offset, addr, count) 390 #define ATA_IDX_OUTSW_STRM(ch, idx, addr, count) \ 391 ATA_OUTSW_STRM(ch->r_io[idx].res, ch->r_io[idx].offset, addr, count) 392 #define ATA_IDX_OUTSL(ch, idx, addr, count) \ 393 ATA_OUTSL(ch->r_io[idx].res, ch->r_io[idx].offset, addr, count) 394 #define ATA_IDX_OUTSL_STRM(ch, idx, addr, count) \ 395 ATA_OUTSL_STRM(ch->r_io[idx].res, ch->r_io[idx].offset, addr, count) 396