ata-all.c revision 112494
1218792Snp/*- 2218792Snp * Copyright (c) 1998 - 2003 S�ren Schmidt <sos@FreeBSD.org> 3218792Snp * All rights reserved. 4218792Snp * 5218792Snp * Redistribution and use in source and binary forms, with or without 6218792Snp * modification, are permitted provided that the following conditions 7218792Snp * are met: 8218792Snp * 1. Redistributions of source code must retain the above copyright 9218792Snp * notice, this list of conditions and the following disclaimer, 10218792Snp * without modification, immediately at the beginning of the file. 11218792Snp * 2. Redistributions in binary form must reproduce the above copyright 12218792Snp * notice, this list of conditions and the following disclaimer in the 13218792Snp * documentation and/or other materials provided with the distribution. 14218792Snp * 3. The name of the author may not be used to endorse or promote products 15218792Snp * derived from this software without specific prior written permission. 16218792Snp * 17218792Snp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18218792Snp * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19218792Snp * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20218792Snp * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21218792Snp * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22218792Snp * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23218792Snp * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24218792Snp * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25218792Snp * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26218792Snp * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27218792Snp * 28218792Snp * $FreeBSD: head/sys/dev/ata/ata-all.c 112494 2003-03-22 12:18:20Z mux $ 29218792Snp */ 30218792Snp 31218792Snp#include "opt_ata.h" 32218792Snp#include <sys/param.h> 33218792Snp#include <sys/systm.h> 34228561Snp#include <sys/ata.h> 35218792Snp#include <sys/kernel.h> 36218792Snp#include <sys/conf.h> 37218792Snp#include <sys/disk.h> 38218792Snp#include <sys/module.h> 39218792Snp#include <sys/bus.h> 40218792Snp#include <sys/bio.h> 41218792Snp#include <sys/malloc.h> 42218792Snp#include <sys/sysctl.h> 43218792Snp#include <machine/stdarg.h> 44218792Snp#include <machine/resource.h> 45218792Snp#include <machine/bus.h> 46218792Snp#include <sys/rman.h> 47235944Sbz#ifdef __alpha__ 48218792Snp#include <machine/md_var.h> 49218792Snp#endif 50218792Snp#include <dev/ata/ata-all.h> 51228561Snp#include <dev/ata/ata-disk.h> 52218792Snp#include <dev/ata/ata-raid.h> 53218792Snp#include <dev/ata/atapi-all.h> 54218792Snp 55218792Snp/* device structures */ 56218792Snpstatic d_ioctl_t ataioctl; 57218792Snpstatic struct cdevsw ata_cdevsw = { 58218792Snp .d_open = nullopen, 59218792Snp .d_close = nullclose, 60218792Snp .d_ioctl = ataioctl, 61218792Snp .d_name = "ata", 62218792Snp .d_maj = 159, 63218792Snp}; 64218792Snp 65218792Snp/* prototypes */ 66218792Snpstatic void ata_boot_attach(void); 67231115Snpstatic void ata_intr(void *); 68231115Snpstatic int ata_getparam(struct ata_device *, u_int8_t); 69231115Snpstatic int ata_service(struct ata_channel *); 70231115Snpstatic void bswap(int8_t *, int); 71231115Snpstatic void btrim(int8_t *, int); 72231115Snpstatic void bpack(int8_t *, int8_t *, int); 73231115Snpstatic void ata_change_mode(struct ata_device *, int); 74231115Snpstatic u_int8_t ata_enclosure_sensor(struct ata_device *, int, u_int8_t, u_int8_t); 75231115Snpstatic int ata_enclosure_status(struct ata_device *, int *, int *, int *, int *); 76231115Snp 77231115Snp/* sysctl vars */ 78218792SnpSYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters"); 79218792Snp 80218792Snp/* global vars */ 81218792Snpstruct intr_config_hook *ata_delayed_attach = NULL; 82218792Snpdevclass_t ata_devclass; 83218792Snp 84219285Snp/* local vars */ 85219285Snpstatic MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer"); 86218792Snp 87218792Snp/* misc defines */ 88218792Snp#define DEV_ATAPIALL defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || \ 89218792Snp defined(DEV_ATAPIST) || defined(DEV_ATAPICAM) 90218792Snp 91218792Snpint 92218792Snpata_probe(device_t dev) 93218792Snp{ 94219285Snp struct ata_channel *ch; 95219285Snp int rid; 96219285Snp 97218792Snp if (!dev || !(ch = device_get_softc(dev))) 98218792Snp return ENXIO; 99218792Snp 100218792Snp if (ch->r_io || ch->r_altio || ch->r_irq) 101218792Snp return EEXIST; 102218792Snp 103218792Snp /* initialize the softc basics */ 104218792Snp ch->active = ATA_IDLE; 105218792Snp ch->dev = dev; 106218792Snp 107218792Snp rid = ATA_IOADDR_RID; 108218792Snp ch->r_io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 109218792Snp ATA_IOSIZE, RF_ACTIVE); 110218792Snp if (!ch->r_io) 111218792Snp goto failure; 112218792Snp 113218792Snp rid = ATA_ALTADDR_RID; 114218792Snp ch->r_altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 115218792Snp ATA_ALTIOSIZE, RF_ACTIVE); 116218792Snp if (!ch->r_altio) 117218792Snp goto failure; 118218792Snp 119218792Snp rid = ATA_BMADDR_RID; 120218792Snp ch->r_bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 121218792Snp ATA_BMIOSIZE, RF_ACTIVE); 122218792Snp if (bootverbose) 123218792Snp ata_printf(ch, -1, "iobase=0x%04x altiobase=0x%04x bmaddr=0x%04x\n", 124218792Snp (int)rman_get_start(ch->r_io), 125218792Snp (int)rman_get_start(ch->r_altio), 126218792Snp (ch->r_bmio) ? (int)rman_get_start(ch->r_bmio) : 0); 127228561Snp 128218792Snp ch->locking(ch, ATA_LF_LOCK); 129228561Snp ata_reset(ch); 130219392Snp ch->locking(ch, ATA_LF_UNLOCK); 131255050Snp 132219392Snp ch->device[MASTER].channel = ch; 133255050Snp ch->device[MASTER].unit = ATA_MASTER; 134219392Snp ch->device[MASTER].mode = ATA_PIO; 135218792Snp ch->device[SLAVE].channel = ch; 136228561Snp ch->device[SLAVE].unit = ATA_SLAVE; 137228561Snp ch->device[SLAVE].mode = ATA_PIO; 138218792Snp TAILQ_INIT(&ch->ata_queue); 139218792Snp TAILQ_INIT(&ch->atapi_queue); 140218792Snp return 0; 141218792Snp 142218792Snpfailure: 143218792Snp if (ch->r_io) 144219944Snp bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ch->r_io); 145219944Snp if (ch->r_altio) 146219944Snp bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ch->r_altio); 147219944Snp if (ch->r_bmio) 148219944Snp bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, ch->r_bmio); 149219944Snp if (bootverbose) 150219944Snp ata_printf(ch, -1, "probe allocation failed\n"); 151245274Snp return ENXIO; 152245274Snp} 153245274Snp 154245274Snpint 155245274Snpata_attach(device_t dev) 156245274Snp{ 157245274Snp struct ata_channel *ch; 158245274Snp int error, rid; 159245274Snp 160245274Snp if (!dev || !(ch = device_get_softc(dev))) 161218792Snp return ENXIO; 162218792Snp 163218792Snp rid = ATA_IRQ_RID; 164228561Snp ch->r_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 165228561Snp RF_SHAREABLE | RF_ACTIVE); 166228561Snp if (!ch->r_irq) { 167237263Snp ata_printf(ch, -1, "unable to allocate interrupt\n"); 168255050Snp return ENXIO; 169218792Snp } 170218792Snp if ((error = bus_setup_intr(dev, ch->r_irq, INTR_TYPE_BIO | INTR_ENTROPY, 171218792Snp ata_intr, ch, &ch->ih))) { 172218792Snp ata_printf(ch, -1, "unable to setup interrupt\n"); 173218792Snp return error; 174228561Snp } 175228561Snp 176253691Snp if (ch->dma) 177218792Snp ch->dma->create(ch); 178218792Snp 179245274Snp /* 180245274Snp * do not attach devices if we are in early boot, this is done later 181245274Snp * when interrupts are enabled by a hook into the boot process. 182245274Snp * otherwise attach what the probe has found in ch->devices. 183245274Snp */ 184218792Snp if (!ata_delayed_attach) { 185218792Snp ch->locking(ch, ATA_LF_LOCK); 186218792Snp if (ch->devices & ATA_ATA_SLAVE) 187218792Snp if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY)) 188218792Snp ch->devices &= ~ATA_ATA_SLAVE; 189218792Snp if (ch->devices & ATA_ATAPI_SLAVE) 190218792Snp if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY)) 191218792Snp ch->devices &= ~ATA_ATAPI_SLAVE; 192218792Snp if (ch->devices & ATA_ATA_MASTER) 193218792Snp if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY)) 194218792Snp ch->devices &= ~ATA_ATA_MASTER; 195218792Snp if (ch->devices & ATA_ATAPI_MASTER) 196218792Snp if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY)) 197218792Snp ch->devices &= ~ATA_ATAPI_MASTER; 198218792Snp#ifdef DEV_ATADISK 199218792Snp if (ch->devices & ATA_ATA_MASTER) 200218792Snp ad_attach(&ch->device[MASTER]); 201218792Snp if (ch->devices & ATA_ATA_SLAVE) 202218792Snp ad_attach(&ch->device[SLAVE]); 203218792Snp#endif 204218792Snp#if DEV_ATAPIALL 205218792Snp if (ch->devices & ATA_ATAPI_MASTER) 206218792Snp atapi_attach(&ch->device[MASTER]); 207218792Snp if (ch->devices & ATA_ATAPI_SLAVE) 208218792Snp atapi_attach(&ch->device[SLAVE]); 209218792Snp#endif 210218792Snp#ifdef DEV_ATAPICAM 211218792Snp atapi_cam_attach_bus(ch); 212237263Snp#endif 213228561Snp ch->locking(ch, ATA_LF_UNLOCK); 214228561Snp } 215228561Snp return 0; 216228561Snp} 217228561Snp 218218792Snpint 219218792Snpata_detach(device_t dev) 220218792Snp{ 221218792Snp struct ata_channel *ch; 222218792Snp int s; 223252747Snp 224218792Snp if (!dev || !(ch = device_get_softc(dev)) || 225218792Snp !ch->r_io || !ch->r_altio || !ch->r_irq) 226218792Snp return ENXIO; 227237263Snp 228237263Snp /* make sure channel is not busy */ 229218792Snp ch->locking(ch, ATA_LF_LOCK); 230228561Snp ATA_SLEEPLOCK_CH(ch, ATA_CONTROL); 231218792Snp 232218792Snp s = splbio(); 233218792Snp#ifdef DEV_ATADISK 234218792Snp if (ch->devices & ATA_ATA_MASTER && ch->device[MASTER].driver) 235218792Snp ad_detach(&ch->device[MASTER], 1); 236218792Snp if (ch->devices & ATA_ATA_SLAVE && ch->device[SLAVE].driver) 237218792Snp ad_detach(&ch->device[SLAVE], 1); 238255050Snp#endif 239218792Snp#if DEV_ATAPIALL 240255050Snp if (ch->devices & ATA_ATAPI_MASTER && ch->device[MASTER].driver) 241218792Snp atapi_detach(&ch->device[MASTER]); 242218792Snp if (ch->devices & ATA_ATAPI_SLAVE && ch->device[SLAVE].driver) 243218792Snp atapi_detach(&ch->device[SLAVE]); 244218792Snp#endif 245218792Snp#ifdef DEV_ATAPICAM 246218792Snp atapi_cam_detach_bus(ch); 247218792Snp#endif 248218792Snp splx(s); 249218792Snp 250218792Snp if (ch->device[MASTER].param) { 251218792Snp free(ch->device[MASTER].param, M_ATA); 252218792Snp ch->device[MASTER].param = NULL; 253228561Snp } 254228561Snp if (ch->device[SLAVE].param) { 255228561Snp free(ch->device[SLAVE].param, M_ATA); 256228561Snp ch->device[SLAVE].param = NULL; 257228561Snp } 258228561Snp ch->device[MASTER].driver = NULL; 259228561Snp ch->device[SLAVE].driver = NULL; 260228561Snp ch->device[MASTER].mode = ATA_PIO; 261228561Snp ch->device[SLAVE].mode = ATA_PIO; 262218792Snp ch->devices = 0; 263218792Snp if (ch->dma) 264220873Snp ch->dma->destroy(ch); 265218792Snp 266218792Snp bus_teardown_intr(dev, ch->r_irq, ch->ih); 267218792Snp bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq); 268218792Snp if (ch->r_bmio) 269228561Snp bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, ch->r_bmio); 270228561Snp bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ch->r_altio); 271228561Snp bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ch->r_io); 272228561Snp ch->r_io = NULL; 273220649Snp ch->r_altio = NULL; 274220649Snp ch->r_bmio = NULL; 275220649Snp ch->r_irq = NULL; 276220649Snp ATA_UNLOCK_CH(ch); 277220649Snp ch->locking(ch, ATA_LF_UNLOCK); 278218792Snp return 0; 279218792Snp} 280218792Snp 281218792Snpint 282218792Snpata_resume(device_t dev) 283218792Snp{ 284218792Snp struct ata_channel *ch; 285218792Snp int error; 286219290Snp 287219290Snp if (!dev || !(ch = device_get_softc(dev))) 288219290Snp return ENXIO; 289219290Snp 290219290Snp ch->locking(ch, ATA_LF_LOCK); 291219290Snp error = ata_reinit(ch); 292219290Snp ch->locking(ch, ATA_LF_UNLOCK); 293228561Snp return error; 294218792Snp} 295218792Snp 296218792Snpstatic int 297218792Snpataioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, struct thread *td) 298228561Snp{ 299218792Snp struct ata_cmd *iocmd = (struct ata_cmd *)addr; 300218792Snp struct ata_channel *ch; 301218792Snp device_t device = devclass_get_device(ata_devclass, iocmd->channel); 302228561Snp int error; 303228561Snp 304228561Snp if (cmd != IOCATA) 305218792Snp return ENOTTY; 306218792Snp 307218792Snp if (iocmd->cmd == ATAGMAXCHANNEL) { 308228561Snp iocmd->u.maxchan = devclass_get_maxunit(ata_devclass); 309228561Snp return 0; 310237263Snp } 311228561Snp 312228561Snp if (iocmd->channel < -1 || iocmd->device < -1 || iocmd->device > SLAVE) 313228561Snp return ENXIO; 314218792Snp 315228561Snp switch (iocmd->cmd) { 316228561Snp case ATAATTACH: 317228561Snp /* should enable channel HW on controller that can SOS XXX */ 318228561Snp error = ata_probe(device); 319228561Snp if (!error) 320218792Snp error = ata_attach(device); 321218792Snp return error; 322248925Snp 323249392Snp case ATADETACH: 324248925Snp error = ata_detach(device); 325218792Snp /* should disable channel HW on controller that can SOS XXX */ 326218792Snp return error; 327218792Snp 328218792Snp case ATAREINIT: 329218792Snp if (!device || !(ch = device_get_softc(device))) 330218792Snp return ENXIO; 331218792Snp ch->locking(ch, ATA_LF_LOCK); 332228561Snp ATA_SLEEPLOCK_CH(ch, ATA_ACTIVE); 333228561Snp error = ata_reinit(ch); 334218792Snp ch->locking(ch, ATA_LF_UNLOCK); 335218792Snp return error; 336218792Snp 337218792Snp case ATAGMODE: 338218792Snp if (!device || !(ch = device_get_softc(device))) 339218792Snp return ENXIO; 340218792Snp 341218792Snp if ((iocmd->device == MASTER || iocmd->device == -1) && 342248925Snp ch->device[MASTER].driver) 343248925Snp iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode; 344248925Snp else 345218792Snp iocmd->u.mode.mode[MASTER] = -1; 346218792Snp 347218792Snp if ((iocmd->device == SLAVE || iocmd->device == -1) && 348218792Snp ch->device[SLAVE].param) 349218792Snp iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode; 350218792Snp else 351219288Snp iocmd->u.mode.mode[SLAVE] = -1; 352228561Snp return 0; 353228561Snp 354228561Snp case ATASMODE: 355228561Snp if (!device || !(ch = device_get_softc(device))) 356228561Snp return ENXIO; 357228561Snp 358228561Snp ch->locking(ch, ATA_LF_LOCK); 359228561Snp if ((iocmd->device == MASTER || iocmd->device == -1) && 360220873Snp iocmd->u.mode.mode[MASTER] >= 0 && ch->device[MASTER].param) { 361218792Snp ata_change_mode(&ch->device[MASTER],iocmd->u.mode.mode[MASTER]); 362255050Snp iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode; 363255050Snp } 364255050Snp else 365255050Snp iocmd->u.mode.mode[MASTER] = -1; 366255050Snp 367255050Snp if ((iocmd->device == SLAVE || iocmd->device == -1) && 368255050Snp iocmd->u.mode.mode[SLAVE] >= 0 && ch->device[SLAVE].param) { 369255050Snp ata_change_mode(&ch->device[SLAVE], iocmd->u.mode.mode[SLAVE]); 370255050Snp iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode; 371255050Snp } 372255050Snp else 373255050Snp iocmd->u.mode.mode[SLAVE] = -1; 374228561Snp ch->locking(ch, ATA_LF_UNLOCK); 375228561Snp return 0; 376228561Snp 377255050Snp case ATAGPARM: 378228561Snp if (!device || !(ch = device_get_softc(device))) 379228561Snp return ENXIO; 380228561Snp 381228561Snp iocmd->u.param.type[MASTER] = 382228561Snp ch->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER); 383218792Snp iocmd->u.param.type[SLAVE] = 384218792Snp ch->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE); 385218792Snp 386255050Snp if (ch->device[MASTER].name) 387255050Snp strcpy(iocmd->u.param.name[MASTER], ch->device[MASTER].name); 388218792Snp if (ch->device[SLAVE].name) 389218792Snp strcpy(iocmd->u.param.name[SLAVE], ch->device[SLAVE].name); 390218792Snp 391228561Snp if (ch->device[MASTER].param) 392218792Snp bcopy(ch->device[MASTER].param, &iocmd->u.param.params[MASTER], 393218792Snp sizeof(struct ata_params)); 394218792Snp if (ch->device[SLAVE].param) 395218792Snp bcopy(ch->device[SLAVE].param, &iocmd->u.param.params[SLAVE], 396218792Snp sizeof(struct ata_params)); 397218792Snp return 0; 398218792Snp 399218792Snp case ATAENCSTAT: { 400255050Snp struct ata_device *atadev; 401218792Snp 402218792Snp if (!device || !(ch = device_get_softc(device))) 403228561Snp return ENXIO; 404218792Snp 405255050Snp if (iocmd->device == SLAVE) 406255050Snp atadev = &ch->device[SLAVE]; 407228561Snp else 408218792Snp atadev = &ch->device[MASTER]; 409218792Snp 410220873Snp return ata_enclosure_status(atadev, 411218792Snp &iocmd->u.enclosure.fan, 412218792Snp &iocmd->u.enclosure.temp, 413220873Snp &iocmd->u.enclosure.v05, 414220873Snp &iocmd->u.enclosure.v12); 415220873Snp } 416220873Snp 417220873Snp#ifdef DEV_ATADISK 418218792Snp case ATARAIDREBUILD: 419218792Snp return ata_raid_rebuild(iocmd->channel); 420228561Snp 421219286Snp case ATARAIDCREATE: 422218792Snp return ata_raid_create(&iocmd->u.raid_setup); 423218792Snp 424218792Snp case ATARAIDDELETE: 425237819Snp return ata_raid_delete(iocmd->channel); 426218792Snp 427218792Snp case ATARAIDSTATUS: 428218792Snp return ata_raid_status(iocmd->channel, &iocmd->u.raid_status); 429218792Snp#endif 430218792Snp#if DEV_ATAPIALL 431218792Snp case ATAPICMD: { 432218792Snp struct ata_device *atadev; 433218792Snp caddr_t buf; 434218792Snp 435218792Snp if (!device || !(ch = device_get_softc(device))) 436218792Snp return ENXIO; 437220873Snp 438218792Snp if (!(atadev = &ch->device[iocmd->device]) || 439218792Snp !(ch->devices & (iocmd->device == MASTER ? 440218792Snp ATA_ATAPI_MASTER : ATA_ATAPI_SLAVE))) 441218792Snp return ENODEV; 442228561Snp 443218792Snp if (!(buf = malloc(iocmd->u.atapi.count, M_ATA, M_NOWAIT))) 444219290Snp return ENOMEM; 445237819Snp 446218792Snp if (iocmd->u.atapi.flags & ATAPI_CMD_WRITE) { 447219290Snp error = copyin(iocmd->u.atapi.data, buf, iocmd->u.atapi.count); 448218792Snp if (error) { 449218792Snp free(buf, M_ATA); 450218792Snp return error; 451218792Snp } 452218792Snp } 453218792Snp error = atapi_queue_cmd(atadev, iocmd->u.atapi.ccb, 454218792Snp buf, iocmd->u.atapi.count, 455218792Snp (iocmd->u.atapi.flags == ATAPI_CMD_READ ? 456218792Snp ATPR_F_READ : 0) | ATPR_F_QUIET, 457218792Snp iocmd->u.atapi.timeout, NULL, NULL); 458237263Snp if (error) { 459237263Snp iocmd->u.atapi.error = error; 460237263Snp bcopy(&atadev->result, iocmd->u.atapi.sense_data, 461237263Snp sizeof(struct atapi_reqsense)); 462241733Sed error = 0; 463237263Snp } 464237263Snp else if (iocmd->u.atapi.flags & ATAPI_CMD_READ) 465237263Snp error = copyout(buf, iocmd->u.atapi.data, iocmd->u.atapi.count); 466237263Snp 467228561Snp free(buf, M_ATA); 468228561Snp return error; 469228561Snp } 470228561Snp#endif 471228561Snp default: 472237263Snp break; 473237263Snp } 474237263Snp return ENOTTY; 475237263Snp} 476237263Snp 477241733Sedstatic int 478237263Snpata_getparam(struct ata_device *atadev, u_int8_t command) 479228561Snp{ 480228561Snp struct ata_params *ata_parm; 481237263Snp int retry = 0; 482237263Snp 483237263Snp if (!(ata_parm = malloc(sizeof(struct ata_params), M_ATA, M_NOWAIT))) { 484237263Snp ata_prtdev(atadev, "malloc for identify data failed\n"); 485237263Snp return -1; 486237263Snp } 487237263Snp 488228561Snp /* apparently some devices needs this repeated */ 489228561Snp do { 490228561Snp if (ata_command(atadev, command, 0, 0, 0, 491228561Snp dumping ? ATA_WAIT_READY : ATA_WAIT_INTR)) { 492228561Snp ata_prtdev(atadev, "%s identify failed\n", 493220873Snp command == ATA_C_ATAPI_IDENTIFY ? "ATAPI" : "ATA"); 494220873Snp free(ata_parm, M_ATA); 495228561Snp return -1; 496228561Snp } 497237263Snp if (retry++ > 4) { 498237263Snp ata_prtdev(atadev, "%s identify retries exceeded\n", 499237263Snp command == ATA_C_ATAPI_IDENTIFY ? "ATAPI" : "ATA"); 500220873Snp free(ata_parm, M_ATA); 501220873Snp return -1; 502228561Snp } 503220873Snp } while (ata_wait(atadev, ((command == ATA_C_ATAPI_IDENTIFY) ? 504220873Snp ATA_S_DRQ : (ATA_S_READY|ATA_S_DSC|ATA_S_DRQ)))); 505220873Snp ATA_INSW(atadev->channel->r_io, ATA_DATA, (int16_t *)ata_parm, 506220873Snp sizeof(struct ata_params)/sizeof(int16_t)); 507220873Snp 508220873Snp if (command == ATA_C_ATA_IDENTIFY || 509218792Snp !((ata_parm->model[0] == 'N' && ata_parm->model[1] == 'E') || 510228561Snp (ata_parm->model[0] == 'F' && ata_parm->model[1] == 'X') || 511228561Snp (ata_parm->model[0] == 'P' && ata_parm->model[1] == 'i'))) 512222701Snp bswap(ata_parm->model, sizeof(ata_parm->model)); 513256794Snp btrim(ata_parm->model, sizeof(ata_parm->model)); 514256794Snp bpack(ata_parm->model, ata_parm->model, sizeof(ata_parm->model)); 515218792Snp bswap(ata_parm->revision, sizeof(ata_parm->revision)); 516228561Snp btrim(ata_parm->revision, sizeof(ata_parm->revision)); 517228561Snp bpack(ata_parm->revision, ata_parm->revision, sizeof(ata_parm->revision)); 518237263Snp bswap(ata_parm->serial, sizeof(ata_parm->serial)); 519228561Snp btrim(ata_parm->serial, sizeof(ata_parm->serial)); 520228561Snp bpack(ata_parm->serial, ata_parm->serial, sizeof(ata_parm->serial)); 521228561Snp atadev->param = ata_parm; 522228561Snp return 0; 523228561Snp} 524218792Snp 525218792Snpstatic void 526228561Snpata_boot_attach(void) 527228561Snp{ 528218792Snp struct ata_channel *ch; 529218792Snp int ctlr; 530237263Snp 531228561Snp /* 532228561Snp * run through all ata devices and look for real ATA & ATAPI devices 533228561Snp * using the hints we found in the early probe, this avoids some of 534218792Snp * the delays probing of non-exsistent devices can cause. 535218792Snp */ 536218792Snp for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) { 537218792Snp if (!(ch = devclass_get_softc(ata_devclass, ctlr))) 538218792Snp continue; 539255050Snp ch->locking(ch, ATA_LF_LOCK); 540255050Snp if (ch->devices & ATA_ATA_SLAVE) 541255050Snp if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY)) 542218792Snp ch->devices &= ~ATA_ATA_SLAVE; 543218792Snp if (ch->devices & ATA_ATAPI_SLAVE) 544228561Snp if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY)) 545228561Snp ch->devices &= ~ATA_ATAPI_SLAVE; 546228561Snp if (ch->devices & ATA_ATA_MASTER) 547237263Snp if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY)) 548239336Snp ch->devices &= ~ATA_ATA_MASTER; 549228561Snp if (ch->devices & ATA_ATAPI_MASTER) 550218792Snp if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY)) 551228561Snp ch->devices &= ~ATA_ATAPI_MASTER; 552218792Snp ch->locking(ch, ATA_LF_UNLOCK); 553218792Snp } 554218792Snp#ifdef DEV_ATADISK 555218792Snp /* now we know whats there, do the real attach, first the ATA disks */ 556218792Snp for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) { 557218792Snp if (!(ch = devclass_get_softc(ata_devclass, ctlr))) 558218792Snp continue; 559218792Snp ch->locking(ch, ATA_LF_LOCK); 560218792Snp if (ch->devices & ATA_ATA_MASTER) 561218792Snp ad_attach(&ch->device[MASTER]); 562218792Snp if (ch->devices & ATA_ATA_SLAVE) 563248925Snp ad_attach(&ch->device[SLAVE]); 564248925Snp ch->locking(ch, ATA_LF_UNLOCK); 565248925Snp } 566218792Snp#endif 567218792Snp /* then the atapi devices */ 568218792Snp for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) { 569218792Snp if (!(ch = devclass_get_softc(ata_devclass, ctlr))) 570218792Snp continue; 571218792Snp ch->locking(ch, ATA_LF_LOCK); 572218792Snp#if DEV_ATAPIALL 573218792Snp if (ch->devices & ATA_ATAPI_MASTER) 574218792Snp atapi_attach(&ch->device[MASTER]); 575218792Snp if (ch->devices & ATA_ATAPI_SLAVE) 576218792Snp atapi_attach(&ch->device[SLAVE]); 577218792Snp#endif 578218792Snp#ifdef DEV_ATAPICAM 579218792Snp atapi_cam_attach_bus(ch); 580218792Snp#endif 581218792Snp ch->locking(ch, ATA_LF_UNLOCK); 582255015Snp } 583218792Snp if (ata_delayed_attach) { 584228561Snp config_intrhook_disestablish(ata_delayed_attach); 585218792Snp free(ata_delayed_attach, M_TEMP); 586218792Snp ata_delayed_attach = NULL; 587218792Snp } 588237263Snp#ifdef DEV_ATADISK 589237263Snp ata_raid_attach(); 590228561Snp#endif 591255005Snp} 592228561Snp 593222509Snpstatic void 594218792Snpata_intr(void *data) 595218792Snp{ 596248925Snp struct ata_channel *ch = (struct ata_channel *)data; 597218792Snp 598237263Snp /* if device is busy it didn't interrupt */ 599228561Snp if (ATA_INB(ch->r_altio, ATA_ALTSTAT) & ATA_S_BUSY) { 600228561Snp DELAY(100); 601218792Snp if (!(ATA_INB(ch->r_altio, ATA_ALTSTAT) & ATA_S_DRQ)) 602218792Snp return; 603253691Snp } 604253691Snp 605253691Snp /* clear interrupt and get status */ 606253691Snp ch->status = ATA_INB(ch->r_io, ATA_STATUS); 607253691Snp 608253691Snp if (ch->status & ATA_S_ERROR) 609253691Snp ch->error = ATA_INB(ch->r_io, ATA_ERROR); 610253691Snp 611218792Snp /* find & call the responsible driver to process this interrupt */ 612245936Snp switch (ch->active) { 613245936Snp#ifdef DEV_ATADISK 614218792Snp case ATA_ACTIVE_ATA: 615218792Snp if (!ch->running || ad_interrupt(ch->running) == ATA_OP_CONTINUES) 616218792Snp return; 617228561Snp break; 618228561Snp#endif 619228561Snp#if DEV_ATAPIALL 620228561Snp case ATA_ACTIVE_ATAPI: 621228561Snp if (!ch->running || atapi_interrupt(ch->running) == ATA_OP_CONTINUES) 622228561Snp return; 623220873Snp break; 624228561Snp#endif 625228561Snp default: 626218792Snp if (ch->active & ATA_WAIT_INTR) 627218792Snp wakeup(ch); 628228561Snp } 629228561Snp 630228561Snp if (ch->active & ATA_CONTROL) { 631228561Snp ATA_FORCELOCK_CH(ch, ATA_CONTROL); 632228561Snp return; 633228561Snp } 634237263Snp 635247291Snp if (ch->active & ATA_WAIT_INTR) { 636239336Snp ATA_UNLOCK_CH(ch); 637245274Snp return; 638245274Snp } 639245274Snp 640245274Snp if ((ch->flags & ATA_QUEUED) && 641245274Snp ATA_INB(ch->r_altio, ATA_ALTSTAT) & ATA_S_SERVICE) { 642218792Snp ATA_FORCELOCK_CH(ch, ATA_ACTIVE); 643218792Snp if (ata_service(ch) == ATA_OP_CONTINUES) 644218792Snp return; 645218792Snp } 646218792Snp ch->running = NULL; 647218792Snp ATA_UNLOCK_CH(ch); 648218792Snp ch->locking(ch, ATA_LF_UNLOCK); 649245274Snp ata_start(ch); 650245274Snp return; 651245274Snp} 652245274Snp 653245274Snpvoid 654245274Snpata_start(struct ata_channel *ch) 655218792Snp{ 656218792Snp#ifdef DEV_ATADISK 657218792Snp struct ad_request *ad_request; 658218792Snp#endif 659218792Snp#if DEV_ATAPIALL 660218792Snp struct atapi_request *atapi_request; 661218792Snp#endif 662218792Snp int s; 663218792Snp 664218792Snp ch->locking(ch, ATA_LF_LOCK); 665218792Snp if (!ATA_LOCK_CH(ch, ATA_ACTIVE)) 666218792Snp return; 667218792Snp 668218792Snp s = splbio(); 669218792Snp#ifdef DEV_ATADISK 670218792Snp /* find & call the responsible driver if anything on the ATA queue */ 671218792Snp if (TAILQ_EMPTY(&ch->ata_queue)) { 672218792Snp if (ch->devices & (ATA_ATA_MASTER) && ch->device[MASTER].driver) 673218792Snp ad_start(&ch->device[MASTER]); 674218792Snp if (ch->devices & (ATA_ATA_SLAVE) && ch->device[SLAVE].driver) 675218792Snp ad_start(&ch->device[SLAVE]); 676218792Snp } 677218792Snp if ((ad_request = TAILQ_FIRST(&ch->ata_queue))) { 678218792Snp TAILQ_REMOVE(&ch->ata_queue, ad_request, chain); 679218792Snp ch->active = ATA_ACTIVE_ATA; 680218792Snp ch->running = ad_request; 681218792Snp if (ad_transfer(ad_request) == ATA_OP_CONTINUES) { 682218792Snp splx(s); 683245517Snp return; 684245567Snp } 685245567Snp } 686245517Snp 687245567Snp#endif 688245567Snp#if DEV_ATAPIALL 689245517Snp /* find & call the responsible driver if anything on the ATAPI queue */ 690245567Snp if (TAILQ_EMPTY(&ch->atapi_queue)) { 691245567Snp if (ch->devices & (ATA_ATAPI_MASTER) && ch->device[MASTER].driver) 692245517Snp atapi_start(&ch->device[MASTER]); 693245567Snp if (ch->devices & (ATA_ATAPI_SLAVE) && ch->device[SLAVE].driver) 694245567Snp atapi_start(&ch->device[SLAVE]); 695218792Snp } 696222510Snp if ((atapi_request = TAILQ_FIRST(&ch->atapi_queue))) { 697222510Snp TAILQ_REMOVE(&ch->atapi_queue, atapi_request, chain); 698218792Snp ch->active = ATA_ACTIVE_ATAPI; 699218792Snp ch->running = atapi_request; 700218792Snp if (atapi_transfer(atapi_request) == ATA_OP_CONTINUES) { 701218792Snp splx(s); 702237263Snp return; 703218792Snp } 704218792Snp } 705218792Snp#endif 706218792Snp ATA_UNLOCK_CH(ch); 707218792Snp ch->locking(ch, ATA_LF_UNLOCK); 708218792Snp splx(s); 709237263Snp} 710218792Snp 711218792Snpvoid 712218792Snpata_reset(struct ata_channel *ch) 713218792Snp{ 714218792Snp u_int8_t lsb, msb, ostat0, ostat1; 715218792Snp u_int8_t stat0 = 0, stat1 = 0; 716237263Snp int mask = 0, timeout; 717218792Snp 718218792Snp /* do we have any signs of ATA/ATAPI HW being present ? */ 719218792Snp ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 720218792Snp DELAY(10); 721218792Snp ostat0 = ATA_INB(ch->r_io, ATA_STATUS); 722218792Snp if ((ostat0 & 0xf8) != 0xf8 && ostat0 != 0xa5) { 723237263Snp stat0 = ATA_S_BUSY; 724218792Snp mask |= 0x01; 725218792Snp } 726218792Snp ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 727218792Snp DELAY(10); 728218792Snp ostat1 = ATA_INB(ch->r_io, ATA_STATUS); 729218792Snp if ((ostat1 & 0xf8) != 0xf8 && ostat1 != 0xa5) { 730237263Snp stat1 = ATA_S_BUSY; 731218792Snp mask |= 0x02; 732218792Snp } 733218792Snp 734218792Snp ch->devices = 0; 735218792Snp if (!mask) 736218792Snp return; 737237263Snp 738218792Snp /* in some setups we dont want to test for a slave */ 739218792Snp if (ch->flags & ATA_NO_SLAVE) { 740218792Snp stat1 = 0x0; 741218792Snp mask &= ~0x02; 742218792Snp } 743218792Snp 744237263Snp if (bootverbose) 745218792Snp ata_printf(ch, -1, "pre reset mask=%02x ostat0=%02x ostat2=%02x\n", 746218792Snp mask, ostat0, ostat1); 747218792Snp 748218792Snp /* reset channel */ 749218792Snp ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 750218792Snp DELAY(10); 751237263Snp ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_RESET); 752218792Snp DELAY(10000); 753218792Snp ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_IDS); 754218792Snp DELAY(100000); 755218792Snp ATA_INB(ch->r_io, ATA_ERROR); 756218792Snp 757218792Snp /* wait for BUSY to go inactive */ 758237263Snp for (timeout = 0; timeout < 310000; timeout++) { 759218792Snp if (stat0 & ATA_S_BUSY) { 760218792Snp ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 761218792Snp DELAY(10); 762218792Snp 763218792Snp /* check for ATAPI signature while its still there */ 764218792Snp lsb = ATA_INB(ch->r_io, ATA_CYL_LSB); 765237263Snp msb = ATA_INB(ch->r_io, ATA_CYL_MSB); 766218792Snp stat0 = ATA_INB(ch->r_io, ATA_STATUS); 767218792Snp if (!(stat0 & ATA_S_BUSY)) { 768218792Snp if (bootverbose) 769218792Snp ata_printf(ch, ATA_MASTER, "ATAPI %02x %02x\n", lsb, msb); 770218792Snp if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB) 771218792Snp ch->devices |= ATA_ATAPI_MASTER; 772237263Snp } 773218792Snp } 774218792Snp if (stat1 & ATA_S_BUSY) { 775218792Snp ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 776218792Snp DELAY(10); 777218792Snp 778218792Snp /* check for ATAPI signature while its still there */ 779237263Snp lsb = ATA_INB(ch->r_io, ATA_CYL_LSB); 780218792Snp msb = ATA_INB(ch->r_io, ATA_CYL_MSB); 781218792Snp stat1 = ATA_INB(ch->r_io, ATA_STATUS); 782218792Snp if (!(stat1 & ATA_S_BUSY)) { 783248925Snp if (bootverbose) 784248925Snp ata_printf(ch, ATA_SLAVE, "ATAPI %02x %02x\n", lsb, msb); 785218792Snp if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB) 786237263Snp ch->devices |= ATA_ATAPI_SLAVE; 787218792Snp } 788218792Snp } 789218792Snp if (mask == 0x01) /* wait for master only */ 790250092Snp if (!(stat0 & ATA_S_BUSY)) 791250092Snp break; 792250092Snp if (mask == 0x02) /* wait for slave only */ 793250092Snp if (!(stat1 & ATA_S_BUSY)) 794250092Snp break; 795250092Snp if (mask == 0x03) /* wait for both master & slave */ 796250092Snp if (!(stat0 & ATA_S_BUSY) && !(stat1 & ATA_S_BUSY)) 797248925Snp break; 798248925Snp DELAY(100); 799228561Snp } 800237263Snp DELAY(10); 801228561Snp ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_4BIT); 802228561Snp 803228561Snp if (stat0 & ATA_S_BUSY) 804219286Snp mask &= ~0x01; 805228561Snp if (stat1 & ATA_S_BUSY) 806228561Snp mask &= ~0x02; 807218792Snp if (bootverbose) 808218792Snp ata_printf(ch, -1, "after reset mask=%02x stat0=%02x stat1=%02x\n", 809218792Snp mask, stat0, stat1); 810218792Snp if (!mask) 811252747Snp return; 812228561Snp 813228561Snp if (mask & 0x01 && ostat0 != 0x00 && !(ch->devices & ATA_ATAPI_MASTER)) { 814237263Snp ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 815239336Snp DELAY(10); 816239338Snp ATA_OUTB(ch->r_io, ATA_ERROR, 0x58); 817245274Snp ATA_OUTB(ch->r_io, ATA_CYL_LSB, 0xa5); 818245274Snp lsb = ATA_INB(ch->r_io, ATA_ERROR); 819218792Snp msb = ATA_INB(ch->r_io, ATA_CYL_LSB); 820219286Snp if (bootverbose) 821219392Snp ata_printf(ch, ATA_MASTER, "ATA %02x %02x\n", lsb, msb); 822248925Snp if (lsb != 0x58 && msb == 0xa5) 823248925Snp ch->devices |= ATA_ATA_MASTER; 824248925Snp } 825218792Snp if (mask & 0x02 && ostat1 != 0x00 && !(ch->devices & ATA_ATAPI_SLAVE)) { 826253829Snp ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 827253829Snp DELAY(10); 828218792Snp ATA_OUTB(ch->r_io, ATA_ERROR, 0x58); 829220873Snp ATA_OUTB(ch->r_io, ATA_CYL_LSB, 0xa5); 830220873Snp lsb = ATA_INB(ch->r_io, ATA_ERROR); 831228561Snp msb = ATA_INB(ch->r_io, ATA_CYL_LSB); 832228561Snp if (bootverbose) 833228561Snp ata_printf(ch, ATA_SLAVE, "ATA %02x %02x\n", lsb, msb); 834228561Snp if (lsb != 0x58 && msb == 0xa5) 835218792Snp ch->devices |= ATA_ATA_SLAVE; 836222510Snp } 837218792Snp if (bootverbose) 838218792Snp ata_printf(ch, -1, "devices=%02x\n", ch->devices); 839237263Snp} 840218792Snp 841218792Snpint 842228561Snpata_reinit(struct ata_channel *ch) 843218792Snp{ 844253691Snp int devices, misdev, newdev; 845253691Snp 846253691Snp ATA_FORCELOCK_CH(ch, ATA_CONTROL); 847253691Snp 848253691Snp if (!ch->r_io || !ch->r_altio || !ch->r_irq) { 849253691Snp ATA_UNLOCK_CH(ch); 850253691Snp return ENXIO; 851253691Snp } 852253691Snp 853253691Snp ch->running = NULL; 854237263Snp devices = ch->devices; 855237263Snp ata_printf(ch, -1, "resetting devices ..\n"); 856228561Snp ata_reset(ch); 857237263Snp 858237263Snp if ((misdev = devices & ~ch->devices)) { 859228561Snp#ifdef DEV_ATADISK 860237263Snp if (misdev & ATA_ATA_MASTER && ch->device[MASTER].driver) 861237263Snp ad_detach(&ch->device[MASTER], 0); 862237263Snp if (misdev & ATA_ATA_SLAVE && ch->device[SLAVE].driver) 863237263Snp ad_detach(&ch->device[SLAVE], 0); 864237263Snp#endif 865237263Snp#if DEV_ATAPIALL 866237263Snp if (misdev & ATA_ATAPI_MASTER && ch->device[MASTER].driver) 867237263Snp atapi_detach(&ch->device[MASTER]); 868237263Snp if (misdev & ATA_ATAPI_SLAVE && ch->device[SLAVE].driver) 869237263Snp atapi_detach(&ch->device[SLAVE]); 870237263Snp#endif 871237263Snp if (misdev & ATA_ATA_MASTER || misdev & ATA_ATAPI_MASTER) { 872237263Snp if (ch->device[MASTER].param) 873237263Snp free(ch->device[MASTER].param, M_ATA); 874237263Snp ch->device[MASTER].param = NULL; 875237263Snp } 876237263Snp if (misdev & ATA_ATA_SLAVE || misdev & ATA_ATAPI_SLAVE) { 877237263Snp if (ch->device[SLAVE].param) 878237263Snp free(ch->device[SLAVE].param, M_ATA); 879237263Snp ch->device[SLAVE].param = NULL; 880237263Snp } 881237263Snp } 882237263Snp if ((newdev = ~devices & ch->devices)) { 883237263Snp if (newdev & ATA_ATA_MASTER) 884237263Snp if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY)) 885228561Snp ch->devices &= ~ATA_ATA_MASTER; 886237263Snp if (newdev & ATA_ATA_SLAVE) 887228561Snp if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY)) 888228561Snp ch->devices &= ~ATA_ATA_SLAVE; 889228561Snp if (newdev & ATA_ATAPI_MASTER) 890218792Snp if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY)) 891 ch->devices &= ~ATA_ATAPI_MASTER; 892 if (newdev & ATA_ATAPI_SLAVE) 893 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY)) 894 ch->devices &= ~ATA_ATAPI_SLAVE; 895 } 896 newdev = ~devices & ch->devices; 897#ifdef DEV_ATADISK 898 if (newdev & ATA_ATA_SLAVE && !ch->device[SLAVE].driver) 899 ad_attach(&ch->device[SLAVE]); 900 else if (ch->devices & (ATA_ATA_SLAVE) && ch->device[SLAVE].driver) { 901 ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY); 902 ad_reinit(&ch->device[SLAVE]); 903 } 904 if (newdev & ATA_ATA_MASTER && !ch->device[MASTER].driver) 905 ad_attach(&ch->device[MASTER]); 906 else if (ch->devices & ATA_ATA_MASTER && ch->device[MASTER].driver) { 907 ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY); 908 ad_reinit(&ch->device[MASTER]); 909 } 910#endif 911#if DEV_ATAPIALL 912 if (newdev & ATA_ATAPI_SLAVE && !ch->device[SLAVE].driver) 913 atapi_attach(&ch->device[SLAVE]); 914 else if (ch->devices & (ATA_ATAPI_SLAVE) && ch->device[SLAVE].driver) { 915 ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY); 916 atapi_reinit(&ch->device[SLAVE]); 917 } 918 if (newdev & ATA_ATAPI_MASTER && !ch->device[MASTER].driver) 919 atapi_attach(&ch->device[MASTER]); 920 else if (ch->devices & (ATA_ATAPI_MASTER) && ch->device[MASTER].driver) { 921 ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY); 922 atapi_reinit(&ch->device[MASTER]); 923 } 924#endif 925#ifdef DEV_ATAPICAM 926 atapi_cam_reinit_bus(ch); 927#endif 928 printf("done\n"); 929 ATA_UNLOCK_CH(ch); 930 ata_start(ch); 931 return 0; 932} 933 934static int 935ata_service(struct ata_channel *ch) 936{ 937 /* do we have a SERVICE request from the drive ? */ 938 if ((ch->status & (ATA_S_SERVICE|ATA_S_ERROR|ATA_S_DRQ)) == ATA_S_SERVICE) { 939#if 0 /* XXX */ 940 ATA_OUTB(ch->r_bmio, ATA_BMSTAT_PORT, 941 ch->dma->status(ch) | ATA_BMSTAT_INTERRUPT); 942#endif 943#ifdef DEV_ATADISK 944 if ((ATA_INB(ch->r_io, ATA_DRIVE) & ATA_SLAVE) == ATA_MASTER) { 945 if ((ch->devices & ATA_ATA_MASTER) && ch->device[MASTER].driver) 946 return ad_service((struct ad_softc *) 947 ch->device[MASTER].driver, 0); 948 } 949 else { 950 if ((ch->devices & ATA_ATA_SLAVE) && ch->device[SLAVE].driver) 951 return ad_service((struct ad_softc *) 952 ch->device[SLAVE].driver, 0); 953 } 954#endif 955 } 956 return ATA_OP_FINISHED; 957} 958 959int 960ata_wait(struct ata_device *atadev, u_int8_t mask) 961{ 962 int timeout = 0; 963 964 DELAY(1); 965 while (timeout < 5000000) { /* timeout 5 secs */ 966 atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS); 967 968 /* if drive fails status, reselect the drive just to be sure */ 969 if (atadev->channel->status == 0xff) { 970 ata_prtdev(atadev, "no status, reselecting device\n"); 971 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM|atadev->unit); 972 DELAY(10); 973 atadev->channel->status = ATA_INB(atadev->channel->r_io,ATA_STATUS); 974 if (atadev->channel->status == 0xff) 975 return -1; 976 } 977 978 /* are we done ? */ 979 if (!(atadev->channel->status & ATA_S_BUSY)) 980 break; 981 982 if (timeout > 1000) { 983 timeout += 1000; 984 DELAY(1000); 985 } 986 else { 987 timeout += 10; 988 DELAY(10); 989 } 990 } 991 if (atadev->channel->status & ATA_S_ERROR) 992 atadev->channel->error = ATA_INB(atadev->channel->r_io, ATA_ERROR); 993 if (timeout >= 5000000) 994 return -1; 995 if (!mask) 996 return (atadev->channel->status & ATA_S_ERROR); 997 998 /* Wait 50 msec for bits wanted. */ 999 timeout = 5000; 1000 while (timeout--) { 1001 atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS); 1002 if ((atadev->channel->status & mask) == mask) { 1003 if (atadev->channel->status & ATA_S_ERROR) 1004 atadev->channel->error=ATA_INB(atadev->channel->r_io,ATA_ERROR); 1005 return (atadev->channel->status & ATA_S_ERROR); 1006 } 1007 DELAY (10); 1008 } 1009 return -1; 1010} 1011 1012int 1013ata_command(struct ata_device *atadev, u_int8_t command, 1014 u_int64_t lba, u_int16_t count, u_int16_t feature, int flags) 1015{ 1016 int error = 0; 1017#ifdef ATA_DEBUG 1018 ata_prtdev(atadev, "ata_command: addr=%04lx, cmd=%02x, " 1019 "lba=%jd, count=%d, feature=%d, flags=%02x\n", 1020 rman_get_start(atadev->channel->r_io), 1021 command, (intmax_t)lba, count, feature, flags); 1022#endif 1023 1024 /* select device */ 1025 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit); 1026 1027 /* disable interrupt from device */ 1028 if (atadev->channel->flags & ATA_QUEUED) 1029 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_4BIT); 1030 1031 /* ready to issue command ? */ 1032 if (ata_wait(atadev, 0) < 0) { 1033 ata_prtdev(atadev, "timeout sending command=%02x s=%02x e=%02x\n", 1034 command, atadev->channel->status, atadev->channel->error); 1035 return -1; 1036 } 1037 1038 /* only use 48bit addressing if needed because of the overhead */ 1039 if ((lba > 268435455 || count > 256) && atadev->param && 1040 atadev->param->support.address48) { 1041 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, (feature>>8) & 0xff); 1042 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, feature); 1043 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, (count>>8) & 0xff); 1044 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, count & 0xff); 1045 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, (lba>>24) & 0xff); 1046 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, lba & 0xff); 1047 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>32) & 0xff); 1048 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>8) & 0xff); 1049 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>40) & 0xff); 1050 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>16) & 0xff); 1051 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_LBA | atadev->unit); 1052 1053 /* translate command into 48bit version */ 1054 switch (command) { 1055 case ATA_C_READ: 1056 command = ATA_C_READ48; break; 1057 case ATA_C_READ_MUL: 1058 command = ATA_C_READ_MUL48; break; 1059 case ATA_C_READ_DMA: 1060 command = ATA_C_READ_DMA48; break; 1061 case ATA_C_READ_DMA_QUEUED: 1062 command = ATA_C_READ_DMA_QUEUED48; break; 1063 case ATA_C_WRITE: 1064 command = ATA_C_WRITE48; break; 1065 case ATA_C_WRITE_MUL: 1066 command = ATA_C_WRITE_MUL48; break; 1067 case ATA_C_WRITE_DMA: 1068 command = ATA_C_WRITE_DMA48; break; 1069 case ATA_C_WRITE_DMA_QUEUED: 1070 command = ATA_C_WRITE_DMA_QUEUED48; break; 1071 case ATA_C_FLUSHCACHE: 1072 command = ATA_C_FLUSHCACHE48; break; 1073 default: 1074 ata_prtdev(atadev, "can't translate cmd to 48bit version\n"); 1075 return -1; 1076 } 1077 atadev->channel->flags |= ATA_48BIT_ACTIVE; 1078 } 1079 else { 1080 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, feature); 1081 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, count); 1082 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, lba & 0xff); 1083 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>8) & 0xff); 1084 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>16) & 0xff); 1085 if (atadev->flags & ATA_D_USE_CHS) 1086 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, 1087 ATA_D_IBM | atadev->unit | ((lba>>24) & 0xf)); 1088 else 1089 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, 1090 ATA_D_IBM | ATA_D_LBA | atadev->unit | ((lba>>24) &0xf)); 1091 atadev->channel->flags &= ~ATA_48BIT_ACTIVE; 1092 } 1093 1094 switch (flags & ATA_WAIT_MASK) { 1095 case ATA_IMMEDIATE: 1096 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command); 1097 1098 /* enable interrupt */ 1099 if (atadev->channel->flags & ATA_QUEUED) 1100 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_4BIT); 1101 break; 1102 1103 case ATA_WAIT_INTR: 1104 atadev->channel->active |= ATA_WAIT_INTR; 1105 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command); 1106 1107 /* enable interrupt */ 1108 if (atadev->channel->flags & ATA_QUEUED) 1109 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_4BIT); 1110 1111 if (tsleep(atadev->channel, PRIBIO, "atacmd", 10 * hz)) { 1112 ata_prtdev(atadev, "timeout waiting for interrupt\n"); 1113 atadev->channel->active &= ~ATA_WAIT_INTR; 1114 error = -1; 1115 } 1116 break; 1117 1118 case ATA_WAIT_READY: 1119 atadev->channel->active |= ATA_WAIT_READY; 1120 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command); 1121 if (ata_wait(atadev, ATA_S_READY) < 0) { 1122 ata_prtdev(atadev, "timeout waiting for cmd=%02x s=%02x e=%02x\n", 1123 command, atadev->channel->status,atadev->channel->error); 1124 error = -1; 1125 } 1126 atadev->channel->active &= ~ATA_WAIT_READY; 1127 break; 1128 } 1129 return error; 1130} 1131 1132static void 1133ata_enclosure_start(struct ata_device *atadev) 1134{ 1135 ATA_INB(atadev->channel->r_io, ATA_DRIVE); 1136 DELAY(1); 1137 ATA_INB(atadev->channel->r_io, ATA_DRIVE); 1138 DELAY(1); 1139 ATA_INB(atadev->channel->r_io, ATA_CMD); 1140 DELAY(1); 1141 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit); 1142 DELAY(1); 1143 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit); 1144 DELAY(1); 1145 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit); 1146 DELAY(1); 1147 ATA_INB(atadev->channel->r_io, ATA_COUNT); 1148 DELAY(1); 1149 ATA_INB(atadev->channel->r_io, ATA_DRIVE); 1150 DELAY(1); 1151} 1152 1153static void 1154ata_enclosure_end(struct ata_device *atadev) 1155{ 1156 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit); 1157 DELAY(1); 1158} 1159 1160static void 1161ata_enclosure_chip_start(struct ata_device *atadev) 1162{ 1163 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0b); 1164 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0a); 1165 DELAY(25); 1166 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08); 1167} 1168 1169static void 1170ata_enclosure_chip_end(struct ata_device *atadev) 1171{ 1172 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08); 1173 DELAY(64); 1174 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0a); 1175 DELAY(25); 1176 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0b); 1177 DELAY(64); 1178} 1179 1180static u_int8_t 1181ata_enclosure_chip_rdbit(struct ata_device *atadev) 1182{ 1183 u_int8_t val; 1184 1185 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0); 1186 DELAY(64); 1187 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x02); 1188 DELAY(25); 1189 val = ATA_INB(atadev->channel->r_io, ATA_SECTOR) & 0x01; 1190 DELAY(38); 1191 return val; 1192} 1193 1194static void 1195ata_enclosure_chip_wrbit(struct ata_device *atadev, u_int8_t data) 1196{ 1197 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08 | (data & 0x01)); 1198 DELAY(64); 1199 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08 | 0x02 | (data & 0x01)); 1200 DELAY(64); 1201} 1202 1203static u_int8_t 1204ata_enclosure_chip_rw(struct ata_device *atadev, int rw, u_int8_t val) 1205{ 1206 int i; 1207 1208 if (rw) { 1209 for (i = 0; i < 8; i++) 1210 ata_enclosure_chip_wrbit(atadev, (val & (0x80 >> i)) ? 1 : 0); 1211 } 1212 else { 1213 for (i = 0; i < 8; i++) 1214 val = (val << 1) | ata_enclosure_chip_rdbit(atadev); 1215 } 1216 ata_enclosure_chip_wrbit(atadev, 0); 1217 return val; 1218} 1219 1220static u_int8_t 1221ata_enclosure_sensor(struct ata_device *atadev, 1222 int rw, u_int8_t idx, u_int8_t data) 1223{ 1224 ata_enclosure_start(atadev); 1225 ata_enclosure_chip_start(atadev); 1226 ata_enclosure_chip_rw(atadev, 1, 0x5a); 1227 ata_enclosure_chip_rw(atadev, 1, idx); 1228 if (rw) { 1229 ata_enclosure_chip_rw(atadev, 1, data); 1230 } 1231 else { 1232 ata_enclosure_chip_end(atadev); 1233 ata_enclosure_chip_start(atadev); 1234 ata_enclosure_chip_rw(atadev, 1, 0x5b); 1235 data = ata_enclosure_chip_rw(atadev, 0, 0); 1236 } 1237 ata_enclosure_chip_end(atadev); 1238 ata_enclosure_end(atadev); 1239 return data; 1240} 1241 1242static int 1243ata_enclosure_status(struct ata_device *atadev, 1244 int *fan, int *temp, int *v05, int *v12) 1245{ 1246 u_int8_t id1, id2, cnt, div; 1247 int error = ENXIO; 1248 1249 if (atadev->flags & ATA_D_ENC_PRESENT) { 1250 atadev->channel->locking(atadev->channel, ATA_LF_LOCK); 1251 ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL); 1252 ata_enclosure_sensor(atadev, 1, 0x4e, 0); 1253 id1 = ata_enclosure_sensor(atadev, 0, 0x4f, 0); 1254 ata_enclosure_sensor(atadev, 1, 0x4e, 0x80); 1255 id2 = ata_enclosure_sensor(atadev, 0, 0x4f, 0); 1256 if (id1 == 0xa3 && id2 == 0x5c) { 1257 div = 1 << (((ata_enclosure_sensor(atadev, 0, 0x5d, 0)&0x20)>>3)+ 1258 ((ata_enclosure_sensor(atadev, 0, 0x47, 0)&0x30)>>4)+1); 1259 cnt = ata_enclosure_sensor(atadev, 0, 0x28, 0); 1260 if (cnt == 0xff) 1261 *fan = 0; 1262 else 1263 *fan = 1350000 / cnt / div; 1264 ata_enclosure_sensor(atadev, 1, 0x4e, 0x01); 1265 *temp = (ata_enclosure_sensor(atadev, 0, 0x50, 0) * 10) + 1266 (ata_enclosure_sensor(atadev, 0, 0x50, 0) & 0x80 ? 5 : 0); 1267 *v05 = ata_enclosure_sensor(atadev, 0, 0x23, 0) * 27; 1268 *v12 = ata_enclosure_sensor(atadev, 0, 0x24, 0) * 61; 1269 error = 0; 1270 } 1271 ATA_UNLOCK_CH(atadev->channel); 1272 atadev->channel->locking(atadev->channel, ATA_LF_UNLOCK); 1273 } 1274 return error; 1275} 1276 1277void 1278ata_enclosure_print(struct ata_device *atadev) 1279{ 1280 u_int8_t id, st; 1281 int fan, temp, v05, v12; 1282 1283 atadev->channel->locking(atadev->channel, ATA_LF_LOCK); 1284 ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL); 1285 ata_enclosure_start(atadev); 1286 id = ATA_INB(atadev->channel->r_io, ATA_DRIVE); 1287 DELAY(1); 1288 st = ATA_INB(atadev->channel->r_io, ATA_COUNT); 1289 DELAY(1); 1290 ata_enclosure_end(atadev); 1291 ATA_UNLOCK_CH(atadev->channel); 1292 atadev->channel->locking(atadev->channel, ATA_LF_UNLOCK); 1293 1294 switch (id & 0x93) { 1295 case 0x00: 1296 ata_prtdev(atadev, "Universal enclosure"); 1297 break; 1298 case 0x01: 1299 ata_prtdev(atadev, "FastSwap enclosure"); 1300 break; 1301 case 0x10: 1302 case 0x11: 1303 ata_prtdev(atadev, "SuperSwap enclosure"); 1304 break; 1305 default: 1306 atadev->flags &= ~ATA_D_ENC_PRESENT; 1307 return; 1308 } 1309 atadev->flags |= ATA_D_ENC_PRESENT; 1310 1311 ata_enclosure_leds(atadev, ATA_LED_GREEN); 1312 if (ata_enclosure_status(atadev, &fan, &temp, &v05, &v12)) 1313 printf(" detected\n"); 1314 else 1315 printf(" [FAN:%drpm TEMP:%d.%01dC %d.%03dV %d.%03dV]\n", 1316 fan, temp/10, temp%10, v05/1000, v05%1000, v12/1000, v12%1000); 1317} 1318 1319void 1320ata_enclosure_leds(struct ata_device *atadev, u_int8_t color) 1321{ 1322 if (atadev->flags & ATA_D_ENC_PRESENT) { 1323 u_int8_t reg; 1324 1325 ata_enclosure_start(atadev); 1326 reg = ATA_INB(atadev->channel->r_io, ATA_COUNT); 1327 DELAY(1); 1328 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, 1329 (color & ATA_LED_MASK) | (reg & ~ATA_LED_MASK)); 1330 DELAY(1); 1331 ata_enclosure_end(atadev); 1332 } 1333} 1334 1335static void 1336ata_change_mode(struct ata_device *atadev, int mode) 1337{ 1338 ATA_SLEEPLOCK_CH(atadev->channel, ATA_ACTIVE); 1339 atadev->setmode(atadev, mode); 1340 ATA_UNLOCK_CH(atadev->channel); 1341 ata_start(atadev->channel); 1342} 1343 1344int 1345ata_printf(struct ata_channel *ch, int device, const char * fmt, ...) 1346{ 1347 va_list ap; 1348 int ret; 1349 1350 if (device == -1) 1351 ret = printf("ata%d: ", device_get_unit(ch->dev)); 1352 else { 1353 if (ch->device[ATA_DEV(device)].name) 1354 ret = printf("%s: ", ch->device[ATA_DEV(device)].name); 1355 else 1356 ret = printf("ata%d-%s: ", device_get_unit(ch->dev), 1357 (device == ATA_MASTER) ? "master" : "slave"); 1358 } 1359 va_start(ap, fmt); 1360 ret += vprintf(fmt, ap); 1361 va_end(ap); 1362 return ret; 1363} 1364 1365int 1366ata_prtdev(struct ata_device *atadev, const char * fmt, ...) 1367{ 1368 va_list ap; 1369 int ret; 1370 1371 if (atadev->name) 1372 ret = printf("%s: ", atadev->name); 1373 else 1374 ret = printf("ata%d-%s: ", device_get_unit(atadev->channel->dev), 1375 (atadev->unit == ATA_MASTER) ? "master" : "slave"); 1376 va_start(ap, fmt); 1377 ret += vprintf(fmt, ap); 1378 va_end(ap); 1379 return ret; 1380} 1381 1382void 1383ata_set_name(struct ata_device *atadev, char *name, int lun) 1384{ 1385 atadev->name = malloc(strlen(name) + 4, M_ATA, M_NOWAIT); 1386 if (atadev->name) 1387 sprintf(atadev->name, "%s%d", name, lun); 1388} 1389 1390void 1391ata_free_name(struct ata_device *atadev) 1392{ 1393 if (atadev->name) 1394 free(atadev->name, M_ATA); 1395 atadev->name = NULL; 1396} 1397 1398int 1399ata_get_lun(u_int32_t *map) 1400{ 1401 int lun = ffs(~*map) - 1; 1402 1403 *map |= (1 << lun); 1404 return lun; 1405} 1406 1407int 1408ata_test_lun(u_int32_t *map, int lun) 1409{ 1410 return (*map & (1 << lun)); 1411} 1412 1413void 1414ata_free_lun(u_int32_t *map, int lun) 1415{ 1416 *map &= ~(1 << lun); 1417} 1418 1419char * 1420ata_mode2str(int mode) 1421{ 1422 switch (mode) { 1423 case ATA_PIO: return "BIOSPIO"; 1424 case ATA_PIO0: return "PIO0"; 1425 case ATA_PIO1: return "PIO1"; 1426 case ATA_PIO2: return "PIO2"; 1427 case ATA_PIO3: return "PIO3"; 1428 case ATA_PIO4: return "PIO4"; 1429 case ATA_DMA: return "BIOSDMA"; 1430 case ATA_WDMA0: return "WDMA0"; 1431 case ATA_WDMA1: return "WDMA1"; 1432 case ATA_WDMA2: return "WDMA2"; 1433 case ATA_UDMA0: return "UDMA16"; 1434 case ATA_UDMA1: return "UDMA25"; 1435 case ATA_UDMA2: return "UDMA33"; 1436 case ATA_UDMA3: return "UDMA40"; 1437 case ATA_UDMA4: return "UDMA66"; 1438 case ATA_UDMA5: return "UDMA100"; 1439 case ATA_UDMA6: return "UDMA133"; 1440 default: return "???"; 1441 } 1442} 1443 1444int 1445ata_pmode(struct ata_params *ap) 1446{ 1447 if (ap->atavalid & ATA_FLAG_64_70) { 1448 if (ap->apiomodes & 0x02) 1449 return ATA_PIO4; 1450 if (ap->apiomodes & 0x01) 1451 return ATA_PIO3; 1452 } 1453 if (ap->retired_piomode == 2) 1454 return ATA_PIO2; 1455 if (ap->retired_piomode == 1) 1456 return ATA_PIO1; 1457 if (ap->retired_piomode == 0) 1458 return ATA_PIO0; 1459 if (ap->support_dma) 1460 return ATA_PIO4; 1461 return ATA_PIO0; 1462} 1463 1464int 1465ata_wmode(struct ata_params *ap) 1466{ 1467 if (ap->mwdmamodes & 0x04) 1468 return ATA_WDMA2; 1469 if (ap->mwdmamodes & 0x02) 1470 return ATA_WDMA1; 1471 if (ap->mwdmamodes & 0x01) 1472 return ATA_WDMA0; 1473 if (ap->support_dma) 1474 return ATA_WDMA2; 1475 return -1; 1476} 1477 1478int 1479ata_umode(struct ata_params *ap) 1480{ 1481 if (ap->atavalid & ATA_FLAG_88) { 1482 if (ap->udmamodes & 0x40) 1483 return ATA_UDMA6; 1484 if (ap->udmamodes & 0x20) 1485 return ATA_UDMA5; 1486 if (ap->udmamodes & 0x10) 1487 return ATA_UDMA4; 1488 if (ap->udmamodes & 0x08) 1489 return ATA_UDMA3; 1490 if (ap->udmamodes & 0x04) 1491 return ATA_UDMA2; 1492 if (ap->udmamodes & 0x02) 1493 return ATA_UDMA1; 1494 if (ap->udmamodes & 0x01) 1495 return ATA_UDMA0; 1496 } 1497 return -1; 1498} 1499 1500int 1501ata_limit_mode(struct ata_device *atadev, int mode, int maxmode) 1502{ 1503 if (maxmode && mode > maxmode) 1504 mode = maxmode; 1505 1506 if (mode >= ATA_UDMA0 && ata_umode(atadev->param) > 0) 1507 return min(mode, ata_umode(atadev->param)); 1508 1509 if (mode >= ATA_WDMA0 && ata_wmode(atadev->param) > 0) 1510 return min(mode, ata_wmode(atadev->param)); 1511 1512 if (mode > ata_pmode(atadev->param)) 1513 return min(mode, ata_pmode(atadev->param)); 1514 1515 return mode; 1516} 1517 1518static void 1519bswap(int8_t *buf, int len) 1520{ 1521 u_int16_t *ptr = (u_int16_t*)(buf + len); 1522 1523 while (--ptr >= (u_int16_t*)buf) 1524 *ptr = ntohs(*ptr); 1525} 1526 1527static void 1528btrim(int8_t *buf, int len) 1529{ 1530 int8_t *ptr; 1531 1532 for (ptr = buf; ptr < buf+len; ++ptr) 1533 if (!*ptr) 1534 *ptr = ' '; 1535 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr) 1536 *ptr = 0; 1537} 1538 1539static void 1540bpack(int8_t *src, int8_t *dst, int len) 1541{ 1542 int i, j, blank; 1543 1544 for (i = j = blank = 0 ; i < len; i++) { 1545 if (blank && src[i] == ' ') continue; 1546 if (blank && src[i] != ' ') { 1547 dst[j++] = src[i]; 1548 blank = 0; 1549 continue; 1550 } 1551 if (src[i] == ' ') { 1552 blank = 1; 1553 if (i == 0) 1554 continue; 1555 } 1556 dst[j++] = src[i]; 1557 } 1558 if (j < len) 1559 dst[j] = 0x00; 1560} 1561 1562static void 1563ata_init(void) 1564{ 1565 /* register controlling device */ 1566 make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "ata"); 1567 1568 /* register boot attach to be run when interrupts are enabled */ 1569 if (!(ata_delayed_attach = (struct intr_config_hook *) 1570 malloc(sizeof(struct intr_config_hook), 1571 M_TEMP, M_NOWAIT | M_ZERO))) { 1572 printf("ata: malloc of delayed attach hook failed\n"); 1573 return; 1574 } 1575 1576 ata_delayed_attach->ich_func = (void*)ata_boot_attach; 1577 if (config_intrhook_establish(ata_delayed_attach) != 0) { 1578 printf("ata: config_intrhook_establish failed\n"); 1579 free(ata_delayed_attach, M_TEMP); 1580 } 1581} 1582SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL) 1583