ide-xfer-mode.c 6.7 KB

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