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)
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 struct htc_endpoint_credit_dist CreditDist; /* credit distribution structure (exposed to driver layer) */
75 struct 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 u32 LocalConnectionFlags; /* local connection flags */
85 #ifdef HTC_EP_STAT_PROFILING
86 struct 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 struct htc_control_buffer {
103 struct htc_packet HtcPacket;
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 struct htc_endpoint EndPoint[ENDPOINT_MAX];
113 struct htc_control_buffer HTCControlBuffers[NUM_CONTROL_BUFFERS];
114 struct 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 struct ar6k_device Device; /* AR6K - specific state */
128 HTC_ENDPOINT_ID EpWaitingForBuffers;
130 #ifdef HTC_CAPTURE_LAST_FRAME
131 struct htc_frame_hdr LastFrameHdr; /* useful for debugging */
133 u8 LastTrailerLength;
135 struct 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, struct htc_packet *pPacket);
166 void HTCControlRecv(void *Context, struct htc_packet *pPacket);
167 int HTCWaitforControlMessage(HTC_TARGET *target, struct htc_packet **ppControlPacket);
168 struct htc_packet *HTCAllocControlBuffer(HTC_TARGET *target, HTC_PACKET_QUEUE *pList);
169 void HTCFreeControlBuffer(HTC_TARGET *target, struct htc_packet *pPacket, HTC_PACKET_QUEUE *pList);
170 int HTCIssueSend(HTC_TARGET *target, struct htc_packet *pPacket);
171 void HTCRecvCompleteHandler(void *Context, struct htc_packet *pPacket);
172 int HTCRecvMessagePendingHandler(void *Context, u32 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(struct htc_endpoint_credit_dist *pEPDist);
180 void DumpCreditDistStates(HTC_TARGET *target);
181 void DebugDumpBytes(u8 *buffer, u16 length, char *pDescription);
184 static INLINE struct htc_packet *HTC_ALLOC_CONTROL_TX(HTC_TARGET *target) {
185 struct 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,struct htc_frame_hdr,PayloadLen,(u16)(pP)->ActualLength); \
207 A_SET_UINT8_FIELD(pHdrBuf,struct htc_frame_hdr,Flags,(sendflags)); \
208 A_SET_UINT8_FIELD(pHdrBuf,struct htc_frame_hdr,EndpointID, (u8)(pP)->Endpoint); \
209 A_SET_UINT8_FIELD(pHdrBuf,struct htc_frame_hdr,ControlBytes[0], (u8)(ctrl0)); \
210 A_SET_UINT8_FIELD(pHdrBuf,struct 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_ */