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. 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. /*
  614. * After NVM upgrade adding root switch device fails because we
  615. * initiated reset. During that time ICM might still send
  616. * XDomain connected message which we ignore here.
  617. */
  618. if (!tb->root_switch)
  619. return;
  620. link = pkg->link_info & ICM_LINK_INFO_LINK_MASK;
  621. depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >>
  622. ICM_LINK_INFO_DEPTH_SHIFT;
  623. if (link > ICM_MAX_LINK || depth > ICM_MAX_DEPTH) {
  624. tb_warn(tb, "invalid topology %u.%u, ignoring\n", link, depth);
  625. return;
  626. }
  627. route = get_route(pkg->local_route_hi, pkg->local_route_lo);
  628. xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid);
  629. if (xd) {
  630. u8 xd_phy_port, phy_port;
  631. xd_phy_port = phy_port_from_route(xd->route, xd->depth);
  632. phy_port = phy_port_from_route(route, depth);
  633. if (xd->depth == depth && xd_phy_port == phy_port) {
  634. update_xdomain(xd, route, link);
  635. tb_xdomain_put(xd);
  636. return;
  637. }
  638. /*
  639. * If we find an existing XDomain connection remove it
  640. * now. We need to go through login handshake and
  641. * everything anyway to be able to re-establish the
  642. * connection.
  643. */
  644. remove_xdomain(xd);
  645. tb_xdomain_put(xd);
  646. }
  647. /*
  648. * Look if there already exists an XDomain in the same place
  649. * than the new one and in that case remove it because it is
  650. * most likely another host that got disconnected.
  651. */
  652. xd = tb_xdomain_find_by_link_depth(tb, link, depth);
  653. if (!xd) {
  654. u8 dual_link;
  655. dual_link = dual_link_from_link(link);
  656. if (dual_link)
  657. xd = tb_xdomain_find_by_link_depth(tb, dual_link,
  658. depth);
  659. }
  660. if (xd) {
  661. remove_xdomain(xd);
  662. tb_xdomain_put(xd);
  663. }
  664. /*
  665. * If the user disconnected a switch during suspend and
  666. * connected another host to the same port, remove the switch
  667. * first.
  668. */
  669. sw = get_switch_at_route(tb->root_switch, route);
  670. if (sw)
  671. remove_switch(sw);
  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. /*
  863. * After NVM upgrade adding root switch device fails because we
  864. * initiated reset. During that time ICM might still send device
  865. * connected message which we ignore here.
  866. */
  867. if (!tb->root_switch)
  868. return;
  869. route = get_route(pkg->route_hi, pkg->route_lo);
  870. authorized = pkg->link_info & ICM_LINK_INFO_APPROVED;
  871. security_level = (pkg->hdr.flags & ICM_FLAGS_SLEVEL_MASK) >>
  872. ICM_FLAGS_SLEVEL_SHIFT;
  873. boot = pkg->link_info & ICM_LINK_INFO_BOOT;
  874. if (pkg->link_info & ICM_LINK_INFO_REJECTED) {
  875. tb_info(tb, "switch at %llx was rejected by ICM firmware because topology limit exceeded\n",
  876. route);
  877. return;
  878. }
  879. sw = tb_switch_find_by_uuid(tb, &pkg->ep_uuid);
  880. if (sw) {
  881. /* Update the switch if it is still in the same place */
  882. if (tb_route(sw) == route && !!sw->authorized == authorized) {
  883. parent_sw = tb_to_switch(sw->dev.parent);
  884. update_switch(parent_sw, sw, route, pkg->connection_id,
  885. 0, 0, 0, boot);
  886. tb_switch_put(sw);
  887. return;
  888. }
  889. remove_switch(sw);
  890. tb_switch_put(sw);
  891. }
  892. /* Another switch with the same address */
  893. sw = tb_switch_find_by_route(tb, route);
  894. if (sw) {
  895. remove_switch(sw);
  896. tb_switch_put(sw);
  897. }
  898. /* XDomain connection with the same address */
  899. xd = tb_xdomain_find_by_route(tb, route);
  900. if (xd) {
  901. remove_xdomain(xd);
  902. tb_xdomain_put(xd);
  903. }
  904. parent_sw = tb_switch_find_by_route(tb, get_parent_route(route));
  905. if (!parent_sw) {
  906. tb_err(tb, "failed to find parent switch for %llx\n", route);
  907. return;
  908. }
  909. add_switch(parent_sw, route, &pkg->ep_uuid, (const u8 *)pkg->ep_name,
  910. sizeof(pkg->ep_name), pkg->connection_id,
  911. 0, 0, 0, security_level, authorized, boot);
  912. tb_switch_put(parent_sw);
  913. }
  914. static void
  915. icm_tr_device_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
  916. {
  917. const struct icm_tr_event_device_disconnected *pkg =
  918. (const struct icm_tr_event_device_disconnected *)hdr;
  919. struct tb_switch *sw;
  920. u64 route;
  921. route = get_route(pkg->route_hi, pkg->route_lo);
  922. sw = tb_switch_find_by_route(tb, route);
  923. if (!sw) {
  924. tb_warn(tb, "no switch exists at %llx, ignoring\n", route);
  925. return;
  926. }
  927. remove_switch(sw);
  928. tb_switch_put(sw);
  929. }
  930. static void
  931. icm_tr_xdomain_connected(struct tb *tb, const struct icm_pkg_header *hdr)
  932. {
  933. const struct icm_tr_event_xdomain_connected *pkg =
  934. (const struct icm_tr_event_xdomain_connected *)hdr;
  935. struct tb_xdomain *xd;
  936. struct tb_switch *sw;
  937. u64 route;
  938. if (!tb->root_switch)
  939. return;
  940. route = get_route(pkg->local_route_hi, pkg->local_route_lo);
  941. xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid);
  942. if (xd) {
  943. if (xd->route == route) {
  944. update_xdomain(xd, route, 0);
  945. tb_xdomain_put(xd);
  946. return;
  947. }
  948. remove_xdomain(xd);
  949. tb_xdomain_put(xd);
  950. }
  951. /* An existing xdomain with the same address */
  952. xd = tb_xdomain_find_by_route(tb, route);
  953. if (xd) {
  954. remove_xdomain(xd);
  955. tb_xdomain_put(xd);
  956. }
  957. /*
  958. * If the user disconnected a switch during suspend and
  959. * connected another host to the same port, remove the switch
  960. * first.
  961. */
  962. sw = get_switch_at_route(tb->root_switch, route);
  963. if (sw)
  964. remove_switch(sw);
  965. sw = tb_switch_find_by_route(tb, get_parent_route(route));
  966. if (!sw) {
  967. tb_warn(tb, "no switch exists at %llx, ignoring\n", route);
  968. return;
  969. }
  970. add_xdomain(sw, route, &pkg->local_uuid, &pkg->remote_uuid, 0, 0);
  971. tb_switch_put(sw);
  972. }
  973. static void
  974. icm_tr_xdomain_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
  975. {
  976. const struct icm_tr_event_xdomain_disconnected *pkg =
  977. (const struct icm_tr_event_xdomain_disconnected *)hdr;
  978. struct tb_xdomain *xd;
  979. u64 route;
  980. route = get_route(pkg->route_hi, pkg->route_lo);
  981. xd = tb_xdomain_find_by_route(tb, route);
  982. if (xd) {
  983. remove_xdomain(xd);
  984. tb_xdomain_put(xd);
  985. }
  986. }
  987. static struct pci_dev *get_upstream_port(struct pci_dev *pdev)
  988. {
  989. struct pci_dev *parent;
  990. parent = pci_upstream_bridge(pdev);
  991. while (parent) {
  992. if (!pci_is_pcie(parent))
  993. return NULL;
  994. if (pci_pcie_type(parent) == PCI_EXP_TYPE_UPSTREAM)
  995. break;
  996. parent = pci_upstream_bridge(parent);
  997. }
  998. if (!parent)
  999. return NULL;
  1000. switch (parent->device) {
  1001. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE:
  1002. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE:
  1003. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE:
  1004. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE:
  1005. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE:
  1006. return parent;
  1007. }
  1008. return NULL;
  1009. }
  1010. static bool icm_ar_is_supported(struct tb *tb)
  1011. {
  1012. struct pci_dev *upstream_port;
  1013. struct icm *icm = tb_priv(tb);
  1014. /*
  1015. * Starting from Alpine Ridge we can use ICM on Apple machines
  1016. * as well. We just need to reset and re-enable it first.
  1017. */
  1018. if (!x86_apple_machine)
  1019. return true;
  1020. /*
  1021. * Find the upstream PCIe port in case we need to do reset
  1022. * through its vendor specific registers.
  1023. */
  1024. upstream_port = get_upstream_port(tb->nhi->pdev);
  1025. if (upstream_port) {
  1026. int cap;
  1027. cap = pci_find_ext_capability(upstream_port,
  1028. PCI_EXT_CAP_ID_VNDR);
  1029. if (cap > 0) {
  1030. icm->upstream_port = upstream_port;
  1031. icm->vnd_cap = cap;
  1032. return true;
  1033. }
  1034. }
  1035. return false;
  1036. }
  1037. static int icm_ar_get_mode(struct tb *tb)
  1038. {
  1039. struct tb_nhi *nhi = tb->nhi;
  1040. int retries = 60;
  1041. u32 val;
  1042. do {
  1043. val = ioread32(nhi->iobase + REG_FW_STS);
  1044. if (val & REG_FW_STS_NVM_AUTH_DONE)
  1045. break;
  1046. msleep(50);
  1047. } while (--retries);
  1048. if (!retries) {
  1049. dev_err(&nhi->pdev->dev, "ICM firmware not authenticated\n");
  1050. return -ENODEV;
  1051. }
  1052. return nhi_mailbox_mode(nhi);
  1053. }
  1054. static int
  1055. icm_ar_driver_ready(struct tb *tb, enum tb_security_level *security_level,
  1056. size_t *nboot_acl, bool *rpm)
  1057. {
  1058. struct icm_ar_pkg_driver_ready_response reply;
  1059. struct icm_pkg_driver_ready request = {
  1060. .hdr.code = ICM_DRIVER_READY,
  1061. };
  1062. int ret;
  1063. memset(&reply, 0, sizeof(reply));
  1064. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  1065. 1, ICM_TIMEOUT);
  1066. if (ret)
  1067. return ret;
  1068. if (security_level)
  1069. *security_level = reply.info & ICM_AR_INFO_SLEVEL_MASK;
  1070. if (nboot_acl && (reply.info & ICM_AR_INFO_BOOT_ACL_SUPPORTED))
  1071. *nboot_acl = (reply.info & ICM_AR_INFO_BOOT_ACL_MASK) >>
  1072. ICM_AR_INFO_BOOT_ACL_SHIFT;
  1073. if (rpm)
  1074. *rpm = !!(reply.hdr.flags & ICM_AR_FLAGS_RTD3);
  1075. return 0;
  1076. }
  1077. static int icm_ar_get_route(struct tb *tb, u8 link, u8 depth, u64 *route)
  1078. {
  1079. struct icm_ar_pkg_get_route_response reply;
  1080. struct icm_ar_pkg_get_route request = {
  1081. .hdr = { .code = ICM_GET_ROUTE },
  1082. .link_info = depth << ICM_LINK_INFO_DEPTH_SHIFT | link,
  1083. };
  1084. int ret;
  1085. memset(&reply, 0, sizeof(reply));
  1086. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  1087. 1, ICM_TIMEOUT);
  1088. if (ret)
  1089. return ret;
  1090. if (reply.hdr.flags & ICM_FLAGS_ERROR)
  1091. return -EIO;
  1092. *route = get_route(reply.route_hi, reply.route_lo);
  1093. return 0;
  1094. }
  1095. static int icm_ar_get_boot_acl(struct tb *tb, uuid_t *uuids, size_t nuuids)
  1096. {
  1097. struct icm_ar_pkg_preboot_acl_response reply;
  1098. struct icm_ar_pkg_preboot_acl request = {
  1099. .hdr = { .code = ICM_PREBOOT_ACL },
  1100. };
  1101. int ret, i;
  1102. memset(&reply, 0, sizeof(reply));
  1103. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  1104. 1, ICM_TIMEOUT);
  1105. if (ret)
  1106. return ret;
  1107. if (reply.hdr.flags & ICM_FLAGS_ERROR)
  1108. return -EIO;
  1109. for (i = 0; i < nuuids; i++) {
  1110. u32 *uuid = (u32 *)&uuids[i];
  1111. uuid[0] = reply.acl[i].uuid_lo;
  1112. uuid[1] = reply.acl[i].uuid_hi;
  1113. if (uuid[0] == 0xffffffff && uuid[1] == 0xffffffff) {
  1114. /* Map empty entries to null UUID */
  1115. uuid[0] = 0;
  1116. uuid[1] = 0;
  1117. } else if (uuid[0] != 0 || uuid[1] != 0) {
  1118. /* Upper two DWs are always one's */
  1119. uuid[2] = 0xffffffff;
  1120. uuid[3] = 0xffffffff;
  1121. }
  1122. }
  1123. return ret;
  1124. }
  1125. static int icm_ar_set_boot_acl(struct tb *tb, const uuid_t *uuids,
  1126. size_t nuuids)
  1127. {
  1128. struct icm_ar_pkg_preboot_acl_response reply;
  1129. struct icm_ar_pkg_preboot_acl request = {
  1130. .hdr = {
  1131. .code = ICM_PREBOOT_ACL,
  1132. .flags = ICM_FLAGS_WRITE,
  1133. },
  1134. };
  1135. int ret, i;
  1136. for (i = 0; i < nuuids; i++) {
  1137. const u32 *uuid = (const u32 *)&uuids[i];
  1138. if (uuid_is_null(&uuids[i])) {
  1139. /*
  1140. * Map null UUID to the empty (all one) entries
  1141. * for ICM.
  1142. */
  1143. request.acl[i].uuid_lo = 0xffffffff;
  1144. request.acl[i].uuid_hi = 0xffffffff;
  1145. } else {
  1146. /* Two high DWs need to be set to all one */
  1147. if (uuid[2] != 0xffffffff || uuid[3] != 0xffffffff)
  1148. return -EINVAL;
  1149. request.acl[i].uuid_lo = uuid[0];
  1150. request.acl[i].uuid_hi = uuid[1];
  1151. }
  1152. }
  1153. memset(&reply, 0, sizeof(reply));
  1154. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  1155. 1, ICM_TIMEOUT);
  1156. if (ret)
  1157. return ret;
  1158. if (reply.hdr.flags & ICM_FLAGS_ERROR)
  1159. return -EIO;
  1160. return 0;
  1161. }
  1162. static void icm_handle_notification(struct work_struct *work)
  1163. {
  1164. struct icm_notification *n = container_of(work, typeof(*n), work);
  1165. struct tb *tb = n->tb;
  1166. struct icm *icm = tb_priv(tb);
  1167. mutex_lock(&tb->lock);
  1168. switch (n->pkg->code) {
  1169. case ICM_EVENT_DEVICE_CONNECTED:
  1170. icm->device_connected(tb, n->pkg);
  1171. break;
  1172. case ICM_EVENT_DEVICE_DISCONNECTED:
  1173. icm->device_disconnected(tb, n->pkg);
  1174. break;
  1175. case ICM_EVENT_XDOMAIN_CONNECTED:
  1176. icm->xdomain_connected(tb, n->pkg);
  1177. break;
  1178. case ICM_EVENT_XDOMAIN_DISCONNECTED:
  1179. icm->xdomain_disconnected(tb, n->pkg);
  1180. break;
  1181. }
  1182. mutex_unlock(&tb->lock);
  1183. kfree(n->pkg);
  1184. kfree(n);
  1185. }
  1186. static void icm_handle_event(struct tb *tb, enum tb_cfg_pkg_type type,
  1187. const void *buf, size_t size)
  1188. {
  1189. struct icm_notification *n;
  1190. n = kmalloc(sizeof(*n), GFP_KERNEL);
  1191. if (!n)
  1192. return;
  1193. INIT_WORK(&n->work, icm_handle_notification);
  1194. n->pkg = kmemdup(buf, size, GFP_KERNEL);
  1195. n->tb = tb;
  1196. queue_work(tb->wq, &n->work);
  1197. }
  1198. static int
  1199. __icm_driver_ready(struct tb *tb, enum tb_security_level *security_level,
  1200. size_t *nboot_acl, bool *rpm)
  1201. {
  1202. struct icm *icm = tb_priv(tb);
  1203. unsigned int retries = 50;
  1204. int ret;
  1205. ret = icm->driver_ready(tb, security_level, nboot_acl, rpm);
  1206. if (ret) {
  1207. tb_err(tb, "failed to send driver ready to ICM\n");
  1208. return ret;
  1209. }
  1210. /*
  1211. * Hold on here until the switch config space is accessible so
  1212. * that we can read root switch config successfully.
  1213. */
  1214. do {
  1215. struct tb_cfg_result res;
  1216. u32 tmp;
  1217. res = tb_cfg_read_raw(tb->ctl, &tmp, 0, 0, TB_CFG_SWITCH,
  1218. 0, 1, 100);
  1219. if (!res.err)
  1220. return 0;
  1221. msleep(50);
  1222. } while (--retries);
  1223. tb_err(tb, "failed to read root switch config space, giving up\n");
  1224. return -ETIMEDOUT;
  1225. }
  1226. static int pci2cio_wait_completion(struct icm *icm, unsigned long timeout_msec)
  1227. {
  1228. unsigned long end = jiffies + msecs_to_jiffies(timeout_msec);
  1229. u32 cmd;
  1230. do {
  1231. pci_read_config_dword(icm->upstream_port,
  1232. icm->vnd_cap + PCIE2CIO_CMD, &cmd);
  1233. if (!(cmd & PCIE2CIO_CMD_START)) {
  1234. if (cmd & PCIE2CIO_CMD_TIMEOUT)
  1235. break;
  1236. return 0;
  1237. }
  1238. msleep(50);
  1239. } while (time_before(jiffies, end));
  1240. return -ETIMEDOUT;
  1241. }
  1242. static int pcie2cio_read(struct icm *icm, enum tb_cfg_space cs,
  1243. unsigned int port, unsigned int index, u32 *data)
  1244. {
  1245. struct pci_dev *pdev = icm->upstream_port;
  1246. int ret, vnd_cap = icm->vnd_cap;
  1247. u32 cmd;
  1248. cmd = index;
  1249. cmd |= (port << PCIE2CIO_CMD_PORT_SHIFT) & PCIE2CIO_CMD_PORT_MASK;
  1250. cmd |= (cs << PCIE2CIO_CMD_CS_SHIFT) & PCIE2CIO_CMD_CS_MASK;
  1251. cmd |= PCIE2CIO_CMD_START;
  1252. pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_CMD, cmd);
  1253. ret = pci2cio_wait_completion(icm, 5000);
  1254. if (ret)
  1255. return ret;
  1256. pci_read_config_dword(pdev, vnd_cap + PCIE2CIO_RDDATA, data);
  1257. return 0;
  1258. }
  1259. static int pcie2cio_write(struct icm *icm, enum tb_cfg_space cs,
  1260. unsigned int port, unsigned int index, u32 data)
  1261. {
  1262. struct pci_dev *pdev = icm->upstream_port;
  1263. int vnd_cap = icm->vnd_cap;
  1264. u32 cmd;
  1265. pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_WRDATA, data);
  1266. cmd = index;
  1267. cmd |= (port << PCIE2CIO_CMD_PORT_SHIFT) & PCIE2CIO_CMD_PORT_MASK;
  1268. cmd |= (cs << PCIE2CIO_CMD_CS_SHIFT) & PCIE2CIO_CMD_CS_MASK;
  1269. cmd |= PCIE2CIO_CMD_WRITE | PCIE2CIO_CMD_START;
  1270. pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_CMD, cmd);
  1271. return pci2cio_wait_completion(icm, 5000);
  1272. }
  1273. static int icm_firmware_reset(struct tb *tb, struct tb_nhi *nhi)
  1274. {
  1275. struct icm *icm = tb_priv(tb);
  1276. u32 val;
  1277. if (!icm->upstream_port)
  1278. return -ENODEV;
  1279. /* Put ARC to wait for CIO reset event to happen */
  1280. val = ioread32(nhi->iobase + REG_FW_STS);
  1281. val |= REG_FW_STS_CIO_RESET_REQ;
  1282. iowrite32(val, nhi->iobase + REG_FW_STS);
  1283. /* Re-start ARC */
  1284. val = ioread32(nhi->iobase + REG_FW_STS);
  1285. val |= REG_FW_STS_ICM_EN_INVERT;
  1286. val |= REG_FW_STS_ICM_EN_CPU;
  1287. iowrite32(val, nhi->iobase + REG_FW_STS);
  1288. /* Trigger CIO reset now */
  1289. return pcie2cio_write(icm, TB_CFG_SWITCH, 0, 0x50, BIT(9));
  1290. }
  1291. static int icm_firmware_start(struct tb *tb, struct tb_nhi *nhi)
  1292. {
  1293. unsigned int retries = 10;
  1294. int ret;
  1295. u32 val;
  1296. /* Check if the ICM firmware is already running */
  1297. val = ioread32(nhi->iobase + REG_FW_STS);
  1298. if (val & REG_FW_STS_ICM_EN)
  1299. return 0;
  1300. dev_info(&nhi->pdev->dev, "starting ICM firmware\n");
  1301. ret = icm_firmware_reset(tb, nhi);
  1302. if (ret)
  1303. return ret;
  1304. /* Wait until the ICM firmware tells us it is up and running */
  1305. do {
  1306. /* Check that the ICM firmware is running */
  1307. val = ioread32(nhi->iobase + REG_FW_STS);
  1308. if (val & REG_FW_STS_NVM_AUTH_DONE)
  1309. return 0;
  1310. msleep(300);
  1311. } while (--retries);
  1312. return -ETIMEDOUT;
  1313. }
  1314. static int icm_reset_phy_port(struct tb *tb, int phy_port)
  1315. {
  1316. struct icm *icm = tb_priv(tb);
  1317. u32 state0, state1;
  1318. int port0, port1;
  1319. u32 val0, val1;
  1320. int ret;
  1321. if (!icm->upstream_port)
  1322. return 0;
  1323. if (phy_port) {
  1324. port0 = 3;
  1325. port1 = 4;
  1326. } else {
  1327. port0 = 1;
  1328. port1 = 2;
  1329. }
  1330. /*
  1331. * Read link status of both null ports belonging to a single
  1332. * physical port.
  1333. */
  1334. ret = pcie2cio_read(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, &val0);
  1335. if (ret)
  1336. return ret;
  1337. ret = pcie2cio_read(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, &val1);
  1338. if (ret)
  1339. return ret;
  1340. state0 = val0 & PHY_PORT_CS1_LINK_STATE_MASK;
  1341. state0 >>= PHY_PORT_CS1_LINK_STATE_SHIFT;
  1342. state1 = val1 & PHY_PORT_CS1_LINK_STATE_MASK;
  1343. state1 >>= PHY_PORT_CS1_LINK_STATE_SHIFT;
  1344. /* If they are both up we need to reset them now */
  1345. if (state0 != TB_PORT_UP || state1 != TB_PORT_UP)
  1346. return 0;
  1347. val0 |= PHY_PORT_CS1_LINK_DISABLE;
  1348. ret = pcie2cio_write(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, val0);
  1349. if (ret)
  1350. return ret;
  1351. val1 |= PHY_PORT_CS1_LINK_DISABLE;
  1352. ret = pcie2cio_write(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, val1);
  1353. if (ret)
  1354. return ret;
  1355. /* Wait a bit and then re-enable both ports */
  1356. usleep_range(10, 100);
  1357. ret = pcie2cio_read(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, &val0);
  1358. if (ret)
  1359. return ret;
  1360. ret = pcie2cio_read(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, &val1);
  1361. if (ret)
  1362. return ret;
  1363. val0 &= ~PHY_PORT_CS1_LINK_DISABLE;
  1364. ret = pcie2cio_write(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, val0);
  1365. if (ret)
  1366. return ret;
  1367. val1 &= ~PHY_PORT_CS1_LINK_DISABLE;
  1368. return pcie2cio_write(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, val1);
  1369. }
  1370. static int icm_firmware_init(struct tb *tb)
  1371. {
  1372. struct icm *icm = tb_priv(tb);
  1373. struct tb_nhi *nhi = tb->nhi;
  1374. int ret;
  1375. ret = icm_firmware_start(tb, nhi);
  1376. if (ret) {
  1377. dev_err(&nhi->pdev->dev, "could not start ICM firmware\n");
  1378. return ret;
  1379. }
  1380. if (icm->get_mode) {
  1381. ret = icm->get_mode(tb);
  1382. switch (ret) {
  1383. case NHI_FW_SAFE_MODE:
  1384. icm->safe_mode = true;
  1385. break;
  1386. case NHI_FW_CM_MODE:
  1387. /* Ask ICM to accept all Thunderbolt devices */
  1388. nhi_mailbox_cmd(nhi, NHI_MAILBOX_ALLOW_ALL_DEVS, 0);
  1389. break;
  1390. default:
  1391. if (ret < 0)
  1392. return ret;
  1393. tb_err(tb, "ICM firmware is in wrong mode: %u\n", ret);
  1394. return -ENODEV;
  1395. }
  1396. }
  1397. /*
  1398. * Reset both physical ports if there is anything connected to
  1399. * them already.
  1400. */
  1401. ret = icm_reset_phy_port(tb, 0);
  1402. if (ret)
  1403. dev_warn(&nhi->pdev->dev, "failed to reset links on port0\n");
  1404. ret = icm_reset_phy_port(tb, 1);
  1405. if (ret)
  1406. dev_warn(&nhi->pdev->dev, "failed to reset links on port1\n");
  1407. return 0;
  1408. }
  1409. static int icm_driver_ready(struct tb *tb)
  1410. {
  1411. struct icm *icm = tb_priv(tb);
  1412. int ret;
  1413. ret = icm_firmware_init(tb);
  1414. if (ret)
  1415. return ret;
  1416. if (icm->safe_mode) {
  1417. tb_info(tb, "Thunderbolt host controller is in safe mode.\n");
  1418. tb_info(tb, "You need to update NVM firmware of the controller before it can be used.\n");
  1419. tb_info(tb, "For latest updates check https://thunderbolttechnology.net/updates.\n");
  1420. return 0;
  1421. }
  1422. ret = __icm_driver_ready(tb, &tb->security_level, &tb->nboot_acl,
  1423. &icm->rpm);
  1424. if (ret)
  1425. return ret;
  1426. /*
  1427. * Make sure the number of supported preboot ACL matches what we
  1428. * expect or disable the whole feature.
  1429. */
  1430. if (tb->nboot_acl > icm->max_boot_acl)
  1431. tb->nboot_acl = 0;
  1432. return 0;
  1433. }
  1434. static int icm_suspend(struct tb *tb)
  1435. {
  1436. struct icm *icm = tb_priv(tb);
  1437. if (icm->save_devices)
  1438. icm->save_devices(tb);
  1439. nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0);
  1440. return 0;
  1441. }
  1442. /*
  1443. * Mark all switches (except root switch) below this one unplugged. ICM
  1444. * firmware will send us an updated list of switches after we have send
  1445. * it driver ready command. If a switch is not in that list it will be
  1446. * removed when we perform rescan.
  1447. */
  1448. static void icm_unplug_children(struct tb_switch *sw)
  1449. {
  1450. unsigned int i;
  1451. if (tb_route(sw))
  1452. sw->is_unplugged = true;
  1453. for (i = 1; i <= sw->config.max_port_number; i++) {
  1454. struct tb_port *port = &sw->ports[i];
  1455. if (tb_is_upstream_port(port))
  1456. continue;
  1457. if (port->xdomain) {
  1458. port->xdomain->is_unplugged = true;
  1459. continue;
  1460. }
  1461. if (!port->remote)
  1462. continue;
  1463. icm_unplug_children(port->remote->sw);
  1464. }
  1465. }
  1466. static void icm_free_unplugged_children(struct tb_switch *sw)
  1467. {
  1468. unsigned int i;
  1469. for (i = 1; i <= sw->config.max_port_number; i++) {
  1470. struct tb_port *port = &sw->ports[i];
  1471. if (tb_is_upstream_port(port))
  1472. continue;
  1473. if (port->xdomain && port->xdomain->is_unplugged) {
  1474. tb_xdomain_remove(port->xdomain);
  1475. port->xdomain = NULL;
  1476. continue;
  1477. }
  1478. if (!port->remote)
  1479. continue;
  1480. if (port->remote->sw->is_unplugged) {
  1481. tb_switch_remove(port->remote->sw);
  1482. port->remote = NULL;
  1483. } else {
  1484. icm_free_unplugged_children(port->remote->sw);
  1485. }
  1486. }
  1487. }
  1488. static void icm_rescan_work(struct work_struct *work)
  1489. {
  1490. struct icm *icm = container_of(work, struct icm, rescan_work.work);
  1491. struct tb *tb = icm_to_tb(icm);
  1492. mutex_lock(&tb->lock);
  1493. if (tb->root_switch)
  1494. icm_free_unplugged_children(tb->root_switch);
  1495. mutex_unlock(&tb->lock);
  1496. }
  1497. static void icm_complete(struct tb *tb)
  1498. {
  1499. struct icm *icm = tb_priv(tb);
  1500. if (tb->nhi->going_away)
  1501. return;
  1502. icm_unplug_children(tb->root_switch);
  1503. /*
  1504. * Now all existing children should be resumed, start events
  1505. * from ICM to get updated status.
  1506. */
  1507. __icm_driver_ready(tb, NULL, NULL, NULL);
  1508. /*
  1509. * We do not get notifications of devices that have been
  1510. * unplugged during suspend so schedule rescan to clean them up
  1511. * if any.
  1512. */
  1513. queue_delayed_work(tb->wq, &icm->rescan_work, msecs_to_jiffies(500));
  1514. }
  1515. static int icm_runtime_suspend(struct tb *tb)
  1516. {
  1517. nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0);
  1518. return 0;
  1519. }
  1520. static int icm_runtime_resume(struct tb *tb)
  1521. {
  1522. /*
  1523. * We can reuse the same resume functionality than with system
  1524. * suspend.
  1525. */
  1526. icm_complete(tb);
  1527. return 0;
  1528. }
  1529. static int icm_start(struct tb *tb)
  1530. {
  1531. struct icm *icm = tb_priv(tb);
  1532. int ret;
  1533. if (icm->safe_mode)
  1534. tb->root_switch = tb_switch_alloc_safe_mode(tb, &tb->dev, 0);
  1535. else
  1536. tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0);
  1537. if (!tb->root_switch)
  1538. return -ENODEV;
  1539. /*
  1540. * NVM upgrade has not been tested on Apple systems and they
  1541. * don't provide images publicly either. To be on the safe side
  1542. * prevent root switch NVM upgrade on Macs for now.
  1543. */
  1544. tb->root_switch->no_nvm_upgrade = x86_apple_machine;
  1545. tb->root_switch->rpm = icm->rpm;
  1546. ret = tb_switch_add(tb->root_switch);
  1547. if (ret) {
  1548. tb_switch_put(tb->root_switch);
  1549. tb->root_switch = NULL;
  1550. }
  1551. return ret;
  1552. }
  1553. static void icm_stop(struct tb *tb)
  1554. {
  1555. struct icm *icm = tb_priv(tb);
  1556. cancel_delayed_work(&icm->rescan_work);
  1557. tb_switch_remove(tb->root_switch);
  1558. tb->root_switch = NULL;
  1559. nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0);
  1560. }
  1561. static int icm_disconnect_pcie_paths(struct tb *tb)
  1562. {
  1563. return nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DISCONNECT_PCIE_PATHS, 0);
  1564. }
  1565. /* Falcon Ridge */
  1566. static const struct tb_cm_ops icm_fr_ops = {
  1567. .driver_ready = icm_driver_ready,
  1568. .start = icm_start,
  1569. .stop = icm_stop,
  1570. .suspend = icm_suspend,
  1571. .complete = icm_complete,
  1572. .handle_event = icm_handle_event,
  1573. .approve_switch = icm_fr_approve_switch,
  1574. .add_switch_key = icm_fr_add_switch_key,
  1575. .challenge_switch_key = icm_fr_challenge_switch_key,
  1576. .disconnect_pcie_paths = icm_disconnect_pcie_paths,
  1577. .approve_xdomain_paths = icm_fr_approve_xdomain_paths,
  1578. .disconnect_xdomain_paths = icm_fr_disconnect_xdomain_paths,
  1579. };
  1580. /* Alpine Ridge */
  1581. static const struct tb_cm_ops icm_ar_ops = {
  1582. .driver_ready = icm_driver_ready,
  1583. .start = icm_start,
  1584. .stop = icm_stop,
  1585. .suspend = icm_suspend,
  1586. .complete = icm_complete,
  1587. .runtime_suspend = icm_runtime_suspend,
  1588. .runtime_resume = icm_runtime_resume,
  1589. .handle_event = icm_handle_event,
  1590. .get_boot_acl = icm_ar_get_boot_acl,
  1591. .set_boot_acl = icm_ar_set_boot_acl,
  1592. .approve_switch = icm_fr_approve_switch,
  1593. .add_switch_key = icm_fr_add_switch_key,
  1594. .challenge_switch_key = icm_fr_challenge_switch_key,
  1595. .disconnect_pcie_paths = icm_disconnect_pcie_paths,
  1596. .approve_xdomain_paths = icm_fr_approve_xdomain_paths,
  1597. .disconnect_xdomain_paths = icm_fr_disconnect_xdomain_paths,
  1598. };
  1599. /* Titan Ridge */
  1600. static const struct tb_cm_ops icm_tr_ops = {
  1601. .driver_ready = icm_driver_ready,
  1602. .start = icm_start,
  1603. .stop = icm_stop,
  1604. .suspend = icm_suspend,
  1605. .complete = icm_complete,
  1606. .runtime_suspend = icm_runtime_suspend,
  1607. .runtime_resume = icm_runtime_resume,
  1608. .handle_event = icm_handle_event,
  1609. .get_boot_acl = icm_ar_get_boot_acl,
  1610. .set_boot_acl = icm_ar_set_boot_acl,
  1611. .approve_switch = icm_tr_approve_switch,
  1612. .add_switch_key = icm_tr_add_switch_key,
  1613. .challenge_switch_key = icm_tr_challenge_switch_key,
  1614. .disconnect_pcie_paths = icm_disconnect_pcie_paths,
  1615. .approve_xdomain_paths = icm_tr_approve_xdomain_paths,
  1616. .disconnect_xdomain_paths = icm_tr_disconnect_xdomain_paths,
  1617. };
  1618. struct tb *icm_probe(struct tb_nhi *nhi)
  1619. {
  1620. struct icm *icm;
  1621. struct tb *tb;
  1622. tb = tb_domain_alloc(nhi, sizeof(struct icm));
  1623. if (!tb)
  1624. return NULL;
  1625. icm = tb_priv(tb);
  1626. INIT_DELAYED_WORK(&icm->rescan_work, icm_rescan_work);
  1627. mutex_init(&icm->request_lock);
  1628. switch (nhi->pdev->device) {
  1629. case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI:
  1630. case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI:
  1631. icm->is_supported = icm_fr_is_supported;
  1632. icm->get_route = icm_fr_get_route;
  1633. icm->save_devices = icm_fr_save_devices;
  1634. icm->driver_ready = icm_fr_driver_ready;
  1635. icm->device_connected = icm_fr_device_connected;
  1636. icm->device_disconnected = icm_fr_device_disconnected;
  1637. icm->xdomain_connected = icm_fr_xdomain_connected;
  1638. icm->xdomain_disconnected = icm_fr_xdomain_disconnected;
  1639. tb->cm_ops = &icm_fr_ops;
  1640. break;
  1641. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_NHI:
  1642. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_NHI:
  1643. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_NHI:
  1644. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_NHI:
  1645. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_NHI:
  1646. icm->max_boot_acl = ICM_AR_PREBOOT_ACL_ENTRIES;
  1647. icm->is_supported = icm_ar_is_supported;
  1648. icm->get_mode = icm_ar_get_mode;
  1649. icm->get_route = icm_ar_get_route;
  1650. icm->save_devices = icm_fr_save_devices;
  1651. icm->driver_ready = icm_ar_driver_ready;
  1652. icm->device_connected = icm_fr_device_connected;
  1653. icm->device_disconnected = icm_fr_device_disconnected;
  1654. icm->xdomain_connected = icm_fr_xdomain_connected;
  1655. icm->xdomain_disconnected = icm_fr_xdomain_disconnected;
  1656. tb->cm_ops = &icm_ar_ops;
  1657. break;
  1658. case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_NHI:
  1659. case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_NHI:
  1660. icm->max_boot_acl = ICM_AR_PREBOOT_ACL_ENTRIES;
  1661. icm->is_supported = icm_ar_is_supported;
  1662. icm->get_mode = icm_ar_get_mode;
  1663. icm->driver_ready = icm_tr_driver_ready;
  1664. icm->device_connected = icm_tr_device_connected;
  1665. icm->device_disconnected = icm_tr_device_disconnected;
  1666. icm->xdomain_connected = icm_tr_xdomain_connected;
  1667. icm->xdomain_disconnected = icm_tr_xdomain_disconnected;
  1668. tb->cm_ops = &icm_tr_ops;
  1669. break;
  1670. }
  1671. if (!icm->is_supported || !icm->is_supported(tb)) {
  1672. dev_dbg(&nhi->pdev->dev, "ICM not supported on this controller\n");
  1673. tb_domain_put(tb);
  1674. return NULL;
  1675. }
  1676. return tb;
  1677. }