cap.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Thunderbolt driver - capabilities lookup
  4. *
  5. * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
  6. * Copyright (C) 2018, Intel Corporation
  7. */
  8. #include <linux/slab.h>
  9. #include <linux/errno.h>
  10. #include "tb.h"
  11. #define CAP_OFFSET_MAX 0xff
  12. #define VSE_CAP_OFFSET_MAX 0xffff
  13. #define TMU_ACCESS_EN BIT(20)
  14. static int tb_port_enable_tmu(struct tb_port *port, bool enable)
  15. {
  16. struct tb_switch *sw = port->sw;
  17. u32 value, offset;
  18. int ret;
  19. /*
  20. * Legacy devices need to have TMU access enabled before port
  21. * space can be fully accessed.
  22. */
  23. if (tb_switch_is_light_ridge(sw))
  24. offset = 0x26;
  25. else if (tb_switch_is_eagle_ridge(sw))
  26. offset = 0x2a;
  27. else
  28. return 0;
  29. ret = tb_sw_read(sw, &value, TB_CFG_SWITCH, offset, 1);
  30. if (ret)
  31. return ret;
  32. if (enable)
  33. value |= TMU_ACCESS_EN;
  34. else
  35. value &= ~TMU_ACCESS_EN;
  36. return tb_sw_write(sw, &value, TB_CFG_SWITCH, offset, 1);
  37. }
  38. static void tb_port_dummy_read(struct tb_port *port)
  39. {
  40. /*
  41. * When reading from next capability pointer location in port
  42. * config space the read data is not cleared on LR. To avoid
  43. * reading stale data on next read perform one dummy read after
  44. * port capabilities are walked.
  45. */
  46. if (tb_switch_is_light_ridge(port->sw)) {
  47. u32 dummy;
  48. tb_port_read(port, &dummy, TB_CFG_PORT, 0, 1);
  49. }
  50. }
  51. /**
  52. * tb_port_next_cap() - Return next capability in the linked list
  53. * @port: Port to find the capability for
  54. * @offset: Previous capability offset (%0 for start)
  55. *
  56. * Finds dword offset of the next capability in port config space
  57. * capability list. When passed %0 in @offset parameter, first entry
  58. * will be returned, if it exists.
  59. *
  60. * Return:
  61. * * Double word offset of the first or next capability - On success.
  62. * * %0 - If no next capability is found.
  63. * * Negative errno - Another error occurred.
  64. */
  65. int tb_port_next_cap(struct tb_port *port, unsigned int offset)
  66. {
  67. struct tb_cap_any header;
  68. int ret;
  69. if (!offset)
  70. return port->config.first_cap_offset;
  71. ret = tb_port_read(port, &header, TB_CFG_PORT, offset, 1);
  72. if (ret)
  73. return ret;
  74. return header.basic.next;
  75. }
  76. static int __tb_port_find_cap(struct tb_port *port, enum tb_port_cap cap)
  77. {
  78. int offset = 0;
  79. do {
  80. struct tb_cap_any header;
  81. int ret;
  82. offset = tb_port_next_cap(port, offset);
  83. if (offset < 0)
  84. return offset;
  85. ret = tb_port_read(port, &header, TB_CFG_PORT, offset, 1);
  86. if (ret)
  87. return ret;
  88. if (header.basic.cap == cap)
  89. return offset;
  90. } while (offset > 0);
  91. return -ENOENT;
  92. }
  93. /**
  94. * tb_port_find_cap() - Find port capability
  95. * @port: Port to find the capability for
  96. * @cap: Capability to look
  97. *
  98. * Return:
  99. * * Offset to the start of capability - On success.
  100. * * %-ENOENT - If no such capability was found.
  101. * * Negative errno - Another error occurred.
  102. */
  103. int tb_port_find_cap(struct tb_port *port, enum tb_port_cap cap)
  104. {
  105. int ret;
  106. ret = tb_port_enable_tmu(port, true);
  107. if (ret)
  108. return ret;
  109. ret = __tb_port_find_cap(port, cap);
  110. tb_port_dummy_read(port);
  111. tb_port_enable_tmu(port, false);
  112. return ret;
  113. }
  114. /**
  115. * tb_switch_next_cap() - Return next capability in the linked list
  116. * @sw: Switch to find the capability for
  117. * @offset: Previous capability offset (%0 for start)
  118. *
  119. * Finds dword offset of the next capability in port config space
  120. * capability list. When passed %0 in @offset parameter, first entry
  121. * will be returned, if it exists.
  122. *
  123. * Return:
  124. * * Double word offset of the first or next capability - On success.
  125. * * %0 - If no next capability is found.
  126. * * Negative errno - Another error occurred.
  127. */
  128. int tb_switch_next_cap(struct tb_switch *sw, unsigned int offset)
  129. {
  130. struct tb_cap_any header;
  131. int ret;
  132. if (!offset)
  133. return sw->config.first_cap_offset;
  134. ret = tb_sw_read(sw, &header, TB_CFG_SWITCH, offset, 2);
  135. if (ret)
  136. return ret;
  137. switch (header.basic.cap) {
  138. case TB_SWITCH_CAP_TMU:
  139. ret = header.basic.next;
  140. break;
  141. case TB_SWITCH_CAP_VSE:
  142. if (!header.extended_short.length)
  143. ret = header.extended_long.next;
  144. else
  145. ret = header.extended_short.next;
  146. break;
  147. default:
  148. tb_sw_dbg(sw, "unknown capability %#x at %#x\n",
  149. header.basic.cap, offset);
  150. ret = -EINVAL;
  151. break;
  152. }
  153. return ret >= VSE_CAP_OFFSET_MAX ? 0 : ret;
  154. }
  155. /**
  156. * tb_switch_find_cap() - Find switch capability
  157. * @sw: Switch to find the capability for
  158. * @cap: Capability to look
  159. *
  160. * Return:
  161. * * Offset to the start of capability - On success.
  162. * * %-ENOENT - If no such capability was found.
  163. * * Negative errno - Another error occurred.
  164. */
  165. int tb_switch_find_cap(struct tb_switch *sw, enum tb_switch_cap cap)
  166. {
  167. int offset = 0;
  168. do {
  169. struct tb_cap_any header;
  170. int ret;
  171. offset = tb_switch_next_cap(sw, offset);
  172. if (offset < 0)
  173. return offset;
  174. ret = tb_sw_read(sw, &header, TB_CFG_SWITCH, offset, 1);
  175. if (ret)
  176. return ret;
  177. if (header.basic.cap == cap)
  178. return offset;
  179. } while (offset);
  180. return -ENOENT;
  181. }
  182. /**
  183. * tb_switch_find_vse_cap() - Find switch vendor specific capability
  184. * @sw: Switch to find the capability for
  185. * @vsec: Vendor specific capability to look
  186. *
  187. * This function enumerates vendor specific capabilities (VSEC) of a
  188. * switch and returns offset when capability matching @vsec is found.
  189. *
  190. * Return:
  191. * * Offset of capability - On success.
  192. * * %-ENOENT - If capability was not found.
  193. * * Negative errno - Another error occurred.
  194. */
  195. int tb_switch_find_vse_cap(struct tb_switch *sw, enum tb_switch_vse_cap vsec)
  196. {
  197. int offset = 0;
  198. do {
  199. struct tb_cap_any header;
  200. int ret;
  201. offset = tb_switch_next_cap(sw, offset);
  202. if (offset < 0)
  203. return offset;
  204. ret = tb_sw_read(sw, &header, TB_CFG_SWITCH, offset, 1);
  205. if (ret)
  206. return ret;
  207. if (header.extended_short.cap == TB_SWITCH_CAP_VSE &&
  208. header.extended_short.vsec_id == vsec)
  209. return offset;
  210. } while (offset);
  211. return -ENOENT;
  212. }