1 //------------------------------------------------------------------------------
2 // <copyright file="htc_internal.h" company="Atheros">
3 // Copyright (c) 2007-2010 Atheros Corporation. All rights reserved.
6 // Permission to use, copy, modify, and/or distribute this software for any
7 // purpose with or without fee is hereby granted, provided that the above
8 // copyright notice and this permission notice appear in all copies.
10 // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 //------------------------------------------------------------------------------
20 //==============================================================================
21 // Author(s): ="Atheros"
22 //==============================================================================
23 #ifndef _HTC_INTERNAL_H_
24 #define _HTC_INTERNAL_H_
26 /* for debugging, uncomment this to capture the last frame header, on frame header
27 * processing errors, the last frame header is dump for comparison */
28 //#define HTC_CAPTURE_LAST_FRAME
30 //#define HTC_EP_STAT_PROFILING
34 #endif /* __cplusplus */
42 #include "htc_debug.h"
47 #include "AR6000/ar6k.h"
49 /* HTC operational parameters */
50 #define HTC_TARGET_RESPONSE_TIMEOUT 2000 /* in ms */
51 #define HTC_TARGET_DEBUG_INTR_MASK 0x01
52 #define HTC_TARGET_CREDIT_INTR_MASK 0xF0
54 #define HTC_HOST_MAX_MSG_PER_BUNDLE 8
55 #define HTC_MIN_HTC_MSGS_TO_BUNDLE 2
59 #define HTC_RX_PKT_IGNORE_LOOKAHEAD (1 << 0)
60 #define HTC_RX_PKT_REFRESH_HDR (1 << 1)
61 #define HTC_RX_PKT_PART_OF_BUNDLE (1 << 2)
62 #define HTC_RX_PKT_NO_RECYCLE (1 << 3)
64 /* scatter request flags */
66 #define HTC_SCATTER_REQ_FLAGS_PARTIAL_BUNDLE (1 << 0)
68 typedef struct _HTC_ENDPOINT {
70 HTC_SERVICE_ID ServiceID; /* service ID this endpoint is bound to
71 non-zero value means this endpoint is in use */
72 HTC_PACKET_QUEUE TxQueue; /* HTC frame buffer TX queue */
73 HTC_PACKET_QUEUE RxBuffers; /* HTC frame buffer RX list */
74 HTC_ENDPOINT_CREDIT_DIST CreditDist; /* credit distribution structure (exposed to driver layer) */
75 HTC_EP_CALLBACKS EpCallBacks; /* callbacks associated with this endpoint */
76 int MaxTxQueueDepth; /* max depth of the TX queue before we need to
77 call driver's full handler */
78 int MaxMsgLength; /* max length of endpoint message */
79 int TxProcessCount; /* reference count to continue tx processing */
80 HTC_PACKET_QUEUE RecvIndicationQueue; /* recv packets ready to be indicated */
81 int RxProcessCount; /* reference count to allow single processing context */
82 struct _HTC_TARGET *target; /* back pointer to target */
83 u8 SeqNo; /* TX seq no (helpful) for debugging */
84 A_UINT32 LocalConnectionFlags; /* local connection flags */
85 #ifdef HTC_EP_STAT_PROFILING
86 HTC_ENDPOINT_STATS EndPointStats; /* endpoint statistics */
90 #ifdef HTC_EP_STAT_PROFILING
91 #define INC_HTC_EP_STAT(p,stat,count) (p)->EndPointStats.stat += (count);
93 #define INC_HTC_EP_STAT(p,stat,count)
96 #define HTC_SERVICE_TX_PACKET_TAG HTC_TX_PACKET_TAG_INTERNAL
98 #define NUM_CONTROL_BUFFERS 8
99 #define NUM_CONTROL_TX_BUFFERS 2
100 #define NUM_CONTROL_RX_BUFFERS (NUM_CONTROL_BUFFERS - NUM_CONTROL_TX_BUFFERS)
102 typedef struct HTC_CONTROL_BUFFER {
103 HTC_PACKET HtcPacket;
105 } HTC_CONTROL_BUFFER;
107 #define HTC_RECV_WAIT_BUFFERS (1 << 0)
108 #define HTC_OP_STATE_STOPPING (1 << 0)
110 /* our HTC target state */
111 typedef struct _HTC_TARGET {
112 HTC_ENDPOINT EndPoint[ENDPOINT_MAX];
113 HTC_CONTROL_BUFFER HTCControlBuffers[NUM_CONTROL_BUFFERS];
114 HTC_ENDPOINT_CREDIT_DIST *EpCreditDistributionListHead;
115 HTC_PACKET_QUEUE ControlBufferTXFreeList;
116 HTC_PACKET_QUEUE ControlBufferRXFreeList;
117 HTC_CREDIT_DIST_CALLBACK DistributeCredits;
118 HTC_CREDIT_INIT_CALLBACK InitCredits;
119 void *pCredDistContext;
121 unsigned int TargetCreditSize;
125 AR6K_DEVICE Device; /* AR6K - specific state */
126 A_UINT32 OpStateFlags;
127 A_UINT32 RecvStateFlags;
128 HTC_ENDPOINT_ID EpWaitingForBuffers;
130 #ifdef HTC_CAPTURE_LAST_FRAME
131 HTC_FRAME_HDR LastFrameHdr; /* useful for debugging */
133 u8 LastTrailerLength;
135 HTC_INIT_INFO HTCInitInfo;
137 int MaxMsgPerBundle; /* max messages per bundle for HTC */
138 bool SendBundlingEnabled; /* run time enable for send bundling (dynamic) */
139 int RecvBundlingEnabled; /* run time enable for recv bundling (dynamic) */
142 #define HTC_STOPPING(t) ((t)->OpStateFlags & HTC_OP_STATE_STOPPING)
143 #define LOCK_HTC(t) A_MUTEX_LOCK(&(t)->HTCLock);
144 #define UNLOCK_HTC(t) A_MUTEX_UNLOCK(&(t)->HTCLock);
145 #define LOCK_HTC_RX(t) A_MUTEX_LOCK(&(t)->HTCRxLock);
146 #define UNLOCK_HTC_RX(t) A_MUTEX_UNLOCK(&(t)->HTCRxLock);
147 #define LOCK_HTC_TX(t) A_MUTEX_LOCK(&(t)->HTCTxLock);
148 #define UNLOCK_HTC_TX(t) A_MUTEX_UNLOCK(&(t)->HTCTxLock);
150 #define GET_HTC_TARGET_FROM_HANDLE(hnd) ((HTC_TARGET *)(hnd))
151 #define HTC_RECYCLE_RX_PKT(target,p,e) \
153 if ((p)->PktInfo.AsRx.HTCRxFlags & HTC_RX_PKT_NO_RECYCLE) { \
154 HTC_PACKET_RESET_RX(pPacket); \
155 pPacket->Status = A_ECANCELED; \
156 (e)->EpCallBacks.EpRecv((e)->EpCallBacks.pContext, \
159 HTC_PACKET_RESET_RX(pPacket); \
160 HTCAddReceivePkt((HTC_HANDLE)(target),(p)); \
164 /* internal HTC functions */
165 void HTCControlTxComplete(void *Context, HTC_PACKET *pPacket);
166 void HTCControlRecv(void *Context, HTC_PACKET *pPacket);
167 int HTCWaitforControlMessage(HTC_TARGET *target, HTC_PACKET **ppControlPacket);
168 HTC_PACKET *HTCAllocControlBuffer(HTC_TARGET *target, HTC_PACKET_QUEUE *pList);
169 void HTCFreeControlBuffer(HTC_TARGET *target, HTC_PACKET *pPacket, HTC_PACKET_QUEUE *pList);
170 int HTCIssueSend(HTC_TARGET *target, HTC_PACKET *pPacket);
171 void HTCRecvCompleteHandler(void *Context, HTC_PACKET *pPacket);
172 int HTCRecvMessagePendingHandler(void *Context, A_UINT32 MsgLookAheads[], int NumLookAheads, bool *pAsyncProc, int *pNumPktsFetched);
173 void HTCProcessCreditRpt(HTC_TARGET *target, HTC_CREDIT_REPORT *pRpt, int NumEntries, HTC_ENDPOINT_ID FromEndpoint);
174 int HTCSendSetupComplete(HTC_TARGET *target);
175 void HTCFlushRecvBuffers(HTC_TARGET *target);
176 void HTCFlushSendPkts(HTC_TARGET *target);
178 #ifdef ATH_DEBUG_MODULE
179 void DumpCreditDist(HTC_ENDPOINT_CREDIT_DIST *pEPDist);
180 void DumpCreditDistStates(HTC_TARGET *target);
181 void DebugDumpBytes(A_UCHAR *buffer, A_UINT16 length, char *pDescription);
184 static INLINE HTC_PACKET *HTC_ALLOC_CONTROL_TX(HTC_TARGET *target) {
185 HTC_PACKET *pPacket = HTCAllocControlBuffer(target,&target->ControlBufferTXFreeList);
186 if (pPacket != NULL) {
187 /* set payload pointer area with some headroom */
188 pPacket->pBuffer = pPacket->pBufferStart + HTC_HDR_LENGTH;
193 #define HTC_FREE_CONTROL_TX(t,p) HTCFreeControlBuffer((t),(p),&(t)->ControlBufferTXFreeList)
194 #define HTC_ALLOC_CONTROL_RX(t) HTCAllocControlBuffer((t),&(t)->ControlBufferRXFreeList)
195 #define HTC_FREE_CONTROL_RX(t,p) \
197 HTC_PACKET_RESET_RX(p); \
198 HTCFreeControlBuffer((t),(p),&(t)->ControlBufferRXFreeList); \
201 #define HTC_PREPARE_SEND_PKT(pP,sendflags,ctrl0,ctrl1) \
204 (pP)->pBuffer -= HTC_HDR_LENGTH; \
205 pHdrBuf = (pP)->pBuffer; \
206 A_SET_UINT16_FIELD(pHdrBuf,HTC_FRAME_HDR,PayloadLen,(A_UINT16)(pP)->ActualLength); \
207 A_SET_UINT8_FIELD(pHdrBuf,HTC_FRAME_HDR,Flags,(sendflags)); \
208 A_SET_UINT8_FIELD(pHdrBuf,HTC_FRAME_HDR,EndpointID, (u8)(pP)->Endpoint); \
209 A_SET_UINT8_FIELD(pHdrBuf,HTC_FRAME_HDR,ControlBytes[0], (u8)(ctrl0)); \
210 A_SET_UINT8_FIELD(pHdrBuf,HTC_FRAME_HDR,ControlBytes[1], (u8)(ctrl1)); \
213 #define HTC_UNPREPARE_SEND_PKT(pP) \
214 (pP)->pBuffer += HTC_HDR_LENGTH; \
220 #endif /* _HTC_INTERNAL_H_ */