xref: /freebsd/sys/dev/ata/ata-pci.c (revision c128b2d129a8e305b673ef5e3da76af1fb91ae60)
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 #ifdef __alpha__
46 #include <machine/md_var.h>
47 #endif
48 #include <sys/rman.h>
49 #include <dev/pci/pcivar.h>
50 #include <dev/pci/pcireg.h>
51 #include <dev/ata/ata-all.h>
52 #include <dev/ata/ata-pci.h>
53 #include <ata_if.h>
54 
55 /* local vars */
56 static MALLOC_DEFINE(M_ATAPCI, "ata_pci", "ATA driver PCI");
57 
58 /* misc defines */
59 #define IOMASK                  0xfffffffc
60 #define ATA_PROBE_OK            -10
61 
62 int
63 ata_legacy(device_t dev)
64 {
65     return ((pci_read_config(dev, PCIR_PROGIF, 1)&PCIP_STORAGE_IDE_MASTERDEV) &&
66 	    ((pci_read_config(dev, PCIR_PROGIF, 1) &
67 	      (PCIP_STORAGE_IDE_MODEPRIM | PCIP_STORAGE_IDE_MODESEC)) !=
68 	     (PCIP_STORAGE_IDE_MODEPRIM | PCIP_STORAGE_IDE_MODESEC)));
69 }
70 
71 int
72 ata_pci_probe(device_t dev)
73 {
74     if (pci_get_class(dev) != PCIC_STORAGE)
75 	return ENXIO;
76 
77     switch (pci_get_vendor(dev)) {
78     case ATA_ACARD_ID:
79 	if (!ata_acard_ident(dev))
80 	    return ATA_PROBE_OK;
81 	break;
82     case ATA_ACER_LABS_ID:
83 	if (!ata_ali_ident(dev))
84 	    return ATA_PROBE_OK;
85 	break;
86     case ATA_AMD_ID:
87 	if (!ata_amd_ident(dev))
88 	    return ATA_PROBE_OK;
89 	break;
90     case ATA_ATI_ID:
91 	if (!ata_ati_ident(dev))
92 	    return ATA_PROBE_OK;
93 	break;
94     case ATA_CYRIX_ID:
95 	if (!ata_cyrix_ident(dev))
96 	    return ATA_PROBE_OK;
97 	break;
98     case ATA_CYPRESS_ID:
99 	if (!ata_cypress_ident(dev))
100 	    return ATA_PROBE_OK;
101 	break;
102     case ATA_HIGHPOINT_ID:
103 	if (!ata_highpoint_ident(dev))
104 	    return ATA_PROBE_OK;
105 	break;
106     case ATA_INTEL_ID:
107 	if (!ata_intel_ident(dev))
108 	    return ATA_PROBE_OK;
109 	break;
110     case ATA_ITE_ID:
111 	if (!ata_ite_ident(dev))
112 	    return ATA_PROBE_OK;
113 	break;
114     case ATA_JMICRON_ID:
115 	if (!ata_jmicron_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     if (ctlr->chipinit(dev))
209 	return ENXIO;
210 
211     /* attach all channels on this controller */
212     for (unit = 0; unit < ctlr->channels; unit++) {
213 	if (unit == 0 && (pci_get_progif(dev) & 0x81) == 0x80) {
214 	    device_add_child(dev, "ata", unit);
215 	    continue;
216 	}
217 	if (unit == 1 && (pci_get_progif(dev) & 0x84) == 0x80) {
218 	    device_add_child(dev, "ata", unit);
219 	    continue;
220 	}
221 	device_add_child(dev, "ata", devclass_find_free_unit(ata_devclass, 2));
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 	free(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 #ifdef __alpha__
292 	    res = alpha_platform_alloc_ide_intr(unit);
293 #else
294 	    int irq = (unit == 0 ? 14 : 15);
295 
296 	    res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
297 				     SYS_RES_IRQ, rid, irq, irq, 1, flags);
298 #endif
299 	}
300 	else
301 	    res = controller->r_irq;
302     }
303     return res;
304 }
305 
306 int
307 ata_pci_release_resource(device_t dev, device_t child, int type, int rid,
308 			 struct resource *r)
309 {
310     int unit = ((struct ata_channel *)device_get_softc(child))->unit;
311 
312     if (type == SYS_RES_IOPORT) {
313 	switch (rid) {
314 	case ATA_IOADDR_RID:
315 	    return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
316 					SYS_RES_IOPORT,
317 					PCIR_BAR(0) + (unit << 3), r);
318 	    break;
319 
320 	case ATA_CTLADDR_RID:
321 	    return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
322 					SYS_RES_IOPORT,
323 					PCIR_BAR(1) + (unit << 3), r);
324 	    break;
325 	default:
326 	    return ENOENT;
327 	}
328     }
329     if (type == SYS_RES_IRQ) {
330 	if (rid != ATA_IRQ_RID)
331 	    return ENOENT;
332 
333 	if (ata_legacy(dev)) {
334 #ifdef __alpha__
335 	    return alpha_platform_release_ide_intr(unit, r);
336 #else
337 	    return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
338 					SYS_RES_IRQ, rid, r);
339 #endif
340 	}
341 	else
342 	    return 0;
343     }
344     return EINVAL;
345 }
346 
347 int
348 ata_pci_setup_intr(device_t dev, device_t child, struct resource *irq,
349 		   int flags, driver_intr_t *function, void *argument,
350 		   void **cookiep)
351 {
352     if (ata_legacy(dev)) {
353 #ifdef __alpha__
354 	return alpha_platform_setup_ide_intr(child, irq, function, argument,
355 					     cookiep);
356 #else
357 	return BUS_SETUP_INTR(device_get_parent(dev), child, irq,
358 			      flags, function, argument, cookiep);
359 #endif
360     }
361     else {
362 	struct ata_pci_controller *controller = device_get_softc(dev);
363 	int unit = ((struct ata_channel *)device_get_softc(child))->unit;
364 
365 	controller->interrupt[unit].function = function;
366 	controller->interrupt[unit].argument = argument;
367 	*cookiep = controller;
368 	return 0;
369     }
370 }
371 
372 int
373 ata_pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
374 		      void *cookie)
375 {
376     if (ata_legacy(dev)) {
377 #ifdef __alpha__
378 	return alpha_platform_teardown_ide_intr(child, irq, cookie);
379 #else
380 	return BUS_TEARDOWN_INTR(device_get_parent(dev), child, irq, cookie);
381 #endif
382     }
383     else {
384 	struct ata_pci_controller *controller = device_get_softc(dev);
385 	int unit = ((struct ata_channel *)device_get_softc(child))->unit;
386 
387 	controller->interrupt[unit].function = NULL;
388 	controller->interrupt[unit].argument = NULL;
389 	return 0;
390     }
391 }
392 
393 int
394 ata_pci_allocate(device_t dev)
395 {
396     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
397     struct ata_channel *ch = device_get_softc(dev);
398     struct resource *io = NULL, *ctlio = NULL;
399     int i, rid;
400 
401     rid = ATA_IOADDR_RID;
402     if (!(io = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE)))
403 	return ENXIO;
404 
405     rid = ATA_CTLADDR_RID;
406     if (!(ctlio = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,RF_ACTIVE))){
407 	bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
408 	return ENXIO;
409     }
410 
411     for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
412 	ch->r_io[i].res = io;
413 	ch->r_io[i].offset = i;
414     }
415     ch->r_io[ATA_CONTROL].res = ctlio;
416     ch->r_io[ATA_CONTROL].offset = ata_legacy(device_get_parent(dev)) ? 0 : 2;
417     ch->r_io[ATA_IDX_ADDR].res = io;
418     ata_default_registers(dev);
419     if (ctlr->r_res1) {
420 	for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {
421 	    ch->r_io[i].res = ctlr->r_res1;
422 	    ch->r_io[i].offset = (i - ATA_BMCMD_PORT) + (ch->unit*ATA_BMIOSIZE);
423 	}
424     }
425 
426     ata_pci_hw(dev);
427     return 0;
428 }
429 
430 void
431 ata_pci_hw(device_t dev)
432 {
433     struct ata_channel *ch = device_get_softc(dev);
434 
435     ata_generic_hw(dev);
436     ch->hw.status = ata_pci_status;
437 }
438 
439 int
440 ata_pci_status(device_t dev)
441 {
442     struct ata_channel *ch = device_get_softc(dev);
443 
444     if ((dumping || !ata_legacy(device_get_parent(dev))) &&
445 	ch->dma && ((ch->flags & ATA_ALWAYS_DMASTAT) ||
446 		    (ch->dma->flags & ATA_DMA_ACTIVE))) {
447 	int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
448 
449 	if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
450 	    ATA_BMSTAT_INTERRUPT)
451 	    return 0;
452 	ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
453 	DELAY(1);
454     }
455     if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) {
456 	DELAY(100);
457 	if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY)
458 	    return 0;
459     }
460     return 1;
461 }
462 
463 static int
464 ata_pci_dmastart(device_t dev)
465 {
466     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
467 
468     ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) |
469 		 (ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR)));
470     ATA_IDX_OUTL(ch, ATA_BMDTP_PORT, ch->dma->sg_bus);
471     ch->dma->flags |= ATA_DMA_ACTIVE;
472     ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
473 		 (ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_WRITE_READ) |
474 		 ((ch->dma->flags & ATA_DMA_READ) ? ATA_BMCMD_WRITE_READ : 0) |
475 		 ATA_BMCMD_START_STOP);
476     return 0;
477 }
478 
479 static int
480 ata_pci_dmastop(device_t dev)
481 {
482     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
483     int error;
484 
485     ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
486 		 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
487     ch->dma->flags &= ~ATA_DMA_ACTIVE;
488     error = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
489     ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
490     return error;
491 }
492 
493 static void
494 ata_pci_dmareset(device_t dev)
495 {
496     struct ata_channel *ch = device_get_softc(dev);
497 
498     ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
499 		 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
500     ch->dma->flags &= ~ATA_DMA_ACTIVE;
501     ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
502     ch->dma->unload(dev);
503 }
504 
505 void
506 ata_pci_dmainit(device_t dev)
507 {
508     struct ata_channel *ch = device_get_softc(dev);
509 
510     ata_dmainit(dev);
511     if (ch->dma) {
512 	ch->dma->start = ata_pci_dmastart;
513 	ch->dma->stop = ata_pci_dmastop;
514 	ch->dma->reset = ata_pci_dmareset;
515     }
516 }
517 
518 static device_method_t ata_pci_methods[] = {
519     /* device interface */
520     DEVMETHOD(device_probe,             ata_pci_probe),
521     DEVMETHOD(device_attach,            ata_pci_attach),
522     DEVMETHOD(device_detach,            ata_pci_detach),
523     DEVMETHOD(device_shutdown,          bus_generic_shutdown),
524     DEVMETHOD(device_suspend,           bus_generic_suspend),
525     DEVMETHOD(device_resume,            bus_generic_resume),
526 
527     /* bus methods */
528     DEVMETHOD(bus_alloc_resource,       ata_pci_alloc_resource),
529     DEVMETHOD(bus_release_resource,     ata_pci_release_resource),
530     DEVMETHOD(bus_activate_resource,    bus_generic_activate_resource),
531     DEVMETHOD(bus_deactivate_resource,  bus_generic_deactivate_resource),
532     DEVMETHOD(bus_setup_intr,           ata_pci_setup_intr),
533     DEVMETHOD(bus_teardown_intr,        ata_pci_teardown_intr),
534 
535     { 0, 0 }
536 };
537 
538 devclass_t atapci_devclass;
539 
540 static driver_t ata_pci_driver = {
541     "atapci",
542     ata_pci_methods,
543     sizeof(struct ata_pci_controller),
544 };
545 
546 DRIVER_MODULE(atapci, pci, ata_pci_driver, atapci_devclass, 0, 0);
547 MODULE_VERSION(atapci, 1);
548 MODULE_DEPEND(atapci, ata, 1, 1, 1);
549 
550 static int
551 ata_pcichannel_probe(device_t dev)
552 {
553     struct ata_channel *ch = device_get_softc(dev);
554     device_t *children;
555     int count, i;
556     char buffer[32];
557 
558     /* take care of green memory */
559     bzero(ch, sizeof(struct ata_channel));
560 
561     /* find channel number on this controller */
562     device_get_children(device_get_parent(dev), &children, &count);
563     for (i = 0; i < count; i++) {
564 	if (children[i] == dev)
565 	    ch->unit = i;
566     }
567     free(children, M_TEMP);
568 
569     sprintf(buffer, "ATA channel %d", ch->unit);
570     device_set_desc_copy(dev, buffer);
571 
572     return ata_probe(dev);
573 }
574 
575 static int
576 ata_pcichannel_attach(device_t dev)
577 {
578     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
579     struct ata_channel *ch = device_get_softc(dev);
580     int error;
581 
582     if (ctlr->dmainit)
583 	ctlr->dmainit(dev);
584     if (ch->dma)
585 	ch->dma->alloc(dev);
586 
587     if ((error = ctlr->allocate(dev)))
588 	return error;
589 
590     return ata_attach(dev);
591 }
592 
593 static int
594 ata_pcichannel_detach(device_t dev)
595 {
596     struct ata_channel *ch = device_get_softc(dev);
597     int error;
598 
599     if ((error = ata_detach(dev)))
600 	return error;
601 
602     if (ch->dma)
603 	ch->dma->free(dev);
604 
605     /* XXX SOS free resources for io and ctlio ?? */
606 
607     return 0;
608 }
609 
610 static int
611 ata_pcichannel_locking(device_t dev, int mode)
612 {
613     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
614     struct ata_channel *ch = device_get_softc(dev);
615 
616     if (ctlr->locking)
617 	return ctlr->locking(dev, mode);
618     else
619 	return ch->unit;
620 }
621 
622 static void
623 ata_pcichannel_reset(device_t dev)
624 {
625     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
626     struct ata_channel *ch = device_get_softc(dev);
627 
628     /* if DMA engine present reset it  */
629     if (ch->dma) {
630 	if (ch->dma->reset)
631 	    ch->dma->reset(dev);
632 	ch->dma->unload(dev);
633     }
634 
635     /* reset the controller HW */
636     if (ctlr->reset)
637 	ctlr->reset(dev);
638     else
639 	ata_generic_reset(dev);
640 }
641 
642 static void
643 ata_pcichannel_setmode(device_t parent, device_t dev)
644 {
645     struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
646     struct ata_device *atadev = device_get_softc(dev);
647     int mode = atadev->mode;
648 
649     ctlr->setmode(dev, ATA_PIO_MAX);
650     if (mode >= ATA_DMA)
651 	ctlr->setmode(dev, mode);
652 }
653 
654 static device_method_t ata_pcichannel_methods[] = {
655     /* device interface */
656     DEVMETHOD(device_probe,     ata_pcichannel_probe),
657     DEVMETHOD(device_attach,    ata_pcichannel_attach),
658     DEVMETHOD(device_detach,    ata_pcichannel_detach),
659     DEVMETHOD(device_shutdown,  bus_generic_shutdown),
660     DEVMETHOD(device_suspend,   ata_suspend),
661     DEVMETHOD(device_resume,    ata_resume),
662 
663     /* ATA methods */
664     DEVMETHOD(ata_setmode,      ata_pcichannel_setmode),
665     DEVMETHOD(ata_locking,      ata_pcichannel_locking),
666     DEVMETHOD(ata_reset,        ata_pcichannel_reset),
667 
668     { 0, 0 }
669 };
670 
671 driver_t ata_pcichannel_driver = {
672     "ata",
673     ata_pcichannel_methods,
674     sizeof(struct ata_channel),
675 };
676 
677 DRIVER_MODULE(ata, atapci, ata_pcichannel_driver, ata_devclass, 0, 0);
678