retimer.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Thunderbolt/USB4 retimer support.
  4. *
  5. * Copyright (C) 2020, Intel Corporation
  6. * Authors: Kranthi Kuntala <kranthi.kuntala@intel.com>
  7. * Mika Westerberg <mika.westerberg@linux.intel.com>
  8. */
  9. #include <linux/delay.h>
  10. #include <linux/pm_runtime.h>
  11. #include <linux/sched/signal.h>
  12. #include "sb_regs.h"
  13. #include "tb.h"
  14. #define TB_MAX_RETIMER_INDEX 6
  15. static int tb_retimer_nvm_read(void *priv, unsigned int offset, void *val,
  16. size_t bytes)
  17. {
  18. struct tb_nvm *nvm = priv;
  19. struct tb_retimer *rt = tb_to_retimer(nvm->dev);
  20. int ret;
  21. pm_runtime_get_sync(&rt->dev);
  22. if (!mutex_trylock(&rt->tb->lock)) {
  23. ret = restart_syscall();
  24. goto out;
  25. }
  26. ret = usb4_port_retimer_nvm_read(rt->port, rt->index, offset, val, bytes);
  27. mutex_unlock(&rt->tb->lock);
  28. out:
  29. pm_runtime_mark_last_busy(&rt->dev);
  30. pm_runtime_put_autosuspend(&rt->dev);
  31. return ret;
  32. }
  33. static int tb_retimer_nvm_write(void *priv, unsigned int offset, void *val,
  34. size_t bytes)
  35. {
  36. struct tb_nvm *nvm = priv;
  37. struct tb_retimer *rt = tb_to_retimer(nvm->dev);
  38. int ret = 0;
  39. if (!mutex_trylock(&rt->tb->lock))
  40. return restart_syscall();
  41. ret = tb_nvm_write_buf(nvm, offset, val, bytes);
  42. mutex_unlock(&rt->tb->lock);
  43. return ret;
  44. }
  45. static int tb_retimer_nvm_add(struct tb_retimer *rt)
  46. {
  47. struct tb_nvm *nvm;
  48. u32 val, nvm_size;
  49. int ret;
  50. nvm = tb_nvm_alloc(&rt->dev);
  51. if (IS_ERR(nvm))
  52. return PTR_ERR(nvm);
  53. ret = usb4_port_retimer_nvm_read(rt->port, rt->index, NVM_VERSION, &val,
  54. sizeof(val));
  55. if (ret)
  56. goto err_nvm;
  57. nvm->major = val >> 16;
  58. nvm->minor = val >> 8;
  59. ret = usb4_port_retimer_nvm_read(rt->port, rt->index, NVM_FLASH_SIZE,
  60. &val, sizeof(val));
  61. if (ret)
  62. goto err_nvm;
  63. nvm_size = (SZ_1M << (val & 7)) / 8;
  64. nvm_size = (nvm_size - SZ_16K) / 2;
  65. ret = tb_nvm_add_active(nvm, nvm_size, tb_retimer_nvm_read);
  66. if (ret)
  67. goto err_nvm;
  68. ret = tb_nvm_add_non_active(nvm, NVM_MAX_SIZE, tb_retimer_nvm_write);
  69. if (ret)
  70. goto err_nvm;
  71. rt->nvm = nvm;
  72. return 0;
  73. err_nvm:
  74. tb_nvm_free(nvm);
  75. return ret;
  76. }
  77. static int tb_retimer_nvm_validate_and_write(struct tb_retimer *rt)
  78. {
  79. unsigned int image_size, hdr_size;
  80. const u8 *buf = rt->nvm->buf;
  81. u16 ds_size, device;
  82. image_size = rt->nvm->buf_data_size;
  83. if (image_size < NVM_MIN_SIZE || image_size > NVM_MAX_SIZE)
  84. return -EINVAL;
  85. /*
  86. * FARB pointer must point inside the image and must at least
  87. * contain parts of the digital section we will be reading here.
  88. */
  89. hdr_size = (*(u32 *)buf) & 0xffffff;
  90. if (hdr_size + NVM_DEVID + 2 >= image_size)
  91. return -EINVAL;
  92. /* Digital section start should be aligned to 4k page */
  93. if (!IS_ALIGNED(hdr_size, SZ_4K))
  94. return -EINVAL;
  95. /*
  96. * Read digital section size and check that it also fits inside
  97. * the image.
  98. */
  99. ds_size = *(u16 *)(buf + hdr_size);
  100. if (ds_size >= image_size)
  101. return -EINVAL;
  102. /*
  103. * Make sure the device ID in the image matches the retimer
  104. * hardware.
  105. */
  106. device = *(u16 *)(buf + hdr_size + NVM_DEVID);
  107. if (device != rt->device)
  108. return -EINVAL;
  109. /* Skip headers in the image */
  110. buf += hdr_size;
  111. image_size -= hdr_size;
  112. return usb4_port_retimer_nvm_write(rt->port, rt->index, 0, buf,
  113. image_size);
  114. }
  115. static ssize_t device_show(struct device *dev, struct device_attribute *attr,
  116. char *buf)
  117. {
  118. struct tb_retimer *rt = tb_to_retimer(dev);
  119. return sprintf(buf, "%#x\n", rt->device);
  120. }
  121. static DEVICE_ATTR_RO(device);
  122. static ssize_t nvm_authenticate_show(struct device *dev,
  123. struct device_attribute *attr, char *buf)
  124. {
  125. struct tb_retimer *rt = tb_to_retimer(dev);
  126. int ret;
  127. if (!mutex_trylock(&rt->tb->lock))
  128. return restart_syscall();
  129. if (!rt->nvm)
  130. ret = -EAGAIN;
  131. else
  132. ret = sprintf(buf, "%#x\n", rt->auth_status);
  133. mutex_unlock(&rt->tb->lock);
  134. return ret;
  135. }
  136. static ssize_t nvm_authenticate_store(struct device *dev,
  137. struct device_attribute *attr, const char *buf, size_t count)
  138. {
  139. struct tb_retimer *rt = tb_to_retimer(dev);
  140. bool val;
  141. int ret;
  142. pm_runtime_get_sync(&rt->dev);
  143. if (!mutex_trylock(&rt->tb->lock)) {
  144. ret = restart_syscall();
  145. goto exit_rpm;
  146. }
  147. if (!rt->nvm) {
  148. ret = -EAGAIN;
  149. goto exit_unlock;
  150. }
  151. ret = kstrtobool(buf, &val);
  152. if (ret)
  153. goto exit_unlock;
  154. /* Always clear status */
  155. rt->auth_status = 0;
  156. if (val) {
  157. if (!rt->nvm->buf) {
  158. ret = -EINVAL;
  159. goto exit_unlock;
  160. }
  161. ret = tb_retimer_nvm_validate_and_write(rt);
  162. if (ret)
  163. goto exit_unlock;
  164. ret = usb4_port_retimer_nvm_authenticate(rt->port, rt->index);
  165. }
  166. exit_unlock:
  167. mutex_unlock(&rt->tb->lock);
  168. exit_rpm:
  169. pm_runtime_mark_last_busy(&rt->dev);
  170. pm_runtime_put_autosuspend(&rt->dev);
  171. if (ret)
  172. return ret;
  173. return count;
  174. }
  175. static DEVICE_ATTR_RW(nvm_authenticate);
  176. static ssize_t nvm_version_show(struct device *dev,
  177. struct device_attribute *attr, char *buf)
  178. {
  179. struct tb_retimer *rt = tb_to_retimer(dev);
  180. int ret;
  181. if (!mutex_trylock(&rt->tb->lock))
  182. return restart_syscall();
  183. if (!rt->nvm)
  184. ret = -EAGAIN;
  185. else
  186. ret = sprintf(buf, "%x.%x\n", rt->nvm->major, rt->nvm->minor);
  187. mutex_unlock(&rt->tb->lock);
  188. return ret;
  189. }
  190. static DEVICE_ATTR_RO(nvm_version);
  191. static ssize_t vendor_show(struct device *dev, struct device_attribute *attr,
  192. char *buf)
  193. {
  194. struct tb_retimer *rt = tb_to_retimer(dev);
  195. return sprintf(buf, "%#x\n", rt->vendor);
  196. }
  197. static DEVICE_ATTR_RO(vendor);
  198. static struct attribute *retimer_attrs[] = {
  199. &dev_attr_device.attr,
  200. &dev_attr_nvm_authenticate.attr,
  201. &dev_attr_nvm_version.attr,
  202. &dev_attr_vendor.attr,
  203. NULL
  204. };
  205. static const struct attribute_group retimer_group = {
  206. .attrs = retimer_attrs,
  207. };
  208. static const struct attribute_group *retimer_groups[] = {
  209. &retimer_group,
  210. NULL
  211. };
  212. static void tb_retimer_release(struct device *dev)
  213. {
  214. struct tb_retimer *rt = tb_to_retimer(dev);
  215. kfree(rt);
  216. }
  217. struct device_type tb_retimer_type = {
  218. .name = "thunderbolt_retimer",
  219. .groups = retimer_groups,
  220. .release = tb_retimer_release,
  221. };
  222. static int tb_retimer_add(struct tb_port *port, u8 index, u32 auth_status)
  223. {
  224. struct tb_retimer *rt;
  225. u32 vendor, device;
  226. int ret;
  227. if (!port->cap_usb4)
  228. return -EINVAL;
  229. ret = usb4_port_retimer_read(port, index, USB4_SB_VENDOR_ID, &vendor,
  230. sizeof(vendor));
  231. if (ret) {
  232. if (ret != -ENODEV)
  233. tb_port_warn(port, "failed read retimer VendorId: %d\n", ret);
  234. return ret;
  235. }
  236. ret = usb4_port_retimer_read(port, index, USB4_SB_PRODUCT_ID, &device,
  237. sizeof(device));
  238. if (ret) {
  239. if (ret != -ENODEV)
  240. tb_port_warn(port, "failed read retimer ProductId: %d\n", ret);
  241. return ret;
  242. }
  243. if (vendor != PCI_VENDOR_ID_INTEL && vendor != 0x8087) {
  244. tb_port_info(port, "retimer NVM format of vendor %#x is not supported\n",
  245. vendor);
  246. return -EOPNOTSUPP;
  247. }
  248. /*
  249. * Check that it supports NVM operations. If not then don't add
  250. * the device at all.
  251. */
  252. ret = usb4_port_retimer_nvm_sector_size(port, index);
  253. if (ret < 0)
  254. return ret;
  255. rt = kzalloc(sizeof(*rt), GFP_KERNEL);
  256. if (!rt)
  257. return -ENOMEM;
  258. rt->index = index;
  259. rt->vendor = vendor;
  260. rt->device = device;
  261. rt->auth_status = auth_status;
  262. rt->port = port;
  263. rt->tb = port->sw->tb;
  264. rt->dev.parent = &port->sw->dev;
  265. rt->dev.bus = &tb_bus_type;
  266. rt->dev.type = &tb_retimer_type;
  267. dev_set_name(&rt->dev, "%s:%u.%u", dev_name(&port->sw->dev),
  268. port->port, index);
  269. ret = device_register(&rt->dev);
  270. if (ret) {
  271. dev_err(&rt->dev, "failed to register retimer: %d\n", ret);
  272. put_device(&rt->dev);
  273. return ret;
  274. }
  275. ret = tb_retimer_nvm_add(rt);
  276. if (ret) {
  277. dev_err(&rt->dev, "failed to add NVM devices: %d\n", ret);
  278. device_unregister(&rt->dev);
  279. return ret;
  280. }
  281. dev_info(&rt->dev, "new retimer found, vendor=%#x device=%#x\n",
  282. rt->vendor, rt->device);
  283. pm_runtime_no_callbacks(&rt->dev);
  284. pm_runtime_set_active(&rt->dev);
  285. pm_runtime_enable(&rt->dev);
  286. pm_runtime_set_autosuspend_delay(&rt->dev, TB_AUTOSUSPEND_DELAY);
  287. pm_runtime_mark_last_busy(&rt->dev);
  288. pm_runtime_use_autosuspend(&rt->dev);
  289. return 0;
  290. }
  291. static void tb_retimer_remove(struct tb_retimer *rt)
  292. {
  293. dev_info(&rt->dev, "retimer disconnected\n");
  294. tb_nvm_free(rt->nvm);
  295. device_unregister(&rt->dev);
  296. }
  297. struct tb_retimer_lookup {
  298. const struct tb_port *port;
  299. u8 index;
  300. };
  301. static int retimer_match(struct device *dev, void *data)
  302. {
  303. const struct tb_retimer_lookup *lookup = data;
  304. struct tb_retimer *rt = tb_to_retimer(dev);
  305. return rt && rt->port == lookup->port && rt->index == lookup->index;
  306. }
  307. static struct tb_retimer *tb_port_find_retimer(struct tb_port *port, u8 index)
  308. {
  309. struct tb_retimer_lookup lookup = { .port = port, .index = index };
  310. struct device *dev;
  311. dev = device_find_child(&port->sw->dev, &lookup, retimer_match);
  312. if (dev)
  313. return tb_to_retimer(dev);
  314. return NULL;
  315. }
  316. /**
  317. * tb_retimer_scan() - Scan for on-board retimers under port
  318. * @port: USB4 port to scan
  319. *
  320. * Tries to enumerate on-board retimers connected to @port. Found
  321. * retimers are registered as children of @port. Does not scan for cable
  322. * retimers for now.
  323. */
  324. int tb_retimer_scan(struct tb_port *port)
  325. {
  326. u32 status[TB_MAX_RETIMER_INDEX + 1] = {};
  327. int ret, i, last_idx = 0;
  328. if (!port->cap_usb4)
  329. return 0;
  330. /*
  331. * Send broadcast RT to make sure retimer indices facing this
  332. * port are set.
  333. */
  334. ret = usb4_port_enumerate_retimers(port);
  335. if (ret)
  336. return ret;
  337. /*
  338. * Before doing anything else, read the authentication status.
  339. * If the retimer has it set, store it for the new retimer
  340. * device instance.
  341. */
  342. for (i = 1; i <= TB_MAX_RETIMER_INDEX; i++)
  343. usb4_port_retimer_nvm_authenticate_status(port, i, &status[i]);
  344. for (i = 1; i <= TB_MAX_RETIMER_INDEX; i++) {
  345. /*
  346. * Last retimer is true only for the last on-board
  347. * retimer (the one connected directly to the Type-C
  348. * port).
  349. */
  350. ret = usb4_port_retimer_is_last(port, i);
  351. if (ret > 0)
  352. last_idx = i;
  353. else if (ret < 0)
  354. break;
  355. }
  356. if (!last_idx)
  357. return 0;
  358. /* Add on-board retimers if they do not exist already */
  359. for (i = 1; i <= last_idx; i++) {
  360. struct tb_retimer *rt;
  361. rt = tb_port_find_retimer(port, i);
  362. if (rt) {
  363. put_device(&rt->dev);
  364. } else {
  365. ret = tb_retimer_add(port, i, status[i]);
  366. if (ret && ret != -EOPNOTSUPP)
  367. return ret;
  368. }
  369. }
  370. return 0;
  371. }
  372. static int remove_retimer(struct device *dev, void *data)
  373. {
  374. struct tb_retimer *rt = tb_to_retimer(dev);
  375. struct tb_port *port = data;
  376. if (rt && rt->port == port)
  377. tb_retimer_remove(rt);
  378. return 0;
  379. }
  380. /**
  381. * tb_retimer_remove_all() - Remove all retimers under port
  382. * @port: USB4 port whose retimers to remove
  383. *
  384. * This removes all previously added retimers under @port.
  385. */
  386. void tb_retimer_remove_all(struct tb_port *port)
  387. {
  388. if (port->cap_usb4)
  389. device_for_each_child_reverse(&port->sw->dev, port,
  390. remove_retimer);
  391. }