ide-lib.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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. if (HWIF(drive)->pci_dev)
  311. addr = HWIF(drive)->pci_dev->dma_mask;
  312. }
  313. if (drive->queue)
  314. blk_queue_bounce_limit(drive->queue, addr);
  315. }
  316. int ide_set_pio_mode(ide_drive_t *drive, const u8 mode)
  317. {
  318. ide_hwif_t *hwif = drive->hwif;
  319. if (hwif->set_pio_mode == NULL)
  320. return -1;
  321. /*
  322. * TODO: temporary hack for some legacy host drivers that didn't
  323. * set transfer mode on the device in ->set_pio_mode method...
  324. */
  325. if (hwif->set_dma_mode == NULL) {
  326. hwif->set_pio_mode(drive, mode - XFER_PIO_0);
  327. return 0;
  328. }
  329. if (hwif->host_flags & IDE_HFLAG_POST_SET_MODE) {
  330. if (ide_config_drive_speed(drive, mode))
  331. return -1;
  332. hwif->set_pio_mode(drive, mode - XFER_PIO_0);
  333. return 0;
  334. } else {
  335. hwif->set_pio_mode(drive, mode - XFER_PIO_0);
  336. return ide_config_drive_speed(drive, mode);
  337. }
  338. }
  339. int ide_set_dma_mode(ide_drive_t *drive, const u8 mode)
  340. {
  341. ide_hwif_t *hwif = drive->hwif;
  342. if (hwif->set_dma_mode == NULL)
  343. return -1;
  344. if (hwif->host_flags & IDE_HFLAG_POST_SET_MODE) {
  345. if (ide_config_drive_speed(drive, mode))
  346. return -1;
  347. hwif->set_dma_mode(drive, mode);
  348. return 0;
  349. } else {
  350. hwif->set_dma_mode(drive, mode);
  351. return ide_config_drive_speed(drive, mode);
  352. }
  353. }
  354. EXPORT_SYMBOL_GPL(ide_set_dma_mode);
  355. /**
  356. * ide_set_xfer_rate - set transfer rate
  357. * @drive: drive to set
  358. * @rate: speed to attempt to set
  359. *
  360. * General helper for setting the speed of an IDE device. This
  361. * function knows about user enforced limits from the configuration
  362. * which ->set_pio_mode/->set_dma_mode does not.
  363. */
  364. int ide_set_xfer_rate(ide_drive_t *drive, u8 rate)
  365. {
  366. ide_hwif_t *hwif = drive->hwif;
  367. if (hwif->set_dma_mode == NULL)
  368. return -1;
  369. rate = ide_rate_filter(drive, rate);
  370. if (rate >= XFER_PIO_0 && rate <= XFER_PIO_5)
  371. return ide_set_pio_mode(drive, rate);
  372. /*
  373. * TODO: transfer modes 0x00-0x07 passed from the user-space are
  374. * currently handled here which needs fixing (please note that such
  375. * case could happen iff the transfer mode has already been set on
  376. * the device by ide-proc.c::set_xfer_rate()).
  377. */
  378. if (rate < XFER_PIO_0) {
  379. if (hwif->host_flags & IDE_HFLAG_ABUSE_SET_DMA_MODE)
  380. return ide_set_dma_mode(drive, rate);
  381. else
  382. return ide_config_drive_speed(drive, rate);
  383. }
  384. return ide_set_dma_mode(drive, rate);
  385. }
  386. static void ide_dump_opcode(ide_drive_t *drive)
  387. {
  388. struct request *rq;
  389. u8 opcode = 0;
  390. int found = 0;
  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_CMD) {
  399. char *args = rq->buffer;
  400. if (args) {
  401. opcode = args[0];
  402. found = 1;
  403. }
  404. } else if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
  405. ide_task_t *args = rq->special;
  406. if (args) {
  407. opcode = args->tf.command;
  408. found = 1;
  409. }
  410. }
  411. printk("ide: failed opcode was: ");
  412. if (!found)
  413. printk("unknown\n");
  414. else
  415. printk("0x%02x\n", opcode);
  416. }
  417. u64 ide_get_lba_addr(struct ide_taskfile *tf, int lba48)
  418. {
  419. u32 high, low;
  420. if (lba48)
  421. high = (tf->hob_lbah << 16) | (tf->hob_lbam << 8) |
  422. tf->hob_lbal;
  423. else
  424. high = tf->device & 0xf;
  425. low = (tf->lbah << 16) | (tf->lbam << 8) | tf->lbal;
  426. return ((u64)high << 24) | low;
  427. }
  428. EXPORT_SYMBOL_GPL(ide_get_lba_addr);
  429. static void ide_dump_sector(ide_drive_t *drive)
  430. {
  431. ide_task_t task;
  432. struct ide_taskfile *tf = &task.tf;
  433. int lba48 = (drive->addressing == 1) ? 1 : 0;
  434. memset(&task, 0, sizeof(task));
  435. if (lba48)
  436. task.tf_flags = IDE_TFLAG_IN_LBA | IDE_TFLAG_IN_HOB_LBA |
  437. IDE_TFLAG_LBA48;
  438. else
  439. task.tf_flags = IDE_TFLAG_IN_LBA | IDE_TFLAG_IN_DEVICE;
  440. ide_tf_read(drive, &task);
  441. if (lba48 || (tf->device & ATA_LBA))
  442. printk(", LBAsect=%llu",
  443. (unsigned long long)ide_get_lba_addr(tf, lba48));
  444. else
  445. printk(", CHS=%d/%d/%d", (tf->lbah << 8) + tf->lbam,
  446. tf->device & 0xf, tf->lbal);
  447. }
  448. static void ide_dump_ata_error(ide_drive_t *drive, u8 err)
  449. {
  450. printk("{ ");
  451. if (err & ABRT_ERR) printk("DriveStatusError ");
  452. if (err & ICRC_ERR)
  453. printk((err & ABRT_ERR) ? "BadCRC " : "BadSector ");
  454. if (err & ECC_ERR) printk("UncorrectableError ");
  455. if (err & ID_ERR) printk("SectorIdNotFound ");
  456. if (err & TRK0_ERR) printk("TrackZeroNotFound ");
  457. if (err & MARK_ERR) printk("AddrMarkNotFound ");
  458. printk("}");
  459. if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR ||
  460. (err & (ECC_ERR|ID_ERR|MARK_ERR))) {
  461. ide_dump_sector(drive);
  462. if (HWGROUP(drive) && HWGROUP(drive)->rq)
  463. printk(", sector=%llu",
  464. (unsigned long long)HWGROUP(drive)->rq->sector);
  465. }
  466. printk("\n");
  467. }
  468. static void ide_dump_atapi_error(ide_drive_t *drive, u8 err)
  469. {
  470. printk("{ ");
  471. if (err & ILI_ERR) printk("IllegalLengthIndication ");
  472. if (err & EOM_ERR) printk("EndOfMedia ");
  473. if (err & ABRT_ERR) printk("AbortedCommand ");
  474. if (err & MCR_ERR) printk("MediaChangeRequested ");
  475. if (err & LFS_ERR) printk("LastFailedSense=0x%02x ",
  476. (err & LFS_ERR) >> 4);
  477. printk("}\n");
  478. }
  479. /**
  480. * ide_dump_status - translate ATA/ATAPI error
  481. * @drive: drive that status applies to
  482. * @msg: text message to print
  483. * @stat: status byte to decode
  484. *
  485. * Error reporting, in human readable form (luxurious, but a memory hog).
  486. * Combines the drive name, message and status byte to provide a
  487. * user understandable explanation of the device error.
  488. */
  489. u8 ide_dump_status(ide_drive_t *drive, const char *msg, u8 stat)
  490. {
  491. unsigned long flags;
  492. u8 err = 0;
  493. local_irq_save(flags);
  494. printk("%s: %s: status=0x%02x { ", drive->name, msg, stat);
  495. if (stat & BUSY_STAT)
  496. printk("Busy ");
  497. else {
  498. if (stat & READY_STAT) printk("DriveReady ");
  499. if (stat & WRERR_STAT) printk("DeviceFault ");
  500. if (stat & SEEK_STAT) printk("SeekComplete ");
  501. if (stat & DRQ_STAT) printk("DataRequest ");
  502. if (stat & ECC_STAT) printk("CorrectedError ");
  503. if (stat & INDEX_STAT) printk("Index ");
  504. if (stat & ERR_STAT) printk("Error ");
  505. }
  506. printk("}\n");
  507. if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
  508. err = drive->hwif->INB(IDE_ERROR_REG);
  509. printk("%s: %s: error=0x%02x ", drive->name, msg, err);
  510. if (drive->media == ide_disk)
  511. ide_dump_ata_error(drive, err);
  512. else
  513. ide_dump_atapi_error(drive, err);
  514. }
  515. ide_dump_opcode(drive);
  516. local_irq_restore(flags);
  517. return err;
  518. }
  519. EXPORT_SYMBOL(ide_dump_status);