ide-eh.c 12 KB

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