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