icm.c 50 KB

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