cap.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. * Returns dword offset of the next capability in port config space
  57. * capability list and returns it. Passing %0 returns the first entry in
  58. * the capability list. If no next capability is found returns %0. In case
  59. * of failure returns negative errno.
  60. */
  61. int tb_port_next_cap(struct tb_port *port, unsigned int offset)
  62. {
  63. struct tb_cap_any header;
  64. int ret;
  65. if (!offset)
  66. return port->config.first_cap_offset;
  67. ret = tb_port_read(port, &header, TB_CFG_PORT, offset, 1);
  68. if (ret)
  69. return ret;
  70. return header.basic.next;
  71. }
  72. static int __tb_port_find_cap(struct tb_port *port, enum tb_port_cap cap)
  73. {
  74. int offset = 0;
  75. do {
  76. struct tb_cap_any header;
  77. int ret;
  78. offset = tb_port_next_cap(port, offset);
  79. if (offset < 0)
  80. return offset;
  81. ret = tb_port_read(port, &header, TB_CFG_PORT, offset, 1);
  82. if (ret)
  83. return ret;
  84. if (header.basic.cap == cap)
  85. return offset;
  86. } while (offset > 0);
  87. return -ENOENT;
  88. }
  89. /**
  90. * tb_port_find_cap() - Find port capability
  91. * @port: Port to find the capability for
  92. * @cap: Capability to look
  93. *
  94. * Returns offset to start of capability or %-ENOENT if no such
  95. * capability was found. Negative errno is returned if there was an
  96. * error.
  97. */
  98. int tb_port_find_cap(struct tb_port *port, enum tb_port_cap cap)
  99. {
  100. int ret;
  101. ret = tb_port_enable_tmu(port, true);
  102. if (ret)
  103. return ret;
  104. ret = __tb_port_find_cap(port, cap);
  105. tb_port_dummy_read(port);
  106. tb_port_enable_tmu(port, false);
  107. return ret;
  108. }
  109. /**
  110. * tb_switch_next_cap() - Return next capability in the linked list
  111. * @sw: Switch to find the capability for
  112. * @offset: Previous capability offset (%0 for start)
  113. *
  114. * Finds dword offset of the next capability in router config space
  115. * capability list and returns it. Passing %0 returns the first entry in
  116. * the capability list. If no next capability is found returns %0. In case
  117. * of failure returns negative errno.
  118. */
  119. int tb_switch_next_cap(struct tb_switch *sw, unsigned int offset)
  120. {
  121. struct tb_cap_any header;
  122. int ret;
  123. if (!offset)
  124. return sw->config.first_cap_offset;
  125. ret = tb_sw_read(sw, &header, TB_CFG_SWITCH, offset, 2);
  126. if (ret)
  127. return ret;
  128. switch (header.basic.cap) {
  129. case TB_SWITCH_CAP_TMU:
  130. ret = header.basic.next;
  131. break;
  132. case TB_SWITCH_CAP_VSE:
  133. if (!header.extended_short.length)
  134. ret = header.extended_long.next;
  135. else
  136. ret = header.extended_short.next;
  137. break;
  138. default:
  139. tb_sw_dbg(sw, "unknown capability %#x at %#x\n",
  140. header.basic.cap, offset);
  141. ret = -EINVAL;
  142. break;
  143. }
  144. return ret >= VSE_CAP_OFFSET_MAX ? 0 : ret;
  145. }
  146. /**
  147. * tb_switch_find_cap() - Find switch capability
  148. * @sw Switch to find the capability for
  149. * @cap: Capability to look
  150. *
  151. * Returns offset to start of capability or %-ENOENT if no such
  152. * capability was found. Negative errno is returned if there was an
  153. * error.
  154. */
  155. int tb_switch_find_cap(struct tb_switch *sw, enum tb_switch_cap cap)
  156. {
  157. int offset = 0;
  158. do {
  159. struct tb_cap_any header;
  160. int ret;
  161. offset = tb_switch_next_cap(sw, offset);
  162. if (offset < 0)
  163. return offset;
  164. ret = tb_sw_read(sw, &header, TB_CFG_SWITCH, offset, 1);
  165. if (ret)
  166. return ret;
  167. if (header.basic.cap == cap)
  168. return offset;
  169. } while (offset);
  170. return -ENOENT;
  171. }
  172. /**
  173. * tb_switch_find_vse_cap() - Find switch vendor specific capability
  174. * @sw: Switch to find the capability for
  175. * @vsec: Vendor specific capability to look
  176. *
  177. * Functions enumerates vendor specific capabilities (VSEC) of a switch
  178. * and returns offset when capability matching @vsec is found. If no
  179. * such capability is found returns %-ENOENT. In case of error returns
  180. * negative errno.
  181. */
  182. int tb_switch_find_vse_cap(struct tb_switch *sw, enum tb_switch_vse_cap vsec)
  183. {
  184. int offset = 0;
  185. do {
  186. struct tb_cap_any header;
  187. int ret;
  188. offset = tb_switch_next_cap(sw, offset);
  189. if (offset < 0)
  190. return offset;
  191. ret = tb_sw_read(sw, &header, TB_CFG_SWITCH, offset, 1);
  192. if (ret)
  193. return ret;
  194. if (header.extended_short.cap == TB_SWITCH_CAP_VSE &&
  195. header.extended_short.vsec_id == vsec)
  196. return offset;
  197. } while (offset);
  198. return -ENOENT;
  199. }