2 * PCI Backend - Functions for creating a virtual configuration space for
3 * exported PCI Devices.
4 * It's dangerous to allow PCI Driver Domains to change their
5 * device's resources (memory, i/o ports, interrupts). We need to
6 * restrict changes to certain PCI Configuration registers:
7 * BARs, INTERRUPT_PIN, most registers in the header...
9 * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
12 #include <linux/kernel.h>
13 #include <linux/moduleparam.h>
14 #include <linux/pci.h>
16 #include "conf_space.h"
17 #include "conf_space_quirks.h"
19 bool xen_pcibk_permissive
;
20 module_param_named(permissive
, xen_pcibk_permissive
, bool, 0644);
22 /* This is where xen_pcibk_read_config_byte, xen_pcibk_read_config_word,
23 * xen_pcibk_write_config_word, and xen_pcibk_write_config_byte are created. */
24 #define DEFINE_PCI_CONFIG(op, size, type) \
25 int xen_pcibk_##op##_config_##size \
26 (struct pci_dev *dev, int offset, type value, void *data) \
28 return pci_##op##_config_##size(dev, offset, value); \
31 DEFINE_PCI_CONFIG(read
, byte
, u8
*)
32 DEFINE_PCI_CONFIG(read
, word
, u16
*)
33 DEFINE_PCI_CONFIG(read
, dword
, u32
*)
35 DEFINE_PCI_CONFIG(write
, byte
, u8
)
36 DEFINE_PCI_CONFIG(write
, word
, u16
)
37 DEFINE_PCI_CONFIG(write
, dword
, u32
)
39 static int conf_space_read(struct pci_dev
*dev
,
40 const struct config_field_entry
*entry
,
41 int offset
, u32
*value
)
44 const struct config_field
*field
= entry
->field
;
48 switch (field
->size
) {
51 ret
= field
->u
.b
.read(dev
, offset
, (u8
*) value
,
56 ret
= field
->u
.w
.read(dev
, offset
, (u16
*) value
,
61 ret
= field
->u
.dw
.read(dev
, offset
, value
, entry
->data
);
67 static int conf_space_write(struct pci_dev
*dev
,
68 const struct config_field_entry
*entry
,
69 int offset
, u32 value
)
72 const struct config_field
*field
= entry
->field
;
74 switch (field
->size
) {
77 ret
= field
->u
.b
.write(dev
, offset
, (u8
) value
,
82 ret
= field
->u
.w
.write(dev
, offset
, (u16
) value
,
86 if (field
->u
.dw
.write
)
87 ret
= field
->u
.dw
.write(dev
, offset
, value
,
94 static inline u32
get_mask(int size
)
104 static inline int valid_request(int offset
, int size
)
106 /* Validate request (no un-aligned requests) */
107 if ((size
== 1 || size
== 2 || size
== 4) && (offset
% size
) == 0)
112 static inline u32
merge_value(u32 val
, u32 new_val
, u32 new_val_mask
,
116 new_val_mask
<<= (offset
* 8);
117 new_val
<<= (offset
* 8);
119 new_val_mask
>>= (offset
* -8);
120 new_val
>>= (offset
* -8);
122 val
= (val
& ~new_val_mask
) | (new_val
& new_val_mask
);
127 static int xen_pcibios_err_to_errno(int err
)
130 case PCIBIOS_SUCCESSFUL
:
131 return XEN_PCI_ERR_success
;
132 case PCIBIOS_DEVICE_NOT_FOUND
:
133 return XEN_PCI_ERR_dev_not_found
;
134 case PCIBIOS_BAD_REGISTER_NUMBER
:
135 return XEN_PCI_ERR_invalid_offset
;
136 case PCIBIOS_FUNC_NOT_SUPPORTED
:
137 return XEN_PCI_ERR_not_implemented
;
138 case PCIBIOS_SET_FAILED
:
139 return XEN_PCI_ERR_access_denied
;
144 int xen_pcibk_config_read(struct pci_dev
*dev
, int offset
, int size
,
148 struct xen_pcibk_dev_data
*dev_data
= pci_get_drvdata(dev
);
149 const struct config_field_entry
*cfg_entry
;
150 const struct config_field
*field
;
151 int field_start
, field_end
;
152 /* if read fails for any reason, return 0
153 * (as if device didn't respond) */
154 u32 value
= 0, tmp_val
;
156 if (unlikely(verbose_request
))
157 printk(KERN_DEBUG DRV_NAME
": %s: read %d bytes at 0x%x\n",
158 pci_name(dev
), size
, offset
);
160 if (!valid_request(offset
, size
)) {
161 err
= XEN_PCI_ERR_invalid_offset
;
165 /* Get the real value first, then modify as appropriate */
168 err
= pci_read_config_byte(dev
, offset
, (u8
*) &value
);
171 err
= pci_read_config_word(dev
, offset
, (u16
*) &value
);
174 err
= pci_read_config_dword(dev
, offset
, &value
);
178 list_for_each_entry(cfg_entry
, &dev_data
->config_fields
, list
) {
179 field
= cfg_entry
->field
;
181 field_start
= OFFSET(cfg_entry
);
182 field_end
= OFFSET(cfg_entry
) + field
->size
;
184 if (offset
+ size
> field_start
&& field_end
> offset
) {
185 err
= conf_space_read(dev
, cfg_entry
, field_start
,
190 value
= merge_value(value
, tmp_val
,
191 get_mask(field
->size
),
192 field_start
- offset
);
197 if (unlikely(verbose_request
))
198 printk(KERN_DEBUG DRV_NAME
": %s: read %d bytes at 0x%x = %x\n",
199 pci_name(dev
), size
, offset
, value
);
202 return xen_pcibios_err_to_errno(err
);
205 int xen_pcibk_config_write(struct pci_dev
*dev
, int offset
, int size
, u32 value
)
207 int err
= 0, handled
= 0;
208 struct xen_pcibk_dev_data
*dev_data
= pci_get_drvdata(dev
);
209 const struct config_field_entry
*cfg_entry
;
210 const struct config_field
*field
;
212 int field_start
, field_end
;
214 if (unlikely(verbose_request
))
216 DRV_NAME
": %s: write request %d bytes at 0x%x = %x\n",
217 pci_name(dev
), size
, offset
, value
);
219 if (!valid_request(offset
, size
))
220 return XEN_PCI_ERR_invalid_offset
;
222 list_for_each_entry(cfg_entry
, &dev_data
->config_fields
, list
) {
223 field
= cfg_entry
->field
;
225 field_start
= OFFSET(cfg_entry
);
226 field_end
= OFFSET(cfg_entry
) + field
->size
;
228 if (offset
+ size
> field_start
&& field_end
> offset
) {
229 err
= conf_space_read(dev
, cfg_entry
, field_start
,
234 tmp_val
= merge_value(tmp_val
, value
, get_mask(size
),
235 offset
- field_start
);
237 err
= conf_space_write(dev
, cfg_entry
, field_start
,
240 /* handled is set true here, but not every byte
241 * may have been written! Properly detecting if
242 * every byte is handled is unnecessary as the
243 * flag is used to detect devices that need
244 * special helpers to work correctly.
250 if (!handled
&& !err
) {
251 /* By default, anything not specificially handled above is
252 * read-only. The permissive flag changes this behavior so
253 * that anything not specifically handled above is writable.
254 * This means that some fields may still be read-only because
255 * they have entries in the config_field list that intercept
256 * the write and do nothing. */
257 if (dev_data
->permissive
|| xen_pcibk_permissive
) {
260 err
= pci_write_config_byte(dev
, offset
,
264 err
= pci_write_config_word(dev
, offset
,
268 err
= pci_write_config_dword(dev
, offset
,
272 } else if (!dev_data
->warned_on_write
) {
273 dev_data
->warned_on_write
= 1;
274 dev_warn(&dev
->dev
, "Driver tried to write to a "
275 "read-only configuration space field at offset"
276 " 0x%x, size %d. This may be harmless, but if "
277 "you have problems with your device:\n"
278 "1) see permissive attribute in sysfs\n"
279 "2) report problems to the xen-devel "
280 "mailing list along with details of your "
281 "device obtained from lspci.\n", offset
, size
);
285 return xen_pcibios_err_to_errno(err
);
288 void xen_pcibk_config_free_dyn_fields(struct pci_dev
*dev
)
290 struct xen_pcibk_dev_data
*dev_data
= pci_get_drvdata(dev
);
291 struct config_field_entry
*cfg_entry
, *t
;
292 const struct config_field
*field
;
294 dev_dbg(&dev
->dev
, "free-ing dynamically allocated virtual "
295 "configuration space fields\n");
299 list_for_each_entry_safe(cfg_entry
, t
, &dev_data
->config_fields
, list
) {
300 field
= cfg_entry
->field
;
303 field
->clean((struct config_field
*)field
);
305 kfree(cfg_entry
->data
);
307 list_del(&cfg_entry
->list
);
314 void xen_pcibk_config_reset_dev(struct pci_dev
*dev
)
316 struct xen_pcibk_dev_data
*dev_data
= pci_get_drvdata(dev
);
317 const struct config_field_entry
*cfg_entry
;
318 const struct config_field
*field
;
320 dev_dbg(&dev
->dev
, "resetting virtual configuration space\n");
324 list_for_each_entry(cfg_entry
, &dev_data
->config_fields
, list
) {
325 field
= cfg_entry
->field
;
328 field
->reset(dev
, OFFSET(cfg_entry
), cfg_entry
->data
);
332 void xen_pcibk_config_free_dev(struct pci_dev
*dev
)
334 struct xen_pcibk_dev_data
*dev_data
= pci_get_drvdata(dev
);
335 struct config_field_entry
*cfg_entry
, *t
;
336 const struct config_field
*field
;
338 dev_dbg(&dev
->dev
, "free-ing virtual configuration space fields\n");
342 list_for_each_entry_safe(cfg_entry
, t
, &dev_data
->config_fields
, list
) {
343 list_del(&cfg_entry
->list
);
345 field
= cfg_entry
->field
;
348 field
->release(dev
, OFFSET(cfg_entry
), cfg_entry
->data
);
354 int xen_pcibk_config_add_field_offset(struct pci_dev
*dev
,
355 const struct config_field
*field
,
356 unsigned int base_offset
)
359 struct xen_pcibk_dev_data
*dev_data
= pci_get_drvdata(dev
);
360 struct config_field_entry
*cfg_entry
;
363 cfg_entry
= kmalloc(sizeof(*cfg_entry
), GFP_KERNEL
);
369 cfg_entry
->data
= NULL
;
370 cfg_entry
->field
= field
;
371 cfg_entry
->base_offset
= base_offset
;
373 /* silently ignore duplicate fields */
374 err
= xen_pcibk_field_is_dup(dev
, OFFSET(cfg_entry
));
379 tmp
= field
->init(dev
, OFFSET(cfg_entry
));
386 cfg_entry
->data
= tmp
;
389 dev_dbg(&dev
->dev
, "added config field at offset 0x%02x\n",
391 list_add_tail(&cfg_entry
->list
, &dev_data
->config_fields
);
400 /* This sets up the device's virtual configuration space to keep track of
401 * certain registers (like the base address registers (BARs) so that we can
402 * keep the client from manipulating them directly.
404 int xen_pcibk_config_init_dev(struct pci_dev
*dev
)
407 struct xen_pcibk_dev_data
*dev_data
= pci_get_drvdata(dev
);
409 dev_dbg(&dev
->dev
, "initializing virtual configuration space\n");
411 INIT_LIST_HEAD(&dev_data
->config_fields
);
413 err
= xen_pcibk_config_header_add_fields(dev
);
417 err
= xen_pcibk_config_capability_add_fields(dev
);
421 err
= xen_pcibk_config_quirks_init(dev
);
427 int xen_pcibk_config_init(void)
429 return xen_pcibk_config_capability_init();