2 BlockIo implementation for Xen PV Block driver.
4 This file is implementing the interface between the actual driver in
5 BlockFront.c to the BlockIo protocol.
7 Copyright (C) 2014, Citrix Ltd.
9 This program and the accompanying materials
10 are licensed and made available under the terms and conditions of the BSD License
11 which accompanies this distribution. The full text of the license may be found at
12 http://opensource.org/licenses/bsd-license.php
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 #include "XenPvBlkDxe.h"
21 #include "BlockFront.h"
24 /// Block I/O Media structure
26 GLOBAL_REMOVE_IF_UNREFERENCED
27 EFI_BLOCK_IO_MEDIA gXenPvBlkDxeBlockIoMedia
= {
29 FALSE
, // RemovableMedia
30 FALSE
, // MediaPresent
31 FALSE
, // LogicalPartition
33 FALSE
, // WriteCaching
35 512, // IoAlign, BlockFront does not support less than 512 bits-aligned.
37 0, // LowestAlignedLba
38 0, // LogicalBlocksPerPhysicalBlock
39 0 // OptimalTransferLengthGranularity
43 /// Block I/O Protocol instance
45 GLOBAL_REMOVE_IF_UNREFERENCED
46 EFI_BLOCK_IO_PROTOCOL gXenPvBlkDxeBlockIo
= {
47 EFI_BLOCK_IO_PROTOCOL_REVISION3
, // Revision
48 &gXenPvBlkDxeBlockIoMedia
, // Media
49 XenPvBlkDxeBlockIoReset
, // Reset
50 XenPvBlkDxeBlockIoReadBlocks
, // ReadBlocks
51 XenPvBlkDxeBlockIoWriteBlocks
, // WriteBlocks
52 XenPvBlkDxeBlockIoFlushBlocks
// FlushBlocks
59 Read/Write BufferSize bytes from Lba into Buffer.
61 This function is commun to XenPvBlkDxeBlockIoReadBlocks and
62 XenPvBlkDxeBlockIoWriteBlocks.
64 @param This Indicates a pointer to the calling context.
65 @param MediaId Id of the media, changes every time the media is replaced.
66 @param Lba The starting Logical Block Address to read from/write to.
67 @param BufferSize Size of Buffer, must be a multiple of device block size.
68 @param Buffer A pointer to the destination/source buffer for the data.
69 @param IsWrite Indicate if the operation is write or read.
71 @return See description of XenPvBlkDxeBlockIoReadBlocks and
72 XenPvBlkDxeBlockIoWriteBlocks.
76 XenPvBlkDxeBlockIoReadWriteBlocks (
77 IN EFI_BLOCK_IO_PROTOCOL
*This
,
85 XEN_BLOCK_FRONT_IO IoData
;
86 EFI_BLOCK_IO_MEDIA
*Media
= This
->Media
;
91 return EFI_INVALID_PARAMETER
;
93 if (BufferSize
== 0) {
97 if (BufferSize
% Media
->BlockSize
!= 0) {
98 DEBUG ((EFI_D_ERROR
, "XenPvBlkDxe: Bad buffer size: 0x%Lx\n",
100 return EFI_BAD_BUFFER_SIZE
;
103 if (Lba
> Media
->LastBlock
||
104 (BufferSize
/ Media
->BlockSize
) - 1 > Media
->LastBlock
- Lba
) {
106 "XenPvBlkDxe: %a with invalid LBA: 0x%Lx, size: 0x%Lx\n",
107 IsWrite
? "Write" : "Read", Lba
, (UINT64
)BufferSize
));
108 return EFI_INVALID_PARAMETER
;
111 if (IsWrite
&& Media
->ReadOnly
) {
112 return EFI_WRITE_PROTECTED
;
115 if ((Media
->IoAlign
> 1) && (UINTN
)Buffer
& (Media
->IoAlign
- 1)) {
117 // Grub2 does not appear to respect IoAlign of 512, so reallocate the
123 // Try again with a properly aligned buffer.
125 NewBuffer
= AllocateAlignedPages((BufferSize
+ EFI_PAGE_SIZE
) / EFI_PAGE_SIZE
,
128 Status
= XenPvBlkDxeBlockIoReadBlocks (This
, MediaId
,
129 Lba
, BufferSize
, NewBuffer
);
130 CopyMem (Buffer
, NewBuffer
, BufferSize
);
132 CopyMem (NewBuffer
, Buffer
, BufferSize
);
133 Status
= XenPvBlkDxeBlockIoWriteBlocks (This
, MediaId
,
134 Lba
, BufferSize
, NewBuffer
);
136 FreeAlignedPages (NewBuffer
, (BufferSize
+ EFI_PAGE_SIZE
) / EFI_PAGE_SIZE
);
140 IoData
.Dev
= XEN_BLOCK_FRONT_FROM_BLOCK_IO (This
);
141 Sector
= (UINTN
)MultU64x32 (Lba
, Media
->BlockSize
/ 512);
143 while (BufferSize
> 0) {
144 if (((UINTN
)Buffer
& EFI_PAGE_MASK
) == 0) {
145 IoData
.Size
= MIN (BLKIF_MAX_SEGMENTS_PER_REQUEST
* EFI_PAGE_SIZE
,
148 IoData
.Size
= MIN ((BLKIF_MAX_SEGMENTS_PER_REQUEST
- 1) * EFI_PAGE_SIZE
,
152 IoData
.Buffer
= Buffer
;
153 IoData
.Sector
= Sector
;
154 BufferSize
-= IoData
.Size
;
155 Buffer
= (VOID
*) ((UINTN
) Buffer
+ IoData
.Size
);
156 Sector
+= IoData
.Size
/ 512;
157 Status
= XenPvBlockIo (&IoData
, IsWrite
);
158 if (EFI_ERROR (Status
)) {
159 DEBUG ((EFI_D_ERROR
, "XenPvBlkDxe: Error during %a operation.\n",
160 IsWrite
? "write" : "read"));
169 Read BufferSize bytes from Lba into Buffer.
171 @param This Indicates a pointer to the calling context.
172 @param MediaId Id of the media, changes every time the media is replaced.
173 @param Lba The starting Logical Block Address to read from
174 @param BufferSize Size of Buffer, must be a multiple of device block size.
175 @param Buffer A pointer to the destination buffer for the data. The caller is
176 responsible for either having implicit or explicit ownership of the buffer.
178 @retval EFI_SUCCESS The data was read correctly from the device.
179 @retval EFI_DEVICE_ERROR The device reported an error while performing the read.
180 @retval EFI_NO_MEDIA There is no media in the device.
181 @retval EFI_MEDIA_CHANGED The MediaId does not matched the current device.
182 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
183 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,
184 or the buffer is not on proper alignment.
189 XenPvBlkDxeBlockIoReadBlocks (
190 IN EFI_BLOCK_IO_PROTOCOL
*This
,
197 return XenPvBlkDxeBlockIoReadWriteBlocks (This
,
198 MediaId
, Lba
, BufferSize
, Buffer
, FALSE
);
202 Write BufferSize bytes from Lba into Buffer.
204 @param This Indicates a pointer to the calling context.
205 @param MediaId The media ID that the write request is for.
206 @param Lba The starting logical block address to be written. The caller is
207 responsible for writing to only legitimate locations.
208 @param BufferSize Size of Buffer, must be a multiple of device block size.
209 @param Buffer A pointer to the source buffer for the data.
211 @retval EFI_SUCCESS The data was written correctly to the device.
212 @retval EFI_WRITE_PROTECTED The device can not be written to.
213 @retval EFI_DEVICE_ERROR The device reported an error while performing the write.
214 @retval EFI_NO_MEDIA There is no media in the device.
215 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.
216 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
217 @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,
218 or the buffer is not on proper alignment.
223 XenPvBlkDxeBlockIoWriteBlocks (
224 IN EFI_BLOCK_IO_PROTOCOL
*This
,
231 return XenPvBlkDxeBlockIoReadWriteBlocks (This
,
232 MediaId
, Lba
, BufferSize
, Buffer
, TRUE
);
236 Flush the Block Device.
238 @param This Indicates a pointer to the calling context.
240 @retval EFI_SUCCESS All outstanding data was written to the device
241 @retval EFI_DEVICE_ERROR The device reported an error while writting back the data
242 @retval EFI_NO_MEDIA There is no media in the device.
247 XenPvBlkDxeBlockIoFlushBlocks (
248 IN EFI_BLOCK_IO_PROTOCOL
*This
251 XenPvBlockSync (XEN_BLOCK_FRONT_FROM_BLOCK_IO (This
));
256 Reset the block device hardware.
258 @param[in] This Indicates a pointer to the calling context.
259 @param[in] ExtendedVerification Not used.
261 @retval EFI_SUCCESS The device was reset.
266 XenPvBlkDxeBlockIoReset (
267 IN EFI_BLOCK_IO_PROTOCOL
*This
,
268 IN BOOLEAN ExtendedVerification
272 // Since the initialization of the devices is done, then the device is
273 // working correctly.