ctl.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Thunderbolt Cactus Ridge driver - control channel and configuration commands
  4. *
  5. * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
  6. */
  7. #ifndef _TB_CFG
  8. #define _TB_CFG
  9. #include <linux/kref.h>
  10. #include <linux/thunderbolt.h>
  11. #include "nhi.h"
  12. #include "tb_msgs.h"
  13. /* control channel */
  14. struct tb_ctl;
  15. typedef bool (*event_cb)(void *data, enum tb_cfg_pkg_type type,
  16. const void *buf, size_t size);
  17. struct tb_ctl *tb_ctl_alloc(struct tb_nhi *nhi, event_cb cb, void *cb_data);
  18. void tb_ctl_start(struct tb_ctl *ctl);
  19. void tb_ctl_stop(struct tb_ctl *ctl);
  20. void tb_ctl_free(struct tb_ctl *ctl);
  21. /* configuration commands */
  22. #define TB_CFG_DEFAULT_TIMEOUT 5000 /* msec */
  23. struct tb_cfg_result {
  24. u64 response_route;
  25. u32 response_port; /*
  26. * If err = 1 then this is the port that send the
  27. * error.
  28. * If err = 0 and if this was a cfg_read/write then
  29. * this is the the upstream port of the responding
  30. * switch.
  31. * Otherwise the field is set to zero.
  32. */
  33. int err; /* negative errors, 0 for success, 1 for tb errors */
  34. enum tb_cfg_error tb_error; /* valid if err == 1 */
  35. };
  36. struct ctl_pkg {
  37. struct tb_ctl *ctl;
  38. void *buffer;
  39. struct ring_frame frame;
  40. };
  41. /**
  42. * struct tb_cfg_request - Control channel request
  43. * @kref: Reference count
  44. * @ctl: Pointer to the control channel structure. Only set when the
  45. * request is queued.
  46. * @request_size: Size of the request packet (in bytes)
  47. * @request_type: Type of the request packet
  48. * @response: Response is stored here
  49. * @response_size: Maximum size of one response packet
  50. * @response_type: Expected type of the response packet
  51. * @npackets: Number of packets expected to be returned with this request
  52. * @match: Function used to match the incoming packet
  53. * @copy: Function used to copy the incoming packet to @response
  54. * @callback: Callback called when the request is finished successfully
  55. * @callback_data: Data to be passed to @callback
  56. * @flags: Flags for the request
  57. * @work: Work item used to complete the request
  58. * @result: Result after the request has been completed
  59. * @list: Requests are queued using this field
  60. *
  61. * An arbitrary request over Thunderbolt control channel. For standard
  62. * control channel message, one should use tb_cfg_read/write() and
  63. * friends if possible.
  64. */
  65. struct tb_cfg_request {
  66. struct kref kref;
  67. struct tb_ctl *ctl;
  68. const void *request;
  69. size_t request_size;
  70. enum tb_cfg_pkg_type request_type;
  71. void *response;
  72. size_t response_size;
  73. enum tb_cfg_pkg_type response_type;
  74. size_t npackets;
  75. bool (*match)(const struct tb_cfg_request *req,
  76. const struct ctl_pkg *pkg);
  77. bool (*copy)(struct tb_cfg_request *req, const struct ctl_pkg *pkg);
  78. void (*callback)(void *callback_data);
  79. void *callback_data;
  80. unsigned long flags;
  81. struct work_struct work;
  82. struct tb_cfg_result result;
  83. struct list_head list;
  84. };
  85. #define TB_CFG_REQUEST_ACTIVE 0
  86. #define TB_CFG_REQUEST_CANCELED 1
  87. struct tb_cfg_request *tb_cfg_request_alloc(void);
  88. void tb_cfg_request_get(struct tb_cfg_request *req);
  89. void tb_cfg_request_put(struct tb_cfg_request *req);
  90. int tb_cfg_request(struct tb_ctl *ctl, struct tb_cfg_request *req,
  91. void (*callback)(void *), void *callback_data);
  92. void tb_cfg_request_cancel(struct tb_cfg_request *req, int err);
  93. struct tb_cfg_result tb_cfg_request_sync(struct tb_ctl *ctl,
  94. struct tb_cfg_request *req, int timeout_msec);
  95. static inline u64 tb_cfg_get_route(const struct tb_cfg_header *header)
  96. {
  97. return (u64) header->route_hi << 32 | header->route_lo;
  98. }
  99. static inline struct tb_cfg_header tb_cfg_make_header(u64 route)
  100. {
  101. struct tb_cfg_header header = {
  102. .route_hi = route >> 32,
  103. .route_lo = route,
  104. };
  105. /* check for overflow, route_hi is not 32 bits! */
  106. WARN_ON(tb_cfg_get_route(&header) != route);
  107. return header;
  108. }
  109. int tb_cfg_error(struct tb_ctl *ctl, u64 route, u32 port,
  110. enum tb_cfg_error error);
  111. struct tb_cfg_result tb_cfg_reset(struct tb_ctl *ctl, u64 route,
  112. int timeout_msec);
  113. struct tb_cfg_result tb_cfg_read_raw(struct tb_ctl *ctl, void *buffer,
  114. u64 route, u32 port,
  115. enum tb_cfg_space space, u32 offset,
  116. u32 length, int timeout_msec);
  117. struct tb_cfg_result tb_cfg_write_raw(struct tb_ctl *ctl, const void *buffer,
  118. u64 route, u32 port,
  119. enum tb_cfg_space space, u32 offset,
  120. u32 length, int timeout_msec);
  121. int tb_cfg_read(struct tb_ctl *ctl, void *buffer, u64 route, u32 port,
  122. enum tb_cfg_space space, u32 offset, u32 length);
  123. int tb_cfg_write(struct tb_ctl *ctl, const void *buffer, u64 route, u32 port,
  124. enum tb_cfg_space space, u32 offset, u32 length);
  125. int tb_cfg_get_upstream_port(struct tb_ctl *ctl, u64 route);
  126. #endif