xdomain.c 66 KB

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