Deleted Added
full compact
ata-all.c (51972) ata-all.c (52067)
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

--- 11 unchanged lines hidden (view full) ---

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

--- 11 unchanged lines hidden (view full) ---

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>

--- 24 unchanged lines hidden (view full) ---

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>

--- 24 unchanged lines hidden (view full) ---

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;

--- 74 unchanged lines hidden (view full) ---

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;

--- 74 unchanged lines hidden (view full) ---

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);

--- 19 unchanged lines hidden (view full) ---

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);

--- 19 unchanged lines hidden (view full) ---

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[] = {

--- 5 unchanged lines hidden (view full) ---

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[] = {

--- 5 unchanged lines hidden (view full) ---

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 ? */

--- 64 unchanged lines hidden (view full) ---

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 ? */

--- 64 unchanged lines hidden (view full) ---

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;

--- 17 unchanged lines hidden (view full) ---

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;

--- 17 unchanged lines hidden (view full) ---

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;

--- 11 unchanged lines hidden (view full) ---

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;

--- 11 unchanged lines hidden (view full) ---

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;

--- 8 unchanged lines hidden (view full) ---

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;

--- 8 unchanged lines hidden (view full) ---

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);

--- 15 unchanged lines hidden (view full) ---

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);

--- 15 unchanged lines hidden (view full) ---

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}

--- 34 unchanged lines hidden ---
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}

--- 34 unchanged lines hidden ---