xref: /wlan-dirver/qca-wifi-host-cmn/htc/htc_packet.h (revision dd4dc88b837a295134aa9869114a2efee0f4894b)
1 /*
2  * Copyright (c) 2013-2014, 2016-2017, 2019 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 
80 #define HTC_TX_PACKET_FLAG_FIXUP_NETBUF (1 << 0)
81 
82 /**
83  * struct htc_rx_packet_info - HTC RX Packet information
84  * @ExpectedHdr: HTC Internal use
85  * @HTCRxFlags: HTC Internal use
86  * @IndicationFlags: indication flags set on each RX packet indication
87  */
88 struct htc_rx_packet_info {
89 	uint32_t ExpectedHdr;
90 	uint32_t HTCRxFlags;
91 	uint32_t IndicationFlags;
92 };
93 
94 /* more packets on this endpoint are being fetched */
95 #define HTC_RX_FLAGS_INDICATE_MORE_PKTS  (1 << 0)
96 #define HTC_PACKET_MAGIC_COOKIE          0xdeadbeef
97 
98 /* wrapper around endpoint-specific packets */
99 /**
100  * struct _HTC_PACKET - HTC Packet data structure
101  * @ListLink: double link
102  * @pPktContext: caller's per packet specific context
103  * @pBufferStart: The true buffer start, the caller can store the real buffer
104  *                start here.  In receive callbacks, the HTC layer sets pBuffer
105  *                to the start of the payload past the header. This field allows
106  *                the caller to reset pBuffer when it recycles receive packets
107  *                back to HTC
108  * @pBuffer: payload start (RX/TX)
109  * @BufferLength: length of buffer
110  * @ActualLength: actual length of payload
111  * @Endpoint: endpoint that this packet was sent/recv'd from
112  * @Status: completion status
113  * @PktInfo: Packet specific info
114  * @netbufOrigHeadRoom: Original head room of skb
115  * @Completion: completion
116  * @pContext: HTC private completion context
117  * @pNetBufContext: optimization for network-oriented data, the HTC packet can
118  *                  pass the network buffer corresponding to the HTC packet
119  *                  lower layers may optimized the transfer knowing this is a
120  *                  network buffer
121  * @magic_cookie: HTC Magic cookie
122  */
123 typedef struct _HTC_PACKET {
124 	DL_LIST ListLink;
125 	void *pPktContext;
126 	uint8_t *pBufferStart;
127 	/*
128 	 * Pointer to the start of the buffer. In the transmit
129 	 * direction this points to the start of the payload. In the
130 	 * receive direction, however, the buffer when queued up
131 	 * points to the start of the HTC header but when returned
132 	 * to the caller points to the start of the payload
133 	 */
134 	uint8_t *pBuffer;
135 	uint32_t BufferLength;
136 	uint32_t ActualLength;
137 	HTC_ENDPOINT_ID Endpoint;
138 	QDF_STATUS Status;
139 	union {
140 		struct htc_tx_packet_info AsTx;
141 		struct htc_rx_packet_info AsRx;
142 	} PktInfo;
143 	/* the following fields are for internal HTC use */
144 	uint32_t netbufOrigHeadRoom;
145 	HTC_PACKET_COMPLETION Completion;
146 	void *pContext;
147 	void *pNetBufContext;
148 	uint32_t magic_cookie;
149 } HTC_PACKET;
150 
151 #define COMPLETE_HTC_PACKET(p, status)	     \
152 	{					     \
153 		(p)->Status = (status);			 \
154 		(p)->Completion((p)->pContext, (p));	 \
155 	}
156 
157 #define INIT_HTC_PACKET_INFO(p, b, len)		  \
158 	{						  \
159 		(p)->pBufferStart = (b);		      \
160 		(p)->BufferLength = (len);		      \
161 	}
162 
163 /* macro to set an initial RX packet for refilling HTC */
164 #define SET_HTC_PACKET_INFO_RX_REFILL(p, c, b, len, ep) \
165 	do { \
166 		(p)->pPktContext = (c);			      \
167 		(p)->pBuffer = (b);			      \
168 		(p)->pBufferStart = (b);		      \
169 		(p)->BufferLength = (len);		      \
170 		(p)->Endpoint = (ep);			      \
171 	} while (0)
172 
173 /* fast macro to recycle an RX packet that will be re-queued to HTC */
174 #define HTC_PACKET_RESET_RX(p)		    \
175 	{ (p)->pBuffer = (p)->pBufferStart; (p)->ActualLength = 0; }
176 
177 /* macro to set packet parameters for TX */
178 #define SET_HTC_PACKET_INFO_TX(p, c, b, len, ep, tag)  \
179 	do {						  \
180 		(p)->pPktContext = (c);			      \
181 		(p)->pBuffer = (b);			      \
182 		(p)->ActualLength = (len);		      \
183 		(p)->Endpoint = (ep);			      \
184 		(p)->PktInfo.AsTx.Tag = (tag);		      \
185 		(p)->PktInfo.AsTx.Flags = 0;		      \
186 		(p)->PktInfo.AsTx.SendFlags = 0;	      \
187 	} while (0)
188 
189 #define SET_HTC_PACKET_NET_BUF_CONTEXT(p, nb) \
190 	{ \
191 		(p)->pNetBufContext = (nb); \
192 	}
193 
194 #define GET_HTC_PACKET_NET_BUF_CONTEXT(p)  (p)->pNetBufContext
195 
196 /* HTC Packet Queueing Macros */
197 typedef struct _HTC_PACKET_QUEUE {
198 	DL_LIST QueueHead;
199 	int Depth;
200 } HTC_PACKET_QUEUE;
201 
202 /* initialize queue */
203 #define INIT_HTC_PACKET_QUEUE(pQ)   \
204 	{				    \
205 		DL_LIST_INIT(&(pQ)->QueueHead); \
206 		(pQ)->Depth = 0;		\
207 	}
208 
209 /* enqueue HTC packet to the tail of the queue */
210 #define HTC_PACKET_ENQUEUE(pQ, p)			\
211 	{   dl_list_insert_tail(&(pQ)->QueueHead, &(p)->ListLink); \
212 	    (pQ)->Depth++;					 \
213 	}
214 
215 /* enqueue HTC packet to the tail of the queue */
216 #define HTC_PACKET_ENQUEUE_TO_HEAD(pQ, p)		\
217 	{   dl_list_insert_head(&(pQ)->QueueHead, &(p)->ListLink); \
218 	    (pQ)->Depth++;					 \
219 	}
220 /* test if a queue is empty */
221 #define HTC_QUEUE_EMPTY(pQ)       ((pQ)->Depth == 0)
222 /* get packet at head without removing it */
223 static inline HTC_PACKET *htc_get_pkt_at_head(HTC_PACKET_QUEUE *queue)
224 {
225 	if (queue->Depth == 0)
226 		return NULL;
227 
228 	return A_CONTAINING_STRUCT((DL_LIST_GET_ITEM_AT_HEAD(
229 					&queue->QueueHead)),
230 				    HTC_PACKET, ListLink);
231 }
232 
233 /* remove a packet from a queue, where-ever it is in the queue */
234 #define HTC_PACKET_REMOVE(pQ, p)	    \
235 	{				    \
236 		dl_list_remove(&(p)->ListLink);  \
237 		(pQ)->Depth--;			 \
238 	}
239 
240 /* dequeue an HTC packet from the head of the queue */
241 static inline HTC_PACKET *htc_packet_dequeue(HTC_PACKET_QUEUE *queue)
242 {
243 	DL_LIST *pItem = dl_list_remove_item_from_head(&queue->QueueHead);
244 
245 	if (pItem) {
246 		queue->Depth--;
247 		return A_CONTAINING_STRUCT(pItem, HTC_PACKET, ListLink);
248 	}
249 	return NULL;
250 }
251 
252 /* dequeue an HTC packet from the tail of the queue */
253 static inline HTC_PACKET *htc_packet_dequeue_tail(HTC_PACKET_QUEUE *queue)
254 {
255 	DL_LIST *pItem = dl_list_remove_item_from_tail(&queue->QueueHead);
256 
257 	if (pItem) {
258 		queue->Depth--;
259 		return A_CONTAINING_STRUCT(pItem, HTC_PACKET, ListLink);
260 	}
261 	return NULL;
262 }
263 
264 #define HTC_PACKET_QUEUE_DEPTH(pQ) (pQ)->Depth
265 
266 #define HTC_GET_ENDPOINT_FROM_PKT(p) (p)->Endpoint
267 #define HTC_GET_TAG_FROM_PKT(p)      (p)->PktInfo.AsTx.Tag
268 
269 /* transfer the packets from one queue to the tail of another queue */
270 #define HTC_PACKET_QUEUE_TRANSFER_TO_TAIL(pQDest, pQSrc) \
271 	{ \
272 		dl_list_transfer_items_to_tail(&(pQDest)->QueueHead, \
273 					       &(pQSrc)->QueueHead); \
274 		(pQDest)->Depth += (pQSrc)->Depth; \
275 		(pQSrc)->Depth = 0; \
276 	}
277 
278 /*
279  * Transfer the packets from one queue to the head of another queue.
280  * This xfer_to_head(q1,q2) is basically equivalent to xfer_to_tail(q2,q1),
281  * but it updates the queue descriptor object for the initial queue to refer
282  * to the concatenated queue.
283  */
284 #define HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(pQDest, pQSrc)  \
285 	{ \
286 		dl_list_transfer_items_to_head(&(pQDest)->QueueHead, \
287 					       &(pQSrc)->QueueHead); \
288 		(pQDest)->Depth += (pQSrc)->Depth; \
289 		(pQSrc)->Depth = 0; \
290 	}
291 
292 /* fast version to init and add a single packet to a queue */
293 #define INIT_HTC_PACKET_QUEUE_AND_ADD(pQ, pP) \
294 	{					     \
295 		DL_LIST_INIT_AND_ADD(&(pQ)->QueueHead, &(pP)->ListLink)	\
296 		(pQ)->Depth = 1;					\
297 	}
298 
299 #define HTC_PACKET_QUEUE_ITERATE_ALLOW_REMOVE(pQ, pPTemp) \
300 		ITERATE_OVER_LIST_ALLOW_REMOVE(&(pQ)->QueueHead, \
301 						(pPTemp), HTC_PACKET, ListLink)
302 
303 #define HTC_PACKET_QUEUE_ITERATE_IS_VALID(pQ) ITERATE_IS_VALID(&(pQ)->QueueHead)
304 #define HTC_PACKET_QUEUE_ITERATE_RESET(pQ) ITERATE_RESET(&(pQ)->QueueHead)
305 
306 #define HTC_PACKET_QUEUE_ITERATE_END ITERATE_END
307 
308 /**
309  * htc_packet_set_magic_cookie() - set magic cookie in htc packet
310  * htc_pkt - pointer to htc packet
311  * value - value to set in magic cookie
312  *
313  * This API sets the magic cookie passed in htc packet.
314  *
315  * Return : None
316  */
317 static inline void htc_packet_set_magic_cookie(HTC_PACKET *htc_pkt,
318 			uint32_t value)
319 {
320 	htc_pkt->magic_cookie = value;
321 }
322 
323 /**
324  * htc_packet_set_magic_cookie() - get magic cookie in htc packet
325  * htc_pkt - pointer to htc packet
326  *
327  * This API returns the magic cookie in htc packet.
328  *
329  * Return : magic cookie
330  */
331 static inline uint32_t htc_packet_get_magic_cookie(HTC_PACKET *htc_pkt)
332 {
333 	return htc_pkt->magic_cookie;
334 }
335 
336 #endif /*HTC_PACKET_H_ */
337