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