retimer.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Thunderbolt/USB4 retimer support.
  4. *
  5. * Copyright (C) 2020, Intel Corporation
  6. * Authors: Kranthi Kuntala <[email protected]>
  7. * Mika Westerberg <[email protected]>
  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. /**
  16. * tb_retimer_nvm_read() - Read contents of retimer NVM
  17. * @rt: Retimer device
  18. * @address: NVM address (in bytes) to start reading
  19. * @buf: Data read from NVM is stored here
  20. * @size: Number of bytes to read
  21. *
  22. * Reads retimer NVM and copies the contents to @buf. Returns %0 if the
  23. * read was successful and negative errno in case of failure.
  24. */
  25. int tb_retimer_nvm_read(struct tb_retimer *rt, unsigned int address, void *buf,
  26. size_t size)
  27. {
  28. return usb4_port_retimer_nvm_read(rt->port, rt->index, address, buf, size);
  29. }
  30. static int nvm_read(void *priv, unsigned int offset, void *val, size_t bytes)
  31. {
  32. struct tb_nvm *nvm = priv;
  33. struct tb_retimer *rt = tb_to_retimer(nvm->dev);
  34. int ret;
  35. pm_runtime_get_sync(&rt->dev);
  36. if (!mutex_trylock(&rt->tb->lock)) {
  37. ret = restart_syscall();
  38. goto out;
  39. }
  40. ret = tb_retimer_nvm_read(rt, offset, val, bytes);
  41. mutex_unlock(&rt->tb->lock);
  42. out:
  43. pm_runtime_mark_last_busy(&rt->dev);
  44. pm_runtime_put_autosuspend(&rt->dev);
  45. return ret;
  46. }
  47. static int nvm_write(void *priv, unsigned int offset, void *val, size_t bytes)
  48. {
  49. struct tb_nvm *nvm = priv;
  50. struct tb_retimer *rt = tb_to_retimer(nvm->dev);
  51. int ret = 0;
  52. if (!mutex_trylock(&rt->tb->lock))
  53. return restart_syscall();
  54. ret = tb_nvm_write_buf(nvm, offset, val, bytes);
  55. mutex_unlock(&rt->tb->lock);
  56. return ret;
  57. }
  58. static int tb_retimer_nvm_add(struct tb_retimer *rt)
  59. {
  60. struct tb_nvm *nvm;
  61. int ret;
  62. nvm = tb_nvm_alloc(&rt->dev);
  63. if (IS_ERR(nvm)) {
  64. ret = PTR_ERR(nvm) == -EOPNOTSUPP ? 0 : PTR_ERR(nvm);
  65. goto err_nvm;
  66. }
  67. ret = tb_nvm_read_version(nvm);
  68. if (ret)
  69. goto err_nvm;
  70. ret = tb_nvm_add_active(nvm, nvm_read);
  71. if (ret)
  72. goto err_nvm;
  73. ret = tb_nvm_add_non_active(nvm, nvm_write);
  74. if (ret)
  75. goto err_nvm;
  76. rt->nvm = nvm;
  77. return 0;
  78. err_nvm:
  79. dev_dbg(&rt->dev, "NVM upgrade disabled\n");
  80. if (!IS_ERR(nvm))
  81. tb_nvm_free(nvm);
  82. return ret;
  83. }
  84. static int tb_retimer_nvm_validate_and_write(struct tb_retimer *rt)
  85. {
  86. unsigned int image_size;
  87. const u8 *buf;
  88. int ret;
  89. ret = tb_nvm_validate(rt->nvm);
  90. if (ret)
  91. return ret;
  92. buf = rt->nvm->buf_data_start;
  93. image_size = rt->nvm->buf_data_size;
  94. ret = usb4_port_retimer_nvm_write(rt->port, rt->index, 0, buf,
  95. image_size);
  96. if (ret)
  97. return ret;
  98. rt->nvm->flushed = true;
  99. return 0;
  100. }
  101. static int tb_retimer_nvm_authenticate(struct tb_retimer *rt, bool auth_only)
  102. {
  103. u32 status;
  104. int ret;
  105. if (auth_only) {
  106. ret = usb4_port_retimer_nvm_set_offset(rt->port, rt->index, 0);
  107. if (ret)
  108. return ret;
  109. }
  110. ret = usb4_port_retimer_nvm_authenticate(rt->port, rt->index);
  111. if (ret)
  112. return ret;
  113. usleep_range(100, 150);
  114. /*
  115. * Check the status now if we still can access the retimer. It
  116. * is expected that the below fails.
  117. */
  118. ret = usb4_port_retimer_nvm_authenticate_status(rt->port, rt->index,
  119. &status);
  120. if (!ret) {
  121. rt->auth_status = status;
  122. return status ? -EINVAL : 0;
  123. }
  124. return 0;
  125. }
  126. static ssize_t device_show(struct device *dev, struct device_attribute *attr,
  127. char *buf)
  128. {
  129. struct tb_retimer *rt = tb_to_retimer(dev);
  130. return sysfs_emit(buf, "%#x\n", rt->device);
  131. }
  132. static DEVICE_ATTR_RO(device);
  133. static ssize_t nvm_authenticate_show(struct device *dev,
  134. struct device_attribute *attr, char *buf)
  135. {
  136. struct tb_retimer *rt = tb_to_retimer(dev);
  137. int ret;
  138. if (!mutex_trylock(&rt->tb->lock))
  139. return restart_syscall();
  140. if (!rt->nvm)
  141. ret = -EAGAIN;
  142. else if (rt->no_nvm_upgrade)
  143. ret = -EOPNOTSUPP;
  144. else
  145. ret = sysfs_emit(buf, "%#x\n", rt->auth_status);
  146. mutex_unlock(&rt->tb->lock);
  147. return ret;
  148. }
  149. static void tb_retimer_nvm_authenticate_status(struct tb_port *port, u32 *status)
  150. {
  151. int i;
  152. tb_port_dbg(port, "reading NVM authentication status of retimers\n");
  153. /*
  154. * Before doing anything else, read the authentication status.
  155. * If the retimer has it set, store it for the new retimer
  156. * device instance.
  157. */
  158. for (i = 1; i <= TB_MAX_RETIMER_INDEX; i++)
  159. usb4_port_retimer_nvm_authenticate_status(port, i, &status[i]);
  160. }
  161. static void tb_retimer_set_inbound_sbtx(struct tb_port *port)
  162. {
  163. int i;
  164. for (i = 1; i <= TB_MAX_RETIMER_INDEX; i++)
  165. usb4_port_retimer_set_inbound_sbtx(port, i);
  166. }
  167. static void tb_retimer_unset_inbound_sbtx(struct tb_port *port)
  168. {
  169. int i;
  170. for (i = TB_MAX_RETIMER_INDEX; i >= 1; i--)
  171. usb4_port_retimer_unset_inbound_sbtx(port, i);
  172. }
  173. static ssize_t nvm_authenticate_store(struct device *dev,
  174. struct device_attribute *attr, const char *buf, size_t count)
  175. {
  176. struct tb_retimer *rt = tb_to_retimer(dev);
  177. int val, ret;
  178. pm_runtime_get_sync(&rt->dev);
  179. if (!mutex_trylock(&rt->tb->lock)) {
  180. ret = restart_syscall();
  181. goto exit_rpm;
  182. }
  183. if (!rt->nvm) {
  184. ret = -EAGAIN;
  185. goto exit_unlock;
  186. }
  187. ret = kstrtoint(buf, 10, &val);
  188. if (ret)
  189. goto exit_unlock;
  190. /* Always clear status */
  191. rt->auth_status = 0;
  192. if (val) {
  193. tb_retimer_set_inbound_sbtx(rt->port);
  194. if (val == AUTHENTICATE_ONLY) {
  195. ret = tb_retimer_nvm_authenticate(rt, true);
  196. } else {
  197. if (!rt->nvm->flushed) {
  198. if (!rt->nvm->buf) {
  199. ret = -EINVAL;
  200. goto exit_unlock;
  201. }
  202. ret = tb_retimer_nvm_validate_and_write(rt);
  203. if (ret || val == WRITE_ONLY)
  204. goto exit_unlock;
  205. }
  206. if (val == WRITE_AND_AUTHENTICATE)
  207. ret = tb_retimer_nvm_authenticate(rt, false);
  208. }
  209. }
  210. exit_unlock:
  211. tb_retimer_unset_inbound_sbtx(rt->port);
  212. mutex_unlock(&rt->tb->lock);
  213. exit_rpm:
  214. pm_runtime_mark_last_busy(&rt->dev);
  215. pm_runtime_put_autosuspend(&rt->dev);
  216. if (ret)
  217. return ret;
  218. return count;
  219. }
  220. static DEVICE_ATTR_RW(nvm_authenticate);
  221. static ssize_t nvm_version_show(struct device *dev,
  222. struct device_attribute *attr, char *buf)
  223. {
  224. struct tb_retimer *rt = tb_to_retimer(dev);
  225. int ret;
  226. if (!mutex_trylock(&rt->tb->lock))
  227. return restart_syscall();
  228. if (!rt->nvm)
  229. ret = -EAGAIN;
  230. else
  231. ret = sysfs_emit(buf, "%x.%x\n", rt->nvm->major, rt->nvm->minor);
  232. mutex_unlock(&rt->tb->lock);
  233. return ret;
  234. }
  235. static DEVICE_ATTR_RO(nvm_version);
  236. static ssize_t vendor_show(struct device *dev, struct device_attribute *attr,
  237. char *buf)
  238. {
  239. struct tb_retimer *rt = tb_to_retimer(dev);
  240. return sysfs_emit(buf, "%#x\n", rt->vendor);
  241. }
  242. static DEVICE_ATTR_RO(vendor);
  243. static struct attribute *retimer_attrs[] = {
  244. &dev_attr_device.attr,
  245. &dev_attr_nvm_authenticate.attr,
  246. &dev_attr_nvm_version.attr,
  247. &dev_attr_vendor.attr,
  248. NULL
  249. };
  250. static const struct attribute_group retimer_group = {
  251. .attrs = retimer_attrs,
  252. };
  253. static const struct attribute_group *retimer_groups[] = {
  254. &retimer_group,
  255. NULL
  256. };
  257. static void tb_retimer_release(struct device *dev)
  258. {
  259. struct tb_retimer *rt = tb_to_retimer(dev);
  260. kfree(rt);
  261. }
  262. struct device_type tb_retimer_type = {
  263. .name = "thunderbolt_retimer",
  264. .groups = retimer_groups,
  265. .release = tb_retimer_release,
  266. };
  267. static int tb_retimer_add(struct tb_port *port, u8 index, u32 auth_status)
  268. {
  269. struct tb_retimer *rt;
  270. u32 vendor, device;
  271. int ret;
  272. ret = usb4_port_retimer_read(port, index, USB4_SB_VENDOR_ID, &vendor,
  273. sizeof(vendor));
  274. if (ret) {
  275. if (ret != -ENODEV)
  276. tb_port_warn(port, "failed read retimer VendorId: %d\n", ret);
  277. return ret;
  278. }
  279. ret = usb4_port_retimer_read(port, index, USB4_SB_PRODUCT_ID, &device,
  280. sizeof(device));
  281. if (ret) {
  282. if (ret != -ENODEV)
  283. tb_port_warn(port, "failed read retimer ProductId: %d\n", ret);
  284. return ret;
  285. }
  286. if (vendor != PCI_VENDOR_ID_INTEL && vendor != 0x8087) {
  287. tb_port_info(port, "retimer NVM format of vendor %#x is not supported\n",
  288. vendor);
  289. return -EOPNOTSUPP;
  290. }
  291. /*
  292. * Check that it supports NVM operations. If not then don't add
  293. * the device at all.
  294. */
  295. ret = usb4_port_retimer_nvm_sector_size(port, index);
  296. if (ret < 0)
  297. return ret;
  298. rt = kzalloc(sizeof(*rt), GFP_KERNEL);
  299. if (!rt)
  300. return -ENOMEM;
  301. rt->index = index;
  302. rt->vendor = vendor;
  303. rt->device = device;
  304. rt->auth_status = auth_status;
  305. rt->port = port;
  306. rt->tb = port->sw->tb;
  307. rt->dev.parent = &port->usb4->dev;
  308. rt->dev.bus = &tb_bus_type;
  309. rt->dev.type = &tb_retimer_type;
  310. dev_set_name(&rt->dev, "%s:%u.%u", dev_name(&port->sw->dev),
  311. port->port, index);
  312. ret = device_register(&rt->dev);
  313. if (ret) {
  314. dev_err(&rt->dev, "failed to register retimer: %d\n", ret);
  315. put_device(&rt->dev);
  316. return ret;
  317. }
  318. ret = tb_retimer_nvm_add(rt);
  319. if (ret) {
  320. dev_err(&rt->dev, "failed to add NVM devices: %d\n", ret);
  321. device_unregister(&rt->dev);
  322. return ret;
  323. }
  324. dev_info(&rt->dev, "new retimer found, vendor=%#x device=%#x\n",
  325. rt->vendor, rt->device);
  326. pm_runtime_no_callbacks(&rt->dev);
  327. pm_runtime_set_active(&rt->dev);
  328. pm_runtime_enable(&rt->dev);
  329. pm_runtime_set_autosuspend_delay(&rt->dev, TB_AUTOSUSPEND_DELAY);
  330. pm_runtime_mark_last_busy(&rt->dev);
  331. pm_runtime_use_autosuspend(&rt->dev);
  332. return 0;
  333. }
  334. static void tb_retimer_remove(struct tb_retimer *rt)
  335. {
  336. dev_info(&rt->dev, "retimer disconnected\n");
  337. tb_nvm_free(rt->nvm);
  338. device_unregister(&rt->dev);
  339. }
  340. struct tb_retimer_lookup {
  341. const struct tb_port *port;
  342. u8 index;
  343. };
  344. static int retimer_match(struct device *dev, void *data)
  345. {
  346. const struct tb_retimer_lookup *lookup = data;
  347. struct tb_retimer *rt = tb_to_retimer(dev);
  348. return rt && rt->port == lookup->port && rt->index == lookup->index;
  349. }
  350. static struct tb_retimer *tb_port_find_retimer(struct tb_port *port, u8 index)
  351. {
  352. struct tb_retimer_lookup lookup = { .port = port, .index = index };
  353. struct device *dev;
  354. dev = device_find_child(&port->usb4->dev, &lookup, retimer_match);
  355. if (dev)
  356. return tb_to_retimer(dev);
  357. return NULL;
  358. }
  359. /**
  360. * tb_retimer_scan() - Scan for on-board retimers under port
  361. * @port: USB4 port to scan
  362. * @add: If true also registers found retimers
  363. *
  364. * Brings the sideband into a state where retimers can be accessed.
  365. * Then Tries to enumerate on-board retimers connected to @port. Found
  366. * retimers are registered as children of @port if @add is set. Does
  367. * not scan for cable retimers for now.
  368. */
  369. int tb_retimer_scan(struct tb_port *port, bool add)
  370. {
  371. u32 status[TB_MAX_RETIMER_INDEX + 1] = {};
  372. int ret, i, last_idx = 0;
  373. /*
  374. * Send broadcast RT to make sure retimer indices facing this
  375. * port are set.
  376. */
  377. ret = usb4_port_enumerate_retimers(port);
  378. if (ret)
  379. return ret;
  380. /*
  381. * Immediately after sending enumerate retimers read the
  382. * authentication status of each retimer.
  383. */
  384. tb_retimer_nvm_authenticate_status(port, status);
  385. /*
  386. * Enable sideband channel for each retimer. We can do this
  387. * regardless whether there is device connected or not.
  388. */
  389. tb_retimer_set_inbound_sbtx(port);
  390. for (i = 1; i <= TB_MAX_RETIMER_INDEX; i++) {
  391. /*
  392. * Last retimer is true only for the last on-board
  393. * retimer (the one connected directly to the Type-C
  394. * port).
  395. */
  396. ret = usb4_port_retimer_is_last(port, i);
  397. if (ret > 0)
  398. last_idx = i;
  399. else if (ret < 0)
  400. break;
  401. }
  402. tb_retimer_unset_inbound_sbtx(port);
  403. if (!last_idx)
  404. return 0;
  405. /* Add on-board retimers if they do not exist already */
  406. ret = 0;
  407. for (i = 1; i <= last_idx; i++) {
  408. struct tb_retimer *rt;
  409. rt = tb_port_find_retimer(port, i);
  410. if (rt) {
  411. put_device(&rt->dev);
  412. } else if (add) {
  413. ret = tb_retimer_add(port, i, status[i]);
  414. if (ret && ret != -EOPNOTSUPP)
  415. break;
  416. }
  417. }
  418. return ret;
  419. }
  420. static int remove_retimer(struct device *dev, void *data)
  421. {
  422. struct tb_retimer *rt = tb_to_retimer(dev);
  423. struct tb_port *port = data;
  424. if (rt && rt->port == port)
  425. tb_retimer_remove(rt);
  426. return 0;
  427. }
  428. /**
  429. * tb_retimer_remove_all() - Remove all retimers under port
  430. * @port: USB4 port whose retimers to remove
  431. *
  432. * This removes all previously added retimers under @port.
  433. */
  434. void tb_retimer_remove_all(struct tb_port *port)
  435. {
  436. struct usb4_port *usb4;
  437. usb4 = port->usb4;
  438. if (usb4)
  439. device_for_each_child_reverse(&usb4->dev, port,
  440. remove_retimer);
  441. }