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 * $FreeBSD: head/sys/dev/ata/ata-all.c 50842 1999-09-03 10:57:17Z phk $
| 28 * $FreeBSD: head/sys/dev/ata/ata-all.c 51520 1999-09-21 19:50:40Z sos $
|
29 */ 30 31#include "ata.h"
| 29 */ 30 31#include "ata.h"
|
| 32#include "apm.h" 33
|
32#if NATA > 0
| 34#if NATA > 0
|
| 35
|
33#include "isa.h" 34#include "pci.h" 35#include "atadisk.h"
| 36#include "isa.h" 37#include "pci.h" 38#include "atadisk.h"
|
| 39#include "atapicd.h" 40#include "atapifd.h" 41#include "atapist.h"
|
36#include "opt_global.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 "opt_global.h" 43#include <sys/param.h> 44#include <sys/systm.h> 45#include <sys/kernel.h> 46#include <sys/interrupt.h> 47#include <sys/conf.h>
|
| 48#include <sys/disk.h>
|
42#include <sys/module.h> 43#include <sys/bus.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>
| 49#include <sys/module.h> 50#include <sys/bus.h> 51#include <sys/buf.h> 52#include <sys/malloc.h> 53#include <sys/devicestat.h> 54#include <vm/vm.h> 55#include <vm/pmap.h> 56#include <machine/resource.h> 57#include <machine/bus.h> 58#include <sys/rman.h>
|
52#include <machine/clock.h> 53#ifdef __i386__ 54#include <machine/smp.h> 55#include <i386/isa/intr_machdep.h> 56#endif
| |
57#if NPCI > 0 58#include <pci/pcivar.h> 59#include <pci/pcireg.h> 60#endif 61#include <isa/isavar.h> 62#include <isa/isareg.h>
| 59#if NPCI > 0 60#include <pci/pcivar.h> 61#include <pci/pcireg.h> 62#endif 63#include <isa/isavar.h> 64#include <isa/isareg.h>
|
| 65#include <machine/clock.h> 66#ifdef __i386__ 67#include <machine/smp.h> 68#include <i386/isa/intr_machdep.h> 69#endif 70#if NAPM > 0 71#include <machine/apm_bios.h> 72#endif
|
63#include <dev/ata/ata-all.h>
| 73#include <dev/ata/ata-all.h>
|
| 74#include <dev/ata/ata-disk.h>
|
64#include <dev/ata/atapi-all.h> 65 66/* misc defines */
| 75#include <dev/ata/atapi-all.h> 76 77/* misc defines */
|
67#define UNIT(dev) (dev>>3 & 0x1f) /* assume 8 minor # per unit */ 68#define MIN(a,b) ((a)>(b)?(b):(a))
| |
69#if SMP == 0
| 78#if SMP == 0
|
70#define isa_apic_irq(x) x
| 79#define isa_apic_irq(x) x
|
71#endif 72 73/* prototypes */ 74#if NPCI > 0 75static void promise_intr(void *); 76#endif 77static int32_t ata_probe(int32_t, int32_t, int32_t, device_t, int32_t *); 78static void ataintr(void *); 79 80static int32_t atanlun = 0; 81struct ata_softc *atadevices[MAXATA]; 82static devclass_t ata_devclass; 83 84#if NISA > 0 85static struct isa_pnp_id ata_ids[] = { 86 {0x0006d041, "Generic ESDI/IDE/ATA controller"}, /* PNP0600 */ 87 {0x0106d041, "Plus Hardcard II"}, /* PNP0601 */ 88 {0x0206d041, "Plus Hardcard IIXL/EZ"}, /* PNP0602 */ 89 {0x0306d041, "Generic ATA"}, /* PNP0603 */ 90 {0} 91}; 92 93static int 94ata_isaprobe(device_t dev) 95{ 96 struct resource *port; 97 int rid; 98 int32_t ctlr, res; 99 int32_t lun; 100 101 /* Check isapnp ids */ 102 if (ISA_PNP_PROBE(device_get_parent(dev), dev, ata_ids) == ENXIO) 103 return (ENXIO); 104 105 /* Allocate the port range */ 106 rid = 0; 107 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE); 108 if (!port) 109 return (ENOMEM); 110 111 /* check if allready in use by a PCI device */ 112 for (ctlr = 0; ctlr < atanlun; ctlr++) { 113 if (atadevices[ctlr]->ioaddr == rman_get_start(port)) { 114 printf("ata-isa%d: already registered as ata%d\n", 115 device_get_unit(dev), ctlr); 116 bus_release_resource(dev, SYS_RES_IOPORT, 0, port); 117 return ENXIO; 118 } 119 } 120 121 lun = 0; 122 res = ata_probe(rman_get_start(port), rman_get_start(port) + ATA_ALTPORT, 123 0, dev, &lun); 124 125 bus_release_resource(dev, SYS_RES_IOPORT, 0, port); 126 127 if (res) { 128 isa_set_portsize(dev, res); 129 *(int *)device_get_softc(dev) = lun; 130 return 0; 131 }
| 80#endif 81 82/* prototypes */ 83#if NPCI > 0 84static void promise_intr(void *); 85#endif 86static int32_t ata_probe(int32_t, int32_t, int32_t, device_t, int32_t *); 87static void ataintr(void *); 88 89static int32_t atanlun = 0; 90struct ata_softc *atadevices[MAXATA]; 91static devclass_t ata_devclass; 92 93#if NISA > 0 94static struct isa_pnp_id ata_ids[] = { 95 {0x0006d041, "Generic ESDI/IDE/ATA controller"}, /* PNP0600 */ 96 {0x0106d041, "Plus Hardcard II"}, /* PNP0601 */ 97 {0x0206d041, "Plus Hardcard IIXL/EZ"}, /* PNP0602 */ 98 {0x0306d041, "Generic ATA"}, /* PNP0603 */ 99 {0} 100}; 101 102static int 103ata_isaprobe(device_t dev) 104{ 105 struct resource *port; 106 int rid; 107 int32_t ctlr, res; 108 int32_t lun; 109 110 /* Check isapnp ids */ 111 if (ISA_PNP_PROBE(device_get_parent(dev), dev, ata_ids) == ENXIO) 112 return (ENXIO); 113 114 /* Allocate the port range */ 115 rid = 0; 116 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE); 117 if (!port) 118 return (ENOMEM); 119 120 /* check if allready in use by a PCI device */ 121 for (ctlr = 0; ctlr < atanlun; ctlr++) { 122 if (atadevices[ctlr]->ioaddr == rman_get_start(port)) { 123 printf("ata-isa%d: already registered as ata%d\n", 124 device_get_unit(dev), ctlr); 125 bus_release_resource(dev, SYS_RES_IOPORT, 0, port); 126 return ENXIO; 127 } 128 } 129 130 lun = 0; 131 res = ata_probe(rman_get_start(port), rman_get_start(port) + ATA_ALTPORT, 132 0, dev, &lun); 133 134 bus_release_resource(dev, SYS_RES_IOPORT, 0, port); 135 136 if (res) { 137 isa_set_portsize(dev, res); 138 *(int *)device_get_softc(dev) = lun; 139 return 0; 140 }
|
132
| |
133 return ENXIO; 134} 135 136static int 137ata_isaattach(device_t dev) 138{ 139 struct resource *port; 140 struct resource *irq; 141 void *ih; 142 int rid; 143 144 /* Allocate the port range and interrupt */ 145 rid = 0; 146 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE); 147 if (!port) 148 return (ENOMEM); 149 150 rid = 0; 151 irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, RF_ACTIVE); 152 if (!irq) { 153 bus_release_resource(dev, SYS_RES_IOPORT, 0, port); 154 return (ENOMEM); 155 } 156 return bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr, 157 atadevices[*(int *)device_get_softc(dev)], &ih); 158} 159 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 } 165}; 166 167static driver_t ata_isa_driver = { 168 "ata", 169 ata_isa_methods, 170 sizeof(int), 171}; 172 173DRIVER_MODULE(ata, isa, ata_isa_driver, ata_devclass, 0, 0);
| 141 return ENXIO; 142} 143 144static int 145ata_isaattach(device_t dev) 146{ 147 struct resource *port; 148 struct resource *irq; 149 void *ih; 150 int rid; 151 152 /* Allocate the port range and interrupt */ 153 rid = 0; 154 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE); 155 if (!port) 156 return (ENOMEM); 157 158 rid = 0; 159 irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, RF_ACTIVE); 160 if (!irq) { 161 bus_release_resource(dev, SYS_RES_IOPORT, 0, port); 162 return (ENOMEM); 163 } 164 return bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr, 165 atadevices[*(int *)device_get_softc(dev)], &ih); 166} 167 168static device_method_t ata_isa_methods[] = { 169 /* Device interface */ 170 DEVMETHOD(device_probe, ata_isaprobe), 171 DEVMETHOD(device_attach, ata_isaattach), 172 { 0, 0 } 173}; 174 175static driver_t ata_isa_driver = { 176 "ata", 177 ata_isa_methods, 178 sizeof(int), 179}; 180 181DRIVER_MODULE(ata, isa, ata_isa_driver, ata_devclass, 0, 0);
|
174
| |
175#endif 176 177#if NPCI > 0
| 182#endif 183 184#if NPCI > 0
|
178
| |
179static const char * 180ata_pcimatch(device_t dev) 181{ 182 u_int32_t data; 183 184 data = pci_read_config(dev, PCI_CLASS_REG, 4); 185 if (pci_get_class(dev) == PCIC_STORAGE && 186 (pci_get_subclass(dev) == PCIS_STORAGE_IDE || 187 pci_get_subclass(dev) == PCIS_STORAGE_RAID || 188 pci_get_subclass(dev) == PCIS_STORAGE_OTHER)) { 189 switch (pci_get_devid(dev)) { 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 0x4d38105a: 199 return "Promise Ultra/66 IDE controller"; 200 case 0x522910b9: 201 return "AcerLabs Aladdin IDE controller";
| 185static const char * 186ata_pcimatch(device_t dev) 187{ 188 u_int32_t data; 189 190 data = pci_read_config(dev, PCI_CLASS_REG, 4); 191 if (pci_get_class(dev) == PCIC_STORAGE && 192 (pci_get_subclass(dev) == PCIS_STORAGE_IDE || 193 pci_get_subclass(dev) == PCIS_STORAGE_RAID || 194 pci_get_subclass(dev) == PCIS_STORAGE_OTHER)) { 195 switch (pci_get_devid(dev)) { 196 case 0x12308086: 197 return "Intel PIIX IDE controller"; 198 case 0x70108086: 199 return "Intel PIIX3 IDE controller"; 200 case 0x71118086: 201 return "Intel PIIX4 IDE controller"; 202 case 0x4d33105a: 203 return "Promise Ultra/33 IDE controller"; 204 case 0x4d38105a: 205 return "Promise Ultra/66 IDE controller"; 206 case 0x522910b9: 207 return "AcerLabs Aladdin IDE controller";
|
202#if 0 203 case 0x05711106: 204 return "VIA Apollo IDE controller";
| 208 case 0x05711106: /* 82c586 */ 209 case 0x05961106: /* 82c596 */ 210 return "VIA Apollo IDE controller (generic mode)";
|
205 case 0x06401095:
| 211 case 0x06401095:
|
206 return "CMD 640 IDE controller";
| 212 return "CMD 640 IDE controller (generic mode)";
|
207 case 0x06461095:
| 213 case 0x06461095:
|
208 return "CMD 646 IDE controller";
| 214 return "CMD 646 IDE controller (generic mode)";
|
209 case 0xc6931080:
| 215 case 0xc6931080:
|
210 return "Cypress 82C693 IDE controller";
| 216 return "Cypress 82C693 IDE controller (generic mode)";
|
211 case 0x01021078:
| 217 case 0x01021078:
|
212 return "Cyrix 5530 IDE controller"; 213#endif
| 218 return "Cyrix 5530 IDE controller (generic mode)";
|
214 default:
| 219 default:
|
215 return "Unknown PCI IDE controller (using generic mode)";
| 220 return "Unknown PCI IDE controller (generic mode)";
|
216 } 217 } 218 return NULL; 219} 220 221static int 222ata_pciprobe(device_t dev) 223{ 224 const char *desc = ata_pcimatch(dev); 225 if (desc) { 226 device_set_desc(dev, desc); 227 return 0; 228 } 229 else 230 return ENXIO; 231} 232 233static int 234ata_pciattach(device_t dev) 235{ 236 int unit = device_get_unit(dev); 237 struct ata_softc *scp; 238 u_int32_t type; 239 u_int8_t class, subclass; 240 u_int32_t cmd; 241 int32_t iobase_1, iobase_2, altiobase_1, altiobase_2; 242 int32_t bmaddr_1 = 0, bmaddr_2 = 0, irq1, irq2; 243 int32_t lun; 244 245 /* set up vendor-specific stuff */ 246 type = pci_get_devid(dev); 247 class = pci_get_class(dev); 248 subclass = pci_get_subclass(dev); 249 cmd = pci_read_config(dev, PCIR_COMMAND, 4); 250 251#ifdef ATA_DEBUG 252 printf("ata-pci%d: type=%08x class=%02x subclass=%02x cmd=%08x\n", 253 unit, type, class, subclass, cmd); 254#endif 255 256 /* if this is a Promise controller handle it specially */ 257 if (type == 0x4d33105a || type == 0x4d38105a) { 258 iobase_1 = pci_read_config(dev, 0x10, 4) & 0xfffc; 259 altiobase_1 = pci_read_config(dev, 0x14, 4) & 0xfffc; 260 iobase_2 = pci_read_config(dev, 0x18, 4) & 0xfffc; 261 altiobase_2 = pci_read_config(dev, 0x1c, 4) & 0xfffc; 262 irq1 = irq2 = pci_read_config(dev, PCI_INTERRUPT_REG, 4) & 0xff;
| 221 } 222 } 223 return NULL; 224} 225 226static int 227ata_pciprobe(device_t dev) 228{ 229 const char *desc = ata_pcimatch(dev); 230 if (desc) { 231 device_set_desc(dev, desc); 232 return 0; 233 } 234 else 235 return ENXIO; 236} 237 238static int 239ata_pciattach(device_t dev) 240{ 241 int unit = device_get_unit(dev); 242 struct ata_softc *scp; 243 u_int32_t type; 244 u_int8_t class, subclass; 245 u_int32_t cmd; 246 int32_t iobase_1, iobase_2, altiobase_1, altiobase_2; 247 int32_t bmaddr_1 = 0, bmaddr_2 = 0, irq1, irq2; 248 int32_t lun; 249 250 /* set up vendor-specific stuff */ 251 type = pci_get_devid(dev); 252 class = pci_get_class(dev); 253 subclass = pci_get_subclass(dev); 254 cmd = pci_read_config(dev, PCIR_COMMAND, 4); 255 256#ifdef ATA_DEBUG 257 printf("ata-pci%d: type=%08x class=%02x subclass=%02x cmd=%08x\n", 258 unit, type, class, subclass, cmd); 259#endif 260 261 /* if this is a Promise controller handle it specially */ 262 if (type == 0x4d33105a || type == 0x4d38105a) { 263 iobase_1 = pci_read_config(dev, 0x10, 4) & 0xfffc; 264 altiobase_1 = pci_read_config(dev, 0x14, 4) & 0xfffc; 265 iobase_2 = pci_read_config(dev, 0x18, 4) & 0xfffc; 266 altiobase_2 = pci_read_config(dev, 0x1c, 4) & 0xfffc; 267 irq1 = irq2 = pci_read_config(dev, PCI_INTERRUPT_REG, 4) & 0xff;
|
263 bmaddr_1 = pci_read_config(dev, 0x20, 4) & 0xfffc;
| 268 bmaddr_1 = pci_read_config(dev, 0x20, 4) & 0xfffc;
|
264 bmaddr_2 = bmaddr_1 + ATA_BM_OFFSET1; 265 outb(bmaddr_1 + 0x1f, inb(bmaddr_1 + 0x1f) | 0x01); 266 printf("ata-pci%d: Busmastering DMA supported\n", unit); 267 } 268 /* everybody else seems to do it this way */ 269 else { 270 if ((unit == 0) && 271 (pci_get_progif(dev) & PCIP_STORAGE_IDE_MODEPRIM) == 0) { 272 iobase_1 = IO_WD1; 273 altiobase_1 = iobase_1 + ATA_ALTPORT; 274 irq1 = 14; 275 } 276 else { 277 iobase_1 = pci_read_config(dev, 0x10, 4) & 0xfffc; 278 altiobase_1 = pci_read_config(dev, 0x14, 4) & 0xfffc; 279 irq1 = pci_read_config(dev, PCI_INTERRUPT_REG, 4) & 0xff; 280 } 281 if ((unit == 0) && 282 (pci_get_progif(dev) & PCIP_STORAGE_IDE_MODESEC) == 0) { 283 iobase_2 = IO_WD2; 284 altiobase_2 = iobase_2 + ATA_ALTPORT; 285 irq2 = 15; 286 } 287 else { 288 iobase_2 = pci_read_config(dev, 0x18, 4) & 0xfffc; 289 altiobase_2 = pci_read_config(dev, 0x1c, 4) & 0xfffc; 290 irq2 = pci_read_config(dev, PCI_INTERRUPT_REG, 4) & 0xff; 291 } 292
| 269 bmaddr_2 = bmaddr_1 + ATA_BM_OFFSET1; 270 outb(bmaddr_1 + 0x1f, inb(bmaddr_1 + 0x1f) | 0x01); 271 printf("ata-pci%d: Busmastering DMA supported\n", unit); 272 } 273 /* everybody else seems to do it this way */ 274 else { 275 if ((unit == 0) && 276 (pci_get_progif(dev) & PCIP_STORAGE_IDE_MODEPRIM) == 0) { 277 iobase_1 = IO_WD1; 278 altiobase_1 = iobase_1 + ATA_ALTPORT; 279 irq1 = 14; 280 } 281 else { 282 iobase_1 = pci_read_config(dev, 0x10, 4) & 0xfffc; 283 altiobase_1 = pci_read_config(dev, 0x14, 4) & 0xfffc; 284 irq1 = pci_read_config(dev, PCI_INTERRUPT_REG, 4) & 0xff; 285 } 286 if ((unit == 0) && 287 (pci_get_progif(dev) & PCIP_STORAGE_IDE_MODESEC) == 0) { 288 iobase_2 = IO_WD2; 289 altiobase_2 = iobase_2 + ATA_ALTPORT; 290 irq2 = 15; 291 } 292 else { 293 iobase_2 = pci_read_config(dev, 0x18, 4) & 0xfffc; 294 altiobase_2 = pci_read_config(dev, 0x1c, 4) & 0xfffc; 295 irq2 = pci_read_config(dev, PCI_INTERRUPT_REG, 4) & 0xff; 296 } 297
|
293 /* is this controller busmaster capable ? */ 294 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) {
| 298 /* is this controller busmaster capable ? */ 299 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) {
|
295 /* is busmastering support turned on ? */ 296 if ((pci_read_config(dev, PCI_COMMAND_STATUS_REG, 4) & 5) == 5) {
| 300 /* is busmastering support turned on ? */ 301 if ((pci_read_config(dev, PCI_COMMAND_STATUS_REG, 4) & 5) == 5) {
|
297 /* is there a valid port range to connect to ? */ 298 if ((bmaddr_1 = pci_read_config(dev, 0x20, 4) & 0xfffc)) {
| 302 /* is there a valid port range to connect to ? */ 303 if ((bmaddr_1 = pci_read_config(dev, 0x20, 4) & 0xfffc)) {
|
299 bmaddr_2 = bmaddr_1 + ATA_BM_OFFSET1; 300 printf("ata-pci%d: Busmastering DMA supported\n", unit);
| 304 bmaddr_2 = bmaddr_1 + ATA_BM_OFFSET1; 305 printf("ata-pci%d: Busmastering DMA supported\n", unit);
|
301 } 302 else
| 306 } 307 else
|
303 printf("ata-pci%d: Busmastering DMA not configured\n",unit); 304 } 305 else
| 308 printf("ata-pci%d: Busmastering DMA not configured\n",unit); 309 } 310 else
|
306 printf("ata-pci%d: Busmastering DMA not enabled\n", unit); 307 } 308 else
| 311 printf("ata-pci%d: Busmastering DMA not enabled\n", unit); 312 } 313 else
|
309 printf("ata-pci%d: Busmastering DMA not supported\n", unit); 310 } 311 312 /* now probe the addresse found for "real" ATA/ATAPI hardware */ 313 lun = 0; 314 if (ata_probe(iobase_1, altiobase_1, bmaddr_1, dev, &lun)) { 315 scp = atadevices[lun]; 316 if (iobase_1 == IO_WD1) 317#ifdef __i386__ 318 inthand_add(device_get_nameunit(dev), irq1, ataintr, scp,
| 314 printf("ata-pci%d: Busmastering DMA not supported\n", unit); 315 } 316 317 /* now probe the addresse found for "real" ATA/ATAPI hardware */ 318 lun = 0; 319 if (ata_probe(iobase_1, altiobase_1, bmaddr_1, dev, &lun)) { 320 scp = atadevices[lun]; 321 if (iobase_1 == IO_WD1) 322#ifdef __i386__ 323 inthand_add(device_get_nameunit(dev), irq1, ataintr, scp,
|
319 &bio_imask, INTR_EXCL);
| 324 &bio_imask, INTR_EXCL);
|
320#endif 321#ifdef __alpha__ 322 alpha_platform_setup_ide_intr(0, ataintr, scp); 323#endif 324 else { 325 struct resource *irq; 326 int rid = 0; 327 void *ih; 328 329 irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 330 RF_SHAREABLE | RF_ACTIVE); 331 if (!irq) 332 printf("ata_pciattach: Unable to alloc interrupt\n"); 333
| 325#endif 326#ifdef __alpha__ 327 alpha_platform_setup_ide_intr(0, ataintr, scp); 328#endif 329 else { 330 struct resource *irq; 331 int rid = 0; 332 void *ih; 333 334 irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 335 RF_SHAREABLE | RF_ACTIVE); 336 if (!irq) 337 printf("ata_pciattach: Unable to alloc interrupt\n"); 338
|
334 if (type == 0x4d33105a || type == 0x4d38105a)
| 339 if (type == 0x4d33105a || type == 0x4d38105a)
|
335 bus_setup_intr(dev, irq, INTR_TYPE_BIO, promise_intr, scp, &ih); 336 else 337 bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr, scp, &ih); 338 } 339 printf("ata%d at 0x%04x irq %d on ata-pci%d\n", 340 lun, iobase_1, isa_apic_irq(irq1), unit); 341 } 342 lun = 1; 343 if (ata_probe(iobase_2, altiobase_2, bmaddr_2, dev, &lun)) { 344 scp = atadevices[lun]; 345 if (iobase_2 == IO_WD2) 346#ifdef __i386__ 347 inthand_add(device_get_nameunit(dev), irq2, ataintr, scp,
| 340 bus_setup_intr(dev, irq, INTR_TYPE_BIO, promise_intr, scp, &ih); 341 else 342 bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr, scp, &ih); 343 } 344 printf("ata%d at 0x%04x irq %d on ata-pci%d\n", 345 lun, iobase_1, isa_apic_irq(irq1), unit); 346 } 347 lun = 1; 348 if (ata_probe(iobase_2, altiobase_2, bmaddr_2, dev, &lun)) { 349 scp = atadevices[lun]; 350 if (iobase_2 == IO_WD2) 351#ifdef __i386__ 352 inthand_add(device_get_nameunit(dev), irq2, ataintr, scp,
|
348 &bio_imask, INTR_EXCL);
| 353 &bio_imask, INTR_EXCL);
|
349#endif 350#ifdef __alpha__ 351 alpha_platform_setup_ide_intr(1, ataintr, scp); 352#endif 353 else { 354 struct resource *irq; 355 int rid = 0; 356 void *ih; 357
| 354#endif 355#ifdef __alpha__ 356 alpha_platform_setup_ide_intr(1, ataintr, scp); 357#endif 358 else { 359 struct resource *irq; 360 int rid = 0; 361 void *ih; 362
|
358 if (type != 0x4d33105a && type != 0x4d38105a) { 359 irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
| 363 if (type != 0x4d33105a && type != 0x4d38105a) { 364 irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
|
360 RF_SHAREABLE | RF_ACTIVE);
| 365 RF_SHAREABLE | RF_ACTIVE);
|
361 if (!irq)
| 366 if (!irq)
|
362 printf("ata_pciattach: Unable to alloc interrupt\n"); 363 364 bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr, scp, &ih); 365 } 366 } 367 printf("ata%d at 0x%04x irq %d on ata-pci%d\n", 368 lun, iobase_2, isa_apic_irq(irq2), unit); 369 } 370 return 0; 371} 372 373static device_method_t ata_pci_methods[] = { 374 /* Device interface */ 375 DEVMETHOD(device_probe, ata_pciprobe), 376 DEVMETHOD(device_attach, ata_pciattach), 377 { 0, 0 } 378}; 379 380static driver_t ata_pci_driver = { 381 "ata-pci", 382 ata_pci_methods, 383 sizeof(int), 384}; 385 386DRIVER_MODULE(ata, pci, ata_pci_driver, ata_devclass, 0, 0); 387 388static void 389promise_intr(void *data) 390{ 391 struct ata_softc *scp = (struct ata_softc *)data; 392 int32_t channel = inl((pci_read_config(scp->dev, 0x20, 4) & 0xfffc) + 0x1c); 393 394 if (channel & 0x00000400) 395 ataintr(data); 396 397 if (channel & 0x00004000) 398 ataintr(atadevices[scp->lun + 1]); 399} 400#endif 401 402static int32_t 403ata_probe(int32_t ioaddr, int32_t altioaddr, int32_t bmaddr, 404 device_t dev, int32_t *unit) 405{ 406 struct ata_softc *scp = atadevices[atanlun]; 407 int32_t mask = 0;
| 367 printf("ata_pciattach: Unable to alloc interrupt\n"); 368 369 bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr, scp, &ih); 370 } 371 } 372 printf("ata%d at 0x%04x irq %d on ata-pci%d\n", 373 lun, iobase_2, isa_apic_irq(irq2), unit); 374 } 375 return 0; 376} 377 378static device_method_t ata_pci_methods[] = { 379 /* Device interface */ 380 DEVMETHOD(device_probe, ata_pciprobe), 381 DEVMETHOD(device_attach, ata_pciattach), 382 { 0, 0 } 383}; 384 385static driver_t ata_pci_driver = { 386 "ata-pci", 387 ata_pci_methods, 388 sizeof(int), 389}; 390 391DRIVER_MODULE(ata, pci, ata_pci_driver, ata_devclass, 0, 0); 392 393static void 394promise_intr(void *data) 395{ 396 struct ata_softc *scp = (struct ata_softc *)data; 397 int32_t channel = inl((pci_read_config(scp->dev, 0x20, 4) & 0xfffc) + 0x1c); 398 399 if (channel & 0x00000400) 400 ataintr(data); 401 402 if (channel & 0x00004000) 403 ataintr(atadevices[scp->lun + 1]); 404} 405#endif 406 407static int32_t 408ata_probe(int32_t ioaddr, int32_t altioaddr, int32_t bmaddr, 409 device_t dev, int32_t *unit) 410{ 411 struct ata_softc *scp = atadevices[atanlun]; 412 int32_t mask = 0;
|
408 int32_t timeout;
| |
409 int32_t lun = atanlun; 410 u_int8_t status0, status1; 411 412#ifdef ATA_STATIC_ID 413 atanlun++; 414#endif 415 if (lun > MAXATA) { 416 printf("ata: unit out of range(%d)\n", lun); 417 return 0; 418 } 419 if (scp) { 420 printf("ata%d: unit already attached\n", lun); 421 return 0; 422 } 423 scp = malloc(sizeof(struct ata_softc), M_DEVBUF, M_NOWAIT); 424 if (scp == NULL) { 425 printf("ata%d: failed to allocate driver storage\n", lun); 426 return 0; 427 } 428 bzero(scp, sizeof(struct ata_softc)); 429 430 scp->unit = *unit; 431 scp->lun = lun; 432 scp->ioaddr = ioaddr; 433 scp->altioaddr = altioaddr; 434 scp->active = ATA_IDLE; 435
| 413 int32_t lun = atanlun; 414 u_int8_t status0, status1; 415 416#ifdef ATA_STATIC_ID 417 atanlun++; 418#endif 419 if (lun > MAXATA) { 420 printf("ata: unit out of range(%d)\n", lun); 421 return 0; 422 } 423 if (scp) { 424 printf("ata%d: unit already attached\n", lun); 425 return 0; 426 } 427 scp = malloc(sizeof(struct ata_softc), M_DEVBUF, M_NOWAIT); 428 if (scp == NULL) { 429 printf("ata%d: failed to allocate driver storage\n", lun); 430 return 0; 431 } 432 bzero(scp, sizeof(struct ata_softc)); 433 434 scp->unit = *unit; 435 scp->lun = lun; 436 scp->ioaddr = ioaddr; 437 scp->altioaddr = altioaddr; 438 scp->active = ATA_IDLE; 439
|
436#ifdef ATA_DEBUG 437 printf("ata%d: iobase=0x%04x altiobase=0x%04x\n", 438 scp->lun, scp->ioaddr, scp->altioaddr); 439#endif
| 440 if (bootverbose) 441 printf("ata%d: iobase=0x%04x altiobase=0x%04x\n", 442 scp->lun, scp->ioaddr, scp->altioaddr);
|
440
| 443
|
| 444
|
441 /* do we have any signs of ATA/ATAPI HW being present ? */ 442 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 443 DELAY(1); 444 status0 = inb(scp->ioaddr + ATA_STATUS); 445 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
| 445 /* do we have any signs of ATA/ATAPI HW being present ? */ 446 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 447 DELAY(1); 448 status0 = inb(scp->ioaddr + ATA_STATUS); 449 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
|
446 DELAY(1);
| 450 DELAY(1);
|
447 status1 = inb(scp->ioaddr + ATA_STATUS); 448 if ((status0 & 0xf8) != 0xf8)
| 451 status1 = inb(scp->ioaddr + ATA_STATUS); 452 if ((status0 & 0xf8) != 0xf8)
|
449 mask |= 0x01;
| 453 mask |= 0x01;
|
450 if ((status1 & 0xf8) != 0xf8)
| 454 if ((status1 & 0xf8) != 0xf8)
|
451 mask |= 0x02; 452#ifdef ATA_DEBUG 453 printf("ata%d: mask=%02x status0=%02x status1=%02x\n", 454 scp->lun, mask, status0, status1); 455#endif
| 455 mask |= 0x02; 456 if (bootverbose) 457 printf("ata%d: mask=%02x status0=%02x status1=%02x\n", 458 scp->lun, mask, status0, status1);
|
456 if (!mask) { 457 free(scp, M_DEVBUF);
| 459 if (!mask) { 460 free(scp, M_DEVBUF);
|
458 return 0;
| 461 return 0;
|
459 }
| 462 }
|
460 /* assert reset for devices and wait for completition */ 461 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 462 DELAY(1); 463 outb(scp->altioaddr, ATA_A_IDS | ATA_A_RESET); 464 DELAY(1000); 465 outb(scp->altioaddr, ATA_A_IDS); 466 DELAY(1000); 467 inb(scp->ioaddr + ATA_ERROR); 468 DELAY(1); 469 outb(scp->altioaddr, ATA_A_4BIT); 470 DELAY(1); 471 472 /* wait for BUSY to go inactive */ 473 for (timeout = 0; timeout < 30000*10; timeout++) { 474 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 475 DELAY(1); 476 status0 = inb(scp->ioaddr + ATA_STATUS); 477 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 478 DELAY(1); 479 status1 = inb(scp->ioaddr + ATA_STATUS); 480 if (mask == 0x01) /* wait for master only */ 481 if (!(status0 & ATA_S_BSY)) 482 break; 483 if (mask == 0x02) /* wait for slave only */ 484 if (!(status1 & ATA_S_BSY)) 485 break; 486 if (mask == 0x03) /* wait for both master & slave */ 487 if (!(status0 & ATA_S_BSY) && !(status1 & ATA_S_BSY)) 488 break; 489 DELAY(100); 490 } 491 if (status0 & ATA_S_BSY) 492 mask &= ~0x01; 493 if (status1 & ATA_S_BSY) 494 mask &= ~0x02; 495#ifdef ATA_DEBUG 496 printf("ata%d: mask=%02x status0=%02x status1=%02x\n", 497 scp->lun, mask, status0, status1); 498#endif
| 463 ata_reset(scp, &mask);
|
499 if (!mask) { 500 free(scp, M_DEVBUF);
| 464 if (!mask) { 465 free(scp, M_DEVBUF);
|
501 return 0;
| 466 return 0;
|
502 } 503 /* 504 * OK, we have at least one device on the chain, 505 * check for ATAPI signatures, if none check if its 506 * a good old ATA device. 507 */
| 467 } 468 /* 469 * OK, we have at least one device on the chain, 470 * check for ATAPI signatures, if none check if its 471 * a good old ATA device. 472 */
|
508
| |
509 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_MASTER)); 510 DELAY(1); 511 if (inb(scp->ioaddr + ATA_CYL_LSB) == ATAPI_MAGIC_LSB && 512 inb(scp->ioaddr + ATA_CYL_MSB) == ATAPI_MAGIC_MSB) { 513 scp->devices |= ATA_ATAPI_MASTER; 514 } 515 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_SLAVE)); 516 DELAY(1); 517 if (inb(scp->ioaddr + ATA_CYL_LSB) == ATAPI_MAGIC_LSB && 518 inb(scp->ioaddr + ATA_CYL_MSB) == ATAPI_MAGIC_MSB) { 519 scp->devices |= ATA_ATAPI_SLAVE; 520 } 521 if (status0 != 0x00 && !(scp->devices & ATA_ATAPI_MASTER)) {
| 473 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_MASTER)); 474 DELAY(1); 475 if (inb(scp->ioaddr + ATA_CYL_LSB) == ATAPI_MAGIC_LSB && 476 inb(scp->ioaddr + ATA_CYL_MSB) == ATAPI_MAGIC_MSB) { 477 scp->devices |= ATA_ATAPI_MASTER; 478 } 479 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_SLAVE)); 480 DELAY(1); 481 if (inb(scp->ioaddr + ATA_CYL_LSB) == ATAPI_MAGIC_LSB && 482 inb(scp->ioaddr + ATA_CYL_MSB) == ATAPI_MAGIC_MSB) { 483 scp->devices |= ATA_ATAPI_SLAVE; 484 } 485 if (status0 != 0x00 && !(scp->devices & ATA_ATAPI_MASTER)) {
|
522 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_MASTER)); 523 DELAY(1); 524 outb(scp->ioaddr + ATA_ERROR, 0x58); 525 outb(scp->ioaddr + ATA_CYL_LSB, 0xa5); 526 if (inb(scp->ioaddr + ATA_ERROR) != 0x58 &&
| 486 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_MASTER)); 487 DELAY(1); 488 outb(scp->ioaddr + ATA_ERROR, 0x58); 489 outb(scp->ioaddr + ATA_CYL_LSB, 0xa5); 490 if (inb(scp->ioaddr + ATA_ERROR) != 0x58 &&
|
527 inb(scp->ioaddr + ATA_CYL_LSB) == 0xa5) { 528 scp->devices |= ATA_ATA_MASTER;
| 491 inb(scp->ioaddr + ATA_CYL_LSB) == 0xa5) { 492 scp->devices |= ATA_ATA_MASTER;
|
529 }
| 493 }
|
530 } 531 if (status1 != 0x00 && !(scp->devices & ATA_ATAPI_SLAVE)) {
| 494 } 495 if (status1 != 0x00 && !(scp->devices & ATA_ATAPI_SLAVE)) {
|
532 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_SLAVE)); 533 DELAY(1); 534 outb(scp->ioaddr + ATA_ERROR, 0x58); 535 outb(scp->ioaddr + ATA_CYL_LSB, 0xa5); 536 if (inb(scp->ioaddr + ATA_ERROR) != 0x58 && 537 inb(scp->ioaddr + ATA_CYL_LSB) == 0xa5) {
| 496 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_SLAVE)); 497 DELAY(1); 498 outb(scp->ioaddr + ATA_ERROR, 0x58); 499 outb(scp->ioaddr + ATA_CYL_LSB, 0xa5); 500 if (inb(scp->ioaddr + ATA_ERROR) != 0x58 && 501 inb(scp->ioaddr + ATA_CYL_LSB) == 0xa5) {
|
538 scp->devices |= ATA_ATA_SLAVE;
| 502 scp->devices |= ATA_ATA_SLAVE;
|
539 }
| 503 }
|
540 }
| 504 }
|
541#ifdef ATA_DEBUG 542 printf("ata%d: devices = 0x%x\n", scp->lun, scp->devices); 543#endif
| 505 if (bootverbose) 506 printf("ata%d: devices = 0x%x\n", scp->lun, scp->devices);
|
544 if (!scp->devices) { 545 free(scp, M_DEVBUF); 546 return 0; 547 } 548 TAILQ_INIT(&scp->ata_queue); 549 TAILQ_INIT(&scp->atapi_queue); 550 *unit = scp->lun; 551 scp->dev = dev; 552 if (bmaddr)
| 507 if (!scp->devices) { 508 free(scp, M_DEVBUF); 509 return 0; 510 } 511 TAILQ_INIT(&scp->ata_queue); 512 TAILQ_INIT(&scp->atapi_queue); 513 *unit = scp->lun; 514 scp->dev = dev; 515 if (bmaddr)
|
553 scp->bmaddr = bmaddr;
| 516 scp->bmaddr = bmaddr;
|
554 atadevices[scp->lun] = scp; 555#ifndef ATA_STATIC_ID 556 atanlun++; 557#endif
| 517 atadevices[scp->lun] = scp; 518#ifndef ATA_STATIC_ID 519 atanlun++; 520#endif
|
| 521#if NAPM > 0 522 scp->resume_hook.ah_fun = (void *)ata_reinit; 523 scp->resume_hook.ah_arg = scp; 524 scp->resume_hook.ah_name = "ATA driver"; 525 scp->resume_hook.ah_order = APM_MID_ORDER; 526 apm_hook_establish(APM_HOOK_RESUME, &scp->resume_hook); 527#endif
|
558 return ATA_IOSIZE; 559} 560 561static void 562ataintr(void *data) 563{
| 528 return ATA_IOSIZE; 529} 530 531static void 532ataintr(void *data) 533{
|
564 struct ata_softc *scp; 565 struct atapi_request *atapi_request; 566 struct ad_request *ad_request; 567 u_int8_t status; 568 static int32_t intr_count = 0;
| 534 struct ata_softc *scp =(struct ata_softc *)data;
|
569
| 535
|
570 scp = (struct ata_softc *)data;
| 536 scp->status = inb(scp->ioaddr + ATA_STATUS);
|
571 572 /* find & call the responsible driver to process this interrupt */ 573 switch (scp->active) { 574#if NATADISK > 0 575 case ATA_ACTIVE_ATA:
| 537 538 /* find & call the responsible driver to process this interrupt */ 539 switch (scp->active) { 540#if NATADISK > 0 541 case ATA_ACTIVE_ATA:
|
576 if ((ad_request = TAILQ_FIRST(&scp->ata_queue))) 577 if (ad_interrupt(ad_request) == ATA_OP_CONTINUES)
| 542 if (ad_interrupt(scp->running) == ATA_OP_CONTINUES)
|
578 return; 579 break; 580#endif
| 543 return; 544 break; 545#endif
|
| 546#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
|
581 case ATA_ACTIVE_ATAPI:
| 547 case ATA_ACTIVE_ATAPI:
|
582 if ((atapi_request = TAILQ_FIRST(&scp->atapi_queue))) 583 if (atapi_interrupt(atapi_request) == ATA_OP_CONTINUES)
| 548 if (atapi_interrupt(scp->running) == ATA_OP_CONTINUES)
|
584 return; 585 break;
| 549 return; 550 break;
|
586
| 551#endif
|
587 case ATA_WAIT_INTR: 588 wakeup((caddr_t)scp); 589 break; 590 591 case ATA_IGNORE_INTR: 592 break; 593 594 default: 595 case ATA_IDLE:
| 552 case ATA_WAIT_INTR: 553 wakeup((caddr_t)scp); 554 break; 555 556 case ATA_IGNORE_INTR: 557 break; 558 559 default: 560 case ATA_IDLE:
|
596 status = inb(scp->ioaddr + ATA_STATUS); 597 if (intr_count++ < 10) 598 printf("ata%d: unwanted interrupt %d status = %02x\n", 599 scp->lun, intr_count, status);
| 561#ifdef ATA_DEBUG 562 { 563 static int32_t intr_count = 0; 564 if (intr_count++ < 10) 565 printf("ata%d: unwanted interrupt %d status = %02x\n", 566 scp->lun, intr_count, scp->status); 567 } 568#endif
|
600 return; 601 } 602 scp->active = ATA_IDLE;
| 569 return; 570 } 571 scp->active = ATA_IDLE;
|
| 572 scp->running = NULL;
|
603 ata_start(scp); 604} 605 606void 607ata_start(struct ata_softc *scp) 608{ 609 struct ad_request *ad_request; 610 struct atapi_request *atapi_request; 611 612#ifdef ATA_DEBUG 613 printf("ata_start: entered\n"); 614#endif
| 573 ata_start(scp); 574} 575 576void 577ata_start(struct ata_softc *scp) 578{ 579 struct ad_request *ad_request; 580 struct atapi_request *atapi_request; 581 582#ifdef ATA_DEBUG 583 printf("ata_start: entered\n"); 584#endif
|
615 if (scp->active != ATA_IDLE) { 616 printf("ata: unwanted ata_start\n");
| 585 if (scp->active != ATA_IDLE)
|
617 return;
| 586 return;
|
618 }
| |
619 620#if NATADISK > 0 621 /* find & call the responsible driver if anything on ATA queue */ 622 if ((ad_request = TAILQ_FIRST(&scp->ata_queue))) {
| 587 588#if NATADISK > 0 589 /* find & call the responsible driver if anything on ATA queue */ 590 if ((ad_request = TAILQ_FIRST(&scp->ata_queue))) {
|
| 591 TAILQ_REMOVE(&scp->ata_queue, ad_request, chain);
|
623 scp->active = ATA_ACTIVE_ATA;
| 592 scp->active = ATA_ACTIVE_ATA;
|
624 ad_transfer(ad_request);
| 593 scp->running = ad_request; 594 ad_transfer(ad_request);
|
625#ifdef ATA_DEBUG
| 595#ifdef ATA_DEBUG
|
626 printf("ata_start: started ata, leaving\n");
| 596 printf("ata_start: started ata, leaving\n");
|
627#endif 628 return; 629 } 630#endif
| 597#endif 598 return; 599 } 600#endif
|
631 632 /* find & call the responsible driver if anything on ATAPI queue */
| 601#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0 602 /* 603 * find & call the responsible driver if anything on ATAPI queue. 604 * check for device busy by polling the DSC bit, if busy, check 605 * for requests to the other device on the channel (if any). 606 * if no request can be served, timeout a call to ata_start to 607 * try again in a moment. the timeout should probably scale 608 * so we dont use too much time polling for slow devices. 609 */
|
633 if ((atapi_request = TAILQ_FIRST(&scp->atapi_queue))) {
| 610 if ((atapi_request = TAILQ_FIRST(&scp->atapi_queue))) {
|
634 scp->active = ATA_ACTIVE_ATAPI;
| 611 struct atapi_softc *atp = atapi_request->device; 612 613 outb(atp->controller->ioaddr + ATA_DRIVE, ATA_D_IBM | atp->unit); 614 DELAY(1); 615 if (!(inb(atp->controller->ioaddr + ATA_STATUS) & ATA_S_DSC)) { 616 while ((atapi_request = TAILQ_NEXT(atapi_request, chain))) { 617 if (atapi_request->device->unit != atp->unit) { 618 struct atapi_softc *tmpatp = atapi_request->device; 619 620 outb(tmpatp->controller->ioaddr + ATA_DRIVE, 621 ATA_D_IBM | tmpatp->unit); 622 DELAY(1); 623 if (!(inb(tmpatp->controller->ioaddr+ATA_STATUS)&ATA_S_DSC)) 624 atapi_request = NULL; 625 break; 626 } 627 } 628 } 629 if (!atapi_request) { 630 timeout((timeout_t*)ata_start, scp, 1); 631 return; 632 } 633 TAILQ_REMOVE(&scp->atapi_queue, atapi_request, chain); 634 scp->active = ATA_ACTIVE_ATAPI; 635 scp->running = atapi_request;
|
635 atapi_transfer(atapi_request); 636#ifdef ATA_DEBUG
| 636 atapi_transfer(atapi_request); 637#ifdef ATA_DEBUG
|
637 printf("ata_start: started atapi, leaving\n");
| 638 printf("ata_start: started atapi, leaving\n");
|
638#endif 639 return; 640 }
| 639#endif 640 return; 641 }
|
| 642#endif
|
641} 642
| 643} 644
|
| 645void 646ata_reset(struct ata_softc *scp, int32_t *mask) 647{ 648 int32_t timeout; 649 int8_t status0, status1; 650 651 /* reset channel */ 652 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 653 DELAY(1); 654 inb(scp->ioaddr + ATA_STATUS); 655 outb(scp->altioaddr, ATA_A_IDS | ATA_A_RESET); 656 DELAY(10000); 657 outb(scp->altioaddr, ATA_A_IDS); 658 DELAY(10000); 659 inb(scp->ioaddr + ATA_ERROR); 660 DELAY(1); 661 outb(scp->altioaddr, ATA_A_4BIT); 662 DELAY(1); 663 664 /* wait for BUSY to go inactive */ 665 for (timeout = 0; timeout < 300000; timeout++) { 666 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER); 667 DELAY(1); 668 status0 = inb(scp->ioaddr + ATA_STATUS); 669 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE); 670 DELAY(1); 671 status1 = inb(scp->ioaddr + ATA_STATUS); 672 if (*mask == 0x01) /* wait for master only */ 673 if (!(status0 & ATA_S_BSY)) 674 break; 675 if (*mask == 0x02) /* wait for slave only */ 676 if (!(status1 & ATA_S_BSY)) 677 break; 678 if (*mask == 0x03) /* wait for both master & slave */ 679 if (!(status0 & ATA_S_BSY) && !(status1 & ATA_S_BSY)) 680 break; 681 DELAY(100); 682 } 683 if (status0 & ATA_S_BSY) 684 *mask &= ~0x01; 685 if (status1 & ATA_S_BSY) 686 *mask &= ~0x02; 687 if (bootverbose) 688 printf("ata%d: mask=%02x status0=%02x status1=%02x\n", 689 scp->lun, *mask, status0, status1); 690} 691
|
643int32_t
| 692int32_t
|
| 693ata_reinit(struct ata_softc *scp) 694{ 695 int32_t mask = 0; 696 697 printf("ata%d: resetting devices .. ", scp->lun); 698 scp->active = ATA_IDLE; 699 if (scp->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER)) 700 mask |= 0x01; 701 if (scp->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE)) 702 mask |= 0x02; 703 ata_reset(scp, &mask); 704 705#if NATADISK > 0 706 if (scp->devices & (ATA_ATA_MASTER)) 707 ad_reinit((struct ad_softc *)scp->dev_softc[0]); 708 if (scp->devices & (ATA_ATA_SLAVE)) 709 ad_reinit((struct ad_softc *)scp->dev_softc[1]); 710#endif 711#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0 712 if (scp->devices & (ATA_ATAPI_MASTER)) 713 atapi_reinit((struct atapi_softc *)scp->dev_softc[0]); 714 if (scp->devices & (ATA_ATAPI_SLAVE)) 715 atapi_reinit((struct atapi_softc *)scp->dev_softc[1]); 716#endif 717 printf("done\n"); 718 ata_start(scp); 719 return 0; 720} 721 722int32_t
|
644ata_wait(struct ata_softc *scp, int32_t device, u_int8_t mask) 645{ 646 u_int8_t status; 647 u_int32_t timeout = 0; 648
| 723ata_wait(struct ata_softc *scp, int32_t device, u_int8_t mask) 724{ 725 u_int8_t status; 726 u_int32_t timeout = 0; 727
|
649 while (timeout++ <= 500000) { /* timeout 5 secs */
| 728 while (timeout <= 5000000) { /* timeout 5 secs */
|
650 status = inb(scp->ioaddr + ATA_STATUS); 651 652 /* if drive fails status, reselect the drive just to be sure */ 653 if (status == 0xff) {
| 729 status = inb(scp->ioaddr + ATA_STATUS); 730 731 /* if drive fails status, reselect the drive just to be sure */ 732 if (status == 0xff) {
|
654 printf("ata%d: %s: no status, reselecting device\n",
| 733 printf("ata%d: %s: no status, reselecting device\n",
|
655 scp->lun, device?"slave":"master");
| 734 scp->lun, device?"slave":"master");
|
656 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device);
| 735 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device);
|
657 DELAY(1); 658 status = inb(scp->ioaddr + ATA_STATUS); 659 } 660 if (status == 0xff) 661 return -1; 662 scp->status = status; 663 if (!(status & ATA_S_BSY)) { 664 if (status & ATA_S_ERROR) 665 scp->error = inb(scp->ioaddr + ATA_ERROR); 666 if ((status & mask) == mask) 667 return (status & ATA_S_ERROR);
| 736 DELAY(1); 737 status = inb(scp->ioaddr + ATA_STATUS); 738 } 739 if (status == 0xff) 740 return -1; 741 scp->status = status; 742 if (!(status & ATA_S_BSY)) { 743 if (status & ATA_S_ERROR) 744 scp->error = inb(scp->ioaddr + ATA_ERROR); 745 if ((status & mask) == mask) 746 return (status & ATA_S_ERROR);
|
668 } 669 if (timeout > 1000)
| 747 } 748 if (timeout > 1000) { 749 timeout += 1000;
|
670 DELAY(1000);
| 750 DELAY(1000);
|
671 else
| 751 } 752 else { 753 timeout += 10;
|
672 DELAY(10);
| 754 DELAY(10);
|
| 755 }
|
673 } 674 return -1; 675} 676 677int32_t 678ata_command(struct ata_softc *scp, int32_t device, u_int32_t command, 679 u_int32_t cylinder, u_int32_t head, u_int32_t sector, 680 u_int32_t count, u_int32_t feature, int32_t flags) 681{ 682#ifdef ATA_DEBUG
| 756 } 757 return -1; 758} 759 760int32_t 761ata_command(struct ata_softc *scp, int32_t device, u_int32_t command, 762 u_int32_t cylinder, u_int32_t head, u_int32_t sector, 763 u_int32_t count, u_int32_t feature, int32_t flags) 764{ 765#ifdef ATA_DEBUG
|
683printf("ata%d: ata_command: addr=%04x, device=%02x, cmd=%02x, c=%d, h=%d, s=%d, count=%d, flags=%02x\n", scp->lun, scp->ioaddr, device, command, cylinder, head, sector, count, flags);
| 766 printf("ata%d: ata_command: addr=%04x, device=%02x, cmd=%02x, " 767 "c=%d, h=%d, s=%d, count=%d, flags=%02x\n", 768 scp->lun, scp->ioaddr, device, command, 769 cylinder, head, sector, count, flags);
|
684#endif 685 686 /* ready to issue command ? */ 687 if (ata_wait(scp, device, 0) < 0) {
| 770#endif 771 772 /* ready to issue command ? */ 773 if (ata_wait(scp, device, 0) < 0) {
|
688 printf("ata%d: %s: timeout waiting to give command s=%02x e=%02x\n",
| 774 printf("ata%d-%s: timeout waiting to give command s=%02x e=%02x\n",
|
689 scp->lun, device?"slave":"master", scp->status, scp->error); 690 } 691 outb(scp->ioaddr + ATA_FEATURE, feature); 692 outb(scp->ioaddr + ATA_CYL_LSB, cylinder); 693 outb(scp->ioaddr + ATA_CYL_MSB, cylinder >> 8); 694 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device | head); 695 outb(scp->ioaddr + ATA_SECTOR, sector); 696 outb(scp->ioaddr + ATA_COUNT, count); 697 698 if (scp->active != ATA_IDLE && flags != ATA_IMMEDIATE) 699 printf("DANGER active=%d\n", scp->active); 700 701 switch (flags) { 702 case ATA_WAIT_INTR:
| 775 scp->lun, device?"slave":"master", scp->status, scp->error); 776 } 777 outb(scp->ioaddr + ATA_FEATURE, feature); 778 outb(scp->ioaddr + ATA_CYL_LSB, cylinder); 779 outb(scp->ioaddr + ATA_CYL_MSB, cylinder >> 8); 780 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device | head); 781 outb(scp->ioaddr + ATA_SECTOR, sector); 782 outb(scp->ioaddr + ATA_COUNT, count); 783 784 if (scp->active != ATA_IDLE && flags != ATA_IMMEDIATE) 785 printf("DANGER active=%d\n", scp->active); 786 787 switch (flags) { 788 case ATA_WAIT_INTR:
|
703 scp->active = ATA_WAIT_INTR; 704 outb(scp->ioaddr + ATA_CMD, command);
| 789 scp->active = ATA_WAIT_INTR; 790 outb(scp->ioaddr + ATA_CMD, command);
|
705 if (tsleep((caddr_t)scp, PRIBIO, "atacmd", 500)) { 706 printf("ata_command: timeout waiting for interrupt\n"); 707 scp->active = ATA_IDLE; 708 return -1; 709 } 710 break; 711 712 case ATA_IGNORE_INTR:
| 791 if (tsleep((caddr_t)scp, PRIBIO, "atacmd", 500)) { 792 printf("ata_command: timeout waiting for interrupt\n"); 793 scp->active = ATA_IDLE; 794 return -1; 795 } 796 break; 797 798 case ATA_IGNORE_INTR:
|
713 scp->active = ATA_IGNORE_INTR; 714 outb(scp->ioaddr + ATA_CMD, command);
| 799 scp->active = ATA_IGNORE_INTR; 800 outb(scp->ioaddr + ATA_CMD, command);
|
715 break; 716 717 case ATA_IMMEDIATE: 718 default:
| 801 break; 802 803 case ATA_IMMEDIATE: 804 default:
|
719 outb(scp->ioaddr + ATA_CMD, command);
| 805 outb(scp->ioaddr + ATA_CMD, command);
|
720 break; 721 } 722#ifdef ATA_DEBUG
| 806 break; 807 } 808#ifdef ATA_DEBUG
|
723printf("ata_command: leaving\n");
| 809 printf("ata_command: leaving\n");
|
724#endif 725 return 0; 726} 727
| 810#endif 811 return 0; 812} 813
|
| 814int8_t * 815ata_mode2str(int32_t mode) 816{ 817 switch (mode) { 818 case ATA_MODE_PIO: 819 return "PIO"; 820 case ATA_MODE_DMA: 821 return "DMA"; 822 case ATA_MODE_UDMA33: 823 return "UDMA33"; 824 case ATA_MODE_UDMA66: 825 return "UDMA66"; 826 default: 827 return "???"; 828 } 829} 830
|
728void 729bswap(int8_t *buf, int32_t len) 730{ 731 u_int16_t *p = (u_int16_t*)(buf + len); 732 733 while (--p >= (u_int16_t*)buf)
| 831void 832bswap(int8_t *buf, int32_t len) 833{ 834 u_int16_t *p = (u_int16_t*)(buf + len); 835 836 while (--p >= (u_int16_t*)buf)
|
734 *p = ntohs(*p);
| 837 *p = ntohs(*p);
|
735} 736 737void 738btrim(int8_t *buf, int32_t len) 739{ 740 int8_t *p; 741 742 for (p = buf; p < buf+len; ++p)
| 838} 839 840void 841btrim(int8_t *buf, int32_t len) 842{ 843 int8_t *p; 844 845 for (p = buf; p < buf+len; ++p)
|
743 if (!*p) 744 *p = ' ';
| 846 if (!*p) 847 *p = ' ';
|
745 for (p = buf + len - 1; p >= buf && *p == ' '; --p)
| 848 for (p = buf + len - 1; p >= buf && *p == ' '; --p)
|
746 *p = 0;
| 849 *p = 0;
|
747} 748 749void 750bpack(int8_t *src, int8_t *dst, int32_t len) 751{ 752 int32_t i, j, blank; 753 754 for (i = j = blank = 0 ; i < len-1; i++) { 755 if (blank && src[i] == ' ') continue; 756 if (blank && src[i] != ' ') { 757 dst[j++] = src[i]; 758 blank = 0; 759 continue; 760 }
| 850} 851 852void 853bpack(int8_t *src, int8_t *dst, int32_t len) 854{ 855 int32_t i, j, blank; 856 857 for (i = j = blank = 0 ; i < len-1; i++) { 858 if (blank && src[i] == ' ') continue; 859 if (blank && src[i] != ' ') { 860 dst[j++] = src[i]; 861 blank = 0; 862 continue; 863 }
|
761 if (src[i] == ' ')
| 864 if (src[i] == ' ') {
|
762 blank = 1;
| 865 blank = 1;
|
| 866 if (i == 0) 867 continue; 868 }
|
763 dst[j++] = src[i]; 764 } 765 dst[j] = 0x00; 766} 767#endif /* NATA > 0 */
| 869 dst[j++] = src[i]; 870 } 871 dst[j] = 0x00; 872} 873#endif /* NATA > 0 */
|