ctl.c 25 KB

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