]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/dev/ata/ata-pci.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / dev / ata / ata-pci.c
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 MALLOC_DEFINE(M_ATAPCI, "ata_pci", "ATA driver PCI");
53
54 /* misc defines */
55 #define IOMASK                  0xfffffffc
56
57 /*
58  * generic PCI ATA device probe
59  */
60 int
61 ata_pci_probe(device_t dev)
62 {
63     struct ata_pci_controller *ctlr = device_get_softc(dev);
64     char buffer[64];
65
66     /* is this a storage class device ? */
67     if (pci_get_class(dev) != PCIC_STORAGE)
68         return (ENXIO);
69
70     /* is this an IDE/ATA type device ? */
71     if (pci_get_subclass(dev) != PCIS_STORAGE_IDE)
72         return (ENXIO);
73     
74     sprintf(buffer, "%s ATA controller", ata_pcivendor2str(dev));
75     device_set_desc_copy(dev, buffer);
76     ctlr->chipinit = ata_generic_chipinit;
77
78     /* we are a low priority handler */
79     return (BUS_PROBE_GENERIC);
80 }
81
82 int
83 ata_pci_attach(device_t dev)
84 {
85     struct ata_pci_controller *ctlr = device_get_softc(dev);
86     device_t child;
87     u_int32_t cmd;
88     int unit;
89
90     /* do chipset specific setups only needed once */
91     ctlr->legacy = ata_legacy(dev);
92     if (ctlr->legacy || pci_read_config(dev, PCIR_BAR(2), 4) & IOMASK)
93         ctlr->channels = 2;
94     else
95         ctlr->channels = 1;
96     ctlr->ichannels = -1;
97     ctlr->ch_attach = ata_pci_ch_attach;
98     ctlr->ch_detach = ata_pci_ch_detach;
99     ctlr->dev = dev;
100
101     /* if needed try to enable busmastering */
102     cmd = pci_read_config(dev, PCIR_COMMAND, 2);
103     if (!(cmd & PCIM_CMD_BUSMASTEREN)) {
104         pci_write_config(dev, PCIR_COMMAND, cmd | PCIM_CMD_BUSMASTEREN, 2);
105         cmd = pci_read_config(dev, PCIR_COMMAND, 2);
106     }
107
108     /* if busmastering mode "stuck" use it */
109     if ((cmd & PCIM_CMD_BUSMASTEREN) == PCIM_CMD_BUSMASTEREN) {
110         ctlr->r_type1 = SYS_RES_IOPORT;
111         ctlr->r_rid1 = ATA_BMADDR_RID;
112         ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1, &ctlr->r_rid1,
113                                               RF_ACTIVE);
114     }
115
116     if (ctlr->chipinit(dev))
117         return ENXIO;
118
119     /* attach all channels on this controller */
120     for (unit = 0; unit < ctlr->channels; unit++) {
121         if ((ctlr->ichannels & (1 << unit)) == 0)
122             continue;
123         child = device_add_child(dev, "ata",
124             ((unit == 0 || unit == 1) && ctlr->legacy) ?
125             unit : devclass_find_free_unit(ata_devclass, 2));
126         if (child == NULL)
127             device_printf(dev, "failed to add ata child device\n");
128         else
129             device_set_ivars(child, (void *)(intptr_t)unit);
130     }
131     bus_generic_attach(dev);
132     return 0;
133 }
134
135 int
136 ata_pci_detach(device_t dev)
137 {
138     struct ata_pci_controller *ctlr = device_get_softc(dev);
139
140     /* detach & delete all children */
141     device_delete_children(dev);
142
143     if (ctlr->r_irq) {
144         bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle);
145         bus_release_resource(dev, SYS_RES_IRQ, ctlr->r_irq_rid, ctlr->r_irq);
146         if (ctlr->r_irq_rid != ATA_IRQ_RID)
147             pci_release_msi(dev);
148     }
149     if (ctlr->chipdeinit != NULL)
150         ctlr->chipdeinit(dev);
151     if (ctlr->r_res2) {
152 #ifdef __sparc64__
153         bus_space_unmap(rman_get_bustag(ctlr->r_res2),
154             rman_get_bushandle(ctlr->r_res2), rman_get_size(ctlr->r_res2));
155 #endif
156         bus_release_resource(dev, ctlr->r_type2, ctlr->r_rid2, ctlr->r_res2);
157     }
158     if (ctlr->r_res1) {
159 #ifdef __sparc64__
160         bus_space_unmap(rman_get_bustag(ctlr->r_res1),
161             rman_get_bushandle(ctlr->r_res1), rman_get_size(ctlr->r_res1));
162 #endif
163         bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1, ctlr->r_res1);
164     }
165
166     return 0;
167 }
168
169 int
170 ata_pci_suspend(device_t dev)
171 {
172     struct ata_pci_controller *ctlr = device_get_softc(dev);
173     int error = 0;
174  
175     bus_generic_suspend(dev);
176     if (ctlr->suspend)
177         error = ctlr->suspend(dev);
178     return error;
179 }
180   
181 int
182 ata_pci_resume(device_t dev)
183 {
184     struct ata_pci_controller *ctlr = device_get_softc(dev);
185     int error = 0;
186  
187     if (ctlr->resume)
188         error = ctlr->resume(dev);
189     bus_generic_resume(dev);
190     return error;
191 }
192
193 int
194 ata_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
195 {
196
197         return (BUS_READ_IVAR(device_get_parent(dev), dev, which, result));
198 }
199
200 int
201 ata_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
202 {
203
204         return (BUS_WRITE_IVAR(device_get_parent(dev), dev, which, value));
205 }
206
207 uint32_t
208 ata_pci_read_config(device_t dev, device_t child, int reg, int width)
209 {
210
211         return (pci_read_config(dev, reg, width));
212 }
213
214 void
215 ata_pci_write_config(device_t dev, device_t child, int reg, 
216     uint32_t val, int width)
217 {
218
219         pci_write_config(dev, reg, val, width);
220 }
221
222 struct resource *
223 ata_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
224                        u_long start, u_long end, u_long count, u_int flags)
225 {
226         struct ata_pci_controller *controller = device_get_softc(dev);
227         struct resource *res = NULL;
228
229         if (device_get_devclass(child) == ata_devclass) {
230                 int unit = ((struct ata_channel *)device_get_softc(child))->unit;
231                 int myrid;
232
233                 if (type == SYS_RES_IOPORT) {
234                         switch (*rid) {
235                         case ATA_IOADDR_RID:
236                             if (controller->legacy) {
237                                 start = (unit ? ATA_SECONDARY : ATA_PRIMARY);
238                                 count = ATA_IOSIZE;
239                                 end = start + count - 1;
240                             }
241                             myrid = PCIR_BAR(0) + (unit << 3);
242                             res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
243                                 SYS_RES_IOPORT, &myrid,
244                                 start, end, count, flags);
245                             break;
246                         case ATA_CTLADDR_RID:
247                             if (controller->legacy) {
248                                 start = (unit ? ATA_SECONDARY : ATA_PRIMARY) +
249                                     ATA_CTLOFFSET;
250                                 count = ATA_CTLIOSIZE;
251                                 end = start + count - 1;
252                             }
253                             myrid = PCIR_BAR(1) + (unit << 3);
254                             res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
255                                 SYS_RES_IOPORT, &myrid,
256                                 start, end, count, flags);
257                             break;
258                         }
259                 }
260                 if (type == SYS_RES_IRQ && *rid == ATA_IRQ_RID) {
261                         if (controller->legacy) {
262                             int irq = (unit == 0 ? 14 : 15);
263             
264                             res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
265                                 SYS_RES_IRQ, rid, irq, irq, 1, flags);
266                         } else
267                             res = controller->r_irq;
268                 }
269         } else {
270                 if (type == SYS_RES_IRQ) {
271                         if (*rid != ATA_IRQ_RID)
272                                 return (NULL);
273                         res = controller->r_irq;
274                 } else {
275                         res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
276                              type, rid, start, end, count, flags);
277                 }
278         }
279         return (res);
280 }
281
282 int
283 ata_pci_release_resource(device_t dev, device_t child, int type, int rid,
284                          struct resource *r)
285 {
286
287         if (device_get_devclass(child) == ata_devclass) {
288                 struct ata_pci_controller *controller = device_get_softc(dev);
289                 int unit = ((struct ata_channel *)device_get_softc(child))->unit;
290
291                 if (type == SYS_RES_IOPORT) {
292                         switch (rid) {
293                         case ATA_IOADDR_RID:
294                             return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
295                                 SYS_RES_IOPORT,
296                                 PCIR_BAR(0) + (unit << 3), r);
297                         case ATA_CTLADDR_RID:
298                             return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
299                                 SYS_RES_IOPORT,
300                                 PCIR_BAR(1) + (unit << 3), r);
301                         default:
302                             return ENOENT;
303                         }
304                 }
305                 if (type == SYS_RES_IRQ) {
306                         if (rid != ATA_IRQ_RID)
307                                 return ENOENT;
308                         if (controller->legacy) {
309                                 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
310                                     SYS_RES_IRQ, rid, r);
311                         } else  
312                                 return 0;
313                 }
314         } else {
315                 if (type == SYS_RES_IRQ) {
316                         if (rid != ATA_IRQ_RID)
317                                 return (ENOENT);
318                         return (0);
319                 } else {
320                         return (BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
321                             type, rid, r));
322                 }
323         }
324         return (EINVAL);
325 }
326
327 int
328 ata_pci_setup_intr(device_t dev, device_t child, struct resource *irq, 
329                    int flags, driver_filter_t *filter, driver_intr_t *function, 
330                    void *argument, void **cookiep)
331 {
332         struct ata_pci_controller *controller = device_get_softc(dev);
333
334         if (controller->legacy) {
335                 return BUS_SETUP_INTR(device_get_parent(dev), child, irq,
336                               flags, filter, function, argument, cookiep);
337         } else {
338                 struct ata_pci_controller *controller = device_get_softc(dev);
339                 int unit;
340
341                 if (filter != NULL) {
342                         printf("ata-pci.c: we cannot use a filter here\n");
343                         return (EINVAL);
344                 }
345                 if (device_get_devclass(child) == ata_devclass)
346                         unit = ((struct ata_channel *)device_get_softc(child))->unit;
347                 else
348                         unit = ATA_PCI_MAX_CH - 1;
349                 controller->interrupt[unit].function = function;
350                 controller->interrupt[unit].argument = argument;
351                 *cookiep = controller;
352                 return 0;
353         }
354 }
355
356 int
357 ata_pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
358                       void *cookie)
359 {
360         struct ata_pci_controller *controller = device_get_softc(dev);
361
362         if (controller->legacy) {
363                 return BUS_TEARDOWN_INTR(device_get_parent(dev), child, irq, cookie);
364         } else {
365                 struct ata_pci_controller *controller = device_get_softc(dev);
366                 int unit;
367
368                 if (device_get_devclass(child) == ata_devclass)
369                         unit = ((struct ata_channel *)device_get_softc(child))->unit;
370                 else
371                         unit = ATA_PCI_MAX_CH - 1;
372                 controller->interrupt[unit].function = NULL;
373                 controller->interrupt[unit].argument = NULL;
374                 return 0;
375         }
376 }
377     
378 int
379 ata_generic_setmode(device_t dev, int target, int mode)
380 {
381
382         return (min(mode, ATA_UDMA2));
383 }
384
385 int
386 ata_generic_chipinit(device_t dev)
387 {
388     struct ata_pci_controller *ctlr = device_get_softc(dev);
389
390     if (ata_setup_interrupt(dev, ata_generic_intr))
391         return ENXIO;
392     ctlr->setmode = ata_generic_setmode;
393     return 0;
394 }
395
396 int
397 ata_pci_ch_attach(device_t dev)
398 {
399     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
400     struct ata_channel *ch = device_get_softc(dev);
401     struct resource *io = NULL, *ctlio = NULL;
402     int i, rid;
403
404     rid = ATA_IOADDR_RID;
405     if (!(io = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE)))
406         return ENXIO;
407
408     rid = ATA_CTLADDR_RID;
409     if (!(ctlio = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,RF_ACTIVE))){
410         bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
411         return ENXIO;
412     }
413
414     ata_pci_dmainit(dev);
415
416     for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
417         ch->r_io[i].res = io;
418         ch->r_io[i].offset = i;
419     }
420     ch->r_io[ATA_CONTROL].res = ctlio;
421     ch->r_io[ATA_CONTROL].offset = ctlr->legacy ? 0 : 2;
422     ch->r_io[ATA_IDX_ADDR].res = io;
423     ata_default_registers(dev);
424     if (ctlr->r_res1) {
425         for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {
426             ch->r_io[i].res = ctlr->r_res1;
427             ch->r_io[i].offset = (i - ATA_BMCMD_PORT) + (ch->unit*ATA_BMIOSIZE);
428         }
429     }
430
431     ata_pci_hw(dev);
432     return 0;
433 }
434
435 int
436 ata_pci_ch_detach(device_t dev)
437 {
438     struct ata_channel *ch = device_get_softc(dev);
439
440     ata_pci_dmafini(dev);
441
442     bus_release_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID,
443         ch->r_io[ATA_CONTROL].res);
444     bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID,
445         ch->r_io[ATA_IDX_ADDR].res);
446
447     return (0);
448 }
449
450 int
451 ata_pci_status(device_t dev)
452 {
453     struct ata_pci_controller *controller =
454         device_get_softc(device_get_parent(dev));
455     struct ata_channel *ch = device_get_softc(dev);
456
457     if ((dumping || !controller->legacy) &&
458         ((ch->flags & ATA_ALWAYS_DMASTAT) ||
459          (ch->dma.flags & ATA_DMA_ACTIVE))) {
460         int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
461
462         if ((bmstat & ATA_BMSTAT_INTERRUPT) == 0)
463             return 0;
464         ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
465         DELAY(1);
466     }
467     if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) {
468         DELAY(100);
469         if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY)
470             return 0;
471     }
472     return 1;
473 }
474
475 void
476 ata_pci_hw(device_t dev)
477 {
478     struct ata_channel *ch = device_get_softc(dev);
479
480     ata_generic_hw(dev);
481     ch->hw.status = ata_pci_status;
482 }
483
484 static int
485 ata_pci_dmastart(struct ata_request *request)
486 {
487     struct ata_channel *ch = device_get_softc(request->parent);
488
489     ATA_DEBUG_RQ(request, "dmastart");
490
491     ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) | 
492                  (ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR)));
493     ATA_IDX_OUTL(ch, ATA_BMDTP_PORT, request->dma->sg_bus);
494     ch->dma.flags |= ATA_DMA_ACTIVE;
495     ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
496                  (ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_WRITE_READ) |
497                  ((request->flags & ATA_R_READ) ? ATA_BMCMD_WRITE_READ : 0)|
498                  ATA_BMCMD_START_STOP);
499     return 0;
500 }
501
502 static int
503 ata_pci_dmastop(struct ata_request *request)
504 {
505     struct ata_channel *ch = device_get_softc(request->parent);
506     int error;
507
508     ATA_DEBUG_RQ(request, "dmastop");
509
510     ATA_IDX_OUTB(ch, ATA_BMCMD_PORT, 
511                  ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
512     ch->dma.flags &= ~ATA_DMA_ACTIVE;
513     error = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
514     ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
515     return error;
516 }
517
518 static void
519 ata_pci_dmareset(device_t dev)
520 {
521     struct ata_channel *ch = device_get_softc(dev);
522     struct ata_request *request;
523
524     ATA_IDX_OUTB(ch, ATA_BMCMD_PORT, 
525                  ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
526     ch->dma.flags &= ~ATA_DMA_ACTIVE;
527     ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
528     if ((request = ch->running)) {
529         device_printf(dev, "DMA reset calling unload\n");
530         ch->dma.unload(request);
531     }
532 }
533
534 void
535 ata_pci_dmainit(device_t dev)
536 {
537     struct ata_channel *ch = device_get_softc(dev);
538
539     ata_dmainit(dev);
540     ch->dma.start = ata_pci_dmastart;
541     ch->dma.stop = ata_pci_dmastop;
542     ch->dma.reset = ata_pci_dmareset;
543 }
544
545 void
546 ata_pci_dmafini(device_t dev)
547 {
548
549     ata_dmafini(dev);
550 }
551
552 int
553 ata_pci_print_child(device_t dev, device_t child)
554 {
555         int retval;
556
557         retval = bus_print_child_header(dev, child);
558         retval += printf(" at channel %d",
559             (int)(intptr_t)device_get_ivars(child));
560         retval += bus_print_child_footer(dev, child);
561
562         return (retval);
563 }
564
565 int
566 ata_pci_child_location_str(device_t dev, device_t child, char *buf,
567     size_t buflen)
568 {
569
570         snprintf(buf, buflen, "channel=%d",
571             (int)(intptr_t)device_get_ivars(child));
572         return (0);
573 }
574
575 static bus_dma_tag_t
576 ata_pci_get_dma_tag(device_t bus, device_t child)
577 {
578
579         return (bus_get_dma_tag(bus));
580 }
581
582 static device_method_t ata_pci_methods[] = {
583     /* device interface */
584     DEVMETHOD(device_probe,             ata_pci_probe),
585     DEVMETHOD(device_attach,            ata_pci_attach),
586     DEVMETHOD(device_detach,            ata_pci_detach),
587     DEVMETHOD(device_suspend,           ata_pci_suspend),
588     DEVMETHOD(device_resume,            ata_pci_resume),
589     DEVMETHOD(device_shutdown,          bus_generic_shutdown),
590
591     /* bus methods */
592     DEVMETHOD(bus_read_ivar,            ata_pci_read_ivar),
593     DEVMETHOD(bus_write_ivar,           ata_pci_write_ivar),
594     DEVMETHOD(bus_alloc_resource,       ata_pci_alloc_resource),
595     DEVMETHOD(bus_release_resource,     ata_pci_release_resource),
596     DEVMETHOD(bus_activate_resource,    bus_generic_activate_resource),
597     DEVMETHOD(bus_deactivate_resource,  bus_generic_deactivate_resource),
598     DEVMETHOD(bus_setup_intr,           ata_pci_setup_intr),
599     DEVMETHOD(bus_teardown_intr,        ata_pci_teardown_intr),
600     DEVMETHOD(pci_read_config,          ata_pci_read_config),
601     DEVMETHOD(pci_write_config,         ata_pci_write_config),
602     DEVMETHOD(bus_print_child,          ata_pci_print_child),
603     DEVMETHOD(bus_child_location_str,   ata_pci_child_location_str),
604     DEVMETHOD(bus_get_dma_tag,          ata_pci_get_dma_tag),
605
606     DEVMETHOD_END
607 };
608
609 devclass_t ata_pci_devclass;
610
611 static driver_t ata_pci_driver = {
612     "atapci",
613     ata_pci_methods,
614     sizeof(struct ata_pci_controller),
615 };
616
617 DRIVER_MODULE(atapci, pci, ata_pci_driver, ata_pci_devclass, NULL, NULL);
618 MODULE_VERSION(atapci, 1);
619 MODULE_DEPEND(atapci, ata, 1, 1, 1);
620
621 static int
622 ata_pcichannel_probe(device_t dev)
623 {
624
625     if ((intptr_t)device_get_ivars(dev) < 0)
626             return (ENXIO);
627     device_set_desc(dev, "ATA channel");
628
629     return ata_probe(dev);
630 }
631
632 static int
633 ata_pcichannel_attach(device_t dev)
634 {
635     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
636     struct ata_channel *ch = device_get_softc(dev);
637     int error;
638
639     if (ch->attached)
640         return (0);
641     ch->attached = 1;
642
643     ch->dev = dev;
644     ch->unit = (intptr_t)device_get_ivars(dev);
645
646     resource_int_value(device_get_name(dev),
647         device_get_unit(dev), "pm_level", &ch->pm_level);
648
649     if ((error = ctlr->ch_attach(dev)))
650         return error;
651
652     return ata_attach(dev);
653 }
654
655 static int
656 ata_pcichannel_detach(device_t dev)
657 {
658     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
659     struct ata_channel *ch = device_get_softc(dev);
660     int error;
661
662     if (!ch->attached)
663         return (0);
664     ch->attached = 0;
665
666     if ((error = ata_detach(dev)))
667         return error;
668
669     if (ctlr->ch_detach)
670         return (ctlr->ch_detach(dev));
671
672     return (0);
673 }
674 static int
675 ata_pcichannel_suspend(device_t dev)
676 {
677     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
678     struct ata_channel *ch = device_get_softc(dev);
679     int error;
680
681     if (!ch->attached)
682         return (0);
683
684     if ((error = ata_suspend(dev)))
685         return (error);
686
687     if (ctlr->ch_suspend != NULL && (error = ctlr->ch_suspend(dev)))
688         return (error);
689
690     return (0);
691 }
692
693 static int
694 ata_pcichannel_resume(device_t dev)
695 {
696     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
697     struct ata_channel *ch = device_get_softc(dev);
698     int error;
699
700     if (!ch->attached)
701         return (0);
702
703     if (ctlr->ch_resume != NULL && (error = ctlr->ch_resume(dev)))
704         return (error);
705
706     return ata_resume(dev);
707 }
708
709
710 #ifndef ATA_CAM
711 static int
712 ata_pcichannel_locking(device_t dev, int mode)
713 {
714     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
715     struct ata_channel *ch = device_get_softc(dev);
716
717     if (ctlr->locking)
718         return ctlr->locking(dev, mode);
719     else
720         return ch->unit;
721 }
722 #endif
723
724 static void
725 ata_pcichannel_reset(device_t dev)
726 {
727     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
728     struct ata_channel *ch = device_get_softc(dev);
729
730     /* if DMA engine present reset it  */
731     if (ch->dma.reset)
732         ch->dma.reset(dev);
733
734     /* reset the controller HW */
735     if (ctlr->reset)
736         ctlr->reset(dev);
737     else
738         ata_generic_reset(dev);
739 }
740
741 static int
742 ata_pcichannel_setmode(device_t dev, int target, int mode)
743 {
744         struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
745
746         if (ctlr->setmode)
747                 return (ctlr->setmode(dev, target, mode));
748         else
749                 return (ata_generic_setmode(dev, target, mode));
750 }
751
752 static int
753 ata_pcichannel_getrev(device_t dev, int target)
754 {
755         struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
756         struct ata_channel *ch = device_get_softc(dev);
757
758         if (ch->flags & ATA_SATA) {
759                 if (ctlr->getrev)
760                         return (ctlr->getrev(dev, target));
761                 else 
762                         return (0xff);
763         } else
764                 return (0);
765 }
766
767 static device_method_t ata_pcichannel_methods[] = {
768     /* device interface */
769     DEVMETHOD(device_probe,     ata_pcichannel_probe),
770     DEVMETHOD(device_attach,    ata_pcichannel_attach),
771     DEVMETHOD(device_detach,    ata_pcichannel_detach),
772     DEVMETHOD(device_shutdown,  bus_generic_shutdown),
773     DEVMETHOD(device_suspend,   ata_pcichannel_suspend),
774     DEVMETHOD(device_resume,    ata_pcichannel_resume),
775
776     /* ATA methods */
777     DEVMETHOD(ata_setmode,      ata_pcichannel_setmode),
778     DEVMETHOD(ata_getrev,       ata_pcichannel_getrev),
779 #ifndef ATA_CAM
780     DEVMETHOD(ata_locking,      ata_pcichannel_locking),
781 #endif
782     DEVMETHOD(ata_reset,        ata_pcichannel_reset),
783
784     DEVMETHOD_END
785 };
786
787 driver_t ata_pcichannel_driver = {
788     "ata",
789     ata_pcichannel_methods,
790     sizeof(struct ata_channel),
791 };
792
793 DRIVER_MODULE(ata, atapci, ata_pcichannel_driver, ata_devclass, NULL, NULL);
794
795 /*
796  * misc support fucntions
797  */
798 int
799 ata_legacy(device_t dev)
800 {
801     return (((pci_read_config(dev, PCIR_SUBCLASS, 1) == PCIS_STORAGE_IDE) &&
802              (pci_read_config(dev, PCIR_PROGIF, 1)&PCIP_STORAGE_IDE_MASTERDEV)&&
803              ((pci_read_config(dev, PCIR_PROGIF, 1) &
804                (PCIP_STORAGE_IDE_MODEPRIM | PCIP_STORAGE_IDE_MODESEC)) !=
805               (PCIP_STORAGE_IDE_MODEPRIM | PCIP_STORAGE_IDE_MODESEC))) ||
806             (!pci_read_config(dev, PCIR_BAR(0), 4) &&
807              !pci_read_config(dev, PCIR_BAR(1), 4) &&
808              !pci_read_config(dev, PCIR_BAR(2), 4) &&
809              !pci_read_config(dev, PCIR_BAR(3), 4) &&
810              !pci_read_config(dev, PCIR_BAR(5), 4)));
811 }
812
813 void
814 ata_generic_intr(void *data)
815 {
816     struct ata_pci_controller *ctlr = data;
817     struct ata_channel *ch;
818     int unit;
819
820     for (unit = 0; unit < ATA_PCI_MAX_CH; unit++) {
821         if ((ch = ctlr->interrupt[unit].argument))
822             ctlr->interrupt[unit].function(ch);
823     }
824 }
825
826 int
827 ata_setup_interrupt(device_t dev, void *intr_func)
828 {
829     struct ata_pci_controller *ctlr = device_get_softc(dev);
830     int i, msi = 0;
831
832     if (!ctlr->legacy) {
833         if (resource_int_value(device_get_name(dev),
834                 device_get_unit(dev), "msi", &i) == 0 && i != 0)
835             msi = 1;
836         if (msi && pci_msi_count(dev) > 0 && pci_alloc_msi(dev, &msi) == 0) {
837             ctlr->r_irq_rid = 0x1;
838         } else {
839             msi = 0;
840             ctlr->r_irq_rid = ATA_IRQ_RID;
841         }
842         if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
843                 &ctlr->r_irq_rid, RF_SHAREABLE | RF_ACTIVE))) {
844             device_printf(dev, "unable to map interrupt\n");
845             if (msi)
846                     pci_release_msi(dev);
847             return ENXIO;
848         }
849         if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS, NULL,
850                             intr_func, ctlr, &ctlr->handle))) {
851             device_printf(dev, "unable to setup interrupt\n");
852             bus_release_resource(dev,
853                 SYS_RES_IRQ, ctlr->r_irq_rid, ctlr->r_irq);
854             if (msi)
855                     pci_release_msi(dev);
856             return ENXIO;
857         }
858     }
859     return 0;
860 }
861
862 void
863 ata_set_desc(device_t dev)
864 {
865     struct ata_pci_controller *ctlr = device_get_softc(dev);
866     char buffer[128];
867
868     sprintf(buffer, "%s %s %s controller",
869             ata_pcivendor2str(dev), ctlr->chip->text, 
870             ata_mode2str(ctlr->chip->max_dma));
871     device_set_desc_copy(dev, buffer);
872 }
873
874 const struct ata_chip_id *
875 ata_match_chip(device_t dev, const struct ata_chip_id *index)
876 {
877     uint32_t devid;
878     uint8_t revid;
879
880     devid = pci_get_devid(dev);
881     revid = pci_get_revid(dev);
882     while (index->chipid != 0) {
883         if (devid == index->chipid && revid >= index->chiprev)
884             return (index);
885         index++;
886     }
887     return (NULL);
888 }
889
890 const struct ata_chip_id *
891 ata_find_chip(device_t dev, const struct ata_chip_id *index, int slot)
892 {
893     const struct ata_chip_id *idx;
894     device_t *children;
895     int nchildren, i;
896     uint8_t s;
897
898     if (device_get_children(device_get_parent(dev), &children, &nchildren))
899         return (NULL);
900
901     for (i = 0; i < nchildren; i++) {
902         s = pci_get_slot(children[i]);
903         if ((slot >= 0 && s == slot) || (slot < 0 && s <= -slot)) {
904             idx = ata_match_chip(children[i], index);
905             if (idx != NULL) {
906                 free(children, M_TEMP);
907                 return (idx);
908             }
909         }
910     }
911     free(children, M_TEMP);
912     return (NULL);
913 }
914
915 const char *
916 ata_pcivendor2str(device_t dev)
917 {
918     switch (pci_get_vendor(dev)) {
919     case ATA_ACARD_ID:          return "Acard";
920     case ATA_ACER_LABS_ID:      return "AcerLabs";
921     case ATA_AMD_ID:            return "AMD";
922     case ATA_ADAPTEC_ID:        return "Adaptec";
923     case ATA_ATI_ID:            return "ATI";
924     case ATA_CYRIX_ID:          return "Cyrix";
925     case ATA_CYPRESS_ID:        return "Cypress";
926     case ATA_HIGHPOINT_ID:      return "HighPoint";
927     case ATA_INTEL_ID:          return "Intel";
928     case ATA_ITE_ID:            return "ITE";
929     case ATA_JMICRON_ID:        return "JMicron";
930     case ATA_MARVELL_ID:        return "Marvell";
931     case ATA_MARVELL2_ID:       return "Marvell";
932     case ATA_NATIONAL_ID:       return "National";
933     case ATA_NETCELL_ID:        return "Netcell";
934     case ATA_NVIDIA_ID:         return "nVidia";
935     case ATA_PROMISE_ID:        return "Promise";
936     case ATA_SERVERWORKS_ID:    return "ServerWorks";
937     case ATA_SILICON_IMAGE_ID:  return "SiI";
938     case ATA_SIS_ID:            return "SiS";
939     case ATA_VIA_ID:            return "VIA";
940     case ATA_CENATEK_ID:        return "Cenatek";
941     case ATA_MICRON_ID:         return "Micron";
942     default:                    return "Generic";
943     }
944 }
945
946 int
947 ata_mode2idx(int mode)
948 {
949     if ((mode & ATA_DMA_MASK) == ATA_UDMA0)
950         return (mode & ATA_MODE_MASK) + 8;
951     if ((mode & ATA_DMA_MASK) == ATA_WDMA0)
952         return (mode & ATA_MODE_MASK) + 5;
953     return (mode & ATA_MODE_MASK) - ATA_PIO0;
954 }