ctl.h 4.6 KB

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