ide-eh.c 12 KB

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