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