]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - OvmfPkg/VirtioBlkDxe/VirtioBlk.c
OvmfPkg: Make the VirtIo devices use the new VIRTIO_DEVICE_PROTOCOL
[mirror_edk2.git] / OvmfPkg / VirtioBlkDxe / VirtioBlk.c
... / ...
CommitLineData
1/** @file\r
2\r
3 This driver produces Block I/O Protocol instances for virtio-blk devices.\r
4\r
5 The implementation is basic:\r
6\r
7 - No attach/detach (ie. removable media).\r
8\r
9 - Although the non-blocking interfaces of EFI_BLOCK_IO2_PROTOCOL could be a\r
10 good match for multiple in-flight virtio-blk requests, we stick to\r
11 synchronous requests and EFI_BLOCK_IO_PROTOCOL for now.\r
12\r
13 Copyright (C) 2012, Red Hat, Inc.\r
14 Copyright (c) 2012, Intel Corporation. All rights reserved.<BR>\r
15\r
16 This program and the accompanying materials are licensed and made available\r
17 under the terms and conditions of the BSD License which accompanies this\r
18 distribution. The full text of the license may be found at\r
19 http://opensource.org/licenses/bsd-license.php\r
20\r
21 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT\r
22 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
23\r
24**/\r
25\r
26#include <IndustryStandard/VirtioBlk.h>\r
27#include <Library/BaseMemoryLib.h>\r
28#include <Library/DebugLib.h>\r
29#include <Library/MemoryAllocationLib.h>\r
30#include <Library/UefiBootServicesTableLib.h>\r
31#include <Library/UefiLib.h>\r
32#include <Library/VirtioLib.h>\r
33\r
34#include "VirtioBlk.h"\r
35\r
36/**\r
37\r
38 Convenience macros to read and write region 0 IO space elements of the\r
39 virtio-blk device, for configuration purposes.\r
40\r
41 The following macros make it possible to specify only the "core parameters"\r
42 for such accesses and to derive the rest. By the time VIRTIO_CFG_WRITE()\r
43 returns, the transaction will have been completed.\r
44\r
45 @param[in] Dev Pointer to the VBLK_DEV structure whose VirtIo space\r
46 we're accessing. Dev->VirtIo must be valid.\r
47\r
48 @param[in] Field A field name from VBLK_HDR, identifying the virtio-blk\r
49 configuration item to access.\r
50\r
51 @param[in] Value (VIRTIO_CFG_WRITE() only.) The value to write to the\r
52 selected configuration item.\r
53\r
54 @param[out] Pointer (VIRTIO_CFG_READ() only.) The object to receive the\r
55 value read from the configuration item. Its type must be\r
56 one of UINT8, UINT16, UINT32, UINT64.\r
57\r
58\r
59 @return Status code returned by VirtioWriteDevice() / VirtioReadDevice().\r
60\r
61**/\r
62\r
63#define VIRTIO_CFG_WRITE(Dev, Field, Value) (VirtioWriteDevice ( \\r
64 (Dev)->VirtIo, \\r
65 OFFSET_OF_VBLK (Field), \\r
66 SIZE_OF_VBLK (Field), \\r
67 (Value) \\r
68 ))\r
69\r
70#define VIRTIO_CFG_READ(Dev, Field, Pointer) (VirtioReadDevice ( \\r
71 (Dev)->VirtIo, \\r
72 OFFSET_OF_VBLK (Field), \\r
73 SIZE_OF_VBLK (Field), \\r
74 sizeof *(Pointer), \\r
75 (Pointer) \\r
76 ))\r
77\r
78\r
79//\r
80// UEFI Spec 2.3.1 + Errata C, 12.8 EFI Block I/O Protocol\r
81// Driver Writer's Guide for UEFI 2.3.1 v1.01,\r
82// 24.2 Block I/O Protocol Implementations\r
83//\r
84EFI_STATUS\r
85EFIAPI\r
86VirtioBlkReset (\r
87 IN EFI_BLOCK_IO_PROTOCOL *This,\r
88 IN BOOLEAN ExtendedVerification\r
89 )\r
90{\r
91 //\r
92 // If we managed to initialize and install the driver, then the device is\r
93 // working correctly.\r
94 //\r
95 return EFI_SUCCESS;\r
96}\r
97\r
98/**\r
99\r
100 Verify correctness of the read/write (not flush) request submitted to the\r
101 EFI_BLOCK_IO_PROTOCOL instance.\r
102\r
103 This function provides most verification steps described in:\r
104\r
105 UEFI Spec 2.3.1 + Errata C, 12.8 EFI Block I/O Protocol, 12.8 EFI Block I/O\r
106 Protocol,\r
107 - EFI_BLOCK_IO_PROTOCOL.ReadBlocks()\r
108 - EFI_BLOCK_IO_PROTOCOL.WriteBlocks()\r
109\r
110 Driver Writer's Guide for UEFI 2.3.1 v1.01,\r
111 - 24.2.2. ReadBlocks() and ReadBlocksEx() Implementation\r
112 - 24.2.3 WriteBlocks() and WriteBlockEx() Implementation\r
113\r
114 Request sizes are limited to 1 GB (checked). This is not a practical\r
115 limitation, just conformance to virtio-0.9.5, 2.3.2 Descriptor Table: "no\r
116 descriptor chain may be more than 2^32 bytes long in total".\r
117\r
118 Some Media characteristics are hardcoded in VirtioBlkInit() below (like\r
119 non-removable media, no restriction on buffer alignment etc); we rely on\r
120 those here without explicit mention.\r
121\r
122 @param[in] Media The EFI_BLOCK_IO_MEDIA characteristics for\r
123 this driver instance, extracted from the\r
124 underlying virtio-blk device at initialization\r
125 time. We validate the request against this set\r
126 of attributes.\r
127\r
128\r
129 @param[in] Lba Logical Block Address: number of logical\r
130 blocks to skip from the beginning of the\r
131 device.\r
132\r
133 @param[in] PositiveBufferSize Size of buffer to transfer, in bytes. The\r
134 caller is responsible to ensure this parameter\r
135 is positive.\r
136\r
137 @param[in] RequestIsWrite TRUE iff data transfer goes from guest to\r
138 device.\r
139\r
140\r
141 @@return Validation result to be forwarded outwards by\r
142 ReadBlocks() and WriteBlocks, as required by\r
143 the specs above.\r
144\r
145**/\r
146STATIC\r
147EFI_STATUS\r
148EFIAPI\r
149VerifyReadWriteRequest (\r
150 IN EFI_BLOCK_IO_MEDIA *Media,\r
151 IN EFI_LBA Lba,\r
152 IN UINTN PositiveBufferSize,\r
153 IN BOOLEAN RequestIsWrite\r
154 )\r
155{\r
156 UINTN BlockCount;\r
157\r
158 ASSERT (PositiveBufferSize > 0);\r
159\r
160 if (PositiveBufferSize > SIZE_1GB ||\r
161 PositiveBufferSize % Media->BlockSize > 0) {\r
162 return EFI_BAD_BUFFER_SIZE;\r
163 }\r
164 BlockCount = PositiveBufferSize / Media->BlockSize;\r
165\r
166 //\r
167 // Avoid unsigned wraparound on either side in the second comparison.\r
168 //\r
169 if (Lba > Media->LastBlock || BlockCount - 1 > Media->LastBlock - Lba) {\r
170 return EFI_INVALID_PARAMETER;\r
171 }\r
172\r
173 if (RequestIsWrite && Media->ReadOnly) {\r
174 return EFI_WRITE_PROTECTED;\r
175 }\r
176\r
177 return EFI_SUCCESS;\r
178}\r
179\r
180\r
181\r
182\r
183/**\r
184\r
185 Format a read / write / flush request as three consecutive virtio\r
186 descriptors, push them to the host, and poll for the response.\r
187\r
188 This is the main workhorse function. Two use cases are supported, read/write\r
189 and flush. The function may only be called after the request parameters have\r
190 been verified by\r
191 - specific checks in ReadBlocks() / WriteBlocks() / FlushBlocks(), and\r
192 - VerifyReadWriteRequest() (for read/write only).\r
193\r
194 Parameters handled commonly:\r
195\r
196 @param[in] Dev The virtio-blk device the request is targeted\r
197 at.\r
198\r
199 Flush request:\r
200\r
201 @param[in] Lba Must be zero.\r
202\r
203 @param[in] BufferSize Must be zero.\r
204\r
205 @param[in out] Buffer Ignored by the function.\r
206\r
207 @param[in] RequestIsWrite Must be TRUE.\r
208\r
209 Read/Write request:\r
210\r
211 @param[in] Lba Logical Block Address: number of logical blocks\r
212 to skip from the beginning of the device.\r
213\r
214 @param[in] BufferSize Size of buffer to transfer, in bytes. The caller\r
215 is responsible to ensure this parameter is\r
216 positive.\r
217\r
218 @param[in out] Buffer The guest side area to read data from the device\r
219 into, or write data to the device from.\r
220\r
221 @param[in] RequestIsWrite TRUE iff data transfer goes from guest to\r
222 device.\r
223\r
224 Return values are common to both use cases, and are appropriate to be\r
225 forwarded by the EFI_BLOCK_IO_PROTOCOL functions (ReadBlocks(),\r
226 WriteBlocks(), FlushBlocks()).\r
227\r
228\r
229 @retval EFI_SUCCESS Transfer complete.\r
230\r
231 @retval EFI_DEVICE_ERROR Failed to notify host side via VirtIo write, or\r
232 unable to parse host response, or host response\r
233 is not VIRTIO_BLK_S_OK.\r
234\r
235**/\r
236\r
237STATIC\r
238EFI_STATUS\r
239EFIAPI\r
240SynchronousRequest (\r
241 IN VBLK_DEV *Dev,\r
242 IN EFI_LBA Lba,\r
243 IN UINTN BufferSize,\r
244 IN OUT volatile VOID *Buffer,\r
245 IN BOOLEAN RequestIsWrite\r
246 )\r
247{\r
248 UINT32 BlockSize;\r
249 volatile VIRTIO_BLK_REQ Request;\r
250 volatile UINT8 HostStatus;\r
251 DESC_INDICES Indices;\r
252\r
253 BlockSize = Dev->BlockIoMedia.BlockSize;\r
254\r
255 //\r
256 // ensured by VirtioBlkInit()\r
257 //\r
258 ASSERT (BlockSize > 0);\r
259 ASSERT (BlockSize % 512 == 0);\r
260\r
261 //\r
262 // ensured by contract above, plus VerifyReadWriteRequest()\r
263 //\r
264 ASSERT (BufferSize % BlockSize == 0);\r
265\r
266 //\r
267 // Prepare virtio-blk request header, setting zero size for flush.\r
268 // IO Priority is homogeneously 0.\r
269 //\r
270 Request.Type = RequestIsWrite ?\r
271 (BufferSize == 0 ? VIRTIO_BLK_T_FLUSH : VIRTIO_BLK_T_OUT) :\r
272 VIRTIO_BLK_T_IN;\r
273 Request.IoPrio = 0;\r
274 Request.Sector = MultU64x32(Lba, BlockSize / 512);\r
275\r
276 VirtioPrepare (&Dev->Ring, &Indices);\r
277\r
278 //\r
279 // preset a host status for ourselves that we do not accept as success\r
280 //\r
281 HostStatus = VIRTIO_BLK_S_IOERR;\r
282\r
283 //\r
284 // ensured by VirtioBlkInit() -- this predicate, in combination with the\r
285 // lock-step progress, ensures we don't have to track free descriptors.\r
286 //\r
287 ASSERT (Dev->Ring.QueueSize >= 3);\r
288\r
289 //\r
290 // virtio-blk header in first desc\r
291 //\r
292 VirtioAppendDesc (&Dev->Ring, (UINTN) &Request, sizeof Request,\r
293 VRING_DESC_F_NEXT, &Indices);\r
294\r
295 //\r
296 // data buffer for read/write in second desc\r
297 //\r
298 if (BufferSize > 0) {\r
299 //\r
300 // From virtio-0.9.5, 2.3.2 Descriptor Table:\r
301 // "no descriptor chain may be more than 2^32 bytes long in total".\r
302 //\r
303 // The predicate is ensured by the call contract above (for flush), or\r
304 // VerifyReadWriteRequest() (for read/write). It also implies that\r
305 // converting BufferSize to UINT32 will not truncate it.\r
306 //\r
307 ASSERT (BufferSize <= SIZE_1GB);\r
308\r
309 //\r
310 // VRING_DESC_F_WRITE is interpreted from the host's point of view.\r
311 //\r
312 VirtioAppendDesc (&Dev->Ring, (UINTN) Buffer, (UINT32) BufferSize,\r
313 VRING_DESC_F_NEXT | (RequestIsWrite ? 0 : VRING_DESC_F_WRITE),\r
314 &Indices);\r
315 }\r
316\r
317 //\r
318 // host status in last (second or third) desc\r
319 //\r
320 VirtioAppendDesc (&Dev->Ring, (UINTN) &HostStatus, sizeof HostStatus,\r
321 VRING_DESC_F_WRITE, &Indices);\r
322\r
323 //\r
324 // virtio-blk's only virtqueue is #0, called "requestq" (see Appendix D).\r
325 //\r
326 if (VirtioFlush (Dev->VirtIo, 0, &Dev->Ring, &Indices) == EFI_SUCCESS &&\r
327 HostStatus == VIRTIO_BLK_S_OK) {\r
328 return EFI_SUCCESS;\r
329 }\r
330\r
331 return EFI_DEVICE_ERROR;\r
332}\r
333\r
334\r
335/**\r
336\r
337 ReadBlocks() operation for virtio-blk.\r
338\r
339 See\r
340 - UEFI Spec 2.3.1 + Errata C, 12.8 EFI Block I/O Protocol, 12.8 EFI Block I/O\r
341 Protocol, EFI_BLOCK_IO_PROTOCOL.ReadBlocks().\r
342 - Driver Writer's Guide for UEFI 2.3.1 v1.01, 24.2.2. ReadBlocks() and\r
343 ReadBlocksEx() Implementation.\r
344\r
345 Parameter checks and conformant return values are implemented in\r
346 VerifyReadWriteRequest() and SynchronousRequest().\r
347\r
348 A zero BufferSize doesn't seem to be prohibited, so do nothing in that case,\r
349 successfully.\r
350\r
351**/\r
352\r
353EFI_STATUS\r
354EFIAPI\r
355VirtioBlkReadBlocks (\r
356 IN EFI_BLOCK_IO_PROTOCOL *This,\r
357 IN UINT32 MediaId,\r
358 IN EFI_LBA Lba,\r
359 IN UINTN BufferSize,\r
360 OUT VOID *Buffer\r
361 )\r
362{\r
363 VBLK_DEV *Dev;\r
364 EFI_STATUS Status;\r
365\r
366 if (BufferSize == 0) {\r
367 return EFI_SUCCESS;\r
368 }\r
369\r
370 Dev = VIRTIO_BLK_FROM_BLOCK_IO (This);\r
371 Status = VerifyReadWriteRequest (\r
372 &Dev->BlockIoMedia,\r
373 Lba,\r
374 BufferSize,\r
375 FALSE // RequestIsWrite\r
376 );\r
377 if (EFI_ERROR (Status)) {\r
378 return Status;\r
379 }\r
380\r
381 return SynchronousRequest (\r
382 Dev,\r
383 Lba,\r
384 BufferSize,\r
385 Buffer,\r
386 FALSE // RequestIsWrite\r
387 );\r
388}\r
389\r
390/**\r
391\r
392 WriteBlocks() operation for virtio-blk.\r
393\r
394 See\r
395 - UEFI Spec 2.3.1 + Errata C, 12.8 EFI Block I/O Protocol, 12.8 EFI Block I/O\r
396 Protocol, EFI_BLOCK_IO_PROTOCOL.WriteBlocks().\r
397 - Driver Writer's Guide for UEFI 2.3.1 v1.01, 24.2.3 WriteBlocks() and\r
398 WriteBlockEx() Implementation.\r
399\r
400 Parameter checks and conformant return values are implemented in\r
401 VerifyReadWriteRequest() and SynchronousRequest().\r
402\r
403 A zero BufferSize doesn't seem to be prohibited, so do nothing in that case,\r
404 successfully.\r
405\r
406**/\r
407\r
408EFI_STATUS\r
409EFIAPI\r
410VirtioBlkWriteBlocks (\r
411 IN EFI_BLOCK_IO_PROTOCOL *This,\r
412 IN UINT32 MediaId,\r
413 IN EFI_LBA Lba,\r
414 IN UINTN BufferSize,\r
415 IN VOID *Buffer\r
416 )\r
417{\r
418 VBLK_DEV *Dev;\r
419 EFI_STATUS Status;\r
420\r
421 if (BufferSize == 0) {\r
422 return EFI_SUCCESS;\r
423 }\r
424\r
425 Dev = VIRTIO_BLK_FROM_BLOCK_IO (This);\r
426 Status = VerifyReadWriteRequest (\r
427 &Dev->BlockIoMedia,\r
428 Lba,\r
429 BufferSize,\r
430 TRUE // RequestIsWrite\r
431 );\r
432 if (EFI_ERROR (Status)) {\r
433 return Status;\r
434 }\r
435\r
436 return SynchronousRequest (\r
437 Dev,\r
438 Lba,\r
439 BufferSize,\r
440 Buffer,\r
441 TRUE // RequestIsWrite\r
442 );\r
443}\r
444\r
445\r
446/**\r
447\r
448 FlushBlocks() operation for virtio-blk.\r
449\r
450 See\r
451 - UEFI Spec 2.3.1 + Errata C, 12.8 EFI Block I/O Protocol, 12.8 EFI Block I/O\r
452 Protocol, EFI_BLOCK_IO_PROTOCOL.FlushBlocks().\r
453 - Driver Writer's Guide for UEFI 2.3.1 v1.01, 24.2.4 FlushBlocks() and\r
454 FlushBlocksEx() Implementation.\r
455\r
456 If the underlying virtio-blk device doesn't support flushing (ie.\r
457 write-caching), then this function should not be called by higher layers,\r
458 according to EFI_BLOCK_IO_MEDIA characteristics set in VirtioBlkInit().\r
459 Should they do nonetheless, we do nothing, successfully.\r
460\r
461**/\r
462\r
463EFI_STATUS\r
464EFIAPI\r
465VirtioBlkFlushBlocks (\r
466 IN EFI_BLOCK_IO_PROTOCOL *This\r
467 )\r
468{\r
469 VBLK_DEV *Dev;\r
470\r
471 Dev = VIRTIO_BLK_FROM_BLOCK_IO (This);\r
472 return Dev->BlockIoMedia.WriteCaching ?\r
473 SynchronousRequest (\r
474 Dev,\r
475 0, // Lba\r
476 0, // BufferSize\r
477 NULL, // Buffer\r
478 TRUE // RequestIsWrite\r
479 ) :\r
480 EFI_SUCCESS;\r
481}\r
482\r
483\r
484/**\r
485\r
486 Device probe function for this driver.\r
487\r
488 The DXE core calls this function for any given device in order to see if the\r
489 driver can drive the device.\r
490\r
491 Specs relevant in the general sense:\r
492\r
493 - UEFI Spec 2.3.1 + Errata C:\r
494 - 6.3 Protocol Handler Services -- for accessing the underlying device\r
495 - 10.1 EFI Driver Binding Protocol -- for exporting ourselves\r
496\r
497 - Driver Writer's Guide for UEFI 2.3.1 v1.01:\r
498 - 5.1.3.4 OpenProtocol() and CloseProtocol() -- for accessing the\r
499 underlying device\r
500 - 9 Driver Binding Protocol -- for exporting ourselves\r
501\r
502 @param[in] This The EFI_DRIVER_BINDING_PROTOCOL object\r
503 incorporating this driver (independently of\r
504 any device).\r
505\r
506 @param[in] DeviceHandle The device to probe.\r
507\r
508 @param[in] RemainingDevicePath Relevant only for bus drivers, ignored.\r
509\r
510\r
511 @retval EFI_SUCCESS The driver supports the device being probed.\r
512\r
513 @retval EFI_UNSUPPORTED Based on virtio-blk discovery, we do not support\r
514 the device.\r
515\r
516 @return Error codes from the OpenProtocol() boot service or\r
517 the VirtIo protocol.\r
518\r
519**/\r
520\r
521EFI_STATUS\r
522EFIAPI\r
523VirtioBlkDriverBindingSupported (\r
524 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
525 IN EFI_HANDLE DeviceHandle,\r
526 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
527 )\r
528{\r
529 EFI_STATUS Status;\r
530 VIRTIO_DEVICE_PROTOCOL *VirtIo;\r
531\r
532 //\r
533 // Attempt to open the device with the VirtIo set of interfaces. On success,\r
534 // the protocol is "instantiated" for the VirtIo device. Covers duplicate open\r
535 // attempts (EFI_ALREADY_STARTED).\r
536 //\r
537 Status = gBS->OpenProtocol (\r
538 DeviceHandle, // candidate device\r
539 &gVirtioDeviceProtocolGuid, // for generic VirtIo access\r
540 (VOID **)&VirtIo, // handle to instantiate\r
541 This->DriverBindingHandle, // requestor driver identity\r
542 DeviceHandle, // ControllerHandle, according to\r
543 // the UEFI Driver Model\r
544 EFI_OPEN_PROTOCOL_BY_DRIVER // get exclusive VirtIo access to\r
545 // the device; to be released\r
546 );\r
547 if (EFI_ERROR (Status)) {\r
548 return Status;\r
549 }\r
550\r
551 if (VirtIo->SubSystemDeviceId != VIRTIO_SUBSYSTEM_BLOCK_DEVICE) {\r
552 Status = EFI_UNSUPPORTED;\r
553 }\r
554\r
555 //\r
556 // We needed VirtIo access only transitorily, to see whether we support the\r
557 // device or not.\r
558 //\r
559 gBS->CloseProtocol (DeviceHandle, &gVirtioDeviceProtocolGuid,\r
560 This->DriverBindingHandle, DeviceHandle);\r
561 return Status;\r
562}\r
563\r
564\r
565/**\r
566\r
567 Set up all BlockIo and virtio-blk aspects of this driver for the specified\r
568 device.\r
569\r
570 @param[in out] Dev The driver instance to configure. The caller is\r
571 responsible for Dev->VirtIo's validity (ie. working IO\r
572 access to the underlying virtio-blk device).\r
573\r
574 @retval EFI_SUCCESS Setup complete.\r
575\r
576 @retval EFI_UNSUPPORTED The driver is unable to work with the virtio ring or\r
577 virtio-blk attributes the host provides.\r
578\r
579 @return Error codes from VirtioRingInit() or\r
580 VIRTIO_CFG_READ() / VIRTIO_CFG_WRITE().\r
581\r
582**/\r
583\r
584STATIC\r
585EFI_STATUS\r
586EFIAPI\r
587VirtioBlkInit (\r
588 IN OUT VBLK_DEV *Dev\r
589 )\r
590{\r
591 UINT8 NextDevStat;\r
592 EFI_STATUS Status;\r
593\r
594 UINT32 Features;\r
595 UINT64 NumSectors;\r
596 UINT32 BlockSize;\r
597 UINT16 QueueSize;\r
598\r
599 //\r
600 // Execute virtio-0.9.5, 2.2.1 Device Initialization Sequence.\r
601 //\r
602 NextDevStat = 0; // step 1 -- reset device\r
603 Status = Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, NextDevStat);\r
604 if (EFI_ERROR (Status)) {\r
605 goto Failed;\r
606 }\r
607\r
608 NextDevStat |= VSTAT_ACK; // step 2 -- acknowledge device presence\r
609 Status = Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, NextDevStat);\r
610 if (EFI_ERROR (Status)) {\r
611 goto Failed;\r
612 }\r
613\r
614 NextDevStat |= VSTAT_DRIVER; // step 3 -- we know how to drive it\r
615 Status = Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, NextDevStat);\r
616 if (EFI_ERROR (Status)) {\r
617 goto Failed;\r
618 }\r
619\r
620 //\r
621 // Set Page Size - MMIO VirtIo Specific\r
622 //\r
623 Status = Dev->VirtIo->SetPageSize (Dev->VirtIo, EFI_PAGE_SIZE);\r
624 if (EFI_ERROR (Status)) {\r
625 goto Failed;\r
626 }\r
627\r
628 //\r
629 // step 4a -- retrieve and validate features\r
630 //\r
631 Status = Dev->VirtIo->GetDeviceFeatures (Dev->VirtIo, &Features);\r
632 if (EFI_ERROR (Status)) {\r
633 goto Failed;\r
634 }\r
635\r
636 Status = VIRTIO_CFG_READ (Dev, Capacity, &NumSectors);\r
637 if (EFI_ERROR (Status)) {\r
638 goto Failed;\r
639 }\r
640 if (NumSectors == 0) {\r
641 Status = EFI_UNSUPPORTED;\r
642 goto Failed;\r
643 }\r
644\r
645 if (Features & VIRTIO_BLK_F_BLK_SIZE) {\r
646 Status = VIRTIO_CFG_READ (Dev, BlkSize, &BlockSize);\r
647 if (EFI_ERROR (Status)) {\r
648 goto Failed;\r
649 }\r
650 if (BlockSize == 0 || BlockSize % 512 != 0 ||\r
651 ModU64x32 (NumSectors, BlockSize / 512) != 0) {\r
652 //\r
653 // We can only handle a logical block consisting of whole sectors,\r
654 // and only a disk composed of whole logical blocks.\r
655 //\r
656 Status = EFI_UNSUPPORTED;\r
657 goto Failed;\r
658 }\r
659 }\r
660 else {\r
661 BlockSize = 512;\r
662 }\r
663\r
664 //\r
665 // step 4b -- allocate virtqueue\r
666 //\r
667 Status = Dev->VirtIo->SetQueueSel (Dev->VirtIo, 0);\r
668 if (EFI_ERROR (Status)) {\r
669 goto Failed;\r
670 }\r
671 Status = Dev->VirtIo->GetQueueNumMax (Dev->VirtIo, &QueueSize);\r
672 if (EFI_ERROR (Status)) {\r
673 goto Failed;\r
674 }\r
675 if (QueueSize < 3) { // SynchronousRequest() uses at most three descriptors\r
676 Status = EFI_UNSUPPORTED;\r
677 goto Failed;\r
678 }\r
679\r
680 Status = VirtioRingInit (QueueSize, &Dev->Ring);\r
681 if (EFI_ERROR (Status)) {\r
682 goto Failed;\r
683 }\r
684\r
685 //\r
686 // Additional steps for MMIO: align the queue appropriately, and set the\r
687 // size. If anything fails from here on, we must release the ring resources.\r
688 //\r
689 Status = Dev->VirtIo->SetQueueNum (Dev->VirtIo, QueueSize);\r
690 if (EFI_ERROR (Status)) {\r
691 goto ReleaseQueue;\r
692 }\r
693\r
694 Status = Dev->VirtIo->SetQueueAlign (Dev->VirtIo, EFI_PAGE_SIZE);\r
695 if (EFI_ERROR (Status)) {\r
696 goto ReleaseQueue;\r
697 }\r
698\r
699 //\r
700 // step 4c -- Report GPFN (guest-physical frame number) of queue.\r
701 //\r
702 Status = Dev->VirtIo->SetQueueAddress (Dev->VirtIo,\r
703 (UINT32)(UINTN) Dev->Ring.Base >> EFI_PAGE_SHIFT);\r
704 if (EFI_ERROR (Status)) {\r
705 goto ReleaseQueue;\r
706 }\r
707\r
708\r
709 //\r
710 // step 5 -- Report understood features. There are no virtio-blk specific\r
711 // features to negotiate in virtio-0.9.5, plus we do not want any of the\r
712 // device-independent (known or unknown) VIRTIO_F_* capabilities (see\r
713 // Appendix B).\r
714 //\r
715 Status = Dev->VirtIo->SetGuestFeatures (Dev->VirtIo, 0);\r
716 if (EFI_ERROR (Status)) {\r
717 goto ReleaseQueue;\r
718 }\r
719\r
720 //\r
721 // step 6 -- initialization complete\r
722 //\r
723 NextDevStat |= VSTAT_DRIVER_OK;\r
724 Status = Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, NextDevStat);\r
725 if (EFI_ERROR (Status)) {\r
726 goto ReleaseQueue;\r
727 }\r
728\r
729 //\r
730 // Populate the exported interface's attributes; see UEFI spec v2.3.1 +\r
731 // Errata C, 12.8 EFI Block I/O Protocol. We stick to the lowest possible\r
732 // EFI_BLOCK_IO_PROTOCOL revision for now.\r
733 //\r
734 Dev->BlockIo.Revision = 0;\r
735 Dev->BlockIo.Media = &Dev->BlockIoMedia;\r
736 Dev->BlockIo.Reset = &VirtioBlkReset;\r
737 Dev->BlockIo.ReadBlocks = &VirtioBlkReadBlocks;\r
738 Dev->BlockIo.WriteBlocks = &VirtioBlkWriteBlocks;\r
739 Dev->BlockIo.FlushBlocks = &VirtioBlkFlushBlocks;\r
740 Dev->BlockIoMedia.MediaId = 0;\r
741 Dev->BlockIoMedia.RemovableMedia = FALSE;\r
742 Dev->BlockIoMedia.MediaPresent = TRUE;\r
743 Dev->BlockIoMedia.LogicalPartition = FALSE;\r
744 Dev->BlockIoMedia.ReadOnly = !!(Features & VIRTIO_BLK_F_RO);\r
745 Dev->BlockIoMedia.WriteCaching = !!(Features & VIRTIO_BLK_F_FLUSH);\r
746 Dev->BlockIoMedia.BlockSize = BlockSize;\r
747 Dev->BlockIoMedia.IoAlign = 0;\r
748 Dev->BlockIoMedia.LastBlock = DivU64x32 (NumSectors,\r
749 BlockSize / 512) - 1;\r
750 return EFI_SUCCESS;\r
751\r
752ReleaseQueue:\r
753 VirtioRingUninit (&Dev->Ring);\r
754\r
755Failed:\r
756 //\r
757 // Notify the host about our failure to setup: virtio-0.9.5, 2.2.2.1 Device\r
758 // Status. VirtIo access failure here should not mask the original error.\r
759 //\r
760 NextDevStat |= VSTAT_FAILED;\r
761 Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, NextDevStat);\r
762\r
763 return Status; // reached only via Failed above\r
764}\r
765\r
766\r
767/**\r
768\r
769 Uninitialize the internals of a virtio-blk device that has been successfully\r
770 set up with VirtioBlkInit().\r
771\r
772 @param[in out] Dev The device to clean up.\r
773\r
774**/\r
775\r
776STATIC\r
777VOID\r
778EFIAPI\r
779VirtioBlkUninit (\r
780 IN OUT VBLK_DEV *Dev\r
781 )\r
782{\r
783 //\r
784 // Reset the virtual device -- see virtio-0.9.5, 2.2.2.1 Device Status. When\r
785 // VIRTIO_CFG_WRITE() returns, the host will have learned to stay away from\r
786 // the old comms area.\r
787 //\r
788 Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, 0);\r
789\r
790 VirtioRingUninit (&Dev->Ring);\r
791\r
792 SetMem (&Dev->BlockIo, sizeof Dev->BlockIo, 0x00);\r
793 SetMem (&Dev->BlockIoMedia, sizeof Dev->BlockIoMedia, 0x00);\r
794}\r
795\r
796\r
797/**\r
798\r
799 After we've pronounced support for a specific device in\r
800 DriverBindingSupported(), we start managing said device (passed in by the\r
801 Driver Exeuction Environment) with the following service.\r
802\r
803 See DriverBindingSupported() for specification references.\r
804\r
805 @param[in] This The EFI_DRIVER_BINDING_PROTOCOL object\r
806 incorporating this driver (independently of\r
807 any device).\r
808\r
809 @param[in] DeviceHandle The supported device to drive.\r
810\r
811 @param[in] RemainingDevicePath Relevant only for bus drivers, ignored.\r
812\r
813\r
814 @retval EFI_SUCCESS Driver instance has been created and\r
815 initialized for the virtio-blk device, it\r
816 is now accessibla via EFI_BLOCK_IO_PROTOCOL.\r
817\r
818 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.\r
819\r
820 @return Error codes from the OpenProtocol() boot\r
821 service, the VirtIo protocol, VirtioBlkInit(),\r
822 or the InstallProtocolInterface() boot service.\r
823\r
824**/\r
825\r
826EFI_STATUS\r
827EFIAPI\r
828VirtioBlkDriverBindingStart (\r
829 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
830 IN EFI_HANDLE DeviceHandle,\r
831 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
832 )\r
833{\r
834 VBLK_DEV *Dev;\r
835 EFI_STATUS Status;\r
836\r
837 Dev = (VBLK_DEV *) AllocateZeroPool (sizeof *Dev);\r
838 if (Dev == NULL) {\r
839 return EFI_OUT_OF_RESOURCES;\r
840 }\r
841\r
842 Status = gBS->OpenProtocol (DeviceHandle, &gVirtioDeviceProtocolGuid,\r
843 (VOID **)&Dev->VirtIo, This->DriverBindingHandle,\r
844 DeviceHandle, EFI_OPEN_PROTOCOL_BY_DRIVER);\r
845 if (EFI_ERROR (Status)) {\r
846 goto FreeVirtioBlk;\r
847 }\r
848\r
849 //\r
850 // VirtIo access granted, configure virtio-blk device.\r
851 //\r
852 Status = VirtioBlkInit (Dev);\r
853 if (EFI_ERROR (Status)) {\r
854 goto CloseVirtIo;\r
855 }\r
856\r
857 //\r
858 // Setup complete, attempt to export the driver instance's BlockIo interface.\r
859 //\r
860 Dev->Signature = VBLK_SIG;\r
861 Status = gBS->InstallProtocolInterface (&DeviceHandle,\r
862 &gEfiBlockIoProtocolGuid, EFI_NATIVE_INTERFACE,\r
863 &Dev->BlockIo);\r
864 if (EFI_ERROR (Status)) {\r
865 goto UninitDev;\r
866 }\r
867\r
868 return EFI_SUCCESS;\r
869\r
870UninitDev:\r
871 VirtioBlkUninit (Dev);\r
872\r
873CloseVirtIo:\r
874 gBS->CloseProtocol (DeviceHandle, &gVirtioDeviceProtocolGuid,\r
875 This->DriverBindingHandle, DeviceHandle);\r
876\r
877FreeVirtioBlk:\r
878 FreePool (Dev);\r
879\r
880 return Status;\r
881}\r
882\r
883\r
884/**\r
885\r
886 Stop driving a virtio-blk device and remove its BlockIo interface.\r
887\r
888 This function replays the success path of DriverBindingStart() in reverse.\r
889 The host side virtio-blk device is reset, so that the OS boot loader or the\r
890 OS may reinitialize it.\r
891\r
892 @param[in] This The EFI_DRIVER_BINDING_PROTOCOL object\r
893 incorporating this driver (independently of any\r
894 device).\r
895\r
896 @param[in] DeviceHandle Stop driving this device.\r
897\r
898 @param[in] NumberOfChildren Since this function belongs to a device driver\r
899 only (as opposed to a bus driver), the caller\r
900 environment sets NumberOfChildren to zero, and\r
901 we ignore it.\r
902\r
903 @param[in] ChildHandleBuffer Ignored (corresponding to NumberOfChildren).\r
904\r
905**/\r
906\r
907EFI_STATUS\r
908EFIAPI\r
909VirtioBlkDriverBindingStop (\r
910 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
911 IN EFI_HANDLE DeviceHandle,\r
912 IN UINTN NumberOfChildren,\r
913 IN EFI_HANDLE *ChildHandleBuffer\r
914 )\r
915{\r
916 EFI_STATUS Status;\r
917 EFI_BLOCK_IO_PROTOCOL *BlockIo;\r
918 VBLK_DEV *Dev;\r
919\r
920 Status = gBS->OpenProtocol (\r
921 DeviceHandle, // candidate device\r
922 &gEfiBlockIoProtocolGuid, // retrieve the BlockIo iface\r
923 (VOID **)&BlockIo, // target pointer\r
924 This->DriverBindingHandle, // requestor driver identity\r
925 DeviceHandle, // requesting lookup for dev.\r
926 EFI_OPEN_PROTOCOL_GET_PROTOCOL // lookup only, no ref. added\r
927 );\r
928 if (EFI_ERROR (Status)) {\r
929 return Status;\r
930 }\r
931\r
932 Dev = VIRTIO_BLK_FROM_BLOCK_IO (BlockIo);\r
933\r
934 //\r
935 // Handle Stop() requests for in-use driver instances gracefully.\r
936 //\r
937 Status = gBS->UninstallProtocolInterface (DeviceHandle,\r
938 &gEfiBlockIoProtocolGuid, &Dev->BlockIo);\r
939 if (EFI_ERROR (Status)) {\r
940 return Status;\r
941 }\r
942\r
943 VirtioBlkUninit (Dev);\r
944\r
945 gBS->CloseProtocol (DeviceHandle, &gVirtioDeviceProtocolGuid,\r
946 This->DriverBindingHandle, DeviceHandle);\r
947\r
948 FreePool (Dev);\r
949\r
950 return EFI_SUCCESS;\r
951}\r
952\r
953\r
954//\r
955// The static object that groups the Supported() (ie. probe), Start() and\r
956// Stop() functions of the driver together. Refer to UEFI Spec 2.3.1 + Errata\r
957// C, 10.1 EFI Driver Binding Protocol.\r
958//\r
959STATIC EFI_DRIVER_BINDING_PROTOCOL gDriverBinding = {\r
960 &VirtioBlkDriverBindingSupported,\r
961 &VirtioBlkDriverBindingStart,\r
962 &VirtioBlkDriverBindingStop,\r
963 0x10, // Version, must be in [0x10 .. 0xFFFFFFEF] for IHV-developed drivers\r
964 NULL, // ImageHandle, to be overwritten by\r
965 // EfiLibInstallDriverBindingComponentName2() in VirtioBlkEntryPoint()\r
966 NULL // DriverBindingHandle, ditto\r
967};\r
968\r
969\r
970//\r
971// The purpose of the following scaffolding (EFI_COMPONENT_NAME_PROTOCOL and\r
972// EFI_COMPONENT_NAME2_PROTOCOL implementation) is to format the driver's name\r
973// in English, for display on standard console devices. This is recommended for\r
974// UEFI drivers that follow the UEFI Driver Model. Refer to the Driver Writer's\r
975// Guide for UEFI 2.3.1 v1.01, 11 UEFI Driver and Controller Names.\r
976//\r
977// Device type names ("Virtio Block Device") are not formatted because the\r
978// driver supports only that device type. Therefore the driver name suffices\r
979// for unambiguous identification.\r
980//\r
981\r
982STATIC\r
983EFI_UNICODE_STRING_TABLE mDriverNameTable[] = {\r
984 { "eng;en", L"Virtio Block Driver" },\r
985 { NULL, NULL }\r
986};\r
987\r
988STATIC\r
989EFI_COMPONENT_NAME_PROTOCOL gComponentName;\r
990\r
991EFI_STATUS\r
992EFIAPI\r
993VirtioBlkGetDriverName (\r
994 IN EFI_COMPONENT_NAME_PROTOCOL *This,\r
995 IN CHAR8 *Language,\r
996 OUT CHAR16 **DriverName\r
997 )\r
998{\r
999 return LookupUnicodeString2 (\r
1000 Language,\r
1001 This->SupportedLanguages,\r
1002 mDriverNameTable,\r
1003 DriverName,\r
1004 (BOOLEAN)(This == &gComponentName) // Iso639Language\r
1005 );\r
1006}\r
1007\r
1008EFI_STATUS\r
1009EFIAPI\r
1010VirtioBlkGetDeviceName (\r
1011 IN EFI_COMPONENT_NAME_PROTOCOL *This,\r
1012 IN EFI_HANDLE DeviceHandle,\r
1013 IN EFI_HANDLE ChildHandle,\r
1014 IN CHAR8 *Language,\r
1015 OUT CHAR16 **ControllerName\r
1016 )\r
1017{\r
1018 return EFI_UNSUPPORTED;\r
1019}\r
1020\r
1021STATIC\r
1022EFI_COMPONENT_NAME_PROTOCOL gComponentName = {\r
1023 &VirtioBlkGetDriverName,\r
1024 &VirtioBlkGetDeviceName,\r
1025 "eng" // SupportedLanguages, ISO 639-2 language codes\r
1026};\r
1027\r
1028STATIC\r
1029EFI_COMPONENT_NAME2_PROTOCOL gComponentName2 = {\r
1030 (EFI_COMPONENT_NAME2_GET_DRIVER_NAME) &VirtioBlkGetDriverName,\r
1031 (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME) &VirtioBlkGetDeviceName,\r
1032 "en" // SupportedLanguages, RFC 4646 language codes\r
1033};\r
1034\r
1035\r
1036//\r
1037// Entry point of this driver.\r
1038//\r
1039EFI_STATUS\r
1040EFIAPI\r
1041VirtioBlkEntryPoint (\r
1042 IN EFI_HANDLE ImageHandle,\r
1043 IN EFI_SYSTEM_TABLE *SystemTable\r
1044 )\r
1045{\r
1046 return EfiLibInstallDriverBindingComponentName2 (\r
1047 ImageHandle,\r
1048 SystemTable,\r
1049 &gDriverBinding,\r
1050 ImageHandle,\r
1051 &gComponentName,\r
1052 &gComponentName2\r
1053 );\r
1054}\r
1055\r