ide-iops.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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, unsigned long timeout, u8 *rstat)
  105. {
  106. ide_hwif_t *hwif = drive->hwif;
  107. const struct ide_tp_ops *tp_ops = hwif->tp_ops;
  108. unsigned long flags;
  109. int i;
  110. u8 stat;
  111. udelay(1); /* spec allows drive 400ns to assert "BUSY" */
  112. stat = tp_ops->read_status(hwif);
  113. if (stat & ATA_BUSY) {
  114. local_save_flags(flags);
  115. local_irq_enable_in_hardirq();
  116. timeout += jiffies;
  117. while ((stat = tp_ops->read_status(hwif)) & ATA_BUSY) {
  118. if (time_after(jiffies, timeout)) {
  119. /*
  120. * One last read after the timeout in case
  121. * heavy interrupt load made us not make any
  122. * progress during the timeout..
  123. */
  124. stat = tp_ops->read_status(hwif);
  125. if ((stat & ATA_BUSY) == 0)
  126. break;
  127. local_irq_restore(flags);
  128. *rstat = stat;
  129. return -EBUSY;
  130. }
  131. }
  132. local_irq_restore(flags);
  133. }
  134. /*
  135. * Allow status to settle, then read it again.
  136. * A few rare drives vastly violate the 400ns spec here,
  137. * so we'll wait up to 10usec for a "good" status
  138. * rather than expensively fail things immediately.
  139. * This fix courtesy of Matthew Faupel & Niccolo Rigacci.
  140. */
  141. for (i = 0; i < 10; i++) {
  142. udelay(1);
  143. stat = tp_ops->read_status(hwif);
  144. if (OK_STAT(stat, good, bad)) {
  145. *rstat = stat;
  146. return 0;
  147. }
  148. }
  149. *rstat = stat;
  150. return -EFAULT;
  151. }
  152. /*
  153. * In case of error returns error value after doing "*startstop = ide_error()".
  154. * The caller should return the updated value of "startstop" in this case,
  155. * "startstop" is unchanged when the function returns 0.
  156. */
  157. int ide_wait_stat(ide_startstop_t *startstop, ide_drive_t *drive, u8 good, u8 bad, unsigned long timeout)
  158. {
  159. int err;
  160. u8 stat;
  161. /* bail early if we've exceeded max_failures */
  162. if (drive->max_failures && (drive->failures > drive->max_failures)) {
  163. *startstop = ide_stopped;
  164. return 1;
  165. }
  166. err = __ide_wait_stat(drive, good, bad, timeout, &stat);
  167. if (err) {
  168. char *s = (err == -EBUSY) ? "status timeout" : "status error";
  169. *startstop = ide_error(drive, s, stat);
  170. }
  171. return err;
  172. }
  173. EXPORT_SYMBOL(ide_wait_stat);
  174. /**
  175. * ide_in_drive_list - look for drive in black/white list
  176. * @id: drive identifier
  177. * @table: list to inspect
  178. *
  179. * Look for a drive in the blacklist and the whitelist tables
  180. * Returns 1 if the drive is found in the table.
  181. */
  182. int ide_in_drive_list(u16 *id, const struct drive_list_entry *table)
  183. {
  184. for ( ; table->id_model; table++)
  185. if ((!strcmp(table->id_model, (char *)&id[ATA_ID_PROD])) &&
  186. (!table->id_firmware ||
  187. strstr((char *)&id[ATA_ID_FW_REV], table->id_firmware)))
  188. return 1;
  189. return 0;
  190. }
  191. EXPORT_SYMBOL_GPL(ide_in_drive_list);
  192. /*
  193. * Early UDMA66 devices don't set bit14 to 1, only bit13 is valid.
  194. * We list them here and depend on the device side cable detection for them.
  195. *
  196. * Some optical devices with the buggy firmwares have the same problem.
  197. */
  198. static const struct drive_list_entry ivb_list[] = {
  199. { "QUANTUM FIREBALLlct10 05" , "A03.0900" },
  200. { "TSSTcorp CDDVDW SH-S202J" , "SB00" },
  201. { "TSSTcorp CDDVDW SH-S202J" , "SB01" },
  202. { "TSSTcorp CDDVDW SH-S202N" , "SB00" },
  203. { "TSSTcorp CDDVDW SH-S202N" , "SB01" },
  204. { "TSSTcorp CDDVDW SH-S202H" , "SB00" },
  205. { "TSSTcorp CDDVDW SH-S202H" , "SB01" },
  206. { "SAMSUNG SP0822N" , "WA100-10" },
  207. { NULL , NULL }
  208. };
  209. /*
  210. * All hosts that use the 80c ribbon must use!
  211. * The name is derived from upper byte of word 93 and the 80c ribbon.
  212. */
  213. u8 eighty_ninty_three (ide_drive_t *drive)
  214. {
  215. ide_hwif_t *hwif = drive->hwif;
  216. u16 *id = drive->id;
  217. int ivb = ide_in_drive_list(id, ivb_list);
  218. if (hwif->cbl == ATA_CBL_PATA40_SHORT)
  219. return 1;
  220. if (ivb)
  221. printk(KERN_DEBUG "%s: skipping word 93 validity check\n",
  222. drive->name);
  223. if (ata_id_is_sata(id) && !ivb)
  224. return 1;
  225. if (hwif->cbl != ATA_CBL_PATA80 && !ivb)
  226. goto no_80w;
  227. /*
  228. * FIXME:
  229. * - change master/slave IDENTIFY order
  230. * - force bit13 (80c cable present) check also for !ivb devices
  231. * (unless the slave device is pre-ATA3)
  232. */
  233. if ((id[ATA_ID_HW_CONFIG] & 0x4000) ||
  234. (ivb && (id[ATA_ID_HW_CONFIG] & 0x2000)))
  235. return 1;
  236. no_80w:
  237. if (drive->dev_flags & IDE_DFLAG_UDMA33_WARNED)
  238. return 0;
  239. printk(KERN_WARNING "%s: %s side 80-wire cable detection failed, "
  240. "limiting max speed to UDMA33\n",
  241. drive->name,
  242. hwif->cbl == ATA_CBL_PATA80 ? "drive" : "host");
  243. drive->dev_flags |= IDE_DFLAG_UDMA33_WARNED;
  244. return 0;
  245. }
  246. int ide_driveid_update(ide_drive_t *drive)
  247. {
  248. ide_hwif_t *hwif = drive->hwif;
  249. const struct ide_tp_ops *tp_ops = hwif->tp_ops;
  250. u16 *id;
  251. unsigned long flags;
  252. u8 stat;
  253. /*
  254. * Re-read drive->id for possible DMA mode
  255. * change (copied from ide-probe.c)
  256. */
  257. SELECT_MASK(drive, 1);
  258. tp_ops->set_irq(hwif, 0);
  259. msleep(50);
  260. tp_ops->exec_command(hwif, ATA_CMD_ID_ATA);
  261. if (ide_busy_sleep(hwif, WAIT_WORSTCASE, 1)) {
  262. SELECT_MASK(drive, 0);
  263. return 0;
  264. }
  265. msleep(50); /* wait for IRQ and ATA_DRQ */
  266. stat = tp_ops->read_status(hwif);
  267. if (!OK_STAT(stat, ATA_DRQ, BAD_R_STAT)) {
  268. SELECT_MASK(drive, 0);
  269. printk("%s: CHECK for good STATUS\n", drive->name);
  270. return 0;
  271. }
  272. local_irq_save(flags);
  273. SELECT_MASK(drive, 0);
  274. id = kmalloc(SECTOR_SIZE, GFP_ATOMIC);
  275. if (!id) {
  276. local_irq_restore(flags);
  277. return 0;
  278. }
  279. tp_ops->input_data(drive, NULL, id, SECTOR_SIZE);
  280. (void)tp_ops->read_status(hwif); /* clear drive IRQ */
  281. local_irq_enable();
  282. local_irq_restore(flags);
  283. ide_fix_driveid(id);
  284. drive->id[ATA_ID_UDMA_MODES] = id[ATA_ID_UDMA_MODES];
  285. drive->id[ATA_ID_MWDMA_MODES] = id[ATA_ID_MWDMA_MODES];
  286. drive->id[ATA_ID_SWDMA_MODES] = id[ATA_ID_SWDMA_MODES];
  287. /* anything more ? */
  288. kfree(id);
  289. if ((drive->dev_flags & IDE_DFLAG_USING_DMA) && ide_id_dma_bug(drive))
  290. ide_dma_off(drive);
  291. return 1;
  292. }
  293. int ide_config_drive_speed(ide_drive_t *drive, u8 speed)
  294. {
  295. ide_hwif_t *hwif = drive->hwif;
  296. const struct ide_tp_ops *tp_ops = hwif->tp_ops;
  297. u16 *id = drive->id, i;
  298. int error = 0;
  299. u8 stat;
  300. ide_task_t task;
  301. #ifdef CONFIG_BLK_DEV_IDEDMA
  302. if (hwif->dma_ops) /* check if host supports DMA */
  303. hwif->dma_ops->dma_host_set(drive, 0);
  304. #endif
  305. /* Skip setting PIO flow-control modes on pre-EIDE drives */
  306. if ((speed & 0xf8) == XFER_PIO_0 && ata_id_has_iordy(drive->id) == 0)
  307. goto skip;
  308. /*
  309. * Don't use ide_wait_cmd here - it will
  310. * attempt to set_geometry and recalibrate,
  311. * but for some reason these don't work at
  312. * this point (lost interrupt).
  313. */
  314. /*
  315. * Select the drive, and issue the SETFEATURES command
  316. */
  317. disable_irq_nosync(hwif->irq);
  318. /*
  319. * FIXME: we race against the running IRQ here if
  320. * this is called from non IRQ context. If we use
  321. * disable_irq() we hang on the error path. Work
  322. * is needed.
  323. */
  324. udelay(1);
  325. SELECT_DRIVE(drive);
  326. SELECT_MASK(drive, 1);
  327. udelay(1);
  328. tp_ops->set_irq(hwif, 0);
  329. memset(&task, 0, sizeof(task));
  330. task.tf_flags = IDE_TFLAG_OUT_FEATURE | IDE_TFLAG_OUT_NSECT;
  331. task.tf.feature = SETFEATURES_XFER;
  332. task.tf.nsect = speed;
  333. tp_ops->tf_load(drive, &task);
  334. tp_ops->exec_command(hwif, ATA_CMD_SET_FEATURES);
  335. if (drive->quirk_list == 2)
  336. tp_ops->set_irq(hwif, 1);
  337. error = __ide_wait_stat(drive, drive->ready_stat,
  338. ATA_BUSY | ATA_DRQ | ATA_ERR,
  339. WAIT_CMD, &stat);
  340. SELECT_MASK(drive, 0);
  341. enable_irq(hwif->irq);
  342. if (error) {
  343. (void) ide_dump_status(drive, "set_drive_speed_status", stat);
  344. return error;
  345. }
  346. id[ATA_ID_UDMA_MODES] &= ~0xFF00;
  347. id[ATA_ID_MWDMA_MODES] &= ~0x0F00;
  348. id[ATA_ID_SWDMA_MODES] &= ~0x0F00;
  349. skip:
  350. #ifdef CONFIG_BLK_DEV_IDEDMA
  351. if (speed >= XFER_SW_DMA_0 && (drive->dev_flags & IDE_DFLAG_USING_DMA))
  352. hwif->dma_ops->dma_host_set(drive, 1);
  353. else if (hwif->dma_ops) /* check if host supports DMA */
  354. ide_dma_off_quietly(drive);
  355. #endif
  356. if (speed >= XFER_UDMA_0) {
  357. i = 1 << (speed - XFER_UDMA_0);
  358. id[ATA_ID_UDMA_MODES] |= (i << 8 | i);
  359. } else if (speed >= XFER_MW_DMA_0) {
  360. i = 1 << (speed - XFER_MW_DMA_0);
  361. id[ATA_ID_MWDMA_MODES] |= (i << 8 | i);
  362. } else if (speed >= XFER_SW_DMA_0) {
  363. i = 1 << (speed - XFER_SW_DMA_0);
  364. id[ATA_ID_SWDMA_MODES] |= (i << 8 | i);
  365. }
  366. if (!drive->init_speed)
  367. drive->init_speed = speed;
  368. drive->current_speed = speed;
  369. return error;
  370. }
  371. /*
  372. * This should get invoked any time we exit the driver to
  373. * wait for an interrupt response from a drive. handler() points
  374. * at the appropriate code to handle the next interrupt, and a
  375. * timer is started to prevent us from waiting forever in case
  376. * something goes wrong (see the ide_timer_expiry() handler later on).
  377. *
  378. * See also ide_execute_command
  379. */
  380. static void __ide_set_handler (ide_drive_t *drive, ide_handler_t *handler,
  381. unsigned int timeout, ide_expiry_t *expiry)
  382. {
  383. ide_hwif_t *hwif = drive->hwif;
  384. BUG_ON(hwif->handler);
  385. hwif->handler = handler;
  386. hwif->expiry = expiry;
  387. hwif->timer.expires = jiffies + timeout;
  388. hwif->req_gen_timer = hwif->req_gen;
  389. add_timer(&hwif->timer);
  390. }
  391. void ide_set_handler (ide_drive_t *drive, ide_handler_t *handler,
  392. unsigned int timeout, ide_expiry_t *expiry)
  393. {
  394. ide_hwif_t *hwif = drive->hwif;
  395. unsigned long flags;
  396. spin_lock_irqsave(&hwif->lock, flags);
  397. __ide_set_handler(drive, handler, timeout, expiry);
  398. spin_unlock_irqrestore(&hwif->lock, flags);
  399. }
  400. EXPORT_SYMBOL(ide_set_handler);
  401. /**
  402. * ide_execute_command - execute an IDE command
  403. * @drive: IDE drive to issue the command against
  404. * @command: command byte to write
  405. * @handler: handler for next phase
  406. * @timeout: timeout for command
  407. * @expiry: handler to run on timeout
  408. *
  409. * Helper function to issue an IDE command. This handles the
  410. * atomicity requirements, command timing and ensures that the
  411. * handler and IRQ setup do not race. All IDE command kick off
  412. * should go via this function or do equivalent locking.
  413. */
  414. void ide_execute_command(ide_drive_t *drive, u8 cmd, ide_handler_t *handler,
  415. unsigned timeout, ide_expiry_t *expiry)
  416. {
  417. ide_hwif_t *hwif = drive->hwif;
  418. unsigned long flags;
  419. spin_lock_irqsave(&hwif->lock, flags);
  420. __ide_set_handler(drive, handler, timeout, expiry);
  421. hwif->tp_ops->exec_command(hwif, cmd);
  422. /*
  423. * Drive takes 400nS to respond, we must avoid the IRQ being
  424. * serviced before that.
  425. *
  426. * FIXME: we could skip this delay with care on non shared devices
  427. */
  428. ndelay(400);
  429. spin_unlock_irqrestore(&hwif->lock, flags);
  430. }
  431. EXPORT_SYMBOL(ide_execute_command);
  432. void ide_execute_pkt_cmd(ide_drive_t *drive)
  433. {
  434. ide_hwif_t *hwif = drive->hwif;
  435. unsigned long flags;
  436. spin_lock_irqsave(&hwif->lock, flags);
  437. hwif->tp_ops->exec_command(hwif, ATA_CMD_PACKET);
  438. ndelay(400);
  439. spin_unlock_irqrestore(&hwif->lock, flags);
  440. }
  441. EXPORT_SYMBOL_GPL(ide_execute_pkt_cmd);
  442. static inline void ide_complete_drive_reset(ide_drive_t *drive, int err)
  443. {
  444. struct request *rq = drive->hwif->rq;
  445. if (rq && blk_special_request(rq) && rq->cmd[0] == REQ_DRIVE_RESET)
  446. ide_end_request(drive, err ? err : 1, 0);
  447. }
  448. /* needed below */
  449. static ide_startstop_t do_reset1 (ide_drive_t *, int);
  450. /*
  451. * atapi_reset_pollfunc() gets invoked to poll the interface for completion every 50ms
  452. * during an atapi drive reset operation. If the drive has not yet responded,
  453. * and we have not yet hit our maximum waiting time, then the timer is restarted
  454. * for another 50ms.
  455. */
  456. static ide_startstop_t atapi_reset_pollfunc (ide_drive_t *drive)
  457. {
  458. ide_hwif_t *hwif = drive->hwif;
  459. u8 stat;
  460. SELECT_DRIVE(drive);
  461. udelay (10);
  462. stat = hwif->tp_ops->read_status(hwif);
  463. if (OK_STAT(stat, 0, ATA_BUSY))
  464. printk(KERN_INFO "%s: ATAPI reset complete\n", drive->name);
  465. else {
  466. if (time_before(jiffies, hwif->poll_timeout)) {
  467. ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20, NULL);
  468. /* continue polling */
  469. return ide_started;
  470. }
  471. /* end of polling */
  472. hwif->polling = 0;
  473. printk(KERN_ERR "%s: ATAPI reset timed-out, status=0x%02x\n",
  474. drive->name, stat);
  475. /* do it the old fashioned way */
  476. return do_reset1(drive, 1);
  477. }
  478. /* done polling */
  479. hwif->polling = 0;
  480. ide_complete_drive_reset(drive, 0);
  481. return ide_stopped;
  482. }
  483. static void ide_reset_report_error(ide_hwif_t *hwif, u8 err)
  484. {
  485. static const char *err_master_vals[] =
  486. { NULL, "passed", "formatter device error",
  487. "sector buffer error", "ECC circuitry error",
  488. "controlling MPU error" };
  489. u8 err_master = err & 0x7f;
  490. printk(KERN_ERR "%s: reset: master: ", hwif->name);
  491. if (err_master && err_master < 6)
  492. printk(KERN_CONT "%s", err_master_vals[err_master]);
  493. else
  494. printk(KERN_CONT "error (0x%02x?)", err);
  495. if (err & 0x80)
  496. printk(KERN_CONT "; slave: failed");
  497. printk(KERN_CONT "\n");
  498. }
  499. /*
  500. * reset_pollfunc() gets invoked to poll the interface for completion every 50ms
  501. * during an ide reset operation. If the drives have not yet responded,
  502. * and we have not yet hit our maximum waiting time, then the timer is restarted
  503. * for another 50ms.
  504. */
  505. static ide_startstop_t reset_pollfunc (ide_drive_t *drive)
  506. {
  507. ide_hwif_t *hwif = drive->hwif;
  508. const struct ide_port_ops *port_ops = hwif->port_ops;
  509. u8 tmp;
  510. int err = 0;
  511. if (port_ops && port_ops->reset_poll) {
  512. err = port_ops->reset_poll(drive);
  513. if (err) {
  514. printk(KERN_ERR "%s: host reset_poll failure for %s.\n",
  515. hwif->name, drive->name);
  516. goto out;
  517. }
  518. }
  519. tmp = hwif->tp_ops->read_status(hwif);
  520. if (!OK_STAT(tmp, 0, ATA_BUSY)) {
  521. if (time_before(jiffies, hwif->poll_timeout)) {
  522. ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL);
  523. /* continue polling */
  524. return ide_started;
  525. }
  526. printk(KERN_ERR "%s: reset timed-out, status=0x%02x\n",
  527. hwif->name, tmp);
  528. drive->failures++;
  529. err = -EIO;
  530. } else {
  531. tmp = ide_read_error(drive);
  532. if (tmp == 1) {
  533. printk(KERN_INFO "%s: reset: success\n", hwif->name);
  534. drive->failures = 0;
  535. } else {
  536. ide_reset_report_error(hwif, tmp);
  537. drive->failures++;
  538. err = -EIO;
  539. }
  540. }
  541. out:
  542. hwif->polling = 0; /* done polling */
  543. ide_complete_drive_reset(drive, err);
  544. return ide_stopped;
  545. }
  546. static void ide_disk_pre_reset(ide_drive_t *drive)
  547. {
  548. int legacy = (drive->id[ATA_ID_CFS_ENABLE_2] & 0x0400) ? 0 : 1;
  549. drive->special.all = 0;
  550. drive->special.b.set_geometry = legacy;
  551. drive->special.b.recalibrate = legacy;
  552. drive->mult_count = 0;
  553. drive->dev_flags &= ~IDE_DFLAG_PARKED;
  554. if ((drive->dev_flags & IDE_DFLAG_KEEP_SETTINGS) == 0 &&
  555. (drive->dev_flags & IDE_DFLAG_USING_DMA) == 0)
  556. drive->mult_req = 0;
  557. if (drive->mult_req != drive->mult_count)
  558. drive->special.b.set_multmode = 1;
  559. }
  560. static void pre_reset(ide_drive_t *drive)
  561. {
  562. const struct ide_port_ops *port_ops = drive->hwif->port_ops;
  563. if (drive->media == ide_disk)
  564. ide_disk_pre_reset(drive);
  565. else
  566. drive->dev_flags |= IDE_DFLAG_POST_RESET;
  567. if (drive->dev_flags & IDE_DFLAG_USING_DMA) {
  568. if (drive->crc_count)
  569. ide_check_dma_crc(drive);
  570. else
  571. ide_dma_off(drive);
  572. }
  573. if ((drive->dev_flags & IDE_DFLAG_KEEP_SETTINGS) == 0) {
  574. if ((drive->dev_flags & IDE_DFLAG_USING_DMA) == 0) {
  575. drive->dev_flags &= ~IDE_DFLAG_UNMASK;
  576. drive->io_32bit = 0;
  577. }
  578. return;
  579. }
  580. if (port_ops && port_ops->pre_reset)
  581. port_ops->pre_reset(drive);
  582. if (drive->current_speed != 0xff)
  583. drive->desired_speed = drive->current_speed;
  584. drive->current_speed = 0xff;
  585. }
  586. /*
  587. * do_reset1() attempts to recover a confused drive by resetting it.
  588. * Unfortunately, resetting a disk drive actually resets all devices on
  589. * the same interface, so it can really be thought of as resetting the
  590. * interface rather than resetting the drive.
  591. *
  592. * ATAPI devices have their own reset mechanism which allows them to be
  593. * individually reset without clobbering other devices on the same interface.
  594. *
  595. * Unfortunately, the IDE interface does not generate an interrupt to let
  596. * us know when the reset operation has finished, so we must poll for this.
  597. * Equally poor, though, is the fact that this may a very long time to complete,
  598. * (up to 30 seconds worstcase). So, instead of busy-waiting here for it,
  599. * we set a timer to poll at 50ms intervals.
  600. */
  601. static ide_startstop_t do_reset1 (ide_drive_t *drive, int do_not_try_atapi)
  602. {
  603. ide_hwif_t *hwif = drive->hwif;
  604. struct ide_io_ports *io_ports = &hwif->io_ports;
  605. const struct ide_tp_ops *tp_ops = hwif->tp_ops;
  606. const struct ide_port_ops *port_ops;
  607. ide_drive_t *tdrive;
  608. unsigned long flags, timeout;
  609. int i;
  610. DEFINE_WAIT(wait);
  611. spin_lock_irqsave(&hwif->lock, flags);
  612. /* We must not reset with running handlers */
  613. BUG_ON(hwif->handler != NULL);
  614. /* For an ATAPI device, first try an ATAPI SRST. */
  615. if (drive->media != ide_disk && !do_not_try_atapi) {
  616. pre_reset(drive);
  617. SELECT_DRIVE(drive);
  618. udelay (20);
  619. tp_ops->exec_command(hwif, ATA_CMD_DEV_RESET);
  620. ndelay(400);
  621. hwif->poll_timeout = jiffies + WAIT_WORSTCASE;
  622. hwif->polling = 1;
  623. __ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20, NULL);
  624. spin_unlock_irqrestore(&hwif->lock, flags);
  625. return ide_started;
  626. }
  627. /* We must not disturb devices in the IDE_DFLAG_PARKED state. */
  628. do {
  629. unsigned long now;
  630. prepare_to_wait(&ide_park_wq, &wait, TASK_UNINTERRUPTIBLE);
  631. timeout = jiffies;
  632. ide_port_for_each_present_dev(i, tdrive, hwif) {
  633. if ((tdrive->dev_flags & IDE_DFLAG_PARKED) &&
  634. time_after(tdrive->sleep, timeout))
  635. timeout = tdrive->sleep;
  636. }
  637. now = jiffies;
  638. if (time_before_eq(timeout, now))
  639. break;
  640. spin_unlock_irqrestore(&hwif->lock, flags);
  641. timeout = schedule_timeout_uninterruptible(timeout - now);
  642. spin_lock_irqsave(&hwif->lock, flags);
  643. } while (timeout);
  644. finish_wait(&ide_park_wq, &wait);
  645. /*
  646. * First, reset any device state data we were maintaining
  647. * for any of the drives on this interface.
  648. */
  649. ide_port_for_each_dev(i, tdrive, hwif)
  650. pre_reset(tdrive);
  651. if (io_ports->ctl_addr == 0) {
  652. spin_unlock_irqrestore(&hwif->lock, flags);
  653. ide_complete_drive_reset(drive, -ENXIO);
  654. return ide_stopped;
  655. }
  656. /*
  657. * Note that we also set nIEN while resetting the device,
  658. * to mask unwanted interrupts from the interface during the reset.
  659. * However, due to the design of PC hardware, this will cause an
  660. * immediate interrupt due to the edge transition it produces.
  661. * This single interrupt gives us a "fast poll" for drives that
  662. * recover from reset very quickly, saving us the first 50ms wait time.
  663. *
  664. * TODO: add ->softreset method and stop abusing ->set_irq
  665. */
  666. /* set SRST and nIEN */
  667. tp_ops->set_irq(hwif, 4);
  668. /* more than enough time */
  669. udelay(10);
  670. /* clear SRST, leave nIEN (unless device is on the quirk list) */
  671. tp_ops->set_irq(hwif, drive->quirk_list == 2);
  672. /* more than enough time */
  673. udelay(10);
  674. hwif->poll_timeout = jiffies + WAIT_WORSTCASE;
  675. hwif->polling = 1;
  676. __ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL);
  677. /*
  678. * Some weird controller like resetting themselves to a strange
  679. * state when the disks are reset this way. At least, the Winbond
  680. * 553 documentation says that
  681. */
  682. port_ops = hwif->port_ops;
  683. if (port_ops && port_ops->resetproc)
  684. port_ops->resetproc(drive);
  685. spin_unlock_irqrestore(&hwif->lock, flags);
  686. return ide_started;
  687. }
  688. /*
  689. * ide_do_reset() is the entry point to the drive/interface reset code.
  690. */
  691. ide_startstop_t ide_do_reset (ide_drive_t *drive)
  692. {
  693. return do_reset1(drive, 0);
  694. }
  695. EXPORT_SYMBOL(ide_do_reset);
  696. /*
  697. * ide_wait_not_busy() waits for the currently selected device on the hwif
  698. * to report a non-busy status, see comments in ide_probe_port().
  699. */
  700. int ide_wait_not_busy(ide_hwif_t *hwif, unsigned long timeout)
  701. {
  702. u8 stat = 0;
  703. while(timeout--) {
  704. /*
  705. * Turn this into a schedule() sleep once I'm sure
  706. * about locking issues (2.5 work ?).
  707. */
  708. mdelay(1);
  709. stat = hwif->tp_ops->read_status(hwif);
  710. if ((stat & ATA_BUSY) == 0)
  711. return 0;
  712. /*
  713. * Assume a value of 0xff means nothing is connected to
  714. * the interface and it doesn't implement the pull-down
  715. * resistor on D7.
  716. */
  717. if (stat == 0xff)
  718. return -ENODEV;
  719. touch_softlockup_watchdog();
  720. touch_nmi_watchdog();
  721. }
  722. return -EBUSY;
  723. }