1 #include <linux/types.h>
2 #include <linux/string.h>
3 #include <linux/kernel.h>
4 #include <linux/interrupt.h>
5 #include <linux/hdreg.h>
7 #include <linux/bitops.h>
9 static const char *udma_str[] =
10 { "UDMA/16", "UDMA/25", "UDMA/33", "UDMA/44",
11 "UDMA/66", "UDMA/100", "UDMA/133", "UDMA7" };
12 static const char *mwdma_str[] =
13 { "MWDMA0", "MWDMA1", "MWDMA2" };
14 static const char *swdma_str[] =
15 { "SWDMA0", "SWDMA1", "SWDMA2" };
16 static const char *pio_str[] =
17 { "PIO0", "PIO1", "PIO2", "PIO3", "PIO4", "PIO5" };
20 * ide_xfer_verbose - return IDE mode names
21 * @mode: transfer mode
23 * Returns a constant string giving the name of the mode
27 const char *ide_xfer_verbose(u8 mode)
32 if (mode >= XFER_UDMA_0 && mode <= XFER_UDMA_7)
34 else if (mode >= XFER_MW_DMA_0 && mode <= XFER_MW_DMA_2)
36 else if (mode >= XFER_SW_DMA_0 && mode <= XFER_SW_DMA_2)
38 else if (mode >= XFER_PIO_0 && mode <= XFER_PIO_5)
40 else if (mode == XFER_PIO_SLOW)
48 EXPORT_SYMBOL(ide_xfer_verbose);
51 * ide_rate_filter - filter transfer mode
53 * @speed: desired speed
55 * Given the available transfer modes this function returns
56 * the best available speed at or below the speed requested.
58 * TODO: check device PIO capabilities
61 static u8 ide_rate_filter(ide_drive_t *drive, u8 speed)
63 ide_hwif_t *hwif = drive->hwif;
64 u8 mode = ide_find_dma_mode(drive, speed);
68 mode = fls(hwif->pio_mask) - 1 + XFER_PIO_0;
73 /* printk("%s: mode 0x%02x, speed 0x%02x\n", __func__, mode, speed); */
75 return min(speed, mode);
79 * ide_get_best_pio_mode - get PIO mode from drive
80 * @drive: drive to consider
81 * @mode_wanted: preferred mode
82 * @max_mode: highest allowed mode
84 * This routine returns the recommended PIO settings for a given drive,
85 * based on the drive->id information and the ide_pio_blacklist[].
87 * Drive PIO mode is auto-selected if 255 is passed as mode_wanted.
88 * This is used by most chipset support modules when "auto-tuning".
91 u8 ide_get_best_pio_mode (ide_drive_t *drive, u8 mode_wanted, u8 max_mode)
94 struct hd_driveid* id = drive->id;
97 if (mode_wanted != 255)
98 return min_t(u8, mode_wanted, max_mode);
100 if ((drive->hwif->host_flags & IDE_HFLAG_PIO_NO_BLACKLIST) == 0 &&
101 (pio_mode = ide_scan_pio_blacklist(id->model)) != -1) {
102 printk(KERN_INFO "%s: is on PIO blacklist\n", drive->name);
105 if (pio_mode > 2) { /* 2 is maximum allowed tPIO value */
109 if (id->field_valid & 2) { /* drive implements ATA2? */
110 if (id->capability & 8) { /* IORDY supported? */
111 if (id->eide_pio_modes & 7) {
113 if (id->eide_pio_modes & 4)
115 else if (id->eide_pio_modes & 2)
124 printk(KERN_INFO "%s: tPIO > 2, assuming tPIO = 2\n",
128 if (pio_mode > max_mode)
134 EXPORT_SYMBOL_GPL(ide_get_best_pio_mode);
136 /* req_pio == "255" for auto-tune */
137 void ide_set_pio(ide_drive_t *drive, u8 req_pio)
139 ide_hwif_t *hwif = drive->hwif;
140 const struct ide_port_ops *port_ops = hwif->port_ops;
143 if (port_ops == NULL || port_ops->set_pio_mode == NULL ||
144 (hwif->host_flags & IDE_HFLAG_NO_SET_MODE))
147 BUG_ON(hwif->pio_mask == 0x00);
149 host_pio = fls(hwif->pio_mask) - 1;
151 pio = ide_get_best_pio_mode(drive, req_pio, host_pio);
155 * - report device max PIO mode
156 * - check req_pio != 255 against device max PIO mode
158 printk(KERN_DEBUG "%s: host max PIO%d wanted PIO%d%s selected PIO%d\n",
159 drive->name, host_pio, req_pio,
160 req_pio == 255 ? "(auto-tune)" : "", pio);
162 (void)ide_set_pio_mode(drive, XFER_PIO_0 + pio);
165 EXPORT_SYMBOL_GPL(ide_set_pio);
168 * ide_toggle_bounce - handle bounce buffering
169 * @drive: drive to update
170 * @on: on/off boolean
172 * Enable or disable bounce buffering for the device. Drives move
173 * between PIO and DMA and that changes the rules we need.
176 void ide_toggle_bounce(ide_drive_t *drive, int on)
178 u64 addr = BLK_BOUNCE_HIGH; /* dma64_addr_t */
180 if (!PCI_DMA_BUS_IS_PHYS) {
181 addr = BLK_BOUNCE_ANY;
182 } else if (on && drive->media == ide_disk) {
183 struct device *dev = drive->hwif->dev;
185 if (dev && dev->dma_mask)
186 addr = *dev->dma_mask;
190 blk_queue_bounce_limit(drive->queue, addr);
193 int ide_set_pio_mode(ide_drive_t *drive, const u8 mode)
195 ide_hwif_t *hwif = drive->hwif;
196 const struct ide_port_ops *port_ops = hwif->port_ops;
198 if (hwif->host_flags & IDE_HFLAG_NO_SET_MODE)
201 if (port_ops == NULL || port_ops->set_pio_mode == NULL)
205 * TODO: temporary hack for some legacy host drivers that didn't
206 * set transfer mode on the device in ->set_pio_mode method...
208 if (port_ops->set_dma_mode == NULL) {
209 port_ops->set_pio_mode(drive, mode - XFER_PIO_0);
213 if (hwif->host_flags & IDE_HFLAG_POST_SET_MODE) {
214 if (ide_config_drive_speed(drive, mode))
216 port_ops->set_pio_mode(drive, mode - XFER_PIO_0);
219 port_ops->set_pio_mode(drive, mode - XFER_PIO_0);
220 return ide_config_drive_speed(drive, mode);
224 int ide_set_dma_mode(ide_drive_t *drive, const u8 mode)
226 ide_hwif_t *hwif = drive->hwif;
227 const struct ide_port_ops *port_ops = hwif->port_ops;
229 if (hwif->host_flags & IDE_HFLAG_NO_SET_MODE)
232 if (port_ops == NULL || port_ops->set_dma_mode == NULL)
235 if (hwif->host_flags & IDE_HFLAG_POST_SET_MODE) {
236 if (ide_config_drive_speed(drive, mode))
238 port_ops->set_dma_mode(drive, mode);
241 port_ops->set_dma_mode(drive, mode);
242 return ide_config_drive_speed(drive, mode);
246 EXPORT_SYMBOL_GPL(ide_set_dma_mode);
249 * ide_set_xfer_rate - set transfer rate
250 * @drive: drive to set
251 * @rate: speed to attempt to set
253 * General helper for setting the speed of an IDE device. This
254 * function knows about user enforced limits from the configuration
255 * which ->set_pio_mode/->set_dma_mode does not.
258 int ide_set_xfer_rate(ide_drive_t *drive, u8 rate)
260 ide_hwif_t *hwif = drive->hwif;
261 const struct ide_port_ops *port_ops = hwif->port_ops;
263 if (port_ops == NULL || port_ops->set_dma_mode == NULL ||
264 (hwif->host_flags & IDE_HFLAG_NO_SET_MODE))
267 rate = ide_rate_filter(drive, rate);
269 if (rate >= XFER_PIO_0 && rate <= XFER_PIO_5)
270 return ide_set_pio_mode(drive, rate);
273 * TODO: transfer modes 0x00-0x07 passed from the user-space are
274 * currently handled here which needs fixing (please note that such
275 * case could happen iff the transfer mode has already been set on
276 * the device by ide-proc.c::set_xfer_rate()).
278 if (rate < XFER_PIO_0) {
279 if (hwif->host_flags & IDE_HFLAG_ABUSE_SET_DMA_MODE)
280 return ide_set_dma_mode(drive, rate);
282 return ide_config_drive_speed(drive, rate);
285 return ide_set_dma_mode(drive, rate);
288 static void ide_dump_opcode(ide_drive_t *drive)
291 ide_task_t *task = NULL;
293 spin_lock(&ide_lock);
296 rq = HWGROUP(drive)->rq;
297 spin_unlock(&ide_lock);
301 if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE)
304 printk("ide: failed opcode was: ");
306 printk(KERN_CONT "unknown\n");
308 printk(KERN_CONT "0x%02x\n", task->tf.command);
311 u64 ide_get_lba_addr(struct ide_taskfile *tf, int lba48)
316 high = (tf->hob_lbah << 16) | (tf->hob_lbam << 8) |
319 high = tf->device & 0xf;
320 low = (tf->lbah << 16) | (tf->lbam << 8) | tf->lbal;
322 return ((u64)high << 24) | low;
324 EXPORT_SYMBOL_GPL(ide_get_lba_addr);
326 static void ide_dump_sector(ide_drive_t *drive)
329 struct ide_taskfile *tf = &task.tf;
330 int lba48 = (drive->addressing == 1) ? 1 : 0;
332 memset(&task, 0, sizeof(task));
334 task.tf_flags = IDE_TFLAG_IN_LBA | IDE_TFLAG_IN_HOB_LBA |
337 task.tf_flags = IDE_TFLAG_IN_LBA | IDE_TFLAG_IN_DEVICE;
339 drive->hwif->tf_read(drive, &task);
341 if (lba48 || (tf->device & ATA_LBA))
342 printk(", LBAsect=%llu",
343 (unsigned long long)ide_get_lba_addr(tf, lba48));
345 printk(", CHS=%d/%d/%d", (tf->lbah << 8) + tf->lbam,
346 tf->device & 0xf, tf->lbal);
349 static void ide_dump_ata_error(ide_drive_t *drive, u8 err)
352 if (err & ABRT_ERR) printk("DriveStatusError ");
354 printk((err & ABRT_ERR) ? "BadCRC " : "BadSector ");
355 if (err & ECC_ERR) printk("UncorrectableError ");
356 if (err & ID_ERR) printk("SectorIdNotFound ");
357 if (err & TRK0_ERR) printk("TrackZeroNotFound ");
358 if (err & MARK_ERR) printk("AddrMarkNotFound ");
360 if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR ||
361 (err & (ECC_ERR|ID_ERR|MARK_ERR))) {
362 ide_dump_sector(drive);
363 if (HWGROUP(drive) && HWGROUP(drive)->rq)
364 printk(", sector=%llu",
365 (unsigned long long)HWGROUP(drive)->rq->sector);
370 static void ide_dump_atapi_error(ide_drive_t *drive, u8 err)
373 if (err & ILI_ERR) printk("IllegalLengthIndication ");
374 if (err & EOM_ERR) printk("EndOfMedia ");
375 if (err & ABRT_ERR) printk("AbortedCommand ");
376 if (err & MCR_ERR) printk("MediaChangeRequested ");
377 if (err & LFS_ERR) printk("LastFailedSense=0x%02x ",
378 (err & LFS_ERR) >> 4);
383 * ide_dump_status - translate ATA/ATAPI error
384 * @drive: drive that status applies to
385 * @msg: text message to print
386 * @stat: status byte to decode
388 * Error reporting, in human readable form (luxurious, but a memory hog).
389 * Combines the drive name, message and status byte to provide a
390 * user understandable explanation of the device error.
393 u8 ide_dump_status(ide_drive_t *drive, const char *msg, u8 stat)
398 local_irq_save(flags);
399 printk("%s: %s: status=0x%02x { ", drive->name, msg, stat);
400 if (stat & BUSY_STAT)
403 if (stat & READY_STAT) printk("DriveReady ");
404 if (stat & WRERR_STAT) printk("DeviceFault ");
405 if (stat & SEEK_STAT) printk("SeekComplete ");
406 if (stat & DRQ_STAT) printk("DataRequest ");
407 if (stat & ECC_STAT) printk("CorrectedError ");
408 if (stat & INDEX_STAT) printk("Index ");
409 if (stat & ERR_STAT) printk("Error ");
412 if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
413 err = ide_read_error(drive);
414 printk("%s: %s: error=0x%02x ", drive->name, msg, err);
415 if (drive->media == ide_disk)
416 ide_dump_ata_error(drive, err);
418 ide_dump_atapi_error(drive, err);
420 ide_dump_opcode(drive);
421 local_irq_restore(flags);
425 EXPORT_SYMBOL(ide_dump_status);