ide-iops.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2000-2002 Andre Hedrick <andre@linux-ide.org>
  4. * Copyright (C) 2003 Red Hat
  5. *
  6. */
  7. #include <linux/module.h>
  8. #include <linux/types.h>
  9. #include <linux/string.h>
  10. #include <linux/kernel.h>
  11. #include <linux/timer.h>
  12. #include <linux/mm.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/major.h>
  15. #include <linux/errno.h>
  16. #include <linux/genhd.h>
  17. #include <linux/blkpg.h>
  18. #include <linux/slab.h>
  19. #include <linux/pci.h>
  20. #include <linux/delay.h>
  21. #include <linux/ide.h>
  22. #include <linux/bitops.h>
  23. #include <linux/nmi.h>
  24. #include <asm/byteorder.h>
  25. #include <asm/irq.h>
  26. #include <linux/uaccess.h>
  27. #include <asm/io.h>
  28. void SELECT_MASK(ide_drive_t *drive, int mask)
  29. {
  30. const struct ide_port_ops *port_ops = drive->hwif->port_ops;
  31. if (port_ops && port_ops->maskproc)
  32. port_ops->maskproc(drive, mask);
  33. }
  34. u8 ide_read_error(ide_drive_t *drive)
  35. {
  36. struct ide_taskfile tf;
  37. drive->hwif->tp_ops->tf_read(drive, &tf, IDE_VALID_ERROR);
  38. return tf.error;
  39. }
  40. EXPORT_SYMBOL_GPL(ide_read_error);
  41. void ide_fix_driveid(u16 *id)
  42. {
  43. #ifndef __LITTLE_ENDIAN
  44. # ifdef __BIG_ENDIAN
  45. int i;
  46. for (i = 0; i < 256; i++)
  47. id[i] = __le16_to_cpu(id[i]);
  48. # else
  49. # error "Please fix <asm/byteorder.h>"
  50. # endif
  51. #endif
  52. }
  53. /*
  54. * ide_fixstring() cleans up and (optionally) byte-swaps a text string,
  55. * removing leading/trailing blanks and compressing internal blanks.
  56. * It is primarily used to tidy up the model name/number fields as
  57. * returned by the ATA_CMD_ID_ATA[PI] commands.
  58. */
  59. void ide_fixstring(u8 *s, const int bytecount, const int byteswap)
  60. {
  61. u8 *p, *end = &s[bytecount & ~1]; /* bytecount must be even */
  62. if (byteswap) {
  63. /* convert from big-endian to host byte order */
  64. for (p = s ; p != end ; p += 2)
  65. be16_to_cpus((u16 *) p);
  66. }
  67. /* strip leading blanks */
  68. p = s;
  69. while (s != end && *s == ' ')
  70. ++s;
  71. /* compress internal blanks and strip trailing blanks */
  72. while (s != end && *s) {
  73. if (*s++ != ' ' || (s != end && *s && *s != ' '))
  74. *p++ = *(s-1);
  75. }
  76. /* wipe out trailing garbage */
  77. while (p != end)
  78. *p++ = '\0';
  79. }
  80. EXPORT_SYMBOL(ide_fixstring);
  81. /*
  82. * This routine busy-waits for the drive status to be not "busy".
  83. * It then checks the status for all of the "good" bits and none
  84. * of the "bad" bits, and if all is okay it returns 0. All other
  85. * cases return error -- caller may then invoke ide_error().
  86. *
  87. * This routine should get fixed to not hog the cpu during extra long waits..
  88. * That could be done by busy-waiting for the first jiffy or two, and then
  89. * setting a timer to wake up at half second intervals thereafter,
  90. * until timeout is achieved, before timing out.
  91. */
  92. int __ide_wait_stat(ide_drive_t *drive, u8 good, u8 bad,
  93. unsigned long timeout, u8 *rstat)
  94. {
  95. ide_hwif_t *hwif = drive->hwif;
  96. const struct ide_tp_ops *tp_ops = hwif->tp_ops;
  97. unsigned long flags;
  98. bool irqs_threaded = force_irqthreads;
  99. int i;
  100. u8 stat;
  101. udelay(1); /* spec allows drive 400ns to assert "BUSY" */
  102. stat = tp_ops->read_status(hwif);
  103. if (stat & ATA_BUSY) {
  104. if (!irqs_threaded) {
  105. local_save_flags(flags);
  106. local_irq_enable_in_hardirq();
  107. }
  108. timeout += jiffies;
  109. while ((stat = tp_ops->read_status(hwif)) & ATA_BUSY) {
  110. if (time_after(jiffies, timeout)) {
  111. /*
  112. * One last read after the timeout in case
  113. * heavy interrupt load made us not make any
  114. * progress during the timeout..
  115. */
  116. stat = tp_ops->read_status(hwif);
  117. if ((stat & ATA_BUSY) == 0)
  118. break;
  119. if (!irqs_threaded)
  120. local_irq_restore(flags);
  121. *rstat = stat;
  122. return -EBUSY;
  123. }
  124. }
  125. if (!irqs_threaded)
  126. local_irq_restore(flags);
  127. }
  128. /*
  129. * Allow status to settle, then read it again.
  130. * A few rare drives vastly violate the 400ns spec here,
  131. * so we'll wait up to 10usec for a "good" status
  132. * rather than expensively fail things immediately.
  133. * This fix courtesy of Matthew Faupel & Niccolo Rigacci.
  134. */
  135. for (i = 0; i < 10; i++) {
  136. udelay(1);
  137. stat = tp_ops->read_status(hwif);
  138. if (OK_STAT(stat, good, bad)) {
  139. *rstat = stat;
  140. return 0;
  141. }
  142. }
  143. *rstat = stat;
  144. return -EFAULT;
  145. }
  146. /*
  147. * In case of error returns error value after doing "*startstop = ide_error()".
  148. * The caller should return the updated value of "startstop" in this case,
  149. * "startstop" is unchanged when the function returns 0.
  150. */
  151. int ide_wait_stat(ide_startstop_t *startstop, ide_drive_t *drive, u8 good,
  152. u8 bad, unsigned long timeout)
  153. {
  154. int err;
  155. u8 stat;
  156. /* bail early if we've exceeded max_failures */
  157. if (drive->max_failures && (drive->failures > drive->max_failures)) {
  158. *startstop = ide_stopped;
  159. return 1;
  160. }
  161. err = __ide_wait_stat(drive, good, bad, timeout, &stat);
  162. if (err) {
  163. char *s = (err == -EBUSY) ? "status timeout" : "status error";
  164. *startstop = ide_error(drive, s, stat);
  165. }
  166. return err;
  167. }
  168. EXPORT_SYMBOL(ide_wait_stat);
  169. /**
  170. * ide_in_drive_list - look for drive in black/white list
  171. * @id: drive identifier
  172. * @table: list to inspect
  173. *
  174. * Look for a drive in the blacklist and the whitelist tables
  175. * Returns 1 if the drive is found in the table.
  176. */
  177. int ide_in_drive_list(u16 *id, const struct drive_list_entry *table)
  178. {
  179. for ( ; table->id_model; table++)
  180. if ((!strcmp(table->id_model, (char *)&id[ATA_ID_PROD])) &&
  181. (!table->id_firmware ||
  182. strstr((char *)&id[ATA_ID_FW_REV], table->id_firmware)))
  183. return 1;
  184. return 0;
  185. }
  186. EXPORT_SYMBOL_GPL(ide_in_drive_list);
  187. /*
  188. * Early UDMA66 devices don't set bit14 to 1, only bit13 is valid.
  189. * Some optical devices with the buggy firmwares have the same problem.
  190. */
  191. static const struct drive_list_entry ivb_list[] = {
  192. { "QUANTUM FIREBALLlct10 05" , "A03.0900" },
  193. { "QUANTUM FIREBALLlct20 30" , "APL.0900" },
  194. { "TSSTcorp CDDVDW SH-S202J" , "SB00" },
  195. { "TSSTcorp CDDVDW SH-S202J" , "SB01" },
  196. { "TSSTcorp CDDVDW SH-S202N" , "SB00" },
  197. { "TSSTcorp CDDVDW SH-S202N" , "SB01" },
  198. { "TSSTcorp CDDVDW SH-S202H" , "SB00" },
  199. { "TSSTcorp CDDVDW SH-S202H" , "SB01" },
  200. { "SAMSUNG SP0822N" , "WA100-10" },
  201. { NULL , NULL }
  202. };
  203. /*
  204. * All hosts that use the 80c ribbon must use!
  205. * The name is derived from upper byte of word 93 and the 80c ribbon.
  206. */
  207. u8 eighty_ninty_three(ide_drive_t *drive)
  208. {
  209. ide_hwif_t *hwif = drive->hwif;
  210. u16 *id = drive->id;
  211. int ivb = ide_in_drive_list(id, ivb_list);
  212. if (hwif->cbl == ATA_CBL_SATA || hwif->cbl == ATA_CBL_PATA40_SHORT)
  213. return 1;
  214. if (ivb)
  215. printk(KERN_DEBUG "%s: skipping word 93 validity check\n",
  216. drive->name);
  217. if (ata_id_is_sata(id) && !ivb)
  218. return 1;
  219. if (hwif->cbl != ATA_CBL_PATA80 && !ivb)
  220. goto no_80w;
  221. /*
  222. * FIXME:
  223. * - change master/slave IDENTIFY order
  224. * - force bit13 (80c cable present) check also for !ivb devices
  225. * (unless the slave device is pre-ATA3)
  226. */
  227. if (id[ATA_ID_HW_CONFIG] & 0x4000)
  228. return 1;
  229. if (ivb) {
  230. const char *model = (char *)&id[ATA_ID_PROD];
  231. if (strstr(model, "TSSTcorp CDDVDW SH-S202")) {
  232. /*
  233. * These ATAPI devices always report 80c cable
  234. * so we have to depend on the host in this case.
  235. */
  236. if (hwif->cbl == ATA_CBL_PATA80)
  237. return 1;
  238. } else {
  239. /* Depend on the device side cable detection. */
  240. if (id[ATA_ID_HW_CONFIG] & 0x2000)
  241. return 1;
  242. }
  243. }
  244. no_80w:
  245. if (drive->dev_flags & IDE_DFLAG_UDMA33_WARNED)
  246. return 0;
  247. printk(KERN_WARNING "%s: %s side 80-wire cable detection failed, "
  248. "limiting max speed to UDMA33\n",
  249. drive->name,
  250. hwif->cbl == ATA_CBL_PATA80 ? "drive" : "host");
  251. drive->dev_flags |= IDE_DFLAG_UDMA33_WARNED;
  252. return 0;
  253. }
  254. static const char *nien_quirk_list[] = {
  255. "QUANTUM FIREBALLlct08 08",
  256. "QUANTUM FIREBALLP KA6.4",
  257. "QUANTUM FIREBALLP KA9.1",
  258. "QUANTUM FIREBALLP KX13.6",
  259. "QUANTUM FIREBALLP KX20.5",
  260. "QUANTUM FIREBALLP KX27.3",
  261. "QUANTUM FIREBALLP LM20.4",
  262. "QUANTUM FIREBALLP LM20.5",
  263. "FUJITSU MHZ2160BH G2",
  264. NULL
  265. };
  266. void ide_check_nien_quirk_list(ide_drive_t *drive)
  267. {
  268. const char **list, *m = (char *)&drive->id[ATA_ID_PROD];
  269. for (list = nien_quirk_list; *list != NULL; list++)
  270. if (strstr(m, *list) != NULL) {
  271. drive->dev_flags |= IDE_DFLAG_NIEN_QUIRK;
  272. return;
  273. }
  274. }
  275. int ide_driveid_update(ide_drive_t *drive)
  276. {
  277. u16 *id;
  278. int rc;
  279. id = kmalloc(SECTOR_SIZE, GFP_ATOMIC);
  280. if (id == NULL)
  281. return 0;
  282. SELECT_MASK(drive, 1);
  283. rc = ide_dev_read_id(drive, ATA_CMD_ID_ATA, id, 1);
  284. SELECT_MASK(drive, 0);
  285. if (rc)
  286. goto out_err;
  287. drive->id[ATA_ID_UDMA_MODES] = id[ATA_ID_UDMA_MODES];
  288. drive->id[ATA_ID_MWDMA_MODES] = id[ATA_ID_MWDMA_MODES];
  289. drive->id[ATA_ID_SWDMA_MODES] = id[ATA_ID_SWDMA_MODES];
  290. drive->id[ATA_ID_CFA_MODES] = id[ATA_ID_CFA_MODES];
  291. /* anything more ? */
  292. kfree(id);
  293. return 1;
  294. out_err:
  295. if (rc == 2)
  296. printk(KERN_ERR "%s: %s: bad status\n", drive->name, __func__);
  297. kfree(id);
  298. return 0;
  299. }
  300. int ide_config_drive_speed(ide_drive_t *drive, u8 speed)
  301. {
  302. ide_hwif_t *hwif = drive->hwif;
  303. const struct ide_tp_ops *tp_ops = hwif->tp_ops;
  304. struct ide_taskfile tf;
  305. u16 *id = drive->id, i;
  306. int error = 0;
  307. u8 stat;
  308. #ifdef CONFIG_BLK_DEV_IDEDMA
  309. if (hwif->dma_ops) /* check if host supports DMA */
  310. hwif->dma_ops->dma_host_set(drive, 0);
  311. #endif
  312. /* Skip setting PIO flow-control modes on pre-EIDE drives */
  313. if ((speed & 0xf8) == XFER_PIO_0 && ata_id_has_iordy(drive->id) == 0)
  314. goto skip;
  315. /*
  316. * Don't use ide_wait_cmd here - it will
  317. * attempt to set_geometry and recalibrate,
  318. * but for some reason these don't work at
  319. * this point (lost interrupt).
  320. */
  321. udelay(1);
  322. tp_ops->dev_select(drive);
  323. SELECT_MASK(drive, 1);
  324. udelay(1);
  325. tp_ops->write_devctl(hwif, ATA_NIEN | ATA_DEVCTL_OBS);
  326. memset(&tf, 0, sizeof(tf));
  327. tf.feature = SETFEATURES_XFER;
  328. tf.nsect = speed;
  329. tp_ops->tf_load(drive, &tf, IDE_VALID_FEATURE | IDE_VALID_NSECT);
  330. tp_ops->exec_command(hwif, ATA_CMD_SET_FEATURES);
  331. if (drive->dev_flags & IDE_DFLAG_NIEN_QUIRK)
  332. tp_ops->write_devctl(hwif, ATA_DEVCTL_OBS);
  333. error = __ide_wait_stat(drive, drive->ready_stat,
  334. ATA_BUSY | ATA_DRQ | ATA_ERR,
  335. WAIT_CMD, &stat);
  336. SELECT_MASK(drive, 0);
  337. if (error) {
  338. (void) ide_dump_status(drive, "set_drive_speed_status", stat);
  339. return error;
  340. }
  341. if (speed >= XFER_SW_DMA_0) {
  342. id[ATA_ID_UDMA_MODES] &= ~0xFF00;
  343. id[ATA_ID_MWDMA_MODES] &= ~0x0700;
  344. id[ATA_ID_SWDMA_MODES] &= ~0x0700;
  345. if (ata_id_is_cfa(id))
  346. id[ATA_ID_CFA_MODES] &= ~0x0E00;
  347. } else if (ata_id_is_cfa(id))
  348. id[ATA_ID_CFA_MODES] &= ~0x01C0;
  349. skip:
  350. #ifdef CONFIG_BLK_DEV_IDEDMA
  351. if (speed >= XFER_SW_DMA_0 && (drive->dev_flags & IDE_DFLAG_USING_DMA))
  352. hwif->dma_ops->dma_host_set(drive, 1);
  353. else if (hwif->dma_ops) /* check if host supports DMA */
  354. ide_dma_off_quietly(drive);
  355. #endif
  356. if (speed >= XFER_UDMA_0) {
  357. i = 1 << (speed - XFER_UDMA_0);
  358. id[ATA_ID_UDMA_MODES] |= (i << 8 | i);
  359. } else if (ata_id_is_cfa(id) && speed >= XFER_MW_DMA_3) {
  360. i = speed - XFER_MW_DMA_2;
  361. id[ATA_ID_CFA_MODES] |= i << 9;
  362. } else if (speed >= XFER_MW_DMA_0) {
  363. i = 1 << (speed - XFER_MW_DMA_0);
  364. id[ATA_ID_MWDMA_MODES] |= (i << 8 | i);
  365. } else if (speed >= XFER_SW_DMA_0) {
  366. i = 1 << (speed - XFER_SW_DMA_0);
  367. id[ATA_ID_SWDMA_MODES] |= (i << 8 | i);
  368. } else if (ata_id_is_cfa(id) && speed >= XFER_PIO_5) {
  369. i = speed - XFER_PIO_4;
  370. id[ATA_ID_CFA_MODES] |= i << 6;
  371. }
  372. if (!drive->init_speed)
  373. drive->init_speed = speed;
  374. drive->current_speed = speed;
  375. return error;
  376. }
  377. /*
  378. * This should get invoked any time we exit the driver to
  379. * wait for an interrupt response from a drive. handler() points
  380. * at the appropriate code to handle the next interrupt, and a
  381. * timer is started to prevent us from waiting forever in case
  382. * something goes wrong (see the ide_timer_expiry() handler later on).
  383. *
  384. * See also ide_execute_command
  385. */
  386. void __ide_set_handler(ide_drive_t *drive, ide_handler_t *handler,
  387. unsigned int timeout)
  388. {
  389. ide_hwif_t *hwif = drive->hwif;
  390. BUG_ON(hwif->handler);
  391. hwif->handler = handler;
  392. hwif->timer.expires = jiffies + timeout;
  393. hwif->req_gen_timer = hwif->req_gen;
  394. add_timer(&hwif->timer);
  395. }
  396. void ide_set_handler(ide_drive_t *drive, ide_handler_t *handler,
  397. unsigned int timeout)
  398. {
  399. ide_hwif_t *hwif = drive->hwif;
  400. unsigned long flags;
  401. spin_lock_irqsave(&hwif->lock, flags);
  402. __ide_set_handler(drive, handler, timeout);
  403. spin_unlock_irqrestore(&hwif->lock, flags);
  404. }
  405. EXPORT_SYMBOL(ide_set_handler);
  406. /**
  407. * ide_execute_command - execute an IDE command
  408. * @drive: IDE drive to issue the command against
  409. * @cmd: command
  410. * @handler: handler for next phase
  411. * @timeout: timeout for command
  412. *
  413. * Helper function to issue an IDE command. This handles the
  414. * atomicity requirements, command timing and ensures that the
  415. * handler and IRQ setup do not race. All IDE command kick off
  416. * should go via this function or do equivalent locking.
  417. */
  418. void ide_execute_command(ide_drive_t *drive, struct ide_cmd *cmd,
  419. ide_handler_t *handler, unsigned timeout)
  420. {
  421. ide_hwif_t *hwif = drive->hwif;
  422. unsigned long flags;
  423. spin_lock_irqsave(&hwif->lock, flags);
  424. if ((cmd->protocol != ATAPI_PROT_DMA &&
  425. cmd->protocol != ATAPI_PROT_PIO) ||
  426. (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT))
  427. __ide_set_handler(drive, handler, timeout);
  428. hwif->tp_ops->exec_command(hwif, cmd->tf.command);
  429. /*
  430. * Drive takes 400nS to respond, we must avoid the IRQ being
  431. * serviced before that.
  432. *
  433. * FIXME: we could skip this delay with care on non shared devices
  434. */
  435. ndelay(400);
  436. spin_unlock_irqrestore(&hwif->lock, flags);
  437. }
  438. /*
  439. * ide_wait_not_busy() waits for the currently selected device on the hwif
  440. * to report a non-busy status, see comments in ide_probe_port().
  441. */
  442. int ide_wait_not_busy(ide_hwif_t *hwif, unsigned long timeout)
  443. {
  444. u8 stat = 0;
  445. while (timeout--) {
  446. /*
  447. * Turn this into a schedule() sleep once I'm sure
  448. * about locking issues (2.5 work ?).
  449. */
  450. mdelay(1);
  451. stat = hwif->tp_ops->read_status(hwif);
  452. if ((stat & ATA_BUSY) == 0)
  453. return 0;
  454. /*
  455. * Assume a value of 0xff means nothing is connected to
  456. * the interface and it doesn't implement the pull-down
  457. * resistor on D7.
  458. */
  459. if (stat == 0xff)
  460. return -ENODEV;
  461. touch_nmi_watchdog();
  462. }
  463. return -EBUSY;
  464. }