ide-lib.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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. if (pio_mode > max_mode)
  244. pio_mode = max_mode;
  245. return pio_mode;
  246. }
  247. EXPORT_SYMBOL_GPL(ide_get_best_pio_mode);
  248. /* req_pio == "255" for auto-tune */
  249. void ide_set_pio(ide_drive_t *drive, u8 req_pio)
  250. {
  251. ide_hwif_t *hwif = drive->hwif;
  252. u8 host_pio, pio;
  253. if (hwif->set_pio_mode == NULL)
  254. return;
  255. BUG_ON(hwif->pio_mask == 0x00);
  256. host_pio = fls(hwif->pio_mask) - 1;
  257. pio = ide_get_best_pio_mode(drive, req_pio, host_pio);
  258. /*
  259. * TODO:
  260. * - report device max PIO mode
  261. * - check req_pio != 255 against device max PIO mode
  262. */
  263. printk(KERN_DEBUG "%s: host max PIO%d wanted PIO%d%s selected PIO%d\n",
  264. drive->name, host_pio, req_pio,
  265. req_pio == 255 ? "(auto-tune)" : "", pio);
  266. (void)ide_set_pio_mode(drive, XFER_PIO_0 + pio);
  267. }
  268. EXPORT_SYMBOL_GPL(ide_set_pio);
  269. /**
  270. * ide_toggle_bounce - handle bounce buffering
  271. * @drive: drive to update
  272. * @on: on/off boolean
  273. *
  274. * Enable or disable bounce buffering for the device. Drives move
  275. * between PIO and DMA and that changes the rules we need.
  276. */
  277. void ide_toggle_bounce(ide_drive_t *drive, int on)
  278. {
  279. u64 addr = BLK_BOUNCE_HIGH; /* dma64_addr_t */
  280. if (!PCI_DMA_BUS_IS_PHYS) {
  281. addr = BLK_BOUNCE_ANY;
  282. } else if (on && drive->media == ide_disk) {
  283. struct device *dev = drive->hwif->dev;
  284. if (dev && dev->dma_mask)
  285. addr = *dev->dma_mask;
  286. }
  287. if (drive->queue)
  288. blk_queue_bounce_limit(drive->queue, addr);
  289. }
  290. int ide_set_pio_mode(ide_drive_t *drive, const u8 mode)
  291. {
  292. ide_hwif_t *hwif = drive->hwif;
  293. if (hwif->set_pio_mode == NULL)
  294. return -1;
  295. /*
  296. * TODO: temporary hack for some legacy host drivers that didn't
  297. * set transfer mode on the device in ->set_pio_mode method...
  298. */
  299. if (hwif->set_dma_mode == NULL) {
  300. hwif->set_pio_mode(drive, mode - XFER_PIO_0);
  301. return 0;
  302. }
  303. if (hwif->host_flags & IDE_HFLAG_POST_SET_MODE) {
  304. if (ide_config_drive_speed(drive, mode))
  305. return -1;
  306. hwif->set_pio_mode(drive, mode - XFER_PIO_0);
  307. return 0;
  308. } else {
  309. hwif->set_pio_mode(drive, mode - XFER_PIO_0);
  310. return ide_config_drive_speed(drive, mode);
  311. }
  312. }
  313. int ide_set_dma_mode(ide_drive_t *drive, const u8 mode)
  314. {
  315. ide_hwif_t *hwif = drive->hwif;
  316. if (hwif->set_dma_mode == NULL)
  317. return -1;
  318. if (hwif->host_flags & IDE_HFLAG_POST_SET_MODE) {
  319. if (ide_config_drive_speed(drive, mode))
  320. return -1;
  321. hwif->set_dma_mode(drive, mode);
  322. return 0;
  323. } else {
  324. hwif->set_dma_mode(drive, mode);
  325. return ide_config_drive_speed(drive, mode);
  326. }
  327. }
  328. EXPORT_SYMBOL_GPL(ide_set_dma_mode);
  329. /**
  330. * ide_set_xfer_rate - set transfer rate
  331. * @drive: drive to set
  332. * @rate: speed to attempt to set
  333. *
  334. * General helper for setting the speed of an IDE device. This
  335. * function knows about user enforced limits from the configuration
  336. * which ->set_pio_mode/->set_dma_mode does not.
  337. */
  338. int ide_set_xfer_rate(ide_drive_t *drive, u8 rate)
  339. {
  340. ide_hwif_t *hwif = drive->hwif;
  341. if (hwif->set_dma_mode == NULL)
  342. return -1;
  343. rate = ide_rate_filter(drive, rate);
  344. if (rate >= XFER_PIO_0 && rate <= XFER_PIO_5)
  345. return ide_set_pio_mode(drive, rate);
  346. /*
  347. * TODO: transfer modes 0x00-0x07 passed from the user-space are
  348. * currently handled here which needs fixing (please note that such
  349. * case could happen iff the transfer mode has already been set on
  350. * the device by ide-proc.c::set_xfer_rate()).
  351. */
  352. if (rate < XFER_PIO_0) {
  353. if (hwif->host_flags & IDE_HFLAG_ABUSE_SET_DMA_MODE)
  354. return ide_set_dma_mode(drive, rate);
  355. else
  356. return ide_config_drive_speed(drive, rate);
  357. }
  358. return ide_set_dma_mode(drive, rate);
  359. }
  360. static void ide_dump_opcode(ide_drive_t *drive)
  361. {
  362. struct request *rq;
  363. ide_task_t *task = NULL;
  364. spin_lock(&ide_lock);
  365. rq = NULL;
  366. if (HWGROUP(drive))
  367. rq = HWGROUP(drive)->rq;
  368. spin_unlock(&ide_lock);
  369. if (!rq)
  370. return;
  371. if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE)
  372. task = rq->special;
  373. printk("ide: failed opcode was: ");
  374. if (task == NULL)
  375. printk(KERN_CONT "unknown\n");
  376. else
  377. printk(KERN_CONT "0x%02x\n", task->tf.command);
  378. }
  379. u64 ide_get_lba_addr(struct ide_taskfile *tf, int lba48)
  380. {
  381. u32 high, low;
  382. if (lba48)
  383. high = (tf->hob_lbah << 16) | (tf->hob_lbam << 8) |
  384. tf->hob_lbal;
  385. else
  386. high = tf->device & 0xf;
  387. low = (tf->lbah << 16) | (tf->lbam << 8) | tf->lbal;
  388. return ((u64)high << 24) | low;
  389. }
  390. EXPORT_SYMBOL_GPL(ide_get_lba_addr);
  391. static void ide_dump_sector(ide_drive_t *drive)
  392. {
  393. ide_task_t task;
  394. struct ide_taskfile *tf = &task.tf;
  395. int lba48 = (drive->addressing == 1) ? 1 : 0;
  396. memset(&task, 0, sizeof(task));
  397. if (lba48)
  398. task.tf_flags = IDE_TFLAG_IN_LBA | IDE_TFLAG_IN_HOB_LBA |
  399. IDE_TFLAG_LBA48;
  400. else
  401. task.tf_flags = IDE_TFLAG_IN_LBA | IDE_TFLAG_IN_DEVICE;
  402. ide_tf_read(drive, &task);
  403. if (lba48 || (tf->device & ATA_LBA))
  404. printk(", LBAsect=%llu",
  405. (unsigned long long)ide_get_lba_addr(tf, lba48));
  406. else
  407. printk(", CHS=%d/%d/%d", (tf->lbah << 8) + tf->lbam,
  408. tf->device & 0xf, tf->lbal);
  409. }
  410. static void ide_dump_ata_error(ide_drive_t *drive, u8 err)
  411. {
  412. printk("{ ");
  413. if (err & ABRT_ERR) printk("DriveStatusError ");
  414. if (err & ICRC_ERR)
  415. printk((err & ABRT_ERR) ? "BadCRC " : "BadSector ");
  416. if (err & ECC_ERR) printk("UncorrectableError ");
  417. if (err & ID_ERR) printk("SectorIdNotFound ");
  418. if (err & TRK0_ERR) printk("TrackZeroNotFound ");
  419. if (err & MARK_ERR) printk("AddrMarkNotFound ");
  420. printk("}");
  421. if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR ||
  422. (err & (ECC_ERR|ID_ERR|MARK_ERR))) {
  423. ide_dump_sector(drive);
  424. if (HWGROUP(drive) && HWGROUP(drive)->rq)
  425. printk(", sector=%llu",
  426. (unsigned long long)HWGROUP(drive)->rq->sector);
  427. }
  428. printk("\n");
  429. }
  430. static void ide_dump_atapi_error(ide_drive_t *drive, u8 err)
  431. {
  432. printk("{ ");
  433. if (err & ILI_ERR) printk("IllegalLengthIndication ");
  434. if (err & EOM_ERR) printk("EndOfMedia ");
  435. if (err & ABRT_ERR) printk("AbortedCommand ");
  436. if (err & MCR_ERR) printk("MediaChangeRequested ");
  437. if (err & LFS_ERR) printk("LastFailedSense=0x%02x ",
  438. (err & LFS_ERR) >> 4);
  439. printk("}\n");
  440. }
  441. /**
  442. * ide_dump_status - translate ATA/ATAPI error
  443. * @drive: drive that status applies to
  444. * @msg: text message to print
  445. * @stat: status byte to decode
  446. *
  447. * Error reporting, in human readable form (luxurious, but a memory hog).
  448. * Combines the drive name, message and status byte to provide a
  449. * user understandable explanation of the device error.
  450. */
  451. u8 ide_dump_status(ide_drive_t *drive, const char *msg, u8 stat)
  452. {
  453. unsigned long flags;
  454. u8 err = 0;
  455. local_irq_save(flags);
  456. printk("%s: %s: status=0x%02x { ", drive->name, msg, stat);
  457. if (stat & BUSY_STAT)
  458. printk("Busy ");
  459. else {
  460. if (stat & READY_STAT) printk("DriveReady ");
  461. if (stat & WRERR_STAT) printk("DeviceFault ");
  462. if (stat & SEEK_STAT) printk("SeekComplete ");
  463. if (stat & DRQ_STAT) printk("DataRequest ");
  464. if (stat & ECC_STAT) printk("CorrectedError ");
  465. if (stat & INDEX_STAT) printk("Index ");
  466. if (stat & ERR_STAT) printk("Error ");
  467. }
  468. printk("}\n");
  469. if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
  470. err = ide_read_error(drive);
  471. printk("%s: %s: error=0x%02x ", drive->name, msg, err);
  472. if (drive->media == ide_disk)
  473. ide_dump_ata_error(drive, err);
  474. else
  475. ide_dump_atapi_error(drive, err);
  476. }
  477. ide_dump_opcode(drive);
  478. local_irq_restore(flags);
  479. return err;
  480. }
  481. EXPORT_SYMBOL(ide_dump_status);