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