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