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