ide-iops.c 14 KB

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