icm.c 65 KB

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