tb.h 14 KB

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