]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/ata/ata-pci.c
MFC 064009e79462:
[FreeBSD/FreeBSD.git] / sys / dev / ata / ata-pci.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1998 - 2008 Søren Schmidt <sos@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer,
12  *    without modification, immediately at the beginning of the file.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/ata.h>
37 #include <sys/bus.h>
38 #include <sys/conf.h>
39 #include <sys/malloc.h>
40 #include <sys/sema.h>
41 #include <sys/taskqueue.h>
42 #include <vm/uma.h>
43 #include <machine/stdarg.h>
44 #include <machine/resource.h>
45 #include <machine/bus.h>
46 #include <sys/rman.h>
47 #include <dev/pci/pcivar.h>
48 #include <dev/pci/pcireg.h>
49 #include <dev/ata/ata-all.h>
50 #include <dev/ata/ata-pci.h>
51 #include <ata_if.h>
52
53 MALLOC_DEFINE(M_ATAPCI, "ata_pci", "ATA driver PCI");
54
55 /* misc defines */
56 #define IOMASK                  0xfffffffc
57
58 /*
59  * generic PCI ATA device probe
60  */
61 int
62 ata_pci_probe(device_t dev)
63 {
64     struct ata_pci_controller *ctlr = device_get_softc(dev);
65     char buffer[64];
66
67     /* is this a storage class device ? */
68     if (pci_get_class(dev) != PCIC_STORAGE)
69         return (ENXIO);
70
71     /* is this an IDE/ATA type device ? */
72     if (pci_get_subclass(dev) != PCIS_STORAGE_IDE)
73         return (ENXIO);
74     
75     sprintf(buffer, "%s ATA controller", ata_pcivendor2str(dev));
76     device_set_desc_copy(dev, buffer);
77     ctlr->chipinit = ata_generic_chipinit;
78
79     /* we are a low priority handler */
80     return (BUS_PROBE_GENERIC);
81 }
82
83 int
84 ata_pci_attach(device_t dev)
85 {
86     struct ata_pci_controller *ctlr = device_get_softc(dev);
87     device_t child;
88     u_int32_t cmd;
89     int unit;
90
91     /* do chipset specific setups only needed once */
92     ctlr->legacy = ata_legacy(dev);
93     if (ctlr->legacy || pci_read_config(dev, PCIR_BAR(2), 4) & IOMASK)
94         ctlr->channels = 2;
95     else
96         ctlr->channels = 1;
97     ctlr->ichannels = -1;
98     ctlr->ch_attach = ata_pci_ch_attach;
99     ctlr->ch_detach = ata_pci_ch_detach;
100     ctlr->dev = dev;
101
102     /* if needed try to enable busmastering */
103     pci_enable_busmaster(dev);
104     cmd = pci_read_config(dev, PCIR_COMMAND, 2);
105
106     /* if busmastering mode "stuck" use it */
107     if ((cmd & PCIM_CMD_BUSMASTEREN) == PCIM_CMD_BUSMASTEREN) {
108         ctlr->r_type1 = SYS_RES_IOPORT;
109         ctlr->r_rid1 = ATA_BMADDR_RID;
110         ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1, &ctlr->r_rid1,
111                                               RF_ACTIVE);
112     }
113
114     if (ctlr->chipinit(dev)) {
115         if (ctlr->r_res1)
116             bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1,
117                                  ctlr->r_res1);
118         return ENXIO;
119     }
120
121     /* attach all channels on this controller */
122     for (unit = 0; unit < ctlr->channels; unit++) {
123         if ((ctlr->ichannels & (1 << unit)) == 0)
124             continue;
125         child = device_add_child(dev, "ata",
126             ((unit == 0 || unit == 1) && ctlr->legacy) ?
127             unit : devclass_find_free_unit(ata_devclass, 2));
128         if (child == NULL)
129             device_printf(dev, "failed to add ata child device\n");
130         else
131             device_set_ivars(child, (void *)(intptr_t)unit);
132     }
133     bus_generic_attach(dev);
134     return 0;
135 }
136
137 int
138 ata_pci_detach(device_t dev)
139 {
140     struct ata_pci_controller *ctlr = device_get_softc(dev);
141
142     /* detach & delete all children */
143     device_delete_children(dev);
144
145     if (ctlr->r_irq) {
146         bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle);
147         bus_release_resource(dev, SYS_RES_IRQ, ctlr->r_irq_rid, ctlr->r_irq);
148         if (ctlr->r_irq_rid != ATA_IRQ_RID)
149             pci_release_msi(dev);
150     }
151     if (ctlr->chipdeinit != NULL)
152         ctlr->chipdeinit(dev);
153     if (ctlr->r_res2) {
154 #ifdef __sparc64__
155         bus_space_unmap(rman_get_bustag(ctlr->r_res2),
156             rman_get_bushandle(ctlr->r_res2), rman_get_size(ctlr->r_res2));
157 #endif
158         bus_release_resource(dev, ctlr->r_type2, ctlr->r_rid2, ctlr->r_res2);
159     }
160     if (ctlr->r_res1) {
161 #ifdef __sparc64__
162         bus_space_unmap(rman_get_bustag(ctlr->r_res1),
163             rman_get_bushandle(ctlr->r_res1), rman_get_size(ctlr->r_res1));
164 #endif
165         bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1, ctlr->r_res1);
166     }
167
168     return 0;
169 }
170
171 int
172 ata_pci_suspend(device_t dev)
173 {
174     struct ata_pci_controller *ctlr = device_get_softc(dev);
175     int error = 0;
176  
177     bus_generic_suspend(dev);
178     if (ctlr->suspend)
179         error = ctlr->suspend(dev);
180     return error;
181 }
182   
183 int
184 ata_pci_resume(device_t dev)
185 {
186     struct ata_pci_controller *ctlr = device_get_softc(dev);
187     int error = 0;
188  
189     if (ctlr->resume)
190         error = ctlr->resume(dev);
191     bus_generic_resume(dev);
192     return error;
193 }
194
195 int
196 ata_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
197 {
198
199         return (BUS_READ_IVAR(device_get_parent(dev), dev, which, result));
200 }
201
202 int
203 ata_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
204 {
205
206         return (BUS_WRITE_IVAR(device_get_parent(dev), dev, which, value));
207 }
208
209 uint32_t
210 ata_pci_read_config(device_t dev, device_t child, int reg, int width)
211 {
212
213         return (pci_read_config(dev, reg, width));
214 }
215
216 void
217 ata_pci_write_config(device_t dev, device_t child, int reg, 
218     uint32_t val, int width)
219 {
220
221         pci_write_config(dev, reg, val, width);
222 }
223
224 struct resource *
225 ata_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
226                        rman_res_t start, rman_res_t end, rman_res_t count,
227                        u_int flags)
228 {
229         struct ata_pci_controller *controller = device_get_softc(dev);
230         struct resource *res = NULL;
231
232         if (device_get_devclass(child) == ata_devclass) {
233                 int unit = ((struct ata_channel *)device_get_softc(child))->unit;
234                 int myrid;
235
236                 if (type == SYS_RES_IOPORT) {
237                         switch (*rid) {
238                         case ATA_IOADDR_RID:
239                             if (controller->legacy) {
240                                 start = (unit ? ATA_SECONDARY : ATA_PRIMARY);
241                                 count = ATA_IOSIZE;
242                                 end = start + count - 1;
243                             }
244                             myrid = PCIR_BAR(0) + (unit << 3);
245                             res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
246                                 SYS_RES_IOPORT, &myrid,
247                                 start, end, count, flags);
248                             break;
249                         case ATA_CTLADDR_RID:
250                             if (controller->legacy) {
251                                 start = (unit ? ATA_SECONDARY : ATA_PRIMARY) +
252                                     ATA_CTLOFFSET;
253                                 count = ATA_CTLIOSIZE;
254                                 end = start + count - 1;
255                             }
256                             myrid = PCIR_BAR(1) + (unit << 3);
257                             res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
258                                 SYS_RES_IOPORT, &myrid,
259                                 start, end, count, flags);
260                             break;
261                         }
262                 }
263                 if (type == SYS_RES_IRQ && *rid == ATA_IRQ_RID) {
264                         if (controller->legacy) {
265                             int irq = (unit == 0 ? 14 : 15);
266             
267                             res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
268                                 SYS_RES_IRQ, rid, irq, irq, 1, flags);
269                         } else
270                             res = controller->r_irq;
271                 }
272         } else {
273                 if (type == SYS_RES_IRQ) {
274                         if (*rid != ATA_IRQ_RID)
275                                 return (NULL);
276                         res = controller->r_irq;
277                 } else {
278                         res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
279                              type, rid, start, end, count, flags);
280                 }
281         }
282         return (res);
283 }
284
285 int
286 ata_pci_release_resource(device_t dev, device_t child, int type, int rid,
287                          struct resource *r)
288 {
289
290         if (device_get_devclass(child) == ata_devclass) {
291                 struct ata_pci_controller *controller = device_get_softc(dev);
292                 int unit = ((struct ata_channel *)device_get_softc(child))->unit;
293
294                 if (type == SYS_RES_IOPORT) {
295                         switch (rid) {
296                         case ATA_IOADDR_RID:
297                             return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
298                                 SYS_RES_IOPORT,
299                                 PCIR_BAR(0) + (unit << 3), r);
300                         case ATA_CTLADDR_RID:
301                             return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
302                                 SYS_RES_IOPORT,
303                                 PCIR_BAR(1) + (unit << 3), r);
304                         default:
305                             return ENOENT;
306                         }
307                 }
308                 if (type == SYS_RES_IRQ) {
309                         if (rid != ATA_IRQ_RID)
310                                 return ENOENT;
311                         if (controller->legacy) {
312                                 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
313                                     SYS_RES_IRQ, rid, r);
314                         } else  
315                                 return 0;
316                 }
317         } else {
318                 if (type == SYS_RES_IRQ) {
319                         if (rid != ATA_IRQ_RID)
320                                 return (ENOENT);
321                         return (0);
322                 } else {
323                         return (BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
324                             type, rid, r));
325                 }
326         }
327         return (EINVAL);
328 }
329
330 int
331 ata_pci_setup_intr(device_t dev, device_t child, struct resource *irq, 
332                    int flags, driver_filter_t *filter, driver_intr_t *function, 
333                    void *argument, void **cookiep)
334 {
335         struct ata_pci_controller *controller = device_get_softc(dev);
336
337         if (controller->legacy) {
338                 return BUS_SETUP_INTR(device_get_parent(dev), child, irq,
339                               flags, filter, function, argument, cookiep);
340         } else {
341                 struct ata_pci_controller *controller = device_get_softc(dev);
342                 int unit;
343
344                 if (filter != NULL) {
345                         printf("ata-pci.c: we cannot use a filter here\n");
346                         return (EINVAL);
347                 }
348                 if (device_get_devclass(child) == ata_devclass)
349                         unit = ((struct ata_channel *)device_get_softc(child))->unit;
350                 else
351                         unit = ATA_PCI_MAX_CH - 1;
352                 controller->interrupt[unit].function = function;
353                 controller->interrupt[unit].argument = argument;
354                 *cookiep = controller;
355                 return 0;
356         }
357 }
358
359 int
360 ata_pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
361                       void *cookie)
362 {
363         struct ata_pci_controller *controller = device_get_softc(dev);
364
365         if (controller->legacy) {
366                 return BUS_TEARDOWN_INTR(device_get_parent(dev), child, irq, cookie);
367         } else {
368                 struct ata_pci_controller *controller = device_get_softc(dev);
369                 int unit;
370
371                 if (device_get_devclass(child) == ata_devclass)
372                         unit = ((struct ata_channel *)device_get_softc(child))->unit;
373                 else
374                         unit = ATA_PCI_MAX_CH - 1;
375                 controller->interrupt[unit].function = NULL;
376                 controller->interrupt[unit].argument = NULL;
377                 return 0;
378         }
379 }
380     
381 int
382 ata_generic_setmode(device_t dev, int target, int mode)
383 {
384
385         return (min(mode, ATA_UDMA2));
386 }
387
388 int
389 ata_generic_chipinit(device_t dev)
390 {
391     struct ata_pci_controller *ctlr = device_get_softc(dev);
392
393     if (ata_setup_interrupt(dev, ata_generic_intr))
394         return ENXIO;
395     ctlr->setmode = ata_generic_setmode;
396     return 0;
397 }
398
399 int
400 ata_pci_ch_attach(device_t dev)
401 {
402     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
403     struct ata_channel *ch = device_get_softc(dev);
404     struct resource *io = NULL, *ctlio = NULL;
405     int i, rid;
406
407     rid = ATA_IOADDR_RID;
408     if (!(io = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE)))
409         return ENXIO;
410
411     rid = ATA_CTLADDR_RID;
412     if (!(ctlio = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,RF_ACTIVE))){
413         bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
414         return ENXIO;
415     }
416
417     ata_pci_dmainit(dev);
418
419     for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
420         ch->r_io[i].res = io;
421         ch->r_io[i].offset = i;
422     }
423     ch->r_io[ATA_CONTROL].res = ctlio;
424     ch->r_io[ATA_CONTROL].offset = ctlr->legacy ? 0 : 2;
425     ch->r_io[ATA_IDX_ADDR].res = io;
426     ata_default_registers(dev);
427     if (ctlr->r_res1) {
428         for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {
429             ch->r_io[i].res = ctlr->r_res1;
430             ch->r_io[i].offset = (i - ATA_BMCMD_PORT) + (ch->unit*ATA_BMIOSIZE);
431         }
432     }
433
434     ata_pci_hw(dev);
435     return 0;
436 }
437
438 int
439 ata_pci_ch_detach(device_t dev)
440 {
441     struct ata_channel *ch = device_get_softc(dev);
442
443     ata_pci_dmafini(dev);
444
445     bus_release_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID,
446         ch->r_io[ATA_CONTROL].res);
447     bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID,
448         ch->r_io[ATA_IDX_ADDR].res);
449
450     return (0);
451 }
452
453 int
454 ata_pci_status(device_t dev)
455 {
456     struct ata_pci_controller *controller =
457         device_get_softc(device_get_parent(dev));
458     struct ata_channel *ch = device_get_softc(dev);
459
460     if ((dumping || !controller->legacy) &&
461         ((ch->flags & ATA_ALWAYS_DMASTAT) ||
462          (ch->dma.flags & ATA_DMA_ACTIVE))) {
463         int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
464
465         if ((bmstat & ATA_BMSTAT_INTERRUPT) == 0)
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(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 void
549 ata_pci_dmafini(device_t dev)
550 {
551
552     ata_dmafini(dev);
553 }
554
555 int
556 ata_pci_print_child(device_t dev, device_t child)
557 {
558         int retval;
559
560         retval = bus_print_child_header(dev, child);
561         retval += printf(" at channel %d",
562             (int)(intptr_t)device_get_ivars(child));
563         retval += bus_print_child_footer(dev, child);
564
565         return (retval);
566 }
567
568 int
569 ata_pci_child_location_str(device_t dev, device_t child, char *buf,
570     size_t buflen)
571 {
572
573         snprintf(buf, buflen, "channel=%d",
574             (int)(intptr_t)device_get_ivars(child));
575         return (0);
576 }
577
578 static bus_dma_tag_t
579 ata_pci_get_dma_tag(device_t bus, device_t child)
580 {
581
582         return (bus_get_dma_tag(bus));
583 }
584
585 static device_method_t ata_pci_methods[] = {
586     /* device interface */
587     DEVMETHOD(device_probe,             ata_pci_probe),
588     DEVMETHOD(device_attach,            ata_pci_attach),
589     DEVMETHOD(device_detach,            ata_pci_detach),
590     DEVMETHOD(device_suspend,           ata_pci_suspend),
591     DEVMETHOD(device_resume,            ata_pci_resume),
592     DEVMETHOD(device_shutdown,          bus_generic_shutdown),
593
594     /* bus methods */
595     DEVMETHOD(bus_read_ivar,            ata_pci_read_ivar),
596     DEVMETHOD(bus_write_ivar,           ata_pci_write_ivar),
597     DEVMETHOD(bus_alloc_resource,       ata_pci_alloc_resource),
598     DEVMETHOD(bus_release_resource,     ata_pci_release_resource),
599     DEVMETHOD(bus_activate_resource,    bus_generic_activate_resource),
600     DEVMETHOD(bus_deactivate_resource,  bus_generic_deactivate_resource),
601     DEVMETHOD(bus_setup_intr,           ata_pci_setup_intr),
602     DEVMETHOD(bus_teardown_intr,        ata_pci_teardown_intr),
603     DEVMETHOD(pci_read_config,          ata_pci_read_config),
604     DEVMETHOD(pci_write_config,         ata_pci_write_config),
605     DEVMETHOD(bus_print_child,          ata_pci_print_child),
606     DEVMETHOD(bus_child_location_str,   ata_pci_child_location_str),
607     DEVMETHOD(bus_get_dma_tag,          ata_pci_get_dma_tag),
608
609     DEVMETHOD_END
610 };
611
612 devclass_t ata_pci_devclass;
613
614 static driver_t ata_pci_driver = {
615     "atapci",
616     ata_pci_methods,
617     sizeof(struct ata_pci_controller),
618 };
619
620 DRIVER_MODULE(atapci, pci, ata_pci_driver, ata_pci_devclass, NULL, NULL);
621 MODULE_VERSION(atapci, 1);
622 MODULE_DEPEND(atapci, ata, 1, 1, 1);
623
624 static int
625 ata_pcichannel_probe(device_t dev)
626 {
627
628     if ((intptr_t)device_get_ivars(dev) < 0)
629             return (ENXIO);
630     device_set_desc(dev, "ATA channel");
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     struct ata_channel *ch = device_get_softc(dev);
640     int error;
641
642     if (ch->attached)
643         return (0);
644     ch->attached = 1;
645
646     ch->dev = dev;
647     ch->unit = (intptr_t)device_get_ivars(dev);
648
649     resource_int_value(device_get_name(dev),
650         device_get_unit(dev), "pm_level", &ch->pm_level);
651
652     if ((error = ctlr->ch_attach(dev)))
653         return error;
654
655     return ata_attach(dev);
656 }
657
658 static int
659 ata_pcichannel_detach(device_t dev)
660 {
661     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
662     struct ata_channel *ch = device_get_softc(dev);
663     int error;
664
665     if (!ch->attached)
666         return (0);
667     ch->attached = 0;
668
669     if ((error = ata_detach(dev)))
670         return error;
671
672     if (ctlr->ch_detach)
673         return (ctlr->ch_detach(dev));
674
675     return (0);
676 }
677 static int
678 ata_pcichannel_suspend(device_t dev)
679 {
680     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
681     struct ata_channel *ch = device_get_softc(dev);
682     int error;
683
684     if (!ch->attached)
685         return (0);
686
687     if ((error = ata_suspend(dev)))
688         return (error);
689
690     if (ctlr->ch_suspend != NULL && (error = ctlr->ch_suspend(dev)))
691         return (error);
692
693     return (0);
694 }
695
696 static int
697 ata_pcichannel_resume(device_t dev)
698 {
699     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
700     struct ata_channel *ch = device_get_softc(dev);
701     int error;
702
703     if (!ch->attached)
704         return (0);
705
706     if (ctlr->ch_resume != NULL && (error = ctlr->ch_resume(dev)))
707         return (error);
708
709     return ata_resume(dev);
710 }
711
712 static void
713 ata_pcichannel_reset(device_t dev)
714 {
715     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
716     struct ata_channel *ch = device_get_softc(dev);
717
718     /* if DMA engine present reset it  */
719     if (ch->dma.reset)
720         ch->dma.reset(dev);
721
722     /* reset the controller HW */
723     if (ctlr->reset)
724         ctlr->reset(dev);
725     else
726         ata_generic_reset(dev);
727 }
728
729 static int
730 ata_pcichannel_setmode(device_t dev, int target, int mode)
731 {
732         struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
733
734         if (ctlr->setmode)
735                 return (ctlr->setmode(dev, target, mode));
736         else
737                 return (ata_generic_setmode(dev, target, mode));
738 }
739
740 static int
741 ata_pcichannel_getrev(device_t dev, int target)
742 {
743         struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
744         struct ata_channel *ch = device_get_softc(dev);
745
746         if (ch->flags & ATA_SATA) {
747                 if (ctlr->getrev)
748                         return (ctlr->getrev(dev, target));
749                 else 
750                         return (0xff);
751         } else
752                 return (0);
753 }
754
755 static device_method_t ata_pcichannel_methods[] = {
756     /* device interface */
757     DEVMETHOD(device_probe,     ata_pcichannel_probe),
758     DEVMETHOD(device_attach,    ata_pcichannel_attach),
759     DEVMETHOD(device_detach,    ata_pcichannel_detach),
760     DEVMETHOD(device_shutdown,  bus_generic_shutdown),
761     DEVMETHOD(device_suspend,   ata_pcichannel_suspend),
762     DEVMETHOD(device_resume,    ata_pcichannel_resume),
763
764     /* ATA methods */
765     DEVMETHOD(ata_setmode,      ata_pcichannel_setmode),
766     DEVMETHOD(ata_getrev,       ata_pcichannel_getrev),
767     DEVMETHOD(ata_reset,        ata_pcichannel_reset),
768
769     DEVMETHOD_END
770 };
771
772 driver_t ata_pcichannel_driver = {
773     "ata",
774     ata_pcichannel_methods,
775     sizeof(struct ata_channel),
776 };
777
778 DRIVER_MODULE(ata, atapci, ata_pcichannel_driver, ata_devclass, NULL, NULL);
779
780 /*
781  * misc support fucntions
782  */
783 int
784 ata_legacy(device_t dev)
785 {
786     return (((pci_read_config(dev, PCIR_SUBCLASS, 1) == PCIS_STORAGE_IDE) &&
787              (pci_read_config(dev, PCIR_PROGIF, 1)&PCIP_STORAGE_IDE_MASTERDEV)&&
788              ((pci_read_config(dev, PCIR_PROGIF, 1) &
789                (PCIP_STORAGE_IDE_MODEPRIM | PCIP_STORAGE_IDE_MODESEC)) !=
790               (PCIP_STORAGE_IDE_MODEPRIM | PCIP_STORAGE_IDE_MODESEC))) ||
791             (!pci_read_config(dev, PCIR_BAR(0), 4) &&
792              !pci_read_config(dev, PCIR_BAR(1), 4) &&
793              !pci_read_config(dev, PCIR_BAR(2), 4) &&
794              !pci_read_config(dev, PCIR_BAR(3), 4) &&
795              !pci_read_config(dev, PCIR_BAR(5), 4)));
796 }
797
798 void
799 ata_generic_intr(void *data)
800 {
801     struct ata_pci_controller *ctlr = data;
802     struct ata_channel *ch;
803     int unit;
804
805     for (unit = 0; unit < ATA_PCI_MAX_CH; unit++) {
806         if ((ch = ctlr->interrupt[unit].argument))
807             ctlr->interrupt[unit].function(ch);
808     }
809 }
810
811 int
812 ata_setup_interrupt(device_t dev, void *intr_func)
813 {
814     struct ata_pci_controller *ctlr = device_get_softc(dev);
815     int i, msi = 0;
816
817     if (!ctlr->legacy) {
818         if (resource_int_value(device_get_name(dev),
819                 device_get_unit(dev), "msi", &i) == 0 && i != 0)
820             msi = 1;
821         if (msi && pci_msi_count(dev) > 0 && pci_alloc_msi(dev, &msi) == 0) {
822             ctlr->r_irq_rid = 0x1;
823         } else {
824             msi = 0;
825             ctlr->r_irq_rid = ATA_IRQ_RID;
826         }
827         if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
828                 &ctlr->r_irq_rid, RF_SHAREABLE | RF_ACTIVE))) {
829             device_printf(dev, "unable to map interrupt\n");
830             if (msi)
831                     pci_release_msi(dev);
832             return ENXIO;
833         }
834         if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS, NULL,
835                             intr_func, ctlr, &ctlr->handle))) {
836             device_printf(dev, "unable to setup interrupt\n");
837             bus_release_resource(dev,
838                 SYS_RES_IRQ, ctlr->r_irq_rid, ctlr->r_irq);
839             if (msi)
840                     pci_release_msi(dev);
841             return ENXIO;
842         }
843     }
844     return 0;
845 }
846
847 void
848 ata_set_desc(device_t dev)
849 {
850     struct ata_pci_controller *ctlr = device_get_softc(dev);
851     char buffer[128];
852
853     sprintf(buffer, "%s %s %s controller",
854             ata_pcivendor2str(dev), ctlr->chip->text, 
855             ata_mode2str(ctlr->chip->max_dma));
856     device_set_desc_copy(dev, buffer);
857 }
858
859 const struct ata_chip_id *
860 ata_match_chip(device_t dev, const struct ata_chip_id *index)
861 {
862     uint32_t devid;
863     uint8_t revid;
864
865     devid = pci_get_devid(dev);
866     revid = pci_get_revid(dev);
867     while (index->chipid != 0) {
868         if (devid == index->chipid && revid >= index->chiprev)
869             return (index);
870         index++;
871     }
872     return (NULL);
873 }
874
875 const struct ata_chip_id *
876 ata_find_chip(device_t dev, const struct ata_chip_id *index, int slot)
877 {
878     const struct ata_chip_id *idx;
879     device_t *children;
880     int nchildren, i;
881     uint8_t s;
882
883     if (device_get_children(device_get_parent(dev), &children, &nchildren))
884         return (NULL);
885
886     for (i = 0; i < nchildren; i++) {
887         s = pci_get_slot(children[i]);
888         if ((slot >= 0 && s == slot) || (slot < 0 && s <= -slot)) {
889             idx = ata_match_chip(children[i], index);
890             if (idx != NULL) {
891                 free(children, M_TEMP);
892                 return (idx);
893             }
894         }
895     }
896     free(children, M_TEMP);
897     return (NULL);
898 }
899
900 const char *
901 ata_pcivendor2str(device_t dev)
902 {
903     switch (pci_get_vendor(dev)) {
904     case ATA_ACARD_ID:          return "Acard";
905     case ATA_ACER_LABS_ID:      return "AcerLabs";
906     case ATA_AMD_ID:            return "AMD";
907     case ATA_ADAPTEC_ID:        return "Adaptec";
908     case ATA_ATI_ID:            return "ATI";
909     case ATA_CYRIX_ID:          return "Cyrix";
910     case ATA_CYPRESS_ID:        return "Cypress";
911     case ATA_HIGHPOINT_ID:      return "HighPoint";
912     case ATA_INTEL_ID:          return "Intel";
913     case ATA_ITE_ID:            return "ITE";
914     case ATA_JMICRON_ID:        return "JMicron";
915     case ATA_MARVELL_ID:        return "Marvell";
916     case ATA_MARVELL2_ID:       return "Marvell";
917     case ATA_NATIONAL_ID:       return "National";
918     case ATA_NETCELL_ID:        return "Netcell";
919     case ATA_NVIDIA_ID:         return "nVidia";
920     case ATA_PROMISE_ID:        return "Promise";
921     case ATA_SERVERWORKS_ID:    return "ServerWorks";
922     case ATA_SILICON_IMAGE_ID:  return "SiI";
923     case ATA_SIS_ID:            return "SiS";
924     case ATA_VIA_ID:            return "VIA";
925     case ATA_CENATEK_ID:        return "Cenatek";
926     case ATA_MICRON_ID:         return "Micron";
927     default:                    return "Generic";
928     }
929 }
930
931 int
932 ata_mode2idx(int mode)
933 {
934     if ((mode & ATA_DMA_MASK) == ATA_UDMA0)
935         return (mode & ATA_MODE_MASK) + 8;
936     if ((mode & ATA_DMA_MASK) == ATA_WDMA0)
937         return (mode & ATA_MODE_MASK) + 5;
938     return (mode & ATA_MODE_MASK) - ATA_PIO0;
939 }