retimer.c 13 KB

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