ctl.h 4.5 KB

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