xref: /freebsd/sys/dev/ata/ata-pci.c (revision e63d20b70ee1dbee9b075f29de6f30cdcfe1abe1)
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/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/module.h>
33 #include <sys/ata.h>
34 #include <sys/bus.h>
35 #include <sys/conf.h>
36 #include <sys/malloc.h>
37 #include <sys/sbuf.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
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
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
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
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
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
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
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
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
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 *
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
276 ata_pci_release_resource(device_t dev, device_t child, struct resource *r)
277 {
278 	int rid = rman_get_rid(r);
279 	int type = rman_get_type(r);
280 
281 	if (device_get_devclass(child) == ata_devclass) {
282 		struct ata_pci_controller *controller = device_get_softc(dev);
283 
284 	        if (type == SYS_RES_IOPORT) {
285 	    		switch (rid) {
286 			case ATA_IOADDR_RID:
287 			case ATA_CTLADDR_RID:
288 			    return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
289 				r);
290 			default:
291 			    return ENOENT;
292 			}
293 		}
294 		if (type == SYS_RES_IRQ) {
295 			if (rid != ATA_IRQ_RID)
296 				return ENOENT;
297 			if (controller->legacy) {
298 				return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
299 				    r);
300 			} else
301 				return 0;
302 		}
303 	} else {
304 		if (type == SYS_RES_IRQ) {
305 			if (rid != ATA_IRQ_RID)
306 				return (ENOENT);
307 			return (0);
308 		} else {
309 			return (BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
310 			    r));
311 		}
312 	}
313 	return (EINVAL);
314 }
315 
316 int
317 ata_pci_setup_intr(device_t dev, device_t child, struct resource *irq,
318 		   int flags, driver_filter_t *filter, driver_intr_t *function,
319 		   void *argument, void **cookiep)
320 {
321 	struct ata_pci_controller *controller = device_get_softc(dev);
322 
323 	if (controller->legacy) {
324 		return BUS_SETUP_INTR(device_get_parent(dev), child, irq,
325 			      flags, filter, function, argument, cookiep);
326 	} else {
327 		struct ata_pci_controller *controller = device_get_softc(dev);
328 		int unit;
329 
330 	    	if (filter != NULL) {
331 			printf("ata-pci.c: we cannot use a filter here\n");
332 			return (EINVAL);
333 		}
334 		if (device_get_devclass(child) == ata_devclass)
335 			unit = ((struct ata_channel *)device_get_softc(child))->unit;
336 		else
337 			unit = ATA_PCI_MAX_CH - 1;
338 		controller->interrupt[unit].function = function;
339 		controller->interrupt[unit].argument = argument;
340 		*cookiep = controller;
341 		return 0;
342 	}
343 }
344 
345 int
346 ata_pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
347 		      void *cookie)
348 {
349 	struct ata_pci_controller *controller = device_get_softc(dev);
350 
351         if (controller->legacy) {
352 		return BUS_TEARDOWN_INTR(device_get_parent(dev), child, irq, cookie);
353 	} else {
354 		struct ata_pci_controller *controller = device_get_softc(dev);
355 		int unit;
356 
357 		if (device_get_devclass(child) == ata_devclass)
358 			unit = ((struct ata_channel *)device_get_softc(child))->unit;
359 		else
360 			unit = ATA_PCI_MAX_CH - 1;
361 		controller->interrupt[unit].function = NULL;
362 		controller->interrupt[unit].argument = NULL;
363 		return 0;
364 	}
365 }
366 
367 int
368 ata_generic_setmode(device_t dev, int target, int mode)
369 {
370 
371 	return (min(mode, ATA_UDMA2));
372 }
373 
374 int
375 ata_generic_chipinit(device_t dev)
376 {
377     struct ata_pci_controller *ctlr = device_get_softc(dev);
378 
379     if (ata_setup_interrupt(dev, ata_generic_intr))
380 	return ENXIO;
381     ctlr->setmode = ata_generic_setmode;
382     return 0;
383 }
384 
385 int
386 ata_pci_ch_attach(device_t dev)
387 {
388     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
389     struct ata_channel *ch = device_get_softc(dev);
390     struct resource *io = NULL, *ctlio = NULL;
391     int i, rid;
392 
393     rid = ATA_IOADDR_RID;
394     if (!(io = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE)))
395 	return ENXIO;
396 
397     rid = ATA_CTLADDR_RID;
398     if (!(ctlio = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,RF_ACTIVE))){
399 	bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
400 	return ENXIO;
401     }
402 
403     ata_pci_dmainit(dev);
404 
405     for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
406 	ch->r_io[i].res = io;
407 	ch->r_io[i].offset = i;
408     }
409     ch->r_io[ATA_CONTROL].res = ctlio;
410     ch->r_io[ATA_CONTROL].offset = ctlr->legacy ? 0 : 2;
411     ch->r_io[ATA_IDX_ADDR].res = io;
412     ata_default_registers(dev);
413     if (ctlr->r_res1) {
414 	for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {
415 	    ch->r_io[i].res = ctlr->r_res1;
416 	    ch->r_io[i].offset = (i - ATA_BMCMD_PORT) + (ch->unit*ATA_BMIOSIZE);
417 	}
418     }
419 
420     ata_pci_hw(dev);
421     return 0;
422 }
423 
424 int
425 ata_pci_ch_detach(device_t dev)
426 {
427     struct ata_channel *ch = device_get_softc(dev);
428 
429     ata_pci_dmafini(dev);
430 
431     bus_release_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID,
432 	ch->r_io[ATA_CONTROL].res);
433     bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID,
434 	ch->r_io[ATA_IDX_ADDR].res);
435 
436     return (0);
437 }
438 
439 int
440 ata_pci_status(device_t dev)
441 {
442     struct ata_pci_controller *controller =
443 	device_get_softc(device_get_parent(dev));
444     struct ata_channel *ch = device_get_softc(dev);
445 
446     if ((dumping || !controller->legacy) &&
447 	((ch->flags & ATA_ALWAYS_DMASTAT) ||
448 	 (ch->dma.flags & ATA_DMA_ACTIVE))) {
449 	int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
450 
451 	if ((bmstat & ATA_BMSTAT_INTERRUPT) == 0)
452 	    return 0;
453 	ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
454 	DELAY(1);
455     }
456     if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) {
457 	DELAY(100);
458 	if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY)
459 	    return 0;
460     }
461     return 1;
462 }
463 
464 void
465 ata_pci_hw(device_t dev)
466 {
467     struct ata_channel *ch = device_get_softc(dev);
468 
469     ata_generic_hw(dev);
470     ch->hw.status = ata_pci_status;
471 }
472 
473 static int
474 ata_pci_dmastart(struct ata_request *request)
475 {
476     struct ata_channel *ch = device_get_softc(request->parent);
477 
478     ATA_DEBUG_RQ(request, "dmastart");
479 
480     ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) |
481 		 (ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR)));
482     ATA_IDX_OUTL(ch, ATA_BMDTP_PORT, request->dma->sg_bus);
483     ch->dma.flags |= ATA_DMA_ACTIVE;
484     ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
485 		 (ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_WRITE_READ) |
486 		 ((request->flags & ATA_R_READ) ? ATA_BMCMD_WRITE_READ : 0)|
487 		 ATA_BMCMD_START_STOP);
488     return 0;
489 }
490 
491 static int
492 ata_pci_dmastop(struct ata_request *request)
493 {
494     struct ata_channel *ch = device_get_softc(request->parent);
495     int error;
496 
497     ATA_DEBUG_RQ(request, "dmastop");
498 
499     ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
500 		 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
501     ch->dma.flags &= ~ATA_DMA_ACTIVE;
502     error = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
503     ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
504     return error;
505 }
506 
507 static void
508 ata_pci_dmareset(device_t dev)
509 {
510     struct ata_channel *ch = device_get_softc(dev);
511     struct ata_request *request;
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     ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
517     if ((request = ch->running)) {
518 	device_printf(dev, "DMA reset calling unload\n");
519 	ch->dma.unload(request);
520     }
521 }
522 
523 void
524 ata_pci_dmainit(device_t dev)
525 {
526     struct ata_channel *ch = device_get_softc(dev);
527 
528     ata_dmainit(dev);
529     ch->dma.start = ata_pci_dmastart;
530     ch->dma.stop = ata_pci_dmastop;
531     ch->dma.reset = ata_pci_dmareset;
532 }
533 
534 void
535 ata_pci_dmafini(device_t dev)
536 {
537 
538     ata_dmafini(dev);
539 }
540 
541 int
542 ata_pci_print_child(device_t dev, device_t child)
543 {
544 	int retval;
545 
546 	retval = bus_print_child_header(dev, child);
547 	retval += printf(" at channel %d",
548 	    (int)(intptr_t)device_get_ivars(child));
549 	retval += bus_print_child_footer(dev, child);
550 
551 	return (retval);
552 }
553 
554 int
555 ata_pci_child_location(device_t dev, device_t child, struct sbuf *sb)
556 {
557 
558 	sbuf_printf(sb, "channel=%d",
559 	    (int)(intptr_t)device_get_ivars(child));
560 	return (0);
561 }
562 
563 static bus_dma_tag_t
564 ata_pci_get_dma_tag(device_t bus, device_t child)
565 {
566 
567 	return (bus_get_dma_tag(bus));
568 }
569 
570 static device_method_t ata_pci_methods[] = {
571     /* device interface */
572     DEVMETHOD(device_probe,             ata_pci_probe),
573     DEVMETHOD(device_attach,            ata_pci_attach),
574     DEVMETHOD(device_detach,            ata_pci_detach),
575     DEVMETHOD(device_suspend,           ata_pci_suspend),
576     DEVMETHOD(device_resume,            ata_pci_resume),
577     DEVMETHOD(device_shutdown,          bus_generic_shutdown),
578 
579     /* bus methods */
580     DEVMETHOD(bus_read_ivar,		ata_pci_read_ivar),
581     DEVMETHOD(bus_write_ivar,		ata_pci_write_ivar),
582     DEVMETHOD(bus_alloc_resource,       ata_pci_alloc_resource),
583     DEVMETHOD(bus_release_resource,     ata_pci_release_resource),
584     DEVMETHOD(bus_activate_resource,    bus_generic_activate_resource),
585     DEVMETHOD(bus_deactivate_resource,  bus_generic_deactivate_resource),
586     DEVMETHOD(bus_setup_intr,           ata_pci_setup_intr),
587     DEVMETHOD(bus_teardown_intr,        ata_pci_teardown_intr),
588     DEVMETHOD(pci_read_config,		ata_pci_read_config),
589     DEVMETHOD(pci_write_config,		ata_pci_write_config),
590     DEVMETHOD(bus_print_child,		ata_pci_print_child),
591     DEVMETHOD(bus_child_location,	ata_pci_child_location),
592     DEVMETHOD(bus_get_dma_tag,		ata_pci_get_dma_tag),
593 
594     DEVMETHOD_END
595 };
596 
597 static driver_t ata_pci_driver = {
598     "atapci",
599     ata_pci_methods,
600     sizeof(struct ata_pci_controller),
601 };
602 
603 DRIVER_MODULE(atapci, pci, ata_pci_driver, NULL, NULL);
604 MODULE_VERSION(atapci, 1);
605 MODULE_DEPEND(atapci, ata, 1, 1, 1);
606 
607 static int
608 ata_pcichannel_probe(device_t dev)
609 {
610 
611     if ((intptr_t)device_get_ivars(dev) < 0)
612 	    return (ENXIO);
613     device_set_desc(dev, "ATA channel");
614 
615     return ata_probe(dev);
616 }
617 
618 static int
619 ata_pcichannel_attach(device_t dev)
620 {
621     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
622     struct ata_channel *ch = device_get_softc(dev);
623     int error;
624 
625     if (ch->attached)
626 	return (0);
627     ch->attached = 1;
628 
629     ch->dev = dev;
630     ch->unit = (intptr_t)device_get_ivars(dev);
631 
632     resource_int_value(device_get_name(dev),
633 	device_get_unit(dev), "pm_level", &ch->pm_level);
634 
635     if ((error = ctlr->ch_attach(dev)))
636 	return error;
637 
638     return ata_attach(dev);
639 }
640 
641 static int
642 ata_pcichannel_detach(device_t dev)
643 {
644     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
645     struct ata_channel *ch = device_get_softc(dev);
646     int error;
647 
648     if (!ch->attached)
649 	return (0);
650     ch->attached = 0;
651 
652     if ((error = ata_detach(dev)))
653 	return error;
654 
655     if (ctlr->ch_detach)
656 	return (ctlr->ch_detach(dev));
657 
658     return (0);
659 }
660 static int
661 ata_pcichannel_suspend(device_t dev)
662 {
663     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
664     struct ata_channel *ch = device_get_softc(dev);
665     int error;
666 
667     if (!ch->attached)
668 	return (0);
669 
670     if ((error = ata_suspend(dev)))
671 	return (error);
672 
673     if (ctlr->ch_suspend != NULL && (error = ctlr->ch_suspend(dev)))
674 	return (error);
675 
676     return (0);
677 }
678 
679 static int
680 ata_pcichannel_resume(device_t dev)
681 {
682     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
683     struct ata_channel *ch = device_get_softc(dev);
684     int error;
685 
686     if (!ch->attached)
687 	return (0);
688 
689     if (ctlr->ch_resume != NULL && (error = ctlr->ch_resume(dev)))
690 	return (error);
691 
692     return ata_resume(dev);
693 }
694 
695 static void
696 ata_pcichannel_reset(device_t dev)
697 {
698     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
699     struct ata_channel *ch = device_get_softc(dev);
700 
701     /* if DMA engine present reset it  */
702     if (ch->dma.reset)
703 	ch->dma.reset(dev);
704 
705     /* reset the controller HW */
706     if (ctlr->reset)
707 	ctlr->reset(dev);
708     else
709 	ata_generic_reset(dev);
710 }
711 
712 static int
713 ata_pcichannel_setmode(device_t dev, int target, int mode)
714 {
715 	struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
716 
717 	if (ctlr->setmode)
718 		return (ctlr->setmode(dev, target, mode));
719 	else
720 		return (ata_generic_setmode(dev, target, mode));
721 }
722 
723 static int
724 ata_pcichannel_getrev(device_t dev, int target)
725 {
726 	struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
727 	struct ata_channel *ch = device_get_softc(dev);
728 
729 	if (ch->flags & ATA_SATA) {
730 		if (ctlr->getrev)
731 			return (ctlr->getrev(dev, target));
732 		else
733 			return (0xff);
734 	} else
735 		return (0);
736 }
737 
738 static device_method_t ata_pcichannel_methods[] = {
739     /* device interface */
740     DEVMETHOD(device_probe,     ata_pcichannel_probe),
741     DEVMETHOD(device_attach,    ata_pcichannel_attach),
742     DEVMETHOD(device_detach,    ata_pcichannel_detach),
743     DEVMETHOD(device_shutdown,  bus_generic_shutdown),
744     DEVMETHOD(device_suspend,   ata_pcichannel_suspend),
745     DEVMETHOD(device_resume,    ata_pcichannel_resume),
746 
747     /* ATA methods */
748     DEVMETHOD(ata_setmode,      ata_pcichannel_setmode),
749     DEVMETHOD(ata_getrev,       ata_pcichannel_getrev),
750     DEVMETHOD(ata_reset,        ata_pcichannel_reset),
751 
752     DEVMETHOD_END
753 };
754 
755 driver_t ata_pcichannel_driver = {
756     "ata",
757     ata_pcichannel_methods,
758     sizeof(struct ata_channel),
759 };
760 
761 DRIVER_MODULE(ata, atapci, ata_pcichannel_driver, NULL, NULL);
762 
763 /*
764  * misc support functions
765  */
766 int
767 ata_legacy(device_t dev)
768 {
769     return (((pci_read_config(dev, PCIR_SUBCLASS, 1) == PCIS_STORAGE_IDE) &&
770 	     (pci_read_config(dev, PCIR_PROGIF, 1)&PCIP_STORAGE_IDE_MASTERDEV)&&
771 	     ((pci_read_config(dev, PCIR_PROGIF, 1) &
772 	       (PCIP_STORAGE_IDE_MODEPRIM | PCIP_STORAGE_IDE_MODESEC)) !=
773 	      (PCIP_STORAGE_IDE_MODEPRIM | PCIP_STORAGE_IDE_MODESEC))) ||
774 	    (!pci_read_config(dev, PCIR_BAR(0), 4) &&
775 	     !pci_read_config(dev, PCIR_BAR(1), 4) &&
776 	     !pci_read_config(dev, PCIR_BAR(2), 4) &&
777 	     !pci_read_config(dev, PCIR_BAR(3), 4) &&
778 	     !pci_read_config(dev, PCIR_BAR(5), 4)));
779 }
780 
781 void
782 ata_generic_intr(void *data)
783 {
784     struct ata_pci_controller *ctlr = data;
785     struct ata_channel *ch;
786     int unit;
787 
788     for (unit = 0; unit < ATA_PCI_MAX_CH; unit++) {
789 	if ((ch = ctlr->interrupt[unit].argument))
790 	    ctlr->interrupt[unit].function(ch);
791     }
792 }
793 
794 int
795 ata_setup_interrupt(device_t dev, void *intr_func)
796 {
797     struct ata_pci_controller *ctlr = device_get_softc(dev);
798     int i, msi = 0;
799 
800     if (!ctlr->legacy) {
801 	if (resource_int_value(device_get_name(dev),
802 		device_get_unit(dev), "msi", &i) == 0 && i != 0)
803 	    msi = 1;
804 	if (msi && pci_msi_count(dev) > 0 && pci_alloc_msi(dev, &msi) == 0) {
805 	    ctlr->r_irq_rid = 0x1;
806 	} else {
807 	    msi = 0;
808 	    ctlr->r_irq_rid = ATA_IRQ_RID;
809 	}
810 	if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
811 		&ctlr->r_irq_rid, RF_SHAREABLE | RF_ACTIVE))) {
812 	    device_printf(dev, "unable to map interrupt\n");
813 	    if (msi)
814 		    pci_release_msi(dev);
815 	    return ENXIO;
816 	}
817 	if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS, NULL,
818 			    intr_func, ctlr, &ctlr->handle))) {
819 	    device_printf(dev, "unable to setup interrupt\n");
820 	    bus_release_resource(dev,
821 		SYS_RES_IRQ, ctlr->r_irq_rid, ctlr->r_irq);
822 	    if (msi)
823 		    pci_release_msi(dev);
824 	    return ENXIO;
825 	}
826     }
827     return 0;
828 }
829 
830 void
831 ata_set_desc(device_t dev)
832 {
833     struct ata_pci_controller *ctlr = device_get_softc(dev);
834     char buffer[128];
835 
836     sprintf(buffer, "%s %s %s controller",
837             ata_pcivendor2str(dev), ctlr->chip->text,
838             ata_mode2str(ctlr->chip->max_dma));
839     device_set_desc_copy(dev, buffer);
840 }
841 
842 const struct ata_chip_id *
843 ata_match_chip(device_t dev, const struct ata_chip_id *index)
844 {
845     uint32_t devid;
846     uint8_t revid;
847 
848     devid = pci_get_devid(dev);
849     revid = pci_get_revid(dev);
850     while (index->chipid != 0) {
851 	if (devid == index->chipid && revid >= index->chiprev)
852 	    return (index);
853 	index++;
854     }
855     return (NULL);
856 }
857 
858 const struct ata_chip_id *
859 ata_find_chip(device_t dev, const struct ata_chip_id *index, int slot)
860 {
861     const struct ata_chip_id *idx;
862     device_t *children;
863     int nchildren, i;
864     uint8_t s;
865 
866     if (device_get_children(device_get_parent(dev), &children, &nchildren))
867 	return (NULL);
868 
869     for (i = 0; i < nchildren; i++) {
870 	s = pci_get_slot(children[i]);
871 	if ((slot >= 0 && s == slot) || (slot < 0 && s <= -slot)) {
872 	    idx = ata_match_chip(children[i], index);
873 	    if (idx != NULL) {
874 		free(children, M_TEMP);
875 		return (idx);
876 	    }
877 	}
878     }
879     free(children, M_TEMP);
880     return (NULL);
881 }
882 
883 const char *
884 ata_pcivendor2str(device_t dev)
885 {
886     switch (pci_get_vendor(dev)) {
887     case ATA_ACARD_ID:          return "Acard";
888     case ATA_ACER_LABS_ID:      return "AcerLabs";
889     case ATA_AMD_ID:            return "AMD";
890     case ATA_ADAPTEC_ID:        return "Adaptec";
891     case ATA_ATI_ID:            return "ATI";
892     case ATA_CYRIX_ID:          return "Cyrix";
893     case ATA_CYPRESS_ID:        return "Cypress";
894     case ATA_HIGHPOINT_ID:      return "HighPoint";
895     case ATA_INTEL_ID:          return "Intel";
896     case ATA_ITE_ID:            return "ITE";
897     case ATA_JMICRON_ID:        return "JMicron";
898     case ATA_MARVELL_ID:        return "Marvell";
899     case ATA_MARVELL2_ID:       return "Marvell";
900     case ATA_NATIONAL_ID:       return "National";
901     case ATA_NETCELL_ID:        return "Netcell";
902     case ATA_NVIDIA_ID:         return "nVidia";
903     case ATA_PROMISE_ID:        return "Promise";
904     case ATA_SERVERWORKS_ID:    return "ServerWorks";
905     case ATA_SILICON_IMAGE_ID:  return "SiI";
906     case ATA_SIS_ID:            return "SiS";
907     case ATA_VIA_ID:            return "VIA";
908     case ATA_CENATEK_ID:        return "Cenatek";
909     case ATA_MICRON_ID:         return "Micron";
910     default:                    return "Generic";
911     }
912 }
913 
914 int
915 ata_mode2idx(int mode)
916 {
917     if ((mode & ATA_DMA_MASK) == ATA_UDMA0)
918 	return (mode & ATA_MODE_MASK) + 8;
919     if ((mode & ATA_DMA_MASK) == ATA_WDMA0)
920 	return (mode & ATA_MODE_MASK) + 5;
921     return (mode & ATA_MODE_MASK) - ATA_PIO0;
922 }
923