icm.c 58 KB

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