Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / core / blockdev.c
1 /*
2  * Copyright (C) 2010 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <errno.h>
23 #include <ipxe/interface.h>
24 #include <ipxe/blockdev.h>
25
26 /** @file
27  *
28  * Block devices
29  *
30  */
31
32 /**
33  * Read from block device
34  *
35  * @v control           Control interface
36  * @v data              Data interface
37  * @v lba               Starting logical block address
38  * @v count             Number of logical blocks
39  * @v buffer            Data buffer
40  * @v len               Length of data buffer
41  * @ret rc              Return status code
42  */
43 int block_read ( struct interface *control, struct interface *data,
44                  uint64_t lba, unsigned int count,
45                  userptr_t buffer, size_t len ) {
46         struct interface *dest;
47         block_read_TYPE ( void * ) *op =
48                 intf_get_dest_op ( control, block_read, &dest );
49         void *object = intf_object ( dest );
50         int rc;
51
52         if ( op ) {
53                 rc = op ( object, data, lba, count, buffer, len );
54         } else {
55                 /* Default is to fail to issue the command */
56                 rc = -EOPNOTSUPP;
57         }
58
59         intf_put ( dest );
60         return rc;
61 }
62
63 /**
64  * Write to block device
65  *
66  * @v control           Control interface
67  * @v data              Data interface
68  * @v lba               Starting logical block address
69  * @v count             Number of logical blocks
70  * @v buffer            Data buffer
71  * @v len               Length of data buffer
72  * @ret rc              Return status code
73  */
74 int block_write ( struct interface *control, struct interface *data,
75                   uint64_t lba, unsigned int count,
76                   userptr_t buffer, size_t len ) {
77         struct interface *dest;
78         block_write_TYPE ( void * ) *op =
79                 intf_get_dest_op ( control, block_write, &dest );
80         void *object = intf_object ( dest );
81         int rc;
82
83         if ( op ) {
84                 rc = op ( object, data, lba, count, buffer, len );
85         } else {
86                 /* Default is to fail to issue the command */
87                 rc = -EOPNOTSUPP;
88         }
89
90         intf_put ( dest );
91         return rc;
92 }
93
94 /**
95  * Read block device capacity
96  *
97  * @v control           Control interface
98  * @v data              Data interface
99  * @ret rc              Return status code
100  */
101 int block_read_capacity ( struct interface *control, struct interface *data ) {
102         struct interface *dest;
103         block_read_capacity_TYPE ( void * ) *op =
104                 intf_get_dest_op ( control, block_read_capacity, &dest );
105         void *object = intf_object ( dest );
106         int rc;
107
108         if ( op ) {
109                 rc = op ( object, data );
110         } else {
111                 /* Default is to fail to issue the command */
112                 rc = -EOPNOTSUPP;
113         }
114
115         intf_put ( dest );
116         return rc;
117 }
118
119 /**
120  * Report block device capacity
121  *
122  * @v intf              Interface
123  * @v capacity          Block device capacity
124  */
125 void block_capacity ( struct interface *intf,
126                       struct block_device_capacity *capacity ) {
127         struct interface *dest;
128         block_capacity_TYPE ( void * ) *op =
129                 intf_get_dest_op ( intf, block_capacity, &dest );
130         void *object = intf_object ( dest );
131
132         if ( op ) {
133                 op ( object, capacity );
134         } else {
135                 /* Default is to do nothing */
136         }
137
138         intf_put ( dest );
139 }