Commit 05cb5fe442dd9574895427f8ea02aec73550b6db
Committed by
Anthony Liguori
1 parent
95747581
qdev/prop: add pci devfn property
So we can parse "$slot.$fn" strings into devfn numbers. Signed-off-by: Gerd Hoffmann <kraxel@redhat.com> Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Showing
2 changed files
with
44 additions
and
0 deletions
hw/qdev-properties.c
@@ -198,6 +198,49 @@ PropertyInfo qdev_prop_macaddr = { | @@ -198,6 +198,49 @@ PropertyInfo qdev_prop_macaddr = { | ||
198 | .print = print_mac, | 198 | .print = print_mac, |
199 | }; | 199 | }; |
200 | 200 | ||
201 | +/* --- pci address --- */ | ||
202 | + | ||
203 | +/* | ||
204 | + * bus-local address, i.e. "$slot" or "$slot.$fn" | ||
205 | + */ | ||
206 | +static int parse_pci_devfn(DeviceState *dev, Property *prop, const char *str) | ||
207 | +{ | ||
208 | + uint32_t *ptr = qdev_get_prop_ptr(dev, prop); | ||
209 | + unsigned int slot, fn, n; | ||
210 | + | ||
211 | + if (sscanf(str, "%x.%x%n", &slot, &fn, &n) != 2) { | ||
212 | + fn = 0; | ||
213 | + if (sscanf(str, "%x%n", &slot, &n) != 1) { | ||
214 | + return -1; | ||
215 | + } | ||
216 | + } | ||
217 | + if (str[n] != '\0') | ||
218 | + return -1; | ||
219 | + if (fn > 7) | ||
220 | + return -1; | ||
221 | + *ptr = slot << 3 | fn; | ||
222 | + return 0; | ||
223 | +} | ||
224 | + | ||
225 | +static int print_pci_devfn(DeviceState *dev, Property *prop, char *dest, size_t len) | ||
226 | +{ | ||
227 | + uint32_t *ptr = qdev_get_prop_ptr(dev, prop); | ||
228 | + | ||
229 | + if (-1 == *ptr) { | ||
230 | + return snprintf(dest, len, "<unset>"); | ||
231 | + } else { | ||
232 | + return snprintf(dest, len, "%02x.%x", *ptr >> 3, *ptr & 7); | ||
233 | + } | ||
234 | +} | ||
235 | + | ||
236 | +PropertyInfo qdev_prop_pci_devfn = { | ||
237 | + .name = "pci-devfn", | ||
238 | + .type = PROP_TYPE_UINT32, | ||
239 | + .size = sizeof(uint32_t), | ||
240 | + .parse = parse_pci_devfn, | ||
241 | + .print = print_pci_devfn, | ||
242 | +}; | ||
243 | + | ||
201 | /* --- public helpers --- */ | 244 | /* --- public helpers --- */ |
202 | 245 | ||
203 | static Property *qdev_prop_walk(Property *props, const char *name) | 246 | static Property *qdev_prop_walk(Property *props, const char *name) |
hw/qdev.h
@@ -151,6 +151,7 @@ extern PropertyInfo qdev_prop_hex32; | @@ -151,6 +151,7 @@ extern PropertyInfo qdev_prop_hex32; | ||
151 | extern PropertyInfo qdev_prop_hex64; | 151 | extern PropertyInfo qdev_prop_hex64; |
152 | extern PropertyInfo qdev_prop_ptr; | 152 | extern PropertyInfo qdev_prop_ptr; |
153 | extern PropertyInfo qdev_prop_macaddr; | 153 | extern PropertyInfo qdev_prop_macaddr; |
154 | +extern PropertyInfo qdev_prop_pci_devfn; | ||
154 | 155 | ||
155 | /* Set properties between creation and init. */ | 156 | /* Set properties between creation and init. */ |
156 | void *qdev_get_prop_ptr(DeviceState *dev, Property *prop); | 157 | void *qdev_get_prop_ptr(DeviceState *dev, Property *prop); |