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%X\n", BufferSize
));
99 return EFI_BAD_BUFFER_SIZE
;
102 if (Lba
> Media
->LastBlock
||
103 (BufferSize
/ Media
->BlockSize
) - 1 > Media
->LastBlock
- Lba
) {
104 DEBUG ((EFI_D_ERROR
, "XenPvBlkDxe: %a with invalid LBA: 0x%LX, size: 0x%x\n",
105 IsWrite
? "Write" : "Read", Lba
, BufferSize
));
106 return EFI_INVALID_PARAMETER
;
109 if (IsWrite
&& Media
->ReadOnly
) {
110 return EFI_WRITE_PROTECTED
;
113 if ((Media
->IoAlign
> 1) && (UINTN
)Buffer
& (Media
->IoAlign
- 1)) {
115 // Grub2 does not appear to respect IoAlign of 512, so reallocate the
121 // Try again with a properly aligned buffer.
123 NewBuffer
= AllocateAlignedPages((BufferSize
+ EFI_PAGE_SIZE
) / EFI_PAGE_SIZE
,
126 Status
= XenPvBlkDxeBlockIoReadBlocks (This
, MediaId
,
127 Lba
, BufferSize
, NewBuffer
);
128 CopyMem (Buffer
, NewBuffer
, BufferSize
);
130 CopyMem (NewBuffer
, Buffer
, BufferSize
);
131 Status
= XenPvBlkDxeBlockIoWriteBlocks (This
, MediaId
,
132 Lba
, BufferSize
, NewBuffer
);
134 FreeAlignedPages (NewBuffer
, (BufferSize
+ EFI_PAGE_SIZE
) / EFI_PAGE_SIZE
);
138 IoData
.Dev
= XEN_BLOCK_FRONT_FROM_BLOCK_IO (This
);
139 Sector
= (UINTN
)MultU64x32 (Lba
, Media
->BlockSize
/ 512);
141 while (BufferSize
> 0) {
142 if (((UINTN
)Buffer
& EFI_PAGE_MASK
) == 0) {
143 IoData
.Size
= MIN (BLKIF_MAX_SEGMENTS_PER_REQUEST
* EFI_PAGE_SIZE
,
146 IoData
.Size
= MIN ((BLKIF_MAX_SEGMENTS_PER_REQUEST
- 1) * EFI_PAGE_SIZE
,
150 IoData
.Buffer
= Buffer
;
151 IoData
.Sector
= Sector
;
152 BufferSize
-= IoData
.Size
;
153 Buffer
= (VOID
*) ((UINTN
) Buffer
+ IoData
.Size
);
154 Sector
+= IoData
.Size
/ 512;
155 Status
= XenPvBlockIo (&IoData
, IsWrite
);
156 if (EFI_ERROR (Status
)) {
157 DEBUG ((EFI_D_ERROR
, "XenPvBlkDxe: Error durring %a operation.\n",
158 IsWrite
? "write" : "read"));
167 Read BufferSize bytes from Lba into Buffer.
169 @param This Indicates a pointer to the calling context.
170 @param MediaId Id of the media, changes every time the media is replaced.
171 @param Lba The starting Logical Block Address to read from
172 @param BufferSize Size of Buffer, must be a multiple of device block size.
173 @param Buffer A pointer to the destination buffer for the data. The caller is
174 responsible for either having implicit or explicit ownership of the buffer.
176 @retval EFI_SUCCESS The data was read correctly from the device.
177 @retval EFI_DEVICE_ERROR The device reported an error while performing the read.
178 @retval EFI_NO_MEDIA There is no media in the device.
179 @retval EFI_MEDIA_CHANGED The MediaId does not matched the current device.
180 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
181 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,
182 or the buffer is not on proper alignment.
187 XenPvBlkDxeBlockIoReadBlocks (
188 IN EFI_BLOCK_IO_PROTOCOL
*This
,
195 return XenPvBlkDxeBlockIoReadWriteBlocks (This
,
196 MediaId
, Lba
, BufferSize
, Buffer
, FALSE
);
200 Write BufferSize bytes from Lba into Buffer.
202 @param This Indicates a pointer to the calling context.
203 @param MediaId The media ID that the write request is for.
204 @param Lba The starting logical block address to be written. The caller is
205 responsible for writing to only legitimate locations.
206 @param BufferSize Size of Buffer, must be a multiple of device block size.
207 @param Buffer A pointer to the source buffer for the data.
209 @retval EFI_SUCCESS The data was written correctly to the device.
210 @retval EFI_WRITE_PROTECTED The device can not be written to.
211 @retval EFI_DEVICE_ERROR The device reported an error while performing the write.
212 @retval EFI_NO_MEDIA There is no media in the device.
213 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.
214 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
215 @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,
216 or the buffer is not on proper alignment.
221 XenPvBlkDxeBlockIoWriteBlocks (
222 IN EFI_BLOCK_IO_PROTOCOL
*This
,
229 return XenPvBlkDxeBlockIoReadWriteBlocks (This
,
230 MediaId
, Lba
, BufferSize
, Buffer
, TRUE
);
234 Flush the Block Device.
236 @param This Indicates a pointer to the calling context.
238 @retval EFI_SUCCESS All outstanding data was written to the device
239 @retval EFI_DEVICE_ERROR The device reported an error while writting back the data
240 @retval EFI_NO_MEDIA There is no media in the device.
245 XenPvBlkDxeBlockIoFlushBlocks (
246 IN EFI_BLOCK_IO_PROTOCOL
*This
249 XenPvBlockSync (XEN_BLOCK_FRONT_FROM_BLOCK_IO (This
));
254 Reset the block device hardware.
256 @param[in] This Indicates a pointer to the calling context.
257 @param[in] ExtendedVerification Not used.
259 @retval EFI_SUCCESS The device was reset.
264 XenPvBlkDxeBlockIoReset (
265 IN EFI_BLOCK_IO_PROTOCOL
*This
,
266 IN BOOLEAN ExtendedVerification
270 // Since the initialization of the devices is done, then the device is
271 // working correctly.