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