thunderbolt.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Thunderbolt service API
  4. *
  5. * Copyright (C) 2014 Andreas Noever <[email protected]>
  6. * Copyright (C) 2017, Intel Corporation
  7. * Authors: Michael Jamet <[email protected]>
  8. * Mika Westerberg <[email protected]>
  9. */
  10. #ifndef THUNDERBOLT_H_
  11. #define THUNDERBOLT_H_
  12. #include <linux/device.h>
  13. #include <linux/idr.h>
  14. #include <linux/list.h>
  15. #include <linux/mutex.h>
  16. #include <linux/mod_devicetable.h>
  17. #include <linux/pci.h>
  18. #include <linux/uuid.h>
  19. #include <linux/workqueue.h>
  20. enum tb_cfg_pkg_type {
  21. TB_CFG_PKG_READ = 1,
  22. TB_CFG_PKG_WRITE = 2,
  23. TB_CFG_PKG_ERROR = 3,
  24. TB_CFG_PKG_NOTIFY_ACK = 4,
  25. TB_CFG_PKG_EVENT = 5,
  26. TB_CFG_PKG_XDOMAIN_REQ = 6,
  27. TB_CFG_PKG_XDOMAIN_RESP = 7,
  28. TB_CFG_PKG_OVERRIDE = 8,
  29. TB_CFG_PKG_RESET = 9,
  30. TB_CFG_PKG_ICM_EVENT = 10,
  31. TB_CFG_PKG_ICM_CMD = 11,
  32. TB_CFG_PKG_ICM_RESP = 12,
  33. TB_CFG_PKG_PREPARE_TO_SLEEP = 13,
  34. };
  35. /**
  36. * enum tb_security_level - Thunderbolt security level
  37. * @TB_SECURITY_NONE: No security, legacy mode
  38. * @TB_SECURITY_USER: User approval required at minimum
  39. * @TB_SECURITY_SECURE: One time saved key required at minimum
  40. * @TB_SECURITY_DPONLY: Only tunnel Display port (and USB)
  41. * @TB_SECURITY_USBONLY: Only tunnel USB controller of the connected
  42. * Thunderbolt dock (and Display Port). All PCIe
  43. * links downstream of the dock are removed.
  44. * @TB_SECURITY_NOPCIE: For USB4 systems this level is used when the
  45. * PCIe tunneling is disabled from the BIOS.
  46. */
  47. enum tb_security_level {
  48. TB_SECURITY_NONE,
  49. TB_SECURITY_USER,
  50. TB_SECURITY_SECURE,
  51. TB_SECURITY_DPONLY,
  52. TB_SECURITY_USBONLY,
  53. TB_SECURITY_NOPCIE,
  54. };
  55. /**
  56. * struct tb - main thunderbolt bus structure
  57. * @dev: Domain device
  58. * @lock: Big lock. Must be held when accessing any struct
  59. * tb_switch / struct tb_port.
  60. * @nhi: Pointer to the NHI structure
  61. * @ctl: Control channel for this domain
  62. * @wq: Ordered workqueue for all domain specific work
  63. * @root_switch: Root switch of this domain
  64. * @cm_ops: Connection manager specific operations vector
  65. * @index: Linux assigned domain number
  66. * @security_level: Current security level
  67. * @nboot_acl: Number of boot ACLs the domain supports
  68. * @privdata: Private connection manager specific data
  69. */
  70. struct tb {
  71. struct device dev;
  72. struct mutex lock;
  73. struct tb_nhi *nhi;
  74. struct tb_ctl *ctl;
  75. struct workqueue_struct *wq;
  76. struct tb_switch *root_switch;
  77. const struct tb_cm_ops *cm_ops;
  78. int index;
  79. enum tb_security_level security_level;
  80. size_t nboot_acl;
  81. unsigned long privdata[];
  82. };
  83. extern struct bus_type tb_bus_type;
  84. extern struct device_type tb_service_type;
  85. extern struct device_type tb_xdomain_type;
  86. #define TB_LINKS_PER_PHY_PORT 2
  87. static inline unsigned int tb_phy_port_from_link(unsigned int link)
  88. {
  89. return (link - 1) / TB_LINKS_PER_PHY_PORT;
  90. }
  91. /**
  92. * struct tb_property_dir - XDomain property directory
  93. * @uuid: Directory UUID or %NULL if root directory
  94. * @properties: List of properties in this directory
  95. *
  96. * User needs to provide serialization if needed.
  97. */
  98. struct tb_property_dir {
  99. const uuid_t *uuid;
  100. struct list_head properties;
  101. };
  102. enum tb_property_type {
  103. TB_PROPERTY_TYPE_UNKNOWN = 0x00,
  104. TB_PROPERTY_TYPE_DIRECTORY = 0x44,
  105. TB_PROPERTY_TYPE_DATA = 0x64,
  106. TB_PROPERTY_TYPE_TEXT = 0x74,
  107. TB_PROPERTY_TYPE_VALUE = 0x76,
  108. };
  109. #define TB_PROPERTY_KEY_SIZE 8
  110. /**
  111. * struct tb_property - XDomain property
  112. * @list: Used to link properties together in a directory
  113. * @key: Key for the property (always terminated).
  114. * @type: Type of the property
  115. * @length: Length of the property data in dwords
  116. * @value: Property value
  117. *
  118. * Users use @type to determine which field in @value is filled.
  119. */
  120. struct tb_property {
  121. struct list_head list;
  122. char key[TB_PROPERTY_KEY_SIZE + 1];
  123. enum tb_property_type type;
  124. size_t length;
  125. union {
  126. struct tb_property_dir *dir;
  127. u8 *data;
  128. char *text;
  129. u32 immediate;
  130. } value;
  131. };
  132. struct tb_property_dir *tb_property_parse_dir(const u32 *block,
  133. size_t block_len);
  134. ssize_t tb_property_format_dir(const struct tb_property_dir *dir, u32 *block,
  135. size_t block_len);
  136. struct tb_property_dir *tb_property_copy_dir(const struct tb_property_dir *dir);
  137. struct tb_property_dir *tb_property_create_dir(const uuid_t *uuid);
  138. void tb_property_free_dir(struct tb_property_dir *dir);
  139. int tb_property_add_immediate(struct tb_property_dir *parent, const char *key,
  140. u32 value);
  141. int tb_property_add_data(struct tb_property_dir *parent, const char *key,
  142. const void *buf, size_t buflen);
  143. int tb_property_add_text(struct tb_property_dir *parent, const char *key,
  144. const char *text);
  145. int tb_property_add_dir(struct tb_property_dir *parent, const char *key,
  146. struct tb_property_dir *dir);
  147. void tb_property_remove(struct tb_property *tb_property);
  148. struct tb_property *tb_property_find(struct tb_property_dir *dir,
  149. const char *key, enum tb_property_type type);
  150. struct tb_property *tb_property_get_next(struct tb_property_dir *dir,
  151. struct tb_property *prev);
  152. #define tb_property_for_each(dir, property) \
  153. for (property = tb_property_get_next(dir, NULL); \
  154. property; \
  155. property = tb_property_get_next(dir, property))
  156. int tb_register_property_dir(const char *key, struct tb_property_dir *dir);
  157. void tb_unregister_property_dir(const char *key, struct tb_property_dir *dir);
  158. /**
  159. * struct tb_xdomain - Cross-domain (XDomain) connection
  160. * @dev: XDomain device
  161. * @tb: Pointer to the domain
  162. * @remote_uuid: UUID of the remote domain (host)
  163. * @local_uuid: Cached local UUID
  164. * @route: Route string the other domain can be reached
  165. * @vendor: Vendor ID of the remote domain
  166. * @device: Device ID of the demote domain
  167. * @local_max_hopid: Maximum input HopID of this host
  168. * @remote_max_hopid: Maximum input HopID of the remote host
  169. * @lock: Lock to serialize access to the following fields of this structure
  170. * @vendor_name: Name of the vendor (or %NULL if not known)
  171. * @device_name: Name of the device (or %NULL if not known)
  172. * @link_speed: Speed of the link in Gb/s
  173. * @link_width: Width of the link (1 or 2)
  174. * @link_usb4: Downstream link is USB4
  175. * @is_unplugged: The XDomain is unplugged
  176. * @needs_uuid: If the XDomain does not have @remote_uuid it will be
  177. * queried first
  178. * @service_ids: Used to generate IDs for the services
  179. * @in_hopids: Input HopIDs for DMA tunneling
  180. * @out_hopids; Output HopIDs for DMA tunneling
  181. * @local_property_block: Local block of properties
  182. * @local_property_block_gen: Generation of @local_property_block
  183. * @local_property_block_len: Length of the @local_property_block in dwords
  184. * @remote_properties: Properties exported by the remote domain
  185. * @remote_property_block_gen: Generation of @remote_properties
  186. * @state: Next XDomain discovery state to run
  187. * @state_work: Work used to run the next state
  188. * @state_retries: Number of retries remain for the state
  189. * @properties_changed_work: Work used to notify the remote domain that
  190. * our properties have changed
  191. * @properties_changed_retries: Number of times left to send properties
  192. * changed notification
  193. * @bonding_possible: True if lane bonding is possible on local side
  194. * @target_link_width: Target link width from the remote host
  195. * @link: Root switch link the remote domain is connected (ICM only)
  196. * @depth: Depth in the chain the remote domain is connected (ICM only)
  197. *
  198. * This structure represents connection across two domains (hosts).
  199. * Each XDomain contains zero or more services which are exposed as
  200. * &struct tb_service objects.
  201. *
  202. * Service drivers may access this structure if they need to enumerate
  203. * non-standard properties but they need hold @lock when doing so
  204. * because properties can be changed asynchronously in response to
  205. * changes in the remote domain.
  206. */
  207. struct tb_xdomain {
  208. struct device dev;
  209. struct tb *tb;
  210. uuid_t *remote_uuid;
  211. const uuid_t *local_uuid;
  212. u64 route;
  213. u16 vendor;
  214. u16 device;
  215. unsigned int local_max_hopid;
  216. unsigned int remote_max_hopid;
  217. struct mutex lock;
  218. const char *vendor_name;
  219. const char *device_name;
  220. unsigned int link_speed;
  221. unsigned int link_width;
  222. bool link_usb4;
  223. bool is_unplugged;
  224. bool needs_uuid;
  225. struct ida service_ids;
  226. struct ida in_hopids;
  227. struct ida out_hopids;
  228. u32 *local_property_block;
  229. u32 local_property_block_gen;
  230. u32 local_property_block_len;
  231. struct tb_property_dir *remote_properties;
  232. u32 remote_property_block_gen;
  233. int state;
  234. struct delayed_work state_work;
  235. int state_retries;
  236. struct delayed_work properties_changed_work;
  237. int properties_changed_retries;
  238. bool bonding_possible;
  239. u8 target_link_width;
  240. u8 link;
  241. u8 depth;
  242. };
  243. int tb_xdomain_lane_bonding_enable(struct tb_xdomain *xd);
  244. void tb_xdomain_lane_bonding_disable(struct tb_xdomain *xd);
  245. int tb_xdomain_alloc_in_hopid(struct tb_xdomain *xd, int hopid);
  246. void tb_xdomain_release_in_hopid(struct tb_xdomain *xd, int hopid);
  247. int tb_xdomain_alloc_out_hopid(struct tb_xdomain *xd, int hopid);
  248. void tb_xdomain_release_out_hopid(struct tb_xdomain *xd, int hopid);
  249. int tb_xdomain_enable_paths(struct tb_xdomain *xd, int transmit_path,
  250. int transmit_ring, int receive_path,
  251. int receive_ring);
  252. int tb_xdomain_disable_paths(struct tb_xdomain *xd, int transmit_path,
  253. int transmit_ring, int receive_path,
  254. int receive_ring);
  255. static inline int tb_xdomain_disable_all_paths(struct tb_xdomain *xd)
  256. {
  257. return tb_xdomain_disable_paths(xd, -1, -1, -1, -1);
  258. }
  259. struct tb_xdomain *tb_xdomain_find_by_uuid(struct tb *tb, const uuid_t *uuid);
  260. struct tb_xdomain *tb_xdomain_find_by_route(struct tb *tb, u64 route);
  261. static inline struct tb_xdomain *
  262. tb_xdomain_find_by_uuid_locked(struct tb *tb, const uuid_t *uuid)
  263. {
  264. struct tb_xdomain *xd;
  265. mutex_lock(&tb->lock);
  266. xd = tb_xdomain_find_by_uuid(tb, uuid);
  267. mutex_unlock(&tb->lock);
  268. return xd;
  269. }
  270. static inline struct tb_xdomain *
  271. tb_xdomain_find_by_route_locked(struct tb *tb, u64 route)
  272. {
  273. struct tb_xdomain *xd;
  274. mutex_lock(&tb->lock);
  275. xd = tb_xdomain_find_by_route(tb, route);
  276. mutex_unlock(&tb->lock);
  277. return xd;
  278. }
  279. static inline struct tb_xdomain *tb_xdomain_get(struct tb_xdomain *xd)
  280. {
  281. if (xd)
  282. get_device(&xd->dev);
  283. return xd;
  284. }
  285. static inline void tb_xdomain_put(struct tb_xdomain *xd)
  286. {
  287. if (xd)
  288. put_device(&xd->dev);
  289. }
  290. static inline bool tb_is_xdomain(const struct device *dev)
  291. {
  292. return dev->type == &tb_xdomain_type;
  293. }
  294. static inline struct tb_xdomain *tb_to_xdomain(struct device *dev)
  295. {
  296. if (tb_is_xdomain(dev))
  297. return container_of(dev, struct tb_xdomain, dev);
  298. return NULL;
  299. }
  300. int tb_xdomain_response(struct tb_xdomain *xd, const void *response,
  301. size_t size, enum tb_cfg_pkg_type type);
  302. int tb_xdomain_request(struct tb_xdomain *xd, const void *request,
  303. size_t request_size, enum tb_cfg_pkg_type request_type,
  304. void *response, size_t response_size,
  305. enum tb_cfg_pkg_type response_type,
  306. unsigned int timeout_msec);
  307. /**
  308. * tb_protocol_handler - Protocol specific handler
  309. * @uuid: XDomain messages with this UUID are dispatched to this handler
  310. * @callback: Callback called with the XDomain message. Returning %1
  311. * here tells the XDomain core that the message was handled
  312. * by this handler and should not be forwared to other
  313. * handlers.
  314. * @data: Data passed with the callback
  315. * @list: Handlers are linked using this
  316. *
  317. * Thunderbolt services can hook into incoming XDomain requests by
  318. * registering protocol handler. Only limitation is that the XDomain
  319. * discovery protocol UUID cannot be registered since it is handled by
  320. * the core XDomain code.
  321. *
  322. * The @callback must check that the message is really directed to the
  323. * service the driver implements.
  324. */
  325. struct tb_protocol_handler {
  326. const uuid_t *uuid;
  327. int (*callback)(const void *buf, size_t size, void *data);
  328. void *data;
  329. struct list_head list;
  330. };
  331. int tb_register_protocol_handler(struct tb_protocol_handler *handler);
  332. void tb_unregister_protocol_handler(struct tb_protocol_handler *handler);
  333. /**
  334. * struct tb_service - Thunderbolt service
  335. * @dev: XDomain device
  336. * @id: ID of the service (shown in sysfs)
  337. * @key: Protocol key from the properties directory
  338. * @prtcid: Protocol ID from the properties directory
  339. * @prtcvers: Protocol version from the properties directory
  340. * @prtcrevs: Protocol software revision from the properties directory
  341. * @prtcstns: Protocol settings mask from the properties directory
  342. * @debugfs_dir: Pointer to the service debugfs directory. Always created
  343. * when debugfs is enabled. Can be used by service drivers to
  344. * add their own entries under the service.
  345. *
  346. * Each domain exposes set of services it supports as collection of
  347. * properties. For each service there will be one corresponding
  348. * &struct tb_service. Service drivers are bound to these.
  349. */
  350. struct tb_service {
  351. struct device dev;
  352. int id;
  353. const char *key;
  354. u32 prtcid;
  355. u32 prtcvers;
  356. u32 prtcrevs;
  357. u32 prtcstns;
  358. struct dentry *debugfs_dir;
  359. };
  360. static inline struct tb_service *tb_service_get(struct tb_service *svc)
  361. {
  362. if (svc)
  363. get_device(&svc->dev);
  364. return svc;
  365. }
  366. static inline void tb_service_put(struct tb_service *svc)
  367. {
  368. if (svc)
  369. put_device(&svc->dev);
  370. }
  371. static inline bool tb_is_service(const struct device *dev)
  372. {
  373. return dev->type == &tb_service_type;
  374. }
  375. static inline struct tb_service *tb_to_service(struct device *dev)
  376. {
  377. if (tb_is_service(dev))
  378. return container_of(dev, struct tb_service, dev);
  379. return NULL;
  380. }
  381. /**
  382. * tb_service_driver - Thunderbolt service driver
  383. * @driver: Driver structure
  384. * @probe: Called when the driver is probed
  385. * @remove: Called when the driver is removed (optional)
  386. * @shutdown: Called at shutdown time to stop the service (optional)
  387. * @id_table: Table of service identifiers the driver supports
  388. */
  389. struct tb_service_driver {
  390. struct device_driver driver;
  391. int (*probe)(struct tb_service *svc, const struct tb_service_id *id);
  392. void (*remove)(struct tb_service *svc);
  393. void (*shutdown)(struct tb_service *svc);
  394. const struct tb_service_id *id_table;
  395. };
  396. #define TB_SERVICE(key, id) \
  397. .match_flags = TBSVC_MATCH_PROTOCOL_KEY | \
  398. TBSVC_MATCH_PROTOCOL_ID, \
  399. .protocol_key = (key), \
  400. .protocol_id = (id)
  401. int tb_register_service_driver(struct tb_service_driver *drv);
  402. void tb_unregister_service_driver(struct tb_service_driver *drv);
  403. static inline void *tb_service_get_drvdata(const struct tb_service *svc)
  404. {
  405. return dev_get_drvdata(&svc->dev);
  406. }
  407. static inline void tb_service_set_drvdata(struct tb_service *svc, void *data)
  408. {
  409. dev_set_drvdata(&svc->dev, data);
  410. }
  411. static inline struct tb_xdomain *tb_service_parent(struct tb_service *svc)
  412. {
  413. return tb_to_xdomain(svc->dev.parent);
  414. }
  415. /**
  416. * struct tb_nhi - thunderbolt native host interface
  417. * @lock: Must be held during ring creation/destruction. Is acquired by
  418. * interrupt_work when dispatching interrupts to individual rings.
  419. * @pdev: Pointer to the PCI device
  420. * @ops: NHI specific optional ops
  421. * @iobase: MMIO space of the NHI
  422. * @tx_rings: All Tx rings available on this host controller
  423. * @rx_rings: All Rx rings available on this host controller
  424. * @msix_ida: Used to allocate MSI-X vectors for rings
  425. * @going_away: The host controller device is about to disappear so when
  426. * this flag is set, avoid touching the hardware anymore.
  427. * @iommu_dma_protection: An IOMMU will isolate external-facing ports.
  428. * @interrupt_work: Work scheduled to handle ring interrupt when no
  429. * MSI-X is used.
  430. * @hop_count: Number of rings (end point hops) supported by NHI.
  431. * @quirks: NHI specific quirks if any
  432. */
  433. struct tb_nhi {
  434. spinlock_t lock;
  435. struct pci_dev *pdev;
  436. const struct tb_nhi_ops *ops;
  437. void __iomem *iobase;
  438. struct tb_ring **tx_rings;
  439. struct tb_ring **rx_rings;
  440. struct ida msix_ida;
  441. bool going_away;
  442. bool iommu_dma_protection;
  443. struct work_struct interrupt_work;
  444. u32 hop_count;
  445. unsigned long quirks;
  446. };
  447. /**
  448. * struct tb_ring - thunderbolt TX or RX ring associated with a NHI
  449. * @lock: Lock serializing actions to this ring. Must be acquired after
  450. * nhi->lock.
  451. * @nhi: Pointer to the native host controller interface
  452. * @size: Size of the ring
  453. * @hop: Hop (DMA channel) associated with this ring
  454. * @head: Head of the ring (write next descriptor here)
  455. * @tail: Tail of the ring (complete next descriptor here)
  456. * @descriptors: Allocated descriptors for this ring
  457. * @queue: Queue holding frames to be transferred over this ring
  458. * @in_flight: Queue holding frames that are currently in flight
  459. * @work: Interrupt work structure
  460. * @is_tx: Is the ring Tx or Rx
  461. * @running: Is the ring running
  462. * @irq: MSI-X irq number if the ring uses MSI-X. %0 otherwise.
  463. * @vector: MSI-X vector number the ring uses (only set if @irq is > 0)
  464. * @flags: Ring specific flags
  465. * @e2e_tx_hop: Transmit HopID when E2E is enabled. Only applicable to
  466. * RX ring. For TX ring this should be set to %0.
  467. * @sof_mask: Bit mask used to detect start of frame PDF
  468. * @eof_mask: Bit mask used to detect end of frame PDF
  469. * @start_poll: Called when ring interrupt is triggered to start
  470. * polling. Passing %NULL keeps the ring in interrupt mode.
  471. * @poll_data: Data passed to @start_poll
  472. */
  473. struct tb_ring {
  474. spinlock_t lock;
  475. struct tb_nhi *nhi;
  476. int size;
  477. int hop;
  478. int head;
  479. int tail;
  480. struct ring_desc *descriptors;
  481. dma_addr_t descriptors_dma;
  482. struct list_head queue;
  483. struct list_head in_flight;
  484. struct work_struct work;
  485. bool is_tx:1;
  486. bool running:1;
  487. int irq;
  488. u8 vector;
  489. unsigned int flags;
  490. int e2e_tx_hop;
  491. u16 sof_mask;
  492. u16 eof_mask;
  493. void (*start_poll)(void *data);
  494. void *poll_data;
  495. };
  496. /* Leave ring interrupt enabled on suspend */
  497. #define RING_FLAG_NO_SUSPEND BIT(0)
  498. /* Configure the ring to be in frame mode */
  499. #define RING_FLAG_FRAME BIT(1)
  500. /* Enable end-to-end flow control */
  501. #define RING_FLAG_E2E BIT(2)
  502. struct ring_frame;
  503. typedef void (*ring_cb)(struct tb_ring *, struct ring_frame *, bool canceled);
  504. /**
  505. * enum ring_desc_flags - Flags for DMA ring descriptor
  506. * %RING_DESC_ISOCH: Enable isonchronous DMA (Tx only)
  507. * %RING_DESC_CRC_ERROR: In frame mode CRC check failed for the frame (Rx only)
  508. * %RING_DESC_COMPLETED: Descriptor completed (set by NHI)
  509. * %RING_DESC_POSTED: Always set this
  510. * %RING_DESC_BUFFER_OVERRUN: RX buffer overrun
  511. * %RING_DESC_INTERRUPT: Request an interrupt on completion
  512. */
  513. enum ring_desc_flags {
  514. RING_DESC_ISOCH = 0x1,
  515. RING_DESC_CRC_ERROR = 0x1,
  516. RING_DESC_COMPLETED = 0x2,
  517. RING_DESC_POSTED = 0x4,
  518. RING_DESC_BUFFER_OVERRUN = 0x04,
  519. RING_DESC_INTERRUPT = 0x8,
  520. };
  521. /**
  522. * struct ring_frame - For use with ring_rx/ring_tx
  523. * @buffer_phy: DMA mapped address of the frame
  524. * @callback: Callback called when the frame is finished (optional)
  525. * @list: Frame is linked to a queue using this
  526. * @size: Size of the frame in bytes (%0 means %4096)
  527. * @flags: Flags for the frame (see &enum ring_desc_flags)
  528. * @eof: End of frame protocol defined field
  529. * @sof: Start of frame protocol defined field
  530. */
  531. struct ring_frame {
  532. dma_addr_t buffer_phy;
  533. ring_cb callback;
  534. struct list_head list;
  535. u32 size:12;
  536. u32 flags:12;
  537. u32 eof:4;
  538. u32 sof:4;
  539. };
  540. /* Minimum size for ring_rx */
  541. #define TB_FRAME_SIZE 0x100
  542. struct tb_ring *tb_ring_alloc_tx(struct tb_nhi *nhi, int hop, int size,
  543. unsigned int flags);
  544. struct tb_ring *tb_ring_alloc_rx(struct tb_nhi *nhi, int hop, int size,
  545. unsigned int flags, int e2e_tx_hop,
  546. u16 sof_mask, u16 eof_mask,
  547. void (*start_poll)(void *), void *poll_data);
  548. void tb_ring_start(struct tb_ring *ring);
  549. void tb_ring_stop(struct tb_ring *ring);
  550. void tb_ring_free(struct tb_ring *ring);
  551. int __tb_ring_enqueue(struct tb_ring *ring, struct ring_frame *frame);
  552. /**
  553. * tb_ring_rx() - enqueue a frame on an RX ring
  554. * @ring: Ring to enqueue the frame
  555. * @frame: Frame to enqueue
  556. *
  557. * @frame->buffer, @frame->buffer_phy have to be set. The buffer must
  558. * contain at least %TB_FRAME_SIZE bytes.
  559. *
  560. * @frame->callback will be invoked with @frame->size, @frame->flags,
  561. * @frame->eof, @frame->sof set once the frame has been received.
  562. *
  563. * If ring_stop() is called after the packet has been enqueued
  564. * @frame->callback will be called with canceled set to true.
  565. *
  566. * Return: Returns %-ESHUTDOWN if ring_stop has been called. Zero otherwise.
  567. */
  568. static inline int tb_ring_rx(struct tb_ring *ring, struct ring_frame *frame)
  569. {
  570. WARN_ON(ring->is_tx);
  571. return __tb_ring_enqueue(ring, frame);
  572. }
  573. /**
  574. * tb_ring_tx() - enqueue a frame on an TX ring
  575. * @ring: Ring the enqueue the frame
  576. * @frame: Frame to enqueue
  577. *
  578. * @frame->buffer, @frame->buffer_phy, @frame->size, @frame->eof and
  579. * @frame->sof have to be set.
  580. *
  581. * @frame->callback will be invoked with once the frame has been transmitted.
  582. *
  583. * If ring_stop() is called after the packet has been enqueued @frame->callback
  584. * will be called with canceled set to true.
  585. *
  586. * Return: Returns %-ESHUTDOWN if ring_stop has been called. Zero otherwise.
  587. */
  588. static inline int tb_ring_tx(struct tb_ring *ring, struct ring_frame *frame)
  589. {
  590. WARN_ON(!ring->is_tx);
  591. return __tb_ring_enqueue(ring, frame);
  592. }
  593. /* Used only when the ring is in polling mode */
  594. struct ring_frame *tb_ring_poll(struct tb_ring *ring);
  595. void tb_ring_poll_complete(struct tb_ring *ring);
  596. /**
  597. * tb_ring_dma_device() - Return device used for DMA mapping
  598. * @ring: Ring whose DMA device is retrieved
  599. *
  600. * Use this function when you are mapping DMA for buffers that are
  601. * passed to the ring for sending/receiving.
  602. */
  603. static inline struct device *tb_ring_dma_device(struct tb_ring *ring)
  604. {
  605. return &ring->nhi->pdev->dev;
  606. }
  607. #endif /* THUNDERBOLT_H_ */