retimer.c 13 KB

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