ide-iops.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /*
  2. * Copyright (C) 2000-2002 Andre Hedrick <andre@linux-ide.org>
  3. * Copyright (C) 2003 Red Hat
  4. *
  5. */
  6. #include <linux/module.h>
  7. #include <linux/types.h>
  8. #include <linux/string.h>
  9. #include <linux/kernel.h>
  10. #include <linux/timer.h>
  11. #include <linux/mm.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/major.h>
  14. #include <linux/errno.h>
  15. #include <linux/genhd.h>
  16. #include <linux/blkpg.h>
  17. #include <linux/slab.h>
  18. #include <linux/pci.h>
  19. #include <linux/delay.h>
  20. #include <linux/ide.h>
  21. #include <linux/bitops.h>
  22. #include <linux/nmi.h>
  23. #include <asm/byteorder.h>
  24. #include <asm/irq.h>
  25. #include <asm/uaccess.h>
  26. #include <asm/io.h>
  27. void SELECT_MASK(ide_drive_t *drive, int mask)
  28. {
  29. const struct ide_port_ops *port_ops = drive->hwif->port_ops;
  30. if (port_ops && port_ops->maskproc)
  31. port_ops->maskproc(drive, mask);
  32. }
  33. u8 ide_read_error(ide_drive_t *drive)
  34. {
  35. struct ide_cmd cmd;
  36. memset(&cmd, 0, sizeof(cmd));
  37. cmd.valid.in.tf = IDE_VALID_ERROR;
  38. drive->hwif->tp_ops->tf_read(drive, &cmd);
  39. return cmd.tf.error;
  40. }
  41. EXPORT_SYMBOL_GPL(ide_read_error);
  42. void ide_fix_driveid(u16 *id)
  43. {
  44. #ifndef __LITTLE_ENDIAN
  45. # ifdef __BIG_ENDIAN
  46. int i;
  47. for (i = 0; i < 256; i++)
  48. id[i] = __le16_to_cpu(id[i]);
  49. # else
  50. # error "Please fix <asm/byteorder.h>"
  51. # endif
  52. #endif
  53. }
  54. /*
  55. * ide_fixstring() cleans up and (optionally) byte-swaps a text string,
  56. * removing leading/trailing blanks and compressing internal blanks.
  57. * It is primarily used to tidy up the model name/number fields as
  58. * returned by the ATA_CMD_ID_ATA[PI] commands.
  59. */
  60. void ide_fixstring(u8 *s, const int bytecount, const int byteswap)
  61. {
  62. u8 *p, *end = &s[bytecount & ~1]; /* bytecount must be even */
  63. if (byteswap) {
  64. /* convert from big-endian to host byte order */
  65. for (p = s ; p != end ; p += 2)
  66. be16_to_cpus((u16 *) p);
  67. }
  68. /* strip leading blanks */
  69. p = s;
  70. while (s != end && *s == ' ')
  71. ++s;
  72. /* compress internal blanks and strip trailing blanks */
  73. while (s != end && *s) {
  74. if (*s++ != ' ' || (s != end && *s && *s != ' '))
  75. *p++ = *(s-1);
  76. }
  77. /* wipe out trailing garbage */
  78. while (p != end)
  79. *p++ = '\0';
  80. }
  81. EXPORT_SYMBOL(ide_fixstring);
  82. /*
  83. * This routine busy-waits for the drive status to be not "busy".
  84. * It then checks the status for all of the "good" bits and none
  85. * of the "bad" bits, and if all is okay it returns 0. All other
  86. * cases return error -- caller may then invoke ide_error().
  87. *
  88. * This routine should get fixed to not hog the cpu during extra long waits..
  89. * That could be done by busy-waiting for the first jiffy or two, and then
  90. * setting a timer to wake up at half second intervals thereafter,
  91. * until timeout is achieved, before timing out.
  92. */
  93. static int __ide_wait_stat(ide_drive_t *drive, u8 good, u8 bad,
  94. unsigned long timeout, u8 *rstat)
  95. {
  96. ide_hwif_t *hwif = drive->hwif;
  97. const struct ide_tp_ops *tp_ops = hwif->tp_ops;
  98. unsigned long flags;
  99. int i;
  100. u8 stat;
  101. udelay(1); /* spec allows drive 400ns to assert "BUSY" */
  102. stat = tp_ops->read_status(hwif);
  103. if (stat & ATA_BUSY) {
  104. local_save_flags(flags);
  105. local_irq_enable_in_hardirq();
  106. timeout += jiffies;
  107. while ((stat = tp_ops->read_status(hwif)) & ATA_BUSY) {
  108. if (time_after(jiffies, timeout)) {
  109. /*
  110. * One last read after the timeout in case
  111. * heavy interrupt load made us not make any
  112. * progress during the timeout..
  113. */
  114. stat = tp_ops->read_status(hwif);
  115. if ((stat & ATA_BUSY) == 0)
  116. break;
  117. local_irq_restore(flags);
  118. *rstat = stat;
  119. return -EBUSY;
  120. }
  121. }
  122. local_irq_restore(flags);
  123. }
  124. /*
  125. * Allow status to settle, then read it again.
  126. * A few rare drives vastly violate the 400ns spec here,
  127. * so we'll wait up to 10usec for a "good" status
  128. * rather than expensively fail things immediately.
  129. * This fix courtesy of Matthew Faupel & Niccolo Rigacci.
  130. */
  131. for (i = 0; i < 10; i++) {
  132. udelay(1);
  133. stat = tp_ops->read_status(hwif);
  134. if (OK_STAT(stat, good, bad)) {
  135. *rstat = stat;
  136. return 0;
  137. }
  138. }
  139. *rstat = stat;
  140. return -EFAULT;
  141. }
  142. /*
  143. * In case of error returns error value after doing "*startstop = ide_error()".
  144. * The caller should return the updated value of "startstop" in this case,
  145. * "startstop" is unchanged when the function returns 0.
  146. */
  147. int ide_wait_stat(ide_startstop_t *startstop, ide_drive_t *drive, u8 good,
  148. u8 bad, unsigned long timeout)
  149. {
  150. int err;
  151. u8 stat;
  152. /* bail early if we've exceeded max_failures */
  153. if (drive->max_failures && (drive->failures > drive->max_failures)) {
  154. *startstop = ide_stopped;
  155. return 1;
  156. }
  157. err = __ide_wait_stat(drive, good, bad, timeout, &stat);
  158. if (err) {
  159. char *s = (err == -EBUSY) ? "status timeout" : "status error";
  160. *startstop = ide_error(drive, s, stat);
  161. }
  162. return err;
  163. }
  164. EXPORT_SYMBOL(ide_wait_stat);
  165. /**
  166. * ide_in_drive_list - look for drive in black/white list
  167. * @id: drive identifier
  168. * @table: list to inspect
  169. *
  170. * Look for a drive in the blacklist and the whitelist tables
  171. * Returns 1 if the drive is found in the table.
  172. */
  173. int ide_in_drive_list(u16 *id, const struct drive_list_entry *table)
  174. {
  175. for ( ; table->id_model; table++)
  176. if ((!strcmp(table->id_model, (char *)&id[ATA_ID_PROD])) &&
  177. (!table->id_firmware ||
  178. strstr((char *)&id[ATA_ID_FW_REV], table->id_firmware)))
  179. return 1;
  180. return 0;
  181. }
  182. EXPORT_SYMBOL_GPL(ide_in_drive_list);
  183. /*
  184. * Early UDMA66 devices don't set bit14 to 1, only bit13 is valid.
  185. * We list them here and depend on the device side cable detection for them.
  186. *
  187. * Some optical devices with the buggy firmwares have the same problem.
  188. */
  189. static const struct drive_list_entry ivb_list[] = {
  190. { "QUANTUM FIREBALLlct10 05" , "A03.0900" },
  191. { "TSSTcorp CDDVDW SH-S202J" , "SB00" },
  192. { "TSSTcorp CDDVDW SH-S202J" , "SB01" },
  193. { "TSSTcorp CDDVDW SH-S202N" , "SB00" },
  194. { "TSSTcorp CDDVDW SH-S202N" , "SB01" },
  195. { "TSSTcorp CDDVDW SH-S202H" , "SB00" },
  196. { "TSSTcorp CDDVDW SH-S202H" , "SB01" },
  197. { "SAMSUNG SP0822N" , "WA100-10" },
  198. { NULL , NULL }
  199. };
  200. /*
  201. * All hosts that use the 80c ribbon must use!
  202. * The name is derived from upper byte of word 93 and the 80c ribbon.
  203. */
  204. u8 eighty_ninty_three(ide_drive_t *drive)
  205. {
  206. ide_hwif_t *hwif = drive->hwif;
  207. u16 *id = drive->id;
  208. int ivb = ide_in_drive_list(id, ivb_list);
  209. if (hwif->cbl == ATA_CBL_PATA40_SHORT)
  210. return 1;
  211. if (ivb)
  212. printk(KERN_DEBUG "%s: skipping word 93 validity check\n",
  213. drive->name);
  214. if (ata_id_is_sata(id) && !ivb)
  215. return 1;
  216. if (hwif->cbl != ATA_CBL_PATA80 && !ivb)
  217. goto no_80w;
  218. /*
  219. * FIXME:
  220. * - change master/slave IDENTIFY order
  221. * - force bit13 (80c cable present) check also for !ivb devices
  222. * (unless the slave device is pre-ATA3)
  223. */
  224. if ((id[ATA_ID_HW_CONFIG] & 0x4000) ||
  225. (ivb && (id[ATA_ID_HW_CONFIG] & 0x2000)))
  226. return 1;
  227. no_80w:
  228. if (drive->dev_flags & IDE_DFLAG_UDMA33_WARNED)
  229. return 0;
  230. printk(KERN_WARNING "%s: %s side 80-wire cable detection failed, "
  231. "limiting max speed to UDMA33\n",
  232. drive->name,
  233. hwif->cbl == ATA_CBL_PATA80 ? "drive" : "host");
  234. drive->dev_flags |= IDE_DFLAG_UDMA33_WARNED;
  235. return 0;
  236. }
  237. int ide_driveid_update(ide_drive_t *drive)
  238. {
  239. u16 *id;
  240. int rc;
  241. id = kmalloc(SECTOR_SIZE, GFP_ATOMIC);
  242. if (id == NULL)
  243. return 0;
  244. SELECT_MASK(drive, 1);
  245. rc = ide_dev_read_id(drive, ATA_CMD_ID_ATA, id);
  246. SELECT_MASK(drive, 0);
  247. if (rc)
  248. goto out_err;
  249. drive->id[ATA_ID_UDMA_MODES] = id[ATA_ID_UDMA_MODES];
  250. drive->id[ATA_ID_MWDMA_MODES] = id[ATA_ID_MWDMA_MODES];
  251. drive->id[ATA_ID_SWDMA_MODES] = id[ATA_ID_SWDMA_MODES];
  252. drive->id[ATA_ID_CFA_MODES] = id[ATA_ID_CFA_MODES];
  253. /* anything more ? */
  254. kfree(id);
  255. if ((drive->dev_flags & IDE_DFLAG_USING_DMA) && ide_id_dma_bug(drive))
  256. ide_dma_off(drive);
  257. return 1;
  258. out_err:
  259. SELECT_MASK(drive, 0);
  260. if (rc == 2)
  261. printk(KERN_ERR "%s: %s: bad status\n", drive->name, __func__);
  262. kfree(id);
  263. return 0;
  264. }
  265. int ide_config_drive_speed(ide_drive_t *drive, u8 speed)
  266. {
  267. ide_hwif_t *hwif = drive->hwif;
  268. const struct ide_tp_ops *tp_ops = hwif->tp_ops;
  269. struct ide_taskfile tf;
  270. u16 *id = drive->id, i;
  271. int error = 0;
  272. u8 stat;
  273. #ifdef CONFIG_BLK_DEV_IDEDMA
  274. if (hwif->dma_ops) /* check if host supports DMA */
  275. hwif->dma_ops->dma_host_set(drive, 0);
  276. #endif
  277. /* Skip setting PIO flow-control modes on pre-EIDE drives */
  278. if ((speed & 0xf8) == XFER_PIO_0 && ata_id_has_iordy(drive->id) == 0)
  279. goto skip;
  280. /*
  281. * Don't use ide_wait_cmd here - it will
  282. * attempt to set_geometry and recalibrate,
  283. * but for some reason these don't work at
  284. * this point (lost interrupt).
  285. */
  286. /*
  287. * FIXME: we race against the running IRQ here if
  288. * this is called from non IRQ context. If we use
  289. * disable_irq() we hang on the error path. Work
  290. * is needed.
  291. */
  292. disable_irq_nosync(hwif->irq);
  293. udelay(1);
  294. tp_ops->dev_select(drive);
  295. SELECT_MASK(drive, 1);
  296. udelay(1);
  297. tp_ops->write_devctl(hwif, ATA_NIEN | ATA_DEVCTL_OBS);
  298. memset(&tf, 0, sizeof(tf));
  299. tf.feature = SETFEATURES_XFER;
  300. tf.nsect = speed;
  301. tp_ops->tf_load(drive, &tf, IDE_VALID_FEATURE | IDE_VALID_NSECT);
  302. tp_ops->exec_command(hwif, ATA_CMD_SET_FEATURES);
  303. if (drive->quirk_list == 2)
  304. tp_ops->write_devctl(hwif, ATA_DEVCTL_OBS);
  305. error = __ide_wait_stat(drive, drive->ready_stat,
  306. ATA_BUSY | ATA_DRQ | ATA_ERR,
  307. WAIT_CMD, &stat);
  308. SELECT_MASK(drive, 0);
  309. enable_irq(hwif->irq);
  310. if (error) {
  311. (void) ide_dump_status(drive, "set_drive_speed_status", stat);
  312. return error;
  313. }
  314. if (speed >= XFER_SW_DMA_0) {
  315. id[ATA_ID_UDMA_MODES] &= ~0xFF00;
  316. id[ATA_ID_MWDMA_MODES] &= ~0x0700;
  317. id[ATA_ID_SWDMA_MODES] &= ~0x0700;
  318. if (ata_id_is_cfa(id))
  319. id[ATA_ID_CFA_MODES] &= ~0x0E00;
  320. } else if (ata_id_is_cfa(id))
  321. id[ATA_ID_CFA_MODES] &= ~0x01C0;
  322. skip:
  323. #ifdef CONFIG_BLK_DEV_IDEDMA
  324. if (speed >= XFER_SW_DMA_0 && (drive->dev_flags & IDE_DFLAG_USING_DMA))
  325. hwif->dma_ops->dma_host_set(drive, 1);
  326. else if (hwif->dma_ops) /* check if host supports DMA */
  327. ide_dma_off_quietly(drive);
  328. #endif
  329. if (speed >= XFER_UDMA_0) {
  330. i = 1 << (speed - XFER_UDMA_0);
  331. id[ATA_ID_UDMA_MODES] |= (i << 8 | i);
  332. } else if (ata_id_is_cfa(id) && speed >= XFER_MW_DMA_3) {
  333. i = speed - XFER_MW_DMA_2;
  334. id[ATA_ID_CFA_MODES] |= i << 9;
  335. } else if (speed >= XFER_MW_DMA_0) {
  336. i = 1 << (speed - XFER_MW_DMA_0);
  337. id[ATA_ID_MWDMA_MODES] |= (i << 8 | i);
  338. } else if (speed >= XFER_SW_DMA_0) {
  339. i = 1 << (speed - XFER_SW_DMA_0);
  340. id[ATA_ID_SWDMA_MODES] |= (i << 8 | i);
  341. } else if (ata_id_is_cfa(id) && speed >= XFER_PIO_5) {
  342. i = speed - XFER_PIO_4;
  343. id[ATA_ID_CFA_MODES] |= i << 6;
  344. }
  345. if (!drive->init_speed)
  346. drive->init_speed = speed;
  347. drive->current_speed = speed;
  348. return error;
  349. }
  350. /*
  351. * This should get invoked any time we exit the driver to
  352. * wait for an interrupt response from a drive. handler() points
  353. * at the appropriate code to handle the next interrupt, and a
  354. * timer is started to prevent us from waiting forever in case
  355. * something goes wrong (see the ide_timer_expiry() handler later on).
  356. *
  357. * See also ide_execute_command
  358. */
  359. void __ide_set_handler(ide_drive_t *drive, ide_handler_t *handler,
  360. unsigned int timeout)
  361. {
  362. ide_hwif_t *hwif = drive->hwif;
  363. BUG_ON(hwif->handler);
  364. hwif->handler = handler;
  365. hwif->timer.expires = jiffies + timeout;
  366. hwif->req_gen_timer = hwif->req_gen;
  367. add_timer(&hwif->timer);
  368. }
  369. void ide_set_handler(ide_drive_t *drive, ide_handler_t *handler,
  370. unsigned int timeout)
  371. {
  372. ide_hwif_t *hwif = drive->hwif;
  373. unsigned long flags;
  374. spin_lock_irqsave(&hwif->lock, flags);
  375. __ide_set_handler(drive, handler, timeout);
  376. spin_unlock_irqrestore(&hwif->lock, flags);
  377. }
  378. EXPORT_SYMBOL(ide_set_handler);
  379. /**
  380. * ide_execute_command - execute an IDE command
  381. * @drive: IDE drive to issue the command against
  382. * @cmd: command
  383. * @handler: handler for next phase
  384. * @timeout: timeout for command
  385. *
  386. * Helper function to issue an IDE command. This handles the
  387. * atomicity requirements, command timing and ensures that the
  388. * handler and IRQ setup do not race. All IDE command kick off
  389. * should go via this function or do equivalent locking.
  390. */
  391. void ide_execute_command(ide_drive_t *drive, struct ide_cmd *cmd,
  392. ide_handler_t *handler, unsigned timeout)
  393. {
  394. ide_hwif_t *hwif = drive->hwif;
  395. unsigned long flags;
  396. spin_lock_irqsave(&hwif->lock, flags);
  397. if ((cmd->protocol != ATAPI_PROT_DMA &&
  398. cmd->protocol != ATAPI_PROT_PIO) ||
  399. (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT))
  400. __ide_set_handler(drive, handler, timeout);
  401. hwif->tp_ops->exec_command(hwif, cmd->tf.command);
  402. /*
  403. * Drive takes 400nS to respond, we must avoid the IRQ being
  404. * serviced before that.
  405. *
  406. * FIXME: we could skip this delay with care on non shared devices
  407. */
  408. ndelay(400);
  409. spin_unlock_irqrestore(&hwif->lock, flags);
  410. }
  411. /*
  412. * ide_wait_not_busy() waits for the currently selected device on the hwif
  413. * to report a non-busy status, see comments in ide_probe_port().
  414. */
  415. int ide_wait_not_busy(ide_hwif_t *hwif, unsigned long timeout)
  416. {
  417. u8 stat = 0;
  418. while (timeout--) {
  419. /*
  420. * Turn this into a schedule() sleep once I'm sure
  421. * about locking issues (2.5 work ?).
  422. */
  423. mdelay(1);
  424. stat = hwif->tp_ops->read_status(hwif);
  425. if ((stat & ATA_BUSY) == 0)
  426. return 0;
  427. /*
  428. * Assume a value of 0xff means nothing is connected to
  429. * the interface and it doesn't implement the pull-down
  430. * resistor on D7.
  431. */
  432. if (stat == 0xff)
  433. return -ENODEV;
  434. touch_softlockup_watchdog();
  435. touch_nmi_watchdog();
  436. }
  437. return -EBUSY;
  438. }