Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Bartlomiej Zolnierkiewicz | 204f47c | 2008-02-26 21:50:36 +0100 | [diff] [blame] | 2 | * IDE DMA support (including IDE PCI BM-DMA). |
| 3 | * |
Bartlomiej Zolnierkiewicz | 59bca8c | 2008-02-01 23:09:33 +0100 | [diff] [blame] | 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 |
Bartlomiej Zolnierkiewicz | 58f189f | 2008-02-01 23:09:33 +0100 | [diff] [blame] | 7 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * May be copied or modified under the terms of the GNU General Public License |
Bartlomiej Zolnierkiewicz | 204f47c | 2008-02-26 21:50:36 +0100 | [diff] [blame] | 9 | * |
| 10 | * DMA is supported for all IDE devices (disk drives, cdroms, tapes, floppies). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | /* |
| 14 | * Special Thanks to Mark for his Six years of work. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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! |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | */ |
| 30 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Bartlomiej Zolnierkiewicz | 5c05ff6 | 2008-02-01 23:09:32 +0100 | [diff] [blame] | 42 | #include <linux/dma-mapping.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| 44 | #include <asm/io.h> |
| 45 | #include <asm/irq.h> |
| 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | static const struct drive_list_entry drive_whitelist [] = { |
| 48 | |
Junio C Hamano | c2d3ce8 | 2007-07-09 23:17:56 +0200 | [diff] [blame] | 49 | { "Micropolis 2112A" , NULL }, |
| 50 | { "CONNER CTMA 4000" , NULL }, |
| 51 | { "CONNER CTT8000-A" , NULL }, |
| 52 | { "ST34342A" , NULL }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | { NULL , NULL } |
| 54 | }; |
| 55 | |
| 56 | static const struct drive_list_entry drive_blacklist [] = { |
| 57 | |
Junio C Hamano | c2d3ce8 | 2007-07-09 23:17:56 +0200 | [diff] [blame] | 58 | { "WDC AC11000H" , NULL }, |
| 59 | { "WDC AC22100H" , NULL }, |
| 60 | { "WDC AC32500H" , NULL }, |
| 61 | { "WDC AC33100H" , NULL }, |
| 62 | { "WDC AC31600H" , NULL }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | { "WDC AC32100H" , "24.09P07" }, |
| 64 | { "WDC AC23200L" , "21.10N21" }, |
Junio C Hamano | c2d3ce8 | 2007-07-09 23:17:56 +0200 | [diff] [blame] | 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 }, |
Junio C Hamano | 5a6248c | 2007-05-24 02:42:38 +0200 | [diff] [blame] | 86 | { "SAMSUNG CD-ROM SN-124", "N001" }, |
Junio C Hamano | c2d3ce8 | 2007-07-09 23:17:56 +0200 | [diff] [blame] | 87 | { "Seagate STT20000A", NULL }, |
Bartlomiej Zolnierkiewicz | b0bc65b | 2007-11-27 21:35:56 +0100 | [diff] [blame] | 88 | { "CD-ROM CDR_U200", "1.09" }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | { NULL , NULL } |
| 90 | |
| 91 | }; |
| 92 | |
| 93 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 105 | dma_stat = HWIF(drive)->ide_dma_end(drive); |
Bartlomiej Zolnierkiewicz | c47137a | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 106 | stat = ide_read_status(drive); |
| 107 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | if (OK_STAT(stat,DRIVE_READY,drive->bad_wstat|DRQ_STAT)) { |
| 109 | if (!dma_stat) { |
| 110 | struct request *rq = HWGROUP(drive)->rq; |
| 111 | |
Tejun Heo | 4d7a984 | 2008-01-26 20:13:11 +0100 | [diff] [blame] | 112 | task_end_request(drive, rq, stat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Bartlomiej Zolnierkiewicz | 75d7d96 | 2007-10-13 17:47:50 +0200 | [diff] [blame] | 123 | static int ide_dma_good_drive(ide_drive_t *drive) |
| 124 | { |
| 125 | return ide_in_drive_list(drive->id, drive_whitelist); |
| 126 | } |
| 127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Bartlomiej Zolnierkiewicz | 5c05ff6 | 2008-02-01 23:09:32 +0100 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | * kernel provide the necessary cache management so that we can |
Bartlomiej Zolnierkiewicz | 5c05ff6 | 2008-02-01 23:09:32 +0100 | [diff] [blame] | 136 | * operate in a portable fashion. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | ide_map_sg(drive, rq); |
| 145 | |
| 146 | if (rq_data_dir(rq) == READ) |
Bartlomiej Zolnierkiewicz | 5c05ff6 | 2008-02-01 23:09:32 +0100 | [diff] [blame] | 147 | hwif->sg_dma_direction = DMA_FROM_DEVICE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | else |
Bartlomiej Zolnierkiewicz | 5c05ff6 | 2008-02-01 23:09:32 +0100 | [diff] [blame] | 149 | hwif->sg_dma_direction = DMA_TO_DEVICE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | |
Bartlomiej Zolnierkiewicz | 5c05ff6 | 2008-02-01 23:09:32 +0100 | [diff] [blame] | 151 | return dma_map_sg(hwif->dev, sg, hwif->sg_nents, |
| 152 | hwif->sg_dma_direction); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | EXPORT_SYMBOL_GPL(ide_build_sglist); |
| 156 | |
Sergei Shtylyov | 8e882ba | 2008-02-11 00:32:14 +0100 | [diff] [blame] | 157 | #ifdef CONFIG_BLK_DEV_IDEDMA_SFF |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Jens Axboe | 55c16a7 | 2007-07-25 08:13:56 +0200 | [diff] [blame] | 233 | sg = sg_next(sg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | i--; |
| 235 | } |
| 236 | |
| 237 | if (count) { |
| 238 | if (!is_trm290) |
| 239 | *--table |= cpu_to_le32(0x80000000); |
| 240 | return count; |
| 241 | } |
Bartlomiej Zolnierkiewicz | f6fb786 | 2008-02-01 23:09:31 +0100 | [diff] [blame] | 242 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | printk(KERN_ERR "%s: empty DMA table?\n", drive->name); |
Bartlomiej Zolnierkiewicz | f6fb786 | 2008-02-01 23:09:31 +0100 | [diff] [blame] | 244 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | use_pio_instead: |
Bartlomiej Zolnierkiewicz | f6fb786 | 2008-02-01 23:09:31 +0100 | [diff] [blame] | 246 | ide_destroy_dmatable(drive); |
| 247 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | return 0; /* revert to PIO for this request */ |
| 249 | } |
| 250 | |
| 251 | EXPORT_SYMBOL_GPL(ide_build_dmatable); |
Bartlomiej Zolnierkiewicz | 062f9f0 | 2008-02-01 23:09:32 +0100 | [diff] [blame] | 252 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { |
Bartlomiej Zolnierkiewicz | 3650165 | 2008-02-01 23:09:31 +0100 | [diff] [blame] | 267 | ide_hwif_t *hwif = drive->hwif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | |
Bartlomiej Zolnierkiewicz | 5c05ff6 | 2008-02-01 23:09:32 +0100 | [diff] [blame] | 269 | dma_unmap_sg(hwif->dev, hwif->sg_table, hwif->sg_nents, |
Bartlomiej Zolnierkiewicz | 3650165 | 2008-02-01 23:09:31 +0100 | [diff] [blame] | 270 | hwif->sg_dma_direction); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | EXPORT_SYMBOL_GPL(ide_destroy_dmatable); |
| 274 | |
Sergei Shtylyov | 8e882ba | 2008-02-11 00:32:14 +0100 | [diff] [blame] | 275 | #ifdef CONFIG_BLK_DEV_IDEDMA_SFF |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { |
Bartlomiej Zolnierkiewicz | 1116fae | 2007-08-20 22:42:55 +0200 | [diff] [blame] | 289 | ide_hwif_t *hwif = drive->hwif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | struct hd_driveid *id = drive->id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | |
Bartlomiej Zolnierkiewicz | 33c1002 | 2007-10-19 00:30:06 +0200 | [diff] [blame] | 292 | if (drive->media != ide_disk) { |
| 293 | if (hwif->host_flags & IDE_HFLAG_NO_ATAPI_DMA) |
Bartlomiej Zolnierkiewicz | bcbf6ee | 2007-11-05 21:42:30 +0100 | [diff] [blame] | 294 | return 0; |
Bartlomiej Zolnierkiewicz | 33c1002 | 2007-10-19 00:30:06 +0200 | [diff] [blame] | 295 | } |
Bartlomiej Zolnierkiewicz | 1116fae | 2007-08-20 22:42:55 +0200 | [diff] [blame] | 296 | |
Bartlomiej Zolnierkiewicz | 0ae2e17 | 2007-10-16 22:29:55 +0200 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | |
Bartlomiej Zolnierkiewicz | 0ae2e17 | 2007-10-16 22:29:55 +0200 | [diff] [blame] | 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; |
Bartlomiej Zolnierkiewicz | 3608b5d | 2007-02-17 02:40:26 +0100 | [diff] [blame] | 312 | |
Bartlomiej Zolnierkiewicz | 0ae2e17 | 2007-10-16 22:29:55 +0200 | [diff] [blame] | 313 | /* Consult the list of known "good" drives */ |
| 314 | if (ide_dma_good_drive(drive)) |
| 315 | return 1; |
| 316 | |
| 317 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | /** |
Bartlomiej Zolnierkiewicz | 15ce926 | 2008-01-26 20:13:03 +0100 | [diff] [blame] | 361 | * ide_dma_host_set - Enable/disable DMA on a host |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | * @drive: drive to control |
| 363 | * |
Bartlomiej Zolnierkiewicz | 15ce926 | 2008-01-26 20:13:03 +0100 | [diff] [blame] | 364 | * Enable/disable DMA on an IDE controller following generic |
| 365 | * bus-mastering IDE controller behaviour. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | */ |
| 367 | |
Bartlomiej Zolnierkiewicz | 15ce926 | 2008-01-26 20:13:03 +0100 | [diff] [blame] | 368 | void ide_dma_host_set(ide_drive_t *drive, int on) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Bartlomiej Zolnierkiewicz | 15ce926 | 2008-01-26 20:13:03 +0100 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Bartlomiej Zolnierkiewicz | 15ce926 | 2008-01-26 20:13:03 +0100 | [diff] [blame] | 382 | EXPORT_SYMBOL_GPL(ide_dma_host_set); |
Sergei Shtylyov | 8e882ba | 2008-02-11 00:32:14 +0100 | [diff] [blame] | 383 | #endif /* CONFIG_BLK_DEV_IDEDMA_SFF */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | |
| 385 | /** |
Bartlomiej Zolnierkiewicz | 7469aaf | 2007-02-17 02:40:26 +0100 | [diff] [blame] | 386 | * ide_dma_off_quietly - Generic DMA kill |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | * @drive: drive to control |
| 388 | * |
| 389 | * Turn off the current DMA on this IDE controller. |
| 390 | */ |
| 391 | |
Bartlomiej Zolnierkiewicz | 7469aaf | 2007-02-17 02:40:26 +0100 | [diff] [blame] | 392 | void ide_dma_off_quietly(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | { |
| 394 | drive->using_dma = 0; |
| 395 | ide_toggle_bounce(drive, 0); |
| 396 | |
Bartlomiej Zolnierkiewicz | 15ce926 | 2008-01-26 20:13:03 +0100 | [diff] [blame] | 397 | drive->hwif->dma_host_set(drive, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | } |
| 399 | |
Bartlomiej Zolnierkiewicz | 7469aaf | 2007-02-17 02:40:26 +0100 | [diff] [blame] | 400 | EXPORT_SYMBOL(ide_dma_off_quietly); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | |
| 402 | /** |
Bartlomiej Zolnierkiewicz | 7469aaf | 2007-02-17 02:40:26 +0100 | [diff] [blame] | 403 | * ide_dma_off - disable DMA on a device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Bartlomiej Zolnierkiewicz | 7469aaf | 2007-02-17 02:40:26 +0100 | [diff] [blame] | 410 | void ide_dma_off(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | { |
| 412 | printk(KERN_INFO "%s: DMA disabled\n", drive->name); |
Bartlomiej Zolnierkiewicz | 4a546e0 | 2008-01-26 20:13:01 +0100 | [diff] [blame] | 413 | ide_dma_off_quietly(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Bartlomiej Zolnierkiewicz | 7469aaf | 2007-02-17 02:40:26 +0100 | [diff] [blame] | 416 | EXPORT_SYMBOL(ide_dma_off); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | /** |
Bartlomiej Zolnierkiewicz | 4a546e0 | 2008-01-26 20:13:01 +0100 | [diff] [blame] | 419 | * ide_dma_on - Enable DMA on a device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | * @drive: drive to enable DMA on |
| 421 | * |
| 422 | * Enable IDE DMA for a device on this IDE controller. |
| 423 | */ |
Bartlomiej Zolnierkiewicz | 4a546e0 | 2008-01-26 20:13:01 +0100 | [diff] [blame] | 424 | |
| 425 | void ide_dma_on(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | drive->using_dma = 1; |
| 428 | ide_toggle_bounce(drive, 1); |
| 429 | |
Bartlomiej Zolnierkiewicz | 15ce926 | 2008-01-26 20:13:03 +0100 | [diff] [blame] | 430 | drive->hwif->dma_host_set(drive, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | } |
| 432 | |
Sergei Shtylyov | 8e882ba | 2008-02-11 00:32:14 +0100 | [diff] [blame] | 433 | #ifdef CONFIG_BLK_DEV_IDEDMA_SFF |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ |
Bartlomiej Zolnierkiewicz | 2ad1e55 | 2007-02-17 02:40:25 +0100 | [diff] [blame] | 466 | if (hwif->mmio) |
Bartlomiej Zolnierkiewicz | 0ecdca2 | 2007-02-17 02:40:25 +0100 | [diff] [blame] | 467 | writel(hwif->dmatable_dma, (void __iomem *)hwif->dma_prdtable); |
| 468 | else |
| 469 | outl(hwif->dmatable_dma, hwif->dma_prdtable); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | |
| 471 | /* specify r/w */ |
| 472 | hwif->OUTB(reading, hwif->dma_command); |
| 473 | |
| 474 | /* read dma_status for INTR & ERROR flags */ |
| 475 | dma_stat = hwif->INB(hwif->dma_status); |
| 476 | |
| 477 | /* clear INTR & ERROR flags */ |
| 478 | hwif->OUTB(dma_stat|6, hwif->dma_status); |
| 479 | drive->waiting_for_dma = 1; |
| 480 | return 0; |
| 481 | } |
| 482 | |
| 483 | EXPORT_SYMBOL_GPL(ide_dma_setup); |
| 484 | |
| 485 | static void ide_dma_exec_cmd(ide_drive_t *drive, u8 command) |
| 486 | { |
| 487 | /* issue cmd to drive */ |
| 488 | ide_execute_command(drive, command, &ide_dma_intr, 2*WAIT_CMD, dma_timer_expiry); |
| 489 | } |
| 490 | |
| 491 | void ide_dma_start(ide_drive_t *drive) |
| 492 | { |
| 493 | ide_hwif_t *hwif = HWIF(drive); |
| 494 | u8 dma_cmd = hwif->INB(hwif->dma_command); |
| 495 | |
| 496 | /* Note that this is done *after* the cmd has |
| 497 | * been issued to the drive, as per the BM-IDE spec. |
| 498 | * The Promise Ultra33 doesn't work correctly when |
| 499 | * we do this part before issuing the drive cmd. |
| 500 | */ |
| 501 | /* start DMA */ |
| 502 | hwif->OUTB(dma_cmd|1, hwif->dma_command); |
| 503 | hwif->dma = 1; |
| 504 | wmb(); |
| 505 | } |
| 506 | |
| 507 | EXPORT_SYMBOL_GPL(ide_dma_start); |
| 508 | |
| 509 | /* returns 1 on error, 0 otherwise */ |
| 510 | int __ide_dma_end (ide_drive_t *drive) |
| 511 | { |
| 512 | ide_hwif_t *hwif = HWIF(drive); |
| 513 | u8 dma_stat = 0, dma_cmd = 0; |
| 514 | |
| 515 | drive->waiting_for_dma = 0; |
| 516 | /* get dma_command mode */ |
| 517 | dma_cmd = hwif->INB(hwif->dma_command); |
| 518 | /* stop DMA */ |
| 519 | hwif->OUTB(dma_cmd&~1, hwif->dma_command); |
| 520 | /* get DMA status */ |
| 521 | dma_stat = hwif->INB(hwif->dma_status); |
| 522 | /* clear the INTR & ERROR bits */ |
| 523 | hwif->OUTB(dma_stat|6, hwif->dma_status); |
| 524 | /* purge DMA mappings */ |
| 525 | ide_destroy_dmatable(drive); |
| 526 | /* verify good DMA status */ |
| 527 | hwif->dma = 0; |
| 528 | wmb(); |
| 529 | return (dma_stat & 7) != 4 ? (0x10 | dma_stat) : 0; |
| 530 | } |
| 531 | |
| 532 | EXPORT_SYMBOL(__ide_dma_end); |
| 533 | |
| 534 | /* returns 1 if dma irq issued, 0 otherwise */ |
| 535 | static int __ide_dma_test_irq(ide_drive_t *drive) |
| 536 | { |
| 537 | ide_hwif_t *hwif = HWIF(drive); |
| 538 | u8 dma_stat = hwif->INB(hwif->dma_status); |
| 539 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | /* return 1 if INTR asserted */ |
| 541 | if ((dma_stat & 4) == 4) |
| 542 | return 1; |
| 543 | if (!drive->waiting_for_dma) |
| 544 | printk(KERN_WARNING "%s: (%s) called while not waiting\n", |
| 545 | drive->name, __FUNCTION__); |
| 546 | return 0; |
| 547 | } |
Bartlomiej Zolnierkiewicz | 0ae2e17 | 2007-10-16 22:29:55 +0200 | [diff] [blame] | 548 | #else |
| 549 | static inline int config_drive_for_dma(ide_drive_t *drive) { return 0; } |
Sergei Shtylyov | 8e882ba | 2008-02-11 00:32:14 +0100 | [diff] [blame] | 550 | #endif /* CONFIG_BLK_DEV_IDEDMA_SFF */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | |
| 552 | int __ide_dma_bad_drive (ide_drive_t *drive) |
| 553 | { |
| 554 | struct hd_driveid *id = drive->id; |
| 555 | |
Jordan Crouse | 65e5f2e | 2005-12-15 02:16:18 +0100 | [diff] [blame] | 556 | int blacklist = ide_in_drive_list(id, drive_blacklist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | if (blacklist) { |
| 558 | printk(KERN_WARNING "%s: Disabling (U)DMA for %s (blacklisted)\n", |
| 559 | drive->name, id->model); |
| 560 | return blacklist; |
| 561 | } |
| 562 | return 0; |
| 563 | } |
| 564 | |
| 565 | EXPORT_SYMBOL(__ide_dma_bad_drive); |
| 566 | |
Bartlomiej Zolnierkiewicz | 2d5eaa6 | 2007-05-10 00:01:08 +0200 | [diff] [blame] | 567 | static const u8 xfer_mode_bases[] = { |
| 568 | XFER_UDMA_0, |
| 569 | XFER_MW_DMA_0, |
| 570 | XFER_SW_DMA_0, |
| 571 | }; |
| 572 | |
Bartlomiej Zolnierkiewicz | 7670df7 | 2007-10-11 23:53:59 +0200 | [diff] [blame] | 573 | static unsigned int ide_get_mode_mask(ide_drive_t *drive, u8 base, u8 req_mode) |
Bartlomiej Zolnierkiewicz | 2d5eaa6 | 2007-05-10 00:01:08 +0200 | [diff] [blame] | 574 | { |
| 575 | struct hd_driveid *id = drive->id; |
| 576 | ide_hwif_t *hwif = drive->hwif; |
| 577 | unsigned int mask = 0; |
| 578 | |
| 579 | switch(base) { |
| 580 | case XFER_UDMA_0: |
| 581 | if ((id->field_valid & 4) == 0) |
| 582 | break; |
| 583 | |
Bartlomiej Zolnierkiewicz | 2d5eaa6 | 2007-05-10 00:01:08 +0200 | [diff] [blame] | 584 | if (hwif->udma_filter) |
Sergei Shtylyov | 851dd33 | 2007-10-11 23:53:59 +0200 | [diff] [blame] | 585 | mask = hwif->udma_filter(drive); |
| 586 | else |
| 587 | mask = hwif->ultra_mask; |
| 588 | mask &= id->dma_ultra; |
Bartlomiej Zolnierkiewicz | 2d5eaa6 | 2007-05-10 00:01:08 +0200 | [diff] [blame] | 589 | |
Bartlomiej Zolnierkiewicz | 7670df7 | 2007-10-11 23:53:59 +0200 | [diff] [blame] | 590 | /* |
| 591 | * avoid false cable warning from eighty_ninty_three() |
| 592 | */ |
| 593 | if (req_mode > XFER_UDMA_2) { |
| 594 | if ((mask & 0x78) && (eighty_ninty_three(drive) == 0)) |
| 595 | mask &= 0x07; |
| 596 | } |
Bartlomiej Zolnierkiewicz | 2d5eaa6 | 2007-05-10 00:01:08 +0200 | [diff] [blame] | 597 | break; |
| 598 | case XFER_MW_DMA_0: |
Sergei Shtylyov | b4e4436 | 2007-10-11 23:53:58 +0200 | [diff] [blame] | 599 | if ((id->field_valid & 2) == 0) |
| 600 | break; |
| 601 | if (hwif->mdma_filter) |
| 602 | mask = hwif->mdma_filter(drive); |
| 603 | else |
| 604 | mask = hwif->mwdma_mask; |
| 605 | mask &= id->dma_mword; |
Bartlomiej Zolnierkiewicz | 2d5eaa6 | 2007-05-10 00:01:08 +0200 | [diff] [blame] | 606 | break; |
| 607 | case XFER_SW_DMA_0: |
Bartlomiej Zolnierkiewicz | 15a4f94 | 2007-07-09 23:17:57 +0200 | [diff] [blame] | 608 | if (id->field_valid & 2) { |
Bartlomiej Zolnierkiewicz | 3649c06 | 2007-05-16 00:51:46 +0200 | [diff] [blame] | 609 | mask = id->dma_1word & hwif->swdma_mask; |
Bartlomiej Zolnierkiewicz | 15a4f94 | 2007-07-09 23:17:57 +0200 | [diff] [blame] | 610 | } else if (id->tDMA) { |
| 611 | /* |
| 612 | * ide_fix_driveid() doesn't convert ->tDMA to the |
| 613 | * CPU endianness so we need to do it here |
| 614 | */ |
| 615 | u8 mode = le16_to_cpu(id->tDMA); |
| 616 | |
| 617 | /* |
| 618 | * if the mode is valid convert it to the mask |
| 619 | * (the maximum allowed mode is XFER_SW_DMA_2) |
| 620 | */ |
| 621 | if (mode <= 2) |
| 622 | mask = ((2 << mode) - 1) & hwif->swdma_mask; |
| 623 | } |
Bartlomiej Zolnierkiewicz | 2d5eaa6 | 2007-05-10 00:01:08 +0200 | [diff] [blame] | 624 | break; |
| 625 | default: |
| 626 | BUG(); |
| 627 | break; |
| 628 | } |
| 629 | |
| 630 | return mask; |
| 631 | } |
| 632 | |
| 633 | /** |
Bartlomiej Zolnierkiewicz | 7670df7 | 2007-10-11 23:53:59 +0200 | [diff] [blame] | 634 | * ide_find_dma_mode - compute DMA speed |
Bartlomiej Zolnierkiewicz | 2d5eaa6 | 2007-05-10 00:01:08 +0200 | [diff] [blame] | 635 | * @drive: IDE device |
Bartlomiej Zolnierkiewicz | 7670df7 | 2007-10-11 23:53:59 +0200 | [diff] [blame] | 636 | * @req_mode: requested mode |
Bartlomiej Zolnierkiewicz | 2d5eaa6 | 2007-05-10 00:01:08 +0200 | [diff] [blame] | 637 | * |
Bartlomiej Zolnierkiewicz | 7670df7 | 2007-10-11 23:53:59 +0200 | [diff] [blame] | 638 | * Checks the drive/host capabilities and finds the speed to use for |
| 639 | * the DMA transfer. The speed is then limited by the requested mode. |
| 640 | * |
| 641 | * Returns 0 if the drive/host combination is incapable of DMA transfers |
| 642 | * or if the requested mode is not a DMA mode. |
Bartlomiej Zolnierkiewicz | 2d5eaa6 | 2007-05-10 00:01:08 +0200 | [diff] [blame] | 643 | */ |
| 644 | |
Bartlomiej Zolnierkiewicz | 7670df7 | 2007-10-11 23:53:59 +0200 | [diff] [blame] | 645 | u8 ide_find_dma_mode(ide_drive_t *drive, u8 req_mode) |
Bartlomiej Zolnierkiewicz | 2d5eaa6 | 2007-05-10 00:01:08 +0200 | [diff] [blame] | 646 | { |
| 647 | ide_hwif_t *hwif = drive->hwif; |
| 648 | unsigned int mask; |
| 649 | int x, i; |
| 650 | u8 mode = 0; |
| 651 | |
Bartlomiej Zolnierkiewicz | 33c1002 | 2007-10-19 00:30:06 +0200 | [diff] [blame] | 652 | if (drive->media != ide_disk) { |
| 653 | if (hwif->host_flags & IDE_HFLAG_NO_ATAPI_DMA) |
| 654 | return 0; |
| 655 | } |
Bartlomiej Zolnierkiewicz | 2d5eaa6 | 2007-05-10 00:01:08 +0200 | [diff] [blame] | 656 | |
| 657 | for (i = 0; i < ARRAY_SIZE(xfer_mode_bases); i++) { |
Bartlomiej Zolnierkiewicz | 7670df7 | 2007-10-11 23:53:59 +0200 | [diff] [blame] | 658 | if (req_mode < xfer_mode_bases[i]) |
| 659 | continue; |
| 660 | mask = ide_get_mode_mask(drive, xfer_mode_bases[i], req_mode); |
Bartlomiej Zolnierkiewicz | 2d5eaa6 | 2007-05-10 00:01:08 +0200 | [diff] [blame] | 661 | x = fls(mask) - 1; |
| 662 | if (x >= 0) { |
| 663 | mode = xfer_mode_bases[i] + x; |
| 664 | break; |
| 665 | } |
| 666 | } |
| 667 | |
Bartlomiej Zolnierkiewicz | 75d7d96 | 2007-10-13 17:47:50 +0200 | [diff] [blame] | 668 | if (hwif->chipset == ide_acorn && mode == 0) { |
| 669 | /* |
| 670 | * is this correct? |
| 671 | */ |
| 672 | if (ide_dma_good_drive(drive) && drive->id->eide_dma_time < 150) |
| 673 | mode = XFER_MW_DMA_1; |
| 674 | } |
| 675 | |
Bartlomiej Zolnierkiewicz | 3ab7efe | 2007-12-12 23:31:58 +0100 | [diff] [blame] | 676 | mode = min(mode, req_mode); |
| 677 | |
| 678 | printk(KERN_INFO "%s: %s mode selected\n", drive->name, |
Bartlomiej Zolnierkiewicz | d34887d | 2007-11-05 21:42:27 +0100 | [diff] [blame] | 679 | mode ? ide_xfer_verbose(mode) : "no DMA"); |
Bartlomiej Zolnierkiewicz | 2d5eaa6 | 2007-05-10 00:01:08 +0200 | [diff] [blame] | 680 | |
Bartlomiej Zolnierkiewicz | 3ab7efe | 2007-12-12 23:31:58 +0100 | [diff] [blame] | 681 | return mode; |
Bartlomiej Zolnierkiewicz | 2d5eaa6 | 2007-05-10 00:01:08 +0200 | [diff] [blame] | 682 | } |
| 683 | |
Bartlomiej Zolnierkiewicz | 7670df7 | 2007-10-11 23:53:59 +0200 | [diff] [blame] | 684 | EXPORT_SYMBOL_GPL(ide_find_dma_mode); |
Bartlomiej Zolnierkiewicz | 2d5eaa6 | 2007-05-10 00:01:08 +0200 | [diff] [blame] | 685 | |
Bartlomiej Zolnierkiewicz | 0ae2e17 | 2007-10-16 22:29:55 +0200 | [diff] [blame] | 686 | static int ide_tune_dma(ide_drive_t *drive) |
Bartlomiej Zolnierkiewicz | 29e744d | 2007-05-10 00:01:09 +0200 | [diff] [blame] | 687 | { |
Bartlomiej Zolnierkiewicz | 8704de8 | 2008-01-26 20:13:00 +0100 | [diff] [blame] | 688 | ide_hwif_t *hwif = drive->hwif; |
Bartlomiej Zolnierkiewicz | 29e744d | 2007-05-10 00:01:09 +0200 | [diff] [blame] | 689 | u8 speed; |
| 690 | |
Bartlomiej Zolnierkiewicz | c223701 | 2007-10-16 22:29:58 +0200 | [diff] [blame] | 691 | if (noautodma || drive->nodma || (drive->id->capability & 1) == 0) |
Bartlomiej Zolnierkiewicz | 122ab08 | 2007-05-16 00:51:46 +0200 | [diff] [blame] | 692 | return 0; |
| 693 | |
| 694 | /* consult the list of known "bad" drives */ |
| 695 | if (__ide_dma_bad_drive(drive)) |
Bartlomiej Zolnierkiewicz | 29e744d | 2007-05-10 00:01:09 +0200 | [diff] [blame] | 696 | return 0; |
| 697 | |
Bartlomiej Zolnierkiewicz | 3ab7efe | 2007-12-12 23:31:58 +0100 | [diff] [blame] | 698 | if (ide_id_dma_bug(drive)) |
| 699 | return 0; |
| 700 | |
Bartlomiej Zolnierkiewicz | 8704de8 | 2008-01-26 20:13:00 +0100 | [diff] [blame] | 701 | if (hwif->host_flags & IDE_HFLAG_TRUST_BIOS_FOR_DMA) |
Bartlomiej Zolnierkiewicz | 0ae2e17 | 2007-10-16 22:29:55 +0200 | [diff] [blame] | 702 | return config_drive_for_dma(drive); |
| 703 | |
Bartlomiej Zolnierkiewicz | 29e744d | 2007-05-10 00:01:09 +0200 | [diff] [blame] | 704 | speed = ide_max_dma_mode(drive); |
| 705 | |
Bartlomiej Zolnierkiewicz | 8704de8 | 2008-01-26 20:13:00 +0100 | [diff] [blame] | 706 | if (!speed) { |
| 707 | /* is this really correct/needed? */ |
| 708 | if ((hwif->host_flags & IDE_HFLAG_CY82C693) && |
| 709 | ide_dma_good_drive(drive)) |
| 710 | return 1; |
| 711 | else |
| 712 | return 0; |
| 713 | } |
Bartlomiej Zolnierkiewicz | 29e744d | 2007-05-10 00:01:09 +0200 | [diff] [blame] | 714 | |
Bartlomiej Zolnierkiewicz | 8704de8 | 2008-01-26 20:13:00 +0100 | [diff] [blame] | 715 | if (hwif->host_flags & IDE_HFLAG_NO_SET_MODE) |
Bartlomiej Zolnierkiewicz | ef4298d | 2008-03-07 21:53:49 +0100 | [diff] [blame] | 716 | return 1; |
Bartlomiej Zolnierkiewicz | 88b2b32 | 2007-10-13 17:47:51 +0200 | [diff] [blame] | 717 | |
| 718 | if (ide_set_dma_mode(drive, speed)) |
Bartlomiej Zolnierkiewicz | 4728d54 | 2007-05-16 00:51:46 +0200 | [diff] [blame] | 719 | return 0; |
Bartlomiej Zolnierkiewicz | 29e744d | 2007-05-10 00:01:09 +0200 | [diff] [blame] | 720 | |
Bartlomiej Zolnierkiewicz | 4728d54 | 2007-05-16 00:51:46 +0200 | [diff] [blame] | 721 | return 1; |
Bartlomiej Zolnierkiewicz | 29e744d | 2007-05-10 00:01:09 +0200 | [diff] [blame] | 722 | } |
| 723 | |
Bartlomiej Zolnierkiewicz | 0ae2e17 | 2007-10-16 22:29:55 +0200 | [diff] [blame] | 724 | static int ide_dma_check(ide_drive_t *drive) |
| 725 | { |
| 726 | ide_hwif_t *hwif = drive->hwif; |
| 727 | int vdma = (hwif->host_flags & IDE_HFLAG_VDMA)? 1 : 0; |
| 728 | |
| 729 | if (!vdma && ide_tune_dma(drive)) |
| 730 | return 0; |
| 731 | |
| 732 | /* TODO: always do PIO fallback */ |
| 733 | if (hwif->host_flags & IDE_HFLAG_TRUST_BIOS_FOR_DMA) |
| 734 | return -1; |
| 735 | |
| 736 | ide_set_max_pio(drive); |
| 737 | |
| 738 | return vdma ? 0 : -1; |
| 739 | } |
| 740 | |
Bartlomiej Zolnierkiewicz | 3ab7efe | 2007-12-12 23:31:58 +0100 | [diff] [blame] | 741 | int ide_id_dma_bug(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | { |
Bartlomiej Zolnierkiewicz | 3ab7efe | 2007-12-12 23:31:58 +0100 | [diff] [blame] | 743 | struct hd_driveid *id = drive->id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | |
| 745 | if (id->field_valid & 4) { |
| 746 | if ((id->dma_ultra >> 8) && (id->dma_mword >> 8)) |
Bartlomiej Zolnierkiewicz | 3ab7efe | 2007-12-12 23:31:58 +0100 | [diff] [blame] | 747 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | } else if (id->field_valid & 2) { |
| 749 | if ((id->dma_mword >> 8) && (id->dma_1word >> 8)) |
Bartlomiej Zolnierkiewicz | 3ab7efe | 2007-12-12 23:31:58 +0100 | [diff] [blame] | 750 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | } |
Bartlomiej Zolnierkiewicz | 3ab7efe | 2007-12-12 23:31:58 +0100 | [diff] [blame] | 752 | return 0; |
| 753 | err_out: |
| 754 | printk(KERN_ERR "%s: bad DMA info in identify block\n", drive->name); |
| 755 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | } |
| 757 | |
Bartlomiej Zolnierkiewicz | 3608b5d | 2007-02-17 02:40:26 +0100 | [diff] [blame] | 758 | int ide_set_dma(ide_drive_t *drive) |
| 759 | { |
Bartlomiej Zolnierkiewicz | 3608b5d | 2007-02-17 02:40:26 +0100 | [diff] [blame] | 760 | int rc; |
| 761 | |
Bartlomiej Zolnierkiewicz | 7b90599 | 2008-01-25 22:17:19 +0100 | [diff] [blame] | 762 | /* |
| 763 | * Force DMAing for the beginning of the check. |
| 764 | * Some chipsets appear to do interesting |
| 765 | * things, if not checked and cleared. |
| 766 | * PARANOIA!!! |
| 767 | */ |
Bartlomiej Zolnierkiewicz | 4a546e0 | 2008-01-26 20:13:01 +0100 | [diff] [blame] | 768 | ide_dma_off_quietly(drive); |
Bartlomiej Zolnierkiewicz | 7b90599 | 2008-01-25 22:17:19 +0100 | [diff] [blame] | 769 | |
Bartlomiej Zolnierkiewicz | 0ae2e17 | 2007-10-16 22:29:55 +0200 | [diff] [blame] | 770 | rc = ide_dma_check(drive); |
Bartlomiej Zolnierkiewicz | 7b90599 | 2008-01-25 22:17:19 +0100 | [diff] [blame] | 771 | if (rc) |
| 772 | return rc; |
Bartlomiej Zolnierkiewicz | 3608b5d | 2007-02-17 02:40:26 +0100 | [diff] [blame] | 773 | |
Bartlomiej Zolnierkiewicz | 4a546e0 | 2008-01-26 20:13:01 +0100 | [diff] [blame] | 774 | ide_dma_on(drive); |
| 775 | |
| 776 | return 0; |
Bartlomiej Zolnierkiewicz | 3608b5d | 2007-02-17 02:40:26 +0100 | [diff] [blame] | 777 | } |
| 778 | |
Bartlomiej Zolnierkiewicz | 578cfa0 | 2008-02-02 19:56:47 +0100 | [diff] [blame] | 779 | void ide_check_dma_crc(ide_drive_t *drive) |
| 780 | { |
| 781 | u8 mode; |
| 782 | |
| 783 | ide_dma_off_quietly(drive); |
| 784 | drive->crc_count = 0; |
| 785 | mode = drive->current_speed; |
| 786 | /* |
| 787 | * Don't try non Ultra-DMA modes without iCRC's. Force the |
| 788 | * device to PIO and make the user enable SWDMA/MWDMA modes. |
| 789 | */ |
| 790 | if (mode > XFER_UDMA_0 && mode <= XFER_UDMA_7) |
| 791 | mode--; |
| 792 | else |
| 793 | mode = XFER_PIO_4; |
| 794 | ide_set_xfer_rate(drive, mode); |
| 795 | if (drive->current_speed >= XFER_SW_DMA_0) |
| 796 | ide_dma_on(drive); |
| 797 | } |
| 798 | |
Sergei Shtylyov | 8e882ba | 2008-02-11 00:32:14 +0100 | [diff] [blame] | 799 | #ifdef CONFIG_BLK_DEV_IDEDMA_SFF |
Sergei Shtylyov | 841d2a9 | 2007-07-09 23:17:54 +0200 | [diff] [blame] | 800 | void ide_dma_lost_irq (ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | { |
| 802 | printk("%s: DMA interrupt recovery\n", drive->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | } |
| 804 | |
Sergei Shtylyov | 841d2a9 | 2007-07-09 23:17:54 +0200 | [diff] [blame] | 805 | EXPORT_SYMBOL(ide_dma_lost_irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | |
Sergei Shtylyov | c283f5d | 2007-07-09 23:17:54 +0200 | [diff] [blame] | 807 | void ide_dma_timeout (ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | { |
Sergei Shtylyov | c283f5d | 2007-07-09 23:17:54 +0200 | [diff] [blame] | 809 | ide_hwif_t *hwif = HWIF(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | |
Sergei Shtylyov | c283f5d | 2007-07-09 23:17:54 +0200 | [diff] [blame] | 811 | printk(KERN_ERR "%s: timeout waiting for DMA\n", drive->name); |
| 812 | |
| 813 | if (hwif->ide_dma_test_irq(drive)) |
| 814 | return; |
| 815 | |
| 816 | hwif->ide_dma_end(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | } |
| 818 | |
Sergei Shtylyov | c283f5d | 2007-07-09 23:17:54 +0200 | [diff] [blame] | 819 | EXPORT_SYMBOL(ide_dma_timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | |
Bartlomiej Zolnierkiewicz | a02bfd3 | 2007-10-20 00:32:37 +0200 | [diff] [blame] | 821 | static void ide_release_dma_engine(ide_hwif_t *hwif) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | { |
| 823 | if (hwif->dmatable_cpu) { |
Bartlomiej Zolnierkiewicz | 3650165 | 2008-02-01 23:09:31 +0100 | [diff] [blame] | 824 | struct pci_dev *pdev = to_pci_dev(hwif->dev); |
| 825 | |
| 826 | pci_free_consistent(pdev, PRD_ENTRIES * PRD_BYTES, |
| 827 | hwif->dmatable_cpu, hwif->dmatable_dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | hwif->dmatable_cpu = NULL; |
| 829 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | static int ide_release_iomio_dma(ide_hwif_t *hwif) |
| 833 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | release_region(hwif->dma_base, 8); |
Sergei Shtylylov | 020e322 | 2006-10-03 01:14:13 -0700 | [diff] [blame] | 835 | if (hwif->extra_ports) |
| 836 | release_region(hwif->extra_base, hwif->extra_ports); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | return 1; |
| 838 | } |
| 839 | |
| 840 | /* |
| 841 | * Needed for allowing full modular support of ide-driver |
| 842 | */ |
Sergei Shtylylov | dc844e0 | 2006-10-03 01:14:14 -0700 | [diff] [blame] | 843 | int ide_release_dma(ide_hwif_t *hwif) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | { |
Sergei Shtylylov | dc844e0 | 2006-10-03 01:14:14 -0700 | [diff] [blame] | 845 | ide_release_dma_engine(hwif); |
| 846 | |
Bartlomiej Zolnierkiewicz | 2ad1e55 | 2007-02-17 02:40:25 +0100 | [diff] [blame] | 847 | if (hwif->mmio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | return 1; |
Sergei Shtylylov | dc844e0 | 2006-10-03 01:14:14 -0700 | [diff] [blame] | 849 | else |
| 850 | return ide_release_iomio_dma(hwif); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | static int ide_allocate_dma_engine(ide_hwif_t *hwif) |
| 854 | { |
Bartlomiej Zolnierkiewicz | 3650165 | 2008-02-01 23:09:31 +0100 | [diff] [blame] | 855 | struct pci_dev *pdev = to_pci_dev(hwif->dev); |
| 856 | |
| 857 | hwif->dmatable_cpu = pci_alloc_consistent(pdev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | PRD_ENTRIES * PRD_BYTES, |
| 859 | &hwif->dmatable_dma); |
| 860 | |
| 861 | if (hwif->dmatable_cpu) |
| 862 | return 0; |
| 863 | |
Sergei Shtylylov | dc844e0 | 2006-10-03 01:14:14 -0700 | [diff] [blame] | 864 | printk(KERN_ERR "%s: -- Error, unable to allocate DMA table.\n", |
| 865 | hwif->cds->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | return 1; |
| 868 | } |
| 869 | |
Sergei Shtylyov | ecf32796 | 2008-02-01 23:09:30 +0100 | [diff] [blame] | 870 | static int ide_mapped_mmio_dma(ide_hwif_t *hwif, unsigned long base) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | { |
| 872 | printk(KERN_INFO " %s: MMIO-DMA ", hwif->name); |
| 873 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | return 0; |
| 875 | } |
| 876 | |
Sergei Shtylyov | ecf32796 | 2008-02-01 23:09:30 +0100 | [diff] [blame] | 877 | static int ide_iomio_dma(ide_hwif_t *hwif, unsigned long base) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | { |
| 879 | printk(KERN_INFO " %s: BM-DMA at 0x%04lx-0x%04lx", |
Sergei Shtylyov | ecf32796 | 2008-02-01 23:09:30 +0100 | [diff] [blame] | 880 | hwif->name, base, base + 7); |
Sergei Shtylylov | 020e322 | 2006-10-03 01:14:13 -0700 | [diff] [blame] | 881 | |
Sergei Shtylyov | ecf32796 | 2008-02-01 23:09:30 +0100 | [diff] [blame] | 882 | if (!request_region(base, 8, hwif->name)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | printk(" -- Error, ports in use.\n"); |
| 884 | return 1; |
| 885 | } |
Sergei Shtylylov | 020e322 | 2006-10-03 01:14:13 -0700 | [diff] [blame] | 886 | |
Sergei Shtylylov | 020e322 | 2006-10-03 01:14:13 -0700 | [diff] [blame] | 887 | if (hwif->cds->extra) { |
| 888 | hwif->extra_base = base + (hwif->channel ? 8 : 16); |
| 889 | |
| 890 | if (!hwif->mate || !hwif->mate->extra_ports) { |
| 891 | if (!request_region(hwif->extra_base, |
| 892 | hwif->cds->extra, hwif->cds->name)) { |
| 893 | printk(" -- Error, extra ports in use.\n"); |
Sergei Shtylyov | ecf32796 | 2008-02-01 23:09:30 +0100 | [diff] [blame] | 894 | release_region(base, 8); |
Sergei Shtylylov | 020e322 | 2006-10-03 01:14:13 -0700 | [diff] [blame] | 895 | return 1; |
| 896 | } |
| 897 | hwif->extra_ports = hwif->cds->extra; |
| 898 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | } |
Sergei Shtylylov | 020e322 | 2006-10-03 01:14:13 -0700 | [diff] [blame] | 900 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | return 0; |
| 902 | } |
| 903 | |
Sergei Shtylyov | ecf32796 | 2008-02-01 23:09:30 +0100 | [diff] [blame] | 904 | static int ide_dma_iobase(ide_hwif_t *hwif, unsigned long base) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | { |
Bartlomiej Zolnierkiewicz | 2ad1e55 | 2007-02-17 02:40:25 +0100 | [diff] [blame] | 906 | if (hwif->mmio) |
Sergei Shtylyov | ecf32796 | 2008-02-01 23:09:30 +0100 | [diff] [blame] | 907 | return ide_mapped_mmio_dma(hwif, base); |
Bartlomiej Zolnierkiewicz | 2ad1e55 | 2007-02-17 02:40:25 +0100 | [diff] [blame] | 908 | |
Sergei Shtylyov | ecf32796 | 2008-02-01 23:09:30 +0100 | [diff] [blame] | 909 | return ide_iomio_dma(hwif, base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | } |
| 911 | |
Sergei Shtylyov | ecf32796 | 2008-02-01 23:09:30 +0100 | [diff] [blame] | 912 | void ide_setup_dma(ide_hwif_t *hwif, unsigned long base) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | { |
Sergei Shtylyov | 4e5a68a | 2008-02-01 23:09:29 +0100 | [diff] [blame] | 914 | u8 dma_stat; |
| 915 | |
Sergei Shtylyov | ecf32796 | 2008-02-01 23:09:30 +0100 | [diff] [blame] | 916 | if (ide_dma_iobase(hwif, base)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | return; |
| 918 | |
| 919 | if (ide_allocate_dma_engine(hwif)) { |
| 920 | ide_release_dma(hwif); |
| 921 | return; |
| 922 | } |
| 923 | |
Bartlomiej Zolnierkiewicz | a02bfd3 | 2007-10-20 00:32:37 +0200 | [diff] [blame] | 924 | hwif->dma_base = base; |
| 925 | |
Sergei Shtylyov | ecf32796 | 2008-02-01 23:09:30 +0100 | [diff] [blame] | 926 | if (!hwif->dma_command) |
| 927 | hwif->dma_command = hwif->dma_base + 0; |
| 928 | if (!hwif->dma_vendor1) |
| 929 | hwif->dma_vendor1 = hwif->dma_base + 1; |
| 930 | if (!hwif->dma_status) |
| 931 | hwif->dma_status = hwif->dma_base + 2; |
| 932 | if (!hwif->dma_vendor3) |
| 933 | hwif->dma_vendor3 = hwif->dma_base + 3; |
| 934 | if (!hwif->dma_prdtable) |
| 935 | hwif->dma_prdtable = hwif->dma_base + 4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | |
Bartlomiej Zolnierkiewicz | 15ce926 | 2008-01-26 20:13:03 +0100 | [diff] [blame] | 937 | if (!hwif->dma_host_set) |
| 938 | hwif->dma_host_set = &ide_dma_host_set; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | if (!hwif->dma_setup) |
| 940 | hwif->dma_setup = &ide_dma_setup; |
| 941 | if (!hwif->dma_exec_cmd) |
| 942 | hwif->dma_exec_cmd = &ide_dma_exec_cmd; |
| 943 | if (!hwif->dma_start) |
| 944 | hwif->dma_start = &ide_dma_start; |
| 945 | if (!hwif->ide_dma_end) |
| 946 | hwif->ide_dma_end = &__ide_dma_end; |
| 947 | if (!hwif->ide_dma_test_irq) |
| 948 | hwif->ide_dma_test_irq = &__ide_dma_test_irq; |
Sergei Shtylyov | c283f5d | 2007-07-09 23:17:54 +0200 | [diff] [blame] | 949 | if (!hwif->dma_timeout) |
| 950 | hwif->dma_timeout = &ide_dma_timeout; |
Sergei Shtylyov | 841d2a9 | 2007-07-09 23:17:54 +0200 | [diff] [blame] | 951 | if (!hwif->dma_lost_irq) |
| 952 | hwif->dma_lost_irq = &ide_dma_lost_irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | |
Sergei Shtylyov | 4e5a68a | 2008-02-01 23:09:29 +0100 | [diff] [blame] | 954 | dma_stat = hwif->INB(hwif->dma_status); |
| 955 | printk(KERN_CONT ", BIOS settings: %s:%s, %s:%s\n", |
| 956 | hwif->drives[0].name, (dma_stat & 0x20) ? "DMA" : "PIO", |
| 957 | hwif->drives[1].name, (dma_stat & 0x40) ? "DMA" : "PIO"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | EXPORT_SYMBOL_GPL(ide_setup_dma); |
Sergei Shtylyov | 8e882ba | 2008-02-11 00:32:14 +0100 | [diff] [blame] | 961 | #endif /* CONFIG_BLK_DEV_IDEDMA_SFF */ |