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