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