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