tb.h 15 KB

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