173897Ssos/*- 2230132Suqs * Copyright (c) 1998 - 2008 S��ren Schmidt <sos@FreeBSD.org> 373897Ssos * All rights reserved. 473897Ssos * 573897Ssos * Redistribution and use in source and binary forms, with or without 673897Ssos * modification, are permitted provided that the following conditions 773897Ssos * are met: 873897Ssos * 1. Redistributions of source code must retain the above copyright 973897Ssos * notice, this list of conditions and the following disclaimer, 1073897Ssos * without modification, immediately at the beginning of the file. 1173897Ssos * 2. Redistributions in binary form must reproduce the above copyright 1273897Ssos * notice, this list of conditions and the following disclaimer in the 1373897Ssos * documentation and/or other materials provided with the distribution. 1473897Ssos * 1573897Ssos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1673897Ssos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1773897Ssos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1873897Ssos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 1973897Ssos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2073897Ssos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2173897Ssos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2273897Ssos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2373897Ssos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2473897Ssos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2573897Ssos */ 2673897Ssos 27119418Sobrien#include <sys/cdefs.h> 28119418Sobrien__FBSDID("$FreeBSD$"); 29119418Sobrien 3073897Ssos#include <sys/param.h> 3173897Ssos#include <sys/systm.h> 3273897Ssos#include <sys/kernel.h> 3373897Ssos#include <sys/module.h> 34144330Ssos#include <sys/ata.h> 3573897Ssos#include <sys/bus.h> 36155478Ssos#include <sys/conf.h> 3773897Ssos#include <sys/malloc.h> 38124403Ssos#include <sys/sema.h> 39119404Ssos#include <sys/taskqueue.h> 40124534Ssos#include <vm/uma.h> 4173897Ssos#include <machine/stdarg.h> 4273897Ssos#include <machine/resource.h> 4373897Ssos#include <machine/bus.h> 4473897Ssos#include <sys/rman.h> 45119277Simp#include <dev/pci/pcivar.h> 46119277Simp#include <dev/pci/pcireg.h> 4773897Ssos#include <dev/ata/ata-all.h> 48111188Ssos#include <dev/ata/ata-pci.h> 49144330Ssos#include <ata_if.h> 5073897Ssos 51224270SmavMALLOC_DEFINE(M_ATAPCI, "ata_pci", "ATA driver PCI"); 5273897Ssos 5383932Ssos/* misc defines */ 54144330Ssos#define IOMASK 0xfffffffc 5583932Ssos 56183724Ssos/* 57183724Ssos * generic PCI ATA device probe 58183724Ssos */ 59144330Ssosint 60111188Ssosata_pci_probe(device_t dev) 6183932Ssos{ 62183724Ssos struct ata_pci_controller *ctlr = device_get_softc(dev); 63183724Ssos char buffer[64]; 64183724Ssos 65183724Ssos /* is this a storage class device ? */ 6673897Ssos if (pci_get_class(dev) != PCIC_STORAGE) 67191600Sjkim return (ENXIO); 6873897Ssos 69183724Ssos /* is this an IDE/ATA type device ? */ 70183724Ssos if (pci_get_subclass(dev) != PCIS_STORAGE_IDE) 71191600Sjkim return (ENXIO); 72183724Ssos 73183724Ssos sprintf(buffer, "%s ATA controller", ata_pcivendor2str(dev)); 74183724Ssos device_set_desc_copy(dev, buffer); 75183724Ssos ctlr->chipinit = ata_generic_chipinit; 76173734Ssos 77183724Ssos /* we are a low priority handler */ 78191600Sjkim return (BUS_PROBE_GENERIC); 7973897Ssos} 8073897Ssos 81144330Ssosint 8273897Ssosata_pci_attach(device_t dev) 8373897Ssos{ 84112791Ssos struct ata_pci_controller *ctlr = device_get_softc(dev); 85188615Smav device_t child; 86127019Ssos u_int32_t cmd; 87128676Ssos int unit; 8873897Ssos 89112791Ssos /* do chipset specific setups only needed once */ 90186182Smav ctlr->legacy = ata_legacy(dev); 91186182Smav if (ctlr->legacy || pci_read_config(dev, PCIR_BAR(2), 4) & IOMASK) 92112791Ssos ctlr->channels = 2; 93112791Ssos else 94112791Ssos ctlr->channels = 1; 95188694Smav ctlr->ichannels = -1; 96188765Smav ctlr->ch_attach = ata_pci_ch_attach; 97188769Smav ctlr->ch_detach = ata_pci_ch_detach; 98144330Ssos ctlr->dev = dev; 99112791Ssos 100127019Ssos /* if needed try to enable busmastering */ 101254263Sscottl pci_enable_busmaster(dev); 102127019Ssos cmd = pci_read_config(dev, PCIR_COMMAND, 2); 103127019Ssos 104127019Ssos /* if busmastering mode "stuck" use it */ 105112791Ssos if ((cmd & PCIM_CMD_BUSMASTEREN) == PCIM_CMD_BUSMASTEREN) { 106127019Ssos ctlr->r_type1 = SYS_RES_IOPORT; 107127019Ssos ctlr->r_rid1 = ATA_BMADDR_RID; 108127135Snjl ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1, &ctlr->r_rid1, 109127135Snjl RF_ACTIVE); 11073897Ssos } 11173897Ssos 112154507Ssos if (ctlr->chipinit(dev)) 113154507Ssos return ENXIO; 114115957Ssos 115112791Ssos /* attach all channels on this controller */ 116128774Ssos for (unit = 0; unit < ctlr->channels; unit++) { 117188694Smav if ((ctlr->ichannels & (1 << unit)) == 0) 118188694Smav continue; 119188615Smav child = device_add_child(dev, "ata", 120188615Smav ((unit == 0 || unit == 1) && ctlr->legacy) ? 121188615Smav unit : devclass_find_free_unit(ata_devclass, 2)); 122188615Smav if (child == NULL) 123188615Smav device_printf(dev, "failed to add ata child device\n"); 124188615Smav else 125188615Smav device_set_ivars(child, (void *)(intptr_t)unit); 126128774Ssos } 127144330Ssos bus_generic_attach(dev); 128144330Ssos return 0; 129128183Ssos} 130127019Ssos 131144330Ssosint 132127019Ssosata_pci_detach(device_t dev) 133127019Ssos{ 134127019Ssos struct ata_pci_controller *ctlr = device_get_softc(dev); 135127019Ssos 136144330Ssos /* detach & delete all children */ 137227849Shselasky device_delete_children(dev); 138227701Shselasky 139127019Ssos if (ctlr->r_irq) { 140127019Ssos bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle); 141188655Smav bus_release_resource(dev, SYS_RES_IRQ, ctlr->r_irq_rid, ctlr->r_irq); 142188655Smav if (ctlr->r_irq_rid != ATA_IRQ_RID) 143188655Smav pci_release_msi(dev); 144127019Ssos } 145224270Smav if (ctlr->chipdeinit != NULL) 146224270Smav ctlr->chipdeinit(dev); 147227000Smarius if (ctlr->r_res2) { 148227000Smarius#ifdef __sparc64__ 149227000Smarius bus_space_unmap(rman_get_bustag(ctlr->r_res2), 150227000Smarius rman_get_bushandle(ctlr->r_res2), rman_get_size(ctlr->r_res2)); 151227000Smarius#endif 152127019Ssos bus_release_resource(dev, ctlr->r_type2, ctlr->r_rid2, ctlr->r_res2); 153227000Smarius } 154227000Smarius if (ctlr->r_res1) { 155227000Smarius#ifdef __sparc64__ 156227000Smarius bus_space_unmap(rman_get_bustag(ctlr->r_res1), 157227000Smarius rman_get_bushandle(ctlr->r_res1), rman_get_size(ctlr->r_res1)); 158227000Smarius#endif 159127019Ssos bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1, ctlr->r_res1); 160227000Smarius } 161127019Ssos 162127019Ssos return 0; 16373897Ssos} 16473897Ssos 165183141Ssosint 166183141Ssosata_pci_suspend(device_t dev) 167183141Ssos{ 168183141Ssos struct ata_pci_controller *ctlr = device_get_softc(dev); 169183141Ssos int error = 0; 170183141Ssos 171183141Ssos bus_generic_suspend(dev); 172183141Ssos if (ctlr->suspend) 173183141Ssos error = ctlr->suspend(dev); 174183141Ssos return error; 175183141Ssos} 176183141Ssos 177183141Ssosint 178183141Ssosata_pci_resume(device_t dev) 179183141Ssos{ 180183141Ssos struct ata_pci_controller *ctlr = device_get_softc(dev); 181183141Ssos int error = 0; 182183141Ssos 183183141Ssos if (ctlr->resume) 184183141Ssos error = ctlr->resume(dev); 185183141Ssos bus_generic_resume(dev); 186183141Ssos return error; 187183141Ssos} 188183141Ssos 189199322Smavint 190199322Smavata_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 191199322Smav{ 192199322Smav 193199322Smav return (BUS_READ_IVAR(device_get_parent(dev), dev, which, result)); 194199322Smav} 195199322Smav 196199322Smavint 197199322Smavata_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value) 198199322Smav{ 199199322Smav 200199322Smav return (BUS_WRITE_IVAR(device_get_parent(dev), dev, which, value)); 201199322Smav} 202199322Smav 203199322Smavuint32_t 204199322Smavata_pci_read_config(device_t dev, device_t child, int reg, int width) 205199322Smav{ 206199322Smav 207199322Smav return (pci_read_config(dev, reg, width)); 208199322Smav} 209199322Smav 210199322Smavvoid 211199322Smavata_pci_write_config(device_t dev, device_t child, int reg, 212199322Smav uint32_t val, int width) 213199322Smav{ 214199322Smav 215199322Smav pci_write_config(dev, reg, val, width); 216199322Smav} 217199322Smav 218144330Ssosstruct resource * 21973897Ssosata_pci_alloc_resource(device_t dev, device_t child, int type, int *rid, 22073897Ssos u_long start, u_long end, u_long count, u_int flags) 22173897Ssos{ 222199322Smav struct ata_pci_controller *controller = device_get_softc(dev); 223199322Smav struct resource *res = NULL; 22473897Ssos 225199322Smav if (device_get_devclass(child) == ata_devclass) { 226199322Smav int unit = ((struct ata_channel *)device_get_softc(child))->unit; 227199322Smav int myrid; 22873897Ssos 229199322Smav if (type == SYS_RES_IOPORT) { 230199322Smav switch (*rid) { 231199322Smav case ATA_IOADDR_RID: 232199322Smav if (controller->legacy) { 233199322Smav start = (unit ? ATA_SECONDARY : ATA_PRIMARY); 234199322Smav count = ATA_IOSIZE; 235199322Smav end = start + count - 1; 236199322Smav } 237199322Smav myrid = PCIR_BAR(0) + (unit << 3); 238199322Smav res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, 239199322Smav SYS_RES_IOPORT, &myrid, 240199322Smav start, end, count, flags); 241199322Smav break; 242199322Smav case ATA_CTLADDR_RID: 243199322Smav if (controller->legacy) { 244199322Smav start = (unit ? ATA_SECONDARY : ATA_PRIMARY) + 245199322Smav ATA_CTLOFFSET; 246199322Smav count = ATA_CTLIOSIZE; 247199322Smav end = start + count - 1; 248199322Smav } 249199322Smav myrid = PCIR_BAR(1) + (unit << 3); 250199322Smav res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, 251199322Smav SYS_RES_IOPORT, &myrid, 252199322Smav start, end, count, flags); 253199322Smav break; 254199322Smav } 255199322Smav } 256199322Smav if (type == SYS_RES_IRQ && *rid == ATA_IRQ_RID) { 257199322Smav if (controller->legacy) { 258199322Smav int irq = (unit == 0 ? 14 : 15); 259111188Ssos 260199322Smav res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child, 261199322Smav SYS_RES_IRQ, rid, irq, irq, 1, flags); 262199322Smav } else 263199322Smav res = controller->r_irq; 264199322Smav } 265199322Smav } else { 266199322Smav if (type == SYS_RES_IRQ) { 267199322Smav if (*rid != ATA_IRQ_RID) 268199322Smav return (NULL); 269199322Smav res = controller->r_irq; 270199322Smav } else { 271199322Smav res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, 272199322Smav type, rid, start, end, count, flags); 273199322Smav } 27473897Ssos } 275199322Smav return (res); 27673897Ssos} 27773897Ssos 278144330Ssosint 27973897Ssosata_pci_release_resource(device_t dev, device_t child, int type, int rid, 28073897Ssos struct resource *r) 28173897Ssos{ 28273897Ssos 283199322Smav if (device_get_devclass(child) == ata_devclass) { 284199322Smav struct ata_pci_controller *controller = device_get_softc(dev); 285199322Smav int unit = ((struct ata_channel *)device_get_softc(child))->unit; 28673897Ssos 287199322Smav if (type == SYS_RES_IOPORT) { 288199322Smav switch (rid) { 289199322Smav case ATA_IOADDR_RID: 290199322Smav return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev, 291199322Smav SYS_RES_IOPORT, 292199322Smav PCIR_BAR(0) + (unit << 3), r); 293199322Smav case ATA_CTLADDR_RID: 294199322Smav return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev, 295199322Smav SYS_RES_IOPORT, 296199322Smav PCIR_BAR(1) + (unit << 3), r); 297199322Smav default: 298199322Smav return ENOENT; 299199322Smav } 300199322Smav } 301199322Smav if (type == SYS_RES_IRQ) { 302199322Smav if (rid != ATA_IRQ_RID) 303199322Smav return ENOENT; 304199322Smav if (controller->legacy) { 305199322Smav return BUS_RELEASE_RESOURCE(device_get_parent(dev), child, 306199322Smav SYS_RES_IRQ, rid, r); 307199322Smav } else 308199322Smav return 0; 309199322Smav } 310199322Smav } else { 311199322Smav if (type == SYS_RES_IRQ) { 312199322Smav if (rid != ATA_IRQ_RID) 313199322Smav return (ENOENT); 314199322Smav return (0); 315199322Smav } else { 316199322Smav return (BUS_RELEASE_RESOURCE(device_get_parent(dev), child, 317199322Smav type, rid, r)); 318199322Smav } 31973897Ssos } 320199322Smav return (EINVAL); 32173897Ssos} 32273897Ssos 323144330Ssosint 32473897Ssosata_pci_setup_intr(device_t dev, device_t child, struct resource *irq, 325166901Spiso int flags, driver_filter_t *filter, driver_intr_t *function, 326166901Spiso void *argument, void **cookiep) 32773897Ssos{ 328199322Smav struct ata_pci_controller *controller = device_get_softc(dev); 329186182Smav 330199322Smav if (controller->legacy) { 331199322Smav return BUS_SETUP_INTR(device_get_parent(dev), child, irq, 332166901Spiso flags, filter, function, argument, cookiep); 333199322Smav } else { 334199322Smav struct ata_pci_controller *controller = device_get_softc(dev); 335199322Smav int unit; 336111188Ssos 337199322Smav if (filter != NULL) { 338199322Smav printf("ata-pci.c: we cannot use a filter here\n"); 339199322Smav return (EINVAL); 340199322Smav } 341199322Smav if (device_get_devclass(child) == ata_devclass) 342199322Smav unit = ((struct ata_channel *)device_get_softc(child))->unit; 343199322Smav else 344199322Smav unit = ATA_PCI_MAX_CH - 1; 345199322Smav controller->interrupt[unit].function = function; 346199322Smav controller->interrupt[unit].argument = argument; 347199322Smav *cookiep = controller; 348199322Smav return 0; 349166901Spiso } 35073897Ssos} 35173897Ssos 352144330Ssosint 35373897Ssosata_pci_teardown_intr(device_t dev, device_t child, struct resource *irq, 35473897Ssos void *cookie) 35573897Ssos{ 356111485Ssos struct ata_pci_controller *controller = device_get_softc(dev); 357111485Ssos 358199322Smav if (controller->legacy) { 359199322Smav return BUS_TEARDOWN_INTR(device_get_parent(dev), child, irq, cookie); 360199322Smav } else { 361199322Smav struct ata_pci_controller *controller = device_get_softc(dev); 362199322Smav int unit; 363199322Smav 364199322Smav if (device_get_devclass(child) == ata_devclass) 365199322Smav unit = ((struct ata_channel *)device_get_softc(child))->unit; 366199322Smav else 367199322Smav unit = ATA_PCI_MAX_CH - 1; 368199322Smav controller->interrupt[unit].function = NULL; 369199322Smav controller->interrupt[unit].argument = NULL; 370199322Smav return 0; 371199322Smav } 37273897Ssos} 373112791Ssos 374200171Smavint 375200171Smavata_generic_setmode(device_t dev, int target, int mode) 376183724Ssos{ 377183724Ssos 378200171Smav return (min(mode, ATA_UDMA2)); 379183724Ssos} 380183724Ssos 381200171Smavint 382183724Ssosata_generic_chipinit(device_t dev) 383183724Ssos{ 384183724Ssos struct ata_pci_controller *ctlr = device_get_softc(dev); 385183724Ssos 386183724Ssos if (ata_setup_interrupt(dev, ata_generic_intr)) 387183724Ssos return ENXIO; 388183724Ssos ctlr->setmode = ata_generic_setmode; 389183724Ssos return 0; 390183724Ssos} 391183724Ssos 392144790Ssosint 393188765Smavata_pci_ch_attach(device_t dev) 394112791Ssos{ 395112791Ssos struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 396144667Ssos struct ata_channel *ch = device_get_softc(dev); 397144707Ssos struct resource *io = NULL, *ctlio = NULL; 398112791Ssos int i, rid; 39973897Ssos 400112791Ssos rid = ATA_IOADDR_RID; 401145499Ssos if (!(io = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE))) 402112791Ssos return ENXIO; 403112791Ssos 404144707Ssos rid = ATA_CTLADDR_RID; 405145499Ssos if (!(ctlio = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,RF_ACTIVE))){ 406112791Ssos bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io); 407112791Ssos return ENXIO; 408112791Ssos } 409112791Ssos 410188765Smav ata_pci_dmainit(dev); 411188765Smav 412144707Ssos for (i = ATA_DATA; i <= ATA_COMMAND; i ++) { 413112791Ssos ch->r_io[i].res = io; 414112791Ssos ch->r_io[i].offset = i; 415112791Ssos } 416144707Ssos ch->r_io[ATA_CONTROL].res = ctlio; 417186182Smav ch->r_io[ATA_CONTROL].offset = ctlr->legacy ? 0 : 2; 418113222Ssos ch->r_io[ATA_IDX_ADDR].res = io; 419145713Ssos ata_default_registers(dev); 420127019Ssos if (ctlr->r_res1) { 421112791Ssos for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) { 422127019Ssos ch->r_io[i].res = ctlr->r_res1; 423144707Ssos ch->r_io[i].offset = (i - ATA_BMCMD_PORT) + (ch->unit*ATA_BMIOSIZE); 424112791Ssos } 425112791Ssos } 426128183Ssos 427154507Ssos ata_pci_hw(dev); 428112791Ssos return 0; 429112791Ssos} 430112791Ssos 431178156Ssosint 432188769Smavata_pci_ch_detach(device_t dev) 433188769Smav{ 434188769Smav struct ata_channel *ch = device_get_softc(dev); 435188769Smav 436188769Smav ata_pci_dmafini(dev); 437188769Smav 438188769Smav bus_release_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID, 439188769Smav ch->r_io[ATA_CONTROL].res); 440188769Smav bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, 441188769Smav ch->r_io[ATA_IDX_ADDR].res); 442188769Smav 443188769Smav return (0); 444188769Smav} 445188769Smav 446188769Smavint 447154507Ssosata_pci_status(device_t dev) 448154507Ssos{ 449186182Smav struct ata_pci_controller *controller = 450186182Smav device_get_softc(device_get_parent(dev)); 451154507Ssos struct ata_channel *ch = device_get_softc(dev); 452154507Ssos 453186182Smav if ((dumping || !controller->legacy) && 454178067Ssos ((ch->flags & ATA_ALWAYS_DMASTAT) || 455178067Ssos (ch->dma.flags & ATA_DMA_ACTIVE))) { 456154507Ssos int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK; 457154507Ssos 458200121Smav if ((bmstat & ATA_BMSTAT_INTERRUPT) == 0) 459154507Ssos return 0; 460154507Ssos ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR); 461154507Ssos DELAY(1); 462154507Ssos } 463154507Ssos if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) { 464154515Ssos DELAY(100); 465154515Ssos if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) 466154515Ssos return 0; 467154507Ssos } 468154507Ssos return 1; 469154507Ssos} 470154507Ssos 471178067Ssosvoid 472178067Ssosata_pci_hw(device_t dev) 473178067Ssos{ 474178067Ssos struct ata_channel *ch = device_get_softc(dev); 475178067Ssos 476178067Ssos ata_generic_hw(dev); 477178067Ssos ch->hw.status = ata_pci_status; 478178067Ssos} 479178067Ssos 480112791Ssosstatic int 481178067Ssosata_pci_dmastart(struct ata_request *request) 482112791Ssos{ 483178067Ssos struct ata_channel *ch = device_get_softc(request->parent); 484145713Ssos 485178067Ssos ATA_DEBUG_RQ(request, "dmastart"); 486178067Ssos 487112791Ssos ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) | 488113222Ssos (ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR))); 489178278Ssos ATA_IDX_OUTL(ch, ATA_BMDTP_PORT, request->dma->sg_bus); 490178067Ssos ch->dma.flags |= ATA_DMA_ACTIVE; 491119404Ssos ATA_IDX_OUTB(ch, ATA_BMCMD_PORT, 492129100Ssos (ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_WRITE_READ) | 493178067Ssos ((request->flags & ATA_R_READ) ? ATA_BMCMD_WRITE_READ : 0)| 494121310Ssos ATA_BMCMD_START_STOP); 495112791Ssos return 0; 496112791Ssos} 497112791Ssos 498112791Ssosstatic int 499178067Ssosata_pci_dmastop(struct ata_request *request) 500112791Ssos{ 501178067Ssos struct ata_channel *ch = device_get_softc(request->parent); 502112791Ssos int error; 503112791Ssos 504178067Ssos ATA_DEBUG_RQ(request, "dmastop"); 505178067Ssos 506112791Ssos ATA_IDX_OUTB(ch, ATA_BMCMD_PORT, 507113222Ssos ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP); 508178067Ssos ch->dma.flags &= ~ATA_DMA_ACTIVE; 509135818Ssos error = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK; 510112791Ssos ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR); 511119404Ssos return error; 512112791Ssos} 513112791Ssos 514119453Ssosstatic void 515145818Ssosata_pci_dmareset(device_t dev) 516145818Ssos{ 517145818Ssos struct ata_channel *ch = device_get_softc(dev); 518178067Ssos struct ata_request *request; 519145818Ssos 520145818Ssos ATA_IDX_OUTB(ch, ATA_BMCMD_PORT, 521145818Ssos ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP); 522178067Ssos ch->dma.flags &= ~ATA_DMA_ACTIVE; 523145818Ssos ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR); 524178067Ssos if ((request = ch->running)) { 525198717Smav device_printf(dev, "DMA reset calling unload\n"); 526178067Ssos ch->dma.unload(request); 527178067Ssos } 528145818Ssos} 529145818Ssos 530155761Ssosvoid 531145713Ssosata_pci_dmainit(device_t dev) 532112791Ssos{ 533145713Ssos struct ata_channel *ch = device_get_softc(dev); 534145713Ssos 535145713Ssos ata_dmainit(dev); 536178067Ssos ch->dma.start = ata_pci_dmastart; 537178067Ssos ch->dma.stop = ata_pci_dmastop; 538178067Ssos ch->dma.reset = ata_pci_dmareset; 539112791Ssos} 540112791Ssos 541188769Smavvoid 542188769Smavata_pci_dmafini(device_t dev) 543188769Smav{ 544173734Ssos 545188769Smav ata_dmafini(dev); 546188769Smav} 547188769Smav 548208410Smavint 549226680Smavata_pci_print_child(device_t dev, device_t child) 550226680Smav{ 551226680Smav int retval; 552226680Smav 553226680Smav retval = bus_print_child_header(dev, child); 554226680Smav retval += printf(" at channel %d", 555226680Smav (int)(intptr_t)device_get_ivars(child)); 556226680Smav retval += bus_print_child_footer(dev, child); 557226680Smav 558226680Smav return (retval); 559226680Smav} 560226680Smav 561226680Smavint 562208410Smavata_pci_child_location_str(device_t dev, device_t child, char *buf, 563208410Smav size_t buflen) 564208410Smav{ 565208410Smav 566208410Smav snprintf(buf, buflen, "channel=%d", 567208410Smav (int)(intptr_t)device_get_ivars(child)); 568208410Smav return (0); 569208410Smav} 570208410Smav 571249476Skibstatic bus_dma_tag_t 572249476Skibata_pci_get_dma_tag(device_t bus, device_t child) 573249476Skib{ 574249476Skib 575249476Skib return (bus_get_dma_tag(bus)); 576249476Skib} 577249476Skib 57873897Ssosstatic device_method_t ata_pci_methods[] = { 57973897Ssos /* device interface */ 580144330Ssos DEVMETHOD(device_probe, ata_pci_probe), 581144330Ssos DEVMETHOD(device_attach, ata_pci_attach), 582144330Ssos DEVMETHOD(device_detach, ata_pci_detach), 583183141Ssos DEVMETHOD(device_suspend, ata_pci_suspend), 584183141Ssos DEVMETHOD(device_resume, ata_pci_resume), 585144330Ssos DEVMETHOD(device_shutdown, bus_generic_shutdown), 58673897Ssos 58773897Ssos /* bus methods */ 588199322Smav DEVMETHOD(bus_read_ivar, ata_pci_read_ivar), 589199322Smav DEVMETHOD(bus_write_ivar, ata_pci_write_ivar), 590144330Ssos DEVMETHOD(bus_alloc_resource, ata_pci_alloc_resource), 591144330Ssos DEVMETHOD(bus_release_resource, ata_pci_release_resource), 592144330Ssos DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 593144330Ssos DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 594144330Ssos DEVMETHOD(bus_setup_intr, ata_pci_setup_intr), 595144330Ssos DEVMETHOD(bus_teardown_intr, ata_pci_teardown_intr), 596199322Smav DEVMETHOD(pci_read_config, ata_pci_read_config), 597199322Smav DEVMETHOD(pci_write_config, ata_pci_write_config), 598226680Smav DEVMETHOD(bus_print_child, ata_pci_print_child), 599208410Smav DEVMETHOD(bus_child_location_str, ata_pci_child_location_str), 600249476Skib DEVMETHOD(bus_get_dma_tag, ata_pci_get_dma_tag), 601144330Ssos 602233282Smarius DEVMETHOD_END 60373897Ssos}; 60473897Ssos 605183724Ssosdevclass_t ata_pci_devclass; 606144330Ssos 60773897Ssosstatic driver_t ata_pci_driver = { 60873897Ssos "atapci", 60973897Ssos ata_pci_methods, 61090215Ssos sizeof(struct ata_pci_controller), 61173897Ssos}; 61273897Ssos 613233282SmariusDRIVER_MODULE(atapci, pci, ata_pci_driver, ata_pci_devclass, NULL, NULL); 614144330SsosMODULE_VERSION(atapci, 1); 615144330SsosMODULE_DEPEND(atapci, ata, 1, 1, 1); 61673897Ssos 61773897Ssosstatic int 618144397Ssosata_pcichannel_probe(device_t dev) 61973897Ssos{ 62073897Ssos 621199322Smav if ((intptr_t)device_get_ivars(dev) < 0) 622199322Smav return (ENXIO); 623226680Smav device_set_desc(dev, "ATA channel"); 624144330Ssos 625133556Ssos return ata_probe(dev); 626133556Ssos} 627133556Ssos 628133556Ssosstatic int 629144397Ssosata_pcichannel_attach(device_t dev) 630133556Ssos{ 631133556Ssos struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 632188615Smav struct ata_channel *ch = device_get_softc(dev); 633133556Ssos int error; 634133556Ssos 635188812Smav if (ch->attached) 636188812Smav return (0); 637188812Smav ch->attached = 1; 638188615Smav 639214016Smav ch->dev = dev; 640188615Smav ch->unit = (intptr_t)device_get_ivars(dev); 641188615Smav 642191674Smav resource_int_value(device_get_name(dev), 643191674Smav device_get_unit(dev), "pm_level", &ch->pm_level); 644191674Smav 645188765Smav if ((error = ctlr->ch_attach(dev))) 646112791Ssos return error; 647112791Ssos 648179717Ssos return ata_attach(dev); 64973897Ssos} 65073897Ssos 651133556Ssosstatic int 652144397Ssosata_pcichannel_detach(device_t dev) 653133556Ssos{ 654188765Smav struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 655188812Smav struct ata_channel *ch = device_get_softc(dev); 656133556Ssos int error; 657133556Ssos 658188812Smav if (!ch->attached) 659188812Smav return (0); 660188812Smav ch->attached = 0; 661188812Smav 662133556Ssos if ((error = ata_detach(dev))) 663133556Ssos return error; 664133556Ssos 665188765Smav if (ctlr->ch_detach) 666188765Smav return (ctlr->ch_detach(dev)); 667144667Ssos 668188765Smav return (0); 669133556Ssos} 670189600Smavstatic int 671189600Smavata_pcichannel_suspend(device_t dev) 672189600Smav{ 673190581Smav struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 674189600Smav struct ata_channel *ch = device_get_softc(dev); 675190581Smav int error; 676133556Ssos 677189600Smav if (!ch->attached) 678189600Smav return (0); 679189600Smav 680190581Smav if ((error = ata_suspend(dev))) 681190581Smav return (error); 682190581Smav 683190581Smav if (ctlr->ch_suspend != NULL && (error = ctlr->ch_suspend(dev))) 684190581Smav return (error); 685190581Smav 686190581Smav return (0); 687189600Smav} 688189600Smav 689144397Ssosstatic int 690189600Smavata_pcichannel_resume(device_t dev) 691189600Smav{ 692190581Smav struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 693189600Smav struct ata_channel *ch = device_get_softc(dev); 694190581Smav int error; 695189600Smav 696189600Smav if (!ch->attached) 697189600Smav return (0); 698189600Smav 699190581Smav if (ctlr->ch_resume != NULL && (error = ctlr->ch_resume(dev))) 700190581Smav return (error); 701190581Smav 702189600Smav return ata_resume(dev); 703189600Smav} 704189600Smav 705144397Ssosstatic void 706144397Ssosata_pcichannel_reset(device_t dev) 707144397Ssos{ 708144397Ssos struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 709144397Ssos struct ata_channel *ch = device_get_softc(dev); 710144397Ssos 711145818Ssos /* if DMA engine present reset it */ 712178067Ssos if (ch->dma.reset) 713178067Ssos ch->dma.reset(dev); 714144790Ssos 715144790Ssos /* reset the controller HW */ 716144397Ssos if (ctlr->reset) 717145713Ssos ctlr->reset(dev); 718145641Ssos else 719145713Ssos ata_generic_reset(dev); 720144397Ssos} 721144397Ssos 722200171Smavstatic int 723200171Smavata_pcichannel_setmode(device_t dev, int target, int mode) 724144397Ssos{ 725200171Smav struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 726144397Ssos 727200171Smav if (ctlr->setmode) 728200171Smav return (ctlr->setmode(dev, target, mode)); 729200171Smav else 730200171Smav return (ata_generic_setmode(dev, target, mode)); 731144397Ssos} 732144397Ssos 733200171Smavstatic int 734200171Smavata_pcichannel_getrev(device_t dev, int target) 735200171Smav{ 736200171Smav struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 737204195Smav struct ata_channel *ch = device_get_softc(dev); 738200171Smav 739204195Smav if (ch->flags & ATA_SATA) { 740204195Smav if (ctlr->getrev) 741204195Smav return (ctlr->getrev(dev, target)); 742204195Smav else 743204195Smav return (0xff); 744204195Smav } else 745200171Smav return (0); 746200171Smav} 747200171Smav 748144397Ssosstatic device_method_t ata_pcichannel_methods[] = { 74973897Ssos /* device interface */ 750144397Ssos DEVMETHOD(device_probe, ata_pcichannel_probe), 751144397Ssos DEVMETHOD(device_attach, ata_pcichannel_attach), 752144397Ssos DEVMETHOD(device_detach, ata_pcichannel_detach), 753144330Ssos DEVMETHOD(device_shutdown, bus_generic_shutdown), 754189600Smav DEVMETHOD(device_suspend, ata_pcichannel_suspend), 755189600Smav DEVMETHOD(device_resume, ata_pcichannel_resume), 756144397Ssos 757144397Ssos /* ATA methods */ 758144397Ssos DEVMETHOD(ata_setmode, ata_pcichannel_setmode), 759200171Smav DEVMETHOD(ata_getrev, ata_pcichannel_getrev), 760144397Ssos DEVMETHOD(ata_reset, ata_pcichannel_reset), 761144397Ssos 762233282Smarius DEVMETHOD_END 76373897Ssos}; 76473897Ssos 765144397Ssosdriver_t ata_pcichannel_driver = { 76673897Ssos "ata", 767144397Ssos ata_pcichannel_methods, 76890215Ssos sizeof(struct ata_channel), 76973897Ssos}; 77073897Ssos 771233282SmariusDRIVER_MODULE(ata, atapci, ata_pcichannel_driver, ata_devclass, NULL, NULL); 772183724Ssos 773183724Ssos/* 774183724Ssos * misc support fucntions 775183724Ssos */ 776183724Ssosint 777183724Ssosata_legacy(device_t dev) 778183724Ssos{ 779210168Smav return (((pci_read_config(dev, PCIR_SUBCLASS, 1) == PCIS_STORAGE_IDE) && 780210168Smav (pci_read_config(dev, PCIR_PROGIF, 1)&PCIP_STORAGE_IDE_MASTERDEV)&& 781183724Ssos ((pci_read_config(dev, PCIR_PROGIF, 1) & 782183724Ssos (PCIP_STORAGE_IDE_MODEPRIM | PCIP_STORAGE_IDE_MODESEC)) != 783183724Ssos (PCIP_STORAGE_IDE_MODEPRIM | PCIP_STORAGE_IDE_MODESEC))) || 784183724Ssos (!pci_read_config(dev, PCIR_BAR(0), 4) && 785183724Ssos !pci_read_config(dev, PCIR_BAR(1), 4) && 786183724Ssos !pci_read_config(dev, PCIR_BAR(2), 4) && 787183724Ssos !pci_read_config(dev, PCIR_BAR(3), 4) && 788183724Ssos !pci_read_config(dev, PCIR_BAR(5), 4))); 789183724Ssos} 790183724Ssos 791183724Ssosvoid 792183724Ssosata_generic_intr(void *data) 793183724Ssos{ 794183724Ssos struct ata_pci_controller *ctlr = data; 795183724Ssos struct ata_channel *ch; 796183724Ssos int unit; 797183724Ssos 798199322Smav for (unit = 0; unit < ATA_PCI_MAX_CH; unit++) { 799183724Ssos if ((ch = ctlr->interrupt[unit].argument)) 800183724Ssos ctlr->interrupt[unit].function(ch); 801183724Ssos } 802183724Ssos} 803183724Ssos 804183724Ssosint 805183724Ssosata_setup_interrupt(device_t dev, void *intr_func) 806183724Ssos{ 807183724Ssos struct ata_pci_controller *ctlr = device_get_softc(dev); 808188655Smav int i, msi = 0; 809183724Ssos 810186182Smav if (!ctlr->legacy) { 811188655Smav if (resource_int_value(device_get_name(dev), 812188655Smav device_get_unit(dev), "msi", &i) == 0 && i != 0) 813188655Smav msi = 1; 814188655Smav if (msi && pci_msi_count(dev) > 0 && pci_alloc_msi(dev, &msi) == 0) { 815188655Smav ctlr->r_irq_rid = 0x1; 816188655Smav } else { 817210165Smav msi = 0; 818188655Smav ctlr->r_irq_rid = ATA_IRQ_RID; 819188655Smav } 820188655Smav if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, 821188655Smav &ctlr->r_irq_rid, RF_SHAREABLE | RF_ACTIVE))) { 822183724Ssos device_printf(dev, "unable to map interrupt\n"); 823210165Smav if (msi) 824210165Smav pci_release_msi(dev); 825183724Ssos return ENXIO; 826183724Ssos } 827183724Ssos if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS, NULL, 828183724Ssos intr_func, ctlr, &ctlr->handle))) { 829183724Ssos device_printf(dev, "unable to setup interrupt\n"); 830210165Smav bus_release_resource(dev, 831210165Smav SYS_RES_IRQ, ctlr->r_irq_rid, ctlr->r_irq); 832210165Smav if (msi) 833210165Smav pci_release_msi(dev); 834183724Ssos return ENXIO; 835183724Ssos } 836183724Ssos } 837183724Ssos return 0; 838183724Ssos} 839183724Ssos 840183724Ssosvoid 841183724Ssosata_set_desc(device_t dev) 842183724Ssos{ 843183724Ssos struct ata_pci_controller *ctlr = device_get_softc(dev); 844183724Ssos char buffer[128]; 845183724Ssos 846183724Ssos sprintf(buffer, "%s %s %s controller", 847183724Ssos ata_pcivendor2str(dev), ctlr->chip->text, 848183724Ssos ata_mode2str(ctlr->chip->max_dma)); 849183724Ssos device_set_desc_copy(dev, buffer); 850183724Ssos} 851183724Ssos 852233282Smariusconst struct ata_chip_id * 853233282Smariusata_match_chip(device_t dev, const struct ata_chip_id *index) 854183724Ssos{ 855191573Sjkim uint32_t devid; 856191573Sjkim uint8_t revid; 857191573Sjkim 858191573Sjkim devid = pci_get_devid(dev); 859191573Sjkim revid = pci_get_revid(dev); 860183724Ssos while (index->chipid != 0) { 861191573Sjkim if (devid == index->chipid && revid >= index->chiprev) 862191573Sjkim return (index); 863183724Ssos index++; 864183724Ssos } 865191573Sjkim return (NULL); 866183724Ssos} 867183724Ssos 868233282Smariusconst struct ata_chip_id * 869233282Smariusata_find_chip(device_t dev, const struct ata_chip_id *index, int slot) 870183724Ssos{ 871233282Smarius const struct ata_chip_id *idx; 872183724Ssos device_t *children; 873183724Ssos int nchildren, i; 874191593Sjkim uint8_t s; 875183724Ssos 876183724Ssos if (device_get_children(device_get_parent(dev), &children, &nchildren)) 877191593Sjkim return (NULL); 878183724Ssos 879191593Sjkim for (i = 0; i < nchildren; i++) { 880191593Sjkim s = pci_get_slot(children[i]); 881191593Sjkim if ((slot >= 0 && s == slot) || (slot < 0 && s <= -slot)) { 882191593Sjkim idx = ata_match_chip(children[i], index); 883191593Sjkim if (idx != NULL) { 884183724Ssos free(children, M_TEMP); 885191593Sjkim return (idx); 886183724Ssos } 887183724Ssos } 888183724Ssos } 889183724Ssos free(children, M_TEMP); 890191593Sjkim return (NULL); 891183724Ssos} 892183724Ssos 893233282Smariusconst char * 894183724Ssosata_pcivendor2str(device_t dev) 895183724Ssos{ 896183724Ssos switch (pci_get_vendor(dev)) { 897183724Ssos case ATA_ACARD_ID: return "Acard"; 898183724Ssos case ATA_ACER_LABS_ID: return "AcerLabs"; 899183724Ssos case ATA_AMD_ID: return "AMD"; 900183724Ssos case ATA_ADAPTEC_ID: return "Adaptec"; 901183724Ssos case ATA_ATI_ID: return "ATI"; 902183724Ssos case ATA_CYRIX_ID: return "Cyrix"; 903183724Ssos case ATA_CYPRESS_ID: return "Cypress"; 904183724Ssos case ATA_HIGHPOINT_ID: return "HighPoint"; 905183724Ssos case ATA_INTEL_ID: return "Intel"; 906183724Ssos case ATA_ITE_ID: return "ITE"; 907183724Ssos case ATA_JMICRON_ID: return "JMicron"; 908183724Ssos case ATA_MARVELL_ID: return "Marvell"; 909203030Smav case ATA_MARVELL2_ID: return "Marvell"; 910183724Ssos case ATA_NATIONAL_ID: return "National"; 911183724Ssos case ATA_NETCELL_ID: return "Netcell"; 912183724Ssos case ATA_NVIDIA_ID: return "nVidia"; 913183724Ssos case ATA_PROMISE_ID: return "Promise"; 914183724Ssos case ATA_SERVERWORKS_ID: return "ServerWorks"; 915183724Ssos case ATA_SILICON_IMAGE_ID: return "SiI"; 916183724Ssos case ATA_SIS_ID: return "SiS"; 917183724Ssos case ATA_VIA_ID: return "VIA"; 918183724Ssos case ATA_CENATEK_ID: return "Cenatek"; 919183724Ssos case ATA_MICRON_ID: return "Micron"; 920183724Ssos default: return "Generic"; 921183724Ssos } 922183724Ssos} 923183724Ssos 924183724Ssosint 925183724Ssosata_mode2idx(int mode) 926183724Ssos{ 927183724Ssos if ((mode & ATA_DMA_MASK) == ATA_UDMA0) 928183724Ssos return (mode & ATA_MODE_MASK) + 8; 929183724Ssos if ((mode & ATA_DMA_MASK) == ATA_WDMA0) 930183724Ssos return (mode & ATA_MODE_MASK) + 5; 931183724Ssos return (mode & ATA_MODE_MASK) - ATA_PIO0; 932183724Ssos} 933