ide-lib.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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>
  6. #include <linux/ide.h>
  7. #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" };
  17. /**
  18. * ide_xfer_verbose - return IDE mode names
  19. * @mode: transfer mode
  20. *
  21. * Returns a constant string giving the name of the mode
  22. * requested.
  23. */
  24. const char *ide_xfer_verbose(u8 mode)
  25. {
  26. const char *s;
  27. u8 i = mode & 0xf;
  28. if (mode >= XFER_UDMA_0 && mode <= XFER_UDMA_7)
  29. s = udma_str[i];
  30. else if (mode >= XFER_MW_DMA_0 && mode <= XFER_MW_DMA_2)
  31. s = mwdma_str[i];
  32. else if (mode >= XFER_SW_DMA_0 && mode <= XFER_SW_DMA_2)
  33. s = swdma_str[i];
  34. else if (mode >= XFER_PIO_0 && mode <= XFER_PIO_5)
  35. s = pio_str[i & 0x7];
  36. else if (mode == XFER_PIO_SLOW)
  37. s = "PIO SLOW";
  38. else
  39. s = "XFER ERROR";
  40. return s;
  41. }
  42. EXPORT_SYMBOL(ide_xfer_verbose);
  43. /**
  44. * ide_rate_filter - filter transfer mode
  45. * @drive: IDE device
  46. * @speed: desired speed
  47. *
  48. * Given the available transfer modes this function returns
  49. * the best available speed at or below the speed requested.
  50. *
  51. * TODO: check device PIO capabilities
  52. */
  53. static u8 ide_rate_filter(ide_drive_t *drive, u8 speed)
  54. {
  55. ide_hwif_t *hwif = drive->hwif;
  56. u8 mode = ide_find_dma_mode(drive, speed);
  57. if (mode == 0) {
  58. if (hwif->pio_mask)
  59. mode = fls(hwif->pio_mask) - 1 + XFER_PIO_0;
  60. else
  61. mode = XFER_PIO_4;
  62. }
  63. /* printk("%s: mode 0x%02x, speed 0x%02x\n", __func__, mode, speed); */
  64. return min(speed, mode);
  65. }
  66. /**
  67. * ide_get_best_pio_mode - get PIO mode from drive
  68. * @drive: drive to consider
  69. * @mode_wanted: preferred mode
  70. * @max_mode: highest allowed mode
  71. *
  72. * This routine returns the recommended PIO settings for a given drive,
  73. * based on the drive->id information and the ide_pio_blacklist[].
  74. *
  75. * Drive PIO mode is auto-selected if 255 is passed as mode_wanted.
  76. * This is used by most chipset support modules when "auto-tuning".
  77. */
  78. u8 ide_get_best_pio_mode (ide_drive_t *drive, u8 mode_wanted, u8 max_mode)
  79. {
  80. int pio_mode;
  81. struct hd_driveid* id = drive->id;
  82. int overridden = 0;
  83. if (mode_wanted != 255)
  84. return min_t(u8, mode_wanted, max_mode);
  85. if ((drive->hwif->host_flags & IDE_HFLAG_PIO_NO_BLACKLIST) == 0 &&
  86. (pio_mode = ide_scan_pio_blacklist(id->model)) != -1) {
  87. printk(KERN_INFO "%s: is on PIO blacklist\n", drive->name);
  88. } else {
  89. pio_mode = id->tPIO;
  90. if (pio_mode > 2) { /* 2 is maximum allowed tPIO value */
  91. pio_mode = 2;
  92. overridden = 1;
  93. }
  94. if (id->field_valid & 2) { /* drive implements ATA2? */
  95. if (id->capability & 8) { /* IORDY supported? */
  96. if (id->eide_pio_modes & 7) {
  97. overridden = 0;
  98. if (id->eide_pio_modes & 4)
  99. pio_mode = 5;
  100. else if (id->eide_pio_modes & 2)
  101. pio_mode = 4;
  102. else
  103. pio_mode = 3;
  104. }
  105. }
  106. }
  107. if (overridden)
  108. printk(KERN_INFO "%s: tPIO > 2, assuming tPIO = 2\n",
  109. drive->name);
  110. }
  111. if (pio_mode > max_mode)
  112. pio_mode = max_mode;
  113. return pio_mode;
  114. }
  115. EXPORT_SYMBOL_GPL(ide_get_best_pio_mode);
  116. /* req_pio == "255" for auto-tune */
  117. void ide_set_pio(ide_drive_t *drive, u8 req_pio)
  118. {
  119. ide_hwif_t *hwif = drive->hwif;
  120. const struct ide_port_ops *port_ops = hwif->port_ops;
  121. u8 host_pio, pio;
  122. if (port_ops == NULL || port_ops->set_pio_mode == NULL ||
  123. (hwif->host_flags & IDE_HFLAG_NO_SET_MODE))
  124. return;
  125. BUG_ON(hwif->pio_mask == 0x00);
  126. host_pio = fls(hwif->pio_mask) - 1;
  127. pio = ide_get_best_pio_mode(drive, req_pio, host_pio);
  128. /*
  129. * TODO:
  130. * - report device max PIO mode
  131. * - check req_pio != 255 against device max PIO mode
  132. */
  133. printk(KERN_DEBUG "%s: host max PIO%d wanted PIO%d%s selected PIO%d\n",
  134. drive->name, host_pio, req_pio,
  135. req_pio == 255 ? "(auto-tune)" : "", pio);
  136. (void)ide_set_pio_mode(drive, XFER_PIO_0 + pio);
  137. }
  138. EXPORT_SYMBOL_GPL(ide_set_pio);
  139. /**
  140. * ide_toggle_bounce - handle bounce buffering
  141. * @drive: drive to update
  142. * @on: on/off boolean
  143. *
  144. * Enable or disable bounce buffering for the device. Drives move
  145. * between PIO and DMA and that changes the rules we need.
  146. */
  147. void ide_toggle_bounce(ide_drive_t *drive, int on)
  148. {
  149. u64 addr = BLK_BOUNCE_HIGH; /* dma64_addr_t */
  150. if (!PCI_DMA_BUS_IS_PHYS) {
  151. addr = BLK_BOUNCE_ANY;
  152. } else if (on && drive->media == ide_disk) {
  153. struct device *dev = drive->hwif->dev;
  154. if (dev && dev->dma_mask)
  155. addr = *dev->dma_mask;
  156. }
  157. if (drive->queue)
  158. blk_queue_bounce_limit(drive->queue, addr);
  159. }
  160. int ide_set_pio_mode(ide_drive_t *drive, const u8 mode)
  161. {
  162. ide_hwif_t *hwif = drive->hwif;
  163. const struct ide_port_ops *port_ops = hwif->port_ops;
  164. if (hwif->host_flags & IDE_HFLAG_NO_SET_MODE)
  165. return 0;
  166. if (port_ops == NULL || port_ops->set_pio_mode == NULL)
  167. return -1;
  168. /*
  169. * TODO: temporary hack for some legacy host drivers that didn't
  170. * set transfer mode on the device in ->set_pio_mode method...
  171. */
  172. if (port_ops->set_dma_mode == NULL) {
  173. port_ops->set_pio_mode(drive, mode - XFER_PIO_0);
  174. return 0;
  175. }
  176. if (hwif->host_flags & IDE_HFLAG_POST_SET_MODE) {
  177. if (ide_config_drive_speed(drive, mode))
  178. return -1;
  179. port_ops->set_pio_mode(drive, mode - XFER_PIO_0);
  180. return 0;
  181. } else {
  182. port_ops->set_pio_mode(drive, mode - XFER_PIO_0);
  183. return ide_config_drive_speed(drive, mode);
  184. }
  185. }
  186. int ide_set_dma_mode(ide_drive_t *drive, const u8 mode)
  187. {
  188. ide_hwif_t *hwif = drive->hwif;
  189. const struct ide_port_ops *port_ops = hwif->port_ops;
  190. if (hwif->host_flags & IDE_HFLAG_NO_SET_MODE)
  191. return 0;
  192. if (port_ops == NULL || port_ops->set_dma_mode == NULL)
  193. return -1;
  194. if (hwif->host_flags & IDE_HFLAG_POST_SET_MODE) {
  195. if (ide_config_drive_speed(drive, mode))
  196. return -1;
  197. port_ops->set_dma_mode(drive, mode);
  198. return 0;
  199. } else {
  200. port_ops->set_dma_mode(drive, mode);
  201. return ide_config_drive_speed(drive, mode);
  202. }
  203. }
  204. EXPORT_SYMBOL_GPL(ide_set_dma_mode);
  205. /**
  206. * ide_set_xfer_rate - set transfer rate
  207. * @drive: drive to set
  208. * @rate: speed to attempt to set
  209. *
  210. * General helper for setting the speed of an IDE device. This
  211. * function knows about user enforced limits from the configuration
  212. * which ->set_pio_mode/->set_dma_mode does not.
  213. */
  214. int ide_set_xfer_rate(ide_drive_t *drive, u8 rate)
  215. {
  216. ide_hwif_t *hwif = drive->hwif;
  217. const struct ide_port_ops *port_ops = hwif->port_ops;
  218. if (port_ops == NULL || port_ops->set_dma_mode == NULL ||
  219. (hwif->host_flags & IDE_HFLAG_NO_SET_MODE))
  220. return -1;
  221. rate = ide_rate_filter(drive, rate);
  222. if (rate >= XFER_PIO_0 && rate <= XFER_PIO_5)
  223. return ide_set_pio_mode(drive, rate);
  224. /*
  225. * TODO: transfer modes 0x00-0x07 passed from the user-space are
  226. * currently handled here which needs fixing (please note that such
  227. * case could happen iff the transfer mode has already been set on
  228. * the device by ide-proc.c::set_xfer_rate()).
  229. */
  230. if (rate < XFER_PIO_0) {
  231. if (hwif->host_flags & IDE_HFLAG_ABUSE_SET_DMA_MODE)
  232. return ide_set_dma_mode(drive, rate);
  233. else
  234. return ide_config_drive_speed(drive, rate);
  235. }
  236. return ide_set_dma_mode(drive, rate);
  237. }
  238. static void ide_dump_opcode(ide_drive_t *drive)
  239. {
  240. struct request *rq;
  241. ide_task_t *task = NULL;
  242. spin_lock(&ide_lock);
  243. rq = NULL;
  244. if (HWGROUP(drive))
  245. rq = HWGROUP(drive)->rq;
  246. spin_unlock(&ide_lock);
  247. if (!rq)
  248. return;
  249. if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE)
  250. task = rq->special;
  251. printk("ide: failed opcode was: ");
  252. if (task == NULL)
  253. printk(KERN_CONT "unknown\n");
  254. else
  255. printk(KERN_CONT "0x%02x\n", task->tf.command);
  256. }
  257. u64 ide_get_lba_addr(struct ide_taskfile *tf, int lba48)
  258. {
  259. u32 high, low;
  260. if (lba48)
  261. high = (tf->hob_lbah << 16) | (tf->hob_lbam << 8) |
  262. tf->hob_lbal;
  263. else
  264. high = tf->device & 0xf;
  265. low = (tf->lbah << 16) | (tf->lbam << 8) | tf->lbal;
  266. return ((u64)high << 24) | low;
  267. }
  268. EXPORT_SYMBOL_GPL(ide_get_lba_addr);
  269. static void ide_dump_sector(ide_drive_t *drive)
  270. {
  271. ide_task_t task;
  272. struct ide_taskfile *tf = &task.tf;
  273. int lba48 = (drive->addressing == 1) ? 1 : 0;
  274. memset(&task, 0, sizeof(task));
  275. if (lba48)
  276. task.tf_flags = IDE_TFLAG_IN_LBA | IDE_TFLAG_IN_HOB_LBA |
  277. IDE_TFLAG_LBA48;
  278. else
  279. task.tf_flags = IDE_TFLAG_IN_LBA | IDE_TFLAG_IN_DEVICE;
  280. drive->hwif->tf_read(drive, &task);
  281. if (lba48 || (tf->device & ATA_LBA))
  282. printk(", LBAsect=%llu",
  283. (unsigned long long)ide_get_lba_addr(tf, lba48));
  284. else
  285. printk(", CHS=%d/%d/%d", (tf->lbah << 8) + tf->lbam,
  286. tf->device & 0xf, tf->lbal);
  287. }
  288. static void ide_dump_ata_error(ide_drive_t *drive, u8 err)
  289. {
  290. printk("{ ");
  291. if (err & ABRT_ERR) printk("DriveStatusError ");
  292. if (err & ICRC_ERR)
  293. printk((err & ABRT_ERR) ? "BadCRC " : "BadSector ");
  294. if (err & ECC_ERR) printk("UncorrectableError ");
  295. if (err & ID_ERR) printk("SectorIdNotFound ");
  296. if (err & TRK0_ERR) printk("TrackZeroNotFound ");
  297. if (err & MARK_ERR) printk("AddrMarkNotFound ");
  298. printk("}");
  299. if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR ||
  300. (err & (ECC_ERR|ID_ERR|MARK_ERR))) {
  301. ide_dump_sector(drive);
  302. if (HWGROUP(drive) && HWGROUP(drive)->rq)
  303. printk(", sector=%llu",
  304. (unsigned long long)HWGROUP(drive)->rq->sector);
  305. }
  306. printk("\n");
  307. }
  308. static void ide_dump_atapi_error(ide_drive_t *drive, u8 err)
  309. {
  310. printk("{ ");
  311. if (err & ILI_ERR) printk("IllegalLengthIndication ");
  312. if (err & EOM_ERR) printk("EndOfMedia ");
  313. if (err & ABRT_ERR) printk("AbortedCommand ");
  314. if (err & MCR_ERR) printk("MediaChangeRequested ");
  315. if (err & LFS_ERR) printk("LastFailedSense=0x%02x ",
  316. (err & LFS_ERR) >> 4);
  317. printk("}\n");
  318. }
  319. /**
  320. * ide_dump_status - translate ATA/ATAPI error
  321. * @drive: drive that status applies to
  322. * @msg: text message to print
  323. * @stat: status byte to decode
  324. *
  325. * Error reporting, in human readable form (luxurious, but a memory hog).
  326. * Combines the drive name, message and status byte to provide a
  327. * user understandable explanation of the device error.
  328. */
  329. u8 ide_dump_status(ide_drive_t *drive, const char *msg, u8 stat)
  330. {
  331. unsigned long flags;
  332. u8 err = 0;
  333. local_irq_save(flags);
  334. printk("%s: %s: status=0x%02x { ", drive->name, msg, stat);
  335. if (stat & BUSY_STAT)
  336. printk("Busy ");
  337. else {
  338. if (stat & READY_STAT) printk("DriveReady ");
  339. if (stat & WRERR_STAT) printk("DeviceFault ");
  340. if (stat & SEEK_STAT) printk("SeekComplete ");
  341. if (stat & DRQ_STAT) printk("DataRequest ");
  342. if (stat & ECC_STAT) printk("CorrectedError ");
  343. if (stat & INDEX_STAT) printk("Index ");
  344. if (stat & ERR_STAT) printk("Error ");
  345. }
  346. printk("}\n");
  347. if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
  348. err = ide_read_error(drive);
  349. printk("%s: %s: error=0x%02x ", drive->name, msg, err);
  350. if (drive->media == ide_disk)
  351. ide_dump_ata_error(drive, err);
  352. else
  353. ide_dump_atapi_error(drive, err);
  354. }
  355. ide_dump_opcode(drive);
  356. local_irq_restore(flags);
  357. return err;
  358. }
  359. EXPORT_SYMBOL(ide_dump_status);