ide-lib.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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. /**
  220. * ide_get_best_pio_mode - get PIO mode from drive
  221. * @drive: drive to consider
  222. * @mode_wanted: preferred mode
  223. * @max_mode: highest allowed mode
  224. * @d: PIO data
  225. *
  226. * This routine returns the recommended PIO settings for a given drive,
  227. * based on the drive->id information and the ide_pio_blacklist[].
  228. *
  229. * Drive PIO mode is auto-selected if 255 is passed as mode_wanted.
  230. * This is used by most chipset support modules when "auto-tuning".
  231. */
  232. u8 ide_get_best_pio_mode (ide_drive_t *drive, u8 mode_wanted, u8 max_mode, ide_pio_data_t *d)
  233. {
  234. int pio_mode;
  235. int cycle_time = 0;
  236. int use_iordy = 0;
  237. struct hd_driveid* id = drive->id;
  238. int overridden = 0;
  239. if (mode_wanted != 255) {
  240. pio_mode = mode_wanted;
  241. use_iordy = (pio_mode > 2);
  242. } else if (!drive->id) {
  243. pio_mode = 0;
  244. } else if ((pio_mode = ide_scan_pio_blacklist(id->model)) != -1) {
  245. overridden = 1;
  246. use_iordy = (pio_mode > 2);
  247. } else {
  248. pio_mode = id->tPIO;
  249. if (pio_mode > 2) { /* 2 is maximum allowed tPIO value */
  250. pio_mode = 2;
  251. overridden = 1;
  252. }
  253. if (id->field_valid & 2) { /* drive implements ATA2? */
  254. if (id->capability & 8) { /* drive supports use_iordy? */
  255. use_iordy = 1;
  256. cycle_time = id->eide_pio_iordy;
  257. if (id->eide_pio_modes & 7) {
  258. overridden = 0;
  259. if (id->eide_pio_modes & 4)
  260. pio_mode = 5;
  261. else if (id->eide_pio_modes & 2)
  262. pio_mode = 4;
  263. else
  264. pio_mode = 3;
  265. }
  266. } else {
  267. cycle_time = id->eide_pio;
  268. }
  269. }
  270. /*
  271. * Conservative "downgrade" for all pre-ATA2 drives
  272. */
  273. if (pio_mode && pio_mode < 4) {
  274. pio_mode--;
  275. overridden = 1;
  276. if (cycle_time && cycle_time < ide_pio_timings[pio_mode].cycle_time)
  277. cycle_time = 0; /* use standard timing */
  278. }
  279. }
  280. if (pio_mode > max_mode) {
  281. pio_mode = max_mode;
  282. cycle_time = 0;
  283. }
  284. if (d) {
  285. d->pio_mode = pio_mode;
  286. d->cycle_time = cycle_time ? cycle_time : ide_pio_timings[pio_mode].cycle_time;
  287. d->use_iordy = use_iordy;
  288. d->overridden = overridden;
  289. }
  290. return pio_mode;
  291. }
  292. EXPORT_SYMBOL_GPL(ide_get_best_pio_mode);
  293. /**
  294. * ide_toggle_bounce - handle bounce buffering
  295. * @drive: drive to update
  296. * @on: on/off boolean
  297. *
  298. * Enable or disable bounce buffering for the device. Drives move
  299. * between PIO and DMA and that changes the rules we need.
  300. */
  301. void ide_toggle_bounce(ide_drive_t *drive, int on)
  302. {
  303. u64 addr = BLK_BOUNCE_HIGH; /* dma64_addr_t */
  304. if (!PCI_DMA_BUS_IS_PHYS) {
  305. addr = BLK_BOUNCE_ANY;
  306. } else if (on && drive->media == ide_disk) {
  307. if (HWIF(drive)->pci_dev)
  308. addr = HWIF(drive)->pci_dev->dma_mask;
  309. }
  310. if (drive->queue)
  311. blk_queue_bounce_limit(drive->queue, addr);
  312. }
  313. /**
  314. * ide_set_xfer_rate - set transfer rate
  315. * @drive: drive to set
  316. * @speed: speed to attempt to set
  317. *
  318. * General helper for setting the speed of an IDE device. This
  319. * function knows about user enforced limits from the configuration
  320. * which speedproc() does not. High level drivers should never
  321. * invoke speedproc() directly.
  322. */
  323. int ide_set_xfer_rate(ide_drive_t *drive, u8 rate)
  324. {
  325. #ifndef CONFIG_BLK_DEV_IDEDMA
  326. rate = min(rate, (u8) XFER_PIO_4);
  327. #endif
  328. if(HWIF(drive)->speedproc)
  329. return HWIF(drive)->speedproc(drive, rate);
  330. else
  331. return -1;
  332. }
  333. static void ide_dump_opcode(ide_drive_t *drive)
  334. {
  335. struct request *rq;
  336. u8 opcode = 0;
  337. int found = 0;
  338. spin_lock(&ide_lock);
  339. rq = NULL;
  340. if (HWGROUP(drive))
  341. rq = HWGROUP(drive)->rq;
  342. spin_unlock(&ide_lock);
  343. if (!rq)
  344. return;
  345. if (rq->cmd_type == REQ_TYPE_ATA_CMD ||
  346. rq->cmd_type == REQ_TYPE_ATA_TASK) {
  347. char *args = rq->buffer;
  348. if (args) {
  349. opcode = args[0];
  350. found = 1;
  351. }
  352. } else if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
  353. ide_task_t *args = rq->special;
  354. if (args) {
  355. task_struct_t *tf = (task_struct_t *) args->tfRegister;
  356. opcode = tf->command;
  357. found = 1;
  358. }
  359. }
  360. printk("ide: failed opcode was: ");
  361. if (!found)
  362. printk("unknown\n");
  363. else
  364. printk("0x%02x\n", opcode);
  365. }
  366. static u8 ide_dump_ata_status(ide_drive_t *drive, const char *msg, u8 stat)
  367. {
  368. ide_hwif_t *hwif = HWIF(drive);
  369. unsigned long flags;
  370. u8 err = 0;
  371. local_irq_save(flags);
  372. printk("%s: %s: status=0x%02x { ", drive->name, msg, stat);
  373. if (stat & BUSY_STAT)
  374. printk("Busy ");
  375. else {
  376. if (stat & READY_STAT) printk("DriveReady ");
  377. if (stat & WRERR_STAT) printk("DeviceFault ");
  378. if (stat & SEEK_STAT) printk("SeekComplete ");
  379. if (stat & DRQ_STAT) printk("DataRequest ");
  380. if (stat & ECC_STAT) printk("CorrectedError ");
  381. if (stat & INDEX_STAT) printk("Index ");
  382. if (stat & ERR_STAT) printk("Error ");
  383. }
  384. printk("}\n");
  385. if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
  386. err = hwif->INB(IDE_ERROR_REG);
  387. printk("%s: %s: error=0x%02x { ", drive->name, msg, err);
  388. if (err & ABRT_ERR) printk("DriveStatusError ");
  389. if (err & ICRC_ERR)
  390. printk((err & ABRT_ERR) ? "BadCRC " : "BadSector ");
  391. if (err & ECC_ERR) printk("UncorrectableError ");
  392. if (err & ID_ERR) printk("SectorIdNotFound ");
  393. if (err & TRK0_ERR) printk("TrackZeroNotFound ");
  394. if (err & MARK_ERR) printk("AddrMarkNotFound ");
  395. printk("}");
  396. if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR ||
  397. (err & (ECC_ERR|ID_ERR|MARK_ERR))) {
  398. if (drive->addressing == 1) {
  399. __u64 sectors = 0;
  400. u32 low = 0, high = 0;
  401. low = ide_read_24(drive);
  402. hwif->OUTB(drive->ctl|0x80, IDE_CONTROL_REG);
  403. high = ide_read_24(drive);
  404. sectors = ((__u64)high << 24) | low;
  405. printk(", LBAsect=%llu, high=%d, low=%d",
  406. (unsigned long long) sectors,
  407. high, low);
  408. } else {
  409. u8 cur = hwif->INB(IDE_SELECT_REG);
  410. if (cur & 0x40) { /* using LBA? */
  411. printk(", LBAsect=%ld", (unsigned long)
  412. ((cur&0xf)<<24)
  413. |(hwif->INB(IDE_HCYL_REG)<<16)
  414. |(hwif->INB(IDE_LCYL_REG)<<8)
  415. | hwif->INB(IDE_SECTOR_REG));
  416. } else {
  417. printk(", CHS=%d/%d/%d",
  418. (hwif->INB(IDE_HCYL_REG)<<8) +
  419. hwif->INB(IDE_LCYL_REG),
  420. cur & 0xf,
  421. hwif->INB(IDE_SECTOR_REG));
  422. }
  423. }
  424. if (HWGROUP(drive) && HWGROUP(drive)->rq)
  425. printk(", sector=%llu",
  426. (unsigned long long)HWGROUP(drive)->rq->sector);
  427. }
  428. printk("\n");
  429. }
  430. ide_dump_opcode(drive);
  431. local_irq_restore(flags);
  432. return err;
  433. }
  434. /**
  435. * ide_dump_atapi_status - print human readable atapi status
  436. * @drive: drive that status applies to
  437. * @msg: text message to print
  438. * @stat: status byte to decode
  439. *
  440. * Error reporting, in human readable form (luxurious, but a memory hog).
  441. */
  442. static u8 ide_dump_atapi_status(ide_drive_t *drive, const char *msg, u8 stat)
  443. {
  444. unsigned long flags;
  445. atapi_status_t status;
  446. atapi_error_t error;
  447. status.all = stat;
  448. error.all = 0;
  449. local_irq_save(flags);
  450. printk("%s: %s: status=0x%02x { ", drive->name, msg, stat);
  451. if (status.b.bsy)
  452. printk("Busy ");
  453. else {
  454. if (status.b.drdy) printk("DriveReady ");
  455. if (status.b.df) printk("DeviceFault ");
  456. if (status.b.dsc) printk("SeekComplete ");
  457. if (status.b.drq) printk("DataRequest ");
  458. if (status.b.corr) printk("CorrectedError ");
  459. if (status.b.idx) printk("Index ");
  460. if (status.b.check) printk("Error ");
  461. }
  462. printk("}\n");
  463. if (status.b.check && !status.b.bsy) {
  464. error.all = HWIF(drive)->INB(IDE_ERROR_REG);
  465. printk("%s: %s: error=0x%02x { ", drive->name, msg, error.all);
  466. if (error.b.ili) printk("IllegalLengthIndication ");
  467. if (error.b.eom) printk("EndOfMedia ");
  468. if (error.b.abrt) printk("AbortedCommand ");
  469. if (error.b.mcr) printk("MediaChangeRequested ");
  470. if (error.b.sense_key) printk("LastFailedSense=0x%02x ",
  471. error.b.sense_key);
  472. printk("}\n");
  473. }
  474. ide_dump_opcode(drive);
  475. local_irq_restore(flags);
  476. return error.all;
  477. }
  478. /**
  479. * ide_dump_status - translate ATA/ATAPI error
  480. * @drive: drive the error occured on
  481. * @msg: information string
  482. * @stat: status byte
  483. *
  484. * Error reporting, in human readable form (luxurious, but a memory hog).
  485. * Combines the drive name, message and status byte to provide a
  486. * user understandable explanation of the device error.
  487. */
  488. u8 ide_dump_status(ide_drive_t *drive, const char *msg, u8 stat)
  489. {
  490. if (drive->media == ide_disk)
  491. return ide_dump_ata_status(drive, msg, stat);
  492. return ide_dump_atapi_status(drive, msg, stat);
  493. }
  494. EXPORT_SYMBOL(ide_dump_status);