ide-eh.c 12 KB

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