summaryrefslogtreecommitdiffstats
path: root/MdeModulePkg/Bus/Ufs/UfsPassThruDxe/UfsDevConfigProtocol.c
blob: 07ec590edc31e6d935f8a2275e4f611369bd604c (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
/** @file
  The implementation of the EFI UFS Device Config Protocol.

  Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
  SPDX-License-Identifier: BSD-2-Clause-Patent

**/

#include "UfsPassThru.h"

/**
  Read or write specified device descriptor of a UFS device.

  The function is used to read/write UFS device descriptors. The consumer of this API is
  responsible for allocating the data buffer pointed by Descriptor.

  @param[in]      This          The pointer to the EFI_UFS_DEVICE_CONFIG_PROTOCOL instance.
  @param[in]      Read          The boolean variable to show r/w direction.
  @param[in]      DescId        The ID of device descriptor.
  @param[in]      Index         The Index of device descriptor.
  @param[in]      Selector      The Selector of device descriptor.
  @param[in, out] Descriptor    The buffer of device descriptor to be read or written.
  @param[in, out] DescSize      The size of device descriptor buffer. On input, the size, in bytes,
                                of the data buffer specified by Descriptor. On output, the number
                                of bytes that were actually transferred.

  @retval EFI_SUCCESS           The device descriptor is read/written successfully.
  @retval EFI_INVALID_PARAMETER This is NULL or Descriptor is NULL or DescSize is NULL.
                                DescId, Index and Selector are invalid combination to point to a
                                type of UFS device descriptor.
  @retval EFI_DEVICE_ERROR      The device descriptor is not read/written successfully.

**/
EFI_STATUS
EFIAPI
UfsRwUfsDescriptor (
  IN EFI_UFS_DEVICE_CONFIG_PROTOCOL  *This,
  IN BOOLEAN                         Read,
  IN UINT8                           DescId,
  IN UINT8                           Index,
  IN UINT8                           Selector,
  IN OUT UINT8                       *Descriptor,
  IN OUT UINT32                      *DescSize
  )
{
  EFI_STATUS                  Status;
  UFS_PASS_THRU_PRIVATE_DATA  *Private;

  Private = UFS_PASS_THRU_PRIVATE_DATA_FROM_DEV_CONFIG (This);

  if ((This == NULL) || (Descriptor == NULL) || (DescSize == NULL)) {
    return EFI_INVALID_PARAMETER;
  }

  Status = UfsRwDeviceDesc (
             Private,
             Read,
             DescId,
             Index,
             Selector,
             Descriptor,
             DescSize
             );
  if (Status == EFI_TIMEOUT) {
    Status = EFI_DEVICE_ERROR;
  }

  return Status;
}

/**
  Read or write specified flag of a UFS device.

  The function is used to read/write UFS flag descriptors. The consumer of this API is responsible
  for allocating the buffer pointed by Flag. The buffer size is 1 byte as UFS flag descriptor is
  just a single Boolean value that represents a TRUE or FALSE, '0' or '1', ON or OFF type of value.

  @param[in]      This          The pointer to the EFI_UFS_DEVICE_CONFIG_PROTOCOL instance.
  @param[in]      Read          The boolean variable to show r/w direction.
  @param[in]      FlagId        The ID of flag to be read or written.
  @param[in, out] Flag          The buffer to set or clear flag.

  @retval EFI_SUCCESS           The flag descriptor is set/clear successfully.
  @retval EFI_INVALID_PARAMETER This is NULL or Flag is NULL.
                                FlagId is an invalid UFS flag ID.
  @retval EFI_DEVICE_ERROR      The flag is not set/clear successfully.

**/
EFI_STATUS
EFIAPI
UfsRwUfsFlag (
  IN EFI_UFS_DEVICE_CONFIG_PROTOCOL  *This,
  IN BOOLEAN                         Read,
  IN UINT8                           FlagId,
  IN OUT UINT8                       *Flag
  )
{
  EFI_STATUS                  Status;
  UFS_PASS_THRU_PRIVATE_DATA  *Private;

  Private = UFS_PASS_THRU_PRIVATE_DATA_FROM_DEV_CONFIG (This);

  if ((This == NULL) || (Flag == NULL)) {
    return EFI_INVALID_PARAMETER;
  }

  Status = UfsRwFlags (Private, Read, FlagId, Flag);
  if (Status == EFI_TIMEOUT) {
    Status = EFI_DEVICE_ERROR;
  }

  return Status;
}

/**
  Read or write specified attribute of a UFS device.

  The function is used to read/write UFS attributes. The consumer of this API is responsible for
  allocating the data buffer pointed by Attribute.

  @param[in]      This          The pointer to the EFI_UFS_DEVICE_CONFIG_PROTOCOL instance.
  @param[in]      Read          The boolean variable to show r/w direction.
  @param[in]      AttrId        The ID of Attribute.
  @param[in]      Index         The Index of Attribute.
  @param[in]      Selector      The Selector of Attribute.
  @param[in, out] Attribute     The buffer of Attribute to be read or written.
  @param[in, out] AttrSize      The size of Attribute buffer. On input, the size, in bytes, of the
                                data buffer specified by Attribute. On output, the number of bytes
                                that were actually transferred.

  @retval EFI_SUCCESS           The attribute is read/written successfully.
  @retval EFI_INVALID_PARAMETER This is NULL or Attribute is NULL or AttrSize is NULL.
                                AttrId, Index and Selector are invalid combination to point to a
                                type of UFS attribute.
  @retval EFI_DEVICE_ERROR      The attribute is not read/written successfully.

**/
EFI_STATUS
EFIAPI
UfsRwUfsAttribute (
  IN EFI_UFS_DEVICE_CONFIG_PROTOCOL  *This,
  IN BOOLEAN                         Read,
  IN UINT8                           AttrId,
  IN UINT8                           Index,
  IN UINT8                           Selector,
  IN OUT UINT8                       *Attribute,
  IN OUT UINT32                      *AttrSize
  )
{
  EFI_STATUS                  Status;
  UFS_PASS_THRU_PRIVATE_DATA  *Private;
  UINT32                      Attribute32;

  Private     = UFS_PASS_THRU_PRIVATE_DATA_FROM_DEV_CONFIG (This);
  Attribute32 = 0;

  if ((This == NULL) || (Attribute == NULL) || (AttrSize == NULL)) {
    return EFI_INVALID_PARAMETER;
  }

  //
  // According to UFS Version 2.1 Spec (JESD220C) Section 14.3, the size of a attribute will not
  // exceed 32-bit.
  //
  if (*AttrSize > 4) {
    return EFI_INVALID_PARAMETER;
  }

  if (!Read) {
    CopyMem (&Attribute32, Attribute, *AttrSize);
  }

  Status = UfsRwAttributes (
             Private,
             Read,
             AttrId,
             Index,
             Selector,
             &Attribute32
             );
  if (!EFI_ERROR (Status)) {
    if (Read) {
      CopyMem (Attribute, &Attribute32, *AttrSize);
    }
  } else {
    *AttrSize = 0;
    if (Status == EFI_TIMEOUT) {
      Status = EFI_DEVICE_ERROR;
    }
  }

  return Status;
}