xref: /freebsd/sys/dev/ata/ata-pci.c (revision c0b9f4fe659b6839541970eb5675e57f4d814969)
1 /*-
2  * Copyright (c) 1998 - 2005 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  * 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 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include "opt_ata.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/ata.h>
38 #include <sys/bus.h>
39 #include <sys/malloc.h>
40 #include <sys/sema.h>
41 #include <sys/taskqueue.h>
42 #include <vm/uma.h>
43 #include <machine/stdarg.h>
44 #include <machine/resource.h>
45 #include <machine/bus.h>
46 #ifdef __alpha__
47 #include <machine/md_var.h>
48 #endif
49 #include <sys/rman.h>
50 #include <dev/pci/pcivar.h>
51 #include <dev/pci/pcireg.h>
52 #include <dev/ata/ata-all.h>
53 #include <dev/ata/ata-pci.h>
54 #include <ata_if.h>
55 
56 /* local vars */
57 static MALLOC_DEFINE(M_ATAPCI, "ata_pci", "ATA driver PCI");
58 
59 /* misc defines */
60 #define IOMASK                  0xfffffffc
61 #define ATA_PROBE_OK		-10
62 
63 /* prototypes */
64 static void ata_pci_dmainit(device_t);
65 
66 int
67 ata_legacy(device_t dev)
68 {
69     return ((pci_read_config(dev, PCIR_PROGIF, 1)&PCIP_STORAGE_IDE_MASTERDEV) &&
70 	    ((pci_read_config(dev, PCIR_PROGIF, 1) &
71 	      (PCIP_STORAGE_IDE_MODEPRIM | PCIP_STORAGE_IDE_MODESEC)) !=
72 	     (PCIP_STORAGE_IDE_MODEPRIM | PCIP_STORAGE_IDE_MODESEC)));
73 }
74 
75 int
76 ata_pci_probe(device_t dev)
77 {
78     if (pci_get_class(dev) != PCIC_STORAGE)
79 	return ENXIO;
80 
81     switch (pci_get_vendor(dev)) {
82     case ATA_ACARD_ID:
83 	if (!ata_acard_ident(dev))
84 	    return ATA_PROBE_OK;
85 	break;
86     case ATA_ACER_LABS_ID:
87 	if (!ata_ali_ident(dev))
88 	    return ATA_PROBE_OK;
89 	break;
90     case ATA_AMD_ID:
91 	if (!ata_amd_ident(dev))
92 	    return ATA_PROBE_OK;
93 	break;
94     case ATA_ATI_ID:
95 	if (!ata_ati_ident(dev))
96 	    return ATA_PROBE_OK;
97 	break;
98     case ATA_CYRIX_ID:
99 	if (!ata_cyrix_ident(dev))
100 	    return ATA_PROBE_OK;
101 	break;
102     case ATA_CYPRESS_ID:
103 	if (!ata_cypress_ident(dev))
104 	    return ATA_PROBE_OK;
105 	break;
106     case ATA_HIGHPOINT_ID:
107 	if (!ata_highpoint_ident(dev))
108 	    return ATA_PROBE_OK;
109 	break;
110     case ATA_INTEL_ID:
111 	if (!ata_intel_ident(dev))
112 	    return ATA_PROBE_OK;
113 	break;
114     case ATA_ITE_ID:
115 	if (!ata_ite_ident(dev))
116 	    return ATA_PROBE_OK;
117 	break;
118     case ATA_MARVELL_ID:
119 	if (!ata_marvell_ident(dev))
120 	    return ATA_PROBE_OK;
121 	break;
122     case ATA_NATIONAL_ID:
123 	if (!ata_national_ident(dev))
124 	    return ATA_PROBE_OK;
125 	break;
126     case ATA_NVIDIA_ID:
127 	if (!ata_nvidia_ident(dev))
128 	    return ATA_PROBE_OK;
129 	break;
130     case ATA_PROMISE_ID:
131 	if (!ata_promise_ident(dev))
132 	    return ATA_PROBE_OK;
133 	break;
134     case ATA_SERVERWORKS_ID:
135 	if (!ata_serverworks_ident(dev))
136 	    return ATA_PROBE_OK;
137 	break;
138     case ATA_SILICON_IMAGE_ID:
139 	if (!ata_sii_ident(dev))
140 	    return ATA_PROBE_OK;
141 	break;
142     case ATA_SIS_ID:
143 	if (!ata_sis_ident(dev))
144 	    return ATA_PROBE_OK;
145 	break;
146     case ATA_VIA_ID:
147 	if (!ata_via_ident(dev))
148 	    return ATA_PROBE_OK;
149 	break;
150     case ATA_CENATEK_ID:
151 	if (pci_get_devid(dev) == ATA_CENATEK_ROCKET) {
152 	    ata_generic_ident(dev);
153 	    device_set_desc(dev, "Cenatek Rocket Drive controller");
154 	    return ATA_PROBE_OK;
155 	}
156 	break;
157     case ATA_MICRON_ID:
158 	if (pci_get_devid(dev) == ATA_MICRON_RZ1000 ||
159 	    pci_get_devid(dev) == ATA_MICRON_RZ1001) {
160 	    ata_generic_ident(dev);
161 	    device_set_desc(dev,
162 		"RZ 100? ATA controller !WARNING! data loss/corruption risk");
163 	    return ATA_PROBE_OK;
164 	}
165 	break;
166     }
167 
168     /* unknown chipset, try generic DMA if it seems possible */
169     if ((pci_get_class(dev) == PCIC_STORAGE) &&
170 	(pci_get_subclass(dev) == PCIS_STORAGE_IDE)) {
171 	if (!ata_generic_ident(dev))
172 	    return ATA_PROBE_OK;
173     }
174     return ENXIO;
175 }
176 
177 int
178 ata_pci_attach(device_t dev)
179 {
180     struct ata_pci_controller *ctlr = device_get_softc(dev);
181     u_int32_t cmd;
182     int unit;
183 
184     /* do chipset specific setups only needed once */
185     if (ata_legacy(dev) || pci_read_config(dev, PCIR_BAR(2), 4) & IOMASK)
186 	ctlr->channels = 2;
187     else
188 	ctlr->channels = 1;
189     ctlr->allocate = ata_pci_allocate;
190     ctlr->dmainit = ata_pci_dmainit;
191     ctlr->dev = dev;
192 
193     /* if needed try to enable busmastering */
194     cmd = pci_read_config(dev, PCIR_COMMAND, 2);
195     if (!(cmd & PCIM_CMD_BUSMASTEREN)) {
196 	pci_write_config(dev, PCIR_COMMAND, cmd | PCIM_CMD_BUSMASTEREN, 2);
197 	cmd = pci_read_config(dev, PCIR_COMMAND, 2);
198     }
199 
200     /* if busmastering mode "stuck" use it */
201     if ((cmd & PCIM_CMD_BUSMASTEREN) == PCIM_CMD_BUSMASTEREN) {
202 	ctlr->r_type1 = SYS_RES_IOPORT;
203 	ctlr->r_rid1 = ATA_BMADDR_RID;
204 	ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1, &ctlr->r_rid1,
205 					      RF_ACTIVE);
206     }
207 
208     ctlr->chipinit(dev);
209 
210     /* attach all channels on this controller */
211     for (unit = 0; unit < ctlr->channels; unit++) {
212 	if (unit == 0 && (pci_get_progif(dev) & 0x81) == 0x80) {
213 	    device_add_child(dev, "ata", unit);
214 	    continue;
215 	}
216 	if (unit == 1 && (pci_get_progif(dev) & 0x84) == 0x80) {
217 	    device_add_child(dev, "ata", unit);
218 	    continue;
219 	}
220 	device_add_child(dev, "ata", devclass_find_free_unit(ata_devclass, 2));
221     }
222     bus_generic_attach(dev);
223     return 0;
224 }
225 
226 int
227 ata_pci_detach(device_t dev)
228 {
229     struct ata_pci_controller *ctlr = device_get_softc(dev);
230     device_t *children;
231     int nchildren, i;
232 
233     /* detach & delete all children */
234     if (!device_get_children(dev, &children, &nchildren)) {
235 	for (i = 0; i < nchildren; i++)
236 	    device_delete_child(dev, children[i]);
237 	free(children, M_TEMP);
238     }
239 
240     if (ctlr->r_irq) {
241 	bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle);
242 	bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ctlr->r_irq);
243     }
244     if (ctlr->r_res2)
245 	bus_release_resource(dev, ctlr->r_type2, ctlr->r_rid2, ctlr->r_res2);
246     if (ctlr->r_res1)
247 	bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1, ctlr->r_res1);
248 
249     return 0;
250 }
251 
252 struct resource *
253 ata_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
254 		       u_long start, u_long end, u_long count, u_int flags)
255 {
256     struct ata_pci_controller *controller = device_get_softc(dev);
257     int unit = ((struct ata_channel *)device_get_softc(child))->unit;
258     struct resource *res = NULL;
259     int myrid;
260 
261     if (type == SYS_RES_IOPORT) {
262 	switch (*rid) {
263 	case ATA_IOADDR_RID:
264 	    if (ata_legacy(dev)) {
265 		start = (unit ? ATA_SECONDARY : ATA_PRIMARY);
266 		count = ATA_IOSIZE;
267 		end = start + count - 1;
268 	    }
269 	    myrid = PCIR_BAR(0) + (unit << 3);
270 	    res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
271 				     SYS_RES_IOPORT, &myrid,
272 				     start, end, count, flags);
273 	    break;
274 
275 	case ATA_CTLADDR_RID:
276 	    if (ata_legacy(dev)) {
277 		start = (unit ? ATA_SECONDARY : ATA_PRIMARY) + ATA_CTLOFFSET;
278 		count = ATA_CTLIOSIZE;
279 		end = start + count - 1;
280 	    }
281 	    myrid = PCIR_BAR(1) + (unit << 3);
282 	    res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
283 				     SYS_RES_IOPORT, &myrid,
284 				     start, end, count, flags);
285 	    break;
286 	}
287     }
288     if (type == SYS_RES_IRQ && *rid == ATA_IRQ_RID) {
289 	if (ata_legacy(dev)) {
290 #ifdef __alpha__
291 	    res = alpha_platform_alloc_ide_intr(unit);
292 #else
293 	    int irq = (unit == 0 ? 14 : 15);
294 
295 	    res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
296 				     SYS_RES_IRQ, rid, irq, irq, 1, flags);
297 #endif
298 	}
299 	else
300 	    res = controller->r_irq;
301     }
302     return res;
303 }
304 
305 int
306 ata_pci_release_resource(device_t dev, device_t child, int type, int rid,
307 			 struct resource *r)
308 {
309     int unit = ((struct ata_channel *)device_get_softc(child))->unit;
310 
311     if (type == SYS_RES_IOPORT) {
312 	switch (rid) {
313 	case ATA_IOADDR_RID:
314 	    return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
315 					SYS_RES_IOPORT,
316 					PCIR_BAR(0) + (unit << 3), r);
317 	    break;
318 
319 	case ATA_CTLADDR_RID:
320 	    return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
321 					SYS_RES_IOPORT,
322 					PCIR_BAR(1) + (unit << 3), r);
323 	    break;
324 	default:
325 	    return ENOENT;
326 	}
327     }
328     if (type == SYS_RES_IRQ) {
329 	if (rid != ATA_IRQ_RID)
330 	    return ENOENT;
331 
332 	if (ata_legacy(dev)) {
333 #ifdef __alpha__
334 	    return alpha_platform_release_ide_intr(unit, r);
335 #else
336 	    return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
337 					SYS_RES_IRQ, rid, r);
338 #endif
339 	}
340 	else
341 	    return 0;
342     }
343     return EINVAL;
344 }
345 
346 int
347 ata_pci_setup_intr(device_t dev, device_t child, struct resource *irq,
348 		   int flags, driver_intr_t *function, void *argument,
349 		   void **cookiep)
350 {
351     if (ata_legacy(dev)) {
352 #ifdef __alpha__
353 	return alpha_platform_setup_ide_intr(child, irq, function, argument,
354 					     cookiep);
355 #else
356 	return BUS_SETUP_INTR(device_get_parent(dev), child, irq,
357 			      flags, function, argument, cookiep);
358 #endif
359     }
360     else {
361 	struct ata_pci_controller *controller = device_get_softc(dev);
362 	int unit = ((struct ata_channel *)device_get_softc(child))->unit;
363 
364 	controller->interrupt[unit].function = function;
365 	controller->interrupt[unit].argument = argument;
366 	*cookiep = controller;
367 	return 0;
368     }
369 }
370 
371 int
372 ata_pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
373 		      void *cookie)
374 {
375     if (ata_legacy(dev)) {
376 #ifdef __alpha__
377 	return alpha_platform_teardown_ide_intr(child, irq, cookie);
378 #else
379 	return BUS_TEARDOWN_INTR(device_get_parent(dev), child, irq, cookie);
380 #endif
381     }
382     else {
383 	struct ata_pci_controller *controller = device_get_softc(dev);
384 	int unit = ((struct ata_channel *)device_get_softc(child))->unit;
385 
386 	controller->interrupt[unit].function = NULL;
387 	controller->interrupt[unit].argument = NULL;
388 	return 0;
389     }
390 }
391 
392 int
393 ata_pci_allocate(device_t dev)
394 {
395     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
396     struct ata_channel *ch = device_get_softc(dev);
397     struct resource *io = NULL, *ctlio = NULL;
398     int i, rid;
399 
400     rid = ATA_IOADDR_RID;
401     if (!(io = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE)))
402 	return ENXIO;
403 
404     rid = ATA_CTLADDR_RID;
405     if (!(ctlio = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,RF_ACTIVE))){
406 	bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
407 	return ENXIO;
408     }
409 
410     for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
411 	ch->r_io[i].res = io;
412 	ch->r_io[i].offset = i;
413     }
414     ch->r_io[ATA_CONTROL].res = ctlio;
415     ch->r_io[ATA_CONTROL].offset = ata_legacy(device_get_parent(dev)) ? 0 : 2;
416     ch->r_io[ATA_IDX_ADDR].res = io;
417     ata_default_registers(dev);
418     if (ctlr->r_res1) {
419 	for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {
420 	    ch->r_io[i].res = ctlr->r_res1;
421 	    ch->r_io[i].offset = (i - ATA_BMCMD_PORT) + (ch->unit*ATA_BMIOSIZE);
422 	}
423     }
424 
425     ata_generic_hw(dev);
426     return 0;
427 }
428 
429 static int
430 ata_pci_dmastart(device_t dev)
431 {
432     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
433 
434     ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) |
435 		 (ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR)));
436     ATA_IDX_OUTL(ch, ATA_BMDTP_PORT, ch->dma->sg_bus);
437     ch->dma->flags |= ATA_DMA_ACTIVE;
438     ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
439 		 (ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_WRITE_READ) |
440 		 ((ch->dma->flags & ATA_DMA_READ) ? ATA_BMCMD_WRITE_READ : 0) |
441 		 ATA_BMCMD_START_STOP);
442     return 0;
443 }
444 
445 static int
446 ata_pci_dmastop(device_t dev)
447 {
448     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
449     int error;
450 
451     ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
452 		 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
453     ch->dma->flags &= ~ATA_DMA_ACTIVE;
454     error = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
455     ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
456     return error;
457 }
458 
459 static void
460 ata_pci_dmareset(device_t dev)
461 {
462     struct ata_channel *ch = device_get_softc(dev);
463 
464     ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
465 		 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
466     ch->dma->flags &= ~ATA_DMA_ACTIVE;
467     ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
468     ch->dma->unload(dev);
469 }
470 
471 static void
472 ata_pci_dmainit(device_t dev)
473 {
474     struct ata_channel *ch = device_get_softc(dev);
475 
476     ata_dmainit(dev);
477     if (ch->dma) {
478 	ch->dma->start = ata_pci_dmastart;
479 	ch->dma->stop = ata_pci_dmastop;
480 	ch->dma->reset = ata_pci_dmareset;
481     }
482 }
483 
484 static device_method_t ata_pci_methods[] = {
485     /* device interface */
486     DEVMETHOD(device_probe,             ata_pci_probe),
487     DEVMETHOD(device_attach,            ata_pci_attach),
488     DEVMETHOD(device_detach,            ata_pci_detach),
489     DEVMETHOD(device_shutdown,          bus_generic_shutdown),
490     DEVMETHOD(device_suspend,           bus_generic_suspend),
491     DEVMETHOD(device_resume,            bus_generic_resume),
492 
493     /* bus methods */
494     DEVMETHOD(bus_alloc_resource,       ata_pci_alloc_resource),
495     DEVMETHOD(bus_release_resource,     ata_pci_release_resource),
496     DEVMETHOD(bus_activate_resource,    bus_generic_activate_resource),
497     DEVMETHOD(bus_deactivate_resource,  bus_generic_deactivate_resource),
498     DEVMETHOD(bus_setup_intr,           ata_pci_setup_intr),
499     DEVMETHOD(bus_teardown_intr,        ata_pci_teardown_intr),
500 
501     { 0, 0 }
502 };
503 
504 devclass_t atapci_devclass;
505 
506 static driver_t ata_pci_driver = {
507     "atapci",
508     ata_pci_methods,
509     sizeof(struct ata_pci_controller),
510 };
511 
512 DRIVER_MODULE(atapci, pci, ata_pci_driver, atapci_devclass, 0, 0);
513 MODULE_VERSION(atapci, 1);
514 MODULE_DEPEND(atapci, ata, 1, 1, 1);
515 
516 static int
517 ata_pcichannel_probe(device_t dev)
518 {
519     struct ata_channel *ch = device_get_softc(dev);
520     device_t *children;
521     int count, i;
522     char buffer[32];
523 
524     /* take care of green memory */
525     bzero(ch, sizeof(struct ata_channel));
526 
527     /* find channel number on this controller */
528     device_get_children(device_get_parent(dev), &children, &count);
529     for (i = 0; i < count; i++) {
530 	if (children[i] == dev)
531 	    ch->unit = i;
532     }
533     free(children, M_TEMP);
534 
535     sprintf(buffer, "ATA channel %d", ch->unit);
536     device_set_desc_copy(dev, buffer);
537 
538     return ata_probe(dev);
539 }
540 
541 static int
542 ata_pcichannel_attach(device_t dev)
543 {
544     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
545     struct ata_channel *ch = device_get_softc(dev);
546     int error;
547 
548     if (ctlr->dmainit)
549 	ctlr->dmainit(dev);
550     if (ch->dma)
551 	ch->dma->alloc(dev);
552 
553     if ((error = ctlr->allocate(dev)))
554 	return error;
555 
556     return ata_attach(dev);
557 }
558 
559 static int
560 ata_pcichannel_detach(device_t dev)
561 {
562     struct ata_channel *ch = device_get_softc(dev);
563     int error;
564 
565     if ((error = ata_detach(dev)))
566 	return error;
567 
568     if (ch->dma)
569 	ch->dma->free(dev);
570 
571     /* XXX SOS free resources for io and ctlio ?? */
572 
573     return 0;
574 }
575 
576 static int
577 ata_pcichannel_locking(device_t dev, int mode)
578 {
579     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
580     struct ata_channel *ch = device_get_softc(dev);
581 
582     if (ctlr->locking)
583 	return ctlr->locking(dev, mode);
584     else
585 	return ch->unit;
586 }
587 
588 static void
589 ata_pcichannel_reset(device_t dev)
590 {
591     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
592     struct ata_channel *ch = device_get_softc(dev);
593 
594     /* if DMA engine present reset it  */
595     if (ch->dma) {
596 	if (ch->dma->reset)
597 	    ch->dma->reset(dev);
598 	ch->dma->unload(dev);
599     }
600 
601     /* reset the controller HW */
602     if (ctlr->reset)
603 	ctlr->reset(dev);
604     else
605 	ata_generic_reset(dev);
606 }
607 
608 static void
609 ata_pcichannel_setmode(device_t parent, device_t dev)
610 {
611     struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
612     struct ata_device *atadev = device_get_softc(dev);
613     int mode = atadev->mode;
614 
615     ctlr->setmode(dev, ATA_PIO_MAX);
616     if (mode >= ATA_DMA)
617 	ctlr->setmode(dev, mode);
618 }
619 
620 static device_method_t ata_pcichannel_methods[] = {
621     /* device interface */
622     DEVMETHOD(device_probe,     ata_pcichannel_probe),
623     DEVMETHOD(device_attach,    ata_pcichannel_attach),
624     DEVMETHOD(device_detach,    ata_pcichannel_detach),
625     DEVMETHOD(device_shutdown,  bus_generic_shutdown),
626     DEVMETHOD(device_suspend,   ata_suspend),
627     DEVMETHOD(device_resume,    ata_resume),
628 
629     /* ATA methods */
630     DEVMETHOD(ata_setmode,      ata_pcichannel_setmode),
631     DEVMETHOD(ata_locking,      ata_pcichannel_locking),
632     DEVMETHOD(ata_reset,        ata_pcichannel_reset),
633 
634     { 0, 0 }
635 };
636 
637 driver_t ata_pcichannel_driver = {
638     "ata",
639     ata_pcichannel_methods,
640     sizeof(struct ata_channel),
641 };
642 
643 DRIVER_MODULE(ata, atapci, ata_pcichannel_driver, ata_devclass, 0, 0);
644