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