Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
204f47c5 BZ |
2 | * IDE DMA support (including IDE PCI BM-DMA). |
3 | * | |
59bca8cc BZ |
4 | * Copyright (C) 1995-1998 Mark Lord |
5 | * Copyright (C) 1999-2000 Andre Hedrick <andre@linux-ide.org> | |
6 | * Copyright (C) 2004, 2007 Bartlomiej Zolnierkiewicz | |
58f189fc | 7 | * |
1da177e4 | 8 | * May be copied or modified under the terms of the GNU General Public License |
204f47c5 BZ |
9 | * |
10 | * DMA is supported for all IDE devices (disk drives, cdroms, tapes, floppies). | |
1da177e4 LT |
11 | */ |
12 | ||
13 | /* | |
14 | * Special Thanks to Mark for his Six years of work. | |
1da177e4 LT |
15 | */ |
16 | ||
17 | /* | |
1da177e4 LT |
18 | * Thanks to "Christopher J. Reimer" <reimer@doe.carleton.ca> for |
19 | * fixing the problem with the BIOS on some Acer motherboards. | |
20 | * | |
21 | * Thanks to "Benoit Poulot-Cazajous" <poulot@chorus.fr> for testing | |
22 | * "TX" chipset compatibility and for providing patches for the "TX" chipset. | |
23 | * | |
24 | * Thanks to Christian Brunner <chb@muc.de> for taking a good first crack | |
25 | * at generic DMA -- his patches were referred to when preparing this code. | |
26 | * | |
27 | * Most importantly, thanks to Robert Bringman <rob@mars.trion.com> | |
28 | * for supplying a Promise UDMA board & WD UDMA drive for this work! | |
1da177e4 LT |
29 | */ |
30 | ||
1da177e4 LT |
31 | #include <linux/module.h> |
32 | #include <linux/types.h> | |
33 | #include <linux/kernel.h> | |
34 | #include <linux/timer.h> | |
35 | #include <linux/mm.h> | |
36 | #include <linux/interrupt.h> | |
37 | #include <linux/pci.h> | |
38 | #include <linux/init.h> | |
39 | #include <linux/ide.h> | |
40 | #include <linux/delay.h> | |
41 | #include <linux/scatterlist.h> | |
5c05ff68 | 42 | #include <linux/dma-mapping.h> |
1da177e4 LT |
43 | |
44 | #include <asm/io.h> | |
45 | #include <asm/irq.h> | |
46 | ||
1da177e4 LT |
47 | static const struct drive_list_entry drive_whitelist [] = { |
48 | ||
c2d3ce8c JH |
49 | { "Micropolis 2112A" , NULL }, |
50 | { "CONNER CTMA 4000" , NULL }, | |
51 | { "CONNER CTT8000-A" , NULL }, | |
52 | { "ST34342A" , NULL }, | |
1da177e4 LT |
53 | { NULL , NULL } |
54 | }; | |
55 | ||
56 | static const struct drive_list_entry drive_blacklist [] = { | |
57 | ||
c2d3ce8c JH |
58 | { "WDC AC11000H" , NULL }, |
59 | { "WDC AC22100H" , NULL }, | |
60 | { "WDC AC32500H" , NULL }, | |
61 | { "WDC AC33100H" , NULL }, | |
62 | { "WDC AC31600H" , NULL }, | |
1da177e4 LT |
63 | { "WDC AC32100H" , "24.09P07" }, |
64 | { "WDC AC23200L" , "21.10N21" }, | |
c2d3ce8c JH |
65 | { "Compaq CRD-8241B" , NULL }, |
66 | { "CRD-8400B" , NULL }, | |
67 | { "CRD-8480B", NULL }, | |
68 | { "CRD-8482B", NULL }, | |
69 | { "CRD-84" , NULL }, | |
70 | { "SanDisk SDP3B" , NULL }, | |
71 | { "SanDisk SDP3B-64" , NULL }, | |
72 | { "SANYO CD-ROM CRD" , NULL }, | |
73 | { "HITACHI CDR-8" , NULL }, | |
74 | { "HITACHI CDR-8335" , NULL }, | |
75 | { "HITACHI CDR-8435" , NULL }, | |
76 | { "Toshiba CD-ROM XM-6202B" , NULL }, | |
77 | { "TOSHIBA CD-ROM XM-1702BC", NULL }, | |
78 | { "CD-532E-A" , NULL }, | |
79 | { "E-IDE CD-ROM CR-840", NULL }, | |
80 | { "CD-ROM Drive/F5A", NULL }, | |
81 | { "WPI CDD-820", NULL }, | |
82 | { "SAMSUNG CD-ROM SC-148C", NULL }, | |
83 | { "SAMSUNG CD-ROM SC", NULL }, | |
84 | { "ATAPI CD-ROM DRIVE 40X MAXIMUM", NULL }, | |
85 | { "_NEC DV5800A", NULL }, | |
5a6248ca | 86 | { "SAMSUNG CD-ROM SN-124", "N001" }, |
c2d3ce8c | 87 | { "Seagate STT20000A", NULL }, |
b0bc65b9 | 88 | { "CD-ROM CDR_U200", "1.09" }, |
1da177e4 LT |
89 | { NULL , NULL } |
90 | ||
91 | }; | |
92 | ||
1da177e4 LT |
93 | /** |
94 | * ide_dma_intr - IDE DMA interrupt handler | |
95 | * @drive: the drive the interrupt is for | |
96 | * | |
97 | * Handle an interrupt completing a read/write DMA transfer on an | |
98 | * IDE device | |
99 | */ | |
100 | ||
101 | ide_startstop_t ide_dma_intr (ide_drive_t *drive) | |
102 | { | |
103 | u8 stat = 0, dma_stat = 0; | |
104 | ||
5e37bdc0 | 105 | dma_stat = drive->hwif->dma_ops->dma_end(drive); |
c47137a9 BZ |
106 | stat = ide_read_status(drive); |
107 | ||
1da177e4 LT |
108 | if (OK_STAT(stat,DRIVE_READY,drive->bad_wstat|DRQ_STAT)) { |
109 | if (!dma_stat) { | |
110 | struct request *rq = HWGROUP(drive)->rq; | |
111 | ||
4d7a984b | 112 | task_end_request(drive, rq, stat); |
1da177e4 LT |
113 | return ide_stopped; |
114 | } | |
115 | printk(KERN_ERR "%s: dma_intr: bad DMA status (dma_stat=%x)\n", | |
116 | drive->name, dma_stat); | |
117 | } | |
118 | return ide_error(drive, "dma_intr", stat); | |
119 | } | |
120 | ||
121 | EXPORT_SYMBOL_GPL(ide_dma_intr); | |
122 | ||
75d7d963 BZ |
123 | static int ide_dma_good_drive(ide_drive_t *drive) |
124 | { | |
125 | return ide_in_drive_list(drive->id, drive_whitelist); | |
126 | } | |
127 | ||
1da177e4 LT |
128 | /** |
129 | * ide_build_sglist - map IDE scatter gather for DMA I/O | |
130 | * @drive: the drive to build the DMA table for | |
131 | * @rq: the request holding the sg list | |
132 | * | |
5c05ff68 BZ |
133 | * Perform the DMA mapping magic necessary to access the source or |
134 | * target buffers of a request via DMA. The lower layers of the | |
1da177e4 | 135 | * kernel provide the necessary cache management so that we can |
5c05ff68 | 136 | * operate in a portable fashion. |
1da177e4 LT |
137 | */ |
138 | ||
139 | int ide_build_sglist(ide_drive_t *drive, struct request *rq) | |
140 | { | |
141 | ide_hwif_t *hwif = HWIF(drive); | |
142 | struct scatterlist *sg = hwif->sg_table; | |
143 | ||
1da177e4 LT |
144 | ide_map_sg(drive, rq); |
145 | ||
146 | if (rq_data_dir(rq) == READ) | |
5c05ff68 | 147 | hwif->sg_dma_direction = DMA_FROM_DEVICE; |
1da177e4 | 148 | else |
5c05ff68 | 149 | hwif->sg_dma_direction = DMA_TO_DEVICE; |
1da177e4 | 150 | |
5c05ff68 BZ |
151 | return dma_map_sg(hwif->dev, sg, hwif->sg_nents, |
152 | hwif->sg_dma_direction); | |
1da177e4 LT |
153 | } |
154 | ||
155 | EXPORT_SYMBOL_GPL(ide_build_sglist); | |
156 | ||
8e882ba1 | 157 | #ifdef CONFIG_BLK_DEV_IDEDMA_SFF |
1da177e4 LT |
158 | /** |
159 | * ide_build_dmatable - build IDE DMA table | |
160 | * | |
161 | * ide_build_dmatable() prepares a dma request. We map the command | |
162 | * to get the pci bus addresses of the buffers and then build up | |
163 | * the PRD table that the IDE layer wants to be fed. The code | |
164 | * knows about the 64K wrap bug in the CS5530. | |
165 | * | |
166 | * Returns the number of built PRD entries if all went okay, | |
167 | * returns 0 otherwise. | |
168 | * | |
169 | * May also be invoked from trm290.c | |
170 | */ | |
171 | ||
172 | int ide_build_dmatable (ide_drive_t *drive, struct request *rq) | |
173 | { | |
174 | ide_hwif_t *hwif = HWIF(drive); | |
175 | unsigned int *table = hwif->dmatable_cpu; | |
176 | unsigned int is_trm290 = (hwif->chipset == ide_trm290) ? 1 : 0; | |
177 | unsigned int count = 0; | |
178 | int i; | |
179 | struct scatterlist *sg; | |
180 | ||
181 | hwif->sg_nents = i = ide_build_sglist(drive, rq); | |
182 | ||
183 | if (!i) | |
184 | return 0; | |
185 | ||
186 | sg = hwif->sg_table; | |
187 | while (i) { | |
188 | u32 cur_addr; | |
189 | u32 cur_len; | |
190 | ||
191 | cur_addr = sg_dma_address(sg); | |
192 | cur_len = sg_dma_len(sg); | |
193 | ||
194 | /* | |
195 | * Fill in the dma table, without crossing any 64kB boundaries. | |
196 | * Most hardware requires 16-bit alignment of all blocks, | |
197 | * but the trm290 requires 32-bit alignment. | |
198 | */ | |
199 | ||
200 | while (cur_len) { | |
201 | if (count++ >= PRD_ENTRIES) { | |
202 | printk(KERN_ERR "%s: DMA table too small\n", drive->name); | |
203 | goto use_pio_instead; | |
204 | } else { | |
205 | u32 xcount, bcount = 0x10000 - (cur_addr & 0xffff); | |
206 | ||
207 | if (bcount > cur_len) | |
208 | bcount = cur_len; | |
209 | *table++ = cpu_to_le32(cur_addr); | |
210 | xcount = bcount & 0xffff; | |
211 | if (is_trm290) | |
212 | xcount = ((xcount >> 2) - 1) << 16; | |
213 | if (xcount == 0x0000) { | |
214 | /* | |
215 | * Most chipsets correctly interpret a length of 0x0000 as 64KB, | |
216 | * but at least one (e.g. CS5530) misinterprets it as zero (!). | |
217 | * So here we break the 64KB entry into two 32KB entries instead. | |
218 | */ | |
219 | if (count++ >= PRD_ENTRIES) { | |
220 | printk(KERN_ERR "%s: DMA table too small\n", drive->name); | |
221 | goto use_pio_instead; | |
222 | } | |
223 | *table++ = cpu_to_le32(0x8000); | |
224 | *table++ = cpu_to_le32(cur_addr + 0x8000); | |
225 | xcount = 0x8000; | |
226 | } | |
227 | *table++ = cpu_to_le32(xcount); | |
228 | cur_addr += bcount; | |
229 | cur_len -= bcount; | |
230 | } | |
231 | } | |
232 | ||
55c16a70 | 233 | sg = sg_next(sg); |
1da177e4 LT |
234 | i--; |
235 | } | |
236 | ||
237 | if (count) { | |
238 | if (!is_trm290) | |
239 | *--table |= cpu_to_le32(0x80000000); | |
240 | return count; | |
241 | } | |
f6fb786d | 242 | |
1da177e4 | 243 | printk(KERN_ERR "%s: empty DMA table?\n", drive->name); |
f6fb786d | 244 | |
1da177e4 | 245 | use_pio_instead: |
f6fb786d BZ |
246 | ide_destroy_dmatable(drive); |
247 | ||
1da177e4 LT |
248 | return 0; /* revert to PIO for this request */ |
249 | } | |
250 | ||
251 | EXPORT_SYMBOL_GPL(ide_build_dmatable); | |
062f9f02 | 252 | #endif |
1da177e4 LT |
253 | |
254 | /** | |
255 | * ide_destroy_dmatable - clean up DMA mapping | |
256 | * @drive: The drive to unmap | |
257 | * | |
258 | * Teardown mappings after DMA has completed. This must be called | |
259 | * after the completion of each use of ide_build_dmatable and before | |
260 | * the next use of ide_build_dmatable. Failure to do so will cause | |
261 | * an oops as only one mapping can be live for each target at a given | |
262 | * time. | |
263 | */ | |
264 | ||
265 | void ide_destroy_dmatable (ide_drive_t *drive) | |
266 | { | |
36501650 | 267 | ide_hwif_t *hwif = drive->hwif; |
1da177e4 | 268 | |
5c05ff68 | 269 | dma_unmap_sg(hwif->dev, hwif->sg_table, hwif->sg_nents, |
36501650 | 270 | hwif->sg_dma_direction); |
1da177e4 LT |
271 | } |
272 | ||
273 | EXPORT_SYMBOL_GPL(ide_destroy_dmatable); | |
274 | ||
8e882ba1 | 275 | #ifdef CONFIG_BLK_DEV_IDEDMA_SFF |
1da177e4 LT |
276 | /** |
277 | * config_drive_for_dma - attempt to activate IDE DMA | |
278 | * @drive: the drive to place in DMA mode | |
279 | * | |
280 | * If the drive supports at least mode 2 DMA or UDMA of any kind | |
281 | * then attempt to place it into DMA mode. Drives that are known to | |
282 | * support DMA but predate the DMA properties or that are known | |
283 | * to have DMA handling bugs are also set up appropriately based | |
284 | * on the good/bad drive lists. | |
285 | */ | |
286 | ||
287 | static int config_drive_for_dma (ide_drive_t *drive) | |
288 | { | |
1116fae5 | 289 | ide_hwif_t *hwif = drive->hwif; |
1da177e4 | 290 | struct hd_driveid *id = drive->id; |
1da177e4 | 291 | |
33c1002e BZ |
292 | if (drive->media != ide_disk) { |
293 | if (hwif->host_flags & IDE_HFLAG_NO_ATAPI_DMA) | |
bcbf6ee3 | 294 | return 0; |
33c1002e | 295 | } |
1116fae5 | 296 | |
0ae2e178 BZ |
297 | /* |
298 | * Enable DMA on any drive that has | |
299 | * UltraDMA (mode 0/1/2/3/4/5/6) enabled | |
300 | */ | |
301 | if ((id->field_valid & 4) && ((id->dma_ultra >> 8) & 0x7f)) | |
302 | return 1; | |
303 | ||
304 | /* | |
305 | * Enable DMA on any drive that has mode2 DMA | |
306 | * (multi or single) enabled | |
307 | */ | |
308 | if (id->field_valid & 2) /* regular DMA */ | |
309 | if ((id->dma_mword & 0x404) == 0x404 || | |
310 | (id->dma_1word & 0x404) == 0x404) | |
311 | return 1; | |
3608b5d7 | 312 | |
0ae2e178 BZ |
313 | /* Consult the list of known "good" drives */ |
314 | if (ide_dma_good_drive(drive)) | |
315 | return 1; | |
316 | ||
317 | return 0; | |
1da177e4 LT |
318 | } |
319 | ||
320 | /** | |
321 | * dma_timer_expiry - handle a DMA timeout | |
322 | * @drive: Drive that timed out | |
323 | * | |
324 | * An IDE DMA transfer timed out. In the event of an error we ask | |
325 | * the driver to resolve the problem, if a DMA transfer is still | |
326 | * in progress we continue to wait (arguably we need to add a | |
327 | * secondary 'I don't care what the drive thinks' timeout here) | |
328 | * Finally if we have an interrupt we let it complete the I/O. | |
329 | * But only one time - we clear expiry and if it's still not | |
330 | * completed after WAIT_CMD, we error and retry in PIO. | |
331 | * This can occur if an interrupt is lost or due to hang or bugs. | |
332 | */ | |
333 | ||
334 | static int dma_timer_expiry (ide_drive_t *drive) | |
335 | { | |
336 | ide_hwif_t *hwif = HWIF(drive); | |
337 | u8 dma_stat = hwif->INB(hwif->dma_status); | |
338 | ||
339 | printk(KERN_WARNING "%s: dma_timer_expiry: dma status == 0x%02x\n", | |
340 | drive->name, dma_stat); | |
341 | ||
342 | if ((dma_stat & 0x18) == 0x18) /* BUSY Stupid Early Timer !! */ | |
343 | return WAIT_CMD; | |
344 | ||
345 | HWGROUP(drive)->expiry = NULL; /* one free ride for now */ | |
346 | ||
347 | /* 1 dmaing, 2 error, 4 intr */ | |
348 | if (dma_stat & 2) /* ERROR */ | |
349 | return -1; | |
350 | ||
351 | if (dma_stat & 1) /* DMAing */ | |
352 | return WAIT_CMD; | |
353 | ||
354 | if (dma_stat & 4) /* Got an Interrupt */ | |
355 | return WAIT_CMD; | |
356 | ||
357 | return 0; /* Status is unknown -- reset the bus */ | |
358 | } | |
359 | ||
360 | /** | |
15ce926a | 361 | * ide_dma_host_set - Enable/disable DMA on a host |
1da177e4 LT |
362 | * @drive: drive to control |
363 | * | |
15ce926a BZ |
364 | * Enable/disable DMA on an IDE controller following generic |
365 | * bus-mastering IDE controller behaviour. | |
1da177e4 LT |
366 | */ |
367 | ||
15ce926a | 368 | void ide_dma_host_set(ide_drive_t *drive, int on) |
1da177e4 LT |
369 | { |
370 | ide_hwif_t *hwif = HWIF(drive); | |
371 | u8 unit = (drive->select.b.unit & 0x01); | |
372 | u8 dma_stat = hwif->INB(hwif->dma_status); | |
373 | ||
15ce926a BZ |
374 | if (on) |
375 | dma_stat |= (1 << (5 + unit)); | |
376 | else | |
377 | dma_stat &= ~(1 << (5 + unit)); | |
378 | ||
379 | hwif->OUTB(dma_stat, hwif->dma_status); | |
1da177e4 LT |
380 | } |
381 | ||
15ce926a | 382 | EXPORT_SYMBOL_GPL(ide_dma_host_set); |
8e882ba1 | 383 | #endif /* CONFIG_BLK_DEV_IDEDMA_SFF */ |
1da177e4 LT |
384 | |
385 | /** | |
7469aaf6 | 386 | * ide_dma_off_quietly - Generic DMA kill |
1da177e4 LT |
387 | * @drive: drive to control |
388 | * | |
389 | * Turn off the current DMA on this IDE controller. | |
390 | */ | |
391 | ||
7469aaf6 | 392 | void ide_dma_off_quietly(ide_drive_t *drive) |
1da177e4 LT |
393 | { |
394 | drive->using_dma = 0; | |
395 | ide_toggle_bounce(drive, 0); | |
396 | ||
5e37bdc0 | 397 | drive->hwif->dma_ops->dma_host_set(drive, 0); |
1da177e4 LT |
398 | } |
399 | ||
7469aaf6 | 400 | EXPORT_SYMBOL(ide_dma_off_quietly); |
1da177e4 LT |
401 | |
402 | /** | |
7469aaf6 | 403 | * ide_dma_off - disable DMA on a device |
1da177e4 LT |
404 | * @drive: drive to disable DMA on |
405 | * | |
406 | * Disable IDE DMA for a device on this IDE controller. | |
407 | * Inform the user that DMA has been disabled. | |
408 | */ | |
409 | ||
7469aaf6 | 410 | void ide_dma_off(ide_drive_t *drive) |
1da177e4 LT |
411 | { |
412 | printk(KERN_INFO "%s: DMA disabled\n", drive->name); | |
4a546e04 | 413 | ide_dma_off_quietly(drive); |
1da177e4 LT |
414 | } |
415 | ||
7469aaf6 | 416 | EXPORT_SYMBOL(ide_dma_off); |
1da177e4 | 417 | |
1da177e4 | 418 | /** |
4a546e04 | 419 | * ide_dma_on - Enable DMA on a device |
1da177e4 LT |
420 | * @drive: drive to enable DMA on |
421 | * | |
422 | * Enable IDE DMA for a device on this IDE controller. | |
423 | */ | |
4a546e04 BZ |
424 | |
425 | void ide_dma_on(ide_drive_t *drive) | |
1da177e4 | 426 | { |
1da177e4 LT |
427 | drive->using_dma = 1; |
428 | ide_toggle_bounce(drive, 1); | |
429 | ||
5e37bdc0 | 430 | drive->hwif->dma_ops->dma_host_set(drive, 1); |
1da177e4 LT |
431 | } |
432 | ||
8e882ba1 | 433 | #ifdef CONFIG_BLK_DEV_IDEDMA_SFF |
1da177e4 LT |
434 | /** |
435 | * ide_dma_setup - begin a DMA phase | |
436 | * @drive: target device | |
437 | * | |
438 | * Build an IDE DMA PRD (IDE speak for scatter gather table) | |
439 | * and then set up the DMA transfer registers for a device | |
440 | * that follows generic IDE PCI DMA behaviour. Controllers can | |
441 | * override this function if they need to | |
442 | * | |
443 | * Returns 0 on success. If a PIO fallback is required then 1 | |
444 | * is returned. | |
445 | */ | |
446 | ||
447 | int ide_dma_setup(ide_drive_t *drive) | |
448 | { | |
449 | ide_hwif_t *hwif = drive->hwif; | |
450 | struct request *rq = HWGROUP(drive)->rq; | |
451 | unsigned int reading; | |
452 | u8 dma_stat; | |
453 | ||
454 | if (rq_data_dir(rq)) | |
455 | reading = 0; | |
456 | else | |
457 | reading = 1 << 3; | |
458 | ||
459 | /* fall back to pio! */ | |
460 | if (!ide_build_dmatable(drive, rq)) { | |
461 | ide_map_sg(drive, rq); | |
462 | return 1; | |
463 | } | |
464 | ||
465 | /* PRD table */ | |
2ad1e558 | 466 | if (hwif->mmio) |
55224bc8 BZ |
467 | writel(hwif->dmatable_dma, |
468 | (void __iomem *)(hwif->dma_base + ATA_DMA_TABLE_OFS)); | |
0ecdca26 | 469 | else |
55224bc8 | 470 | outl(hwif->dmatable_dma, hwif->dma_base + ATA_DMA_TABLE_OFS); |
1da177e4 LT |
471 | |
472 | /* specify r/w */ | |
473 | hwif->OUTB(reading, hwif->dma_command); | |
474 | ||
475 | /* read dma_status for INTR & ERROR flags */ | |
476 | dma_stat = hwif->INB(hwif->dma_status); | |
477 | ||
478 | /* clear INTR & ERROR flags */ | |
479 | hwif->OUTB(dma_stat|6, hwif->dma_status); | |
480 | drive->waiting_for_dma = 1; | |
481 | return 0; | |
482 | } | |
483 | ||
484 | EXPORT_SYMBOL_GPL(ide_dma_setup); | |
485 | ||
f37afdac | 486 | void ide_dma_exec_cmd(ide_drive_t *drive, u8 command) |
1da177e4 LT |
487 | { |
488 | /* issue cmd to drive */ | |
489 | ide_execute_command(drive, command, &ide_dma_intr, 2*WAIT_CMD, dma_timer_expiry); | |
490 | } | |
f37afdac | 491 | EXPORT_SYMBOL_GPL(ide_dma_exec_cmd); |
1da177e4 LT |
492 | |
493 | void ide_dma_start(ide_drive_t *drive) | |
494 | { | |
495 | ide_hwif_t *hwif = HWIF(drive); | |
496 | u8 dma_cmd = hwif->INB(hwif->dma_command); | |
497 | ||
498 | /* Note that this is done *after* the cmd has | |
499 | * been issued to the drive, as per the BM-IDE spec. | |
500 | * The Promise Ultra33 doesn't work correctly when | |
501 | * we do this part before issuing the drive cmd. | |
502 | */ | |
503 | /* start DMA */ | |
504 | hwif->OUTB(dma_cmd|1, hwif->dma_command); | |
505 | hwif->dma = 1; | |
506 | wmb(); | |
507 | } | |
508 | ||
509 | EXPORT_SYMBOL_GPL(ide_dma_start); | |
510 | ||
511 | /* returns 1 on error, 0 otherwise */ | |
512 | int __ide_dma_end (ide_drive_t *drive) | |
513 | { | |
514 | ide_hwif_t *hwif = HWIF(drive); | |
515 | u8 dma_stat = 0, dma_cmd = 0; | |
516 | ||
517 | drive->waiting_for_dma = 0; | |
518 | /* get dma_command mode */ | |
519 | dma_cmd = hwif->INB(hwif->dma_command); | |
520 | /* stop DMA */ | |
521 | hwif->OUTB(dma_cmd&~1, hwif->dma_command); | |
522 | /* get DMA status */ | |
523 | dma_stat = hwif->INB(hwif->dma_status); | |
524 | /* clear the INTR & ERROR bits */ | |
525 | hwif->OUTB(dma_stat|6, hwif->dma_status); | |
526 | /* purge DMA mappings */ | |
527 | ide_destroy_dmatable(drive); | |
528 | /* verify good DMA status */ | |
529 | hwif->dma = 0; | |
530 | wmb(); | |
531 | return (dma_stat & 7) != 4 ? (0x10 | dma_stat) : 0; | |
532 | } | |
533 | ||
534 | EXPORT_SYMBOL(__ide_dma_end); | |
535 | ||
536 | /* returns 1 if dma irq issued, 0 otherwise */ | |
f37afdac | 537 | int ide_dma_test_irq(ide_drive_t *drive) |
1da177e4 LT |
538 | { |
539 | ide_hwif_t *hwif = HWIF(drive); | |
540 | u8 dma_stat = hwif->INB(hwif->dma_status); | |
541 | ||
1da177e4 LT |
542 | /* return 1 if INTR asserted */ |
543 | if ((dma_stat & 4) == 4) | |
544 | return 1; | |
545 | if (!drive->waiting_for_dma) | |
546 | printk(KERN_WARNING "%s: (%s) called while not waiting\n", | |
eb63963a | 547 | drive->name, __func__); |
1da177e4 LT |
548 | return 0; |
549 | } | |
f37afdac | 550 | EXPORT_SYMBOL_GPL(ide_dma_test_irq); |
0ae2e178 BZ |
551 | #else |
552 | static inline int config_drive_for_dma(ide_drive_t *drive) { return 0; } | |
8e882ba1 | 553 | #endif /* CONFIG_BLK_DEV_IDEDMA_SFF */ |
1da177e4 LT |
554 | |
555 | int __ide_dma_bad_drive (ide_drive_t *drive) | |
556 | { | |
557 | struct hd_driveid *id = drive->id; | |
558 | ||
65e5f2e3 | 559 | int blacklist = ide_in_drive_list(id, drive_blacklist); |
1da177e4 LT |
560 | if (blacklist) { |
561 | printk(KERN_WARNING "%s: Disabling (U)DMA for %s (blacklisted)\n", | |
562 | drive->name, id->model); | |
563 | return blacklist; | |
564 | } | |
565 | return 0; | |
566 | } | |
567 | ||
568 | EXPORT_SYMBOL(__ide_dma_bad_drive); | |
569 | ||
2d5eaa6d BZ |
570 | static const u8 xfer_mode_bases[] = { |
571 | XFER_UDMA_0, | |
572 | XFER_MW_DMA_0, | |
573 | XFER_SW_DMA_0, | |
574 | }; | |
575 | ||
7670df73 | 576 | static unsigned int ide_get_mode_mask(ide_drive_t *drive, u8 base, u8 req_mode) |
2d5eaa6d BZ |
577 | { |
578 | struct hd_driveid *id = drive->id; | |
579 | ide_hwif_t *hwif = drive->hwif; | |
ac95beed | 580 | const struct ide_port_ops *port_ops = hwif->port_ops; |
2d5eaa6d BZ |
581 | unsigned int mask = 0; |
582 | ||
583 | switch(base) { | |
584 | case XFER_UDMA_0: | |
585 | if ((id->field_valid & 4) == 0) | |
586 | break; | |
587 | ||
ac95beed BZ |
588 | if (port_ops && port_ops->udma_filter) |
589 | mask = port_ops->udma_filter(drive); | |
851dd33b SS |
590 | else |
591 | mask = hwif->ultra_mask; | |
592 | mask &= id->dma_ultra; | |
2d5eaa6d | 593 | |
7670df73 BZ |
594 | /* |
595 | * avoid false cable warning from eighty_ninty_three() | |
596 | */ | |
597 | if (req_mode > XFER_UDMA_2) { | |
598 | if ((mask & 0x78) && (eighty_ninty_three(drive) == 0)) | |
599 | mask &= 0x07; | |
600 | } | |
2d5eaa6d BZ |
601 | break; |
602 | case XFER_MW_DMA_0: | |
b4e44369 SS |
603 | if ((id->field_valid & 2) == 0) |
604 | break; | |
ac95beed BZ |
605 | if (port_ops && port_ops->mdma_filter) |
606 | mask = port_ops->mdma_filter(drive); | |
b4e44369 SS |
607 | else |
608 | mask = hwif->mwdma_mask; | |
609 | mask &= id->dma_mword; | |
2d5eaa6d BZ |
610 | break; |
611 | case XFER_SW_DMA_0: | |
15a4f943 | 612 | if (id->field_valid & 2) { |
3649c06e | 613 | mask = id->dma_1word & hwif->swdma_mask; |
15a4f943 BZ |
614 | } else if (id->tDMA) { |
615 | /* | |
616 | * ide_fix_driveid() doesn't convert ->tDMA to the | |
617 | * CPU endianness so we need to do it here | |
618 | */ | |
619 | u8 mode = le16_to_cpu(id->tDMA); | |
620 | ||
621 | /* | |
622 | * if the mode is valid convert it to the mask | |
623 | * (the maximum allowed mode is XFER_SW_DMA_2) | |
624 | */ | |
625 | if (mode <= 2) | |
626 | mask = ((2 << mode) - 1) & hwif->swdma_mask; | |
627 | } | |
2d5eaa6d BZ |
628 | break; |
629 | default: | |
630 | BUG(); | |
631 | break; | |
632 | } | |
633 | ||
634 | return mask; | |
635 | } | |
636 | ||
637 | /** | |
7670df73 | 638 | * ide_find_dma_mode - compute DMA speed |
2d5eaa6d | 639 | * @drive: IDE device |
7670df73 BZ |
640 | * @req_mode: requested mode |
641 | * | |
642 | * Checks the drive/host capabilities and finds the speed to use for | |
643 | * the DMA transfer. The speed is then limited by the requested mode. | |
2d5eaa6d | 644 | * |
7670df73 BZ |
645 | * Returns 0 if the drive/host combination is incapable of DMA transfers |
646 | * or if the requested mode is not a DMA mode. | |
2d5eaa6d BZ |
647 | */ |
648 | ||
7670df73 | 649 | u8 ide_find_dma_mode(ide_drive_t *drive, u8 req_mode) |
2d5eaa6d BZ |
650 | { |
651 | ide_hwif_t *hwif = drive->hwif; | |
652 | unsigned int mask; | |
653 | int x, i; | |
654 | u8 mode = 0; | |
655 | ||
33c1002e BZ |
656 | if (drive->media != ide_disk) { |
657 | if (hwif->host_flags & IDE_HFLAG_NO_ATAPI_DMA) | |
658 | return 0; | |
659 | } | |
2d5eaa6d BZ |
660 | |
661 | for (i = 0; i < ARRAY_SIZE(xfer_mode_bases); i++) { | |
7670df73 BZ |
662 | if (req_mode < xfer_mode_bases[i]) |
663 | continue; | |
664 | mask = ide_get_mode_mask(drive, xfer_mode_bases[i], req_mode); | |
2d5eaa6d BZ |
665 | x = fls(mask) - 1; |
666 | if (x >= 0) { | |
667 | mode = xfer_mode_bases[i] + x; | |
668 | break; | |
669 | } | |
670 | } | |
671 | ||
75d7d963 BZ |
672 | if (hwif->chipset == ide_acorn && mode == 0) { |
673 | /* | |
674 | * is this correct? | |
675 | */ | |
676 | if (ide_dma_good_drive(drive) && drive->id->eide_dma_time < 150) | |
677 | mode = XFER_MW_DMA_1; | |
678 | } | |
679 | ||
3ab7efe8 BZ |
680 | mode = min(mode, req_mode); |
681 | ||
682 | printk(KERN_INFO "%s: %s mode selected\n", drive->name, | |
d34887da | 683 | mode ? ide_xfer_verbose(mode) : "no DMA"); |
2d5eaa6d | 684 | |
3ab7efe8 | 685 | return mode; |
2d5eaa6d BZ |
686 | } |
687 | ||
7670df73 | 688 | EXPORT_SYMBOL_GPL(ide_find_dma_mode); |
2d5eaa6d | 689 | |
0ae2e178 | 690 | static int ide_tune_dma(ide_drive_t *drive) |
29e744d0 | 691 | { |
8704de8f | 692 | ide_hwif_t *hwif = drive->hwif; |
29e744d0 BZ |
693 | u8 speed; |
694 | ||
931ee0dc | 695 | if (drive->nodma || (drive->id->capability & 1) == 0) |
122ab088 BZ |
696 | return 0; |
697 | ||
698 | /* consult the list of known "bad" drives */ | |
699 | if (__ide_dma_bad_drive(drive)) | |
29e744d0 BZ |
700 | return 0; |
701 | ||
3ab7efe8 BZ |
702 | if (ide_id_dma_bug(drive)) |
703 | return 0; | |
704 | ||
8704de8f | 705 | if (hwif->host_flags & IDE_HFLAG_TRUST_BIOS_FOR_DMA) |
0ae2e178 BZ |
706 | return config_drive_for_dma(drive); |
707 | ||
29e744d0 BZ |
708 | speed = ide_max_dma_mode(drive); |
709 | ||
951784b6 BZ |
710 | if (!speed) |
711 | return 0; | |
29e744d0 | 712 | |
88b2b32b | 713 | if (ide_set_dma_mode(drive, speed)) |
4728d546 | 714 | return 0; |
29e744d0 | 715 | |
4728d546 | 716 | return 1; |
29e744d0 BZ |
717 | } |
718 | ||
0ae2e178 BZ |
719 | static int ide_dma_check(ide_drive_t *drive) |
720 | { | |
721 | ide_hwif_t *hwif = drive->hwif; | |
722 | int vdma = (hwif->host_flags & IDE_HFLAG_VDMA)? 1 : 0; | |
723 | ||
724 | if (!vdma && ide_tune_dma(drive)) | |
725 | return 0; | |
726 | ||
727 | /* TODO: always do PIO fallback */ | |
728 | if (hwif->host_flags & IDE_HFLAG_TRUST_BIOS_FOR_DMA) | |
729 | return -1; | |
730 | ||
731 | ide_set_max_pio(drive); | |
732 | ||
733 | return vdma ? 0 : -1; | |
734 | } | |
735 | ||
3ab7efe8 | 736 | int ide_id_dma_bug(ide_drive_t *drive) |
1da177e4 | 737 | { |
3ab7efe8 | 738 | struct hd_driveid *id = drive->id; |
1da177e4 LT |
739 | |
740 | if (id->field_valid & 4) { | |
741 | if ((id->dma_ultra >> 8) && (id->dma_mword >> 8)) | |
3ab7efe8 | 742 | goto err_out; |
1da177e4 LT |
743 | } else if (id->field_valid & 2) { |
744 | if ((id->dma_mword >> 8) && (id->dma_1word >> 8)) | |
3ab7efe8 | 745 | goto err_out; |
1da177e4 | 746 | } |
3ab7efe8 BZ |
747 | return 0; |
748 | err_out: | |
749 | printk(KERN_ERR "%s: bad DMA info in identify block\n", drive->name); | |
750 | return 1; | |
1da177e4 LT |
751 | } |
752 | ||
3608b5d7 BZ |
753 | int ide_set_dma(ide_drive_t *drive) |
754 | { | |
3608b5d7 BZ |
755 | int rc; |
756 | ||
7b905994 BZ |
757 | /* |
758 | * Force DMAing for the beginning of the check. | |
759 | * Some chipsets appear to do interesting | |
760 | * things, if not checked and cleared. | |
761 | * PARANOIA!!! | |
762 | */ | |
4a546e04 | 763 | ide_dma_off_quietly(drive); |
3608b5d7 | 764 | |
7b905994 BZ |
765 | rc = ide_dma_check(drive); |
766 | if (rc) | |
767 | return rc; | |
3608b5d7 | 768 | |
4a546e04 BZ |
769 | ide_dma_on(drive); |
770 | ||
771 | return 0; | |
3608b5d7 BZ |
772 | } |
773 | ||
578cfa0d BZ |
774 | void ide_check_dma_crc(ide_drive_t *drive) |
775 | { | |
776 | u8 mode; | |
777 | ||
778 | ide_dma_off_quietly(drive); | |
779 | drive->crc_count = 0; | |
780 | mode = drive->current_speed; | |
781 | /* | |
782 | * Don't try non Ultra-DMA modes without iCRC's. Force the | |
783 | * device to PIO and make the user enable SWDMA/MWDMA modes. | |
784 | */ | |
785 | if (mode > XFER_UDMA_0 && mode <= XFER_UDMA_7) | |
786 | mode--; | |
787 | else | |
788 | mode = XFER_PIO_4; | |
789 | ide_set_xfer_rate(drive, mode); | |
790 | if (drive->current_speed >= XFER_SW_DMA_0) | |
791 | ide_dma_on(drive); | |
792 | } | |
793 | ||
8e882ba1 | 794 | #ifdef CONFIG_BLK_DEV_IDEDMA_SFF |
841d2a9b | 795 | void ide_dma_lost_irq (ide_drive_t *drive) |
1da177e4 LT |
796 | { |
797 | printk("%s: DMA interrupt recovery\n", drive->name); | |
1da177e4 LT |
798 | } |
799 | ||
841d2a9b | 800 | EXPORT_SYMBOL(ide_dma_lost_irq); |
1da177e4 | 801 | |
c283f5db | 802 | void ide_dma_timeout (ide_drive_t *drive) |
1da177e4 | 803 | { |
c283f5db SS |
804 | ide_hwif_t *hwif = HWIF(drive); |
805 | ||
1da177e4 | 806 | printk(KERN_ERR "%s: timeout waiting for DMA\n", drive->name); |
1da177e4 | 807 | |
5e37bdc0 | 808 | if (hwif->dma_ops->dma_test_irq(drive)) |
c283f5db SS |
809 | return; |
810 | ||
5e37bdc0 | 811 | hwif->dma_ops->dma_end(drive); |
1da177e4 LT |
812 | } |
813 | ||
c283f5db | 814 | EXPORT_SYMBOL(ide_dma_timeout); |
1da177e4 | 815 | |
0d1bad21 | 816 | void ide_release_dma_engine(ide_hwif_t *hwif) |
1da177e4 LT |
817 | { |
818 | if (hwif->dmatable_cpu) { | |
36501650 BZ |
819 | struct pci_dev *pdev = to_pci_dev(hwif->dev); |
820 | ||
821 | pci_free_consistent(pdev, PRD_ENTRIES * PRD_BYTES, | |
822 | hwif->dmatable_cpu, hwif->dmatable_dma); | |
1da177e4 LT |
823 | hwif->dmatable_cpu = NULL; |
824 | } | |
1da177e4 LT |
825 | } |
826 | ||
b8e73fba | 827 | int ide_allocate_dma_engine(ide_hwif_t *hwif) |
1da177e4 | 828 | { |
36501650 BZ |
829 | struct pci_dev *pdev = to_pci_dev(hwif->dev); |
830 | ||
831 | hwif->dmatable_cpu = pci_alloc_consistent(pdev, | |
1da177e4 LT |
832 | PRD_ENTRIES * PRD_BYTES, |
833 | &hwif->dmatable_dma); | |
834 | ||
835 | if (hwif->dmatable_cpu) | |
836 | return 0; | |
837 | ||
dc844e05 | 838 | printk(KERN_ERR "%s: -- Error, unable to allocate DMA table.\n", |
5e59c236 | 839 | hwif->name); |
1da177e4 | 840 | |
1da177e4 LT |
841 | return 1; |
842 | } | |
b8e73fba | 843 | EXPORT_SYMBOL_GPL(ide_allocate_dma_engine); |
1da177e4 | 844 | |
f37afdac | 845 | static const struct ide_dma_ops sff_dma_ops = { |
5e37bdc0 BZ |
846 | .dma_host_set = ide_dma_host_set, |
847 | .dma_setup = ide_dma_setup, | |
848 | .dma_exec_cmd = ide_dma_exec_cmd, | |
849 | .dma_start = ide_dma_start, | |
850 | .dma_end = __ide_dma_end, | |
f37afdac | 851 | .dma_test_irq = ide_dma_test_irq, |
5e37bdc0 BZ |
852 | .dma_timeout = ide_dma_timeout, |
853 | .dma_lost_irq = ide_dma_lost_irq, | |
854 | }; | |
855 | ||
f37afdac | 856 | void ide_setup_dma(ide_hwif_t *hwif, unsigned long base) |
1da177e4 | 857 | { |
a02bfd3c BZ |
858 | hwif->dma_base = base; |
859 | ||
ecf32796 SS |
860 | if (!hwif->dma_command) |
861 | hwif->dma_command = hwif->dma_base + 0; | |
ecf32796 SS |
862 | if (!hwif->dma_status) |
863 | hwif->dma_status = hwif->dma_base + 2; | |
1da177e4 | 864 | |
f37afdac | 865 | hwif->dma_ops = &sff_dma_ops; |
1da177e4 LT |
866 | } |
867 | ||
868 | EXPORT_SYMBOL_GPL(ide_setup_dma); | |
8e882ba1 | 869 | #endif /* CONFIG_BLK_DEV_IDEDMA_SFF */ |