1 /** @file
2 
3   Copyright (c) 2014 - 2019, Intel Corporation. All rights reserved.<BR>
4   Copyright (c) Microsoft Corporation.<BR>
5   SPDX-License-Identifier: BSD-2-Clause-Patent
6 
7 **/
8 
9 #ifndef _UFS_PASS_THRU_H_
10 #define _UFS_PASS_THRU_H_
11 
12 #include <Uefi.h>
13 
14 #include <Protocol/ScsiPassThruExt.h>
15 #include <Protocol/UfsDeviceConfig.h>
16 #include <Protocol/UfsHostController.h>
17 #include <Protocol/UfsHostControllerPlatform.h>
18 
19 #include <Library/DebugLib.h>
20 #include <Library/UefiDriverEntryPoint.h>
21 #include <Library/BaseLib.h>
22 #include <Library/UefiLib.h>
23 #include <Library/BaseMemoryLib.h>
24 #include <Library/MemoryAllocationLib.h>
25 #include <Library/UefiBootServicesTableLib.h>
26 #include <Library/DevicePathLib.h>
27 #include <Library/TimerLib.h>
28 
29 #include "UfsPassThruHci.h"
30 
31 #define UFS_PASS_THRU_SIG           SIGNATURE_32 ('U', 'F', 'S', 'P')
32 
33 //
34 // Lun 0~7 is for 8 common luns.
35 // Lun 8~11 is for those 4 well known luns (Refer to UFS 2.0 spec Table 10.58 for details):
36 //  Lun 8:  REPORT LUNS
37 //  Lun 9:  UFS DEVICE
38 //  Lun 10: BOOT
39 //  Lun 11: RPMB
40 //
41 #define UFS_MAX_LUNS                12
42 #define UFS_WLUN_PREFIX             0xC1
43 
44 typedef struct {
45   UINT8    Lun[UFS_MAX_LUNS];
46   UINT16   BitMask:12;              // Bit 0~7 is 1/1 mapping to common luns. Bit 8~11 is 1/1 mapping to well-known luns.
47   UINT16   Rsvd:4;
48 } UFS_EXPOSED_LUNS;
49 
50 typedef struct _UFS_PASS_THRU_PRIVATE_DATA {
51   UINT32                              Signature;
52   EFI_HANDLE                          Handle;
53   EFI_EXT_SCSI_PASS_THRU_MODE         ExtScsiPassThruMode;
54   EFI_EXT_SCSI_PASS_THRU_PROTOCOL     ExtScsiPassThru;
55   EFI_UFS_DEVICE_CONFIG_PROTOCOL      UfsDevConfig;
56   EDKII_UFS_HOST_CONTROLLER_PROTOCOL  *UfsHostController;
57   UINTN                               UfsHcBase;
58   EDKII_UFS_HC_INFO                   UfsHcInfo;
59   EDKII_UFS_HC_DRIVER_INTERFACE       UfsHcDriverInterface;
60 
61   UINT8                               TaskTag;
62 
63   VOID                                *UtpTrlBase;
64   UINT8                               Nutrs;
65   VOID                                *TrlMapping;
66   VOID                                *UtpTmrlBase;
67   UINT8                               Nutmrs;
68   VOID                                *TmrlMapping;
69 
70   UFS_EXPOSED_LUNS                    Luns;
71 
72   //
73   // For Non-blocking operation.
74   //
75   EFI_EVENT                           TimerEvent;
76   LIST_ENTRY                          Queue;
77 } UFS_PASS_THRU_PRIVATE_DATA;
78 
79 #define UFS_PASS_THRU_TRANS_REQ_SIG   SIGNATURE_32 ('U', 'F', 'S', 'T')
80 
81 typedef struct {
82   UINT32                                        Signature;
83   LIST_ENTRY                                    TransferList;
84 
85   UINT8                                         Slot;
86   UTP_TRD                                       *Trd;
87   UINT32                                        CmdDescSize;
88   VOID                                          *CmdDescHost;
89   VOID                                          *CmdDescMapping;
90   VOID                                          *AlignedDataBuf;
91   UINTN                                         AlignedDataBufSize;
92   VOID                                          *DataBufMapping;
93 
94   EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET    *Packet;
95   UINT64                                        TimeoutRemain;
96   EFI_EVENT                                     CallerEvent;
97 } UFS_PASS_THRU_TRANS_REQ;
98 
99 #define UFS_PASS_THRU_TRANS_REQ_FROM_THIS(a) \
100     CR(a, UFS_PASS_THRU_TRANS_REQ, TransferList, UFS_PASS_THRU_TRANS_REQ_SIG)
101 
102 #define UFS_TIMEOUT                   EFI_TIMER_PERIOD_SECONDS(3)
103 #define UFS_HC_ASYNC_TIMER            EFI_TIMER_PERIOD_MILLISECONDS(1)
104 
105 #define ROUNDUP8(x) (((x) % 8 == 0) ? (x) : ((x) / 8 + 1) * 8)
106 
107 #define IS_ALIGNED(addr, size)        (((UINTN) (addr) & (size - 1)) == 0)
108 
109 #define UFS_PASS_THRU_PRIVATE_DATA_FROM_THIS(a) \
110   CR (a, \
111       UFS_PASS_THRU_PRIVATE_DATA, \
112       ExtScsiPassThru, \
113       UFS_PASS_THRU_SIG \
114       )
115 
116 #define UFS_PASS_THRU_PRIVATE_DATA_FROM_DEV_CONFIG(a) \
117   CR (a, \
118       UFS_PASS_THRU_PRIVATE_DATA, \
119       UfsDevConfig, \
120       UFS_PASS_THRU_SIG \
121       )
122 
123 #define UFS_PASS_THRU_PRIVATE_DATA_FROM_DRIVER_INTF(a) \
124   CR (a, \
125       UFS_PASS_THRU_PRIVATE_DATA, \
126       UfsHcDriverInterface, \
127       UFS_PASS_THRU_SIG \
128       )
129 
130 typedef struct _UFS_DEVICE_MANAGEMENT_REQUEST_PACKET {
131   UINT64           Timeout;
132   VOID             *DataBuffer;
133   UINT8            Opcode;
134   UINT8            DescId;
135   UINT8            Index;
136   UINT8            Selector;
137   UINT32           TransferLength;
138   UINT8            DataDirection;
139 } UFS_DEVICE_MANAGEMENT_REQUEST_PACKET;
140 
141 //
142 // function prototype
143 //
144 /**
145   Tests to see if this driver supports a given controller. If a child device is provided,
146   it further tests to see if this driver supports creating a handle for the specified child device.
147 
148   This function checks to see if the driver specified by This supports the device specified by
149   ControllerHandle. Drivers will typically use the device path attached to
150   ControllerHandle and/or the services from the bus I/O abstraction attached to
151   ControllerHandle to determine if the driver supports ControllerHandle. This function
152   may be called many times during platform initialization. In order to reduce boot times, the tests
153   performed by this function must be very small, and take as little time as possible to execute. This
154   function must not change the state of any hardware devices, and this function must be aware that the
155   device specified by ControllerHandle may already be managed by the same driver or a
156   different driver. This function must match its calls to AllocatePages() with FreePages(),
157   AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
158   Since ControllerHandle may have been previously started by the same driver, if a protocol is
159   already in the opened state, then it must not be closed with CloseProtocol(). This is required
160   to guarantee the state of ControllerHandle is not modified by this function.
161 
162   @param[in]  This                 A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
163   @param[in]  ControllerHandle     The handle of the controller to test. This handle
164                                    must support a protocol interface that supplies
165                                    an I/O abstraction to the driver.
166   @param[in]  RemainingDevicePath  A pointer to the remaining portion of a device path.  This
167                                    parameter is ignored by device drivers, and is optional for bus
168                                    drivers. For bus drivers, if this parameter is not NULL, then
169                                    the bus driver must determine if the bus controller specified
170                                    by ControllerHandle and the child controller specified
171                                    by RemainingDevicePath are both supported by this
172                                    bus driver.
173 
174   @retval EFI_SUCCESS              The device specified by ControllerHandle and
175                                    RemainingDevicePath is supported by the driver specified by This.
176   @retval EFI_ALREADY_STARTED      The device specified by ControllerHandle and
177                                    RemainingDevicePath is already being managed by the driver
178                                    specified by This.
179   @retval EFI_ACCESS_DENIED        The device specified by ControllerHandle and
180                                    RemainingDevicePath is already being managed by a different
181                                    driver or an application that requires exclusive access.
182                                    Currently not implemented.
183   @retval EFI_UNSUPPORTED          The device specified by ControllerHandle and
184                                    RemainingDevicePath is not supported by the driver specified by This.
185 **/
186 EFI_STATUS
187 EFIAPI
188 UfsPassThruDriverBindingSupported (
189   IN EFI_DRIVER_BINDING_PROTOCOL  *This,
190   IN EFI_HANDLE                   Controller,
191   IN EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath
192   );
193 
194 /**
195   Starts a device controller or a bus controller.
196 
197   The Start() function is designed to be invoked from the EFI boot service ConnectController().
198   As a result, much of the error checking on the parameters to Start() has been moved into this
199   common boot service. It is legal to call Start() from other locations,
200   but the following calling restrictions must be followed or the system behavior will not be deterministic.
201   1. ControllerHandle must be a valid EFI_HANDLE.
202   2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
203      EFI_DEVICE_PATH_PROTOCOL.
204   3. Prior to calling Start(), the Supported() function for the driver specified by This must
205      have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
206 
207   @param[in]  This                 A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
208   @param[in]  ControllerHandle     The handle of the controller to start. This handle
209                                    must support a protocol interface that supplies
210                                    an I/O abstraction to the driver.
211   @param[in]  RemainingDevicePath  A pointer to the remaining portion of a device path.  This
212                                    parameter is ignored by device drivers, and is optional for bus
213                                    drivers. For a bus driver, if this parameter is NULL, then handles
214                                    for all the children of Controller are created by this driver.
215                                    If this parameter is not NULL and the first Device Path Node is
216                                    not the End of Device Path Node, then only the handle for the
217                                    child device specified by the first Device Path Node of
218                                    RemainingDevicePath is created by this driver.
219                                    If the first Device Path Node of RemainingDevicePath is
220                                    the End of Device Path Node, no child handle is created by this
221                                    driver.
222 
223   @retval EFI_SUCCESS              The device was started.
224   @retval EFI_DEVICE_ERROR         The device could not be started due to a device error.Currently not implemented.
225   @retval EFI_OUT_OF_RESOURCES     The request could not be completed due to a lack of resources.
226   @retval Others                   The driver failded to start the device.
227 
228 **/
229 EFI_STATUS
230 EFIAPI
231 UfsPassThruDriverBindingStart (
232   IN EFI_DRIVER_BINDING_PROTOCOL  *This,
233   IN EFI_HANDLE                   Controller,
234   IN EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath
235   );
236 
237 /**
238   Stops a device controller or a bus controller.
239 
240   The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
241   As a result, much of the error checking on the parameters to Stop() has been moved
242   into this common boot service. It is legal to call Stop() from other locations,
243   but the following calling restrictions must be followed or the system behavior will not be deterministic.
244   1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
245      same driver's Start() function.
246   2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
247      EFI_HANDLE. In addition, all of these handles must have been created in this driver's
248      Start() function, and the Start() function must have called OpenProtocol() on
249      ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
250 
251   @param[in]  This              A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
252   @param[in]  ControllerHandle  A handle to the device being stopped. The handle must
253                                 support a bus specific I/O protocol for the driver
254                                 to use to stop the device.
255   @param[in]  NumberOfChildren  The number of child device handles in ChildHandleBuffer.
256   @param[in]  ChildHandleBuffer An array of child handles to be freed. May be NULL
257                                 if NumberOfChildren is 0.
258 
259   @retval EFI_SUCCESS           The device was stopped.
260   @retval EFI_DEVICE_ERROR      The device could not be stopped due to a device error.
261 
262 **/
263 EFI_STATUS
264 EFIAPI
265 UfsPassThruDriverBindingStop (
266   IN  EFI_DRIVER_BINDING_PROTOCOL     *This,
267   IN  EFI_HANDLE                      Controller,
268   IN  UINTN                           NumberOfChildren,
269   IN  EFI_HANDLE                      *ChildHandleBuffer
270   );
271 
272 //
273 // EFI Component Name Functions
274 //
275 /**
276   Retrieves a Unicode string that is the user readable name of the driver.
277 
278   This function retrieves the user readable name of a driver in the form of a
279   Unicode string. If the driver specified by This has a user readable name in
280   the language specified by Language, then a pointer to the driver name is
281   returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
282   by This does not support the language specified by Language,
283   then EFI_UNSUPPORTED is returned.
284 
285   @param  This[in]              A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
286                                 EFI_COMPONENT_NAME_PROTOCOL instance.
287 
288   @param  Language[in]          A pointer to a Null-terminated ASCII string
289                                 array indicating the language. This is the
290                                 language of the driver name that the caller is
291                                 requesting, and it must match one of the
292                                 languages specified in SupportedLanguages. The
293                                 number of languages supported by a driver is up
294                                 to the driver writer. Language is specified
295                                 in RFC 4646 or ISO 639-2 language code format.
296 
297   @param  DriverName[out]       A pointer to the Unicode string to return.
298                                 This Unicode string is the name of the
299                                 driver specified by This in the language
300                                 specified by Language.
301 
302   @retval EFI_SUCCESS           The Unicode string for the Driver specified by
303                                 This and the language specified by Language was
304                                 returned in DriverName.
305 
306   @retval EFI_INVALID_PARAMETER Language is NULL.
307 
308   @retval EFI_INVALID_PARAMETER DriverName is NULL.
309 
310   @retval EFI_UNSUPPORTED       The driver specified by This does not support
311                                 the language specified by Language.
312 
313 **/
314 EFI_STATUS
315 EFIAPI
316 UfsPassThruComponentNameGetDriverName (
317   IN  EFI_COMPONENT_NAME_PROTOCOL  *This,
318   IN  CHAR8                        *Language,
319   OUT CHAR16                       **DriverName
320   );
321 
322 
323 /**
324   Retrieves a Unicode string that is the user readable name of the controller
325   that is being managed by a driver.
326 
327   This function retrieves the user readable name of the controller specified by
328   ControllerHandle and ChildHandle in the form of a Unicode string. If the
329   driver specified by This has a user readable name in the language specified by
330   Language, then a pointer to the controller name is returned in ControllerName,
331   and EFI_SUCCESS is returned.  If the driver specified by This is not currently
332   managing the controller specified by ControllerHandle and ChildHandle,
333   then EFI_UNSUPPORTED is returned.  If the driver specified by This does not
334   support the language specified by Language, then EFI_UNSUPPORTED is returned.
335 
336   @param  This[in]              A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
337                                 EFI_COMPONENT_NAME_PROTOCOL instance.
338 
339   @param  ControllerHandle[in]  The handle of a controller that the driver
340                                 specified by This is managing.  This handle
341                                 specifies the controller whose name is to be
342                                 returned.
343 
344   @param  ChildHandle[in]       The handle of the child controller to retrieve
345                                 the name of.  This is an optional parameter that
346                                 may be NULL.  It will be NULL for device
347                                 drivers.  It will also be NULL for a bus drivers
348                                 that wish to retrieve the name of the bus
349                                 controller.  It will not be NULL for a bus
350                                 driver that wishes to retrieve the name of a
351                                 child controller.
352 
353   @param  Language[in]          A pointer to a Null-terminated ASCII string
354                                 array indicating the language.  This is the
355                                 language of the driver name that the caller is
356                                 requesting, and it must match one of the
357                                 languages specified in SupportedLanguages. The
358                                 number of languages supported by a driver is up
359                                 to the driver writer. Language is specified in
360                                 RFC 4646 or ISO 639-2 language code format.
361 
362   @param  ControllerName[out]   A pointer to the Unicode string to return.
363                                 This Unicode string is the name of the
364                                 controller specified by ControllerHandle and
365                                 ChildHandle in the language specified by
366                                 Language from the point of view of the driver
367                                 specified by This.
368 
369   @retval EFI_SUCCESS           The Unicode string for the user readable name in
370                                 the language specified by Language for the
371                                 driver specified by This was returned in
372                                 DriverName.
373 
374   @retval EFI_INVALID_PARAMETER ControllerHandle is NULL.
375 
376   @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid
377                                 EFI_HANDLE.
378 
379   @retval EFI_INVALID_PARAMETER Language is NULL.
380 
381   @retval EFI_INVALID_PARAMETER ControllerName is NULL.
382 
383   @retval EFI_UNSUPPORTED       The driver specified by This is not currently
384                                 managing the controller specified by
385                                 ControllerHandle and ChildHandle.
386 
387   @retval EFI_UNSUPPORTED       The driver specified by This does not support
388                                 the language specified by Language.
389 
390 **/
391 EFI_STATUS
392 EFIAPI
393 UfsPassThruComponentNameGetControllerName (
394   IN  EFI_COMPONENT_NAME_PROTOCOL                     *This,
395   IN  EFI_HANDLE                                      ControllerHandle,
396   IN  EFI_HANDLE                                      ChildHandle        OPTIONAL,
397   IN  CHAR8                                           *Language,
398   OUT CHAR16                                          **ControllerName
399   );
400 
401 /**
402   Sends a SCSI Request Packet to a SCSI device that is attached to the SCSI channel. This function
403   supports both blocking I/O and nonblocking I/O. The blocking I/O functionality is required, and the
404   nonblocking I/O functionality is optional.
405 
406   @param  This    A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
407   @param  Target  The Target is an array of size TARGET_MAX_BYTES and it represents
408                   the id of the SCSI device to send the SCSI Request Packet. Each
409                   transport driver may choose to utilize a subset of this size to suit the needs
410                   of transport target representation. For example, a Fibre Channel driver
411                   may use only 8 bytes (WWN) to represent an FC target.
412   @param  Lun     The LUN of the SCSI device to send the SCSI Request Packet.
413   @param  Packet  A pointer to the SCSI Request Packet to send to the SCSI device
414                   specified by Target and Lun.
415   @param  Event   If nonblocking I/O is not supported then Event is ignored, and blocking
416                   I/O is performed. If Event is NULL, then blocking I/O is performed. If
417                   Event is not NULL and non blocking I/O is supported, then
418                   nonblocking I/O is performed, and Event will be signaled when the
419                   SCSI Request Packet completes.
420 
421   @retval EFI_SUCCESS           The SCSI Request Packet was sent by the host. For bi-directional
422                                 commands, InTransferLength bytes were transferred from
423                                 InDataBuffer. For write and bi-directional commands,
424                                 OutTransferLength bytes were transferred by
425                                 OutDataBuffer.
426   @retval EFI_BAD_BUFFER_SIZE   The SCSI Request Packet was not executed. The number of bytes that
427                                 could be transferred is returned in InTransferLength. For write
428                                 and bi-directional commands, OutTransferLength bytes were
429                                 transferred by OutDataBuffer.
430   @retval EFI_NOT_READY         The SCSI Request Packet could not be sent because there are too many
431                                 SCSI Request Packets already queued. The caller may retry again later.
432   @retval EFI_DEVICE_ERROR      A device error occurred while attempting to send the SCSI Request
433                                 Packet.
434   @retval EFI_INVALID_PARAMETER Target, Lun, or the contents of ScsiRequestPacket are invalid.
435   @retval EFI_UNSUPPORTED       The command described by the SCSI Request Packet is not supported
436                                 by the host adapter. This includes the case of Bi-directional SCSI
437                                 commands not supported by the implementation. The SCSI Request
438                                 Packet was not sent, so no additional status information is available.
439   @retval EFI_TIMEOUT           A timeout occurred while waiting for the SCSI Request Packet to execute.
440 
441 **/
442 EFI_STATUS
443 EFIAPI
444 UfsPassThruPassThru (
445   IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL                    *This,
446   IN UINT8                                              *Target,
447   IN UINT64                                             Lun,
448   IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET     *Packet,
449   IN EFI_EVENT                                          Event OPTIONAL
450   );
451 
452 /**
453   Used to retrieve the list of legal Target IDs and LUNs for SCSI devices on a SCSI channel. These
454   can either be the list SCSI devices that are actually present on the SCSI channel, or the list of legal
455   Target Ids and LUNs for the SCSI channel. Regardless, the caller of this function must probe the
456   Target ID and LUN returned to see if a SCSI device is actually present at that location on the SCSI
457   channel.
458 
459   @param  This   A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
460   @param  Target On input, a pointer to the Target ID (an array of size
461                  TARGET_MAX_BYTES) of a SCSI device present on the SCSI channel.
462                  On output, a pointer to the Target ID (an array of
463                  TARGET_MAX_BYTES) of the next SCSI device present on a SCSI
464                  channel. An input value of 0xF(all bytes in the array are 0xF) in the
465                  Target array retrieves the Target ID of the first SCSI device present on a
466                  SCSI channel.
467   @param  Lun    On input, a pointer to the LUN of a SCSI device present on the SCSI
468                  channel. On output, a pointer to the LUN of the next SCSI device present
469                  on a SCSI channel.
470 
471   @retval EFI_SUCCESS           The Target ID and LUN of the next SCSI device on the SCSI
472                                 channel was returned in Target and Lun.
473   @retval EFI_INVALID_PARAMETER Target array is not all 0xF, and Target and Lun were
474                                 not returned on a previous call to GetNextTargetLun().
475   @retval EFI_NOT_FOUND         There are no more SCSI devices on this SCSI channel.
476 
477 **/
478 EFI_STATUS
479 EFIAPI
480 UfsPassThruGetNextTargetLun (
481   IN  EFI_EXT_SCSI_PASS_THRU_PROTOCOL    *This,
482   IN OUT UINT8                           **Target,
483   IN OUT UINT64                          *Lun
484   );
485 
486 /**
487   Used to allocate and build a device path node for a SCSI device on a SCSI channel.
488 
489   @param  This       A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
490   @param  Target     The Target is an array of size TARGET_MAX_BYTES and it specifies the
491                      Target ID of the SCSI device for which a device path node is to be
492                      allocated and built. Transport drivers may chose to utilize a subset of
493                      this size to suit the representation of targets. For example, a Fibre
494                      Channel driver may use only 8 bytes (WWN) in the array to represent a
495                      FC target.
496   @param  Lun        The LUN of the SCSI device for which a device path node is to be
497                      allocated and built.
498   @param  DevicePath A pointer to a single device path node that describes the SCSI device
499                      specified by Target and Lun. This function is responsible for
500                      allocating the buffer DevicePath with the boot service
501                      AllocatePool(). It is the caller's responsibility to free
502                      DevicePath when the caller is finished with DevicePath.
503 
504   @retval EFI_SUCCESS           The device path node that describes the SCSI device specified by
505                                 Target and Lun was allocated and returned in
506                                 DevicePath.
507   @retval EFI_INVALID_PARAMETER DevicePath is NULL.
508   @retval EFI_NOT_FOUND         The SCSI devices specified by Target and Lun does not exist
509                                 on the SCSI channel.
510   @retval EFI_OUT_OF_RESOURCES  There are not enough resources to allocate DevicePath.
511 
512 **/
513 EFI_STATUS
514 EFIAPI
515 UfsPassThruBuildDevicePath (
516   IN     EFI_EXT_SCSI_PASS_THRU_PROTOCOL    *This,
517   IN     UINT8                              *Target,
518   IN     UINT64                             Lun,
519   IN OUT EFI_DEVICE_PATH_PROTOCOL           **DevicePath
520   );
521 
522 /**
523   Used to translate a device path node to a Target ID and LUN.
524 
525   @param  This       A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
526   @param  DevicePath A pointer to a single device path node that describes the SCSI device
527                      on the SCSI channel.
528   @param  Target     A pointer to the Target Array which represents the ID of a SCSI device
529                      on the SCSI channel.
530   @param  Lun        A pointer to the LUN of a SCSI device on the SCSI channel.
531 
532   @retval EFI_SUCCESS           DevicePath was successfully translated to a Target ID and
533                                 LUN, and they were returned in Target and Lun.
534   @retval EFI_INVALID_PARAMETER DevicePath or Target or Lun is NULL.
535   @retval EFI_NOT_FOUND         A valid translation from DevicePath to a Target ID and LUN
536                                 does not exist.
537   @retval EFI_UNSUPPORTED       This driver does not support the device path node type in
538                                  DevicePath.
539 
540 **/
541 EFI_STATUS
542 EFIAPI
543 UfsPassThruGetTargetLun (
544   IN  EFI_EXT_SCSI_PASS_THRU_PROTOCOL    *This,
545   IN  EFI_DEVICE_PATH_PROTOCOL           *DevicePath,
546   OUT UINT8                              **Target,
547   OUT UINT64                             *Lun
548   );
549 
550 /**
551   Resets a SCSI channel. This operation resets all the SCSI devices connected to the SCSI channel.
552 
553   @param  This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
554 
555   @retval EFI_SUCCESS      The SCSI channel was reset.
556   @retval EFI_DEVICE_ERROR A device error occurred while attempting to reset the SCSI channel.
557   @retval EFI_TIMEOUT      A timeout occurred while attempting to reset the SCSI channel.
558   @retval EFI_UNSUPPORTED  The SCSI channel does not support a channel reset operation.
559 
560 **/
561 EFI_STATUS
562 EFIAPI
563 UfsPassThruResetChannel (
564   IN  EFI_EXT_SCSI_PASS_THRU_PROTOCOL   *This
565   );
566 
567 /**
568   Resets a SCSI logical unit that is connected to a SCSI channel.
569 
570   @param  This   A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
571   @param  Target The Target is an array of size TARGET_MAX_BYTE and it represents the
572                  target port ID of the SCSI device containing the SCSI logical unit to
573                  reset. Transport drivers may chose to utilize a subset of this array to suit
574                  the representation of their targets.
575   @param  Lun    The LUN of the SCSI device to reset.
576 
577   @retval EFI_SUCCESS           The SCSI device specified by Target and Lun was reset.
578   @retval EFI_INVALID_PARAMETER Target or Lun is NULL.
579   @retval EFI_TIMEOUT           A timeout occurred while attempting to reset the SCSI device
580                                 specified by Target and Lun.
581   @retval EFI_UNSUPPORTED       The SCSI channel does not support a target reset operation.
582   @retval EFI_DEVICE_ERROR      A device error occurred while attempting to reset the SCSI device
583                                  specified by Target and Lun.
584 
585 **/
586 EFI_STATUS
587 EFIAPI
588 UfsPassThruResetTargetLun (
589   IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL    *This,
590   IN UINT8                              *Target,
591   IN UINT64                             Lun
592   );
593 
594 /**
595   Used to retrieve the list of legal Target IDs for SCSI devices on a SCSI channel. These can either
596   be the list SCSI devices that are actually present on the SCSI channel, or the list of legal Target IDs
597   for the SCSI channel. Regardless, the caller of this function must probe the Target ID returned to
598   see if a SCSI device is actually present at that location on the SCSI channel.
599 
600   @param  This   A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
601   @param  Target (TARGET_MAX_BYTES) of a SCSI device present on the SCSI channel.
602                  On output, a pointer to the Target ID (an array of
603                  TARGET_MAX_BYTES) of the next SCSI device present on a SCSI
604                  channel. An input value of 0xF(all bytes in the array are 0xF) in the
605                  Target array retrieves the Target ID of the first SCSI device present on a
606                  SCSI channel.
607 
608   @retval EFI_SUCCESS           The Target ID of the next SCSI device on the SCSI
609                                 channel was returned in Target.
610   @retval EFI_INVALID_PARAMETER Target or Lun is NULL.
611   @retval EFI_TIMEOUT           Target array is not all 0xF, and Target was not
612                                 returned on a previous call to GetNextTarget().
613   @retval EFI_NOT_FOUND         There are no more SCSI devices on this SCSI channel.
614 
615 **/
616 EFI_STATUS
617 EFIAPI
618 UfsPassThruGetNextTarget (
619   IN  EFI_EXT_SCSI_PASS_THRU_PROTOCOL    *This,
620   IN OUT UINT8                           **Target
621   );
622 
623 /**
624   Sends a UFS-supported SCSI Request Packet to a UFS device that is attached to the UFS host controller.
625 
626   @param[in]      Private       The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
627   @param[in]      Lun           The LUN of the UFS device to send the SCSI Request Packet.
628   @param[in, out] Packet        A pointer to the SCSI Request Packet to send to a specified Lun of the
629                                 UFS device.
630   @param[in]      Event         If nonblocking I/O is not supported then Event is ignored, and blocking
631                                 I/O is performed. If Event is NULL, then blocking I/O is performed. If
632                                 Event is not NULL and non blocking I/O is supported, then
633                                 nonblocking I/O is performed, and Event will be signaled when the
634                                 SCSI Request Packet completes.
635 
636   @retval EFI_SUCCESS           The SCSI Request Packet was sent by the host. For bi-directional
637                                 commands, InTransferLength bytes were transferred from
638                                 InDataBuffer. For write and bi-directional commands,
639                                 OutTransferLength bytes were transferred by
640                                 OutDataBuffer.
641   @retval EFI_DEVICE_ERROR      A device error occurred while attempting to send the SCSI Request
642                                 Packet.
643   @retval EFI_OUT_OF_RESOURCES  The resource for transfer is not available.
644   @retval EFI_TIMEOUT           A timeout occurred while waiting for the SCSI Request Packet to execute.
645 
646 **/
647 EFI_STATUS
648 UfsExecScsiCmds (
649   IN     UFS_PASS_THRU_PRIVATE_DATA                  *Private,
650   IN     UINT8                                       Lun,
651   IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET  *Packet,
652   IN     EFI_EVENT                                   Event    OPTIONAL
653   );
654 
655 /**
656   Initialize the UFS host controller.
657 
658   @param[in] Private                 The pointer to the NVME_CONTROLLER_PRIVATE_DATA data structure.
659 
660   @retval EFI_SUCCESS                The NVM Express Controller is initialized successfully.
661   @retval Others                     A device error occurred while initializing the controller.
662 
663 **/
664 EFI_STATUS
665 UfsControllerInit (
666   IN  UFS_PASS_THRU_PRIVATE_DATA     *Private
667   );
668 
669 /**
670   Stop the UFS host controller.
671 
672   @param[in] Private                 The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
673 
674   @retval EFI_SUCCESS                The Ufs Host Controller is stopped successfully.
675   @retval Others                     A device error occurred while stopping the controller.
676 
677 **/
678 EFI_STATUS
679 UfsControllerStop (
680   IN  UFS_PASS_THRU_PRIVATE_DATA     *Private
681   );
682 
683 /**
684   Allocate common buffer for host and UFS bus master access simultaneously.
685 
686   @param[in]  Private                The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
687   @param[in]  Size                   The length of buffer to be allocated.
688   @param[out] CmdDescHost            A pointer to store the base system memory address of the allocated range.
689   @param[out] CmdDescPhyAddr         The resulting map address for the UFS bus master to use to access the hosts CmdDescHost.
690   @param[out] CmdDescMapping         A resulting value to pass to Unmap().
691 
692   @retval EFI_SUCCESS                The common buffer was allocated successfully.
693   @retval EFI_DEVICE_ERROR           The allocation fails.
694   @retval EFI_OUT_OF_RESOURCES       The memory resource is insufficient.
695 
696 **/
697 EFI_STATUS
698 UfsAllocateAlignCommonBuffer (
699   IN     UFS_PASS_THRU_PRIVATE_DATA    *Private,
700   IN     UINTN                         Size,
701      OUT VOID                          **CmdDescHost,
702      OUT EFI_PHYSICAL_ADDRESS          *CmdDescPhyAddr,
703      OUT VOID                          **CmdDescMapping
704   );
705 
706 /**
707   Set specified flag to 1 on a UFS device.
708 
709   @param[in]  Private           The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
710   @param[in]  FlagId            The ID of flag to be set.
711 
712   @retval EFI_SUCCESS           The flag was set successfully.
713   @retval EFI_DEVICE_ERROR      A device error occurred while attempting to set the flag.
714   @retval EFI_TIMEOUT           A timeout occurred while waiting for the completion of setting the flag.
715 
716 **/
717 EFI_STATUS
718 UfsSetFlag (
719   IN  UFS_PASS_THRU_PRIVATE_DATA   *Private,
720   IN  UINT8                        FlagId
721   );
722 
723 /**
724   Read specified flag from a UFS device.
725 
726   @param[in]  Private           The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
727   @param[in]  FlagId            The ID of flag to be read.
728   @param[out] Value             The flag's value.
729 
730   @retval EFI_SUCCESS           The flag was read successfully.
731   @retval EFI_DEVICE_ERROR      A device error occurred while attempting to read the flag.
732   @retval EFI_TIMEOUT           A timeout occurred while waiting for the completion of reading the flag.
733 
734 **/
735 EFI_STATUS
736 UfsReadFlag (
737   IN     UFS_PASS_THRU_PRIVATE_DATA   *Private,
738   IN     UINT8                        FlagId,
739      OUT UINT8                        *Value
740   );
741 
742 /**
743   Read or write specified flag of a UFS device.
744 
745   @param[in]      Private       The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
746   @param[in]      Read          The boolean variable to show r/w direction.
747   @param[in]      FlagId        The ID of flag to be read or written.
748   @param[in, out] Value         The value to set or clear flag.
749 
750   @retval EFI_SUCCESS           The flag was read/written successfully.
751   @retval EFI_DEVICE_ERROR      A device error occurred while attempting to r/w the flag.
752   @retval EFI_TIMEOUT           A timeout occurred while waiting for the completion of r/w the flag.
753 
754 **/
755 EFI_STATUS
756 UfsRwFlags (
757   IN     UFS_PASS_THRU_PRIVATE_DATA   *Private,
758   IN     BOOLEAN                      Read,
759   IN     UINT8                        FlagId,
760   IN OUT UINT8                        *Value
761   );
762 
763 /**
764   Read or write specified device descriptor of a UFS device.
765 
766   @param[in]      Private       The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
767   @param[in]      Read          The boolean variable to show r/w direction.
768   @param[in]      DescId        The ID of device descriptor.
769   @param[in]      Index         The Index of device descriptor.
770   @param[in]      Selector      The Selector of device descriptor.
771   @param[in, out] Descriptor    The buffer of device descriptor to be read or written.
772   @param[in, out] DescSize      The size of device descriptor buffer. On input, the size, in bytes,
773                                 of the data buffer specified by Descriptor. On output, the number
774                                 of bytes that were actually transferred.
775 
776   @retval EFI_SUCCESS           The device descriptor was read/written successfully.
777   @retval EFI_DEVICE_ERROR      A device error occurred while attempting to r/w the device descriptor.
778   @retval EFI_TIMEOUT           A timeout occurred while waiting for the completion of r/w the device descriptor.
779 
780 **/
781 EFI_STATUS
782 UfsRwDeviceDesc (
783   IN     UFS_PASS_THRU_PRIVATE_DATA   *Private,
784   IN     BOOLEAN                      Read,
785   IN     UINT8                        DescId,
786   IN     UINT8                        Index,
787   IN     UINT8                        Selector,
788   IN OUT VOID                         *Descriptor,
789   IN OUT UINT32                       *DescSize
790   );
791 
792 /**
793   Read or write specified attribute of a UFS device.
794 
795   @param[in]      Private       The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
796   @param[in]      Read          The boolean variable to show r/w direction.
797   @param[in]      AttrId        The ID of Attribute.
798   @param[in]      Index         The Index of Attribute.
799   @param[in]      Selector      The Selector of Attribute.
800   @param[in, out] Attributes    The value of Attribute to be read or written.
801 
802   @retval EFI_SUCCESS           The Attribute was read/written successfully.
803   @retval EFI_DEVICE_ERROR      A device error occurred while attempting to r/w the Attribute.
804   @retval EFI_TIMEOUT           A timeout occurred while waiting for the completion of r/w the Attribute.
805 
806 **/
807 EFI_STATUS
808 UfsRwAttributes (
809   IN     UFS_PASS_THRU_PRIVATE_DATA   *Private,
810   IN     BOOLEAN                      Read,
811   IN     UINT8                        AttrId,
812   IN     UINT8                        Index,
813   IN     UINT8                        Selector,
814   IN OUT UINT32                       *Attributes
815   );
816 
817 /**
818   Sends NOP IN cmd to a UFS device for initialization process request.
819   For more details, please refer to UFS 2.0 spec Figure 13.3.
820 
821   @param[in]  Private           The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
822 
823   @retval EFI_SUCCESS           The NOP IN command was sent by the host. The NOP OUT response was
824                                 received successfully.
825   @retval EFI_DEVICE_ERROR      A device error occurred while attempting to execute NOP IN command.
826   @retval EFI_OUT_OF_RESOURCES  The resource for transfer is not available.
827   @retval EFI_TIMEOUT           A timeout occurred while waiting for the NOP IN command to execute.
828 
829 **/
830 EFI_STATUS
831 UfsExecNopCmds (
832   IN  UFS_PASS_THRU_PRIVATE_DATA       *Private
833   );
834 
835 /**
836   Call back function when the timer event is signaled.
837 
838   @param[in]  Event     The Event this notify function registered to.
839   @param[in]  Context   Pointer to the context data registered to the Event.
840 
841 **/
842 VOID
843 EFIAPI
844 ProcessAsyncTaskList (
845   IN EFI_EVENT          Event,
846   IN VOID               *Context
847   );
848 
849 /**
850   Internal helper function which will signal the caller event and clean up
851   resources.
852 
853   @param[in] Private   The pointer to the UFS_PASS_THRU_PRIVATE_DATA data
854                        structure.
855   @param[in] TransReq  The pointer to the UFS_PASS_THRU_TRANS_REQ data
856                        structure.
857 
858 **/
859 VOID
860 EFIAPI
861 SignalCallerEvent (
862   IN UFS_PASS_THRU_PRIVATE_DATA      *Private,
863   IN UFS_PASS_THRU_TRANS_REQ         *TransReq
864   );
865 
866 /**
867   Read or write specified device descriptor of a UFS device.
868 
869   The function is used to read/write UFS device descriptors. The consumer of this API is
870   responsible for allocating the data buffer pointed by Descriptor.
871 
872   @param[in]      This          The pointer to the EFI_UFS_DEVICE_CONFIG_PROTOCOL instance.
873   @param[in]      Read          The boolean variable to show r/w direction.
874   @param[in]      DescId        The ID of device descriptor.
875   @param[in]      Index         The Index of device descriptor.
876   @param[in]      Selector      The Selector of device descriptor.
877   @param[in, out] Descriptor    The buffer of device descriptor to be read or written.
878   @param[in, out] DescSize      The size of device descriptor buffer. On input, the size, in bytes,
879                                 of the data buffer specified by Descriptor. On output, the number
880                                 of bytes that were actually transferred.
881 
882   @retval EFI_SUCCESS           The device descriptor is read/written successfully.
883   @retval EFI_INVALID_PARAMETER This is NULL or Descriptor is NULL or DescSize is NULL.
884                                 DescId, Index and Selector are invalid combination to point to a
885                                 type of UFS device descriptor.
886   @retval EFI_DEVICE_ERROR      The device descriptor is not read/written successfully.
887 
888 **/
889 EFI_STATUS
890 EFIAPI
891 UfsRwUfsDescriptor (
892   IN EFI_UFS_DEVICE_CONFIG_PROTOCOL    *This,
893   IN BOOLEAN                           Read,
894   IN UINT8                             DescId,
895   IN UINT8                             Index,
896   IN UINT8                             Selector,
897   IN OUT UINT8                         *Descriptor,
898   IN OUT UINT32                        *DescSize
899   );
900 
901 /**
902   Read or write specified flag of a UFS device.
903 
904   The function is used to read/write UFS flag descriptors. The consumer of this API is responsible
905   for allocating the buffer pointed by Flag. The buffer size is 1 byte as UFS flag descriptor is
906   just a single Boolean value that represents a TRUE or FALSE, '0' or '1', ON or OFF type of value.
907 
908   @param[in]      This          The pointer to the EFI_UFS_DEVICE_CONFIG_PROTOCOL instance.
909   @param[in]      Read          The boolean variable to show r/w direction.
910   @param[in]      FlagId        The ID of flag to be read or written.
911   @param[in, out] Flag          The buffer to set or clear flag.
912 
913   @retval EFI_SUCCESS           The flag descriptor is set/clear successfully.
914   @retval EFI_INVALID_PARAMETER This is NULL or Flag is NULL.
915                                 FlagId is an invalid UFS flag ID.
916   @retval EFI_DEVICE_ERROR      The flag is not set/clear successfully.
917 
918 **/
919 EFI_STATUS
920 EFIAPI
921 UfsRwUfsFlag (
922   IN EFI_UFS_DEVICE_CONFIG_PROTOCOL    *This,
923   IN BOOLEAN                           Read,
924   IN UINT8                             FlagId,
925   IN OUT UINT8                         *Flag
926   );
927 
928 /**
929   Read or write specified attribute of a UFS device.
930 
931   The function is used to read/write UFS attributes. The consumer of this API is responsible for
932   allocating the data buffer pointed by Attribute.
933 
934   @param[in]      This          The pointer to the EFI_UFS_DEVICE_CONFIG_PROTOCOL instance.
935   @param[in]      Read          The boolean variable to show r/w direction.
936   @param[in]      AttrId        The ID of Attribute.
937   @param[in]      Index         The Index of Attribute.
938   @param[in]      Selector      The Selector of Attribute.
939   @param[in, out] Attribute     The buffer of Attribute to be read or written.
940   @param[in, out] AttrSize      The size of Attribute buffer. On input, the size, in bytes, of the
941                                 data buffer specified by Attribute. On output, the number of bytes
942                                 that were actually transferred.
943 
944   @retval EFI_SUCCESS           The attribute is read/written successfully.
945   @retval EFI_INVALID_PARAMETER This is NULL or Attribute is NULL or AttrSize is NULL.
946                                 AttrId, Index and Selector are invalid combination to point to a
947                                 type of UFS attribute.
948   @retval EFI_DEVICE_ERROR      The attribute is not read/written successfully.
949 
950 **/
951 EFI_STATUS
952 EFIAPI
953 UfsRwUfsAttribute (
954   IN EFI_UFS_DEVICE_CONFIG_PROTOCOL    *This,
955   IN BOOLEAN                           Read,
956   IN UINT8                             AttrId,
957   IN UINT8                             Index,
958   IN UINT8                             Selector,
959   IN OUT UINT8                         *Attribute,
960   IN OUT UINT32                        *AttrSize
961   );
962 
963 /**
964   Execute UIC command.
965 
966   @param[in]      This        Pointer to driver interface produced by the UFS controller.
967   @param[in, out] UicCommand  Descriptor of the command that will be executed.
968 
969   @retval EFI_SUCCESS            Command executed successfully.
970   @retval EFI_INVALID_PARAMETER  This or UicCommand is NULL.
971   @retval Others                 Command failed to execute.
972 **/
973 EFI_STATUS
974 EFIAPI
975 UfsHcDriverInterfaceExecUicCommand (
976   IN     EDKII_UFS_HC_DRIVER_INTERFACE  *This,
977   IN OUT EDKII_UIC_COMMAND              *UicCommand
978   );
979 
980 /**
981   Initializes UfsHcInfo field in private data.
982 
983   @param[in] Private  Pointer to host controller private data.
984 
985   @retval EFI_SUCCESS  UfsHcInfo initialized successfully.
986   @retval Others       Failed to initalize UfsHcInfo.
987 **/
988 EFI_STATUS
989 GetUfsHcInfo (
990   IN UFS_PASS_THRU_PRIVATE_DATA  *Private
991   );
992 
993 extern EFI_COMPONENT_NAME_PROTOCOL  gUfsPassThruComponentName;
994 extern EFI_COMPONENT_NAME2_PROTOCOL gUfsPassThruComponentName2;
995 extern EFI_DRIVER_BINDING_PROTOCOL  gUfsPassThruDriverBinding;
996 extern EDKII_UFS_HC_PLATFORM_PROTOCOL  *mUfsHcPlatform;
997 
998 #endif
999