ide-xfer-mode.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/types.h>
  3. #include <linux/string.h>
  4. #include <linux/kernel.h>
  5. #include <linux/export.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/ide.h>
  8. #include <linux/bitops.h>
  9. static const char *udma_str[] =
  10. { "UDMA/16", "UDMA/25", "UDMA/33", "UDMA/44",
  11. "UDMA/66", "UDMA/100", "UDMA/133", "UDMA7" };
  12. static const char *mwdma_str[] =
  13. { "MWDMA0", "MWDMA1", "MWDMA2", "MWDMA3", "MWDMA4" };
  14. static const char *swdma_str[] =
  15. { "SWDMA0", "SWDMA1", "SWDMA2" };
  16. static const char *pio_str[] =
  17. { "PIO0", "PIO1", "PIO2", "PIO3", "PIO4", "PIO5", "PIO6" };
  18. /**
  19. * ide_xfer_verbose - return IDE mode names
  20. * @mode: transfer mode
  21. *
  22. * Returns a constant string giving the name of the mode
  23. * requested.
  24. */
  25. const char *ide_xfer_verbose(u8 mode)
  26. {
  27. const char *s;
  28. u8 i = mode & 0xf;
  29. if (mode >= XFER_UDMA_0 && mode <= XFER_UDMA_7)
  30. s = udma_str[i];
  31. else if (mode >= XFER_MW_DMA_0 && mode <= XFER_MW_DMA_4)
  32. s = mwdma_str[i];
  33. else if (mode >= XFER_SW_DMA_0 && mode <= XFER_SW_DMA_2)
  34. s = swdma_str[i];
  35. else if (mode >= XFER_PIO_0 && mode <= XFER_PIO_6)
  36. s = pio_str[i & 0x7];
  37. else if (mode == XFER_PIO_SLOW)
  38. s = "PIO SLOW";
  39. else
  40. s = "XFER ERROR";
  41. return s;
  42. }
  43. EXPORT_SYMBOL(ide_xfer_verbose);
  44. /**
  45. * ide_get_best_pio_mode - get PIO mode from drive
  46. * @drive: drive to consider
  47. * @mode_wanted: preferred mode
  48. * @max_mode: highest allowed mode
  49. *
  50. * This routine returns the recommended PIO settings for a given drive,
  51. * based on the drive->id information and the ide_pio_blacklist[].
  52. *
  53. * Drive PIO mode is auto-selected if 255 is passed as mode_wanted.
  54. * This is used by most chipset support modules when "auto-tuning".
  55. */
  56. static u8 ide_get_best_pio_mode(ide_drive_t *drive, u8 mode_wanted, u8 max_mode)
  57. {
  58. u16 *id = drive->id;
  59. int pio_mode = -1, overridden = 0;
  60. if (mode_wanted != 255)
  61. return min_t(u8, mode_wanted, max_mode);
  62. if ((drive->hwif->host_flags & IDE_HFLAG_PIO_NO_BLACKLIST) == 0)
  63. pio_mode = ide_scan_pio_blacklist((char *)&id[ATA_ID_PROD]);
  64. if (pio_mode != -1) {
  65. printk(KERN_INFO "%s: is on PIO blacklist\n", drive->name);
  66. } else {
  67. pio_mode = id[ATA_ID_OLD_PIO_MODES] >> 8;
  68. if (pio_mode > 2) { /* 2 is maximum allowed tPIO value */
  69. pio_mode = 2;
  70. overridden = 1;
  71. }
  72. if (id[ATA_ID_FIELD_VALID] & 2) { /* ATA2? */
  73. if (ata_id_is_cfa(id) && (id[ATA_ID_CFA_MODES] & 7))
  74. pio_mode = 4 + min_t(int, 2,
  75. id[ATA_ID_CFA_MODES] & 7);
  76. else if (ata_id_has_iordy(id)) {
  77. if (id[ATA_ID_PIO_MODES] & 7) {
  78. overridden = 0;
  79. if (id[ATA_ID_PIO_MODES] & 4)
  80. pio_mode = 5;
  81. else if (id[ATA_ID_PIO_MODES] & 2)
  82. pio_mode = 4;
  83. else
  84. pio_mode = 3;
  85. }
  86. }
  87. }
  88. if (overridden)
  89. printk(KERN_INFO "%s: tPIO > 2, assuming tPIO = 2\n",
  90. drive->name);
  91. }
  92. if (pio_mode > max_mode)
  93. pio_mode = max_mode;
  94. return pio_mode;
  95. }
  96. int ide_pio_need_iordy(ide_drive_t *drive, const u8 pio)
  97. {
  98. /*
  99. * IORDY may lead to controller lock up on certain controllers
  100. * if the port is not occupied.
  101. */
  102. if (pio == 0 && (drive->hwif->port_flags & IDE_PFLAG_PROBING))
  103. return 0;
  104. return ata_id_pio_need_iordy(drive->id, pio);
  105. }
  106. EXPORT_SYMBOL_GPL(ide_pio_need_iordy);
  107. int ide_set_pio_mode(ide_drive_t *drive, const u8 mode)
  108. {
  109. ide_hwif_t *hwif = drive->hwif;
  110. const struct ide_port_ops *port_ops = hwif->port_ops;
  111. if (hwif->host_flags & IDE_HFLAG_NO_SET_MODE)
  112. return 0;
  113. if (port_ops == NULL || port_ops->set_pio_mode == NULL)
  114. return -1;
  115. /*
  116. * TODO: temporary hack for some legacy host drivers that didn't
  117. * set transfer mode on the device in ->set_pio_mode method...
  118. */
  119. if (port_ops->set_dma_mode == NULL) {
  120. drive->pio_mode = mode;
  121. port_ops->set_pio_mode(hwif, drive);
  122. return 0;
  123. }
  124. if (hwif->host_flags & IDE_HFLAG_POST_SET_MODE) {
  125. if (ide_config_drive_speed(drive, mode))
  126. return -1;
  127. drive->pio_mode = mode;
  128. port_ops->set_pio_mode(hwif, drive);
  129. return 0;
  130. } else {
  131. drive->pio_mode = mode;
  132. port_ops->set_pio_mode(hwif, drive);
  133. return ide_config_drive_speed(drive, mode);
  134. }
  135. }
  136. int ide_set_dma_mode(ide_drive_t *drive, const u8 mode)
  137. {
  138. ide_hwif_t *hwif = drive->hwif;
  139. const struct ide_port_ops *port_ops = hwif->port_ops;
  140. if (hwif->host_flags & IDE_HFLAG_NO_SET_MODE)
  141. return 0;
  142. if (port_ops == NULL || port_ops->set_dma_mode == NULL)
  143. return -1;
  144. if (hwif->host_flags & IDE_HFLAG_POST_SET_MODE) {
  145. if (ide_config_drive_speed(drive, mode))
  146. return -1;
  147. drive->dma_mode = mode;
  148. port_ops->set_dma_mode(hwif, drive);
  149. return 0;
  150. } else {
  151. drive->dma_mode = mode;
  152. port_ops->set_dma_mode(hwif, drive);
  153. return ide_config_drive_speed(drive, mode);
  154. }
  155. }
  156. EXPORT_SYMBOL_GPL(ide_set_dma_mode);
  157. /* req_pio == "255" for auto-tune */
  158. void ide_set_pio(ide_drive_t *drive, u8 req_pio)
  159. {
  160. ide_hwif_t *hwif = drive->hwif;
  161. const struct ide_port_ops *port_ops = hwif->port_ops;
  162. u8 host_pio, pio;
  163. if (port_ops == NULL || port_ops->set_pio_mode == NULL ||
  164. (hwif->host_flags & IDE_HFLAG_NO_SET_MODE))
  165. return;
  166. BUG_ON(hwif->pio_mask == 0x00);
  167. host_pio = fls(hwif->pio_mask) - 1;
  168. pio = ide_get_best_pio_mode(drive, req_pio, host_pio);
  169. /*
  170. * TODO:
  171. * - report device max PIO mode
  172. * - check req_pio != 255 against device max PIO mode
  173. */
  174. printk(KERN_DEBUG "%s: host max PIO%d wanted PIO%d%s selected PIO%d\n",
  175. drive->name, host_pio, req_pio,
  176. req_pio == 255 ? "(auto-tune)" : "", pio);
  177. (void)ide_set_pio_mode(drive, XFER_PIO_0 + pio);
  178. }
  179. EXPORT_SYMBOL_GPL(ide_set_pio);
  180. /**
  181. * ide_rate_filter - filter transfer mode
  182. * @drive: IDE device
  183. * @speed: desired speed
  184. *
  185. * Given the available transfer modes this function returns
  186. * the best available speed at or below the speed requested.
  187. *
  188. * TODO: check device PIO capabilities
  189. */
  190. static u8 ide_rate_filter(ide_drive_t *drive, u8 speed)
  191. {
  192. ide_hwif_t *hwif = drive->hwif;
  193. u8 mode = ide_find_dma_mode(drive, speed);
  194. if (mode == 0) {
  195. if (hwif->pio_mask)
  196. mode = fls(hwif->pio_mask) - 1 + XFER_PIO_0;
  197. else
  198. mode = XFER_PIO_4;
  199. }
  200. /* printk("%s: mode 0x%02x, speed 0x%02x\n", __func__, mode, speed); */
  201. return min(speed, mode);
  202. }
  203. /**
  204. * ide_set_xfer_rate - set transfer rate
  205. * @drive: drive to set
  206. * @rate: speed to attempt to set
  207. *
  208. * General helper for setting the speed of an IDE device. This
  209. * function knows about user enforced limits from the configuration
  210. * which ->set_pio_mode/->set_dma_mode does not.
  211. */
  212. int ide_set_xfer_rate(ide_drive_t *drive, u8 rate)
  213. {
  214. ide_hwif_t *hwif = drive->hwif;
  215. const struct ide_port_ops *port_ops = hwif->port_ops;
  216. if (port_ops == NULL || port_ops->set_dma_mode == NULL ||
  217. (hwif->host_flags & IDE_HFLAG_NO_SET_MODE))
  218. return -1;
  219. rate = ide_rate_filter(drive, rate);
  220. BUG_ON(rate < XFER_PIO_0);
  221. if (rate >= XFER_PIO_0 && rate <= XFER_PIO_6)
  222. return ide_set_pio_mode(drive, rate);
  223. return ide_set_dma_mode(drive, rate);
  224. }