htc_packet.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * Copyright (c) 2013-2014, 2016-2017, 2019-2021 The Linux Foundation. All rights reserved.
  3. * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for
  6. * any purpose with or without fee is hereby granted, provided that the
  7. * above copyright notice and this permission notice appear in all
  8. * copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  11. * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  12. * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  13. * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  14. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  15. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  16. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  17. * PERFORMANCE OF THIS SOFTWARE.
  18. */
  19. #ifndef HTC_PACKET_H_
  20. #define HTC_PACKET_H_
  21. #include <osdep.h>
  22. #include "dl_list.h"
  23. /* ------ Endpoint IDS ------ */
  24. typedef enum {
  25. ENDPOINT_UNUSED = -1,
  26. ENDPOINT_0 = 0,
  27. ENDPOINT_1 = 1,
  28. ENDPOINT_2 = 2,
  29. ENDPOINT_3,
  30. ENDPOINT_4,
  31. ENDPOINT_5,
  32. ENDPOINT_6,
  33. ENDPOINT_7,
  34. ENDPOINT_8,
  35. ENDPOINT_MAX,
  36. } HTC_ENDPOINT_ID;
  37. struct _HTC_PACKET;
  38. typedef void (*HTC_PACKET_COMPLETION)(void *, struct _HTC_PACKET *);
  39. typedef uint16_t HTC_TX_TAG;
  40. /**
  41. * struct htc_tx_packet_info - HTC TX packet information
  42. * @Tag: tag used to selective flush packets
  43. * @CreditsUsed: number of credits used for this TX packet (HTC internal)
  44. * @SendFlags: send flags (HTC internal)
  45. * @SeqNo: internal seq no for debugging (HTC internal)
  46. * @Flags: Internal use
  47. */
  48. struct htc_tx_packet_info {
  49. HTC_TX_TAG Tag;
  50. int CreditsUsed;
  51. uint8_t SendFlags;
  52. int SeqNo;
  53. uint32_t Flags;
  54. };
  55. /*
  56. * HTC_TX_PACKET_TAG_XXX - #defines for tagging packets for special handling
  57. * HTC_TX_PACKET_TAG_ALL: zero is reserved and used to flush ALL packets
  58. * HTC_TX_PACKET_TAG_INTERNAL: internal tags start here
  59. * HTC_TX_PACKET_TAG_USER_DEFINED: user-defined tags start here
  60. * HTC_TX_PACKET_TAG_BUNDLED: indicate this is a bundled tx packet
  61. * HTC_TX_PACKET_TAG_AUTO_PM: indicate a power management wmi command
  62. */
  63. #define HTC_TX_PACKET_TAG_ALL 0
  64. #define HTC_TX_PACKET_TAG_INTERNAL 1
  65. #define HTC_TX_PACKET_TAG_USER_DEFINED (HTC_TX_PACKET_TAG_INTERNAL + 9)
  66. #define HTC_TX_PACKET_TAG_BUNDLED (HTC_TX_PACKET_TAG_USER_DEFINED + 1)
  67. #define HTC_TX_PACKET_TAG_AUTO_PM (HTC_TX_PACKET_TAG_USER_DEFINED + 2)
  68. /* Tag packet for runtime put after sending */
  69. #define HTC_TX_PACKET_TAG_RUNTIME_PUT (HTC_TX_PACKET_TAG_USER_DEFINED + 3)
  70. /*Tag packet for runtime put in response or cleanup */
  71. #define HTC_TX_PACKET_TAG_RTPM_PUT_RC (HTC_TX_PACKET_TAG_USER_DEFINED + 4)
  72. #define HTC_TX_PACKET_SYSTEM_SUSPEND (HTC_TX_PACKET_TAG_USER_DEFINED + 5)
  73. #define HTC_TX_PACKET_SYSTEM_RESUME (HTC_TX_PACKET_TAG_USER_DEFINED + 6)
  74. #define HTC_TX_PACKET_FLAG_FIXUP_NETBUF (1 << 0)
  75. #define HTC_TX_PACKET_FLAG_HTC_HEADER_IN_NETBUF_DATA (1 << 1)
  76. /**
  77. * struct htc_rx_packet_info - HTC RX Packet information
  78. * @ExpectedHdr: HTC Internal use
  79. * @HTCRxFlags: HTC Internal use
  80. * @IndicationFlags: indication flags set on each RX packet indication
  81. */
  82. struct htc_rx_packet_info {
  83. uint32_t ExpectedHdr;
  84. uint32_t HTCRxFlags;
  85. uint32_t IndicationFlags;
  86. };
  87. /* more packets on this endpoint are being fetched */
  88. #define HTC_RX_FLAGS_INDICATE_MORE_PKTS (1 << 0)
  89. #define HTC_PACKET_MAGIC_COOKIE 0xdeadbeef
  90. /* wrapper around endpoint-specific packets */
  91. /**
  92. * struct _HTC_PACKET - HTC Packet data structure
  93. * @ListLink: double link
  94. * @pPktContext: caller's per packet specific context
  95. * @pBufferStart: The true buffer start, the caller can store the real buffer
  96. * start here. In receive callbacks, the HTC layer sets pBuffer
  97. * to the start of the payload past the header. This field allows
  98. * the caller to reset pBuffer when it recycles receive packets
  99. * back to HTC
  100. * @pBuffer: payload start (RX/TX)
  101. * @BufferLength: length of buffer
  102. * @ActualLength: actual length of payload
  103. * @Endpoint: endpoint that this packet was sent/recv'd from
  104. * @Status: completion status
  105. * @PktInfo: Packet specific info
  106. * @netbufOrigHeadRoom: Original head room of skb
  107. * @Completion: completion
  108. * @pContext: HTC private completion context
  109. * @pNetBufContext: optimization for network-oriented data, the HTC packet can
  110. * pass the network buffer corresponding to the HTC packet
  111. * lower layers may optimized the transfer knowing this is a
  112. * network buffer
  113. * @magic_cookie: HTC Magic cookie
  114. */
  115. typedef struct _HTC_PACKET {
  116. DL_LIST ListLink;
  117. void *pPktContext;
  118. uint8_t *pBufferStart;
  119. /*
  120. * Pointer to the start of the buffer. In the transmit
  121. * direction this points to the start of the payload. In the
  122. * receive direction, however, the buffer when queued up
  123. * points to the start of the HTC header but when returned
  124. * to the caller points to the start of the payload
  125. */
  126. uint8_t *pBuffer;
  127. uint32_t BufferLength;
  128. uint32_t ActualLength;
  129. HTC_ENDPOINT_ID Endpoint;
  130. QDF_STATUS Status;
  131. union {
  132. struct htc_tx_packet_info AsTx;
  133. struct htc_rx_packet_info AsRx;
  134. } PktInfo;
  135. /* the following fields are for internal HTC use */
  136. uint32_t netbufOrigHeadRoom;
  137. HTC_PACKET_COMPLETION Completion;
  138. void *pContext;
  139. void *pNetBufContext;
  140. uint32_t magic_cookie;
  141. } HTC_PACKET;
  142. #define COMPLETE_HTC_PACKET(p, status) \
  143. { \
  144. (p)->Status = (status); \
  145. (p)->Completion((p)->pContext, (p)); \
  146. }
  147. #define INIT_HTC_PACKET_INFO(p, b, len) \
  148. { \
  149. (p)->pBufferStart = (b); \
  150. (p)->BufferLength = (len); \
  151. }
  152. /* macro to set an initial RX packet for refilling HTC */
  153. #define SET_HTC_PACKET_INFO_RX_REFILL(p, c, b, len, ep) \
  154. do { \
  155. (p)->pPktContext = (c); \
  156. (p)->pBuffer = (b); \
  157. (p)->pBufferStart = (b); \
  158. (p)->BufferLength = (len); \
  159. (p)->Endpoint = (ep); \
  160. } while (0)
  161. /* fast macro to recycle an RX packet that will be re-queued to HTC */
  162. #define HTC_PACKET_RESET_RX(p) \
  163. { (p)->pBuffer = (p)->pBufferStart; (p)->ActualLength = 0; }
  164. /* macro to set packet parameters for TX */
  165. #define SET_HTC_PACKET_INFO_TX(p, c, b, len, ep, tag) \
  166. do { \
  167. (p)->pPktContext = (c); \
  168. (p)->pBuffer = (b); \
  169. (p)->ActualLength = (len); \
  170. (p)->Endpoint = (ep); \
  171. (p)->PktInfo.AsTx.Tag = (tag); \
  172. (p)->PktInfo.AsTx.Flags = 0; \
  173. (p)->PktInfo.AsTx.SendFlags = 0; \
  174. } while (0)
  175. #define SET_HTC_PACKET_NET_BUF_CONTEXT(p, nb) \
  176. { \
  177. (p)->pNetBufContext = (nb); \
  178. }
  179. #define GET_HTC_PACKET_NET_BUF_CONTEXT(p) (p)->pNetBufContext
  180. /* HTC Packet Queueing Macros */
  181. typedef struct _HTC_PACKET_QUEUE {
  182. DL_LIST QueueHead;
  183. int Depth;
  184. } HTC_PACKET_QUEUE;
  185. /* initialize queue */
  186. #define INIT_HTC_PACKET_QUEUE(pQ) \
  187. { \
  188. DL_LIST_INIT(&(pQ)->QueueHead); \
  189. (pQ)->Depth = 0; \
  190. }
  191. /* enqueue HTC packet to the tail of the queue */
  192. #define HTC_PACKET_ENQUEUE(pQ, p) \
  193. { dl_list_insert_tail(&(pQ)->QueueHead, &(p)->ListLink); \
  194. (pQ)->Depth++; \
  195. }
  196. /* enqueue HTC packet to the tail of the queue */
  197. #define HTC_PACKET_ENQUEUE_TO_HEAD(pQ, p) \
  198. { dl_list_insert_head(&(pQ)->QueueHead, &(p)->ListLink); \
  199. (pQ)->Depth++; \
  200. }
  201. /* test if a queue is empty */
  202. #define HTC_QUEUE_EMPTY(pQ) ((pQ)->Depth == 0)
  203. /* get packet at head without removing it */
  204. static inline HTC_PACKET *htc_get_pkt_at_head(HTC_PACKET_QUEUE *queue)
  205. {
  206. if (queue->Depth == 0)
  207. return NULL;
  208. return A_CONTAINING_STRUCT((DL_LIST_GET_ITEM_AT_HEAD(
  209. &queue->QueueHead)),
  210. HTC_PACKET, ListLink);
  211. }
  212. /* remove a packet from a queue, where-ever it is in the queue */
  213. #define HTC_PACKET_REMOVE(pQ, p) \
  214. { \
  215. dl_list_remove(&(p)->ListLink); \
  216. (pQ)->Depth--; \
  217. }
  218. /* dequeue an HTC packet from the head of the queue */
  219. static inline HTC_PACKET *htc_packet_dequeue(HTC_PACKET_QUEUE *queue)
  220. {
  221. DL_LIST *pItem = dl_list_remove_item_from_head(&queue->QueueHead);
  222. if (pItem) {
  223. queue->Depth--;
  224. return A_CONTAINING_STRUCT(pItem, HTC_PACKET, ListLink);
  225. }
  226. return NULL;
  227. }
  228. /* dequeue an HTC packet from the tail of the queue */
  229. static inline HTC_PACKET *htc_packet_dequeue_tail(HTC_PACKET_QUEUE *queue)
  230. {
  231. DL_LIST *pItem = dl_list_remove_item_from_tail(&queue->QueueHead);
  232. if (pItem) {
  233. queue->Depth--;
  234. return A_CONTAINING_STRUCT(pItem, HTC_PACKET, ListLink);
  235. }
  236. return NULL;
  237. }
  238. #define HTC_PACKET_QUEUE_DEPTH(pQ) (pQ)->Depth
  239. #define HTC_GET_ENDPOINT_FROM_PKT(p) (p)->Endpoint
  240. #define HTC_GET_TAG_FROM_PKT(p) (p)->PktInfo.AsTx.Tag
  241. /* transfer the packets from one queue to the tail of another queue */
  242. #define HTC_PACKET_QUEUE_TRANSFER_TO_TAIL(pQDest, pQSrc) \
  243. { \
  244. dl_list_transfer_items_to_tail(&(pQDest)->QueueHead, \
  245. &(pQSrc)->QueueHead); \
  246. (pQDest)->Depth += (pQSrc)->Depth; \
  247. (pQSrc)->Depth = 0; \
  248. }
  249. /*
  250. * Transfer the packets from one queue to the head of another queue.
  251. * This xfer_to_head(q1,q2) is basically equivalent to xfer_to_tail(q2,q1),
  252. * but it updates the queue descriptor object for the initial queue to refer
  253. * to the concatenated queue.
  254. */
  255. #define HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(pQDest, pQSrc) \
  256. { \
  257. dl_list_transfer_items_to_head(&(pQDest)->QueueHead, \
  258. &(pQSrc)->QueueHead); \
  259. (pQDest)->Depth += (pQSrc)->Depth; \
  260. (pQSrc)->Depth = 0; \
  261. }
  262. /* fast version to init and add a single packet to a queue */
  263. #define INIT_HTC_PACKET_QUEUE_AND_ADD(pQ, pP) \
  264. { \
  265. DL_LIST_INIT_AND_ADD(&(pQ)->QueueHead, &(pP)->ListLink) \
  266. (pQ)->Depth = 1; \
  267. }
  268. #define HTC_PACKET_QUEUE_ITERATE_ALLOW_REMOVE(pQ, pPTemp) \
  269. ITERATE_OVER_LIST_ALLOW_REMOVE(&(pQ)->QueueHead, \
  270. (pPTemp), HTC_PACKET, ListLink)
  271. #define HTC_PACKET_QUEUE_ITERATE_IS_VALID(pQ) ITERATE_IS_VALID(&(pQ)->QueueHead)
  272. #define HTC_PACKET_QUEUE_ITERATE_RESET(pQ) ITERATE_RESET(&(pQ)->QueueHead)
  273. #define HTC_PACKET_QUEUE_ITERATE_END ITERATE_END
  274. /**
  275. * htc_packet_set_magic_cookie() - set magic cookie in htc packet
  276. * @htc_pkt: pointer to htc packet
  277. * @value: value to set in magic cookie
  278. *
  279. * This API sets the magic cookie passed in htc packet.
  280. *
  281. * Return : None
  282. */
  283. static inline void htc_packet_set_magic_cookie(HTC_PACKET *htc_pkt,
  284. uint32_t value)
  285. {
  286. htc_pkt->magic_cookie = value;
  287. }
  288. /**
  289. * htc_packet_get_magic_cookie() - get magic cookie in htc packet
  290. * @htc_pkt: pointer to htc packet
  291. *
  292. * This API returns the magic cookie in htc packet.
  293. *
  294. * Return : magic cookie
  295. */
  296. static inline uint32_t htc_packet_get_magic_cookie(HTC_PACKET *htc_pkt)
  297. {
  298. return htc_pkt->magic_cookie;
  299. }
  300. #endif /*HTC_PACKET_H_ */