xdomain.c 39 KB

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