ide-lib.c 15 KB

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