xref: /freebsd/sys/dev/ata/ata-pci.c (revision d2387d42b8da231a5b95cbc313825fb2aadf26f6)
1 /*-
2  * Copyright (c) 1998 - 2004 S�ren Schmidt <sos@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification, immediately at the beginning of the file.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
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 "opt_ata.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/bus.h>
38 #include <sys/malloc.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 #ifdef __alpha__
46 #include <machine/md_var.h>
47 #endif
48 #include <sys/rman.h>
49 #include <dev/pci/pcivar.h>
50 #include <dev/pci/pcireg.h>
51 #include <dev/ata/ata-all.h>
52 #include <dev/ata/ata-pci.h>
53 
54 /* local vars */
55 static MALLOC_DEFINE(M_ATAPCI, "ATA PCI", "ATA driver PCI");
56 
57 /* misc defines */
58 #define IOMASK			0xfffffffc
59 
60 /* prototypes */
61 static int ata_pci_allocate(device_t, struct ata_channel *);
62 static void ata_pci_dmainit(struct ata_channel *);
63 static void ata_pci_locknoop(struct ata_channel *, int);
64 
65 static int
66 ata_pci_probe(device_t dev)
67 {
68     if (pci_get_class(dev) != PCIC_STORAGE)
69 	return ENXIO;
70 
71     switch (pci_get_vendor(dev)) {
72     case ATA_ACARD_ID:
73  	if (!ata_acard_ident(dev))
74  	    return 0;
75  	break;
76     case ATA_ACER_LABS_ID:
77  	if (!ata_ali_ident(dev))
78  	    return 0;
79  	break;
80     case ATA_AMD_ID:
81  	if (!ata_amd_ident(dev))
82  	    return 0;
83  	break;
84     case ATA_CYRIX_ID:
85  	if (!ata_cyrix_ident(dev))
86  	    return 0;
87  	break;
88     case ATA_CYPRESS_ID:
89  	if (!ata_cypress_ident(dev))
90  	    return 0;
91  	break;
92     case ATA_HIGHPOINT_ID:
93  	if (!ata_highpoint_ident(dev))
94  	    return 0;
95  	break;
96     case ATA_INTEL_ID:
97  	if (!ata_intel_ident(dev))
98  	    return 0;
99  	break;
100     case ATA_NATIONAL_ID:
101         if (!ata_national_ident(dev))
102 	    return 0;
103 	break;
104     case ATA_NVIDIA_ID:
105  	if (!ata_nvidia_ident(dev))
106  	    return 0;
107  	break;
108     case ATA_PROMISE_ID:
109  	if (!ata_promise_ident(dev))
110  	    return 0;
111  	break;
112     case ATA_SERVERWORKS_ID:
113  	if (!ata_serverworks_ident(dev))
114  	    return 0;
115  	break;
116     case ATA_SILICON_IMAGE_ID:
117  	if (!ata_sii_ident(dev))
118  	    return 0;
119  	break;
120     case ATA_SIS_ID:
121  	if (!ata_sis_ident(dev))
122  	    return 0;
123  	break;
124     case ATA_VIA_ID:
125  	if (!ata_via_ident(dev))
126  	    return 0;
127  	break;
128     case 0x16ca:
129 	if (pci_get_devid(dev) == 0x000116ca) {
130 	    ata_generic_ident(dev);
131 	    device_set_desc(dev, "Cenatek Rocket Drive controller");
132 	    return 0;
133 	}
134 	break;
135     case 0x1042:
136 	if (pci_get_devid(dev)==0x10001042 || pci_get_devid(dev)==0x10011042) {
137 	    ata_generic_ident(dev);
138 	    device_set_desc(dev,
139 		"RZ 100? ATA controller !WARNING! buggy HW data loss possible");
140 	    return 0;
141 	}
142 	break;
143     }
144 
145     /* unknown chipset, try generic DMA if it seems possible */
146     if ((pci_get_class(dev) == PCIC_STORAGE) &&
147 	(pci_get_subclass(dev) == PCIS_STORAGE_IDE))
148 	return ata_generic_ident(dev);
149 
150     return ENXIO;
151 }
152 
153 static int
154 ata_pci_attach(device_t dev)
155 {
156     struct ata_pci_controller *ctlr = device_get_softc(dev);
157     u_int32_t cmd;
158     int unit;
159 
160     /* do chipset specific setups only needed once */
161     if (ATA_MASTERDEV(dev) || pci_read_config(dev, 0x18, 4) & IOMASK)
162 	ctlr->channels = 2;
163     else
164 	ctlr->channels = 1;
165     ctlr->allocate = ata_pci_allocate;
166     ctlr->dmainit = ata_pci_dmainit;
167     ctlr->locking = ata_pci_locknoop;
168 
169     /* if needed try to enable busmastering */
170     cmd = pci_read_config(dev, PCIR_COMMAND, 2);
171     if (!(cmd & PCIM_CMD_BUSMASTEREN)) {
172 	pci_write_config(dev, PCIR_COMMAND, cmd | PCIM_CMD_BUSMASTEREN, 2);
173 	cmd = pci_read_config(dev, PCIR_COMMAND, 2);
174     }
175 
176     /* if busmastering mode "stuck" use it */
177     if ((cmd & PCIM_CMD_BUSMASTEREN) == PCIM_CMD_BUSMASTEREN) {
178 	ctlr->r_type1 = SYS_RES_IOPORT;
179 	ctlr->r_rid1 = ATA_BMADDR_RID;
180 	ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1, &ctlr->r_rid1,
181 					      RF_ACTIVE);
182     }
183 
184     ctlr->chipinit(dev);
185 
186     /* attach all channels on this controller */
187     for (unit = 0; unit < ctlr->channels; unit++)
188 	device_add_child(dev, "ata", ATA_MASTERDEV(dev) ?
189 			 unit : devclass_find_free_unit(ata_devclass, 2));
190 
191     return bus_generic_attach(dev); }
192 
193 static int
194 ata_pci_detach(device_t dev)
195 {
196     struct ata_pci_controller *ctlr = device_get_softc(dev);
197     struct ata_channel *ch;
198     int unit;
199 
200     /* mark HW as gone, we dont want to issue commands to HW no longer there */
201     for (unit = 0; unit < ctlr->channels; unit++) {
202 	if ((ch = ctlr->interrupt[unit].argument))
203 	    ch->flags |= ATA_HWGONE;
204     }
205 
206     bus_generic_detach(dev);
207 
208     if (ctlr->r_irq) {
209 	bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle);
210 	bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ctlr->r_irq);
211     }
212     if (ctlr->r_res2)
213 	bus_release_resource(dev, ctlr->r_type2, ctlr->r_rid2, ctlr->r_res2);
214     if (ctlr->r_res1)
215 	bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1, ctlr->r_res1);
216 
217     return 0;
218 }
219 
220 static int
221 ata_pci_print_child(device_t dev, device_t child)
222 {
223     struct ata_channel *ch = device_get_softc(child);
224     int retval = 0;
225 
226     retval += bus_print_child_header(dev, child);
227     retval += printf(": at 0x%lx", rman_get_start(ch->r_io[ATA_IDX_ADDR].res));
228 
229     if (ATA_MASTERDEV(dev))
230 	retval += printf(" irq %d", 14 + ch->unit);
231 
232     retval += bus_print_child_footer(dev, child);
233 
234     return retval;
235 }
236 
237 static struct resource *
238 ata_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
239 		       u_long start, u_long end, u_long count, u_int flags)
240 {
241     struct ata_pci_controller *controller = device_get_softc(dev);
242     int unit = ((struct ata_channel *)device_get_softc(child))->unit;
243     struct resource *res = NULL;
244     int myrid;
245 
246     if (type == SYS_RES_IOPORT) {
247 	switch (*rid) {
248 	case ATA_IOADDR_RID:
249 	    if (ATA_MASTERDEV(dev)) {
250 		myrid = 0;
251 		start = (unit ? ATA_SECONDARY : ATA_PRIMARY);
252 		end = start + ATA_IOSIZE - 1;
253 		count = ATA_IOSIZE;
254 		res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
255 					 SYS_RES_IOPORT, &myrid,
256 					 start, end, count, flags);
257 	    }
258 	    else {
259 		myrid = 0x10 + 8 * unit;
260 		res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
261 					 SYS_RES_IOPORT, &myrid,
262 					 start, end, count, flags);
263 	    }
264 	    break;
265 
266 	case ATA_ALTADDR_RID:
267 	    if (ATA_MASTERDEV(dev)) {
268 		myrid = 0;
269 		start = (unit ? ATA_SECONDARY : ATA_PRIMARY) + ATA_ALTOFFSET;
270 		end = start + ATA_ALTIOSIZE - 1;
271 		count = ATA_ALTIOSIZE;
272 		res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
273 					 SYS_RES_IOPORT, &myrid,
274 					 start, end, count, flags);
275 	    }
276 	    else {
277 		myrid = 0x14 + 8 * unit;
278 		res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
279 					 SYS_RES_IOPORT, &myrid,
280 					 start, end, count, flags);
281 		if (res) {
282 			start = rman_get_start(res) + 2;
283 			end = start + ATA_ALTIOSIZE - 1;
284 			count = ATA_ALTIOSIZE;
285 			BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
286 					     SYS_RES_IOPORT, myrid, res);
287 			res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
288 						 SYS_RES_IOPORT, &myrid,
289 						 start, end, count, flags);
290 		}
291 	    }
292 	    break;
293 	}
294 	return res;
295     }
296 
297     if (type == SYS_RES_IRQ && *rid == ATA_IRQ_RID) {
298 		if (ATA_MASTERDEV(dev)) {
299 #ifdef __alpha__
300 	    return alpha_platform_alloc_ide_intr(unit);
301 #else
302 	    int irq = (unit == 0 ? 14 : 15);
303 
304 	    return BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
305 				      SYS_RES_IRQ, rid, irq, irq, 1, flags);
306 #endif
307 	}
308 	else {
309 	    return controller->r_irq;
310 	}
311     }
312     return 0;
313 }
314 
315 static int
316 ata_pci_release_resource(device_t dev, device_t child, int type, int rid,
317 			 struct resource *r)
318 {
319     int unit = ((struct ata_channel *)device_get_softc(child))->unit;
320 
321     if (type == SYS_RES_IOPORT) {
322 	switch (rid) {
323 	case ATA_IOADDR_RID:
324 	    if (ATA_MASTERDEV(dev))
325 		return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
326 					    SYS_RES_IOPORT, 0x0, r);
327 	    else
328 		return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
329 					    SYS_RES_IOPORT, 0x10 + 8 * unit, r);
330 	    break;
331 
332 	case ATA_ALTADDR_RID:
333 	    if (ATA_MASTERDEV(dev))
334 		return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
335 					    SYS_RES_IOPORT, 0x0, r);
336 	    else
337 		return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
338 					    SYS_RES_IOPORT, 0x14 + 8 * unit, r);
339 	    break;
340 	default:
341 	    return ENOENT;
342 	}
343     }
344     if (type == SYS_RES_IRQ) {
345 	if (rid != ATA_IRQ_RID)
346 	    return ENOENT;
347 
348 	if (ATA_MASTERDEV(dev)) {
349 #ifdef __alpha__
350 	    return alpha_platform_release_ide_intr(unit, r);
351 #else
352 	    return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
353 					SYS_RES_IRQ, rid, r);
354 #endif
355 	}
356 	else
357 	    return 0;
358     }
359     return EINVAL;
360 }
361 
362 static int
363 ata_pci_setup_intr(device_t dev, device_t child, struct resource *irq,
364 		   int flags, driver_intr_t *function, void *argument,
365 		   void **cookiep)
366 {
367     if (ATA_MASTERDEV(dev)) {
368 #ifdef __alpha__
369 	return alpha_platform_setup_ide_intr(child, irq, function, argument,
370 					     cookiep);
371 #else
372 	return BUS_SETUP_INTR(device_get_parent(dev), child, irq,
373 			      flags, function, argument, cookiep);
374 #endif
375     }
376     else {
377 	struct ata_pci_controller *controller = device_get_softc(dev);
378 	int unit = ((struct ata_channel *)device_get_softc(child))->unit;
379 
380 	controller->interrupt[unit].function = function;
381 	controller->interrupt[unit].argument = argument;
382 	*cookiep = controller;
383 	return 0;
384     }
385 }
386 
387 static int
388 ata_pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
389 		      void *cookie)
390 {
391     if (ATA_MASTERDEV(dev)) {
392 #ifdef __alpha__
393 	return alpha_platform_teardown_ide_intr(child, irq, cookie);
394 #else
395 	return BUS_TEARDOWN_INTR(device_get_parent(dev), child, irq, cookie);
396 #endif
397     }
398     else {
399 	struct ata_pci_controller *controller = device_get_softc(dev);
400 	int unit = ((struct ata_channel *)device_get_softc(child))->unit;
401 
402 	controller->interrupt[unit].function = NULL;
403 	controller->interrupt[unit].argument = NULL;
404 	return 0;
405     }
406 }
407 
408 static int
409 ata_pci_allocate(device_t dev, struct ata_channel *ch)
410 {
411     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
412     struct resource *io = NULL, *altio = NULL;
413     int i, rid;
414 
415     rid = ATA_IOADDR_RID;
416     io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
417 			    0, ~0, ATA_IOSIZE, RF_ACTIVE);
418     if (!io)
419 	return ENXIO;
420 
421     rid = ATA_ALTADDR_RID;
422     altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
423 			       0, ~0, ATA_ALTIOSIZE, RF_ACTIVE);
424     if (!altio) {
425 	bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
426 	return ENXIO;
427     }
428 
429     for (i = ATA_DATA; i <= ATA_STATUS; i ++) {
430 	ch->r_io[i].res = io;
431 	ch->r_io[i].offset = i;
432     }
433     ch->r_io[ATA_ALTSTAT].res = altio;
434     ch->r_io[ATA_ALTSTAT].offset = 0;
435     ch->r_io[ATA_IDX_ADDR].res = io;
436 
437     if (ctlr->r_res1) {
438 	for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {
439 	    ch->r_io[i].res = ctlr->r_res1;
440 	    ch->r_io[i].offset = (i - ATA_BMCMD_PORT)+(ch->unit * ATA_BMIOSIZE);
441 	}
442 
443 	/* if simplex controller, only allow DMA on primary channel */
444 	ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_IDX_INB(ch, ATA_BMSTAT_PORT) &
445 		     (ATA_BMSTAT_DMA_MASTER | ATA_BMSTAT_DMA_SLAVE));
446 	if (ch->unit > 0 &&
447 	    (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_DMA_SIMPLEX))
448 	    device_printf(dev, "simplex device, DMA on primary only\n");
449 	else
450 	    ctlr->dmainit(ch);
451     }
452     return 0;
453 }
454 
455 static int
456 ata_pci_dmastart(struct ata_channel *ch)
457 {
458     ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) |
459 		 (ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR)));
460     ATA_IDX_OUTL(ch, ATA_BMDTP_PORT, ch->dma->mdmatab);
461     ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
462 		 ((ch->dma->flags & ATA_DMA_READ) ? ATA_BMCMD_WRITE_READ : 0) |
463 		 ATA_BMCMD_START_STOP);
464     return 0;
465 }
466 
467 static int
468 ata_pci_dmastop(struct ata_channel *ch)
469 {
470     int error;
471 
472     error = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
473     ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
474 		 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
475     ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
476     return error;
477 }
478 
479 static void
480 ata_pci_dmainit(struct ata_channel *ch)
481 {
482     ata_dmainit(ch);
483     if (ch->dma) {
484 	ch->dma->start = ata_pci_dmastart;
485 	ch->dma->stop = ata_pci_dmastop;
486     }
487 }
488 
489 static void
490 ata_pci_locknoop(struct ata_channel *ch, int flags)
491 {
492 }
493 
494 static device_method_t ata_pci_methods[] = {
495     /* device interface */
496     DEVMETHOD(device_probe,		ata_pci_probe),
497     DEVMETHOD(device_attach,		ata_pci_attach),
498     DEVMETHOD(device_detach,		ata_pci_detach),
499     DEVMETHOD(device_shutdown,		bus_generic_shutdown),
500     DEVMETHOD(device_suspend,		bus_generic_suspend),
501     DEVMETHOD(device_resume,		bus_generic_resume),
502 
503     /* bus methods */
504     DEVMETHOD(bus_print_child,		ata_pci_print_child),
505     DEVMETHOD(bus_alloc_resource,	ata_pci_alloc_resource),
506     DEVMETHOD(bus_release_resource,	ata_pci_release_resource),
507     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
508     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
509     DEVMETHOD(bus_setup_intr,		ata_pci_setup_intr),
510     DEVMETHOD(bus_teardown_intr,	ata_pci_teardown_intr),
511     { 0, 0 }
512 };
513 
514 static driver_t ata_pci_driver = {
515     "atapci",
516     ata_pci_methods,
517     sizeof(struct ata_pci_controller),
518 };
519 
520 static devclass_t ata_pci_devclass;
521 
522 DRIVER_MODULE(atapci, pci, ata_pci_driver, ata_pci_devclass, 0, 0);
523 
524 static int
525 ata_pcisub_probe(device_t dev)
526 {
527     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
528     struct ata_channel *ch = device_get_softc(dev);
529     device_t *children;
530     int count, error, i;
531 
532     /* take care of green memory */
533     bzero(ch, sizeof(struct ata_channel));
534 
535     /* find channel number on this controller */
536     device_get_children(device_get_parent(dev), &children, &count);
537     for (i = 0; i < count; i++) {
538 	if (children[i] == dev)
539 	    ch->unit = i;
540     }
541     free(children, M_TEMP);
542 
543     if ((error = ctlr->allocate(dev, ch)))
544 	return error;
545 
546     ch->device[MASTER].setmode = ctlr->setmode;
547     ch->device[SLAVE].setmode = ctlr->setmode;
548     ch->locking = ctlr->locking;
549     if (ch->reset)
550 	ch->reset(ch);
551     return ata_probe(dev);
552 }
553 
554 static device_method_t ata_pcisub_methods[] = {
555     /* device interface */
556     DEVMETHOD(device_probe,	ata_pcisub_probe),
557     DEVMETHOD(device_attach,	ata_attach),
558     DEVMETHOD(device_detach,	ata_detach),
559     DEVMETHOD(device_suspend,	ata_suspend),
560     DEVMETHOD(device_resume,	ata_resume),
561     { 0, 0 }
562 };
563 
564 static driver_t ata_pcisub_driver = {
565     "ata",
566     ata_pcisub_methods,
567     sizeof(struct ata_channel),
568 };
569 
570 DRIVER_MODULE(ata, atapci, ata_pcisub_driver, ata_devclass, 0, 0);
571