ctl.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Thunderbolt driver - control channel and configuration commands
  4. *
  5. * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
  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 index, int timeout_msec,
  19. event_cb cb, 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: Request is stored here
  48. * @request_size: Size of the request packet (in bytes)
  49. * @request_type: Type of the request packet
  50. * @response: Response is stored here
  51. * @response_size: Maximum size of one response packet
  52. * @response_type: Expected type of the response packet
  53. * @npackets: Number of packets expected to be returned with this request
  54. * @match: Function used to match the incoming packet
  55. * @copy: Function used to copy the incoming packet to @response
  56. * @callback: Callback called when the request is finished successfully
  57. * @callback_data: Data to be passed to @callback
  58. * @flags: Flags for the request
  59. * @work: Work item used to complete the request
  60. * @result: Result after the request has been completed
  61. * @list: Requests are queued using this field
  62. *
  63. * An arbitrary request over Thunderbolt control channel. For standard
  64. * control channel message, one should use tb_cfg_read/write() and
  65. * friends if possible.
  66. */
  67. struct tb_cfg_request {
  68. struct kref kref;
  69. struct tb_ctl *ctl;
  70. const void *request;
  71. size_t request_size;
  72. enum tb_cfg_pkg_type request_type;
  73. void *response;
  74. size_t response_size;
  75. enum tb_cfg_pkg_type response_type;
  76. size_t npackets;
  77. bool (*match)(const struct tb_cfg_request *req,
  78. const struct ctl_pkg *pkg);
  79. bool (*copy)(struct tb_cfg_request *req, const struct ctl_pkg *pkg);
  80. void (*callback)(void *callback_data);
  81. void *callback_data;
  82. unsigned long flags;
  83. struct work_struct work;
  84. struct tb_cfg_result result;
  85. struct list_head list;
  86. };
  87. #define TB_CFG_REQUEST_ACTIVE 0
  88. #define TB_CFG_REQUEST_CANCELED 1
  89. struct tb_cfg_request *tb_cfg_request_alloc(void);
  90. void tb_cfg_request_get(struct tb_cfg_request *req);
  91. void tb_cfg_request_put(struct tb_cfg_request *req);
  92. int tb_cfg_request(struct tb_ctl *ctl, struct tb_cfg_request *req,
  93. void (*callback)(void *), void *callback_data);
  94. void tb_cfg_request_cancel(struct tb_cfg_request *req, int err);
  95. struct tb_cfg_result tb_cfg_request_sync(struct tb_ctl *ctl,
  96. struct tb_cfg_request *req, int timeout_msec);
  97. static inline u64 tb_cfg_get_route(const struct tb_cfg_header *header)
  98. {
  99. return (u64) header->route_hi << 32 | header->route_lo;
  100. }
  101. static inline struct tb_cfg_header tb_cfg_make_header(u64 route)
  102. {
  103. struct tb_cfg_header header = {
  104. .route_hi = route >> 32,
  105. .route_lo = route,
  106. };
  107. /* check for overflow, route_hi is not 32 bits! */
  108. WARN_ON(tb_cfg_get_route(&header) != route);
  109. return header;
  110. }
  111. int tb_cfg_ack_notification(struct tb_ctl *ctl, u64 route,
  112. const struct cfg_error_pkg *error);
  113. int tb_cfg_ack_plug(struct tb_ctl *ctl, u64 route, u32 port, bool unplug);
  114. struct tb_cfg_result tb_cfg_reset(struct tb_ctl *ctl, u64 route);
  115. struct tb_cfg_result tb_cfg_read_raw(struct tb_ctl *ctl, void *buffer,
  116. u64 route, u32 port,
  117. enum tb_cfg_space space, u32 offset,
  118. u32 length, int timeout_msec);
  119. struct tb_cfg_result tb_cfg_write_raw(struct tb_ctl *ctl, const void *buffer,
  120. u64 route, u32 port,
  121. enum tb_cfg_space space, u32 offset,
  122. u32 length, int timeout_msec);
  123. int tb_cfg_read(struct tb_ctl *ctl, void *buffer, u64 route, u32 port,
  124. enum tb_cfg_space space, u32 offset, u32 length);
  125. int tb_cfg_write(struct tb_ctl *ctl, const void *buffer, u64 route, u32 port,
  126. enum tb_cfg_space space, u32 offset, u32 length);
  127. int tb_cfg_get_upstream_port(struct tb_ctl *ctl, u64 route);
  128. #endif