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