ide-lib.c 15 KB

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