ide-eh.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. #include <linux/kernel.h>
  2. #include <linux/ide.h>
  3. #include <linux/delay.h>
  4. static ide_startstop_t ide_ata_error(ide_drive_t *drive, struct request *rq,
  5. u8 stat, u8 err)
  6. {
  7. ide_hwif_t *hwif = drive->hwif;
  8. if ((stat & ATA_BUSY) ||
  9. ((stat & ATA_DF) && (drive->dev_flags & IDE_DFLAG_NOWERR) == 0)) {
  10. /* other bits are useless when BUSY */
  11. rq->errors |= ERROR_RESET;
  12. } else if (stat & ATA_ERR) {
  13. /* err has different meaning on cdrom and tape */
  14. if (err == ATA_ABORTED) {
  15. if ((drive->dev_flags & IDE_DFLAG_LBA) &&
  16. /* some newer drives don't support ATA_CMD_INIT_DEV_PARAMS */
  17. hwif->tp_ops->read_status(hwif) == ATA_CMD_INIT_DEV_PARAMS)
  18. return ide_stopped;
  19. } else if ((err & BAD_CRC) == BAD_CRC) {
  20. /* UDMA crc error, just retry the operation */
  21. drive->crc_count++;
  22. } else if (err & (ATA_BBK | ATA_UNC)) {
  23. /* retries won't help these */
  24. rq->errors = ERROR_MAX;
  25. } else if (err & ATA_TRK0NF) {
  26. /* help it find track zero */
  27. rq->errors |= ERROR_RECAL;
  28. }
  29. }
  30. if ((stat & ATA_DRQ) && rq_data_dir(rq) == READ &&
  31. (hwif->host_flags & IDE_HFLAG_ERROR_STOPS_FIFO) == 0) {
  32. int nsect = drive->mult_count ? drive->mult_count : 1;
  33. ide_pad_transfer(drive, READ, nsect * SECTOR_SIZE);
  34. }
  35. if (rq->errors >= ERROR_MAX || blk_noretry_request(rq)) {
  36. ide_kill_rq(drive, rq);
  37. return ide_stopped;
  38. }
  39. if (hwif->tp_ops->read_status(hwif) & (ATA_BUSY | ATA_DRQ))
  40. rq->errors |= ERROR_RESET;
  41. if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
  42. ++rq->errors;
  43. return ide_do_reset(drive);
  44. }
  45. if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
  46. drive->special.b.recalibrate = 1;
  47. ++rq->errors;
  48. return ide_stopped;
  49. }
  50. static ide_startstop_t ide_atapi_error(ide_drive_t *drive, struct request *rq,
  51. u8 stat, u8 err)
  52. {
  53. ide_hwif_t *hwif = drive->hwif;
  54. if ((stat & ATA_BUSY) ||
  55. ((stat & ATA_DF) && (drive->dev_flags & IDE_DFLAG_NOWERR) == 0)) {
  56. /* other bits are useless when BUSY */
  57. rq->errors |= ERROR_RESET;
  58. } else {
  59. /* add decoding error stuff */
  60. }
  61. if (hwif->tp_ops->read_status(hwif) & (ATA_BUSY | ATA_DRQ))
  62. /* force an abort */
  63. hwif->tp_ops->exec_command(hwif, ATA_CMD_IDLEIMMEDIATE);
  64. if (rq->errors >= ERROR_MAX) {
  65. ide_kill_rq(drive, rq);
  66. } else {
  67. if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
  68. ++rq->errors;
  69. return ide_do_reset(drive);
  70. }
  71. ++rq->errors;
  72. }
  73. return ide_stopped;
  74. }
  75. static ide_startstop_t __ide_error(ide_drive_t *drive, struct request *rq,
  76. u8 stat, u8 err)
  77. {
  78. if (drive->media == ide_disk)
  79. return ide_ata_error(drive, rq, stat, err);
  80. return ide_atapi_error(drive, rq, stat, err);
  81. }
  82. /**
  83. * ide_error - handle an error on the IDE
  84. * @drive: drive the error occurred on
  85. * @msg: message to report
  86. * @stat: status bits
  87. *
  88. * ide_error() takes action based on the error returned by the drive.
  89. * For normal I/O that may well include retries. We deal with
  90. * both new-style (taskfile) and old style command handling here.
  91. * In the case of taskfile command handling there is work left to
  92. * do
  93. */
  94. ide_startstop_t ide_error(ide_drive_t *drive, const char *msg, u8 stat)
  95. {
  96. struct request *rq;
  97. u8 err;
  98. err = ide_dump_status(drive, msg, stat);
  99. rq = drive->hwif->rq;
  100. if (rq == NULL)
  101. return ide_stopped;
  102. /* retry only "normal" I/O: */
  103. if (!blk_fs_request(rq)) {
  104. rq->errors = 1;
  105. ide_end_drive_cmd(drive, stat, err);
  106. return ide_stopped;
  107. }
  108. return __ide_error(drive, rq, stat, err);
  109. }
  110. EXPORT_SYMBOL_GPL(ide_error);
  111. static inline void ide_complete_drive_reset(ide_drive_t *drive, int err)
  112. {
  113. struct request *rq = drive->hwif->rq;
  114. if (rq && blk_special_request(rq) && rq->cmd[0] == REQ_DRIVE_RESET)
  115. ide_end_request(drive, err ? err : 1, 0);
  116. }
  117. /* needed below */
  118. static ide_startstop_t do_reset1(ide_drive_t *, int);
  119. /*
  120. * atapi_reset_pollfunc() gets invoked to poll the interface for completion
  121. * every 50ms during an atapi drive reset operation. If the drive has not yet
  122. * responded, and we have not yet hit our maximum waiting time, then the timer
  123. * is restarted for another 50ms.
  124. */
  125. static ide_startstop_t atapi_reset_pollfunc(ide_drive_t *drive)
  126. {
  127. ide_hwif_t *hwif = drive->hwif;
  128. u8 stat;
  129. SELECT_DRIVE(drive);
  130. udelay(10);
  131. stat = hwif->tp_ops->read_status(hwif);
  132. if (OK_STAT(stat, 0, ATA_BUSY))
  133. printk(KERN_INFO "%s: ATAPI reset complete\n", drive->name);
  134. else {
  135. if (time_before(jiffies, hwif->poll_timeout)) {
  136. ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20,
  137. NULL);
  138. /* continue polling */
  139. return ide_started;
  140. }
  141. /* end of polling */
  142. hwif->polling = 0;
  143. printk(KERN_ERR "%s: ATAPI reset timed-out, status=0x%02x\n",
  144. drive->name, stat);
  145. /* do it the old fashioned way */
  146. return do_reset1(drive, 1);
  147. }
  148. /* done polling */
  149. hwif->polling = 0;
  150. ide_complete_drive_reset(drive, 0);
  151. return ide_stopped;
  152. }
  153. static void ide_reset_report_error(ide_hwif_t *hwif, u8 err)
  154. {
  155. static const char *err_master_vals[] =
  156. { NULL, "passed", "formatter device error",
  157. "sector buffer error", "ECC circuitry error",
  158. "controlling MPU error" };
  159. u8 err_master = err & 0x7f;
  160. printk(KERN_ERR "%s: reset: master: ", hwif->name);
  161. if (err_master && err_master < 6)
  162. printk(KERN_CONT "%s", err_master_vals[err_master]);
  163. else
  164. printk(KERN_CONT "error (0x%02x?)", err);
  165. if (err & 0x80)
  166. printk(KERN_CONT "; slave: failed");
  167. printk(KERN_CONT "\n");
  168. }
  169. /*
  170. * reset_pollfunc() gets invoked to poll the interface for completion every 50ms
  171. * during an ide reset operation. If the drives have not yet responded,
  172. * and we have not yet hit our maximum waiting time, then the timer is restarted
  173. * for another 50ms.
  174. */
  175. static ide_startstop_t reset_pollfunc(ide_drive_t *drive)
  176. {
  177. ide_hwif_t *hwif = drive->hwif;
  178. const struct ide_port_ops *port_ops = hwif->port_ops;
  179. u8 tmp;
  180. int err = 0;
  181. if (port_ops && port_ops->reset_poll) {
  182. err = port_ops->reset_poll(drive);
  183. if (err) {
  184. printk(KERN_ERR "%s: host reset_poll failure for %s.\n",
  185. hwif->name, drive->name);
  186. goto out;
  187. }
  188. }
  189. tmp = hwif->tp_ops->read_status(hwif);
  190. if (!OK_STAT(tmp, 0, ATA_BUSY)) {
  191. if (time_before(jiffies, hwif->poll_timeout)) {
  192. ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL);
  193. /* continue polling */
  194. return ide_started;
  195. }
  196. printk(KERN_ERR "%s: reset timed-out, status=0x%02x\n",
  197. hwif->name, tmp);
  198. drive->failures++;
  199. err = -EIO;
  200. } else {
  201. tmp = ide_read_error(drive);
  202. if (tmp == 1) {
  203. printk(KERN_INFO "%s: reset: success\n", hwif->name);
  204. drive->failures = 0;
  205. } else {
  206. ide_reset_report_error(hwif, tmp);
  207. drive->failures++;
  208. err = -EIO;
  209. }
  210. }
  211. out:
  212. hwif->polling = 0; /* done polling */
  213. ide_complete_drive_reset(drive, err);
  214. return ide_stopped;
  215. }
  216. static void ide_disk_pre_reset(ide_drive_t *drive)
  217. {
  218. int legacy = (drive->id[ATA_ID_CFS_ENABLE_2] & 0x0400) ? 0 : 1;
  219. drive->special.all = 0;
  220. drive->special.b.set_geometry = legacy;
  221. drive->special.b.recalibrate = legacy;
  222. drive->mult_count = 0;
  223. drive->dev_flags &= ~IDE_DFLAG_PARKED;
  224. if ((drive->dev_flags & IDE_DFLAG_KEEP_SETTINGS) == 0 &&
  225. (drive->dev_flags & IDE_DFLAG_USING_DMA) == 0)
  226. drive->mult_req = 0;
  227. if (drive->mult_req != drive->mult_count)
  228. drive->special.b.set_multmode = 1;
  229. }
  230. static void pre_reset(ide_drive_t *drive)
  231. {
  232. const struct ide_port_ops *port_ops = drive->hwif->port_ops;
  233. if (drive->media == ide_disk)
  234. ide_disk_pre_reset(drive);
  235. else
  236. drive->dev_flags |= IDE_DFLAG_POST_RESET;
  237. if (drive->dev_flags & IDE_DFLAG_USING_DMA) {
  238. if (drive->crc_count)
  239. ide_check_dma_crc(drive);
  240. else
  241. ide_dma_off(drive);
  242. }
  243. if ((drive->dev_flags & IDE_DFLAG_KEEP_SETTINGS) == 0) {
  244. if ((drive->dev_flags & IDE_DFLAG_USING_DMA) == 0) {
  245. drive->dev_flags &= ~IDE_DFLAG_UNMASK;
  246. drive->io_32bit = 0;
  247. }
  248. return;
  249. }
  250. if (port_ops && port_ops->pre_reset)
  251. port_ops->pre_reset(drive);
  252. if (drive->current_speed != 0xff)
  253. drive->desired_speed = drive->current_speed;
  254. drive->current_speed = 0xff;
  255. }
  256. /*
  257. * do_reset1() attempts to recover a confused drive by resetting it.
  258. * Unfortunately, resetting a disk drive actually resets all devices on
  259. * the same interface, so it can really be thought of as resetting the
  260. * interface rather than resetting the drive.
  261. *
  262. * ATAPI devices have their own reset mechanism which allows them to be
  263. * individually reset without clobbering other devices on the same interface.
  264. *
  265. * Unfortunately, the IDE interface does not generate an interrupt to let
  266. * us know when the reset operation has finished, so we must poll for this.
  267. * Equally poor, though, is the fact that this may a very long time to complete,
  268. * (up to 30 seconds worstcase). So, instead of busy-waiting here for it,
  269. * we set a timer to poll at 50ms intervals.
  270. */
  271. static ide_startstop_t do_reset1(ide_drive_t *drive, int do_not_try_atapi)
  272. {
  273. ide_hwif_t *hwif = drive->hwif;
  274. struct ide_io_ports *io_ports = &hwif->io_ports;
  275. const struct ide_tp_ops *tp_ops = hwif->tp_ops;
  276. const struct ide_port_ops *port_ops;
  277. ide_drive_t *tdrive;
  278. unsigned long flags, timeout;
  279. int i;
  280. DEFINE_WAIT(wait);
  281. spin_lock_irqsave(&hwif->lock, flags);
  282. /* We must not reset with running handlers */
  283. BUG_ON(hwif->handler != NULL);
  284. /* For an ATAPI device, first try an ATAPI SRST. */
  285. if (drive->media != ide_disk && !do_not_try_atapi) {
  286. pre_reset(drive);
  287. SELECT_DRIVE(drive);
  288. udelay(20);
  289. tp_ops->exec_command(hwif, ATA_CMD_DEV_RESET);
  290. ndelay(400);
  291. hwif->poll_timeout = jiffies + WAIT_WORSTCASE;
  292. hwif->polling = 1;
  293. __ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20, NULL);
  294. spin_unlock_irqrestore(&hwif->lock, flags);
  295. return ide_started;
  296. }
  297. /* We must not disturb devices in the IDE_DFLAG_PARKED state. */
  298. do {
  299. unsigned long now;
  300. prepare_to_wait(&ide_park_wq, &wait, TASK_UNINTERRUPTIBLE);
  301. timeout = jiffies;
  302. ide_port_for_each_present_dev(i, tdrive, hwif) {
  303. if ((tdrive->dev_flags & IDE_DFLAG_PARKED) &&
  304. time_after(tdrive->sleep, timeout))
  305. timeout = tdrive->sleep;
  306. }
  307. now = jiffies;
  308. if (time_before_eq(timeout, now))
  309. break;
  310. spin_unlock_irqrestore(&hwif->lock, flags);
  311. timeout = schedule_timeout_uninterruptible(timeout - now);
  312. spin_lock_irqsave(&hwif->lock, flags);
  313. } while (timeout);
  314. finish_wait(&ide_park_wq, &wait);
  315. /*
  316. * First, reset any device state data we were maintaining
  317. * for any of the drives on this interface.
  318. */
  319. ide_port_for_each_dev(i, tdrive, hwif)
  320. pre_reset(tdrive);
  321. if (io_ports->ctl_addr == 0) {
  322. spin_unlock_irqrestore(&hwif->lock, flags);
  323. ide_complete_drive_reset(drive, -ENXIO);
  324. return ide_stopped;
  325. }
  326. /*
  327. * Note that we also set nIEN while resetting the device,
  328. * to mask unwanted interrupts from the interface during the reset.
  329. * However, due to the design of PC hardware, this will cause an
  330. * immediate interrupt due to the edge transition it produces.
  331. * This single interrupt gives us a "fast poll" for drives that
  332. * recover from reset very quickly, saving us the first 50ms wait time.
  333. *
  334. * TODO: add ->softreset method and stop abusing ->set_irq
  335. */
  336. /* set SRST and nIEN */
  337. tp_ops->set_irq(hwif, 4);
  338. /* more than enough time */
  339. udelay(10);
  340. /* clear SRST, leave nIEN (unless device is on the quirk list) */
  341. tp_ops->set_irq(hwif, drive->quirk_list == 2);
  342. /* more than enough time */
  343. udelay(10);
  344. hwif->poll_timeout = jiffies + WAIT_WORSTCASE;
  345. hwif->polling = 1;
  346. __ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL);
  347. /*
  348. * Some weird controller like resetting themselves to a strange
  349. * state when the disks are reset this way. At least, the Winbond
  350. * 553 documentation says that
  351. */
  352. port_ops = hwif->port_ops;
  353. if (port_ops && port_ops->resetproc)
  354. port_ops->resetproc(drive);
  355. spin_unlock_irqrestore(&hwif->lock, flags);
  356. return ide_started;
  357. }
  358. /*
  359. * ide_do_reset() is the entry point to the drive/interface reset code.
  360. */
  361. ide_startstop_t ide_do_reset(ide_drive_t *drive)
  362. {
  363. return do_reset1(drive, 0);
  364. }
  365. EXPORT_SYMBOL(ide_do_reset);