xref: /freebsd/sys/dev/ata/ata-pci.c (revision 52267f7411adcc76ede961420e08c0e42f42d415)
1 /*-
2  * Copyright (c) 1998 - 2008 S�ren Schmidt <sos@FreeBSD.org>
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  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include "opt_ata.h"
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/ata.h>
36 #include <sys/bus.h>
37 #include <sys/conf.h>
38 #include <sys/malloc.h>
39 #include <sys/sema.h>
40 #include <sys/taskqueue.h>
41 #include <vm/uma.h>
42 #include <machine/stdarg.h>
43 #include <machine/resource.h>
44 #include <machine/bus.h>
45 #include <sys/rman.h>
46 #include <dev/pci/pcivar.h>
47 #include <dev/pci/pcireg.h>
48 #include <dev/ata/ata-all.h>
49 #include <dev/ata/ata-pci.h>
50 #include <ata_if.h>
51 
52 /* local vars */
53 static MALLOC_DEFINE(M_ATAPCI, "ata_pci", "ATA driver PCI");
54 
55 /* misc defines */
56 #define IOMASK                  0xfffffffc
57 
58 /* local prototypes */
59 static int ata_generic_chipinit(device_t dev);
60 static void ata_generic_setmode(device_t dev, int mode);
61 
62 /*
63  * generic PCI ATA device probe
64  */
65 int
66 ata_pci_probe(device_t dev)
67 {
68     struct ata_pci_controller *ctlr = device_get_softc(dev);
69     char buffer[64];
70 
71     /* is this a storage class device ? */
72     if (pci_get_class(dev) != PCIC_STORAGE)
73 	return ENXIO;
74 
75     /* is this an IDE/ATA type device ? */
76     if (pci_get_subclass(dev) != PCIS_STORAGE_IDE)
77 	return ENXIO;
78 
79     sprintf(buffer, "%s ATA controller", ata_pcivendor2str(dev));
80     device_set_desc_copy(dev, buffer);
81     ctlr->chipinit = ata_generic_chipinit;
82 
83     /* we are a low priority handler */
84     return -100;
85 }
86 
87 int
88 ata_pci_attach(device_t dev)
89 {
90     struct ata_pci_controller *ctlr = device_get_softc(dev);
91     u_int32_t cmd;
92     int unit;
93 
94     /* do chipset specific setups only needed once */
95     if (ata_legacy(dev) || pci_read_config(dev, PCIR_BAR(2), 4) & IOMASK)
96 	ctlr->channels = 2;
97     else
98 	ctlr->channels = 1;
99     ctlr->allocate = ata_pci_allocate;
100     ctlr->dmainit = ata_pci_dmainit;
101     ctlr->dev = dev;
102 
103     /* if needed try to enable busmastering */
104     cmd = pci_read_config(dev, PCIR_COMMAND, 2);
105     if (!(cmd & PCIM_CMD_BUSMASTEREN)) {
106 	pci_write_config(dev, PCIR_COMMAND, cmd | PCIM_CMD_BUSMASTEREN, 2);
107 	cmd = pci_read_config(dev, PCIR_COMMAND, 2);
108     }
109 
110     /* if busmastering mode "stuck" use it */
111     if ((cmd & PCIM_CMD_BUSMASTEREN) == PCIM_CMD_BUSMASTEREN) {
112 	ctlr->r_type1 = SYS_RES_IOPORT;
113 	ctlr->r_rid1 = ATA_BMADDR_RID;
114 	ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1, &ctlr->r_rid1,
115 					      RF_ACTIVE);
116     }
117 
118     if (ctlr->chipinit(dev))
119 	return ENXIO;
120 
121     /* attach all channels on this controller */
122     for (unit = 0; unit < ctlr->channels; unit++) {
123 	if ((unit == 0 || unit == 1) && ata_legacy(dev)) {
124 	    device_add_child(dev, "ata", unit);
125 	    continue;
126 	}
127 	device_add_child(dev, "ata", devclass_find_free_unit(ata_devclass, 2));
128     }
129     bus_generic_attach(dev);
130     return 0;
131 }
132 
133 int
134 ata_pci_detach(device_t dev)
135 {
136     struct ata_pci_controller *ctlr = device_get_softc(dev);
137     device_t *children;
138     int nchildren, i;
139 
140     /* detach & delete all children */
141     if (!device_get_children(dev, &children, &nchildren)) {
142 	for (i = 0; i < nchildren; i++)
143 	    device_delete_child(dev, children[i]);
144 	free(children, M_TEMP);
145     }
146 
147     if (ctlr->r_irq) {
148 	bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle);
149 	bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ctlr->r_irq);
150     }
151     if (ctlr->r_res2)
152 	bus_release_resource(dev, ctlr->r_type2, ctlr->r_rid2, ctlr->r_res2);
153     if (ctlr->r_res1)
154 	bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1, ctlr->r_res1);
155 
156     return 0;
157 }
158 
159 int
160 ata_pci_suspend(device_t dev)
161 {
162     struct ata_pci_controller *ctlr = device_get_softc(dev);
163     int error = 0;
164 
165     bus_generic_suspend(dev);
166     if (ctlr->suspend)
167 	error = ctlr->suspend(dev);
168     return error;
169 }
170 
171 int
172 ata_pci_resume(device_t dev)
173 {
174     struct ata_pci_controller *ctlr = device_get_softc(dev);
175     int error = 0;
176 
177     if (ctlr->resume)
178 	error = ctlr->resume(dev);
179     bus_generic_resume(dev);
180     return error;
181 }
182 
183 struct resource *
184 ata_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
185 		       u_long start, u_long end, u_long count, u_int flags)
186 {
187     struct ata_pci_controller *controller = device_get_softc(dev);
188     int unit = ((struct ata_channel *)device_get_softc(child))->unit;
189     struct resource *res = NULL;
190     int myrid;
191 
192     if (type == SYS_RES_IOPORT) {
193 	switch (*rid) {
194 	case ATA_IOADDR_RID:
195 	    if (ata_legacy(dev)) {
196 		start = (unit ? ATA_SECONDARY : ATA_PRIMARY);
197 		count = ATA_IOSIZE;
198 		end = start + count - 1;
199 	    }
200 	    myrid = PCIR_BAR(0) + (unit << 3);
201 	    res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
202 				     SYS_RES_IOPORT, &myrid,
203 				     start, end, count, flags);
204 	    break;
205 
206 	case ATA_CTLADDR_RID:
207 	    if (ata_legacy(dev)) {
208 		start = (unit ? ATA_SECONDARY : ATA_PRIMARY) + ATA_CTLOFFSET;
209 		count = ATA_CTLIOSIZE;
210 		end = start + count - 1;
211 	    }
212 	    myrid = PCIR_BAR(1) + (unit << 3);
213 	    res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
214 				     SYS_RES_IOPORT, &myrid,
215 				     start, end, count, flags);
216 	    break;
217 	}
218     }
219     if (type == SYS_RES_IRQ && *rid == ATA_IRQ_RID) {
220 	if (ata_legacy(dev)) {
221 	    int irq = (unit == 0 ? 14 : 15);
222 
223 	    res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
224 				     SYS_RES_IRQ, rid, irq, irq, 1, flags);
225 	}
226 	else
227 	    res = controller->r_irq;
228     }
229     return res;
230 }
231 
232 int
233 ata_pci_release_resource(device_t dev, device_t child, int type, int rid,
234 			 struct resource *r)
235 {
236     int unit = ((struct ata_channel *)device_get_softc(child))->unit;
237 
238     if (type == SYS_RES_IOPORT) {
239 	switch (rid) {
240 	case ATA_IOADDR_RID:
241 	    return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
242 					SYS_RES_IOPORT,
243 					PCIR_BAR(0) + (unit << 3), r);
244 	    break;
245 
246 	case ATA_CTLADDR_RID:
247 	    return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
248 					SYS_RES_IOPORT,
249 					PCIR_BAR(1) + (unit << 3), r);
250 	    break;
251 	default:
252 	    return ENOENT;
253 	}
254     }
255     if (type == SYS_RES_IRQ) {
256 	if (rid != ATA_IRQ_RID)
257 	    return ENOENT;
258 
259 	if (ata_legacy(dev)) {
260 	    return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
261 					SYS_RES_IRQ, rid, r);
262 	}
263 	else
264 	    return 0;
265     }
266     return EINVAL;
267 }
268 
269 int
270 ata_pci_setup_intr(device_t dev, device_t child, struct resource *irq,
271 		   int flags, driver_filter_t *filter, driver_intr_t *function,
272 		   void *argument, void **cookiep)
273 {
274     if (ata_legacy(dev)) {
275 	return BUS_SETUP_INTR(device_get_parent(dev), child, irq,
276 			      flags, filter, function, argument, cookiep);
277     }
278     else {
279 	struct ata_pci_controller *controller = device_get_softc(dev);
280 	int unit = ((struct ata_channel *)device_get_softc(child))->unit;
281 
282 	if (filter != NULL) {
283 		printf("ata-pci.c: we cannot use a filter here\n");
284 		return (EINVAL);
285 	}
286 	controller->interrupt[unit].function = function;
287 	controller->interrupt[unit].argument = argument;
288 	*cookiep = controller;
289 	return 0;
290     }
291 }
292 
293 int
294 ata_pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
295 		      void *cookie)
296 {
297     if (ata_legacy(dev)) {
298 	return BUS_TEARDOWN_INTR(device_get_parent(dev), child, irq, cookie);
299     }
300     else {
301 	struct ata_pci_controller *controller = device_get_softc(dev);
302 	int unit = ((struct ata_channel *)device_get_softc(child))->unit;
303 
304 	controller->interrupt[unit].function = NULL;
305 	controller->interrupt[unit].argument = NULL;
306 	return 0;
307     }
308 }
309 
310 static void
311 ata_generic_setmode(device_t dev, int mode)
312 {
313     struct ata_device *atadev = device_get_softc(dev);
314 
315     mode = ata_limit_mode(dev, mode, ATA_UDMA2);
316     mode = ata_check_80pin(dev, mode);
317     if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode))
318 	atadev->mode = mode;
319 }
320 
321 static int
322 ata_generic_chipinit(device_t dev)
323 {
324     struct ata_pci_controller *ctlr = device_get_softc(dev);
325 
326     if (ata_setup_interrupt(dev, ata_generic_intr))
327 	return ENXIO;
328     ctlr->setmode = ata_generic_setmode;
329     return 0;
330 }
331 
332 int
333 ata_pci_allocate(device_t dev)
334 {
335     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
336     struct ata_channel *ch = device_get_softc(dev);
337     struct resource *io = NULL, *ctlio = NULL;
338     int i, rid;
339 
340     rid = ATA_IOADDR_RID;
341     if (!(io = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE)))
342 	return ENXIO;
343 
344     rid = ATA_CTLADDR_RID;
345     if (!(ctlio = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,RF_ACTIVE))){
346 	bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
347 	return ENXIO;
348     }
349 
350     for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
351 	ch->r_io[i].res = io;
352 	ch->r_io[i].offset = i;
353     }
354     ch->r_io[ATA_CONTROL].res = ctlio;
355     ch->r_io[ATA_CONTROL].offset = ata_legacy(device_get_parent(dev)) ? 0 : 2;
356     ch->r_io[ATA_IDX_ADDR].res = io;
357     ata_default_registers(dev);
358     if (ctlr->r_res1) {
359 	for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {
360 	    ch->r_io[i].res = ctlr->r_res1;
361 	    ch->r_io[i].offset = (i - ATA_BMCMD_PORT) + (ch->unit*ATA_BMIOSIZE);
362 	}
363     }
364 
365     ata_pci_hw(dev);
366     return 0;
367 }
368 
369 int
370 ata_pci_status(device_t dev)
371 {
372     struct ata_channel *ch = device_get_softc(dev);
373 
374     if ((dumping || !ata_legacy(device_get_parent(dev))) &&
375 	((ch->flags & ATA_ALWAYS_DMASTAT) ||
376 	 (ch->dma.flags & ATA_DMA_ACTIVE))) {
377 	int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
378 
379 	if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
380 	    ATA_BMSTAT_INTERRUPT)
381 	    return 0;
382 	ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
383 	DELAY(1);
384     }
385     if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) {
386 	DELAY(100);
387 	if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY)
388 	    return 0;
389     }
390     return 1;
391 }
392 
393 void
394 ata_pci_hw(device_t dev)
395 {
396     struct ata_channel *ch = device_get_softc(dev);
397 
398     ata_generic_hw(dev);
399     ch->hw.status = ata_pci_status;
400 }
401 
402 static int
403 ata_pci_dmastart(struct ata_request *request)
404 {
405     struct ata_channel *ch = device_get_softc(request->parent);
406 
407     ATA_DEBUG_RQ(request, "dmastart");
408 
409     ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) |
410 		 (ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR)));
411     ATA_IDX_OUTL(ch, ATA_BMDTP_PORT, request->dma->sg_bus);
412     ch->dma.flags |= ATA_DMA_ACTIVE;
413     ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
414 		 (ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_WRITE_READ) |
415 		 ((request->flags & ATA_R_READ) ? ATA_BMCMD_WRITE_READ : 0)|
416 		 ATA_BMCMD_START_STOP);
417     return 0;
418 }
419 
420 static int
421 ata_pci_dmastop(struct ata_request *request)
422 {
423     struct ata_channel *ch = device_get_softc(request->parent);
424     int error;
425 
426     ATA_DEBUG_RQ(request, "dmastop");
427 
428     ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
429 		 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
430     ch->dma.flags &= ~ATA_DMA_ACTIVE;
431     error = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
432     ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
433     return error;
434 }
435 
436 static void
437 ata_pci_dmareset(device_t dev)
438 {
439     struct ata_channel *ch = device_get_softc(dev);
440     struct ata_request *request;
441 
442     ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
443 		 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
444     ch->dma.flags &= ~ATA_DMA_ACTIVE;
445     ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
446     if ((request = ch->running)) {
447 	device_printf(request->dev, "DMA reset calling unload\n");
448 	ch->dma.unload(request);
449     }
450 }
451 
452 void
453 ata_pci_dmainit(device_t dev)
454 {
455     struct ata_channel *ch = device_get_softc(dev);
456 
457     ata_dmainit(dev);
458     ch->dma.start = ata_pci_dmastart;
459     ch->dma.stop = ata_pci_dmastop;
460     ch->dma.reset = ata_pci_dmareset;
461 }
462 
463 
464 static device_method_t ata_pci_methods[] = {
465     /* device interface */
466     DEVMETHOD(device_probe,             ata_pci_probe),
467     DEVMETHOD(device_attach,            ata_pci_attach),
468     DEVMETHOD(device_detach,            ata_pci_detach),
469     DEVMETHOD(device_suspend,           ata_pci_suspend),
470     DEVMETHOD(device_resume,            ata_pci_resume),
471     DEVMETHOD(device_shutdown,          bus_generic_shutdown),
472 
473     /* bus methods */
474     DEVMETHOD(bus_alloc_resource,       ata_pci_alloc_resource),
475     DEVMETHOD(bus_release_resource,     ata_pci_release_resource),
476     DEVMETHOD(bus_activate_resource,    bus_generic_activate_resource),
477     DEVMETHOD(bus_deactivate_resource,  bus_generic_deactivate_resource),
478     DEVMETHOD(bus_setup_intr,           ata_pci_setup_intr),
479     DEVMETHOD(bus_teardown_intr,        ata_pci_teardown_intr),
480 
481     { 0, 0 }
482 };
483 
484 devclass_t ata_pci_devclass;
485 
486 static driver_t ata_pci_driver = {
487     "atapci",
488     ata_pci_methods,
489     sizeof(struct ata_pci_controller),
490 };
491 
492 DRIVER_MODULE(atapci, pci, ata_pci_driver, ata_pci_devclass, 0, 0);
493 MODULE_VERSION(atapci, 1);
494 MODULE_DEPEND(atapci, ata, 1, 1, 1);
495 
496 static int
497 ata_pcichannel_probe(device_t dev)
498 {
499     struct ata_channel *ch = device_get_softc(dev);
500     device_t *children;
501     int count, i;
502     char buffer[32];
503 
504     /* take care of green memory */
505     bzero(ch, sizeof(struct ata_channel));
506 
507     /* find channel number on this controller */
508     device_get_children(device_get_parent(dev), &children, &count);
509     for (i = 0; i < count; i++) {
510 	if (children[i] == dev)
511 	    ch->unit = i;
512     }
513     free(children, M_TEMP);
514 
515     sprintf(buffer, "ATA channel %d", ch->unit);
516     device_set_desc_copy(dev, buffer);
517 
518     return ata_probe(dev);
519 }
520 
521 static int
522 ata_pcichannel_attach(device_t dev)
523 {
524     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
525     int error;
526 
527     if (ctlr->dmainit)
528 	ctlr->dmainit(dev);
529 
530     if ((error = ctlr->allocate(dev)))
531 	return error;
532 
533     return ata_attach(dev);
534 }
535 
536 static int
537 ata_pcichannel_detach(device_t dev)
538 {
539     struct ata_channel *ch = device_get_softc(dev);
540     int error;
541 
542     if ((error = ata_detach(dev)))
543 	return error;
544 
545     ch->dma.free(dev);
546 
547     /* XXX SOS free resources for io and ctlio ?? */
548 
549     return 0;
550 }
551 
552 static int
553 ata_pcichannel_locking(device_t dev, int mode)
554 {
555     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
556     struct ata_channel *ch = device_get_softc(dev);
557 
558     if (ctlr->locking)
559 	return ctlr->locking(dev, mode);
560     else
561 	return ch->unit;
562 }
563 
564 static void
565 ata_pcichannel_reset(device_t dev)
566 {
567     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
568     struct ata_channel *ch = device_get_softc(dev);
569 
570     /* if DMA engine present reset it  */
571     if (ch->dma.reset)
572 	ch->dma.reset(dev);
573 
574     /* reset the controller HW */
575     if (ctlr->reset)
576 	ctlr->reset(dev);
577     else
578 	ata_generic_reset(dev);
579 }
580 
581 static void
582 ata_pcichannel_setmode(device_t parent, device_t dev)
583 {
584     struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
585     struct ata_device *atadev = device_get_softc(dev);
586     int mode = atadev->mode;
587 
588     ctlr->setmode(dev, ATA_PIO_MAX);
589     if (mode >= ATA_DMA)
590 	ctlr->setmode(dev, mode);
591 }
592 
593 static device_method_t ata_pcichannel_methods[] = {
594     /* device interface */
595     DEVMETHOD(device_probe,     ata_pcichannel_probe),
596     DEVMETHOD(device_attach,    ata_pcichannel_attach),
597     DEVMETHOD(device_detach,    ata_pcichannel_detach),
598     DEVMETHOD(device_shutdown,  bus_generic_shutdown),
599     DEVMETHOD(device_suspend,   ata_suspend),
600     DEVMETHOD(device_resume,    ata_resume),
601 
602     /* ATA methods */
603     DEVMETHOD(ata_setmode,      ata_pcichannel_setmode),
604     DEVMETHOD(ata_locking,      ata_pcichannel_locking),
605     DEVMETHOD(ata_reset,        ata_pcichannel_reset),
606 
607     { 0, 0 }
608 };
609 
610 driver_t ata_pcichannel_driver = {
611     "ata",
612     ata_pcichannel_methods,
613     sizeof(struct ata_channel),
614 };
615 
616 DRIVER_MODULE(ata, atapci, ata_pcichannel_driver, ata_devclass, 0, 0);
617 
618 
619 /*
620  * misc support fucntions
621  */
622 int
623 ata_legacy(device_t dev)
624 {
625     return (((pci_read_config(dev, PCIR_PROGIF, 1)&PCIP_STORAGE_IDE_MASTERDEV)&&
626 	     ((pci_read_config(dev, PCIR_PROGIF, 1) &
627 	       (PCIP_STORAGE_IDE_MODEPRIM | PCIP_STORAGE_IDE_MODESEC)) !=
628 	      (PCIP_STORAGE_IDE_MODEPRIM | PCIP_STORAGE_IDE_MODESEC))) ||
629 	    (!pci_read_config(dev, PCIR_BAR(0), 4) &&
630 	     !pci_read_config(dev, PCIR_BAR(1), 4) &&
631 	     !pci_read_config(dev, PCIR_BAR(2), 4) &&
632 	     !pci_read_config(dev, PCIR_BAR(3), 4) &&
633 	     !pci_read_config(dev, PCIR_BAR(5), 4)));
634 }
635 
636 void
637 ata_generic_intr(void *data)
638 {
639     struct ata_pci_controller *ctlr = data;
640     struct ata_channel *ch;
641     int unit;
642 
643     for (unit = 0; unit < ctlr->channels; unit++) {
644 	if ((ch = ctlr->interrupt[unit].argument))
645 	    ctlr->interrupt[unit].function(ch);
646     }
647 }
648 
649 int
650 ata_setup_interrupt(device_t dev, void *intr_func)
651 {
652     struct ata_pci_controller *ctlr = device_get_softc(dev);
653     int rid = ATA_IRQ_RID;
654 
655     if (!ata_legacy(dev)) {
656 	if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
657 						   RF_SHAREABLE | RF_ACTIVE))) {
658 	    device_printf(dev, "unable to map interrupt\n");
659 	    return ENXIO;
660 	}
661 	if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS, NULL,
662 			    intr_func, ctlr, &ctlr->handle))) {
663 	    /* SOS XXX release r_irq */
664 	    device_printf(dev, "unable to setup interrupt\n");
665 	    return ENXIO;
666 	}
667     }
668     return 0;
669 }
670 
671 void
672 ata_set_desc(device_t dev)
673 {
674     struct ata_pci_controller *ctlr = device_get_softc(dev);
675     char buffer[128];
676 
677     sprintf(buffer, "%s %s %s controller",
678             ata_pcivendor2str(dev), ctlr->chip->text,
679             ata_mode2str(ctlr->chip->max_dma));
680     device_set_desc_copy(dev, buffer);
681 }
682 
683 struct ata_chip_id *
684 ata_match_chip(device_t dev, struct ata_chip_id *index)
685 {
686     while (index->chipid != 0) {
687 	if (pci_get_devid(dev) == index->chipid &&
688 	    pci_get_revid(dev) >= index->chiprev)
689 	    return index;
690 	index++;
691     }
692     return NULL;
693 }
694 
695 struct ata_chip_id *
696 ata_find_chip(device_t dev, struct ata_chip_id *index, int slot)
697 {
698     device_t *children;
699     int nchildren, i;
700 
701     if (device_get_children(device_get_parent(dev), &children, &nchildren))
702 	return 0;
703 
704     while (index->chipid != 0) {
705 	for (i = 0; i < nchildren; i++) {
706 	    if (((slot >= 0 && pci_get_slot(children[i]) == slot) ||
707 		 (slot < 0 && pci_get_slot(children[i]) <= -slot)) &&
708 		pci_get_devid(children[i]) == index->chipid &&
709 		pci_get_revid(children[i]) >= index->chiprev) {
710 		free(children, M_TEMP);
711 		return index;
712 	    }
713 	}
714 	index++;
715     }
716     free(children, M_TEMP);
717     return NULL;
718 }
719 
720 void
721 ata_print_cable(device_t dev, u_int8_t *who)
722 {
723     device_printf(dev,
724                   "DMA limited to UDMA33, %s found non-ATA66 cable\n", who);
725 }
726 
727 int
728 ata_check_80pin(device_t dev, int mode)
729 {
730     struct ata_device *atadev = device_get_softc(dev);
731 
732     if (!ata_dma_check_80pin) {
733         if (bootverbose)
734             device_printf(dev, "Skipping 80pin cable check\n");
735         return mode;
736     }
737 
738     if (mode > ATA_UDMA2 && !(atadev->param.hwres & ATA_CABLE_ID)) {
739         ata_print_cable(dev, "device");
740         mode = ATA_UDMA2;
741     }
742     return mode;
743 }
744 
745 char *
746 ata_pcivendor2str(device_t dev)
747 {
748     switch (pci_get_vendor(dev)) {
749     case ATA_ACARD_ID:          return "Acard";
750     case ATA_ACER_LABS_ID:      return "AcerLabs";
751     case ATA_AMD_ID:            return "AMD";
752     case ATA_ADAPTEC_ID:        return "Adaptec";
753     case ATA_ATI_ID:            return "ATI";
754     case ATA_CYRIX_ID:          return "Cyrix";
755     case ATA_CYPRESS_ID:        return "Cypress";
756     case ATA_HIGHPOINT_ID:      return "HighPoint";
757     case ATA_INTEL_ID:          return "Intel";
758     case ATA_ITE_ID:            return "ITE";
759     case ATA_JMICRON_ID:        return "JMicron";
760     case ATA_MARVELL_ID:        return "Marvell";
761     case ATA_NATIONAL_ID:       return "National";
762     case ATA_NETCELL_ID:        return "Netcell";
763     case ATA_NVIDIA_ID:         return "nVidia";
764     case ATA_PROMISE_ID:        return "Promise";
765     case ATA_SERVERWORKS_ID:    return "ServerWorks";
766     case ATA_SILICON_IMAGE_ID:  return "SiI";
767     case ATA_SIS_ID:            return "SiS";
768     case ATA_VIA_ID:            return "VIA";
769     case ATA_CENATEK_ID:        return "Cenatek";
770     case ATA_MICRON_ID:         return "Micron";
771     default:                    return "Generic";
772     }
773 }
774 
775 int
776 ata_mode2idx(int mode)
777 {
778     if ((mode & ATA_DMA_MASK) == ATA_UDMA0)
779 	return (mode & ATA_MODE_MASK) + 8;
780     if ((mode & ATA_DMA_MASK) == ATA_WDMA0)
781 	return (mode & ATA_MODE_MASK) + 5;
782     return (mode & ATA_MODE_MASK) - ATA_PIO0;
783 }
784 
785