ide-iops.c 14 KB

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