]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Disk/DiskIoDxe/DiskIo.c
Add DiskIo2 protocol definition to MdePkg.
[mirror_edk2.git] / MdeModulePkg / Universal / Disk / DiskIoDxe / DiskIo.c
1 /** @file
2 DiskIo driver that lays on every BlockIo protocol in the system.
3 DiskIo converts a block oriented device to a byte oriented device.
4
5 Disk access may have to handle unaligned request about sector boundaries.
6 There are three cases:
7 UnderRun - The first byte is not on a sector boundary or the read request is
8 less than a sector in length.
9 Aligned - A read of N contiguous sectors.
10 OverRun - The last byte is not on a sector boundary.
11
12 Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>
13 This program and the accompanying materials
14 are licensed and made available under the terms and conditions of the BSD License
15 which accompanies this distribution. The full text of the license may be found at
16 http://opensource.org/licenses/bsd-license.php
17
18 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
19 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
20
21 **/
22
23 #include "DiskIo.h"
24
25 //
26 // Driver binding protocol implementation for DiskIo driver.
27 //
28 EFI_DRIVER_BINDING_PROTOCOL gDiskIoDriverBinding = {
29 DiskIoDriverBindingSupported,
30 DiskIoDriverBindingStart,
31 DiskIoDriverBindingStop,
32 0xa,
33 NULL,
34 NULL
35 };
36
37 //
38 // Template for DiskIo private data structure.
39 // The pointer to BlockIo protocol interface is assigned dynamically.
40 //
41 DISK_IO_PRIVATE_DATA gDiskIoPrivateDataTemplate = {
42 DISK_IO_PRIVATE_DATA_SIGNATURE,
43 {
44 EFI_DISK_IO_PROTOCOL_REVISION,
45 DiskIoReadDisk,
46 DiskIoWriteDisk
47 },
48 {
49 EFI_DISK_IO2_PROTOCOL_REVISION,
50 DiskIo2Cancel,
51 DiskIo2ReadDiskEx,
52 DiskIo2WriteDiskEx,
53 DiskIo2FlushDiskEx
54 }
55 };
56
57 /**
58 Test to see if this driver supports ControllerHandle.
59
60 @param This Protocol instance pointer.
61 @param ControllerHandle Handle of device to test
62 @param RemainingDevicePath Optional parameter use to pick a specific child
63 device to start.
64
65 @retval EFI_SUCCESS This driver supports this device
66 @retval EFI_ALREADY_STARTED This driver is already running on this device
67 @retval other This driver does not support this device
68
69 **/
70 EFI_STATUS
71 EFIAPI
72 DiskIoDriverBindingSupported (
73 IN EFI_DRIVER_BINDING_PROTOCOL *This,
74 IN EFI_HANDLE ControllerHandle,
75 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
76 )
77 {
78 EFI_STATUS Status;
79 EFI_BLOCK_IO_PROTOCOL *BlockIo;
80
81 //
82 // Open the IO Abstraction(s) needed to perform the supported test.
83 //
84 Status = gBS->OpenProtocol (
85 ControllerHandle,
86 &gEfiBlockIoProtocolGuid,
87 (VOID **) &BlockIo,
88 This->DriverBindingHandle,
89 ControllerHandle,
90 EFI_OPEN_PROTOCOL_BY_DRIVER
91 );
92 if (EFI_ERROR (Status)) {
93 return Status;
94 }
95
96 //
97 // Close the I/O Abstraction(s) used to perform the supported test.
98 //
99 gBS->CloseProtocol (
100 ControllerHandle,
101 &gEfiBlockIoProtocolGuid,
102 This->DriverBindingHandle,
103 ControllerHandle
104 );
105 return EFI_SUCCESS;
106 }
107
108
109 /**
110 Start this driver on ControllerHandle by opening a Block IO protocol and
111 installing a Disk IO protocol on ControllerHandle.
112
113 @param This Protocol instance pointer.
114 @param ControllerHandle Handle of device to bind driver to
115 @param RemainingDevicePath Optional parameter use to pick a specific child
116 device to start.
117
118 @retval EFI_SUCCESS This driver is added to ControllerHandle
119 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
120 @retval other This driver does not support this device
121
122 **/
123 EFI_STATUS
124 EFIAPI
125 DiskIoDriverBindingStart (
126 IN EFI_DRIVER_BINDING_PROTOCOL *This,
127 IN EFI_HANDLE ControllerHandle,
128 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
129 )
130 {
131 EFI_STATUS Status;
132 DISK_IO_PRIVATE_DATA *Instance;
133 EFI_TPL OldTpl;
134 EFI_BLOCK_IO_MEDIA *Media;
135
136 Instance = NULL;
137
138 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
139
140 //
141 // Connect to the Block IO and Block IO2 interface on ControllerHandle.
142 //
143 Status = gBS->OpenProtocol (
144 ControllerHandle,
145 &gEfiBlockIoProtocolGuid,
146 (VOID **) &gDiskIoPrivateDataTemplate.BlockIo,
147 This->DriverBindingHandle,
148 ControllerHandle,
149 EFI_OPEN_PROTOCOL_BY_DRIVER
150 );
151 if (EFI_ERROR (Status)) {
152 goto ErrorExit1;
153 }
154
155 Status = gBS->OpenProtocol (
156 ControllerHandle,
157 &gEfiBlockIo2ProtocolGuid,
158 (VOID **) &gDiskIoPrivateDataTemplate.BlockIo2,
159 This->DriverBindingHandle,
160 ControllerHandle,
161 EFI_OPEN_PROTOCOL_BY_DRIVER
162 );
163 if (EFI_ERROR (Status)) {
164 gDiskIoPrivateDataTemplate.BlockIo2 = NULL;
165 }
166
167 //
168 // Initialize the Disk IO device instance.
169 //
170 Instance = AllocateCopyPool (sizeof (DISK_IO_PRIVATE_DATA), &gDiskIoPrivateDataTemplate);
171 if (Instance == NULL) {
172 Status = EFI_OUT_OF_RESOURCES;
173 goto ErrorExit;
174 }
175
176 Media = Instance->BlockIo->Media;
177
178 //
179 // The BlockSize and IoAlign of BlockIo and BlockIo2 should equal.
180 //
181 ASSERT ((Instance->BlockIo2 == NULL) ||
182 ((Instance->BlockIo->Media->IoAlign == Instance->BlockIo2->Media->IoAlign) &&
183 (Instance->BlockIo->Media->BlockSize == Instance->BlockIo2->Media->BlockSize)
184 ));
185
186 InitializeListHead (&Instance->TaskQueue);
187 EfiInitializeLock (&Instance->TaskQueueLock, TPL_NOTIFY);
188 Instance->SharedWorkingBuffer = AllocateAlignedPages (
189 EFI_SIZE_TO_PAGES (DATA_BUFFER_BLOCK_NUM * Instance->BlockIo->Media->BlockSize),
190 Instance->BlockIo->Media->IoAlign
191 );
192 if (Instance->SharedWorkingBuffer == NULL) {
193 Status = EFI_OUT_OF_RESOURCES;
194 goto ErrorExit;
195 }
196
197 //
198 // Install protocol interfaces for the Disk IO device.
199 //
200 if (Instance->BlockIo2 != NULL) {
201 Status = gBS->InstallMultipleProtocolInterfaces (
202 &ControllerHandle,
203 &gEfiDiskIoProtocolGuid, &Instance->DiskIo,
204 &gEfiDiskIo2ProtocolGuid, &Instance->DiskIo2,
205 NULL
206 );
207 } else {
208 Status = gBS->InstallMultipleProtocolInterfaces (
209 &ControllerHandle,
210 &gEfiDiskIoProtocolGuid, &Instance->DiskIo,
211 NULL
212 );
213 }
214
215 ErrorExit:
216 if (EFI_ERROR (Status)) {
217 if (Instance != NULL && Instance->SharedWorkingBuffer != NULL) {
218 FreeAlignedPages (
219 Instance->SharedWorkingBuffer,
220 EFI_SIZE_TO_PAGES (DATA_BUFFER_BLOCK_NUM * Instance->BlockIo->Media->BlockSize)
221 );
222 }
223
224 if (Instance != NULL) {
225 FreePool (Instance);
226 }
227
228 gBS->CloseProtocol (
229 ControllerHandle,
230 &gEfiBlockIoProtocolGuid,
231 This->DriverBindingHandle,
232 ControllerHandle
233 );
234 }
235
236 ErrorExit1:
237 gBS->RestoreTPL (OldTpl);
238 return Status;
239 }
240
241 /**
242 Stop this driver on ControllerHandle by removing Disk IO protocol and closing
243 the Block IO protocol on ControllerHandle.
244
245 @param This Protocol instance pointer.
246 @param ControllerHandle Handle of device to stop driver on
247 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
248 children is zero stop the entire bus driver.
249 @param ChildHandleBuffer List of Child Handles to Stop.
250
251 @retval EFI_SUCCESS This driver is removed ControllerHandle
252 @retval other This driver was not removed from this device
253
254 **/
255 EFI_STATUS
256 EFIAPI
257 DiskIoDriverBindingStop (
258 IN EFI_DRIVER_BINDING_PROTOCOL *This,
259 IN EFI_HANDLE ControllerHandle,
260 IN UINTN NumberOfChildren,
261 IN EFI_HANDLE *ChildHandleBuffer
262 )
263 {
264 EFI_STATUS Status;
265 EFI_DISK_IO_PROTOCOL *DiskIo;
266 EFI_DISK_IO2_PROTOCOL *DiskIo2;
267 DISK_IO_PRIVATE_DATA *Instance;
268 BOOLEAN AllTaskDone;
269
270 //
271 // Get our context back.
272 //
273 Status = gBS->OpenProtocol (
274 ControllerHandle,
275 &gEfiDiskIoProtocolGuid,
276 (VOID **) &DiskIo,
277 This->DriverBindingHandle,
278 ControllerHandle,
279 EFI_OPEN_PROTOCOL_GET_PROTOCOL
280 );
281 if (EFI_ERROR (Status)) {
282 return Status;
283 }
284 Status = gBS->OpenProtocol (
285 ControllerHandle,
286 &gEfiDiskIo2ProtocolGuid,
287 (VOID **) &DiskIo2,
288 This->DriverBindingHandle,
289 ControllerHandle,
290 EFI_OPEN_PROTOCOL_GET_PROTOCOL
291 );
292 if (EFI_ERROR (Status)) {
293 DiskIo2 = NULL;
294 }
295
296 Instance = DISK_IO_PRIVATE_DATA_FROM_DISK_IO (DiskIo);
297
298 if (DiskIo2 != NULL) {
299 //
300 // Call BlockIo2::Reset() to terminate any in-flight non-blocking I/O requests
301 //
302 ASSERT (Instance->BlockIo2 != NULL);
303 Status = Instance->BlockIo2->Reset (Instance->BlockIo2, FALSE);
304 if (EFI_ERROR (Status)) {
305 return Status;
306 }
307 Status = gBS->UninstallMultipleProtocolInterfaces (
308 ControllerHandle,
309 &gEfiDiskIoProtocolGuid, &Instance->DiskIo,
310 &gEfiDiskIo2ProtocolGuid, &Instance->DiskIo2,
311 NULL
312 );
313 } else {
314 Status = gBS->UninstallMultipleProtocolInterfaces (
315 ControllerHandle,
316 &gEfiDiskIoProtocolGuid, &Instance->DiskIo,
317 NULL
318 );
319 }
320 if (!EFI_ERROR (Status)) {
321
322 do {
323 EfiAcquireLock (&Instance->TaskQueueLock);
324 AllTaskDone = IsListEmpty (&Instance->TaskQueue);
325 EfiReleaseLock (&Instance->TaskQueueLock);
326 } while (!AllTaskDone);
327
328 FreeAlignedPages (
329 Instance->SharedWorkingBuffer,
330 EFI_SIZE_TO_PAGES (DATA_BUFFER_BLOCK_NUM * Instance->BlockIo->Media->BlockSize)
331 );
332
333 Status = gBS->CloseProtocol (
334 ControllerHandle,
335 &gEfiBlockIoProtocolGuid,
336 This->DriverBindingHandle,
337 ControllerHandle
338 );
339 ASSERT_EFI_ERROR (Status);
340 if (DiskIo2 != NULL) {
341 Status = gBS->CloseProtocol (
342 ControllerHandle,
343 &gEfiBlockIo2ProtocolGuid,
344 This->DriverBindingHandle,
345 ControllerHandle
346 );
347 ASSERT_EFI_ERROR (Status);
348 }
349
350 FreePool (Instance);
351 }
352
353 return Status;
354 }
355
356
357 /**
358 Destroy the sub task.
359
360 @param Subtask Subtask.
361
362 @return LIST_ENTRY * Pointer to the next link of subtask.
363 **/
364 LIST_ENTRY *
365 DiskIoDestroySubtask (
366 IN DISK_IO_PRIVATE_DATA *Instance,
367 IN DISK_IO_SUBTASK *Subtask
368 )
369 {
370 LIST_ENTRY *Link;
371 Link = RemoveEntryList (&Subtask->Link);
372 if (!Subtask->Blocking) {
373 if (Subtask->WorkingBuffer != NULL) {
374 FreeAlignedPages (
375 Subtask->WorkingBuffer,
376 Subtask->Length < Instance->BlockIo->Media->BlockSize
377 ? EFI_SIZE_TO_PAGES (Instance->BlockIo->Media->BlockSize)
378 : EFI_SIZE_TO_PAGES (Subtask->Length)
379 );
380 }
381 if (Subtask->BlockIo2Token.Event != NULL) {
382 gBS->CloseEvent (Subtask->BlockIo2Token.Event);
383 }
384 }
385 FreePool (Subtask);
386
387 return Link;
388 }
389
390 /**
391 The callback for the BlockIo2 ReadBlocksEx/WriteBlocksEx.
392 @param Event Event whose notification function is being invoked.
393 @param Context The pointer to the notification function's context,
394 which points to the DISK_IO_SUBTASK instance.
395 **/
396 VOID
397 EFIAPI
398 DiskIo2OnReadWriteComplete (
399 IN EFI_EVENT Event,
400 IN VOID *Context
401 )
402 {
403 DISK_IO_SUBTASK *Subtask;
404 DISK_IO2_TASK *Task;
405 EFI_STATUS TransactionStatus;
406 DISK_IO_PRIVATE_DATA *Instance;
407
408 gBS->CloseEvent (Event);
409
410 Subtask = (DISK_IO_SUBTASK *) Context;
411 TransactionStatus = Subtask->BlockIo2Token.TransactionStatus;
412 Task = Subtask->Task;
413 Instance = Task->Instance;
414
415 ASSERT (Subtask->Signature == DISK_IO_SUBTASK_SIGNATURE);
416 ASSERT (Instance->Signature == DISK_IO_PRIVATE_DATA_SIGNATURE);
417 ASSERT (Task->Signature == DISK_IO2_TASK_SIGNATURE);
418
419 if (Subtask->WorkingBuffer != NULL) {
420 if (!EFI_ERROR (TransactionStatus) && (Task->Token != NULL) && !Subtask->Write) {
421 CopyMem (Subtask->Buffer, Subtask->WorkingBuffer + Subtask->Offset, Subtask->Length);
422 }
423
424 //
425 // The WorkingBuffer of blocking subtask either points to SharedWorkingBuffer
426 // or will be used by non-blocking subtask which will be freed below.
427 //
428 if (!Subtask->Blocking) {
429 FreeAlignedPages (
430 Subtask->WorkingBuffer,
431 Subtask->Length < Instance->BlockIo->Media->BlockSize
432 ? EFI_SIZE_TO_PAGES (Instance->BlockIo->Media->BlockSize)
433 : EFI_SIZE_TO_PAGES (Subtask->Length)
434 );
435 }
436 }
437 RemoveEntryList (&Subtask->Link);
438 FreePool (Subtask);
439
440 if (EFI_ERROR (TransactionStatus) || IsListEmpty (&Task->Subtasks)) {
441 if (Task->Token != NULL) {
442 //
443 // Signal error status once the subtask is failed.
444 // Or signal the last status once the last subtask is finished.
445 //
446 Task->Token->TransactionStatus = TransactionStatus;
447 gBS->SignalEvent (Task->Token->Event);
448
449 //
450 // Mark token to NULL
451 //
452 Task->Token = NULL;
453 }
454 }
455
456 if (IsListEmpty (&Task->Subtasks)) {
457 EfiAcquireLock (&Instance->TaskQueueLock);
458 RemoveEntryList (&Task->Link);
459 EfiReleaseLock (&Instance->TaskQueueLock);
460
461 FreePool (Task);
462 }
463 }
464
465 /**
466 Create the subtask.
467
468 @param Write TRUE: Write request; FALSE: Read request.
469 @param Lba The starting logical block address to read from on the device.
470 @param Offset The starting byte offset to read from the LBA.
471 @param Length The number of bytes to read from the device.
472 @param WorkingBuffer The aligned buffer to hold the data for reading or writing.
473 @param Buffer The buffer to hold the data for reading or writing.
474 @param Blocking TRUE: Blocking request; FALSE: Non-blocking request.
475
476 @return A pointer to the created subtask.
477 **/
478 DISK_IO_SUBTASK *
479 DiskIoCreateSubtask (
480 IN BOOLEAN Write,
481 IN UINT64 Lba,
482 IN UINT32 Offset,
483 IN UINTN Length,
484 IN VOID *WorkingBuffer, OPTIONAL
485 IN VOID *Buffer,
486 IN BOOLEAN Blocking
487 )
488 {
489 DISK_IO_SUBTASK *Subtask;
490 EFI_STATUS Status;
491
492 Subtask = AllocateZeroPool (sizeof (DISK_IO_SUBTASK));
493 if (Subtask == NULL) {
494 return NULL;
495 }
496 Subtask->Signature = DISK_IO_SUBTASK_SIGNATURE;
497 Subtask->Write = Write;
498 Subtask->Lba = Lba;
499 Subtask->Offset = Offset;
500 Subtask->Length = Length;
501 Subtask->WorkingBuffer = WorkingBuffer;
502 Subtask->Buffer = Buffer;
503 Subtask->Blocking = Blocking;
504 if (!Blocking) {
505 Status = gBS->CreateEvent (
506 EVT_NOTIFY_SIGNAL,
507 TPL_NOTIFY,
508 DiskIo2OnReadWriteComplete,
509 Subtask,
510 &Subtask->BlockIo2Token.Event
511 );
512 if (EFI_ERROR (Status)) {
513 FreePool (Subtask);
514 return NULL;
515 }
516 }
517 DEBUG ((
518 EFI_D_BLKIO,
519 " %c:Lba/Offset/Length/WorkingBuffer/Buffer = %016lx/%08x/%08x/%08x/%08x\n",
520 Write ? 'W': 'R', Lba, Offset, Length, WorkingBuffer, Buffer
521 ));
522
523 return Subtask;
524 }
525
526 /**
527 Create the subtask list.
528
529 @param Instance Pointer to the DISK_IO_PRIVATE_DATA.
530 @param Write TRUE: Write request; FALSE: Read request.
531 @param Offset The starting byte offset to read from the device.
532 @param BufferSize The size in bytes of Buffer. The number of bytes to read from the device.
533 @param Buffer A pointer to the buffer for the data.
534 @param Blocking TRUE: Blocking request; FALSE: Non-blocking request.
535 @param SharedWorkingBuffer The aligned buffer to hold the data for reading or writing.
536 @param Subtasks The subtask list header.
537
538 @retval TRUE The subtask list is created successfully.
539 @retval FALSE The subtask list is not created.
540 **/
541 BOOLEAN
542 DiskIoCreateSubtaskList (
543 IN DISK_IO_PRIVATE_DATA *Instance,
544 IN BOOLEAN Write,
545 IN UINT64 Offset,
546 IN UINTN BufferSize,
547 IN VOID *Buffer,
548 IN BOOLEAN Blocking,
549 IN VOID *SharedWorkingBuffer,
550 IN OUT LIST_ENTRY *Subtasks
551 )
552 {
553 UINT32 BlockSize;
554 UINT32 IoAlign;
555 UINT64 Lba;
556 UINT64 OverRunLba;
557 UINT32 UnderRun;
558 UINT32 OverRun;
559 UINT8 *BufferPtr;
560 UINTN Length;
561 UINTN DataBufferSize;
562 DISK_IO_SUBTASK *Subtask;
563 VOID *WorkingBuffer;
564 LIST_ENTRY *Link;
565
566 DEBUG ((EFI_D_BLKIO, "DiskIo: Create subtasks for task: Offset/BufferSize/Buffer = %016lx/%08x/%08x\n", Offset, BufferSize, Buffer));
567
568 BlockSize = Instance->BlockIo->Media->BlockSize;
569 IoAlign = Instance->BlockIo->Media->IoAlign;
570 if (IoAlign == 0) {
571 IoAlign = 1;
572 }
573
574 Lba = DivU64x32Remainder (Offset, BlockSize, &UnderRun);
575 BufferPtr = (UINT8 *) Buffer;
576
577 //
578 // Special handling for zero BufferSize
579 //
580 if (BufferSize == 0) {
581 Subtask = DiskIoCreateSubtask (Write, Lba, UnderRun, 0, NULL, BufferPtr, Blocking);
582 if (Subtask == NULL) {
583 goto Done;
584 }
585 InsertTailList (Subtasks, &Subtask->Link);
586 return TRUE;
587 }
588
589 if (UnderRun != 0) {
590 Length = MIN (BlockSize - UnderRun, BufferSize);
591 if (Blocking) {
592 WorkingBuffer = SharedWorkingBuffer;
593 } else {
594 WorkingBuffer = AllocateAlignedPages (EFI_SIZE_TO_PAGES (BlockSize), IoAlign);
595 if (WorkingBuffer == NULL) {
596 goto Done;
597 }
598 }
599 if (Write) {
600 //
601 // A half write operation can be splitted to a blocking block-read and half write operation
602 // This can simplify the sub task processing logic
603 //
604 Subtask = DiskIoCreateSubtask (FALSE, Lba, 0, BlockSize, NULL, WorkingBuffer, TRUE);
605 if (Subtask == NULL) {
606 goto Done;
607 }
608 InsertTailList (Subtasks, &Subtask->Link);
609 }
610
611 Subtask = DiskIoCreateSubtask (Write, Lba, UnderRun, Length, WorkingBuffer, BufferPtr, Blocking);
612 if (Subtask == NULL) {
613 goto Done;
614 }
615 InsertTailList (Subtasks, &Subtask->Link);
616
617 BufferPtr += Length;
618 Offset += Length;
619 BufferSize -= Length;
620 Lba ++;
621 }
622
623 OverRunLba = Lba + DivU64x32Remainder (BufferSize, BlockSize, &OverRun);
624 BufferSize -= OverRun;
625
626 if (OverRun != 0) {
627 if (Blocking) {
628 WorkingBuffer = SharedWorkingBuffer;
629 } else {
630 WorkingBuffer = AllocateAlignedPages (EFI_SIZE_TO_PAGES (BlockSize), IoAlign);
631 if (WorkingBuffer == NULL) {
632 goto Done;
633 }
634 }
635 if (Write) {
636 //
637 // A half write operation can be splitted to a blocking block-read and half write operation
638 // This can simplify the sub task processing logic
639 //
640 Subtask = DiskIoCreateSubtask (FALSE, OverRunLba, 0, BlockSize, NULL, WorkingBuffer, TRUE);
641 if (Subtask == NULL) {
642 goto Done;
643 }
644 InsertTailList (Subtasks, &Subtask->Link);
645 }
646
647 Subtask = DiskIoCreateSubtask (Write, OverRunLba, 0, OverRun, WorkingBuffer, BufferPtr, Blocking);
648 if (Subtask == NULL) {
649 goto Done;
650 }
651 InsertTailList (Subtasks, &Subtask->Link);
652 }
653
654 if (OverRunLba > Lba) {
655 //
656 // If the DiskIo maps directly to a BlockIo device do the read.
657 //
658 if (ALIGN_POINTER (BufferPtr, IoAlign) == BufferPtr) {
659 Subtask = DiskIoCreateSubtask (Write, Lba, 0, BufferSize, NULL, BufferPtr, Blocking);
660 if (Subtask == NULL) {
661 goto Done;
662 }
663 InsertTailList (Subtasks, &Subtask->Link);
664
665 BufferPtr += BufferSize;
666 Offset += BufferSize;
667 BufferSize -= BufferSize;
668
669 } else {
670 if (Blocking) {
671 //
672 // Use the allocated buffer instead of the original buffer
673 // to avoid alignment issue.
674 //
675 for (; Lba < OverRunLba; Lba += DATA_BUFFER_BLOCK_NUM) {
676 DataBufferSize = MIN (BufferSize, DATA_BUFFER_BLOCK_NUM * BlockSize);
677
678 Subtask = DiskIoCreateSubtask (Write, Lba, 0, DataBufferSize, SharedWorkingBuffer, BufferPtr, Blocking);
679 if (Subtask == NULL) {
680 goto Done;
681 }
682 InsertTailList (Subtasks, &Subtask->Link);
683
684 BufferPtr += DataBufferSize;
685 Offset += DataBufferSize;
686 BufferSize -= DataBufferSize;
687 }
688 } else {
689 WorkingBuffer = AllocateAlignedPages (EFI_SIZE_TO_PAGES (BufferSize), IoAlign);
690 if (WorkingBuffer == NULL) {
691 //
692 // If there is not enough memory, downgrade to blocking access
693 //
694 DEBUG ((EFI_D_VERBOSE, "DiskIo: No enough memory so downgrade to blocking access\n"));
695 if (!DiskIoCreateSubtaskList (Instance, Write, Offset, BufferSize, BufferPtr, TRUE, SharedWorkingBuffer, Subtasks)) {
696 goto Done;
697 }
698 } else {
699 Subtask = DiskIoCreateSubtask (Write, Lba, 0, BufferSize, WorkingBuffer, BufferPtr, Blocking);
700 if (Subtask == NULL) {
701 goto Done;
702 }
703 InsertTailList (Subtasks, &Subtask->Link);
704 }
705
706 BufferPtr += BufferSize;
707 Offset += BufferSize;
708 BufferSize -= BufferSize;
709 }
710 }
711 }
712
713 ASSERT (BufferSize == 0);
714
715 return TRUE;
716
717 Done:
718 //
719 // Remove all the subtasks.
720 //
721 for (Link = GetFirstNode (Subtasks); !IsNull (Subtasks, Link); ) {
722 Subtask = CR (Link, DISK_IO_SUBTASK, Link, DISK_IO_SUBTASK_SIGNATURE);
723 Link = DiskIoDestroySubtask (Instance, Subtask);
724 }
725 return FALSE;
726 }
727
728 /**
729 Terminate outstanding asynchronous requests to a device.
730
731 @param This Indicates a pointer to the calling context.
732
733 @retval EFI_SUCCESS All outstanding requests were successfully terminated.
734 @retval EFI_DEVICE_ERROR The device reported an error while performing the cancel
735 operation.
736 **/
737 EFI_STATUS
738 EFIAPI
739 DiskIo2Cancel (
740 IN EFI_DISK_IO2_PROTOCOL *This
741 )
742 {
743 DISK_IO_PRIVATE_DATA *Instance;
744 DISK_IO2_TASK *Task;
745 LIST_ENTRY *Link;
746
747 Instance = DISK_IO_PRIVATE_DATA_FROM_DISK_IO2 (This);
748
749 EfiAcquireLock (&Instance->TaskQueueLock);
750
751 for (Link = GetFirstNode (&Instance->TaskQueue)
752 ; !IsNull (&Instance->TaskQueue, Link)
753 ; Link = GetNextNode (&Instance->TaskQueue, Link)
754 ) {
755 Task = CR (Link, DISK_IO2_TASK, Link, DISK_IO2_TASK_SIGNATURE);
756
757 if (Task->Token != NULL) {
758 Task->Token->TransactionStatus = EFI_ABORTED;
759 gBS->SignalEvent (Task->Token->Event);
760 //
761 // Set Token to NULL so that the further BlockIo2 responses will be ignored
762 //
763 Task->Token = NULL;
764 }
765 }
766
767 EfiReleaseLock (&Instance->TaskQueueLock);
768
769 return EFI_SUCCESS;
770 }
771
772 /**
773 Common routine to access the disk.
774
775 @param Instance Pointer to the DISK_IO_PRIVATE_DATA.
776 @param Write TRUE: Write operation; FALSE: Read operation.
777 @param MediaId ID of the medium to access.
778 @param Offset The starting byte offset on the logical block I/O device to access.
779 @param Token A pointer to the token associated with the transaction.
780 If this field is NULL, synchronous/blocking IO is performed.
781 @param BufferSize The size in bytes of Buffer. The number of bytes to read from the device.
782 @param Buffer A pointer to the destination buffer for the data.
783 The caller is responsible either having implicit or explicit ownership of the buffer.
784 **/
785 EFI_STATUS
786 DiskIo2ReadWriteDisk (
787 IN DISK_IO_PRIVATE_DATA *Instance,
788 IN BOOLEAN Write,
789 IN UINT32 MediaId,
790 IN UINT64 Offset,
791 IN EFI_DISK_IO2_TOKEN *Token,
792 IN UINTN BufferSize,
793 IN UINT8 *Buffer
794 )
795 {
796 EFI_STATUS Status;
797 EFI_BLOCK_IO_PROTOCOL *BlockIo;
798 EFI_BLOCK_IO2_PROTOCOL *BlockIo2;
799 EFI_BLOCK_IO_MEDIA *Media;
800 LIST_ENTRY *Link;
801 LIST_ENTRY Subtasks;
802 DISK_IO_SUBTASK *Subtask;
803 DISK_IO2_TASK *Task;
804 BOOLEAN TaskQueueEmpty;
805 EFI_TPL OldTpl;
806 BOOLEAN Blocking;
807 LIST_ENTRY *SubtasksPtr;
808
809 Task = NULL;
810 BlockIo = Instance->BlockIo;
811 BlockIo2 = Instance->BlockIo2;
812 Media = BlockIo->Media;
813 Status = EFI_SUCCESS;
814 Blocking = ((Token == NULL) || (Token->Event == NULL));
815
816 if (Media->MediaId != MediaId) {
817 return EFI_MEDIA_CHANGED;
818 }
819
820 if (Write && Media->ReadOnly) {
821 return EFI_WRITE_PROTECTED;
822 }
823
824 if (Blocking) {
825 //
826 // Wait till pending async task is completed.
827 //
828 do {
829 EfiAcquireLock (&Instance->TaskQueueLock);
830 TaskQueueEmpty = IsListEmpty (&Instance->TaskQueue);
831 EfiReleaseLock (&Instance->TaskQueueLock);
832 } while (!TaskQueueEmpty);
833
834 SubtasksPtr = &Subtasks;
835 } else {
836 Task = AllocatePool (sizeof (DISK_IO2_TASK));
837 if (Task == NULL) {
838 return EFI_OUT_OF_RESOURCES;
839 }
840
841 EfiAcquireLock (&Instance->TaskQueueLock);
842 InsertTailList (&Instance->TaskQueue, &Task->Link);
843 EfiReleaseLock (&Instance->TaskQueueLock);
844
845 Task->Signature = DISK_IO2_TASK_SIGNATURE;
846 Task->Instance = Instance;
847 Task->Token = Token;
848
849 SubtasksPtr = &Task->Subtasks;
850 }
851
852 InitializeListHead (SubtasksPtr);
853 if (!DiskIoCreateSubtaskList (Instance, Write, Offset, BufferSize, Buffer, Blocking, Instance->SharedWorkingBuffer, SubtasksPtr)) {
854 if (Task != NULL) {
855 FreePool (Task);
856 }
857 return EFI_OUT_OF_RESOURCES;
858 }
859 ASSERT (!IsListEmpty (SubtasksPtr));
860
861 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
862 for (Link = GetFirstNode (SubtasksPtr); !IsNull (SubtasksPtr, Link); Link = GetNextNode (SubtasksPtr, Link)) {
863 Subtask = CR (Link, DISK_IO_SUBTASK, Link, DISK_IO_SUBTASK_SIGNATURE);
864
865 if (!Subtask->Blocking) {
866 Subtask->Task = Task;
867 }
868
869 if (Subtask->Write) {
870 //
871 // Write
872 //
873 if (Subtask->WorkingBuffer != NULL) {
874 //
875 // A sub task before this one should be a block read operation, causing the WorkingBuffer filled with the entire one block data.
876 //
877 CopyMem (Subtask->WorkingBuffer + Subtask->Offset, Subtask->Buffer, Subtask->Length);
878 }
879
880 if (Subtask->Blocking) {
881 Status = BlockIo->WriteBlocks (
882 BlockIo,
883 MediaId,
884 Subtask->Lba,
885 Subtask->Length < Media->BlockSize ? Media->BlockSize : Subtask->Length,
886 (Subtask->WorkingBuffer != NULL) ? Subtask->WorkingBuffer : Subtask->Buffer
887 );
888 } else {
889 Status = BlockIo2->WriteBlocksEx (
890 BlockIo2,
891 MediaId,
892 Subtask->Lba,
893 &Subtask->BlockIo2Token,
894 Subtask->Length < Media->BlockSize ? Media->BlockSize : Subtask->Length,
895 (Subtask->WorkingBuffer != NULL) ? Subtask->WorkingBuffer : Subtask->Buffer
896 );
897 }
898
899 } else {
900 //
901 // Read
902 //
903 if (Subtask->Blocking) {
904 Status = BlockIo->ReadBlocks (
905 BlockIo,
906 MediaId,
907 Subtask->Lba,
908 Subtask->Length < Media->BlockSize ? Media->BlockSize : Subtask->Length,
909 (Subtask->WorkingBuffer != NULL) ? Subtask->WorkingBuffer : Subtask->Buffer
910 );
911 if (!EFI_ERROR (Status) && (Subtask->WorkingBuffer != NULL)) {
912 CopyMem (Subtask->Buffer, Subtask->WorkingBuffer + Subtask->Offset, Subtask->Length);
913 }
914 } else {
915 Status = BlockIo2->ReadBlocksEx (
916 BlockIo2,
917 MediaId,
918 Subtask->Lba,
919 &Subtask->BlockIo2Token,
920 Subtask->Length < Media->BlockSize ? Media->BlockSize : Subtask->Length,
921 (Subtask->WorkingBuffer != NULL) ? Subtask->WorkingBuffer : Subtask->Buffer
922 );
923 }
924 }
925
926 if (EFI_ERROR (Status)) {
927 break;
928 }
929 }
930
931 gBS->RaiseTPL (TPL_NOTIFY);
932
933 //
934 // Remove all the remaining subtasks when failure.
935 // We shouldn't remove all the tasks because the non-blocking requests have been submitted and cannot be canceled.
936 //
937 if (EFI_ERROR (Status)) {
938 while (!IsNull (SubtasksPtr, Link)) {
939 Subtask = CR (Link, DISK_IO_SUBTASK, Link, DISK_IO_SUBTASK_SIGNATURE);
940 Link = DiskIoDestroySubtask (Instance, Subtask);
941 }
942 }
943
944 //
945 // Remove all the blocking subtasks because the non-blocking callback only removes the non-blocking subtask.
946 //
947 for (Link = GetFirstNode (SubtasksPtr); !IsNull (SubtasksPtr, Link); ) {
948 Subtask = CR (Link, DISK_IO_SUBTASK, Link, DISK_IO_SUBTASK_SIGNATURE);
949 if (Subtask->Blocking) {
950 Link = DiskIoDestroySubtask (Instance, Subtask);
951 } else {
952 Link = GetNextNode (SubtasksPtr, Link);
953 }
954 }
955
956 //
957 // It's possible that the callback runs before raising TPL to NOTIFY,
958 // so the subtasks list only contains blocking subtask.
959 // Remove the Task after the blocking subtasks are removed in above.
960 //
961 if (!Blocking && IsListEmpty (SubtasksPtr)) {
962 EfiAcquireLock (&Instance->TaskQueueLock);
963 RemoveEntryList (&Task->Link);
964 EfiReleaseLock (&Instance->TaskQueueLock);
965
966 if (Task->Token != NULL) {
967 //
968 // Task->Token should be set to NULL by the DiskIo2OnReadWriteComplete
969 // It it's not, that means the non-blocking request was downgraded to blocking request.
970 //
971 DEBUG ((EFI_D_VERBOSE, "DiskIo: Non-blocking request was downgraded to blocking request, signal event directly.\n"));
972 Task->Token->TransactionStatus = Status;
973 gBS->SignalEvent (Task->Token->Event);
974 }
975
976 FreePool (Task);
977 }
978
979 gBS->RestoreTPL (OldTpl);
980
981 return Status;
982 }
983
984 /**
985 Reads a specified number of bytes from a device.
986
987 @param This Indicates a pointer to the calling context.
988 @param MediaId ID of the medium to be read.
989 @param Offset The starting byte offset on the logical block I/O device to read from.
990 @param Token A pointer to the token associated with the transaction.
991 If this field is NULL, synchronous/blocking IO is performed.
992 @param BufferSize The size in bytes of Buffer. The number of bytes to read from the device.
993 @param Buffer A pointer to the destination buffer for the data.
994 The caller is responsible either having implicit or explicit ownership of the buffer.
995
996 @retval EFI_SUCCESS If Event is NULL (blocking I/O): The data was read correctly from the device.
997 If Event is not NULL (asynchronous I/O): The request was successfully queued for processing.
998 Event will be signaled upon completion.
999 @retval EFI_DEVICE_ERROR The device reported an error while performing the write.
1000 @retval EFI_NO_MEDIA There is no medium in the device.
1001 @retval EFI_MEDIA_CHNAGED The MediaId is not for the current medium.
1002 @retval EFI_INVALID_PARAMETER The read request contains device addresses that are not valid for the device.
1003 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
1004
1005 **/
1006 EFI_STATUS
1007 EFIAPI
1008 DiskIo2ReadDiskEx (
1009 IN EFI_DISK_IO2_PROTOCOL *This,
1010 IN UINT32 MediaId,
1011 IN UINT64 Offset,
1012 IN OUT EFI_DISK_IO2_TOKEN *Token,
1013 IN UINTN BufferSize,
1014 OUT VOID *Buffer
1015 )
1016 {
1017 return DiskIo2ReadWriteDisk (
1018 DISK_IO_PRIVATE_DATA_FROM_DISK_IO2 (This),
1019 FALSE, MediaId, Offset, Token, BufferSize, (UINT8 *) Buffer
1020 );
1021 }
1022
1023 /**
1024 Writes a specified number of bytes to a device.
1025
1026 @param This Indicates a pointer to the calling context.
1027 @param MediaId ID of the medium to be written.
1028 @param Offset The starting byte offset on the logical block I/O device to write to.
1029 @param Token A pointer to the token associated with the transaction.
1030 If this field is NULL, synchronous/blocking IO is performed.
1031 @param BufferSize The size in bytes of Buffer. The number of bytes to write to the device.
1032 @param Buffer A pointer to the buffer containing the data to be written.
1033
1034 @retval EFI_SUCCESS If Event is NULL (blocking I/O): The data was written correctly to the device.
1035 If Event is not NULL (asynchronous I/O): The request was successfully queued for processing.
1036 Event will be signaled upon completion.
1037 @retval EFI_WRITE_PROTECTED The device cannot be written to.
1038 @retval EFI_DEVICE_ERROR The device reported an error while performing the write operation.
1039 @retval EFI_NO_MEDIA There is no medium in the device.
1040 @retval EFI_MEDIA_CHNAGED The MediaId is not for the current medium.
1041 @retval EFI_INVALID_PARAMETER The write request contains device addresses that are not valid for the device.
1042 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
1043
1044 **/
1045 EFI_STATUS
1046 EFIAPI
1047 DiskIo2WriteDiskEx (
1048 IN EFI_DISK_IO2_PROTOCOL *This,
1049 IN UINT32 MediaId,
1050 IN UINT64 Offset,
1051 IN OUT EFI_DISK_IO2_TOKEN *Token,
1052 IN UINTN BufferSize,
1053 IN VOID *Buffer
1054 )
1055 {
1056 return DiskIo2ReadWriteDisk (
1057 DISK_IO_PRIVATE_DATA_FROM_DISK_IO2 (This),
1058 TRUE, MediaId, Offset, Token, BufferSize, (UINT8 *) Buffer
1059 );
1060 }
1061
1062 /**
1063 The callback for the BlockIo2 FlushBlocksEx.
1064 @param Event Event whose notification function is being invoked.
1065 @param Context The pointer to the notification function's context,
1066 which points to the DISK_IO2_FLUSH_TASK instance.
1067 **/
1068 VOID
1069 EFIAPI
1070 DiskIo2OnFlushComplete (
1071 IN EFI_EVENT Event,
1072 IN VOID *Context
1073 )
1074 {
1075 DISK_IO2_FLUSH_TASK *Task;
1076
1077 gBS->CloseEvent (Event);
1078
1079 Task = (DISK_IO2_FLUSH_TASK *) Context;
1080 ASSERT (Task->Signature == DISK_IO2_FLUSH_TASK_SIGNATURE);
1081 Task->Token->TransactionStatus = Task->BlockIo2Token.TransactionStatus;
1082 gBS->SignalEvent (Task->Token->Event);
1083 }
1084
1085 /**
1086 Flushes all modified data to the physical device.
1087
1088 @param This Indicates a pointer to the calling context.
1089 @param Token A pointer to the token associated with the transaction.
1090 If this field is NULL, synchronous/blocking IO is performed.
1091
1092 @retval EFI_SUCCESS If Event is NULL (blocking I/O): The data was flushed successfully to the device.
1093 If Event is not NULL (asynchronous I/O): The request was successfully queued for processing.
1094 Event will be signaled upon completion.
1095 @retval EFI_WRITE_PROTECTED The device cannot be written to.
1096 @retval EFI_DEVICE_ERROR The device reported an error while performing the write operation.
1097 @retval EFI_NO_MEDIA There is no medium in the device.
1098 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
1099 **/
1100 EFI_STATUS
1101 EFIAPI
1102 DiskIo2FlushDiskEx (
1103 IN EFI_DISK_IO2_PROTOCOL *This,
1104 IN OUT EFI_DISK_IO2_TOKEN *Token
1105 )
1106 {
1107 EFI_STATUS Status;
1108 DISK_IO2_FLUSH_TASK *Task;
1109 DISK_IO_PRIVATE_DATA *Private;
1110
1111 Private = DISK_IO_PRIVATE_DATA_FROM_DISK_IO2 (This);
1112
1113 if ((Token != NULL) && (Token->Event != NULL)) {
1114 Task = AllocatePool (sizeof (DISK_IO2_FLUSH_TASK));
1115 if (Task == NULL) {
1116 return EFI_OUT_OF_RESOURCES;
1117 }
1118
1119 Status = gBS->CreateEvent (
1120 EVT_NOTIFY_SIGNAL,
1121 TPL_CALLBACK,
1122 DiskIo2OnFlushComplete,
1123 Task,
1124 &Task->BlockIo2Token.Event
1125 );
1126 if (EFI_ERROR (Status)) {
1127 FreePool (Task);
1128 return Status;
1129 }
1130 Task->Signature = DISK_IO2_FLUSH_TASK_SIGNATURE;
1131 Status = Private->BlockIo2->FlushBlocksEx (Private->BlockIo2, &Task->BlockIo2Token);
1132 if (EFI_ERROR (Status)) {
1133 gBS->CloseEvent (Task->BlockIo2Token.Event);
1134 FreePool (Task);
1135 }
1136 } else {
1137 Status = Private->BlockIo2->FlushBlocksEx (Private->BlockIo2, NULL);
1138 }
1139
1140 return Status;
1141 }
1142
1143 /**
1144 Read BufferSize bytes from Offset into Buffer.
1145 Reads may support reads that are not aligned on
1146 sector boundaries. There are three cases:
1147 UnderRun - The first byte is not on a sector boundary or the read request is
1148 less than a sector in length.
1149 Aligned - A read of N contiguous sectors.
1150 OverRun - The last byte is not on a sector boundary.
1151
1152 @param This Protocol instance pointer.
1153 @param MediaId Id of the media, changes every time the media is replaced.
1154 @param Offset The starting byte offset to read from
1155 @param BufferSize Size of Buffer
1156 @param Buffer Buffer containing read data
1157
1158 @retval EFI_SUCCESS The data was read correctly from the device.
1159 @retval EFI_DEVICE_ERROR The device reported an error while performing the read.
1160 @retval EFI_NO_MEDIA There is no media in the device.
1161 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.
1162 @retval EFI_INVALID_PARAMETER The read request contains device addresses that are not
1163 valid for the device.
1164
1165 **/
1166 EFI_STATUS
1167 EFIAPI
1168 DiskIoReadDisk (
1169 IN EFI_DISK_IO_PROTOCOL *This,
1170 IN UINT32 MediaId,
1171 IN UINT64 Offset,
1172 IN UINTN BufferSize,
1173 OUT VOID *Buffer
1174 )
1175 {
1176 return DiskIo2ReadWriteDisk (
1177 DISK_IO_PRIVATE_DATA_FROM_DISK_IO (This),
1178 FALSE, MediaId, Offset, NULL, BufferSize, (UINT8 *) Buffer
1179 );
1180 }
1181
1182
1183 /**
1184 Writes BufferSize bytes from Buffer into Offset.
1185 Writes may require a read modify write to support writes that are not
1186 aligned on sector boundaries. There are three cases:
1187 UnderRun - The first byte is not on a sector boundary or the write request
1188 is less than a sector in length. Read modify write is required.
1189 Aligned - A write of N contiguous sectors.
1190 OverRun - The last byte is not on a sector boundary. Read modified write
1191 required.
1192
1193 @param This Protocol instance pointer.
1194 @param MediaId Id of the media, changes every time the media is replaced.
1195 @param Offset The starting byte offset to read from
1196 @param BufferSize Size of Buffer
1197 @param Buffer Buffer containing read data
1198
1199 @retval EFI_SUCCESS The data was written correctly to the device.
1200 @retval EFI_WRITE_PROTECTED The device can not be written to.
1201 @retval EFI_DEVICE_ERROR The device reported an error while performing the write.
1202 @retval EFI_NO_MEDIA There is no media in the device.
1203 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.
1204 @retval EFI_INVALID_PARAMETER The write request contains device addresses that are not
1205 valid for the device.
1206
1207 **/
1208 EFI_STATUS
1209 EFIAPI
1210 DiskIoWriteDisk (
1211 IN EFI_DISK_IO_PROTOCOL *This,
1212 IN UINT32 MediaId,
1213 IN UINT64 Offset,
1214 IN UINTN BufferSize,
1215 IN VOID *Buffer
1216 )
1217 {
1218 return DiskIo2ReadWriteDisk (
1219 DISK_IO_PRIVATE_DATA_FROM_DISK_IO (This),
1220 TRUE, MediaId, Offset, NULL, BufferSize, (UINT8 *) Buffer
1221 );
1222 }
1223
1224 /**
1225 The user Entry Point for module DiskIo. The user code starts with this function.
1226
1227 @param[in] ImageHandle The firmware allocated handle for the EFI image.
1228 @param[in] SystemTable A pointer to the EFI System Table.
1229
1230 @retval EFI_SUCCESS The entry point is executed successfully.
1231 @retval other Some error occurs when executing this entry point.
1232
1233 **/
1234 EFI_STATUS
1235 EFIAPI
1236 InitializeDiskIo (
1237 IN EFI_HANDLE ImageHandle,
1238 IN EFI_SYSTEM_TABLE *SystemTable
1239 )
1240 {
1241 EFI_STATUS Status;
1242
1243 //
1244 // Install driver model protocol(s).
1245 //
1246 Status = EfiLibInstallDriverBindingComponentName2 (
1247 ImageHandle,
1248 SystemTable,
1249 &gDiskIoDriverBinding,
1250 ImageHandle,
1251 &gDiskIoComponentName,
1252 &gDiskIoComponentName2
1253 );
1254 ASSERT_EFI_ERROR (Status);
1255
1256 return Status;
1257 }