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