ctl.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Thunderbolt driver - control channel and configuration commands
  4. *
  5. * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
  6. * Copyright (C) 2018, Intel Corporation
  7. */
  8. #include <linux/crc32.h>
  9. #include <linux/delay.h>
  10. #include <linux/slab.h>
  11. #include <linux/pci.h>
  12. #include <linux/dmapool.h>
  13. #include <linux/workqueue.h>
  14. #include "ctl.h"
  15. #define CREATE_TRACE_POINTS
  16. #include "trace.h"
  17. #define TB_CTL_RX_PKG_COUNT 10
  18. #define TB_CTL_RETRIES 4
  19. /**
  20. * struct tb_ctl - Thunderbolt control channel
  21. * @nhi: Pointer to the NHI structure
  22. * @tx: Transmit ring
  23. * @rx: Receive ring
  24. * @frame_pool: DMA pool for control messages
  25. * @rx_packets: Received control messages
  26. * @request_queue_lock: Lock protecting @request_queue
  27. * @request_queue: List of outstanding requests
  28. * @running: Is the control channel running at the moment
  29. * @timeout_msec: Default timeout for non-raw control messages
  30. * @callback: Callback called when hotplug message is received
  31. * @callback_data: Data passed to @callback
  32. * @index: Domain number. This will be output with the trace record.
  33. */
  34. struct tb_ctl {
  35. struct tb_nhi *nhi;
  36. struct tb_ring *tx;
  37. struct tb_ring *rx;
  38. struct dma_pool *frame_pool;
  39. struct ctl_pkg *rx_packets[TB_CTL_RX_PKG_COUNT];
  40. struct mutex request_queue_lock;
  41. struct list_head request_queue;
  42. bool running;
  43. int timeout_msec;
  44. event_cb callback;
  45. void *callback_data;
  46. int index;
  47. };
  48. #define tb_ctl_WARN(ctl, format, arg...) \
  49. dev_WARN(&(ctl)->nhi->pdev->dev, format, ## arg)
  50. #define tb_ctl_err(ctl, format, arg...) \
  51. dev_err(&(ctl)->nhi->pdev->dev, format, ## arg)
  52. #define tb_ctl_warn(ctl, format, arg...) \
  53. dev_warn(&(ctl)->nhi->pdev->dev, format, ## arg)
  54. #define tb_ctl_info(ctl, format, arg...) \
  55. dev_info(&(ctl)->nhi->pdev->dev, format, ## arg)
  56. #define tb_ctl_dbg(ctl, format, arg...) \
  57. dev_dbg(&(ctl)->nhi->pdev->dev, format, ## arg)
  58. #define tb_ctl_dbg_once(ctl, format, arg...) \
  59. dev_dbg_once(&(ctl)->nhi->pdev->dev, format, ## arg)
  60. static DECLARE_WAIT_QUEUE_HEAD(tb_cfg_request_cancel_queue);
  61. /* Serializes access to request kref_get/put */
  62. static DEFINE_MUTEX(tb_cfg_request_lock);
  63. /**
  64. * tb_cfg_request_alloc() - Allocates a new config request
  65. *
  66. * This is refcounted object so when you are done with this, call
  67. * tb_cfg_request_put() to it.
  68. *
  69. * Return: &struct tb_cfg_request on success, %NULL otherwise.
  70. */
  71. struct tb_cfg_request *tb_cfg_request_alloc(void)
  72. {
  73. struct tb_cfg_request *req;
  74. req = kzalloc_obj(*req);
  75. if (!req)
  76. return NULL;
  77. kref_init(&req->kref);
  78. return req;
  79. }
  80. /**
  81. * tb_cfg_request_get() - Increase refcount of a request
  82. * @req: Request whose refcount is increased
  83. */
  84. void tb_cfg_request_get(struct tb_cfg_request *req)
  85. {
  86. mutex_lock(&tb_cfg_request_lock);
  87. kref_get(&req->kref);
  88. mutex_unlock(&tb_cfg_request_lock);
  89. }
  90. static void tb_cfg_request_destroy(struct kref *kref)
  91. {
  92. struct tb_cfg_request *req = container_of(kref, typeof(*req), kref);
  93. kfree(req);
  94. }
  95. /**
  96. * tb_cfg_request_put() - Decrease refcount and possibly release the request
  97. * @req: Request whose refcount is decreased
  98. *
  99. * Call this function when you are done with the request. When refcount
  100. * goes to %0 the object is released.
  101. */
  102. void tb_cfg_request_put(struct tb_cfg_request *req)
  103. {
  104. mutex_lock(&tb_cfg_request_lock);
  105. kref_put(&req->kref, tb_cfg_request_destroy);
  106. mutex_unlock(&tb_cfg_request_lock);
  107. }
  108. static int tb_cfg_request_enqueue(struct tb_ctl *ctl,
  109. struct tb_cfg_request *req)
  110. {
  111. WARN_ON(test_bit(TB_CFG_REQUEST_ACTIVE, &req->flags));
  112. WARN_ON(req->ctl);
  113. mutex_lock(&ctl->request_queue_lock);
  114. if (!ctl->running) {
  115. mutex_unlock(&ctl->request_queue_lock);
  116. return -ENOTCONN;
  117. }
  118. req->ctl = ctl;
  119. list_add_tail(&req->list, &ctl->request_queue);
  120. set_bit(TB_CFG_REQUEST_ACTIVE, &req->flags);
  121. mutex_unlock(&ctl->request_queue_lock);
  122. return 0;
  123. }
  124. static void tb_cfg_request_dequeue(struct tb_cfg_request *req)
  125. {
  126. struct tb_ctl *ctl = req->ctl;
  127. mutex_lock(&ctl->request_queue_lock);
  128. if (!test_bit(TB_CFG_REQUEST_ACTIVE, &req->flags)) {
  129. mutex_unlock(&ctl->request_queue_lock);
  130. return;
  131. }
  132. list_del(&req->list);
  133. clear_bit(TB_CFG_REQUEST_ACTIVE, &req->flags);
  134. if (test_bit(TB_CFG_REQUEST_CANCELED, &req->flags))
  135. wake_up(&tb_cfg_request_cancel_queue);
  136. mutex_unlock(&ctl->request_queue_lock);
  137. }
  138. static bool tb_cfg_request_is_active(struct tb_cfg_request *req)
  139. {
  140. return test_bit(TB_CFG_REQUEST_ACTIVE, &req->flags);
  141. }
  142. static struct tb_cfg_request *
  143. tb_cfg_request_find(struct tb_ctl *ctl, struct ctl_pkg *pkg)
  144. {
  145. struct tb_cfg_request *req = NULL, *iter;
  146. mutex_lock(&pkg->ctl->request_queue_lock);
  147. list_for_each_entry(iter, &pkg->ctl->request_queue, list) {
  148. tb_cfg_request_get(iter);
  149. if (iter->match(iter, pkg)) {
  150. req = iter;
  151. break;
  152. }
  153. tb_cfg_request_put(iter);
  154. }
  155. mutex_unlock(&pkg->ctl->request_queue_lock);
  156. return req;
  157. }
  158. /* utility functions */
  159. static int check_header(const struct ctl_pkg *pkg, u32 len,
  160. enum tb_cfg_pkg_type type, u64 route)
  161. {
  162. struct tb_cfg_header *header = pkg->buffer;
  163. /* check frame, TODO: frame flags */
  164. if (WARN(len != pkg->frame.size,
  165. "wrong framesize (expected %#x, got %#x)\n",
  166. len, pkg->frame.size))
  167. return -EIO;
  168. if (WARN(type != pkg->frame.eof, "wrong eof (expected %#x, got %#x)\n",
  169. type, pkg->frame.eof))
  170. return -EIO;
  171. if (WARN(pkg->frame.sof, "wrong sof (expected 0x0, got %#x)\n",
  172. pkg->frame.sof))
  173. return -EIO;
  174. /* check header */
  175. if (WARN(header->unknown != 1 << 9,
  176. "header->unknown is %#x\n", header->unknown))
  177. return -EIO;
  178. if (WARN(route != tb_cfg_get_route(header),
  179. "wrong route (expected %llx, got %llx)",
  180. route, tb_cfg_get_route(header)))
  181. return -EIO;
  182. return 0;
  183. }
  184. static int check_config_address(struct tb_cfg_address addr,
  185. enum tb_cfg_space space, u32 offset,
  186. u32 length)
  187. {
  188. if (WARN(addr.zero, "addr.zero is %#x\n", addr.zero))
  189. return -EIO;
  190. if (WARN(space != addr.space, "wrong space (expected %x, got %x\n)",
  191. space, addr.space))
  192. return -EIO;
  193. if (WARN(offset != addr.offset, "wrong offset (expected %x, got %x\n)",
  194. offset, addr.offset))
  195. return -EIO;
  196. if (WARN(length != addr.length, "wrong space (expected %x, got %x\n)",
  197. length, addr.length))
  198. return -EIO;
  199. /*
  200. * We cannot check addr->port as it is set to the upstream port of the
  201. * sender.
  202. */
  203. return 0;
  204. }
  205. static struct tb_cfg_result decode_error(const struct ctl_pkg *response)
  206. {
  207. struct cfg_error_pkg *pkg = response->buffer;
  208. struct tb_cfg_result res = { 0 };
  209. res.response_route = tb_cfg_get_route(&pkg->header);
  210. res.response_port = 0;
  211. res.err = check_header(response, sizeof(*pkg), TB_CFG_PKG_ERROR,
  212. tb_cfg_get_route(&pkg->header));
  213. if (res.err)
  214. return res;
  215. res.err = 1;
  216. res.tb_error = pkg->error;
  217. res.response_port = pkg->port;
  218. return res;
  219. }
  220. static struct tb_cfg_result parse_header(const struct ctl_pkg *pkg, u32 len,
  221. enum tb_cfg_pkg_type type, u64 route)
  222. {
  223. struct tb_cfg_header *header = pkg->buffer;
  224. struct tb_cfg_result res = { 0 };
  225. if (pkg->frame.eof == TB_CFG_PKG_ERROR)
  226. return decode_error(pkg);
  227. res.response_port = 0; /* will be updated later for cfg_read/write */
  228. res.response_route = tb_cfg_get_route(header);
  229. res.err = check_header(pkg, len, type, route);
  230. return res;
  231. }
  232. static void tb_cfg_print_error(struct tb_ctl *ctl, enum tb_cfg_space space,
  233. const struct tb_cfg_result *res)
  234. {
  235. WARN_ON(res->err != 1);
  236. switch (res->tb_error) {
  237. case TB_CFG_ERROR_PORT_NOT_CONNECTED:
  238. /* Port is not connected. This can happen during surprise
  239. * removal. Do not warn. */
  240. return;
  241. case TB_CFG_ERROR_INVALID_CONFIG_SPACE:
  242. /*
  243. * Invalid cfg_space/offset/length combination in
  244. * cfg_read/cfg_write.
  245. */
  246. tb_ctl_dbg_once(ctl, "%llx:%x: invalid config space (%u) or offset\n",
  247. res->response_route, res->response_port, space);
  248. return;
  249. case TB_CFG_ERROR_NO_SUCH_PORT:
  250. /*
  251. * - The route contains a non-existent port.
  252. * - The route contains a non-PHY port (e.g. PCIe).
  253. * - The port in cfg_read/cfg_write does not exist.
  254. */
  255. tb_ctl_WARN(ctl, "CFG_ERROR(%llx:%x): Invalid port\n",
  256. res->response_route, res->response_port);
  257. return;
  258. case TB_CFG_ERROR_LOOP:
  259. tb_ctl_WARN(ctl, "CFG_ERROR(%llx:%x): Route contains a loop\n",
  260. res->response_route, res->response_port);
  261. return;
  262. case TB_CFG_ERROR_LOCK:
  263. tb_ctl_warn(ctl, "%llx:%x: downstream port is locked\n",
  264. res->response_route, res->response_port);
  265. return;
  266. default:
  267. /* 5,6,7,9 and 11 are also valid error codes */
  268. tb_ctl_WARN(ctl, "CFG_ERROR(%llx:%x): Unknown error\n",
  269. res->response_route, res->response_port);
  270. return;
  271. }
  272. }
  273. static __be32 tb_crc(const void *data, size_t len)
  274. {
  275. return cpu_to_be32(~crc32c(~0, data, len));
  276. }
  277. static void tb_ctl_pkg_free(struct ctl_pkg *pkg)
  278. {
  279. if (pkg) {
  280. dma_pool_free(pkg->ctl->frame_pool,
  281. pkg->buffer, pkg->frame.buffer_phy);
  282. kfree(pkg);
  283. }
  284. }
  285. static struct ctl_pkg *tb_ctl_pkg_alloc(struct tb_ctl *ctl)
  286. {
  287. struct ctl_pkg *pkg = kzalloc_obj(*pkg);
  288. if (!pkg)
  289. return NULL;
  290. pkg->ctl = ctl;
  291. pkg->buffer = dma_pool_alloc(ctl->frame_pool, GFP_KERNEL,
  292. &pkg->frame.buffer_phy);
  293. if (!pkg->buffer) {
  294. kfree(pkg);
  295. return NULL;
  296. }
  297. return pkg;
  298. }
  299. /* RX/TX handling */
  300. static void tb_ctl_tx_callback(struct tb_ring *ring, struct ring_frame *frame,
  301. bool canceled)
  302. {
  303. struct ctl_pkg *pkg = container_of(frame, typeof(*pkg), frame);
  304. tb_ctl_pkg_free(pkg);
  305. }
  306. /*
  307. * tb_cfg_tx() - transmit a packet on the control channel
  308. *
  309. * len must be a multiple of four.
  310. *
  311. * Return: %0 on success, negative errno otherwise.
  312. */
  313. static int tb_ctl_tx(struct tb_ctl *ctl, const void *data, size_t len,
  314. enum tb_cfg_pkg_type type)
  315. {
  316. int res;
  317. struct ctl_pkg *pkg;
  318. if (len % 4 != 0) { /* required for le->be conversion */
  319. tb_ctl_WARN(ctl, "TX: invalid size: %zu\n", len);
  320. return -EINVAL;
  321. }
  322. if (len > TB_FRAME_SIZE - 4) { /* checksum is 4 bytes */
  323. tb_ctl_WARN(ctl, "TX: packet too large: %zu/%d\n",
  324. len, TB_FRAME_SIZE - 4);
  325. return -EINVAL;
  326. }
  327. pkg = tb_ctl_pkg_alloc(ctl);
  328. if (!pkg)
  329. return -ENOMEM;
  330. pkg->frame.callback = tb_ctl_tx_callback;
  331. pkg->frame.size = len + 4;
  332. pkg->frame.sof = type;
  333. pkg->frame.eof = type;
  334. trace_tb_tx(ctl->index, type, data, len);
  335. cpu_to_be32_array(pkg->buffer, data, len / 4);
  336. *(__be32 *) (pkg->buffer + len) = tb_crc(pkg->buffer, len);
  337. res = tb_ring_tx(ctl->tx, &pkg->frame);
  338. if (res) /* ring is stopped */
  339. tb_ctl_pkg_free(pkg);
  340. return res;
  341. }
  342. /*
  343. * tb_ctl_handle_event() - acknowledge a plug event, invoke ctl->callback
  344. */
  345. static bool tb_ctl_handle_event(struct tb_ctl *ctl, enum tb_cfg_pkg_type type,
  346. struct ctl_pkg *pkg, size_t size)
  347. {
  348. trace_tb_event(ctl->index, type, pkg->buffer, size);
  349. return ctl->callback(ctl->callback_data, type, pkg->buffer, size);
  350. }
  351. static void tb_ctl_rx_submit(struct ctl_pkg *pkg)
  352. {
  353. tb_ring_rx(pkg->ctl->rx, &pkg->frame); /*
  354. * We ignore failures during stop.
  355. * All rx packets are referenced
  356. * from ctl->rx_packets, so we do
  357. * not lose them.
  358. */
  359. }
  360. static int tb_async_error(const struct ctl_pkg *pkg)
  361. {
  362. const struct cfg_error_pkg *error = pkg->buffer;
  363. if (pkg->frame.eof != TB_CFG_PKG_ERROR)
  364. return false;
  365. switch (error->error) {
  366. case TB_CFG_ERROR_LINK_ERROR:
  367. case TB_CFG_ERROR_HEC_ERROR_DETECTED:
  368. case TB_CFG_ERROR_FLOW_CONTROL_ERROR:
  369. case TB_CFG_ERROR_DP_BW:
  370. case TB_CFG_ERROR_ROP_CMPLT:
  371. case TB_CFG_ERROR_POP_CMPLT:
  372. case TB_CFG_ERROR_PCIE_WAKE:
  373. case TB_CFG_ERROR_DP_CON_CHANGE:
  374. case TB_CFG_ERROR_DPTX_DISCOVERY:
  375. case TB_CFG_ERROR_LINK_RECOVERY:
  376. case TB_CFG_ERROR_ASYM_LINK:
  377. return true;
  378. default:
  379. return false;
  380. }
  381. }
  382. static void tb_ctl_rx_callback(struct tb_ring *ring, struct ring_frame *frame,
  383. bool canceled)
  384. {
  385. struct ctl_pkg *pkg = container_of(frame, typeof(*pkg), frame);
  386. struct tb_cfg_request *req;
  387. __be32 crc32;
  388. if (canceled)
  389. return; /*
  390. * ring is stopped, packet is referenced from
  391. * ctl->rx_packets.
  392. */
  393. if (frame->size < 4 || frame->size % 4 != 0) {
  394. tb_ctl_err(pkg->ctl, "RX: invalid size %#x, dropping packet\n",
  395. frame->size);
  396. goto rx;
  397. }
  398. frame->size -= 4; /* remove checksum */
  399. crc32 = tb_crc(pkg->buffer, frame->size);
  400. be32_to_cpu_array(pkg->buffer, pkg->buffer, frame->size / 4);
  401. switch (frame->eof) {
  402. case TB_CFG_PKG_READ:
  403. case TB_CFG_PKG_WRITE:
  404. case TB_CFG_PKG_ERROR:
  405. case TB_CFG_PKG_OVERRIDE:
  406. case TB_CFG_PKG_RESET:
  407. if (*(__be32 *)(pkg->buffer + frame->size) != crc32) {
  408. tb_ctl_err(pkg->ctl,
  409. "RX: checksum mismatch, dropping packet\n");
  410. goto rx;
  411. }
  412. if (tb_async_error(pkg)) {
  413. tb_ctl_handle_event(pkg->ctl, frame->eof,
  414. pkg, frame->size);
  415. goto rx;
  416. }
  417. break;
  418. case TB_CFG_PKG_EVENT:
  419. case TB_CFG_PKG_XDOMAIN_RESP:
  420. case TB_CFG_PKG_XDOMAIN_REQ:
  421. if (*(__be32 *)(pkg->buffer + frame->size) != crc32) {
  422. tb_ctl_err(pkg->ctl,
  423. "RX: checksum mismatch, dropping packet\n");
  424. goto rx;
  425. }
  426. fallthrough;
  427. case TB_CFG_PKG_ICM_EVENT:
  428. if (tb_ctl_handle_event(pkg->ctl, frame->eof, pkg, frame->size))
  429. goto rx;
  430. break;
  431. default:
  432. break;
  433. }
  434. /*
  435. * The received packet will be processed only if there is an
  436. * active request and that the packet is what is expected. This
  437. * prevents packets such as replies coming after timeout has
  438. * triggered from messing with the active requests.
  439. */
  440. req = tb_cfg_request_find(pkg->ctl, pkg);
  441. trace_tb_rx(pkg->ctl->index, frame->eof, pkg->buffer, frame->size, !req);
  442. if (req) {
  443. if (req->copy(req, pkg))
  444. schedule_work(&req->work);
  445. tb_cfg_request_put(req);
  446. }
  447. rx:
  448. tb_ctl_rx_submit(pkg);
  449. }
  450. static void tb_cfg_request_work(struct work_struct *work)
  451. {
  452. struct tb_cfg_request *req = container_of(work, typeof(*req), work);
  453. if (!test_bit(TB_CFG_REQUEST_CANCELED, &req->flags))
  454. req->callback(req->callback_data);
  455. tb_cfg_request_dequeue(req);
  456. tb_cfg_request_put(req);
  457. }
  458. /**
  459. * tb_cfg_request() - Start control request not waiting for it to complete
  460. * @ctl: Control channel to use
  461. * @req: Request to start
  462. * @callback: Callback called when the request is completed
  463. * @callback_data: Data to be passed to @callback
  464. *
  465. * This queues @req on the given control channel without waiting for it
  466. * to complete. When the request completes @callback is called.
  467. *
  468. * Return: %0 on success, negative errno otherwise.
  469. */
  470. int tb_cfg_request(struct tb_ctl *ctl, struct tb_cfg_request *req,
  471. void (*callback)(void *), void *callback_data)
  472. {
  473. int ret;
  474. req->flags = 0;
  475. req->callback = callback;
  476. req->callback_data = callback_data;
  477. INIT_WORK(&req->work, tb_cfg_request_work);
  478. INIT_LIST_HEAD(&req->list);
  479. tb_cfg_request_get(req);
  480. ret = tb_cfg_request_enqueue(ctl, req);
  481. if (ret)
  482. goto err_put;
  483. ret = tb_ctl_tx(ctl, req->request, req->request_size,
  484. req->request_type);
  485. if (ret)
  486. goto err_dequeue;
  487. if (!req->response)
  488. schedule_work(&req->work);
  489. return 0;
  490. err_dequeue:
  491. tb_cfg_request_dequeue(req);
  492. err_put:
  493. tb_cfg_request_put(req);
  494. return ret;
  495. }
  496. /**
  497. * tb_cfg_request_cancel() - Cancel a control request
  498. * @req: Request to cancel
  499. * @err: Error to assign to the request
  500. *
  501. * This function can be used to cancel ongoing request. It will wait
  502. * until the request is not active anymore.
  503. */
  504. void tb_cfg_request_cancel(struct tb_cfg_request *req, int err)
  505. {
  506. set_bit(TB_CFG_REQUEST_CANCELED, &req->flags);
  507. schedule_work(&req->work);
  508. wait_event(tb_cfg_request_cancel_queue, !tb_cfg_request_is_active(req));
  509. req->result.err = err;
  510. }
  511. static void tb_cfg_request_complete(void *data)
  512. {
  513. complete(data);
  514. }
  515. /**
  516. * tb_cfg_request_sync() - Start control request and wait until it completes
  517. * @ctl: Control channel to use
  518. * @req: Request to start
  519. * @timeout_msec: Timeout how long to wait @req to complete
  520. *
  521. * Starts a control request and waits until it completes. If timeout
  522. * triggers the request is canceled before function returns. Note the
  523. * caller needs to make sure only one message for given switch is active
  524. * at a time.
  525. *
  526. * Return: &struct tb_cfg_result with non-zero @err field if error
  527. * has occurred.
  528. */
  529. struct tb_cfg_result tb_cfg_request_sync(struct tb_ctl *ctl,
  530. struct tb_cfg_request *req,
  531. int timeout_msec)
  532. {
  533. unsigned long timeout = msecs_to_jiffies(timeout_msec);
  534. struct tb_cfg_result res = { 0 };
  535. DECLARE_COMPLETION_ONSTACK(done);
  536. int ret;
  537. ret = tb_cfg_request(ctl, req, tb_cfg_request_complete, &done);
  538. if (ret) {
  539. res.err = ret;
  540. return res;
  541. }
  542. if (!wait_for_completion_timeout(&done, timeout))
  543. tb_cfg_request_cancel(req, -ETIMEDOUT);
  544. flush_work(&req->work);
  545. return req->result;
  546. }
  547. /* public interface, alloc/start/stop/free */
  548. /**
  549. * tb_ctl_alloc() - allocate a control channel
  550. * @nhi: Pointer to NHI
  551. * @index: Domain number
  552. * @timeout_msec: Default timeout used with non-raw control messages
  553. * @cb: Callback called for plug events
  554. * @cb_data: Data passed to @cb
  555. *
  556. * cb will be invoked once for every hot plug event.
  557. *
  558. * Return: Pointer to &struct tb_ctl, %NULL on failure.
  559. */
  560. struct tb_ctl *tb_ctl_alloc(struct tb_nhi *nhi, int index, int timeout_msec,
  561. event_cb cb, void *cb_data)
  562. {
  563. int i;
  564. struct tb_ctl *ctl = kzalloc_obj(*ctl);
  565. if (!ctl)
  566. return NULL;
  567. ctl->nhi = nhi;
  568. ctl->index = index;
  569. ctl->timeout_msec = timeout_msec;
  570. ctl->callback = cb;
  571. ctl->callback_data = cb_data;
  572. mutex_init(&ctl->request_queue_lock);
  573. INIT_LIST_HEAD(&ctl->request_queue);
  574. ctl->frame_pool = dma_pool_create("thunderbolt_ctl", &nhi->pdev->dev,
  575. TB_FRAME_SIZE, 4, 0);
  576. if (!ctl->frame_pool)
  577. goto err;
  578. ctl->tx = tb_ring_alloc_tx(nhi, 0, 10, RING_FLAG_NO_SUSPEND);
  579. if (!ctl->tx)
  580. goto err;
  581. ctl->rx = tb_ring_alloc_rx(nhi, 0, 10, RING_FLAG_NO_SUSPEND, 0, 0xffff,
  582. 0xffff, NULL, NULL);
  583. if (!ctl->rx)
  584. goto err;
  585. for (i = 0; i < TB_CTL_RX_PKG_COUNT; i++) {
  586. ctl->rx_packets[i] = tb_ctl_pkg_alloc(ctl);
  587. if (!ctl->rx_packets[i])
  588. goto err;
  589. ctl->rx_packets[i]->frame.callback = tb_ctl_rx_callback;
  590. }
  591. tb_ctl_dbg(ctl, "control channel created\n");
  592. return ctl;
  593. err:
  594. tb_ctl_free(ctl);
  595. return NULL;
  596. }
  597. /**
  598. * tb_ctl_free() - free a control channel
  599. * @ctl: Control channel to free
  600. *
  601. * Must be called after tb_ctl_stop.
  602. *
  603. * Must NOT be called from ctl->callback.
  604. */
  605. void tb_ctl_free(struct tb_ctl *ctl)
  606. {
  607. int i;
  608. if (!ctl)
  609. return;
  610. if (ctl->rx)
  611. tb_ring_free(ctl->rx);
  612. if (ctl->tx)
  613. tb_ring_free(ctl->tx);
  614. /* free RX packets */
  615. for (i = 0; i < TB_CTL_RX_PKG_COUNT; i++)
  616. tb_ctl_pkg_free(ctl->rx_packets[i]);
  617. dma_pool_destroy(ctl->frame_pool);
  618. kfree(ctl);
  619. }
  620. /**
  621. * tb_ctl_start() - start/resume the control channel
  622. * @ctl: Control channel to start
  623. */
  624. void tb_ctl_start(struct tb_ctl *ctl)
  625. {
  626. int i;
  627. tb_ctl_dbg(ctl, "control channel starting...\n");
  628. tb_ring_start(ctl->tx); /* is used to ack hotplug packets, start first */
  629. tb_ring_start(ctl->rx);
  630. for (i = 0; i < TB_CTL_RX_PKG_COUNT; i++)
  631. tb_ctl_rx_submit(ctl->rx_packets[i]);
  632. ctl->running = true;
  633. }
  634. /**
  635. * tb_ctl_stop() - pause the control channel
  636. * @ctl: Control channel to stop
  637. *
  638. * All invocations of ctl->callback will have finished after this method
  639. * returns.
  640. *
  641. * Must NOT be called from ctl->callback.
  642. */
  643. void tb_ctl_stop(struct tb_ctl *ctl)
  644. {
  645. mutex_lock(&ctl->request_queue_lock);
  646. ctl->running = false;
  647. mutex_unlock(&ctl->request_queue_lock);
  648. tb_ring_stop(ctl->rx);
  649. tb_ring_stop(ctl->tx);
  650. if (!list_empty(&ctl->request_queue))
  651. tb_ctl_WARN(ctl, "dangling request in request_queue\n");
  652. INIT_LIST_HEAD(&ctl->request_queue);
  653. tb_ctl_dbg(ctl, "control channel stopped\n");
  654. }
  655. /* public interface, commands */
  656. /**
  657. * tb_cfg_ack_notification() - Ack notification
  658. * @ctl: Control channel to use
  659. * @route: Router that originated the event
  660. * @error: Pointer to the notification package
  661. *
  662. * Call this as a response for non-plug notification to ack it.
  663. *
  664. * Return: %0 on success, negative errno otherwise.
  665. */
  666. int tb_cfg_ack_notification(struct tb_ctl *ctl, u64 route,
  667. const struct cfg_error_pkg *error)
  668. {
  669. struct cfg_ack_pkg pkg = {
  670. .header = tb_cfg_make_header(route),
  671. };
  672. const char *name;
  673. switch (error->error) {
  674. case TB_CFG_ERROR_LINK_ERROR:
  675. name = "link error";
  676. break;
  677. case TB_CFG_ERROR_HEC_ERROR_DETECTED:
  678. name = "HEC error";
  679. break;
  680. case TB_CFG_ERROR_FLOW_CONTROL_ERROR:
  681. name = "flow control error";
  682. break;
  683. case TB_CFG_ERROR_DP_BW:
  684. name = "DP_BW";
  685. break;
  686. case TB_CFG_ERROR_ROP_CMPLT:
  687. name = "router operation completion";
  688. break;
  689. case TB_CFG_ERROR_POP_CMPLT:
  690. name = "port operation completion";
  691. break;
  692. case TB_CFG_ERROR_PCIE_WAKE:
  693. name = "PCIe wake";
  694. break;
  695. case TB_CFG_ERROR_DP_CON_CHANGE:
  696. name = "DP connector change";
  697. break;
  698. case TB_CFG_ERROR_DPTX_DISCOVERY:
  699. name = "DPTX discovery";
  700. break;
  701. case TB_CFG_ERROR_LINK_RECOVERY:
  702. name = "link recovery";
  703. break;
  704. case TB_CFG_ERROR_ASYM_LINK:
  705. name = "asymmetric link";
  706. break;
  707. default:
  708. name = "unknown";
  709. break;
  710. }
  711. tb_ctl_dbg(ctl, "acking %s (%#x) notification on %llx\n", name,
  712. error->error, route);
  713. return tb_ctl_tx(ctl, &pkg, sizeof(pkg), TB_CFG_PKG_NOTIFY_ACK);
  714. }
  715. /**
  716. * tb_cfg_ack_plug() - Ack hot plug/unplug event
  717. * @ctl: Control channel to use
  718. * @route: Router that originated the event
  719. * @port: Port where the hot plug/unplug happened
  720. * @unplug: Ack hot plug or unplug
  721. *
  722. * Call this as a response for hot plug/unplug event to ack it.
  723. *
  724. * Return: %0 on success, negative errno otherwise.
  725. */
  726. int tb_cfg_ack_plug(struct tb_ctl *ctl, u64 route, u32 port, bool unplug)
  727. {
  728. struct cfg_error_pkg pkg = {
  729. .header = tb_cfg_make_header(route),
  730. .port = port,
  731. .error = TB_CFG_ERROR_ACK_PLUG_EVENT,
  732. .pg = unplug ? TB_CFG_ERROR_PG_HOT_UNPLUG
  733. : TB_CFG_ERROR_PG_HOT_PLUG,
  734. };
  735. tb_ctl_dbg(ctl, "acking hot %splug event on %llx:%u\n",
  736. unplug ? "un" : "", route, port);
  737. return tb_ctl_tx(ctl, &pkg, sizeof(pkg), TB_CFG_PKG_ERROR);
  738. }
  739. static bool tb_cfg_match(const struct tb_cfg_request *req,
  740. const struct ctl_pkg *pkg)
  741. {
  742. u64 route = tb_cfg_get_route(pkg->buffer) & ~BIT_ULL(63);
  743. if (pkg->frame.eof == TB_CFG_PKG_ERROR)
  744. return true;
  745. if (pkg->frame.eof != req->response_type)
  746. return false;
  747. if (route != tb_cfg_get_route(req->request))
  748. return false;
  749. if (pkg->frame.size != req->response_size)
  750. return false;
  751. if (pkg->frame.eof == TB_CFG_PKG_READ ||
  752. pkg->frame.eof == TB_CFG_PKG_WRITE) {
  753. const struct cfg_read_pkg *req_hdr = req->request;
  754. const struct cfg_read_pkg *res_hdr = pkg->buffer;
  755. if (req_hdr->addr.seq != res_hdr->addr.seq)
  756. return false;
  757. }
  758. return true;
  759. }
  760. static bool tb_cfg_copy(struct tb_cfg_request *req, const struct ctl_pkg *pkg)
  761. {
  762. struct tb_cfg_result res;
  763. /* Now make sure it is in expected format */
  764. res = parse_header(pkg, req->response_size, req->response_type,
  765. tb_cfg_get_route(req->request));
  766. if (!res.err)
  767. memcpy(req->response, pkg->buffer, req->response_size);
  768. req->result = res;
  769. /* Always complete when first response is received */
  770. return true;
  771. }
  772. /**
  773. * tb_cfg_reset() - send a reset packet and wait for a response
  774. * @ctl: Control channel pointer
  775. * @route: Router string for the router to send reset
  776. *
  777. * If the switch at route is incorrectly configured then we will not receive a
  778. * reply (even though the switch will reset). The caller should check for
  779. * -ETIMEDOUT and attempt to reconfigure the switch.
  780. *
  781. * Return: &struct tb_cfg_result with non-zero @err field if error
  782. * has occurred.
  783. */
  784. struct tb_cfg_result tb_cfg_reset(struct tb_ctl *ctl, u64 route)
  785. {
  786. struct cfg_reset_pkg request = { .header = tb_cfg_make_header(route) };
  787. struct tb_cfg_result res = { 0 };
  788. struct tb_cfg_header reply;
  789. struct tb_cfg_request *req;
  790. req = tb_cfg_request_alloc();
  791. if (!req) {
  792. res.err = -ENOMEM;
  793. return res;
  794. }
  795. req->match = tb_cfg_match;
  796. req->copy = tb_cfg_copy;
  797. req->request = &request;
  798. req->request_size = sizeof(request);
  799. req->request_type = TB_CFG_PKG_RESET;
  800. req->response = &reply;
  801. req->response_size = sizeof(reply);
  802. req->response_type = TB_CFG_PKG_RESET;
  803. res = tb_cfg_request_sync(ctl, req, ctl->timeout_msec);
  804. tb_cfg_request_put(req);
  805. return res;
  806. }
  807. /**
  808. * tb_cfg_read_raw() - read from config space into buffer
  809. * @ctl: Pointer to the control channel
  810. * @buffer: Buffer where the data is read
  811. * @route: Route string of the router
  812. * @port: Port number when reading from %TB_CFG_PORT, %0 otherwise
  813. * @space: Config space selector
  814. * @offset: Dword word offset of the register to start reading
  815. * @length: Number of dwords to read
  816. * @timeout_msec: Timeout in ms how long to wait for the response
  817. *
  818. * Reads from router config space without translating the possible error.
  819. *
  820. * Return: &struct tb_cfg_result with non-zero @err field if error
  821. * has occurred.
  822. */
  823. struct tb_cfg_result tb_cfg_read_raw(struct tb_ctl *ctl, void *buffer,
  824. u64 route, u32 port, enum tb_cfg_space space,
  825. u32 offset, u32 length, int timeout_msec)
  826. {
  827. struct tb_cfg_result res = { 0 };
  828. struct cfg_read_pkg request = {
  829. .header = tb_cfg_make_header(route),
  830. .addr = {
  831. .port = port,
  832. .space = space,
  833. .offset = offset,
  834. .length = length,
  835. },
  836. };
  837. struct cfg_write_pkg reply;
  838. int retries = 0;
  839. while (retries < TB_CTL_RETRIES) {
  840. struct tb_cfg_request *req;
  841. req = tb_cfg_request_alloc();
  842. if (!req) {
  843. res.err = -ENOMEM;
  844. return res;
  845. }
  846. request.addr.seq = retries++;
  847. req->match = tb_cfg_match;
  848. req->copy = tb_cfg_copy;
  849. req->request = &request;
  850. req->request_size = sizeof(request);
  851. req->request_type = TB_CFG_PKG_READ;
  852. req->response = &reply;
  853. req->response_size = 12 + 4 * length;
  854. req->response_type = TB_CFG_PKG_READ;
  855. res = tb_cfg_request_sync(ctl, req, timeout_msec);
  856. tb_cfg_request_put(req);
  857. if (res.err != -ETIMEDOUT)
  858. break;
  859. /* Wait a bit (arbitrary time) until we send a retry */
  860. usleep_range(10, 100);
  861. }
  862. if (res.err)
  863. return res;
  864. res.response_port = reply.addr.port;
  865. res.err = check_config_address(reply.addr, space, offset, length);
  866. if (!res.err)
  867. memcpy(buffer, &reply.data, 4 * length);
  868. return res;
  869. }
  870. /**
  871. * tb_cfg_write_raw() - write from buffer into config space
  872. * @ctl: Pointer to the control channel
  873. * @buffer: Data to write
  874. * @route: Route string of the router
  875. * @port: Port number when writing to %TB_CFG_PORT, %0 otherwise
  876. * @space: Config space selector
  877. * @offset: Dword word offset of the register to start writing
  878. * @length: Number of dwords to write
  879. * @timeout_msec: Timeout in ms how long to wait for the response
  880. *
  881. * Writes to router config space without translating the possible error.
  882. *
  883. * Return: &struct tb_cfg_result with non-zero @err field if error
  884. * has occurred.
  885. */
  886. struct tb_cfg_result tb_cfg_write_raw(struct tb_ctl *ctl, const void *buffer,
  887. u64 route, u32 port, enum tb_cfg_space space,
  888. u32 offset, u32 length, int timeout_msec)
  889. {
  890. struct tb_cfg_result res = { 0 };
  891. struct cfg_write_pkg request = {
  892. .header = tb_cfg_make_header(route),
  893. .addr = {
  894. .port = port,
  895. .space = space,
  896. .offset = offset,
  897. .length = length,
  898. },
  899. };
  900. struct cfg_read_pkg reply;
  901. int retries = 0;
  902. memcpy(&request.data, buffer, length * 4);
  903. while (retries < TB_CTL_RETRIES) {
  904. struct tb_cfg_request *req;
  905. req = tb_cfg_request_alloc();
  906. if (!req) {
  907. res.err = -ENOMEM;
  908. return res;
  909. }
  910. request.addr.seq = retries++;
  911. req->match = tb_cfg_match;
  912. req->copy = tb_cfg_copy;
  913. req->request = &request;
  914. req->request_size = 12 + 4 * length;
  915. req->request_type = TB_CFG_PKG_WRITE;
  916. req->response = &reply;
  917. req->response_size = sizeof(reply);
  918. req->response_type = TB_CFG_PKG_WRITE;
  919. res = tb_cfg_request_sync(ctl, req, timeout_msec);
  920. tb_cfg_request_put(req);
  921. if (res.err != -ETIMEDOUT)
  922. break;
  923. /* Wait a bit (arbitrary time) until we send a retry */
  924. usleep_range(10, 100);
  925. }
  926. if (res.err)
  927. return res;
  928. res.response_port = reply.addr.port;
  929. res.err = check_config_address(reply.addr, space, offset, length);
  930. return res;
  931. }
  932. static int tb_cfg_get_error(struct tb_ctl *ctl, enum tb_cfg_space space,
  933. const struct tb_cfg_result *res)
  934. {
  935. /*
  936. * For unimplemented ports access to port config space may return
  937. * TB_CFG_ERROR_INVALID_CONFIG_SPACE (alternatively their type is
  938. * set to TB_TYPE_INACTIVE). In the former case return -ENODEV so
  939. * that the caller can mark the port as disabled.
  940. */
  941. if (space == TB_CFG_PORT &&
  942. res->tb_error == TB_CFG_ERROR_INVALID_CONFIG_SPACE)
  943. return -ENODEV;
  944. tb_cfg_print_error(ctl, space, res);
  945. if (res->tb_error == TB_CFG_ERROR_LOCK)
  946. return -EACCES;
  947. if (res->tb_error == TB_CFG_ERROR_PORT_NOT_CONNECTED)
  948. return -ENOTCONN;
  949. return -EIO;
  950. }
  951. int tb_cfg_read(struct tb_ctl *ctl, void *buffer, u64 route, u32 port,
  952. enum tb_cfg_space space, u32 offset, u32 length)
  953. {
  954. struct tb_cfg_result res = tb_cfg_read_raw(ctl, buffer, route, port,
  955. space, offset, length, ctl->timeout_msec);
  956. switch (res.err) {
  957. case 0:
  958. /* Success */
  959. break;
  960. case 1:
  961. /* Thunderbolt error, tb_error holds the actual number */
  962. return tb_cfg_get_error(ctl, space, &res);
  963. case -ETIMEDOUT:
  964. tb_ctl_warn(ctl, "%llx: timeout reading config space %u from %#x\n",
  965. route, space, offset);
  966. break;
  967. default:
  968. WARN(1, "tb_cfg_read: %d\n", res.err);
  969. break;
  970. }
  971. return res.err;
  972. }
  973. int tb_cfg_write(struct tb_ctl *ctl, const void *buffer, u64 route, u32 port,
  974. enum tb_cfg_space space, u32 offset, u32 length)
  975. {
  976. struct tb_cfg_result res = tb_cfg_write_raw(ctl, buffer, route, port,
  977. space, offset, length, ctl->timeout_msec);
  978. switch (res.err) {
  979. case 0:
  980. /* Success */
  981. break;
  982. case 1:
  983. /* Thunderbolt error, tb_error holds the actual number */
  984. return tb_cfg_get_error(ctl, space, &res);
  985. case -ETIMEDOUT:
  986. tb_ctl_warn(ctl, "%llx: timeout writing config space %u to %#x\n",
  987. route, space, offset);
  988. break;
  989. default:
  990. WARN(1, "tb_cfg_write: %d\n", res.err);
  991. break;
  992. }
  993. return res.err;
  994. }
  995. /**
  996. * tb_cfg_get_upstream_port() - get upstream port number of switch at route
  997. * @ctl: Pointer to the control channel
  998. * @route: Route string of the router
  999. *
  1000. * Reads the first dword from the switches TB_CFG_SWITCH config area and
  1001. * returns the port number from which the reply originated.
  1002. *
  1003. * Return: Upstream port number on success or negative error code on failure.
  1004. */
  1005. int tb_cfg_get_upstream_port(struct tb_ctl *ctl, u64 route)
  1006. {
  1007. u32 dummy;
  1008. struct tb_cfg_result res = tb_cfg_read_raw(ctl, &dummy, route, 0,
  1009. TB_CFG_SWITCH, 0, 1,
  1010. ctl->timeout_msec);
  1011. if (res.err == 1)
  1012. return -EIO;
  1013. if (res.err)
  1014. return res.err;
  1015. return res.response_port;
  1016. }