Merge commit 'alsa/master' into for-linus
[pandora-kernel.git] / drivers / ide / ide-iops.c
1 /*
2  *  Copyright (C) 2000-2002     Andre Hedrick <andre@linux-ide.org>
3  *  Copyright (C) 2003          Red Hat
4  *
5  */
6
7 #include <linux/module.h>
8 #include <linux/types.h>
9 #include <linux/string.h>
10 #include <linux/kernel.h>
11 #include <linux/timer.h>
12 #include <linux/mm.h>
13 #include <linux/interrupt.h>
14 #include <linux/major.h>
15 #include <linux/errno.h>
16 #include <linux/genhd.h>
17 #include <linux/blkpg.h>
18 #include <linux/slab.h>
19 #include <linux/pci.h>
20 #include <linux/delay.h>
21 #include <linux/ide.h>
22 #include <linux/bitops.h>
23 #include <linux/nmi.h>
24
25 #include <asm/byteorder.h>
26 #include <asm/irq.h>
27 #include <asm/uaccess.h>
28 #include <asm/io.h>
29
30 void SELECT_MASK(ide_drive_t *drive, int mask)
31 {
32         const struct ide_port_ops *port_ops = drive->hwif->port_ops;
33
34         if (port_ops && port_ops->maskproc)
35                 port_ops->maskproc(drive, mask);
36 }
37
38 u8 ide_read_error(ide_drive_t *drive)
39 {
40         struct ide_taskfile tf;
41
42         drive->hwif->tp_ops->tf_read(drive, &tf, IDE_VALID_ERROR);
43
44         return tf.error;
45 }
46 EXPORT_SYMBOL_GPL(ide_read_error);
47
48 void ide_fix_driveid(u16 *id)
49 {
50 #ifndef __LITTLE_ENDIAN
51 # ifdef __BIG_ENDIAN
52         int i;
53
54         for (i = 0; i < 256; i++)
55                 id[i] = __le16_to_cpu(id[i]);
56 # else
57 #  error "Please fix <asm/byteorder.h>"
58 # endif
59 #endif
60 }
61
62 /*
63  * ide_fixstring() cleans up and (optionally) byte-swaps a text string,
64  * removing leading/trailing blanks and compressing internal blanks.
65  * It is primarily used to tidy up the model name/number fields as
66  * returned by the ATA_CMD_ID_ATA[PI] commands.
67  */
68
69 void ide_fixstring(u8 *s, const int bytecount, const int byteswap)
70 {
71         u8 *p, *end = &s[bytecount & ~1]; /* bytecount must be even */
72
73         if (byteswap) {
74                 /* convert from big-endian to host byte order */
75                 for (p = s ; p != end ; p += 2)
76                         be16_to_cpus((u16 *) p);
77         }
78
79         /* strip leading blanks */
80         p = s;
81         while (s != end && *s == ' ')
82                 ++s;
83         /* compress internal blanks and strip trailing blanks */
84         while (s != end && *s) {
85                 if (*s++ != ' ' || (s != end && *s && *s != ' '))
86                         *p++ = *(s-1);
87         }
88         /* wipe out trailing garbage */
89         while (p != end)
90                 *p++ = '\0';
91 }
92 EXPORT_SYMBOL(ide_fixstring);
93
94 /*
95  * This routine busy-waits for the drive status to be not "busy".
96  * It then checks the status for all of the "good" bits and none
97  * of the "bad" bits, and if all is okay it returns 0.  All other
98  * cases return error -- caller may then invoke ide_error().
99  *
100  * This routine should get fixed to not hog the cpu during extra long waits..
101  * That could be done by busy-waiting for the first jiffy or two, and then
102  * setting a timer to wake up at half second intervals thereafter,
103  * until timeout is achieved, before timing out.
104  */
105 static int __ide_wait_stat(ide_drive_t *drive, u8 good, u8 bad,
106                            unsigned long timeout, u8 *rstat)
107 {
108         ide_hwif_t *hwif = drive->hwif;
109         const struct ide_tp_ops *tp_ops = hwif->tp_ops;
110         unsigned long flags;
111         int i;
112         u8 stat;
113
114         udelay(1);      /* spec allows drive 400ns to assert "BUSY" */
115         stat = tp_ops->read_status(hwif);
116
117         if (stat & ATA_BUSY) {
118                 local_save_flags(flags);
119                 local_irq_enable_in_hardirq();
120                 timeout += jiffies;
121                 while ((stat = tp_ops->read_status(hwif)) & ATA_BUSY) {
122                         if (time_after(jiffies, timeout)) {
123                                 /*
124                                  * One last read after the timeout in case
125                                  * heavy interrupt load made us not make any
126                                  * progress during the timeout..
127                                  */
128                                 stat = tp_ops->read_status(hwif);
129                                 if ((stat & ATA_BUSY) == 0)
130                                         break;
131
132                                 local_irq_restore(flags);
133                                 *rstat = stat;
134                                 return -EBUSY;
135                         }
136                 }
137                 local_irq_restore(flags);
138         }
139         /*
140          * Allow status to settle, then read it again.
141          * A few rare drives vastly violate the 400ns spec here,
142          * so we'll wait up to 10usec for a "good" status
143          * rather than expensively fail things immediately.
144          * This fix courtesy of Matthew Faupel & Niccolo Rigacci.
145          */
146         for (i = 0; i < 10; i++) {
147                 udelay(1);
148                 stat = tp_ops->read_status(hwif);
149
150                 if (OK_STAT(stat, good, bad)) {
151                         *rstat = stat;
152                         return 0;
153                 }
154         }
155         *rstat = stat;
156         return -EFAULT;
157 }
158
159 /*
160  * In case of error returns error value after doing "*startstop = ide_error()".
161  * The caller should return the updated value of "startstop" in this case,
162  * "startstop" is unchanged when the function returns 0.
163  */
164 int ide_wait_stat(ide_startstop_t *startstop, ide_drive_t *drive, u8 good,
165                   u8 bad, unsigned long timeout)
166 {
167         int err;
168         u8 stat;
169
170         /* bail early if we've exceeded max_failures */
171         if (drive->max_failures && (drive->failures > drive->max_failures)) {
172                 *startstop = ide_stopped;
173                 return 1;
174         }
175
176         err = __ide_wait_stat(drive, good, bad, timeout, &stat);
177
178         if (err) {
179                 char *s = (err == -EBUSY) ? "status timeout" : "status error";
180                 *startstop = ide_error(drive, s, stat);
181         }
182
183         return err;
184 }
185 EXPORT_SYMBOL(ide_wait_stat);
186
187 /**
188  *      ide_in_drive_list       -       look for drive in black/white list
189  *      @id: drive identifier
190  *      @table: list to inspect
191  *
192  *      Look for a drive in the blacklist and the whitelist tables
193  *      Returns 1 if the drive is found in the table.
194  */
195
196 int ide_in_drive_list(u16 *id, const struct drive_list_entry *table)
197 {
198         for ( ; table->id_model; table++)
199                 if ((!strcmp(table->id_model, (char *)&id[ATA_ID_PROD])) &&
200                     (!table->id_firmware ||
201                      strstr((char *)&id[ATA_ID_FW_REV], table->id_firmware)))
202                         return 1;
203         return 0;
204 }
205 EXPORT_SYMBOL_GPL(ide_in_drive_list);
206
207 /*
208  * Early UDMA66 devices don't set bit14 to 1, only bit13 is valid.
209  * We list them here and depend on the device side cable detection for them.
210  *
211  * Some optical devices with the buggy firmwares have the same problem.
212  */
213 static const struct drive_list_entry ivb_list[] = {
214         { "QUANTUM FIREBALLlct10 05"    , "A03.0900"    },
215         { "TSSTcorp CDDVDW SH-S202J"    , "SB00"        },
216         { "TSSTcorp CDDVDW SH-S202J"    , "SB01"        },
217         { "TSSTcorp CDDVDW SH-S202N"    , "SB00"        },
218         { "TSSTcorp CDDVDW SH-S202N"    , "SB01"        },
219         { "TSSTcorp CDDVDW SH-S202H"    , "SB00"        },
220         { "TSSTcorp CDDVDW SH-S202H"    , "SB01"        },
221         { "SAMSUNG SP0822N"             , "WA100-10"    },
222         { NULL                          , NULL          }
223 };
224
225 /*
226  *  All hosts that use the 80c ribbon must use!
227  *  The name is derived from upper byte of word 93 and the 80c ribbon.
228  */
229 u8 eighty_ninty_three(ide_drive_t *drive)
230 {
231         ide_hwif_t *hwif = drive->hwif;
232         u16 *id = drive->id;
233         int ivb = ide_in_drive_list(id, ivb_list);
234
235         if (hwif->cbl == ATA_CBL_PATA40_SHORT)
236                 return 1;
237
238         if (ivb)
239                 printk(KERN_DEBUG "%s: skipping word 93 validity check\n",
240                                   drive->name);
241
242         if (ata_id_is_sata(id) && !ivb)
243                 return 1;
244
245         if (hwif->cbl != ATA_CBL_PATA80 && !ivb)
246                 goto no_80w;
247
248         /*
249          * FIXME:
250          * - change master/slave IDENTIFY order
251          * - force bit13 (80c cable present) check also for !ivb devices
252          *   (unless the slave device is pre-ATA3)
253          */
254         if ((id[ATA_ID_HW_CONFIG] & 0x4000) ||
255             (ivb && (id[ATA_ID_HW_CONFIG] & 0x2000)))
256                 return 1;
257
258 no_80w:
259         if (drive->dev_flags & IDE_DFLAG_UDMA33_WARNED)
260                 return 0;
261
262         printk(KERN_WARNING "%s: %s side 80-wire cable detection failed, "
263                             "limiting max speed to UDMA33\n",
264                             drive->name,
265                             hwif->cbl == ATA_CBL_PATA80 ? "drive" : "host");
266
267         drive->dev_flags |= IDE_DFLAG_UDMA33_WARNED;
268
269         return 0;
270 }
271
272 int ide_driveid_update(ide_drive_t *drive)
273 {
274         u16 *id;
275         int rc;
276
277         id = kmalloc(SECTOR_SIZE, GFP_ATOMIC);
278         if (id == NULL)
279                 return 0;
280
281         SELECT_MASK(drive, 1);
282         rc = ide_dev_read_id(drive, ATA_CMD_ID_ATA, id);
283         SELECT_MASK(drive, 0);
284
285         if (rc)
286                 goto out_err;
287
288         drive->id[ATA_ID_UDMA_MODES]  = id[ATA_ID_UDMA_MODES];
289         drive->id[ATA_ID_MWDMA_MODES] = id[ATA_ID_MWDMA_MODES];
290         drive->id[ATA_ID_SWDMA_MODES] = id[ATA_ID_SWDMA_MODES];
291         drive->id[ATA_ID_CFA_MODES]   = id[ATA_ID_CFA_MODES];
292         /* anything more ? */
293
294         kfree(id);
295
296         if ((drive->dev_flags & IDE_DFLAG_USING_DMA) && ide_id_dma_bug(drive))
297                 ide_dma_off(drive);
298
299         return 1;
300 out_err:
301         SELECT_MASK(drive, 0);
302         if (rc == 2)
303                 printk(KERN_ERR "%s: %s: bad status\n", drive->name, __func__);
304         kfree(id);
305         return 0;
306 }
307
308 int ide_config_drive_speed(ide_drive_t *drive, u8 speed)
309 {
310         ide_hwif_t *hwif = drive->hwif;
311         const struct ide_tp_ops *tp_ops = hwif->tp_ops;
312         struct ide_taskfile tf;
313         u16 *id = drive->id, i;
314         int error = 0;
315         u8 stat;
316
317 #ifdef CONFIG_BLK_DEV_IDEDMA
318         if (hwif->dma_ops)      /* check if host supports DMA */
319                 hwif->dma_ops->dma_host_set(drive, 0);
320 #endif
321
322         /* Skip setting PIO flow-control modes on pre-EIDE drives */
323         if ((speed & 0xf8) == XFER_PIO_0 && ata_id_has_iordy(drive->id) == 0)
324                 goto skip;
325
326         /*
327          * Don't use ide_wait_cmd here - it will
328          * attempt to set_geometry and recalibrate,
329          * but for some reason these don't work at
330          * this point (lost interrupt).
331          */
332
333         /*
334          *      FIXME: we race against the running IRQ here if
335          *      this is called from non IRQ context. If we use
336          *      disable_irq() we hang on the error path. Work
337          *      is needed.
338          */
339         disable_irq_nosync(hwif->irq);
340
341         udelay(1);
342         tp_ops->dev_select(drive);
343         SELECT_MASK(drive, 1);
344         udelay(1);
345         tp_ops->write_devctl(hwif, ATA_NIEN | ATA_DEVCTL_OBS);
346
347         memset(&tf, 0, sizeof(tf));
348         tf.feature = SETFEATURES_XFER;
349         tf.nsect   = speed;
350
351         tp_ops->tf_load(drive, &tf, IDE_VALID_FEATURE | IDE_VALID_NSECT);
352
353         tp_ops->exec_command(hwif, ATA_CMD_SET_FEATURES);
354
355         if (drive->quirk_list == 2)
356                 tp_ops->write_devctl(hwif, ATA_DEVCTL_OBS);
357
358         error = __ide_wait_stat(drive, drive->ready_stat,
359                                 ATA_BUSY | ATA_DRQ | ATA_ERR,
360                                 WAIT_CMD, &stat);
361
362         SELECT_MASK(drive, 0);
363
364         enable_irq(hwif->irq);
365
366         if (error) {
367                 (void) ide_dump_status(drive, "set_drive_speed_status", stat);
368                 return error;
369         }
370
371         if (speed >= XFER_SW_DMA_0) {
372                 id[ATA_ID_UDMA_MODES]  &= ~0xFF00;
373                 id[ATA_ID_MWDMA_MODES] &= ~0x0700;
374                 id[ATA_ID_SWDMA_MODES] &= ~0x0700;
375                 if (ata_id_is_cfa(id))
376                         id[ATA_ID_CFA_MODES] &= ~0x0E00;
377         } else  if (ata_id_is_cfa(id))
378                 id[ATA_ID_CFA_MODES] &= ~0x01C0;
379
380  skip:
381 #ifdef CONFIG_BLK_DEV_IDEDMA
382         if (speed >= XFER_SW_DMA_0 && (drive->dev_flags & IDE_DFLAG_USING_DMA))
383                 hwif->dma_ops->dma_host_set(drive, 1);
384         else if (hwif->dma_ops) /* check if host supports DMA */
385                 ide_dma_off_quietly(drive);
386 #endif
387
388         if (speed >= XFER_UDMA_0) {
389                 i = 1 << (speed - XFER_UDMA_0);
390                 id[ATA_ID_UDMA_MODES] |= (i << 8 | i);
391         } else if (ata_id_is_cfa(id) && speed >= XFER_MW_DMA_3) {
392                 i = speed - XFER_MW_DMA_2;
393                 id[ATA_ID_CFA_MODES] |= i << 9;
394         } else if (speed >= XFER_MW_DMA_0) {
395                 i = 1 << (speed - XFER_MW_DMA_0);
396                 id[ATA_ID_MWDMA_MODES] |= (i << 8 | i);
397         } else if (speed >= XFER_SW_DMA_0) {
398                 i = 1 << (speed - XFER_SW_DMA_0);
399                 id[ATA_ID_SWDMA_MODES] |= (i << 8 | i);
400         } else if (ata_id_is_cfa(id) && speed >= XFER_PIO_5) {
401                 i = speed - XFER_PIO_4;
402                 id[ATA_ID_CFA_MODES] |= i << 6;
403         }
404
405         if (!drive->init_speed)
406                 drive->init_speed = speed;
407         drive->current_speed = speed;
408         return error;
409 }
410
411 /*
412  * This should get invoked any time we exit the driver to
413  * wait for an interrupt response from a drive.  handler() points
414  * at the appropriate code to handle the next interrupt, and a
415  * timer is started to prevent us from waiting forever in case
416  * something goes wrong (see the ide_timer_expiry() handler later on).
417  *
418  * See also ide_execute_command
419  */
420 void __ide_set_handler(ide_drive_t *drive, ide_handler_t *handler,
421                        unsigned int timeout)
422 {
423         ide_hwif_t *hwif = drive->hwif;
424
425         BUG_ON(hwif->handler);
426         hwif->handler           = handler;
427         hwif->timer.expires     = jiffies + timeout;
428         hwif->req_gen_timer     = hwif->req_gen;
429         add_timer(&hwif->timer);
430 }
431
432 void ide_set_handler(ide_drive_t *drive, ide_handler_t *handler,
433                      unsigned int timeout)
434 {
435         ide_hwif_t *hwif = drive->hwif;
436         unsigned long flags;
437
438         spin_lock_irqsave(&hwif->lock, flags);
439         __ide_set_handler(drive, handler, timeout);
440         spin_unlock_irqrestore(&hwif->lock, flags);
441 }
442 EXPORT_SYMBOL(ide_set_handler);
443
444 /**
445  *      ide_execute_command     -       execute an IDE command
446  *      @drive: IDE drive to issue the command against
447  *      @cmd: command
448  *      @handler: handler for next phase
449  *      @timeout: timeout for command
450  *
451  *      Helper function to issue an IDE command. This handles the
452  *      atomicity requirements, command timing and ensures that the
453  *      handler and IRQ setup do not race. All IDE command kick off
454  *      should go via this function or do equivalent locking.
455  */
456
457 void ide_execute_command(ide_drive_t *drive, struct ide_cmd *cmd,
458                          ide_handler_t *handler, unsigned timeout)
459 {
460         ide_hwif_t *hwif = drive->hwif;
461         unsigned long flags;
462
463         spin_lock_irqsave(&hwif->lock, flags);
464         if ((cmd->protocol != ATAPI_PROT_DMA &&
465              cmd->protocol != ATAPI_PROT_PIO) ||
466             (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT))
467                 __ide_set_handler(drive, handler, timeout);
468         hwif->tp_ops->exec_command(hwif, cmd->tf.command);
469         /*
470          * Drive takes 400nS to respond, we must avoid the IRQ being
471          * serviced before that.
472          *
473          * FIXME: we could skip this delay with care on non shared devices
474          */
475         ndelay(400);
476         spin_unlock_irqrestore(&hwif->lock, flags);
477 }
478
479 /*
480  * ide_wait_not_busy() waits for the currently selected device on the hwif
481  * to report a non-busy status, see comments in ide_probe_port().
482  */
483 int ide_wait_not_busy(ide_hwif_t *hwif, unsigned long timeout)
484 {
485         u8 stat = 0;
486
487         while (timeout--) {
488                 /*
489                  * Turn this into a schedule() sleep once I'm sure
490                  * about locking issues (2.5 work ?).
491                  */
492                 mdelay(1);
493                 stat = hwif->tp_ops->read_status(hwif);
494                 if ((stat & ATA_BUSY) == 0)
495                         return 0;
496                 /*
497                  * Assume a value of 0xff means nothing is connected to
498                  * the interface and it doesn't implement the pull-down
499                  * resistor on D7.
500                  */
501                 if (stat == 0xff)
502                         return -ENODEV;
503                 touch_softlockup_watchdog();
504                 touch_nmi_watchdog();
505         }
506         return -EBUSY;
507 }