1 #include <linux/types.h>
2 #include <linux/string.h>
3 #include <linux/kernel.h>
4 #include <linux/interrupt.h>
6 #include <linux/bitops.h>
8 static const char *udma_str[] =
9 { "UDMA/16", "UDMA/25", "UDMA/33", "UDMA/44",
10 "UDMA/66", "UDMA/100", "UDMA/133", "UDMA7" };
11 static const char *mwdma_str[] =
12 { "MWDMA0", "MWDMA1", "MWDMA2" };
13 static const char *swdma_str[] =
14 { "SWDMA0", "SWDMA1", "SWDMA2" };
15 static const char *pio_str[] =
16 { "PIO0", "PIO1", "PIO2", "PIO3", "PIO4", "PIO5" };
19 * ide_xfer_verbose - return IDE mode names
20 * @mode: transfer mode
22 * Returns a constant string giving the name of the mode
26 const char *ide_xfer_verbose(u8 mode)
31 if (mode >= XFER_UDMA_0 && mode <= XFER_UDMA_7)
33 else if (mode >= XFER_MW_DMA_0 && mode <= XFER_MW_DMA_2)
35 else if (mode >= XFER_SW_DMA_0 && mode <= XFER_SW_DMA_2)
37 else if (mode >= XFER_PIO_0 && mode <= XFER_PIO_5)
39 else if (mode == XFER_PIO_SLOW)
46 EXPORT_SYMBOL(ide_xfer_verbose);
49 * ide_rate_filter - filter transfer mode
51 * @speed: desired speed
53 * Given the available transfer modes this function returns
54 * the best available speed at or below the speed requested.
56 * TODO: check device PIO capabilities
59 static u8 ide_rate_filter(ide_drive_t *drive, u8 speed)
61 ide_hwif_t *hwif = drive->hwif;
62 u8 mode = ide_find_dma_mode(drive, speed);
66 mode = fls(hwif->pio_mask) - 1 + XFER_PIO_0;
71 /* printk("%s: mode 0x%02x, speed 0x%02x\n", __func__, mode, speed); */
73 return min(speed, mode);
77 * ide_get_best_pio_mode - get PIO mode from drive
78 * @drive: drive to consider
79 * @mode_wanted: preferred mode
80 * @max_mode: highest allowed mode
82 * This routine returns the recommended PIO settings for a given drive,
83 * based on the drive->id information and the ide_pio_blacklist[].
85 * Drive PIO mode is auto-selected if 255 is passed as mode_wanted.
86 * This is used by most chipset support modules when "auto-tuning".
89 u8 ide_get_best_pio_mode(ide_drive_t *drive, u8 mode_wanted, u8 max_mode)
92 int pio_mode = -1, overridden = 0;
94 if (mode_wanted != 255)
95 return min_t(u8, mode_wanted, max_mode);
97 if ((drive->hwif->host_flags & IDE_HFLAG_PIO_NO_BLACKLIST) == 0)
98 pio_mode = ide_scan_pio_blacklist((char *)&id[ATA_ID_PROD]);
100 if (pio_mode != -1) {
101 printk(KERN_INFO "%s: is on PIO blacklist\n", drive->name);
103 pio_mode = id[ATA_ID_OLD_PIO_MODES] >> 8;
104 if (pio_mode > 2) { /* 2 is maximum allowed tPIO value */
109 if (id[ATA_ID_FIELD_VALID] & 2) { /* ATA2? */
110 if (ata_id_has_iordy(id)) {
111 if (id[ATA_ID_PIO_MODES] & 7) {
113 if (id[ATA_ID_PIO_MODES] & 4)
115 else if (id[ATA_ID_PIO_MODES] & 2)
124 printk(KERN_INFO "%s: tPIO > 2, assuming tPIO = 2\n",
128 if (pio_mode > max_mode)
133 EXPORT_SYMBOL_GPL(ide_get_best_pio_mode);
135 /* req_pio == "255" for auto-tune */
136 void ide_set_pio(ide_drive_t *drive, u8 req_pio)
138 ide_hwif_t *hwif = drive->hwif;
139 const struct ide_port_ops *port_ops = hwif->port_ops;
142 if (port_ops == NULL || port_ops->set_pio_mode == NULL ||
143 (hwif->host_flags & IDE_HFLAG_NO_SET_MODE))
146 BUG_ON(hwif->pio_mask == 0x00);
148 host_pio = fls(hwif->pio_mask) - 1;
150 pio = ide_get_best_pio_mode(drive, req_pio, host_pio);
154 * - report device max PIO mode
155 * - check req_pio != 255 against device max PIO mode
157 printk(KERN_DEBUG "%s: host max PIO%d wanted PIO%d%s selected PIO%d\n",
158 drive->name, host_pio, req_pio,
159 req_pio == 255 ? "(auto-tune)" : "", pio);
161 (void)ide_set_pio_mode(drive, XFER_PIO_0 + pio);
163 EXPORT_SYMBOL_GPL(ide_set_pio);
166 * ide_toggle_bounce - handle bounce buffering
167 * @drive: drive to update
168 * @on: on/off boolean
170 * Enable or disable bounce buffering for the device. Drives move
171 * between PIO and DMA and that changes the rules we need.
174 void ide_toggle_bounce(ide_drive_t *drive, int on)
176 u64 addr = BLK_BOUNCE_HIGH; /* dma64_addr_t */
178 if (!PCI_DMA_BUS_IS_PHYS) {
179 addr = BLK_BOUNCE_ANY;
180 } else if (on && drive->media == ide_disk) {
181 struct device *dev = drive->hwif->dev;
183 if (dev && dev->dma_mask)
184 addr = *dev->dma_mask;
188 blk_queue_bounce_limit(drive->queue, addr);
191 int ide_set_pio_mode(ide_drive_t *drive, const u8 mode)
193 ide_hwif_t *hwif = drive->hwif;
194 const struct ide_port_ops *port_ops = hwif->port_ops;
196 if (hwif->host_flags & IDE_HFLAG_NO_SET_MODE)
199 if (port_ops == NULL || port_ops->set_pio_mode == NULL)
203 * TODO: temporary hack for some legacy host drivers that didn't
204 * set transfer mode on the device in ->set_pio_mode method...
206 if (port_ops->set_dma_mode == NULL) {
207 port_ops->set_pio_mode(drive, mode - XFER_PIO_0);
211 if (hwif->host_flags & IDE_HFLAG_POST_SET_MODE) {
212 if (ide_config_drive_speed(drive, mode))
214 port_ops->set_pio_mode(drive, mode - XFER_PIO_0);
217 port_ops->set_pio_mode(drive, mode - XFER_PIO_0);
218 return ide_config_drive_speed(drive, mode);
222 int ide_set_dma_mode(ide_drive_t *drive, const u8 mode)
224 ide_hwif_t *hwif = drive->hwif;
225 const struct ide_port_ops *port_ops = hwif->port_ops;
227 if (hwif->host_flags & IDE_HFLAG_NO_SET_MODE)
230 if (port_ops == NULL || port_ops->set_dma_mode == NULL)
233 if (hwif->host_flags & IDE_HFLAG_POST_SET_MODE) {
234 if (ide_config_drive_speed(drive, mode))
236 port_ops->set_dma_mode(drive, mode);
239 port_ops->set_dma_mode(drive, mode);
240 return ide_config_drive_speed(drive, mode);
243 EXPORT_SYMBOL_GPL(ide_set_dma_mode);
246 * ide_set_xfer_rate - set transfer rate
247 * @drive: drive to set
248 * @rate: speed to attempt to set
250 * General helper for setting the speed of an IDE device. This
251 * function knows about user enforced limits from the configuration
252 * which ->set_pio_mode/->set_dma_mode does not.
255 int ide_set_xfer_rate(ide_drive_t *drive, u8 rate)
257 ide_hwif_t *hwif = drive->hwif;
258 const struct ide_port_ops *port_ops = hwif->port_ops;
260 if (port_ops == NULL || port_ops->set_dma_mode == NULL ||
261 (hwif->host_flags & IDE_HFLAG_NO_SET_MODE))
264 rate = ide_rate_filter(drive, rate);
266 BUG_ON(rate < XFER_PIO_0);
268 if (rate >= XFER_PIO_0 && rate <= XFER_PIO_5)
269 return ide_set_pio_mode(drive, rate);
271 return ide_set_dma_mode(drive, rate);
274 static void ide_dump_opcode(ide_drive_t *drive)
276 struct request *rq = drive->hwif->hwgroup->rq;
277 ide_task_t *task = NULL;
282 if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE)
285 printk(KERN_ERR "ide: failed opcode was: ");
287 printk(KERN_CONT "unknown\n");
289 printk(KERN_CONT "0x%02x\n", task->tf.command);
292 u64 ide_get_lba_addr(struct ide_taskfile *tf, int lba48)
297 high = (tf->hob_lbah << 16) | (tf->hob_lbam << 8) |
300 high = tf->device & 0xf;
301 low = (tf->lbah << 16) | (tf->lbam << 8) | tf->lbal;
303 return ((u64)high << 24) | low;
305 EXPORT_SYMBOL_GPL(ide_get_lba_addr);
307 static void ide_dump_sector(ide_drive_t *drive)
310 struct ide_taskfile *tf = &task.tf;
311 u8 lba48 = !!(drive->dev_flags & IDE_DFLAG_LBA48);
313 memset(&task, 0, sizeof(task));
315 task.tf_flags = IDE_TFLAG_IN_LBA | IDE_TFLAG_IN_HOB_LBA |
318 task.tf_flags = IDE_TFLAG_IN_LBA | IDE_TFLAG_IN_DEVICE;
320 drive->hwif->tp_ops->tf_read(drive, &task);
322 if (lba48 || (tf->device & ATA_LBA))
323 printk(KERN_CONT ", LBAsect=%llu",
324 (unsigned long long)ide_get_lba_addr(tf, lba48));
326 printk(KERN_CONT ", CHS=%d/%d/%d", (tf->lbah << 8) + tf->lbam,
327 tf->device & 0xf, tf->lbal);
330 static void ide_dump_ata_error(ide_drive_t *drive, u8 err)
332 printk(KERN_ERR "{ ");
333 if (err & ATA_ABORTED)
334 printk(KERN_CONT "DriveStatusError ");
336 printk(KERN_CONT "%s",
337 (err & ATA_ABORTED) ? "BadCRC " : "BadSector ");
339 printk(KERN_CONT "UncorrectableError ");
341 printk(KERN_CONT "SectorIdNotFound ");
342 if (err & ATA_TRK0NF)
343 printk(KERN_CONT "TrackZeroNotFound ");
345 printk(KERN_CONT "AddrMarkNotFound ");
346 printk(KERN_CONT "}");
347 if ((err & (ATA_BBK | ATA_ABORTED)) == ATA_BBK ||
348 (err & (ATA_UNC | ATA_IDNF | ATA_AMNF))) {
349 ide_dump_sector(drive);
350 if (HWGROUP(drive) && HWGROUP(drive)->rq)
351 printk(KERN_CONT ", sector=%llu",
352 (unsigned long long)HWGROUP(drive)->rq->sector);
354 printk(KERN_CONT "\n");
357 static void ide_dump_atapi_error(ide_drive_t *drive, u8 err)
359 printk(KERN_ERR "{ ");
361 printk(KERN_CONT "IllegalLengthIndication ");
363 printk(KERN_CONT "EndOfMedia ");
364 if (err & ATA_ABORTED)
365 printk(KERN_CONT "AbortedCommand ");
367 printk(KERN_CONT "MediaChangeRequested ");
369 printk(KERN_CONT "LastFailedSense=0x%02x ",
370 (err & ATAPI_LFS) >> 4);
371 printk(KERN_CONT "}\n");
375 * ide_dump_status - translate ATA/ATAPI error
376 * @drive: drive that status applies to
377 * @msg: text message to print
378 * @stat: status byte to decode
380 * Error reporting, in human readable form (luxurious, but a memory hog).
381 * Combines the drive name, message and status byte to provide a
382 * user understandable explanation of the device error.
385 u8 ide_dump_status(ide_drive_t *drive, const char *msg, u8 stat)
389 printk(KERN_ERR "%s: %s: status=0x%02x { ", drive->name, msg, stat);
391 printk(KERN_CONT "Busy ");
394 printk(KERN_CONT "DriveReady ");
396 printk(KERN_CONT "DeviceFault ");
398 printk(KERN_CONT "SeekComplete ");
400 printk(KERN_CONT "DataRequest ");
402 printk(KERN_CONT "CorrectedError ");
404 printk(KERN_CONT "Index ");
406 printk(KERN_CONT "Error ");
408 printk(KERN_CONT "}\n");
409 if ((stat & (ATA_BUSY | ATA_ERR)) == ATA_ERR) {
410 err = ide_read_error(drive);
411 printk(KERN_ERR "%s: %s: error=0x%02x ", drive->name, msg, err);
412 if (drive->media == ide_disk)
413 ide_dump_ata_error(drive, err);
415 ide_dump_atapi_error(drive, err);
417 ide_dump_opcode(drive);
420 EXPORT_SYMBOL(ide_dump_status);