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