ide-lib.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. #include <linux/module.h>
  2. #include <linux/types.h>
  3. #include <linux/string.h>
  4. #include <linux/kernel.h>
  5. #include <linux/timer.h>
  6. #include <linux/mm.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/major.h>
  9. #include <linux/errno.h>
  10. #include <linux/genhd.h>
  11. #include <linux/blkpg.h>
  12. #include <linux/slab.h>
  13. #include <linux/pci.h>
  14. #include <linux/delay.h>
  15. #include <linux/hdreg.h>
  16. #include <linux/ide.h>
  17. #include <linux/bitops.h>
  18. #include <asm/byteorder.h>
  19. #include <asm/irq.h>
  20. #include <asm/uaccess.h>
  21. #include <asm/io.h>
  22. static const char *udma_str[] =
  23. { "UDMA/16", "UDMA/25", "UDMA/33", "UDMA/44",
  24. "UDMA/66", "UDMA/100", "UDMA/133", "UDMA7" };
  25. static const char *mwdma_str[] =
  26. { "MWDMA0", "MWDMA1", "MWDMA2" };
  27. static const char *swdma_str[] =
  28. { "SWDMA0", "SWDMA1", "SWDMA2" };
  29. static const char *pio_str[] =
  30. { "PIO0", "PIO1", "PIO2", "PIO3", "PIO4", "PIO5" };
  31. /**
  32. * ide_xfer_verbose - return IDE mode names
  33. * @mode: transfer mode
  34. *
  35. * Returns a constant string giving the name of the mode
  36. * requested.
  37. */
  38. const char *ide_xfer_verbose(u8 mode)
  39. {
  40. const char *s;
  41. u8 i = mode & 0xf;
  42. if (mode >= XFER_UDMA_0 && mode <= XFER_UDMA_7)
  43. s = udma_str[i];
  44. else if (mode >= XFER_MW_DMA_0 && mode <= XFER_MW_DMA_2)
  45. s = mwdma_str[i];
  46. else if (mode >= XFER_SW_DMA_0 && mode <= XFER_SW_DMA_2)
  47. s = swdma_str[i];
  48. else if (mode >= XFER_PIO_0 && mode <= XFER_PIO_5)
  49. s = pio_str[i & 0x7];
  50. else if (mode == XFER_PIO_SLOW)
  51. s = "PIO SLOW";
  52. else
  53. s = "XFER ERROR";
  54. return s;
  55. }
  56. EXPORT_SYMBOL(ide_xfer_verbose);
  57. /**
  58. * ide_rate_filter - filter transfer mode
  59. * @drive: IDE device
  60. * @speed: desired speed
  61. *
  62. * Given the available transfer modes this function returns
  63. * the best available speed at or below the speed requested.
  64. *
  65. * TODO: check device PIO capabilities
  66. */
  67. static u8 ide_rate_filter(ide_drive_t *drive, u8 speed)
  68. {
  69. ide_hwif_t *hwif = drive->hwif;
  70. u8 mode = ide_find_dma_mode(drive, speed);
  71. if (mode == 0) {
  72. if (hwif->pio_mask)
  73. mode = fls(hwif->pio_mask) - 1 + XFER_PIO_0;
  74. else
  75. mode = XFER_PIO_4;
  76. }
  77. // printk("%s: mode 0x%02x, speed 0x%02x\n", __FUNCTION__, mode, speed);
  78. return min(speed, mode);
  79. }
  80. /*
  81. * Standard (generic) timings for PIO modes, from ATA2 specification.
  82. * These timings are for access to the IDE data port register *only*.
  83. * Some drives may specify a mode, while also specifying a different
  84. * value for cycle_time (from drive identification data).
  85. */
  86. const ide_pio_timings_t ide_pio_timings[6] = {
  87. { 70, 165, 600 }, /* PIO Mode 0 */
  88. { 50, 125, 383 }, /* PIO Mode 1 */
  89. { 30, 100, 240 }, /* PIO Mode 2 */
  90. { 30, 80, 180 }, /* PIO Mode 3 with IORDY */
  91. { 25, 70, 120 }, /* PIO Mode 4 with IORDY */
  92. { 20, 50, 100 } /* PIO Mode 5 with IORDY (nonstandard) */
  93. };
  94. EXPORT_SYMBOL_GPL(ide_pio_timings);
  95. /*
  96. * Shared data/functions for determining best PIO mode for an IDE drive.
  97. * Most of this stuff originally lived in cmd640.c, and changes to the
  98. * ide_pio_blacklist[] table should be made with EXTREME CAUTION to avoid
  99. * breaking the fragile cmd640.c support.
  100. */
  101. /*
  102. * Black list. Some drives incorrectly report their maximal PIO mode,
  103. * at least in respect to CMD640. Here we keep info on some known drives.
  104. */
  105. static struct ide_pio_info {
  106. const char *name;
  107. int pio;
  108. } ide_pio_blacklist [] = {
  109. { "Conner Peripherals 540MB - CFS540A", 3 },
  110. { "WDC AC2700", 3 },
  111. { "WDC AC2540", 3 },
  112. { "WDC AC2420", 3 },
  113. { "WDC AC2340", 3 },
  114. { "WDC AC2250", 0 },
  115. { "WDC AC2200", 0 },
  116. { "WDC AC21200", 4 },
  117. { "WDC AC2120", 0 },
  118. { "WDC AC2850", 3 },
  119. { "WDC AC1270", 3 },
  120. { "WDC AC1170", 1 },
  121. { "WDC AC1210", 1 },
  122. { "WDC AC280", 0 },
  123. { "WDC AC31000", 3 },
  124. { "WDC AC31200", 3 },
  125. { "Maxtor 7131 AT", 1 },
  126. { "Maxtor 7171 AT", 1 },
  127. { "Maxtor 7213 AT", 1 },
  128. { "Maxtor 7245 AT", 1 },
  129. { "Maxtor 7345 AT", 1 },
  130. { "Maxtor 7546 AT", 3 },
  131. { "Maxtor 7540 AV", 3 },
  132. { "SAMSUNG SHD-3121A", 1 },
  133. { "SAMSUNG SHD-3122A", 1 },
  134. { "SAMSUNG SHD-3172A", 1 },
  135. { "ST5660A", 3 },
  136. { "ST3660A", 3 },
  137. { "ST3630A", 3 },
  138. { "ST3655A", 3 },
  139. { "ST3391A", 3 },
  140. { "ST3390A", 1 },
  141. { "ST3600A", 1 },
  142. { "ST3290A", 0 },
  143. { "ST3144A", 0 },
  144. { "ST3491A", 1 }, /* reports 3, should be 1 or 2 (depending on */
  145. /* drive) according to Seagates FIND-ATA program */
  146. { "QUANTUM ELS127A", 0 },
  147. { "QUANTUM ELS170A", 0 },
  148. { "QUANTUM LPS240A", 0 },
  149. { "QUANTUM LPS210A", 3 },
  150. { "QUANTUM LPS270A", 3 },
  151. { "QUANTUM LPS365A", 3 },
  152. { "QUANTUM LPS540A", 3 },
  153. { "QUANTUM LIGHTNING 540A", 3 },
  154. { "QUANTUM LIGHTNING 730A", 3 },
  155. { "QUANTUM FIREBALL_540", 3 }, /* Older Quantum Fireballs don't work */
  156. { "QUANTUM FIREBALL_640", 3 },
  157. { "QUANTUM FIREBALL_1080", 3 },
  158. { "QUANTUM FIREBALL_1280", 3 },
  159. { NULL, 0 }
  160. };
  161. /**
  162. * ide_scan_pio_blacklist - check for a blacklisted drive
  163. * @model: Drive model string
  164. *
  165. * This routine searches the ide_pio_blacklist for an entry
  166. * matching the start/whole of the supplied model name.
  167. *
  168. * Returns -1 if no match found.
  169. * Otherwise returns the recommended PIO mode from ide_pio_blacklist[].
  170. */
  171. static int ide_scan_pio_blacklist (char *model)
  172. {
  173. struct ide_pio_info *p;
  174. for (p = ide_pio_blacklist; p->name != NULL; p++) {
  175. if (strncmp(p->name, model, strlen(p->name)) == 0)
  176. return p->pio;
  177. }
  178. return -1;
  179. }
  180. unsigned int ide_pio_cycle_time(ide_drive_t *drive, u8 pio)
  181. {
  182. struct hd_driveid *id = drive->id;
  183. int cycle_time = 0;
  184. if (id->field_valid & 2) {
  185. if (id->capability & 8)
  186. cycle_time = id->eide_pio_iordy;
  187. else
  188. cycle_time = id->eide_pio;
  189. }
  190. /* conservative "downgrade" for all pre-ATA2 drives */
  191. if (pio < 3) {
  192. if (cycle_time && cycle_time < ide_pio_timings[pio].cycle_time)
  193. cycle_time = 0; /* use standard timing */
  194. }
  195. return cycle_time ? cycle_time : ide_pio_timings[pio].cycle_time;
  196. }
  197. EXPORT_SYMBOL_GPL(ide_pio_cycle_time);
  198. /**
  199. * ide_get_best_pio_mode - get PIO mode from drive
  200. * @drive: drive to consider
  201. * @mode_wanted: preferred mode
  202. * @max_mode: highest allowed mode
  203. *
  204. * This routine returns the recommended PIO settings for a given drive,
  205. * based on the drive->id information and the ide_pio_blacklist[].
  206. *
  207. * Drive PIO mode is auto-selected if 255 is passed as mode_wanted.
  208. * This is used by most chipset support modules when "auto-tuning".
  209. */
  210. u8 ide_get_best_pio_mode (ide_drive_t *drive, u8 mode_wanted, u8 max_mode)
  211. {
  212. int pio_mode;
  213. struct hd_driveid* id = drive->id;
  214. int overridden = 0;
  215. if (mode_wanted != 255)
  216. return min_t(u8, mode_wanted, max_mode);
  217. if ((drive->hwif->host_flags & IDE_HFLAG_PIO_NO_BLACKLIST) == 0 &&
  218. (pio_mode = ide_scan_pio_blacklist(id->model)) != -1) {
  219. printk(KERN_INFO "%s: is on PIO blacklist\n", drive->name);
  220. } else {
  221. pio_mode = id->tPIO;
  222. if (pio_mode > 2) { /* 2 is maximum allowed tPIO value */
  223. pio_mode = 2;
  224. overridden = 1;
  225. }
  226. if (id->field_valid & 2) { /* drive implements ATA2? */
  227. if (id->capability & 8) { /* IORDY supported? */
  228. if (id->eide_pio_modes & 7) {
  229. overridden = 0;
  230. if (id->eide_pio_modes & 4)
  231. pio_mode = 5;
  232. else if (id->eide_pio_modes & 2)
  233. pio_mode = 4;
  234. else
  235. pio_mode = 3;
  236. }
  237. }
  238. }
  239. if (overridden)
  240. printk(KERN_INFO "%s: tPIO > 2, assuming tPIO = 2\n",
  241. drive->name);
  242. /*
  243. * Conservative "downgrade" for all pre-ATA2 drives
  244. */
  245. if ((drive->hwif->host_flags & IDE_HFLAG_PIO_NO_DOWNGRADE) == 0 &&
  246. pio_mode && pio_mode < 4) {
  247. pio_mode--;
  248. printk(KERN_INFO "%s: applying conservative "
  249. "PIO \"downgrade\"\n", drive->name);
  250. }
  251. }
  252. if (pio_mode > max_mode)
  253. pio_mode = max_mode;
  254. return pio_mode;
  255. }
  256. EXPORT_SYMBOL_GPL(ide_get_best_pio_mode);
  257. /* req_pio == "255" for auto-tune */
  258. void ide_set_pio(ide_drive_t *drive, u8 req_pio)
  259. {
  260. ide_hwif_t *hwif = drive->hwif;
  261. u8 host_pio, pio;
  262. if (hwif->set_pio_mode == NULL)
  263. return;
  264. BUG_ON(hwif->pio_mask == 0x00);
  265. host_pio = fls(hwif->pio_mask) - 1;
  266. pio = ide_get_best_pio_mode(drive, req_pio, host_pio);
  267. /*
  268. * TODO:
  269. * - report device max PIO mode
  270. * - check req_pio != 255 against device max PIO mode
  271. */
  272. printk(KERN_DEBUG "%s: host max PIO%d wanted PIO%d%s selected PIO%d\n",
  273. drive->name, host_pio, req_pio,
  274. req_pio == 255 ? "(auto-tune)" : "", pio);
  275. (void)ide_set_pio_mode(drive, XFER_PIO_0 + pio);
  276. }
  277. EXPORT_SYMBOL_GPL(ide_set_pio);
  278. /**
  279. * ide_toggle_bounce - handle bounce buffering
  280. * @drive: drive to update
  281. * @on: on/off boolean
  282. *
  283. * Enable or disable bounce buffering for the device. Drives move
  284. * between PIO and DMA and that changes the rules we need.
  285. */
  286. void ide_toggle_bounce(ide_drive_t *drive, int on)
  287. {
  288. u64 addr = BLK_BOUNCE_HIGH; /* dma64_addr_t */
  289. if (!PCI_DMA_BUS_IS_PHYS) {
  290. addr = BLK_BOUNCE_ANY;
  291. } else if (on && drive->media == ide_disk) {
  292. struct device *dev = drive->hwif->dev;
  293. if (dev && dev->dma_mask)
  294. addr = *dev->dma_mask;
  295. }
  296. if (drive->queue)
  297. blk_queue_bounce_limit(drive->queue, addr);
  298. }
  299. int ide_set_pio_mode(ide_drive_t *drive, const u8 mode)
  300. {
  301. ide_hwif_t *hwif = drive->hwif;
  302. if (hwif->set_pio_mode == NULL)
  303. return -1;
  304. /*
  305. * TODO: temporary hack for some legacy host drivers that didn't
  306. * set transfer mode on the device in ->set_pio_mode method...
  307. */
  308. if (hwif->set_dma_mode == NULL) {
  309. hwif->set_pio_mode(drive, mode - XFER_PIO_0);
  310. return 0;
  311. }
  312. if (hwif->host_flags & IDE_HFLAG_POST_SET_MODE) {
  313. if (ide_config_drive_speed(drive, mode))
  314. return -1;
  315. hwif->set_pio_mode(drive, mode - XFER_PIO_0);
  316. return 0;
  317. } else {
  318. hwif->set_pio_mode(drive, mode - XFER_PIO_0);
  319. return ide_config_drive_speed(drive, mode);
  320. }
  321. }
  322. int ide_set_dma_mode(ide_drive_t *drive, const u8 mode)
  323. {
  324. ide_hwif_t *hwif = drive->hwif;
  325. if (hwif->set_dma_mode == NULL)
  326. return -1;
  327. if (hwif->host_flags & IDE_HFLAG_POST_SET_MODE) {
  328. if (ide_config_drive_speed(drive, mode))
  329. return -1;
  330. hwif->set_dma_mode(drive, mode);
  331. return 0;
  332. } else {
  333. hwif->set_dma_mode(drive, mode);
  334. return ide_config_drive_speed(drive, mode);
  335. }
  336. }
  337. EXPORT_SYMBOL_GPL(ide_set_dma_mode);
  338. /**
  339. * ide_set_xfer_rate - set transfer rate
  340. * @drive: drive to set
  341. * @rate: speed to attempt to set
  342. *
  343. * General helper for setting the speed of an IDE device. This
  344. * function knows about user enforced limits from the configuration
  345. * which ->set_pio_mode/->set_dma_mode does not.
  346. */
  347. int ide_set_xfer_rate(ide_drive_t *drive, u8 rate)
  348. {
  349. ide_hwif_t *hwif = drive->hwif;
  350. if (hwif->set_dma_mode == NULL)
  351. return -1;
  352. rate = ide_rate_filter(drive, rate);
  353. if (rate >= XFER_PIO_0 && rate <= XFER_PIO_5)
  354. return ide_set_pio_mode(drive, rate);
  355. /*
  356. * TODO: transfer modes 0x00-0x07 passed from the user-space are
  357. * currently handled here which needs fixing (please note that such
  358. * case could happen iff the transfer mode has already been set on
  359. * the device by ide-proc.c::set_xfer_rate()).
  360. */
  361. if (rate < XFER_PIO_0) {
  362. if (hwif->host_flags & IDE_HFLAG_ABUSE_SET_DMA_MODE)
  363. return ide_set_dma_mode(drive, rate);
  364. else
  365. return ide_config_drive_speed(drive, rate);
  366. }
  367. return ide_set_dma_mode(drive, rate);
  368. }
  369. static void ide_dump_opcode(ide_drive_t *drive)
  370. {
  371. struct request *rq;
  372. ide_task_t *task = NULL;
  373. spin_lock(&ide_lock);
  374. rq = NULL;
  375. if (HWGROUP(drive))
  376. rq = HWGROUP(drive)->rq;
  377. spin_unlock(&ide_lock);
  378. if (!rq)
  379. return;
  380. if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE)
  381. task = rq->special;
  382. printk("ide: failed opcode was: ");
  383. if (task == NULL)
  384. printk(KERN_CONT "unknown\n");
  385. else
  386. printk(KERN_CONT "0x%02x\n", task->tf.command);
  387. }
  388. u64 ide_get_lba_addr(struct ide_taskfile *tf, int lba48)
  389. {
  390. u32 high, low;
  391. if (lba48)
  392. high = (tf->hob_lbah << 16) | (tf->hob_lbam << 8) |
  393. tf->hob_lbal;
  394. else
  395. high = tf->device & 0xf;
  396. low = (tf->lbah << 16) | (tf->lbam << 8) | tf->lbal;
  397. return ((u64)high << 24) | low;
  398. }
  399. EXPORT_SYMBOL_GPL(ide_get_lba_addr);
  400. static void ide_dump_sector(ide_drive_t *drive)
  401. {
  402. ide_task_t task;
  403. struct ide_taskfile *tf = &task.tf;
  404. int lba48 = (drive->addressing == 1) ? 1 : 0;
  405. memset(&task, 0, sizeof(task));
  406. if (lba48)
  407. task.tf_flags = IDE_TFLAG_IN_LBA | IDE_TFLAG_IN_HOB_LBA |
  408. IDE_TFLAG_LBA48;
  409. else
  410. task.tf_flags = IDE_TFLAG_IN_LBA | IDE_TFLAG_IN_DEVICE;
  411. ide_tf_read(drive, &task);
  412. if (lba48 || (tf->device & ATA_LBA))
  413. printk(", LBAsect=%llu",
  414. (unsigned long long)ide_get_lba_addr(tf, lba48));
  415. else
  416. printk(", CHS=%d/%d/%d", (tf->lbah << 8) + tf->lbam,
  417. tf->device & 0xf, tf->lbal);
  418. }
  419. static void ide_dump_ata_error(ide_drive_t *drive, u8 err)
  420. {
  421. printk("{ ");
  422. if (err & ABRT_ERR) printk("DriveStatusError ");
  423. if (err & ICRC_ERR)
  424. printk((err & ABRT_ERR) ? "BadCRC " : "BadSector ");
  425. if (err & ECC_ERR) printk("UncorrectableError ");
  426. if (err & ID_ERR) printk("SectorIdNotFound ");
  427. if (err & TRK0_ERR) printk("TrackZeroNotFound ");
  428. if (err & MARK_ERR) printk("AddrMarkNotFound ");
  429. printk("}");
  430. if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR ||
  431. (err & (ECC_ERR|ID_ERR|MARK_ERR))) {
  432. ide_dump_sector(drive);
  433. if (HWGROUP(drive) && HWGROUP(drive)->rq)
  434. printk(", sector=%llu",
  435. (unsigned long long)HWGROUP(drive)->rq->sector);
  436. }
  437. printk("\n");
  438. }
  439. static void ide_dump_atapi_error(ide_drive_t *drive, u8 err)
  440. {
  441. printk("{ ");
  442. if (err & ILI_ERR) printk("IllegalLengthIndication ");
  443. if (err & EOM_ERR) printk("EndOfMedia ");
  444. if (err & ABRT_ERR) printk("AbortedCommand ");
  445. if (err & MCR_ERR) printk("MediaChangeRequested ");
  446. if (err & LFS_ERR) printk("LastFailedSense=0x%02x ",
  447. (err & LFS_ERR) >> 4);
  448. printk("}\n");
  449. }
  450. /**
  451. * ide_dump_status - translate ATA/ATAPI error
  452. * @drive: drive that status applies to
  453. * @msg: text message to print
  454. * @stat: status byte to decode
  455. *
  456. * Error reporting, in human readable form (luxurious, but a memory hog).
  457. * Combines the drive name, message and status byte to provide a
  458. * user understandable explanation of the device error.
  459. */
  460. u8 ide_dump_status(ide_drive_t *drive, const char *msg, u8 stat)
  461. {
  462. unsigned long flags;
  463. u8 err = 0;
  464. local_irq_save(flags);
  465. printk("%s: %s: status=0x%02x { ", drive->name, msg, stat);
  466. if (stat & BUSY_STAT)
  467. printk("Busy ");
  468. else {
  469. if (stat & READY_STAT) printk("DriveReady ");
  470. if (stat & WRERR_STAT) printk("DeviceFault ");
  471. if (stat & SEEK_STAT) printk("SeekComplete ");
  472. if (stat & DRQ_STAT) printk("DataRequest ");
  473. if (stat & ECC_STAT) printk("CorrectedError ");
  474. if (stat & INDEX_STAT) printk("Index ");
  475. if (stat & ERR_STAT) printk("Error ");
  476. }
  477. printk("}\n");
  478. if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
  479. err = ide_read_error(drive);
  480. printk("%s: %s: error=0x%02x ", drive->name, msg, err);
  481. if (drive->media == ide_disk)
  482. ide_dump_ata_error(drive, err);
  483. else
  484. ide_dump_atapi_error(drive, err);
  485. }
  486. ide_dump_opcode(drive);
  487. local_irq_restore(flags);
  488. return err;
  489. }
  490. EXPORT_SYMBOL(ide_dump_status);