icm.c 50 KB

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