1/*- 2 * Copyright (c) 1998,1999 S�ren Schmidt 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 *
| 1/*- 2 * Copyright (c) 1998,1999 S�ren Schmidt 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 * $Id: ata-all.c,v 1.6 1999/04/10 18:53:35 sos Exp $
| 28 * $Id: ata-all.c,v 1.7 1999/04/16 21:21:52 peter Exp $
|
29 */ 30 31#include "ata.h" 32#if NATA > 0 33#include "isa.h" 34#include "pci.h" 35#include "atadisk.h"
| 29 */ 30 31#include "ata.h" 32#if NATA > 0 33#include "isa.h" 34#include "pci.h" 35#include "atadisk.h"
|
| 36#include "opt_global.h"
|
36#include <sys/param.h> 37#include <sys/systm.h> 38#include <sys/kernel.h> 39#include <sys/interrupt.h> 40#include <sys/conf.h>
| 37#include <sys/param.h> 38#include <sys/systm.h> 39#include <sys/kernel.h> 40#include <sys/interrupt.h> 41#include <sys/conf.h>
|
| 42#include <sys/module.h> 43#include <sys/bus.h>
|
41#include <sys/buf.h> 42#include <sys/malloc.h> 43#include <sys/devicestat.h> 44#include <vm/vm.h> 45#include <vm/pmap.h>
| 44#include <sys/buf.h> 45#include <sys/malloc.h> 46#include <sys/devicestat.h> 47#include <vm/vm.h> 48#include <vm/pmap.h>
|
| 49#include <machine/resource.h> 50#include <machine/bus.h> 51#include <sys/rman.h>
|
46#include <machine/clock.h> 47#ifdef __i386__ 48#include <machine/smp.h> 49#endif 50#include <pci/pcivar.h> 51#include <pci/pcireg.h>
| 52#include <machine/clock.h> 53#ifdef __i386__ 54#include <machine/smp.h> 55#endif 56#include <pci/pcivar.h> 57#include <pci/pcireg.h>
|
52#ifdef __i386__ 53#include <i386/isa/icu.h> 54#include <i386/isa/isa.h> 55#include <i386/isa/isa_device.h> 56#else
| 58#include <isa/isavar.h>
|
57#include <isa/isareg.h>
| 59#include <isa/isareg.h>
|
58#endif
| |
59#include <dev/ata/ata-all.h> 60#include <dev/ata/ata-disk.h> 61#include <dev/ata/atapi-all.h> 62 63/* misc defines */ 64#define UNIT(dev) (dev>>3 & 0x1f) /* assume 8 minor # per unit */ 65#define MIN(a,b) ((a)>(b)?(b):(a))
| 60#include <dev/ata/ata-all.h> 61#include <dev/ata/ata-disk.h> 62#include <dev/ata/atapi-all.h> 63 64/* misc defines */ 65#define UNIT(dev) (dev>>3 & 0x1f) /* assume 8 minor # per unit */ 66#define MIN(a,b) ((a)>(b)?(b):(a))
|
66#if NSMP == 0
| 67#if SMP == 0
|
67#define isa_apic_irq(x) x 68#endif 69 70/* prototypes */
| 68#define isa_apic_irq(x) x 69#endif 70 71/* prototypes */
|
71#if NISA > 0 && defined(__i386__) 72static int32_t ata_isaprobe(struct isa_device *); 73static int32_t ata_isaattach(struct isa_device *); 74#endif
| |
75#if NPCI > 0
| 72#if NPCI > 0
|
76static const char *ata_pciprobe(pcici_t, pcidi_t); 77static void ata_pciattach(pcici_t, int32_t);
| |
78static void promise_intr(int32_t); 79#endif
| 73static void promise_intr(int32_t); 74#endif
|
80static int32_t ata_probe(int32_t, int32_t, int32_t, pcici_t, int32_t *);
| 75static int32_t ata_probe(int32_t, int32_t, int32_t, device_t, int32_t *);
|
81static void ataintr(int32_t); 82
| 76static void ataintr(int32_t); 77
|
| 78/* 79 * Ought to be handled by the devclass. 80 */
|
83static int32_t atanlun = 0; 84struct ata_softc *atadevices[MAXATA]; 85
| 81static int32_t atanlun = 0; 82struct ata_softc *atadevices[MAXATA]; 83
|
86#if NISA > 0 && defined(__i386__) 87struct isa_driver atadriver = { ata_isaprobe, ata_isaattach, "ata" };
| 84static devclass_t ata_devclass;
|
88
| 85
|
89static int32_t 90ata_isaprobe(struct isa_device *devp)
| 86#if NISA > 0 87 88static int 89ata_isaprobe(device_t dev)
|
91{
| 90{
|
| 91 struct resource *port; 92 int rid;
|
92 int32_t ctlr, res;
| 93 int32_t ctlr, res;
|
93
| 94 int unit; 95 96 /* Allocate the port range */ 97 rid = 0; 98 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE); 99 if (!port) 100 return (ENOMEM); 101
|
94 for (ctlr = 0; ctlr < atanlun; ctlr++) {
| 102 for (ctlr = 0; ctlr < atanlun; ctlr++) {
|
95 if (atadevices[ctlr]->ioaddr == devp->id_iobase) {
| 103 if (atadevices[ctlr]->ioaddr == rman_get_start(port)) {
|
96 printf("ata-isa%d: already registered as ata%d\n",
| 104 printf("ata-isa%d: already registered as ata%d\n",
|
97 devp->id_unit, ctlr); 98 return 0;
| 105 device_get_unit(dev), ctlr); 106 bus_release_resource(dev, SYS_RES_IOPORT, 0, port); 107 return ENXIO;
|
99 } 100 }
| 108 } 109 }
|
101 res = ata_probe(devp->id_iobase, devp->id_iobase + ATA_ALTPORT, 0, 0, 102 &devp->id_unit); 103 if (res) 104 devp->id_intr = (inthand2_t *)ataintr; 105 return res;
| 110 111 /* 112 * XXX not sure what to do with the unit. The new bus code will 113 * automatically assign unit numbers for both pci and isa. For 114 * now, store it in the softc. 115 */ 116 res = ata_probe(rman_get_start(port), rman_get_start(port) + ATA_ALTPORT, 117 0, dev, &unit); 118 *(int *) device_get_softc(dev) = unit; 119 120 bus_release_resource(dev, SYS_RES_IOPORT, 0, port); 121 122 if (res) { 123 isa_set_portsize(dev, res); 124 return 0; 125 } 126 127 return ENXIO;
|
106} 107
| 128} 129
|
108static int32_t 109ata_isaattach(struct isa_device *devp)
| 130static int 131ata_isaattach(device_t dev)
|
110{
| 132{
|
111 return 1;
| 133 struct resource *port; 134 struct resource *irq; 135 int rid, unit; 136 void *ih; 137 138 /* Allocate the port range and interrupt */ 139 rid = 0; 140 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE); 141 if (!port) 142 return (ENOMEM); 143 144 rid = 0; 145 irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, RF_ACTIVE); 146 if (!irq) { 147 bus_release_resource(dev, SYS_RES_IOPORT, 0, port); 148 return (ENOMEM); 149 } 150 151 /* 152 * The interrupt code could be changed to take the ata_softc as 153 * its argument directly. 154 */ 155 unit = *(int *) device_get_softc(dev); 156 return bus_setup_intr(dev, irq, (driver_intr_t *) ataintr, 157 (void*)(uintptr_t) unit, &ih);
|
112}
| 158}
|
113#endif
| |
114
| 159
|
115#if NPCI > 0 116static u_long ata_pcicount; 117static struct pci_device ata_pcidevice = { 118 "ata-pci", ata_pciprobe, ata_pciattach, &ata_pcicount, 0
| 160static device_method_t ata_isa_methods[] = { 161 /* Device interface */ 162 DEVMETHOD(device_probe, ata_isaprobe), 163 DEVMETHOD(device_attach, ata_isaattach), 164 { 0, 0 }
|
119}; 120
| 165}; 166
|
121DATA_SET(pcidevice_set, ata_pcidevice);
| 167static driver_t ata_isa_driver = { 168 "ata", 169 ata_isa_methods, 170 DRIVER_TYPE_BIO, 171 sizeof(int), 172};
|
122
| 173
|
| 174DRIVER_MODULE(ata, isa, ata_isa_driver, ata_devclass, 0, 0); 175 176#endif 177 178#if NPCI > 0 179
|
123static const char *
| 180static const char *
|
124ata_pciprobe(pcici_t tag, pcidi_t type)
| 181ata_pcimatch(device_t dev)
|
125{ 126 u_int32_t data; 127
| 182{ 183 u_int32_t data; 184
|
128 data = pci_conf_read(tag, PCI_CLASS_REG); 129 if ((data & PCI_CLASS_MASK) == PCI_CLASS_MASS_STORAGE && 130 ((data & PCI_SUBCLASS_MASK) == 0x00010000 || 131 ((data & PCI_SUBCLASS_MASK) == 0x00040000))) { 132 switch (type) {
| 185 data = pci_read_config(dev, PCI_CLASS_REG, 4); 186 if (pci_get_class(dev) == PCIC_STORAGE && 187 (pci_get_subclass(dev) == PCIS_STORAGE_IDE || 188 pci_get_subclass(dev) == PCIS_STORAGE_RAID)) { 189 switch (pci_get_devid(dev)) {
|
133 case 0x12308086: 134 return "Intel PIIX IDE controller"; 135 case 0x70108086: 136 return "Intel PIIX3 IDE controller"; 137 case 0x71118086: 138 return "Intel PIIX4 IDE controller"; 139 case 0x4d33105a: 140 return "Promise Ultra/33 IDE controller"; 141 case 0x522910b9: 142 return "AcerLabs Aladdin IDE controller";
| 190 case 0x12308086: 191 return "Intel PIIX IDE controller"; 192 case 0x70108086: 193 return "Intel PIIX3 IDE controller"; 194 case 0x71118086: 195 return "Intel PIIX4 IDE controller"; 196 case 0x4d33105a: 197 return "Promise Ultra/33 IDE controller"; 198 case 0x522910b9: 199 return "AcerLabs Aladdin IDE controller";
|
| 200#if 0 201 case 0x05711106: 202 return "VIA Apollo IDE controller";
|
143 case 0x06401095: 144 return "CMD 640 IDE controller"; 145 case 0x06461095: 146 return "CMD 646 IDE controller";
| 203 case 0x06401095: 204 return "CMD 640 IDE controller"; 205 case 0x06461095: 206 return "CMD 646 IDE controller";
|
147#if 0 148 case 0x05711106: 149 return "VIA Apollo IDE controller";
| 207 case 0xc6931080: 208 return "Cypress 82C693 IDE controller";
|
150 case 0x01021078: 151 return "Cyrix 5530 IDE controller"; 152#endif 153 default: 154 return "Unknown PCI IDE controller"; 155 } 156 } 157 return NULL; 158} 159
| 209 case 0x01021078: 210 return "Cyrix 5530 IDE controller"; 211#endif 212 default: 213 return "Unknown PCI IDE controller"; 214 } 215 } 216 return NULL; 217} 218
|
160static void 161ata_pciattach(pcici_t tag, int32_t unit)
| 219static int 220ata_pciprobe(device_t dev)
|
162{
| 221{
|
163 pcidi_t type, class, cmd;
| 222 const char *desc = ata_pcimatch(dev); 223 if (desc) { 224 device_set_desc(dev, desc); 225 return 0; 226 } 227 else 228 return ENXIO; 229} 230 231static int 232ata_pciattach(device_t dev) 233{ 234 int unit = device_get_unit(dev); 235 u_int32_t type; 236 u_int8_t class, subclass; 237 u_int32_t cmd;
|
164 int32_t iobase_1, iobase_2, altiobase_1, altiobase_2; 165 int32_t bmaddr_1 = 0, bmaddr_2 = 0, sysctrl = 0, irq1, irq2; 166 int32_t lun; 167 168 /* set up vendor-specific stuff */
| 238 int32_t iobase_1, iobase_2, altiobase_1, altiobase_2; 239 int32_t bmaddr_1 = 0, bmaddr_2 = 0, sysctrl = 0, irq1, irq2; 240 int32_t lun; 241 242 /* set up vendor-specific stuff */
|
169 type = pci_conf_read(tag, PCI_ID_REG); 170 class = pci_conf_read(tag, PCI_CLASS_REG); 171 cmd = pci_conf_read(tag, PCI_COMMAND_STATUS_REG);
| 243 type = pci_get_devid(dev); 244 class = pci_get_class(dev); 245 subclass = pci_get_subclass(dev); 246 cmd = pci_read_config(dev, PCIR_COMMAND, 4);
|
172 173#ifdef ATA_DEBUG
| 247 248#ifdef ATA_DEBUG
|
174 printf("ata%d: type=%08x class=%08x cmd=%08x\n", unit, type, class, cmd);
| 249 printf("ata%d: type=%08x class=%02x subclass=%02x cmd=%08x\n", 250 unit, type, class, subclass, cmd);
|
175#endif 176 177 /* if this is a Promise controller handle it specially */ 178 if (type == 0x4d33105a) {
| 251#endif 252 253 /* if this is a Promise controller handle it specially */ 254 if (type == 0x4d33105a) {
|
179 iobase_1 = pci_conf_read(tag, 0x10) & 0xfffc; 180 altiobase_1 = pci_conf_read(tag, 0x14) & 0xfffc; 181 iobase_2 = pci_conf_read(tag, 0x18) & 0xfffc; 182 altiobase_2 = pci_conf_read(tag, 0x1c) & 0xfffc; 183 irq1 = irq2 = pci_conf_read(tag, PCI_INTERRUPT_REG) & 0xff; 184 bmaddr_1 = pci_conf_read(tag, 0x20) & 0xfffc;
| 255 iobase_1 = pci_read_config(dev, 0x10, 4) & 0xfffc; 256 altiobase_1 = pci_read_config(dev, 0x14, 4) & 0xfffc; 257 iobase_2 = pci_read_config(dev, 0x18, 4) & 0xfffc; 258 altiobase_2 = pci_read_config(dev, 0x1c, 4) & 0xfffc; 259 irq1 = irq2 = pci_read_config(dev, PCI_INTERRUPT_REG, 4) & 0xff; 260 bmaddr_1 = pci_read_config(dev, 0x20, 4) & 0xfffc;
|
185 bmaddr_2 = bmaddr_1 + ATA_BM_OFFSET1;
| 261 bmaddr_2 = bmaddr_1 + ATA_BM_OFFSET1;
|
186 sysctrl = (pci_conf_read(tag, 0x20) & 0xfffc) + 0x1c;
| 262 sysctrl = (pci_read_config(dev, 0x20, 4) & 0xfffc) + 0x1c;
|
187 outb(bmaddr_1 + 0x1f, inb(bmaddr_1 + 0x1f) | 0x01); 188 printf("ata-pci%d: Busmastering DMA supported\n", unit); 189 } 190 /* everybody else seems to do it this way */ 191 else {
| 263 outb(bmaddr_1 + 0x1f, inb(bmaddr_1 + 0x1f) | 0x01); 264 printf("ata-pci%d: Busmastering DMA supported\n", unit); 265 } 266 /* everybody else seems to do it this way */ 267 else {
|
192 if ((class & 0x100) == 0) {
| 268 if ((unit == 0) && 269 (pci_get_progif(dev) & PCIP_STORAGE_IDE_MODEPRIM) == 0) {
|
193 iobase_1 = IO_WD1; 194 altiobase_1 = iobase_1 + ATA_ALTPORT; 195 irq1 = 14; 196 } 197 else {
| 270 iobase_1 = IO_WD1; 271 altiobase_1 = iobase_1 + ATA_ALTPORT; 272 irq1 = 14; 273 } 274 else {
|
198 iobase_1 = pci_conf_read(tag, 0x10) & 0xfffc; 199 altiobase_1 = pci_conf_read(tag, 0x14) & 0xfffc; 200 irq1 = pci_conf_read(tag, PCI_INTERRUPT_REG) & 0xff;
| 275 iobase_1 = pci_read_config(dev, 0x10, 4) & 0xfffc; 276 altiobase_1 = pci_read_config(dev, 0x14, 4) & 0xfffc; 277 irq1 = pci_read_config(dev, PCI_INTERRUPT_REG, 4) & 0xff;
|
201 }
| 278 }
|
202 if ((class & 0x400) == 0) {
| 279 if ((unit == 0) && 280 (pci_get_progif(dev) & PCIP_STORAGE_IDE_MODESEC) == 0) {
|
203 iobase_2 = IO_WD2; 204 altiobase_2 = iobase_2 + ATA_ALTPORT; 205 irq2 = 15; 206 } 207 else {
| 281 iobase_2 = IO_WD2; 282 altiobase_2 = iobase_2 + ATA_ALTPORT; 283 irq2 = 15; 284 } 285 else {
|
208 iobase_2 = pci_conf_read(tag, 0x18) & 0xfffc; 209 altiobase_2 = pci_conf_read(tag, 0x1c) & 0xfffc; 210 irq2 = pci_conf_read(tag, PCI_INTERRUPT_REG) & 0xff;
| 286 iobase_2 = pci_read_config(dev, 0x18, 4) & 0xfffc; 287 altiobase_2 = pci_read_config(dev, 0x1c, 4) & 0xfffc; 288 irq2 = pci_read_config(dev, PCI_INTERRUPT_REG, 4) & 0xff;
|
211 } 212 213 /* is this controller busmaster capable ? */
| 289 } 290 291 /* is this controller busmaster capable ? */
|
214 if (pci_conf_read(tag, PCI_CLASS_REG) & 0x8000) {
| 292 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) {
|
215 /* is busmastering support turned on ? */
| 293 /* is busmastering support turned on ? */
|
216 if ((pci_conf_read(tag, PCI_COMMAND_STATUS_REG) & 5) == 5) {
| 294 if ((pci_read_config(dev, PCI_COMMAND_STATUS_REG, 4) & 5) == 5) {
|
217 /* is there a valid port range to connect to ? */
| 295 /* is there a valid port range to connect to ? */
|
218 if ((bmaddr_1 = pci_conf_read(tag, 0x20) & 0xfffc)) {
| 296 if ((bmaddr_1 = pci_read_config(dev, 0x20, 4) & 0xfffc)) {
|
219 bmaddr_2 = bmaddr_1 + ATA_BM_OFFSET1; 220 printf("ata-pci%d: Busmastering DMA supported\n", unit); 221 } 222 else 223 printf("ata-pci%d: Busmastering DMA not configured\n",unit); 224 } 225 else 226 printf("ata-pci%d: Busmastering DMA not enabled\n", unit); 227 } 228 else 229 printf("ata-pci%d: Busmastering DMA not supported\n", unit); 230 } 231 232 /* now probe the addresse found for "real" ATA/ATAPI hardware */ 233 lun = 0;
| 297 bmaddr_2 = bmaddr_1 + ATA_BM_OFFSET1; 298 printf("ata-pci%d: Busmastering DMA supported\n", unit); 299 } 300 else 301 printf("ata-pci%d: Busmastering DMA not configured\n",unit); 302 } 303 else 304 printf("ata-pci%d: Busmastering DMA not enabled\n", unit); 305 } 306 else 307 printf("ata-pci%d: Busmastering DMA not supported\n", unit); 308 } 309 310 /* now probe the addresse found for "real" ATA/ATAPI hardware */ 311 lun = 0;
|
234 if (ata_probe(iobase_1, altiobase_1, bmaddr_1, tag, &lun)) {
| 312 if (ata_probe(iobase_1, altiobase_1, bmaddr_1, dev, &lun)) {
|
235 if (iobase_1 == IO_WD1) 236#ifdef __i386__
| 313 if (iobase_1 == IO_WD1) 314#ifdef __i386__
|
237 register_intr(irq1, (int)"", 0, (inthand2_t *)ataintr, 238 &bio_imask, lun); 239#else
| 315 register_intr(irq1,(int)"",0,(inthand2_t *)ataintr,&bio_imask,lun); 316#endif 317#ifdef __alpha__
|
240 alpha_platform_setup_ide_intr(0, ataintr, (void *)(intptr_t)lun); 241#endif 242 else {
| 318 alpha_platform_setup_ide_intr(0, ataintr, (void *)(intptr_t)lun); 319#endif 320 else {
|
| 321 struct resource *irq; 322 int rid = 0; 323 void *ih; 324 325 irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0,1,RF_ACTIVE);
|
243 if (sysctrl)
| 326 if (sysctrl)
|
244 pci_map_int(tag, (inthand2_t *)promise_intr, 245 (void *)(intptr_t)lun, &bio_imask);
| 327 bus_setup_intr(dev, irq, (driver_intr_t *)promise_intr, 328 (void *)lun, &ih);
|
246 else
| 329 else
|
247 pci_map_int(tag, (inthand2_t *)ataintr, (void *)(intptr_t)lun,&bio_imask);
| 330 bus_setup_intr(dev, irq, (driver_intr_t *)ataintr, 331 (void *)lun, &ih);
|
248 } 249 printf("ata%d at 0x%04x irq %d on ata-pci%d\n", 250 lun, iobase_1, isa_apic_irq(irq1), unit); 251 } 252 lun = 1;
| 332 } 333 printf("ata%d at 0x%04x irq %d on ata-pci%d\n", 334 lun, iobase_1, isa_apic_irq(irq1), unit); 335 } 336 lun = 1;
|
253 if (ata_probe(iobase_2, altiobase_2, bmaddr_2, tag, &lun)) {
| 337 if (ata_probe(iobase_2, altiobase_2, bmaddr_2, dev, &lun)) {
|
254 if (iobase_2 == IO_WD2) 255#ifdef __i386__
| 338 if (iobase_2 == IO_WD2) 339#ifdef __i386__
|
256 register_intr(irq2, (int)"", 0, (inthand2_t *)ataintr, 257 &bio_imask, lun); 258#else
| 340 register_intr(irq2,(int)"",0,(inthand2_t *)ataintr,&bio_imask,lun); 341#endif 342#ifdef __alpha__
|
259 alpha_platform_setup_ide_intr(1, ataintr, (void *)(intptr_t)lun); 260#endif 261 else {
| 343 alpha_platform_setup_ide_intr(1, ataintr, (void *)(intptr_t)lun); 344#endif 345 else {
|
| 346 struct resource *irq; 347 int rid = 0; 348 void *ih; 349 350 irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0,1,RF_ACTIVE);
|
262 if (!sysctrl)
| 351 if (!sysctrl)
|
263 pci_map_int(tag, (inthand2_t *)ataintr, (void *)(intptr_t)lun,&bio_imask);
| 352 bus_setup_intr(dev, irq, (driver_intr_t *)ataintr, 353 (void *)lun, &ih);
|
264 } 265 printf("ata%d at 0x%04x irq %d on ata-pci%d\n", 266 lun, iobase_2, isa_apic_irq(irq2), unit); 267 }
| 354 } 355 printf("ata%d at 0x%04x irq %d on ata-pci%d\n", 356 lun, iobase_2, isa_apic_irq(irq2), unit); 357 }
|
| 358 return 0;
|
268} 269
| 359} 360
|
| 361static device_method_t ata_pci_methods[] = { 362 /* Device interface */ 363 DEVMETHOD(device_probe, ata_pciprobe), 364 DEVMETHOD(device_attach, ata_pciattach), 365 { 0, 0 } 366}; 367 368static driver_t ata_pci_driver = { 369 "ata", 370 ata_pci_methods, 371 DRIVER_TYPE_BIO, 372 sizeof(int), 373}; 374 375DRIVER_MODULE(ata, pci, ata_pci_driver, ata_devclass, 0, 0); 376
|
270static void 271promise_intr(int32_t unit) 272{ 273 struct ata_softc *scp = atadevices[unit];
| 377static void 378promise_intr(int32_t unit) 379{ 380 struct ata_softc *scp = atadevices[unit];
|
274 int32_t channel = inl((pci_conf_read(scp->tag, 0x20) & 0xfffc) + 0x1c);
| 381 int32_t channel = inl((pci_read_config(scp->dev, 0x20, 4) & 0xfffc) + 0x1c);
|
275 276 if (channel & 0x00000400) 277 ataintr(unit); 278 279 if (channel & 0x00004000) 280 ataintr(unit+1); 281} 282#endif 283 284static int32_t 285ata_probe(int32_t ioaddr, int32_t altioaddr, int32_t bmaddr,
| 382 383 if (channel & 0x00000400) 384 ataintr(unit); 385 386 if (channel & 0x00004000) 387 ataintr(unit+1); 388} 389#endif 390 391static int32_t 392ata_probe(int32_t ioaddr, int32_t altioaddr, int32_t bmaddr,
|
286 pcici_t tag, int32_t *unit)
| 393 device_t dev, int32_t *unit)
|
287{ 288 struct ata_softc *scp = atadevices[atanlun]; 289 int32_t mask = 0; 290 int32_t timeout; 291 int32_t lun = atanlun; 292 u_int8_t status0, status1; 293 294#ifdef ATA_STATIC_ID 295 atanlun++; 296#endif 297 if (lun > MAXATA) { 298 printf("ata: unit out of range(%d)\n", lun); 299 return 0; 300 } 301 if (scp) { 302 printf("ata%d: unit already attached\n", lun); 303 return 0; 304 } 305 scp = malloc(sizeof(struct ata_softc), M_DEVBUF, M_NOWAIT); 306 if (scp == NULL) { 307 printf("ata%d: failed to allocate driver storage\n", lun); 308 return 0; 309 } 310 bzero(scp, sizeof(struct ata_softc)); 311 312 scp->unit = *unit; 313 scp->lun = lun; 314 scp->ioaddr = ioaddr; 315 scp->altioaddr = altioaddr; 316 scp->active = ATA_IDLE; 317 318#ifdef ATA_DEBUG 319 printf("ata%d: iobase=0x%04x altiobase=0x%04x\n", 320 scp->lun, scp->ioaddr, scp->altioaddr); 321#endif 322 323 /* do we have any signs of ATA/ATAPI HW being present ? */ 324 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 325 DELAY(1); 326 status0 = inb(scp->ioaddr + ATA_STATUS); 327 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 328 DELAY(1); 329 status1 = inb(scp->ioaddr + ATA_STATUS); 330 if ((status0 & 0xf8) != 0xf8) 331 mask |= 0x01; 332 if ((status1 & 0xf8) != 0xf8) 333 mask |= 0x02; 334#ifdef ATA_DEBUG 335 printf("ata%d: mask=%02x status0=%02x status1=%02x\n", 336 scp->lun, mask, status0, status1); 337#endif 338 if (!mask) { 339 free(scp, M_DEVBUF); 340 return 0; 341 } 342 /* assert reset for devices and wait for completition */ 343 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 344 DELAY(1); 345 outb(scp->altioaddr, ATA_A_IDS | ATA_A_RESET); 346 DELAY(1000); 347 outb(scp->altioaddr, ATA_A_IDS); 348 DELAY(1000); 349 inb(scp->ioaddr + ATA_ERROR); 350 DELAY(1); 351 outb(scp->altioaddr, ATA_A_4BIT); 352 DELAY(1); 353 354 /* wait for BUSY to go inactive */ 355 for (timeout = 0; timeout < 30000*10; timeout++) { 356 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 357 DELAY(1); 358 status0 = inb(scp->ioaddr + ATA_STATUS); 359 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 360 DELAY(1); 361 status1 = inb(scp->ioaddr + ATA_STATUS); 362 if (mask == 0x01) /* wait for master only */ 363 if (!(status0 & ATA_S_BSY)) 364 break; 365 if (mask == 0x02) /* wait for slave only */ 366 if (!(status1 & ATA_S_BSY)) 367 break; 368 if (mask == 0x03) /* wait for both master & slave */ 369 if (!(status0 & ATA_S_BSY) && !(status1 & ATA_S_BSY)) 370 break; 371 DELAY(100); 372 } 373 if (status0 & ATA_S_BSY) 374 mask &= ~0x01; 375 if (status1 & ATA_S_BSY) 376 mask &= ~0x02; 377#ifdef ATA_DEBUG 378 printf("ata%d: mask=%02x status0=%02x status1=%02x\n", 379 scp->lun, mask, status0, status1); 380#endif 381 if (!mask) { 382 free(scp, M_DEVBUF); 383 return 0; 384 } 385 /* 386 * OK, we have at least one device on the chain, 387 * check for ATAPI signatures, if none check if its 388 * a good old ATA device. 389 */ 390 391 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_MASTER)); 392 DELAY(1); 393 if (inb(scp->ioaddr + ATA_CYL_LSB) == ATAPI_MAGIC_LSB && 394 inb(scp->ioaddr + ATA_CYL_MSB) == ATAPI_MAGIC_MSB) { 395 scp->devices |= ATA_ATAPI_MASTER; 396 } 397 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_SLAVE)); 398 DELAY(1); 399 if (inb(scp->ioaddr + ATA_CYL_LSB) == ATAPI_MAGIC_LSB && 400 inb(scp->ioaddr + ATA_CYL_MSB) == ATAPI_MAGIC_MSB) { 401 scp->devices |= ATA_ATAPI_SLAVE; 402 } 403 if (status0 != 0x00 && !(scp->devices & ATA_ATAPI_MASTER)) { 404 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_MASTER)); 405 DELAY(1); 406 outb(scp->ioaddr + ATA_ERROR, 0x58); 407 outb(scp->ioaddr + ATA_CYL_LSB, 0xa5); 408 if (inb(scp->ioaddr + ATA_ERROR) != 0x58 && 409 inb(scp->ioaddr + ATA_CYL_LSB) == 0xa5) { 410 scp->devices |= ATA_ATA_MASTER; 411 } 412 } 413 if (status1 != 0x00 && !(scp->devices & ATA_ATAPI_SLAVE)) { 414 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_SLAVE)); 415 DELAY(1); 416 outb(scp->ioaddr + ATA_ERROR, 0x58); 417 outb(scp->ioaddr + ATA_CYL_LSB, 0xa5); 418 if (inb(scp->ioaddr + ATA_ERROR) != 0x58 && 419 inb(scp->ioaddr + ATA_CYL_LSB) == 0xa5) { 420 scp->devices |= ATA_ATA_SLAVE; 421 } 422 } 423#ifdef ATA_DEBUG 424 printf("ata%d: devices = 0x%x\n", scp->lun, scp->devices); 425#endif 426 if (!scp->devices) { 427 free(scp, M_DEVBUF); 428 return 0; 429 } 430 bufq_init(&scp->ata_queue); 431 TAILQ_INIT(&scp->atapi_queue); 432 *unit = scp->lun;
| 394{ 395 struct ata_softc *scp = atadevices[atanlun]; 396 int32_t mask = 0; 397 int32_t timeout; 398 int32_t lun = atanlun; 399 u_int8_t status0, status1; 400 401#ifdef ATA_STATIC_ID 402 atanlun++; 403#endif 404 if (lun > MAXATA) { 405 printf("ata: unit out of range(%d)\n", lun); 406 return 0; 407 } 408 if (scp) { 409 printf("ata%d: unit already attached\n", lun); 410 return 0; 411 } 412 scp = malloc(sizeof(struct ata_softc), M_DEVBUF, M_NOWAIT); 413 if (scp == NULL) { 414 printf("ata%d: failed to allocate driver storage\n", lun); 415 return 0; 416 } 417 bzero(scp, sizeof(struct ata_softc)); 418 419 scp->unit = *unit; 420 scp->lun = lun; 421 scp->ioaddr = ioaddr; 422 scp->altioaddr = altioaddr; 423 scp->active = ATA_IDLE; 424 425#ifdef ATA_DEBUG 426 printf("ata%d: iobase=0x%04x altiobase=0x%04x\n", 427 scp->lun, scp->ioaddr, scp->altioaddr); 428#endif 429 430 /* do we have any signs of ATA/ATAPI HW being present ? */ 431 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 432 DELAY(1); 433 status0 = inb(scp->ioaddr + ATA_STATUS); 434 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 435 DELAY(1); 436 status1 = inb(scp->ioaddr + ATA_STATUS); 437 if ((status0 & 0xf8) != 0xf8) 438 mask |= 0x01; 439 if ((status1 & 0xf8) != 0xf8) 440 mask |= 0x02; 441#ifdef ATA_DEBUG 442 printf("ata%d: mask=%02x status0=%02x status1=%02x\n", 443 scp->lun, mask, status0, status1); 444#endif 445 if (!mask) { 446 free(scp, M_DEVBUF); 447 return 0; 448 } 449 /* assert reset for devices and wait for completition */ 450 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 451 DELAY(1); 452 outb(scp->altioaddr, ATA_A_IDS | ATA_A_RESET); 453 DELAY(1000); 454 outb(scp->altioaddr, ATA_A_IDS); 455 DELAY(1000); 456 inb(scp->ioaddr + ATA_ERROR); 457 DELAY(1); 458 outb(scp->altioaddr, ATA_A_4BIT); 459 DELAY(1); 460 461 /* wait for BUSY to go inactive */ 462 for (timeout = 0; timeout < 30000*10; timeout++) { 463 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 464 DELAY(1); 465 status0 = inb(scp->ioaddr + ATA_STATUS); 466 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 467 DELAY(1); 468 status1 = inb(scp->ioaddr + ATA_STATUS); 469 if (mask == 0x01) /* wait for master only */ 470 if (!(status0 & ATA_S_BSY)) 471 break; 472 if (mask == 0x02) /* wait for slave only */ 473 if (!(status1 & ATA_S_BSY)) 474 break; 475 if (mask == 0x03) /* wait for both master & slave */ 476 if (!(status0 & ATA_S_BSY) && !(status1 & ATA_S_BSY)) 477 break; 478 DELAY(100); 479 } 480 if (status0 & ATA_S_BSY) 481 mask &= ~0x01; 482 if (status1 & ATA_S_BSY) 483 mask &= ~0x02; 484#ifdef ATA_DEBUG 485 printf("ata%d: mask=%02x status0=%02x status1=%02x\n", 486 scp->lun, mask, status0, status1); 487#endif 488 if (!mask) { 489 free(scp, M_DEVBUF); 490 return 0; 491 } 492 /* 493 * OK, we have at least one device on the chain, 494 * check for ATAPI signatures, if none check if its 495 * a good old ATA device. 496 */ 497 498 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_MASTER)); 499 DELAY(1); 500 if (inb(scp->ioaddr + ATA_CYL_LSB) == ATAPI_MAGIC_LSB && 501 inb(scp->ioaddr + ATA_CYL_MSB) == ATAPI_MAGIC_MSB) { 502 scp->devices |= ATA_ATAPI_MASTER; 503 } 504 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_SLAVE)); 505 DELAY(1); 506 if (inb(scp->ioaddr + ATA_CYL_LSB) == ATAPI_MAGIC_LSB && 507 inb(scp->ioaddr + ATA_CYL_MSB) == ATAPI_MAGIC_MSB) { 508 scp->devices |= ATA_ATAPI_SLAVE; 509 } 510 if (status0 != 0x00 && !(scp->devices & ATA_ATAPI_MASTER)) { 511 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_MASTER)); 512 DELAY(1); 513 outb(scp->ioaddr + ATA_ERROR, 0x58); 514 outb(scp->ioaddr + ATA_CYL_LSB, 0xa5); 515 if (inb(scp->ioaddr + ATA_ERROR) != 0x58 && 516 inb(scp->ioaddr + ATA_CYL_LSB) == 0xa5) { 517 scp->devices |= ATA_ATA_MASTER; 518 } 519 } 520 if (status1 != 0x00 && !(scp->devices & ATA_ATAPI_SLAVE)) { 521 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_SLAVE)); 522 DELAY(1); 523 outb(scp->ioaddr + ATA_ERROR, 0x58); 524 outb(scp->ioaddr + ATA_CYL_LSB, 0xa5); 525 if (inb(scp->ioaddr + ATA_ERROR) != 0x58 && 526 inb(scp->ioaddr + ATA_CYL_LSB) == 0xa5) { 527 scp->devices |= ATA_ATA_SLAVE; 528 } 529 } 530#ifdef ATA_DEBUG 531 printf("ata%d: devices = 0x%x\n", scp->lun, scp->devices); 532#endif 533 if (!scp->devices) { 534 free(scp, M_DEVBUF); 535 return 0; 536 } 537 bufq_init(&scp->ata_queue); 538 TAILQ_INIT(&scp->atapi_queue); 539 *unit = scp->lun;
|
433 scp->tag = tag;
| 540 scp->dev = dev;
|
434 if (bmaddr) 435 scp->bmaddr = bmaddr; 436 atadevices[scp->lun] = scp; 437#ifndef ATA_STATIC_ID 438 atanlun++; 439#endif
| 541 if (bmaddr) 542 scp->bmaddr = bmaddr; 543 atadevices[scp->lun] = scp; 544#ifndef ATA_STATIC_ID 545 atanlun++; 546#endif
|
440 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
| |
441 return ATA_IOSIZE; 442} 443 444static void 445ataintr(int32_t unit) 446{ 447 struct ata_softc *scp; 448 struct atapi_request *atapi_request; 449 struct buf *ata_request; 450 u_int8_t status; 451 static int32_t intr_count = 0; 452 453 if (unit < 0 || unit > atanlun) { 454 printf("ataintr: unit %d unusable\n", unit); 455 return; 456 } 457 458 scp = atadevices[unit]; 459 460 /* find & call the responsible driver to process this interrupt */ 461 switch (scp->active) { 462#if NATADISK > 0 463 case ATA_ACTIVE_ATA: 464 if ((ata_request = bufq_first(&scp->ata_queue))) 465 if (ad_interrupt(ata_request) == ATA_OP_CONTINUES) 466 return; 467 break; 468#endif 469 case ATA_ACTIVE_ATAPI: 470 if ((atapi_request = TAILQ_FIRST(&scp->atapi_queue))) 471 if (atapi_interrupt(atapi_request) == ATA_OP_CONTINUES) 472 return; 473 break; 474 475 case ATA_WAIT_INTR: 476 wakeup((caddr_t)scp); 477 break; 478 479 case ATA_IGNORE_INTR: 480 break; 481 482 default: 483 case ATA_IDLE: 484 status = inb(scp->ioaddr + ATA_STATUS); 485 if (intr_count++ < 10) 486 printf("ata%d: unwanted interrupt %d status = %02x\n", 487 unit, intr_count, status); 488 return; 489 } 490 scp->active = ATA_IDLE; 491 ata_start(scp); 492} 493 494void 495ata_start(struct ata_softc *scp) 496{ 497 struct buf *ata_request; 498 struct atapi_request *atapi_request; 499 500#ifdef ATA_DEBUG 501 printf("ata_start: entered\n"); 502#endif 503 if (scp->active != ATA_IDLE) { 504 printf("ata: unwanted ata_start\n"); 505 return; 506 } 507 508#if NATADISK > 0 509 /* find & call the responsible driver if anything on ATA queue */ 510 if ((ata_request = bufq_first(&scp->ata_queue))) { 511 scp->active = ATA_ACTIVE_ATA; 512 ad_transfer(ata_request); 513#ifdef ATA_DEBUG 514 printf("ata_start: started ata, leaving\n"); 515#endif 516 return; 517 } 518#endif 519 520 /* find & call the responsible driver if anything on ATAPI queue */ 521 if ((atapi_request = TAILQ_FIRST(&scp->atapi_queue))) { 522 scp->active = ATA_ACTIVE_ATAPI; 523 atapi_transfer(atapi_request); 524#ifdef ATA_DEBUG 525 printf("ata_start: started atapi, leaving\n"); 526#endif 527 return; 528 } 529} 530 531int32_t 532ata_wait(struct ata_softc *scp, int32_t device, u_int8_t mask) 533{ 534 u_int8_t status; 535 u_int32_t timeout = 0; 536 537 while (timeout++ <= 500000) { /* timeout 5 secs */ 538 status = inb(scp->ioaddr + ATA_STATUS); 539 540 /* if drive fails status, reselect the drive just to be sure */ 541 if (status == 0xff) { 542 printf("ata%d: %s: no status, reselecting device\n", 543 scp->lun, device?"slave":"master"); 544 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device); 545 DELAY(1); 546 status = inb(scp->ioaddr + ATA_STATUS); 547 } 548 if (status == 0xff) 549 return -1; 550 scp->status = status; 551 if (!(status & ATA_S_BSY)) { 552 if (status & ATA_S_ERROR) 553 scp->error = inb(scp->ioaddr + ATA_ERROR); 554 if ((status & mask) == mask) 555 return (status & ATA_S_ERROR); 556 } 557 if (timeout > 1000) 558 DELAY(1000); 559 else 560 DELAY(10); 561 } 562 return -1; 563} 564 565int32_t 566ata_command(struct ata_softc *scp, int32_t device, u_int32_t command, 567 u_int32_t cylinder, u_int32_t head, u_int32_t sector, 568 u_int32_t count, u_int32_t feature, int32_t flags) 569{ 570#ifdef ATA_DEBUG 571printf("ata_command: addr=%04x, device=%02x, cmd=%02x, c=%d, h=%d, s=%d, count=%d, flags=%02x\n", scp->ioaddr, device, command, cylinder, head, sector, count, flags); 572#endif 573 574 /* ready to issue command ? */ 575 if (ata_wait(scp, device, 0) < 0) { 576 printf("ata%d: %s: timeout waiting to give command s=%02x e=%02x\n", 577 scp->lun, device?"slave":"master", scp->status, scp->error); 578 } 579 outb(scp->ioaddr + ATA_FEATURE, feature); 580 outb(scp->ioaddr + ATA_CYL_LSB, cylinder); 581 outb(scp->ioaddr + ATA_CYL_MSB, cylinder >> 8); 582 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device | head); 583 outb(scp->ioaddr + ATA_SECTOR, sector); 584 outb(scp->ioaddr + ATA_COUNT, count); 585 586 if (scp->active != ATA_IDLE && flags != ATA_IMMEDIATE) 587 printf("DANGER active=%d\n", scp->active); 588 589 switch (flags) { 590 case ATA_WAIT_INTR: 591 scp->active = ATA_WAIT_INTR; 592 outb(scp->ioaddr + ATA_CMD, command); 593 if (tsleep((caddr_t)scp, PRIBIO, "atacmd", 500)) { 594 printf("ata_command: timeout waiting for interrupt\n"); 595 scp->active = ATA_IDLE; 596 return -1; 597 } 598 break; 599 600 case ATA_IGNORE_INTR: 601 scp->active = ATA_IGNORE_INTR; 602 outb(scp->ioaddr + ATA_CMD, command); 603 break; 604 605 case ATA_IMMEDIATE: 606 default: 607 outb(scp->ioaddr + ATA_CMD, command); 608 break; 609 } 610#ifdef ATA_DEBUG 611printf("ata_command: leaving\n"); 612#endif 613 return 0; 614} 615 616void 617bswap(int8_t *buf, int32_t len) 618{ 619 u_int16_t *p = (u_int16_t*)(buf + len); 620 621 while (--p >= (u_int16_t*)buf) 622 *p = ntohs(*p); 623} 624 625void 626btrim(int8_t *buf, int32_t len) 627{ 628 int8_t *p; 629 630 for (p = buf; p < buf+len; ++p) 631 if (!*p) 632 *p = ' '; 633 for (p = buf + len - 1; p >= buf && *p == ' '; --p) 634 *p = 0; 635} 636 637void 638bpack(int8_t *src, int8_t *dst, int32_t len) 639{ 640 int32_t i, j, blank; 641 642 for (i = j = blank = 0 ; i < len-1; i++) { 643 if (blank && src[i] == ' ') continue; 644 if (blank && src[i] != ' ') { 645 dst[j++] = src[i]; 646 blank = 0; 647 continue; 648 } 649 if (src[i] == ' ') 650 blank = 1; 651 dst[j++] = src[i]; 652 } 653 dst[j] = 0x00; 654} 655#endif /* NATA > 0 */
| 547 return ATA_IOSIZE; 548} 549 550static void 551ataintr(int32_t unit) 552{ 553 struct ata_softc *scp; 554 struct atapi_request *atapi_request; 555 struct buf *ata_request; 556 u_int8_t status; 557 static int32_t intr_count = 0; 558 559 if (unit < 0 || unit > atanlun) { 560 printf("ataintr: unit %d unusable\n", unit); 561 return; 562 } 563 564 scp = atadevices[unit]; 565 566 /* find & call the responsible driver to process this interrupt */ 567 switch (scp->active) { 568#if NATADISK > 0 569 case ATA_ACTIVE_ATA: 570 if ((ata_request = bufq_first(&scp->ata_queue))) 571 if (ad_interrupt(ata_request) == ATA_OP_CONTINUES) 572 return; 573 break; 574#endif 575 case ATA_ACTIVE_ATAPI: 576 if ((atapi_request = TAILQ_FIRST(&scp->atapi_queue))) 577 if (atapi_interrupt(atapi_request) == ATA_OP_CONTINUES) 578 return; 579 break; 580 581 case ATA_WAIT_INTR: 582 wakeup((caddr_t)scp); 583 break; 584 585 case ATA_IGNORE_INTR: 586 break; 587 588 default: 589 case ATA_IDLE: 590 status = inb(scp->ioaddr + ATA_STATUS); 591 if (intr_count++ < 10) 592 printf("ata%d: unwanted interrupt %d status = %02x\n", 593 unit, intr_count, status); 594 return; 595 } 596 scp->active = ATA_IDLE; 597 ata_start(scp); 598} 599 600void 601ata_start(struct ata_softc *scp) 602{ 603 struct buf *ata_request; 604 struct atapi_request *atapi_request; 605 606#ifdef ATA_DEBUG 607 printf("ata_start: entered\n"); 608#endif 609 if (scp->active != ATA_IDLE) { 610 printf("ata: unwanted ata_start\n"); 611 return; 612 } 613 614#if NATADISK > 0 615 /* find & call the responsible driver if anything on ATA queue */ 616 if ((ata_request = bufq_first(&scp->ata_queue))) { 617 scp->active = ATA_ACTIVE_ATA; 618 ad_transfer(ata_request); 619#ifdef ATA_DEBUG 620 printf("ata_start: started ata, leaving\n"); 621#endif 622 return; 623 } 624#endif 625 626 /* find & call the responsible driver if anything on ATAPI queue */ 627 if ((atapi_request = TAILQ_FIRST(&scp->atapi_queue))) { 628 scp->active = ATA_ACTIVE_ATAPI; 629 atapi_transfer(atapi_request); 630#ifdef ATA_DEBUG 631 printf("ata_start: started atapi, leaving\n"); 632#endif 633 return; 634 } 635} 636 637int32_t 638ata_wait(struct ata_softc *scp, int32_t device, u_int8_t mask) 639{ 640 u_int8_t status; 641 u_int32_t timeout = 0; 642 643 while (timeout++ <= 500000) { /* timeout 5 secs */ 644 status = inb(scp->ioaddr + ATA_STATUS); 645 646 /* if drive fails status, reselect the drive just to be sure */ 647 if (status == 0xff) { 648 printf("ata%d: %s: no status, reselecting device\n", 649 scp->lun, device?"slave":"master"); 650 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device); 651 DELAY(1); 652 status = inb(scp->ioaddr + ATA_STATUS); 653 } 654 if (status == 0xff) 655 return -1; 656 scp->status = status; 657 if (!(status & ATA_S_BSY)) { 658 if (status & ATA_S_ERROR) 659 scp->error = inb(scp->ioaddr + ATA_ERROR); 660 if ((status & mask) == mask) 661 return (status & ATA_S_ERROR); 662 } 663 if (timeout > 1000) 664 DELAY(1000); 665 else 666 DELAY(10); 667 } 668 return -1; 669} 670 671int32_t 672ata_command(struct ata_softc *scp, int32_t device, u_int32_t command, 673 u_int32_t cylinder, u_int32_t head, u_int32_t sector, 674 u_int32_t count, u_int32_t feature, int32_t flags) 675{ 676#ifdef ATA_DEBUG 677printf("ata_command: addr=%04x, device=%02x, cmd=%02x, c=%d, h=%d, s=%d, count=%d, flags=%02x\n", scp->ioaddr, device, command, cylinder, head, sector, count, flags); 678#endif 679 680 /* ready to issue command ? */ 681 if (ata_wait(scp, device, 0) < 0) { 682 printf("ata%d: %s: timeout waiting to give command s=%02x e=%02x\n", 683 scp->lun, device?"slave":"master", scp->status, scp->error); 684 } 685 outb(scp->ioaddr + ATA_FEATURE, feature); 686 outb(scp->ioaddr + ATA_CYL_LSB, cylinder); 687 outb(scp->ioaddr + ATA_CYL_MSB, cylinder >> 8); 688 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device | head); 689 outb(scp->ioaddr + ATA_SECTOR, sector); 690 outb(scp->ioaddr + ATA_COUNT, count); 691 692 if (scp->active != ATA_IDLE && flags != ATA_IMMEDIATE) 693 printf("DANGER active=%d\n", scp->active); 694 695 switch (flags) { 696 case ATA_WAIT_INTR: 697 scp->active = ATA_WAIT_INTR; 698 outb(scp->ioaddr + ATA_CMD, command); 699 if (tsleep((caddr_t)scp, PRIBIO, "atacmd", 500)) { 700 printf("ata_command: timeout waiting for interrupt\n"); 701 scp->active = ATA_IDLE; 702 return -1; 703 } 704 break; 705 706 case ATA_IGNORE_INTR: 707 scp->active = ATA_IGNORE_INTR; 708 outb(scp->ioaddr + ATA_CMD, command); 709 break; 710 711 case ATA_IMMEDIATE: 712 default: 713 outb(scp->ioaddr + ATA_CMD, command); 714 break; 715 } 716#ifdef ATA_DEBUG 717printf("ata_command: leaving\n"); 718#endif 719 return 0; 720} 721 722void 723bswap(int8_t *buf, int32_t len) 724{ 725 u_int16_t *p = (u_int16_t*)(buf + len); 726 727 while (--p >= (u_int16_t*)buf) 728 *p = ntohs(*p); 729} 730 731void 732btrim(int8_t *buf, int32_t len) 733{ 734 int8_t *p; 735 736 for (p = buf; p < buf+len; ++p) 737 if (!*p) 738 *p = ' '; 739 for (p = buf + len - 1; p >= buf && *p == ' '; --p) 740 *p = 0; 741} 742 743void 744bpack(int8_t *src, int8_t *dst, int32_t len) 745{ 746 int32_t i, j, blank; 747 748 for (i = j = blank = 0 ; i < len-1; i++) { 749 if (blank && src[i] == ' ') continue; 750 if (blank && src[i] != ' ') { 751 dst[j++] = src[i]; 752 blank = 0; 753 continue; 754 } 755 if (src[i] == ' ') 756 blank = 1; 757 dst[j++] = src[i]; 758 } 759 dst[j] = 0x00; 760} 761#endif /* NATA > 0 */
|