ide-eh.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
  106. struct ide_cmd *cmd = rq->special;
  107. if (cmd)
  108. ide_complete_cmd(drive, cmd, stat, err);
  109. } else if (blk_pm_request(rq)) {
  110. ide_complete_pm_rq(drive, rq);
  111. return ide_stopped;
  112. }
  113. ide_complete_rq(drive, err);
  114. return ide_stopped;
  115. }
  116. return __ide_error(drive, rq, stat, err);
  117. }
  118. EXPORT_SYMBOL_GPL(ide_error);
  119. static inline void ide_complete_drive_reset(ide_drive_t *drive, int err)
  120. {
  121. struct request *rq = drive->hwif->rq;
  122. if (rq && blk_special_request(rq) && rq->cmd[0] == REQ_DRIVE_RESET)
  123. ide_end_request(drive, err ? err : 1, 0);
  124. }
  125. /* needed below */
  126. static ide_startstop_t do_reset1(ide_drive_t *, int);
  127. /*
  128. * atapi_reset_pollfunc() gets invoked to poll the interface for completion
  129. * every 50ms during an atapi drive reset operation. If the drive has not yet
  130. * responded, and we have not yet hit our maximum waiting time, then the timer
  131. * is restarted for another 50ms.
  132. */
  133. static ide_startstop_t atapi_reset_pollfunc(ide_drive_t *drive)
  134. {
  135. ide_hwif_t *hwif = drive->hwif;
  136. u8 stat;
  137. SELECT_DRIVE(drive);
  138. udelay(10);
  139. stat = hwif->tp_ops->read_status(hwif);
  140. if (OK_STAT(stat, 0, ATA_BUSY))
  141. printk(KERN_INFO "%s: ATAPI reset complete\n", drive->name);
  142. else {
  143. if (time_before(jiffies, hwif->poll_timeout)) {
  144. ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20,
  145. NULL);
  146. /* continue polling */
  147. return ide_started;
  148. }
  149. /* end of polling */
  150. hwif->polling = 0;
  151. printk(KERN_ERR "%s: ATAPI reset timed-out, status=0x%02x\n",
  152. drive->name, stat);
  153. /* do it the old fashioned way */
  154. return do_reset1(drive, 1);
  155. }
  156. /* done polling */
  157. hwif->polling = 0;
  158. ide_complete_drive_reset(drive, 0);
  159. return ide_stopped;
  160. }
  161. static void ide_reset_report_error(ide_hwif_t *hwif, u8 err)
  162. {
  163. static const char *err_master_vals[] =
  164. { NULL, "passed", "formatter device error",
  165. "sector buffer error", "ECC circuitry error",
  166. "controlling MPU error" };
  167. u8 err_master = err & 0x7f;
  168. printk(KERN_ERR "%s: reset: master: ", hwif->name);
  169. if (err_master && err_master < 6)
  170. printk(KERN_CONT "%s", err_master_vals[err_master]);
  171. else
  172. printk(KERN_CONT "error (0x%02x?)", err);
  173. if (err & 0x80)
  174. printk(KERN_CONT "; slave: failed");
  175. printk(KERN_CONT "\n");
  176. }
  177. /*
  178. * reset_pollfunc() gets invoked to poll the interface for completion every 50ms
  179. * during an ide reset operation. If the drives have not yet responded,
  180. * and we have not yet hit our maximum waiting time, then the timer is restarted
  181. * for another 50ms.
  182. */
  183. static ide_startstop_t reset_pollfunc(ide_drive_t *drive)
  184. {
  185. ide_hwif_t *hwif = drive->hwif;
  186. const struct ide_port_ops *port_ops = hwif->port_ops;
  187. u8 tmp;
  188. int err = 0;
  189. if (port_ops && port_ops->reset_poll) {
  190. err = port_ops->reset_poll(drive);
  191. if (err) {
  192. printk(KERN_ERR "%s: host reset_poll failure for %s.\n",
  193. hwif->name, drive->name);
  194. goto out;
  195. }
  196. }
  197. tmp = hwif->tp_ops->read_status(hwif);
  198. if (!OK_STAT(tmp, 0, ATA_BUSY)) {
  199. if (time_before(jiffies, hwif->poll_timeout)) {
  200. ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL);
  201. /* continue polling */
  202. return ide_started;
  203. }
  204. printk(KERN_ERR "%s: reset timed-out, status=0x%02x\n",
  205. hwif->name, tmp);
  206. drive->failures++;
  207. err = -EIO;
  208. } else {
  209. tmp = ide_read_error(drive);
  210. if (tmp == 1) {
  211. printk(KERN_INFO "%s: reset: success\n", hwif->name);
  212. drive->failures = 0;
  213. } else {
  214. ide_reset_report_error(hwif, tmp);
  215. drive->failures++;
  216. err = -EIO;
  217. }
  218. }
  219. out:
  220. hwif->polling = 0; /* done polling */
  221. ide_complete_drive_reset(drive, err);
  222. return ide_stopped;
  223. }
  224. static void ide_disk_pre_reset(ide_drive_t *drive)
  225. {
  226. int legacy = (drive->id[ATA_ID_CFS_ENABLE_2] & 0x0400) ? 0 : 1;
  227. drive->special.all = 0;
  228. drive->special.b.set_geometry = legacy;
  229. drive->special.b.recalibrate = legacy;
  230. drive->mult_count = 0;
  231. drive->dev_flags &= ~IDE_DFLAG_PARKED;
  232. if ((drive->dev_flags & IDE_DFLAG_KEEP_SETTINGS) == 0 &&
  233. (drive->dev_flags & IDE_DFLAG_USING_DMA) == 0)
  234. drive->mult_req = 0;
  235. if (drive->mult_req != drive->mult_count)
  236. drive->special.b.set_multmode = 1;
  237. }
  238. static void pre_reset(ide_drive_t *drive)
  239. {
  240. const struct ide_port_ops *port_ops = drive->hwif->port_ops;
  241. if (drive->media == ide_disk)
  242. ide_disk_pre_reset(drive);
  243. else
  244. drive->dev_flags |= IDE_DFLAG_POST_RESET;
  245. if (drive->dev_flags & IDE_DFLAG_USING_DMA) {
  246. if (drive->crc_count)
  247. ide_check_dma_crc(drive);
  248. else
  249. ide_dma_off(drive);
  250. }
  251. if ((drive->dev_flags & IDE_DFLAG_KEEP_SETTINGS) == 0) {
  252. if ((drive->dev_flags & IDE_DFLAG_USING_DMA) == 0) {
  253. drive->dev_flags &= ~IDE_DFLAG_UNMASK;
  254. drive->io_32bit = 0;
  255. }
  256. return;
  257. }
  258. if (port_ops && port_ops->pre_reset)
  259. port_ops->pre_reset(drive);
  260. if (drive->current_speed != 0xff)
  261. drive->desired_speed = drive->current_speed;
  262. drive->current_speed = 0xff;
  263. }
  264. /*
  265. * do_reset1() attempts to recover a confused drive by resetting it.
  266. * Unfortunately, resetting a disk drive actually resets all devices on
  267. * the same interface, so it can really be thought of as resetting the
  268. * interface rather than resetting the drive.
  269. *
  270. * ATAPI devices have their own reset mechanism which allows them to be
  271. * individually reset without clobbering other devices on the same interface.
  272. *
  273. * Unfortunately, the IDE interface does not generate an interrupt to let
  274. * us know when the reset operation has finished, so we must poll for this.
  275. * Equally poor, though, is the fact that this may a very long time to complete,
  276. * (up to 30 seconds worstcase). So, instead of busy-waiting here for it,
  277. * we set a timer to poll at 50ms intervals.
  278. */
  279. static ide_startstop_t do_reset1(ide_drive_t *drive, int do_not_try_atapi)
  280. {
  281. ide_hwif_t *hwif = drive->hwif;
  282. struct ide_io_ports *io_ports = &hwif->io_ports;
  283. const struct ide_tp_ops *tp_ops = hwif->tp_ops;
  284. const struct ide_port_ops *port_ops;
  285. ide_drive_t *tdrive;
  286. unsigned long flags, timeout;
  287. int i;
  288. DEFINE_WAIT(wait);
  289. spin_lock_irqsave(&hwif->lock, flags);
  290. /* We must not reset with running handlers */
  291. BUG_ON(hwif->handler != NULL);
  292. /* For an ATAPI device, first try an ATAPI SRST. */
  293. if (drive->media != ide_disk && !do_not_try_atapi) {
  294. pre_reset(drive);
  295. SELECT_DRIVE(drive);
  296. udelay(20);
  297. tp_ops->exec_command(hwif, ATA_CMD_DEV_RESET);
  298. ndelay(400);
  299. hwif->poll_timeout = jiffies + WAIT_WORSTCASE;
  300. hwif->polling = 1;
  301. __ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20, NULL);
  302. spin_unlock_irqrestore(&hwif->lock, flags);
  303. return ide_started;
  304. }
  305. /* We must not disturb devices in the IDE_DFLAG_PARKED state. */
  306. do {
  307. unsigned long now;
  308. prepare_to_wait(&ide_park_wq, &wait, TASK_UNINTERRUPTIBLE);
  309. timeout = jiffies;
  310. ide_port_for_each_present_dev(i, tdrive, hwif) {
  311. if ((tdrive->dev_flags & IDE_DFLAG_PARKED) &&
  312. time_after(tdrive->sleep, timeout))
  313. timeout = tdrive->sleep;
  314. }
  315. now = jiffies;
  316. if (time_before_eq(timeout, now))
  317. break;
  318. spin_unlock_irqrestore(&hwif->lock, flags);
  319. timeout = schedule_timeout_uninterruptible(timeout - now);
  320. spin_lock_irqsave(&hwif->lock, flags);
  321. } while (timeout);
  322. finish_wait(&ide_park_wq, &wait);
  323. /*
  324. * First, reset any device state data we were maintaining
  325. * for any of the drives on this interface.
  326. */
  327. ide_port_for_each_dev(i, tdrive, hwif)
  328. pre_reset(tdrive);
  329. if (io_ports->ctl_addr == 0) {
  330. spin_unlock_irqrestore(&hwif->lock, flags);
  331. ide_complete_drive_reset(drive, -ENXIO);
  332. return ide_stopped;
  333. }
  334. /*
  335. * Note that we also set nIEN while resetting the device,
  336. * to mask unwanted interrupts from the interface during the reset.
  337. * However, due to the design of PC hardware, this will cause an
  338. * immediate interrupt due to the edge transition it produces.
  339. * This single interrupt gives us a "fast poll" for drives that
  340. * recover from reset very quickly, saving us the first 50ms wait time.
  341. *
  342. * TODO: add ->softreset method and stop abusing ->set_irq
  343. */
  344. /* set SRST and nIEN */
  345. tp_ops->set_irq(hwif, 4);
  346. /* more than enough time */
  347. udelay(10);
  348. /* clear SRST, leave nIEN (unless device is on the quirk list) */
  349. tp_ops->set_irq(hwif, drive->quirk_list == 2);
  350. /* more than enough time */
  351. udelay(10);
  352. hwif->poll_timeout = jiffies + WAIT_WORSTCASE;
  353. hwif->polling = 1;
  354. __ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL);
  355. /*
  356. * Some weird controller like resetting themselves to a strange
  357. * state when the disks are reset this way. At least, the Winbond
  358. * 553 documentation says that
  359. */
  360. port_ops = hwif->port_ops;
  361. if (port_ops && port_ops->resetproc)
  362. port_ops->resetproc(drive);
  363. spin_unlock_irqrestore(&hwif->lock, flags);
  364. return ide_started;
  365. }
  366. /*
  367. * ide_do_reset() is the entry point to the drive/interface reset code.
  368. */
  369. ide_startstop_t ide_do_reset(ide_drive_t *drive)
  370. {
  371. return do_reset1(drive, 0);
  372. }
  373. EXPORT_SYMBOL(ide_do_reset);