ide-lib.c 11 KB

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