ide-lib.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. BUG_ON(rate < XFER_PIO_0);
  223. if (rate >= XFER_PIO_0 && rate <= XFER_PIO_5)
  224. return ide_set_pio_mode(drive, rate);
  225. return ide_set_dma_mode(drive, rate);
  226. }
  227. static void ide_dump_opcode(ide_drive_t *drive)
  228. {
  229. struct request *rq;
  230. ide_task_t *task = NULL;
  231. spin_lock(&ide_lock);
  232. rq = NULL;
  233. if (HWGROUP(drive))
  234. rq = HWGROUP(drive)->rq;
  235. spin_unlock(&ide_lock);
  236. if (!rq)
  237. return;
  238. if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE)
  239. task = rq->special;
  240. printk("ide: failed opcode was: ");
  241. if (task == NULL)
  242. printk(KERN_CONT "unknown\n");
  243. else
  244. printk(KERN_CONT "0x%02x\n", task->tf.command);
  245. }
  246. u64 ide_get_lba_addr(struct ide_taskfile *tf, int lba48)
  247. {
  248. u32 high, low;
  249. if (lba48)
  250. high = (tf->hob_lbah << 16) | (tf->hob_lbam << 8) |
  251. tf->hob_lbal;
  252. else
  253. high = tf->device & 0xf;
  254. low = (tf->lbah << 16) | (tf->lbam << 8) | tf->lbal;
  255. return ((u64)high << 24) | low;
  256. }
  257. EXPORT_SYMBOL_GPL(ide_get_lba_addr);
  258. static void ide_dump_sector(ide_drive_t *drive)
  259. {
  260. ide_task_t task;
  261. struct ide_taskfile *tf = &task.tf;
  262. int lba48 = (drive->addressing == 1) ? 1 : 0;
  263. memset(&task, 0, sizeof(task));
  264. if (lba48)
  265. task.tf_flags = IDE_TFLAG_IN_LBA | IDE_TFLAG_IN_HOB_LBA |
  266. IDE_TFLAG_LBA48;
  267. else
  268. task.tf_flags = IDE_TFLAG_IN_LBA | IDE_TFLAG_IN_DEVICE;
  269. drive->hwif->tp_ops->tf_read(drive, &task);
  270. if (lba48 || (tf->device & ATA_LBA))
  271. printk(", LBAsect=%llu",
  272. (unsigned long long)ide_get_lba_addr(tf, lba48));
  273. else
  274. printk(", CHS=%d/%d/%d", (tf->lbah << 8) + tf->lbam,
  275. tf->device & 0xf, tf->lbal);
  276. }
  277. static void ide_dump_ata_error(ide_drive_t *drive, u8 err)
  278. {
  279. printk("{ ");
  280. if (err & ABRT_ERR) printk("DriveStatusError ");
  281. if (err & ICRC_ERR)
  282. printk((err & ABRT_ERR) ? "BadCRC " : "BadSector ");
  283. if (err & ECC_ERR) printk("UncorrectableError ");
  284. if (err & ID_ERR) printk("SectorIdNotFound ");
  285. if (err & TRK0_ERR) printk("TrackZeroNotFound ");
  286. if (err & MARK_ERR) printk("AddrMarkNotFound ");
  287. printk("}");
  288. if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR ||
  289. (err & (ECC_ERR|ID_ERR|MARK_ERR))) {
  290. ide_dump_sector(drive);
  291. if (HWGROUP(drive) && HWGROUP(drive)->rq)
  292. printk(", sector=%llu",
  293. (unsigned long long)HWGROUP(drive)->rq->sector);
  294. }
  295. printk("\n");
  296. }
  297. static void ide_dump_atapi_error(ide_drive_t *drive, u8 err)
  298. {
  299. printk("{ ");
  300. if (err & ILI_ERR) printk("IllegalLengthIndication ");
  301. if (err & EOM_ERR) printk("EndOfMedia ");
  302. if (err & ABRT_ERR) printk("AbortedCommand ");
  303. if (err & MCR_ERR) printk("MediaChangeRequested ");
  304. if (err & LFS_ERR) printk("LastFailedSense=0x%02x ",
  305. (err & LFS_ERR) >> 4);
  306. printk("}\n");
  307. }
  308. /**
  309. * ide_dump_status - translate ATA/ATAPI error
  310. * @drive: drive that status applies to
  311. * @msg: text message to print
  312. * @stat: status byte to decode
  313. *
  314. * Error reporting, in human readable form (luxurious, but a memory hog).
  315. * Combines the drive name, message and status byte to provide a
  316. * user understandable explanation of the device error.
  317. */
  318. u8 ide_dump_status(ide_drive_t *drive, const char *msg, u8 stat)
  319. {
  320. unsigned long flags;
  321. u8 err = 0;
  322. local_irq_save(flags);
  323. printk("%s: %s: status=0x%02x { ", drive->name, msg, stat);
  324. if (stat & BUSY_STAT)
  325. printk("Busy ");
  326. else {
  327. if (stat & READY_STAT) printk("DriveReady ");
  328. if (stat & WRERR_STAT) printk("DeviceFault ");
  329. if (stat & SEEK_STAT) printk("SeekComplete ");
  330. if (stat & DRQ_STAT) printk("DataRequest ");
  331. if (stat & ECC_STAT) printk("CorrectedError ");
  332. if (stat & INDEX_STAT) printk("Index ");
  333. if (stat & ERR_STAT) printk("Error ");
  334. }
  335. printk("}\n");
  336. if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
  337. err = ide_read_error(drive);
  338. printk("%s: %s: error=0x%02x ", drive->name, msg, err);
  339. if (drive->media == ide_disk)
  340. ide_dump_ata_error(drive, err);
  341. else
  342. ide_dump_atapi_error(drive, err);
  343. }
  344. ide_dump_opcode(drive);
  345. local_irq_restore(flags);
  346. return err;
  347. }
  348. EXPORT_SYMBOL(ide_dump_status);