icm.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557
  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 <[email protected]>
  9. * Mika Westerberg <[email protected]>
  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 *parent_sw, struct tb_switch *sw,
  531. u64 route, u8 connection_id, u8 connection_key,
  532. u8 link, u8 depth, bool boot)
  533. {
  534. /* Disconnect from parent */
  535. tb_port_at(tb_route(sw), parent_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. struct tb_switch *parent_sw;
  554. parent_sw = tb_to_switch(sw->dev.parent);
  555. tb_port_at(tb_route(sw), parent_sw)->remote = NULL;
  556. tb_switch_remove(sw);
  557. }
  558. static void add_xdomain(struct tb_switch *sw, u64 route,
  559. const uuid_t *local_uuid, const uuid_t *remote_uuid,
  560. u8 link, u8 depth)
  561. {
  562. struct tb_xdomain *xd;
  563. pm_runtime_get_sync(&sw->dev);
  564. xd = tb_xdomain_alloc(sw->tb, &sw->dev, route, local_uuid, remote_uuid);
  565. if (!xd)
  566. goto out;
  567. xd->link = link;
  568. xd->depth = depth;
  569. tb_port_at(route, sw)->xdomain = xd;
  570. tb_xdomain_add(xd);
  571. out:
  572. pm_runtime_mark_last_busy(&sw->dev);
  573. pm_runtime_put_autosuspend(&sw->dev);
  574. }
  575. static void update_xdomain(struct tb_xdomain *xd, u64 route, u8 link)
  576. {
  577. xd->link = link;
  578. xd->route = route;
  579. xd->is_unplugged = false;
  580. }
  581. static void remove_xdomain(struct tb_xdomain *xd)
  582. {
  583. struct tb_switch *sw;
  584. sw = tb_to_switch(xd->dev.parent);
  585. tb_port_at(xd->route, sw)->xdomain = NULL;
  586. tb_xdomain_remove(xd);
  587. }
  588. static void
  589. icm_fr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr)
  590. {
  591. const struct icm_fr_event_device_connected *pkg =
  592. (const struct icm_fr_event_device_connected *)hdr;
  593. enum tb_security_level security_level;
  594. struct tb_switch *sw, *parent_sw;
  595. bool boot, dual_lane, speed_gen3;
  596. struct icm *icm = tb_priv(tb);
  597. bool authorized = false;
  598. struct tb_xdomain *xd;
  599. u8 link, depth;
  600. u64 route;
  601. int ret;
  602. icm_postpone_rescan(tb);
  603. link = pkg->link_info & ICM_LINK_INFO_LINK_MASK;
  604. depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >>
  605. ICM_LINK_INFO_DEPTH_SHIFT;
  606. authorized = pkg->link_info & ICM_LINK_INFO_APPROVED;
  607. security_level = (pkg->hdr.flags & ICM_FLAGS_SLEVEL_MASK) >>
  608. ICM_FLAGS_SLEVEL_SHIFT;
  609. boot = pkg->link_info & ICM_LINK_INFO_BOOT;
  610. dual_lane = pkg->hdr.flags & ICM_FLAGS_DUAL_LANE;
  611. speed_gen3 = pkg->hdr.flags & ICM_FLAGS_SPEED_GEN3;
  612. if (pkg->link_info & ICM_LINK_INFO_REJECTED) {
  613. tb_info(tb, "switch at %u.%u was rejected by ICM firmware because topology limit exceeded\n",
  614. link, depth);
  615. return;
  616. }
  617. sw = tb_switch_find_by_uuid(tb, &pkg->ep_uuid);
  618. if (sw) {
  619. u8 phy_port, sw_phy_port;
  620. parent_sw = tb_to_switch(sw->dev.parent);
  621. sw_phy_port = tb_phy_port_from_link(sw->link);
  622. phy_port = tb_phy_port_from_link(link);
  623. /*
  624. * On resume ICM will send us connected events for the
  625. * devices that still are present. However, that
  626. * information might have changed for example by the
  627. * fact that a switch on a dual-link connection might
  628. * have been enumerated using the other link now. Make
  629. * sure our book keeping matches that.
  630. */
  631. if (sw->depth == depth && sw_phy_port == phy_port &&
  632. !!sw->authorized == authorized) {
  633. /*
  634. * It was enumerated through another link so update
  635. * route string accordingly.
  636. */
  637. if (sw->link != link) {
  638. ret = icm->get_route(tb, link, depth, &route);
  639. if (ret) {
  640. tb_err(tb, "failed to update route string for switch at %u.%u\n",
  641. link, depth);
  642. tb_switch_put(sw);
  643. return;
  644. }
  645. } else {
  646. route = tb_route(sw);
  647. }
  648. update_switch(parent_sw, sw, route, pkg->connection_id,
  649. pkg->connection_key, link, depth, boot);
  650. tb_switch_put(sw);
  651. return;
  652. }
  653. /*
  654. * User connected the same switch to another physical
  655. * port or to another part of the topology. Remove the
  656. * existing switch now before adding the new one.
  657. */
  658. remove_switch(sw);
  659. tb_switch_put(sw);
  660. }
  661. /*
  662. * If the switch was not found by UUID, look for a switch on
  663. * same physical port (taking possible link aggregation into
  664. * account) and depth. If we found one it is definitely a stale
  665. * one so remove it first.
  666. */
  667. sw = tb_switch_find_by_link_depth(tb, link, depth);
  668. if (!sw) {
  669. u8 dual_link;
  670. dual_link = dual_link_from_link(link);
  671. if (dual_link)
  672. sw = tb_switch_find_by_link_depth(tb, dual_link, depth);
  673. }
  674. if (sw) {
  675. remove_switch(sw);
  676. tb_switch_put(sw);
  677. }
  678. /* Remove existing XDomain connection if found */
  679. xd = tb_xdomain_find_by_link_depth(tb, link, depth);
  680. if (xd) {
  681. remove_xdomain(xd);
  682. tb_xdomain_put(xd);
  683. }
  684. parent_sw = tb_switch_find_by_link_depth(tb, link, depth - 1);
  685. if (!parent_sw) {
  686. tb_err(tb, "failed to find parent switch for %u.%u\n",
  687. link, depth);
  688. return;
  689. }
  690. ret = icm->get_route(tb, link, depth, &route);
  691. if (ret) {
  692. tb_err(tb, "failed to find route string for switch at %u.%u\n",
  693. link, depth);
  694. tb_switch_put(parent_sw);
  695. return;
  696. }
  697. pm_runtime_get_sync(&parent_sw->dev);
  698. sw = alloc_switch(parent_sw, route, &pkg->ep_uuid);
  699. if (!IS_ERR(sw)) {
  700. sw->connection_id = pkg->connection_id;
  701. sw->connection_key = pkg->connection_key;
  702. sw->link = link;
  703. sw->depth = depth;
  704. sw->authorized = authorized;
  705. sw->security_level = security_level;
  706. sw->boot = boot;
  707. sw->link_speed = speed_gen3 ? 20 : 10;
  708. sw->link_width = dual_lane ? 2 : 1;
  709. sw->rpm = intel_vss_is_rtd3(pkg->ep_name, sizeof(pkg->ep_name));
  710. if (add_switch(parent_sw, sw))
  711. tb_switch_put(sw);
  712. }
  713. pm_runtime_mark_last_busy(&parent_sw->dev);
  714. pm_runtime_put_autosuspend(&parent_sw->dev);
  715. tb_switch_put(parent_sw);
  716. }
  717. static void
  718. icm_fr_device_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
  719. {
  720. const struct icm_fr_event_device_disconnected *pkg =
  721. (const struct icm_fr_event_device_disconnected *)hdr;
  722. struct tb_switch *sw;
  723. u8 link, depth;
  724. link = pkg->link_info & ICM_LINK_INFO_LINK_MASK;
  725. depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >>
  726. ICM_LINK_INFO_DEPTH_SHIFT;
  727. if (link > ICM_MAX_LINK || depth > TB_SWITCH_MAX_DEPTH) {
  728. tb_warn(tb, "invalid topology %u.%u, ignoring\n", link, depth);
  729. return;
  730. }
  731. sw = tb_switch_find_by_link_depth(tb, link, depth);
  732. if (!sw) {
  733. tb_warn(tb, "no switch exists at %u.%u, ignoring\n", link,
  734. depth);
  735. return;
  736. }
  737. pm_runtime_get_sync(sw->dev.parent);
  738. remove_switch(sw);
  739. pm_runtime_mark_last_busy(sw->dev.parent);
  740. pm_runtime_put_autosuspend(sw->dev.parent);
  741. tb_switch_put(sw);
  742. }
  743. static void
  744. icm_fr_xdomain_connected(struct tb *tb, const struct icm_pkg_header *hdr)
  745. {
  746. const struct icm_fr_event_xdomain_connected *pkg =
  747. (const struct icm_fr_event_xdomain_connected *)hdr;
  748. struct tb_xdomain *xd;
  749. struct tb_switch *sw;
  750. u8 link, depth;
  751. u64 route;
  752. link = pkg->link_info & ICM_LINK_INFO_LINK_MASK;
  753. depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >>
  754. ICM_LINK_INFO_DEPTH_SHIFT;
  755. if (link > ICM_MAX_LINK || depth > TB_SWITCH_MAX_DEPTH) {
  756. tb_warn(tb, "invalid topology %u.%u, ignoring\n", link, depth);
  757. return;
  758. }
  759. route = get_route(pkg->local_route_hi, pkg->local_route_lo);
  760. xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid);
  761. if (xd) {
  762. u8 xd_phy_port, phy_port;
  763. xd_phy_port = phy_port_from_route(xd->route, xd->depth);
  764. phy_port = phy_port_from_route(route, depth);
  765. if (xd->depth == depth && xd_phy_port == phy_port) {
  766. update_xdomain(xd, route, link);
  767. tb_xdomain_put(xd);
  768. return;
  769. }
  770. /*
  771. * If we find an existing XDomain connection remove it
  772. * now. We need to go through login handshake and
  773. * everything anyway to be able to re-establish the
  774. * connection.
  775. */
  776. remove_xdomain(xd);
  777. tb_xdomain_put(xd);
  778. }
  779. /*
  780. * Look if there already exists an XDomain in the same place
  781. * than the new one and in that case remove it because it is
  782. * most likely another host that got disconnected.
  783. */
  784. xd = tb_xdomain_find_by_link_depth(tb, link, depth);
  785. if (!xd) {
  786. u8 dual_link;
  787. dual_link = dual_link_from_link(link);
  788. if (dual_link)
  789. xd = tb_xdomain_find_by_link_depth(tb, dual_link,
  790. depth);
  791. }
  792. if (xd) {
  793. remove_xdomain(xd);
  794. tb_xdomain_put(xd);
  795. }
  796. /*
  797. * If the user disconnected a switch during suspend and
  798. * connected another host to the same port, remove the switch
  799. * first.
  800. */
  801. sw = tb_switch_find_by_route(tb, route);
  802. if (sw) {
  803. remove_switch(sw);
  804. tb_switch_put(sw);
  805. }
  806. sw = tb_switch_find_by_link_depth(tb, link, depth);
  807. if (!sw) {
  808. tb_warn(tb, "no switch exists at %u.%u, ignoring\n", link,
  809. depth);
  810. return;
  811. }
  812. add_xdomain(sw, route, &pkg->local_uuid, &pkg->remote_uuid, link,
  813. depth);
  814. tb_switch_put(sw);
  815. }
  816. static void
  817. icm_fr_xdomain_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
  818. {
  819. const struct icm_fr_event_xdomain_disconnected *pkg =
  820. (const struct icm_fr_event_xdomain_disconnected *)hdr;
  821. struct tb_xdomain *xd;
  822. /*
  823. * If the connection is through one or multiple devices, the
  824. * XDomain device is removed along with them so it is fine if we
  825. * cannot find it here.
  826. */
  827. xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid);
  828. if (xd) {
  829. remove_xdomain(xd);
  830. tb_xdomain_put(xd);
  831. }
  832. }
  833. static int icm_tr_cio_reset(struct tb *tb)
  834. {
  835. return pcie2cio_write(tb_priv(tb), TB_CFG_SWITCH, 0, 0x777, BIT(1));
  836. }
  837. static int
  838. icm_tr_driver_ready(struct tb *tb, enum tb_security_level *security_level,
  839. u8 *proto_version, size_t *nboot_acl, bool *rpm)
  840. {
  841. struct icm_tr_pkg_driver_ready_response reply;
  842. struct icm_pkg_driver_ready request = {
  843. .hdr.code = ICM_DRIVER_READY,
  844. };
  845. int ret;
  846. memset(&reply, 0, sizeof(reply));
  847. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  848. 1, 10, 2000);
  849. if (ret)
  850. return ret;
  851. if (security_level)
  852. *security_level = reply.info & ICM_TR_INFO_SLEVEL_MASK;
  853. if (proto_version)
  854. *proto_version = (reply.info & ICM_TR_INFO_PROTO_VERSION_MASK) >>
  855. ICM_TR_INFO_PROTO_VERSION_SHIFT;
  856. if (nboot_acl)
  857. *nboot_acl = (reply.info & ICM_TR_INFO_BOOT_ACL_MASK) >>
  858. ICM_TR_INFO_BOOT_ACL_SHIFT;
  859. if (rpm)
  860. *rpm = !!(reply.hdr.flags & ICM_TR_FLAGS_RTD3);
  861. return 0;
  862. }
  863. static int icm_tr_approve_switch(struct tb *tb, struct tb_switch *sw)
  864. {
  865. struct icm_tr_pkg_approve_device request;
  866. struct icm_tr_pkg_approve_device reply;
  867. int ret;
  868. memset(&request, 0, sizeof(request));
  869. memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
  870. request.hdr.code = ICM_APPROVE_DEVICE;
  871. request.route_lo = sw->config.route_lo;
  872. request.route_hi = sw->config.route_hi;
  873. request.connection_id = sw->connection_id;
  874. memset(&reply, 0, sizeof(reply));
  875. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  876. 1, ICM_RETRIES, ICM_APPROVE_TIMEOUT);
  877. if (ret)
  878. return ret;
  879. if (reply.hdr.flags & ICM_FLAGS_ERROR) {
  880. tb_warn(tb, "PCIe tunnel creation failed\n");
  881. return -EIO;
  882. }
  883. return 0;
  884. }
  885. static int icm_tr_add_switch_key(struct tb *tb, struct tb_switch *sw)
  886. {
  887. struct icm_tr_pkg_add_device_key_response reply;
  888. struct icm_tr_pkg_add_device_key request;
  889. int ret;
  890. memset(&request, 0, sizeof(request));
  891. memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
  892. request.hdr.code = ICM_ADD_DEVICE_KEY;
  893. request.route_lo = sw->config.route_lo;
  894. request.route_hi = sw->config.route_hi;
  895. request.connection_id = sw->connection_id;
  896. memcpy(request.key, sw->key, TB_SWITCH_KEY_SIZE);
  897. memset(&reply, 0, sizeof(reply));
  898. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  899. 1, ICM_RETRIES, ICM_TIMEOUT);
  900. if (ret)
  901. return ret;
  902. if (reply.hdr.flags & ICM_FLAGS_ERROR) {
  903. tb_warn(tb, "Adding key to switch failed\n");
  904. return -EIO;
  905. }
  906. return 0;
  907. }
  908. static int icm_tr_challenge_switch_key(struct tb *tb, struct tb_switch *sw,
  909. const u8 *challenge, u8 *response)
  910. {
  911. struct icm_tr_pkg_challenge_device_response reply;
  912. struct icm_tr_pkg_challenge_device request;
  913. int ret;
  914. memset(&request, 0, sizeof(request));
  915. memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
  916. request.hdr.code = ICM_CHALLENGE_DEVICE;
  917. request.route_lo = sw->config.route_lo;
  918. request.route_hi = sw->config.route_hi;
  919. request.connection_id = sw->connection_id;
  920. memcpy(request.challenge, challenge, TB_SWITCH_KEY_SIZE);
  921. memset(&reply, 0, sizeof(reply));
  922. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  923. 1, ICM_RETRIES, ICM_TIMEOUT);
  924. if (ret)
  925. return ret;
  926. if (reply.hdr.flags & ICM_FLAGS_ERROR)
  927. return -EKEYREJECTED;
  928. if (reply.hdr.flags & ICM_FLAGS_NO_KEY)
  929. return -ENOKEY;
  930. memcpy(response, reply.response, TB_SWITCH_KEY_SIZE);
  931. return 0;
  932. }
  933. static int icm_tr_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
  934. int transmit_path, int transmit_ring,
  935. int receive_path, int receive_ring)
  936. {
  937. struct icm_tr_pkg_approve_xdomain_response reply;
  938. struct icm_tr_pkg_approve_xdomain request;
  939. int ret;
  940. memset(&request, 0, sizeof(request));
  941. request.hdr.code = ICM_APPROVE_XDOMAIN;
  942. request.route_hi = upper_32_bits(xd->route);
  943. request.route_lo = lower_32_bits(xd->route);
  944. request.transmit_path = transmit_path;
  945. request.transmit_ring = transmit_ring;
  946. request.receive_path = receive_path;
  947. request.receive_ring = receive_ring;
  948. memcpy(&request.remote_uuid, xd->remote_uuid, sizeof(*xd->remote_uuid));
  949. memset(&reply, 0, sizeof(reply));
  950. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  951. 1, ICM_RETRIES, ICM_TIMEOUT);
  952. if (ret)
  953. return ret;
  954. if (reply.hdr.flags & ICM_FLAGS_ERROR)
  955. return -EIO;
  956. return 0;
  957. }
  958. static int icm_tr_xdomain_tear_down(struct tb *tb, struct tb_xdomain *xd,
  959. int stage)
  960. {
  961. struct icm_tr_pkg_disconnect_xdomain_response reply;
  962. struct icm_tr_pkg_disconnect_xdomain request;
  963. int ret;
  964. memset(&request, 0, sizeof(request));
  965. request.hdr.code = ICM_DISCONNECT_XDOMAIN;
  966. request.stage = stage;
  967. request.route_hi = upper_32_bits(xd->route);
  968. request.route_lo = lower_32_bits(xd->route);
  969. memcpy(&request.remote_uuid, xd->remote_uuid, sizeof(*xd->remote_uuid));
  970. memset(&reply, 0, sizeof(reply));
  971. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  972. 1, ICM_RETRIES, ICM_TIMEOUT);
  973. if (ret)
  974. return ret;
  975. if (reply.hdr.flags & ICM_FLAGS_ERROR)
  976. return -EIO;
  977. return 0;
  978. }
  979. static int icm_tr_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
  980. int transmit_path, int transmit_ring,
  981. int receive_path, int receive_ring)
  982. {
  983. int ret;
  984. ret = icm_tr_xdomain_tear_down(tb, xd, 1);
  985. if (ret)
  986. return ret;
  987. usleep_range(10, 50);
  988. return icm_tr_xdomain_tear_down(tb, xd, 2);
  989. }
  990. static void
  991. __icm_tr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr,
  992. bool force_rtd3)
  993. {
  994. const struct icm_tr_event_device_connected *pkg =
  995. (const struct icm_tr_event_device_connected *)hdr;
  996. bool authorized, boot, dual_lane, speed_gen3;
  997. enum tb_security_level security_level;
  998. struct tb_switch *sw, *parent_sw;
  999. struct tb_xdomain *xd;
  1000. u64 route;
  1001. icm_postpone_rescan(tb);
  1002. /*
  1003. * Currently we don't use the QoS information coming with the
  1004. * device connected message so simply just ignore that extra
  1005. * packet for now.
  1006. */
  1007. if (pkg->hdr.packet_id)
  1008. return;
  1009. route = get_route(pkg->route_hi, pkg->route_lo);
  1010. authorized = pkg->link_info & ICM_LINK_INFO_APPROVED;
  1011. security_level = (pkg->hdr.flags & ICM_FLAGS_SLEVEL_MASK) >>
  1012. ICM_FLAGS_SLEVEL_SHIFT;
  1013. boot = pkg->link_info & ICM_LINK_INFO_BOOT;
  1014. dual_lane = pkg->hdr.flags & ICM_FLAGS_DUAL_LANE;
  1015. speed_gen3 = pkg->hdr.flags & ICM_FLAGS_SPEED_GEN3;
  1016. if (pkg->link_info & ICM_LINK_INFO_REJECTED) {
  1017. tb_info(tb, "switch at %llx was rejected by ICM firmware because topology limit exceeded\n",
  1018. route);
  1019. return;
  1020. }
  1021. sw = tb_switch_find_by_uuid(tb, &pkg->ep_uuid);
  1022. if (sw) {
  1023. /* Update the switch if it is still in the same place */
  1024. if (tb_route(sw) == route && !!sw->authorized == authorized) {
  1025. parent_sw = tb_to_switch(sw->dev.parent);
  1026. update_switch(parent_sw, sw, route, pkg->connection_id,
  1027. 0, 0, 0, boot);
  1028. tb_switch_put(sw);
  1029. return;
  1030. }
  1031. remove_switch(sw);
  1032. tb_switch_put(sw);
  1033. }
  1034. /* Another switch with the same address */
  1035. sw = tb_switch_find_by_route(tb, route);
  1036. if (sw) {
  1037. remove_switch(sw);
  1038. tb_switch_put(sw);
  1039. }
  1040. /* XDomain connection with the same address */
  1041. xd = tb_xdomain_find_by_route(tb, route);
  1042. if (xd) {
  1043. remove_xdomain(xd);
  1044. tb_xdomain_put(xd);
  1045. }
  1046. parent_sw = tb_switch_find_by_route(tb, get_parent_route(route));
  1047. if (!parent_sw) {
  1048. tb_err(tb, "failed to find parent switch for %llx\n", route);
  1049. return;
  1050. }
  1051. pm_runtime_get_sync(&parent_sw->dev);
  1052. sw = alloc_switch(parent_sw, route, &pkg->ep_uuid);
  1053. if (!IS_ERR(sw)) {
  1054. sw->connection_id = pkg->connection_id;
  1055. sw->authorized = authorized;
  1056. sw->security_level = security_level;
  1057. sw->boot = boot;
  1058. sw->link_speed = speed_gen3 ? 20 : 10;
  1059. sw->link_width = dual_lane ? 2 : 1;
  1060. sw->rpm = force_rtd3;
  1061. if (!sw->rpm)
  1062. sw->rpm = intel_vss_is_rtd3(pkg->ep_name,
  1063. sizeof(pkg->ep_name));
  1064. if (add_switch(parent_sw, sw))
  1065. tb_switch_put(sw);
  1066. }
  1067. pm_runtime_mark_last_busy(&parent_sw->dev);
  1068. pm_runtime_put_autosuspend(&parent_sw->dev);
  1069. tb_switch_put(parent_sw);
  1070. }
  1071. static void
  1072. icm_tr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr)
  1073. {
  1074. __icm_tr_device_connected(tb, hdr, false);
  1075. }
  1076. static void
  1077. icm_tr_device_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
  1078. {
  1079. const struct icm_tr_event_device_disconnected *pkg =
  1080. (const struct icm_tr_event_device_disconnected *)hdr;
  1081. struct tb_switch *sw;
  1082. u64 route;
  1083. route = get_route(pkg->route_hi, pkg->route_lo);
  1084. sw = tb_switch_find_by_route(tb, route);
  1085. if (!sw) {
  1086. tb_warn(tb, "no switch exists at %llx, ignoring\n", route);
  1087. return;
  1088. }
  1089. pm_runtime_get_sync(sw->dev.parent);
  1090. remove_switch(sw);
  1091. pm_runtime_mark_last_busy(sw->dev.parent);
  1092. pm_runtime_put_autosuspend(sw->dev.parent);
  1093. tb_switch_put(sw);
  1094. }
  1095. static void
  1096. icm_tr_xdomain_connected(struct tb *tb, const struct icm_pkg_header *hdr)
  1097. {
  1098. const struct icm_tr_event_xdomain_connected *pkg =
  1099. (const struct icm_tr_event_xdomain_connected *)hdr;
  1100. struct tb_xdomain *xd;
  1101. struct tb_switch *sw;
  1102. u64 route;
  1103. if (!tb->root_switch)
  1104. return;
  1105. route = get_route(pkg->local_route_hi, pkg->local_route_lo);
  1106. xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid);
  1107. if (xd) {
  1108. if (xd->route == route) {
  1109. update_xdomain(xd, route, 0);
  1110. tb_xdomain_put(xd);
  1111. return;
  1112. }
  1113. remove_xdomain(xd);
  1114. tb_xdomain_put(xd);
  1115. }
  1116. /* An existing xdomain with the same address */
  1117. xd = tb_xdomain_find_by_route(tb, route);
  1118. if (xd) {
  1119. remove_xdomain(xd);
  1120. tb_xdomain_put(xd);
  1121. }
  1122. /*
  1123. * If the user disconnected a switch during suspend and
  1124. * connected another host to the same port, remove the switch
  1125. * first.
  1126. */
  1127. sw = tb_switch_find_by_route(tb, route);
  1128. if (sw) {
  1129. remove_switch(sw);
  1130. tb_switch_put(sw);
  1131. }
  1132. sw = tb_switch_find_by_route(tb, get_parent_route(route));
  1133. if (!sw) {
  1134. tb_warn(tb, "no switch exists at %llx, ignoring\n", route);
  1135. return;
  1136. }
  1137. add_xdomain(sw, route, &pkg->local_uuid, &pkg->remote_uuid, 0, 0);
  1138. tb_switch_put(sw);
  1139. }
  1140. static void
  1141. icm_tr_xdomain_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
  1142. {
  1143. const struct icm_tr_event_xdomain_disconnected *pkg =
  1144. (const struct icm_tr_event_xdomain_disconnected *)hdr;
  1145. struct tb_xdomain *xd;
  1146. u64 route;
  1147. route = get_route(pkg->route_hi, pkg->route_lo);
  1148. xd = tb_xdomain_find_by_route(tb, route);
  1149. if (xd) {
  1150. remove_xdomain(xd);
  1151. tb_xdomain_put(xd);
  1152. }
  1153. }
  1154. static struct pci_dev *get_upstream_port(struct pci_dev *pdev)
  1155. {
  1156. struct pci_dev *parent;
  1157. parent = pci_upstream_bridge(pdev);
  1158. while (parent) {
  1159. if (!pci_is_pcie(parent))
  1160. return NULL;
  1161. if (pci_pcie_type(parent) == PCI_EXP_TYPE_UPSTREAM)
  1162. break;
  1163. parent = pci_upstream_bridge(parent);
  1164. }
  1165. if (!parent)
  1166. return NULL;
  1167. switch (parent->device) {
  1168. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE:
  1169. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE:
  1170. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE:
  1171. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE:
  1172. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE:
  1173. case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_BRIDGE:
  1174. case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_BRIDGE:
  1175. return parent;
  1176. }
  1177. return NULL;
  1178. }
  1179. static bool icm_ar_is_supported(struct tb *tb)
  1180. {
  1181. struct pci_dev *upstream_port;
  1182. struct icm *icm = tb_priv(tb);
  1183. /*
  1184. * Starting from Alpine Ridge we can use ICM on Apple machines
  1185. * as well. We just need to reset and re-enable it first.
  1186. * However, only start it if explicitly asked by the user.
  1187. */
  1188. if (icm_firmware_running(tb->nhi))
  1189. return true;
  1190. if (!start_icm)
  1191. return false;
  1192. /*
  1193. * Find the upstream PCIe port in case we need to do reset
  1194. * through its vendor specific registers.
  1195. */
  1196. upstream_port = get_upstream_port(tb->nhi->pdev);
  1197. if (upstream_port) {
  1198. int cap;
  1199. cap = pci_find_ext_capability(upstream_port,
  1200. PCI_EXT_CAP_ID_VNDR);
  1201. if (cap > 0) {
  1202. icm->upstream_port = upstream_port;
  1203. icm->vnd_cap = cap;
  1204. return true;
  1205. }
  1206. }
  1207. return false;
  1208. }
  1209. static int icm_ar_cio_reset(struct tb *tb)
  1210. {
  1211. return pcie2cio_write(tb_priv(tb), TB_CFG_SWITCH, 0, 0x50, BIT(9));
  1212. }
  1213. static int icm_ar_get_mode(struct tb *tb)
  1214. {
  1215. struct tb_nhi *nhi = tb->nhi;
  1216. int retries = 60;
  1217. u32 val;
  1218. do {
  1219. val = ioread32(nhi->iobase + REG_FW_STS);
  1220. if (val & REG_FW_STS_NVM_AUTH_DONE)
  1221. break;
  1222. msleep(50);
  1223. } while (--retries);
  1224. if (!retries) {
  1225. dev_err(&nhi->pdev->dev, "ICM firmware not authenticated\n");
  1226. return -ENODEV;
  1227. }
  1228. return nhi_mailbox_mode(nhi);
  1229. }
  1230. static int
  1231. icm_ar_driver_ready(struct tb *tb, enum tb_security_level *security_level,
  1232. u8 *proto_version, size_t *nboot_acl, bool *rpm)
  1233. {
  1234. struct icm_ar_pkg_driver_ready_response reply;
  1235. struct icm_pkg_driver_ready request = {
  1236. .hdr.code = ICM_DRIVER_READY,
  1237. };
  1238. int ret;
  1239. memset(&reply, 0, sizeof(reply));
  1240. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  1241. 1, ICM_RETRIES, ICM_TIMEOUT);
  1242. if (ret)
  1243. return ret;
  1244. if (security_level)
  1245. *security_level = reply.info & ICM_AR_INFO_SLEVEL_MASK;
  1246. if (nboot_acl && (reply.info & ICM_AR_INFO_BOOT_ACL_SUPPORTED))
  1247. *nboot_acl = (reply.info & ICM_AR_INFO_BOOT_ACL_MASK) >>
  1248. ICM_AR_INFO_BOOT_ACL_SHIFT;
  1249. if (rpm)
  1250. *rpm = !!(reply.hdr.flags & ICM_AR_FLAGS_RTD3);
  1251. return 0;
  1252. }
  1253. static int icm_ar_get_route(struct tb *tb, u8 link, u8 depth, u64 *route)
  1254. {
  1255. struct icm_ar_pkg_get_route_response reply;
  1256. struct icm_ar_pkg_get_route request = {
  1257. .hdr = { .code = ICM_GET_ROUTE },
  1258. .link_info = depth << ICM_LINK_INFO_DEPTH_SHIFT | link,
  1259. };
  1260. int ret;
  1261. memset(&reply, 0, sizeof(reply));
  1262. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  1263. 1, ICM_RETRIES, ICM_TIMEOUT);
  1264. if (ret)
  1265. return ret;
  1266. if (reply.hdr.flags & ICM_FLAGS_ERROR)
  1267. return -EIO;
  1268. *route = get_route(reply.route_hi, reply.route_lo);
  1269. return 0;
  1270. }
  1271. static int icm_ar_get_boot_acl(struct tb *tb, uuid_t *uuids, size_t nuuids)
  1272. {
  1273. struct icm_ar_pkg_preboot_acl_response reply;
  1274. struct icm_ar_pkg_preboot_acl request = {
  1275. .hdr = { .code = ICM_PREBOOT_ACL },
  1276. };
  1277. int ret, i;
  1278. memset(&reply, 0, sizeof(reply));
  1279. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  1280. 1, ICM_RETRIES, ICM_TIMEOUT);
  1281. if (ret)
  1282. return ret;
  1283. if (reply.hdr.flags & ICM_FLAGS_ERROR)
  1284. return -EIO;
  1285. for (i = 0; i < nuuids; i++) {
  1286. u32 *uuid = (u32 *)&uuids[i];
  1287. uuid[0] = reply.acl[i].uuid_lo;
  1288. uuid[1] = reply.acl[i].uuid_hi;
  1289. if (uuid[0] == 0xffffffff && uuid[1] == 0xffffffff) {
  1290. /* Map empty entries to null UUID */
  1291. uuid[0] = 0;
  1292. uuid[1] = 0;
  1293. } else if (uuid[0] != 0 || uuid[1] != 0) {
  1294. /* Upper two DWs are always one's */
  1295. uuid[2] = 0xffffffff;
  1296. uuid[3] = 0xffffffff;
  1297. }
  1298. }
  1299. return ret;
  1300. }
  1301. static int icm_ar_set_boot_acl(struct tb *tb, const uuid_t *uuids,
  1302. size_t nuuids)
  1303. {
  1304. struct icm_ar_pkg_preboot_acl_response reply;
  1305. struct icm_ar_pkg_preboot_acl request = {
  1306. .hdr = {
  1307. .code = ICM_PREBOOT_ACL,
  1308. .flags = ICM_FLAGS_WRITE,
  1309. },
  1310. };
  1311. int ret, i;
  1312. for (i = 0; i < nuuids; i++) {
  1313. const u32 *uuid = (const u32 *)&uuids[i];
  1314. if (uuid_is_null(&uuids[i])) {
  1315. /*
  1316. * Map null UUID to the empty (all one) entries
  1317. * for ICM.
  1318. */
  1319. request.acl[i].uuid_lo = 0xffffffff;
  1320. request.acl[i].uuid_hi = 0xffffffff;
  1321. } else {
  1322. /* Two high DWs need to be set to all one */
  1323. if (uuid[2] != 0xffffffff || uuid[3] != 0xffffffff)
  1324. return -EINVAL;
  1325. request.acl[i].uuid_lo = uuid[0];
  1326. request.acl[i].uuid_hi = uuid[1];
  1327. }
  1328. }
  1329. memset(&reply, 0, sizeof(reply));
  1330. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  1331. 1, ICM_RETRIES, ICM_TIMEOUT);
  1332. if (ret)
  1333. return ret;
  1334. if (reply.hdr.flags & ICM_FLAGS_ERROR)
  1335. return -EIO;
  1336. return 0;
  1337. }
  1338. static int
  1339. icm_icl_driver_ready(struct tb *tb, enum tb_security_level *security_level,
  1340. u8 *proto_version, size_t *nboot_acl, bool *rpm)
  1341. {
  1342. struct icm_tr_pkg_driver_ready_response reply;
  1343. struct icm_pkg_driver_ready request = {
  1344. .hdr.code = ICM_DRIVER_READY,
  1345. };
  1346. int ret;
  1347. memset(&reply, 0, sizeof(reply));
  1348. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  1349. 1, ICM_RETRIES, 20000);
  1350. if (ret)
  1351. return ret;
  1352. if (proto_version)
  1353. *proto_version = (reply.info & ICM_TR_INFO_PROTO_VERSION_MASK) >>
  1354. ICM_TR_INFO_PROTO_VERSION_SHIFT;
  1355. /* Ice Lake always supports RTD3 */
  1356. if (rpm)
  1357. *rpm = true;
  1358. return 0;
  1359. }
  1360. static void icm_icl_set_uuid(struct tb *tb)
  1361. {
  1362. struct tb_nhi *nhi = tb->nhi;
  1363. u32 uuid[4];
  1364. pci_read_config_dword(nhi->pdev, VS_CAP_10, &uuid[0]);
  1365. pci_read_config_dword(nhi->pdev, VS_CAP_11, &uuid[1]);
  1366. uuid[2] = 0xffffffff;
  1367. uuid[3] = 0xffffffff;
  1368. tb->root_switch->uuid = kmemdup(uuid, sizeof(uuid), GFP_KERNEL);
  1369. }
  1370. static void
  1371. icm_icl_device_connected(struct tb *tb, const struct icm_pkg_header *hdr)
  1372. {
  1373. __icm_tr_device_connected(tb, hdr, true);
  1374. }
  1375. static void icm_icl_rtd3_veto(struct tb *tb, const struct icm_pkg_header *hdr)
  1376. {
  1377. const struct icm_icl_event_rtd3_veto *pkg =
  1378. (const struct icm_icl_event_rtd3_veto *)hdr;
  1379. tb_dbg(tb, "ICM rtd3 veto=0x%08x\n", pkg->veto_reason);
  1380. if (pkg->veto_reason)
  1381. icm_veto_begin(tb);
  1382. else
  1383. icm_veto_end(tb);
  1384. }
  1385. static bool icm_tgl_is_supported(struct tb *tb)
  1386. {
  1387. unsigned long end = jiffies + msecs_to_jiffies(10);
  1388. do {
  1389. u32 val;
  1390. val = ioread32(tb->nhi->iobase + REG_FW_STS);
  1391. if (val & REG_FW_STS_NVM_AUTH_DONE)
  1392. return true;
  1393. usleep_range(100, 500);
  1394. } while (time_before(jiffies, end));
  1395. return false;
  1396. }
  1397. static void icm_handle_notification(struct work_struct *work)
  1398. {
  1399. struct icm_notification *n = container_of(work, typeof(*n), work);
  1400. struct tb *tb = n->tb;
  1401. struct icm *icm = tb_priv(tb);
  1402. mutex_lock(&tb->lock);
  1403. /*
  1404. * When the domain is stopped we flush its workqueue but before
  1405. * that the root switch is removed. In that case we should treat
  1406. * the queued events as being canceled.
  1407. */
  1408. if (tb->root_switch) {
  1409. switch (n->pkg->code) {
  1410. case ICM_EVENT_DEVICE_CONNECTED:
  1411. icm->device_connected(tb, n->pkg);
  1412. break;
  1413. case ICM_EVENT_DEVICE_DISCONNECTED:
  1414. icm->device_disconnected(tb, n->pkg);
  1415. break;
  1416. case ICM_EVENT_XDOMAIN_CONNECTED:
  1417. if (tb_is_xdomain_enabled())
  1418. icm->xdomain_connected(tb, n->pkg);
  1419. break;
  1420. case ICM_EVENT_XDOMAIN_DISCONNECTED:
  1421. if (tb_is_xdomain_enabled())
  1422. icm->xdomain_disconnected(tb, n->pkg);
  1423. break;
  1424. case ICM_EVENT_RTD3_VETO:
  1425. icm->rtd3_veto(tb, n->pkg);
  1426. break;
  1427. }
  1428. }
  1429. mutex_unlock(&tb->lock);
  1430. kfree(n->pkg);
  1431. kfree(n);
  1432. }
  1433. static void icm_handle_event(struct tb *tb, enum tb_cfg_pkg_type type,
  1434. const void *buf, size_t size)
  1435. {
  1436. struct icm_notification *n;
  1437. n = kmalloc(sizeof(*n), GFP_KERNEL);
  1438. if (!n)
  1439. return;
  1440. n->pkg = kmemdup(buf, size, GFP_KERNEL);
  1441. if (!n->pkg) {
  1442. kfree(n);
  1443. return;
  1444. }
  1445. INIT_WORK(&n->work, icm_handle_notification);
  1446. n->tb = tb;
  1447. queue_work(tb->wq, &n->work);
  1448. }
  1449. static int
  1450. __icm_driver_ready(struct tb *tb, enum tb_security_level *security_level,
  1451. u8 *proto_version, size_t *nboot_acl, bool *rpm)
  1452. {
  1453. struct icm *icm = tb_priv(tb);
  1454. unsigned int retries = 50;
  1455. int ret;
  1456. ret = icm->driver_ready(tb, security_level, proto_version, nboot_acl,
  1457. rpm);
  1458. if (ret) {
  1459. tb_err(tb, "failed to send driver ready to ICM\n");
  1460. return ret;
  1461. }
  1462. /*
  1463. * Hold on here until the switch config space is accessible so
  1464. * that we can read root switch config successfully.
  1465. */
  1466. do {
  1467. struct tb_cfg_result res;
  1468. u32 tmp;
  1469. res = tb_cfg_read_raw(tb->ctl, &tmp, 0, 0, TB_CFG_SWITCH,
  1470. 0, 1, 100);
  1471. if (!res.err)
  1472. return 0;
  1473. msleep(50);
  1474. } while (--retries);
  1475. tb_err(tb, "failed to read root switch config space, giving up\n");
  1476. return -ETIMEDOUT;
  1477. }
  1478. static int icm_firmware_reset(struct tb *tb, struct tb_nhi *nhi)
  1479. {
  1480. struct icm *icm = tb_priv(tb);
  1481. u32 val;
  1482. if (!icm->upstream_port)
  1483. return -ENODEV;
  1484. /* Put ARC to wait for CIO reset event to happen */
  1485. val = ioread32(nhi->iobase + REG_FW_STS);
  1486. val |= REG_FW_STS_CIO_RESET_REQ;
  1487. iowrite32(val, nhi->iobase + REG_FW_STS);
  1488. /* Re-start ARC */
  1489. val = ioread32(nhi->iobase + REG_FW_STS);
  1490. val |= REG_FW_STS_ICM_EN_INVERT;
  1491. val |= REG_FW_STS_ICM_EN_CPU;
  1492. iowrite32(val, nhi->iobase + REG_FW_STS);
  1493. /* Trigger CIO reset now */
  1494. return icm->cio_reset(tb);
  1495. }
  1496. static int icm_firmware_start(struct tb *tb, struct tb_nhi *nhi)
  1497. {
  1498. unsigned int retries = 10;
  1499. int ret;
  1500. u32 val;
  1501. /* Check if the ICM firmware is already running */
  1502. if (icm_firmware_running(nhi))
  1503. return 0;
  1504. dev_dbg(&nhi->pdev->dev, "starting ICM firmware\n");
  1505. ret = icm_firmware_reset(tb, nhi);
  1506. if (ret)
  1507. return ret;
  1508. /* Wait until the ICM firmware tells us it is up and running */
  1509. do {
  1510. /* Check that the ICM firmware is running */
  1511. val = ioread32(nhi->iobase + REG_FW_STS);
  1512. if (val & REG_FW_STS_NVM_AUTH_DONE)
  1513. return 0;
  1514. msleep(300);
  1515. } while (--retries);
  1516. return -ETIMEDOUT;
  1517. }
  1518. static int icm_reset_phy_port(struct tb *tb, int phy_port)
  1519. {
  1520. struct icm *icm = tb_priv(tb);
  1521. u32 state0, state1;
  1522. int port0, port1;
  1523. u32 val0, val1;
  1524. int ret;
  1525. if (!icm->upstream_port)
  1526. return 0;
  1527. if (phy_port) {
  1528. port0 = 3;
  1529. port1 = 4;
  1530. } else {
  1531. port0 = 1;
  1532. port1 = 2;
  1533. }
  1534. /*
  1535. * Read link status of both null ports belonging to a single
  1536. * physical port.
  1537. */
  1538. ret = pcie2cio_read(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, &val0);
  1539. if (ret)
  1540. return ret;
  1541. ret = pcie2cio_read(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, &val1);
  1542. if (ret)
  1543. return ret;
  1544. state0 = val0 & PHY_PORT_CS1_LINK_STATE_MASK;
  1545. state0 >>= PHY_PORT_CS1_LINK_STATE_SHIFT;
  1546. state1 = val1 & PHY_PORT_CS1_LINK_STATE_MASK;
  1547. state1 >>= PHY_PORT_CS1_LINK_STATE_SHIFT;
  1548. /* If they are both up we need to reset them now */
  1549. if (state0 != TB_PORT_UP || state1 != TB_PORT_UP)
  1550. return 0;
  1551. val0 |= PHY_PORT_CS1_LINK_DISABLE;
  1552. ret = pcie2cio_write(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, val0);
  1553. if (ret)
  1554. return ret;
  1555. val1 |= PHY_PORT_CS1_LINK_DISABLE;
  1556. ret = pcie2cio_write(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, val1);
  1557. if (ret)
  1558. return ret;
  1559. /* Wait a bit and then re-enable both ports */
  1560. usleep_range(10, 100);
  1561. ret = pcie2cio_read(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, &val0);
  1562. if (ret)
  1563. return ret;
  1564. ret = pcie2cio_read(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, &val1);
  1565. if (ret)
  1566. return ret;
  1567. val0 &= ~PHY_PORT_CS1_LINK_DISABLE;
  1568. ret = pcie2cio_write(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, val0);
  1569. if (ret)
  1570. return ret;
  1571. val1 &= ~PHY_PORT_CS1_LINK_DISABLE;
  1572. return pcie2cio_write(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, val1);
  1573. }
  1574. static int icm_firmware_init(struct tb *tb)
  1575. {
  1576. struct icm *icm = tb_priv(tb);
  1577. struct tb_nhi *nhi = tb->nhi;
  1578. int ret;
  1579. ret = icm_firmware_start(tb, nhi);
  1580. if (ret) {
  1581. dev_err(&nhi->pdev->dev, "could not start ICM firmware\n");
  1582. return ret;
  1583. }
  1584. if (icm->get_mode) {
  1585. ret = icm->get_mode(tb);
  1586. switch (ret) {
  1587. case NHI_FW_SAFE_MODE:
  1588. icm->safe_mode = true;
  1589. break;
  1590. case NHI_FW_CM_MODE:
  1591. /* Ask ICM to accept all Thunderbolt devices */
  1592. nhi_mailbox_cmd(nhi, NHI_MAILBOX_ALLOW_ALL_DEVS, 0);
  1593. break;
  1594. default:
  1595. if (ret < 0)
  1596. return ret;
  1597. tb_err(tb, "ICM firmware is in wrong mode: %u\n", ret);
  1598. return -ENODEV;
  1599. }
  1600. }
  1601. /*
  1602. * Reset both physical ports if there is anything connected to
  1603. * them already.
  1604. */
  1605. ret = icm_reset_phy_port(tb, 0);
  1606. if (ret)
  1607. dev_warn(&nhi->pdev->dev, "failed to reset links on port0\n");
  1608. ret = icm_reset_phy_port(tb, 1);
  1609. if (ret)
  1610. dev_warn(&nhi->pdev->dev, "failed to reset links on port1\n");
  1611. return 0;
  1612. }
  1613. static int icm_driver_ready(struct tb *tb)
  1614. {
  1615. struct icm *icm = tb_priv(tb);
  1616. int ret;
  1617. ret = icm_firmware_init(tb);
  1618. if (ret)
  1619. return ret;
  1620. if (icm->safe_mode) {
  1621. tb_info(tb, "Thunderbolt host controller is in safe mode.\n");
  1622. tb_info(tb, "You need to update NVM firmware of the controller before it can be used.\n");
  1623. tb_info(tb, "For latest updates check https://thunderbolttechnology.net/updates.\n");
  1624. return 0;
  1625. }
  1626. ret = __icm_driver_ready(tb, &tb->security_level, &icm->proto_version,
  1627. &tb->nboot_acl, &icm->rpm);
  1628. if (ret)
  1629. return ret;
  1630. /*
  1631. * Make sure the number of supported preboot ACL matches what we
  1632. * expect or disable the whole feature.
  1633. */
  1634. if (tb->nboot_acl > icm->max_boot_acl)
  1635. tb->nboot_acl = 0;
  1636. if (icm->proto_version >= 3)
  1637. tb_dbg(tb, "USB4 proxy operations supported\n");
  1638. return 0;
  1639. }
  1640. static int icm_suspend(struct tb *tb)
  1641. {
  1642. struct icm *icm = tb_priv(tb);
  1643. if (icm->save_devices)
  1644. icm->save_devices(tb);
  1645. nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0);
  1646. return 0;
  1647. }
  1648. /*
  1649. * Mark all switches (except root switch) below this one unplugged. ICM
  1650. * firmware will send us an updated list of switches after we have send
  1651. * it driver ready command. If a switch is not in that list it will be
  1652. * removed when we perform rescan.
  1653. */
  1654. static void icm_unplug_children(struct tb_switch *sw)
  1655. {
  1656. struct tb_port *port;
  1657. if (tb_route(sw))
  1658. sw->is_unplugged = true;
  1659. tb_switch_for_each_port(sw, port) {
  1660. if (port->xdomain)
  1661. port->xdomain->is_unplugged = true;
  1662. else if (tb_port_has_remote(port))
  1663. icm_unplug_children(port->remote->sw);
  1664. }
  1665. }
  1666. static int complete_rpm(struct device *dev, void *data)
  1667. {
  1668. struct tb_switch *sw = tb_to_switch(dev);
  1669. if (sw)
  1670. complete(&sw->rpm_complete);
  1671. return 0;
  1672. }
  1673. static void remove_unplugged_switch(struct tb_switch *sw)
  1674. {
  1675. struct device *parent = get_device(sw->dev.parent);
  1676. pm_runtime_get_sync(parent);
  1677. /*
  1678. * Signal this and switches below for rpm_complete because
  1679. * tb_switch_remove() calls pm_runtime_get_sync() that then waits
  1680. * for it.
  1681. */
  1682. complete_rpm(&sw->dev, NULL);
  1683. bus_for_each_dev(&tb_bus_type, &sw->dev, NULL, complete_rpm);
  1684. tb_switch_remove(sw);
  1685. pm_runtime_mark_last_busy(parent);
  1686. pm_runtime_put_autosuspend(parent);
  1687. put_device(parent);
  1688. }
  1689. static void icm_free_unplugged_children(struct tb_switch *sw)
  1690. {
  1691. struct tb_port *port;
  1692. tb_switch_for_each_port(sw, port) {
  1693. if (port->xdomain && port->xdomain->is_unplugged) {
  1694. tb_xdomain_remove(port->xdomain);
  1695. port->xdomain = NULL;
  1696. } else if (tb_port_has_remote(port)) {
  1697. if (port->remote->sw->is_unplugged) {
  1698. remove_unplugged_switch(port->remote->sw);
  1699. port->remote = NULL;
  1700. } else {
  1701. icm_free_unplugged_children(port->remote->sw);
  1702. }
  1703. }
  1704. }
  1705. }
  1706. static void icm_rescan_work(struct work_struct *work)
  1707. {
  1708. struct icm *icm = container_of(work, struct icm, rescan_work.work);
  1709. struct tb *tb = icm_to_tb(icm);
  1710. mutex_lock(&tb->lock);
  1711. if (tb->root_switch)
  1712. icm_free_unplugged_children(tb->root_switch);
  1713. mutex_unlock(&tb->lock);
  1714. }
  1715. static void icm_complete(struct tb *tb)
  1716. {
  1717. struct icm *icm = tb_priv(tb);
  1718. if (tb->nhi->going_away)
  1719. return;
  1720. /*
  1721. * If RTD3 was vetoed before we entered system suspend allow it
  1722. * again now before driver ready is sent. Firmware sends a new RTD3
  1723. * veto if it is still the case after we have sent it driver ready
  1724. * command.
  1725. */
  1726. icm_veto_end(tb);
  1727. icm_unplug_children(tb->root_switch);
  1728. /*
  1729. * Now all existing children should be resumed, start events
  1730. * from ICM to get updated status.
  1731. */
  1732. __icm_driver_ready(tb, NULL, NULL, NULL, NULL);
  1733. /*
  1734. * We do not get notifications of devices that have been
  1735. * unplugged during suspend so schedule rescan to clean them up
  1736. * if any.
  1737. */
  1738. queue_delayed_work(tb->wq, &icm->rescan_work, msecs_to_jiffies(500));
  1739. }
  1740. static int icm_runtime_suspend(struct tb *tb)
  1741. {
  1742. nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0);
  1743. return 0;
  1744. }
  1745. static int icm_runtime_suspend_switch(struct tb_switch *sw)
  1746. {
  1747. if (tb_route(sw))
  1748. reinit_completion(&sw->rpm_complete);
  1749. return 0;
  1750. }
  1751. static int icm_runtime_resume_switch(struct tb_switch *sw)
  1752. {
  1753. if (tb_route(sw)) {
  1754. if (!wait_for_completion_timeout(&sw->rpm_complete,
  1755. msecs_to_jiffies(500))) {
  1756. dev_dbg(&sw->dev, "runtime resuming timed out\n");
  1757. }
  1758. }
  1759. return 0;
  1760. }
  1761. static int icm_runtime_resume(struct tb *tb)
  1762. {
  1763. /*
  1764. * We can reuse the same resume functionality than with system
  1765. * suspend.
  1766. */
  1767. icm_complete(tb);
  1768. return 0;
  1769. }
  1770. static int icm_start(struct tb *tb)
  1771. {
  1772. struct icm *icm = tb_priv(tb);
  1773. int ret;
  1774. if (icm->safe_mode)
  1775. tb->root_switch = tb_switch_alloc_safe_mode(tb, &tb->dev, 0);
  1776. else
  1777. tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0);
  1778. if (IS_ERR(tb->root_switch))
  1779. return PTR_ERR(tb->root_switch);
  1780. tb->root_switch->no_nvm_upgrade = !icm->can_upgrade_nvm;
  1781. tb->root_switch->rpm = icm->rpm;
  1782. if (icm->set_uuid)
  1783. icm->set_uuid(tb);
  1784. ret = tb_switch_add(tb->root_switch);
  1785. if (ret) {
  1786. tb_switch_put(tb->root_switch);
  1787. tb->root_switch = NULL;
  1788. }
  1789. return ret;
  1790. }
  1791. static void icm_stop(struct tb *tb)
  1792. {
  1793. struct icm *icm = tb_priv(tb);
  1794. cancel_delayed_work(&icm->rescan_work);
  1795. tb_switch_remove(tb->root_switch);
  1796. tb->root_switch = NULL;
  1797. nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0);
  1798. kfree(icm->last_nvm_auth);
  1799. icm->last_nvm_auth = NULL;
  1800. }
  1801. static int icm_disconnect_pcie_paths(struct tb *tb)
  1802. {
  1803. return nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DISCONNECT_PCIE_PATHS, 0);
  1804. }
  1805. static void icm_usb4_switch_nvm_auth_complete(void *data)
  1806. {
  1807. struct usb4_switch_nvm_auth *auth = data;
  1808. struct icm *icm = auth->icm;
  1809. struct tb *tb = icm_to_tb(icm);
  1810. tb_dbg(tb, "NVM_AUTH response for %llx flags %#x status %#x\n",
  1811. get_route(auth->reply.route_hi, auth->reply.route_lo),
  1812. auth->reply.hdr.flags, auth->reply.status);
  1813. mutex_lock(&tb->lock);
  1814. if (WARN_ON(icm->last_nvm_auth))
  1815. kfree(icm->last_nvm_auth);
  1816. icm->last_nvm_auth = auth;
  1817. mutex_unlock(&tb->lock);
  1818. }
  1819. static int icm_usb4_switch_nvm_authenticate(struct tb *tb, u64 route)
  1820. {
  1821. struct usb4_switch_nvm_auth *auth;
  1822. struct icm *icm = tb_priv(tb);
  1823. struct tb_cfg_request *req;
  1824. int ret;
  1825. auth = kzalloc(sizeof(*auth), GFP_KERNEL);
  1826. if (!auth)
  1827. return -ENOMEM;
  1828. auth->icm = icm;
  1829. auth->request.hdr.code = ICM_USB4_SWITCH_OP;
  1830. auth->request.route_hi = upper_32_bits(route);
  1831. auth->request.route_lo = lower_32_bits(route);
  1832. auth->request.opcode = USB4_SWITCH_OP_NVM_AUTH;
  1833. req = tb_cfg_request_alloc();
  1834. if (!req) {
  1835. ret = -ENOMEM;
  1836. goto err_free_auth;
  1837. }
  1838. req->match = icm_match;
  1839. req->copy = icm_copy;
  1840. req->request = &auth->request;
  1841. req->request_size = sizeof(auth->request);
  1842. req->request_type = TB_CFG_PKG_ICM_CMD;
  1843. req->response = &auth->reply;
  1844. req->npackets = 1;
  1845. req->response_size = sizeof(auth->reply);
  1846. req->response_type = TB_CFG_PKG_ICM_RESP;
  1847. tb_dbg(tb, "NVM_AUTH request for %llx\n", route);
  1848. mutex_lock(&icm->request_lock);
  1849. ret = tb_cfg_request(tb->ctl, req, icm_usb4_switch_nvm_auth_complete,
  1850. auth);
  1851. mutex_unlock(&icm->request_lock);
  1852. tb_cfg_request_put(req);
  1853. if (ret)
  1854. goto err_free_auth;
  1855. return 0;
  1856. err_free_auth:
  1857. kfree(auth);
  1858. return ret;
  1859. }
  1860. static int icm_usb4_switch_op(struct tb_switch *sw, u16 opcode, u32 *metadata,
  1861. u8 *status, const void *tx_data, size_t tx_data_len,
  1862. void *rx_data, size_t rx_data_len)
  1863. {
  1864. struct icm_usb4_switch_op_response reply;
  1865. struct icm_usb4_switch_op request;
  1866. struct tb *tb = sw->tb;
  1867. struct icm *icm = tb_priv(tb);
  1868. u64 route = tb_route(sw);
  1869. int ret;
  1870. /*
  1871. * USB4 router operation proxy is supported in firmware if the
  1872. * protocol version is 3 or higher.
  1873. */
  1874. if (icm->proto_version < 3)
  1875. return -EOPNOTSUPP;
  1876. /*
  1877. * NVM_AUTH is a special USB4 proxy operation that does not
  1878. * return immediately so handle it separately.
  1879. */
  1880. if (opcode == USB4_SWITCH_OP_NVM_AUTH)
  1881. return icm_usb4_switch_nvm_authenticate(tb, route);
  1882. memset(&request, 0, sizeof(request));
  1883. request.hdr.code = ICM_USB4_SWITCH_OP;
  1884. request.route_hi = upper_32_bits(route);
  1885. request.route_lo = lower_32_bits(route);
  1886. request.opcode = opcode;
  1887. if (metadata)
  1888. request.metadata = *metadata;
  1889. if (tx_data_len) {
  1890. request.data_len_valid |= ICM_USB4_SWITCH_DATA_VALID;
  1891. if (tx_data_len < ARRAY_SIZE(request.data))
  1892. request.data_len_valid =
  1893. tx_data_len & ICM_USB4_SWITCH_DATA_LEN_MASK;
  1894. memcpy(request.data, tx_data, tx_data_len * sizeof(u32));
  1895. }
  1896. memset(&reply, 0, sizeof(reply));
  1897. ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
  1898. 1, ICM_RETRIES, ICM_TIMEOUT);
  1899. if (ret)
  1900. return ret;
  1901. if (reply.hdr.flags & ICM_FLAGS_ERROR)
  1902. return -EIO;
  1903. if (status)
  1904. *status = reply.status;
  1905. if (metadata)
  1906. *metadata = reply.metadata;
  1907. if (rx_data_len)
  1908. memcpy(rx_data, reply.data, rx_data_len * sizeof(u32));
  1909. return 0;
  1910. }
  1911. static int icm_usb4_switch_nvm_authenticate_status(struct tb_switch *sw,
  1912. u32 *status)
  1913. {
  1914. struct usb4_switch_nvm_auth *auth;
  1915. struct tb *tb = sw->tb;
  1916. struct icm *icm = tb_priv(tb);
  1917. int ret = 0;
  1918. if (icm->proto_version < 3)
  1919. return -EOPNOTSUPP;
  1920. auth = icm->last_nvm_auth;
  1921. icm->last_nvm_auth = NULL;
  1922. if (auth && auth->reply.route_hi == sw->config.route_hi &&
  1923. auth->reply.route_lo == sw->config.route_lo) {
  1924. tb_dbg(tb, "NVM_AUTH found for %llx flags %#x status %#x\n",
  1925. tb_route(sw), auth->reply.hdr.flags, auth->reply.status);
  1926. if (auth->reply.hdr.flags & ICM_FLAGS_ERROR)
  1927. ret = -EIO;
  1928. else
  1929. *status = auth->reply.status;
  1930. } else {
  1931. *status = 0;
  1932. }
  1933. kfree(auth);
  1934. return ret;
  1935. }
  1936. /* Falcon Ridge */
  1937. static const struct tb_cm_ops icm_fr_ops = {
  1938. .driver_ready = icm_driver_ready,
  1939. .start = icm_start,
  1940. .stop = icm_stop,
  1941. .suspend = icm_suspend,
  1942. .complete = icm_complete,
  1943. .handle_event = icm_handle_event,
  1944. .approve_switch = icm_fr_approve_switch,
  1945. .add_switch_key = icm_fr_add_switch_key,
  1946. .challenge_switch_key = icm_fr_challenge_switch_key,
  1947. .disconnect_pcie_paths = icm_disconnect_pcie_paths,
  1948. .approve_xdomain_paths = icm_fr_approve_xdomain_paths,
  1949. .disconnect_xdomain_paths = icm_fr_disconnect_xdomain_paths,
  1950. };
  1951. /* Alpine Ridge */
  1952. static const struct tb_cm_ops icm_ar_ops = {
  1953. .driver_ready = icm_driver_ready,
  1954. .start = icm_start,
  1955. .stop = icm_stop,
  1956. .suspend = icm_suspend,
  1957. .complete = icm_complete,
  1958. .runtime_suspend = icm_runtime_suspend,
  1959. .runtime_resume = icm_runtime_resume,
  1960. .runtime_suspend_switch = icm_runtime_suspend_switch,
  1961. .runtime_resume_switch = icm_runtime_resume_switch,
  1962. .handle_event = icm_handle_event,
  1963. .get_boot_acl = icm_ar_get_boot_acl,
  1964. .set_boot_acl = icm_ar_set_boot_acl,
  1965. .approve_switch = icm_fr_approve_switch,
  1966. .add_switch_key = icm_fr_add_switch_key,
  1967. .challenge_switch_key = icm_fr_challenge_switch_key,
  1968. .disconnect_pcie_paths = icm_disconnect_pcie_paths,
  1969. .approve_xdomain_paths = icm_fr_approve_xdomain_paths,
  1970. .disconnect_xdomain_paths = icm_fr_disconnect_xdomain_paths,
  1971. };
  1972. /* Titan Ridge */
  1973. static const struct tb_cm_ops icm_tr_ops = {
  1974. .driver_ready = icm_driver_ready,
  1975. .start = icm_start,
  1976. .stop = icm_stop,
  1977. .suspend = icm_suspend,
  1978. .complete = icm_complete,
  1979. .runtime_suspend = icm_runtime_suspend,
  1980. .runtime_resume = icm_runtime_resume,
  1981. .runtime_suspend_switch = icm_runtime_suspend_switch,
  1982. .runtime_resume_switch = icm_runtime_resume_switch,
  1983. .handle_event = icm_handle_event,
  1984. .get_boot_acl = icm_ar_get_boot_acl,
  1985. .set_boot_acl = icm_ar_set_boot_acl,
  1986. .approve_switch = icm_tr_approve_switch,
  1987. .add_switch_key = icm_tr_add_switch_key,
  1988. .challenge_switch_key = icm_tr_challenge_switch_key,
  1989. .disconnect_pcie_paths = icm_disconnect_pcie_paths,
  1990. .approve_xdomain_paths = icm_tr_approve_xdomain_paths,
  1991. .disconnect_xdomain_paths = icm_tr_disconnect_xdomain_paths,
  1992. .usb4_switch_op = icm_usb4_switch_op,
  1993. .usb4_switch_nvm_authenticate_status =
  1994. icm_usb4_switch_nvm_authenticate_status,
  1995. };
  1996. /* Ice Lake */
  1997. static const struct tb_cm_ops icm_icl_ops = {
  1998. .driver_ready = icm_driver_ready,
  1999. .start = icm_start,
  2000. .stop = icm_stop,
  2001. .complete = icm_complete,
  2002. .runtime_suspend = icm_runtime_suspend,
  2003. .runtime_resume = icm_runtime_resume,
  2004. .handle_event = icm_handle_event,
  2005. .approve_xdomain_paths = icm_tr_approve_xdomain_paths,
  2006. .disconnect_xdomain_paths = icm_tr_disconnect_xdomain_paths,
  2007. .usb4_switch_op = icm_usb4_switch_op,
  2008. .usb4_switch_nvm_authenticate_status =
  2009. icm_usb4_switch_nvm_authenticate_status,
  2010. };
  2011. struct tb *icm_probe(struct tb_nhi *nhi)
  2012. {
  2013. struct icm *icm;
  2014. struct tb *tb;
  2015. tb = tb_domain_alloc(nhi, ICM_TIMEOUT, sizeof(struct icm));
  2016. if (!tb)
  2017. return NULL;
  2018. icm = tb_priv(tb);
  2019. INIT_DELAYED_WORK(&icm->rescan_work, icm_rescan_work);
  2020. mutex_init(&icm->request_lock);
  2021. switch (nhi->pdev->device) {
  2022. case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI:
  2023. case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI:
  2024. icm->can_upgrade_nvm = true;
  2025. icm->is_supported = icm_fr_is_supported;
  2026. icm->get_route = icm_fr_get_route;
  2027. icm->save_devices = icm_fr_save_devices;
  2028. icm->driver_ready = icm_fr_driver_ready;
  2029. icm->device_connected = icm_fr_device_connected;
  2030. icm->device_disconnected = icm_fr_device_disconnected;
  2031. icm->xdomain_connected = icm_fr_xdomain_connected;
  2032. icm->xdomain_disconnected = icm_fr_xdomain_disconnected;
  2033. tb->cm_ops = &icm_fr_ops;
  2034. break;
  2035. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_NHI:
  2036. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_NHI:
  2037. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_NHI:
  2038. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_NHI:
  2039. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_NHI:
  2040. icm->max_boot_acl = ICM_AR_PREBOOT_ACL_ENTRIES;
  2041. /*
  2042. * NVM upgrade has not been tested on Apple systems and
  2043. * they don't provide images publicly either. To be on
  2044. * the safe side prevent root switch NVM upgrade on Macs
  2045. * for now.
  2046. */
  2047. icm->can_upgrade_nvm = !x86_apple_machine;
  2048. icm->is_supported = icm_ar_is_supported;
  2049. icm->cio_reset = icm_ar_cio_reset;
  2050. icm->get_mode = icm_ar_get_mode;
  2051. icm->get_route = icm_ar_get_route;
  2052. icm->save_devices = icm_fr_save_devices;
  2053. icm->driver_ready = icm_ar_driver_ready;
  2054. icm->device_connected = icm_fr_device_connected;
  2055. icm->device_disconnected = icm_fr_device_disconnected;
  2056. icm->xdomain_connected = icm_fr_xdomain_connected;
  2057. icm->xdomain_disconnected = icm_fr_xdomain_disconnected;
  2058. tb->cm_ops = &icm_ar_ops;
  2059. break;
  2060. case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_NHI:
  2061. case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_NHI:
  2062. icm->max_boot_acl = ICM_AR_PREBOOT_ACL_ENTRIES;
  2063. icm->can_upgrade_nvm = !x86_apple_machine;
  2064. icm->is_supported = icm_ar_is_supported;
  2065. icm->cio_reset = icm_tr_cio_reset;
  2066. icm->get_mode = icm_ar_get_mode;
  2067. icm->driver_ready = icm_tr_driver_ready;
  2068. icm->device_connected = icm_tr_device_connected;
  2069. icm->device_disconnected = icm_tr_device_disconnected;
  2070. icm->xdomain_connected = icm_tr_xdomain_connected;
  2071. icm->xdomain_disconnected = icm_tr_xdomain_disconnected;
  2072. tb->cm_ops = &icm_tr_ops;
  2073. break;
  2074. case PCI_DEVICE_ID_INTEL_ICL_NHI0:
  2075. case PCI_DEVICE_ID_INTEL_ICL_NHI1:
  2076. icm->is_supported = icm_fr_is_supported;
  2077. icm->driver_ready = icm_icl_driver_ready;
  2078. icm->set_uuid = icm_icl_set_uuid;
  2079. icm->device_connected = icm_icl_device_connected;
  2080. icm->device_disconnected = icm_tr_device_disconnected;
  2081. icm->xdomain_connected = icm_tr_xdomain_connected;
  2082. icm->xdomain_disconnected = icm_tr_xdomain_disconnected;
  2083. icm->rtd3_veto = icm_icl_rtd3_veto;
  2084. tb->cm_ops = &icm_icl_ops;
  2085. break;
  2086. case PCI_DEVICE_ID_INTEL_TGL_NHI0:
  2087. case PCI_DEVICE_ID_INTEL_TGL_NHI1:
  2088. case PCI_DEVICE_ID_INTEL_TGL_H_NHI0:
  2089. case PCI_DEVICE_ID_INTEL_TGL_H_NHI1:
  2090. case PCI_DEVICE_ID_INTEL_ADL_NHI0:
  2091. case PCI_DEVICE_ID_INTEL_ADL_NHI1:
  2092. case PCI_DEVICE_ID_INTEL_RPL_NHI0:
  2093. case PCI_DEVICE_ID_INTEL_RPL_NHI1:
  2094. case PCI_DEVICE_ID_INTEL_MTL_M_NHI0:
  2095. case PCI_DEVICE_ID_INTEL_MTL_P_NHI0:
  2096. case PCI_DEVICE_ID_INTEL_MTL_P_NHI1:
  2097. icm->is_supported = icm_tgl_is_supported;
  2098. icm->driver_ready = icm_icl_driver_ready;
  2099. icm->set_uuid = icm_icl_set_uuid;
  2100. icm->device_connected = icm_icl_device_connected;
  2101. icm->device_disconnected = icm_tr_device_disconnected;
  2102. icm->xdomain_connected = icm_tr_xdomain_connected;
  2103. icm->xdomain_disconnected = icm_tr_xdomain_disconnected;
  2104. icm->rtd3_veto = icm_icl_rtd3_veto;
  2105. tb->cm_ops = &icm_icl_ops;
  2106. break;
  2107. case PCI_DEVICE_ID_INTEL_MAPLE_RIDGE_2C_NHI:
  2108. case PCI_DEVICE_ID_INTEL_MAPLE_RIDGE_4C_NHI:
  2109. icm->is_supported = icm_tgl_is_supported;
  2110. icm->get_mode = icm_ar_get_mode;
  2111. icm->driver_ready = icm_tr_driver_ready;
  2112. icm->device_connected = icm_tr_device_connected;
  2113. icm->device_disconnected = icm_tr_device_disconnected;
  2114. icm->xdomain_connected = icm_tr_xdomain_connected;
  2115. icm->xdomain_disconnected = icm_tr_xdomain_disconnected;
  2116. tb->cm_ops = &icm_tr_ops;
  2117. break;
  2118. }
  2119. if (!icm->is_supported || !icm->is_supported(tb)) {
  2120. dev_dbg(&nhi->pdev->dev, "ICM not supported on this controller\n");
  2121. tb_domain_put(tb);
  2122. return NULL;
  2123. }
  2124. tb_dbg(tb, "using firmware connection manager\n");
  2125. return tb;
  2126. }