ide-lib.c 15 KB

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