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
33 #endif /* __cplusplus */
40 #include "htc_debug.h"
45 #include "AR6000/ar6k.h"
47 /* HTC operational parameters */
48 #define HTC_TARGET_RESPONSE_TIMEOUT 2000 /* in ms */
49 #define HTC_TARGET_DEBUG_INTR_MASK 0x01
50 #define HTC_TARGET_CREDIT_INTR_MASK 0xF0
52 #define HTC_HOST_MAX_MSG_PER_BUNDLE 8
53 #define HTC_MIN_HTC_MSGS_TO_BUNDLE 2
57 #define HTC_RX_PKT_IGNORE_LOOKAHEAD (1 << 0)
58 #define HTC_RX_PKT_REFRESH_HDR (1 << 1)
59 #define HTC_RX_PKT_PART_OF_BUNDLE (1 << 2)
60 #define HTC_RX_PKT_NO_RECYCLE (1 << 3)
62 /* scatter request flags */
64 #define HTC_SCATTER_REQ_FLAGS_PARTIAL_BUNDLE (1 << 0)
68 HTC_SERVICE_ID ServiceID; /* service ID this endpoint is bound to
69 non-zero value means this endpoint is in use */
70 struct htc_packet_queue TxQueue; /* HTC frame buffer TX queue */
71 struct htc_packet_queue RxBuffers; /* HTC frame buffer RX list */
72 struct htc_endpoint_credit_dist CreditDist; /* credit distribution structure (exposed to driver layer) */
73 struct htc_ep_callbacks EpCallBacks; /* callbacks associated with this endpoint */
74 int MaxTxQueueDepth; /* max depth of the TX queue before we need to
75 call driver's full handler */
76 int MaxMsgLength; /* max length of endpoint message */
77 int TxProcessCount; /* reference count to continue tx processing */
78 struct htc_packet_queue RecvIndicationQueue; /* recv packets ready to be indicated */
79 int RxProcessCount; /* reference count to allow single processing context */
80 struct htc_target *target; /* back pointer to target */
81 u8 SeqNo; /* TX seq no (helpful) for debugging */
82 u32 LocalConnectionFlags; /* local connection flags */
83 struct htc_endpoint_stats EndPointStats; /* endpoint statistics */
86 #define INC_HTC_EP_STAT(p,stat,count) (p)->EndPointStats.stat += (count);
87 #define HTC_SERVICE_TX_PACKET_TAG HTC_TX_PACKET_TAG_INTERNAL
89 #define NUM_CONTROL_BUFFERS 8
90 #define NUM_CONTROL_TX_BUFFERS 2
91 #define NUM_CONTROL_RX_BUFFERS (NUM_CONTROL_BUFFERS - NUM_CONTROL_TX_BUFFERS)
93 struct htc_control_buffer {
94 struct htc_packet HtcPacket;
98 #define HTC_RECV_WAIT_BUFFERS (1 << 0)
99 #define HTC_OP_STATE_STOPPING (1 << 0)
101 /* our HTC target state */
103 struct htc_endpoint EndPoint[ENDPOINT_MAX];
104 struct htc_control_buffer HTCControlBuffers[NUM_CONTROL_BUFFERS];
105 struct htc_endpoint_credit_dist *EpCreditDistributionListHead;
106 struct htc_packet_queue ControlBufferTXFreeList;
107 struct htc_packet_queue ControlBufferRXFreeList;
108 HTC_CREDIT_DIST_CALLBACK DistributeCredits;
109 HTC_CREDIT_INIT_CALLBACK InitCredits;
110 void *pCredDistContext;
112 unsigned int TargetCreditSize;
116 struct ar6k_device Device; /* AR6K - specific state */
119 HTC_ENDPOINT_ID EpWaitingForBuffers;
121 #ifdef HTC_CAPTURE_LAST_FRAME
122 struct htc_frame_hdr LastFrameHdr; /* useful for debugging */
124 u8 LastTrailerLength;
126 struct htc_init_info HTCInitInfo;
128 int MaxMsgPerBundle; /* max messages per bundle for HTC */
129 bool SendBundlingEnabled; /* run time enable for send bundling (dynamic) */
130 int RecvBundlingEnabled; /* run time enable for recv bundling (dynamic) */
133 #define HTC_STOPPING(t) ((t)->OpStateFlags & HTC_OP_STATE_STOPPING)
134 #define LOCK_HTC(t) A_MUTEX_LOCK(&(t)->HTCLock);
135 #define UNLOCK_HTC(t) A_MUTEX_UNLOCK(&(t)->HTCLock);
136 #define LOCK_HTC_RX(t) A_MUTEX_LOCK(&(t)->HTCRxLock);
137 #define UNLOCK_HTC_RX(t) A_MUTEX_UNLOCK(&(t)->HTCRxLock);
138 #define LOCK_HTC_TX(t) A_MUTEX_LOCK(&(t)->HTCTxLock);
139 #define UNLOCK_HTC_TX(t) A_MUTEX_UNLOCK(&(t)->HTCTxLock);
141 #define GET_HTC_TARGET_FROM_HANDLE(hnd) ((struct htc_target *)(hnd))
142 #define HTC_RECYCLE_RX_PKT(target,p,e) \
144 if ((p)->PktInfo.AsRx.HTCRxFlags & HTC_RX_PKT_NO_RECYCLE) { \
145 HTC_PACKET_RESET_RX(pPacket); \
146 pPacket->Status = A_ECANCELED; \
147 (e)->EpCallBacks.EpRecv((e)->EpCallBacks.pContext, \
150 HTC_PACKET_RESET_RX(pPacket); \
151 HTCAddReceivePkt((HTC_HANDLE)(target),(p)); \
155 /* internal HTC functions */
156 void HTCControlTxComplete(void *Context, struct htc_packet *pPacket);
157 void HTCControlRecv(void *Context, struct htc_packet *pPacket);
158 int HTCWaitforControlMessage(struct htc_target *target, struct htc_packet **ppControlPacket);
159 struct htc_packet *HTCAllocControlBuffer(struct htc_target *target, struct htc_packet_queue *pList);
160 void HTCFreeControlBuffer(struct htc_target *target, struct htc_packet *pPacket, struct htc_packet_queue *pList);
161 int HTCIssueSend(struct htc_target *target, struct htc_packet *pPacket);
162 void HTCRecvCompleteHandler(void *Context, struct htc_packet *pPacket);
163 int HTCRecvMessagePendingHandler(void *Context, u32 MsgLookAheads[], int NumLookAheads, bool *pAsyncProc, int *pNumPktsFetched);
164 void HTCProcessCreditRpt(struct htc_target *target, HTC_CREDIT_REPORT *pRpt, int NumEntries, HTC_ENDPOINT_ID FromEndpoint);
165 int HTCSendSetupComplete(struct htc_target *target);
166 void HTCFlushRecvBuffers(struct htc_target *target);
167 void HTCFlushSendPkts(struct htc_target *target);
169 #ifdef ATH_DEBUG_MODULE
170 void DumpCreditDist(struct htc_endpoint_credit_dist *pEPDist);
171 void DumpCreditDistStates(struct htc_target *target);
172 void DebugDumpBytes(u8 *buffer, u16 length, char *pDescription);
175 static INLINE struct htc_packet *HTC_ALLOC_CONTROL_TX(struct htc_target *target) {
176 struct htc_packet *pPacket = HTCAllocControlBuffer(target,&target->ControlBufferTXFreeList);
177 if (pPacket != NULL) {
178 /* set payload pointer area with some headroom */
179 pPacket->pBuffer = pPacket->pBufferStart + HTC_HDR_LENGTH;
184 #define HTC_FREE_CONTROL_TX(t,p) HTCFreeControlBuffer((t),(p),&(t)->ControlBufferTXFreeList)
185 #define HTC_ALLOC_CONTROL_RX(t) HTCAllocControlBuffer((t),&(t)->ControlBufferRXFreeList)
186 #define HTC_FREE_CONTROL_RX(t,p) \
188 HTC_PACKET_RESET_RX(p); \
189 HTCFreeControlBuffer((t),(p),&(t)->ControlBufferRXFreeList); \
192 #define HTC_PREPARE_SEND_PKT(pP,sendflags,ctrl0,ctrl1) \
195 (pP)->pBuffer -= HTC_HDR_LENGTH; \
196 pHdrBuf = (pP)->pBuffer; \
197 A_SET_UINT16_FIELD(pHdrBuf,struct htc_frame_hdr,PayloadLen,(u16)(pP)->ActualLength); \
198 A_SET_UINT8_FIELD(pHdrBuf,struct htc_frame_hdr,Flags,(sendflags)); \
199 A_SET_UINT8_FIELD(pHdrBuf,struct htc_frame_hdr,EndpointID, (u8)(pP)->Endpoint); \
200 A_SET_UINT8_FIELD(pHdrBuf,struct htc_frame_hdr,ControlBytes[0], (u8)(ctrl0)); \
201 A_SET_UINT8_FIELD(pHdrBuf,struct htc_frame_hdr,ControlBytes[1], (u8)(ctrl1)); \
204 #define HTC_UNPREPARE_SEND_PKT(pP) \
205 (pP)->pBuffer += HTC_HDR_LENGTH; \
211 #endif /* _HTC_INTERNAL_H_ */