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