ctl.c 24 KB

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