xdomain.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617
  1. /*
  2. * Thunderbolt XDomain discovery protocol support
  3. *
  4. * Copyright (C) 2017, Intel Corporation
  5. * Authors: Michael Jamet <michael.jamet@intel.com>
  6. * Mika Westerberg <mika.westerberg@linux.intel.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/device.h>
  13. #include <linux/kmod.h>
  14. #include <linux/module.h>
  15. #include <linux/pm_runtime.h>
  16. #include <linux/utsname.h>
  17. #include <linux/uuid.h>
  18. #include <linux/workqueue.h>
  19. #include "tb.h"
  20. #define XDOMAIN_DEFAULT_TIMEOUT 5000 /* ms */
  21. #define XDOMAIN_PROPERTIES_RETRIES 60
  22. #define XDOMAIN_PROPERTIES_CHANGED_RETRIES 10
  23. struct xdomain_request_work {
  24. struct work_struct work;
  25. struct tb_xdp_header *pkg;
  26. struct tb *tb;
  27. };
  28. /* Serializes access to the properties and protocol handlers below */
  29. static DEFINE_MUTEX(xdomain_lock);
  30. /* Properties exposed to the remote domains */
  31. static struct tb_property_dir *xdomain_property_dir;
  32. static u32 *xdomain_property_block;
  33. static u32 xdomain_property_block_len;
  34. static u32 xdomain_property_block_gen;
  35. /* Additional protocol handlers */
  36. static LIST_HEAD(protocol_handlers);
  37. /* UUID for XDomain discovery protocol: b638d70e-42ff-40bb-97c2-90e2c0b2ff07 */
  38. static const uuid_t tb_xdp_uuid =
  39. UUID_INIT(0xb638d70e, 0x42ff, 0x40bb,
  40. 0x97, 0xc2, 0x90, 0xe2, 0xc0, 0xb2, 0xff, 0x07);
  41. static bool tb_xdomain_match(const struct tb_cfg_request *req,
  42. const struct ctl_pkg *pkg)
  43. {
  44. switch (pkg->frame.eof) {
  45. case TB_CFG_PKG_ERROR:
  46. return true;
  47. case TB_CFG_PKG_XDOMAIN_RESP: {
  48. const struct tb_xdp_header *res_hdr = pkg->buffer;
  49. const struct tb_xdp_header *req_hdr = req->request;
  50. if (pkg->frame.size < req->response_size / 4)
  51. return false;
  52. /* Make sure route matches */
  53. if ((res_hdr->xd_hdr.route_hi & ~BIT(31)) !=
  54. req_hdr->xd_hdr.route_hi)
  55. return false;
  56. if ((res_hdr->xd_hdr.route_lo) != req_hdr->xd_hdr.route_lo)
  57. return false;
  58. /* Check that the XDomain protocol matches */
  59. if (!uuid_equal(&res_hdr->uuid, &req_hdr->uuid))
  60. return false;
  61. return true;
  62. }
  63. default:
  64. return false;
  65. }
  66. }
  67. static bool tb_xdomain_copy(struct tb_cfg_request *req,
  68. const struct ctl_pkg *pkg)
  69. {
  70. memcpy(req->response, pkg->buffer, req->response_size);
  71. req->result.err = 0;
  72. return true;
  73. }
  74. static void response_ready(void *data)
  75. {
  76. tb_cfg_request_put(data);
  77. }
  78. static int __tb_xdomain_response(struct tb_ctl *ctl, const void *response,
  79. size_t size, enum tb_cfg_pkg_type type)
  80. {
  81. struct tb_cfg_request *req;
  82. req = tb_cfg_request_alloc();
  83. if (!req)
  84. return -ENOMEM;
  85. req->match = tb_xdomain_match;
  86. req->copy = tb_xdomain_copy;
  87. req->request = response;
  88. req->request_size = size;
  89. req->request_type = type;
  90. return tb_cfg_request(ctl, req, response_ready, req);
  91. }
  92. /**
  93. * tb_xdomain_response() - Send a XDomain response message
  94. * @xd: XDomain to send the message
  95. * @response: Response to send
  96. * @size: Size of the response
  97. * @type: PDF type of the response
  98. *
  99. * This can be used to send a XDomain response message to the other
  100. * domain. No response for the message is expected.
  101. *
  102. * Return: %0 in case of success and negative errno in case of failure
  103. */
  104. int tb_xdomain_response(struct tb_xdomain *xd, const void *response,
  105. size_t size, enum tb_cfg_pkg_type type)
  106. {
  107. return __tb_xdomain_response(xd->tb->ctl, response, size, type);
  108. }
  109. EXPORT_SYMBOL_GPL(tb_xdomain_response);
  110. static int __tb_xdomain_request(struct tb_ctl *ctl, const void *request,
  111. size_t request_size, enum tb_cfg_pkg_type request_type, void *response,
  112. size_t response_size, enum tb_cfg_pkg_type response_type,
  113. unsigned int timeout_msec)
  114. {
  115. struct tb_cfg_request *req;
  116. struct tb_cfg_result res;
  117. req = tb_cfg_request_alloc();
  118. if (!req)
  119. return -ENOMEM;
  120. req->match = tb_xdomain_match;
  121. req->copy = tb_xdomain_copy;
  122. req->request = request;
  123. req->request_size = request_size;
  124. req->request_type = request_type;
  125. req->response = response;
  126. req->response_size = response_size;
  127. req->response_type = response_type;
  128. res = tb_cfg_request_sync(ctl, req, timeout_msec);
  129. tb_cfg_request_put(req);
  130. return res.err == 1 ? -EIO : res.err;
  131. }
  132. /**
  133. * tb_xdomain_request() - Send a XDomain request
  134. * @xd: XDomain to send the request
  135. * @request: Request to send
  136. * @request_size: Size of the request in bytes
  137. * @request_type: PDF type of the request
  138. * @response: Response is copied here
  139. * @response_size: Expected size of the response in bytes
  140. * @response_type: Expected PDF type of the response
  141. * @timeout_msec: Timeout in milliseconds to wait for the response
  142. *
  143. * This function can be used to send XDomain control channel messages to
  144. * the other domain. The function waits until the response is received
  145. * or when timeout triggers. Whichever comes first.
  146. *
  147. * Return: %0 in case of success and negative errno in case of failure
  148. */
  149. int tb_xdomain_request(struct tb_xdomain *xd, const void *request,
  150. size_t request_size, enum tb_cfg_pkg_type request_type,
  151. void *response, size_t response_size,
  152. enum tb_cfg_pkg_type response_type, unsigned int timeout_msec)
  153. {
  154. return __tb_xdomain_request(xd->tb->ctl, request, request_size,
  155. request_type, response, response_size,
  156. response_type, timeout_msec);
  157. }
  158. EXPORT_SYMBOL_GPL(tb_xdomain_request);
  159. static inline void tb_xdp_fill_header(struct tb_xdp_header *hdr, u64 route,
  160. u8 sequence, enum tb_xdp_type type, size_t size)
  161. {
  162. u32 length_sn;
  163. length_sn = (size - sizeof(hdr->xd_hdr)) / 4;
  164. length_sn |= (sequence << TB_XDOMAIN_SN_SHIFT) & TB_XDOMAIN_SN_MASK;
  165. hdr->xd_hdr.route_hi = upper_32_bits(route);
  166. hdr->xd_hdr.route_lo = lower_32_bits(route);
  167. hdr->xd_hdr.length_sn = length_sn;
  168. hdr->type = type;
  169. memcpy(&hdr->uuid, &tb_xdp_uuid, sizeof(tb_xdp_uuid));
  170. }
  171. static int tb_xdp_handle_error(const struct tb_xdp_header *hdr)
  172. {
  173. const struct tb_xdp_error_response *error;
  174. if (hdr->type != ERROR_RESPONSE)
  175. return 0;
  176. error = (const struct tb_xdp_error_response *)hdr;
  177. switch (error->error) {
  178. case ERROR_UNKNOWN_PACKET:
  179. case ERROR_UNKNOWN_DOMAIN:
  180. return -EIO;
  181. case ERROR_NOT_SUPPORTED:
  182. return -ENOTSUPP;
  183. case ERROR_NOT_READY:
  184. return -EAGAIN;
  185. default:
  186. break;
  187. }
  188. return 0;
  189. }
  190. static int tb_xdp_error_response(struct tb_ctl *ctl, u64 route, u8 sequence,
  191. enum tb_xdp_error error)
  192. {
  193. struct tb_xdp_error_response res;
  194. memset(&res, 0, sizeof(res));
  195. tb_xdp_fill_header(&res.hdr, route, sequence, ERROR_RESPONSE,
  196. sizeof(res));
  197. res.error = error;
  198. return __tb_xdomain_response(ctl, &res, sizeof(res),
  199. TB_CFG_PKG_XDOMAIN_RESP);
  200. }
  201. static int tb_xdp_properties_request(struct tb_ctl *ctl, u64 route,
  202. const uuid_t *src_uuid, const uuid_t *dst_uuid, int retry,
  203. u32 **block, u32 *generation)
  204. {
  205. struct tb_xdp_properties_response *res;
  206. struct tb_xdp_properties req;
  207. u16 data_len, len;
  208. size_t total_size;
  209. u32 *data = NULL;
  210. int ret;
  211. total_size = sizeof(*res) + TB_XDP_PROPERTIES_MAX_DATA_LENGTH * 4;
  212. res = kzalloc(total_size, GFP_KERNEL);
  213. if (!res)
  214. return -ENOMEM;
  215. memset(&req, 0, sizeof(req));
  216. tb_xdp_fill_header(&req.hdr, route, retry % 4, PROPERTIES_REQUEST,
  217. sizeof(req));
  218. memcpy(&req.src_uuid, src_uuid, sizeof(*src_uuid));
  219. memcpy(&req.dst_uuid, dst_uuid, sizeof(*dst_uuid));
  220. len = 0;
  221. data_len = 0;
  222. do {
  223. ret = __tb_xdomain_request(ctl, &req, sizeof(req),
  224. TB_CFG_PKG_XDOMAIN_REQ, res,
  225. total_size, TB_CFG_PKG_XDOMAIN_RESP,
  226. XDOMAIN_DEFAULT_TIMEOUT);
  227. if (ret)
  228. goto err;
  229. ret = tb_xdp_handle_error(&res->hdr);
  230. if (ret)
  231. goto err;
  232. /*
  233. * Package length includes the whole payload without the
  234. * XDomain header. Validate first that the package is at
  235. * least size of the response structure.
  236. */
  237. len = res->hdr.xd_hdr.length_sn & TB_XDOMAIN_LENGTH_MASK;
  238. if (len < sizeof(*res) / 4) {
  239. ret = -EINVAL;
  240. goto err;
  241. }
  242. len += sizeof(res->hdr.xd_hdr) / 4;
  243. len -= sizeof(*res) / 4;
  244. if (res->offset != req.offset) {
  245. ret = -EINVAL;
  246. goto err;
  247. }
  248. /*
  249. * First time allocate block that has enough space for
  250. * the whole properties block.
  251. */
  252. if (!data) {
  253. data_len = res->data_length;
  254. if (data_len > TB_XDP_PROPERTIES_MAX_LENGTH) {
  255. ret = -E2BIG;
  256. goto err;
  257. }
  258. data = kcalloc(data_len, sizeof(u32), GFP_KERNEL);
  259. if (!data) {
  260. ret = -ENOMEM;
  261. goto err;
  262. }
  263. }
  264. memcpy(data + req.offset, res->data, len * 4);
  265. req.offset += len;
  266. } while (!data_len || req.offset < data_len);
  267. *block = data;
  268. *generation = res->generation;
  269. kfree(res);
  270. return data_len;
  271. err:
  272. kfree(data);
  273. kfree(res);
  274. return ret;
  275. }
  276. static int tb_xdp_properties_response(struct tb *tb, struct tb_ctl *ctl,
  277. u64 route, u8 sequence, const uuid_t *src_uuid,
  278. const struct tb_xdp_properties *req)
  279. {
  280. struct tb_xdp_properties_response *res;
  281. size_t total_size;
  282. u16 len;
  283. int ret;
  284. /*
  285. * Currently we expect all requests to be directed to us. The
  286. * protocol supports forwarding, though which we might add
  287. * support later on.
  288. */
  289. if (!uuid_equal(src_uuid, &req->dst_uuid)) {
  290. tb_xdp_error_response(ctl, route, sequence,
  291. ERROR_UNKNOWN_DOMAIN);
  292. return 0;
  293. }
  294. mutex_lock(&xdomain_lock);
  295. if (req->offset >= xdomain_property_block_len) {
  296. mutex_unlock(&xdomain_lock);
  297. return -EINVAL;
  298. }
  299. len = xdomain_property_block_len - req->offset;
  300. len = min_t(u16, len, TB_XDP_PROPERTIES_MAX_DATA_LENGTH);
  301. total_size = sizeof(*res) + len * 4;
  302. res = kzalloc(total_size, GFP_KERNEL);
  303. if (!res) {
  304. mutex_unlock(&xdomain_lock);
  305. return -ENOMEM;
  306. }
  307. tb_xdp_fill_header(&res->hdr, route, sequence, PROPERTIES_RESPONSE,
  308. total_size);
  309. res->generation = xdomain_property_block_gen;
  310. res->data_length = xdomain_property_block_len;
  311. res->offset = req->offset;
  312. uuid_copy(&res->src_uuid, src_uuid);
  313. uuid_copy(&res->dst_uuid, &req->src_uuid);
  314. memcpy(res->data, &xdomain_property_block[req->offset], len * 4);
  315. mutex_unlock(&xdomain_lock);
  316. ret = __tb_xdomain_response(ctl, res, total_size,
  317. TB_CFG_PKG_XDOMAIN_RESP);
  318. kfree(res);
  319. return ret;
  320. }
  321. static int tb_xdp_properties_changed_request(struct tb_ctl *ctl, u64 route,
  322. int retry, const uuid_t *uuid)
  323. {
  324. struct tb_xdp_properties_changed_response res;
  325. struct tb_xdp_properties_changed req;
  326. int ret;
  327. memset(&req, 0, sizeof(req));
  328. tb_xdp_fill_header(&req.hdr, route, retry % 4,
  329. PROPERTIES_CHANGED_REQUEST, sizeof(req));
  330. uuid_copy(&req.src_uuid, uuid);
  331. memset(&res, 0, sizeof(res));
  332. ret = __tb_xdomain_request(ctl, &req, sizeof(req),
  333. TB_CFG_PKG_XDOMAIN_REQ, &res, sizeof(res),
  334. TB_CFG_PKG_XDOMAIN_RESP,
  335. XDOMAIN_DEFAULT_TIMEOUT);
  336. if (ret)
  337. return ret;
  338. return tb_xdp_handle_error(&res.hdr);
  339. }
  340. static int
  341. tb_xdp_properties_changed_response(struct tb_ctl *ctl, u64 route, u8 sequence)
  342. {
  343. struct tb_xdp_properties_changed_response res;
  344. memset(&res, 0, sizeof(res));
  345. tb_xdp_fill_header(&res.hdr, route, sequence,
  346. PROPERTIES_CHANGED_RESPONSE, sizeof(res));
  347. return __tb_xdomain_response(ctl, &res, sizeof(res),
  348. TB_CFG_PKG_XDOMAIN_RESP);
  349. }
  350. /**
  351. * tb_register_protocol_handler() - Register protocol handler
  352. * @handler: Handler to register
  353. *
  354. * This allows XDomain service drivers to hook into incoming XDomain
  355. * messages. After this function is called the service driver needs to
  356. * be able to handle calls to callback whenever a package with the
  357. * registered protocol is received.
  358. */
  359. int tb_register_protocol_handler(struct tb_protocol_handler *handler)
  360. {
  361. if (!handler->uuid || !handler->callback)
  362. return -EINVAL;
  363. if (uuid_equal(handler->uuid, &tb_xdp_uuid))
  364. return -EINVAL;
  365. mutex_lock(&xdomain_lock);
  366. list_add_tail(&handler->list, &protocol_handlers);
  367. mutex_unlock(&xdomain_lock);
  368. return 0;
  369. }
  370. EXPORT_SYMBOL_GPL(tb_register_protocol_handler);
  371. /**
  372. * tb_unregister_protocol_handler() - Unregister protocol handler
  373. * @handler: Handler to unregister
  374. *
  375. * Removes the previously registered protocol handler.
  376. */
  377. void tb_unregister_protocol_handler(struct tb_protocol_handler *handler)
  378. {
  379. mutex_lock(&xdomain_lock);
  380. list_del_init(&handler->list);
  381. mutex_unlock(&xdomain_lock);
  382. }
  383. EXPORT_SYMBOL_GPL(tb_unregister_protocol_handler);
  384. static void tb_xdp_handle_request(struct work_struct *work)
  385. {
  386. struct xdomain_request_work *xw = container_of(work, typeof(*xw), work);
  387. const struct tb_xdp_header *pkg = xw->pkg;
  388. const struct tb_xdomain_header *xhdr = &pkg->xd_hdr;
  389. struct tb *tb = xw->tb;
  390. struct tb_ctl *ctl = tb->ctl;
  391. const uuid_t *uuid;
  392. int ret = 0;
  393. u32 sequence;
  394. u64 route;
  395. route = ((u64)xhdr->route_hi << 32 | xhdr->route_lo) & ~BIT_ULL(63);
  396. sequence = xhdr->length_sn & TB_XDOMAIN_SN_MASK;
  397. sequence >>= TB_XDOMAIN_SN_SHIFT;
  398. mutex_lock(&tb->lock);
  399. if (tb->root_switch)
  400. uuid = tb->root_switch->uuid;
  401. else
  402. uuid = NULL;
  403. mutex_unlock(&tb->lock);
  404. if (!uuid) {
  405. tb_xdp_error_response(ctl, route, sequence, ERROR_NOT_READY);
  406. goto out;
  407. }
  408. switch (pkg->type) {
  409. case PROPERTIES_REQUEST:
  410. ret = tb_xdp_properties_response(tb, ctl, route, sequence, uuid,
  411. (const struct tb_xdp_properties *)pkg);
  412. break;
  413. case PROPERTIES_CHANGED_REQUEST: {
  414. const struct tb_xdp_properties_changed *xchg =
  415. (const struct tb_xdp_properties_changed *)pkg;
  416. struct tb_xdomain *xd;
  417. ret = tb_xdp_properties_changed_response(ctl, route, sequence);
  418. /*
  419. * Since the properties have been changed, let's update
  420. * the xdomain related to this connection as well in
  421. * case there is a change in services it offers.
  422. */
  423. xd = tb_xdomain_find_by_uuid_locked(tb, &xchg->src_uuid);
  424. if (xd) {
  425. queue_delayed_work(tb->wq, &xd->get_properties_work,
  426. msecs_to_jiffies(50));
  427. tb_xdomain_put(xd);
  428. }
  429. break;
  430. }
  431. default:
  432. break;
  433. }
  434. if (ret) {
  435. tb_warn(tb, "failed to send XDomain response for %#x\n",
  436. pkg->type);
  437. }
  438. out:
  439. kfree(xw->pkg);
  440. kfree(xw);
  441. }
  442. static void
  443. tb_xdp_schedule_request(struct tb *tb, const struct tb_xdp_header *hdr,
  444. size_t size)
  445. {
  446. struct xdomain_request_work *xw;
  447. xw = kmalloc(sizeof(*xw), GFP_KERNEL);
  448. if (!xw)
  449. return;
  450. INIT_WORK(&xw->work, tb_xdp_handle_request);
  451. xw->pkg = kmemdup(hdr, size, GFP_KERNEL);
  452. xw->tb = tb;
  453. queue_work(tb->wq, &xw->work);
  454. }
  455. /**
  456. * tb_register_service_driver() - Register XDomain service driver
  457. * @drv: Driver to register
  458. *
  459. * Registers new service driver from @drv to the bus.
  460. */
  461. int tb_register_service_driver(struct tb_service_driver *drv)
  462. {
  463. drv->driver.bus = &tb_bus_type;
  464. return driver_register(&drv->driver);
  465. }
  466. EXPORT_SYMBOL_GPL(tb_register_service_driver);
  467. /**
  468. * tb_unregister_service_driver() - Unregister XDomain service driver
  469. * @xdrv: Driver to unregister
  470. *
  471. * Unregisters XDomain service driver from the bus.
  472. */
  473. void tb_unregister_service_driver(struct tb_service_driver *drv)
  474. {
  475. driver_unregister(&drv->driver);
  476. }
  477. EXPORT_SYMBOL_GPL(tb_unregister_service_driver);
  478. static ssize_t key_show(struct device *dev, struct device_attribute *attr,
  479. char *buf)
  480. {
  481. struct tb_service *svc = container_of(dev, struct tb_service, dev);
  482. /*
  483. * It should be null terminated but anything else is pretty much
  484. * allowed.
  485. */
  486. return sprintf(buf, "%*pEp\n", (int)strlen(svc->key), svc->key);
  487. }
  488. static DEVICE_ATTR_RO(key);
  489. static int get_modalias(struct tb_service *svc, char *buf, size_t size)
  490. {
  491. return snprintf(buf, size, "tbsvc:k%sp%08Xv%08Xr%08X", svc->key,
  492. svc->prtcid, svc->prtcvers, svc->prtcrevs);
  493. }
  494. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
  495. char *buf)
  496. {
  497. struct tb_service *svc = container_of(dev, struct tb_service, dev);
  498. /* Full buffer size except new line and null termination */
  499. get_modalias(svc, buf, PAGE_SIZE - 2);
  500. return sprintf(buf, "%s\n", buf);
  501. }
  502. static DEVICE_ATTR_RO(modalias);
  503. static ssize_t prtcid_show(struct device *dev, struct device_attribute *attr,
  504. char *buf)
  505. {
  506. struct tb_service *svc = container_of(dev, struct tb_service, dev);
  507. return sprintf(buf, "%u\n", svc->prtcid);
  508. }
  509. static DEVICE_ATTR_RO(prtcid);
  510. static ssize_t prtcvers_show(struct device *dev, struct device_attribute *attr,
  511. char *buf)
  512. {
  513. struct tb_service *svc = container_of(dev, struct tb_service, dev);
  514. return sprintf(buf, "%u\n", svc->prtcvers);
  515. }
  516. static DEVICE_ATTR_RO(prtcvers);
  517. static ssize_t prtcrevs_show(struct device *dev, struct device_attribute *attr,
  518. char *buf)
  519. {
  520. struct tb_service *svc = container_of(dev, struct tb_service, dev);
  521. return sprintf(buf, "%u\n", svc->prtcrevs);
  522. }
  523. static DEVICE_ATTR_RO(prtcrevs);
  524. static ssize_t prtcstns_show(struct device *dev, struct device_attribute *attr,
  525. char *buf)
  526. {
  527. struct tb_service *svc = container_of(dev, struct tb_service, dev);
  528. return sprintf(buf, "0x%08x\n", svc->prtcstns);
  529. }
  530. static DEVICE_ATTR_RO(prtcstns);
  531. static struct attribute *tb_service_attrs[] = {
  532. &dev_attr_key.attr,
  533. &dev_attr_modalias.attr,
  534. &dev_attr_prtcid.attr,
  535. &dev_attr_prtcvers.attr,
  536. &dev_attr_prtcrevs.attr,
  537. &dev_attr_prtcstns.attr,
  538. NULL,
  539. };
  540. static struct attribute_group tb_service_attr_group = {
  541. .attrs = tb_service_attrs,
  542. };
  543. static const struct attribute_group *tb_service_attr_groups[] = {
  544. &tb_service_attr_group,
  545. NULL,
  546. };
  547. static int tb_service_uevent(struct device *dev, struct kobj_uevent_env *env)
  548. {
  549. struct tb_service *svc = container_of(dev, struct tb_service, dev);
  550. char modalias[64];
  551. get_modalias(svc, modalias, sizeof(modalias));
  552. return add_uevent_var(env, "MODALIAS=%s", modalias);
  553. }
  554. static void tb_service_release(struct device *dev)
  555. {
  556. struct tb_service *svc = container_of(dev, struct tb_service, dev);
  557. struct tb_xdomain *xd = tb_service_parent(svc);
  558. ida_simple_remove(&xd->service_ids, svc->id);
  559. kfree(svc->key);
  560. kfree(svc);
  561. }
  562. struct device_type tb_service_type = {
  563. .name = "thunderbolt_service",
  564. .groups = tb_service_attr_groups,
  565. .uevent = tb_service_uevent,
  566. .release = tb_service_release,
  567. };
  568. EXPORT_SYMBOL_GPL(tb_service_type);
  569. static int remove_missing_service(struct device *dev, void *data)
  570. {
  571. struct tb_xdomain *xd = data;
  572. struct tb_service *svc;
  573. svc = tb_to_service(dev);
  574. if (!svc)
  575. return 0;
  576. if (!tb_property_find(xd->properties, svc->key,
  577. TB_PROPERTY_TYPE_DIRECTORY))
  578. device_unregister(dev);
  579. return 0;
  580. }
  581. static int find_service(struct device *dev, void *data)
  582. {
  583. const struct tb_property *p = data;
  584. struct tb_service *svc;
  585. svc = tb_to_service(dev);
  586. if (!svc)
  587. return 0;
  588. return !strcmp(svc->key, p->key);
  589. }
  590. static int populate_service(struct tb_service *svc,
  591. struct tb_property *property)
  592. {
  593. struct tb_property_dir *dir = property->value.dir;
  594. struct tb_property *p;
  595. /* Fill in standard properties */
  596. p = tb_property_find(dir, "prtcid", TB_PROPERTY_TYPE_VALUE);
  597. if (p)
  598. svc->prtcid = p->value.immediate;
  599. p = tb_property_find(dir, "prtcvers", TB_PROPERTY_TYPE_VALUE);
  600. if (p)
  601. svc->prtcvers = p->value.immediate;
  602. p = tb_property_find(dir, "prtcrevs", TB_PROPERTY_TYPE_VALUE);
  603. if (p)
  604. svc->prtcrevs = p->value.immediate;
  605. p = tb_property_find(dir, "prtcstns", TB_PROPERTY_TYPE_VALUE);
  606. if (p)
  607. svc->prtcstns = p->value.immediate;
  608. svc->key = kstrdup(property->key, GFP_KERNEL);
  609. if (!svc->key)
  610. return -ENOMEM;
  611. return 0;
  612. }
  613. static void enumerate_services(struct tb_xdomain *xd)
  614. {
  615. struct tb_service *svc;
  616. struct tb_property *p;
  617. struct device *dev;
  618. int id;
  619. /*
  620. * First remove all services that are not available anymore in
  621. * the updated property block.
  622. */
  623. device_for_each_child_reverse(&xd->dev, xd, remove_missing_service);
  624. /* Then re-enumerate properties creating new services as we go */
  625. tb_property_for_each(xd->properties, p) {
  626. if (p->type != TB_PROPERTY_TYPE_DIRECTORY)
  627. continue;
  628. /* If the service exists already we are fine */
  629. dev = device_find_child(&xd->dev, p, find_service);
  630. if (dev) {
  631. put_device(dev);
  632. continue;
  633. }
  634. svc = kzalloc(sizeof(*svc), GFP_KERNEL);
  635. if (!svc)
  636. break;
  637. if (populate_service(svc, p)) {
  638. kfree(svc);
  639. break;
  640. }
  641. id = ida_simple_get(&xd->service_ids, 0, 0, GFP_KERNEL);
  642. if (id < 0) {
  643. kfree(svc);
  644. break;
  645. }
  646. svc->id = id;
  647. svc->dev.bus = &tb_bus_type;
  648. svc->dev.type = &tb_service_type;
  649. svc->dev.parent = &xd->dev;
  650. dev_set_name(&svc->dev, "%s.%d", dev_name(&xd->dev), svc->id);
  651. if (device_register(&svc->dev)) {
  652. put_device(&svc->dev);
  653. break;
  654. }
  655. }
  656. }
  657. static int populate_properties(struct tb_xdomain *xd,
  658. struct tb_property_dir *dir)
  659. {
  660. const struct tb_property *p;
  661. /* Required properties */
  662. p = tb_property_find(dir, "deviceid", TB_PROPERTY_TYPE_VALUE);
  663. if (!p)
  664. return -EINVAL;
  665. xd->device = p->value.immediate;
  666. p = tb_property_find(dir, "vendorid", TB_PROPERTY_TYPE_VALUE);
  667. if (!p)
  668. return -EINVAL;
  669. xd->vendor = p->value.immediate;
  670. kfree(xd->device_name);
  671. xd->device_name = NULL;
  672. kfree(xd->vendor_name);
  673. xd->vendor_name = NULL;
  674. /* Optional properties */
  675. p = tb_property_find(dir, "deviceid", TB_PROPERTY_TYPE_TEXT);
  676. if (p)
  677. xd->device_name = kstrdup(p->value.text, GFP_KERNEL);
  678. p = tb_property_find(dir, "vendorid", TB_PROPERTY_TYPE_TEXT);
  679. if (p)
  680. xd->vendor_name = kstrdup(p->value.text, GFP_KERNEL);
  681. return 0;
  682. }
  683. /* Called with @xd->lock held */
  684. static void tb_xdomain_restore_paths(struct tb_xdomain *xd)
  685. {
  686. if (!xd->resume)
  687. return;
  688. xd->resume = false;
  689. if (xd->transmit_path) {
  690. dev_dbg(&xd->dev, "re-establishing DMA path\n");
  691. tb_domain_approve_xdomain_paths(xd->tb, xd);
  692. }
  693. }
  694. static void tb_xdomain_get_properties(struct work_struct *work)
  695. {
  696. struct tb_xdomain *xd = container_of(work, typeof(*xd),
  697. get_properties_work.work);
  698. struct tb_property_dir *dir;
  699. struct tb *tb = xd->tb;
  700. bool update = false;
  701. u32 *block = NULL;
  702. u32 gen = 0;
  703. int ret;
  704. ret = tb_xdp_properties_request(tb->ctl, xd->route, xd->local_uuid,
  705. xd->remote_uuid, xd->properties_retries,
  706. &block, &gen);
  707. if (ret < 0) {
  708. if (xd->properties_retries-- > 0) {
  709. queue_delayed_work(xd->tb->wq, &xd->get_properties_work,
  710. msecs_to_jiffies(1000));
  711. } else {
  712. /* Give up now */
  713. dev_err(&xd->dev,
  714. "failed read XDomain properties from %pUb\n",
  715. xd->remote_uuid);
  716. }
  717. return;
  718. }
  719. xd->properties_retries = XDOMAIN_PROPERTIES_RETRIES;
  720. mutex_lock(&xd->lock);
  721. /* Only accept newer generation properties */
  722. if (xd->properties && gen <= xd->property_block_gen) {
  723. /*
  724. * On resume it is likely that the properties block is
  725. * not changed (unless the other end added or removed
  726. * services). However, we need to make sure the existing
  727. * DMA paths are restored properly.
  728. */
  729. tb_xdomain_restore_paths(xd);
  730. goto err_free_block;
  731. }
  732. dir = tb_property_parse_dir(block, ret);
  733. if (!dir) {
  734. dev_err(&xd->dev, "failed to parse XDomain properties\n");
  735. goto err_free_block;
  736. }
  737. ret = populate_properties(xd, dir);
  738. if (ret) {
  739. dev_err(&xd->dev, "missing XDomain properties in response\n");
  740. goto err_free_dir;
  741. }
  742. /* Release the existing one */
  743. if (xd->properties) {
  744. tb_property_free_dir(xd->properties);
  745. update = true;
  746. }
  747. xd->properties = dir;
  748. xd->property_block_gen = gen;
  749. tb_xdomain_restore_paths(xd);
  750. mutex_unlock(&xd->lock);
  751. kfree(block);
  752. /*
  753. * Now the device should be ready enough so we can add it to the
  754. * bus and let userspace know about it. If the device is already
  755. * registered, we notify the userspace that it has changed.
  756. */
  757. if (!update) {
  758. if (device_add(&xd->dev)) {
  759. dev_err(&xd->dev, "failed to add XDomain device\n");
  760. return;
  761. }
  762. } else {
  763. kobject_uevent(&xd->dev.kobj, KOBJ_CHANGE);
  764. }
  765. enumerate_services(xd);
  766. return;
  767. err_free_dir:
  768. tb_property_free_dir(dir);
  769. err_free_block:
  770. kfree(block);
  771. mutex_unlock(&xd->lock);
  772. }
  773. static void tb_xdomain_properties_changed(struct work_struct *work)
  774. {
  775. struct tb_xdomain *xd = container_of(work, typeof(*xd),
  776. properties_changed_work.work);
  777. int ret;
  778. ret = tb_xdp_properties_changed_request(xd->tb->ctl, xd->route,
  779. xd->properties_changed_retries, xd->local_uuid);
  780. if (ret) {
  781. if (xd->properties_changed_retries-- > 0)
  782. queue_delayed_work(xd->tb->wq,
  783. &xd->properties_changed_work,
  784. msecs_to_jiffies(1000));
  785. return;
  786. }
  787. xd->properties_changed_retries = XDOMAIN_PROPERTIES_CHANGED_RETRIES;
  788. }
  789. static ssize_t device_show(struct device *dev, struct device_attribute *attr,
  790. char *buf)
  791. {
  792. struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
  793. return sprintf(buf, "%#x\n", xd->device);
  794. }
  795. static DEVICE_ATTR_RO(device);
  796. static ssize_t
  797. device_name_show(struct device *dev, struct device_attribute *attr, char *buf)
  798. {
  799. struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
  800. int ret;
  801. if (mutex_lock_interruptible(&xd->lock))
  802. return -ERESTARTSYS;
  803. ret = sprintf(buf, "%s\n", xd->device_name ? xd->device_name : "");
  804. mutex_unlock(&xd->lock);
  805. return ret;
  806. }
  807. static DEVICE_ATTR_RO(device_name);
  808. static ssize_t vendor_show(struct device *dev, struct device_attribute *attr,
  809. char *buf)
  810. {
  811. struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
  812. return sprintf(buf, "%#x\n", xd->vendor);
  813. }
  814. static DEVICE_ATTR_RO(vendor);
  815. static ssize_t
  816. vendor_name_show(struct device *dev, struct device_attribute *attr, char *buf)
  817. {
  818. struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
  819. int ret;
  820. if (mutex_lock_interruptible(&xd->lock))
  821. return -ERESTARTSYS;
  822. ret = sprintf(buf, "%s\n", xd->vendor_name ? xd->vendor_name : "");
  823. mutex_unlock(&xd->lock);
  824. return ret;
  825. }
  826. static DEVICE_ATTR_RO(vendor_name);
  827. static ssize_t unique_id_show(struct device *dev, struct device_attribute *attr,
  828. char *buf)
  829. {
  830. struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
  831. return sprintf(buf, "%pUb\n", xd->remote_uuid);
  832. }
  833. static DEVICE_ATTR_RO(unique_id);
  834. static struct attribute *xdomain_attrs[] = {
  835. &dev_attr_device.attr,
  836. &dev_attr_device_name.attr,
  837. &dev_attr_unique_id.attr,
  838. &dev_attr_vendor.attr,
  839. &dev_attr_vendor_name.attr,
  840. NULL,
  841. };
  842. static struct attribute_group xdomain_attr_group = {
  843. .attrs = xdomain_attrs,
  844. };
  845. static const struct attribute_group *xdomain_attr_groups[] = {
  846. &xdomain_attr_group,
  847. NULL,
  848. };
  849. static void tb_xdomain_release(struct device *dev)
  850. {
  851. struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
  852. put_device(xd->dev.parent);
  853. tb_property_free_dir(xd->properties);
  854. ida_destroy(&xd->service_ids);
  855. kfree(xd->local_uuid);
  856. kfree(xd->remote_uuid);
  857. kfree(xd->device_name);
  858. kfree(xd->vendor_name);
  859. kfree(xd);
  860. }
  861. static void start_handshake(struct tb_xdomain *xd)
  862. {
  863. xd->properties_retries = XDOMAIN_PROPERTIES_RETRIES;
  864. xd->properties_changed_retries = XDOMAIN_PROPERTIES_CHANGED_RETRIES;
  865. /* Start exchanging properties with the other host */
  866. queue_delayed_work(xd->tb->wq, &xd->properties_changed_work,
  867. msecs_to_jiffies(100));
  868. queue_delayed_work(xd->tb->wq, &xd->get_properties_work,
  869. msecs_to_jiffies(1000));
  870. }
  871. static void stop_handshake(struct tb_xdomain *xd)
  872. {
  873. xd->properties_retries = 0;
  874. xd->properties_changed_retries = 0;
  875. cancel_delayed_work_sync(&xd->get_properties_work);
  876. cancel_delayed_work_sync(&xd->properties_changed_work);
  877. }
  878. static int __maybe_unused tb_xdomain_suspend(struct device *dev)
  879. {
  880. stop_handshake(tb_to_xdomain(dev));
  881. return 0;
  882. }
  883. static int __maybe_unused tb_xdomain_resume(struct device *dev)
  884. {
  885. struct tb_xdomain *xd = tb_to_xdomain(dev);
  886. /*
  887. * Ask tb_xdomain_get_properties() restore any existing DMA
  888. * paths after properties are re-read.
  889. */
  890. xd->resume = true;
  891. start_handshake(xd);
  892. return 0;
  893. }
  894. static const struct dev_pm_ops tb_xdomain_pm_ops = {
  895. SET_SYSTEM_SLEEP_PM_OPS(tb_xdomain_suspend, tb_xdomain_resume)
  896. };
  897. struct device_type tb_xdomain_type = {
  898. .name = "thunderbolt_xdomain",
  899. .release = tb_xdomain_release,
  900. .pm = &tb_xdomain_pm_ops,
  901. };
  902. EXPORT_SYMBOL_GPL(tb_xdomain_type);
  903. /**
  904. * tb_xdomain_alloc() - Allocate new XDomain object
  905. * @tb: Domain where the XDomain belongs
  906. * @parent: Parent device (the switch through the connection to the
  907. * other domain is reached).
  908. * @route: Route string used to reach the other domain
  909. * @local_uuid: Our local domain UUID
  910. * @remote_uuid: UUID of the other domain
  911. *
  912. * Allocates new XDomain structure and returns pointer to that. The
  913. * object must be released by calling tb_xdomain_put().
  914. */
  915. struct tb_xdomain *tb_xdomain_alloc(struct tb *tb, struct device *parent,
  916. u64 route, const uuid_t *local_uuid,
  917. const uuid_t *remote_uuid)
  918. {
  919. struct tb_xdomain *xd;
  920. xd = kzalloc(sizeof(*xd), GFP_KERNEL);
  921. if (!xd)
  922. return NULL;
  923. xd->tb = tb;
  924. xd->route = route;
  925. ida_init(&xd->service_ids);
  926. mutex_init(&xd->lock);
  927. INIT_DELAYED_WORK(&xd->get_properties_work, tb_xdomain_get_properties);
  928. INIT_DELAYED_WORK(&xd->properties_changed_work,
  929. tb_xdomain_properties_changed);
  930. xd->local_uuid = kmemdup(local_uuid, sizeof(uuid_t), GFP_KERNEL);
  931. if (!xd->local_uuid)
  932. goto err_free;
  933. xd->remote_uuid = kmemdup(remote_uuid, sizeof(uuid_t), GFP_KERNEL);
  934. if (!xd->remote_uuid)
  935. goto err_free_local_uuid;
  936. device_initialize(&xd->dev);
  937. xd->dev.parent = get_device(parent);
  938. xd->dev.bus = &tb_bus_type;
  939. xd->dev.type = &tb_xdomain_type;
  940. xd->dev.groups = xdomain_attr_groups;
  941. dev_set_name(&xd->dev, "%u-%llx", tb->index, route);
  942. /*
  943. * This keeps the DMA powered on as long as we have active
  944. * connection to another host.
  945. */
  946. pm_runtime_set_active(&xd->dev);
  947. pm_runtime_get_noresume(&xd->dev);
  948. pm_runtime_enable(&xd->dev);
  949. return xd;
  950. err_free_local_uuid:
  951. kfree(xd->local_uuid);
  952. err_free:
  953. kfree(xd);
  954. return NULL;
  955. }
  956. /**
  957. * tb_xdomain_add() - Add XDomain to the bus
  958. * @xd: XDomain to add
  959. *
  960. * This function starts XDomain discovery protocol handshake and
  961. * eventually adds the XDomain to the bus. After calling this function
  962. * the caller needs to call tb_xdomain_remove() in order to remove and
  963. * release the object regardless whether the handshake succeeded or not.
  964. */
  965. void tb_xdomain_add(struct tb_xdomain *xd)
  966. {
  967. /* Start exchanging properties with the other host */
  968. start_handshake(xd);
  969. }
  970. static int unregister_service(struct device *dev, void *data)
  971. {
  972. device_unregister(dev);
  973. return 0;
  974. }
  975. /**
  976. * tb_xdomain_remove() - Remove XDomain from the bus
  977. * @xd: XDomain to remove
  978. *
  979. * This will stop all ongoing configuration work and remove the XDomain
  980. * along with any services from the bus. When the last reference to @xd
  981. * is released the object will be released as well.
  982. */
  983. void tb_xdomain_remove(struct tb_xdomain *xd)
  984. {
  985. stop_handshake(xd);
  986. device_for_each_child_reverse(&xd->dev, xd, unregister_service);
  987. /*
  988. * Undo runtime PM here explicitly because it is possible that
  989. * the XDomain was never added to the bus and thus device_del()
  990. * is not called for it (device_del() would handle this otherwise).
  991. */
  992. pm_runtime_disable(&xd->dev);
  993. pm_runtime_put_noidle(&xd->dev);
  994. pm_runtime_set_suspended(&xd->dev);
  995. if (!device_is_registered(&xd->dev))
  996. put_device(&xd->dev);
  997. else
  998. device_unregister(&xd->dev);
  999. }
  1000. /**
  1001. * tb_xdomain_enable_paths() - Enable DMA paths for XDomain connection
  1002. * @xd: XDomain connection
  1003. * @transmit_path: HopID of the transmit path the other end is using to
  1004. * send packets
  1005. * @transmit_ring: DMA ring used to receive packets from the other end
  1006. * @receive_path: HopID of the receive path the other end is using to
  1007. * receive packets
  1008. * @receive_ring: DMA ring used to send packets to the other end
  1009. *
  1010. * The function enables DMA paths accordingly so that after successful
  1011. * return the caller can send and receive packets using high-speed DMA
  1012. * path.
  1013. *
  1014. * Return: %0 in case of success and negative errno in case of error
  1015. */
  1016. int tb_xdomain_enable_paths(struct tb_xdomain *xd, u16 transmit_path,
  1017. u16 transmit_ring, u16 receive_path,
  1018. u16 receive_ring)
  1019. {
  1020. int ret;
  1021. mutex_lock(&xd->lock);
  1022. if (xd->transmit_path) {
  1023. ret = xd->transmit_path == transmit_path ? 0 : -EBUSY;
  1024. goto exit_unlock;
  1025. }
  1026. xd->transmit_path = transmit_path;
  1027. xd->transmit_ring = transmit_ring;
  1028. xd->receive_path = receive_path;
  1029. xd->receive_ring = receive_ring;
  1030. ret = tb_domain_approve_xdomain_paths(xd->tb, xd);
  1031. exit_unlock:
  1032. mutex_unlock(&xd->lock);
  1033. return ret;
  1034. }
  1035. EXPORT_SYMBOL_GPL(tb_xdomain_enable_paths);
  1036. /**
  1037. * tb_xdomain_disable_paths() - Disable DMA paths for XDomain connection
  1038. * @xd: XDomain connection
  1039. *
  1040. * This does the opposite of tb_xdomain_enable_paths(). After call to
  1041. * this the caller is not expected to use the rings anymore.
  1042. *
  1043. * Return: %0 in case of success and negative errno in case of error
  1044. */
  1045. int tb_xdomain_disable_paths(struct tb_xdomain *xd)
  1046. {
  1047. int ret = 0;
  1048. mutex_lock(&xd->lock);
  1049. if (xd->transmit_path) {
  1050. xd->transmit_path = 0;
  1051. xd->transmit_ring = 0;
  1052. xd->receive_path = 0;
  1053. xd->receive_ring = 0;
  1054. ret = tb_domain_disconnect_xdomain_paths(xd->tb, xd);
  1055. }
  1056. mutex_unlock(&xd->lock);
  1057. return ret;
  1058. }
  1059. EXPORT_SYMBOL_GPL(tb_xdomain_disable_paths);
  1060. struct tb_xdomain_lookup {
  1061. const uuid_t *uuid;
  1062. u8 link;
  1063. u8 depth;
  1064. u64 route;
  1065. };
  1066. static struct tb_xdomain *switch_find_xdomain(struct tb_switch *sw,
  1067. const struct tb_xdomain_lookup *lookup)
  1068. {
  1069. int i;
  1070. for (i = 1; i <= sw->config.max_port_number; i++) {
  1071. struct tb_port *port = &sw->ports[i];
  1072. struct tb_xdomain *xd;
  1073. if (tb_is_upstream_port(port))
  1074. continue;
  1075. if (port->xdomain) {
  1076. xd = port->xdomain;
  1077. if (lookup->uuid) {
  1078. if (uuid_equal(xd->remote_uuid, lookup->uuid))
  1079. return xd;
  1080. } else if (lookup->link &&
  1081. lookup->link == xd->link &&
  1082. lookup->depth == xd->depth) {
  1083. return xd;
  1084. } else if (lookup->route &&
  1085. lookup->route == xd->route) {
  1086. return xd;
  1087. }
  1088. } else if (port->remote) {
  1089. xd = switch_find_xdomain(port->remote->sw, lookup);
  1090. if (xd)
  1091. return xd;
  1092. }
  1093. }
  1094. return NULL;
  1095. }
  1096. /**
  1097. * tb_xdomain_find_by_uuid() - Find an XDomain by UUID
  1098. * @tb: Domain where the XDomain belongs to
  1099. * @uuid: UUID to look for
  1100. *
  1101. * Finds XDomain by walking through the Thunderbolt topology below @tb.
  1102. * The returned XDomain will have its reference count increased so the
  1103. * caller needs to call tb_xdomain_put() when it is done with the
  1104. * object.
  1105. *
  1106. * This will find all XDomains including the ones that are not yet added
  1107. * to the bus (handshake is still in progress).
  1108. *
  1109. * The caller needs to hold @tb->lock.
  1110. */
  1111. struct tb_xdomain *tb_xdomain_find_by_uuid(struct tb *tb, const uuid_t *uuid)
  1112. {
  1113. struct tb_xdomain_lookup lookup;
  1114. struct tb_xdomain *xd;
  1115. memset(&lookup, 0, sizeof(lookup));
  1116. lookup.uuid = uuid;
  1117. xd = switch_find_xdomain(tb->root_switch, &lookup);
  1118. return tb_xdomain_get(xd);
  1119. }
  1120. EXPORT_SYMBOL_GPL(tb_xdomain_find_by_uuid);
  1121. /**
  1122. * tb_xdomain_find_by_link_depth() - Find an XDomain by link and depth
  1123. * @tb: Domain where the XDomain belongs to
  1124. * @link: Root switch link number
  1125. * @depth: Depth in the link
  1126. *
  1127. * Finds XDomain by walking through the Thunderbolt topology below @tb.
  1128. * The returned XDomain will have its reference count increased so the
  1129. * caller needs to call tb_xdomain_put() when it is done with the
  1130. * object.
  1131. *
  1132. * This will find all XDomains including the ones that are not yet added
  1133. * to the bus (handshake is still in progress).
  1134. *
  1135. * The caller needs to hold @tb->lock.
  1136. */
  1137. struct tb_xdomain *tb_xdomain_find_by_link_depth(struct tb *tb, u8 link,
  1138. u8 depth)
  1139. {
  1140. struct tb_xdomain_lookup lookup;
  1141. struct tb_xdomain *xd;
  1142. memset(&lookup, 0, sizeof(lookup));
  1143. lookup.link = link;
  1144. lookup.depth = depth;
  1145. xd = switch_find_xdomain(tb->root_switch, &lookup);
  1146. return tb_xdomain_get(xd);
  1147. }
  1148. /**
  1149. * tb_xdomain_find_by_route() - Find an XDomain by route string
  1150. * @tb: Domain where the XDomain belongs to
  1151. * @route: XDomain route string
  1152. *
  1153. * Finds XDomain by walking through the Thunderbolt topology below @tb.
  1154. * The returned XDomain will have its reference count increased so the
  1155. * caller needs to call tb_xdomain_put() when it is done with the
  1156. * object.
  1157. *
  1158. * This will find all XDomains including the ones that are not yet added
  1159. * to the bus (handshake is still in progress).
  1160. *
  1161. * The caller needs to hold @tb->lock.
  1162. */
  1163. struct tb_xdomain *tb_xdomain_find_by_route(struct tb *tb, u64 route)
  1164. {
  1165. struct tb_xdomain_lookup lookup;
  1166. struct tb_xdomain *xd;
  1167. memset(&lookup, 0, sizeof(lookup));
  1168. lookup.route = route;
  1169. xd = switch_find_xdomain(tb->root_switch, &lookup);
  1170. return tb_xdomain_get(xd);
  1171. }
  1172. EXPORT_SYMBOL_GPL(tb_xdomain_find_by_route);
  1173. bool tb_xdomain_handle_request(struct tb *tb, enum tb_cfg_pkg_type type,
  1174. const void *buf, size_t size)
  1175. {
  1176. const struct tb_protocol_handler *handler, *tmp;
  1177. const struct tb_xdp_header *hdr = buf;
  1178. unsigned int length;
  1179. int ret = 0;
  1180. /* We expect the packet is at least size of the header */
  1181. length = hdr->xd_hdr.length_sn & TB_XDOMAIN_LENGTH_MASK;
  1182. if (length != size / 4 - sizeof(hdr->xd_hdr) / 4)
  1183. return true;
  1184. if (length < sizeof(*hdr) / 4 - sizeof(hdr->xd_hdr) / 4)
  1185. return true;
  1186. /*
  1187. * Handle XDomain discovery protocol packets directly here. For
  1188. * other protocols (based on their UUID) we call registered
  1189. * handlers in turn.
  1190. */
  1191. if (uuid_equal(&hdr->uuid, &tb_xdp_uuid)) {
  1192. if (type == TB_CFG_PKG_XDOMAIN_REQ) {
  1193. tb_xdp_schedule_request(tb, hdr, size);
  1194. return true;
  1195. }
  1196. return false;
  1197. }
  1198. mutex_lock(&xdomain_lock);
  1199. list_for_each_entry_safe(handler, tmp, &protocol_handlers, list) {
  1200. if (!uuid_equal(&hdr->uuid, handler->uuid))
  1201. continue;
  1202. mutex_unlock(&xdomain_lock);
  1203. ret = handler->callback(buf, size, handler->data);
  1204. mutex_lock(&xdomain_lock);
  1205. if (ret)
  1206. break;
  1207. }
  1208. mutex_unlock(&xdomain_lock);
  1209. return ret > 0;
  1210. }
  1211. static int rebuild_property_block(void)
  1212. {
  1213. u32 *block, len;
  1214. int ret;
  1215. ret = tb_property_format_dir(xdomain_property_dir, NULL, 0);
  1216. if (ret < 0)
  1217. return ret;
  1218. len = ret;
  1219. block = kcalloc(len, sizeof(u32), GFP_KERNEL);
  1220. if (!block)
  1221. return -ENOMEM;
  1222. ret = tb_property_format_dir(xdomain_property_dir, block, len);
  1223. if (ret) {
  1224. kfree(block);
  1225. return ret;
  1226. }
  1227. kfree(xdomain_property_block);
  1228. xdomain_property_block = block;
  1229. xdomain_property_block_len = len;
  1230. xdomain_property_block_gen++;
  1231. return 0;
  1232. }
  1233. static int update_xdomain(struct device *dev, void *data)
  1234. {
  1235. struct tb_xdomain *xd;
  1236. xd = tb_to_xdomain(dev);
  1237. if (xd) {
  1238. queue_delayed_work(xd->tb->wq, &xd->properties_changed_work,
  1239. msecs_to_jiffies(50));
  1240. }
  1241. return 0;
  1242. }
  1243. static void update_all_xdomains(void)
  1244. {
  1245. bus_for_each_dev(&tb_bus_type, NULL, NULL, update_xdomain);
  1246. }
  1247. static bool remove_directory(const char *key, const struct tb_property_dir *dir)
  1248. {
  1249. struct tb_property *p;
  1250. p = tb_property_find(xdomain_property_dir, key,
  1251. TB_PROPERTY_TYPE_DIRECTORY);
  1252. if (p && p->value.dir == dir) {
  1253. tb_property_remove(p);
  1254. return true;
  1255. }
  1256. return false;
  1257. }
  1258. /**
  1259. * tb_register_property_dir() - Register property directory to the host
  1260. * @key: Key (name) of the directory to add
  1261. * @dir: Directory to add
  1262. *
  1263. * Service drivers can use this function to add new property directory
  1264. * to the host available properties. The other connected hosts are
  1265. * notified so they can re-read properties of this host if they are
  1266. * interested.
  1267. *
  1268. * Return: %0 on success and negative errno on failure
  1269. */
  1270. int tb_register_property_dir(const char *key, struct tb_property_dir *dir)
  1271. {
  1272. int ret;
  1273. if (WARN_ON(!xdomain_property_dir))
  1274. return -EAGAIN;
  1275. if (!key || strlen(key) > 8)
  1276. return -EINVAL;
  1277. mutex_lock(&xdomain_lock);
  1278. if (tb_property_find(xdomain_property_dir, key,
  1279. TB_PROPERTY_TYPE_DIRECTORY)) {
  1280. ret = -EEXIST;
  1281. goto err_unlock;
  1282. }
  1283. ret = tb_property_add_dir(xdomain_property_dir, key, dir);
  1284. if (ret)
  1285. goto err_unlock;
  1286. ret = rebuild_property_block();
  1287. if (ret) {
  1288. remove_directory(key, dir);
  1289. goto err_unlock;
  1290. }
  1291. mutex_unlock(&xdomain_lock);
  1292. update_all_xdomains();
  1293. return 0;
  1294. err_unlock:
  1295. mutex_unlock(&xdomain_lock);
  1296. return ret;
  1297. }
  1298. EXPORT_SYMBOL_GPL(tb_register_property_dir);
  1299. /**
  1300. * tb_unregister_property_dir() - Removes property directory from host
  1301. * @key: Key (name) of the directory
  1302. * @dir: Directory to remove
  1303. *
  1304. * This will remove the existing directory from this host and notify the
  1305. * connected hosts about the change.
  1306. */
  1307. void tb_unregister_property_dir(const char *key, struct tb_property_dir *dir)
  1308. {
  1309. int ret = 0;
  1310. mutex_lock(&xdomain_lock);
  1311. if (remove_directory(key, dir))
  1312. ret = rebuild_property_block();
  1313. mutex_unlock(&xdomain_lock);
  1314. if (!ret)
  1315. update_all_xdomains();
  1316. }
  1317. EXPORT_SYMBOL_GPL(tb_unregister_property_dir);
  1318. int tb_xdomain_init(void)
  1319. {
  1320. int ret;
  1321. xdomain_property_dir = tb_property_create_dir(NULL);
  1322. if (!xdomain_property_dir)
  1323. return -ENOMEM;
  1324. /*
  1325. * Initialize standard set of properties without any service
  1326. * directories. Those will be added by service drivers
  1327. * themselves when they are loaded.
  1328. */
  1329. tb_property_add_immediate(xdomain_property_dir, "vendorid",
  1330. PCI_VENDOR_ID_INTEL);
  1331. tb_property_add_text(xdomain_property_dir, "vendorid", "Intel Corp.");
  1332. tb_property_add_immediate(xdomain_property_dir, "deviceid", 0x1);
  1333. tb_property_add_text(xdomain_property_dir, "deviceid",
  1334. utsname()->nodename);
  1335. tb_property_add_immediate(xdomain_property_dir, "devicerv", 0x80000100);
  1336. ret = rebuild_property_block();
  1337. if (ret) {
  1338. tb_property_free_dir(xdomain_property_dir);
  1339. xdomain_property_dir = NULL;
  1340. }
  1341. return ret;
  1342. }
  1343. void tb_xdomain_exit(void)
  1344. {
  1345. kfree(xdomain_property_block);
  1346. tb_property_free_dir(xdomain_property_dir);
  1347. }