1 //------------------------------------------------------------------------------
2 // <copyright file="htc_internal.h" company="Atheros">
3 //    Copyright (c) 2007-2010 Atheros Corporation.  All rights reserved.
4 //
5 //
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.
9 //
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.
17 //
18 //
19 //------------------------------------------------------------------------------
20 //==============================================================================
21 // Author(s): ="Atheros"
22 //==============================================================================
23 #ifndef _HTC_INTERNAL_H_
24 #define _HTC_INTERNAL_H_
25 
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
29 
30 //#define HTC_EP_STAT_PROFILING
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif /* __cplusplus */
35 
36 /* Header files */
37 
38 #include "a_config.h"
39 #include "athdefs.h"
40 #include "a_types.h"
41 #include "a_osapi.h"
42 #include "htc_debug.h"
43 #include "htc.h"
44 #include "htc_api.h"
45 #include "bmi_msg.h"
46 #include "hif.h"
47 #include "AR6000/ar6k.h"
48 
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
53 
54 #define HTC_HOST_MAX_MSG_PER_BUNDLE        8
55 #define HTC_MIN_HTC_MSGS_TO_BUNDLE         2
56 
57 /* packet flags */
58 
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)
63 
64 /* scatter request flags */
65 
66 #define HTC_SCATTER_REQ_FLAGS_PARTIAL_BUNDLE  (1 << 0)
67 
68 struct htc_endpoint {
69     HTC_ENDPOINT_ID             Id;
70     HTC_SERVICE_ID              ServiceID;      /* service ID this endpoint is bound to
71                                                    non-zero value means this endpoint is in use */
72     struct htc_packet_queue            TxQueue;        /* HTC frame buffer TX queue */
73     struct 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     struct 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 */
87 #endif
88 };
89 
90 #ifdef HTC_EP_STAT_PROFILING
91 #define INC_HTC_EP_STAT(p,stat,count) (p)->EndPointStats.stat += (count);
92 #else
93 #define INC_HTC_EP_STAT(p,stat,count)
94 #endif
95 
96 #define HTC_SERVICE_TX_PACKET_TAG  HTC_TX_PACKET_TAG_INTERNAL
97 
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)
101 
102 struct htc_control_buffer {
103     struct htc_packet    HtcPacket;
104     u8 *Buffer;
105 };
106 
107 #define HTC_RECV_WAIT_BUFFERS        (1 << 0)
108 #define HTC_OP_STATE_STOPPING        (1 << 0)
109 
110 /* our HTC target state */
111 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     struct htc_packet_queue            ControlBufferTXFreeList;
116     struct htc_packet_queue            ControlBufferRXFreeList;
117     HTC_CREDIT_DIST_CALLBACK    DistributeCredits;
118     HTC_CREDIT_INIT_CALLBACK    InitCredits;
119     void                       *pCredDistContext;
120     int                         TargetCredits;
121     unsigned int                TargetCreditSize;
122     A_MUTEX_T                   HTCLock;
123     A_MUTEX_T                   HTCRxLock;
124     A_MUTEX_T                   HTCTxLock;
125     struct ar6k_device                 Device;         /* AR6K - specific state */
126     u32 OpStateFlags;
127     u32 RecvStateFlags;
128     HTC_ENDPOINT_ID             EpWaitingForBuffers;
129     bool                      TargetFailure;
130 #ifdef HTC_CAPTURE_LAST_FRAME
131     struct htc_frame_hdr               LastFrameHdr;  /* useful for debugging */
132     u8 LastTrailer[256];
133     u8 LastTrailerLength;
134 #endif
135     struct htc_init_info               HTCInitInfo;
136     u8 HTCTargetVersion;
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) */
140 };
141 
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);
149 
150 #define GET_HTC_TARGET_FROM_HANDLE(hnd) ((struct htc_target *)(hnd))
151 #define HTC_RECYCLE_RX_PKT(target,p,e)                           \
152 {                                                                \
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,      \
157                                  (p));                           \
158     } else {                                                     \
159         HTC_PACKET_RESET_RX(pPacket);                            \
160         HTCAddReceivePkt((HTC_HANDLE)(target),(p));              \
161     }                                                            \
162 }
163 
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(struct htc_target *target, struct htc_packet **ppControlPacket);
168 struct htc_packet *HTCAllocControlBuffer(struct htc_target *target, struct htc_packet_queue *pList);
169 void        HTCFreeControlBuffer(struct htc_target *target, struct htc_packet *pPacket, struct htc_packet_queue *pList);
170 int    HTCIssueSend(struct 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(struct htc_target *target, HTC_CREDIT_REPORT *pRpt, int NumEntries, HTC_ENDPOINT_ID FromEndpoint);
174 int    HTCSendSetupComplete(struct htc_target *target);
175 void        HTCFlushRecvBuffers(struct htc_target *target);
176 void        HTCFlushSendPkts(struct htc_target *target);
177 
178 #ifdef ATH_DEBUG_MODULE
179 void        DumpCreditDist(struct htc_endpoint_credit_dist *pEPDist);
180 void        DumpCreditDistStates(struct htc_target *target);
181 void 		DebugDumpBytes(u8 *buffer, u16 length, char *pDescription);
182 #endif
183 
HTC_ALLOC_CONTROL_TX(struct htc_target * target)184 static INLINE struct htc_packet *HTC_ALLOC_CONTROL_TX(struct 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;
189     }
190     return pPacket;
191 }
192 
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) \
196 {                                                                \
197     HTC_PACKET_RESET_RX(p);                                      \
198     HTCFreeControlBuffer((t),(p),&(t)->ControlBufferRXFreeList); \
199 }
200 
201 #define HTC_PREPARE_SEND_PKT(pP,sendflags,ctrl0,ctrl1)       \
202 {                                                   \
203     u8 *pHdrBuf;                               \
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));   \
211 }
212 
213 #define HTC_UNPREPARE_SEND_PKT(pP)     \
214     (pP)->pBuffer += HTC_HDR_LENGTH;   \
215 
216 #ifdef __cplusplus
217 }
218 #endif
219 
220 #endif /* _HTC_INTERNAL_H_ */
221