33
|
1 |
// Copyright (c) 1999-2010 Nokia Corporation and/or its subsidiary(-ies).
|
|
2 |
// All rights reserved.
|
|
3 |
// This component and the accompanying materials are made available
|
|
4 |
// under the terms of "Eclipse Public License v1.0"
|
|
5 |
// which accompanies this distribution, and is available
|
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
7 |
//
|
|
8 |
// Initial Contributors:
|
|
9 |
// Nokia Corporation - initial contribution.
|
|
10 |
//
|
|
11 |
// Contributors:
|
|
12 |
//
|
|
13 |
// Description:
|
|
14 |
//
|
|
15 |
|
|
16 |
#include "hostmbufpool.h"
|
|
17 |
|
|
18 |
#include <bluetooth/hcicommandqueue.h>
|
|
19 |
#include <bluetooth/hci/hostnumberofcompletedpacketscommand.h>
|
|
20 |
#include <bluetooth/hci/hcievents.h>
|
|
21 |
#include <bluetooth/hci/commandcompleteevent.h>
|
|
22 |
#include <bluetooth/hci/hciutil.h>
|
48
|
23 |
#include <bluetooth/hci/hciconsts.h>
|
33
|
24 |
|
|
25 |
#include "linkconsts.h"
|
|
26 |
#include "linkutil.h"
|
|
27 |
|
|
28 |
#include <bluetooth/logger.h>
|
|
29 |
|
|
30 |
#ifdef HOSTCONTROLLER_TO_HOST_FLOW_CONTROL
|
|
31 |
|
|
32 |
#ifdef __FLOG_ACTIVE
|
|
33 |
_LIT8(KLogComponent, LOG_COMPONENT_LINKMGR);
|
|
34 |
#endif
|
|
35 |
|
|
36 |
__DEBUG_ONLY(PANICCATEGORY("mbufpool");)
|
|
37 |
|
|
38 |
|
|
39 |
CHostMBufPool* CHostMBufPool::NewL(MHCICommandQueue& aCommandQueue)
|
|
40 |
{
|
|
41 |
LOG_STATIC_FUNC
|
|
42 |
CHostMBufPool* self = new (ELeave) CHostMBufPool(aCommandQueue);
|
|
43 |
CleanupStack::PushL(self);
|
|
44 |
self->ConstructL();
|
|
45 |
CleanupStack::Pop(self);
|
|
46 |
return self;
|
|
47 |
}
|
|
48 |
|
|
49 |
void CHostMBufPool::DeletePool(TSglQue<TPoolBuffer>& aQueue)
|
|
50 |
{
|
|
51 |
LOG_FUNC
|
|
52 |
TPoolBuffer* buffer = NULL;
|
|
53 |
TSglQueIter<TPoolBuffer> iter(aQueue);
|
|
54 |
while(buffer=iter++, buffer)
|
|
55 |
{
|
|
56 |
aQueue.Remove(*buffer);
|
|
57 |
DeleteBuffer(buffer);
|
|
58 |
}
|
|
59 |
}
|
|
60 |
|
|
61 |
void CHostMBufPool::DeleteBuffer(TPoolBuffer*& aBuffer)
|
|
62 |
{
|
|
63 |
LOG_FUNC
|
|
64 |
if(aBuffer)
|
|
65 |
{
|
|
66 |
aBuffer->iMBufChain.Free();
|
|
67 |
delete aBuffer;
|
|
68 |
aBuffer = NULL;
|
|
69 |
}
|
|
70 |
}
|
|
71 |
|
|
72 |
CHostMBufPool::~CHostMBufPool()
|
|
73 |
{
|
|
74 |
LOG_FUNC
|
|
75 |
Cancel();
|
|
76 |
// iMBufRequester is cleaned in it's destructor (not much of an R-class...but it is what it is)
|
|
77 |
DeletePool(iBufferPool);
|
|
78 |
DeletePool(iWaitingAllocPool);
|
|
79 |
DeleteBuffer(iBufferBeingAllocd);
|
|
80 |
}
|
|
81 |
|
|
82 |
CHostMBufPool::CHostMBufPool(MHCICommandQueue& aCommandQueue)
|
|
83 |
: CActive(EPriorityHigh)
|
|
84 |
// High priority so that buffers are allocated occur before more data is read, this prevents
|
|
85 |
// the cases the data floods the device and exhausts the buffers before any more can be allocated.
|
|
86 |
// This maximises throughput since we will ensure we send packet completion notifications in a
|
|
87 |
// timely manner.
|
|
88 |
, iCmdQ(aCommandQueue)
|
|
89 |
, iBufferPool(_FOFF(TPoolBuffer,iLink))
|
|
90 |
, iWaitingAllocPool(_FOFF(TPoolBuffer,iLink))
|
|
91 |
, iCurrAckHandle(KErrNotFound)
|
|
92 |
{
|
|
93 |
LOG_FUNC
|
|
94 |
}
|
|
95 |
|
|
96 |
void CHostMBufPool::ConstructL()
|
|
97 |
/**
|
|
98 |
2nd phase constructor for the Host MBuf Pool.
|
|
99 |
|
|
100 |
This method will attempt to reserve enough MBufs from the global pool
|
|
101 |
for bluetooth use.
|
|
102 |
@leave KErrNoMemory If the required number of MBufs couldn't be reserved
|
|
103 |
*/
|
|
104 |
{
|
|
105 |
LOG_FUNC
|
|
106 |
LOG2(_L("CHostMBufPool: now reserving %d size %d MBufChains"), KStackACLBuffersNum, KLinkMgrIncomingBufferSize);
|
|
107 |
|
|
108 |
for (TInt i=0; i<=KStackACLBuffersNum-1; i++)
|
|
109 |
{
|
|
110 |
TPoolBuffer* thisBuffer = CreatePoolBufferL();
|
|
111 |
AddToBufferPool(*thisBuffer);
|
|
112 |
}
|
|
113 |
|
|
114 |
CActiveScheduler::Add(this);
|
|
115 |
}
|
|
116 |
|
|
117 |
CHostMBufPool::TPoolBuffer* CHostMBufPool::CreatePoolBufferL()
|
|
118 |
{
|
|
119 |
LOG_FUNC
|
|
120 |
TPoolBuffer* newBuffer = new(ELeave) TPoolBuffer();
|
|
121 |
CleanupStack::PushL(newBuffer);
|
|
122 |
newBuffer->iCurrentHandle = KInvalidConnectionHandle; // we assert on this later
|
|
123 |
newBuffer->iMBufChain.AllocL(KLinkMgrIncomingBufferSize);
|
|
124 |
CleanupStack::Pop(newBuffer);
|
|
125 |
return newBuffer;
|
|
126 |
}
|
|
127 |
|
|
128 |
void CHostMBufPool::DoCancel()
|
|
129 |
{
|
|
130 |
LOG_FUNC
|
|
131 |
iMBufRequester.Cancel();
|
|
132 |
}
|
|
133 |
|
|
134 |
RMBufChain CHostMBufPool::TakeBufferL(THCIConnHandle aConnHandle)
|
|
135 |
/**
|
|
136 |
Takes a buffer from the pool and schedules an asynchronous allocation
|
|
137 |
of the next buffer. Only when that allocation has succeeded will the host
|
|
138 |
controller be signalled with a host_number_of_completed_packets. Hence,
|
|
139 |
if we cannot allocate a buffer from the global MBuf pool, the host controller
|
|
140 |
will be flowed off and no data will be lost.
|
|
141 |
*/
|
|
142 |
{
|
|
143 |
LOG_FUNC
|
|
144 |
ASSERT_DEBUG(aConnHandle != KInvalidConnectionHandle);
|
|
145 |
|
|
146 |
// Speculatively attempt to allocate any queued allocations that may have previously failed.
|
|
147 |
TryToAllocQueuedBuffer();
|
|
148 |
|
|
149 |
TPoolBuffer* ready = iBufferPool.First();
|
|
150 |
|
|
151 |
if(!ready)
|
|
152 |
{
|
|
153 |
// Whoops run out of buffers - even though we were trying to prevent this with
|
|
154 |
// flow control, in the case of disconnection the controller will assume all the
|
|
155 |
// data for a connection handle will be flushed and therefore the buffers associated
|
|
156 |
// with that connection handle will be free. Unfortunately for us we don't have
|
|
157 |
// that much control with the MBuf pool (since flow control is for asynchronous
|
|
158 |
// buffer allocation rather than waiting for the given MBufs to be relinquished
|
|
159 |
// by a higher layer).
|
|
160 |
// So the controller could think we have more buffers than we actually have...
|
|
161 |
LOG(_L8("CHostMBufPool: Ran out of buffers!!!!"));
|
|
162 |
LEAVEL(KErrOverflow);
|
|
163 |
}
|
|
164 |
|
|
165 |
// If here then we should have a valid pool buffer to use
|
|
166 |
__ASSERT_DEBUG(!ready->iMBufChain.IsEmpty(), Panic(ELinkMgrHostControllerHasOverflowedHost));
|
|
167 |
__ASSERT_DEBUG(ready->iCurrentHandle == KInvalidConnectionHandle, Panic(ELinkMgrHostControllerHasOverflowedHost));
|
|
168 |
|
|
169 |
RemoveFromBufferPool(*ready);
|
|
170 |
ready->iCurrentHandle = aConnHandle;
|
|
171 |
|
|
172 |
RMBufChain retChain;
|
|
173 |
retChain.Assign(ready->iMBufChain);
|
|
174 |
|
|
175 |
if (IsActive())
|
|
176 |
{
|
|
177 |
//This buffer will be reclaimed from the global pool
|
|
178 |
//after the one(s) we're currently trying to reclaim
|
|
179 |
LOG(_L8("CHostMBufPool: TakeBuffer, buffer taken while alloc outstanding: queued alloc"));
|
|
180 |
iWaitingAllocPool.AddLast(*ready);
|
|
181 |
}
|
|
182 |
else
|
|
183 |
{
|
|
184 |
LOG(_L8("CHostMBufPool: TakeBuffer, buffer taken"));
|
|
185 |
AllocNewBuffer(*ready);
|
|
186 |
}
|
|
187 |
|
|
188 |
return retChain;
|
|
189 |
}
|
|
190 |
|
|
191 |
void CHostMBufPool::InvalidateByConnH(THCIConnHandle aConnHandle)
|
|
192 |
{
|
|
193 |
LOG_FUNC
|
|
194 |
ASSERT_DEBUG(aConnHandle != KInvalidConnectionHandle);
|
|
195 |
|
|
196 |
// We need to scan through the two pending "lists" to remove the
|
|
197 |
// connection handle from record so we don't try to provide a
|
|
198 |
// packet completion notification (the controller already assumes
|
|
199 |
// the buffers are free as they are entitled to by the spec).
|
|
200 |
|
|
201 |
// The current buffer being allocated
|
|
202 |
if(iBufferBeingAllocd && iBufferBeingAllocd->iCurrentHandle == aConnHandle)
|
|
203 |
{
|
|
204 |
iBufferBeingAllocd->iCurrentHandle = KInvalidConnectionHandle;
|
|
205 |
}
|
|
206 |
|
|
207 |
// The list of buffers waiting to be allocted
|
|
208 |
TPoolBuffer* buffer = NULL;
|
|
209 |
TSglQueIter<TPoolBuffer> iter(iWaitingAllocPool);
|
|
210 |
while(buffer=iter++, buffer)
|
|
211 |
{
|
|
212 |
if(buffer->iCurrentHandle == aConnHandle)
|
|
213 |
{
|
|
214 |
buffer->iCurrentHandle = KInvalidConnectionHandle;
|
|
215 |
}
|
|
216 |
}
|
|
217 |
|
|
218 |
// Finally we need to purge any batched up completions if they
|
|
219 |
// are for this connection handle
|
|
220 |
if(iCurrAckHandle == aConnHandle)
|
|
221 |
{
|
|
222 |
iCurrAckHandle = KErrNotFound;
|
|
223 |
iCurrCompletedPackets = 0;
|
|
224 |
}
|
|
225 |
}
|
|
226 |
|
|
227 |
void CHostMBufPool::RunL()
|
|
228 |
{
|
|
229 |
LOG_FUNC
|
|
230 |
LEAVEIFERRORL(iStatus.Int());
|
|
231 |
|
|
232 |
// We've successfully allocated a new MBufChain
|
|
233 |
TPoolBuffer* justAllocd = iBufferBeingAllocd;
|
|
234 |
iBufferBeingAllocd = NULL;
|
|
235 |
THCIConnHandle justAllocdHandle = justAllocd->iCurrentHandle;
|
|
236 |
|
|
237 |
// Return buffer to pool for re-use
|
|
238 |
AddToBufferPool(*justAllocd);
|
|
239 |
justAllocd->iCurrentHandle = KInvalidConnectionHandle;
|
|
240 |
|
|
241 |
// If connection handle is still valid then we need to provide a completion
|
|
242 |
// notification for the packet to the connection handle it was from.
|
|
243 |
if(justAllocdHandle != KInvalidConnectionHandle)
|
|
244 |
{
|
|
245 |
if (iCurrAckHandle == KErrNotFound)
|
|
246 |
{
|
|
247 |
// This is the first completion we have seen
|
|
248 |
iCurrAckHandle = justAllocdHandle;
|
|
249 |
}
|
|
250 |
ASSERT_DEBUG(iCurrAckHandle != KInvalidConnectionHandle); // just to be sure
|
|
251 |
|
|
252 |
TBool ackNow = (justAllocdHandle != iCurrAckHandle);
|
|
253 |
|
|
254 |
if (!ackNow)
|
|
255 |
{
|
|
256 |
iCurrCompletedPackets++;
|
|
257 |
LOG2(_L8("CHostMBufPool: CompletedPackets++ for conn: %d [->%d]"), justAllocdHandle, iCurrCompletedPackets);
|
|
258 |
ackNow = (iCurrCompletedPackets >= KStackACLBuffersTideMarkNum);
|
|
259 |
}
|
|
260 |
|
|
261 |
if (ackNow)
|
|
262 |
{
|
|
263 |
TInt err = KErrNone;
|
|
264 |
|
|
265 |
if (iCurrCompletedPackets > 0)
|
|
266 |
{
|
|
267 |
LOG2(_L8("CHostMBufPool: Sending HostNumberOfCompletedPackets for conn: %d [%d completed]"), iCurrAckHandle, iCurrCompletedPackets);
|
|
268 |
//Acknowledge the completed packets
|
|
269 |
TRAP(err, HostNumberOfCompletedPacketsL(iCurrAckHandle, iCurrCompletedPackets));
|
|
270 |
//if this failed we probably couldn't alloc the memory for the command frame,
|
|
271 |
//the HC is still flowed off.
|
|
272 |
__ASSERT_DEBUG(err == KErrNone, Panic(ELinkMgrCouldNotSendHostNumberOfCompletedPackets));
|
|
273 |
LEAVEIFERRORL(err);
|
|
274 |
}
|
|
275 |
|
|
276 |
iCurrCompletedPackets = (justAllocdHandle != iCurrAckHandle) ? 1 : 0;
|
|
277 |
iCurrAckHandle = justAllocdHandle;
|
|
278 |
}
|
|
279 |
}
|
|
280 |
|
|
281 |
TryToAllocQueuedBuffer();
|
|
282 |
}
|
|
283 |
|
|
284 |
void CHostMBufPool::TryToAllocQueuedBuffer()
|
|
285 |
{
|
|
286 |
LOG_FUNC
|
|
287 |
if (!iWaitingAllocPool.IsEmpty() && !IsActive())
|
|
288 |
{
|
|
289 |
TPoolBuffer* needsAlloc = iWaitingAllocPool.First();
|
|
290 |
iWaitingAllocPool.Remove(*needsAlloc);
|
|
291 |
AllocNewBuffer(*needsAlloc);
|
|
292 |
}
|
|
293 |
}
|
|
294 |
|
|
295 |
void CHostMBufPool::AllocNewBuffer(TPoolBuffer& aBuffer)
|
|
296 |
{
|
|
297 |
LOG_FUNC
|
|
298 |
ASSERT_DEBUG(!iBufferBeingAllocd);
|
|
299 |
iBufferBeingAllocd = &aBuffer;
|
|
300 |
iMBufRequester.Alloc(aBuffer.iMBufChain, KLinkMgrIncomingBufferSize, iStatus);
|
|
301 |
SetActive();
|
|
302 |
}
|
|
303 |
|
|
304 |
void CHostMBufPool::HostNumberOfCompletedPacketsL(THCIConnHandle aConnH, TUint16 aNumPackets)
|
|
305 |
{
|
|
306 |
RArray<THCIConnectionHandle> connHandles;
|
|
307 |
connHandles.AppendL(aConnH);
|
|
308 |
CleanupClosePushL(connHandles);
|
|
309 |
|
|
310 |
RArray<THCINumOfCompletedPackets> numPackets;
|
|
311 |
numPackets.AppendL(aNumPackets);
|
|
312 |
CleanupClosePushL(numPackets);
|
|
313 |
|
|
314 |
CHostNumberOfCompletedPacketsCommand* cmd = CHostNumberOfCompletedPacketsCommand::NewL(1, connHandles, numPackets);
|
|
315 |
// Ownership of the arrays is taken by the command object.
|
|
316 |
CleanupStack::Pop(2, &connHandles); // &numPackets, &connHandles
|
|
317 |
|
|
318 |
// This is a priority command as we want to try to get this out as soon as possible (and not wait
|
|
319 |
// for all normal control aspects to be processed). This command shouldn't normally consume any credits
|
|
320 |
// so as a priority command it has little impact.
|
|
321 |
// Ownership of cmd transfered even if MhcqAddPriorityCommandL leaves
|
|
322 |
iCmdQ.MhcqAddPriorityCommandL(cmd, *this);
|
|
323 |
}
|
|
324 |
|
|
325 |
void CHostMBufPool::MhcqcCommandEventReceived(const THCIEventBase& aEvent, const CHCICommandBase* /*aRelatedCommand*/)
|
|
326 |
{
|
|
327 |
LOG_FUNC
|
|
328 |
// We don't expect a non-error event back because we're only sending Host_Number_of_Completed_Packet commands
|
|
329 |
if(aEvent.EventCode() == ECommandCompleteEvent)
|
|
330 |
{
|
|
331 |
const THCICommandCompleteEvent& completeEvent = THCICommandCompleteEvent::Cast(aEvent);
|
|
332 |
if(completeEvent.CommandOpcode() == KHostNumberOfCompletedPacketsOpcode)
|
|
333 |
{
|
|
334 |
// a regular error for a Host_Number_Of_Completed_Packets command
|
|
335 |
TInt err = CHciUtil::SymbianErrorCode(completeEvent.ErrorCode());
|
|
336 |
if(err != KErrNone) // we shouldn't get a non-erroring event back, but just in case
|
|
337 |
{
|
|
338 |
Error(err);
|
|
339 |
}
|
|
340 |
}
|
|
341 |
else // an unexpected command complete event
|
|
342 |
{
|
|
343 |
LOG1(_L8("CHostMBufPool: Unexpected HCI command complete event; opcode = 0x%04x"), completeEvent.CommandOpcode());
|
|
344 |
DEBUG_PANIC_LINENUM;
|
|
345 |
}
|
|
346 |
}
|
|
347 |
else // some unexpected event
|
|
348 |
{
|
|
349 |
LOG1(_L8("CHostMBufPool: Unexpected HCI event received; code = 0x%02x"), aEvent.EventCode());
|
|
350 |
DEBUG_PANIC_LINENUM;
|
|
351 |
}
|
|
352 |
}
|
|
353 |
|
|
354 |
void CHostMBufPool::MhcqcCommandErrored(TInt aErrorCode, const CHCICommandBase* /*aCommand*/)
|
|
355 |
{
|
|
356 |
LOG_FUNC
|
|
357 |
Error(aErrorCode);
|
|
358 |
}
|
|
359 |
|
|
360 |
TInt CHostMBufPool::RunError(TInt aError)
|
|
361 |
{
|
|
362 |
LOG_FUNC
|
|
363 |
if(iBufferBeingAllocd)
|
|
364 |
{
|
|
365 |
TPoolBuffer* justFailedToAlloc = iBufferBeingAllocd;
|
|
366 |
iBufferBeingAllocd = NULL;
|
|
367 |
// Add to wait for alloc queue - we may get another chance
|
|
368 |
iWaitingAllocPool.AddFirst(*justFailedToAlloc);
|
|
369 |
}
|
|
370 |
Error(aError);
|
|
371 |
return KErrNone;
|
|
372 |
}
|
|
373 |
|
|
374 |
void CHostMBufPool::Error(TInt IF_FLOGGING(aError))
|
|
375 |
{
|
|
376 |
LOG_FUNC
|
|
377 |
// So there has been some internal error when handling controller to host
|
|
378 |
// flow control. Tough, we've done our best for now - the only real thing
|
|
379 |
// that might be worth doing is a hard reset to start-up clean.
|
|
380 |
LOG1(_L8("CHostMBufPool: ERROR (%d) - inbound data to the stack may stall soon"), aError);
|
|
381 |
}
|
|
382 |
|
|
383 |
#endif
|