1 /*- 2 * Copyright (c) 1998 - 2004 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 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include "opt_ata.h" 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/kernel.h> 36 #include <sys/module.h> 37 #include <sys/bus.h> 38 #include <sys/malloc.h> 39 #include <sys/sema.h> 40 #include <sys/taskqueue.h> 41 #include <vm/uma.h> 42 #include <machine/stdarg.h> 43 #include <machine/resource.h> 44 #include <machine/bus.h> 45 #ifdef __alpha__ 46 #include <machine/md_var.h> 47 #endif 48 #include <sys/rman.h> 49 #include <dev/pci/pcivar.h> 50 #include <dev/pci/pcireg.h> 51 #include <dev/ata/ata-all.h> 52 #include <dev/ata/ata-pci.h> 53 54 /* local vars */ 55 static MALLOC_DEFINE(M_ATAPCI, "ATA PCI", "ATA driver PCI"); 56 57 /* misc defines */ 58 #define IOMASK 0xfffffffc 59 60 /* prototypes */ 61 static int ata_pci_allocate(device_t, struct ata_channel *); 62 static void ata_pci_dmainit(struct ata_channel *); 63 static void ata_pci_locknoop(struct ata_channel *, int); 64 65 static int 66 ata_pci_probe(device_t dev) 67 { 68 if (pci_get_class(dev) != PCIC_STORAGE) 69 return ENXIO; 70 71 switch (pci_get_vendor(dev)) { 72 case ATA_ACARD_ID: 73 if (!ata_acard_ident(dev)) 74 return 0; 75 break; 76 case ATA_ACER_LABS_ID: 77 if (!ata_ali_ident(dev)) 78 return 0; 79 break; 80 case ATA_AMD_ID: 81 if (!ata_amd_ident(dev)) 82 return 0; 83 break; 84 case ATA_CYRIX_ID: 85 if (!ata_cyrix_ident(dev)) 86 return 0; 87 break; 88 case ATA_CYPRESS_ID: 89 if (!ata_cypress_ident(dev)) 90 return 0; 91 break; 92 case ATA_HIGHPOINT_ID: 93 if (!ata_highpoint_ident(dev)) 94 return 0; 95 break; 96 case ATA_INTEL_ID: 97 if (!ata_intel_ident(dev)) 98 return 0; 99 break; 100 case ATA_NATIONAL_ID: 101 if (!ata_national_ident(dev)) 102 return 0; 103 break; 104 case ATA_NVIDIA_ID: 105 if (!ata_nvidia_ident(dev)) 106 return 0; 107 break; 108 case ATA_PROMISE_ID: 109 if (!ata_promise_ident(dev)) 110 return 0; 111 break; 112 case ATA_SERVERWORKS_ID: 113 if (!ata_serverworks_ident(dev)) 114 return 0; 115 break; 116 case ATA_SILICON_IMAGE_ID: 117 if (!ata_sii_ident(dev)) 118 return 0; 119 break; 120 case ATA_SIS_ID: 121 if (!ata_sis_ident(dev)) 122 return 0; 123 break; 124 case ATA_VIA_ID: 125 if (!ata_via_ident(dev)) 126 return 0; 127 break; 128 case 0x16ca: 129 if (pci_get_devid(dev) == 0x000116ca) { 130 ata_generic_ident(dev); 131 device_set_desc(dev, "Cenatek Rocket Drive controller"); 132 return 0; 133 } 134 break; 135 case 0x1042: 136 if (pci_get_devid(dev)==0x10001042 || pci_get_devid(dev)==0x10011042) { 137 ata_generic_ident(dev); 138 device_set_desc(dev, 139 "RZ 100? ATA controller !WARNING! buggy HW data loss possible"); 140 return 0; 141 } 142 break; 143 } 144 145 /* unknown chipset, try generic DMA if it seems possible */ 146 if ((pci_get_class(dev) == PCIC_STORAGE) && 147 (pci_get_subclass(dev) == PCIS_STORAGE_IDE)) 148 return ata_generic_ident(dev); 149 150 return ENXIO; 151 } 152 153 static int 154 ata_pci_attach(device_t dev) 155 { 156 struct ata_pci_controller *ctlr = device_get_softc(dev); 157 u_int8_t class, subclass; 158 u_int32_t type, cmd; 159 int unit; 160 161 /* set up vendor-specific stuff */ 162 type = pci_get_devid(dev); 163 class = pci_get_class(dev); 164 subclass = pci_get_subclass(dev); 165 cmd = pci_read_config(dev, PCIR_COMMAND, 2); 166 167 if (!(cmd & PCIM_CMD_PORTEN)) { 168 device_printf(dev, "ATA channel disabled by BIOS\n"); 169 return 0; 170 } 171 172 /* do chipset specific setups only needed once */ 173 if (ATA_MASTERDEV(dev) || pci_read_config(dev, 0x18, 4) & IOMASK) 174 ctlr->channels = 2; 175 else 176 ctlr->channels = 1; 177 ctlr->allocate = ata_pci_allocate; 178 ctlr->dmainit = ata_pci_dmainit; 179 ctlr->locking = ata_pci_locknoop; 180 181 #ifdef __sparc64__ 182 if (!(cmd & PCIM_CMD_BUSMASTEREN)) { 183 pci_write_config(dev, PCIR_COMMAND, cmd | PCIM_CMD_BUSMASTEREN, 2); 184 cmd = pci_read_config(dev, PCIR_COMMAND, 2); 185 } 186 #endif 187 /* if busmastering configured get the I/O resource */ 188 if ((cmd & PCIM_CMD_BUSMASTEREN) == PCIM_CMD_BUSMASTEREN) { 189 int rid = ATA_BMADDR_RID; 190 191 ctlr->r_io1 = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 192 0, ~0, 1, RF_ACTIVE); 193 } 194 195 ctlr->chipinit(dev); 196 197 /* attach all channels on this controller */ 198 for (unit = 0; unit < ctlr->channels; unit++) 199 device_add_child(dev, "ata", ATA_MASTERDEV(dev) ? 200 unit : devclass_find_free_unit(ata_devclass, 2)); 201 202 return bus_generic_attach(dev); 203 } 204 205 206 static int 207 ata_pci_print_child(device_t dev, device_t child) 208 { 209 struct ata_channel *ch = device_get_softc(child); 210 int retval = 0; 211 212 retval += bus_print_child_header(dev, child); 213 retval += printf(": at 0x%lx", rman_get_start(ch->r_io[ATA_IDX_ADDR].res)); 214 215 if (ATA_MASTERDEV(dev)) 216 retval += printf(" irq %d", 14 + ch->unit); 217 218 retval += bus_print_child_footer(dev, child); 219 220 return retval; 221 } 222 223 static struct resource * 224 ata_pci_alloc_resource(device_t dev, device_t child, int type, int *rid, 225 u_long start, u_long end, u_long count, u_int flags) 226 { 227 struct ata_pci_controller *controller = device_get_softc(dev); 228 int unit = ((struct ata_channel *)device_get_softc(child))->unit; 229 struct resource *res = NULL; 230 int myrid; 231 232 if (type == SYS_RES_IOPORT) { 233 switch (*rid) { 234 case ATA_IOADDR_RID: 235 if (ATA_MASTERDEV(dev)) { 236 myrid = 0; 237 start = (unit ? ATA_SECONDARY : ATA_PRIMARY); 238 end = start + ATA_IOSIZE - 1; 239 count = ATA_IOSIZE; 240 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child, 241 SYS_RES_IOPORT, &myrid, 242 start, end, count, flags); 243 } 244 else { 245 myrid = 0x10 + 8 * unit; 246 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, 247 SYS_RES_IOPORT, &myrid, 248 start, end, count, flags); 249 } 250 break; 251 252 case ATA_ALTADDR_RID: 253 if (ATA_MASTERDEV(dev)) { 254 myrid = 0; 255 start = (unit ? ATA_SECONDARY : ATA_PRIMARY) + ATA_ALTOFFSET; 256 end = start + ATA_ALTIOSIZE - 1; 257 count = ATA_ALTIOSIZE; 258 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child, 259 SYS_RES_IOPORT, &myrid, 260 start, end, count, flags); 261 } 262 else { 263 myrid = 0x14 + 8 * unit; 264 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, 265 SYS_RES_IOPORT, &myrid, 266 start, end, count, flags); 267 if (res) { 268 start = rman_get_start(res) + 2; 269 end = start + ATA_ALTIOSIZE - 1; 270 count = ATA_ALTIOSIZE; 271 BUS_RELEASE_RESOURCE(device_get_parent(dev), dev, 272 SYS_RES_IOPORT, myrid, res); 273 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, 274 SYS_RES_IOPORT, &myrid, 275 start, end, count, flags); 276 } 277 } 278 break; 279 } 280 return res; 281 } 282 283 if (type == SYS_RES_IRQ && *rid == ATA_IRQ_RID) { 284 if (ATA_MASTERDEV(dev)) { 285 #ifdef __alpha__ 286 return alpha_platform_alloc_ide_intr(unit); 287 #else 288 int irq = (unit == 0 ? 14 : 15); 289 290 return BUS_ALLOC_RESOURCE(device_get_parent(dev), child, 291 SYS_RES_IRQ, rid, irq, irq, 1, flags); 292 #endif 293 } 294 else { 295 return controller->r_irq; 296 } 297 } 298 return 0; 299 } 300 301 static int 302 ata_pci_release_resource(device_t dev, device_t child, int type, int rid, 303 struct resource *r) 304 { 305 int unit = ((struct ata_channel *)device_get_softc(child))->unit; 306 307 if (type == SYS_RES_IOPORT) { 308 switch (rid) { 309 case ATA_IOADDR_RID: 310 if (ATA_MASTERDEV(dev)) 311 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child, 312 SYS_RES_IOPORT, 0x0, r); 313 else 314 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev, 315 SYS_RES_IOPORT, 0x10 + 8 * unit, r); 316 break; 317 318 case ATA_ALTADDR_RID: 319 if (ATA_MASTERDEV(dev)) 320 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child, 321 SYS_RES_IOPORT, 0x0, r); 322 else 323 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev, 324 SYS_RES_IOPORT, 0x14 + 8 * unit, r); 325 break; 326 default: 327 return ENOENT; 328 } 329 } 330 if (type == SYS_RES_IRQ) { 331 if (rid != ATA_IRQ_RID) 332 return ENOENT; 333 334 if (ATA_MASTERDEV(dev)) { 335 #ifdef __alpha__ 336 return alpha_platform_release_ide_intr(unit, r); 337 #else 338 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child, 339 SYS_RES_IRQ, rid, r); 340 #endif 341 } 342 else 343 return 0; 344 } 345 return EINVAL; 346 } 347 348 static int 349 ata_pci_setup_intr(device_t dev, device_t child, struct resource *irq, 350 int flags, driver_intr_t *function, void *argument, 351 void **cookiep) 352 { 353 if (ATA_MASTERDEV(dev)) { 354 #ifdef __alpha__ 355 return alpha_platform_setup_ide_intr(child, irq, function, argument, 356 cookiep); 357 #else 358 return BUS_SETUP_INTR(device_get_parent(dev), child, irq, 359 flags, function, argument, cookiep); 360 #endif 361 } 362 else { 363 struct ata_pci_controller *controller = device_get_softc(dev); 364 int unit = ((struct ata_channel *)device_get_softc(child))->unit; 365 366 controller->interrupt[unit].function = function; 367 controller->interrupt[unit].argument = argument; 368 *cookiep = controller; 369 return 0; 370 } 371 } 372 373 static int 374 ata_pci_teardown_intr(device_t dev, device_t child, struct resource *irq, 375 void *cookie) 376 { 377 if (ATA_MASTERDEV(dev)) { 378 #ifdef __alpha__ 379 return alpha_platform_teardown_ide_intr(child, irq, cookie); 380 #else 381 return BUS_TEARDOWN_INTR(device_get_parent(dev), child, irq, cookie); 382 #endif 383 } 384 else { 385 struct ata_pci_controller *controller = device_get_softc(dev); 386 int unit = ((struct ata_channel *)device_get_softc(child))->unit; 387 388 controller->interrupt[unit].function = NULL; 389 controller->interrupt[unit].argument = NULL; 390 return 0; 391 } 392 } 393 394 static int 395 ata_pci_allocate(device_t dev, struct ata_channel *ch) 396 { 397 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 398 struct resource *io = NULL, *altio = NULL; 399 int i, rid; 400 401 rid = ATA_IOADDR_RID; 402 io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 403 0, ~0, ATA_IOSIZE, RF_ACTIVE); 404 if (!io) 405 return ENXIO; 406 407 rid = ATA_ALTADDR_RID; 408 altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 409 0, ~0, ATA_ALTIOSIZE, RF_ACTIVE); 410 if (!altio) { 411 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io); 412 return ENXIO; 413 } 414 415 for (i = ATA_DATA; i <= ATA_STATUS; i ++) { 416 ch->r_io[i].res = io; 417 ch->r_io[i].offset = i; 418 } 419 ch->r_io[ATA_ALTSTAT].res = altio; 420 ch->r_io[ATA_ALTSTAT].offset = 0; 421 ch->r_io[ATA_IDX_ADDR].res = io; 422 423 if (ctlr->r_io1) { 424 for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) { 425 ch->r_io[i].res = ctlr->r_io1; 426 ch->r_io[i].offset = (i - ATA_BMCMD_PORT)+(ch->unit * ATA_BMIOSIZE); 427 } 428 429 /* if simplex controller, only allow DMA on primary channel */ 430 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & 431 (ATA_BMSTAT_DMA_MASTER | ATA_BMSTAT_DMA_SLAVE)); 432 if (ch->unit > 0 && 433 (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_DMA_SIMPLEX)) 434 device_printf(dev, "simplex device, DMA on primary only\n"); 435 else 436 ctlr->dmainit(ch); 437 } 438 return 0; 439 } 440 441 static int 442 ata_pci_dmastart(struct ata_channel *ch) 443 { 444 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) | 445 (ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR))); 446 ATA_IDX_OUTL(ch, ATA_BMDTP_PORT, ch->dma->mdmatab); 447 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT, 448 ((ch->dma->flags & ATA_DMA_READ) ? ATA_BMCMD_WRITE_READ : 0) | 449 ATA_BMCMD_START_STOP); 450 return 0; 451 } 452 453 static int 454 ata_pci_dmastop(struct ata_channel *ch) 455 { 456 int error; 457 458 error = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK; 459 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT, 460 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP); 461 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR); 462 return error; 463 } 464 465 static void 466 ata_pci_dmainit(struct ata_channel *ch) 467 { 468 ata_dmainit(ch); 469 if (ch->dma) { 470 ch->dma->start = ata_pci_dmastart; 471 ch->dma->stop = ata_pci_dmastop; 472 } 473 } 474 475 static void 476 ata_pci_locknoop(struct ata_channel *ch, int flags) 477 { 478 } 479 480 static device_method_t ata_pci_methods[] = { 481 /* device interface */ 482 DEVMETHOD(device_probe, ata_pci_probe), 483 DEVMETHOD(device_attach, ata_pci_attach), 484 DEVMETHOD(device_shutdown, bus_generic_shutdown), 485 DEVMETHOD(device_suspend, bus_generic_suspend), 486 DEVMETHOD(device_resume, bus_generic_resume), 487 488 /* bus methods */ 489 DEVMETHOD(bus_print_child, ata_pci_print_child), 490 DEVMETHOD(bus_alloc_resource, ata_pci_alloc_resource), 491 DEVMETHOD(bus_release_resource, ata_pci_release_resource), 492 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 493 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 494 DEVMETHOD(bus_setup_intr, ata_pci_setup_intr), 495 DEVMETHOD(bus_teardown_intr, ata_pci_teardown_intr), 496 { 0, 0 } 497 }; 498 499 static driver_t ata_pci_driver = { 500 "atapci", 501 ata_pci_methods, 502 sizeof(struct ata_pci_controller), 503 }; 504 505 static devclass_t ata_pci_devclass; 506 507 DRIVER_MODULE(atapci, pci, ata_pci_driver, ata_pci_devclass, 0, 0); 508 509 static int 510 ata_pcisub_probe(device_t dev) 511 { 512 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 513 struct ata_channel *ch = device_get_softc(dev); 514 device_t *children; 515 int count, error, i; 516 517 /* find channel number on this controller */ 518 device_get_children(device_get_parent(dev), &children, &count); 519 for (i = 0; i < count; i++) { 520 if (children[i] == dev) 521 ch->unit = i; 522 } 523 free(children, M_TEMP); 524 525 if ((error = ctlr->allocate(dev, ch))) 526 return error; 527 528 ch->device[MASTER].setmode = ctlr->setmode; 529 ch->device[SLAVE].setmode = ctlr->setmode; 530 ch->locking = ctlr->locking; 531 return ata_probe(dev); 532 } 533 534 static device_method_t ata_pcisub_methods[] = { 535 /* device interface */ 536 DEVMETHOD(device_probe, ata_pcisub_probe), 537 DEVMETHOD(device_attach, ata_attach), 538 DEVMETHOD(device_detach, ata_detach), 539 DEVMETHOD(device_suspend, ata_suspend), 540 DEVMETHOD(device_resume, ata_resume), 541 { 0, 0 } 542 }; 543 544 static driver_t ata_pcisub_driver = { 545 "ata", 546 ata_pcisub_methods, 547 sizeof(struct ata_channel), 548 }; 549 550 DRIVER_MODULE(ata, atapci, ata_pcisub_driver, ata_devclass, 0, 0); 551