ide: sanitize ide_build_sglist() and ide_destroy_dmatable()
[safe/jmp/linux-2.6] / drivers / ide / ide-dma.c
1 /*
2  *  IDE DMA support (including IDE PCI BM-DMA).
3  *
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
7  *
8  *  May be copied or modified under the terms of the GNU General Public License
9  *
10  *  DMA is supported for all IDE devices (disk drives, cdroms, tapes, floppies).
11  */
12
13 /*
14  *  Special Thanks to Mark for his Six years of work.
15  */
16
17 /*
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!
29  */
30
31 #include <linux/types.h>
32 #include <linux/kernel.h>
33 #include <linux/ide.h>
34 #include <linux/scatterlist.h>
35 #include <linux/dma-mapping.h>
36
37 static const struct drive_list_entry drive_whitelist[] = {
38         { "Micropolis 2112A"    ,       NULL            },
39         { "CONNER CTMA 4000"    ,       NULL            },
40         { "CONNER CTT8000-A"    ,       NULL            },
41         { "ST34342A"            ,       NULL            },
42         { NULL                  ,       NULL            }
43 };
44
45 static const struct drive_list_entry drive_blacklist[] = {
46         { "WDC AC11000H"        ,       NULL            },
47         { "WDC AC22100H"        ,       NULL            },
48         { "WDC AC32500H"        ,       NULL            },
49         { "WDC AC33100H"        ,       NULL            },
50         { "WDC AC31600H"        ,       NULL            },
51         { "WDC AC32100H"        ,       "24.09P07"      },
52         { "WDC AC23200L"        ,       "21.10N21"      },
53         { "Compaq CRD-8241B"    ,       NULL            },
54         { "CRD-8400B"           ,       NULL            },
55         { "CRD-8480B",                  NULL            },
56         { "CRD-8482B",                  NULL            },
57         { "CRD-84"              ,       NULL            },
58         { "SanDisk SDP3B"       ,       NULL            },
59         { "SanDisk SDP3B-64"    ,       NULL            },
60         { "SANYO CD-ROM CRD"    ,       NULL            },
61         { "HITACHI CDR-8"       ,       NULL            },
62         { "HITACHI CDR-8335"    ,       NULL            },
63         { "HITACHI CDR-8435"    ,       NULL            },
64         { "Toshiba CD-ROM XM-6202B"     ,       NULL            },
65         { "TOSHIBA CD-ROM XM-1702BC",   NULL            },
66         { "CD-532E-A"           ,       NULL            },
67         { "E-IDE CD-ROM CR-840",        NULL            },
68         { "CD-ROM Drive/F5A",   NULL            },
69         { "WPI CDD-820",                NULL            },
70         { "SAMSUNG CD-ROM SC-148C",     NULL            },
71         { "SAMSUNG CD-ROM SC",  NULL            },
72         { "ATAPI CD-ROM DRIVE 40X MAXIMUM",     NULL            },
73         { "_NEC DV5800A",               NULL            },
74         { "SAMSUNG CD-ROM SN-124",      "N001" },
75         { "Seagate STT20000A",          NULL  },
76         { "CD-ROM CDR_U200",            "1.09" },
77         { NULL                  ,       NULL            }
78
79 };
80
81 /**
82  *      ide_dma_intr    -       IDE DMA interrupt handler
83  *      @drive: the drive the interrupt is for
84  *
85  *      Handle an interrupt completing a read/write DMA transfer on an
86  *      IDE device
87  */
88
89 ide_startstop_t ide_dma_intr(ide_drive_t *drive)
90 {
91         ide_hwif_t *hwif = drive->hwif;
92         struct ide_cmd *cmd = &hwif->cmd;
93         u8 stat = 0, dma_stat = 0;
94
95         drive->waiting_for_dma = 0;
96         dma_stat = hwif->dma_ops->dma_end(drive);
97         ide_dma_unmap_sg(drive, cmd);
98         stat = hwif->tp_ops->read_status(hwif);
99
100         if (OK_STAT(stat, DRIVE_READY, drive->bad_wstat | ATA_DRQ)) {
101                 if (!dma_stat) {
102                         if ((cmd->tf_flags & IDE_TFLAG_FS) == 0)
103                                 ide_finish_cmd(drive, cmd, stat);
104                         else
105                                 ide_complete_rq(drive, 0,
106                                                 cmd->rq->nr_sectors << 9);
107                         return ide_stopped;
108                 }
109                 printk(KERN_ERR "%s: %s: bad DMA status (0x%02x)\n",
110                         drive->name, __func__, dma_stat);
111         }
112         return ide_error(drive, "dma_intr", stat);
113 }
114
115 int ide_dma_good_drive(ide_drive_t *drive)
116 {
117         return ide_in_drive_list(drive->id, drive_whitelist);
118 }
119
120 /**
121  *      ide_dma_map_sg  -       map IDE scatter gather for DMA I/O
122  *      @drive: the drive to map the DMA table for
123  *      @cmd: command
124  *
125  *      Perform the DMA mapping magic necessary to access the source or
126  *      target buffers of a request via DMA.  The lower layers of the
127  *      kernel provide the necessary cache management so that we can
128  *      operate in a portable fashion.
129  */
130
131 static int ide_dma_map_sg(ide_drive_t *drive, struct ide_cmd *cmd)
132 {
133         ide_hwif_t *hwif = drive->hwif;
134         struct scatterlist *sg = hwif->sg_table;
135         int i;
136
137         if (cmd->tf_flags & IDE_TFLAG_WRITE)
138                 cmd->sg_dma_direction = DMA_TO_DEVICE;
139         else
140                 cmd->sg_dma_direction = DMA_FROM_DEVICE;
141
142         i = dma_map_sg(hwif->dev, sg, cmd->sg_nents, cmd->sg_dma_direction);
143         if (i) {
144                 cmd->orig_sg_nents = cmd->sg_nents;
145                 cmd->sg_nents = i;
146         }
147
148         return i;
149 }
150
151 /**
152  *      ide_dma_unmap_sg        -       clean up DMA mapping
153  *      @drive: The drive to unmap
154  *
155  *      Teardown mappings after DMA has completed. This must be called
156  *      after the completion of each use of ide_build_dmatable and before
157  *      the next use of ide_build_dmatable. Failure to do so will cause
158  *      an oops as only one mapping can be live for each target at a given
159  *      time.
160  */
161
162 void ide_dma_unmap_sg(ide_drive_t *drive, struct ide_cmd *cmd)
163 {
164         ide_hwif_t *hwif = drive->hwif;
165
166         dma_unmap_sg(hwif->dev, hwif->sg_table, cmd->orig_sg_nents,
167                      cmd->sg_dma_direction);
168 }
169 EXPORT_SYMBOL_GPL(ide_dma_unmap_sg);
170
171 /**
172  *      ide_dma_off_quietly     -       Generic DMA kill
173  *      @drive: drive to control
174  *
175  *      Turn off the current DMA on this IDE controller.
176  */
177
178 void ide_dma_off_quietly(ide_drive_t *drive)
179 {
180         drive->dev_flags &= ~IDE_DFLAG_USING_DMA;
181         ide_toggle_bounce(drive, 0);
182
183         drive->hwif->dma_ops->dma_host_set(drive, 0);
184 }
185 EXPORT_SYMBOL(ide_dma_off_quietly);
186
187 /**
188  *      ide_dma_off     -       disable DMA on a device
189  *      @drive: drive to disable DMA on
190  *
191  *      Disable IDE DMA for a device on this IDE controller.
192  *      Inform the user that DMA has been disabled.
193  */
194
195 void ide_dma_off(ide_drive_t *drive)
196 {
197         printk(KERN_INFO "%s: DMA disabled\n", drive->name);
198         ide_dma_off_quietly(drive);
199 }
200 EXPORT_SYMBOL(ide_dma_off);
201
202 /**
203  *      ide_dma_on              -       Enable DMA on a device
204  *      @drive: drive to enable DMA on
205  *
206  *      Enable IDE DMA for a device on this IDE controller.
207  */
208
209 void ide_dma_on(ide_drive_t *drive)
210 {
211         drive->dev_flags |= IDE_DFLAG_USING_DMA;
212         ide_toggle_bounce(drive, 1);
213
214         drive->hwif->dma_ops->dma_host_set(drive, 1);
215 }
216
217 int __ide_dma_bad_drive(ide_drive_t *drive)
218 {
219         u16 *id = drive->id;
220
221         int blacklist = ide_in_drive_list(id, drive_blacklist);
222         if (blacklist) {
223                 printk(KERN_WARNING "%s: Disabling (U)DMA for %s (blacklisted)\n",
224                                     drive->name, (char *)&id[ATA_ID_PROD]);
225                 return blacklist;
226         }
227         return 0;
228 }
229 EXPORT_SYMBOL(__ide_dma_bad_drive);
230
231 static const u8 xfer_mode_bases[] = {
232         XFER_UDMA_0,
233         XFER_MW_DMA_0,
234         XFER_SW_DMA_0,
235 };
236
237 static unsigned int ide_get_mode_mask(ide_drive_t *drive, u8 base, u8 req_mode)
238 {
239         u16 *id = drive->id;
240         ide_hwif_t *hwif = drive->hwif;
241         const struct ide_port_ops *port_ops = hwif->port_ops;
242         unsigned int mask = 0;
243
244         switch (base) {
245         case XFER_UDMA_0:
246                 if ((id[ATA_ID_FIELD_VALID] & 4) == 0)
247                         break;
248
249                 if (port_ops && port_ops->udma_filter)
250                         mask = port_ops->udma_filter(drive);
251                 else
252                         mask = hwif->ultra_mask;
253                 mask &= id[ATA_ID_UDMA_MODES];
254
255                 /*
256                  * avoid false cable warning from eighty_ninty_three()
257                  */
258                 if (req_mode > XFER_UDMA_2) {
259                         if ((mask & 0x78) && (eighty_ninty_three(drive) == 0))
260                                 mask &= 0x07;
261                 }
262                 break;
263         case XFER_MW_DMA_0:
264                 if ((id[ATA_ID_FIELD_VALID] & 2) == 0)
265                         break;
266                 if (port_ops && port_ops->mdma_filter)
267                         mask = port_ops->mdma_filter(drive);
268                 else
269                         mask = hwif->mwdma_mask;
270                 mask &= id[ATA_ID_MWDMA_MODES];
271                 break;
272         case XFER_SW_DMA_0:
273                 if (id[ATA_ID_FIELD_VALID] & 2) {
274                         mask = id[ATA_ID_SWDMA_MODES] & hwif->swdma_mask;
275                 } else if (id[ATA_ID_OLD_DMA_MODES] >> 8) {
276                         u8 mode = id[ATA_ID_OLD_DMA_MODES] >> 8;
277
278                         /*
279                          * if the mode is valid convert it to the mask
280                          * (the maximum allowed mode is XFER_SW_DMA_2)
281                          */
282                         if (mode <= 2)
283                                 mask = ((2 << mode) - 1) & hwif->swdma_mask;
284                 }
285                 break;
286         default:
287                 BUG();
288                 break;
289         }
290
291         return mask;
292 }
293
294 /**
295  *      ide_find_dma_mode       -       compute DMA speed
296  *      @drive: IDE device
297  *      @req_mode: requested mode
298  *
299  *      Checks the drive/host capabilities and finds the speed to use for
300  *      the DMA transfer.  The speed is then limited by the requested mode.
301  *
302  *      Returns 0 if the drive/host combination is incapable of DMA transfers
303  *      or if the requested mode is not a DMA mode.
304  */
305
306 u8 ide_find_dma_mode(ide_drive_t *drive, u8 req_mode)
307 {
308         ide_hwif_t *hwif = drive->hwif;
309         unsigned int mask;
310         int x, i;
311         u8 mode = 0;
312
313         if (drive->media != ide_disk) {
314                 if (hwif->host_flags & IDE_HFLAG_NO_ATAPI_DMA)
315                         return 0;
316         }
317
318         for (i = 0; i < ARRAY_SIZE(xfer_mode_bases); i++) {
319                 if (req_mode < xfer_mode_bases[i])
320                         continue;
321                 mask = ide_get_mode_mask(drive, xfer_mode_bases[i], req_mode);
322                 x = fls(mask) - 1;
323                 if (x >= 0) {
324                         mode = xfer_mode_bases[i] + x;
325                         break;
326                 }
327         }
328
329         if (hwif->chipset == ide_acorn && mode == 0) {
330                 /*
331                  * is this correct?
332                  */
333                 if (ide_dma_good_drive(drive) &&
334                     drive->id[ATA_ID_EIDE_DMA_TIME] < 150)
335                         mode = XFER_MW_DMA_1;
336         }
337
338         mode = min(mode, req_mode);
339
340         printk(KERN_INFO "%s: %s mode selected\n", drive->name,
341                           mode ? ide_xfer_verbose(mode) : "no DMA");
342
343         return mode;
344 }
345 EXPORT_SYMBOL_GPL(ide_find_dma_mode);
346
347 static int ide_tune_dma(ide_drive_t *drive)
348 {
349         ide_hwif_t *hwif = drive->hwif;
350         u8 speed;
351
352         if (ata_id_has_dma(drive->id) == 0 ||
353             (drive->dev_flags & IDE_DFLAG_NODMA))
354                 return 0;
355
356         /* consult the list of known "bad" drives */
357         if (__ide_dma_bad_drive(drive))
358                 return 0;
359
360         if (ide_id_dma_bug(drive))
361                 return 0;
362
363         if (hwif->host_flags & IDE_HFLAG_TRUST_BIOS_FOR_DMA)
364                 return config_drive_for_dma(drive);
365
366         speed = ide_max_dma_mode(drive);
367
368         if (!speed)
369                 return 0;
370
371         if (ide_set_dma_mode(drive, speed))
372                 return 0;
373
374         return 1;
375 }
376
377 static int ide_dma_check(ide_drive_t *drive)
378 {
379         ide_hwif_t *hwif = drive->hwif;
380
381         if (ide_tune_dma(drive))
382                 return 0;
383
384         /* TODO: always do PIO fallback */
385         if (hwif->host_flags & IDE_HFLAG_TRUST_BIOS_FOR_DMA)
386                 return -1;
387
388         ide_set_max_pio(drive);
389
390         return -1;
391 }
392
393 int ide_id_dma_bug(ide_drive_t *drive)
394 {
395         u16 *id = drive->id;
396
397         if (id[ATA_ID_FIELD_VALID] & 4) {
398                 if ((id[ATA_ID_UDMA_MODES] >> 8) &&
399                     (id[ATA_ID_MWDMA_MODES] >> 8))
400                         goto err_out;
401         } else if (id[ATA_ID_FIELD_VALID] & 2) {
402                 if ((id[ATA_ID_MWDMA_MODES] >> 8) &&
403                     (id[ATA_ID_SWDMA_MODES] >> 8))
404                         goto err_out;
405         }
406         return 0;
407 err_out:
408         printk(KERN_ERR "%s: bad DMA info in identify block\n", drive->name);
409         return 1;
410 }
411
412 int ide_set_dma(ide_drive_t *drive)
413 {
414         int rc;
415
416         /*
417          * Force DMAing for the beginning of the check.
418          * Some chipsets appear to do interesting
419          * things, if not checked and cleared.
420          *   PARANOIA!!!
421          */
422         ide_dma_off_quietly(drive);
423
424         rc = ide_dma_check(drive);
425         if (rc)
426                 return rc;
427
428         ide_dma_on(drive);
429
430         return 0;
431 }
432
433 void ide_check_dma_crc(ide_drive_t *drive)
434 {
435         u8 mode;
436
437         ide_dma_off_quietly(drive);
438         drive->crc_count = 0;
439         mode = drive->current_speed;
440         /*
441          * Don't try non Ultra-DMA modes without iCRC's.  Force the
442          * device to PIO and make the user enable SWDMA/MWDMA modes.
443          */
444         if (mode > XFER_UDMA_0 && mode <= XFER_UDMA_7)
445                 mode--;
446         else
447                 mode = XFER_PIO_4;
448         ide_set_xfer_rate(drive, mode);
449         if (drive->current_speed >= XFER_SW_DMA_0)
450                 ide_dma_on(drive);
451 }
452
453 void ide_dma_lost_irq(ide_drive_t *drive)
454 {
455         printk(KERN_ERR "%s: DMA interrupt recovery\n", drive->name);
456 }
457 EXPORT_SYMBOL_GPL(ide_dma_lost_irq);
458
459 /*
460  * un-busy the port etc, and clear any pending DMA status. we want to
461  * retry the current request in pio mode instead of risking tossing it
462  * all away
463  */
464 ide_startstop_t ide_dma_timeout_retry(ide_drive_t *drive, int error)
465 {
466         ide_hwif_t *hwif = drive->hwif;
467         const struct ide_dma_ops *dma_ops = hwif->dma_ops;
468         struct ide_cmd *cmd = &hwif->cmd;
469         struct request *rq;
470         ide_startstop_t ret = ide_stopped;
471
472         /*
473          * end current dma transaction
474          */
475
476         if (error < 0) {
477                 printk(KERN_WARNING "%s: DMA timeout error\n", drive->name);
478                 drive->waiting_for_dma = 0;
479                 (void)dma_ops->dma_end(drive);
480                 ide_dma_unmap_sg(drive, cmd);
481                 ret = ide_error(drive, "dma timeout error",
482                                 hwif->tp_ops->read_status(hwif));
483         } else {
484                 printk(KERN_WARNING "%s: DMA timeout retry\n", drive->name);
485                 if (dma_ops->dma_clear)
486                         dma_ops->dma_clear(drive);
487                 printk(KERN_ERR "%s: timeout waiting for DMA\n", drive->name);
488                 if (dma_ops->dma_test_irq(drive) == 0) {
489                         ide_dump_status(drive, "DMA timeout",
490                                         hwif->tp_ops->read_status(hwif));
491                         drive->waiting_for_dma = 0;
492                         (void)dma_ops->dma_end(drive);
493                         ide_dma_unmap_sg(drive, cmd);
494                 }
495         }
496
497         /*
498          * disable dma for now, but remember that we did so because of
499          * a timeout -- we'll reenable after we finish this next request
500          * (or rather the first chunk of it) in pio.
501          */
502         drive->dev_flags |= IDE_DFLAG_DMA_PIO_RETRY;
503         drive->retry_pio++;
504         ide_dma_off_quietly(drive);
505
506         /*
507          * un-busy drive etc and make sure request is sane
508          */
509
510         rq = hwif->rq;
511         if (!rq)
512                 goto out;
513
514         hwif->rq = NULL;
515
516         rq->errors = 0;
517
518         if (!rq->bio)
519                 goto out;
520
521         rq->sector = rq->bio->bi_sector;
522         rq->current_nr_sectors = bio_iovec(rq->bio)->bv_len >> 9;
523         rq->hard_cur_sectors = rq->current_nr_sectors;
524         rq->buffer = bio_data(rq->bio);
525 out:
526         return ret;
527 }
528
529 void ide_release_dma_engine(ide_hwif_t *hwif)
530 {
531         if (hwif->dmatable_cpu) {
532                 int prd_size = hwif->prd_max_nents * hwif->prd_ent_size;
533
534                 dma_free_coherent(hwif->dev, prd_size,
535                                   hwif->dmatable_cpu, hwif->dmatable_dma);
536                 hwif->dmatable_cpu = NULL;
537         }
538 }
539 EXPORT_SYMBOL_GPL(ide_release_dma_engine);
540
541 int ide_allocate_dma_engine(ide_hwif_t *hwif)
542 {
543         int prd_size;
544
545         if (hwif->prd_max_nents == 0)
546                 hwif->prd_max_nents = PRD_ENTRIES;
547         if (hwif->prd_ent_size == 0)
548                 hwif->prd_ent_size = PRD_BYTES;
549
550         prd_size = hwif->prd_max_nents * hwif->prd_ent_size;
551
552         hwif->dmatable_cpu = dma_alloc_coherent(hwif->dev, prd_size,
553                                                 &hwif->dmatable_dma,
554                                                 GFP_ATOMIC);
555         if (hwif->dmatable_cpu == NULL) {
556                 printk(KERN_ERR "%s: unable to allocate PRD table\n",
557                         hwif->name);
558                 return -ENOMEM;
559         }
560
561         return 0;
562 }
563 EXPORT_SYMBOL_GPL(ide_allocate_dma_engine);
564
565 int ide_dma_prepare(ide_drive_t *drive, struct ide_cmd *cmd)
566 {
567         const struct ide_dma_ops *dma_ops = drive->hwif->dma_ops;
568
569         if ((drive->dev_flags & IDE_DFLAG_USING_DMA) == 0 ||
570             (dma_ops->dma_check && dma_ops->dma_check(drive, cmd)))
571                 goto out;
572         ide_map_sg(drive, cmd);
573         if (ide_dma_map_sg(drive, cmd) == 0)
574                 goto out_map;
575         if (dma_ops->dma_setup(drive, cmd))
576                 goto out_dma_unmap;
577         drive->waiting_for_dma = 1;
578         return 0;
579 out_dma_unmap:
580         ide_dma_unmap_sg(drive, cmd);
581 out_map:
582         ide_map_sg(drive, cmd);
583 out:
584         return 1;
585 }