xdomain.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618
  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->key);
  644. kfree(svc);
  645. break;
  646. }
  647. svc->id = id;
  648. svc->dev.bus = &tb_bus_type;
  649. svc->dev.type = &tb_service_type;
  650. svc->dev.parent = &xd->dev;
  651. dev_set_name(&svc->dev, "%s.%d", dev_name(&xd->dev), svc->id);
  652. if (device_register(&svc->dev)) {
  653. put_device(&svc->dev);
  654. break;
  655. }
  656. }
  657. }
  658. static int populate_properties(struct tb_xdomain *xd,
  659. struct tb_property_dir *dir)
  660. {
  661. const struct tb_property *p;
  662. /* Required properties */
  663. p = tb_property_find(dir, "deviceid", TB_PROPERTY_TYPE_VALUE);
  664. if (!p)
  665. return -EINVAL;
  666. xd->device = p->value.immediate;
  667. p = tb_property_find(dir, "vendorid", TB_PROPERTY_TYPE_VALUE);
  668. if (!p)
  669. return -EINVAL;
  670. xd->vendor = p->value.immediate;
  671. kfree(xd->device_name);
  672. xd->device_name = NULL;
  673. kfree(xd->vendor_name);
  674. xd->vendor_name = NULL;
  675. /* Optional properties */
  676. p = tb_property_find(dir, "deviceid", TB_PROPERTY_TYPE_TEXT);
  677. if (p)
  678. xd->device_name = kstrdup(p->value.text, GFP_KERNEL);
  679. p = tb_property_find(dir, "vendorid", TB_PROPERTY_TYPE_TEXT);
  680. if (p)
  681. xd->vendor_name = kstrdup(p->value.text, GFP_KERNEL);
  682. return 0;
  683. }
  684. /* Called with @xd->lock held */
  685. static void tb_xdomain_restore_paths(struct tb_xdomain *xd)
  686. {
  687. if (!xd->resume)
  688. return;
  689. xd->resume = false;
  690. if (xd->transmit_path) {
  691. dev_dbg(&xd->dev, "re-establishing DMA path\n");
  692. tb_domain_approve_xdomain_paths(xd->tb, xd);
  693. }
  694. }
  695. static void tb_xdomain_get_properties(struct work_struct *work)
  696. {
  697. struct tb_xdomain *xd = container_of(work, typeof(*xd),
  698. get_properties_work.work);
  699. struct tb_property_dir *dir;
  700. struct tb *tb = xd->tb;
  701. bool update = false;
  702. u32 *block = NULL;
  703. u32 gen = 0;
  704. int ret;
  705. ret = tb_xdp_properties_request(tb->ctl, xd->route, xd->local_uuid,
  706. xd->remote_uuid, xd->properties_retries,
  707. &block, &gen);
  708. if (ret < 0) {
  709. if (xd->properties_retries-- > 0) {
  710. queue_delayed_work(xd->tb->wq, &xd->get_properties_work,
  711. msecs_to_jiffies(1000));
  712. } else {
  713. /* Give up now */
  714. dev_err(&xd->dev,
  715. "failed read XDomain properties from %pUb\n",
  716. xd->remote_uuid);
  717. }
  718. return;
  719. }
  720. xd->properties_retries = XDOMAIN_PROPERTIES_RETRIES;
  721. mutex_lock(&xd->lock);
  722. /* Only accept newer generation properties */
  723. if (xd->properties && gen <= xd->property_block_gen) {
  724. /*
  725. * On resume it is likely that the properties block is
  726. * not changed (unless the other end added or removed
  727. * services). However, we need to make sure the existing
  728. * DMA paths are restored properly.
  729. */
  730. tb_xdomain_restore_paths(xd);
  731. goto err_free_block;
  732. }
  733. dir = tb_property_parse_dir(block, ret);
  734. if (!dir) {
  735. dev_err(&xd->dev, "failed to parse XDomain properties\n");
  736. goto err_free_block;
  737. }
  738. ret = populate_properties(xd, dir);
  739. if (ret) {
  740. dev_err(&xd->dev, "missing XDomain properties in response\n");
  741. goto err_free_dir;
  742. }
  743. /* Release the existing one */
  744. if (xd->properties) {
  745. tb_property_free_dir(xd->properties);
  746. update = true;
  747. }
  748. xd->properties = dir;
  749. xd->property_block_gen = gen;
  750. tb_xdomain_restore_paths(xd);
  751. mutex_unlock(&xd->lock);
  752. kfree(block);
  753. /*
  754. * Now the device should be ready enough so we can add it to the
  755. * bus and let userspace know about it. If the device is already
  756. * registered, we notify the userspace that it has changed.
  757. */
  758. if (!update) {
  759. if (device_add(&xd->dev)) {
  760. dev_err(&xd->dev, "failed to add XDomain device\n");
  761. return;
  762. }
  763. } else {
  764. kobject_uevent(&xd->dev.kobj, KOBJ_CHANGE);
  765. }
  766. enumerate_services(xd);
  767. return;
  768. err_free_dir:
  769. tb_property_free_dir(dir);
  770. err_free_block:
  771. kfree(block);
  772. mutex_unlock(&xd->lock);
  773. }
  774. static void tb_xdomain_properties_changed(struct work_struct *work)
  775. {
  776. struct tb_xdomain *xd = container_of(work, typeof(*xd),
  777. properties_changed_work.work);
  778. int ret;
  779. ret = tb_xdp_properties_changed_request(xd->tb->ctl, xd->route,
  780. xd->properties_changed_retries, xd->local_uuid);
  781. if (ret) {
  782. if (xd->properties_changed_retries-- > 0)
  783. queue_delayed_work(xd->tb->wq,
  784. &xd->properties_changed_work,
  785. msecs_to_jiffies(1000));
  786. return;
  787. }
  788. xd->properties_changed_retries = XDOMAIN_PROPERTIES_CHANGED_RETRIES;
  789. }
  790. static ssize_t device_show(struct device *dev, struct device_attribute *attr,
  791. char *buf)
  792. {
  793. struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
  794. return sprintf(buf, "%#x\n", xd->device);
  795. }
  796. static DEVICE_ATTR_RO(device);
  797. static ssize_t
  798. device_name_show(struct device *dev, struct device_attribute *attr, char *buf)
  799. {
  800. struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
  801. int ret;
  802. if (mutex_lock_interruptible(&xd->lock))
  803. return -ERESTARTSYS;
  804. ret = sprintf(buf, "%s\n", xd->device_name ? xd->device_name : "");
  805. mutex_unlock(&xd->lock);
  806. return ret;
  807. }
  808. static DEVICE_ATTR_RO(device_name);
  809. static ssize_t vendor_show(struct device *dev, struct device_attribute *attr,
  810. char *buf)
  811. {
  812. struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
  813. return sprintf(buf, "%#x\n", xd->vendor);
  814. }
  815. static DEVICE_ATTR_RO(vendor);
  816. static ssize_t
  817. vendor_name_show(struct device *dev, struct device_attribute *attr, char *buf)
  818. {
  819. struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
  820. int ret;
  821. if (mutex_lock_interruptible(&xd->lock))
  822. return -ERESTARTSYS;
  823. ret = sprintf(buf, "%s\n", xd->vendor_name ? xd->vendor_name : "");
  824. mutex_unlock(&xd->lock);
  825. return ret;
  826. }
  827. static DEVICE_ATTR_RO(vendor_name);
  828. static ssize_t unique_id_show(struct device *dev, struct device_attribute *attr,
  829. char *buf)
  830. {
  831. struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
  832. return sprintf(buf, "%pUb\n", xd->remote_uuid);
  833. }
  834. static DEVICE_ATTR_RO(unique_id);
  835. static struct attribute *xdomain_attrs[] = {
  836. &dev_attr_device.attr,
  837. &dev_attr_device_name.attr,
  838. &dev_attr_unique_id.attr,
  839. &dev_attr_vendor.attr,
  840. &dev_attr_vendor_name.attr,
  841. NULL,
  842. };
  843. static struct attribute_group xdomain_attr_group = {
  844. .attrs = xdomain_attrs,
  845. };
  846. static const struct attribute_group *xdomain_attr_groups[] = {
  847. &xdomain_attr_group,
  848. NULL,
  849. };
  850. static void tb_xdomain_release(struct device *dev)
  851. {
  852. struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
  853. put_device(xd->dev.parent);
  854. tb_property_free_dir(xd->properties);
  855. ida_destroy(&xd->service_ids);
  856. kfree(xd->local_uuid);
  857. kfree(xd->remote_uuid);
  858. kfree(xd->device_name);
  859. kfree(xd->vendor_name);
  860. kfree(xd);
  861. }
  862. static void start_handshake(struct tb_xdomain *xd)
  863. {
  864. xd->properties_retries = XDOMAIN_PROPERTIES_RETRIES;
  865. xd->properties_changed_retries = XDOMAIN_PROPERTIES_CHANGED_RETRIES;
  866. /* Start exchanging properties with the other host */
  867. queue_delayed_work(xd->tb->wq, &xd->properties_changed_work,
  868. msecs_to_jiffies(100));
  869. queue_delayed_work(xd->tb->wq, &xd->get_properties_work,
  870. msecs_to_jiffies(1000));
  871. }
  872. static void stop_handshake(struct tb_xdomain *xd)
  873. {
  874. xd->properties_retries = 0;
  875. xd->properties_changed_retries = 0;
  876. cancel_delayed_work_sync(&xd->get_properties_work);
  877. cancel_delayed_work_sync(&xd->properties_changed_work);
  878. }
  879. static int __maybe_unused tb_xdomain_suspend(struct device *dev)
  880. {
  881. stop_handshake(tb_to_xdomain(dev));
  882. return 0;
  883. }
  884. static int __maybe_unused tb_xdomain_resume(struct device *dev)
  885. {
  886. struct tb_xdomain *xd = tb_to_xdomain(dev);
  887. /*
  888. * Ask tb_xdomain_get_properties() restore any existing DMA
  889. * paths after properties are re-read.
  890. */
  891. xd->resume = true;
  892. start_handshake(xd);
  893. return 0;
  894. }
  895. static const struct dev_pm_ops tb_xdomain_pm_ops = {
  896. SET_SYSTEM_SLEEP_PM_OPS(tb_xdomain_suspend, tb_xdomain_resume)
  897. };
  898. struct device_type tb_xdomain_type = {
  899. .name = "thunderbolt_xdomain",
  900. .release = tb_xdomain_release,
  901. .pm = &tb_xdomain_pm_ops,
  902. };
  903. EXPORT_SYMBOL_GPL(tb_xdomain_type);
  904. /**
  905. * tb_xdomain_alloc() - Allocate new XDomain object
  906. * @tb: Domain where the XDomain belongs
  907. * @parent: Parent device (the switch through the connection to the
  908. * other domain is reached).
  909. * @route: Route string used to reach the other domain
  910. * @local_uuid: Our local domain UUID
  911. * @remote_uuid: UUID of the other domain
  912. *
  913. * Allocates new XDomain structure and returns pointer to that. The
  914. * object must be released by calling tb_xdomain_put().
  915. */
  916. struct tb_xdomain *tb_xdomain_alloc(struct tb *tb, struct device *parent,
  917. u64 route, const uuid_t *local_uuid,
  918. const uuid_t *remote_uuid)
  919. {
  920. struct tb_xdomain *xd;
  921. xd = kzalloc(sizeof(*xd), GFP_KERNEL);
  922. if (!xd)
  923. return NULL;
  924. xd->tb = tb;
  925. xd->route = route;
  926. ida_init(&xd->service_ids);
  927. mutex_init(&xd->lock);
  928. INIT_DELAYED_WORK(&xd->get_properties_work, tb_xdomain_get_properties);
  929. INIT_DELAYED_WORK(&xd->properties_changed_work,
  930. tb_xdomain_properties_changed);
  931. xd->local_uuid = kmemdup(local_uuid, sizeof(uuid_t), GFP_KERNEL);
  932. if (!xd->local_uuid)
  933. goto err_free;
  934. xd->remote_uuid = kmemdup(remote_uuid, sizeof(uuid_t), GFP_KERNEL);
  935. if (!xd->remote_uuid)
  936. goto err_free_local_uuid;
  937. device_initialize(&xd->dev);
  938. xd->dev.parent = get_device(parent);
  939. xd->dev.bus = &tb_bus_type;
  940. xd->dev.type = &tb_xdomain_type;
  941. xd->dev.groups = xdomain_attr_groups;
  942. dev_set_name(&xd->dev, "%u-%llx", tb->index, route);
  943. /*
  944. * This keeps the DMA powered on as long as we have active
  945. * connection to another host.
  946. */
  947. pm_runtime_set_active(&xd->dev);
  948. pm_runtime_get_noresume(&xd->dev);
  949. pm_runtime_enable(&xd->dev);
  950. return xd;
  951. err_free_local_uuid:
  952. kfree(xd->local_uuid);
  953. err_free:
  954. kfree(xd);
  955. return NULL;
  956. }
  957. /**
  958. * tb_xdomain_add() - Add XDomain to the bus
  959. * @xd: XDomain to add
  960. *
  961. * This function starts XDomain discovery protocol handshake and
  962. * eventually adds the XDomain to the bus. After calling this function
  963. * the caller needs to call tb_xdomain_remove() in order to remove and
  964. * release the object regardless whether the handshake succeeded or not.
  965. */
  966. void tb_xdomain_add(struct tb_xdomain *xd)
  967. {
  968. /* Start exchanging properties with the other host */
  969. start_handshake(xd);
  970. }
  971. static int unregister_service(struct device *dev, void *data)
  972. {
  973. device_unregister(dev);
  974. return 0;
  975. }
  976. /**
  977. * tb_xdomain_remove() - Remove XDomain from the bus
  978. * @xd: XDomain to remove
  979. *
  980. * This will stop all ongoing configuration work and remove the XDomain
  981. * along with any services from the bus. When the last reference to @xd
  982. * is released the object will be released as well.
  983. */
  984. void tb_xdomain_remove(struct tb_xdomain *xd)
  985. {
  986. stop_handshake(xd);
  987. device_for_each_child_reverse(&xd->dev, xd, unregister_service);
  988. /*
  989. * Undo runtime PM here explicitly because it is possible that
  990. * the XDomain was never added to the bus and thus device_del()
  991. * is not called for it (device_del() would handle this otherwise).
  992. */
  993. pm_runtime_disable(&xd->dev);
  994. pm_runtime_put_noidle(&xd->dev);
  995. pm_runtime_set_suspended(&xd->dev);
  996. if (!device_is_registered(&xd->dev))
  997. put_device(&xd->dev);
  998. else
  999. device_unregister(&xd->dev);
  1000. }
  1001. /**
  1002. * tb_xdomain_enable_paths() - Enable DMA paths for XDomain connection
  1003. * @xd: XDomain connection
  1004. * @transmit_path: HopID of the transmit path the other end is using to
  1005. * send packets
  1006. * @transmit_ring: DMA ring used to receive packets from the other end
  1007. * @receive_path: HopID of the receive path the other end is using to
  1008. * receive packets
  1009. * @receive_ring: DMA ring used to send packets to the other end
  1010. *
  1011. * The function enables DMA paths accordingly so that after successful
  1012. * return the caller can send and receive packets using high-speed DMA
  1013. * path.
  1014. *
  1015. * Return: %0 in case of success and negative errno in case of error
  1016. */
  1017. int tb_xdomain_enable_paths(struct tb_xdomain *xd, u16 transmit_path,
  1018. u16 transmit_ring, u16 receive_path,
  1019. u16 receive_ring)
  1020. {
  1021. int ret;
  1022. mutex_lock(&xd->lock);
  1023. if (xd->transmit_path) {
  1024. ret = xd->transmit_path == transmit_path ? 0 : -EBUSY;
  1025. goto exit_unlock;
  1026. }
  1027. xd->transmit_path = transmit_path;
  1028. xd->transmit_ring = transmit_ring;
  1029. xd->receive_path = receive_path;
  1030. xd->receive_ring = receive_ring;
  1031. ret = tb_domain_approve_xdomain_paths(xd->tb, xd);
  1032. exit_unlock:
  1033. mutex_unlock(&xd->lock);
  1034. return ret;
  1035. }
  1036. EXPORT_SYMBOL_GPL(tb_xdomain_enable_paths);
  1037. /**
  1038. * tb_xdomain_disable_paths() - Disable DMA paths for XDomain connection
  1039. * @xd: XDomain connection
  1040. *
  1041. * This does the opposite of tb_xdomain_enable_paths(). After call to
  1042. * this the caller is not expected to use the rings anymore.
  1043. *
  1044. * Return: %0 in case of success and negative errno in case of error
  1045. */
  1046. int tb_xdomain_disable_paths(struct tb_xdomain *xd)
  1047. {
  1048. int ret = 0;
  1049. mutex_lock(&xd->lock);
  1050. if (xd->transmit_path) {
  1051. xd->transmit_path = 0;
  1052. xd->transmit_ring = 0;
  1053. xd->receive_path = 0;
  1054. xd->receive_ring = 0;
  1055. ret = tb_domain_disconnect_xdomain_paths(xd->tb, xd);
  1056. }
  1057. mutex_unlock(&xd->lock);
  1058. return ret;
  1059. }
  1060. EXPORT_SYMBOL_GPL(tb_xdomain_disable_paths);
  1061. struct tb_xdomain_lookup {
  1062. const uuid_t *uuid;
  1063. u8 link;
  1064. u8 depth;
  1065. u64 route;
  1066. };
  1067. static struct tb_xdomain *switch_find_xdomain(struct tb_switch *sw,
  1068. const struct tb_xdomain_lookup *lookup)
  1069. {
  1070. int i;
  1071. for (i = 1; i <= sw->config.max_port_number; i++) {
  1072. struct tb_port *port = &sw->ports[i];
  1073. struct tb_xdomain *xd;
  1074. if (tb_is_upstream_port(port))
  1075. continue;
  1076. if (port->xdomain) {
  1077. xd = port->xdomain;
  1078. if (lookup->uuid) {
  1079. if (uuid_equal(xd->remote_uuid, lookup->uuid))
  1080. return xd;
  1081. } else if (lookup->link &&
  1082. lookup->link == xd->link &&
  1083. lookup->depth == xd->depth) {
  1084. return xd;
  1085. } else if (lookup->route &&
  1086. lookup->route == xd->route) {
  1087. return xd;
  1088. }
  1089. } else if (port->remote) {
  1090. xd = switch_find_xdomain(port->remote->sw, lookup);
  1091. if (xd)
  1092. return xd;
  1093. }
  1094. }
  1095. return NULL;
  1096. }
  1097. /**
  1098. * tb_xdomain_find_by_uuid() - Find an XDomain by UUID
  1099. * @tb: Domain where the XDomain belongs to
  1100. * @uuid: UUID to look for
  1101. *
  1102. * Finds XDomain by walking through the Thunderbolt topology below @tb.
  1103. * The returned XDomain will have its reference count increased so the
  1104. * caller needs to call tb_xdomain_put() when it is done with the
  1105. * object.
  1106. *
  1107. * This will find all XDomains including the ones that are not yet added
  1108. * to the bus (handshake is still in progress).
  1109. *
  1110. * The caller needs to hold @tb->lock.
  1111. */
  1112. struct tb_xdomain *tb_xdomain_find_by_uuid(struct tb *tb, const uuid_t *uuid)
  1113. {
  1114. struct tb_xdomain_lookup lookup;
  1115. struct tb_xdomain *xd;
  1116. memset(&lookup, 0, sizeof(lookup));
  1117. lookup.uuid = uuid;
  1118. xd = switch_find_xdomain(tb->root_switch, &lookup);
  1119. return tb_xdomain_get(xd);
  1120. }
  1121. EXPORT_SYMBOL_GPL(tb_xdomain_find_by_uuid);
  1122. /**
  1123. * tb_xdomain_find_by_link_depth() - Find an XDomain by link and depth
  1124. * @tb: Domain where the XDomain belongs to
  1125. * @link: Root switch link number
  1126. * @depth: Depth in the link
  1127. *
  1128. * Finds XDomain by walking through the Thunderbolt topology below @tb.
  1129. * The returned XDomain will have its reference count increased so the
  1130. * caller needs to call tb_xdomain_put() when it is done with the
  1131. * object.
  1132. *
  1133. * This will find all XDomains including the ones that are not yet added
  1134. * to the bus (handshake is still in progress).
  1135. *
  1136. * The caller needs to hold @tb->lock.
  1137. */
  1138. struct tb_xdomain *tb_xdomain_find_by_link_depth(struct tb *tb, u8 link,
  1139. u8 depth)
  1140. {
  1141. struct tb_xdomain_lookup lookup;
  1142. struct tb_xdomain *xd;
  1143. memset(&lookup, 0, sizeof(lookup));
  1144. lookup.link = link;
  1145. lookup.depth = depth;
  1146. xd = switch_find_xdomain(tb->root_switch, &lookup);
  1147. return tb_xdomain_get(xd);
  1148. }
  1149. /**
  1150. * tb_xdomain_find_by_route() - Find an XDomain by route string
  1151. * @tb: Domain where the XDomain belongs to
  1152. * @route: XDomain route string
  1153. *
  1154. * Finds XDomain by walking through the Thunderbolt topology below @tb.
  1155. * The returned XDomain will have its reference count increased so the
  1156. * caller needs to call tb_xdomain_put() when it is done with the
  1157. * object.
  1158. *
  1159. * This will find all XDomains including the ones that are not yet added
  1160. * to the bus (handshake is still in progress).
  1161. *
  1162. * The caller needs to hold @tb->lock.
  1163. */
  1164. struct tb_xdomain *tb_xdomain_find_by_route(struct tb *tb, u64 route)
  1165. {
  1166. struct tb_xdomain_lookup lookup;
  1167. struct tb_xdomain *xd;
  1168. memset(&lookup, 0, sizeof(lookup));
  1169. lookup.route = route;
  1170. xd = switch_find_xdomain(tb->root_switch, &lookup);
  1171. return tb_xdomain_get(xd);
  1172. }
  1173. EXPORT_SYMBOL_GPL(tb_xdomain_find_by_route);
  1174. bool tb_xdomain_handle_request(struct tb *tb, enum tb_cfg_pkg_type type,
  1175. const void *buf, size_t size)
  1176. {
  1177. const struct tb_protocol_handler *handler, *tmp;
  1178. const struct tb_xdp_header *hdr = buf;
  1179. unsigned int length;
  1180. int ret = 0;
  1181. /* We expect the packet is at least size of the header */
  1182. length = hdr->xd_hdr.length_sn & TB_XDOMAIN_LENGTH_MASK;
  1183. if (length != size / 4 - sizeof(hdr->xd_hdr) / 4)
  1184. return true;
  1185. if (length < sizeof(*hdr) / 4 - sizeof(hdr->xd_hdr) / 4)
  1186. return true;
  1187. /*
  1188. * Handle XDomain discovery protocol packets directly here. For
  1189. * other protocols (based on their UUID) we call registered
  1190. * handlers in turn.
  1191. */
  1192. if (uuid_equal(&hdr->uuid, &tb_xdp_uuid)) {
  1193. if (type == TB_CFG_PKG_XDOMAIN_REQ) {
  1194. tb_xdp_schedule_request(tb, hdr, size);
  1195. return true;
  1196. }
  1197. return false;
  1198. }
  1199. mutex_lock(&xdomain_lock);
  1200. list_for_each_entry_safe(handler, tmp, &protocol_handlers, list) {
  1201. if (!uuid_equal(&hdr->uuid, handler->uuid))
  1202. continue;
  1203. mutex_unlock(&xdomain_lock);
  1204. ret = handler->callback(buf, size, handler->data);
  1205. mutex_lock(&xdomain_lock);
  1206. if (ret)
  1207. break;
  1208. }
  1209. mutex_unlock(&xdomain_lock);
  1210. return ret > 0;
  1211. }
  1212. static int rebuild_property_block(void)
  1213. {
  1214. u32 *block, len;
  1215. int ret;
  1216. ret = tb_property_format_dir(xdomain_property_dir, NULL, 0);
  1217. if (ret < 0)
  1218. return ret;
  1219. len = ret;
  1220. block = kcalloc(len, sizeof(u32), GFP_KERNEL);
  1221. if (!block)
  1222. return -ENOMEM;
  1223. ret = tb_property_format_dir(xdomain_property_dir, block, len);
  1224. if (ret) {
  1225. kfree(block);
  1226. return ret;
  1227. }
  1228. kfree(xdomain_property_block);
  1229. xdomain_property_block = block;
  1230. xdomain_property_block_len = len;
  1231. xdomain_property_block_gen++;
  1232. return 0;
  1233. }
  1234. static int update_xdomain(struct device *dev, void *data)
  1235. {
  1236. struct tb_xdomain *xd;
  1237. xd = tb_to_xdomain(dev);
  1238. if (xd) {
  1239. queue_delayed_work(xd->tb->wq, &xd->properties_changed_work,
  1240. msecs_to_jiffies(50));
  1241. }
  1242. return 0;
  1243. }
  1244. static void update_all_xdomains(void)
  1245. {
  1246. bus_for_each_dev(&tb_bus_type, NULL, NULL, update_xdomain);
  1247. }
  1248. static bool remove_directory(const char *key, const struct tb_property_dir *dir)
  1249. {
  1250. struct tb_property *p;
  1251. p = tb_property_find(xdomain_property_dir, key,
  1252. TB_PROPERTY_TYPE_DIRECTORY);
  1253. if (p && p->value.dir == dir) {
  1254. tb_property_remove(p);
  1255. return true;
  1256. }
  1257. return false;
  1258. }
  1259. /**
  1260. * tb_register_property_dir() - Register property directory to the host
  1261. * @key: Key (name) of the directory to add
  1262. * @dir: Directory to add
  1263. *
  1264. * Service drivers can use this function to add new property directory
  1265. * to the host available properties. The other connected hosts are
  1266. * notified so they can re-read properties of this host if they are
  1267. * interested.
  1268. *
  1269. * Return: %0 on success and negative errno on failure
  1270. */
  1271. int tb_register_property_dir(const char *key, struct tb_property_dir *dir)
  1272. {
  1273. int ret;
  1274. if (WARN_ON(!xdomain_property_dir))
  1275. return -EAGAIN;
  1276. if (!key || strlen(key) > 8)
  1277. return -EINVAL;
  1278. mutex_lock(&xdomain_lock);
  1279. if (tb_property_find(xdomain_property_dir, key,
  1280. TB_PROPERTY_TYPE_DIRECTORY)) {
  1281. ret = -EEXIST;
  1282. goto err_unlock;
  1283. }
  1284. ret = tb_property_add_dir(xdomain_property_dir, key, dir);
  1285. if (ret)
  1286. goto err_unlock;
  1287. ret = rebuild_property_block();
  1288. if (ret) {
  1289. remove_directory(key, dir);
  1290. goto err_unlock;
  1291. }
  1292. mutex_unlock(&xdomain_lock);
  1293. update_all_xdomains();
  1294. return 0;
  1295. err_unlock:
  1296. mutex_unlock(&xdomain_lock);
  1297. return ret;
  1298. }
  1299. EXPORT_SYMBOL_GPL(tb_register_property_dir);
  1300. /**
  1301. * tb_unregister_property_dir() - Removes property directory from host
  1302. * @key: Key (name) of the directory
  1303. * @dir: Directory to remove
  1304. *
  1305. * This will remove the existing directory from this host and notify the
  1306. * connected hosts about the change.
  1307. */
  1308. void tb_unregister_property_dir(const char *key, struct tb_property_dir *dir)
  1309. {
  1310. int ret = 0;
  1311. mutex_lock(&xdomain_lock);
  1312. if (remove_directory(key, dir))
  1313. ret = rebuild_property_block();
  1314. mutex_unlock(&xdomain_lock);
  1315. if (!ret)
  1316. update_all_xdomains();
  1317. }
  1318. EXPORT_SYMBOL_GPL(tb_unregister_property_dir);
  1319. int tb_xdomain_init(void)
  1320. {
  1321. int ret;
  1322. xdomain_property_dir = tb_property_create_dir(NULL);
  1323. if (!xdomain_property_dir)
  1324. return -ENOMEM;
  1325. /*
  1326. * Initialize standard set of properties without any service
  1327. * directories. Those will be added by service drivers
  1328. * themselves when they are loaded.
  1329. */
  1330. tb_property_add_immediate(xdomain_property_dir, "vendorid",
  1331. PCI_VENDOR_ID_INTEL);
  1332. tb_property_add_text(xdomain_property_dir, "vendorid", "Intel Corp.");
  1333. tb_property_add_immediate(xdomain_property_dir, "deviceid", 0x1);
  1334. tb_property_add_text(xdomain_property_dir, "deviceid",
  1335. utsname()->nodename);
  1336. tb_property_add_immediate(xdomain_property_dir, "devicerv", 0x80000100);
  1337. ret = rebuild_property_block();
  1338. if (ret) {
  1339. tb_property_free_dir(xdomain_property_dir);
  1340. xdomain_property_dir = NULL;
  1341. }
  1342. return ret;
  1343. }
  1344. void tb_xdomain_exit(void)
  1345. {
  1346. kfree(xdomain_property_block);
  1347. tb_property_free_dir(xdomain_property_dir);
  1348. }