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