icm.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556
  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/moduleparam.h>
  14. #include <linux/pci.h>
  15. #include <linux/pm_runtime.h>
  16. #include <linux/platform_data/x86/apple.h>
  17. #include <linux/sizes.h>
  18. #include <linux/slab.h>
  19. #include <linux/workqueue.h>
  20. #include "ctl.h"
  21. #include "nhi_regs.h"
  22. #include "tb.h"
  23. #define PCIE2CIO_CMD 0x30
  24. #define PCIE2CIO_CMD_TIMEOUT BIT(31)
  25. #define PCIE2CIO_CMD_START BIT(30)
  26. #define PCIE2CIO_CMD_WRITE BIT(21)
  27. #define PCIE2CIO_CMD_CS_MASK GENMASK(20, 19)
  28. #define PCIE2CIO_CMD_CS_SHIFT 19
  29. #define PCIE2CIO_CMD_PORT_MASK GENMASK(18, 13)
  30. #define PCIE2CIO_CMD_PORT_SHIFT 13
  31. #define PCIE2CIO_WRDATA 0x34
  32. #define PCIE2CIO_RDDATA 0x38
  33. #define PHY_PORT_CS1 0x37
  34. #define PHY_PORT_CS1_LINK_DISABLE BIT(14)
  35. #define PHY_PORT_CS1_LINK_STATE_MASK GENMASK(29, 26)
  36. #define PHY_PORT_CS1_LINK_STATE_SHIFT 26
  37. #define ICM_TIMEOUT 5000 /* ms */
  38. #define ICM_RETRIES 3
  39. #define ICM_APPROVE_TIMEOUT 10000 /* ms */
  40. #define ICM_MAX_LINK 4
  41. static bool start_icm;
  42. module_param(start_icm, bool, 0444);
  43. MODULE_PARM_DESC(start_icm, "start ICM firmware if it is not running (default: false)");
  44. /**
  45. * struct usb4_switch_nvm_auth - Holds USB4 NVM_AUTH status
  46. * @reply: Reply from ICM firmware is placed here
  47. * @request: Request that is sent to ICM firmware
  48. * @icm: Pointer to ICM private data
  49. */
  50. struct usb4_switch_nvm_auth {
  51. struct icm_usb4_switch_op_response reply;
  52. struct icm_usb4_switch_op request;
  53. struct icm *icm;
  54. };
  55. /**
  56. * struct icm - Internal connection manager private data
  57. * @request_lock: Makes sure only one message is send to ICM at time
  58. * @rescan_work: Work used to rescan the surviving switches after resume
  59. * @upstream_port: Pointer to the PCIe upstream port this host
  60. * controller is connected. This is only set for systems
  61. * where ICM needs to be started manually
  62. * @vnd_cap: Vendor defined capability where PCIe2CIO mailbox resides
  63. * (only set when @upstream_port is not %NULL)
  64. * @safe_mode: ICM is in safe mode
  65. * @max_boot_acl: Maximum number of preboot ACL entries (%0 if not supported)
  66. * @rpm: Does the controller support runtime PM (RTD3)
  67. * @can_upgrade_nvm: Can the NVM firmware be upgrade on this controller
  68. * @proto_version: Firmware protocol version
  69. * @last_nvm_auth: Last USB4 router NVM_AUTH result (or %NULL if not set)
  70. * @veto: Is RTD3 veto in effect
  71. * @is_supported: Checks if we can support ICM on this controller
  72. * @cio_reset: Trigger CIO reset
  73. * @get_mode: Read and return the ICM firmware mode (optional)
  74. * @get_route: Find a route string for given switch
  75. * @save_devices: Ask ICM to save devices to ACL when suspending (optional)
  76. * @driver_ready: Send driver ready message to ICM
  77. * @set_uuid: Set UUID for the root switch (optional)
  78. * @device_connected: Handle device connected ICM message
  79. * @device_disconnected: Handle device disconnected ICM message
  80. * @xdomain_connected: Handle XDomain connected ICM message
  81. * @xdomain_disconnected: Handle XDomain disconnected ICM message
  82. * @rtd3_veto: Handle RTD3 veto notification ICM message
  83. */
  84. struct icm {
  85. struct mutex request_lock;
  86. struct delayed_work rescan_work;
  87. struct pci_dev *upstream_port;
  88. int vnd_cap;
  89. bool safe_mode;
  90. size_t max_boot_acl;
  91. bool rpm;
  92. bool can_upgrade_nvm;
  93. u8 proto_version;
  94. struct usb4_switch_nvm_auth *last_nvm_auth;
  95. bool veto;
  96. bool (*is_supported)(struct tb *tb);
  97. int (*cio_reset)(struct tb *tb);
  98. int (*get_mode)(struct tb *tb);
  99. int (*get_route)(struct tb *tb, u8 link, u8 depth, u64 *route);
  100. void (*save_devices)(struct tb *tb);
  101. int (*driver_ready)(struct tb *tb,
  102. enum tb_security_level *security_level,
  103. u8 *proto_version, size_t *nboot_acl, bool *rpm);
  104. void (*set_uuid)(struct tb *tb);
  105. void (*device_connected)(struct tb *tb,
  106. const struct icm_pkg_header *hdr);
  107. void (*device_disconnected)(struct tb *tb,
  108. const struct icm_pkg_header *hdr);
  109. void (*xdomain_connected)(struct tb *tb,
  110. const struct icm_pkg_header *hdr);
  111. void (*xdomain_disconnected)(struct tb *tb,
  112. const struct icm_pkg_header *hdr);
  113. void (*rtd3_veto)(struct tb *tb, const struct icm_pkg_header *hdr);
  114. };
  115. struct icm_notification {
  116. struct work_struct work;
  117. struct icm_pkg_header *pkg;
  118. struct tb *tb;
  119. };
  120. struct ep_name_entry {
  121. u8 len;
  122. u8 type;
  123. u8 data[];
  124. };
  125. #define EP_NAME_INTEL_VSS 0x10
  126. /* Intel Vendor specific structure */
  127. struct intel_vss {
  128. u16 vendor;
  129. u16 model;
  130. u8 mc;
  131. u8 flags;
  132. u16 pci_devid;
  133. u32 nvm_version;
  134. };
  135. #define INTEL_VSS_FLAGS_RTD3 BIT(0)
  136. static const struct intel_vss *parse_intel_vss(const void *ep_name, size_t size)
  137. {
  138. const void *end = ep_name + size;
  139. while (ep_name < end) {
  140. const struct ep_name_entry *ep = ep_name;
  141. if (!ep->len)
  142. break;
  143. if (ep_name + ep->len > end)
  144. break;
  145. if (ep->type == EP_NAME_INTEL_VSS)
  146. return (const struct intel_vss *)ep->data;
  147. ep_name += ep->len;
  148. }
  149. return NULL;
  150. }
  151. static bool intel_vss_is_rtd3(const void *ep_name, size_t size)
  152. {
  153. const struct intel_vss *vss;
  154. vss = parse_intel_vss(ep_name, size);
  155. if (vss)
  156. return !!(vss->flags & INTEL_VSS_FLAGS_RTD3);
  157. return false;
  158. }
  159. static inline struct tb *icm_to_tb(struct icm *icm)
  160. {
  161. return ((void *)icm - sizeof(struct tb));
  162. }
  163. static inline u8 phy_port_from_route(u64 route, u8 depth)
  164. {
  165. u8 link;
  166. link = depth ? route >> ((depth - 1) * 8) : route;
  167. return tb_phy_port_from_link(link);
  168. }
  169. static inline u8 dual_link_from_link(u8 link)
  170. {
  171. return link ? ((link - 1) ^ 0x01) + 1 : 0;
  172. }
  173. static inline u64 get_route(u32 route_hi, u32 route_lo)
  174. {
  175. return (u64)route_hi << 32 | route_lo;
  176. }
  177. static inline u64 get_parent_route(u64 route)
  178. {
  179. int depth = tb_route_length(route);
  180. return depth ? route & ~(0xffULL << (depth - 1) * TB_ROUTE_SHIFT) : 0;
  181. }
  182. static int pci2cio_wait_completion(struct icm *icm, unsigned long timeout_msec)
  183. {
  184. unsigned long end = jiffies + msecs_to_jiffies(timeout_msec);
  185. u32 cmd;
  186. do {
  187. pci_read_config_dword(icm->upstream_port,
  188. icm->vnd_cap + PCIE2CIO_CMD, &cmd);
  189. if (!(cmd & PCIE2CIO_CMD_START)) {
  190. if (cmd & PCIE2CIO_CMD_TIMEOUT)
  191. break;
  192. return 0;
  193. }
  194. msleep(50);
  195. } while (time_before(jiffies, end));
  196. return -ETIMEDOUT;
  197. }
  198. static int pcie2cio_read(struct icm *icm, enum tb_cfg_space cs,
  199. unsigned int port, unsigned int index, u32 *data)
  200. {
  201. struct pci_dev *pdev = icm->upstream_port;
  202. int ret, vnd_cap = icm->vnd_cap;
  203. u32 cmd;
  204. cmd = index;
  205. cmd |= (port << PCIE2CIO_CMD_PORT_SHIFT) & PCIE2CIO_CMD_PORT_MASK;
  206. cmd |= (cs << PCIE2CIO_CMD_CS_SHIFT) & PCIE2CIO_CMD_CS_MASK;
  207. cmd |= PCIE2CIO_CMD_START;
  208. pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_CMD, cmd);
  209. ret = pci2cio_wait_completion(icm, 5000);
  210. if (ret)
  211. return ret;
  212. pci_read_config_dword(pdev, vnd_cap + PCIE2CIO_RDDATA, data);
  213. return 0;
  214. }
  215. static int pcie2cio_write(struct icm *icm, enum tb_cfg_space cs,
  216. unsigned int port, unsigned int index, u32 data)
  217. {
  218. struct pci_dev *pdev = icm->upstream_port;
  219. int vnd_cap = icm->vnd_cap;
  220. u32 cmd;
  221. pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_WRDATA, data);
  222. cmd = index;
  223. cmd |= (port << PCIE2CIO_CMD_PORT_SHIFT) & PCIE2CIO_CMD_PORT_MASK;
  224. cmd |= (cs << PCIE2CIO_CMD_CS_SHIFT) & PCIE2CIO_CMD_CS_MASK;
  225. cmd |= PCIE2CIO_CMD_WRITE | PCIE2CIO_CMD_START;
  226. pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_CMD, cmd);
  227. return pci2cio_wait_completion(icm, 5000);
  228. }
  229. static bool icm_match(const struct tb_cfg_request *req,
  230. const struct ctl_pkg *pkg)
  231. {
  232. const struct icm_pkg_header *res_hdr = pkg->buffer;
  233. const struct icm_pkg_header *req_hdr = req->request;
  234. if (pkg->frame.eof != req->response_type)
  235. return false;
  236. if (res_hdr->code != req_hdr->code)
  237. return false;
  238. return true;
  239. }
  240. static bool icm_copy(struct tb_cfg_request *req, const struct ctl_pkg *pkg)
  241. {
  242. const struct icm_pkg_header *hdr = pkg->buffer;
  243. if (hdr->packet_id < req->npackets) {
  244. size_t offset = hdr->packet_id * req->response_size;
  245. memcpy(req->response + offset, pkg->buffer, req->response_size);
  246. }
  247. return hdr->packet_id == hdr->total_packets - 1;
  248. }
  249. static int icm_request(struct tb *tb, const void *request, size_t request_size,
  250. void *response, size_t response_size, size_t npackets,
  251. int retries, unsigned int timeout_msec)
  252. {
  253. struct icm *icm = tb_priv(tb);
  254. do {
  255. struct tb_cfg_request *req;
  256. struct tb_cfg_result res;
  257. req = tb_cfg_request_alloc();
  258. if (!req)
  259. return -ENOMEM;
  260. req->match = icm_match;
  261. req->copy = icm_copy;
  262. req->request = request;
  263. req->request_size = request_size;
  264. req->request_type = TB_CFG_PKG_ICM_CMD;
  265. req->response = response;
  266. req->npackets = npackets;
  267. req->response_size = response_size;
  268. req->response_type = TB_CFG_PKG_ICM_RESP;
  269. mutex_lock(&icm->request_lock);
  270. res = tb_cfg_request_sync(tb->ctl, req, timeout_msec);
  271. mutex_unlock(&icm->request_lock);
  272. tb_cfg_request_put(req);
  273. if (res.err != -ETIMEDOUT)
  274. return res.err == 1 ? -EIO : res.err;
  275. usleep_range(20, 50);
  276. } while (retries--);
  277. return -ETIMEDOUT;
  278. }
  279. /*
  280. * If rescan is queued to run (we are resuming), postpone it to give the
  281. * firmware some more time to send device connected notifications for next
  282. * devices in the chain.
  283. */
  284. static void icm_postpone_rescan(struct tb *tb)
  285. {
  286. struct icm *icm = tb_priv(tb);
  287. if (delayed_work_pending(&icm->rescan_work))
  288. mod_delayed_work(tb->wq, &icm->rescan_work,
  289. msecs_to_jiffies(500));
  290. }
  291. static void icm_veto_begin(struct tb *tb)
  292. {
  293. struct icm *icm = tb_priv(tb);
  294. if (!icm->veto) {
  295. icm->veto = true;
  296. /* Keep the domain powered while veto is in effect */
  297. pm_runtime_get(&tb->dev);
  298. }
  299. }
  300. static void icm_veto_end(struct tb *tb)
  301. {
  302. struct icm *icm = tb_priv(tb);
  303. if (icm->veto) {
  304. icm->veto = false;
  305. /* Allow the domain suspend now */
  306. pm_runtime_mark_last_busy(&tb->dev);
  307. pm_runtime_put_autosuspend(&tb->dev);
  308. }
  309. }
  310. static bool icm_firmware_running(const struct tb_nhi *nhi)
  311. {
  312. u32 val;
  313. val = ioread32(nhi->iobase + REG_FW_STS);
  314. return !!(val & REG_FW_STS_ICM_EN);
  315. }
  316. static bool icm_fr_is_supported(struct tb *tb)
  317. {
  318. return !x86_apple_machine;
  319. }
  320. static inline int icm_fr_get_switch_index(u32 port)
  321. {
  322. int index;
  323. if ((port & ICM_PORT_TYPE_MASK) != TB_TYPE_PORT)
  324. return 0;
  325. index = port >> ICM_PORT_INDEX_SHIFT;
  326. return index != 0xff ? index : 0;
  327. }
  328. static int icm_fr_get_route(struct tb *tb, u8 link, u8 depth, u64 *route)
  329. {
  330. struct icm_fr_pkg_get_topology_response *switches, *sw;
  331. struct icm_fr_pkg_get_topology request = {
  332. .hdr = { .code = ICM_GET_TOPOLOGY },
  333. };
  334. size_t npackets = ICM_GET_TOPOLOGY_PACKETS;
  335. int ret, index;
  336. u8 i;
  337. switches = kcalloc(npackets, sizeof(*switches), GFP_KERNEL);
  338. if (!switches)
  339. return -ENOMEM;
  340. ret = icm_request(tb, &request, sizeof(request), switches,
  341. sizeof(*switches), npackets, ICM_RETRIES, ICM_TIMEOUT);
  342. if (ret)
  343. goto err_free;
  344. sw = &switches[0];
  345. index = icm_fr_get_switch_index(sw->ports[link]);
  346. if (!index) {
  347. ret = -ENODEV;
  348. goto err_free;
  349. }
  350. sw = &switches[index];
  351. for (i = 1; i < depth; i++) {
  352. unsigned int j;
  353. if (!(sw->first_data & ICM_SWITCH_USED)) {
  354. ret = -ENODEV;
  355. goto err_free;
  356. }
  357. for (j = 0; j < ARRAY_SIZE(sw->ports); j++) {
  358. index = icm_fr_get_switch_index(sw->ports[j]);
  359. if (index > sw->switch_index) {
  360. sw = &switches[index];
  361. break;
  362. }
  363. }
  364. }
  365. *route = get_route(sw->route_hi, sw->route_lo);
  366. err_free:
  367. kfree(switches);
  368. return ret;
  369. }
  370. static void icm_fr_save_devices(struct tb *tb)
  371. {
  372. nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_SAVE_DEVS, 0);
  373. }
  374. static int
  375. icm_fr_driver_ready(struct tb *tb, enum tb_security_level *security_level,
  376. u8 *proto_version, size_t *nboot_acl, bool *rpm)
  377. {
  378. struct icm_fr_pkg_driver_ready_response reply;
  379. struct icm_pkg_driver_ready request = {
  380. .hdr.code = ICM_DRIVER_READY,
  381. };
  382. int ret;
  383. memset(&reply, 0, sizeof(reply));
  384. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  385. 1, ICM_RETRIES, ICM_TIMEOUT);
  386. if (ret)
  387. return ret;
  388. if (security_level)
  389. *security_level = reply.security_level & ICM_FR_SLEVEL_MASK;
  390. return 0;
  391. }
  392. static int icm_fr_approve_switch(struct tb *tb, struct tb_switch *sw)
  393. {
  394. struct icm_fr_pkg_approve_device request;
  395. struct icm_fr_pkg_approve_device reply;
  396. int ret;
  397. memset(&request, 0, sizeof(request));
  398. memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
  399. request.hdr.code = ICM_APPROVE_DEVICE;
  400. request.connection_id = sw->connection_id;
  401. request.connection_key = sw->connection_key;
  402. memset(&reply, 0, sizeof(reply));
  403. /* Use larger timeout as establishing tunnels can take some time */
  404. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  405. 1, ICM_RETRIES, ICM_APPROVE_TIMEOUT);
  406. if (ret)
  407. return ret;
  408. if (reply.hdr.flags & ICM_FLAGS_ERROR) {
  409. tb_warn(tb, "PCIe tunnel creation failed\n");
  410. return -EIO;
  411. }
  412. return 0;
  413. }
  414. static int icm_fr_add_switch_key(struct tb *tb, struct tb_switch *sw)
  415. {
  416. struct icm_fr_pkg_add_device_key request;
  417. struct icm_fr_pkg_add_device_key_response reply;
  418. int ret;
  419. memset(&request, 0, sizeof(request));
  420. memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
  421. request.hdr.code = ICM_ADD_DEVICE_KEY;
  422. request.connection_id = sw->connection_id;
  423. request.connection_key = sw->connection_key;
  424. memcpy(request.key, sw->key, TB_SWITCH_KEY_SIZE);
  425. memset(&reply, 0, sizeof(reply));
  426. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  427. 1, ICM_RETRIES, ICM_TIMEOUT);
  428. if (ret)
  429. return ret;
  430. if (reply.hdr.flags & ICM_FLAGS_ERROR) {
  431. tb_warn(tb, "Adding key to switch failed\n");
  432. return -EIO;
  433. }
  434. return 0;
  435. }
  436. static int icm_fr_challenge_switch_key(struct tb *tb, struct tb_switch *sw,
  437. const u8 *challenge, u8 *response)
  438. {
  439. struct icm_fr_pkg_challenge_device request;
  440. struct icm_fr_pkg_challenge_device_response reply;
  441. int ret;
  442. memset(&request, 0, sizeof(request));
  443. memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
  444. request.hdr.code = ICM_CHALLENGE_DEVICE;
  445. request.connection_id = sw->connection_id;
  446. request.connection_key = sw->connection_key;
  447. memcpy(request.challenge, challenge, TB_SWITCH_KEY_SIZE);
  448. memset(&reply, 0, sizeof(reply));
  449. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  450. 1, ICM_RETRIES, ICM_TIMEOUT);
  451. if (ret)
  452. return ret;
  453. if (reply.hdr.flags & ICM_FLAGS_ERROR)
  454. return -EKEYREJECTED;
  455. if (reply.hdr.flags & ICM_FLAGS_NO_KEY)
  456. return -ENOKEY;
  457. memcpy(response, reply.response, TB_SWITCH_KEY_SIZE);
  458. return 0;
  459. }
  460. static int icm_fr_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
  461. int transmit_path, int transmit_ring,
  462. int receive_path, int receive_ring)
  463. {
  464. struct icm_fr_pkg_approve_xdomain_response reply;
  465. struct icm_fr_pkg_approve_xdomain request;
  466. int ret;
  467. memset(&request, 0, sizeof(request));
  468. request.hdr.code = ICM_APPROVE_XDOMAIN;
  469. request.link_info = xd->depth << ICM_LINK_INFO_DEPTH_SHIFT | xd->link;
  470. memcpy(&request.remote_uuid, xd->remote_uuid, sizeof(*xd->remote_uuid));
  471. request.transmit_path = transmit_path;
  472. request.transmit_ring = transmit_ring;
  473. request.receive_path = receive_path;
  474. request.receive_ring = receive_ring;
  475. memset(&reply, 0, sizeof(reply));
  476. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  477. 1, ICM_RETRIES, ICM_TIMEOUT);
  478. if (ret)
  479. return ret;
  480. if (reply.hdr.flags & ICM_FLAGS_ERROR)
  481. return -EIO;
  482. return 0;
  483. }
  484. static int icm_fr_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
  485. int transmit_path, int transmit_ring,
  486. int receive_path, int receive_ring)
  487. {
  488. u8 phy_port;
  489. u8 cmd;
  490. phy_port = tb_phy_port_from_link(xd->link);
  491. if (phy_port == 0)
  492. cmd = NHI_MAILBOX_DISCONNECT_PA;
  493. else
  494. cmd = NHI_MAILBOX_DISCONNECT_PB;
  495. nhi_mailbox_cmd(tb->nhi, cmd, 1);
  496. usleep_range(10, 50);
  497. nhi_mailbox_cmd(tb->nhi, cmd, 2);
  498. return 0;
  499. }
  500. static struct tb_switch *alloc_switch(struct tb_switch *parent_sw, u64 route,
  501. const uuid_t *uuid)
  502. {
  503. struct tb *tb = parent_sw->tb;
  504. struct tb_switch *sw;
  505. sw = tb_switch_alloc(tb, &parent_sw->dev, route);
  506. if (IS_ERR(sw)) {
  507. tb_warn(tb, "failed to allocate switch at %llx\n", route);
  508. return sw;
  509. }
  510. sw->uuid = kmemdup(uuid, sizeof(*uuid), GFP_KERNEL);
  511. if (!sw->uuid) {
  512. tb_switch_put(sw);
  513. return ERR_PTR(-ENOMEM);
  514. }
  515. init_completion(&sw->rpm_complete);
  516. return sw;
  517. }
  518. static int add_switch(struct tb_switch *parent_sw, struct tb_switch *sw)
  519. {
  520. u64 route = tb_route(sw);
  521. int ret;
  522. /* Link the two switches now */
  523. tb_port_at(route, parent_sw)->remote = tb_upstream_port(sw);
  524. tb_upstream_port(sw)->remote = tb_port_at(route, parent_sw);
  525. ret = tb_switch_add(sw);
  526. if (ret)
  527. tb_port_at(tb_route(sw), parent_sw)->remote = NULL;
  528. return ret;
  529. }
  530. static void update_switch(struct tb_switch *sw, u64 route, u8 connection_id,
  531. u8 connection_key, u8 link, u8 depth, bool boot)
  532. {
  533. struct tb_switch *parent_sw = tb_switch_parent(sw);
  534. /* Disconnect from parent */
  535. tb_switch_downstream_port(sw)->remote = NULL;
  536. /* Re-connect via updated port */
  537. tb_port_at(route, parent_sw)->remote = tb_upstream_port(sw);
  538. /* Update with the new addressing information */
  539. sw->config.route_hi = upper_32_bits(route);
  540. sw->config.route_lo = lower_32_bits(route);
  541. sw->connection_id = connection_id;
  542. sw->connection_key = connection_key;
  543. sw->link = link;
  544. sw->depth = depth;
  545. sw->boot = boot;
  546. /* This switch still exists */
  547. sw->is_unplugged = false;
  548. /* Runtime resume is now complete */
  549. complete(&sw->rpm_complete);
  550. }
  551. static void remove_switch(struct tb_switch *sw)
  552. {
  553. tb_switch_downstream_port(sw)->remote = NULL;
  554. tb_switch_remove(sw);
  555. }
  556. static void add_xdomain(struct tb_switch *sw, u64 route,
  557. const uuid_t *local_uuid, const uuid_t *remote_uuid,
  558. u8 link, u8 depth)
  559. {
  560. struct tb_xdomain *xd;
  561. pm_runtime_get_sync(&sw->dev);
  562. xd = tb_xdomain_alloc(sw->tb, &sw->dev, route, local_uuid, remote_uuid);
  563. if (!xd)
  564. goto out;
  565. xd->link = link;
  566. xd->depth = depth;
  567. tb_port_at(route, sw)->xdomain = xd;
  568. tb_xdomain_add(xd);
  569. out:
  570. pm_runtime_mark_last_busy(&sw->dev);
  571. pm_runtime_put_autosuspend(&sw->dev);
  572. }
  573. static void update_xdomain(struct tb_xdomain *xd, u64 route, u8 link)
  574. {
  575. xd->link = link;
  576. xd->route = route;
  577. xd->is_unplugged = false;
  578. }
  579. static void remove_xdomain(struct tb_xdomain *xd)
  580. {
  581. struct tb_switch *sw;
  582. sw = tb_to_switch(xd->dev.parent);
  583. tb_port_at(xd->route, sw)->xdomain = NULL;
  584. tb_xdomain_remove(xd);
  585. }
  586. static void
  587. icm_fr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr)
  588. {
  589. const struct icm_fr_event_device_connected *pkg =
  590. (const struct icm_fr_event_device_connected *)hdr;
  591. enum tb_security_level security_level;
  592. struct tb_switch *sw, *parent_sw;
  593. bool boot, dual_lane, speed_gen3;
  594. struct icm *icm = tb_priv(tb);
  595. bool authorized = false;
  596. struct tb_xdomain *xd;
  597. u8 link, depth;
  598. u64 route;
  599. int ret;
  600. icm_postpone_rescan(tb);
  601. link = pkg->link_info & ICM_LINK_INFO_LINK_MASK;
  602. depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >>
  603. ICM_LINK_INFO_DEPTH_SHIFT;
  604. authorized = pkg->link_info & ICM_LINK_INFO_APPROVED;
  605. security_level = (pkg->hdr.flags & ICM_FLAGS_SLEVEL_MASK) >>
  606. ICM_FLAGS_SLEVEL_SHIFT;
  607. boot = pkg->link_info & ICM_LINK_INFO_BOOT;
  608. dual_lane = pkg->hdr.flags & ICM_FLAGS_DUAL_LANE;
  609. speed_gen3 = pkg->hdr.flags & ICM_FLAGS_SPEED_GEN3;
  610. if (pkg->link_info & ICM_LINK_INFO_REJECTED) {
  611. tb_info(tb, "switch at %u.%u was rejected by ICM firmware because topology limit exceeded\n",
  612. link, depth);
  613. return;
  614. }
  615. sw = tb_switch_find_by_uuid(tb, &pkg->ep_uuid);
  616. if (sw) {
  617. u8 phy_port, sw_phy_port;
  618. sw_phy_port = tb_phy_port_from_link(sw->link);
  619. phy_port = tb_phy_port_from_link(link);
  620. /*
  621. * On resume ICM will send us connected events for the
  622. * devices that still are present. However, that
  623. * information might have changed for example by the
  624. * fact that a switch on a dual-link connection might
  625. * have been enumerated using the other link now. Make
  626. * sure our book keeping matches that.
  627. */
  628. if (sw->depth == depth && sw_phy_port == phy_port &&
  629. !!sw->authorized == authorized) {
  630. /*
  631. * It was enumerated through another link so update
  632. * route string accordingly.
  633. */
  634. if (sw->link != link) {
  635. ret = icm->get_route(tb, link, depth, &route);
  636. if (ret) {
  637. tb_err(tb, "failed to update route string for switch at %u.%u\n",
  638. link, depth);
  639. tb_switch_put(sw);
  640. return;
  641. }
  642. } else {
  643. route = tb_route(sw);
  644. }
  645. update_switch(sw, route, pkg->connection_id,
  646. pkg->connection_key, link, depth, boot);
  647. tb_switch_put(sw);
  648. return;
  649. }
  650. /*
  651. * User connected the same switch to another physical
  652. * port or to another part of the topology. Remove the
  653. * existing switch now before adding the new one.
  654. */
  655. remove_switch(sw);
  656. tb_switch_put(sw);
  657. }
  658. /*
  659. * If the switch was not found by UUID, look for a switch on
  660. * same physical port (taking possible link aggregation into
  661. * account) and depth. If we found one it is definitely a stale
  662. * one so remove it first.
  663. */
  664. sw = tb_switch_find_by_link_depth(tb, link, depth);
  665. if (!sw) {
  666. u8 dual_link;
  667. dual_link = dual_link_from_link(link);
  668. if (dual_link)
  669. sw = tb_switch_find_by_link_depth(tb, dual_link, depth);
  670. }
  671. if (sw) {
  672. remove_switch(sw);
  673. tb_switch_put(sw);
  674. }
  675. /* Remove existing XDomain connection if found */
  676. xd = tb_xdomain_find_by_link_depth(tb, link, depth);
  677. if (xd) {
  678. remove_xdomain(xd);
  679. tb_xdomain_put(xd);
  680. }
  681. parent_sw = tb_switch_find_by_link_depth(tb, link, depth - 1);
  682. if (!parent_sw) {
  683. tb_err(tb, "failed to find parent switch for %u.%u\n",
  684. link, depth);
  685. return;
  686. }
  687. ret = icm->get_route(tb, link, depth, &route);
  688. if (ret) {
  689. tb_err(tb, "failed to find route string for switch at %u.%u\n",
  690. link, depth);
  691. tb_switch_put(parent_sw);
  692. return;
  693. }
  694. pm_runtime_get_sync(&parent_sw->dev);
  695. sw = alloc_switch(parent_sw, route, &pkg->ep_uuid);
  696. if (!IS_ERR(sw)) {
  697. sw->connection_id = pkg->connection_id;
  698. sw->connection_key = pkg->connection_key;
  699. sw->link = link;
  700. sw->depth = depth;
  701. sw->authorized = authorized;
  702. sw->security_level = security_level;
  703. sw->boot = boot;
  704. sw->link_speed = speed_gen3 ? 20 : 10;
  705. sw->link_width = dual_lane ? TB_LINK_WIDTH_DUAL :
  706. TB_LINK_WIDTH_SINGLE;
  707. sw->rpm = intel_vss_is_rtd3(pkg->ep_name, sizeof(pkg->ep_name));
  708. if (add_switch(parent_sw, sw))
  709. tb_switch_put(sw);
  710. }
  711. pm_runtime_mark_last_busy(&parent_sw->dev);
  712. pm_runtime_put_autosuspend(&parent_sw->dev);
  713. tb_switch_put(parent_sw);
  714. }
  715. static void
  716. icm_fr_device_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
  717. {
  718. const struct icm_fr_event_device_disconnected *pkg =
  719. (const struct icm_fr_event_device_disconnected *)hdr;
  720. struct tb_switch *sw;
  721. u8 link, depth;
  722. link = pkg->link_info & ICM_LINK_INFO_LINK_MASK;
  723. depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >>
  724. ICM_LINK_INFO_DEPTH_SHIFT;
  725. if (link > ICM_MAX_LINK || depth > TB_SWITCH_MAX_DEPTH) {
  726. tb_warn(tb, "invalid topology %u.%u, ignoring\n", link, depth);
  727. return;
  728. }
  729. sw = tb_switch_find_by_link_depth(tb, link, depth);
  730. if (!sw) {
  731. tb_warn(tb, "no switch exists at %u.%u, ignoring\n", link,
  732. depth);
  733. return;
  734. }
  735. pm_runtime_get_sync(sw->dev.parent);
  736. remove_switch(sw);
  737. pm_runtime_mark_last_busy(sw->dev.parent);
  738. pm_runtime_put_autosuspend(sw->dev.parent);
  739. tb_switch_put(sw);
  740. }
  741. static void
  742. icm_fr_xdomain_connected(struct tb *tb, const struct icm_pkg_header *hdr)
  743. {
  744. const struct icm_fr_event_xdomain_connected *pkg =
  745. (const struct icm_fr_event_xdomain_connected *)hdr;
  746. struct tb_xdomain *xd;
  747. struct tb_switch *sw;
  748. u8 link, depth;
  749. u64 route;
  750. link = pkg->link_info & ICM_LINK_INFO_LINK_MASK;
  751. depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >>
  752. ICM_LINK_INFO_DEPTH_SHIFT;
  753. if (link > ICM_MAX_LINK || depth > TB_SWITCH_MAX_DEPTH) {
  754. tb_warn(tb, "invalid topology %u.%u, ignoring\n", link, depth);
  755. return;
  756. }
  757. route = get_route(pkg->local_route_hi, pkg->local_route_lo);
  758. xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid);
  759. if (xd) {
  760. u8 xd_phy_port, phy_port;
  761. xd_phy_port = phy_port_from_route(xd->route, xd->depth);
  762. phy_port = phy_port_from_route(route, depth);
  763. if (xd->depth == depth && xd_phy_port == phy_port) {
  764. update_xdomain(xd, route, link);
  765. tb_xdomain_put(xd);
  766. return;
  767. }
  768. /*
  769. * If we find an existing XDomain connection remove it
  770. * now. We need to go through login handshake and
  771. * everything anyway to be able to re-establish the
  772. * connection.
  773. */
  774. remove_xdomain(xd);
  775. tb_xdomain_put(xd);
  776. }
  777. /*
  778. * Look if there already exists an XDomain in the same place
  779. * than the new one and in that case remove it because it is
  780. * most likely another host that got disconnected.
  781. */
  782. xd = tb_xdomain_find_by_link_depth(tb, link, depth);
  783. if (!xd) {
  784. u8 dual_link;
  785. dual_link = dual_link_from_link(link);
  786. if (dual_link)
  787. xd = tb_xdomain_find_by_link_depth(tb, dual_link,
  788. depth);
  789. }
  790. if (xd) {
  791. remove_xdomain(xd);
  792. tb_xdomain_put(xd);
  793. }
  794. /*
  795. * If the user disconnected a switch during suspend and
  796. * connected another host to the same port, remove the switch
  797. * first.
  798. */
  799. sw = tb_switch_find_by_route(tb, route);
  800. if (sw) {
  801. remove_switch(sw);
  802. tb_switch_put(sw);
  803. }
  804. sw = tb_switch_find_by_link_depth(tb, link, depth);
  805. if (!sw) {
  806. tb_warn(tb, "no switch exists at %u.%u, ignoring\n", link,
  807. depth);
  808. return;
  809. }
  810. add_xdomain(sw, route, &pkg->local_uuid, &pkg->remote_uuid, link,
  811. depth);
  812. tb_switch_put(sw);
  813. }
  814. static void
  815. icm_fr_xdomain_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
  816. {
  817. const struct icm_fr_event_xdomain_disconnected *pkg =
  818. (const struct icm_fr_event_xdomain_disconnected *)hdr;
  819. struct tb_xdomain *xd;
  820. /*
  821. * If the connection is through one or multiple devices, the
  822. * XDomain device is removed along with them so it is fine if we
  823. * cannot find it here.
  824. */
  825. xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid);
  826. if (xd) {
  827. remove_xdomain(xd);
  828. tb_xdomain_put(xd);
  829. }
  830. }
  831. static int icm_tr_cio_reset(struct tb *tb)
  832. {
  833. return pcie2cio_write(tb_priv(tb), TB_CFG_SWITCH, 0, 0x777, BIT(1));
  834. }
  835. static int
  836. icm_tr_driver_ready(struct tb *tb, enum tb_security_level *security_level,
  837. u8 *proto_version, size_t *nboot_acl, bool *rpm)
  838. {
  839. struct icm_tr_pkg_driver_ready_response reply;
  840. struct icm_pkg_driver_ready request = {
  841. .hdr.code = ICM_DRIVER_READY,
  842. };
  843. int ret;
  844. memset(&reply, 0, sizeof(reply));
  845. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  846. 1, 10, 250);
  847. if (ret)
  848. return ret;
  849. if (security_level)
  850. *security_level = reply.info & ICM_TR_INFO_SLEVEL_MASK;
  851. if (proto_version)
  852. *proto_version = (reply.info & ICM_TR_INFO_PROTO_VERSION_MASK) >>
  853. ICM_TR_INFO_PROTO_VERSION_SHIFT;
  854. if (nboot_acl)
  855. *nboot_acl = (reply.info & ICM_TR_INFO_BOOT_ACL_MASK) >>
  856. ICM_TR_INFO_BOOT_ACL_SHIFT;
  857. if (rpm)
  858. *rpm = !!(reply.hdr.flags & ICM_TR_FLAGS_RTD3);
  859. return 0;
  860. }
  861. static int icm_tr_approve_switch(struct tb *tb, struct tb_switch *sw)
  862. {
  863. struct icm_tr_pkg_approve_device request;
  864. struct icm_tr_pkg_approve_device reply;
  865. int ret;
  866. memset(&request, 0, sizeof(request));
  867. memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
  868. request.hdr.code = ICM_APPROVE_DEVICE;
  869. request.route_lo = sw->config.route_lo;
  870. request.route_hi = sw->config.route_hi;
  871. request.connection_id = sw->connection_id;
  872. memset(&reply, 0, sizeof(reply));
  873. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  874. 1, ICM_RETRIES, ICM_APPROVE_TIMEOUT);
  875. if (ret)
  876. return ret;
  877. if (reply.hdr.flags & ICM_FLAGS_ERROR) {
  878. tb_warn(tb, "PCIe tunnel creation failed\n");
  879. return -EIO;
  880. }
  881. return 0;
  882. }
  883. static int icm_tr_add_switch_key(struct tb *tb, struct tb_switch *sw)
  884. {
  885. struct icm_tr_pkg_add_device_key_response reply;
  886. struct icm_tr_pkg_add_device_key request;
  887. int ret;
  888. memset(&request, 0, sizeof(request));
  889. memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
  890. request.hdr.code = ICM_ADD_DEVICE_KEY;
  891. request.route_lo = sw->config.route_lo;
  892. request.route_hi = sw->config.route_hi;
  893. request.connection_id = sw->connection_id;
  894. memcpy(request.key, sw->key, TB_SWITCH_KEY_SIZE);
  895. memset(&reply, 0, sizeof(reply));
  896. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  897. 1, ICM_RETRIES, ICM_TIMEOUT);
  898. if (ret)
  899. return ret;
  900. if (reply.hdr.flags & ICM_FLAGS_ERROR) {
  901. tb_warn(tb, "Adding key to switch failed\n");
  902. return -EIO;
  903. }
  904. return 0;
  905. }
  906. static int icm_tr_challenge_switch_key(struct tb *tb, struct tb_switch *sw,
  907. const u8 *challenge, u8 *response)
  908. {
  909. struct icm_tr_pkg_challenge_device_response reply;
  910. struct icm_tr_pkg_challenge_device request;
  911. int ret;
  912. memset(&request, 0, sizeof(request));
  913. memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
  914. request.hdr.code = ICM_CHALLENGE_DEVICE;
  915. request.route_lo = sw->config.route_lo;
  916. request.route_hi = sw->config.route_hi;
  917. request.connection_id = sw->connection_id;
  918. memcpy(request.challenge, challenge, TB_SWITCH_KEY_SIZE);
  919. memset(&reply, 0, sizeof(reply));
  920. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  921. 1, ICM_RETRIES, ICM_TIMEOUT);
  922. if (ret)
  923. return ret;
  924. if (reply.hdr.flags & ICM_FLAGS_ERROR)
  925. return -EKEYREJECTED;
  926. if (reply.hdr.flags & ICM_FLAGS_NO_KEY)
  927. return -ENOKEY;
  928. memcpy(response, reply.response, TB_SWITCH_KEY_SIZE);
  929. return 0;
  930. }
  931. static int icm_tr_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
  932. int transmit_path, int transmit_ring,
  933. int receive_path, int receive_ring)
  934. {
  935. struct icm_tr_pkg_approve_xdomain_response reply;
  936. struct icm_tr_pkg_approve_xdomain request;
  937. int ret;
  938. memset(&request, 0, sizeof(request));
  939. request.hdr.code = ICM_APPROVE_XDOMAIN;
  940. request.route_hi = upper_32_bits(xd->route);
  941. request.route_lo = lower_32_bits(xd->route);
  942. request.transmit_path = transmit_path;
  943. request.transmit_ring = transmit_ring;
  944. request.receive_path = receive_path;
  945. request.receive_ring = receive_ring;
  946. memcpy(&request.remote_uuid, xd->remote_uuid, sizeof(*xd->remote_uuid));
  947. memset(&reply, 0, sizeof(reply));
  948. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  949. 1, ICM_RETRIES, ICM_TIMEOUT);
  950. if (ret)
  951. return ret;
  952. if (reply.hdr.flags & ICM_FLAGS_ERROR)
  953. return -EIO;
  954. return 0;
  955. }
  956. static int icm_tr_xdomain_tear_down(struct tb *tb, struct tb_xdomain *xd,
  957. int stage)
  958. {
  959. struct icm_tr_pkg_disconnect_xdomain_response reply;
  960. struct icm_tr_pkg_disconnect_xdomain request;
  961. int ret;
  962. memset(&request, 0, sizeof(request));
  963. request.hdr.code = ICM_DISCONNECT_XDOMAIN;
  964. request.stage = stage;
  965. request.route_hi = upper_32_bits(xd->route);
  966. request.route_lo = lower_32_bits(xd->route);
  967. memcpy(&request.remote_uuid, xd->remote_uuid, sizeof(*xd->remote_uuid));
  968. memset(&reply, 0, sizeof(reply));
  969. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  970. 1, ICM_RETRIES, ICM_TIMEOUT);
  971. if (ret)
  972. return ret;
  973. if (reply.hdr.flags & ICM_FLAGS_ERROR)
  974. return -EIO;
  975. return 0;
  976. }
  977. static int icm_tr_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
  978. int transmit_path, int transmit_ring,
  979. int receive_path, int receive_ring)
  980. {
  981. int ret;
  982. ret = icm_tr_xdomain_tear_down(tb, xd, 1);
  983. if (ret)
  984. return ret;
  985. usleep_range(10, 50);
  986. return icm_tr_xdomain_tear_down(tb, xd, 2);
  987. }
  988. static void
  989. __icm_tr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr,
  990. bool force_rtd3)
  991. {
  992. const struct icm_tr_event_device_connected *pkg =
  993. (const struct icm_tr_event_device_connected *)hdr;
  994. bool authorized, boot, dual_lane, speed_gen3;
  995. enum tb_security_level security_level;
  996. struct tb_switch *sw, *parent_sw;
  997. struct tb_xdomain *xd;
  998. u64 route;
  999. icm_postpone_rescan(tb);
  1000. /*
  1001. * Currently we don't use the QoS information coming with the
  1002. * device connected message so simply just ignore that extra
  1003. * packet for now.
  1004. */
  1005. if (pkg->hdr.packet_id)
  1006. return;
  1007. route = get_route(pkg->route_hi, pkg->route_lo);
  1008. authorized = pkg->link_info & ICM_LINK_INFO_APPROVED;
  1009. security_level = (pkg->hdr.flags & ICM_FLAGS_SLEVEL_MASK) >>
  1010. ICM_FLAGS_SLEVEL_SHIFT;
  1011. boot = pkg->link_info & ICM_LINK_INFO_BOOT;
  1012. dual_lane = pkg->hdr.flags & ICM_FLAGS_DUAL_LANE;
  1013. speed_gen3 = pkg->hdr.flags & ICM_FLAGS_SPEED_GEN3;
  1014. if (pkg->link_info & ICM_LINK_INFO_REJECTED) {
  1015. tb_info(tb, "switch at %llx was rejected by ICM firmware because topology limit exceeded\n",
  1016. route);
  1017. return;
  1018. }
  1019. sw = tb_switch_find_by_uuid(tb, &pkg->ep_uuid);
  1020. if (sw) {
  1021. /* Update the switch if it is still in the same place */
  1022. if (tb_route(sw) == route && !!sw->authorized == authorized) {
  1023. update_switch(sw, route, pkg->connection_id, 0, 0, 0,
  1024. boot);
  1025. tb_switch_put(sw);
  1026. return;
  1027. }
  1028. remove_switch(sw);
  1029. tb_switch_put(sw);
  1030. }
  1031. /* Another switch with the same address */
  1032. sw = tb_switch_find_by_route(tb, route);
  1033. if (sw) {
  1034. remove_switch(sw);
  1035. tb_switch_put(sw);
  1036. }
  1037. /* XDomain connection with the same address */
  1038. xd = tb_xdomain_find_by_route(tb, route);
  1039. if (xd) {
  1040. remove_xdomain(xd);
  1041. tb_xdomain_put(xd);
  1042. }
  1043. parent_sw = tb_switch_find_by_route(tb, get_parent_route(route));
  1044. if (!parent_sw) {
  1045. tb_err(tb, "failed to find parent switch for %llx\n", route);
  1046. return;
  1047. }
  1048. pm_runtime_get_sync(&parent_sw->dev);
  1049. sw = alloc_switch(parent_sw, route, &pkg->ep_uuid);
  1050. if (!IS_ERR(sw)) {
  1051. sw->connection_id = pkg->connection_id;
  1052. sw->authorized = authorized;
  1053. sw->security_level = security_level;
  1054. sw->boot = boot;
  1055. sw->link_speed = speed_gen3 ? 20 : 10;
  1056. sw->link_width = dual_lane ? TB_LINK_WIDTH_DUAL :
  1057. TB_LINK_WIDTH_SINGLE;
  1058. sw->rpm = force_rtd3;
  1059. if (!sw->rpm)
  1060. sw->rpm = intel_vss_is_rtd3(pkg->ep_name,
  1061. sizeof(pkg->ep_name));
  1062. if (add_switch(parent_sw, sw))
  1063. tb_switch_put(sw);
  1064. }
  1065. pm_runtime_mark_last_busy(&parent_sw->dev);
  1066. pm_runtime_put_autosuspend(&parent_sw->dev);
  1067. tb_switch_put(parent_sw);
  1068. }
  1069. static void
  1070. icm_tr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr)
  1071. {
  1072. __icm_tr_device_connected(tb, hdr, false);
  1073. }
  1074. static void
  1075. icm_tr_device_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
  1076. {
  1077. const struct icm_tr_event_device_disconnected *pkg =
  1078. (const struct icm_tr_event_device_disconnected *)hdr;
  1079. struct tb_switch *sw;
  1080. u64 route;
  1081. route = get_route(pkg->route_hi, pkg->route_lo);
  1082. sw = tb_switch_find_by_route(tb, route);
  1083. if (!sw) {
  1084. tb_warn(tb, "no switch exists at %llx, ignoring\n", route);
  1085. return;
  1086. }
  1087. pm_runtime_get_sync(sw->dev.parent);
  1088. remove_switch(sw);
  1089. pm_runtime_mark_last_busy(sw->dev.parent);
  1090. pm_runtime_put_autosuspend(sw->dev.parent);
  1091. tb_switch_put(sw);
  1092. }
  1093. static void
  1094. icm_tr_xdomain_connected(struct tb *tb, const struct icm_pkg_header *hdr)
  1095. {
  1096. const struct icm_tr_event_xdomain_connected *pkg =
  1097. (const struct icm_tr_event_xdomain_connected *)hdr;
  1098. struct tb_xdomain *xd;
  1099. struct tb_switch *sw;
  1100. u64 route;
  1101. if (!tb->root_switch)
  1102. return;
  1103. route = get_route(pkg->local_route_hi, pkg->local_route_lo);
  1104. xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid);
  1105. if (xd) {
  1106. if (xd->route == route) {
  1107. update_xdomain(xd, route, 0);
  1108. tb_xdomain_put(xd);
  1109. return;
  1110. }
  1111. remove_xdomain(xd);
  1112. tb_xdomain_put(xd);
  1113. }
  1114. /* An existing xdomain with the same address */
  1115. xd = tb_xdomain_find_by_route(tb, route);
  1116. if (xd) {
  1117. remove_xdomain(xd);
  1118. tb_xdomain_put(xd);
  1119. }
  1120. /*
  1121. * If the user disconnected a switch during suspend and
  1122. * connected another host to the same port, remove the switch
  1123. * first.
  1124. */
  1125. sw = tb_switch_find_by_route(tb, route);
  1126. if (sw) {
  1127. remove_switch(sw);
  1128. tb_switch_put(sw);
  1129. }
  1130. sw = tb_switch_find_by_route(tb, get_parent_route(route));
  1131. if (!sw) {
  1132. tb_warn(tb, "no switch exists at %llx, ignoring\n", route);
  1133. return;
  1134. }
  1135. add_xdomain(sw, route, &pkg->local_uuid, &pkg->remote_uuid, 0, 0);
  1136. tb_switch_put(sw);
  1137. }
  1138. static void
  1139. icm_tr_xdomain_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
  1140. {
  1141. const struct icm_tr_event_xdomain_disconnected *pkg =
  1142. (const struct icm_tr_event_xdomain_disconnected *)hdr;
  1143. struct tb_xdomain *xd;
  1144. u64 route;
  1145. route = get_route(pkg->route_hi, pkg->route_lo);
  1146. xd = tb_xdomain_find_by_route(tb, route);
  1147. if (xd) {
  1148. remove_xdomain(xd);
  1149. tb_xdomain_put(xd);
  1150. }
  1151. }
  1152. static struct pci_dev *get_upstream_port(struct pci_dev *pdev)
  1153. {
  1154. struct pci_dev *parent;
  1155. parent = pci_upstream_bridge(pdev);
  1156. while (parent) {
  1157. if (!pci_is_pcie(parent))
  1158. return NULL;
  1159. if (pci_pcie_type(parent) == PCI_EXP_TYPE_UPSTREAM)
  1160. break;
  1161. parent = pci_upstream_bridge(parent);
  1162. }
  1163. if (!parent)
  1164. return NULL;
  1165. switch (parent->device) {
  1166. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE:
  1167. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE:
  1168. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE:
  1169. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE:
  1170. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE:
  1171. case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_BRIDGE:
  1172. case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_BRIDGE:
  1173. return parent;
  1174. }
  1175. return NULL;
  1176. }
  1177. static bool icm_ar_is_supported(struct tb *tb)
  1178. {
  1179. struct pci_dev *upstream_port;
  1180. struct icm *icm = tb_priv(tb);
  1181. /*
  1182. * Starting from Alpine Ridge we can use ICM on Apple machines
  1183. * as well. We just need to reset and re-enable it first.
  1184. * However, only start it if explicitly asked by the user.
  1185. */
  1186. if (icm_firmware_running(tb->nhi))
  1187. return true;
  1188. if (!start_icm)
  1189. return false;
  1190. /*
  1191. * Find the upstream PCIe port in case we need to do reset
  1192. * through its vendor specific registers.
  1193. */
  1194. upstream_port = get_upstream_port(tb->nhi->pdev);
  1195. if (upstream_port) {
  1196. int cap;
  1197. cap = pci_find_ext_capability(upstream_port,
  1198. PCI_EXT_CAP_ID_VNDR);
  1199. if (cap > 0) {
  1200. icm->upstream_port = upstream_port;
  1201. icm->vnd_cap = cap;
  1202. return true;
  1203. }
  1204. }
  1205. return false;
  1206. }
  1207. static int icm_ar_cio_reset(struct tb *tb)
  1208. {
  1209. return pcie2cio_write(tb_priv(tb), TB_CFG_SWITCH, 0, 0x50, BIT(9));
  1210. }
  1211. static int icm_ar_get_mode(struct tb *tb)
  1212. {
  1213. struct tb_nhi *nhi = tb->nhi;
  1214. int retries = 60;
  1215. u32 val;
  1216. do {
  1217. val = ioread32(nhi->iobase + REG_FW_STS);
  1218. if (val & REG_FW_STS_NVM_AUTH_DONE)
  1219. break;
  1220. msleep(50);
  1221. } while (--retries);
  1222. if (!retries) {
  1223. dev_err(&nhi->pdev->dev, "ICM firmware not authenticated\n");
  1224. return -ENODEV;
  1225. }
  1226. return nhi_mailbox_mode(nhi);
  1227. }
  1228. static int
  1229. icm_ar_driver_ready(struct tb *tb, enum tb_security_level *security_level,
  1230. u8 *proto_version, size_t *nboot_acl, bool *rpm)
  1231. {
  1232. struct icm_ar_pkg_driver_ready_response reply;
  1233. struct icm_pkg_driver_ready request = {
  1234. .hdr.code = ICM_DRIVER_READY,
  1235. };
  1236. int ret;
  1237. memset(&reply, 0, sizeof(reply));
  1238. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  1239. 1, ICM_RETRIES, ICM_TIMEOUT);
  1240. if (ret)
  1241. return ret;
  1242. if (security_level)
  1243. *security_level = reply.info & ICM_AR_INFO_SLEVEL_MASK;
  1244. if (nboot_acl && (reply.info & ICM_AR_INFO_BOOT_ACL_SUPPORTED))
  1245. *nboot_acl = (reply.info & ICM_AR_INFO_BOOT_ACL_MASK) >>
  1246. ICM_AR_INFO_BOOT_ACL_SHIFT;
  1247. if (rpm)
  1248. *rpm = !!(reply.hdr.flags & ICM_AR_FLAGS_RTD3);
  1249. return 0;
  1250. }
  1251. static int icm_ar_get_route(struct tb *tb, u8 link, u8 depth, u64 *route)
  1252. {
  1253. struct icm_ar_pkg_get_route_response reply;
  1254. struct icm_ar_pkg_get_route request = {
  1255. .hdr = { .code = ICM_GET_ROUTE },
  1256. .link_info = depth << ICM_LINK_INFO_DEPTH_SHIFT | link,
  1257. };
  1258. int ret;
  1259. memset(&reply, 0, sizeof(reply));
  1260. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  1261. 1, ICM_RETRIES, ICM_TIMEOUT);
  1262. if (ret)
  1263. return ret;
  1264. if (reply.hdr.flags & ICM_FLAGS_ERROR)
  1265. return -EIO;
  1266. *route = get_route(reply.route_hi, reply.route_lo);
  1267. return 0;
  1268. }
  1269. static int icm_ar_get_boot_acl(struct tb *tb, uuid_t *uuids, size_t nuuids)
  1270. {
  1271. struct icm_ar_pkg_preboot_acl_response reply;
  1272. struct icm_ar_pkg_preboot_acl request = {
  1273. .hdr = { .code = ICM_PREBOOT_ACL },
  1274. };
  1275. int ret, i;
  1276. memset(&reply, 0, sizeof(reply));
  1277. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  1278. 1, ICM_RETRIES, ICM_TIMEOUT);
  1279. if (ret)
  1280. return ret;
  1281. if (reply.hdr.flags & ICM_FLAGS_ERROR)
  1282. return -EIO;
  1283. for (i = 0; i < nuuids; i++) {
  1284. u32 *uuid = (u32 *)&uuids[i];
  1285. uuid[0] = reply.acl[i].uuid_lo;
  1286. uuid[1] = reply.acl[i].uuid_hi;
  1287. if (uuid[0] == 0xffffffff && uuid[1] == 0xffffffff) {
  1288. /* Map empty entries to null UUID */
  1289. uuid[0] = 0;
  1290. uuid[1] = 0;
  1291. } else if (uuid[0] != 0 || uuid[1] != 0) {
  1292. /* Upper two DWs are always one's */
  1293. uuid[2] = 0xffffffff;
  1294. uuid[3] = 0xffffffff;
  1295. }
  1296. }
  1297. return ret;
  1298. }
  1299. static int icm_ar_set_boot_acl(struct tb *tb, const uuid_t *uuids,
  1300. size_t nuuids)
  1301. {
  1302. struct icm_ar_pkg_preboot_acl_response reply;
  1303. struct icm_ar_pkg_preboot_acl request = {
  1304. .hdr = {
  1305. .code = ICM_PREBOOT_ACL,
  1306. .flags = ICM_FLAGS_WRITE,
  1307. },
  1308. };
  1309. int ret, i;
  1310. for (i = 0; i < nuuids; i++) {
  1311. const u32 *uuid = (const u32 *)&uuids[i];
  1312. if (uuid_is_null(&uuids[i])) {
  1313. /*
  1314. * Map null UUID to the empty (all one) entries
  1315. * for ICM.
  1316. */
  1317. request.acl[i].uuid_lo = 0xffffffff;
  1318. request.acl[i].uuid_hi = 0xffffffff;
  1319. } else {
  1320. /* Two high DWs need to be set to all one */
  1321. if (uuid[2] != 0xffffffff || uuid[3] != 0xffffffff)
  1322. return -EINVAL;
  1323. request.acl[i].uuid_lo = uuid[0];
  1324. request.acl[i].uuid_hi = uuid[1];
  1325. }
  1326. }
  1327. memset(&reply, 0, sizeof(reply));
  1328. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  1329. 1, ICM_RETRIES, ICM_TIMEOUT);
  1330. if (ret)
  1331. return ret;
  1332. if (reply.hdr.flags & ICM_FLAGS_ERROR)
  1333. return -EIO;
  1334. return 0;
  1335. }
  1336. static int
  1337. icm_icl_driver_ready(struct tb *tb, enum tb_security_level *security_level,
  1338. u8 *proto_version, size_t *nboot_acl, bool *rpm)
  1339. {
  1340. struct icm_tr_pkg_driver_ready_response reply;
  1341. struct icm_pkg_driver_ready request = {
  1342. .hdr.code = ICM_DRIVER_READY,
  1343. };
  1344. int ret;
  1345. memset(&reply, 0, sizeof(reply));
  1346. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  1347. 1, ICM_RETRIES, 20000);
  1348. if (ret)
  1349. return ret;
  1350. if (proto_version)
  1351. *proto_version = (reply.info & ICM_TR_INFO_PROTO_VERSION_MASK) >>
  1352. ICM_TR_INFO_PROTO_VERSION_SHIFT;
  1353. /* Ice Lake always supports RTD3 */
  1354. if (rpm)
  1355. *rpm = true;
  1356. return 0;
  1357. }
  1358. static void icm_icl_set_uuid(struct tb *tb)
  1359. {
  1360. struct tb_nhi *nhi = tb->nhi;
  1361. u32 uuid[4];
  1362. pci_read_config_dword(nhi->pdev, VS_CAP_10, &uuid[0]);
  1363. pci_read_config_dword(nhi->pdev, VS_CAP_11, &uuid[1]);
  1364. uuid[2] = 0xffffffff;
  1365. uuid[3] = 0xffffffff;
  1366. tb->root_switch->uuid = kmemdup(uuid, sizeof(uuid), GFP_KERNEL);
  1367. }
  1368. static void
  1369. icm_icl_device_connected(struct tb *tb, const struct icm_pkg_header *hdr)
  1370. {
  1371. __icm_tr_device_connected(tb, hdr, true);
  1372. }
  1373. static void icm_icl_rtd3_veto(struct tb *tb, const struct icm_pkg_header *hdr)
  1374. {
  1375. const struct icm_icl_event_rtd3_veto *pkg =
  1376. (const struct icm_icl_event_rtd3_veto *)hdr;
  1377. tb_dbg(tb, "ICM rtd3 veto=0x%08x\n", pkg->veto_reason);
  1378. if (pkg->veto_reason)
  1379. icm_veto_begin(tb);
  1380. else
  1381. icm_veto_end(tb);
  1382. }
  1383. static bool icm_tgl_is_supported(struct tb *tb)
  1384. {
  1385. unsigned long end = jiffies + msecs_to_jiffies(10);
  1386. do {
  1387. u32 val;
  1388. val = ioread32(tb->nhi->iobase + REG_FW_STS);
  1389. if (val & REG_FW_STS_NVM_AUTH_DONE)
  1390. return true;
  1391. usleep_range(100, 500);
  1392. } while (time_before(jiffies, end));
  1393. return false;
  1394. }
  1395. static void icm_handle_notification(struct work_struct *work)
  1396. {
  1397. struct icm_notification *n = container_of(work, typeof(*n), work);
  1398. struct tb *tb = n->tb;
  1399. struct icm *icm = tb_priv(tb);
  1400. mutex_lock(&tb->lock);
  1401. /*
  1402. * When the domain is stopped we flush its workqueue but before
  1403. * that the root switch is removed. In that case we should treat
  1404. * the queued events as being canceled.
  1405. */
  1406. if (tb->root_switch) {
  1407. switch (n->pkg->code) {
  1408. case ICM_EVENT_DEVICE_CONNECTED:
  1409. icm->device_connected(tb, n->pkg);
  1410. break;
  1411. case ICM_EVENT_DEVICE_DISCONNECTED:
  1412. icm->device_disconnected(tb, n->pkg);
  1413. break;
  1414. case ICM_EVENT_XDOMAIN_CONNECTED:
  1415. if (tb_is_xdomain_enabled())
  1416. icm->xdomain_connected(tb, n->pkg);
  1417. break;
  1418. case ICM_EVENT_XDOMAIN_DISCONNECTED:
  1419. if (tb_is_xdomain_enabled())
  1420. icm->xdomain_disconnected(tb, n->pkg);
  1421. break;
  1422. case ICM_EVENT_RTD3_VETO:
  1423. icm->rtd3_veto(tb, n->pkg);
  1424. break;
  1425. }
  1426. }
  1427. mutex_unlock(&tb->lock);
  1428. kfree(n->pkg);
  1429. kfree(n);
  1430. }
  1431. static void icm_handle_event(struct tb *tb, enum tb_cfg_pkg_type type,
  1432. const void *buf, size_t size)
  1433. {
  1434. struct icm_notification *n;
  1435. n = kmalloc(sizeof(*n), GFP_KERNEL);
  1436. if (!n)
  1437. return;
  1438. n->pkg = kmemdup(buf, size, GFP_KERNEL);
  1439. if (!n->pkg) {
  1440. kfree(n);
  1441. return;
  1442. }
  1443. INIT_WORK(&n->work, icm_handle_notification);
  1444. n->tb = tb;
  1445. queue_work(tb->wq, &n->work);
  1446. }
  1447. static int
  1448. __icm_driver_ready(struct tb *tb, enum tb_security_level *security_level,
  1449. u8 *proto_version, size_t *nboot_acl, bool *rpm)
  1450. {
  1451. struct icm *icm = tb_priv(tb);
  1452. unsigned int retries = 50;
  1453. int ret;
  1454. ret = icm->driver_ready(tb, security_level, proto_version, nboot_acl,
  1455. rpm);
  1456. if (ret) {
  1457. tb_err(tb, "failed to send driver ready to ICM\n");
  1458. return ret;
  1459. }
  1460. /*
  1461. * Hold on here until the switch config space is accessible so
  1462. * that we can read root switch config successfully.
  1463. */
  1464. do {
  1465. struct tb_cfg_result res;
  1466. u32 tmp;
  1467. res = tb_cfg_read_raw(tb->ctl, &tmp, 0, 0, TB_CFG_SWITCH,
  1468. 0, 1, 100);
  1469. if (!res.err)
  1470. return 0;
  1471. msleep(50);
  1472. } while (--retries);
  1473. tb_err(tb, "failed to read root switch config space, giving up\n");
  1474. return -ETIMEDOUT;
  1475. }
  1476. static int icm_firmware_reset(struct tb *tb, struct tb_nhi *nhi)
  1477. {
  1478. struct icm *icm = tb_priv(tb);
  1479. u32 val;
  1480. if (!icm->upstream_port)
  1481. return -ENODEV;
  1482. /* Put ARC to wait for CIO reset event to happen */
  1483. val = ioread32(nhi->iobase + REG_FW_STS);
  1484. val |= REG_FW_STS_CIO_RESET_REQ;
  1485. iowrite32(val, nhi->iobase + REG_FW_STS);
  1486. /* Re-start ARC */
  1487. val = ioread32(nhi->iobase + REG_FW_STS);
  1488. val |= REG_FW_STS_ICM_EN_INVERT;
  1489. val |= REG_FW_STS_ICM_EN_CPU;
  1490. iowrite32(val, nhi->iobase + REG_FW_STS);
  1491. /* Trigger CIO reset now */
  1492. return icm->cio_reset(tb);
  1493. }
  1494. static int icm_firmware_start(struct tb *tb, struct tb_nhi *nhi)
  1495. {
  1496. unsigned int retries = 10;
  1497. int ret;
  1498. u32 val;
  1499. /* Check if the ICM firmware is already running */
  1500. if (icm_firmware_running(nhi))
  1501. return 0;
  1502. dev_dbg(&nhi->pdev->dev, "starting ICM firmware\n");
  1503. ret = icm_firmware_reset(tb, nhi);
  1504. if (ret)
  1505. return ret;
  1506. /* Wait until the ICM firmware tells us it is up and running */
  1507. do {
  1508. /* Check that the ICM firmware is running */
  1509. val = ioread32(nhi->iobase + REG_FW_STS);
  1510. if (val & REG_FW_STS_NVM_AUTH_DONE)
  1511. return 0;
  1512. msleep(300);
  1513. } while (--retries);
  1514. return -ETIMEDOUT;
  1515. }
  1516. static int icm_reset_phy_port(struct tb *tb, int phy_port)
  1517. {
  1518. struct icm *icm = tb_priv(tb);
  1519. u32 state0, state1;
  1520. int port0, port1;
  1521. u32 val0, val1;
  1522. int ret;
  1523. if (!icm->upstream_port)
  1524. return 0;
  1525. if (phy_port) {
  1526. port0 = 3;
  1527. port1 = 4;
  1528. } else {
  1529. port0 = 1;
  1530. port1 = 2;
  1531. }
  1532. /*
  1533. * Read link status of both null ports belonging to a single
  1534. * physical port.
  1535. */
  1536. ret = pcie2cio_read(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, &val0);
  1537. if (ret)
  1538. return ret;
  1539. ret = pcie2cio_read(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, &val1);
  1540. if (ret)
  1541. return ret;
  1542. state0 = val0 & PHY_PORT_CS1_LINK_STATE_MASK;
  1543. state0 >>= PHY_PORT_CS1_LINK_STATE_SHIFT;
  1544. state1 = val1 & PHY_PORT_CS1_LINK_STATE_MASK;
  1545. state1 >>= PHY_PORT_CS1_LINK_STATE_SHIFT;
  1546. /* If they are both up we need to reset them now */
  1547. if (state0 != TB_PORT_UP || state1 != TB_PORT_UP)
  1548. return 0;
  1549. val0 |= PHY_PORT_CS1_LINK_DISABLE;
  1550. ret = pcie2cio_write(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, val0);
  1551. if (ret)
  1552. return ret;
  1553. val1 |= PHY_PORT_CS1_LINK_DISABLE;
  1554. ret = pcie2cio_write(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, val1);
  1555. if (ret)
  1556. return ret;
  1557. /* Wait a bit and then re-enable both ports */
  1558. usleep_range(10, 100);
  1559. ret = pcie2cio_read(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, &val0);
  1560. if (ret)
  1561. return ret;
  1562. ret = pcie2cio_read(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, &val1);
  1563. if (ret)
  1564. return ret;
  1565. val0 &= ~PHY_PORT_CS1_LINK_DISABLE;
  1566. ret = pcie2cio_write(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, val0);
  1567. if (ret)
  1568. return ret;
  1569. val1 &= ~PHY_PORT_CS1_LINK_DISABLE;
  1570. return pcie2cio_write(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, val1);
  1571. }
  1572. static int icm_firmware_init(struct tb *tb)
  1573. {
  1574. struct icm *icm = tb_priv(tb);
  1575. struct tb_nhi *nhi = tb->nhi;
  1576. int ret;
  1577. ret = icm_firmware_start(tb, nhi);
  1578. if (ret) {
  1579. dev_err(&nhi->pdev->dev, "could not start ICM firmware\n");
  1580. return ret;
  1581. }
  1582. if (icm->get_mode) {
  1583. ret = icm->get_mode(tb);
  1584. switch (ret) {
  1585. case NHI_FW_SAFE_MODE:
  1586. icm->safe_mode = true;
  1587. break;
  1588. case NHI_FW_CM_MODE:
  1589. /* Ask ICM to accept all Thunderbolt devices */
  1590. nhi_mailbox_cmd(nhi, NHI_MAILBOX_ALLOW_ALL_DEVS, 0);
  1591. break;
  1592. default:
  1593. if (ret < 0)
  1594. return ret;
  1595. tb_err(tb, "ICM firmware is in wrong mode: %u\n", ret);
  1596. return -ENODEV;
  1597. }
  1598. }
  1599. /*
  1600. * Reset both physical ports if there is anything connected to
  1601. * them already.
  1602. */
  1603. ret = icm_reset_phy_port(tb, 0);
  1604. if (ret)
  1605. dev_warn(&nhi->pdev->dev, "failed to reset links on port0\n");
  1606. ret = icm_reset_phy_port(tb, 1);
  1607. if (ret)
  1608. dev_warn(&nhi->pdev->dev, "failed to reset links on port1\n");
  1609. return 0;
  1610. }
  1611. static int icm_driver_ready(struct tb *tb)
  1612. {
  1613. struct icm *icm = tb_priv(tb);
  1614. int ret;
  1615. ret = icm_firmware_init(tb);
  1616. if (ret)
  1617. return ret;
  1618. if (icm->safe_mode) {
  1619. tb_info(tb, "Thunderbolt host controller is in safe mode.\n");
  1620. tb_info(tb, "You need to update NVM firmware of the controller before it can be used.\n");
  1621. tb_info(tb, "For latest updates check https://thunderbolttechnology.net/updates.\n");
  1622. return 0;
  1623. }
  1624. ret = __icm_driver_ready(tb, &tb->security_level, &icm->proto_version,
  1625. &tb->nboot_acl, &icm->rpm);
  1626. if (ret)
  1627. return ret;
  1628. /*
  1629. * Make sure the number of supported preboot ACL matches what we
  1630. * expect or disable the whole feature.
  1631. */
  1632. if (tb->nboot_acl > icm->max_boot_acl)
  1633. tb->nboot_acl = 0;
  1634. if (icm->proto_version >= 3)
  1635. tb_dbg(tb, "USB4 proxy operations supported\n");
  1636. return 0;
  1637. }
  1638. static int icm_suspend(struct tb *tb)
  1639. {
  1640. struct icm *icm = tb_priv(tb);
  1641. if (icm->save_devices)
  1642. icm->save_devices(tb);
  1643. nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0);
  1644. return 0;
  1645. }
  1646. /*
  1647. * Mark all switches (except root switch) below this one unplugged. ICM
  1648. * firmware will send us an updated list of switches after we have send
  1649. * it driver ready command. If a switch is not in that list it will be
  1650. * removed when we perform rescan.
  1651. */
  1652. static void icm_unplug_children(struct tb_switch *sw)
  1653. {
  1654. struct tb_port *port;
  1655. if (tb_route(sw))
  1656. sw->is_unplugged = true;
  1657. tb_switch_for_each_port(sw, port) {
  1658. if (port->xdomain)
  1659. port->xdomain->is_unplugged = true;
  1660. else if (tb_port_has_remote(port))
  1661. icm_unplug_children(port->remote->sw);
  1662. }
  1663. }
  1664. static int complete_rpm(struct device *dev, void *data)
  1665. {
  1666. struct tb_switch *sw = tb_to_switch(dev);
  1667. if (sw)
  1668. complete(&sw->rpm_complete);
  1669. return 0;
  1670. }
  1671. static void remove_unplugged_switch(struct tb_switch *sw)
  1672. {
  1673. struct device *parent = get_device(sw->dev.parent);
  1674. pm_runtime_get_sync(parent);
  1675. /*
  1676. * Signal this and switches below for rpm_complete because
  1677. * tb_switch_remove() calls pm_runtime_get_sync() that then waits
  1678. * for it.
  1679. */
  1680. complete_rpm(&sw->dev, NULL);
  1681. bus_for_each_dev(&tb_bus_type, &sw->dev, NULL, complete_rpm);
  1682. tb_switch_remove(sw);
  1683. pm_runtime_mark_last_busy(parent);
  1684. pm_runtime_put_autosuspend(parent);
  1685. put_device(parent);
  1686. }
  1687. static void icm_free_unplugged_children(struct tb_switch *sw)
  1688. {
  1689. struct tb_port *port;
  1690. tb_switch_for_each_port(sw, port) {
  1691. if (port->xdomain && port->xdomain->is_unplugged) {
  1692. tb_xdomain_remove(port->xdomain);
  1693. port->xdomain = NULL;
  1694. } else if (tb_port_has_remote(port)) {
  1695. if (port->remote->sw->is_unplugged) {
  1696. remove_unplugged_switch(port->remote->sw);
  1697. port->remote = NULL;
  1698. } else {
  1699. icm_free_unplugged_children(port->remote->sw);
  1700. }
  1701. }
  1702. }
  1703. }
  1704. static void icm_rescan_work(struct work_struct *work)
  1705. {
  1706. struct icm *icm = container_of(work, struct icm, rescan_work.work);
  1707. struct tb *tb = icm_to_tb(icm);
  1708. mutex_lock(&tb->lock);
  1709. if (tb->root_switch)
  1710. icm_free_unplugged_children(tb->root_switch);
  1711. mutex_unlock(&tb->lock);
  1712. }
  1713. static void icm_complete(struct tb *tb)
  1714. {
  1715. struct icm *icm = tb_priv(tb);
  1716. if (tb->nhi->going_away)
  1717. return;
  1718. /*
  1719. * If RTD3 was vetoed before we entered system suspend allow it
  1720. * again now before driver ready is sent. Firmware sends a new RTD3
  1721. * veto if it is still the case after we have sent it driver ready
  1722. * command.
  1723. */
  1724. icm_veto_end(tb);
  1725. icm_unplug_children(tb->root_switch);
  1726. /*
  1727. * Now all existing children should be resumed, start events
  1728. * from ICM to get updated status.
  1729. */
  1730. __icm_driver_ready(tb, NULL, NULL, NULL, NULL);
  1731. /*
  1732. * We do not get notifications of devices that have been
  1733. * unplugged during suspend so schedule rescan to clean them up
  1734. * if any.
  1735. */
  1736. queue_delayed_work(tb->wq, &icm->rescan_work, msecs_to_jiffies(500));
  1737. }
  1738. static int icm_runtime_suspend(struct tb *tb)
  1739. {
  1740. nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0);
  1741. return 0;
  1742. }
  1743. static int icm_runtime_suspend_switch(struct tb_switch *sw)
  1744. {
  1745. if (tb_route(sw))
  1746. reinit_completion(&sw->rpm_complete);
  1747. return 0;
  1748. }
  1749. static int icm_runtime_resume_switch(struct tb_switch *sw)
  1750. {
  1751. if (tb_route(sw)) {
  1752. if (!wait_for_completion_timeout(&sw->rpm_complete,
  1753. msecs_to_jiffies(500))) {
  1754. dev_dbg(&sw->dev, "runtime resuming timed out\n");
  1755. }
  1756. }
  1757. return 0;
  1758. }
  1759. static int icm_runtime_resume(struct tb *tb)
  1760. {
  1761. /*
  1762. * We can reuse the same resume functionality than with system
  1763. * suspend.
  1764. */
  1765. icm_complete(tb);
  1766. return 0;
  1767. }
  1768. static int icm_start(struct tb *tb, bool not_used)
  1769. {
  1770. struct icm *icm = tb_priv(tb);
  1771. int ret;
  1772. if (icm->safe_mode)
  1773. tb->root_switch = tb_switch_alloc_safe_mode(tb, &tb->dev, 0);
  1774. else
  1775. tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0);
  1776. if (IS_ERR(tb->root_switch))
  1777. return PTR_ERR(tb->root_switch);
  1778. tb->root_switch->no_nvm_upgrade = !icm->can_upgrade_nvm;
  1779. tb->root_switch->rpm = icm->rpm;
  1780. if (icm->set_uuid)
  1781. icm->set_uuid(tb);
  1782. ret = tb_switch_add(tb->root_switch);
  1783. if (ret) {
  1784. tb_switch_put(tb->root_switch);
  1785. tb->root_switch = NULL;
  1786. }
  1787. return ret;
  1788. }
  1789. static void icm_stop(struct tb *tb)
  1790. {
  1791. struct icm *icm = tb_priv(tb);
  1792. cancel_delayed_work(&icm->rescan_work);
  1793. tb_switch_remove(tb->root_switch);
  1794. tb->root_switch = NULL;
  1795. nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0);
  1796. kfree(icm->last_nvm_auth);
  1797. icm->last_nvm_auth = NULL;
  1798. }
  1799. static int icm_disconnect_pcie_paths(struct tb *tb)
  1800. {
  1801. return nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DISCONNECT_PCIE_PATHS, 0);
  1802. }
  1803. static void icm_usb4_switch_nvm_auth_complete(void *data)
  1804. {
  1805. struct usb4_switch_nvm_auth *auth = data;
  1806. struct icm *icm = auth->icm;
  1807. struct tb *tb = icm_to_tb(icm);
  1808. tb_dbg(tb, "NVM_AUTH response for %llx flags %#x status %#x\n",
  1809. get_route(auth->reply.route_hi, auth->reply.route_lo),
  1810. auth->reply.hdr.flags, auth->reply.status);
  1811. mutex_lock(&tb->lock);
  1812. if (WARN_ON(icm->last_nvm_auth))
  1813. kfree(icm->last_nvm_auth);
  1814. icm->last_nvm_auth = auth;
  1815. mutex_unlock(&tb->lock);
  1816. }
  1817. static int icm_usb4_switch_nvm_authenticate(struct tb *tb, u64 route)
  1818. {
  1819. struct usb4_switch_nvm_auth *auth;
  1820. struct icm *icm = tb_priv(tb);
  1821. struct tb_cfg_request *req;
  1822. int ret;
  1823. auth = kzalloc(sizeof(*auth), GFP_KERNEL);
  1824. if (!auth)
  1825. return -ENOMEM;
  1826. auth->icm = icm;
  1827. auth->request.hdr.code = ICM_USB4_SWITCH_OP;
  1828. auth->request.route_hi = upper_32_bits(route);
  1829. auth->request.route_lo = lower_32_bits(route);
  1830. auth->request.opcode = USB4_SWITCH_OP_NVM_AUTH;
  1831. req = tb_cfg_request_alloc();
  1832. if (!req) {
  1833. ret = -ENOMEM;
  1834. goto err_free_auth;
  1835. }
  1836. req->match = icm_match;
  1837. req->copy = icm_copy;
  1838. req->request = &auth->request;
  1839. req->request_size = sizeof(auth->request);
  1840. req->request_type = TB_CFG_PKG_ICM_CMD;
  1841. req->response = &auth->reply;
  1842. req->npackets = 1;
  1843. req->response_size = sizeof(auth->reply);
  1844. req->response_type = TB_CFG_PKG_ICM_RESP;
  1845. tb_dbg(tb, "NVM_AUTH request for %llx\n", route);
  1846. mutex_lock(&icm->request_lock);
  1847. ret = tb_cfg_request(tb->ctl, req, icm_usb4_switch_nvm_auth_complete,
  1848. auth);
  1849. mutex_unlock(&icm->request_lock);
  1850. tb_cfg_request_put(req);
  1851. if (ret)
  1852. goto err_free_auth;
  1853. return 0;
  1854. err_free_auth:
  1855. kfree(auth);
  1856. return ret;
  1857. }
  1858. static int icm_usb4_switch_op(struct tb_switch *sw, u16 opcode, u32 *metadata,
  1859. u8 *status, const void *tx_data, size_t tx_data_len,
  1860. void *rx_data, size_t rx_data_len)
  1861. {
  1862. struct icm_usb4_switch_op_response reply;
  1863. struct icm_usb4_switch_op request;
  1864. struct tb *tb = sw->tb;
  1865. struct icm *icm = tb_priv(tb);
  1866. u64 route = tb_route(sw);
  1867. int ret;
  1868. /*
  1869. * USB4 router operation proxy is supported in firmware if the
  1870. * protocol version is 3 or higher.
  1871. */
  1872. if (icm->proto_version < 3)
  1873. return -EOPNOTSUPP;
  1874. /*
  1875. * NVM_AUTH is a special USB4 proxy operation that does not
  1876. * return immediately so handle it separately.
  1877. */
  1878. if (opcode == USB4_SWITCH_OP_NVM_AUTH)
  1879. return icm_usb4_switch_nvm_authenticate(tb, route);
  1880. memset(&request, 0, sizeof(request));
  1881. request.hdr.code = ICM_USB4_SWITCH_OP;
  1882. request.route_hi = upper_32_bits(route);
  1883. request.route_lo = lower_32_bits(route);
  1884. request.opcode = opcode;
  1885. if (metadata)
  1886. request.metadata = *metadata;
  1887. if (tx_data_len) {
  1888. request.data_len_valid |= ICM_USB4_SWITCH_DATA_VALID;
  1889. if (tx_data_len < ARRAY_SIZE(request.data))
  1890. request.data_len_valid =
  1891. tx_data_len & ICM_USB4_SWITCH_DATA_LEN_MASK;
  1892. memcpy(request.data, tx_data, tx_data_len * sizeof(u32));
  1893. }
  1894. memset(&reply, 0, sizeof(reply));
  1895. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  1896. 1, ICM_RETRIES, ICM_TIMEOUT);
  1897. if (ret)
  1898. return ret;
  1899. if (reply.hdr.flags & ICM_FLAGS_ERROR)
  1900. return -EIO;
  1901. if (status)
  1902. *status = reply.status;
  1903. if (metadata)
  1904. *metadata = reply.metadata;
  1905. if (rx_data_len)
  1906. memcpy(rx_data, reply.data, rx_data_len * sizeof(u32));
  1907. return 0;
  1908. }
  1909. static int icm_usb4_switch_nvm_authenticate_status(struct tb_switch *sw,
  1910. u32 *status)
  1911. {
  1912. struct usb4_switch_nvm_auth *auth;
  1913. struct tb *tb = sw->tb;
  1914. struct icm *icm = tb_priv(tb);
  1915. int ret = 0;
  1916. if (icm->proto_version < 3)
  1917. return -EOPNOTSUPP;
  1918. auth = icm->last_nvm_auth;
  1919. icm->last_nvm_auth = NULL;
  1920. if (auth && auth->reply.route_hi == sw->config.route_hi &&
  1921. auth->reply.route_lo == sw->config.route_lo) {
  1922. tb_dbg(tb, "NVM_AUTH found for %llx flags %#x status %#x\n",
  1923. tb_route(sw), auth->reply.hdr.flags, auth->reply.status);
  1924. if (auth->reply.hdr.flags & ICM_FLAGS_ERROR)
  1925. ret = -EIO;
  1926. else
  1927. *status = auth->reply.status;
  1928. } else {
  1929. *status = 0;
  1930. }
  1931. kfree(auth);
  1932. return ret;
  1933. }
  1934. /* Falcon Ridge */
  1935. static const struct tb_cm_ops icm_fr_ops = {
  1936. .driver_ready = icm_driver_ready,
  1937. .start = icm_start,
  1938. .stop = icm_stop,
  1939. .suspend = icm_suspend,
  1940. .complete = icm_complete,
  1941. .handle_event = icm_handle_event,
  1942. .approve_switch = icm_fr_approve_switch,
  1943. .add_switch_key = icm_fr_add_switch_key,
  1944. .challenge_switch_key = icm_fr_challenge_switch_key,
  1945. .disconnect_pcie_paths = icm_disconnect_pcie_paths,
  1946. .approve_xdomain_paths = icm_fr_approve_xdomain_paths,
  1947. .disconnect_xdomain_paths = icm_fr_disconnect_xdomain_paths,
  1948. };
  1949. /* Alpine Ridge */
  1950. static const struct tb_cm_ops icm_ar_ops = {
  1951. .driver_ready = icm_driver_ready,
  1952. .start = icm_start,
  1953. .stop = icm_stop,
  1954. .suspend = icm_suspend,
  1955. .complete = icm_complete,
  1956. .runtime_suspend = icm_runtime_suspend,
  1957. .runtime_resume = icm_runtime_resume,
  1958. .runtime_suspend_switch = icm_runtime_suspend_switch,
  1959. .runtime_resume_switch = icm_runtime_resume_switch,
  1960. .handle_event = icm_handle_event,
  1961. .get_boot_acl = icm_ar_get_boot_acl,
  1962. .set_boot_acl = icm_ar_set_boot_acl,
  1963. .approve_switch = icm_fr_approve_switch,
  1964. .add_switch_key = icm_fr_add_switch_key,
  1965. .challenge_switch_key = icm_fr_challenge_switch_key,
  1966. .disconnect_pcie_paths = icm_disconnect_pcie_paths,
  1967. .approve_xdomain_paths = icm_fr_approve_xdomain_paths,
  1968. .disconnect_xdomain_paths = icm_fr_disconnect_xdomain_paths,
  1969. };
  1970. /* Titan Ridge */
  1971. static const struct tb_cm_ops icm_tr_ops = {
  1972. .driver_ready = icm_driver_ready,
  1973. .start = icm_start,
  1974. .stop = icm_stop,
  1975. .suspend = icm_suspend,
  1976. .complete = icm_complete,
  1977. .runtime_suspend = icm_runtime_suspend,
  1978. .runtime_resume = icm_runtime_resume,
  1979. .runtime_suspend_switch = icm_runtime_suspend_switch,
  1980. .runtime_resume_switch = icm_runtime_resume_switch,
  1981. .handle_event = icm_handle_event,
  1982. .get_boot_acl = icm_ar_get_boot_acl,
  1983. .set_boot_acl = icm_ar_set_boot_acl,
  1984. .approve_switch = icm_tr_approve_switch,
  1985. .add_switch_key = icm_tr_add_switch_key,
  1986. .challenge_switch_key = icm_tr_challenge_switch_key,
  1987. .disconnect_pcie_paths = icm_disconnect_pcie_paths,
  1988. .approve_xdomain_paths = icm_tr_approve_xdomain_paths,
  1989. .disconnect_xdomain_paths = icm_tr_disconnect_xdomain_paths,
  1990. .usb4_switch_op = icm_usb4_switch_op,
  1991. .usb4_switch_nvm_authenticate_status =
  1992. icm_usb4_switch_nvm_authenticate_status,
  1993. };
  1994. /* Ice Lake */
  1995. static const struct tb_cm_ops icm_icl_ops = {
  1996. .driver_ready = icm_driver_ready,
  1997. .start = icm_start,
  1998. .stop = icm_stop,
  1999. .complete = icm_complete,
  2000. .runtime_suspend = icm_runtime_suspend,
  2001. .runtime_resume = icm_runtime_resume,
  2002. .handle_event = icm_handle_event,
  2003. .approve_xdomain_paths = icm_tr_approve_xdomain_paths,
  2004. .disconnect_xdomain_paths = icm_tr_disconnect_xdomain_paths,
  2005. .usb4_switch_op = icm_usb4_switch_op,
  2006. .usb4_switch_nvm_authenticate_status =
  2007. icm_usb4_switch_nvm_authenticate_status,
  2008. };
  2009. struct tb *icm_probe(struct tb_nhi *nhi)
  2010. {
  2011. struct icm *icm;
  2012. struct tb *tb;
  2013. tb = tb_domain_alloc(nhi, ICM_TIMEOUT, sizeof(struct icm));
  2014. if (!tb)
  2015. return NULL;
  2016. icm = tb_priv(tb);
  2017. INIT_DELAYED_WORK(&icm->rescan_work, icm_rescan_work);
  2018. mutex_init(&icm->request_lock);
  2019. switch (nhi->pdev->device) {
  2020. case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI:
  2021. case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI:
  2022. icm->can_upgrade_nvm = true;
  2023. icm->is_supported = icm_fr_is_supported;
  2024. icm->get_route = icm_fr_get_route;
  2025. icm->save_devices = icm_fr_save_devices;
  2026. icm->driver_ready = icm_fr_driver_ready;
  2027. icm->device_connected = icm_fr_device_connected;
  2028. icm->device_disconnected = icm_fr_device_disconnected;
  2029. icm->xdomain_connected = icm_fr_xdomain_connected;
  2030. icm->xdomain_disconnected = icm_fr_xdomain_disconnected;
  2031. tb->cm_ops = &icm_fr_ops;
  2032. break;
  2033. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_NHI:
  2034. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_NHI:
  2035. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_NHI:
  2036. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_NHI:
  2037. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_NHI:
  2038. icm->max_boot_acl = ICM_AR_PREBOOT_ACL_ENTRIES;
  2039. /*
  2040. * NVM upgrade has not been tested on Apple systems and
  2041. * they don't provide images publicly either. To be on
  2042. * the safe side prevent root switch NVM upgrade on Macs
  2043. * for now.
  2044. */
  2045. icm->can_upgrade_nvm = !x86_apple_machine;
  2046. icm->is_supported = icm_ar_is_supported;
  2047. icm->cio_reset = icm_ar_cio_reset;
  2048. icm->get_mode = icm_ar_get_mode;
  2049. icm->get_route = icm_ar_get_route;
  2050. icm->save_devices = icm_fr_save_devices;
  2051. icm->driver_ready = icm_ar_driver_ready;
  2052. icm->device_connected = icm_fr_device_connected;
  2053. icm->device_disconnected = icm_fr_device_disconnected;
  2054. icm->xdomain_connected = icm_fr_xdomain_connected;
  2055. icm->xdomain_disconnected = icm_fr_xdomain_disconnected;
  2056. tb->cm_ops = &icm_ar_ops;
  2057. break;
  2058. case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_NHI:
  2059. case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_NHI:
  2060. icm->max_boot_acl = ICM_AR_PREBOOT_ACL_ENTRIES;
  2061. icm->can_upgrade_nvm = !x86_apple_machine;
  2062. icm->is_supported = icm_ar_is_supported;
  2063. icm->cio_reset = icm_tr_cio_reset;
  2064. icm->get_mode = icm_ar_get_mode;
  2065. icm->driver_ready = icm_tr_driver_ready;
  2066. icm->device_connected = icm_tr_device_connected;
  2067. icm->device_disconnected = icm_tr_device_disconnected;
  2068. icm->xdomain_connected = icm_tr_xdomain_connected;
  2069. icm->xdomain_disconnected = icm_tr_xdomain_disconnected;
  2070. tb->cm_ops = &icm_tr_ops;
  2071. break;
  2072. case PCI_DEVICE_ID_INTEL_ICL_NHI0:
  2073. case PCI_DEVICE_ID_INTEL_ICL_NHI1:
  2074. icm->is_supported = icm_fr_is_supported;
  2075. icm->driver_ready = icm_icl_driver_ready;
  2076. icm->set_uuid = icm_icl_set_uuid;
  2077. icm->device_connected = icm_icl_device_connected;
  2078. icm->device_disconnected = icm_tr_device_disconnected;
  2079. icm->xdomain_connected = icm_tr_xdomain_connected;
  2080. icm->xdomain_disconnected = icm_tr_xdomain_disconnected;
  2081. icm->rtd3_veto = icm_icl_rtd3_veto;
  2082. tb->cm_ops = &icm_icl_ops;
  2083. break;
  2084. case PCI_DEVICE_ID_INTEL_TGL_NHI0:
  2085. case PCI_DEVICE_ID_INTEL_TGL_NHI1:
  2086. case PCI_DEVICE_ID_INTEL_TGL_H_NHI0:
  2087. case PCI_DEVICE_ID_INTEL_TGL_H_NHI1:
  2088. case PCI_DEVICE_ID_INTEL_ADL_NHI0:
  2089. case PCI_DEVICE_ID_INTEL_ADL_NHI1:
  2090. case PCI_DEVICE_ID_INTEL_RPL_NHI0:
  2091. case PCI_DEVICE_ID_INTEL_RPL_NHI1:
  2092. case PCI_DEVICE_ID_INTEL_MTL_M_NHI0:
  2093. case PCI_DEVICE_ID_INTEL_MTL_P_NHI0:
  2094. case PCI_DEVICE_ID_INTEL_MTL_P_NHI1:
  2095. icm->is_supported = icm_tgl_is_supported;
  2096. icm->driver_ready = icm_icl_driver_ready;
  2097. icm->set_uuid = icm_icl_set_uuid;
  2098. icm->device_connected = icm_icl_device_connected;
  2099. icm->device_disconnected = icm_tr_device_disconnected;
  2100. icm->xdomain_connected = icm_tr_xdomain_connected;
  2101. icm->xdomain_disconnected = icm_tr_xdomain_disconnected;
  2102. icm->rtd3_veto = icm_icl_rtd3_veto;
  2103. tb->cm_ops = &icm_icl_ops;
  2104. break;
  2105. case PCI_DEVICE_ID_INTEL_MAPLE_RIDGE_2C_NHI:
  2106. case PCI_DEVICE_ID_INTEL_MAPLE_RIDGE_4C_NHI:
  2107. icm->can_upgrade_nvm = true;
  2108. icm->is_supported = icm_tgl_is_supported;
  2109. icm->get_mode = icm_ar_get_mode;
  2110. icm->driver_ready = icm_tr_driver_ready;
  2111. icm->device_connected = icm_tr_device_connected;
  2112. icm->device_disconnected = icm_tr_device_disconnected;
  2113. icm->xdomain_connected = icm_tr_xdomain_connected;
  2114. icm->xdomain_disconnected = icm_tr_xdomain_disconnected;
  2115. tb->cm_ops = &icm_tr_ops;
  2116. break;
  2117. }
  2118. if (!icm->is_supported || !icm->is_supported(tb)) {
  2119. dev_dbg(&nhi->pdev->dev, "ICM not supported on this controller\n");
  2120. tb_domain_put(tb);
  2121. return NULL;
  2122. }
  2123. tb_dbg(tb, "using firmware connection manager\n");
  2124. return tb;
  2125. }