ctl.c 24 KB

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