icm.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025
  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. if (!sw->uuid) {
  393. tb_sw_warn(sw, "cannot allocate memory for switch\n");
  394. tb_switch_put(sw);
  395. goto out;
  396. }
  397. sw->connection_id = connection_id;
  398. sw->connection_key = connection_key;
  399. sw->link = link;
  400. sw->depth = depth;
  401. sw->authorized = authorized;
  402. sw->security_level = security_level;
  403. sw->boot = boot;
  404. vss = parse_intel_vss(ep_name, ep_name_size);
  405. if (vss)
  406. sw->rpm = !!(vss->flags & INTEL_VSS_FLAGS_RTD3);
  407. /* Link the two switches now */
  408. tb_port_at(route, parent_sw)->remote = tb_upstream_port(sw);
  409. tb_upstream_port(sw)->remote = tb_port_at(route, parent_sw);
  410. if (tb_switch_add(sw)) {
  411. tb_port_at(tb_route(sw), parent_sw)->remote = NULL;
  412. tb_switch_put(sw);
  413. }
  414. out:
  415. pm_runtime_mark_last_busy(&parent_sw->dev);
  416. pm_runtime_put_autosuspend(&parent_sw->dev);
  417. }
  418. static void update_switch(struct tb_switch *parent_sw, struct tb_switch *sw,
  419. u64 route, u8 connection_id, u8 connection_key,
  420. u8 link, u8 depth, bool boot)
  421. {
  422. /* Disconnect from parent */
  423. tb_port_at(tb_route(sw), parent_sw)->remote = NULL;
  424. /* Re-connect via updated port*/
  425. tb_port_at(route, parent_sw)->remote = tb_upstream_port(sw);
  426. /* Update with the new addressing information */
  427. sw->config.route_hi = upper_32_bits(route);
  428. sw->config.route_lo = lower_32_bits(route);
  429. sw->connection_id = connection_id;
  430. sw->connection_key = connection_key;
  431. sw->link = link;
  432. sw->depth = depth;
  433. sw->boot = boot;
  434. /* This switch still exists */
  435. sw->is_unplugged = false;
  436. }
  437. static void remove_switch(struct tb_switch *sw)
  438. {
  439. struct tb_switch *parent_sw;
  440. parent_sw = tb_to_switch(sw->dev.parent);
  441. tb_port_at(tb_route(sw), parent_sw)->remote = NULL;
  442. tb_switch_remove(sw);
  443. }
  444. static void add_xdomain(struct tb_switch *sw, u64 route,
  445. const uuid_t *local_uuid, const uuid_t *remote_uuid,
  446. u8 link, u8 depth)
  447. {
  448. struct tb_xdomain *xd;
  449. pm_runtime_get_sync(&sw->dev);
  450. xd = tb_xdomain_alloc(sw->tb, &sw->dev, route, local_uuid, remote_uuid);
  451. if (!xd)
  452. goto out;
  453. xd->link = link;
  454. xd->depth = depth;
  455. tb_port_at(route, sw)->xdomain = xd;
  456. tb_xdomain_add(xd);
  457. out:
  458. pm_runtime_mark_last_busy(&sw->dev);
  459. pm_runtime_put_autosuspend(&sw->dev);
  460. }
  461. static void update_xdomain(struct tb_xdomain *xd, u64 route, u8 link)
  462. {
  463. xd->link = link;
  464. xd->route = route;
  465. xd->is_unplugged = false;
  466. }
  467. static void remove_xdomain(struct tb_xdomain *xd)
  468. {
  469. struct tb_switch *sw;
  470. sw = tb_to_switch(xd->dev.parent);
  471. tb_port_at(xd->route, sw)->xdomain = NULL;
  472. tb_xdomain_remove(xd);
  473. }
  474. static void
  475. icm_fr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr)
  476. {
  477. const struct icm_fr_event_device_connected *pkg =
  478. (const struct icm_fr_event_device_connected *)hdr;
  479. enum tb_security_level security_level;
  480. struct tb_switch *sw, *parent_sw;
  481. struct icm *icm = tb_priv(tb);
  482. bool authorized = false;
  483. struct tb_xdomain *xd;
  484. u8 link, depth;
  485. bool boot;
  486. u64 route;
  487. int ret;
  488. link = pkg->link_info & ICM_LINK_INFO_LINK_MASK;
  489. depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >>
  490. ICM_LINK_INFO_DEPTH_SHIFT;
  491. authorized = pkg->link_info & ICM_LINK_INFO_APPROVED;
  492. security_level = (pkg->hdr.flags & ICM_FLAGS_SLEVEL_MASK) >>
  493. ICM_FLAGS_SLEVEL_SHIFT;
  494. boot = pkg->link_info & ICM_LINK_INFO_BOOT;
  495. if (pkg->link_info & ICM_LINK_INFO_REJECTED) {
  496. tb_info(tb, "switch at %u.%u was rejected by ICM firmware because topology limit exceeded\n",
  497. link, depth);
  498. return;
  499. }
  500. sw = tb_switch_find_by_uuid(tb, &pkg->ep_uuid);
  501. if (sw) {
  502. u8 phy_port, sw_phy_port;
  503. parent_sw = tb_to_switch(sw->dev.parent);
  504. sw_phy_port = tb_phy_port_from_link(sw->link);
  505. phy_port = tb_phy_port_from_link(link);
  506. /*
  507. * On resume ICM will send us connected events for the
  508. * devices that still are present. However, that
  509. * information might have changed for example by the
  510. * fact that a switch on a dual-link connection might
  511. * have been enumerated using the other link now. Make
  512. * sure our book keeping matches that.
  513. */
  514. if (sw->depth == depth && sw_phy_port == phy_port &&
  515. !!sw->authorized == authorized) {
  516. /*
  517. * It was enumerated through another link so update
  518. * route string accordingly.
  519. */
  520. if (sw->link != link) {
  521. ret = icm->get_route(tb, link, depth, &route);
  522. if (ret) {
  523. tb_err(tb, "failed to update route string for switch at %u.%u\n",
  524. link, depth);
  525. tb_switch_put(sw);
  526. return;
  527. }
  528. } else {
  529. route = tb_route(sw);
  530. }
  531. update_switch(parent_sw, sw, route, pkg->connection_id,
  532. pkg->connection_key, link, depth, boot);
  533. tb_switch_put(sw);
  534. return;
  535. }
  536. /*
  537. * User connected the same switch to another physical
  538. * port or to another part of the topology. Remove the
  539. * existing switch now before adding the new one.
  540. */
  541. remove_switch(sw);
  542. tb_switch_put(sw);
  543. }
  544. /*
  545. * If the switch was not found by UUID, look for a switch on
  546. * same physical port (taking possible link aggregation into
  547. * account) and depth. If we found one it is definitely a stale
  548. * one so remove it first.
  549. */
  550. sw = tb_switch_find_by_link_depth(tb, link, depth);
  551. if (!sw) {
  552. u8 dual_link;
  553. dual_link = dual_link_from_link(link);
  554. if (dual_link)
  555. sw = tb_switch_find_by_link_depth(tb, dual_link, depth);
  556. }
  557. if (sw) {
  558. remove_switch(sw);
  559. tb_switch_put(sw);
  560. }
  561. /* Remove existing XDomain connection if found */
  562. xd = tb_xdomain_find_by_link_depth(tb, link, depth);
  563. if (xd) {
  564. remove_xdomain(xd);
  565. tb_xdomain_put(xd);
  566. }
  567. parent_sw = tb_switch_find_by_link_depth(tb, link, depth - 1);
  568. if (!parent_sw) {
  569. tb_err(tb, "failed to find parent switch for %u.%u\n",
  570. link, depth);
  571. return;
  572. }
  573. ret = icm->get_route(tb, link, depth, &route);
  574. if (ret) {
  575. tb_err(tb, "failed to find route string for switch at %u.%u\n",
  576. link, depth);
  577. tb_switch_put(parent_sw);
  578. return;
  579. }
  580. add_switch(parent_sw, route, &pkg->ep_uuid, (const u8 *)pkg->ep_name,
  581. sizeof(pkg->ep_name), pkg->connection_id,
  582. pkg->connection_key, link, depth, security_level,
  583. authorized, boot);
  584. tb_switch_put(parent_sw);
  585. }
  586. static void
  587. icm_fr_device_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
  588. {
  589. const struct icm_fr_event_device_disconnected *pkg =
  590. (const struct icm_fr_event_device_disconnected *)hdr;
  591. struct tb_switch *sw;
  592. u8 link, depth;
  593. link = pkg->link_info & ICM_LINK_INFO_LINK_MASK;
  594. depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >>
  595. ICM_LINK_INFO_DEPTH_SHIFT;
  596. if (link > ICM_MAX_LINK || depth > ICM_MAX_DEPTH) {
  597. tb_warn(tb, "invalid topology %u.%u, ignoring\n", link, depth);
  598. return;
  599. }
  600. sw = tb_switch_find_by_link_depth(tb, link, depth);
  601. if (!sw) {
  602. tb_warn(tb, "no switch exists at %u.%u, ignoring\n", link,
  603. depth);
  604. return;
  605. }
  606. remove_switch(sw);
  607. tb_switch_put(sw);
  608. }
  609. static void
  610. icm_fr_xdomain_connected(struct tb *tb, const struct icm_pkg_header *hdr)
  611. {
  612. const struct icm_fr_event_xdomain_connected *pkg =
  613. (const struct icm_fr_event_xdomain_connected *)hdr;
  614. struct tb_xdomain *xd;
  615. struct tb_switch *sw;
  616. u8 link, depth;
  617. u64 route;
  618. link = pkg->link_info & ICM_LINK_INFO_LINK_MASK;
  619. depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >>
  620. ICM_LINK_INFO_DEPTH_SHIFT;
  621. if (link > ICM_MAX_LINK || depth > ICM_MAX_DEPTH) {
  622. tb_warn(tb, "invalid topology %u.%u, ignoring\n", link, depth);
  623. return;
  624. }
  625. route = get_route(pkg->local_route_hi, pkg->local_route_lo);
  626. xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid);
  627. if (xd) {
  628. u8 xd_phy_port, phy_port;
  629. xd_phy_port = phy_port_from_route(xd->route, xd->depth);
  630. phy_port = phy_port_from_route(route, depth);
  631. if (xd->depth == depth && xd_phy_port == phy_port) {
  632. update_xdomain(xd, route, link);
  633. tb_xdomain_put(xd);
  634. return;
  635. }
  636. /*
  637. * If we find an existing XDomain connection remove it
  638. * now. We need to go through login handshake and
  639. * everything anyway to be able to re-establish the
  640. * connection.
  641. */
  642. remove_xdomain(xd);
  643. tb_xdomain_put(xd);
  644. }
  645. /*
  646. * Look if there already exists an XDomain in the same place
  647. * than the new one and in that case remove it because it is
  648. * most likely another host that got disconnected.
  649. */
  650. xd = tb_xdomain_find_by_link_depth(tb, link, depth);
  651. if (!xd) {
  652. u8 dual_link;
  653. dual_link = dual_link_from_link(link);
  654. if (dual_link)
  655. xd = tb_xdomain_find_by_link_depth(tb, dual_link,
  656. depth);
  657. }
  658. if (xd) {
  659. remove_xdomain(xd);
  660. tb_xdomain_put(xd);
  661. }
  662. /*
  663. * If the user disconnected a switch during suspend and
  664. * connected another host to the same port, remove the switch
  665. * first.
  666. */
  667. sw = tb_switch_find_by_route(tb, route);
  668. if (sw) {
  669. remove_switch(sw);
  670. tb_switch_put(sw);
  671. }
  672. sw = tb_switch_find_by_link_depth(tb, link, depth);
  673. if (!sw) {
  674. tb_warn(tb, "no switch exists at %u.%u, ignoring\n", link,
  675. depth);
  676. return;
  677. }
  678. add_xdomain(sw, route, &pkg->local_uuid, &pkg->remote_uuid, link,
  679. depth);
  680. tb_switch_put(sw);
  681. }
  682. static void
  683. icm_fr_xdomain_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
  684. {
  685. const struct icm_fr_event_xdomain_disconnected *pkg =
  686. (const struct icm_fr_event_xdomain_disconnected *)hdr;
  687. struct tb_xdomain *xd;
  688. /*
  689. * If the connection is through one or multiple devices, the
  690. * XDomain device is removed along with them so it is fine if we
  691. * cannot find it here.
  692. */
  693. xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid);
  694. if (xd) {
  695. remove_xdomain(xd);
  696. tb_xdomain_put(xd);
  697. }
  698. }
  699. static int
  700. icm_tr_driver_ready(struct tb *tb, enum tb_security_level *security_level,
  701. size_t *nboot_acl, bool *rpm)
  702. {
  703. struct icm_tr_pkg_driver_ready_response reply;
  704. struct icm_pkg_driver_ready request = {
  705. .hdr.code = ICM_DRIVER_READY,
  706. };
  707. int ret;
  708. memset(&reply, 0, sizeof(reply));
  709. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  710. 1, 20000);
  711. if (ret)
  712. return ret;
  713. if (security_level)
  714. *security_level = reply.info & ICM_TR_INFO_SLEVEL_MASK;
  715. if (nboot_acl)
  716. *nboot_acl = (reply.info & ICM_TR_INFO_BOOT_ACL_MASK) >>
  717. ICM_TR_INFO_BOOT_ACL_SHIFT;
  718. if (rpm)
  719. *rpm = !!(reply.hdr.flags & ICM_TR_FLAGS_RTD3);
  720. return 0;
  721. }
  722. static int icm_tr_approve_switch(struct tb *tb, struct tb_switch *sw)
  723. {
  724. struct icm_tr_pkg_approve_device request;
  725. struct icm_tr_pkg_approve_device reply;
  726. int ret;
  727. memset(&request, 0, sizeof(request));
  728. memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
  729. request.hdr.code = ICM_APPROVE_DEVICE;
  730. request.route_lo = sw->config.route_lo;
  731. request.route_hi = sw->config.route_hi;
  732. request.connection_id = sw->connection_id;
  733. memset(&reply, 0, sizeof(reply));
  734. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  735. 1, ICM_APPROVE_TIMEOUT);
  736. if (ret)
  737. return ret;
  738. if (reply.hdr.flags & ICM_FLAGS_ERROR) {
  739. tb_warn(tb, "PCIe tunnel creation failed\n");
  740. return -EIO;
  741. }
  742. return 0;
  743. }
  744. static int icm_tr_add_switch_key(struct tb *tb, struct tb_switch *sw)
  745. {
  746. struct icm_tr_pkg_add_device_key_response reply;
  747. struct icm_tr_pkg_add_device_key request;
  748. int ret;
  749. memset(&request, 0, sizeof(request));
  750. memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
  751. request.hdr.code = ICM_ADD_DEVICE_KEY;
  752. request.route_lo = sw->config.route_lo;
  753. request.route_hi = sw->config.route_hi;
  754. request.connection_id = sw->connection_id;
  755. memcpy(request.key, sw->key, TB_SWITCH_KEY_SIZE);
  756. memset(&reply, 0, sizeof(reply));
  757. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  758. 1, ICM_TIMEOUT);
  759. if (ret)
  760. return ret;
  761. if (reply.hdr.flags & ICM_FLAGS_ERROR) {
  762. tb_warn(tb, "Adding key to switch failed\n");
  763. return -EIO;
  764. }
  765. return 0;
  766. }
  767. static int icm_tr_challenge_switch_key(struct tb *tb, struct tb_switch *sw,
  768. const u8 *challenge, u8 *response)
  769. {
  770. struct icm_tr_pkg_challenge_device_response reply;
  771. struct icm_tr_pkg_challenge_device request;
  772. int ret;
  773. memset(&request, 0, sizeof(request));
  774. memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
  775. request.hdr.code = ICM_CHALLENGE_DEVICE;
  776. request.route_lo = sw->config.route_lo;
  777. request.route_hi = sw->config.route_hi;
  778. request.connection_id = sw->connection_id;
  779. memcpy(request.challenge, challenge, TB_SWITCH_KEY_SIZE);
  780. memset(&reply, 0, sizeof(reply));
  781. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  782. 1, ICM_TIMEOUT);
  783. if (ret)
  784. return ret;
  785. if (reply.hdr.flags & ICM_FLAGS_ERROR)
  786. return -EKEYREJECTED;
  787. if (reply.hdr.flags & ICM_FLAGS_NO_KEY)
  788. return -ENOKEY;
  789. memcpy(response, reply.response, TB_SWITCH_KEY_SIZE);
  790. return 0;
  791. }
  792. static int icm_tr_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
  793. {
  794. struct icm_tr_pkg_approve_xdomain_response reply;
  795. struct icm_tr_pkg_approve_xdomain request;
  796. int ret;
  797. memset(&request, 0, sizeof(request));
  798. request.hdr.code = ICM_APPROVE_XDOMAIN;
  799. request.route_hi = upper_32_bits(xd->route);
  800. request.route_lo = lower_32_bits(xd->route);
  801. request.transmit_path = xd->transmit_path;
  802. request.transmit_ring = xd->transmit_ring;
  803. request.receive_path = xd->receive_path;
  804. request.receive_ring = xd->receive_ring;
  805. memcpy(&request.remote_uuid, xd->remote_uuid, sizeof(*xd->remote_uuid));
  806. memset(&reply, 0, sizeof(reply));
  807. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  808. 1, ICM_TIMEOUT);
  809. if (ret)
  810. return ret;
  811. if (reply.hdr.flags & ICM_FLAGS_ERROR)
  812. return -EIO;
  813. return 0;
  814. }
  815. static int icm_tr_xdomain_tear_down(struct tb *tb, struct tb_xdomain *xd,
  816. int stage)
  817. {
  818. struct icm_tr_pkg_disconnect_xdomain_response reply;
  819. struct icm_tr_pkg_disconnect_xdomain request;
  820. int ret;
  821. memset(&request, 0, sizeof(request));
  822. request.hdr.code = ICM_DISCONNECT_XDOMAIN;
  823. request.stage = stage;
  824. request.route_hi = upper_32_bits(xd->route);
  825. request.route_lo = lower_32_bits(xd->route);
  826. memcpy(&request.remote_uuid, xd->remote_uuid, sizeof(*xd->remote_uuid));
  827. memset(&reply, 0, sizeof(reply));
  828. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  829. 1, ICM_TIMEOUT);
  830. if (ret)
  831. return ret;
  832. if (reply.hdr.flags & ICM_FLAGS_ERROR)
  833. return -EIO;
  834. return 0;
  835. }
  836. static int icm_tr_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
  837. {
  838. int ret;
  839. ret = icm_tr_xdomain_tear_down(tb, xd, 1);
  840. if (ret)
  841. return ret;
  842. usleep_range(10, 50);
  843. return icm_tr_xdomain_tear_down(tb, xd, 2);
  844. }
  845. static void
  846. icm_tr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr)
  847. {
  848. const struct icm_tr_event_device_connected *pkg =
  849. (const struct icm_tr_event_device_connected *)hdr;
  850. enum tb_security_level security_level;
  851. struct tb_switch *sw, *parent_sw;
  852. struct tb_xdomain *xd;
  853. bool authorized, boot;
  854. u64 route;
  855. /*
  856. * Currently we don't use the QoS information coming with the
  857. * device connected message so simply just ignore that extra
  858. * packet for now.
  859. */
  860. if (pkg->hdr.packet_id)
  861. return;
  862. route = get_route(pkg->route_hi, pkg->route_lo);
  863. authorized = pkg->link_info & ICM_LINK_INFO_APPROVED;
  864. security_level = (pkg->hdr.flags & ICM_FLAGS_SLEVEL_MASK) >>
  865. ICM_FLAGS_SLEVEL_SHIFT;
  866. boot = pkg->link_info & ICM_LINK_INFO_BOOT;
  867. if (pkg->link_info & ICM_LINK_INFO_REJECTED) {
  868. tb_info(tb, "switch at %llx was rejected by ICM firmware because topology limit exceeded\n",
  869. route);
  870. return;
  871. }
  872. sw = tb_switch_find_by_uuid(tb, &pkg->ep_uuid);
  873. if (sw) {
  874. /* Update the switch if it is still in the same place */
  875. if (tb_route(sw) == route && !!sw->authorized == authorized) {
  876. parent_sw = tb_to_switch(sw->dev.parent);
  877. update_switch(parent_sw, sw, route, pkg->connection_id,
  878. 0, 0, 0, boot);
  879. tb_switch_put(sw);
  880. return;
  881. }
  882. remove_switch(sw);
  883. tb_switch_put(sw);
  884. }
  885. /* Another switch with the same address */
  886. sw = tb_switch_find_by_route(tb, route);
  887. if (sw) {
  888. remove_switch(sw);
  889. tb_switch_put(sw);
  890. }
  891. /* XDomain connection with the same address */
  892. xd = tb_xdomain_find_by_route(tb, route);
  893. if (xd) {
  894. remove_xdomain(xd);
  895. tb_xdomain_put(xd);
  896. }
  897. parent_sw = tb_switch_find_by_route(tb, get_parent_route(route));
  898. if (!parent_sw) {
  899. tb_err(tb, "failed to find parent switch for %llx\n", route);
  900. return;
  901. }
  902. add_switch(parent_sw, route, &pkg->ep_uuid, (const u8 *)pkg->ep_name,
  903. sizeof(pkg->ep_name), pkg->connection_id,
  904. 0, 0, 0, security_level, authorized, boot);
  905. tb_switch_put(parent_sw);
  906. }
  907. static void
  908. icm_tr_device_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
  909. {
  910. const struct icm_tr_event_device_disconnected *pkg =
  911. (const struct icm_tr_event_device_disconnected *)hdr;
  912. struct tb_switch *sw;
  913. u64 route;
  914. route = get_route(pkg->route_hi, pkg->route_lo);
  915. sw = tb_switch_find_by_route(tb, route);
  916. if (!sw) {
  917. tb_warn(tb, "no switch exists at %llx, ignoring\n", route);
  918. return;
  919. }
  920. remove_switch(sw);
  921. tb_switch_put(sw);
  922. }
  923. static void
  924. icm_tr_xdomain_connected(struct tb *tb, const struct icm_pkg_header *hdr)
  925. {
  926. const struct icm_tr_event_xdomain_connected *pkg =
  927. (const struct icm_tr_event_xdomain_connected *)hdr;
  928. struct tb_xdomain *xd;
  929. struct tb_switch *sw;
  930. u64 route;
  931. if (!tb->root_switch)
  932. return;
  933. route = get_route(pkg->local_route_hi, pkg->local_route_lo);
  934. xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid);
  935. if (xd) {
  936. if (xd->route == route) {
  937. update_xdomain(xd, route, 0);
  938. tb_xdomain_put(xd);
  939. return;
  940. }
  941. remove_xdomain(xd);
  942. tb_xdomain_put(xd);
  943. }
  944. /* An existing xdomain with the same address */
  945. xd = tb_xdomain_find_by_route(tb, route);
  946. if (xd) {
  947. remove_xdomain(xd);
  948. tb_xdomain_put(xd);
  949. }
  950. /*
  951. * If the user disconnected a switch during suspend and
  952. * connected another host to the same port, remove the switch
  953. * first.
  954. */
  955. sw = tb_switch_find_by_route(tb, route);
  956. if (sw) {
  957. remove_switch(sw);
  958. tb_switch_put(sw);
  959. }
  960. sw = tb_switch_find_by_route(tb, get_parent_route(route));
  961. if (!sw) {
  962. tb_warn(tb, "no switch exists at %llx, ignoring\n", route);
  963. return;
  964. }
  965. add_xdomain(sw, route, &pkg->local_uuid, &pkg->remote_uuid, 0, 0);
  966. tb_switch_put(sw);
  967. }
  968. static void
  969. icm_tr_xdomain_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
  970. {
  971. const struct icm_tr_event_xdomain_disconnected *pkg =
  972. (const struct icm_tr_event_xdomain_disconnected *)hdr;
  973. struct tb_xdomain *xd;
  974. u64 route;
  975. route = get_route(pkg->route_hi, pkg->route_lo);
  976. xd = tb_xdomain_find_by_route(tb, route);
  977. if (xd) {
  978. remove_xdomain(xd);
  979. tb_xdomain_put(xd);
  980. }
  981. }
  982. static struct pci_dev *get_upstream_port(struct pci_dev *pdev)
  983. {
  984. struct pci_dev *parent;
  985. parent = pci_upstream_bridge(pdev);
  986. while (parent) {
  987. if (!pci_is_pcie(parent))
  988. return NULL;
  989. if (pci_pcie_type(parent) == PCI_EXP_TYPE_UPSTREAM)
  990. break;
  991. parent = pci_upstream_bridge(parent);
  992. }
  993. if (!parent)
  994. return NULL;
  995. switch (parent->device) {
  996. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE:
  997. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE:
  998. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE:
  999. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE:
  1000. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE:
  1001. return parent;
  1002. }
  1003. return NULL;
  1004. }
  1005. static bool icm_ar_is_supported(struct tb *tb)
  1006. {
  1007. struct pci_dev *upstream_port;
  1008. struct icm *icm = tb_priv(tb);
  1009. /*
  1010. * Starting from Alpine Ridge we can use ICM on Apple machines
  1011. * as well. We just need to reset and re-enable it first.
  1012. */
  1013. if (!x86_apple_machine)
  1014. return true;
  1015. /*
  1016. * Find the upstream PCIe port in case we need to do reset
  1017. * through its vendor specific registers.
  1018. */
  1019. upstream_port = get_upstream_port(tb->nhi->pdev);
  1020. if (upstream_port) {
  1021. int cap;
  1022. cap = pci_find_ext_capability(upstream_port,
  1023. PCI_EXT_CAP_ID_VNDR);
  1024. if (cap > 0) {
  1025. icm->upstream_port = upstream_port;
  1026. icm->vnd_cap = cap;
  1027. return true;
  1028. }
  1029. }
  1030. return false;
  1031. }
  1032. static int icm_ar_get_mode(struct tb *tb)
  1033. {
  1034. struct tb_nhi *nhi = tb->nhi;
  1035. int retries = 60;
  1036. u32 val;
  1037. do {
  1038. val = ioread32(nhi->iobase + REG_FW_STS);
  1039. if (val & REG_FW_STS_NVM_AUTH_DONE)
  1040. break;
  1041. msleep(50);
  1042. } while (--retries);
  1043. if (!retries) {
  1044. dev_err(&nhi->pdev->dev, "ICM firmware not authenticated\n");
  1045. return -ENODEV;
  1046. }
  1047. return nhi_mailbox_mode(nhi);
  1048. }
  1049. static int
  1050. icm_ar_driver_ready(struct tb *tb, enum tb_security_level *security_level,
  1051. size_t *nboot_acl, bool *rpm)
  1052. {
  1053. struct icm_ar_pkg_driver_ready_response reply;
  1054. struct icm_pkg_driver_ready request = {
  1055. .hdr.code = ICM_DRIVER_READY,
  1056. };
  1057. int ret;
  1058. memset(&reply, 0, sizeof(reply));
  1059. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  1060. 1, ICM_TIMEOUT);
  1061. if (ret)
  1062. return ret;
  1063. if (security_level)
  1064. *security_level = reply.info & ICM_AR_INFO_SLEVEL_MASK;
  1065. if (nboot_acl && (reply.info & ICM_AR_INFO_BOOT_ACL_SUPPORTED))
  1066. *nboot_acl = (reply.info & ICM_AR_INFO_BOOT_ACL_MASK) >>
  1067. ICM_AR_INFO_BOOT_ACL_SHIFT;
  1068. if (rpm)
  1069. *rpm = !!(reply.hdr.flags & ICM_AR_FLAGS_RTD3);
  1070. return 0;
  1071. }
  1072. static int icm_ar_get_route(struct tb *tb, u8 link, u8 depth, u64 *route)
  1073. {
  1074. struct icm_ar_pkg_get_route_response reply;
  1075. struct icm_ar_pkg_get_route request = {
  1076. .hdr = { .code = ICM_GET_ROUTE },
  1077. .link_info = depth << ICM_LINK_INFO_DEPTH_SHIFT | link,
  1078. };
  1079. int ret;
  1080. memset(&reply, 0, sizeof(reply));
  1081. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  1082. 1, ICM_TIMEOUT);
  1083. if (ret)
  1084. return ret;
  1085. if (reply.hdr.flags & ICM_FLAGS_ERROR)
  1086. return -EIO;
  1087. *route = get_route(reply.route_hi, reply.route_lo);
  1088. return 0;
  1089. }
  1090. static int icm_ar_get_boot_acl(struct tb *tb, uuid_t *uuids, size_t nuuids)
  1091. {
  1092. struct icm_ar_pkg_preboot_acl_response reply;
  1093. struct icm_ar_pkg_preboot_acl request = {
  1094. .hdr = { .code = ICM_PREBOOT_ACL },
  1095. };
  1096. int ret, i;
  1097. memset(&reply, 0, sizeof(reply));
  1098. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  1099. 1, ICM_TIMEOUT);
  1100. if (ret)
  1101. return ret;
  1102. if (reply.hdr.flags & ICM_FLAGS_ERROR)
  1103. return -EIO;
  1104. for (i = 0; i < nuuids; i++) {
  1105. u32 *uuid = (u32 *)&uuids[i];
  1106. uuid[0] = reply.acl[i].uuid_lo;
  1107. uuid[1] = reply.acl[i].uuid_hi;
  1108. if (uuid[0] == 0xffffffff && uuid[1] == 0xffffffff) {
  1109. /* Map empty entries to null UUID */
  1110. uuid[0] = 0;
  1111. uuid[1] = 0;
  1112. } else if (uuid[0] != 0 || uuid[1] != 0) {
  1113. /* Upper two DWs are always one's */
  1114. uuid[2] = 0xffffffff;
  1115. uuid[3] = 0xffffffff;
  1116. }
  1117. }
  1118. return ret;
  1119. }
  1120. static int icm_ar_set_boot_acl(struct tb *tb, const uuid_t *uuids,
  1121. size_t nuuids)
  1122. {
  1123. struct icm_ar_pkg_preboot_acl_response reply;
  1124. struct icm_ar_pkg_preboot_acl request = {
  1125. .hdr = {
  1126. .code = ICM_PREBOOT_ACL,
  1127. .flags = ICM_FLAGS_WRITE,
  1128. },
  1129. };
  1130. int ret, i;
  1131. for (i = 0; i < nuuids; i++) {
  1132. const u32 *uuid = (const u32 *)&uuids[i];
  1133. if (uuid_is_null(&uuids[i])) {
  1134. /*
  1135. * Map null UUID to the empty (all one) entries
  1136. * for ICM.
  1137. */
  1138. request.acl[i].uuid_lo = 0xffffffff;
  1139. request.acl[i].uuid_hi = 0xffffffff;
  1140. } else {
  1141. /* Two high DWs need to be set to all one */
  1142. if (uuid[2] != 0xffffffff || uuid[3] != 0xffffffff)
  1143. return -EINVAL;
  1144. request.acl[i].uuid_lo = uuid[0];
  1145. request.acl[i].uuid_hi = uuid[1];
  1146. }
  1147. }
  1148. memset(&reply, 0, sizeof(reply));
  1149. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  1150. 1, ICM_TIMEOUT);
  1151. if (ret)
  1152. return ret;
  1153. if (reply.hdr.flags & ICM_FLAGS_ERROR)
  1154. return -EIO;
  1155. return 0;
  1156. }
  1157. static void icm_handle_notification(struct work_struct *work)
  1158. {
  1159. struct icm_notification *n = container_of(work, typeof(*n), work);
  1160. struct tb *tb = n->tb;
  1161. struct icm *icm = tb_priv(tb);
  1162. mutex_lock(&tb->lock);
  1163. /*
  1164. * When the domain is stopped we flush its workqueue but before
  1165. * that the root switch is removed. In that case we should treat
  1166. * the queued events as being canceled.
  1167. */
  1168. if (tb->root_switch) {
  1169. switch (n->pkg->code) {
  1170. case ICM_EVENT_DEVICE_CONNECTED:
  1171. icm->device_connected(tb, n->pkg);
  1172. break;
  1173. case ICM_EVENT_DEVICE_DISCONNECTED:
  1174. icm->device_disconnected(tb, n->pkg);
  1175. break;
  1176. case ICM_EVENT_XDOMAIN_CONNECTED:
  1177. icm->xdomain_connected(tb, n->pkg);
  1178. break;
  1179. case ICM_EVENT_XDOMAIN_DISCONNECTED:
  1180. icm->xdomain_disconnected(tb, n->pkg);
  1181. break;
  1182. }
  1183. }
  1184. mutex_unlock(&tb->lock);
  1185. kfree(n->pkg);
  1186. kfree(n);
  1187. }
  1188. static void icm_handle_event(struct tb *tb, enum tb_cfg_pkg_type type,
  1189. const void *buf, size_t size)
  1190. {
  1191. struct icm_notification *n;
  1192. n = kmalloc(sizeof(*n), GFP_KERNEL);
  1193. if (!n)
  1194. return;
  1195. INIT_WORK(&n->work, icm_handle_notification);
  1196. n->pkg = kmemdup(buf, size, GFP_KERNEL);
  1197. n->tb = tb;
  1198. queue_work(tb->wq, &n->work);
  1199. }
  1200. static int
  1201. __icm_driver_ready(struct tb *tb, enum tb_security_level *security_level,
  1202. size_t *nboot_acl, bool *rpm)
  1203. {
  1204. struct icm *icm = tb_priv(tb);
  1205. unsigned int retries = 50;
  1206. int ret;
  1207. ret = icm->driver_ready(tb, security_level, nboot_acl, rpm);
  1208. if (ret) {
  1209. tb_err(tb, "failed to send driver ready to ICM\n");
  1210. return ret;
  1211. }
  1212. /*
  1213. * Hold on here until the switch config space is accessible so
  1214. * that we can read root switch config successfully.
  1215. */
  1216. do {
  1217. struct tb_cfg_result res;
  1218. u32 tmp;
  1219. res = tb_cfg_read_raw(tb->ctl, &tmp, 0, 0, TB_CFG_SWITCH,
  1220. 0, 1, 100);
  1221. if (!res.err)
  1222. return 0;
  1223. msleep(50);
  1224. } while (--retries);
  1225. tb_err(tb, "failed to read root switch config space, giving up\n");
  1226. return -ETIMEDOUT;
  1227. }
  1228. static int pci2cio_wait_completion(struct icm *icm, unsigned long timeout_msec)
  1229. {
  1230. unsigned long end = jiffies + msecs_to_jiffies(timeout_msec);
  1231. u32 cmd;
  1232. do {
  1233. pci_read_config_dword(icm->upstream_port,
  1234. icm->vnd_cap + PCIE2CIO_CMD, &cmd);
  1235. if (!(cmd & PCIE2CIO_CMD_START)) {
  1236. if (cmd & PCIE2CIO_CMD_TIMEOUT)
  1237. break;
  1238. return 0;
  1239. }
  1240. msleep(50);
  1241. } while (time_before(jiffies, end));
  1242. return -ETIMEDOUT;
  1243. }
  1244. static int pcie2cio_read(struct icm *icm, enum tb_cfg_space cs,
  1245. unsigned int port, unsigned int index, u32 *data)
  1246. {
  1247. struct pci_dev *pdev = icm->upstream_port;
  1248. int ret, vnd_cap = icm->vnd_cap;
  1249. u32 cmd;
  1250. cmd = index;
  1251. cmd |= (port << PCIE2CIO_CMD_PORT_SHIFT) & PCIE2CIO_CMD_PORT_MASK;
  1252. cmd |= (cs << PCIE2CIO_CMD_CS_SHIFT) & PCIE2CIO_CMD_CS_MASK;
  1253. cmd |= PCIE2CIO_CMD_START;
  1254. pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_CMD, cmd);
  1255. ret = pci2cio_wait_completion(icm, 5000);
  1256. if (ret)
  1257. return ret;
  1258. pci_read_config_dword(pdev, vnd_cap + PCIE2CIO_RDDATA, data);
  1259. return 0;
  1260. }
  1261. static int pcie2cio_write(struct icm *icm, enum tb_cfg_space cs,
  1262. unsigned int port, unsigned int index, u32 data)
  1263. {
  1264. struct pci_dev *pdev = icm->upstream_port;
  1265. int vnd_cap = icm->vnd_cap;
  1266. u32 cmd;
  1267. pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_WRDATA, data);
  1268. cmd = index;
  1269. cmd |= (port << PCIE2CIO_CMD_PORT_SHIFT) & PCIE2CIO_CMD_PORT_MASK;
  1270. cmd |= (cs << PCIE2CIO_CMD_CS_SHIFT) & PCIE2CIO_CMD_CS_MASK;
  1271. cmd |= PCIE2CIO_CMD_WRITE | PCIE2CIO_CMD_START;
  1272. pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_CMD, cmd);
  1273. return pci2cio_wait_completion(icm, 5000);
  1274. }
  1275. static int icm_firmware_reset(struct tb *tb, struct tb_nhi *nhi)
  1276. {
  1277. struct icm *icm = tb_priv(tb);
  1278. u32 val;
  1279. if (!icm->upstream_port)
  1280. return -ENODEV;
  1281. /* Put ARC to wait for CIO reset event to happen */
  1282. val = ioread32(nhi->iobase + REG_FW_STS);
  1283. val |= REG_FW_STS_CIO_RESET_REQ;
  1284. iowrite32(val, nhi->iobase + REG_FW_STS);
  1285. /* Re-start ARC */
  1286. val = ioread32(nhi->iobase + REG_FW_STS);
  1287. val |= REG_FW_STS_ICM_EN_INVERT;
  1288. val |= REG_FW_STS_ICM_EN_CPU;
  1289. iowrite32(val, nhi->iobase + REG_FW_STS);
  1290. /* Trigger CIO reset now */
  1291. return pcie2cio_write(icm, TB_CFG_SWITCH, 0, 0x50, BIT(9));
  1292. }
  1293. static int icm_firmware_start(struct tb *tb, struct tb_nhi *nhi)
  1294. {
  1295. unsigned int retries = 10;
  1296. int ret;
  1297. u32 val;
  1298. /* Check if the ICM firmware is already running */
  1299. val = ioread32(nhi->iobase + REG_FW_STS);
  1300. if (val & REG_FW_STS_ICM_EN)
  1301. return 0;
  1302. dev_info(&nhi->pdev->dev, "starting ICM firmware\n");
  1303. ret = icm_firmware_reset(tb, nhi);
  1304. if (ret)
  1305. return ret;
  1306. /* Wait until the ICM firmware tells us it is up and running */
  1307. do {
  1308. /* Check that the ICM firmware is running */
  1309. val = ioread32(nhi->iobase + REG_FW_STS);
  1310. if (val & REG_FW_STS_NVM_AUTH_DONE)
  1311. return 0;
  1312. msleep(300);
  1313. } while (--retries);
  1314. return -ETIMEDOUT;
  1315. }
  1316. static int icm_reset_phy_port(struct tb *tb, int phy_port)
  1317. {
  1318. struct icm *icm = tb_priv(tb);
  1319. u32 state0, state1;
  1320. int port0, port1;
  1321. u32 val0, val1;
  1322. int ret;
  1323. if (!icm->upstream_port)
  1324. return 0;
  1325. if (phy_port) {
  1326. port0 = 3;
  1327. port1 = 4;
  1328. } else {
  1329. port0 = 1;
  1330. port1 = 2;
  1331. }
  1332. /*
  1333. * Read link status of both null ports belonging to a single
  1334. * physical port.
  1335. */
  1336. ret = pcie2cio_read(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, &val0);
  1337. if (ret)
  1338. return ret;
  1339. ret = pcie2cio_read(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, &val1);
  1340. if (ret)
  1341. return ret;
  1342. state0 = val0 & PHY_PORT_CS1_LINK_STATE_MASK;
  1343. state0 >>= PHY_PORT_CS1_LINK_STATE_SHIFT;
  1344. state1 = val1 & PHY_PORT_CS1_LINK_STATE_MASK;
  1345. state1 >>= PHY_PORT_CS1_LINK_STATE_SHIFT;
  1346. /* If they are both up we need to reset them now */
  1347. if (state0 != TB_PORT_UP || state1 != TB_PORT_UP)
  1348. return 0;
  1349. val0 |= PHY_PORT_CS1_LINK_DISABLE;
  1350. ret = pcie2cio_write(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, val0);
  1351. if (ret)
  1352. return ret;
  1353. val1 |= PHY_PORT_CS1_LINK_DISABLE;
  1354. ret = pcie2cio_write(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, val1);
  1355. if (ret)
  1356. return ret;
  1357. /* Wait a bit and then re-enable both ports */
  1358. usleep_range(10, 100);
  1359. ret = pcie2cio_read(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, &val0);
  1360. if (ret)
  1361. return ret;
  1362. ret = pcie2cio_read(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, &val1);
  1363. if (ret)
  1364. return ret;
  1365. val0 &= ~PHY_PORT_CS1_LINK_DISABLE;
  1366. ret = pcie2cio_write(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, val0);
  1367. if (ret)
  1368. return ret;
  1369. val1 &= ~PHY_PORT_CS1_LINK_DISABLE;
  1370. return pcie2cio_write(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, val1);
  1371. }
  1372. static int icm_firmware_init(struct tb *tb)
  1373. {
  1374. struct icm *icm = tb_priv(tb);
  1375. struct tb_nhi *nhi = tb->nhi;
  1376. int ret;
  1377. ret = icm_firmware_start(tb, nhi);
  1378. if (ret) {
  1379. dev_err(&nhi->pdev->dev, "could not start ICM firmware\n");
  1380. return ret;
  1381. }
  1382. if (icm->get_mode) {
  1383. ret = icm->get_mode(tb);
  1384. switch (ret) {
  1385. case NHI_FW_SAFE_MODE:
  1386. icm->safe_mode = true;
  1387. break;
  1388. case NHI_FW_CM_MODE:
  1389. /* Ask ICM to accept all Thunderbolt devices */
  1390. nhi_mailbox_cmd(nhi, NHI_MAILBOX_ALLOW_ALL_DEVS, 0);
  1391. break;
  1392. default:
  1393. if (ret < 0)
  1394. return ret;
  1395. tb_err(tb, "ICM firmware is in wrong mode: %u\n", ret);
  1396. return -ENODEV;
  1397. }
  1398. }
  1399. /*
  1400. * Reset both physical ports if there is anything connected to
  1401. * them already.
  1402. */
  1403. ret = icm_reset_phy_port(tb, 0);
  1404. if (ret)
  1405. dev_warn(&nhi->pdev->dev, "failed to reset links on port0\n");
  1406. ret = icm_reset_phy_port(tb, 1);
  1407. if (ret)
  1408. dev_warn(&nhi->pdev->dev, "failed to reset links on port1\n");
  1409. return 0;
  1410. }
  1411. static int icm_driver_ready(struct tb *tb)
  1412. {
  1413. struct icm *icm = tb_priv(tb);
  1414. int ret;
  1415. ret = icm_firmware_init(tb);
  1416. if (ret)
  1417. return ret;
  1418. if (icm->safe_mode) {
  1419. tb_info(tb, "Thunderbolt host controller is in safe mode.\n");
  1420. tb_info(tb, "You need to update NVM firmware of the controller before it can be used.\n");
  1421. tb_info(tb, "For latest updates check https://thunderbolttechnology.net/updates.\n");
  1422. return 0;
  1423. }
  1424. ret = __icm_driver_ready(tb, &tb->security_level, &tb->nboot_acl,
  1425. &icm->rpm);
  1426. if (ret)
  1427. return ret;
  1428. /*
  1429. * Make sure the number of supported preboot ACL matches what we
  1430. * expect or disable the whole feature.
  1431. */
  1432. if (tb->nboot_acl > icm->max_boot_acl)
  1433. tb->nboot_acl = 0;
  1434. return 0;
  1435. }
  1436. static int icm_suspend(struct tb *tb)
  1437. {
  1438. struct icm *icm = tb_priv(tb);
  1439. if (icm->save_devices)
  1440. icm->save_devices(tb);
  1441. nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0);
  1442. return 0;
  1443. }
  1444. /*
  1445. * Mark all switches (except root switch) below this one unplugged. ICM
  1446. * firmware will send us an updated list of switches after we have send
  1447. * it driver ready command. If a switch is not in that list it will be
  1448. * removed when we perform rescan.
  1449. */
  1450. static void icm_unplug_children(struct tb_switch *sw)
  1451. {
  1452. unsigned int i;
  1453. if (tb_route(sw))
  1454. sw->is_unplugged = true;
  1455. for (i = 1; i <= sw->config.max_port_number; i++) {
  1456. struct tb_port *port = &sw->ports[i];
  1457. if (tb_is_upstream_port(port))
  1458. continue;
  1459. if (port->xdomain) {
  1460. port->xdomain->is_unplugged = true;
  1461. continue;
  1462. }
  1463. if (!port->remote)
  1464. continue;
  1465. icm_unplug_children(port->remote->sw);
  1466. }
  1467. }
  1468. static void icm_free_unplugged_children(struct tb_switch *sw)
  1469. {
  1470. unsigned int i;
  1471. for (i = 1; i <= sw->config.max_port_number; i++) {
  1472. struct tb_port *port = &sw->ports[i];
  1473. if (tb_is_upstream_port(port))
  1474. continue;
  1475. if (port->xdomain && port->xdomain->is_unplugged) {
  1476. tb_xdomain_remove(port->xdomain);
  1477. port->xdomain = NULL;
  1478. continue;
  1479. }
  1480. if (!port->remote)
  1481. continue;
  1482. if (port->remote->sw->is_unplugged) {
  1483. tb_switch_remove(port->remote->sw);
  1484. port->remote = NULL;
  1485. } else {
  1486. icm_free_unplugged_children(port->remote->sw);
  1487. }
  1488. }
  1489. }
  1490. static void icm_rescan_work(struct work_struct *work)
  1491. {
  1492. struct icm *icm = container_of(work, struct icm, rescan_work.work);
  1493. struct tb *tb = icm_to_tb(icm);
  1494. mutex_lock(&tb->lock);
  1495. if (tb->root_switch)
  1496. icm_free_unplugged_children(tb->root_switch);
  1497. mutex_unlock(&tb->lock);
  1498. }
  1499. static void icm_complete(struct tb *tb)
  1500. {
  1501. struct icm *icm = tb_priv(tb);
  1502. if (tb->nhi->going_away)
  1503. return;
  1504. icm_unplug_children(tb->root_switch);
  1505. /*
  1506. * Now all existing children should be resumed, start events
  1507. * from ICM to get updated status.
  1508. */
  1509. __icm_driver_ready(tb, NULL, NULL, NULL);
  1510. /*
  1511. * We do not get notifications of devices that have been
  1512. * unplugged during suspend so schedule rescan to clean them up
  1513. * if any.
  1514. */
  1515. queue_delayed_work(tb->wq, &icm->rescan_work, msecs_to_jiffies(500));
  1516. }
  1517. static int icm_runtime_suspend(struct tb *tb)
  1518. {
  1519. nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0);
  1520. return 0;
  1521. }
  1522. static int icm_runtime_resume(struct tb *tb)
  1523. {
  1524. /*
  1525. * We can reuse the same resume functionality than with system
  1526. * suspend.
  1527. */
  1528. icm_complete(tb);
  1529. return 0;
  1530. }
  1531. static int icm_start(struct tb *tb)
  1532. {
  1533. struct icm *icm = tb_priv(tb);
  1534. int ret;
  1535. if (icm->safe_mode)
  1536. tb->root_switch = tb_switch_alloc_safe_mode(tb, &tb->dev, 0);
  1537. else
  1538. tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0);
  1539. if (!tb->root_switch)
  1540. return -ENODEV;
  1541. /*
  1542. * NVM upgrade has not been tested on Apple systems and they
  1543. * don't provide images publicly either. To be on the safe side
  1544. * prevent root switch NVM upgrade on Macs for now.
  1545. */
  1546. tb->root_switch->no_nvm_upgrade = x86_apple_machine;
  1547. tb->root_switch->rpm = icm->rpm;
  1548. ret = tb_switch_add(tb->root_switch);
  1549. if (ret) {
  1550. tb_switch_put(tb->root_switch);
  1551. tb->root_switch = NULL;
  1552. }
  1553. return ret;
  1554. }
  1555. static void icm_stop(struct tb *tb)
  1556. {
  1557. struct icm *icm = tb_priv(tb);
  1558. cancel_delayed_work(&icm->rescan_work);
  1559. tb_switch_remove(tb->root_switch);
  1560. tb->root_switch = NULL;
  1561. nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0);
  1562. }
  1563. static int icm_disconnect_pcie_paths(struct tb *tb)
  1564. {
  1565. return nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DISCONNECT_PCIE_PATHS, 0);
  1566. }
  1567. /* Falcon Ridge */
  1568. static const struct tb_cm_ops icm_fr_ops = {
  1569. .driver_ready = icm_driver_ready,
  1570. .start = icm_start,
  1571. .stop = icm_stop,
  1572. .suspend = icm_suspend,
  1573. .complete = icm_complete,
  1574. .handle_event = icm_handle_event,
  1575. .approve_switch = icm_fr_approve_switch,
  1576. .add_switch_key = icm_fr_add_switch_key,
  1577. .challenge_switch_key = icm_fr_challenge_switch_key,
  1578. .disconnect_pcie_paths = icm_disconnect_pcie_paths,
  1579. .approve_xdomain_paths = icm_fr_approve_xdomain_paths,
  1580. .disconnect_xdomain_paths = icm_fr_disconnect_xdomain_paths,
  1581. };
  1582. /* Alpine Ridge */
  1583. static const struct tb_cm_ops icm_ar_ops = {
  1584. .driver_ready = icm_driver_ready,
  1585. .start = icm_start,
  1586. .stop = icm_stop,
  1587. .suspend = icm_suspend,
  1588. .complete = icm_complete,
  1589. .runtime_suspend = icm_runtime_suspend,
  1590. .runtime_resume = icm_runtime_resume,
  1591. .handle_event = icm_handle_event,
  1592. .get_boot_acl = icm_ar_get_boot_acl,
  1593. .set_boot_acl = icm_ar_set_boot_acl,
  1594. .approve_switch = icm_fr_approve_switch,
  1595. .add_switch_key = icm_fr_add_switch_key,
  1596. .challenge_switch_key = icm_fr_challenge_switch_key,
  1597. .disconnect_pcie_paths = icm_disconnect_pcie_paths,
  1598. .approve_xdomain_paths = icm_fr_approve_xdomain_paths,
  1599. .disconnect_xdomain_paths = icm_fr_disconnect_xdomain_paths,
  1600. };
  1601. /* Titan Ridge */
  1602. static const struct tb_cm_ops icm_tr_ops = {
  1603. .driver_ready = icm_driver_ready,
  1604. .start = icm_start,
  1605. .stop = icm_stop,
  1606. .suspend = icm_suspend,
  1607. .complete = icm_complete,
  1608. .runtime_suspend = icm_runtime_suspend,
  1609. .runtime_resume = icm_runtime_resume,
  1610. .handle_event = icm_handle_event,
  1611. .get_boot_acl = icm_ar_get_boot_acl,
  1612. .set_boot_acl = icm_ar_set_boot_acl,
  1613. .approve_switch = icm_tr_approve_switch,
  1614. .add_switch_key = icm_tr_add_switch_key,
  1615. .challenge_switch_key = icm_tr_challenge_switch_key,
  1616. .disconnect_pcie_paths = icm_disconnect_pcie_paths,
  1617. .approve_xdomain_paths = icm_tr_approve_xdomain_paths,
  1618. .disconnect_xdomain_paths = icm_tr_disconnect_xdomain_paths,
  1619. };
  1620. struct tb *icm_probe(struct tb_nhi *nhi)
  1621. {
  1622. struct icm *icm;
  1623. struct tb *tb;
  1624. tb = tb_domain_alloc(nhi, sizeof(struct icm));
  1625. if (!tb)
  1626. return NULL;
  1627. icm = tb_priv(tb);
  1628. INIT_DELAYED_WORK(&icm->rescan_work, icm_rescan_work);
  1629. mutex_init(&icm->request_lock);
  1630. switch (nhi->pdev->device) {
  1631. case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI:
  1632. case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI:
  1633. icm->is_supported = icm_fr_is_supported;
  1634. icm->get_route = icm_fr_get_route;
  1635. icm->save_devices = icm_fr_save_devices;
  1636. icm->driver_ready = icm_fr_driver_ready;
  1637. icm->device_connected = icm_fr_device_connected;
  1638. icm->device_disconnected = icm_fr_device_disconnected;
  1639. icm->xdomain_connected = icm_fr_xdomain_connected;
  1640. icm->xdomain_disconnected = icm_fr_xdomain_disconnected;
  1641. tb->cm_ops = &icm_fr_ops;
  1642. break;
  1643. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_NHI:
  1644. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_NHI:
  1645. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_NHI:
  1646. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_NHI:
  1647. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_NHI:
  1648. icm->max_boot_acl = ICM_AR_PREBOOT_ACL_ENTRIES;
  1649. icm->is_supported = icm_ar_is_supported;
  1650. icm->get_mode = icm_ar_get_mode;
  1651. icm->get_route = icm_ar_get_route;
  1652. icm->save_devices = icm_fr_save_devices;
  1653. icm->driver_ready = icm_ar_driver_ready;
  1654. icm->device_connected = icm_fr_device_connected;
  1655. icm->device_disconnected = icm_fr_device_disconnected;
  1656. icm->xdomain_connected = icm_fr_xdomain_connected;
  1657. icm->xdomain_disconnected = icm_fr_xdomain_disconnected;
  1658. tb->cm_ops = &icm_ar_ops;
  1659. break;
  1660. case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_NHI:
  1661. case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_NHI:
  1662. icm->max_boot_acl = ICM_AR_PREBOOT_ACL_ENTRIES;
  1663. icm->is_supported = icm_ar_is_supported;
  1664. icm->get_mode = icm_ar_get_mode;
  1665. icm->driver_ready = icm_tr_driver_ready;
  1666. icm->device_connected = icm_tr_device_connected;
  1667. icm->device_disconnected = icm_tr_device_disconnected;
  1668. icm->xdomain_connected = icm_tr_xdomain_connected;
  1669. icm->xdomain_disconnected = icm_tr_xdomain_disconnected;
  1670. tb->cm_ops = &icm_tr_ops;
  1671. break;
  1672. }
  1673. if (!icm->is_supported || !icm->is_supported(tb)) {
  1674. dev_dbg(&nhi->pdev->dev, "ICM not supported on this controller\n");
  1675. tb_domain_put(tb);
  1676. return NULL;
  1677. }
  1678. return tb;
  1679. }