ide-lib.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. * Shared data/functions for determining best PIO mode for an IDE drive.
  68. * Most of this stuff originally lived in cmd640.c, and changes to the
  69. * ide_pio_blacklist[] table should be made with EXTREME CAUTION to avoid
  70. * breaking the fragile cmd640.c support.
  71. */
  72. /*
  73. * Black list. Some drives incorrectly report their maximal PIO mode,
  74. * at least in respect to CMD640. Here we keep info on some known drives.
  75. */
  76. static struct ide_pio_info {
  77. const char *name;
  78. int pio;
  79. } ide_pio_blacklist [] = {
  80. { "Conner Peripherals 540MB - CFS540A", 3 },
  81. { "WDC AC2700", 3 },
  82. { "WDC AC2540", 3 },
  83. { "WDC AC2420", 3 },
  84. { "WDC AC2340", 3 },
  85. { "WDC AC2250", 0 },
  86. { "WDC AC2200", 0 },
  87. { "WDC AC21200", 4 },
  88. { "WDC AC2120", 0 },
  89. { "WDC AC2850", 3 },
  90. { "WDC AC1270", 3 },
  91. { "WDC AC1170", 1 },
  92. { "WDC AC1210", 1 },
  93. { "WDC AC280", 0 },
  94. { "WDC AC31000", 3 },
  95. { "WDC AC31200", 3 },
  96. { "Maxtor 7131 AT", 1 },
  97. { "Maxtor 7171 AT", 1 },
  98. { "Maxtor 7213 AT", 1 },
  99. { "Maxtor 7245 AT", 1 },
  100. { "Maxtor 7345 AT", 1 },
  101. { "Maxtor 7546 AT", 3 },
  102. { "Maxtor 7540 AV", 3 },
  103. { "SAMSUNG SHD-3121A", 1 },
  104. { "SAMSUNG SHD-3122A", 1 },
  105. { "SAMSUNG SHD-3172A", 1 },
  106. { "ST5660A", 3 },
  107. { "ST3660A", 3 },
  108. { "ST3630A", 3 },
  109. { "ST3655A", 3 },
  110. { "ST3391A", 3 },
  111. { "ST3390A", 1 },
  112. { "ST3600A", 1 },
  113. { "ST3290A", 0 },
  114. { "ST3144A", 0 },
  115. { "ST3491A", 1 }, /* reports 3, should be 1 or 2 (depending on */
  116. /* drive) according to Seagates FIND-ATA program */
  117. { "QUANTUM ELS127A", 0 },
  118. { "QUANTUM ELS170A", 0 },
  119. { "QUANTUM LPS240A", 0 },
  120. { "QUANTUM LPS210A", 3 },
  121. { "QUANTUM LPS270A", 3 },
  122. { "QUANTUM LPS365A", 3 },
  123. { "QUANTUM LPS540A", 3 },
  124. { "QUANTUM LIGHTNING 540A", 3 },
  125. { "QUANTUM LIGHTNING 730A", 3 },
  126. { "QUANTUM FIREBALL_540", 3 }, /* Older Quantum Fireballs don't work */
  127. { "QUANTUM FIREBALL_640", 3 },
  128. { "QUANTUM FIREBALL_1080", 3 },
  129. { "QUANTUM FIREBALL_1280", 3 },
  130. { NULL, 0 }
  131. };
  132. /**
  133. * ide_scan_pio_blacklist - check for a blacklisted drive
  134. * @model: Drive model string
  135. *
  136. * This routine searches the ide_pio_blacklist for an entry
  137. * matching the start/whole of the supplied model name.
  138. *
  139. * Returns -1 if no match found.
  140. * Otherwise returns the recommended PIO mode from ide_pio_blacklist[].
  141. */
  142. static int ide_scan_pio_blacklist (char *model)
  143. {
  144. struct ide_pio_info *p;
  145. for (p = ide_pio_blacklist; p->name != NULL; p++) {
  146. if (strncmp(p->name, model, strlen(p->name)) == 0)
  147. return p->pio;
  148. }
  149. return -1;
  150. }
  151. /**
  152. * ide_get_best_pio_mode - get PIO mode from drive
  153. * @drive: drive to consider
  154. * @mode_wanted: preferred mode
  155. * @max_mode: highest allowed mode
  156. *
  157. * This routine returns the recommended PIO settings for a given drive,
  158. * based on the drive->id information and the ide_pio_blacklist[].
  159. *
  160. * Drive PIO mode is auto-selected if 255 is passed as mode_wanted.
  161. * This is used by most chipset support modules when "auto-tuning".
  162. */
  163. u8 ide_get_best_pio_mode (ide_drive_t *drive, u8 mode_wanted, u8 max_mode)
  164. {
  165. int pio_mode;
  166. struct hd_driveid* id = drive->id;
  167. int overridden = 0;
  168. if (mode_wanted != 255)
  169. return min_t(u8, mode_wanted, max_mode);
  170. if ((drive->hwif->host_flags & IDE_HFLAG_PIO_NO_BLACKLIST) == 0 &&
  171. (pio_mode = ide_scan_pio_blacklist(id->model)) != -1) {
  172. printk(KERN_INFO "%s: is on PIO blacklist\n", drive->name);
  173. } else {
  174. pio_mode = id->tPIO;
  175. if (pio_mode > 2) { /* 2 is maximum allowed tPIO value */
  176. pio_mode = 2;
  177. overridden = 1;
  178. }
  179. if (id->field_valid & 2) { /* drive implements ATA2? */
  180. if (id->capability & 8) { /* IORDY supported? */
  181. if (id->eide_pio_modes & 7) {
  182. overridden = 0;
  183. if (id->eide_pio_modes & 4)
  184. pio_mode = 5;
  185. else if (id->eide_pio_modes & 2)
  186. pio_mode = 4;
  187. else
  188. pio_mode = 3;
  189. }
  190. }
  191. }
  192. if (overridden)
  193. printk(KERN_INFO "%s: tPIO > 2, assuming tPIO = 2\n",
  194. drive->name);
  195. }
  196. if (pio_mode > max_mode)
  197. pio_mode = max_mode;
  198. return pio_mode;
  199. }
  200. EXPORT_SYMBOL_GPL(ide_get_best_pio_mode);
  201. /* req_pio == "255" for auto-tune */
  202. void ide_set_pio(ide_drive_t *drive, u8 req_pio)
  203. {
  204. ide_hwif_t *hwif = drive->hwif;
  205. const struct ide_port_ops *port_ops = hwif->port_ops;
  206. u8 host_pio, pio;
  207. if (port_ops == NULL || port_ops->set_pio_mode == NULL ||
  208. (hwif->host_flags & IDE_HFLAG_NO_SET_MODE))
  209. return;
  210. BUG_ON(hwif->pio_mask == 0x00);
  211. host_pio = fls(hwif->pio_mask) - 1;
  212. pio = ide_get_best_pio_mode(drive, req_pio, host_pio);
  213. /*
  214. * TODO:
  215. * - report device max PIO mode
  216. * - check req_pio != 255 against device max PIO mode
  217. */
  218. printk(KERN_DEBUG "%s: host max PIO%d wanted PIO%d%s selected PIO%d\n",
  219. drive->name, host_pio, req_pio,
  220. req_pio == 255 ? "(auto-tune)" : "", pio);
  221. (void)ide_set_pio_mode(drive, XFER_PIO_0 + pio);
  222. }
  223. EXPORT_SYMBOL_GPL(ide_set_pio);
  224. /**
  225. * ide_toggle_bounce - handle bounce buffering
  226. * @drive: drive to update
  227. * @on: on/off boolean
  228. *
  229. * Enable or disable bounce buffering for the device. Drives move
  230. * between PIO and DMA and that changes the rules we need.
  231. */
  232. void ide_toggle_bounce(ide_drive_t *drive, int on)
  233. {
  234. u64 addr = BLK_BOUNCE_HIGH; /* dma64_addr_t */
  235. if (!PCI_DMA_BUS_IS_PHYS) {
  236. addr = BLK_BOUNCE_ANY;
  237. } else if (on && drive->media == ide_disk) {
  238. struct device *dev = drive->hwif->dev;
  239. if (dev && dev->dma_mask)
  240. addr = *dev->dma_mask;
  241. }
  242. if (drive->queue)
  243. blk_queue_bounce_limit(drive->queue, addr);
  244. }
  245. int ide_set_pio_mode(ide_drive_t *drive, const u8 mode)
  246. {
  247. ide_hwif_t *hwif = drive->hwif;
  248. const struct ide_port_ops *port_ops = hwif->port_ops;
  249. if (hwif->host_flags & IDE_HFLAG_NO_SET_MODE)
  250. return 0;
  251. if (port_ops == NULL || port_ops->set_pio_mode == NULL)
  252. return -1;
  253. /*
  254. * TODO: temporary hack for some legacy host drivers that didn't
  255. * set transfer mode on the device in ->set_pio_mode method...
  256. */
  257. if (port_ops->set_dma_mode == NULL) {
  258. port_ops->set_pio_mode(drive, mode - XFER_PIO_0);
  259. return 0;
  260. }
  261. if (hwif->host_flags & IDE_HFLAG_POST_SET_MODE) {
  262. if (ide_config_drive_speed(drive, mode))
  263. return -1;
  264. port_ops->set_pio_mode(drive, mode - XFER_PIO_0);
  265. return 0;
  266. } else {
  267. port_ops->set_pio_mode(drive, mode - XFER_PIO_0);
  268. return ide_config_drive_speed(drive, mode);
  269. }
  270. }
  271. int ide_set_dma_mode(ide_drive_t *drive, const u8 mode)
  272. {
  273. ide_hwif_t *hwif = drive->hwif;
  274. const struct ide_port_ops *port_ops = hwif->port_ops;
  275. if (hwif->host_flags & IDE_HFLAG_NO_SET_MODE)
  276. return 0;
  277. if (port_ops == NULL || port_ops->set_dma_mode == NULL)
  278. return -1;
  279. if (hwif->host_flags & IDE_HFLAG_POST_SET_MODE) {
  280. if (ide_config_drive_speed(drive, mode))
  281. return -1;
  282. port_ops->set_dma_mode(drive, mode);
  283. return 0;
  284. } else {
  285. port_ops->set_dma_mode(drive, mode);
  286. return ide_config_drive_speed(drive, mode);
  287. }
  288. }
  289. EXPORT_SYMBOL_GPL(ide_set_dma_mode);
  290. /**
  291. * ide_set_xfer_rate - set transfer rate
  292. * @drive: drive to set
  293. * @rate: speed to attempt to set
  294. *
  295. * General helper for setting the speed of an IDE device. This
  296. * function knows about user enforced limits from the configuration
  297. * which ->set_pio_mode/->set_dma_mode does not.
  298. */
  299. int ide_set_xfer_rate(ide_drive_t *drive, u8 rate)
  300. {
  301. ide_hwif_t *hwif = drive->hwif;
  302. const struct ide_port_ops *port_ops = hwif->port_ops;
  303. if (port_ops == NULL || port_ops->set_dma_mode == NULL ||
  304. (hwif->host_flags & IDE_HFLAG_NO_SET_MODE))
  305. return -1;
  306. rate = ide_rate_filter(drive, rate);
  307. if (rate >= XFER_PIO_0 && rate <= XFER_PIO_5)
  308. return ide_set_pio_mode(drive, rate);
  309. /*
  310. * TODO: transfer modes 0x00-0x07 passed from the user-space are
  311. * currently handled here which needs fixing (please note that such
  312. * case could happen iff the transfer mode has already been set on
  313. * the device by ide-proc.c::set_xfer_rate()).
  314. */
  315. if (rate < XFER_PIO_0) {
  316. if (hwif->host_flags & IDE_HFLAG_ABUSE_SET_DMA_MODE)
  317. return ide_set_dma_mode(drive, rate);
  318. else
  319. return ide_config_drive_speed(drive, rate);
  320. }
  321. return ide_set_dma_mode(drive, rate);
  322. }
  323. static void ide_dump_opcode(ide_drive_t *drive)
  324. {
  325. struct request *rq;
  326. ide_task_t *task = NULL;
  327. spin_lock(&ide_lock);
  328. rq = NULL;
  329. if (HWGROUP(drive))
  330. rq = HWGROUP(drive)->rq;
  331. spin_unlock(&ide_lock);
  332. if (!rq)
  333. return;
  334. if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE)
  335. task = rq->special;
  336. printk("ide: failed opcode was: ");
  337. if (task == NULL)
  338. printk(KERN_CONT "unknown\n");
  339. else
  340. printk(KERN_CONT "0x%02x\n", task->tf.command);
  341. }
  342. u64 ide_get_lba_addr(struct ide_taskfile *tf, int lba48)
  343. {
  344. u32 high, low;
  345. if (lba48)
  346. high = (tf->hob_lbah << 16) | (tf->hob_lbam << 8) |
  347. tf->hob_lbal;
  348. else
  349. high = tf->device & 0xf;
  350. low = (tf->lbah << 16) | (tf->lbam << 8) | tf->lbal;
  351. return ((u64)high << 24) | low;
  352. }
  353. EXPORT_SYMBOL_GPL(ide_get_lba_addr);
  354. static void ide_dump_sector(ide_drive_t *drive)
  355. {
  356. ide_task_t task;
  357. struct ide_taskfile *tf = &task.tf;
  358. int lba48 = (drive->addressing == 1) ? 1 : 0;
  359. memset(&task, 0, sizeof(task));
  360. if (lba48)
  361. task.tf_flags = IDE_TFLAG_IN_LBA | IDE_TFLAG_IN_HOB_LBA |
  362. IDE_TFLAG_LBA48;
  363. else
  364. task.tf_flags = IDE_TFLAG_IN_LBA | IDE_TFLAG_IN_DEVICE;
  365. drive->hwif->tf_read(drive, &task);
  366. if (lba48 || (tf->device & ATA_LBA))
  367. printk(", LBAsect=%llu",
  368. (unsigned long long)ide_get_lba_addr(tf, lba48));
  369. else
  370. printk(", CHS=%d/%d/%d", (tf->lbah << 8) + tf->lbam,
  371. tf->device & 0xf, tf->lbal);
  372. }
  373. static void ide_dump_ata_error(ide_drive_t *drive, u8 err)
  374. {
  375. printk("{ ");
  376. if (err & ABRT_ERR) printk("DriveStatusError ");
  377. if (err & ICRC_ERR)
  378. printk((err & ABRT_ERR) ? "BadCRC " : "BadSector ");
  379. if (err & ECC_ERR) printk("UncorrectableError ");
  380. if (err & ID_ERR) printk("SectorIdNotFound ");
  381. if (err & TRK0_ERR) printk("TrackZeroNotFound ");
  382. if (err & MARK_ERR) printk("AddrMarkNotFound ");
  383. printk("}");
  384. if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR ||
  385. (err & (ECC_ERR|ID_ERR|MARK_ERR))) {
  386. ide_dump_sector(drive);
  387. if (HWGROUP(drive) && HWGROUP(drive)->rq)
  388. printk(", sector=%llu",
  389. (unsigned long long)HWGROUP(drive)->rq->sector);
  390. }
  391. printk("\n");
  392. }
  393. static void ide_dump_atapi_error(ide_drive_t *drive, u8 err)
  394. {
  395. printk("{ ");
  396. if (err & ILI_ERR) printk("IllegalLengthIndication ");
  397. if (err & EOM_ERR) printk("EndOfMedia ");
  398. if (err & ABRT_ERR) printk("AbortedCommand ");
  399. if (err & MCR_ERR) printk("MediaChangeRequested ");
  400. if (err & LFS_ERR) printk("LastFailedSense=0x%02x ",
  401. (err & LFS_ERR) >> 4);
  402. printk("}\n");
  403. }
  404. /**
  405. * ide_dump_status - translate ATA/ATAPI error
  406. * @drive: drive that status applies to
  407. * @msg: text message to print
  408. * @stat: status byte to decode
  409. *
  410. * Error reporting, in human readable form (luxurious, but a memory hog).
  411. * Combines the drive name, message and status byte to provide a
  412. * user understandable explanation of the device error.
  413. */
  414. u8 ide_dump_status(ide_drive_t *drive, const char *msg, u8 stat)
  415. {
  416. unsigned long flags;
  417. u8 err = 0;
  418. local_irq_save(flags);
  419. printk("%s: %s: status=0x%02x { ", drive->name, msg, stat);
  420. if (stat & BUSY_STAT)
  421. printk("Busy ");
  422. else {
  423. if (stat & READY_STAT) printk("DriveReady ");
  424. if (stat & WRERR_STAT) printk("DeviceFault ");
  425. if (stat & SEEK_STAT) printk("SeekComplete ");
  426. if (stat & DRQ_STAT) printk("DataRequest ");
  427. if (stat & ECC_STAT) printk("CorrectedError ");
  428. if (stat & INDEX_STAT) printk("Index ");
  429. if (stat & ERR_STAT) printk("Error ");
  430. }
  431. printk("}\n");
  432. if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
  433. err = ide_read_error(drive);
  434. printk("%s: %s: error=0x%02x ", drive->name, msg, err);
  435. if (drive->media == ide_disk)
  436. ide_dump_ata_error(drive, err);
  437. else
  438. ide_dump_atapi_error(drive, err);
  439. }
  440. ide_dump_opcode(drive);
  441. local_irq_restore(flags);
  442. return err;
  443. }
  444. EXPORT_SYMBOL(ide_dump_status);