summaryrefslogtreecommitdiff
path: root/Core/EM/UsbRecovery/UsbBotPeimSrc/BotPeim.c
blob: 7ebdb8844bc020fd741d64491ced875d1fb55c16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2006, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**         5555 Oakbrook Pkwy, Suite 200, Norcross, GA 30093        **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************
//**********************************************************************
//
// $Header: /Alaska/SOURCE/Modules/USBRecovery/UsbBotPeimSrc/BotPeim.c 7     11/24/12 5:47a Ryanchou $
//
// $Revision: 7 $
//
// $Date: 11/24/12 5:47a $
//
//**********************************************************************
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Modules/USBRecovery/UsbBotPeimSrc/BotPeim.c $
// 
// 7     11/24/12 5:47a Ryanchou
// [TAG]  		EIP103990
// [Category]  	Improvement
// [Description]  	Synchronized with USB PEI module 4.6.3_USB_08.10.24.
// [Files]  		EhciPei.c, EhciPei.h, OhciPei.c, OhciPei.h, UhcPeim.c,
// BotPeim.c, BotPeim.h, PeiAtapi.c, UsbBotPeim.c, UsbBotPeim.h,
// HubPeim.c, UsbPeim.c, XhciPei.c, XhciPei.h, HubPeim.h, PeiUsbLib.c,
// PeiUsbLib.h, UsbPeim.h
// 
// 6     4/16/11 3:41a Ryanchou
// [TAG]  		EIP50311
// [Category]  	New Feature
// [Description]  	Multiple LUN device support added.
// [Files]  		BotPeim.c, UsbBotPeim.c, UsbBotPeim.h
// 
// 5     2/18/11 2:13a Ryanchou
// [TAG]  		EIP52191
// [Category]  	Bug Fix
// [Severity]  	Minor
// [Symptom]  	Recovery from card reader causes "EHCI Time-Out".
// [RootCause]  	The card reader returns zero length during BOT data
// transport, BIOS will do data transport again if the remain data length
// isn't zero, then CSW will be received in data transport phase.
// [Solution]  	Added the code that check if the transfered length is less
// than expect length, break the loop.
// [Files]  		BotPeim.c
// 
// 4     4/06/10 3:27p Fasihm
// EIP#31987 - Added the generic USBRecovery Fix in the module.
// 
// 3     7/10/08 6:38p Michaela
// Updated to support OHCI controllers
//
// 2     8/17/07 4:12p Ambikas
//
// 1     9/22/06 12:17p Sivagarn
// - Included Recovery code in Source
//
// 1     9/22/06 12:14p Sivagarn
// - Initial checkin
// - Included Recovery code in Source
//
//*****************************************************************************

//<AMI_FHDR_START>
//----------------------------------------------------------------------------
//
// Name:        BotPeim.C
//
// Description: This file belongs to "Framework".
//              This file is modified by AMI to include copyright message,
//              appropriate header and integration code.
//              This file contains generic routines needed for USB recovery
//              Mass Storage BOT PEIM
//
//----------------------------------------------------------------------------
//<AMI_FHDR_END>

/*++
   This file contains a 'Sample Driver' and is licensed as such
   under the terms of your license agreement with Intel or your
   vendor.  This file may be modified by the user, subject to
   the additional terms of the license agreement
   --*/

/*++

   Copyright (c)  1999 - 2002 Intel Corporation. All rights reserved
   This software and associated documentation (if any) is furnished
   under a license and may only be used or copied in accordance
   with the terms of the license. Except as permitted by such
   license, no part of this software or documentation may be
   reproduced, stored in a retrieval system, or transmitted in any
   form or by any means without the express written consent of
   Intel Corporation.


   Module Name:

   BotPeim.c

   Abstract:

   BOT Transportation implementation

   --*/

#include "UsbBotPeim.h"
#include "BotPeim.h"
#include "PeiUsbLib.h"

extern VOID ZeroMem (
    IN VOID  *Buffer,
    IN UINTN Size );
extern VOID PeiCopyMem (
    IN VOID  *Destination,
    IN VOID  *Source,
    IN UINTN Length );


STATIC
EFI_STATUS BotRecoveryReset (
    IN EFI_PEI_SERVICES **PeiServices,
    IN PEI_BOT_DEVICE   *PeiBotDev )
{
    EFI_USB_DEVICE_REQUEST DevReq;
    UINT32 Timeout;
    PEI_USB_IO_PPI         *UsbIoPpi;
    UINT8      EndpointAddr;
    EFI_STATUS Status;

    UsbIoPpi = PeiBotDev->UsbIoPpi;

    if (UsbIoPpi == NULL) {
        return EFI_INVALID_PARAMETER;
    }

    ZeroMem( &DevReq, sizeof(EFI_USB_DEVICE_REQUEST) );

    DevReq.RequestType = 0x21;
    DevReq.Request = 0xff;
    DevReq.Value = 0;
    DevReq.Index = 0;
    DevReq.Length = 0;

    Timeout = 3000;

    Status = UsbIoPpi->UsbControlTransfer(
        PeiServices,
        UsbIoPpi,
        &DevReq,
        EfiUsbNoData,
        Timeout,
        NULL,
        0
             );

    //
    // clear bulk in endpoint stall feature
    //
    EndpointAddr = (PeiBotDev->BulkInEndpoint)->EndpointAddress;
    PeiUsbClearEndpointHalt( PeiServices, UsbIoPpi, EndpointAddr );

    //
    // clear bulk out endpoint stall feature
    //
    EndpointAddr = (PeiBotDev->BulkOutEndpoint)->EndpointAddress;
    PeiUsbClearEndpointHalt( PeiServices, UsbIoPpi, EndpointAddr );

    return Status;
}


STATIC
EFI_STATUS BotCommandPhase (
    IN EFI_PEI_SERVICES       **PeiServices,
    IN PEI_BOT_DEVICE         *PeiBotDev,
    IN VOID                   *Command,
    IN UINT8                  CommandSize,
    IN UINT32                 DataTransferLength,
    IN EFI_USB_DATA_DIRECTION Direction,
    IN UINT16                 Timeout )
{
    CBW            cbw;
    EFI_STATUS     Status;
    PEI_USB_IO_PPI *UsbIoPpi;
    UINTN          DataSize;

    UsbIoPpi = PeiBotDev->UsbIoPpi;

    ZeroMem( &cbw, sizeof(CBW) );

    //
    // Fill the command block, detailed see BOT spec
    //
    cbw.dCBWSignature = CBWSIG;
    cbw.dCBWTag = 0x01;
    cbw.dCBWDataTransferLength = DataTransferLength;
    cbw.bmCBWFlags = (Direction == EfiUsbDataIn)? 0x80 : 0;
    cbw.bCBWLUN = PeiBotDev->Lun;
    cbw.bCBWCBLength = CommandSize;

    PeiCopyMem( cbw.CBWCB, Command, CommandSize );

    DataSize = sizeof(CBW);

    Status = UsbIoPpi->UsbBulkTransfer(
        PeiServices,
        UsbIoPpi,
        (PeiBotDev->BulkOutEndpoint)->EndpointAddress,
        (UINT8 *) &cbw,
        &DataSize,
        Timeout
             );
    if ( EFI_ERROR( Status ) ) {
        //
        // Command phase fail, we need to recovery reset this device
        //
        BotRecoveryReset( PeiServices, PeiBotDev );
        return EFI_DEVICE_ERROR;
    }

    return EFI_SUCCESS;
}


STATIC
EFI_STATUS BotDataPhase (
    IN EFI_PEI_SERVICES       **PeiServices,
    IN PEI_BOT_DEVICE         *PeiBotDev,
    IN UINT32                 *DataSize,
    IN OUT VOID               *DataBuffer,
    IN EFI_USB_DATA_DIRECTION Direction,
    IN UINT16                 Timeout )
{
    EFI_STATUS     Status;
    PEI_USB_IO_PPI *UsbIoPpi;
    UINT8  EndpointAddr;
    UINTN  Remain;
    UINTN  Increment;
    UINT32 MaxPacketLen;
    UINT8  *BufferPtr;
    UINTN  TransferredSize;
	UINTN  TransferSize;

    UsbIoPpi = PeiBotDev->UsbIoPpi;

    Remain = *DataSize;
    BufferPtr = (UINT8 *) DataBuffer;
    TransferredSize = 0;

    //
    // retrieve the the max packet length of the given endpoint
    //
    if (Direction == EfiUsbDataIn) {
        MaxPacketLen = (PeiBotDev->BulkInEndpoint)->MaxPacketSize;
        EndpointAddr = (PeiBotDev->BulkInEndpoint)->EndpointAddress;
    }
    else {
        MaxPacketLen = (PeiBotDev->BulkOutEndpoint)->MaxPacketSize;
        EndpointAddr = (PeiBotDev->BulkOutEndpoint)->EndpointAddress;
    }

    while (Remain > 0) {
        //
        // Using 15 packets to avoid Bitstuff error
        //
        if (Remain > 16 * MaxPacketLen) {
            TransferSize = 16 * MaxPacketLen;
        }
        else {
            TransferSize = Remain;
        }

		Increment = TransferSize;
	
        Status = UsbIoPpi->UsbBulkTransfer(
            PeiServices,
            UsbIoPpi,
            EndpointAddr,
            BufferPtr,
            &Increment,
            Timeout
                 );

        TransferredSize += Increment;

        if ( EFI_ERROR( Status ) ) {
            PeiUsbClearEndpointHalt( PeiServices, UsbIoPpi, EndpointAddr );
            return Status;
        }
	
		if (Increment < TransferSize) {
			break;
		}
		
        BufferPtr += Increment;
        Remain -= Increment;
    }

    *DataSize = (UINT32) TransferredSize;

    return EFI_SUCCESS;
}


STATIC
EFI_STATUS BotStatusPhase (
    IN EFI_PEI_SERVICES **PeiServices,
    IN PEI_BOT_DEVICE   *PeiBotDev,
    OUT UINT8           *TransferStatus,
    IN UINT16           Timeout )
{
    CSW            csw;
    EFI_STATUS     Status;
    PEI_USB_IO_PPI *UsbIoPpi;
    UINT8          EndpointAddr;
    UINTN          DataSize;

    //UINT32 Temp;

    UsbIoPpi = PeiBotDev->UsbIoPpi;

    ZeroMem( &csw, sizeof(CSW) );

    EndpointAddr = (PeiBotDev->BulkInEndpoint)->EndpointAddress;

    //  DataSize = sizeof(CSW);
    DataSize = 0x0d; //bala changed

    //
    // Get the status field from bulk transfer
    //
    Status = UsbIoPpi->UsbBulkTransfer(
        PeiServices,
        UsbIoPpi,
        EndpointAddr,
        &csw,
        &DataSize,
        Timeout
             );

    if ( EFI_ERROR( Status ) ) {
        return Status;
    }

    if (csw.dCSWSignature == CSWSIG)
    {
        *TransferStatus = csw.bCSWStatus;
    }
    else {
        return EFI_DEVICE_ERROR;
    }

    return EFI_SUCCESS;
}


EFI_STATUS PeiAtapiCommand (
    IN EFI_PEI_SERVICES       **PeiServices,
    IN PEI_BOT_DEVICE         *PeiBotDev,
    IN VOID                   *Command,
    IN UINT8                  CommandSize,
    IN VOID                   *DataBuffer,
    IN UINT32                 BufferLength,
    IN EFI_USB_DATA_DIRECTION Direction,
    IN UINT16                 TimeOutInMilliSeconds )

/*++

   Routine Description:
    Send ATAPI command using BOT protocol.

   Arguments:
    This                  - Protocol instance pointer.
    Command               - Command buffer
    CommandSize           - Size of Command Buffer
    DataBuffer            - Data buffer
    BufferLength          - Length of Data buffer
    Direction             - Data direction of this command
    TimeoutInMilliseconds - Timeout value in ms

   Returns:
    EFI_SUCCES          - Commond succeeded.
    EFI_DEVICE_ERROR    - Command failed.

   --*/
{
    EFI_STATUS Status;
    EFI_STATUS BotDataStatus = EFI_SUCCESS;
    UINT8      TransferStatus;
    UINT32     BufferSize;
    UINT8      *Tmp;
    UINT32     Temp;

    Tmp = (UINT8 *) Command;
    Temp = Tmp[0];
    PEI_TRACE( (EFI_D_ERROR, PeiServices, "Executing ScsiCmd(%x)\n", Temp) );
    //
    // First send ATAPI command through Bot
    //
    Status = BotCommandPhase(
        PeiServices,
        PeiBotDev,
        Command,
        CommandSize,
        BufferLength,
        Direction,
        TimeOutInMilliSeconds
             );

    if ( EFI_ERROR( Status ) ) {
        return EFI_DEVICE_ERROR;
    }

    //
    // Send/Get Data if there is a Data Stage
    //
    switch (Direction)
    {
    case EfiUsbDataIn:
    case EfiUsbDataOut:
        BufferSize = BufferLength;

        BotDataStatus = BotDataPhase(
            PeiServices,
            PeiBotDev,
            &BufferSize,
            DataBuffer,
            Direction,
            TimeOutInMilliSeconds
                        );

        break;

    case EfiUsbNoData:
        break;
    }

    //
    // Status Phase
    //

    Status = BotStatusPhase(
        PeiServices,
        PeiBotDev,
        &TransferStatus,
        TimeOutInMilliSeconds
             );
    if ( EFI_ERROR( Status ) ) {
        BotRecoveryReset( PeiServices, PeiBotDev );
        return EFI_DEVICE_ERROR;
    }

    if (TransferStatus == 0x01) {
        return EFI_DEVICE_ERROR;
    }

    return BotDataStatus;
}


//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2006, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**         5555 Oakbrook Pkwy, Suite 200, Norcross, GA 30093        **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************
//**********************************************************************