ide-lib.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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. /**
  31. * ide_xfer_verbose - return IDE mode names
  32. * @xfer_rate: rate to name
  33. *
  34. * Returns a constant string giving the name of the mode
  35. * requested.
  36. */
  37. char *ide_xfer_verbose (u8 xfer_rate)
  38. {
  39. switch(xfer_rate) {
  40. case XFER_UDMA_7: return("UDMA 7");
  41. case XFER_UDMA_6: return("UDMA 6");
  42. case XFER_UDMA_5: return("UDMA 5");
  43. case XFER_UDMA_4: return("UDMA 4");
  44. case XFER_UDMA_3: return("UDMA 3");
  45. case XFER_UDMA_2: return("UDMA 2");
  46. case XFER_UDMA_1: return("UDMA 1");
  47. case XFER_UDMA_0: return("UDMA 0");
  48. case XFER_MW_DMA_2: return("MW DMA 2");
  49. case XFER_MW_DMA_1: return("MW DMA 1");
  50. case XFER_MW_DMA_0: return("MW DMA 0");
  51. case XFER_SW_DMA_2: return("SW DMA 2");
  52. case XFER_SW_DMA_1: return("SW DMA 1");
  53. case XFER_SW_DMA_0: return("SW DMA 0");
  54. case XFER_PIO_4: return("PIO 4");
  55. case XFER_PIO_3: return("PIO 3");
  56. case XFER_PIO_2: return("PIO 2");
  57. case XFER_PIO_1: return("PIO 1");
  58. case XFER_PIO_0: return("PIO 0");
  59. case XFER_PIO_SLOW: return("PIO SLOW");
  60. default: return("XFER ERROR");
  61. }
  62. }
  63. EXPORT_SYMBOL(ide_xfer_verbose);
  64. /**
  65. * ide_rate_filter - filter transfer mode
  66. * @drive: IDE device
  67. * @speed: desired speed
  68. *
  69. * Given the available transfer modes this function returns
  70. * the best available speed at or below the speed requested.
  71. *
  72. * FIXME: filter also PIO/SWDMA/MWDMA modes
  73. */
  74. u8 ide_rate_filter(ide_drive_t *drive, u8 speed)
  75. {
  76. #ifdef CONFIG_BLK_DEV_IDEDMA
  77. ide_hwif_t *hwif = drive->hwif;
  78. u8 mask = hwif->ultra_mask, mode = XFER_MW_DMA_2;
  79. if (hwif->udma_filter)
  80. mask = hwif->udma_filter(drive);
  81. /*
  82. * TODO: speed > XFER_UDMA_2 extra check is needed to avoid false
  83. * cable warning from eighty_ninty_three(), moving ide_rate_filter()
  84. * calls from ->speedproc to core code will make this hack go away
  85. */
  86. if (speed > XFER_UDMA_2) {
  87. if ((mask & 0x78) && (eighty_ninty_three(drive) == 0))
  88. mask &= 0x07;
  89. }
  90. if (mask)
  91. mode = fls(mask) - 1 + XFER_UDMA_0;
  92. // printk("%s: mode 0x%02x, speed 0x%02x\n", __FUNCTION__, mode, speed);
  93. return min(speed, mode);
  94. #else /* !CONFIG_BLK_DEV_IDEDMA */
  95. return min(speed, (u8)XFER_PIO_4);
  96. #endif /* CONFIG_BLK_DEV_IDEDMA */
  97. }
  98. EXPORT_SYMBOL(ide_rate_filter);
  99. int ide_use_fast_pio(ide_drive_t *drive)
  100. {
  101. struct hd_driveid *id = drive->id;
  102. if ((id->capability & 1) && drive->autodma)
  103. return 1;
  104. if ((id->capability & 8) || (id->field_valid & 2))
  105. return 1;
  106. return 0;
  107. }
  108. EXPORT_SYMBOL_GPL(ide_use_fast_pio);
  109. /*
  110. * Standard (generic) timings for PIO modes, from ATA2 specification.
  111. * These timings are for access to the IDE data port register *only*.
  112. * Some drives may specify a mode, while also specifying a different
  113. * value for cycle_time (from drive identification data).
  114. */
  115. const ide_pio_timings_t ide_pio_timings[6] = {
  116. { 70, 165, 600 }, /* PIO Mode 0 */
  117. { 50, 125, 383 }, /* PIO Mode 1 */
  118. { 30, 100, 240 }, /* PIO Mode 2 */
  119. { 30, 80, 180 }, /* PIO Mode 3 with IORDY */
  120. { 25, 70, 120 }, /* PIO Mode 4 with IORDY */
  121. { 20, 50, 100 } /* PIO Mode 5 with IORDY (nonstandard) */
  122. };
  123. EXPORT_SYMBOL_GPL(ide_pio_timings);
  124. /*
  125. * Shared data/functions for determining best PIO mode for an IDE drive.
  126. * Most of this stuff originally lived in cmd640.c, and changes to the
  127. * ide_pio_blacklist[] table should be made with EXTREME CAUTION to avoid
  128. * breaking the fragile cmd640.c support.
  129. */
  130. /*
  131. * Black list. Some drives incorrectly report their maximal PIO mode,
  132. * at least in respect to CMD640. Here we keep info on some known drives.
  133. */
  134. static struct ide_pio_info {
  135. const char *name;
  136. int pio;
  137. } ide_pio_blacklist [] = {
  138. /* { "Conner Peripherals 1275MB - CFS1275A", 4 }, */
  139. { "Conner Peripherals 540MB - CFS540A", 3 },
  140. { "WDC AC2700", 3 },
  141. { "WDC AC2540", 3 },
  142. { "WDC AC2420", 3 },
  143. { "WDC AC2340", 3 },
  144. { "WDC AC2250", 0 },
  145. { "WDC AC2200", 0 },
  146. { "WDC AC21200", 4 },
  147. { "WDC AC2120", 0 },
  148. { "WDC AC2850", 3 },
  149. { "WDC AC1270", 3 },
  150. { "WDC AC1170", 1 },
  151. { "WDC AC1210", 1 },
  152. { "WDC AC280", 0 },
  153. /* { "WDC AC21000", 4 }, */
  154. { "WDC AC31000", 3 },
  155. { "WDC AC31200", 3 },
  156. /* { "WDC AC31600", 4 }, */
  157. { "Maxtor 7131 AT", 1 },
  158. { "Maxtor 7171 AT", 1 },
  159. { "Maxtor 7213 AT", 1 },
  160. { "Maxtor 7245 AT", 1 },
  161. { "Maxtor 7345 AT", 1 },
  162. { "Maxtor 7546 AT", 3 },
  163. { "Maxtor 7540 AV", 3 },
  164. { "SAMSUNG SHD-3121A", 1 },
  165. { "SAMSUNG SHD-3122A", 1 },
  166. { "SAMSUNG SHD-3172A", 1 },
  167. /* { "ST51080A", 4 },
  168. * { "ST51270A", 4 },
  169. * { "ST31220A", 4 },
  170. * { "ST31640A", 4 },
  171. * { "ST32140A", 4 },
  172. * { "ST3780A", 4 },
  173. */
  174. { "ST5660A", 3 },
  175. { "ST3660A", 3 },
  176. { "ST3630A", 3 },
  177. { "ST3655A", 3 },
  178. { "ST3391A", 3 },
  179. { "ST3390A", 1 },
  180. { "ST3600A", 1 },
  181. { "ST3290A", 0 },
  182. { "ST3144A", 0 },
  183. { "ST3491A", 1 }, /* reports 3, should be 1 or 2 (depending on */
  184. /* drive) according to Seagates FIND-ATA program */
  185. { "QUANTUM ELS127A", 0 },
  186. { "QUANTUM ELS170A", 0 },
  187. { "QUANTUM LPS240A", 0 },
  188. { "QUANTUM LPS210A", 3 },
  189. { "QUANTUM LPS270A", 3 },
  190. { "QUANTUM LPS365A", 3 },
  191. { "QUANTUM LPS540A", 3 },
  192. { "QUANTUM LIGHTNING 540A", 3 },
  193. { "QUANTUM LIGHTNING 730A", 3 },
  194. { "QUANTUM FIREBALL_540", 3 }, /* Older Quantum Fireballs don't work */
  195. { "QUANTUM FIREBALL_640", 3 },
  196. { "QUANTUM FIREBALL_1080", 3 },
  197. { "QUANTUM FIREBALL_1280", 3 },
  198. { NULL, 0 }
  199. };
  200. /**
  201. * ide_scan_pio_blacklist - check for a blacklisted drive
  202. * @model: Drive model string
  203. *
  204. * This routine searches the ide_pio_blacklist for an entry
  205. * matching the start/whole of the supplied model name.
  206. *
  207. * Returns -1 if no match found.
  208. * Otherwise returns the recommended PIO mode from ide_pio_blacklist[].
  209. */
  210. static int ide_scan_pio_blacklist (char *model)
  211. {
  212. struct ide_pio_info *p;
  213. for (p = ide_pio_blacklist; p->name != NULL; p++) {
  214. if (strncmp(p->name, model, strlen(p->name)) == 0)
  215. return p->pio;
  216. }
  217. return -1;
  218. }
  219. unsigned int ide_pio_cycle_time(ide_drive_t *drive, u8 pio)
  220. {
  221. struct hd_driveid *id = drive->id;
  222. int cycle_time = 0;
  223. if (id->field_valid & 2) {
  224. if (id->capability & 8)
  225. cycle_time = id->eide_pio_iordy;
  226. else
  227. cycle_time = id->eide_pio;
  228. }
  229. /* conservative "downgrade" for all pre-ATA2 drives */
  230. if (pio < 3) {
  231. if (cycle_time && cycle_time < ide_pio_timings[pio].cycle_time)
  232. cycle_time = 0; /* use standard timing */
  233. }
  234. return cycle_time ? cycle_time : ide_pio_timings[pio].cycle_time;
  235. }
  236. EXPORT_SYMBOL_GPL(ide_pio_cycle_time);
  237. /**
  238. * ide_get_best_pio_mode - get PIO mode from drive
  239. * @drive: drive to consider
  240. * @mode_wanted: preferred mode
  241. * @max_mode: highest allowed mode
  242. *
  243. * This routine returns the recommended PIO settings for a given drive,
  244. * based on the drive->id information and the ide_pio_blacklist[].
  245. *
  246. * Drive PIO mode is auto-selected if 255 is passed as mode_wanted.
  247. * This is used by most chipset support modules when "auto-tuning".
  248. */
  249. u8 ide_get_best_pio_mode (ide_drive_t *drive, u8 mode_wanted, u8 max_mode)
  250. {
  251. int pio_mode;
  252. struct hd_driveid* id = drive->id;
  253. int overridden = 0;
  254. if (mode_wanted != 255)
  255. return min_t(u8, mode_wanted, max_mode);
  256. if ((drive->hwif->host_flags & IDE_HFLAG_PIO_NO_BLACKLIST) == 0 &&
  257. (pio_mode = ide_scan_pio_blacklist(id->model)) != -1) {
  258. printk(KERN_INFO "%s: is on PIO blacklist\n", drive->name);
  259. } else {
  260. pio_mode = id->tPIO;
  261. if (pio_mode > 2) { /* 2 is maximum allowed tPIO value */
  262. pio_mode = 2;
  263. overridden = 1;
  264. }
  265. if (id->field_valid & 2) { /* drive implements ATA2? */
  266. if (id->capability & 8) { /* IORDY supported? */
  267. if (id->eide_pio_modes & 7) {
  268. overridden = 0;
  269. if (id->eide_pio_modes & 4)
  270. pio_mode = 5;
  271. else if (id->eide_pio_modes & 2)
  272. pio_mode = 4;
  273. else
  274. pio_mode = 3;
  275. }
  276. }
  277. }
  278. if (overridden)
  279. printk(KERN_INFO "%s: tPIO > 2, assuming tPIO = 2\n",
  280. drive->name);
  281. /*
  282. * Conservative "downgrade" for all pre-ATA2 drives
  283. */
  284. if ((drive->hwif->host_flags & IDE_HFLAG_PIO_NO_DOWNGRADE) == 0 &&
  285. pio_mode && pio_mode < 4) {
  286. pio_mode--;
  287. printk(KERN_INFO "%s: applying conservative "
  288. "PIO \"downgrade\"\n", drive->name);
  289. }
  290. }
  291. if (pio_mode > max_mode)
  292. pio_mode = max_mode;
  293. return pio_mode;
  294. }
  295. EXPORT_SYMBOL_GPL(ide_get_best_pio_mode);
  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. /**
  317. * ide_set_xfer_rate - set transfer rate
  318. * @drive: drive to set
  319. * @speed: speed to attempt to set
  320. *
  321. * General helper for setting the speed of an IDE device. This
  322. * function knows about user enforced limits from the configuration
  323. * which speedproc() does not. High level drivers should never
  324. * invoke speedproc() directly.
  325. */
  326. int ide_set_xfer_rate(ide_drive_t *drive, u8 rate)
  327. {
  328. #ifndef CONFIG_BLK_DEV_IDEDMA
  329. rate = min(rate, (u8) XFER_PIO_4);
  330. #endif
  331. if(HWIF(drive)->speedproc)
  332. return HWIF(drive)->speedproc(drive, rate);
  333. else
  334. return -1;
  335. }
  336. static void ide_dump_opcode(ide_drive_t *drive)
  337. {
  338. struct request *rq;
  339. u8 opcode = 0;
  340. int found = 0;
  341. spin_lock(&ide_lock);
  342. rq = NULL;
  343. if (HWGROUP(drive))
  344. rq = HWGROUP(drive)->rq;
  345. spin_unlock(&ide_lock);
  346. if (!rq)
  347. return;
  348. if (rq->cmd_type == REQ_TYPE_ATA_CMD ||
  349. rq->cmd_type == REQ_TYPE_ATA_TASK) {
  350. char *args = rq->buffer;
  351. if (args) {
  352. opcode = args[0];
  353. found = 1;
  354. }
  355. } else if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
  356. ide_task_t *args = rq->special;
  357. if (args) {
  358. task_struct_t *tf = (task_struct_t *) args->tfRegister;
  359. opcode = tf->command;
  360. found = 1;
  361. }
  362. }
  363. printk("ide: failed opcode was: ");
  364. if (!found)
  365. printk("unknown\n");
  366. else
  367. printk("0x%02x\n", opcode);
  368. }
  369. static u8 ide_dump_ata_status(ide_drive_t *drive, const char *msg, u8 stat)
  370. {
  371. ide_hwif_t *hwif = HWIF(drive);
  372. unsigned long flags;
  373. u8 err = 0;
  374. local_irq_save(flags);
  375. printk("%s: %s: status=0x%02x { ", drive->name, msg, stat);
  376. if (stat & BUSY_STAT)
  377. printk("Busy ");
  378. else {
  379. if (stat & READY_STAT) printk("DriveReady ");
  380. if (stat & WRERR_STAT) printk("DeviceFault ");
  381. if (stat & SEEK_STAT) printk("SeekComplete ");
  382. if (stat & DRQ_STAT) printk("DataRequest ");
  383. if (stat & ECC_STAT) printk("CorrectedError ");
  384. if (stat & INDEX_STAT) printk("Index ");
  385. if (stat & ERR_STAT) printk("Error ");
  386. }
  387. printk("}\n");
  388. if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
  389. err = hwif->INB(IDE_ERROR_REG);
  390. printk("%s: %s: error=0x%02x { ", drive->name, msg, err);
  391. if (err & ABRT_ERR) printk("DriveStatusError ");
  392. if (err & ICRC_ERR)
  393. printk((err & ABRT_ERR) ? "BadCRC " : "BadSector ");
  394. if (err & ECC_ERR) printk("UncorrectableError ");
  395. if (err & ID_ERR) printk("SectorIdNotFound ");
  396. if (err & TRK0_ERR) printk("TrackZeroNotFound ");
  397. if (err & MARK_ERR) printk("AddrMarkNotFound ");
  398. printk("}");
  399. if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR ||
  400. (err & (ECC_ERR|ID_ERR|MARK_ERR))) {
  401. if (drive->addressing == 1) {
  402. __u64 sectors = 0;
  403. u32 low = 0, high = 0;
  404. low = ide_read_24(drive);
  405. hwif->OUTB(drive->ctl|0x80, IDE_CONTROL_REG);
  406. high = ide_read_24(drive);
  407. sectors = ((__u64)high << 24) | low;
  408. printk(", LBAsect=%llu, high=%d, low=%d",
  409. (unsigned long long) sectors,
  410. high, low);
  411. } else {
  412. u8 cur = hwif->INB(IDE_SELECT_REG);
  413. if (cur & 0x40) { /* using LBA? */
  414. printk(", LBAsect=%ld", (unsigned long)
  415. ((cur&0xf)<<24)
  416. |(hwif->INB(IDE_HCYL_REG)<<16)
  417. |(hwif->INB(IDE_LCYL_REG)<<8)
  418. | hwif->INB(IDE_SECTOR_REG));
  419. } else {
  420. printk(", CHS=%d/%d/%d",
  421. (hwif->INB(IDE_HCYL_REG)<<8) +
  422. hwif->INB(IDE_LCYL_REG),
  423. cur & 0xf,
  424. hwif->INB(IDE_SECTOR_REG));
  425. }
  426. }
  427. if (HWGROUP(drive) && HWGROUP(drive)->rq)
  428. printk(", sector=%llu",
  429. (unsigned long long)HWGROUP(drive)->rq->sector);
  430. }
  431. printk("\n");
  432. }
  433. ide_dump_opcode(drive);
  434. local_irq_restore(flags);
  435. return err;
  436. }
  437. /**
  438. * ide_dump_atapi_status - print human readable atapi status
  439. * @drive: drive that status applies to
  440. * @msg: text message to print
  441. * @stat: status byte to decode
  442. *
  443. * Error reporting, in human readable form (luxurious, but a memory hog).
  444. */
  445. static u8 ide_dump_atapi_status(ide_drive_t *drive, const char *msg, u8 stat)
  446. {
  447. unsigned long flags;
  448. atapi_status_t status;
  449. atapi_error_t error;
  450. status.all = stat;
  451. error.all = 0;
  452. local_irq_save(flags);
  453. printk("%s: %s: status=0x%02x { ", drive->name, msg, stat);
  454. if (status.b.bsy)
  455. printk("Busy ");
  456. else {
  457. if (status.b.drdy) printk("DriveReady ");
  458. if (status.b.df) printk("DeviceFault ");
  459. if (status.b.dsc) printk("SeekComplete ");
  460. if (status.b.drq) printk("DataRequest ");
  461. if (status.b.corr) printk("CorrectedError ");
  462. if (status.b.idx) printk("Index ");
  463. if (status.b.check) printk("Error ");
  464. }
  465. printk("}\n");
  466. if (status.b.check && !status.b.bsy) {
  467. error.all = HWIF(drive)->INB(IDE_ERROR_REG);
  468. printk("%s: %s: error=0x%02x { ", drive->name, msg, error.all);
  469. if (error.b.ili) printk("IllegalLengthIndication ");
  470. if (error.b.eom) printk("EndOfMedia ");
  471. if (error.b.abrt) printk("AbortedCommand ");
  472. if (error.b.mcr) printk("MediaChangeRequested ");
  473. if (error.b.sense_key) printk("LastFailedSense=0x%02x ",
  474. error.b.sense_key);
  475. printk("}\n");
  476. }
  477. ide_dump_opcode(drive);
  478. local_irq_restore(flags);
  479. return error.all;
  480. }
  481. /**
  482. * ide_dump_status - translate ATA/ATAPI error
  483. * @drive: drive the error occured on
  484. * @msg: information string
  485. * @stat: status byte
  486. *
  487. * Error reporting, in human readable form (luxurious, but a memory hog).
  488. * Combines the drive name, message and status byte to provide a
  489. * user understandable explanation of the device error.
  490. */
  491. u8 ide_dump_status(ide_drive_t *drive, const char *msg, u8 stat)
  492. {
  493. if (drive->media == ide_disk)
  494. return ide_dump_ata_status(drive, msg, stat);
  495. return ide_dump_atapi_status(drive, msg, stat);
  496. }
  497. EXPORT_SYMBOL(ide_dump_status);