xdomain.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Thunderbolt XDomain discovery protocol support
  4. *
  5. * Copyright (C) 2017, Intel Corporation
  6. * Authors: Michael Jamet <[email protected]>
  7. * Mika Westerberg <[email protected]>
  8. */
  9. #include <linux/device.h>
  10. #include <linux/delay.h>
  11. #include <linux/kmod.h>
  12. #include <linux/module.h>
  13. #include <linux/pm_runtime.h>
  14. #include <linux/prandom.h>
  15. #include <linux/utsname.h>
  16. #include <linux/uuid.h>
  17. #include <linux/workqueue.h>
  18. #include "tb.h"
  19. #define XDOMAIN_SHORT_TIMEOUT 100 /* ms */
  20. #define XDOMAIN_DEFAULT_TIMEOUT 1000 /* ms */
  21. #define XDOMAIN_BONDING_TIMEOUT 10000 /* ms */
  22. #define XDOMAIN_RETRIES 10
  23. #define XDOMAIN_DEFAULT_MAX_HOPID 15
  24. enum {
  25. XDOMAIN_STATE_INIT,
  26. XDOMAIN_STATE_UUID,
  27. XDOMAIN_STATE_LINK_STATUS,
  28. XDOMAIN_STATE_LINK_STATE_CHANGE,
  29. XDOMAIN_STATE_LINK_STATUS2,
  30. XDOMAIN_STATE_BONDING_UUID_LOW,
  31. XDOMAIN_STATE_BONDING_UUID_HIGH,
  32. XDOMAIN_STATE_PROPERTIES,
  33. XDOMAIN_STATE_ENUMERATED,
  34. XDOMAIN_STATE_ERROR,
  35. };
  36. static const char * const state_names[] = {
  37. [XDOMAIN_STATE_INIT] = "INIT",
  38. [XDOMAIN_STATE_UUID] = "UUID",
  39. [XDOMAIN_STATE_LINK_STATUS] = "LINK_STATUS",
  40. [XDOMAIN_STATE_LINK_STATE_CHANGE] = "LINK_STATE_CHANGE",
  41. [XDOMAIN_STATE_LINK_STATUS2] = "LINK_STATUS2",
  42. [XDOMAIN_STATE_BONDING_UUID_LOW] = "BONDING_UUID_LOW",
  43. [XDOMAIN_STATE_BONDING_UUID_HIGH] = "BONDING_UUID_HIGH",
  44. [XDOMAIN_STATE_PROPERTIES] = "PROPERTIES",
  45. [XDOMAIN_STATE_ENUMERATED] = "ENUMERATED",
  46. [XDOMAIN_STATE_ERROR] = "ERROR",
  47. };
  48. struct xdomain_request_work {
  49. struct work_struct work;
  50. struct tb_xdp_header *pkg;
  51. struct tb *tb;
  52. };
  53. static bool tb_xdomain_enabled = true;
  54. module_param_named(xdomain, tb_xdomain_enabled, bool, 0444);
  55. MODULE_PARM_DESC(xdomain, "allow XDomain protocol (default: true)");
  56. /*
  57. * Serializes access to the properties and protocol handlers below. If
  58. * you need to take both this lock and the struct tb_xdomain lock, take
  59. * this one first.
  60. */
  61. static DEFINE_MUTEX(xdomain_lock);
  62. /* Properties exposed to the remote domains */
  63. static struct tb_property_dir *xdomain_property_dir;
  64. static u32 xdomain_property_block_gen;
  65. /* Additional protocol handlers */
  66. static LIST_HEAD(protocol_handlers);
  67. /* UUID for XDomain discovery protocol: b638d70e-42ff-40bb-97c2-90e2c0b2ff07 */
  68. static const uuid_t tb_xdp_uuid =
  69. UUID_INIT(0xb638d70e, 0x42ff, 0x40bb,
  70. 0x97, 0xc2, 0x90, 0xe2, 0xc0, 0xb2, 0xff, 0x07);
  71. bool tb_is_xdomain_enabled(void)
  72. {
  73. return tb_xdomain_enabled && tb_acpi_is_xdomain_allowed();
  74. }
  75. static bool tb_xdomain_match(const struct tb_cfg_request *req,
  76. const struct ctl_pkg *pkg)
  77. {
  78. switch (pkg->frame.eof) {
  79. case TB_CFG_PKG_ERROR:
  80. return true;
  81. case TB_CFG_PKG_XDOMAIN_RESP: {
  82. const struct tb_xdp_header *res_hdr = pkg->buffer;
  83. const struct tb_xdp_header *req_hdr = req->request;
  84. if (pkg->frame.size < req->response_size / 4)
  85. return false;
  86. /* Make sure route matches */
  87. if ((res_hdr->xd_hdr.route_hi & ~BIT(31)) !=
  88. req_hdr->xd_hdr.route_hi)
  89. return false;
  90. if ((res_hdr->xd_hdr.route_lo) != req_hdr->xd_hdr.route_lo)
  91. return false;
  92. /* Check that the XDomain protocol matches */
  93. if (!uuid_equal(&res_hdr->uuid, &req_hdr->uuid))
  94. return false;
  95. return true;
  96. }
  97. default:
  98. return false;
  99. }
  100. }
  101. static bool tb_xdomain_copy(struct tb_cfg_request *req,
  102. const struct ctl_pkg *pkg)
  103. {
  104. memcpy(req->response, pkg->buffer, req->response_size);
  105. req->result.err = 0;
  106. return true;
  107. }
  108. static void response_ready(void *data)
  109. {
  110. tb_cfg_request_put(data);
  111. }
  112. static int __tb_xdomain_response(struct tb_ctl *ctl, const void *response,
  113. size_t size, enum tb_cfg_pkg_type type)
  114. {
  115. struct tb_cfg_request *req;
  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 = response;
  122. req->request_size = size;
  123. req->request_type = type;
  124. return tb_cfg_request(ctl, req, response_ready, req);
  125. }
  126. /**
  127. * tb_xdomain_response() - Send a XDomain response message
  128. * @xd: XDomain to send the message
  129. * @response: Response to send
  130. * @size: Size of the response
  131. * @type: PDF type of the response
  132. *
  133. * This can be used to send a XDomain response message to the other
  134. * domain. No response for the message is expected.
  135. *
  136. * Return: %0 in case of success and negative errno in case of failure
  137. */
  138. int tb_xdomain_response(struct tb_xdomain *xd, const void *response,
  139. size_t size, enum tb_cfg_pkg_type type)
  140. {
  141. return __tb_xdomain_response(xd->tb->ctl, response, size, type);
  142. }
  143. EXPORT_SYMBOL_GPL(tb_xdomain_response);
  144. static int __tb_xdomain_request(struct tb_ctl *ctl, const void *request,
  145. size_t request_size, enum tb_cfg_pkg_type request_type, void *response,
  146. size_t response_size, enum tb_cfg_pkg_type response_type,
  147. unsigned int timeout_msec)
  148. {
  149. struct tb_cfg_request *req;
  150. struct tb_cfg_result res;
  151. req = tb_cfg_request_alloc();
  152. if (!req)
  153. return -ENOMEM;
  154. req->match = tb_xdomain_match;
  155. req->copy = tb_xdomain_copy;
  156. req->request = request;
  157. req->request_size = request_size;
  158. req->request_type = request_type;
  159. req->response = response;
  160. req->response_size = response_size;
  161. req->response_type = response_type;
  162. res = tb_cfg_request_sync(ctl, req, timeout_msec);
  163. tb_cfg_request_put(req);
  164. return res.err == 1 ? -EIO : res.err;
  165. }
  166. /**
  167. * tb_xdomain_request() - Send a XDomain request
  168. * @xd: XDomain to send the request
  169. * @request: Request to send
  170. * @request_size: Size of the request in bytes
  171. * @request_type: PDF type of the request
  172. * @response: Response is copied here
  173. * @response_size: Expected size of the response in bytes
  174. * @response_type: Expected PDF type of the response
  175. * @timeout_msec: Timeout in milliseconds to wait for the response
  176. *
  177. * This function can be used to send XDomain control channel messages to
  178. * the other domain. The function waits until the response is received
  179. * or when timeout triggers. Whichever comes first.
  180. *
  181. * Return: %0 in case of success and negative errno in case of failure
  182. */
  183. int tb_xdomain_request(struct tb_xdomain *xd, const void *request,
  184. size_t request_size, enum tb_cfg_pkg_type request_type,
  185. void *response, size_t response_size,
  186. enum tb_cfg_pkg_type response_type, unsigned int timeout_msec)
  187. {
  188. return __tb_xdomain_request(xd->tb->ctl, request, request_size,
  189. request_type, response, response_size,
  190. response_type, timeout_msec);
  191. }
  192. EXPORT_SYMBOL_GPL(tb_xdomain_request);
  193. static inline void tb_xdp_fill_header(struct tb_xdp_header *hdr, u64 route,
  194. u8 sequence, enum tb_xdp_type type, size_t size)
  195. {
  196. u32 length_sn;
  197. length_sn = (size - sizeof(hdr->xd_hdr)) / 4;
  198. length_sn |= (sequence << TB_XDOMAIN_SN_SHIFT) & TB_XDOMAIN_SN_MASK;
  199. hdr->xd_hdr.route_hi = upper_32_bits(route);
  200. hdr->xd_hdr.route_lo = lower_32_bits(route);
  201. hdr->xd_hdr.length_sn = length_sn;
  202. hdr->type = type;
  203. memcpy(&hdr->uuid, &tb_xdp_uuid, sizeof(tb_xdp_uuid));
  204. }
  205. static int tb_xdp_handle_error(const struct tb_xdp_error_response *res)
  206. {
  207. if (res->hdr.type != ERROR_RESPONSE)
  208. return 0;
  209. switch (res->error) {
  210. case ERROR_UNKNOWN_PACKET:
  211. case ERROR_UNKNOWN_DOMAIN:
  212. return -EIO;
  213. case ERROR_NOT_SUPPORTED:
  214. return -ENOTSUPP;
  215. case ERROR_NOT_READY:
  216. return -EAGAIN;
  217. default:
  218. break;
  219. }
  220. return 0;
  221. }
  222. static int tb_xdp_uuid_request(struct tb_ctl *ctl, u64 route, int retry,
  223. uuid_t *uuid, u64 *remote_route)
  224. {
  225. struct tb_xdp_uuid_response res;
  226. struct tb_xdp_uuid req;
  227. int ret;
  228. memset(&req, 0, sizeof(req));
  229. tb_xdp_fill_header(&req.hdr, route, retry % 4, UUID_REQUEST,
  230. sizeof(req));
  231. memset(&res, 0, sizeof(res));
  232. ret = __tb_xdomain_request(ctl, &req, sizeof(req),
  233. TB_CFG_PKG_XDOMAIN_REQ, &res, sizeof(res),
  234. TB_CFG_PKG_XDOMAIN_RESP,
  235. XDOMAIN_DEFAULT_TIMEOUT);
  236. if (ret)
  237. return ret;
  238. ret = tb_xdp_handle_error(&res.err);
  239. if (ret)
  240. return ret;
  241. uuid_copy(uuid, &res.src_uuid);
  242. *remote_route = (u64)res.src_route_hi << 32 | res.src_route_lo;
  243. return 0;
  244. }
  245. static int tb_xdp_uuid_response(struct tb_ctl *ctl, u64 route, u8 sequence,
  246. const uuid_t *uuid)
  247. {
  248. struct tb_xdp_uuid_response res;
  249. memset(&res, 0, sizeof(res));
  250. tb_xdp_fill_header(&res.hdr, route, sequence, UUID_RESPONSE,
  251. sizeof(res));
  252. uuid_copy(&res.src_uuid, uuid);
  253. res.src_route_hi = upper_32_bits(route);
  254. res.src_route_lo = lower_32_bits(route);
  255. return __tb_xdomain_response(ctl, &res, sizeof(res),
  256. TB_CFG_PKG_XDOMAIN_RESP);
  257. }
  258. static int tb_xdp_error_response(struct tb_ctl *ctl, u64 route, u8 sequence,
  259. enum tb_xdp_error error)
  260. {
  261. struct tb_xdp_error_response res;
  262. memset(&res, 0, sizeof(res));
  263. tb_xdp_fill_header(&res.hdr, route, sequence, ERROR_RESPONSE,
  264. sizeof(res));
  265. res.error = error;
  266. return __tb_xdomain_response(ctl, &res, sizeof(res),
  267. TB_CFG_PKG_XDOMAIN_RESP);
  268. }
  269. static int tb_xdp_properties_request(struct tb_ctl *ctl, u64 route,
  270. const uuid_t *src_uuid, const uuid_t *dst_uuid, int retry,
  271. u32 **block, u32 *generation)
  272. {
  273. struct tb_xdp_properties_response *res;
  274. struct tb_xdp_properties req;
  275. u16 data_len, len;
  276. size_t total_size;
  277. u32 *data = NULL;
  278. int ret;
  279. total_size = sizeof(*res) + TB_XDP_PROPERTIES_MAX_DATA_LENGTH * 4;
  280. res = kzalloc(total_size, GFP_KERNEL);
  281. if (!res)
  282. return -ENOMEM;
  283. memset(&req, 0, sizeof(req));
  284. tb_xdp_fill_header(&req.hdr, route, retry % 4, PROPERTIES_REQUEST,
  285. sizeof(req));
  286. memcpy(&req.src_uuid, src_uuid, sizeof(*src_uuid));
  287. memcpy(&req.dst_uuid, dst_uuid, sizeof(*dst_uuid));
  288. len = 0;
  289. data_len = 0;
  290. do {
  291. ret = __tb_xdomain_request(ctl, &req, sizeof(req),
  292. TB_CFG_PKG_XDOMAIN_REQ, res,
  293. total_size, TB_CFG_PKG_XDOMAIN_RESP,
  294. XDOMAIN_DEFAULT_TIMEOUT);
  295. if (ret)
  296. goto err;
  297. ret = tb_xdp_handle_error(&res->err);
  298. if (ret)
  299. goto err;
  300. /*
  301. * Package length includes the whole payload without the
  302. * XDomain header. Validate first that the package is at
  303. * least size of the response structure.
  304. */
  305. len = res->hdr.xd_hdr.length_sn & TB_XDOMAIN_LENGTH_MASK;
  306. if (len < sizeof(*res) / 4) {
  307. ret = -EINVAL;
  308. goto err;
  309. }
  310. len += sizeof(res->hdr.xd_hdr) / 4;
  311. len -= sizeof(*res) / 4;
  312. if (res->offset != req.offset) {
  313. ret = -EINVAL;
  314. goto err;
  315. }
  316. /*
  317. * First time allocate block that has enough space for
  318. * the whole properties block.
  319. */
  320. if (!data) {
  321. data_len = res->data_length;
  322. if (data_len > TB_XDP_PROPERTIES_MAX_LENGTH) {
  323. ret = -E2BIG;
  324. goto err;
  325. }
  326. data = kcalloc(data_len, sizeof(u32), GFP_KERNEL);
  327. if (!data) {
  328. ret = -ENOMEM;
  329. goto err;
  330. }
  331. }
  332. memcpy(data + req.offset, res->data, len * 4);
  333. req.offset += len;
  334. } while (!data_len || req.offset < data_len);
  335. *block = data;
  336. *generation = res->generation;
  337. kfree(res);
  338. return data_len;
  339. err:
  340. kfree(data);
  341. kfree(res);
  342. return ret;
  343. }
  344. static int tb_xdp_properties_response(struct tb *tb, struct tb_ctl *ctl,
  345. struct tb_xdomain *xd, u8 sequence, const struct tb_xdp_properties *req)
  346. {
  347. struct tb_xdp_properties_response *res;
  348. size_t total_size;
  349. u16 len;
  350. int ret;
  351. /*
  352. * Currently we expect all requests to be directed to us. The
  353. * protocol supports forwarding, though which we might add
  354. * support later on.
  355. */
  356. if (!uuid_equal(xd->local_uuid, &req->dst_uuid)) {
  357. tb_xdp_error_response(ctl, xd->route, sequence,
  358. ERROR_UNKNOWN_DOMAIN);
  359. return 0;
  360. }
  361. mutex_lock(&xd->lock);
  362. if (req->offset >= xd->local_property_block_len) {
  363. mutex_unlock(&xd->lock);
  364. return -EINVAL;
  365. }
  366. len = xd->local_property_block_len - req->offset;
  367. len = min_t(u16, len, TB_XDP_PROPERTIES_MAX_DATA_LENGTH);
  368. total_size = sizeof(*res) + len * 4;
  369. res = kzalloc(total_size, GFP_KERNEL);
  370. if (!res) {
  371. mutex_unlock(&xd->lock);
  372. return -ENOMEM;
  373. }
  374. tb_xdp_fill_header(&res->hdr, xd->route, sequence, PROPERTIES_RESPONSE,
  375. total_size);
  376. res->generation = xd->local_property_block_gen;
  377. res->data_length = xd->local_property_block_len;
  378. res->offset = req->offset;
  379. uuid_copy(&res->src_uuid, xd->local_uuid);
  380. uuid_copy(&res->dst_uuid, &req->src_uuid);
  381. memcpy(res->data, &xd->local_property_block[req->offset], len * 4);
  382. mutex_unlock(&xd->lock);
  383. ret = __tb_xdomain_response(ctl, res, total_size,
  384. TB_CFG_PKG_XDOMAIN_RESP);
  385. kfree(res);
  386. return ret;
  387. }
  388. static int tb_xdp_properties_changed_request(struct tb_ctl *ctl, u64 route,
  389. int retry, const uuid_t *uuid)
  390. {
  391. struct tb_xdp_properties_changed_response res;
  392. struct tb_xdp_properties_changed req;
  393. int ret;
  394. memset(&req, 0, sizeof(req));
  395. tb_xdp_fill_header(&req.hdr, route, retry % 4,
  396. PROPERTIES_CHANGED_REQUEST, sizeof(req));
  397. uuid_copy(&req.src_uuid, uuid);
  398. memset(&res, 0, sizeof(res));
  399. ret = __tb_xdomain_request(ctl, &req, sizeof(req),
  400. TB_CFG_PKG_XDOMAIN_REQ, &res, sizeof(res),
  401. TB_CFG_PKG_XDOMAIN_RESP,
  402. XDOMAIN_DEFAULT_TIMEOUT);
  403. if (ret)
  404. return ret;
  405. return tb_xdp_handle_error(&res.err);
  406. }
  407. static int
  408. tb_xdp_properties_changed_response(struct tb_ctl *ctl, u64 route, u8 sequence)
  409. {
  410. struct tb_xdp_properties_changed_response res;
  411. memset(&res, 0, sizeof(res));
  412. tb_xdp_fill_header(&res.hdr, route, sequence,
  413. PROPERTIES_CHANGED_RESPONSE, sizeof(res));
  414. return __tb_xdomain_response(ctl, &res, sizeof(res),
  415. TB_CFG_PKG_XDOMAIN_RESP);
  416. }
  417. static int tb_xdp_link_state_status_request(struct tb_ctl *ctl, u64 route,
  418. u8 sequence, u8 *slw, u8 *tlw,
  419. u8 *sls, u8 *tls)
  420. {
  421. struct tb_xdp_link_state_status_response res;
  422. struct tb_xdp_link_state_status req;
  423. int ret;
  424. memset(&req, 0, sizeof(req));
  425. tb_xdp_fill_header(&req.hdr, route, sequence, LINK_STATE_STATUS_REQUEST,
  426. sizeof(req));
  427. memset(&res, 0, sizeof(res));
  428. ret = __tb_xdomain_request(ctl, &req, sizeof(req), TB_CFG_PKG_XDOMAIN_REQ,
  429. &res, sizeof(res), TB_CFG_PKG_XDOMAIN_RESP,
  430. XDOMAIN_DEFAULT_TIMEOUT);
  431. if (ret)
  432. return ret;
  433. ret = tb_xdp_handle_error(&res.err);
  434. if (ret)
  435. return ret;
  436. if (res.status != 0)
  437. return -EREMOTEIO;
  438. *slw = res.slw;
  439. *tlw = res.tlw;
  440. *sls = res.sls;
  441. *tls = res.tls;
  442. return 0;
  443. }
  444. static int tb_xdp_link_state_status_response(struct tb *tb, struct tb_ctl *ctl,
  445. struct tb_xdomain *xd, u8 sequence)
  446. {
  447. struct tb_switch *sw = tb_to_switch(xd->dev.parent);
  448. struct tb_xdp_link_state_status_response res;
  449. struct tb_port *port = tb_port_at(xd->route, sw);
  450. u32 val[2];
  451. int ret;
  452. memset(&res, 0, sizeof(res));
  453. tb_xdp_fill_header(&res.hdr, xd->route, sequence,
  454. LINK_STATE_STATUS_RESPONSE, sizeof(res));
  455. ret = tb_port_read(port, val, TB_CFG_PORT,
  456. port->cap_phy + LANE_ADP_CS_0, ARRAY_SIZE(val));
  457. if (ret)
  458. return ret;
  459. res.slw = (val[0] & LANE_ADP_CS_0_SUPPORTED_WIDTH_MASK) >>
  460. LANE_ADP_CS_0_SUPPORTED_WIDTH_SHIFT;
  461. res.sls = (val[0] & LANE_ADP_CS_0_SUPPORTED_SPEED_MASK) >>
  462. LANE_ADP_CS_0_SUPPORTED_SPEED_SHIFT;
  463. res.tls = val[1] & LANE_ADP_CS_1_TARGET_SPEED_MASK;
  464. res.tlw = (val[1] & LANE_ADP_CS_1_TARGET_WIDTH_MASK) >>
  465. LANE_ADP_CS_1_TARGET_WIDTH_SHIFT;
  466. return __tb_xdomain_response(ctl, &res, sizeof(res),
  467. TB_CFG_PKG_XDOMAIN_RESP);
  468. }
  469. static int tb_xdp_link_state_change_request(struct tb_ctl *ctl, u64 route,
  470. u8 sequence, u8 tlw, u8 tls)
  471. {
  472. struct tb_xdp_link_state_change_response res;
  473. struct tb_xdp_link_state_change req;
  474. int ret;
  475. memset(&req, 0, sizeof(req));
  476. tb_xdp_fill_header(&req.hdr, route, sequence, LINK_STATE_CHANGE_REQUEST,
  477. sizeof(req));
  478. req.tlw = tlw;
  479. req.tls = tls;
  480. memset(&res, 0, sizeof(res));
  481. ret = __tb_xdomain_request(ctl, &req, sizeof(req), TB_CFG_PKG_XDOMAIN_REQ,
  482. &res, sizeof(res), TB_CFG_PKG_XDOMAIN_RESP,
  483. XDOMAIN_DEFAULT_TIMEOUT);
  484. if (ret)
  485. return ret;
  486. ret = tb_xdp_handle_error(&res.err);
  487. if (ret)
  488. return ret;
  489. return res.status != 0 ? -EREMOTEIO : 0;
  490. }
  491. static int tb_xdp_link_state_change_response(struct tb_ctl *ctl, u64 route,
  492. u8 sequence, u32 status)
  493. {
  494. struct tb_xdp_link_state_change_response res;
  495. memset(&res, 0, sizeof(res));
  496. tb_xdp_fill_header(&res.hdr, route, sequence, LINK_STATE_CHANGE_RESPONSE,
  497. sizeof(res));
  498. res.status = status;
  499. return __tb_xdomain_response(ctl, &res, sizeof(res),
  500. TB_CFG_PKG_XDOMAIN_RESP);
  501. }
  502. /**
  503. * tb_register_protocol_handler() - Register protocol handler
  504. * @handler: Handler to register
  505. *
  506. * This allows XDomain service drivers to hook into incoming XDomain
  507. * messages. After this function is called the service driver needs to
  508. * be able to handle calls to callback whenever a package with the
  509. * registered protocol is received.
  510. */
  511. int tb_register_protocol_handler(struct tb_protocol_handler *handler)
  512. {
  513. if (!handler->uuid || !handler->callback)
  514. return -EINVAL;
  515. if (uuid_equal(handler->uuid, &tb_xdp_uuid))
  516. return -EINVAL;
  517. mutex_lock(&xdomain_lock);
  518. list_add_tail(&handler->list, &protocol_handlers);
  519. mutex_unlock(&xdomain_lock);
  520. return 0;
  521. }
  522. EXPORT_SYMBOL_GPL(tb_register_protocol_handler);
  523. /**
  524. * tb_unregister_protocol_handler() - Unregister protocol handler
  525. * @handler: Handler to unregister
  526. *
  527. * Removes the previously registered protocol handler.
  528. */
  529. void tb_unregister_protocol_handler(struct tb_protocol_handler *handler)
  530. {
  531. mutex_lock(&xdomain_lock);
  532. list_del_init(&handler->list);
  533. mutex_unlock(&xdomain_lock);
  534. }
  535. EXPORT_SYMBOL_GPL(tb_unregister_protocol_handler);
  536. static void update_property_block(struct tb_xdomain *xd)
  537. {
  538. mutex_lock(&xdomain_lock);
  539. mutex_lock(&xd->lock);
  540. /*
  541. * If the local property block is not up-to-date, rebuild it now
  542. * based on the global property template.
  543. */
  544. if (!xd->local_property_block ||
  545. xd->local_property_block_gen < xdomain_property_block_gen) {
  546. struct tb_property_dir *dir;
  547. int ret, block_len;
  548. u32 *block;
  549. dir = tb_property_copy_dir(xdomain_property_dir);
  550. if (!dir) {
  551. dev_warn(&xd->dev, "failed to copy properties\n");
  552. goto out_unlock;
  553. }
  554. /* Fill in non-static properties now */
  555. tb_property_add_text(dir, "deviceid", utsname()->nodename);
  556. tb_property_add_immediate(dir, "maxhopid", xd->local_max_hopid);
  557. ret = tb_property_format_dir(dir, NULL, 0);
  558. if (ret < 0) {
  559. dev_warn(&xd->dev, "local property block creation failed\n");
  560. tb_property_free_dir(dir);
  561. goto out_unlock;
  562. }
  563. block_len = ret;
  564. block = kcalloc(block_len, sizeof(*block), GFP_KERNEL);
  565. if (!block) {
  566. tb_property_free_dir(dir);
  567. goto out_unlock;
  568. }
  569. ret = tb_property_format_dir(dir, block, block_len);
  570. if (ret) {
  571. dev_warn(&xd->dev, "property block generation failed\n");
  572. tb_property_free_dir(dir);
  573. kfree(block);
  574. goto out_unlock;
  575. }
  576. tb_property_free_dir(dir);
  577. /* Release the previous block */
  578. kfree(xd->local_property_block);
  579. /* Assign new one */
  580. xd->local_property_block = block;
  581. xd->local_property_block_len = block_len;
  582. xd->local_property_block_gen = xdomain_property_block_gen;
  583. }
  584. out_unlock:
  585. mutex_unlock(&xd->lock);
  586. mutex_unlock(&xdomain_lock);
  587. }
  588. static void start_handshake(struct tb_xdomain *xd)
  589. {
  590. xd->state = XDOMAIN_STATE_INIT;
  591. queue_delayed_work(xd->tb->wq, &xd->state_work,
  592. msecs_to_jiffies(XDOMAIN_SHORT_TIMEOUT));
  593. }
  594. /* Can be called from state_work */
  595. static void __stop_handshake(struct tb_xdomain *xd)
  596. {
  597. cancel_delayed_work_sync(&xd->properties_changed_work);
  598. xd->properties_changed_retries = 0;
  599. xd->state_retries = 0;
  600. }
  601. static void stop_handshake(struct tb_xdomain *xd)
  602. {
  603. cancel_delayed_work_sync(&xd->state_work);
  604. __stop_handshake(xd);
  605. }
  606. static void tb_xdp_handle_request(struct work_struct *work)
  607. {
  608. struct xdomain_request_work *xw = container_of(work, typeof(*xw), work);
  609. const struct tb_xdp_header *pkg = xw->pkg;
  610. const struct tb_xdomain_header *xhdr = &pkg->xd_hdr;
  611. struct tb *tb = xw->tb;
  612. struct tb_ctl *ctl = tb->ctl;
  613. struct tb_xdomain *xd;
  614. const uuid_t *uuid;
  615. int ret = 0;
  616. u32 sequence;
  617. u64 route;
  618. route = ((u64)xhdr->route_hi << 32 | xhdr->route_lo) & ~BIT_ULL(63);
  619. sequence = xhdr->length_sn & TB_XDOMAIN_SN_MASK;
  620. sequence >>= TB_XDOMAIN_SN_SHIFT;
  621. mutex_lock(&tb->lock);
  622. if (tb->root_switch)
  623. uuid = tb->root_switch->uuid;
  624. else
  625. uuid = NULL;
  626. mutex_unlock(&tb->lock);
  627. if (!uuid) {
  628. tb_xdp_error_response(ctl, route, sequence, ERROR_NOT_READY);
  629. goto out;
  630. }
  631. xd = tb_xdomain_find_by_route_locked(tb, route);
  632. if (xd)
  633. update_property_block(xd);
  634. switch (pkg->type) {
  635. case PROPERTIES_REQUEST:
  636. tb_dbg(tb, "%llx: received XDomain properties request\n", route);
  637. if (xd) {
  638. ret = tb_xdp_properties_response(tb, ctl, xd, sequence,
  639. (const struct tb_xdp_properties *)pkg);
  640. }
  641. break;
  642. case PROPERTIES_CHANGED_REQUEST:
  643. tb_dbg(tb, "%llx: received XDomain properties changed request\n",
  644. route);
  645. ret = tb_xdp_properties_changed_response(ctl, route, sequence);
  646. /*
  647. * Since the properties have been changed, let's update
  648. * the xdomain related to this connection as well in
  649. * case there is a change in services it offers.
  650. */
  651. if (xd && device_is_registered(&xd->dev))
  652. queue_delayed_work(tb->wq, &xd->state_work,
  653. msecs_to_jiffies(XDOMAIN_SHORT_TIMEOUT));
  654. break;
  655. case UUID_REQUEST_OLD:
  656. case UUID_REQUEST:
  657. tb_dbg(tb, "%llx: received XDomain UUID request\n", route);
  658. ret = tb_xdp_uuid_response(ctl, route, sequence, uuid);
  659. /*
  660. * If we've stopped the discovery with an error such as
  661. * timing out, we will restart the handshake now that we
  662. * received UUID request from the remote host.
  663. */
  664. if (!ret && xd && xd->state == XDOMAIN_STATE_ERROR) {
  665. dev_dbg(&xd->dev, "restarting handshake\n");
  666. start_handshake(xd);
  667. }
  668. break;
  669. case LINK_STATE_STATUS_REQUEST:
  670. tb_dbg(tb, "%llx: received XDomain link state status request\n",
  671. route);
  672. if (xd) {
  673. ret = tb_xdp_link_state_status_response(tb, ctl, xd,
  674. sequence);
  675. } else {
  676. tb_xdp_error_response(ctl, route, sequence,
  677. ERROR_NOT_READY);
  678. }
  679. break;
  680. case LINK_STATE_CHANGE_REQUEST:
  681. tb_dbg(tb, "%llx: received XDomain link state change request\n",
  682. route);
  683. if (xd && xd->state == XDOMAIN_STATE_BONDING_UUID_HIGH) {
  684. const struct tb_xdp_link_state_change *lsc =
  685. (const struct tb_xdp_link_state_change *)pkg;
  686. ret = tb_xdp_link_state_change_response(ctl, route,
  687. sequence, 0);
  688. xd->target_link_width = lsc->tlw;
  689. queue_delayed_work(tb->wq, &xd->state_work,
  690. msecs_to_jiffies(XDOMAIN_SHORT_TIMEOUT));
  691. } else {
  692. tb_xdp_error_response(ctl, route, sequence,
  693. ERROR_NOT_READY);
  694. }
  695. break;
  696. default:
  697. tb_dbg(tb, "%llx: unknown XDomain request %#x\n", route, pkg->type);
  698. tb_xdp_error_response(ctl, route, sequence,
  699. ERROR_NOT_SUPPORTED);
  700. break;
  701. }
  702. tb_xdomain_put(xd);
  703. if (ret) {
  704. tb_warn(tb, "failed to send XDomain response for %#x\n",
  705. pkg->type);
  706. }
  707. out:
  708. kfree(xw->pkg);
  709. kfree(xw);
  710. tb_domain_put(tb);
  711. }
  712. static bool
  713. tb_xdp_schedule_request(struct tb *tb, const struct tb_xdp_header *hdr,
  714. size_t size)
  715. {
  716. struct xdomain_request_work *xw;
  717. xw = kmalloc(sizeof(*xw), GFP_KERNEL);
  718. if (!xw)
  719. return false;
  720. INIT_WORK(&xw->work, tb_xdp_handle_request);
  721. xw->pkg = kmemdup(hdr, size, GFP_KERNEL);
  722. if (!xw->pkg) {
  723. kfree(xw);
  724. return false;
  725. }
  726. xw->tb = tb_domain_get(tb);
  727. schedule_work(&xw->work);
  728. return true;
  729. }
  730. /**
  731. * tb_register_service_driver() - Register XDomain service driver
  732. * @drv: Driver to register
  733. *
  734. * Registers new service driver from @drv to the bus.
  735. */
  736. int tb_register_service_driver(struct tb_service_driver *drv)
  737. {
  738. drv->driver.bus = &tb_bus_type;
  739. return driver_register(&drv->driver);
  740. }
  741. EXPORT_SYMBOL_GPL(tb_register_service_driver);
  742. /**
  743. * tb_unregister_service_driver() - Unregister XDomain service driver
  744. * @drv: Driver to unregister
  745. *
  746. * Unregisters XDomain service driver from the bus.
  747. */
  748. void tb_unregister_service_driver(struct tb_service_driver *drv)
  749. {
  750. driver_unregister(&drv->driver);
  751. }
  752. EXPORT_SYMBOL_GPL(tb_unregister_service_driver);
  753. static ssize_t key_show(struct device *dev, struct device_attribute *attr,
  754. char *buf)
  755. {
  756. struct tb_service *svc = container_of(dev, struct tb_service, dev);
  757. /*
  758. * It should be null terminated but anything else is pretty much
  759. * allowed.
  760. */
  761. return sysfs_emit(buf, "%*pE\n", (int)strlen(svc->key), svc->key);
  762. }
  763. static DEVICE_ATTR_RO(key);
  764. static int get_modalias(struct tb_service *svc, char *buf, size_t size)
  765. {
  766. return snprintf(buf, size, "tbsvc:k%sp%08Xv%08Xr%08X", svc->key,
  767. svc->prtcid, svc->prtcvers, svc->prtcrevs);
  768. }
  769. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
  770. char *buf)
  771. {
  772. struct tb_service *svc = container_of(dev, struct tb_service, dev);
  773. /* Full buffer size except new line and null termination */
  774. get_modalias(svc, buf, PAGE_SIZE - 2);
  775. return strlen(strcat(buf, "\n"));
  776. }
  777. static DEVICE_ATTR_RO(modalias);
  778. static ssize_t prtcid_show(struct device *dev, struct device_attribute *attr,
  779. char *buf)
  780. {
  781. struct tb_service *svc = container_of(dev, struct tb_service, dev);
  782. return sysfs_emit(buf, "%u\n", svc->prtcid);
  783. }
  784. static DEVICE_ATTR_RO(prtcid);
  785. static ssize_t prtcvers_show(struct device *dev, struct device_attribute *attr,
  786. char *buf)
  787. {
  788. struct tb_service *svc = container_of(dev, struct tb_service, dev);
  789. return sysfs_emit(buf, "%u\n", svc->prtcvers);
  790. }
  791. static DEVICE_ATTR_RO(prtcvers);
  792. static ssize_t prtcrevs_show(struct device *dev, struct device_attribute *attr,
  793. char *buf)
  794. {
  795. struct tb_service *svc = container_of(dev, struct tb_service, dev);
  796. return sysfs_emit(buf, "%u\n", svc->prtcrevs);
  797. }
  798. static DEVICE_ATTR_RO(prtcrevs);
  799. static ssize_t prtcstns_show(struct device *dev, struct device_attribute *attr,
  800. char *buf)
  801. {
  802. struct tb_service *svc = container_of(dev, struct tb_service, dev);
  803. return sysfs_emit(buf, "0x%08x\n", svc->prtcstns);
  804. }
  805. static DEVICE_ATTR_RO(prtcstns);
  806. static struct attribute *tb_service_attrs[] = {
  807. &dev_attr_key.attr,
  808. &dev_attr_modalias.attr,
  809. &dev_attr_prtcid.attr,
  810. &dev_attr_prtcvers.attr,
  811. &dev_attr_prtcrevs.attr,
  812. &dev_attr_prtcstns.attr,
  813. NULL,
  814. };
  815. static const struct attribute_group tb_service_attr_group = {
  816. .attrs = tb_service_attrs,
  817. };
  818. static const struct attribute_group *tb_service_attr_groups[] = {
  819. &tb_service_attr_group,
  820. NULL,
  821. };
  822. static int tb_service_uevent(struct device *dev, struct kobj_uevent_env *env)
  823. {
  824. struct tb_service *svc = container_of(dev, struct tb_service, dev);
  825. char modalias[64];
  826. get_modalias(svc, modalias, sizeof(modalias));
  827. return add_uevent_var(env, "MODALIAS=%s", modalias);
  828. }
  829. static void tb_service_release(struct device *dev)
  830. {
  831. struct tb_service *svc = container_of(dev, struct tb_service, dev);
  832. struct tb_xdomain *xd = tb_service_parent(svc);
  833. tb_service_debugfs_remove(svc);
  834. ida_simple_remove(&xd->service_ids, svc->id);
  835. kfree(svc->key);
  836. kfree(svc);
  837. }
  838. struct device_type tb_service_type = {
  839. .name = "thunderbolt_service",
  840. .groups = tb_service_attr_groups,
  841. .uevent = tb_service_uevent,
  842. .release = tb_service_release,
  843. };
  844. EXPORT_SYMBOL_GPL(tb_service_type);
  845. static int remove_missing_service(struct device *dev, void *data)
  846. {
  847. struct tb_xdomain *xd = data;
  848. struct tb_service *svc;
  849. svc = tb_to_service(dev);
  850. if (!svc)
  851. return 0;
  852. if (!tb_property_find(xd->remote_properties, svc->key,
  853. TB_PROPERTY_TYPE_DIRECTORY))
  854. device_unregister(dev);
  855. return 0;
  856. }
  857. static int find_service(struct device *dev, void *data)
  858. {
  859. const struct tb_property *p = data;
  860. struct tb_service *svc;
  861. svc = tb_to_service(dev);
  862. if (!svc)
  863. return 0;
  864. return !strcmp(svc->key, p->key);
  865. }
  866. static int populate_service(struct tb_service *svc,
  867. struct tb_property *property)
  868. {
  869. struct tb_property_dir *dir = property->value.dir;
  870. struct tb_property *p;
  871. /* Fill in standard properties */
  872. p = tb_property_find(dir, "prtcid", TB_PROPERTY_TYPE_VALUE);
  873. if (p)
  874. svc->prtcid = p->value.immediate;
  875. p = tb_property_find(dir, "prtcvers", TB_PROPERTY_TYPE_VALUE);
  876. if (p)
  877. svc->prtcvers = p->value.immediate;
  878. p = tb_property_find(dir, "prtcrevs", TB_PROPERTY_TYPE_VALUE);
  879. if (p)
  880. svc->prtcrevs = p->value.immediate;
  881. p = tb_property_find(dir, "prtcstns", TB_PROPERTY_TYPE_VALUE);
  882. if (p)
  883. svc->prtcstns = p->value.immediate;
  884. svc->key = kstrdup(property->key, GFP_KERNEL);
  885. if (!svc->key)
  886. return -ENOMEM;
  887. return 0;
  888. }
  889. static void enumerate_services(struct tb_xdomain *xd)
  890. {
  891. struct tb_service *svc;
  892. struct tb_property *p;
  893. struct device *dev;
  894. int id;
  895. /*
  896. * First remove all services that are not available anymore in
  897. * the updated property block.
  898. */
  899. device_for_each_child_reverse(&xd->dev, xd, remove_missing_service);
  900. /* Then re-enumerate properties creating new services as we go */
  901. tb_property_for_each(xd->remote_properties, p) {
  902. if (p->type != TB_PROPERTY_TYPE_DIRECTORY)
  903. continue;
  904. /* If the service exists already we are fine */
  905. dev = device_find_child(&xd->dev, p, find_service);
  906. if (dev) {
  907. put_device(dev);
  908. continue;
  909. }
  910. svc = kzalloc(sizeof(*svc), GFP_KERNEL);
  911. if (!svc)
  912. break;
  913. if (populate_service(svc, p)) {
  914. kfree(svc);
  915. break;
  916. }
  917. id = ida_simple_get(&xd->service_ids, 0, 0, GFP_KERNEL);
  918. if (id < 0) {
  919. kfree(svc->key);
  920. kfree(svc);
  921. break;
  922. }
  923. svc->id = id;
  924. svc->dev.bus = &tb_bus_type;
  925. svc->dev.type = &tb_service_type;
  926. svc->dev.parent = &xd->dev;
  927. dev_set_name(&svc->dev, "%s.%d", dev_name(&xd->dev), svc->id);
  928. tb_service_debugfs_init(svc);
  929. if (device_register(&svc->dev)) {
  930. put_device(&svc->dev);
  931. break;
  932. }
  933. }
  934. }
  935. static int populate_properties(struct tb_xdomain *xd,
  936. struct tb_property_dir *dir)
  937. {
  938. const struct tb_property *p;
  939. /* Required properties */
  940. p = tb_property_find(dir, "deviceid", TB_PROPERTY_TYPE_VALUE);
  941. if (!p)
  942. return -EINVAL;
  943. xd->device = p->value.immediate;
  944. p = tb_property_find(dir, "vendorid", TB_PROPERTY_TYPE_VALUE);
  945. if (!p)
  946. return -EINVAL;
  947. xd->vendor = p->value.immediate;
  948. p = tb_property_find(dir, "maxhopid", TB_PROPERTY_TYPE_VALUE);
  949. /*
  950. * USB4 inter-domain spec suggests using 15 as HopID if the
  951. * other end does not announce it in a property. This is for
  952. * TBT3 compatibility.
  953. */
  954. xd->remote_max_hopid = p ? p->value.immediate : XDOMAIN_DEFAULT_MAX_HOPID;
  955. kfree(xd->device_name);
  956. xd->device_name = NULL;
  957. kfree(xd->vendor_name);
  958. xd->vendor_name = NULL;
  959. /* Optional properties */
  960. p = tb_property_find(dir, "deviceid", TB_PROPERTY_TYPE_TEXT);
  961. if (p)
  962. xd->device_name = kstrdup(p->value.text, GFP_KERNEL);
  963. p = tb_property_find(dir, "vendorid", TB_PROPERTY_TYPE_TEXT);
  964. if (p)
  965. xd->vendor_name = kstrdup(p->value.text, GFP_KERNEL);
  966. return 0;
  967. }
  968. static int tb_xdomain_update_link_attributes(struct tb_xdomain *xd)
  969. {
  970. bool change = false;
  971. struct tb_port *port;
  972. int ret;
  973. port = tb_port_at(xd->route, tb_xdomain_parent(xd));
  974. ret = tb_port_get_link_speed(port);
  975. if (ret < 0)
  976. return ret;
  977. if (xd->link_speed != ret)
  978. change = true;
  979. xd->link_speed = ret;
  980. ret = tb_port_get_link_width(port);
  981. if (ret < 0)
  982. return ret;
  983. if (xd->link_width != ret)
  984. change = true;
  985. xd->link_width = ret;
  986. if (change)
  987. kobject_uevent(&xd->dev.kobj, KOBJ_CHANGE);
  988. return 0;
  989. }
  990. static int tb_xdomain_get_uuid(struct tb_xdomain *xd)
  991. {
  992. struct tb *tb = xd->tb;
  993. uuid_t uuid;
  994. u64 route;
  995. int ret;
  996. dev_dbg(&xd->dev, "requesting remote UUID\n");
  997. ret = tb_xdp_uuid_request(tb->ctl, xd->route, xd->state_retries, &uuid,
  998. &route);
  999. if (ret < 0) {
  1000. if (xd->state_retries-- > 0) {
  1001. dev_dbg(&xd->dev, "failed to request UUID, retrying\n");
  1002. return -EAGAIN;
  1003. } else {
  1004. dev_dbg(&xd->dev, "failed to read remote UUID\n");
  1005. }
  1006. return ret;
  1007. }
  1008. dev_dbg(&xd->dev, "got remote UUID %pUb\n", &uuid);
  1009. if (uuid_equal(&uuid, xd->local_uuid)) {
  1010. if (route == xd->route)
  1011. dev_dbg(&xd->dev, "loop back detected\n");
  1012. else
  1013. dev_dbg(&xd->dev, "intra-domain loop detected\n");
  1014. /* Don't bond lanes automatically for loops */
  1015. xd->bonding_possible = false;
  1016. }
  1017. /*
  1018. * If the UUID is different, there is another domain connected
  1019. * so mark this one unplugged and wait for the connection
  1020. * manager to replace it.
  1021. */
  1022. if (xd->remote_uuid && !uuid_equal(&uuid, xd->remote_uuid)) {
  1023. dev_dbg(&xd->dev, "remote UUID is different, unplugging\n");
  1024. xd->is_unplugged = true;
  1025. return -ENODEV;
  1026. }
  1027. /* First time fill in the missing UUID */
  1028. if (!xd->remote_uuid) {
  1029. xd->remote_uuid = kmemdup(&uuid, sizeof(uuid_t), GFP_KERNEL);
  1030. if (!xd->remote_uuid)
  1031. return -ENOMEM;
  1032. }
  1033. return 0;
  1034. }
  1035. static int tb_xdomain_get_link_status(struct tb_xdomain *xd)
  1036. {
  1037. struct tb *tb = xd->tb;
  1038. u8 slw, tlw, sls, tls;
  1039. int ret;
  1040. dev_dbg(&xd->dev, "sending link state status request to %pUb\n",
  1041. xd->remote_uuid);
  1042. ret = tb_xdp_link_state_status_request(tb->ctl, xd->route,
  1043. xd->state_retries, &slw, &tlw, &sls,
  1044. &tls);
  1045. if (ret) {
  1046. if (ret != -EOPNOTSUPP && xd->state_retries-- > 0) {
  1047. dev_dbg(&xd->dev,
  1048. "failed to request remote link status, retrying\n");
  1049. return -EAGAIN;
  1050. }
  1051. dev_dbg(&xd->dev, "failed to receive remote link status\n");
  1052. return ret;
  1053. }
  1054. dev_dbg(&xd->dev, "remote link supports width %#x speed %#x\n", slw, sls);
  1055. if (slw < LANE_ADP_CS_0_SUPPORTED_WIDTH_DUAL) {
  1056. dev_dbg(&xd->dev, "remote adapter is single lane only\n");
  1057. return -EOPNOTSUPP;
  1058. }
  1059. return 0;
  1060. }
  1061. static int tb_xdomain_link_state_change(struct tb_xdomain *xd,
  1062. unsigned int width)
  1063. {
  1064. struct tb_switch *sw = tb_to_switch(xd->dev.parent);
  1065. struct tb_port *port = tb_port_at(xd->route, sw);
  1066. struct tb *tb = xd->tb;
  1067. u8 tlw, tls;
  1068. u32 val;
  1069. int ret;
  1070. if (width == 2)
  1071. tlw = LANE_ADP_CS_1_TARGET_WIDTH_DUAL;
  1072. else if (width == 1)
  1073. tlw = LANE_ADP_CS_1_TARGET_WIDTH_SINGLE;
  1074. else
  1075. return -EINVAL;
  1076. /* Use the current target speed */
  1077. ret = tb_port_read(port, &val, TB_CFG_PORT, port->cap_phy + LANE_ADP_CS_1, 1);
  1078. if (ret)
  1079. return ret;
  1080. tls = val & LANE_ADP_CS_1_TARGET_SPEED_MASK;
  1081. dev_dbg(&xd->dev, "sending link state change request with width %#x speed %#x\n",
  1082. tlw, tls);
  1083. ret = tb_xdp_link_state_change_request(tb->ctl, xd->route,
  1084. xd->state_retries, tlw, tls);
  1085. if (ret) {
  1086. if (ret != -EOPNOTSUPP && xd->state_retries-- > 0) {
  1087. dev_dbg(&xd->dev,
  1088. "failed to change remote link state, retrying\n");
  1089. return -EAGAIN;
  1090. }
  1091. dev_err(&xd->dev, "failed request link state change, aborting\n");
  1092. return ret;
  1093. }
  1094. dev_dbg(&xd->dev, "received link state change response\n");
  1095. return 0;
  1096. }
  1097. static int tb_xdomain_bond_lanes_uuid_high(struct tb_xdomain *xd)
  1098. {
  1099. struct tb_port *port;
  1100. int ret, width;
  1101. if (xd->target_link_width == LANE_ADP_CS_1_TARGET_WIDTH_SINGLE) {
  1102. width = 1;
  1103. } else if (xd->target_link_width == LANE_ADP_CS_1_TARGET_WIDTH_DUAL) {
  1104. width = 2;
  1105. } else {
  1106. if (xd->state_retries-- > 0) {
  1107. dev_dbg(&xd->dev,
  1108. "link state change request not received yet, retrying\n");
  1109. return -EAGAIN;
  1110. }
  1111. dev_dbg(&xd->dev, "timeout waiting for link change request\n");
  1112. return -ETIMEDOUT;
  1113. }
  1114. port = tb_port_at(xd->route, tb_xdomain_parent(xd));
  1115. /*
  1116. * We can't use tb_xdomain_lane_bonding_enable() here because it
  1117. * is the other side that initiates lane bonding. So here we
  1118. * just set the width to both lane adapters and wait for the
  1119. * link to transition bonded.
  1120. */
  1121. ret = tb_port_set_link_width(port->dual_link_port, width);
  1122. if (ret) {
  1123. tb_port_warn(port->dual_link_port,
  1124. "failed to set link width to %d\n", width);
  1125. return ret;
  1126. }
  1127. ret = tb_port_set_link_width(port, width);
  1128. if (ret) {
  1129. tb_port_warn(port, "failed to set link width to %d\n", width);
  1130. return ret;
  1131. }
  1132. ret = tb_port_wait_for_link_width(port, width, XDOMAIN_BONDING_TIMEOUT);
  1133. if (ret) {
  1134. dev_warn(&xd->dev, "error waiting for link width to become %d\n",
  1135. width);
  1136. return ret;
  1137. }
  1138. port->bonded = width == 2;
  1139. port->dual_link_port->bonded = width == 2;
  1140. tb_port_update_credits(port);
  1141. tb_xdomain_update_link_attributes(xd);
  1142. dev_dbg(&xd->dev, "lane bonding %sabled\n", width == 2 ? "en" : "dis");
  1143. return 0;
  1144. }
  1145. static int tb_xdomain_get_properties(struct tb_xdomain *xd)
  1146. {
  1147. struct tb_property_dir *dir;
  1148. struct tb *tb = xd->tb;
  1149. bool update = false;
  1150. u32 *block = NULL;
  1151. u32 gen = 0;
  1152. int ret;
  1153. dev_dbg(&xd->dev, "requesting remote properties\n");
  1154. ret = tb_xdp_properties_request(tb->ctl, xd->route, xd->local_uuid,
  1155. xd->remote_uuid, xd->state_retries,
  1156. &block, &gen);
  1157. if (ret < 0) {
  1158. if (xd->state_retries-- > 0) {
  1159. dev_dbg(&xd->dev,
  1160. "failed to request remote properties, retrying\n");
  1161. return -EAGAIN;
  1162. } else {
  1163. /* Give up now */
  1164. dev_err(&xd->dev,
  1165. "failed read XDomain properties from %pUb\n",
  1166. xd->remote_uuid);
  1167. }
  1168. return ret;
  1169. }
  1170. mutex_lock(&xd->lock);
  1171. /* Only accept newer generation properties */
  1172. if (xd->remote_properties && gen <= xd->remote_property_block_gen) {
  1173. ret = 0;
  1174. goto err_free_block;
  1175. }
  1176. dir = tb_property_parse_dir(block, ret);
  1177. if (!dir) {
  1178. dev_err(&xd->dev, "failed to parse XDomain properties\n");
  1179. ret = -ENOMEM;
  1180. goto err_free_block;
  1181. }
  1182. ret = populate_properties(xd, dir);
  1183. if (ret) {
  1184. dev_err(&xd->dev, "missing XDomain properties in response\n");
  1185. goto err_free_dir;
  1186. }
  1187. /* Release the existing one */
  1188. if (xd->remote_properties) {
  1189. tb_property_free_dir(xd->remote_properties);
  1190. update = true;
  1191. }
  1192. xd->remote_properties = dir;
  1193. xd->remote_property_block_gen = gen;
  1194. tb_xdomain_update_link_attributes(xd);
  1195. mutex_unlock(&xd->lock);
  1196. kfree(block);
  1197. /*
  1198. * Now the device should be ready enough so we can add it to the
  1199. * bus and let userspace know about it. If the device is already
  1200. * registered, we notify the userspace that it has changed.
  1201. */
  1202. if (!update) {
  1203. /*
  1204. * Now disable lane 1 if bonding was not enabled. Do
  1205. * this only if bonding was possible at the beginning
  1206. * (that is we are the connection manager and there are
  1207. * two lanes).
  1208. */
  1209. if (xd->bonding_possible) {
  1210. struct tb_port *port;
  1211. port = tb_port_at(xd->route, tb_xdomain_parent(xd));
  1212. if (!port->bonded)
  1213. tb_port_disable(port->dual_link_port);
  1214. }
  1215. if (device_add(&xd->dev)) {
  1216. dev_err(&xd->dev, "failed to add XDomain device\n");
  1217. return -ENODEV;
  1218. }
  1219. dev_info(&xd->dev, "new host found, vendor=%#x device=%#x\n",
  1220. xd->vendor, xd->device);
  1221. if (xd->vendor_name && xd->device_name)
  1222. dev_info(&xd->dev, "%s %s\n", xd->vendor_name,
  1223. xd->device_name);
  1224. tb_xdomain_debugfs_init(xd);
  1225. } else {
  1226. kobject_uevent(&xd->dev.kobj, KOBJ_CHANGE);
  1227. }
  1228. enumerate_services(xd);
  1229. return 0;
  1230. err_free_dir:
  1231. tb_property_free_dir(dir);
  1232. err_free_block:
  1233. kfree(block);
  1234. mutex_unlock(&xd->lock);
  1235. return ret;
  1236. }
  1237. static void tb_xdomain_queue_uuid(struct tb_xdomain *xd)
  1238. {
  1239. xd->state = XDOMAIN_STATE_UUID;
  1240. xd->state_retries = XDOMAIN_RETRIES;
  1241. queue_delayed_work(xd->tb->wq, &xd->state_work,
  1242. msecs_to_jiffies(XDOMAIN_SHORT_TIMEOUT));
  1243. }
  1244. static void tb_xdomain_queue_link_status(struct tb_xdomain *xd)
  1245. {
  1246. xd->state = XDOMAIN_STATE_LINK_STATUS;
  1247. xd->state_retries = XDOMAIN_RETRIES;
  1248. queue_delayed_work(xd->tb->wq, &xd->state_work,
  1249. msecs_to_jiffies(XDOMAIN_DEFAULT_TIMEOUT));
  1250. }
  1251. static void tb_xdomain_queue_link_status2(struct tb_xdomain *xd)
  1252. {
  1253. xd->state = XDOMAIN_STATE_LINK_STATUS2;
  1254. xd->state_retries = XDOMAIN_RETRIES;
  1255. queue_delayed_work(xd->tb->wq, &xd->state_work,
  1256. msecs_to_jiffies(XDOMAIN_DEFAULT_TIMEOUT));
  1257. }
  1258. static void tb_xdomain_queue_bonding(struct tb_xdomain *xd)
  1259. {
  1260. if (memcmp(xd->local_uuid, xd->remote_uuid, UUID_SIZE) > 0) {
  1261. dev_dbg(&xd->dev, "we have higher UUID, other side bonds the lanes\n");
  1262. xd->state = XDOMAIN_STATE_BONDING_UUID_HIGH;
  1263. } else {
  1264. dev_dbg(&xd->dev, "we have lower UUID, bonding lanes\n");
  1265. xd->state = XDOMAIN_STATE_LINK_STATE_CHANGE;
  1266. }
  1267. xd->state_retries = XDOMAIN_RETRIES;
  1268. queue_delayed_work(xd->tb->wq, &xd->state_work,
  1269. msecs_to_jiffies(XDOMAIN_DEFAULT_TIMEOUT));
  1270. }
  1271. static void tb_xdomain_queue_bonding_uuid_low(struct tb_xdomain *xd)
  1272. {
  1273. xd->state = XDOMAIN_STATE_BONDING_UUID_LOW;
  1274. xd->state_retries = XDOMAIN_RETRIES;
  1275. queue_delayed_work(xd->tb->wq, &xd->state_work,
  1276. msecs_to_jiffies(XDOMAIN_DEFAULT_TIMEOUT));
  1277. }
  1278. static void tb_xdomain_queue_properties(struct tb_xdomain *xd)
  1279. {
  1280. xd->state = XDOMAIN_STATE_PROPERTIES;
  1281. xd->state_retries = XDOMAIN_RETRIES;
  1282. queue_delayed_work(xd->tb->wq, &xd->state_work,
  1283. msecs_to_jiffies(XDOMAIN_DEFAULT_TIMEOUT));
  1284. }
  1285. static void tb_xdomain_queue_properties_changed(struct tb_xdomain *xd)
  1286. {
  1287. xd->properties_changed_retries = XDOMAIN_RETRIES;
  1288. queue_delayed_work(xd->tb->wq, &xd->properties_changed_work,
  1289. msecs_to_jiffies(XDOMAIN_SHORT_TIMEOUT));
  1290. }
  1291. static void tb_xdomain_failed(struct tb_xdomain *xd)
  1292. {
  1293. xd->state = XDOMAIN_STATE_ERROR;
  1294. queue_delayed_work(xd->tb->wq, &xd->state_work,
  1295. msecs_to_jiffies(XDOMAIN_DEFAULT_TIMEOUT));
  1296. }
  1297. static void tb_xdomain_state_work(struct work_struct *work)
  1298. {
  1299. struct tb_xdomain *xd = container_of(work, typeof(*xd), state_work.work);
  1300. int ret, state = xd->state;
  1301. if (WARN_ON_ONCE(state < XDOMAIN_STATE_INIT ||
  1302. state > XDOMAIN_STATE_ERROR))
  1303. return;
  1304. dev_dbg(&xd->dev, "running state %s\n", state_names[state]);
  1305. switch (state) {
  1306. case XDOMAIN_STATE_INIT:
  1307. if (xd->needs_uuid) {
  1308. tb_xdomain_queue_uuid(xd);
  1309. } else {
  1310. tb_xdomain_queue_properties_changed(xd);
  1311. tb_xdomain_queue_properties(xd);
  1312. }
  1313. break;
  1314. case XDOMAIN_STATE_UUID:
  1315. ret = tb_xdomain_get_uuid(xd);
  1316. if (ret) {
  1317. if (ret == -EAGAIN)
  1318. goto retry_state;
  1319. tb_xdomain_failed(xd);
  1320. } else {
  1321. tb_xdomain_queue_properties_changed(xd);
  1322. if (xd->bonding_possible)
  1323. tb_xdomain_queue_link_status(xd);
  1324. else
  1325. tb_xdomain_queue_properties(xd);
  1326. }
  1327. break;
  1328. case XDOMAIN_STATE_LINK_STATUS:
  1329. ret = tb_xdomain_get_link_status(xd);
  1330. if (ret) {
  1331. if (ret == -EAGAIN)
  1332. goto retry_state;
  1333. /*
  1334. * If any of the lane bonding states fail we skip
  1335. * bonding completely and try to continue from
  1336. * reading properties.
  1337. */
  1338. tb_xdomain_queue_properties(xd);
  1339. } else {
  1340. tb_xdomain_queue_bonding(xd);
  1341. }
  1342. break;
  1343. case XDOMAIN_STATE_LINK_STATE_CHANGE:
  1344. ret = tb_xdomain_link_state_change(xd, 2);
  1345. if (ret) {
  1346. if (ret == -EAGAIN)
  1347. goto retry_state;
  1348. tb_xdomain_queue_properties(xd);
  1349. } else {
  1350. tb_xdomain_queue_link_status2(xd);
  1351. }
  1352. break;
  1353. case XDOMAIN_STATE_LINK_STATUS2:
  1354. ret = tb_xdomain_get_link_status(xd);
  1355. if (ret) {
  1356. if (ret == -EAGAIN)
  1357. goto retry_state;
  1358. tb_xdomain_queue_properties(xd);
  1359. } else {
  1360. tb_xdomain_queue_bonding_uuid_low(xd);
  1361. }
  1362. break;
  1363. case XDOMAIN_STATE_BONDING_UUID_LOW:
  1364. tb_xdomain_lane_bonding_enable(xd);
  1365. tb_xdomain_queue_properties(xd);
  1366. break;
  1367. case XDOMAIN_STATE_BONDING_UUID_HIGH:
  1368. if (tb_xdomain_bond_lanes_uuid_high(xd) == -EAGAIN)
  1369. goto retry_state;
  1370. tb_xdomain_queue_properties(xd);
  1371. break;
  1372. case XDOMAIN_STATE_PROPERTIES:
  1373. ret = tb_xdomain_get_properties(xd);
  1374. if (ret) {
  1375. if (ret == -EAGAIN)
  1376. goto retry_state;
  1377. tb_xdomain_failed(xd);
  1378. } else {
  1379. xd->state = XDOMAIN_STATE_ENUMERATED;
  1380. }
  1381. break;
  1382. case XDOMAIN_STATE_ENUMERATED:
  1383. tb_xdomain_queue_properties(xd);
  1384. break;
  1385. case XDOMAIN_STATE_ERROR:
  1386. dev_dbg(&xd->dev, "discovery failed, stopping handshake\n");
  1387. __stop_handshake(xd);
  1388. break;
  1389. default:
  1390. dev_warn(&xd->dev, "unexpected state %d\n", state);
  1391. break;
  1392. }
  1393. return;
  1394. retry_state:
  1395. queue_delayed_work(xd->tb->wq, &xd->state_work,
  1396. msecs_to_jiffies(XDOMAIN_DEFAULT_TIMEOUT));
  1397. }
  1398. static void tb_xdomain_properties_changed(struct work_struct *work)
  1399. {
  1400. struct tb_xdomain *xd = container_of(work, typeof(*xd),
  1401. properties_changed_work.work);
  1402. int ret;
  1403. dev_dbg(&xd->dev, "sending properties changed notification\n");
  1404. ret = tb_xdp_properties_changed_request(xd->tb->ctl, xd->route,
  1405. xd->properties_changed_retries, xd->local_uuid);
  1406. if (ret) {
  1407. if (xd->properties_changed_retries-- > 0) {
  1408. dev_dbg(&xd->dev,
  1409. "failed to send properties changed notification, retrying\n");
  1410. queue_delayed_work(xd->tb->wq,
  1411. &xd->properties_changed_work,
  1412. msecs_to_jiffies(XDOMAIN_DEFAULT_TIMEOUT));
  1413. }
  1414. dev_err(&xd->dev, "failed to send properties changed notification\n");
  1415. return;
  1416. }
  1417. xd->properties_changed_retries = XDOMAIN_RETRIES;
  1418. }
  1419. static ssize_t device_show(struct device *dev, struct device_attribute *attr,
  1420. char *buf)
  1421. {
  1422. struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
  1423. return sysfs_emit(buf, "%#x\n", xd->device);
  1424. }
  1425. static DEVICE_ATTR_RO(device);
  1426. static ssize_t
  1427. device_name_show(struct device *dev, struct device_attribute *attr, char *buf)
  1428. {
  1429. struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
  1430. int ret;
  1431. if (mutex_lock_interruptible(&xd->lock))
  1432. return -ERESTARTSYS;
  1433. ret = sysfs_emit(buf, "%s\n", xd->device_name ?: "");
  1434. mutex_unlock(&xd->lock);
  1435. return ret;
  1436. }
  1437. static DEVICE_ATTR_RO(device_name);
  1438. static ssize_t maxhopid_show(struct device *dev, struct device_attribute *attr,
  1439. char *buf)
  1440. {
  1441. struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
  1442. return sysfs_emit(buf, "%d\n", xd->remote_max_hopid);
  1443. }
  1444. static DEVICE_ATTR_RO(maxhopid);
  1445. static ssize_t vendor_show(struct device *dev, struct device_attribute *attr,
  1446. char *buf)
  1447. {
  1448. struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
  1449. return sysfs_emit(buf, "%#x\n", xd->vendor);
  1450. }
  1451. static DEVICE_ATTR_RO(vendor);
  1452. static ssize_t
  1453. vendor_name_show(struct device *dev, struct device_attribute *attr, char *buf)
  1454. {
  1455. struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
  1456. int ret;
  1457. if (mutex_lock_interruptible(&xd->lock))
  1458. return -ERESTARTSYS;
  1459. ret = sysfs_emit(buf, "%s\n", xd->vendor_name ?: "");
  1460. mutex_unlock(&xd->lock);
  1461. return ret;
  1462. }
  1463. static DEVICE_ATTR_RO(vendor_name);
  1464. static ssize_t unique_id_show(struct device *dev, struct device_attribute *attr,
  1465. char *buf)
  1466. {
  1467. struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
  1468. return sysfs_emit(buf, "%pUb\n", xd->remote_uuid);
  1469. }
  1470. static DEVICE_ATTR_RO(unique_id);
  1471. static ssize_t speed_show(struct device *dev, struct device_attribute *attr,
  1472. char *buf)
  1473. {
  1474. struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
  1475. return sysfs_emit(buf, "%u.0 Gb/s\n", xd->link_speed);
  1476. }
  1477. static DEVICE_ATTR(rx_speed, 0444, speed_show, NULL);
  1478. static DEVICE_ATTR(tx_speed, 0444, speed_show, NULL);
  1479. static ssize_t lanes_show(struct device *dev, struct device_attribute *attr,
  1480. char *buf)
  1481. {
  1482. struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
  1483. return sysfs_emit(buf, "%u\n", xd->link_width);
  1484. }
  1485. static DEVICE_ATTR(rx_lanes, 0444, lanes_show, NULL);
  1486. static DEVICE_ATTR(tx_lanes, 0444, lanes_show, NULL);
  1487. static struct attribute *xdomain_attrs[] = {
  1488. &dev_attr_device.attr,
  1489. &dev_attr_device_name.attr,
  1490. &dev_attr_maxhopid.attr,
  1491. &dev_attr_rx_lanes.attr,
  1492. &dev_attr_rx_speed.attr,
  1493. &dev_attr_tx_lanes.attr,
  1494. &dev_attr_tx_speed.attr,
  1495. &dev_attr_unique_id.attr,
  1496. &dev_attr_vendor.attr,
  1497. &dev_attr_vendor_name.attr,
  1498. NULL,
  1499. };
  1500. static const struct attribute_group xdomain_attr_group = {
  1501. .attrs = xdomain_attrs,
  1502. };
  1503. static const struct attribute_group *xdomain_attr_groups[] = {
  1504. &xdomain_attr_group,
  1505. NULL,
  1506. };
  1507. static void tb_xdomain_release(struct device *dev)
  1508. {
  1509. struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
  1510. put_device(xd->dev.parent);
  1511. kfree(xd->local_property_block);
  1512. tb_property_free_dir(xd->remote_properties);
  1513. ida_destroy(&xd->out_hopids);
  1514. ida_destroy(&xd->in_hopids);
  1515. ida_destroy(&xd->service_ids);
  1516. kfree(xd->local_uuid);
  1517. kfree(xd->remote_uuid);
  1518. kfree(xd->device_name);
  1519. kfree(xd->vendor_name);
  1520. kfree(xd);
  1521. }
  1522. static int __maybe_unused tb_xdomain_suspend(struct device *dev)
  1523. {
  1524. stop_handshake(tb_to_xdomain(dev));
  1525. return 0;
  1526. }
  1527. static int __maybe_unused tb_xdomain_resume(struct device *dev)
  1528. {
  1529. start_handshake(tb_to_xdomain(dev));
  1530. return 0;
  1531. }
  1532. static const struct dev_pm_ops tb_xdomain_pm_ops = {
  1533. SET_SYSTEM_SLEEP_PM_OPS(tb_xdomain_suspend, tb_xdomain_resume)
  1534. };
  1535. struct device_type tb_xdomain_type = {
  1536. .name = "thunderbolt_xdomain",
  1537. .release = tb_xdomain_release,
  1538. .pm = &tb_xdomain_pm_ops,
  1539. };
  1540. EXPORT_SYMBOL_GPL(tb_xdomain_type);
  1541. /**
  1542. * tb_xdomain_alloc() - Allocate new XDomain object
  1543. * @tb: Domain where the XDomain belongs
  1544. * @parent: Parent device (the switch through the connection to the
  1545. * other domain is reached).
  1546. * @route: Route string used to reach the other domain
  1547. * @local_uuid: Our local domain UUID
  1548. * @remote_uuid: UUID of the other domain (optional)
  1549. *
  1550. * Allocates new XDomain structure and returns pointer to that. The
  1551. * object must be released by calling tb_xdomain_put().
  1552. */
  1553. struct tb_xdomain *tb_xdomain_alloc(struct tb *tb, struct device *parent,
  1554. u64 route, const uuid_t *local_uuid,
  1555. const uuid_t *remote_uuid)
  1556. {
  1557. struct tb_switch *parent_sw = tb_to_switch(parent);
  1558. struct tb_xdomain *xd;
  1559. struct tb_port *down;
  1560. /* Make sure the downstream domain is accessible */
  1561. down = tb_port_at(route, parent_sw);
  1562. tb_port_unlock(down);
  1563. xd = kzalloc(sizeof(*xd), GFP_KERNEL);
  1564. if (!xd)
  1565. return NULL;
  1566. xd->tb = tb;
  1567. xd->route = route;
  1568. xd->local_max_hopid = down->config.max_in_hop_id;
  1569. ida_init(&xd->service_ids);
  1570. ida_init(&xd->in_hopids);
  1571. ida_init(&xd->out_hopids);
  1572. mutex_init(&xd->lock);
  1573. INIT_DELAYED_WORK(&xd->state_work, tb_xdomain_state_work);
  1574. INIT_DELAYED_WORK(&xd->properties_changed_work,
  1575. tb_xdomain_properties_changed);
  1576. xd->local_uuid = kmemdup(local_uuid, sizeof(uuid_t), GFP_KERNEL);
  1577. if (!xd->local_uuid)
  1578. goto err_free;
  1579. if (remote_uuid) {
  1580. xd->remote_uuid = kmemdup(remote_uuid, sizeof(uuid_t),
  1581. GFP_KERNEL);
  1582. if (!xd->remote_uuid)
  1583. goto err_free_local_uuid;
  1584. } else {
  1585. xd->needs_uuid = true;
  1586. xd->bonding_possible = !!down->dual_link_port;
  1587. }
  1588. device_initialize(&xd->dev);
  1589. xd->dev.parent = get_device(parent);
  1590. xd->dev.bus = &tb_bus_type;
  1591. xd->dev.type = &tb_xdomain_type;
  1592. xd->dev.groups = xdomain_attr_groups;
  1593. dev_set_name(&xd->dev, "%u-%llx", tb->index, route);
  1594. dev_dbg(&xd->dev, "local UUID %pUb\n", local_uuid);
  1595. if (remote_uuid)
  1596. dev_dbg(&xd->dev, "remote UUID %pUb\n", remote_uuid);
  1597. /*
  1598. * This keeps the DMA powered on as long as we have active
  1599. * connection to another host.
  1600. */
  1601. pm_runtime_set_active(&xd->dev);
  1602. pm_runtime_get_noresume(&xd->dev);
  1603. pm_runtime_enable(&xd->dev);
  1604. return xd;
  1605. err_free_local_uuid:
  1606. kfree(xd->local_uuid);
  1607. err_free:
  1608. kfree(xd);
  1609. return NULL;
  1610. }
  1611. /**
  1612. * tb_xdomain_add() - Add XDomain to the bus
  1613. * @xd: XDomain to add
  1614. *
  1615. * This function starts XDomain discovery protocol handshake and
  1616. * eventually adds the XDomain to the bus. After calling this function
  1617. * the caller needs to call tb_xdomain_remove() in order to remove and
  1618. * release the object regardless whether the handshake succeeded or not.
  1619. */
  1620. void tb_xdomain_add(struct tb_xdomain *xd)
  1621. {
  1622. /* Start exchanging properties with the other host */
  1623. start_handshake(xd);
  1624. }
  1625. static int unregister_service(struct device *dev, void *data)
  1626. {
  1627. device_unregister(dev);
  1628. return 0;
  1629. }
  1630. /**
  1631. * tb_xdomain_remove() - Remove XDomain from the bus
  1632. * @xd: XDomain to remove
  1633. *
  1634. * This will stop all ongoing configuration work and remove the XDomain
  1635. * along with any services from the bus. When the last reference to @xd
  1636. * is released the object will be released as well.
  1637. */
  1638. void tb_xdomain_remove(struct tb_xdomain *xd)
  1639. {
  1640. tb_xdomain_debugfs_remove(xd);
  1641. stop_handshake(xd);
  1642. device_for_each_child_reverse(&xd->dev, xd, unregister_service);
  1643. /*
  1644. * Undo runtime PM here explicitly because it is possible that
  1645. * the XDomain was never added to the bus and thus device_del()
  1646. * is not called for it (device_del() would handle this otherwise).
  1647. */
  1648. pm_runtime_disable(&xd->dev);
  1649. pm_runtime_put_noidle(&xd->dev);
  1650. pm_runtime_set_suspended(&xd->dev);
  1651. if (!device_is_registered(&xd->dev)) {
  1652. put_device(&xd->dev);
  1653. } else {
  1654. dev_info(&xd->dev, "host disconnected\n");
  1655. device_unregister(&xd->dev);
  1656. }
  1657. }
  1658. /**
  1659. * tb_xdomain_lane_bonding_enable() - Enable lane bonding on XDomain
  1660. * @xd: XDomain connection
  1661. *
  1662. * Lane bonding is disabled by default for XDomains. This function tries
  1663. * to enable bonding by first enabling the port and waiting for the CL0
  1664. * state.
  1665. *
  1666. * Return: %0 in case of success and negative errno in case of error.
  1667. */
  1668. int tb_xdomain_lane_bonding_enable(struct tb_xdomain *xd)
  1669. {
  1670. struct tb_port *port;
  1671. int ret;
  1672. port = tb_port_at(xd->route, tb_xdomain_parent(xd));
  1673. if (!port->dual_link_port)
  1674. return -ENODEV;
  1675. ret = tb_port_enable(port->dual_link_port);
  1676. if (ret)
  1677. return ret;
  1678. ret = tb_wait_for_port(port->dual_link_port, true);
  1679. if (ret < 0)
  1680. return ret;
  1681. if (!ret)
  1682. return -ENOTCONN;
  1683. ret = tb_port_lane_bonding_enable(port);
  1684. if (ret) {
  1685. tb_port_warn(port, "failed to enable lane bonding\n");
  1686. return ret;
  1687. }
  1688. ret = tb_port_wait_for_link_width(port, 2, XDOMAIN_BONDING_TIMEOUT);
  1689. if (ret) {
  1690. tb_port_warn(port, "failed to enable lane bonding\n");
  1691. return ret;
  1692. }
  1693. tb_port_update_credits(port);
  1694. tb_xdomain_update_link_attributes(xd);
  1695. dev_dbg(&xd->dev, "lane bonding enabled\n");
  1696. return 0;
  1697. }
  1698. EXPORT_SYMBOL_GPL(tb_xdomain_lane_bonding_enable);
  1699. /**
  1700. * tb_xdomain_lane_bonding_disable() - Disable lane bonding
  1701. * @xd: XDomain connection
  1702. *
  1703. * Lane bonding is disabled by default for XDomains. If bonding has been
  1704. * enabled, this function can be used to disable it.
  1705. */
  1706. void tb_xdomain_lane_bonding_disable(struct tb_xdomain *xd)
  1707. {
  1708. struct tb_port *port;
  1709. port = tb_port_at(xd->route, tb_xdomain_parent(xd));
  1710. if (port->dual_link_port) {
  1711. tb_port_lane_bonding_disable(port);
  1712. if (tb_port_wait_for_link_width(port, 1, 100) == -ETIMEDOUT)
  1713. tb_port_warn(port, "timeout disabling lane bonding\n");
  1714. tb_port_disable(port->dual_link_port);
  1715. tb_port_update_credits(port);
  1716. tb_xdomain_update_link_attributes(xd);
  1717. dev_dbg(&xd->dev, "lane bonding disabled\n");
  1718. }
  1719. }
  1720. EXPORT_SYMBOL_GPL(tb_xdomain_lane_bonding_disable);
  1721. /**
  1722. * tb_xdomain_alloc_in_hopid() - Allocate input HopID for tunneling
  1723. * @xd: XDomain connection
  1724. * @hopid: Preferred HopID or %-1 for next available
  1725. *
  1726. * Returns allocated HopID or negative errno. Specifically returns
  1727. * %-ENOSPC if there are no more available HopIDs. Returned HopID is
  1728. * guaranteed to be within range supported by the input lane adapter.
  1729. * Call tb_xdomain_release_in_hopid() to release the allocated HopID.
  1730. */
  1731. int tb_xdomain_alloc_in_hopid(struct tb_xdomain *xd, int hopid)
  1732. {
  1733. if (hopid < 0)
  1734. hopid = TB_PATH_MIN_HOPID;
  1735. if (hopid < TB_PATH_MIN_HOPID || hopid > xd->local_max_hopid)
  1736. return -EINVAL;
  1737. return ida_alloc_range(&xd->in_hopids, hopid, xd->local_max_hopid,
  1738. GFP_KERNEL);
  1739. }
  1740. EXPORT_SYMBOL_GPL(tb_xdomain_alloc_in_hopid);
  1741. /**
  1742. * tb_xdomain_alloc_out_hopid() - Allocate output HopID for tunneling
  1743. * @xd: XDomain connection
  1744. * @hopid: Preferred HopID or %-1 for next available
  1745. *
  1746. * Returns allocated HopID or negative errno. Specifically returns
  1747. * %-ENOSPC if there are no more available HopIDs. Returned HopID is
  1748. * guaranteed to be within range supported by the output lane adapter.
  1749. * Call tb_xdomain_release_in_hopid() to release the allocated HopID.
  1750. */
  1751. int tb_xdomain_alloc_out_hopid(struct tb_xdomain *xd, int hopid)
  1752. {
  1753. if (hopid < 0)
  1754. hopid = TB_PATH_MIN_HOPID;
  1755. if (hopid < TB_PATH_MIN_HOPID || hopid > xd->remote_max_hopid)
  1756. return -EINVAL;
  1757. return ida_alloc_range(&xd->out_hopids, hopid, xd->remote_max_hopid,
  1758. GFP_KERNEL);
  1759. }
  1760. EXPORT_SYMBOL_GPL(tb_xdomain_alloc_out_hopid);
  1761. /**
  1762. * tb_xdomain_release_in_hopid() - Release input HopID
  1763. * @xd: XDomain connection
  1764. * @hopid: HopID to release
  1765. */
  1766. void tb_xdomain_release_in_hopid(struct tb_xdomain *xd, int hopid)
  1767. {
  1768. ida_free(&xd->in_hopids, hopid);
  1769. }
  1770. EXPORT_SYMBOL_GPL(tb_xdomain_release_in_hopid);
  1771. /**
  1772. * tb_xdomain_release_out_hopid() - Release output HopID
  1773. * @xd: XDomain connection
  1774. * @hopid: HopID to release
  1775. */
  1776. void tb_xdomain_release_out_hopid(struct tb_xdomain *xd, int hopid)
  1777. {
  1778. ida_free(&xd->out_hopids, hopid);
  1779. }
  1780. EXPORT_SYMBOL_GPL(tb_xdomain_release_out_hopid);
  1781. /**
  1782. * tb_xdomain_enable_paths() - Enable DMA paths for XDomain connection
  1783. * @xd: XDomain connection
  1784. * @transmit_path: HopID we are using to send out packets
  1785. * @transmit_ring: DMA ring used to send out packets
  1786. * @receive_path: HopID the other end is using to send packets to us
  1787. * @receive_ring: DMA ring used to receive packets from @receive_path
  1788. *
  1789. * The function enables DMA paths accordingly so that after successful
  1790. * return the caller can send and receive packets using high-speed DMA
  1791. * path. If a transmit or receive path is not needed, pass %-1 for those
  1792. * parameters.
  1793. *
  1794. * Return: %0 in case of success and negative errno in case of error
  1795. */
  1796. int tb_xdomain_enable_paths(struct tb_xdomain *xd, int transmit_path,
  1797. int transmit_ring, int receive_path,
  1798. int receive_ring)
  1799. {
  1800. return tb_domain_approve_xdomain_paths(xd->tb, xd, transmit_path,
  1801. transmit_ring, receive_path,
  1802. receive_ring);
  1803. }
  1804. EXPORT_SYMBOL_GPL(tb_xdomain_enable_paths);
  1805. /**
  1806. * tb_xdomain_disable_paths() - Disable DMA paths for XDomain connection
  1807. * @xd: XDomain connection
  1808. * @transmit_path: HopID we are using to send out packets
  1809. * @transmit_ring: DMA ring used to send out packets
  1810. * @receive_path: HopID the other end is using to send packets to us
  1811. * @receive_ring: DMA ring used to receive packets from @receive_path
  1812. *
  1813. * This does the opposite of tb_xdomain_enable_paths(). After call to
  1814. * this the caller is not expected to use the rings anymore. Passing %-1
  1815. * as path/ring parameter means don't care. Normally the callers should
  1816. * pass the same values here as they do when paths are enabled.
  1817. *
  1818. * Return: %0 in case of success and negative errno in case of error
  1819. */
  1820. int tb_xdomain_disable_paths(struct tb_xdomain *xd, int transmit_path,
  1821. int transmit_ring, int receive_path,
  1822. int receive_ring)
  1823. {
  1824. return tb_domain_disconnect_xdomain_paths(xd->tb, xd, transmit_path,
  1825. transmit_ring, receive_path,
  1826. receive_ring);
  1827. }
  1828. EXPORT_SYMBOL_GPL(tb_xdomain_disable_paths);
  1829. struct tb_xdomain_lookup {
  1830. const uuid_t *uuid;
  1831. u8 link;
  1832. u8 depth;
  1833. u64 route;
  1834. };
  1835. static struct tb_xdomain *switch_find_xdomain(struct tb_switch *sw,
  1836. const struct tb_xdomain_lookup *lookup)
  1837. {
  1838. struct tb_port *port;
  1839. tb_switch_for_each_port(sw, port) {
  1840. struct tb_xdomain *xd;
  1841. if (port->xdomain) {
  1842. xd = port->xdomain;
  1843. if (lookup->uuid) {
  1844. if (xd->remote_uuid &&
  1845. uuid_equal(xd->remote_uuid, lookup->uuid))
  1846. return xd;
  1847. } else if (lookup->link &&
  1848. lookup->link == xd->link &&
  1849. lookup->depth == xd->depth) {
  1850. return xd;
  1851. } else if (lookup->route &&
  1852. lookup->route == xd->route) {
  1853. return xd;
  1854. }
  1855. } else if (tb_port_has_remote(port)) {
  1856. xd = switch_find_xdomain(port->remote->sw, lookup);
  1857. if (xd)
  1858. return xd;
  1859. }
  1860. }
  1861. return NULL;
  1862. }
  1863. /**
  1864. * tb_xdomain_find_by_uuid() - Find an XDomain by UUID
  1865. * @tb: Domain where the XDomain belongs to
  1866. * @uuid: UUID to look for
  1867. *
  1868. * Finds XDomain by walking through the Thunderbolt topology below @tb.
  1869. * The returned XDomain will have its reference count increased so the
  1870. * caller needs to call tb_xdomain_put() when it is done with the
  1871. * object.
  1872. *
  1873. * This will find all XDomains including the ones that are not yet added
  1874. * to the bus (handshake is still in progress).
  1875. *
  1876. * The caller needs to hold @tb->lock.
  1877. */
  1878. struct tb_xdomain *tb_xdomain_find_by_uuid(struct tb *tb, const uuid_t *uuid)
  1879. {
  1880. struct tb_xdomain_lookup lookup;
  1881. struct tb_xdomain *xd;
  1882. memset(&lookup, 0, sizeof(lookup));
  1883. lookup.uuid = uuid;
  1884. xd = switch_find_xdomain(tb->root_switch, &lookup);
  1885. return tb_xdomain_get(xd);
  1886. }
  1887. EXPORT_SYMBOL_GPL(tb_xdomain_find_by_uuid);
  1888. /**
  1889. * tb_xdomain_find_by_link_depth() - Find an XDomain by link and depth
  1890. * @tb: Domain where the XDomain belongs to
  1891. * @link: Root switch link number
  1892. * @depth: Depth in the link
  1893. *
  1894. * Finds XDomain by walking through the Thunderbolt topology below @tb.
  1895. * The returned XDomain will have its reference count increased so the
  1896. * caller needs to call tb_xdomain_put() when it is done with the
  1897. * object.
  1898. *
  1899. * This will find all XDomains including the ones that are not yet added
  1900. * to the bus (handshake is still in progress).
  1901. *
  1902. * The caller needs to hold @tb->lock.
  1903. */
  1904. struct tb_xdomain *tb_xdomain_find_by_link_depth(struct tb *tb, u8 link,
  1905. u8 depth)
  1906. {
  1907. struct tb_xdomain_lookup lookup;
  1908. struct tb_xdomain *xd;
  1909. memset(&lookup, 0, sizeof(lookup));
  1910. lookup.link = link;
  1911. lookup.depth = depth;
  1912. xd = switch_find_xdomain(tb->root_switch, &lookup);
  1913. return tb_xdomain_get(xd);
  1914. }
  1915. /**
  1916. * tb_xdomain_find_by_route() - Find an XDomain by route string
  1917. * @tb: Domain where the XDomain belongs to
  1918. * @route: XDomain route string
  1919. *
  1920. * Finds XDomain by walking through the Thunderbolt topology below @tb.
  1921. * The returned XDomain will have its reference count increased so the
  1922. * caller needs to call tb_xdomain_put() when it is done with the
  1923. * object.
  1924. *
  1925. * This will find all XDomains including the ones that are not yet added
  1926. * to the bus (handshake is still in progress).
  1927. *
  1928. * The caller needs to hold @tb->lock.
  1929. */
  1930. struct tb_xdomain *tb_xdomain_find_by_route(struct tb *tb, u64 route)
  1931. {
  1932. struct tb_xdomain_lookup lookup;
  1933. struct tb_xdomain *xd;
  1934. memset(&lookup, 0, sizeof(lookup));
  1935. lookup.route = route;
  1936. xd = switch_find_xdomain(tb->root_switch, &lookup);
  1937. return tb_xdomain_get(xd);
  1938. }
  1939. EXPORT_SYMBOL_GPL(tb_xdomain_find_by_route);
  1940. bool tb_xdomain_handle_request(struct tb *tb, enum tb_cfg_pkg_type type,
  1941. const void *buf, size_t size)
  1942. {
  1943. const struct tb_protocol_handler *handler, *tmp;
  1944. const struct tb_xdp_header *hdr = buf;
  1945. unsigned int length;
  1946. int ret = 0;
  1947. /* We expect the packet is at least size of the header */
  1948. length = hdr->xd_hdr.length_sn & TB_XDOMAIN_LENGTH_MASK;
  1949. if (length != size / 4 - sizeof(hdr->xd_hdr) / 4)
  1950. return true;
  1951. if (length < sizeof(*hdr) / 4 - sizeof(hdr->xd_hdr) / 4)
  1952. return true;
  1953. /*
  1954. * Handle XDomain discovery protocol packets directly here. For
  1955. * other protocols (based on their UUID) we call registered
  1956. * handlers in turn.
  1957. */
  1958. if (uuid_equal(&hdr->uuid, &tb_xdp_uuid)) {
  1959. if (type == TB_CFG_PKG_XDOMAIN_REQ)
  1960. return tb_xdp_schedule_request(tb, hdr, size);
  1961. return false;
  1962. }
  1963. mutex_lock(&xdomain_lock);
  1964. list_for_each_entry_safe(handler, tmp, &protocol_handlers, list) {
  1965. if (!uuid_equal(&hdr->uuid, handler->uuid))
  1966. continue;
  1967. mutex_unlock(&xdomain_lock);
  1968. ret = handler->callback(buf, size, handler->data);
  1969. mutex_lock(&xdomain_lock);
  1970. if (ret)
  1971. break;
  1972. }
  1973. mutex_unlock(&xdomain_lock);
  1974. return ret > 0;
  1975. }
  1976. static int update_xdomain(struct device *dev, void *data)
  1977. {
  1978. struct tb_xdomain *xd;
  1979. xd = tb_to_xdomain(dev);
  1980. if (xd) {
  1981. queue_delayed_work(xd->tb->wq, &xd->properties_changed_work,
  1982. msecs_to_jiffies(50));
  1983. }
  1984. return 0;
  1985. }
  1986. static void update_all_xdomains(void)
  1987. {
  1988. bus_for_each_dev(&tb_bus_type, NULL, NULL, update_xdomain);
  1989. }
  1990. static bool remove_directory(const char *key, const struct tb_property_dir *dir)
  1991. {
  1992. struct tb_property *p;
  1993. p = tb_property_find(xdomain_property_dir, key,
  1994. TB_PROPERTY_TYPE_DIRECTORY);
  1995. if (p && p->value.dir == dir) {
  1996. tb_property_remove(p);
  1997. return true;
  1998. }
  1999. return false;
  2000. }
  2001. /**
  2002. * tb_register_property_dir() - Register property directory to the host
  2003. * @key: Key (name) of the directory to add
  2004. * @dir: Directory to add
  2005. *
  2006. * Service drivers can use this function to add new property directory
  2007. * to the host available properties. The other connected hosts are
  2008. * notified so they can re-read properties of this host if they are
  2009. * interested.
  2010. *
  2011. * Return: %0 on success and negative errno on failure
  2012. */
  2013. int tb_register_property_dir(const char *key, struct tb_property_dir *dir)
  2014. {
  2015. int ret;
  2016. if (WARN_ON(!xdomain_property_dir))
  2017. return -EAGAIN;
  2018. if (!key || strlen(key) > 8)
  2019. return -EINVAL;
  2020. mutex_lock(&xdomain_lock);
  2021. if (tb_property_find(xdomain_property_dir, key,
  2022. TB_PROPERTY_TYPE_DIRECTORY)) {
  2023. ret = -EEXIST;
  2024. goto err_unlock;
  2025. }
  2026. ret = tb_property_add_dir(xdomain_property_dir, key, dir);
  2027. if (ret)
  2028. goto err_unlock;
  2029. xdomain_property_block_gen++;
  2030. mutex_unlock(&xdomain_lock);
  2031. update_all_xdomains();
  2032. return 0;
  2033. err_unlock:
  2034. mutex_unlock(&xdomain_lock);
  2035. return ret;
  2036. }
  2037. EXPORT_SYMBOL_GPL(tb_register_property_dir);
  2038. /**
  2039. * tb_unregister_property_dir() - Removes property directory from host
  2040. * @key: Key (name) of the directory
  2041. * @dir: Directory to remove
  2042. *
  2043. * This will remove the existing directory from this host and notify the
  2044. * connected hosts about the change.
  2045. */
  2046. void tb_unregister_property_dir(const char *key, struct tb_property_dir *dir)
  2047. {
  2048. int ret = 0;
  2049. mutex_lock(&xdomain_lock);
  2050. if (remove_directory(key, dir))
  2051. xdomain_property_block_gen++;
  2052. mutex_unlock(&xdomain_lock);
  2053. if (!ret)
  2054. update_all_xdomains();
  2055. }
  2056. EXPORT_SYMBOL_GPL(tb_unregister_property_dir);
  2057. int tb_xdomain_init(void)
  2058. {
  2059. xdomain_property_dir = tb_property_create_dir(NULL);
  2060. if (!xdomain_property_dir)
  2061. return -ENOMEM;
  2062. /*
  2063. * Initialize standard set of properties without any service
  2064. * directories. Those will be added by service drivers
  2065. * themselves when they are loaded.
  2066. *
  2067. * Rest of the properties are filled dynamically based on these
  2068. * when the P2P connection is made.
  2069. */
  2070. tb_property_add_immediate(xdomain_property_dir, "vendorid",
  2071. PCI_VENDOR_ID_INTEL);
  2072. tb_property_add_text(xdomain_property_dir, "vendorid", "Intel Corp.");
  2073. tb_property_add_immediate(xdomain_property_dir, "deviceid", 0x1);
  2074. tb_property_add_immediate(xdomain_property_dir, "devicerv", 0x80000100);
  2075. xdomain_property_block_gen = get_random_u32();
  2076. return 0;
  2077. }
  2078. void tb_xdomain_exit(void)
  2079. {
  2080. tb_property_free_dir(xdomain_property_dir);
  2081. }