OvmfPkg/XenPvBlkDxe: fix VS2010 build failures
[mirror_edk2.git] / OvmfPkg / XenPvBlkDxe / BlockIo.c
CommitLineData
5de8a35c
AP
1/** @file\r
2 BlockIo implementation for Xen PV Block driver.\r
3\r
4 This file is implementing the interface between the actual driver in\r
5 BlockFront.c to the BlockIo protocol.\r
6\r
7 Copyright (C) 2014, Citrix Ltd.\r
8\r
9 This program and the accompanying materials\r
10 are licensed and made available under the terms and conditions of the BSD License\r
11 which accompanies this distribution. The full text of the license may be found at\r
12 http://opensource.org/licenses/bsd-license.php\r
13\r
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
16\r
17**/\r
18\r
19#include "XenPvBlkDxe.h"\r
20\r
21#include "BlockFront.h"\r
22\r
23///\r
24/// Block I/O Media structure\r
25///\r
26GLOBAL_REMOVE_IF_UNREFERENCED\r
27EFI_BLOCK_IO_MEDIA gXenPvBlkDxeBlockIoMedia = {\r
28 0, // MediaId\r
29 FALSE, // RemovableMedia\r
30 FALSE, // MediaPresent\r
31 FALSE, // LogicalPartition\r
32 TRUE, // ReadOnly\r
33 FALSE, // WriteCaching\r
34 512, // BlockSize\r
35 512, // IoAlign, BlockFront does not support less than 512 bits-aligned.\r
36 0, // LastBlock\r
37 0, // LowestAlignedLba\r
38 0, // LogicalBlocksPerPhysicalBlock\r
39 0 // OptimalTransferLengthGranularity\r
40};\r
41\r
42///\r
43/// Block I/O Protocol instance\r
44///\r
45GLOBAL_REMOVE_IF_UNREFERENCED\r
46EFI_BLOCK_IO_PROTOCOL gXenPvBlkDxeBlockIo = {\r
47 EFI_BLOCK_IO_PROTOCOL_REVISION3, // Revision\r
48 &gXenPvBlkDxeBlockIoMedia, // Media\r
49 XenPvBlkDxeBlockIoReset, // Reset\r
50 XenPvBlkDxeBlockIoReadBlocks, // ReadBlocks\r
51 XenPvBlkDxeBlockIoWriteBlocks, // WriteBlocks\r
52 XenPvBlkDxeBlockIoFlushBlocks // FlushBlocks\r
53};\r
54\r
55\r
56\r
57\r
58/**\r
59 Read/Write BufferSize bytes from Lba into Buffer.\r
60\r
61 This function is commun to XenPvBlkDxeBlockIoReadBlocks and\r
62 XenPvBlkDxeBlockIoWriteBlocks.\r
63\r
64 @param This Indicates a pointer to the calling context.\r
65 @param MediaId Id of the media, changes every time the media is replaced.\r
66 @param Lba The starting Logical Block Address to read from/write to.\r
67 @param BufferSize Size of Buffer, must be a multiple of device block size.\r
68 @param Buffer A pointer to the destination/source buffer for the data.\r
69 @param IsWrite Indicate if the operation is write or read.\r
70\r
71 @return See description of XenPvBlkDxeBlockIoReadBlocks and\r
72 XenPvBlkDxeBlockIoWriteBlocks.\r
73**/\r
74STATIC\r
75EFI_STATUS\r
76XenPvBlkDxeBlockIoReadWriteBlocks (\r
77 IN EFI_BLOCK_IO_PROTOCOL *This,\r
78 IN UINT32 MediaId,\r
79 IN EFI_LBA Lba,\r
80 IN UINTN BufferSize,\r
81 IN OUT VOID *Buffer,\r
82 IN BOOLEAN IsWrite\r
83 )\r
84{\r
85 XEN_BLOCK_FRONT_IO IoData;\r
86 EFI_BLOCK_IO_MEDIA *Media = This->Media;\r
87 UINTN Sector;\r
88 EFI_STATUS Status;\r
89\r
90 if (Buffer == NULL) {\r
91 return EFI_INVALID_PARAMETER;\r
92 }\r
93 if (BufferSize == 0) {\r
94 return EFI_SUCCESS;\r
95 }\r
96\r
97 if (BufferSize % Media->BlockSize != 0) {\r
98 DEBUG ((EFI_D_ERROR, "XenPvBlkDxe: Bad buffer size: 0x%X\n", BufferSize));\r
99 return EFI_BAD_BUFFER_SIZE;\r
100 }\r
101\r
102 if (Lba > Media->LastBlock ||\r
103 (BufferSize / Media->BlockSize) - 1 > Media->LastBlock - Lba) {\r
104 DEBUG ((EFI_D_ERROR, "XenPvBlkDxe: %a with invalid LBA: 0x%LX, size: 0x%x\n",\r
105 IsWrite ? "Write" : "Read", Lba, BufferSize));\r
106 return EFI_INVALID_PARAMETER;\r
107 }\r
108\r
109 if (IsWrite && Media->ReadOnly) {\r
110 return EFI_WRITE_PROTECTED;\r
111 }\r
112\r
113 if ((Media->IoAlign > 1) && (UINTN)Buffer & (Media->IoAlign - 1)) {\r
114 //\r
115 // Grub2 does not appear to respect IoAlign of 512, so reallocate the\r
116 // buffer here.\r
117 //\r
118 VOID *NewBuffer;\r
119\r
120 //\r
121 // Try again with a properly aligned buffer.\r
122 //\r
123 NewBuffer = AllocateAlignedPages((BufferSize + EFI_PAGE_SIZE) / EFI_PAGE_SIZE,\r
124 Media->IoAlign);\r
125 if (!IsWrite) {\r
126 Status = XenPvBlkDxeBlockIoReadBlocks (This, MediaId,\r
127 Lba, BufferSize, NewBuffer);\r
128 CopyMem (Buffer, NewBuffer, BufferSize);\r
129 } else {\r
130 CopyMem (NewBuffer, Buffer, BufferSize);\r
131 Status = XenPvBlkDxeBlockIoWriteBlocks (This, MediaId,\r
132 Lba, BufferSize, NewBuffer);\r
133 }\r
134 FreeAlignedPages (NewBuffer, (BufferSize + EFI_PAGE_SIZE) / EFI_PAGE_SIZE);\r
135 return Status;\r
136 }\r
137\r
138 IoData.Dev = XEN_BLOCK_FRONT_FROM_BLOCK_IO (This);\r
860088f2 139 Sector = (UINTN)MultU64x32 (Lba, Media->BlockSize / 512);\r
5de8a35c
AP
140\r
141 while (BufferSize > 0) {\r
142 if (((UINTN)Buffer & EFI_PAGE_MASK) == 0) {\r
143 IoData.Size = MIN (BLKIF_MAX_SEGMENTS_PER_REQUEST * EFI_PAGE_SIZE,\r
144 BufferSize);\r
145 } else {\r
146 IoData.Size = MIN ((BLKIF_MAX_SEGMENTS_PER_REQUEST - 1) * EFI_PAGE_SIZE,\r
147 BufferSize);\r
148 }\r
149\r
150 IoData.Buffer = Buffer;\r
151 IoData.Sector = Sector;\r
152 BufferSize -= IoData.Size;\r
153 Buffer = (VOID*) ((UINTN) Buffer + IoData.Size);\r
154 Sector += IoData.Size / 512;\r
155 Status = XenPvBlockIo (&IoData, IsWrite);\r
156 if (EFI_ERROR (Status)) {\r
157 DEBUG ((EFI_D_ERROR, "XenPvBlkDxe: Error durring %a operation.\n",\r
158 IsWrite ? "write" : "read"));\r
159 return Status;\r
160 }\r
161 }\r
162 return EFI_SUCCESS;\r
163}\r
164\r
165\r
166/**\r
167 Read BufferSize bytes from Lba into Buffer.\r
168\r
169 @param This Indicates a pointer to the calling context.\r
170 @param MediaId Id of the media, changes every time the media is replaced.\r
171 @param Lba The starting Logical Block Address to read from\r
172 @param BufferSize Size of Buffer, must be a multiple of device block size.\r
173 @param Buffer A pointer to the destination buffer for the data. The caller is\r
174 responsible for either having implicit or explicit ownership of the buffer.\r
175\r
176 @retval EFI_SUCCESS The data was read correctly from the device.\r
177 @retval EFI_DEVICE_ERROR The device reported an error while performing the read.\r
178 @retval EFI_NO_MEDIA There is no media in the device.\r
179 @retval EFI_MEDIA_CHANGED The MediaId does not matched the current device.\r
180 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.\r
181 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,\r
182 or the buffer is not on proper alignment.\r
183\r
184**/\r
185EFI_STATUS\r
186EFIAPI\r
187XenPvBlkDxeBlockIoReadBlocks (\r
188 IN EFI_BLOCK_IO_PROTOCOL *This,\r
189 IN UINT32 MediaId,\r
190 IN EFI_LBA Lba,\r
191 IN UINTN BufferSize,\r
192 OUT VOID *Buffer\r
193 )\r
194{\r
195 return XenPvBlkDxeBlockIoReadWriteBlocks (This,\r
196 MediaId, Lba, BufferSize, Buffer, FALSE);\r
197}\r
198\r
199/**\r
200 Write BufferSize bytes from Lba into Buffer.\r
201\r
202 @param This Indicates a pointer to the calling context.\r
203 @param MediaId The media ID that the write request is for.\r
204 @param Lba The starting logical block address to be written. The caller is\r
205 responsible for writing to only legitimate locations.\r
206 @param BufferSize Size of Buffer, must be a multiple of device block size.\r
207 @param Buffer A pointer to the source buffer for the data.\r
208\r
209 @retval EFI_SUCCESS The data was written correctly to the device.\r
210 @retval EFI_WRITE_PROTECTED The device can not be written to.\r
211 @retval EFI_DEVICE_ERROR The device reported an error while performing the write.\r
212 @retval EFI_NO_MEDIA There is no media in the device.\r
213 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.\r
214 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.\r
215 @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,\r
216 or the buffer is not on proper alignment.\r
217\r
218**/\r
219EFI_STATUS\r
220EFIAPI\r
221XenPvBlkDxeBlockIoWriteBlocks (\r
222 IN EFI_BLOCK_IO_PROTOCOL *This,\r
223 IN UINT32 MediaId,\r
224 IN EFI_LBA Lba,\r
225 IN UINTN BufferSize,\r
226 IN VOID *Buffer\r
227 )\r
228{\r
229 return XenPvBlkDxeBlockIoReadWriteBlocks (This,\r
230 MediaId, Lba, BufferSize, Buffer, TRUE);\r
231}\r
232\r
233/**\r
234 Flush the Block Device.\r
235\r
236 @param This Indicates a pointer to the calling context.\r
237\r
238 @retval EFI_SUCCESS All outstanding data was written to the device\r
239 @retval EFI_DEVICE_ERROR The device reported an error while writting back the data\r
240 @retval EFI_NO_MEDIA There is no media in the device.\r
241\r
242**/\r
243EFI_STATUS\r
244EFIAPI\r
245XenPvBlkDxeBlockIoFlushBlocks (\r
246 IN EFI_BLOCK_IO_PROTOCOL *This\r
247 )\r
248{\r
249 XenPvBlockSync (XEN_BLOCK_FRONT_FROM_BLOCK_IO (This));\r
250 return EFI_SUCCESS;\r
251}\r
252\r
253/**\r
254 Reset the block device hardware.\r
255\r
256 @param[in] This Indicates a pointer to the calling context.\r
257 @param[in] ExtendedVerification Not used.\r
258\r
259 @retval EFI_SUCCESS The device was reset.\r
260\r
261**/\r
262EFI_STATUS\r
263EFIAPI\r
264XenPvBlkDxeBlockIoReset (\r
265 IN EFI_BLOCK_IO_PROTOCOL *This,\r
266 IN BOOLEAN ExtendedVerification\r
267 )\r
268{\r
269 //\r
270 // Since the initialization of the devices is done, then the device is\r
271 // working correctly.\r
272 //\r
273 return EFI_SUCCESS;\r
274}\r