tb.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. /**
  18. * struct tb_switch_nvm - Structure holding switch NVM information
  19. * @major: Major version number of the active NVM portion
  20. * @minor: Minor version number of the active NVM portion
  21. * @id: Identifier used with both NVM portions
  22. * @active: Active portion NVMem device
  23. * @non_active: Non-active portion NVMem device
  24. * @buf: Buffer where the NVM image is stored before it is written to
  25. * the actual NVM flash device
  26. * @buf_data_size: Number of bytes actually consumed by the new NVM
  27. * image
  28. * @authenticating: The switch is authenticating the new NVM
  29. */
  30. struct tb_switch_nvm {
  31. u8 major;
  32. u8 minor;
  33. int id;
  34. struct nvmem_device *active;
  35. struct nvmem_device *non_active;
  36. void *buf;
  37. size_t buf_data_size;
  38. bool authenticating;
  39. };
  40. #define TB_SWITCH_KEY_SIZE 32
  41. /**
  42. * struct tb_switch - a thunderbolt switch
  43. * @dev: Device for the switch
  44. * @config: Switch configuration
  45. * @ports: Ports in this switch
  46. * @dma_port: If the switch has port supporting DMA configuration based
  47. * mailbox this will hold the pointer to that (%NULL
  48. * otherwise). If set it also means the switch has
  49. * upgradeable NVM.
  50. * @tb: Pointer to the domain the switch belongs to
  51. * @uid: Unique ID of the switch
  52. * @uuid: UUID of the switch (or %NULL if not supported)
  53. * @vendor: Vendor ID of the switch
  54. * @device: Device ID of the switch
  55. * @vendor_name: Name of the vendor (or %NULL if not known)
  56. * @device_name: Name of the device (or %NULL if not known)
  57. * @generation: Switch Thunderbolt generation
  58. * @cap_plug_events: Offset to the plug events capability (%0 if not found)
  59. * @is_unplugged: The switch is going away
  60. * @drom: DROM of the switch (%NULL if not found)
  61. * @nvm: Pointer to the NVM if the switch has one (%NULL otherwise)
  62. * @no_nvm_upgrade: Prevent NVM upgrade of this switch
  63. * @safe_mode: The switch is in safe-mode
  64. * @boot: Whether the switch was already authorized on boot or not
  65. * @rpm: The switch supports runtime PM
  66. * @authorized: Whether the switch is authorized by user or policy
  67. * @work: Work used to automatically authorize a switch
  68. * @security_level: Switch supported security level
  69. * @key: Contains the key used to challenge the device or %NULL if not
  70. * supported. Size of the key is %TB_SWITCH_KEY_SIZE.
  71. * @connection_id: Connection ID used with ICM messaging
  72. * @connection_key: Connection key used with ICM messaging
  73. * @link: Root switch link this switch is connected (ICM only)
  74. * @depth: Depth in the chain this switch is connected (ICM only)
  75. *
  76. * When the switch is being added or removed to the domain (other
  77. * switches) you need to have domain lock held. For switch authorization
  78. * internal switch_lock is enough.
  79. */
  80. struct tb_switch {
  81. struct device dev;
  82. struct tb_regs_switch_header config;
  83. struct tb_port *ports;
  84. struct tb_dma_port *dma_port;
  85. struct tb *tb;
  86. u64 uid;
  87. uuid_t *uuid;
  88. u16 vendor;
  89. u16 device;
  90. const char *vendor_name;
  91. const char *device_name;
  92. unsigned int generation;
  93. int cap_plug_events;
  94. bool is_unplugged;
  95. u8 *drom;
  96. struct tb_switch_nvm *nvm;
  97. bool no_nvm_upgrade;
  98. bool safe_mode;
  99. bool boot;
  100. bool rpm;
  101. unsigned int authorized;
  102. struct work_struct work;
  103. enum tb_security_level security_level;
  104. u8 *key;
  105. u8 connection_id;
  106. u8 connection_key;
  107. u8 link;
  108. u8 depth;
  109. };
  110. /**
  111. * struct tb_port - a thunderbolt port, part of a tb_switch
  112. * @config: Cached port configuration read from registers
  113. * @sw: Switch the port belongs to
  114. * @remote: Remote port (%NULL if not connected)
  115. * @xdomain: Remote host (%NULL if not connected)
  116. * @cap_phy: Offset, zero if not found
  117. * @port: Port number on switch
  118. * @disabled: Disabled by eeprom
  119. * @dual_link_port: If the switch is connected using two ports, points
  120. * to the other port.
  121. * @link_nr: Is this primary or secondary port on the dual_link.
  122. */
  123. struct tb_port {
  124. struct tb_regs_port_header config;
  125. struct tb_switch *sw;
  126. struct tb_port *remote;
  127. struct tb_xdomain *xdomain;
  128. int cap_phy;
  129. u8 port;
  130. bool disabled;
  131. struct tb_port *dual_link_port;
  132. u8 link_nr:1;
  133. };
  134. /**
  135. * struct tb_path_hop - routing information for a tb_path
  136. *
  137. * Hop configuration is always done on the IN port of a switch.
  138. * in_port and out_port have to be on the same switch. Packets arriving on
  139. * in_port with "hop" = in_hop_index will get routed to through out_port. The
  140. * next hop to take (on out_port->remote) is determined by next_hop_index.
  141. *
  142. * in_counter_index is the index of a counter (in TB_CFG_COUNTERS) on the in
  143. * port.
  144. */
  145. struct tb_path_hop {
  146. struct tb_port *in_port;
  147. struct tb_port *out_port;
  148. int in_hop_index;
  149. int in_counter_index; /* write -1 to disable counters for this hop. */
  150. int next_hop_index;
  151. };
  152. /**
  153. * enum tb_path_port - path options mask
  154. */
  155. enum tb_path_port {
  156. TB_PATH_NONE = 0,
  157. TB_PATH_SOURCE = 1, /* activate on the first hop (out of src) */
  158. TB_PATH_INTERNAL = 2, /* activate on other hops (not the first/last) */
  159. TB_PATH_DESTINATION = 4, /* activate on the last hop (into dst) */
  160. TB_PATH_ALL = 7,
  161. };
  162. /**
  163. * struct tb_path - a unidirectional path between two ports
  164. *
  165. * A path consists of a number of hops (see tb_path_hop). To establish a PCIe
  166. * tunnel two paths have to be created between the two PCIe ports.
  167. *
  168. */
  169. struct tb_path {
  170. struct tb *tb;
  171. int nfc_credits; /* non flow controlled credits */
  172. enum tb_path_port ingress_shared_buffer;
  173. enum tb_path_port egress_shared_buffer;
  174. enum tb_path_port ingress_fc_enable;
  175. enum tb_path_port egress_fc_enable;
  176. int priority:3;
  177. int weight:4;
  178. bool drop_packages;
  179. bool activated;
  180. struct tb_path_hop *hops;
  181. int path_length; /* number of hops */
  182. };
  183. /**
  184. * struct tb_cm_ops - Connection manager specific operations vector
  185. * @driver_ready: Called right after control channel is started. Used by
  186. * ICM to send driver ready message to the firmware.
  187. * @start: Starts the domain
  188. * @stop: Stops the domain
  189. * @suspend_noirq: Connection manager specific suspend_noirq
  190. * @resume_noirq: Connection manager specific resume_noirq
  191. * @suspend: Connection manager specific suspend
  192. * @complete: Connection manager specific complete
  193. * @runtime_suspend: Connection manager specific runtime_suspend
  194. * @runtime_resume: Connection manager specific runtime_resume
  195. * @handle_event: Handle thunderbolt event
  196. * @get_boot_acl: Get boot ACL list
  197. * @set_boot_acl: Set boot ACL list
  198. * @approve_switch: Approve switch
  199. * @add_switch_key: Add key to switch
  200. * @challenge_switch_key: Challenge switch using key
  201. * @disconnect_pcie_paths: Disconnects PCIe paths before NVM update
  202. * @approve_xdomain_paths: Approve (establish) XDomain DMA paths
  203. * @disconnect_xdomain_paths: Disconnect XDomain DMA paths
  204. */
  205. struct tb_cm_ops {
  206. int (*driver_ready)(struct tb *tb);
  207. int (*start)(struct tb *tb);
  208. void (*stop)(struct tb *tb);
  209. int (*suspend_noirq)(struct tb *tb);
  210. int (*resume_noirq)(struct tb *tb);
  211. int (*suspend)(struct tb *tb);
  212. void (*complete)(struct tb *tb);
  213. int (*runtime_suspend)(struct tb *tb);
  214. int (*runtime_resume)(struct tb *tb);
  215. void (*handle_event)(struct tb *tb, enum tb_cfg_pkg_type,
  216. const void *buf, size_t size);
  217. int (*get_boot_acl)(struct tb *tb, uuid_t *uuids, size_t nuuids);
  218. int (*set_boot_acl)(struct tb *tb, const uuid_t *uuids, size_t nuuids);
  219. int (*approve_switch)(struct tb *tb, struct tb_switch *sw);
  220. int (*add_switch_key)(struct tb *tb, struct tb_switch *sw);
  221. int (*challenge_switch_key)(struct tb *tb, struct tb_switch *sw,
  222. const u8 *challenge, u8 *response);
  223. int (*disconnect_pcie_paths)(struct tb *tb);
  224. int (*approve_xdomain_paths)(struct tb *tb, struct tb_xdomain *xd);
  225. int (*disconnect_xdomain_paths)(struct tb *tb, struct tb_xdomain *xd);
  226. };
  227. static inline void *tb_priv(struct tb *tb)
  228. {
  229. return (void *)tb->privdata;
  230. }
  231. #define TB_AUTOSUSPEND_DELAY 15000 /* ms */
  232. /* helper functions & macros */
  233. /**
  234. * tb_upstream_port() - return the upstream port of a switch
  235. *
  236. * Every switch has an upstream port (for the root switch it is the NHI).
  237. *
  238. * During switch alloc/init tb_upstream_port()->remote may be NULL, even for
  239. * non root switches (on the NHI port remote is always NULL).
  240. *
  241. * Return: Returns the upstream port of the switch.
  242. */
  243. static inline struct tb_port *tb_upstream_port(struct tb_switch *sw)
  244. {
  245. return &sw->ports[sw->config.upstream_port_number];
  246. }
  247. static inline u64 tb_route(struct tb_switch *sw)
  248. {
  249. return ((u64) sw->config.route_hi) << 32 | sw->config.route_lo;
  250. }
  251. static inline struct tb_port *tb_port_at(u64 route, struct tb_switch *sw)
  252. {
  253. u8 port;
  254. port = route >> (sw->config.depth * 8);
  255. if (WARN_ON(port > sw->config.max_port_number))
  256. return NULL;
  257. return &sw->ports[port];
  258. }
  259. static inline int tb_sw_read(struct tb_switch *sw, void *buffer,
  260. enum tb_cfg_space space, u32 offset, u32 length)
  261. {
  262. return tb_cfg_read(sw->tb->ctl,
  263. buffer,
  264. tb_route(sw),
  265. 0,
  266. space,
  267. offset,
  268. length);
  269. }
  270. static inline int tb_sw_write(struct tb_switch *sw, void *buffer,
  271. enum tb_cfg_space space, u32 offset, u32 length)
  272. {
  273. return tb_cfg_write(sw->tb->ctl,
  274. buffer,
  275. tb_route(sw),
  276. 0,
  277. space,
  278. offset,
  279. length);
  280. }
  281. static inline int tb_port_read(struct tb_port *port, void *buffer,
  282. enum tb_cfg_space space, u32 offset, u32 length)
  283. {
  284. return tb_cfg_read(port->sw->tb->ctl,
  285. buffer,
  286. tb_route(port->sw),
  287. port->port,
  288. space,
  289. offset,
  290. length);
  291. }
  292. static inline int tb_port_write(struct tb_port *port, const void *buffer,
  293. enum tb_cfg_space space, u32 offset, u32 length)
  294. {
  295. return tb_cfg_write(port->sw->tb->ctl,
  296. buffer,
  297. tb_route(port->sw),
  298. port->port,
  299. space,
  300. offset,
  301. length);
  302. }
  303. #define tb_err(tb, fmt, arg...) dev_err(&(tb)->nhi->pdev->dev, fmt, ## arg)
  304. #define tb_WARN(tb, fmt, arg...) dev_WARN(&(tb)->nhi->pdev->dev, fmt, ## arg)
  305. #define tb_warn(tb, fmt, arg...) dev_warn(&(tb)->nhi->pdev->dev, fmt, ## arg)
  306. #define tb_info(tb, fmt, arg...) dev_info(&(tb)->nhi->pdev->dev, fmt, ## arg)
  307. #define tb_dbg(tb, fmt, arg...) dev_dbg(&(tb)->nhi->pdev->dev, fmt, ## arg)
  308. #define __TB_SW_PRINT(level, sw, fmt, arg...) \
  309. do { \
  310. struct tb_switch *__sw = (sw); \
  311. level(__sw->tb, "%llx: " fmt, \
  312. tb_route(__sw), ## arg); \
  313. } while (0)
  314. #define tb_sw_WARN(sw, fmt, arg...) __TB_SW_PRINT(tb_WARN, sw, fmt, ##arg)
  315. #define tb_sw_warn(sw, fmt, arg...) __TB_SW_PRINT(tb_warn, sw, fmt, ##arg)
  316. #define tb_sw_info(sw, fmt, arg...) __TB_SW_PRINT(tb_info, sw, fmt, ##arg)
  317. #define tb_sw_dbg(sw, fmt, arg...) __TB_SW_PRINT(tb_dbg, sw, fmt, ##arg)
  318. #define __TB_PORT_PRINT(level, _port, fmt, arg...) \
  319. do { \
  320. struct tb_port *__port = (_port); \
  321. level(__port->sw->tb, "%llx:%x: " fmt, \
  322. tb_route(__port->sw), __port->port, ## arg); \
  323. } while (0)
  324. #define tb_port_WARN(port, fmt, arg...) \
  325. __TB_PORT_PRINT(tb_WARN, port, fmt, ##arg)
  326. #define tb_port_warn(port, fmt, arg...) \
  327. __TB_PORT_PRINT(tb_warn, port, fmt, ##arg)
  328. #define tb_port_info(port, fmt, arg...) \
  329. __TB_PORT_PRINT(tb_info, port, fmt, ##arg)
  330. #define tb_port_dbg(port, fmt, arg...) \
  331. __TB_PORT_PRINT(tb_dbg, port, fmt, ##arg)
  332. struct tb *icm_probe(struct tb_nhi *nhi);
  333. struct tb *tb_probe(struct tb_nhi *nhi);
  334. extern struct device_type tb_domain_type;
  335. extern struct device_type tb_switch_type;
  336. int tb_domain_init(void);
  337. void tb_domain_exit(void);
  338. void tb_switch_exit(void);
  339. int tb_xdomain_init(void);
  340. void tb_xdomain_exit(void);
  341. struct tb *tb_domain_alloc(struct tb_nhi *nhi, size_t privsize);
  342. int tb_domain_add(struct tb *tb);
  343. void tb_domain_remove(struct tb *tb);
  344. int tb_domain_suspend_noirq(struct tb *tb);
  345. int tb_domain_resume_noirq(struct tb *tb);
  346. int tb_domain_suspend(struct tb *tb);
  347. void tb_domain_complete(struct tb *tb);
  348. int tb_domain_runtime_suspend(struct tb *tb);
  349. int tb_domain_runtime_resume(struct tb *tb);
  350. int tb_domain_approve_switch(struct tb *tb, struct tb_switch *sw);
  351. int tb_domain_approve_switch_key(struct tb *tb, struct tb_switch *sw);
  352. int tb_domain_challenge_switch_key(struct tb *tb, struct tb_switch *sw);
  353. int tb_domain_disconnect_pcie_paths(struct tb *tb);
  354. int tb_domain_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd);
  355. int tb_domain_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd);
  356. int tb_domain_disconnect_all_paths(struct tb *tb);
  357. static inline void tb_domain_put(struct tb *tb)
  358. {
  359. put_device(&tb->dev);
  360. }
  361. struct tb_switch *tb_switch_alloc(struct tb *tb, struct device *parent,
  362. u64 route);
  363. struct tb_switch *tb_switch_alloc_safe_mode(struct tb *tb,
  364. struct device *parent, u64 route);
  365. int tb_switch_configure(struct tb_switch *sw);
  366. int tb_switch_add(struct tb_switch *sw);
  367. void tb_switch_remove(struct tb_switch *sw);
  368. void tb_switch_suspend(struct tb_switch *sw);
  369. int tb_switch_resume(struct tb_switch *sw);
  370. int tb_switch_reset(struct tb *tb, u64 route);
  371. void tb_sw_set_unplugged(struct tb_switch *sw);
  372. struct tb_switch *get_switch_at_route(struct tb_switch *sw, u64 route);
  373. struct tb_switch *tb_switch_find_by_link_depth(struct tb *tb, u8 link,
  374. u8 depth);
  375. struct tb_switch *tb_switch_find_by_uuid(struct tb *tb, const uuid_t *uuid);
  376. struct tb_switch *tb_switch_find_by_route(struct tb *tb, u64 route);
  377. static inline struct tb_switch *tb_switch_get(struct tb_switch *sw)
  378. {
  379. if (sw)
  380. get_device(&sw->dev);
  381. return sw;
  382. }
  383. static inline void tb_switch_put(struct tb_switch *sw)
  384. {
  385. put_device(&sw->dev);
  386. }
  387. static inline bool tb_is_switch(const struct device *dev)
  388. {
  389. return dev->type == &tb_switch_type;
  390. }
  391. static inline struct tb_switch *tb_to_switch(struct device *dev)
  392. {
  393. if (tb_is_switch(dev))
  394. return container_of(dev, struct tb_switch, dev);
  395. return NULL;
  396. }
  397. int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged);
  398. int tb_port_add_nfc_credits(struct tb_port *port, int credits);
  399. int tb_port_clear_counter(struct tb_port *port, int counter);
  400. int tb_switch_find_vse_cap(struct tb_switch *sw, enum tb_switch_vse_cap vsec);
  401. int tb_port_find_cap(struct tb_port *port, enum tb_port_cap cap);
  402. struct tb_path *tb_path_alloc(struct tb *tb, int num_hops);
  403. void tb_path_free(struct tb_path *path);
  404. int tb_path_activate(struct tb_path *path);
  405. void tb_path_deactivate(struct tb_path *path);
  406. bool tb_path_is_invalid(struct tb_path *path);
  407. int tb_drom_read(struct tb_switch *sw);
  408. int tb_drom_read_uid_only(struct tb_switch *sw, u64 *uid);
  409. static inline int tb_route_length(u64 route)
  410. {
  411. return (fls64(route) + TB_ROUTE_SHIFT - 1) / TB_ROUTE_SHIFT;
  412. }
  413. static inline bool tb_is_upstream_port(struct tb_port *port)
  414. {
  415. return port == tb_upstream_port(port->sw);
  416. }
  417. /**
  418. * tb_downstream_route() - get route to downstream switch
  419. *
  420. * Port must not be the upstream port (otherwise a loop is created).
  421. *
  422. * Return: Returns a route to the switch behind @port.
  423. */
  424. static inline u64 tb_downstream_route(struct tb_port *port)
  425. {
  426. return tb_route(port->sw)
  427. | ((u64) port->port << (port->sw->config.depth * 8));
  428. }
  429. bool tb_xdomain_handle_request(struct tb *tb, enum tb_cfg_pkg_type type,
  430. const void *buf, size_t size);
  431. struct tb_xdomain *tb_xdomain_alloc(struct tb *tb, struct device *parent,
  432. u64 route, const uuid_t *local_uuid,
  433. const uuid_t *remote_uuid);
  434. void tb_xdomain_add(struct tb_xdomain *xd);
  435. void tb_xdomain_remove(struct tb_xdomain *xd);
  436. struct tb_xdomain *tb_xdomain_find_by_link_depth(struct tb *tb, u8 link,
  437. u8 depth);
  438. #endif