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