tb.h 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Thunderbolt driver - bus logic (NHI independent)
  4. *
  5. * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
  6. * Copyright (C) 2018, Intel Corporation
  7. */
  8. #ifndef TB_H_
  9. #define TB_H_
  10. #include <linux/nvmem-provider.h>
  11. #include <linux/pci.h>
  12. #include <linux/thunderbolt.h>
  13. #include <linux/uuid.h>
  14. #include "tb_regs.h"
  15. #include "ctl.h"
  16. #include "dma_port.h"
  17. #define NVM_MIN_SIZE SZ_32K
  18. #define NVM_MAX_SIZE SZ_512K
  19. /* Intel specific NVM offsets */
  20. #define NVM_DEVID 0x05
  21. #define NVM_VERSION 0x08
  22. #define NVM_FLASH_SIZE 0x45
  23. /**
  24. * struct tb_nvm - Structure holding NVM information
  25. * @dev: Owner of the NVM
  26. * @major: Major version number of the active NVM portion
  27. * @minor: Minor version number of the active NVM portion
  28. * @id: Identifier used with both NVM portions
  29. * @active: Active portion NVMem device
  30. * @non_active: Non-active portion NVMem device
  31. * @buf: Buffer where the NVM image is stored before it is written to
  32. * the actual NVM flash device
  33. * @buf_data_size: Number of bytes actually consumed by the new NVM
  34. * image
  35. * @authenticating: The device is authenticating the new NVM
  36. * @flushed: The image has been flushed to the storage area
  37. *
  38. * The user of this structure needs to handle serialization of possible
  39. * concurrent access.
  40. */
  41. struct tb_nvm {
  42. struct device *dev;
  43. u8 major;
  44. u8 minor;
  45. int id;
  46. struct nvmem_device *active;
  47. struct nvmem_device *non_active;
  48. void *buf;
  49. size_t buf_data_size;
  50. bool authenticating;
  51. bool flushed;
  52. };
  53. #define TB_SWITCH_KEY_SIZE 32
  54. #define TB_SWITCH_MAX_DEPTH 6
  55. #define USB4_SWITCH_MAX_DEPTH 5
  56. /**
  57. * enum tb_switch_tmu_rate - TMU refresh rate
  58. * @TB_SWITCH_TMU_RATE_OFF: %0 (Disable Time Sync handshake)
  59. * @TB_SWITCH_TMU_RATE_HIFI: %16 us time interval between successive
  60. * transmission of the Delay Request TSNOS
  61. * (Time Sync Notification Ordered Set) on a Link
  62. * @TB_SWITCH_TMU_RATE_NORMAL: %1 ms time interval between successive
  63. * transmission of the Delay Request TSNOS on
  64. * a Link
  65. */
  66. enum tb_switch_tmu_rate {
  67. TB_SWITCH_TMU_RATE_OFF = 0,
  68. TB_SWITCH_TMU_RATE_HIFI = 16,
  69. TB_SWITCH_TMU_RATE_NORMAL = 1000,
  70. };
  71. /**
  72. * struct tb_switch_tmu - Structure holding switch TMU configuration
  73. * @cap: Offset to the TMU capability (%0 if not found)
  74. * @has_ucap: Does the switch support uni-directional mode
  75. * @rate: TMU refresh rate related to upstream switch. In case of root
  76. * switch this holds the domain rate.
  77. * @unidirectional: Is the TMU in uni-directional or bi-directional mode
  78. * related to upstream switch. Don't case for root switch.
  79. */
  80. struct tb_switch_tmu {
  81. int cap;
  82. bool has_ucap;
  83. enum tb_switch_tmu_rate rate;
  84. bool unidirectional;
  85. };
  86. /**
  87. * struct tb_switch - a thunderbolt switch
  88. * @dev: Device for the switch
  89. * @config: Switch configuration
  90. * @ports: Ports in this switch
  91. * @dma_port: If the switch has port supporting DMA configuration based
  92. * mailbox this will hold the pointer to that (%NULL
  93. * otherwise). If set it also means the switch has
  94. * upgradeable NVM.
  95. * @tmu: The switch TMU configuration
  96. * @tb: Pointer to the domain the switch belongs to
  97. * @uid: Unique ID of the switch
  98. * @uuid: UUID of the switch (or %NULL if not supported)
  99. * @vendor: Vendor ID of the switch
  100. * @device: Device ID of the switch
  101. * @vendor_name: Name of the vendor (or %NULL if not known)
  102. * @device_name: Name of the device (or %NULL if not known)
  103. * @link_speed: Speed of the link in Gb/s
  104. * @link_width: Width of the link (1 or 2)
  105. * @link_usb4: Upstream link is USB4
  106. * @generation: Switch Thunderbolt generation
  107. * @cap_plug_events: Offset to the plug events capability (%0 if not found)
  108. * @cap_lc: Offset to the link controller capability (%0 if not found)
  109. * @is_unplugged: The switch is going away
  110. * @drom: DROM of the switch (%NULL if not found)
  111. * @nvm: Pointer to the NVM if the switch has one (%NULL otherwise)
  112. * @no_nvm_upgrade: Prevent NVM upgrade of this switch
  113. * @safe_mode: The switch is in safe-mode
  114. * @boot: Whether the switch was already authorized on boot or not
  115. * @rpm: The switch supports runtime PM
  116. * @authorized: Whether the switch is authorized by user or policy
  117. * @security_level: Switch supported security level
  118. * @debugfs_dir: Pointer to the debugfs structure
  119. * @key: Contains the key used to challenge the device or %NULL if not
  120. * supported. Size of the key is %TB_SWITCH_KEY_SIZE.
  121. * @connection_id: Connection ID used with ICM messaging
  122. * @connection_key: Connection key used with ICM messaging
  123. * @link: Root switch link this switch is connected (ICM only)
  124. * @depth: Depth in the chain this switch is connected (ICM only)
  125. * @rpm_complete: Completion used to wait for runtime resume to
  126. * complete (ICM only)
  127. * @quirks: Quirks used for this Thunderbolt switch
  128. *
  129. * When the switch is being added or removed to the domain (other
  130. * switches) you need to have domain lock held.
  131. */
  132. struct tb_switch {
  133. struct device dev;
  134. struct tb_regs_switch_header config;
  135. struct tb_port *ports;
  136. struct tb_dma_port *dma_port;
  137. struct tb_switch_tmu tmu;
  138. struct tb *tb;
  139. u64 uid;
  140. uuid_t *uuid;
  141. u16 vendor;
  142. u16 device;
  143. const char *vendor_name;
  144. const char *device_name;
  145. unsigned int link_speed;
  146. unsigned int link_width;
  147. bool link_usb4;
  148. unsigned int generation;
  149. int cap_plug_events;
  150. int cap_lc;
  151. bool is_unplugged;
  152. u8 *drom;
  153. struct tb_nvm *nvm;
  154. bool no_nvm_upgrade;
  155. bool safe_mode;
  156. bool boot;
  157. bool rpm;
  158. unsigned int authorized;
  159. enum tb_security_level security_level;
  160. struct dentry *debugfs_dir;
  161. u8 *key;
  162. u8 connection_id;
  163. u8 connection_key;
  164. u8 link;
  165. u8 depth;
  166. struct completion rpm_complete;
  167. unsigned long quirks;
  168. };
  169. /**
  170. * struct tb_port - a thunderbolt port, part of a tb_switch
  171. * @config: Cached port configuration read from registers
  172. * @sw: Switch the port belongs to
  173. * @remote: Remote port (%NULL if not connected)
  174. * @xdomain: Remote host (%NULL if not connected)
  175. * @cap_phy: Offset, zero if not found
  176. * @cap_tmu: Offset of the adapter specific TMU capability (%0 if not present)
  177. * @cap_adap: Offset of the adapter specific capability (%0 if not present)
  178. * @cap_usb4: Offset to the USB4 port capability (%0 if not present)
  179. * @port: Port number on switch
  180. * @disabled: Disabled by eeprom or enabled but not implemented
  181. * @bonded: true if the port is bonded (two lanes combined as one)
  182. * @dual_link_port: If the switch is connected using two ports, points
  183. * to the other port.
  184. * @link_nr: Is this primary or secondary port on the dual_link.
  185. * @in_hopids: Currently allocated input HopIDs
  186. * @out_hopids: Currently allocated output HopIDs
  187. * @list: Used to link ports to DP resources list
  188. */
  189. struct tb_port {
  190. struct tb_regs_port_header config;
  191. struct tb_switch *sw;
  192. struct tb_port *remote;
  193. struct tb_xdomain *xdomain;
  194. int cap_phy;
  195. int cap_tmu;
  196. int cap_adap;
  197. int cap_usb4;
  198. u8 port;
  199. bool disabled;
  200. bool bonded;
  201. struct tb_port *dual_link_port;
  202. u8 link_nr:1;
  203. struct ida in_hopids;
  204. struct ida out_hopids;
  205. struct list_head list;
  206. };
  207. /**
  208. * tb_retimer: Thunderbolt retimer
  209. * @dev: Device for the retimer
  210. * @tb: Pointer to the domain the retimer belongs to
  211. * @index: Retimer index facing the router USB4 port
  212. * @vendor: Vendor ID of the retimer
  213. * @device: Device ID of the retimer
  214. * @port: Pointer to the lane 0 adapter
  215. * @nvm: Pointer to the NVM if the retimer has one (%NULL otherwise)
  216. * @auth_status: Status of last NVM authentication
  217. */
  218. struct tb_retimer {
  219. struct device dev;
  220. struct tb *tb;
  221. u8 index;
  222. u32 vendor;
  223. u32 device;
  224. struct tb_port *port;
  225. struct tb_nvm *nvm;
  226. u32 auth_status;
  227. };
  228. /**
  229. * struct tb_path_hop - routing information for a tb_path
  230. * @in_port: Ingress port of a switch
  231. * @out_port: Egress port of a switch where the packet is routed out
  232. * (must be on the same switch than @in_port)
  233. * @in_hop_index: HopID where the path configuration entry is placed in
  234. * the path config space of @in_port.
  235. * @in_counter_index: Used counter index (not used in the driver
  236. * currently, %-1 to disable)
  237. * @next_hop_index: HopID of the packet when it is routed out from @out_port
  238. * @initial_credits: Number of initial flow control credits allocated for
  239. * the path
  240. *
  241. * Hop configuration is always done on the IN port of a switch.
  242. * in_port and out_port have to be on the same switch. Packets arriving on
  243. * in_port with "hop" = in_hop_index will get routed to through out_port. The
  244. * next hop to take (on out_port->remote) is determined by
  245. * next_hop_index. When routing packet to another switch (out->remote is
  246. * set) the @next_hop_index must match the @in_hop_index of that next
  247. * hop to make routing possible.
  248. *
  249. * in_counter_index is the index of a counter (in TB_CFG_COUNTERS) on the in
  250. * port.
  251. */
  252. struct tb_path_hop {
  253. struct tb_port *in_port;
  254. struct tb_port *out_port;
  255. int in_hop_index;
  256. int in_counter_index;
  257. int next_hop_index;
  258. unsigned int initial_credits;
  259. };
  260. /**
  261. * enum tb_path_port - path options mask
  262. * @TB_PATH_NONE: Do not activate on any hop on path
  263. * @TB_PATH_SOURCE: Activate on the first hop (out of src)
  264. * @TB_PATH_INTERNAL: Activate on the intermediate hops (not the first/last)
  265. * @TB_PATH_DESTINATION: Activate on the last hop (into dst)
  266. * @TB_PATH_ALL: Activate on all hops on the path
  267. */
  268. enum tb_path_port {
  269. TB_PATH_NONE = 0,
  270. TB_PATH_SOURCE = 1,
  271. TB_PATH_INTERNAL = 2,
  272. TB_PATH_DESTINATION = 4,
  273. TB_PATH_ALL = 7,
  274. };
  275. /**
  276. * struct tb_path - a unidirectional path between two ports
  277. * @tb: Pointer to the domain structure
  278. * @name: Name of the path (used for debugging)
  279. * @nfc_credits: Number of non flow controlled credits allocated for the path
  280. * @ingress_shared_buffer: Shared buffering used for ingress ports on the path
  281. * @egress_shared_buffer: Shared buffering used for egress ports on the path
  282. * @ingress_fc_enable: Flow control for ingress ports on the path
  283. * @egress_fc_enable: Flow control for egress ports on the path
  284. * @priority: Priority group if the path
  285. * @weight: Weight of the path inside the priority group
  286. * @drop_packages: Drop packages from queue tail or head
  287. * @activated: Is the path active
  288. * @clear_fc: Clear all flow control from the path config space entries
  289. * when deactivating this path
  290. * @hops: Path hops
  291. * @path_length: How many hops the path uses
  292. *
  293. * A path consists of a number of hops (see &struct tb_path_hop). To
  294. * establish a PCIe tunnel two paths have to be created between the two
  295. * PCIe ports.
  296. */
  297. struct tb_path {
  298. struct tb *tb;
  299. const char *name;
  300. int nfc_credits;
  301. enum tb_path_port ingress_shared_buffer;
  302. enum tb_path_port egress_shared_buffer;
  303. enum tb_path_port ingress_fc_enable;
  304. enum tb_path_port egress_fc_enable;
  305. unsigned int priority:3;
  306. int weight:4;
  307. bool drop_packages;
  308. bool activated;
  309. bool clear_fc;
  310. struct tb_path_hop *hops;
  311. int path_length;
  312. };
  313. /* HopIDs 0-7 are reserved by the Thunderbolt protocol */
  314. #define TB_PATH_MIN_HOPID 8
  315. /*
  316. * Support paths from the farthest (depth 6) router to the host and back
  317. * to the same level (not necessarily to the same router).
  318. */
  319. #define TB_PATH_MAX_HOPS (7 * 2)
  320. /* Possible wake types */
  321. #define TB_WAKE_ON_CONNECT BIT(0)
  322. #define TB_WAKE_ON_DISCONNECT BIT(1)
  323. #define TB_WAKE_ON_USB4 BIT(2)
  324. #define TB_WAKE_ON_USB3 BIT(3)
  325. #define TB_WAKE_ON_PCIE BIT(4)
  326. /**
  327. * struct tb_cm_ops - Connection manager specific operations vector
  328. * @driver_ready: Called right after control channel is started. Used by
  329. * ICM to send driver ready message to the firmware.
  330. * @start: Starts the domain
  331. * @stop: Stops the domain
  332. * @suspend_noirq: Connection manager specific suspend_noirq
  333. * @resume_noirq: Connection manager specific resume_noirq
  334. * @suspend: Connection manager specific suspend
  335. * @freeze_noirq: Connection manager specific freeze_noirq
  336. * @thaw_noirq: Connection manager specific thaw_noirq
  337. * @complete: Connection manager specific complete
  338. * @runtime_suspend: Connection manager specific runtime_suspend
  339. * @runtime_resume: Connection manager specific runtime_resume
  340. * @runtime_suspend_switch: Runtime suspend a switch
  341. * @runtime_resume_switch: Runtime resume a switch
  342. * @handle_event: Handle thunderbolt event
  343. * @get_boot_acl: Get boot ACL list
  344. * @set_boot_acl: Set boot ACL list
  345. * @approve_switch: Approve switch
  346. * @add_switch_key: Add key to switch
  347. * @challenge_switch_key: Challenge switch using key
  348. * @disconnect_pcie_paths: Disconnects PCIe paths before NVM update
  349. * @approve_xdomain_paths: Approve (establish) XDomain DMA paths
  350. * @disconnect_xdomain_paths: Disconnect XDomain DMA paths
  351. */
  352. struct tb_cm_ops {
  353. int (*driver_ready)(struct tb *tb);
  354. int (*start)(struct tb *tb);
  355. void (*stop)(struct tb *tb);
  356. int (*suspend_noirq)(struct tb *tb);
  357. int (*resume_noirq)(struct tb *tb);
  358. int (*suspend)(struct tb *tb);
  359. int (*freeze_noirq)(struct tb *tb);
  360. int (*thaw_noirq)(struct tb *tb);
  361. void (*complete)(struct tb *tb);
  362. int (*runtime_suspend)(struct tb *tb);
  363. int (*runtime_resume)(struct tb *tb);
  364. int (*runtime_suspend_switch)(struct tb_switch *sw);
  365. int (*runtime_resume_switch)(struct tb_switch *sw);
  366. void (*handle_event)(struct tb *tb, enum tb_cfg_pkg_type,
  367. const void *buf, size_t size);
  368. int (*get_boot_acl)(struct tb *tb, uuid_t *uuids, size_t nuuids);
  369. int (*set_boot_acl)(struct tb *tb, const uuid_t *uuids, size_t nuuids);
  370. int (*approve_switch)(struct tb *tb, struct tb_switch *sw);
  371. int (*add_switch_key)(struct tb *tb, struct tb_switch *sw);
  372. int (*challenge_switch_key)(struct tb *tb, struct tb_switch *sw,
  373. const u8 *challenge, u8 *response);
  374. int (*disconnect_pcie_paths)(struct tb *tb);
  375. int (*approve_xdomain_paths)(struct tb *tb, struct tb_xdomain *xd);
  376. int (*disconnect_xdomain_paths)(struct tb *tb, struct tb_xdomain *xd);
  377. };
  378. static inline void *tb_priv(struct tb *tb)
  379. {
  380. return (void *)tb->privdata;
  381. }
  382. #define TB_AUTOSUSPEND_DELAY 15000 /* ms */
  383. /* helper functions & macros */
  384. /**
  385. * tb_upstream_port() - return the upstream port of a switch
  386. *
  387. * Every switch has an upstream port (for the root switch it is the NHI).
  388. *
  389. * During switch alloc/init tb_upstream_port()->remote may be NULL, even for
  390. * non root switches (on the NHI port remote is always NULL).
  391. *
  392. * Return: Returns the upstream port of the switch.
  393. */
  394. static inline struct tb_port *tb_upstream_port(struct tb_switch *sw)
  395. {
  396. return &sw->ports[sw->config.upstream_port_number];
  397. }
  398. /**
  399. * tb_is_upstream_port() - Is the port upstream facing
  400. * @port: Port to check
  401. *
  402. * Returns true if @port is upstream facing port. In case of dual link
  403. * ports both return true.
  404. */
  405. static inline bool tb_is_upstream_port(const struct tb_port *port)
  406. {
  407. const struct tb_port *upstream_port = tb_upstream_port(port->sw);
  408. return port == upstream_port || port->dual_link_port == upstream_port;
  409. }
  410. static inline u64 tb_route(const struct tb_switch *sw)
  411. {
  412. return ((u64) sw->config.route_hi) << 32 | sw->config.route_lo;
  413. }
  414. static inline struct tb_port *tb_port_at(u64 route, struct tb_switch *sw)
  415. {
  416. u8 port;
  417. port = route >> (sw->config.depth * 8);
  418. if (WARN_ON(port > sw->config.max_port_number))
  419. return NULL;
  420. return &sw->ports[port];
  421. }
  422. /**
  423. * tb_port_has_remote() - Does the port have switch connected downstream
  424. * @port: Port to check
  425. *
  426. * Returns true only when the port is primary port and has remote set.
  427. */
  428. static inline bool tb_port_has_remote(const struct tb_port *port)
  429. {
  430. if (tb_is_upstream_port(port))
  431. return false;
  432. if (!port->remote)
  433. return false;
  434. if (port->dual_link_port && port->link_nr)
  435. return false;
  436. return true;
  437. }
  438. static inline bool tb_port_is_null(const struct tb_port *port)
  439. {
  440. return port && port->port && port->config.type == TB_TYPE_PORT;
  441. }
  442. static inline bool tb_port_is_nhi(const struct tb_port *port)
  443. {
  444. return port && port->config.type == TB_TYPE_NHI;
  445. }
  446. static inline bool tb_port_is_pcie_down(const struct tb_port *port)
  447. {
  448. return port && port->config.type == TB_TYPE_PCIE_DOWN;
  449. }
  450. static inline bool tb_port_is_pcie_up(const struct tb_port *port)
  451. {
  452. return port && port->config.type == TB_TYPE_PCIE_UP;
  453. }
  454. static inline bool tb_port_is_dpin(const struct tb_port *port)
  455. {
  456. return port && port->config.type == TB_TYPE_DP_HDMI_IN;
  457. }
  458. static inline bool tb_port_is_dpout(const struct tb_port *port)
  459. {
  460. return port && port->config.type == TB_TYPE_DP_HDMI_OUT;
  461. }
  462. static inline bool tb_port_is_usb3_down(const struct tb_port *port)
  463. {
  464. return port && port->config.type == TB_TYPE_USB3_DOWN;
  465. }
  466. static inline bool tb_port_is_usb3_up(const struct tb_port *port)
  467. {
  468. return port && port->config.type == TB_TYPE_USB3_UP;
  469. }
  470. static inline int tb_sw_read(struct tb_switch *sw, void *buffer,
  471. enum tb_cfg_space space, u32 offset, u32 length)
  472. {
  473. if (sw->is_unplugged)
  474. return -ENODEV;
  475. return tb_cfg_read(sw->tb->ctl,
  476. buffer,
  477. tb_route(sw),
  478. 0,
  479. space,
  480. offset,
  481. length);
  482. }
  483. static inline int tb_sw_write(struct tb_switch *sw, const void *buffer,
  484. enum tb_cfg_space space, u32 offset, u32 length)
  485. {
  486. if (sw->is_unplugged)
  487. return -ENODEV;
  488. return tb_cfg_write(sw->tb->ctl,
  489. buffer,
  490. tb_route(sw),
  491. 0,
  492. space,
  493. offset,
  494. length);
  495. }
  496. static inline int tb_port_read(struct tb_port *port, void *buffer,
  497. enum tb_cfg_space space, u32 offset, u32 length)
  498. {
  499. if (port->sw->is_unplugged)
  500. return -ENODEV;
  501. return tb_cfg_read(port->sw->tb->ctl,
  502. buffer,
  503. tb_route(port->sw),
  504. port->port,
  505. space,
  506. offset,
  507. length);
  508. }
  509. static inline int tb_port_write(struct tb_port *port, const void *buffer,
  510. enum tb_cfg_space space, u32 offset, u32 length)
  511. {
  512. if (port->sw->is_unplugged)
  513. return -ENODEV;
  514. return tb_cfg_write(port->sw->tb->ctl,
  515. buffer,
  516. tb_route(port->sw),
  517. port->port,
  518. space,
  519. offset,
  520. length);
  521. }
  522. #define tb_err(tb, fmt, arg...) dev_err(&(tb)->nhi->pdev->dev, fmt, ## arg)
  523. #define tb_WARN(tb, fmt, arg...) dev_WARN(&(tb)->nhi->pdev->dev, fmt, ## arg)
  524. #define tb_warn(tb, fmt, arg...) dev_warn(&(tb)->nhi->pdev->dev, fmt, ## arg)
  525. #define tb_info(tb, fmt, arg...) dev_info(&(tb)->nhi->pdev->dev, fmt, ## arg)
  526. #define tb_dbg(tb, fmt, arg...) dev_dbg(&(tb)->nhi->pdev->dev, fmt, ## arg)
  527. #define __TB_SW_PRINT(level, sw, fmt, arg...) \
  528. do { \
  529. const struct tb_switch *__sw = (sw); \
  530. level(__sw->tb, "%llx: " fmt, \
  531. tb_route(__sw), ## arg); \
  532. } while (0)
  533. #define tb_sw_WARN(sw, fmt, arg...) __TB_SW_PRINT(tb_WARN, sw, fmt, ##arg)
  534. #define tb_sw_warn(sw, fmt, arg...) __TB_SW_PRINT(tb_warn, sw, fmt, ##arg)
  535. #define tb_sw_info(sw, fmt, arg...) __TB_SW_PRINT(tb_info, sw, fmt, ##arg)
  536. #define tb_sw_dbg(sw, fmt, arg...) __TB_SW_PRINT(tb_dbg, sw, fmt, ##arg)
  537. #define __TB_PORT_PRINT(level, _port, fmt, arg...) \
  538. do { \
  539. const struct tb_port *__port = (_port); \
  540. level(__port->sw->tb, "%llx:%x: " fmt, \
  541. tb_route(__port->sw), __port->port, ## arg); \
  542. } while (0)
  543. #define tb_port_WARN(port, fmt, arg...) \
  544. __TB_PORT_PRINT(tb_WARN, port, fmt, ##arg)
  545. #define tb_port_warn(port, fmt, arg...) \
  546. __TB_PORT_PRINT(tb_warn, port, fmt, ##arg)
  547. #define tb_port_info(port, fmt, arg...) \
  548. __TB_PORT_PRINT(tb_info, port, fmt, ##arg)
  549. #define tb_port_dbg(port, fmt, arg...) \
  550. __TB_PORT_PRINT(tb_dbg, port, fmt, ##arg)
  551. struct tb *icm_probe(struct tb_nhi *nhi);
  552. struct tb *tb_probe(struct tb_nhi *nhi);
  553. extern struct device_type tb_domain_type;
  554. extern struct device_type tb_retimer_type;
  555. extern struct device_type tb_switch_type;
  556. int tb_domain_init(void);
  557. void tb_domain_exit(void);
  558. int tb_xdomain_init(void);
  559. void tb_xdomain_exit(void);
  560. struct tb *tb_domain_alloc(struct tb_nhi *nhi, size_t privsize);
  561. int tb_domain_add(struct tb *tb);
  562. void tb_domain_remove(struct tb *tb);
  563. int tb_domain_suspend_noirq(struct tb *tb);
  564. int tb_domain_resume_noirq(struct tb *tb);
  565. int tb_domain_suspend(struct tb *tb);
  566. int tb_domain_freeze_noirq(struct tb *tb);
  567. int tb_domain_thaw_noirq(struct tb *tb);
  568. void tb_domain_complete(struct tb *tb);
  569. int tb_domain_runtime_suspend(struct tb *tb);
  570. int tb_domain_runtime_resume(struct tb *tb);
  571. int tb_domain_approve_switch(struct tb *tb, struct tb_switch *sw);
  572. int tb_domain_approve_switch_key(struct tb *tb, struct tb_switch *sw);
  573. int tb_domain_challenge_switch_key(struct tb *tb, struct tb_switch *sw);
  574. int tb_domain_disconnect_pcie_paths(struct tb *tb);
  575. int tb_domain_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd);
  576. int tb_domain_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd);
  577. int tb_domain_disconnect_all_paths(struct tb *tb);
  578. static inline struct tb *tb_domain_get(struct tb *tb)
  579. {
  580. if (tb)
  581. get_device(&tb->dev);
  582. return tb;
  583. }
  584. static inline void tb_domain_put(struct tb *tb)
  585. {
  586. put_device(&tb->dev);
  587. }
  588. struct tb_nvm *tb_nvm_alloc(struct device *dev);
  589. int tb_nvm_add_active(struct tb_nvm *nvm, size_t size, nvmem_reg_read_t reg_read);
  590. int tb_nvm_write_buf(struct tb_nvm *nvm, unsigned int offset, void *val,
  591. size_t bytes);
  592. int tb_nvm_add_non_active(struct tb_nvm *nvm, size_t size,
  593. nvmem_reg_write_t reg_write);
  594. void tb_nvm_free(struct tb_nvm *nvm);
  595. void tb_nvm_exit(void);
  596. struct tb_switch *tb_switch_alloc(struct tb *tb, struct device *parent,
  597. u64 route);
  598. struct tb_switch *tb_switch_alloc_safe_mode(struct tb *tb,
  599. struct device *parent, u64 route);
  600. int tb_switch_configure(struct tb_switch *sw);
  601. int tb_switch_add(struct tb_switch *sw);
  602. void tb_switch_remove(struct tb_switch *sw);
  603. void tb_switch_suspend(struct tb_switch *sw, bool runtime);
  604. int tb_switch_resume(struct tb_switch *sw);
  605. int tb_switch_reset(struct tb_switch *sw);
  606. void tb_sw_set_unplugged(struct tb_switch *sw);
  607. struct tb_port *tb_switch_find_port(struct tb_switch *sw,
  608. enum tb_port_type type);
  609. struct tb_switch *tb_switch_find_by_link_depth(struct tb *tb, u8 link,
  610. u8 depth);
  611. struct tb_switch *tb_switch_find_by_uuid(struct tb *tb, const uuid_t *uuid);
  612. struct tb_switch *tb_switch_find_by_route(struct tb *tb, u64 route);
  613. /**
  614. * tb_switch_for_each_port() - Iterate over each switch port
  615. * @sw: Switch whose ports to iterate
  616. * @p: Port used as iterator
  617. *
  618. * Iterates over each switch port skipping the control port (port %0).
  619. */
  620. #define tb_switch_for_each_port(sw, p) \
  621. for ((p) = &(sw)->ports[1]; \
  622. (p) <= &(sw)->ports[(sw)->config.max_port_number]; (p)++)
  623. static inline struct tb_switch *tb_switch_get(struct tb_switch *sw)
  624. {
  625. if (sw)
  626. get_device(&sw->dev);
  627. return sw;
  628. }
  629. static inline void tb_switch_put(struct tb_switch *sw)
  630. {
  631. put_device(&sw->dev);
  632. }
  633. static inline bool tb_is_switch(const struct device *dev)
  634. {
  635. return dev->type == &tb_switch_type;
  636. }
  637. static inline struct tb_switch *tb_to_switch(struct device *dev)
  638. {
  639. if (tb_is_switch(dev))
  640. return container_of(dev, struct tb_switch, dev);
  641. return NULL;
  642. }
  643. static inline struct tb_switch *tb_switch_parent(struct tb_switch *sw)
  644. {
  645. return tb_to_switch(sw->dev.parent);
  646. }
  647. static inline bool tb_switch_is_light_ridge(const struct tb_switch *sw)
  648. {
  649. return sw->config.vendor_id == PCI_VENDOR_ID_INTEL &&
  650. sw->config.device_id == PCI_DEVICE_ID_INTEL_LIGHT_RIDGE;
  651. }
  652. static inline bool tb_switch_is_eagle_ridge(const struct tb_switch *sw)
  653. {
  654. return sw->config.vendor_id == PCI_VENDOR_ID_INTEL &&
  655. sw->config.device_id == PCI_DEVICE_ID_INTEL_EAGLE_RIDGE;
  656. }
  657. static inline bool tb_switch_is_cactus_ridge(const struct tb_switch *sw)
  658. {
  659. if (sw->config.vendor_id == PCI_VENDOR_ID_INTEL) {
  660. switch (sw->config.device_id) {
  661. case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_2C:
  662. case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C:
  663. return true;
  664. }
  665. }
  666. return false;
  667. }
  668. static inline bool tb_switch_is_falcon_ridge(const struct tb_switch *sw)
  669. {
  670. if (sw->config.vendor_id == PCI_VENDOR_ID_INTEL) {
  671. switch (sw->config.device_id) {
  672. case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_BRIDGE:
  673. case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_BRIDGE:
  674. return true;
  675. }
  676. }
  677. return false;
  678. }
  679. static inline bool tb_switch_is_alpine_ridge(const struct tb_switch *sw)
  680. {
  681. if (sw->config.vendor_id == PCI_VENDOR_ID_INTEL) {
  682. switch (sw->config.device_id) {
  683. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE:
  684. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE:
  685. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE:
  686. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE:
  687. return true;
  688. }
  689. }
  690. return false;
  691. }
  692. static inline bool tb_switch_is_titan_ridge(const struct tb_switch *sw)
  693. {
  694. if (sw->config.vendor_id == PCI_VENDOR_ID_INTEL) {
  695. switch (sw->config.device_id) {
  696. case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_BRIDGE:
  697. case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_BRIDGE:
  698. case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_DD_BRIDGE:
  699. return true;
  700. }
  701. }
  702. return false;
  703. }
  704. static inline bool tb_switch_is_ice_lake(const struct tb_switch *sw)
  705. {
  706. if (sw->config.vendor_id == PCI_VENDOR_ID_INTEL) {
  707. switch (sw->config.device_id) {
  708. case PCI_DEVICE_ID_INTEL_ICL_NHI0:
  709. case PCI_DEVICE_ID_INTEL_ICL_NHI1:
  710. return true;
  711. }
  712. }
  713. return false;
  714. }
  715. static inline bool tb_switch_is_tiger_lake(const struct tb_switch *sw)
  716. {
  717. if (sw->config.vendor_id == PCI_VENDOR_ID_INTEL) {
  718. switch (sw->config.device_id) {
  719. case PCI_DEVICE_ID_INTEL_TGL_NHI0:
  720. case PCI_DEVICE_ID_INTEL_TGL_NHI1:
  721. case PCI_DEVICE_ID_INTEL_TGL_H_NHI0:
  722. case PCI_DEVICE_ID_INTEL_TGL_H_NHI1:
  723. return true;
  724. }
  725. }
  726. return false;
  727. }
  728. /**
  729. * tb_switch_is_usb4() - Is the switch USB4 compliant
  730. * @sw: Switch to check
  731. *
  732. * Returns true if the @sw is USB4 compliant router, false otherwise.
  733. */
  734. static inline bool tb_switch_is_usb4(const struct tb_switch *sw)
  735. {
  736. return sw->config.thunderbolt_version == USB4_VERSION_1_0;
  737. }
  738. /**
  739. * tb_switch_is_icm() - Is the switch handled by ICM firmware
  740. * @sw: Switch to check
  741. *
  742. * In case there is a need to differentiate whether ICM firmware or SW CM
  743. * is handling @sw this function can be called. It is valid to call this
  744. * after tb_switch_alloc() and tb_switch_configure() has been called
  745. * (latter only for SW CM case).
  746. */
  747. static inline bool tb_switch_is_icm(const struct tb_switch *sw)
  748. {
  749. return !sw->config.enabled;
  750. }
  751. int tb_switch_lane_bonding_enable(struct tb_switch *sw);
  752. void tb_switch_lane_bonding_disable(struct tb_switch *sw);
  753. int tb_switch_configure_link(struct tb_switch *sw);
  754. void tb_switch_unconfigure_link(struct tb_switch *sw);
  755. bool tb_switch_query_dp_resource(struct tb_switch *sw, struct tb_port *in);
  756. int tb_switch_alloc_dp_resource(struct tb_switch *sw, struct tb_port *in);
  757. void tb_switch_dealloc_dp_resource(struct tb_switch *sw, struct tb_port *in);
  758. int tb_switch_tmu_init(struct tb_switch *sw);
  759. int tb_switch_tmu_post_time(struct tb_switch *sw);
  760. int tb_switch_tmu_disable(struct tb_switch *sw);
  761. int tb_switch_tmu_enable(struct tb_switch *sw);
  762. static inline bool tb_switch_tmu_is_enabled(const struct tb_switch *sw)
  763. {
  764. return sw->tmu.rate == TB_SWITCH_TMU_RATE_HIFI &&
  765. !sw->tmu.unidirectional;
  766. }
  767. int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged);
  768. int tb_port_add_nfc_credits(struct tb_port *port, int credits);
  769. int tb_port_set_initial_credits(struct tb_port *port, u32 credits);
  770. int tb_port_clear_counter(struct tb_port *port, int counter);
  771. int tb_port_unlock(struct tb_port *port);
  772. int tb_port_enable(struct tb_port *port);
  773. int tb_port_disable(struct tb_port *port);
  774. int tb_port_alloc_in_hopid(struct tb_port *port, int hopid, int max_hopid);
  775. void tb_port_release_in_hopid(struct tb_port *port, int hopid);
  776. int tb_port_alloc_out_hopid(struct tb_port *port, int hopid, int max_hopid);
  777. void tb_port_release_out_hopid(struct tb_port *port, int hopid);
  778. struct tb_port *tb_next_port_on_path(struct tb_port *start, struct tb_port *end,
  779. struct tb_port *prev);
  780. /**
  781. * tb_for_each_port_on_path() - Iterate over each port on path
  782. * @src: Source port
  783. * @dst: Destination port
  784. * @p: Port used as iterator
  785. *
  786. * Walks over each port on path from @src to @dst.
  787. */
  788. #define tb_for_each_port_on_path(src, dst, p) \
  789. for ((p) = tb_next_port_on_path((src), (dst), NULL); (p); \
  790. (p) = tb_next_port_on_path((src), (dst), (p)))
  791. int tb_port_get_link_speed(struct tb_port *port);
  792. int tb_switch_find_vse_cap(struct tb_switch *sw, enum tb_switch_vse_cap vsec);
  793. int tb_switch_find_cap(struct tb_switch *sw, enum tb_switch_cap cap);
  794. int tb_switch_next_cap(struct tb_switch *sw, unsigned int offset);
  795. int tb_port_find_cap(struct tb_port *port, enum tb_port_cap cap);
  796. int tb_port_next_cap(struct tb_port *port, unsigned int offset);
  797. bool tb_port_is_enabled(struct tb_port *port);
  798. bool tb_usb3_port_is_enabled(struct tb_port *port);
  799. int tb_usb3_port_enable(struct tb_port *port, bool enable);
  800. bool tb_pci_port_is_enabled(struct tb_port *port);
  801. int tb_pci_port_enable(struct tb_port *port, bool enable);
  802. int tb_dp_port_hpd_is_active(struct tb_port *port);
  803. int tb_dp_port_hpd_clear(struct tb_port *port);
  804. int tb_dp_port_set_hops(struct tb_port *port, unsigned int video,
  805. unsigned int aux_tx, unsigned int aux_rx);
  806. bool tb_dp_port_is_enabled(struct tb_port *port);
  807. int tb_dp_port_enable(struct tb_port *port, bool enable);
  808. struct tb_path *tb_path_discover(struct tb_port *src, int src_hopid,
  809. struct tb_port *dst, int dst_hopid,
  810. struct tb_port **last, const char *name);
  811. struct tb_path *tb_path_alloc(struct tb *tb, struct tb_port *src, int src_hopid,
  812. struct tb_port *dst, int dst_hopid, int link_nr,
  813. const char *name);
  814. void tb_path_free(struct tb_path *path);
  815. int tb_path_activate(struct tb_path *path);
  816. void tb_path_deactivate(struct tb_path *path);
  817. bool tb_path_is_invalid(struct tb_path *path);
  818. bool tb_path_port_on_path(const struct tb_path *path,
  819. const struct tb_port *port);
  820. int tb_drom_read(struct tb_switch *sw);
  821. int tb_drom_read_uid_only(struct tb_switch *sw, u64 *uid);
  822. int tb_lc_read_uuid(struct tb_switch *sw, u32 *uuid);
  823. int tb_lc_configure_port(struct tb_port *port);
  824. void tb_lc_unconfigure_port(struct tb_port *port);
  825. int tb_lc_configure_xdomain(struct tb_port *port);
  826. void tb_lc_unconfigure_xdomain(struct tb_port *port);
  827. int tb_lc_set_wake(struct tb_switch *sw, unsigned int flags);
  828. int tb_lc_set_sleep(struct tb_switch *sw);
  829. bool tb_lc_lane_bonding_possible(struct tb_switch *sw);
  830. bool tb_lc_dp_sink_query(struct tb_switch *sw, struct tb_port *in);
  831. int tb_lc_dp_sink_alloc(struct tb_switch *sw, struct tb_port *in);
  832. int tb_lc_dp_sink_dealloc(struct tb_switch *sw, struct tb_port *in);
  833. int tb_lc_force_power(struct tb_switch *sw);
  834. static inline int tb_route_length(u64 route)
  835. {
  836. return (fls64(route) + TB_ROUTE_SHIFT - 1) / TB_ROUTE_SHIFT;
  837. }
  838. /**
  839. * tb_downstream_route() - get route to downstream switch
  840. *
  841. * Port must not be the upstream port (otherwise a loop is created).
  842. *
  843. * Return: Returns a route to the switch behind @port.
  844. */
  845. static inline u64 tb_downstream_route(struct tb_port *port)
  846. {
  847. return tb_route(port->sw)
  848. | ((u64) port->port << (port->sw->config.depth * 8));
  849. }
  850. bool tb_xdomain_handle_request(struct tb *tb, enum tb_cfg_pkg_type type,
  851. const void *buf, size_t size);
  852. struct tb_xdomain *tb_xdomain_alloc(struct tb *tb, struct device *parent,
  853. u64 route, const uuid_t *local_uuid,
  854. const uuid_t *remote_uuid);
  855. void tb_xdomain_add(struct tb_xdomain *xd);
  856. void tb_xdomain_remove(struct tb_xdomain *xd);
  857. struct tb_xdomain *tb_xdomain_find_by_link_depth(struct tb *tb, u8 link,
  858. u8 depth);
  859. int tb_retimer_scan(struct tb_port *port);
  860. void tb_retimer_remove_all(struct tb_port *port);
  861. static inline bool tb_is_retimer(const struct device *dev)
  862. {
  863. return dev->type == &tb_retimer_type;
  864. }
  865. static inline struct tb_retimer *tb_to_retimer(struct device *dev)
  866. {
  867. if (tb_is_retimer(dev))
  868. return container_of(dev, struct tb_retimer, dev);
  869. return NULL;
  870. }
  871. int usb4_switch_setup(struct tb_switch *sw);
  872. int usb4_switch_read_uid(struct tb_switch *sw, u64 *uid);
  873. int usb4_switch_drom_read(struct tb_switch *sw, unsigned int address, void *buf,
  874. size_t size);
  875. bool usb4_switch_lane_bonding_possible(struct tb_switch *sw);
  876. int usb4_switch_set_wake(struct tb_switch *sw, unsigned int flags);
  877. int usb4_switch_set_sleep(struct tb_switch *sw);
  878. int usb4_switch_nvm_sector_size(struct tb_switch *sw);
  879. int usb4_switch_nvm_read(struct tb_switch *sw, unsigned int address, void *buf,
  880. size_t size);
  881. int usb4_switch_nvm_write(struct tb_switch *sw, unsigned int address,
  882. const void *buf, size_t size);
  883. int usb4_switch_nvm_authenticate(struct tb_switch *sw);
  884. bool usb4_switch_query_dp_resource(struct tb_switch *sw, struct tb_port *in);
  885. int usb4_switch_alloc_dp_resource(struct tb_switch *sw, struct tb_port *in);
  886. int usb4_switch_dealloc_dp_resource(struct tb_switch *sw, struct tb_port *in);
  887. struct tb_port *usb4_switch_map_pcie_down(struct tb_switch *sw,
  888. const struct tb_port *port);
  889. struct tb_port *usb4_switch_map_usb3_down(struct tb_switch *sw,
  890. const struct tb_port *port);
  891. int usb4_port_unlock(struct tb_port *port);
  892. int usb4_port_configure(struct tb_port *port);
  893. void usb4_port_unconfigure(struct tb_port *port);
  894. int usb4_port_configure_xdomain(struct tb_port *port);
  895. void usb4_port_unconfigure_xdomain(struct tb_port *port);
  896. int usb4_port_enumerate_retimers(struct tb_port *port);
  897. int usb4_port_retimer_read(struct tb_port *port, u8 index, u8 reg, void *buf,
  898. u8 size);
  899. int usb4_port_retimer_write(struct tb_port *port, u8 index, u8 reg,
  900. const void *buf, u8 size);
  901. int usb4_port_retimer_is_last(struct tb_port *port, u8 index);
  902. int usb4_port_retimer_nvm_sector_size(struct tb_port *port, u8 index);
  903. int usb4_port_retimer_nvm_write(struct tb_port *port, u8 index,
  904. unsigned int address, const void *buf,
  905. size_t size);
  906. int usb4_port_retimer_nvm_authenticate(struct tb_port *port, u8 index);
  907. int usb4_port_retimer_nvm_authenticate_status(struct tb_port *port, u8 index,
  908. u32 *status);
  909. int usb4_port_retimer_nvm_read(struct tb_port *port, u8 index,
  910. unsigned int address, void *buf, size_t size);
  911. int usb4_usb3_port_max_link_rate(struct tb_port *port);
  912. int usb4_usb3_port_actual_link_rate(struct tb_port *port);
  913. int usb4_usb3_port_allocated_bandwidth(struct tb_port *port, int *upstream_bw,
  914. int *downstream_bw);
  915. int usb4_usb3_port_allocate_bandwidth(struct tb_port *port, int *upstream_bw,
  916. int *downstream_bw);
  917. int usb4_usb3_port_release_bandwidth(struct tb_port *port, int *upstream_bw,
  918. int *downstream_bw);
  919. /* Keep link controller awake during update */
  920. #define QUIRK_FORCE_POWER_LINK_CONTROLLER BIT(0)
  921. void tb_check_quirks(struct tb_switch *sw);
  922. #ifdef CONFIG_ACPI
  923. void tb_acpi_add_links(struct tb_nhi *nhi);
  924. #else
  925. static inline void tb_acpi_add_links(struct tb_nhi *nhi) { }
  926. #endif
  927. #ifdef CONFIG_DEBUG_FS
  928. void tb_debugfs_init(void);
  929. void tb_debugfs_exit(void);
  930. void tb_switch_debugfs_init(struct tb_switch *sw);
  931. void tb_switch_debugfs_remove(struct tb_switch *sw);
  932. #else
  933. static inline void tb_debugfs_init(void) { }
  934. static inline void tb_debugfs_exit(void) { }
  935. static inline void tb_switch_debugfs_init(struct tb_switch *sw) { }
  936. static inline void tb_switch_debugfs_remove(struct tb_switch *sw) { }
  937. #endif
  938. #ifdef CONFIG_USB4_KUNIT_TEST
  939. int tb_test_init(void);
  940. void tb_test_exit(void);
  941. #else
  942. static inline int tb_test_init(void) { return 0; }
  943. static inline void tb_test_exit(void) { }
  944. #endif
  945. #endif