icm.c 50 KB

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