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