icm.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911
  1. /*
  2. * Internal Thunderbolt Connection Manager. This is a firmware running on
  3. * the Thunderbolt host controller performing most of the low-level
  4. * handling.
  5. *
  6. * Copyright (C) 2017, Intel Corporation
  7. * Authors: Michael Jamet <michael.jamet@intel.com>
  8. * Mika Westerberg <mika.westerberg@linux.intel.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/delay.h>
  15. #include <linux/mutex.h>
  16. #include <linux/pci.h>
  17. #include <linux/platform_data/x86/apple.h>
  18. #include <linux/sizes.h>
  19. #include <linux/slab.h>
  20. #include <linux/workqueue.h>
  21. #include "ctl.h"
  22. #include "nhi_regs.h"
  23. #include "tb.h"
  24. #define PCIE2CIO_CMD 0x30
  25. #define PCIE2CIO_CMD_TIMEOUT BIT(31)
  26. #define PCIE2CIO_CMD_START BIT(30)
  27. #define PCIE2CIO_CMD_WRITE BIT(21)
  28. #define PCIE2CIO_CMD_CS_MASK GENMASK(20, 19)
  29. #define PCIE2CIO_CMD_CS_SHIFT 19
  30. #define PCIE2CIO_CMD_PORT_MASK GENMASK(18, 13)
  31. #define PCIE2CIO_CMD_PORT_SHIFT 13
  32. #define PCIE2CIO_WRDATA 0x34
  33. #define PCIE2CIO_RDDATA 0x38
  34. #define PHY_PORT_CS1 0x37
  35. #define PHY_PORT_CS1_LINK_DISABLE BIT(14)
  36. #define PHY_PORT_CS1_LINK_STATE_MASK GENMASK(29, 26)
  37. #define PHY_PORT_CS1_LINK_STATE_SHIFT 26
  38. #define ICM_TIMEOUT 5000 /* ms */
  39. #define ICM_APPROVE_TIMEOUT 10000 /* ms */
  40. #define ICM_MAX_LINK 4
  41. #define ICM_MAX_DEPTH 6
  42. /**
  43. * struct icm - Internal connection manager private data
  44. * @request_lock: Makes sure only one message is send to ICM at time
  45. * @rescan_work: Work used to rescan the surviving switches after resume
  46. * @upstream_port: Pointer to the PCIe upstream port this host
  47. * controller is connected. This is only set for systems
  48. * where ICM needs to be started manually
  49. * @vnd_cap: Vendor defined capability where PCIe2CIO mailbox resides
  50. * (only set when @upstream_port is not %NULL)
  51. * @safe_mode: ICM is in safe mode
  52. * @max_boot_acl: Maximum number of preboot ACL entries (%0 if not supported)
  53. * @is_supported: Checks if we can support ICM on this controller
  54. * @get_mode: Read and return the ICM firmware mode (optional)
  55. * @get_route: Find a route string for given switch
  56. * @driver_ready: Send driver ready message to ICM
  57. * @device_connected: Handle device connected ICM message
  58. * @device_disconnected: Handle device disconnected ICM message
  59. * @xdomain_connected - Handle XDomain connected ICM message
  60. * @xdomain_disconnected - Handle XDomain disconnected ICM message
  61. */
  62. struct icm {
  63. struct mutex request_lock;
  64. struct delayed_work rescan_work;
  65. struct pci_dev *upstream_port;
  66. size_t max_boot_acl;
  67. int vnd_cap;
  68. bool safe_mode;
  69. bool (*is_supported)(struct tb *tb);
  70. int (*get_mode)(struct tb *tb);
  71. int (*get_route)(struct tb *tb, u8 link, u8 depth, u64 *route);
  72. int (*driver_ready)(struct tb *tb,
  73. enum tb_security_level *security_level,
  74. size_t *nboot_acl);
  75. void (*device_connected)(struct tb *tb,
  76. const struct icm_pkg_header *hdr);
  77. void (*device_disconnected)(struct tb *tb,
  78. const struct icm_pkg_header *hdr);
  79. void (*xdomain_connected)(struct tb *tb,
  80. const struct icm_pkg_header *hdr);
  81. void (*xdomain_disconnected)(struct tb *tb,
  82. const struct icm_pkg_header *hdr);
  83. };
  84. struct icm_notification {
  85. struct work_struct work;
  86. struct icm_pkg_header *pkg;
  87. struct tb *tb;
  88. };
  89. static inline struct tb *icm_to_tb(struct icm *icm)
  90. {
  91. return ((void *)icm - sizeof(struct tb));
  92. }
  93. static inline u8 phy_port_from_route(u64 route, u8 depth)
  94. {
  95. u8 link;
  96. link = depth ? route >> ((depth - 1) * 8) : route;
  97. return tb_phy_port_from_link(link);
  98. }
  99. static inline u8 dual_link_from_link(u8 link)
  100. {
  101. return link ? ((link - 1) ^ 0x01) + 1 : 0;
  102. }
  103. static inline u64 get_route(u32 route_hi, u32 route_lo)
  104. {
  105. return (u64)route_hi << 32 | route_lo;
  106. }
  107. static inline u64 get_parent_route(u64 route)
  108. {
  109. int depth = tb_route_length(route);
  110. return depth ? route & ~(0xffULL << (depth - 1) * TB_ROUTE_SHIFT) : 0;
  111. }
  112. static bool icm_match(const struct tb_cfg_request *req,
  113. const struct ctl_pkg *pkg)
  114. {
  115. const struct icm_pkg_header *res_hdr = pkg->buffer;
  116. const struct icm_pkg_header *req_hdr = req->request;
  117. if (pkg->frame.eof != req->response_type)
  118. return false;
  119. if (res_hdr->code != req_hdr->code)
  120. return false;
  121. return true;
  122. }
  123. static bool icm_copy(struct tb_cfg_request *req, const struct ctl_pkg *pkg)
  124. {
  125. const struct icm_pkg_header *hdr = pkg->buffer;
  126. if (hdr->packet_id < req->npackets) {
  127. size_t offset = hdr->packet_id * req->response_size;
  128. memcpy(req->response + offset, pkg->buffer, req->response_size);
  129. }
  130. return hdr->packet_id == hdr->total_packets - 1;
  131. }
  132. static int icm_request(struct tb *tb, const void *request, size_t request_size,
  133. void *response, size_t response_size, size_t npackets,
  134. unsigned int timeout_msec)
  135. {
  136. struct icm *icm = tb_priv(tb);
  137. int retries = 3;
  138. do {
  139. struct tb_cfg_request *req;
  140. struct tb_cfg_result res;
  141. req = tb_cfg_request_alloc();
  142. if (!req)
  143. return -ENOMEM;
  144. req->match = icm_match;
  145. req->copy = icm_copy;
  146. req->request = request;
  147. req->request_size = request_size;
  148. req->request_type = TB_CFG_PKG_ICM_CMD;
  149. req->response = response;
  150. req->npackets = npackets;
  151. req->response_size = response_size;
  152. req->response_type = TB_CFG_PKG_ICM_RESP;
  153. mutex_lock(&icm->request_lock);
  154. res = tb_cfg_request_sync(tb->ctl, req, timeout_msec);
  155. mutex_unlock(&icm->request_lock);
  156. tb_cfg_request_put(req);
  157. if (res.err != -ETIMEDOUT)
  158. return res.err == 1 ? -EIO : res.err;
  159. usleep_range(20, 50);
  160. } while (retries--);
  161. return -ETIMEDOUT;
  162. }
  163. static bool icm_fr_is_supported(struct tb *tb)
  164. {
  165. return !x86_apple_machine;
  166. }
  167. static inline int icm_fr_get_switch_index(u32 port)
  168. {
  169. int index;
  170. if ((port & ICM_PORT_TYPE_MASK) != TB_TYPE_PORT)
  171. return 0;
  172. index = port >> ICM_PORT_INDEX_SHIFT;
  173. return index != 0xff ? index : 0;
  174. }
  175. static int icm_fr_get_route(struct tb *tb, u8 link, u8 depth, u64 *route)
  176. {
  177. struct icm_fr_pkg_get_topology_response *switches, *sw;
  178. struct icm_fr_pkg_get_topology request = {
  179. .hdr = { .code = ICM_GET_TOPOLOGY },
  180. };
  181. size_t npackets = ICM_GET_TOPOLOGY_PACKETS;
  182. int ret, index;
  183. u8 i;
  184. switches = kcalloc(npackets, sizeof(*switches), GFP_KERNEL);
  185. if (!switches)
  186. return -ENOMEM;
  187. ret = icm_request(tb, &request, sizeof(request), switches,
  188. sizeof(*switches), npackets, ICM_TIMEOUT);
  189. if (ret)
  190. goto err_free;
  191. sw = &switches[0];
  192. index = icm_fr_get_switch_index(sw->ports[link]);
  193. if (!index) {
  194. ret = -ENODEV;
  195. goto err_free;
  196. }
  197. sw = &switches[index];
  198. for (i = 1; i < depth; i++) {
  199. unsigned int j;
  200. if (!(sw->first_data & ICM_SWITCH_USED)) {
  201. ret = -ENODEV;
  202. goto err_free;
  203. }
  204. for (j = 0; j < ARRAY_SIZE(sw->ports); j++) {
  205. index = icm_fr_get_switch_index(sw->ports[j]);
  206. if (index > sw->switch_index) {
  207. sw = &switches[index];
  208. break;
  209. }
  210. }
  211. }
  212. *route = get_route(sw->route_hi, sw->route_lo);
  213. err_free:
  214. kfree(switches);
  215. return ret;
  216. }
  217. static int
  218. icm_fr_driver_ready(struct tb *tb, enum tb_security_level *security_level,
  219. size_t *nboot_acl)
  220. {
  221. struct icm_fr_pkg_driver_ready_response reply;
  222. struct icm_pkg_driver_ready request = {
  223. .hdr.code = ICM_DRIVER_READY,
  224. };
  225. int ret;
  226. memset(&reply, 0, sizeof(reply));
  227. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  228. 1, ICM_TIMEOUT);
  229. if (ret)
  230. return ret;
  231. if (security_level)
  232. *security_level = reply.security_level & ICM_FR_SLEVEL_MASK;
  233. return 0;
  234. }
  235. static int icm_fr_approve_switch(struct tb *tb, struct tb_switch *sw)
  236. {
  237. struct icm_fr_pkg_approve_device request;
  238. struct icm_fr_pkg_approve_device reply;
  239. int ret;
  240. memset(&request, 0, sizeof(request));
  241. memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
  242. request.hdr.code = ICM_APPROVE_DEVICE;
  243. request.connection_id = sw->connection_id;
  244. request.connection_key = sw->connection_key;
  245. memset(&reply, 0, sizeof(reply));
  246. /* Use larger timeout as establishing tunnels can take some time */
  247. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  248. 1, ICM_APPROVE_TIMEOUT);
  249. if (ret)
  250. return ret;
  251. if (reply.hdr.flags & ICM_FLAGS_ERROR) {
  252. tb_warn(tb, "PCIe tunnel creation failed\n");
  253. return -EIO;
  254. }
  255. return 0;
  256. }
  257. static int icm_fr_add_switch_key(struct tb *tb, struct tb_switch *sw)
  258. {
  259. struct icm_fr_pkg_add_device_key request;
  260. struct icm_fr_pkg_add_device_key_response reply;
  261. int ret;
  262. memset(&request, 0, sizeof(request));
  263. memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
  264. request.hdr.code = ICM_ADD_DEVICE_KEY;
  265. request.connection_id = sw->connection_id;
  266. request.connection_key = sw->connection_key;
  267. memcpy(request.key, sw->key, TB_SWITCH_KEY_SIZE);
  268. memset(&reply, 0, sizeof(reply));
  269. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  270. 1, ICM_TIMEOUT);
  271. if (ret)
  272. return ret;
  273. if (reply.hdr.flags & ICM_FLAGS_ERROR) {
  274. tb_warn(tb, "Adding key to switch failed\n");
  275. return -EIO;
  276. }
  277. return 0;
  278. }
  279. static int icm_fr_challenge_switch_key(struct tb *tb, struct tb_switch *sw,
  280. const u8 *challenge, u8 *response)
  281. {
  282. struct icm_fr_pkg_challenge_device request;
  283. struct icm_fr_pkg_challenge_device_response reply;
  284. int ret;
  285. memset(&request, 0, sizeof(request));
  286. memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
  287. request.hdr.code = ICM_CHALLENGE_DEVICE;
  288. request.connection_id = sw->connection_id;
  289. request.connection_key = sw->connection_key;
  290. memcpy(request.challenge, challenge, TB_SWITCH_KEY_SIZE);
  291. memset(&reply, 0, sizeof(reply));
  292. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  293. 1, ICM_TIMEOUT);
  294. if (ret)
  295. return ret;
  296. if (reply.hdr.flags & ICM_FLAGS_ERROR)
  297. return -EKEYREJECTED;
  298. if (reply.hdr.flags & ICM_FLAGS_NO_KEY)
  299. return -ENOKEY;
  300. memcpy(response, reply.response, TB_SWITCH_KEY_SIZE);
  301. return 0;
  302. }
  303. static int icm_fr_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
  304. {
  305. struct icm_fr_pkg_approve_xdomain_response reply;
  306. struct icm_fr_pkg_approve_xdomain request;
  307. int ret;
  308. memset(&request, 0, sizeof(request));
  309. request.hdr.code = ICM_APPROVE_XDOMAIN;
  310. request.link_info = xd->depth << ICM_LINK_INFO_DEPTH_SHIFT | xd->link;
  311. memcpy(&request.remote_uuid, xd->remote_uuid, sizeof(*xd->remote_uuid));
  312. request.transmit_path = xd->transmit_path;
  313. request.transmit_ring = xd->transmit_ring;
  314. request.receive_path = xd->receive_path;
  315. request.receive_ring = xd->receive_ring;
  316. memset(&reply, 0, sizeof(reply));
  317. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  318. 1, ICM_TIMEOUT);
  319. if (ret)
  320. return ret;
  321. if (reply.hdr.flags & ICM_FLAGS_ERROR)
  322. return -EIO;
  323. return 0;
  324. }
  325. static int icm_fr_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
  326. {
  327. u8 phy_port;
  328. u8 cmd;
  329. phy_port = tb_phy_port_from_link(xd->link);
  330. if (phy_port == 0)
  331. cmd = NHI_MAILBOX_DISCONNECT_PA;
  332. else
  333. cmd = NHI_MAILBOX_DISCONNECT_PB;
  334. nhi_mailbox_cmd(tb->nhi, cmd, 1);
  335. usleep_range(10, 50);
  336. nhi_mailbox_cmd(tb->nhi, cmd, 2);
  337. return 0;
  338. }
  339. static void add_switch(struct tb_switch *parent_sw, u64 route,
  340. const uuid_t *uuid, u8 connection_id, u8 connection_key,
  341. u8 link, u8 depth, enum tb_security_level security_level,
  342. bool authorized, bool boot)
  343. {
  344. struct tb_switch *sw;
  345. sw = tb_switch_alloc(parent_sw->tb, &parent_sw->dev, route);
  346. if (!sw)
  347. return;
  348. sw->uuid = kmemdup(uuid, sizeof(*uuid), GFP_KERNEL);
  349. sw->connection_id = connection_id;
  350. sw->connection_key = connection_key;
  351. sw->link = link;
  352. sw->depth = depth;
  353. sw->authorized = authorized;
  354. sw->security_level = security_level;
  355. sw->boot = boot;
  356. /* Link the two switches now */
  357. tb_port_at(route, parent_sw)->remote = tb_upstream_port(sw);
  358. tb_upstream_port(sw)->remote = tb_port_at(route, parent_sw);
  359. if (tb_switch_add(sw)) {
  360. tb_port_at(tb_route(sw), parent_sw)->remote = NULL;
  361. tb_switch_put(sw);
  362. return;
  363. }
  364. }
  365. static void update_switch(struct tb_switch *parent_sw, struct tb_switch *sw,
  366. u64 route, u8 connection_id, u8 connection_key,
  367. u8 link, u8 depth, bool boot)
  368. {
  369. /* Disconnect from parent */
  370. tb_port_at(tb_route(sw), parent_sw)->remote = NULL;
  371. /* Re-connect via updated port*/
  372. tb_port_at(route, parent_sw)->remote = tb_upstream_port(sw);
  373. /* Update with the new addressing information */
  374. sw->config.route_hi = upper_32_bits(route);
  375. sw->config.route_lo = lower_32_bits(route);
  376. sw->connection_id = connection_id;
  377. sw->connection_key = connection_key;
  378. sw->link = link;
  379. sw->depth = depth;
  380. sw->boot = boot;
  381. /* This switch still exists */
  382. sw->is_unplugged = false;
  383. }
  384. static void remove_switch(struct tb_switch *sw)
  385. {
  386. struct tb_switch *parent_sw;
  387. parent_sw = tb_to_switch(sw->dev.parent);
  388. tb_port_at(tb_route(sw), parent_sw)->remote = NULL;
  389. tb_switch_remove(sw);
  390. }
  391. static void add_xdomain(struct tb_switch *sw, u64 route,
  392. const uuid_t *local_uuid, const uuid_t *remote_uuid,
  393. u8 link, u8 depth)
  394. {
  395. struct tb_xdomain *xd;
  396. xd = tb_xdomain_alloc(sw->tb, &sw->dev, route, local_uuid, remote_uuid);
  397. if (!xd)
  398. return;
  399. xd->link = link;
  400. xd->depth = depth;
  401. tb_port_at(route, sw)->xdomain = xd;
  402. tb_xdomain_add(xd);
  403. }
  404. static void update_xdomain(struct tb_xdomain *xd, u64 route, u8 link)
  405. {
  406. xd->link = link;
  407. xd->route = route;
  408. xd->is_unplugged = false;
  409. }
  410. static void remove_xdomain(struct tb_xdomain *xd)
  411. {
  412. struct tb_switch *sw;
  413. sw = tb_to_switch(xd->dev.parent);
  414. tb_port_at(xd->route, sw)->xdomain = NULL;
  415. tb_xdomain_remove(xd);
  416. }
  417. static void
  418. icm_fr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr)
  419. {
  420. const struct icm_fr_event_device_connected *pkg =
  421. (const struct icm_fr_event_device_connected *)hdr;
  422. enum tb_security_level security_level;
  423. struct tb_switch *sw, *parent_sw;
  424. struct icm *icm = tb_priv(tb);
  425. bool authorized = false;
  426. struct tb_xdomain *xd;
  427. u8 link, depth;
  428. bool boot;
  429. u64 route;
  430. int ret;
  431. link = pkg->link_info & ICM_LINK_INFO_LINK_MASK;
  432. depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >>
  433. ICM_LINK_INFO_DEPTH_SHIFT;
  434. authorized = pkg->link_info & ICM_LINK_INFO_APPROVED;
  435. security_level = (pkg->hdr.flags & ICM_FLAGS_SLEVEL_MASK) >>
  436. ICM_FLAGS_SLEVEL_SHIFT;
  437. boot = pkg->link_info & ICM_LINK_INFO_BOOT;
  438. if (pkg->link_info & ICM_LINK_INFO_REJECTED) {
  439. tb_info(tb, "switch at %u.%u was rejected by ICM firmware because topology limit exceeded\n",
  440. link, depth);
  441. return;
  442. }
  443. ret = icm->get_route(tb, link, depth, &route);
  444. if (ret) {
  445. tb_err(tb, "failed to find route string for switch at %u.%u\n",
  446. link, depth);
  447. return;
  448. }
  449. sw = tb_switch_find_by_uuid(tb, &pkg->ep_uuid);
  450. if (sw) {
  451. u8 phy_port, sw_phy_port;
  452. parent_sw = tb_to_switch(sw->dev.parent);
  453. sw_phy_port = phy_port_from_route(tb_route(sw), sw->depth);
  454. phy_port = phy_port_from_route(route, depth);
  455. /*
  456. * On resume ICM will send us connected events for the
  457. * devices that still are present. However, that
  458. * information might have changed for example by the
  459. * fact that a switch on a dual-link connection might
  460. * have been enumerated using the other link now. Make
  461. * sure our book keeping matches that.
  462. */
  463. if (sw->depth == depth && sw_phy_port == phy_port &&
  464. !!sw->authorized == authorized) {
  465. update_switch(parent_sw, sw, route, pkg->connection_id,
  466. pkg->connection_key, link, depth, boot);
  467. tb_switch_put(sw);
  468. return;
  469. }
  470. /*
  471. * User connected the same switch to another physical
  472. * port or to another part of the topology. Remove the
  473. * existing switch now before adding the new one.
  474. */
  475. remove_switch(sw);
  476. tb_switch_put(sw);
  477. }
  478. /*
  479. * If the switch was not found by UUID, look for a switch on
  480. * same physical port (taking possible link aggregation into
  481. * account) and depth. If we found one it is definitely a stale
  482. * one so remove it first.
  483. */
  484. sw = tb_switch_find_by_link_depth(tb, link, depth);
  485. if (!sw) {
  486. u8 dual_link;
  487. dual_link = dual_link_from_link(link);
  488. if (dual_link)
  489. sw = tb_switch_find_by_link_depth(tb, dual_link, depth);
  490. }
  491. if (sw) {
  492. remove_switch(sw);
  493. tb_switch_put(sw);
  494. }
  495. /* Remove existing XDomain connection if found */
  496. xd = tb_xdomain_find_by_link_depth(tb, link, depth);
  497. if (xd) {
  498. remove_xdomain(xd);
  499. tb_xdomain_put(xd);
  500. }
  501. parent_sw = tb_switch_find_by_link_depth(tb, link, depth - 1);
  502. if (!parent_sw) {
  503. tb_err(tb, "failed to find parent switch for %u.%u\n",
  504. link, depth);
  505. return;
  506. }
  507. add_switch(parent_sw, route, &pkg->ep_uuid, pkg->connection_id,
  508. pkg->connection_key, link, depth, security_level,
  509. authorized, boot);
  510. tb_switch_put(parent_sw);
  511. }
  512. static void
  513. icm_fr_device_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
  514. {
  515. const struct icm_fr_event_device_disconnected *pkg =
  516. (const struct icm_fr_event_device_disconnected *)hdr;
  517. struct tb_switch *sw;
  518. u8 link, depth;
  519. link = pkg->link_info & ICM_LINK_INFO_LINK_MASK;
  520. depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >>
  521. ICM_LINK_INFO_DEPTH_SHIFT;
  522. if (link > ICM_MAX_LINK || depth > ICM_MAX_DEPTH) {
  523. tb_warn(tb, "invalid topology %u.%u, ignoring\n", link, depth);
  524. return;
  525. }
  526. sw = tb_switch_find_by_link_depth(tb, link, depth);
  527. if (!sw) {
  528. tb_warn(tb, "no switch exists at %u.%u, ignoring\n", link,
  529. depth);
  530. return;
  531. }
  532. remove_switch(sw);
  533. tb_switch_put(sw);
  534. }
  535. static void
  536. icm_fr_xdomain_connected(struct tb *tb, const struct icm_pkg_header *hdr)
  537. {
  538. const struct icm_fr_event_xdomain_connected *pkg =
  539. (const struct icm_fr_event_xdomain_connected *)hdr;
  540. struct tb_xdomain *xd;
  541. struct tb_switch *sw;
  542. u8 link, depth;
  543. bool approved;
  544. u64 route;
  545. /*
  546. * After NVM upgrade adding root switch device fails because we
  547. * initiated reset. During that time ICM might still send
  548. * XDomain connected message which we ignore here.
  549. */
  550. if (!tb->root_switch)
  551. return;
  552. link = pkg->link_info & ICM_LINK_INFO_LINK_MASK;
  553. depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >>
  554. ICM_LINK_INFO_DEPTH_SHIFT;
  555. approved = pkg->link_info & ICM_LINK_INFO_APPROVED;
  556. if (link > ICM_MAX_LINK || depth > ICM_MAX_DEPTH) {
  557. tb_warn(tb, "invalid topology %u.%u, ignoring\n", link, depth);
  558. return;
  559. }
  560. route = get_route(pkg->local_route_hi, pkg->local_route_lo);
  561. xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid);
  562. if (xd) {
  563. u8 xd_phy_port, phy_port;
  564. xd_phy_port = phy_port_from_route(xd->route, xd->depth);
  565. phy_port = phy_port_from_route(route, depth);
  566. if (xd->depth == depth && xd_phy_port == phy_port) {
  567. update_xdomain(xd, route, link);
  568. tb_xdomain_put(xd);
  569. return;
  570. }
  571. /*
  572. * If we find an existing XDomain connection remove it
  573. * now. We need to go through login handshake and
  574. * everything anyway to be able to re-establish the
  575. * connection.
  576. */
  577. remove_xdomain(xd);
  578. tb_xdomain_put(xd);
  579. }
  580. /*
  581. * Look if there already exists an XDomain in the same place
  582. * than the new one and in that case remove it because it is
  583. * most likely another host that got disconnected.
  584. */
  585. xd = tb_xdomain_find_by_link_depth(tb, link, depth);
  586. if (!xd) {
  587. u8 dual_link;
  588. dual_link = dual_link_from_link(link);
  589. if (dual_link)
  590. xd = tb_xdomain_find_by_link_depth(tb, dual_link,
  591. depth);
  592. }
  593. if (xd) {
  594. remove_xdomain(xd);
  595. tb_xdomain_put(xd);
  596. }
  597. /*
  598. * If the user disconnected a switch during suspend and
  599. * connected another host to the same port, remove the switch
  600. * first.
  601. */
  602. sw = get_switch_at_route(tb->root_switch, route);
  603. if (sw)
  604. remove_switch(sw);
  605. sw = tb_switch_find_by_link_depth(tb, link, depth);
  606. if (!sw) {
  607. tb_warn(tb, "no switch exists at %u.%u, ignoring\n", link,
  608. depth);
  609. return;
  610. }
  611. add_xdomain(sw, route, &pkg->local_uuid, &pkg->remote_uuid, link,
  612. depth);
  613. tb_switch_put(sw);
  614. }
  615. static void
  616. icm_fr_xdomain_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
  617. {
  618. const struct icm_fr_event_xdomain_disconnected *pkg =
  619. (const struct icm_fr_event_xdomain_disconnected *)hdr;
  620. struct tb_xdomain *xd;
  621. /*
  622. * If the connection is through one or multiple devices, the
  623. * XDomain device is removed along with them so it is fine if we
  624. * cannot find it here.
  625. */
  626. xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid);
  627. if (xd) {
  628. remove_xdomain(xd);
  629. tb_xdomain_put(xd);
  630. }
  631. }
  632. static int
  633. icm_tr_driver_ready(struct tb *tb, enum tb_security_level *security_level,
  634. size_t *nboot_acl)
  635. {
  636. struct icm_tr_pkg_driver_ready_response reply;
  637. struct icm_pkg_driver_ready request = {
  638. .hdr.code = ICM_DRIVER_READY,
  639. };
  640. int ret;
  641. memset(&reply, 0, sizeof(reply));
  642. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  643. 1, 20000);
  644. if (ret)
  645. return ret;
  646. if (security_level)
  647. *security_level = reply.info & ICM_TR_INFO_SLEVEL_MASK;
  648. if (nboot_acl)
  649. *nboot_acl = (reply.info & ICM_TR_INFO_BOOT_ACL_MASK) >>
  650. ICM_TR_INFO_BOOT_ACL_SHIFT;
  651. return 0;
  652. }
  653. static int icm_tr_approve_switch(struct tb *tb, struct tb_switch *sw)
  654. {
  655. struct icm_tr_pkg_approve_device request;
  656. struct icm_tr_pkg_approve_device reply;
  657. int ret;
  658. memset(&request, 0, sizeof(request));
  659. memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
  660. request.hdr.code = ICM_APPROVE_DEVICE;
  661. request.route_lo = sw->config.route_lo;
  662. request.route_hi = sw->config.route_hi;
  663. request.connection_id = sw->connection_id;
  664. memset(&reply, 0, sizeof(reply));
  665. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  666. 1, ICM_APPROVE_TIMEOUT);
  667. if (ret)
  668. return ret;
  669. if (reply.hdr.flags & ICM_FLAGS_ERROR) {
  670. tb_warn(tb, "PCIe tunnel creation failed\n");
  671. return -EIO;
  672. }
  673. return 0;
  674. }
  675. static int icm_tr_add_switch_key(struct tb *tb, struct tb_switch *sw)
  676. {
  677. struct icm_tr_pkg_add_device_key_response reply;
  678. struct icm_tr_pkg_add_device_key request;
  679. int ret;
  680. memset(&request, 0, sizeof(request));
  681. memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
  682. request.hdr.code = ICM_ADD_DEVICE_KEY;
  683. request.route_lo = sw->config.route_lo;
  684. request.route_hi = sw->config.route_hi;
  685. request.connection_id = sw->connection_id;
  686. memcpy(request.key, sw->key, TB_SWITCH_KEY_SIZE);
  687. memset(&reply, 0, sizeof(reply));
  688. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  689. 1, ICM_TIMEOUT);
  690. if (ret)
  691. return ret;
  692. if (reply.hdr.flags & ICM_FLAGS_ERROR) {
  693. tb_warn(tb, "Adding key to switch failed\n");
  694. return -EIO;
  695. }
  696. return 0;
  697. }
  698. static int icm_tr_challenge_switch_key(struct tb *tb, struct tb_switch *sw,
  699. const u8 *challenge, u8 *response)
  700. {
  701. struct icm_tr_pkg_challenge_device_response reply;
  702. struct icm_tr_pkg_challenge_device request;
  703. int ret;
  704. memset(&request, 0, sizeof(request));
  705. memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
  706. request.hdr.code = ICM_CHALLENGE_DEVICE;
  707. request.route_lo = sw->config.route_lo;
  708. request.route_hi = sw->config.route_hi;
  709. request.connection_id = sw->connection_id;
  710. memcpy(request.challenge, challenge, TB_SWITCH_KEY_SIZE);
  711. memset(&reply, 0, sizeof(reply));
  712. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  713. 1, ICM_TIMEOUT);
  714. if (ret)
  715. return ret;
  716. if (reply.hdr.flags & ICM_FLAGS_ERROR)
  717. return -EKEYREJECTED;
  718. if (reply.hdr.flags & ICM_FLAGS_NO_KEY)
  719. return -ENOKEY;
  720. memcpy(response, reply.response, TB_SWITCH_KEY_SIZE);
  721. return 0;
  722. }
  723. static int icm_tr_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
  724. {
  725. struct icm_tr_pkg_approve_xdomain_response reply;
  726. struct icm_tr_pkg_approve_xdomain request;
  727. int ret;
  728. memset(&request, 0, sizeof(request));
  729. request.hdr.code = ICM_APPROVE_XDOMAIN;
  730. request.route_hi = upper_32_bits(xd->route);
  731. request.route_lo = lower_32_bits(xd->route);
  732. request.transmit_path = xd->transmit_path;
  733. request.transmit_ring = xd->transmit_ring;
  734. request.receive_path = xd->receive_path;
  735. request.receive_ring = xd->receive_ring;
  736. memcpy(&request.remote_uuid, xd->remote_uuid, sizeof(*xd->remote_uuid));
  737. memset(&reply, 0, sizeof(reply));
  738. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  739. 1, ICM_TIMEOUT);
  740. if (ret)
  741. return ret;
  742. if (reply.hdr.flags & ICM_FLAGS_ERROR)
  743. return -EIO;
  744. return 0;
  745. }
  746. static int icm_tr_xdomain_tear_down(struct tb *tb, struct tb_xdomain *xd,
  747. int stage)
  748. {
  749. struct icm_tr_pkg_disconnect_xdomain_response reply;
  750. struct icm_tr_pkg_disconnect_xdomain request;
  751. int ret;
  752. memset(&request, 0, sizeof(request));
  753. request.hdr.code = ICM_DISCONNECT_XDOMAIN;
  754. request.stage = stage;
  755. request.route_hi = upper_32_bits(xd->route);
  756. request.route_lo = lower_32_bits(xd->route);
  757. memcpy(&request.remote_uuid, xd->remote_uuid, sizeof(*xd->remote_uuid));
  758. memset(&reply, 0, sizeof(reply));
  759. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  760. 1, ICM_TIMEOUT);
  761. if (ret)
  762. return ret;
  763. if (reply.hdr.flags & ICM_FLAGS_ERROR)
  764. return -EIO;
  765. return 0;
  766. }
  767. static int icm_tr_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
  768. {
  769. int ret;
  770. ret = icm_tr_xdomain_tear_down(tb, xd, 1);
  771. if (ret)
  772. return ret;
  773. usleep_range(10, 50);
  774. return icm_tr_xdomain_tear_down(tb, xd, 2);
  775. }
  776. static void
  777. icm_tr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr)
  778. {
  779. const struct icm_tr_event_device_connected *pkg =
  780. (const struct icm_tr_event_device_connected *)hdr;
  781. enum tb_security_level security_level;
  782. struct tb_switch *sw, *parent_sw;
  783. struct tb_xdomain *xd;
  784. bool authorized, boot;
  785. u64 route;
  786. /*
  787. * Currently we don't use the QoS information coming with the
  788. * device connected message so simply just ignore that extra
  789. * packet for now.
  790. */
  791. if (pkg->hdr.packet_id)
  792. return;
  793. /*
  794. * After NVM upgrade adding root switch device fails because we
  795. * initiated reset. During that time ICM might still send device
  796. * connected message which we ignore here.
  797. */
  798. if (!tb->root_switch)
  799. return;
  800. route = get_route(pkg->route_hi, pkg->route_lo);
  801. authorized = pkg->link_info & ICM_LINK_INFO_APPROVED;
  802. security_level = (pkg->hdr.flags & ICM_FLAGS_SLEVEL_MASK) >>
  803. ICM_FLAGS_SLEVEL_SHIFT;
  804. boot = pkg->link_info & ICM_LINK_INFO_BOOT;
  805. if (pkg->link_info & ICM_LINK_INFO_REJECTED) {
  806. tb_info(tb, "switch at %llx was rejected by ICM firmware because topology limit exceeded\n",
  807. route);
  808. return;
  809. }
  810. sw = tb_switch_find_by_uuid(tb, &pkg->ep_uuid);
  811. if (sw) {
  812. /* Update the switch if it is still in the same place */
  813. if (tb_route(sw) == route && !!sw->authorized == authorized) {
  814. parent_sw = tb_to_switch(sw->dev.parent);
  815. update_switch(parent_sw, sw, route, pkg->connection_id,
  816. 0, 0, 0, boot);
  817. tb_switch_put(sw);
  818. return;
  819. }
  820. remove_switch(sw);
  821. tb_switch_put(sw);
  822. }
  823. /* Another switch with the same address */
  824. sw = tb_switch_find_by_route(tb, route);
  825. if (sw) {
  826. remove_switch(sw);
  827. tb_switch_put(sw);
  828. }
  829. /* XDomain connection with the same address */
  830. xd = tb_xdomain_find_by_route(tb, route);
  831. if (xd) {
  832. remove_xdomain(xd);
  833. tb_xdomain_put(xd);
  834. }
  835. parent_sw = tb_switch_find_by_route(tb, get_parent_route(route));
  836. if (!parent_sw) {
  837. tb_err(tb, "failed to find parent switch for %llx\n", route);
  838. return;
  839. }
  840. add_switch(parent_sw, route, &pkg->ep_uuid, pkg->connection_id,
  841. 0, 0, 0, security_level, authorized, boot);
  842. tb_switch_put(parent_sw);
  843. }
  844. static void
  845. icm_tr_device_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
  846. {
  847. const struct icm_tr_event_device_disconnected *pkg =
  848. (const struct icm_tr_event_device_disconnected *)hdr;
  849. struct tb_switch *sw;
  850. u64 route;
  851. route = get_route(pkg->route_hi, pkg->route_lo);
  852. sw = tb_switch_find_by_route(tb, route);
  853. if (!sw) {
  854. tb_warn(tb, "no switch exists at %llx, ignoring\n", route);
  855. return;
  856. }
  857. remove_switch(sw);
  858. tb_switch_put(sw);
  859. }
  860. static void
  861. icm_tr_xdomain_connected(struct tb *tb, const struct icm_pkg_header *hdr)
  862. {
  863. const struct icm_tr_event_xdomain_connected *pkg =
  864. (const struct icm_tr_event_xdomain_connected *)hdr;
  865. struct tb_xdomain *xd;
  866. struct tb_switch *sw;
  867. u64 route;
  868. if (!tb->root_switch)
  869. return;
  870. route = get_route(pkg->local_route_hi, pkg->local_route_lo);
  871. xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid);
  872. if (xd) {
  873. if (xd->route == route) {
  874. update_xdomain(xd, route, 0);
  875. tb_xdomain_put(xd);
  876. return;
  877. }
  878. remove_xdomain(xd);
  879. tb_xdomain_put(xd);
  880. }
  881. /* An existing xdomain with the same address */
  882. xd = tb_xdomain_find_by_route(tb, route);
  883. if (xd) {
  884. remove_xdomain(xd);
  885. tb_xdomain_put(xd);
  886. }
  887. /*
  888. * If the user disconnected a switch during suspend and
  889. * connected another host to the same port, remove the switch
  890. * first.
  891. */
  892. sw = get_switch_at_route(tb->root_switch, route);
  893. if (sw)
  894. remove_switch(sw);
  895. sw = tb_switch_find_by_route(tb, get_parent_route(route));
  896. if (!sw) {
  897. tb_warn(tb, "no switch exists at %llx, ignoring\n", route);
  898. return;
  899. }
  900. add_xdomain(sw, route, &pkg->local_uuid, &pkg->remote_uuid, 0, 0);
  901. tb_switch_put(sw);
  902. }
  903. static void
  904. icm_tr_xdomain_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
  905. {
  906. const struct icm_tr_event_xdomain_disconnected *pkg =
  907. (const struct icm_tr_event_xdomain_disconnected *)hdr;
  908. struct tb_xdomain *xd;
  909. u64 route;
  910. route = get_route(pkg->route_hi, pkg->route_lo);
  911. xd = tb_xdomain_find_by_route(tb, route);
  912. if (xd) {
  913. remove_xdomain(xd);
  914. tb_xdomain_put(xd);
  915. }
  916. }
  917. static struct pci_dev *get_upstream_port(struct pci_dev *pdev)
  918. {
  919. struct pci_dev *parent;
  920. parent = pci_upstream_bridge(pdev);
  921. while (parent) {
  922. if (!pci_is_pcie(parent))
  923. return NULL;
  924. if (pci_pcie_type(parent) == PCI_EXP_TYPE_UPSTREAM)
  925. break;
  926. parent = pci_upstream_bridge(parent);
  927. }
  928. if (!parent)
  929. return NULL;
  930. switch (parent->device) {
  931. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE:
  932. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE:
  933. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE:
  934. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE:
  935. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE:
  936. return parent;
  937. }
  938. return NULL;
  939. }
  940. static bool icm_ar_is_supported(struct tb *tb)
  941. {
  942. struct pci_dev *upstream_port;
  943. struct icm *icm = tb_priv(tb);
  944. /*
  945. * Starting from Alpine Ridge we can use ICM on Apple machines
  946. * as well. We just need to reset and re-enable it first.
  947. */
  948. if (!x86_apple_machine)
  949. return true;
  950. /*
  951. * Find the upstream PCIe port in case we need to do reset
  952. * through its vendor specific registers.
  953. */
  954. upstream_port = get_upstream_port(tb->nhi->pdev);
  955. if (upstream_port) {
  956. int cap;
  957. cap = pci_find_ext_capability(upstream_port,
  958. PCI_EXT_CAP_ID_VNDR);
  959. if (cap > 0) {
  960. icm->upstream_port = upstream_port;
  961. icm->vnd_cap = cap;
  962. return true;
  963. }
  964. }
  965. return false;
  966. }
  967. static int icm_ar_get_mode(struct tb *tb)
  968. {
  969. struct tb_nhi *nhi = tb->nhi;
  970. int retries = 60;
  971. u32 val;
  972. do {
  973. val = ioread32(nhi->iobase + REG_FW_STS);
  974. if (val & REG_FW_STS_NVM_AUTH_DONE)
  975. break;
  976. msleep(50);
  977. } while (--retries);
  978. if (!retries) {
  979. dev_err(&nhi->pdev->dev, "ICM firmware not authenticated\n");
  980. return -ENODEV;
  981. }
  982. return nhi_mailbox_mode(nhi);
  983. }
  984. static int
  985. icm_ar_driver_ready(struct tb *tb, enum tb_security_level *security_level,
  986. size_t *nboot_acl)
  987. {
  988. struct icm_ar_pkg_driver_ready_response reply;
  989. struct icm_pkg_driver_ready request = {
  990. .hdr.code = ICM_DRIVER_READY,
  991. };
  992. int ret;
  993. memset(&reply, 0, sizeof(reply));
  994. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  995. 1, ICM_TIMEOUT);
  996. if (ret)
  997. return ret;
  998. if (security_level)
  999. *security_level = reply.info & ICM_AR_INFO_SLEVEL_MASK;
  1000. if (nboot_acl && (reply.info & ICM_AR_INFO_BOOT_ACL_SUPPORTED))
  1001. *nboot_acl = (reply.info & ICM_AR_INFO_BOOT_ACL_MASK) >>
  1002. ICM_AR_INFO_BOOT_ACL_SHIFT;
  1003. return 0;
  1004. }
  1005. static int icm_ar_get_route(struct tb *tb, u8 link, u8 depth, u64 *route)
  1006. {
  1007. struct icm_ar_pkg_get_route_response reply;
  1008. struct icm_ar_pkg_get_route request = {
  1009. .hdr = { .code = ICM_GET_ROUTE },
  1010. .link_info = depth << ICM_LINK_INFO_DEPTH_SHIFT | link,
  1011. };
  1012. int ret;
  1013. memset(&reply, 0, sizeof(reply));
  1014. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  1015. 1, ICM_TIMEOUT);
  1016. if (ret)
  1017. return ret;
  1018. if (reply.hdr.flags & ICM_FLAGS_ERROR)
  1019. return -EIO;
  1020. *route = get_route(reply.route_hi, reply.route_lo);
  1021. return 0;
  1022. }
  1023. static int icm_ar_get_boot_acl(struct tb *tb, uuid_t *uuids, size_t nuuids)
  1024. {
  1025. struct icm_ar_pkg_preboot_acl_response reply;
  1026. struct icm_ar_pkg_preboot_acl request = {
  1027. .hdr = { .code = ICM_PREBOOT_ACL },
  1028. };
  1029. int ret, i;
  1030. memset(&reply, 0, sizeof(reply));
  1031. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  1032. 1, ICM_TIMEOUT);
  1033. if (ret)
  1034. return ret;
  1035. if (reply.hdr.flags & ICM_FLAGS_ERROR)
  1036. return -EIO;
  1037. for (i = 0; i < nuuids; i++) {
  1038. u32 *uuid = (u32 *)&uuids[i];
  1039. uuid[0] = reply.acl[i].uuid_lo;
  1040. uuid[1] = reply.acl[i].uuid_hi;
  1041. if (uuid[0] == 0xffffffff && uuid[1] == 0xffffffff) {
  1042. /* Map empty entries to null UUID */
  1043. uuid[0] = 0;
  1044. uuid[1] = 0;
  1045. } else {
  1046. /* Upper two DWs are always one's */
  1047. uuid[2] = 0xffffffff;
  1048. uuid[3] = 0xffffffff;
  1049. }
  1050. }
  1051. return ret;
  1052. }
  1053. static int icm_ar_set_boot_acl(struct tb *tb, const uuid_t *uuids,
  1054. size_t nuuids)
  1055. {
  1056. struct icm_ar_pkg_preboot_acl_response reply;
  1057. struct icm_ar_pkg_preboot_acl request = {
  1058. .hdr = {
  1059. .code = ICM_PREBOOT_ACL,
  1060. .flags = ICM_FLAGS_WRITE,
  1061. },
  1062. };
  1063. int ret, i;
  1064. for (i = 0; i < nuuids; i++) {
  1065. const u32 *uuid = (const u32 *)&uuids[i];
  1066. if (uuid_is_null(&uuids[i])) {
  1067. /*
  1068. * Map null UUID to the empty (all one) entries
  1069. * for ICM.
  1070. */
  1071. request.acl[i].uuid_lo = 0xffffffff;
  1072. request.acl[i].uuid_hi = 0xffffffff;
  1073. } else {
  1074. /* Two high DWs need to be set to all one */
  1075. if (uuid[2] != 0xffffffff || uuid[3] != 0xffffffff)
  1076. return -EINVAL;
  1077. request.acl[i].uuid_lo = uuid[0];
  1078. request.acl[i].uuid_hi = uuid[1];
  1079. }
  1080. }
  1081. memset(&reply, 0, sizeof(reply));
  1082. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  1083. 1, ICM_TIMEOUT);
  1084. if (ret)
  1085. return ret;
  1086. if (reply.hdr.flags & ICM_FLAGS_ERROR)
  1087. return -EIO;
  1088. return 0;
  1089. }
  1090. static void icm_handle_notification(struct work_struct *work)
  1091. {
  1092. struct icm_notification *n = container_of(work, typeof(*n), work);
  1093. struct tb *tb = n->tb;
  1094. struct icm *icm = tb_priv(tb);
  1095. mutex_lock(&tb->lock);
  1096. switch (n->pkg->code) {
  1097. case ICM_EVENT_DEVICE_CONNECTED:
  1098. icm->device_connected(tb, n->pkg);
  1099. break;
  1100. case ICM_EVENT_DEVICE_DISCONNECTED:
  1101. icm->device_disconnected(tb, n->pkg);
  1102. break;
  1103. case ICM_EVENT_XDOMAIN_CONNECTED:
  1104. icm->xdomain_connected(tb, n->pkg);
  1105. break;
  1106. case ICM_EVENT_XDOMAIN_DISCONNECTED:
  1107. icm->xdomain_disconnected(tb, n->pkg);
  1108. break;
  1109. }
  1110. mutex_unlock(&tb->lock);
  1111. kfree(n->pkg);
  1112. kfree(n);
  1113. }
  1114. static void icm_handle_event(struct tb *tb, enum tb_cfg_pkg_type type,
  1115. const void *buf, size_t size)
  1116. {
  1117. struct icm_notification *n;
  1118. n = kmalloc(sizeof(*n), GFP_KERNEL);
  1119. if (!n)
  1120. return;
  1121. INIT_WORK(&n->work, icm_handle_notification);
  1122. n->pkg = kmemdup(buf, size, GFP_KERNEL);
  1123. n->tb = tb;
  1124. queue_work(tb->wq, &n->work);
  1125. }
  1126. static int
  1127. __icm_driver_ready(struct tb *tb, enum tb_security_level *security_level,
  1128. size_t *nboot_acl)
  1129. {
  1130. struct icm *icm = tb_priv(tb);
  1131. unsigned int retries = 50;
  1132. int ret;
  1133. ret = icm->driver_ready(tb, security_level, nboot_acl);
  1134. if (ret) {
  1135. tb_err(tb, "failed to send driver ready to ICM\n");
  1136. return ret;
  1137. }
  1138. /*
  1139. * Hold on here until the switch config space is accessible so
  1140. * that we can read root switch config successfully.
  1141. */
  1142. do {
  1143. struct tb_cfg_result res;
  1144. u32 tmp;
  1145. res = tb_cfg_read_raw(tb->ctl, &tmp, 0, 0, TB_CFG_SWITCH,
  1146. 0, 1, 100);
  1147. if (!res.err)
  1148. return 0;
  1149. msleep(50);
  1150. } while (--retries);
  1151. tb_err(tb, "failed to read root switch config space, giving up\n");
  1152. return -ETIMEDOUT;
  1153. }
  1154. static int pci2cio_wait_completion(struct icm *icm, unsigned long timeout_msec)
  1155. {
  1156. unsigned long end = jiffies + msecs_to_jiffies(timeout_msec);
  1157. u32 cmd;
  1158. do {
  1159. pci_read_config_dword(icm->upstream_port,
  1160. icm->vnd_cap + PCIE2CIO_CMD, &cmd);
  1161. if (!(cmd & PCIE2CIO_CMD_START)) {
  1162. if (cmd & PCIE2CIO_CMD_TIMEOUT)
  1163. break;
  1164. return 0;
  1165. }
  1166. msleep(50);
  1167. } while (time_before(jiffies, end));
  1168. return -ETIMEDOUT;
  1169. }
  1170. static int pcie2cio_read(struct icm *icm, enum tb_cfg_space cs,
  1171. unsigned int port, unsigned int index, u32 *data)
  1172. {
  1173. struct pci_dev *pdev = icm->upstream_port;
  1174. int ret, vnd_cap = icm->vnd_cap;
  1175. u32 cmd;
  1176. cmd = index;
  1177. cmd |= (port << PCIE2CIO_CMD_PORT_SHIFT) & PCIE2CIO_CMD_PORT_MASK;
  1178. cmd |= (cs << PCIE2CIO_CMD_CS_SHIFT) & PCIE2CIO_CMD_CS_MASK;
  1179. cmd |= PCIE2CIO_CMD_START;
  1180. pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_CMD, cmd);
  1181. ret = pci2cio_wait_completion(icm, 5000);
  1182. if (ret)
  1183. return ret;
  1184. pci_read_config_dword(pdev, vnd_cap + PCIE2CIO_RDDATA, data);
  1185. return 0;
  1186. }
  1187. static int pcie2cio_write(struct icm *icm, enum tb_cfg_space cs,
  1188. unsigned int port, unsigned int index, u32 data)
  1189. {
  1190. struct pci_dev *pdev = icm->upstream_port;
  1191. int vnd_cap = icm->vnd_cap;
  1192. u32 cmd;
  1193. pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_WRDATA, data);
  1194. cmd = index;
  1195. cmd |= (port << PCIE2CIO_CMD_PORT_SHIFT) & PCIE2CIO_CMD_PORT_MASK;
  1196. cmd |= (cs << PCIE2CIO_CMD_CS_SHIFT) & PCIE2CIO_CMD_CS_MASK;
  1197. cmd |= PCIE2CIO_CMD_WRITE | PCIE2CIO_CMD_START;
  1198. pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_CMD, cmd);
  1199. return pci2cio_wait_completion(icm, 5000);
  1200. }
  1201. static int icm_firmware_reset(struct tb *tb, struct tb_nhi *nhi)
  1202. {
  1203. struct icm *icm = tb_priv(tb);
  1204. u32 val;
  1205. if (!icm->upstream_port)
  1206. return -ENODEV;
  1207. /* Put ARC to wait for CIO reset event to happen */
  1208. val = ioread32(nhi->iobase + REG_FW_STS);
  1209. val |= REG_FW_STS_CIO_RESET_REQ;
  1210. iowrite32(val, nhi->iobase + REG_FW_STS);
  1211. /* Re-start ARC */
  1212. val = ioread32(nhi->iobase + REG_FW_STS);
  1213. val |= REG_FW_STS_ICM_EN_INVERT;
  1214. val |= REG_FW_STS_ICM_EN_CPU;
  1215. iowrite32(val, nhi->iobase + REG_FW_STS);
  1216. /* Trigger CIO reset now */
  1217. return pcie2cio_write(icm, TB_CFG_SWITCH, 0, 0x50, BIT(9));
  1218. }
  1219. static int icm_firmware_start(struct tb *tb, struct tb_nhi *nhi)
  1220. {
  1221. unsigned int retries = 10;
  1222. int ret;
  1223. u32 val;
  1224. /* Check if the ICM firmware is already running */
  1225. val = ioread32(nhi->iobase + REG_FW_STS);
  1226. if (val & REG_FW_STS_ICM_EN)
  1227. return 0;
  1228. dev_info(&nhi->pdev->dev, "starting ICM firmware\n");
  1229. ret = icm_firmware_reset(tb, nhi);
  1230. if (ret)
  1231. return ret;
  1232. /* Wait until the ICM firmware tells us it is up and running */
  1233. do {
  1234. /* Check that the ICM firmware is running */
  1235. val = ioread32(nhi->iobase + REG_FW_STS);
  1236. if (val & REG_FW_STS_NVM_AUTH_DONE)
  1237. return 0;
  1238. msleep(300);
  1239. } while (--retries);
  1240. return -ETIMEDOUT;
  1241. }
  1242. static int icm_reset_phy_port(struct tb *tb, int phy_port)
  1243. {
  1244. struct icm *icm = tb_priv(tb);
  1245. u32 state0, state1;
  1246. int port0, port1;
  1247. u32 val0, val1;
  1248. int ret;
  1249. if (!icm->upstream_port)
  1250. return 0;
  1251. if (phy_port) {
  1252. port0 = 3;
  1253. port1 = 4;
  1254. } else {
  1255. port0 = 1;
  1256. port1 = 2;
  1257. }
  1258. /*
  1259. * Read link status of both null ports belonging to a single
  1260. * physical port.
  1261. */
  1262. ret = pcie2cio_read(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, &val0);
  1263. if (ret)
  1264. return ret;
  1265. ret = pcie2cio_read(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, &val1);
  1266. if (ret)
  1267. return ret;
  1268. state0 = val0 & PHY_PORT_CS1_LINK_STATE_MASK;
  1269. state0 >>= PHY_PORT_CS1_LINK_STATE_SHIFT;
  1270. state1 = val1 & PHY_PORT_CS1_LINK_STATE_MASK;
  1271. state1 >>= PHY_PORT_CS1_LINK_STATE_SHIFT;
  1272. /* If they are both up we need to reset them now */
  1273. if (state0 != TB_PORT_UP || state1 != TB_PORT_UP)
  1274. return 0;
  1275. val0 |= PHY_PORT_CS1_LINK_DISABLE;
  1276. ret = pcie2cio_write(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, val0);
  1277. if (ret)
  1278. return ret;
  1279. val1 |= PHY_PORT_CS1_LINK_DISABLE;
  1280. ret = pcie2cio_write(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, val1);
  1281. if (ret)
  1282. return ret;
  1283. /* Wait a bit and then re-enable both ports */
  1284. usleep_range(10, 100);
  1285. ret = pcie2cio_read(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, &val0);
  1286. if (ret)
  1287. return ret;
  1288. ret = pcie2cio_read(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, &val1);
  1289. if (ret)
  1290. return ret;
  1291. val0 &= ~PHY_PORT_CS1_LINK_DISABLE;
  1292. ret = pcie2cio_write(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, val0);
  1293. if (ret)
  1294. return ret;
  1295. val1 &= ~PHY_PORT_CS1_LINK_DISABLE;
  1296. return pcie2cio_write(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, val1);
  1297. }
  1298. static int icm_firmware_init(struct tb *tb)
  1299. {
  1300. struct icm *icm = tb_priv(tb);
  1301. struct tb_nhi *nhi = tb->nhi;
  1302. int ret;
  1303. ret = icm_firmware_start(tb, nhi);
  1304. if (ret) {
  1305. dev_err(&nhi->pdev->dev, "could not start ICM firmware\n");
  1306. return ret;
  1307. }
  1308. if (icm->get_mode) {
  1309. ret = icm->get_mode(tb);
  1310. switch (ret) {
  1311. case NHI_FW_SAFE_MODE:
  1312. icm->safe_mode = true;
  1313. break;
  1314. case NHI_FW_CM_MODE:
  1315. /* Ask ICM to accept all Thunderbolt devices */
  1316. nhi_mailbox_cmd(nhi, NHI_MAILBOX_ALLOW_ALL_DEVS, 0);
  1317. break;
  1318. default:
  1319. if (ret < 0)
  1320. return ret;
  1321. tb_err(tb, "ICM firmware is in wrong mode: %u\n", ret);
  1322. return -ENODEV;
  1323. }
  1324. }
  1325. /*
  1326. * Reset both physical ports if there is anything connected to
  1327. * them already.
  1328. */
  1329. ret = icm_reset_phy_port(tb, 0);
  1330. if (ret)
  1331. dev_warn(&nhi->pdev->dev, "failed to reset links on port0\n");
  1332. ret = icm_reset_phy_port(tb, 1);
  1333. if (ret)
  1334. dev_warn(&nhi->pdev->dev, "failed to reset links on port1\n");
  1335. return 0;
  1336. }
  1337. static int icm_driver_ready(struct tb *tb)
  1338. {
  1339. struct icm *icm = tb_priv(tb);
  1340. int ret;
  1341. ret = icm_firmware_init(tb);
  1342. if (ret)
  1343. return ret;
  1344. if (icm->safe_mode) {
  1345. tb_info(tb, "Thunderbolt host controller is in safe mode.\n");
  1346. tb_info(tb, "You need to update NVM firmware of the controller before it can be used.\n");
  1347. tb_info(tb, "For latest updates check https://thunderbolttechnology.net/updates.\n");
  1348. return 0;
  1349. }
  1350. ret = __icm_driver_ready(tb, &tb->security_level, &tb->nboot_acl);
  1351. if (ret)
  1352. return ret;
  1353. /*
  1354. * Make sure the number of supported preboot ACL matches what we
  1355. * expect or disable the whole feature.
  1356. */
  1357. if (tb->nboot_acl > icm->max_boot_acl)
  1358. tb->nboot_acl = 0;
  1359. return 0;
  1360. }
  1361. static int icm_suspend(struct tb *tb)
  1362. {
  1363. int ret;
  1364. ret = nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_SAVE_DEVS, 0);
  1365. if (ret)
  1366. tb_info(tb, "Ignoring mailbox command error (%d) in %s\n",
  1367. ret, __func__);
  1368. return 0;
  1369. }
  1370. /*
  1371. * Mark all switches (except root switch) below this one unplugged. ICM
  1372. * firmware will send us an updated list of switches after we have send
  1373. * it driver ready command. If a switch is not in that list it will be
  1374. * removed when we perform rescan.
  1375. */
  1376. static void icm_unplug_children(struct tb_switch *sw)
  1377. {
  1378. unsigned int i;
  1379. if (tb_route(sw))
  1380. sw->is_unplugged = true;
  1381. for (i = 1; i <= sw->config.max_port_number; i++) {
  1382. struct tb_port *port = &sw->ports[i];
  1383. if (tb_is_upstream_port(port))
  1384. continue;
  1385. if (port->xdomain) {
  1386. port->xdomain->is_unplugged = true;
  1387. continue;
  1388. }
  1389. if (!port->remote)
  1390. continue;
  1391. icm_unplug_children(port->remote->sw);
  1392. }
  1393. }
  1394. static void icm_free_unplugged_children(struct tb_switch *sw)
  1395. {
  1396. unsigned int i;
  1397. for (i = 1; i <= sw->config.max_port_number; i++) {
  1398. struct tb_port *port = &sw->ports[i];
  1399. if (tb_is_upstream_port(port))
  1400. continue;
  1401. if (port->xdomain && port->xdomain->is_unplugged) {
  1402. tb_xdomain_remove(port->xdomain);
  1403. port->xdomain = NULL;
  1404. continue;
  1405. }
  1406. if (!port->remote)
  1407. continue;
  1408. if (port->remote->sw->is_unplugged) {
  1409. tb_switch_remove(port->remote->sw);
  1410. port->remote = NULL;
  1411. } else {
  1412. icm_free_unplugged_children(port->remote->sw);
  1413. }
  1414. }
  1415. }
  1416. static void icm_rescan_work(struct work_struct *work)
  1417. {
  1418. struct icm *icm = container_of(work, struct icm, rescan_work.work);
  1419. struct tb *tb = icm_to_tb(icm);
  1420. mutex_lock(&tb->lock);
  1421. if (tb->root_switch)
  1422. icm_free_unplugged_children(tb->root_switch);
  1423. mutex_unlock(&tb->lock);
  1424. }
  1425. static void icm_complete(struct tb *tb)
  1426. {
  1427. struct icm *icm = tb_priv(tb);
  1428. if (tb->nhi->going_away)
  1429. return;
  1430. icm_unplug_children(tb->root_switch);
  1431. /*
  1432. * Now all existing children should be resumed, start events
  1433. * from ICM to get updated status.
  1434. */
  1435. __icm_driver_ready(tb, NULL, NULL);
  1436. /*
  1437. * We do not get notifications of devices that have been
  1438. * unplugged during suspend so schedule rescan to clean them up
  1439. * if any.
  1440. */
  1441. queue_delayed_work(tb->wq, &icm->rescan_work, msecs_to_jiffies(500));
  1442. }
  1443. static int icm_start(struct tb *tb)
  1444. {
  1445. struct icm *icm = tb_priv(tb);
  1446. int ret;
  1447. if (icm->safe_mode)
  1448. tb->root_switch = tb_switch_alloc_safe_mode(tb, &tb->dev, 0);
  1449. else
  1450. tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0);
  1451. if (!tb->root_switch)
  1452. return -ENODEV;
  1453. /*
  1454. * NVM upgrade has not been tested on Apple systems and they
  1455. * don't provide images publicly either. To be on the safe side
  1456. * prevent root switch NVM upgrade on Macs for now.
  1457. */
  1458. tb->root_switch->no_nvm_upgrade = x86_apple_machine;
  1459. ret = tb_switch_add(tb->root_switch);
  1460. if (ret) {
  1461. tb_switch_put(tb->root_switch);
  1462. tb->root_switch = NULL;
  1463. }
  1464. return ret;
  1465. }
  1466. static void icm_stop(struct tb *tb)
  1467. {
  1468. struct icm *icm = tb_priv(tb);
  1469. cancel_delayed_work(&icm->rescan_work);
  1470. tb_switch_remove(tb->root_switch);
  1471. tb->root_switch = NULL;
  1472. nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0);
  1473. }
  1474. static int icm_disconnect_pcie_paths(struct tb *tb)
  1475. {
  1476. return nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DISCONNECT_PCIE_PATHS, 0);
  1477. }
  1478. /* Falcon Ridge */
  1479. static const struct tb_cm_ops icm_fr_ops = {
  1480. .driver_ready = icm_driver_ready,
  1481. .start = icm_start,
  1482. .stop = icm_stop,
  1483. .suspend = icm_suspend,
  1484. .complete = icm_complete,
  1485. .handle_event = icm_handle_event,
  1486. .approve_switch = icm_fr_approve_switch,
  1487. .add_switch_key = icm_fr_add_switch_key,
  1488. .challenge_switch_key = icm_fr_challenge_switch_key,
  1489. .disconnect_pcie_paths = icm_disconnect_pcie_paths,
  1490. .approve_xdomain_paths = icm_fr_approve_xdomain_paths,
  1491. .disconnect_xdomain_paths = icm_fr_disconnect_xdomain_paths,
  1492. };
  1493. /* Alpine Ridge */
  1494. static const struct tb_cm_ops icm_ar_ops = {
  1495. .driver_ready = icm_driver_ready,
  1496. .start = icm_start,
  1497. .stop = icm_stop,
  1498. .suspend = icm_suspend,
  1499. .complete = icm_complete,
  1500. .handle_event = icm_handle_event,
  1501. .get_boot_acl = icm_ar_get_boot_acl,
  1502. .set_boot_acl = icm_ar_set_boot_acl,
  1503. .approve_switch = icm_fr_approve_switch,
  1504. .add_switch_key = icm_fr_add_switch_key,
  1505. .challenge_switch_key = icm_fr_challenge_switch_key,
  1506. .disconnect_pcie_paths = icm_disconnect_pcie_paths,
  1507. .approve_xdomain_paths = icm_fr_approve_xdomain_paths,
  1508. .disconnect_xdomain_paths = icm_fr_disconnect_xdomain_paths,
  1509. };
  1510. /* Titan Ridge */
  1511. static const struct tb_cm_ops icm_tr_ops = {
  1512. .driver_ready = icm_driver_ready,
  1513. .start = icm_start,
  1514. .stop = icm_stop,
  1515. .suspend = icm_suspend,
  1516. .complete = icm_complete,
  1517. .handle_event = icm_handle_event,
  1518. .get_boot_acl = icm_ar_get_boot_acl,
  1519. .set_boot_acl = icm_ar_set_boot_acl,
  1520. .approve_switch = icm_tr_approve_switch,
  1521. .add_switch_key = icm_tr_add_switch_key,
  1522. .challenge_switch_key = icm_tr_challenge_switch_key,
  1523. .disconnect_pcie_paths = icm_disconnect_pcie_paths,
  1524. .approve_xdomain_paths = icm_tr_approve_xdomain_paths,
  1525. .disconnect_xdomain_paths = icm_tr_disconnect_xdomain_paths,
  1526. };
  1527. struct tb *icm_probe(struct tb_nhi *nhi)
  1528. {
  1529. struct icm *icm;
  1530. struct tb *tb;
  1531. tb = tb_domain_alloc(nhi, sizeof(struct icm));
  1532. if (!tb)
  1533. return NULL;
  1534. icm = tb_priv(tb);
  1535. INIT_DELAYED_WORK(&icm->rescan_work, icm_rescan_work);
  1536. mutex_init(&icm->request_lock);
  1537. switch (nhi->pdev->device) {
  1538. case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI:
  1539. case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI:
  1540. icm->is_supported = icm_fr_is_supported;
  1541. icm->get_route = icm_fr_get_route;
  1542. icm->driver_ready = icm_fr_driver_ready;
  1543. icm->device_connected = icm_fr_device_connected;
  1544. icm->device_disconnected = icm_fr_device_disconnected;
  1545. icm->xdomain_connected = icm_fr_xdomain_connected;
  1546. icm->xdomain_disconnected = icm_fr_xdomain_disconnected;
  1547. tb->cm_ops = &icm_fr_ops;
  1548. break;
  1549. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_NHI:
  1550. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_NHI:
  1551. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_NHI:
  1552. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_NHI:
  1553. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_NHI:
  1554. icm->max_boot_acl = ICM_AR_PREBOOT_ACL_ENTRIES;
  1555. icm->is_supported = icm_ar_is_supported;
  1556. icm->get_mode = icm_ar_get_mode;
  1557. icm->get_route = icm_ar_get_route;
  1558. icm->driver_ready = icm_ar_driver_ready;
  1559. icm->device_connected = icm_fr_device_connected;
  1560. icm->device_disconnected = icm_fr_device_disconnected;
  1561. icm->xdomain_connected = icm_fr_xdomain_connected;
  1562. icm->xdomain_disconnected = icm_fr_xdomain_disconnected;
  1563. tb->cm_ops = &icm_ar_ops;
  1564. break;
  1565. case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_NHI:
  1566. case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_NHI:
  1567. icm->max_boot_acl = ICM_AR_PREBOOT_ACL_ENTRIES;
  1568. icm->is_supported = icm_ar_is_supported;
  1569. icm->get_mode = icm_ar_get_mode;
  1570. icm->driver_ready = icm_tr_driver_ready;
  1571. icm->device_connected = icm_tr_device_connected;
  1572. icm->device_disconnected = icm_tr_device_disconnected;
  1573. icm->xdomain_connected = icm_tr_xdomain_connected;
  1574. icm->xdomain_disconnected = icm_tr_xdomain_disconnected;
  1575. tb->cm_ops = &icm_tr_ops;
  1576. break;
  1577. }
  1578. if (!icm->is_supported || !icm->is_supported(tb)) {
  1579. dev_dbg(&nhi->pdev->dev, "ICM not supported on this controller\n");
  1580. tb_domain_put(tb);
  1581. return NULL;
  1582. }
  1583. return tb;
  1584. }