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/pci.h>
15 #include "conf_space.h"
16 #include "conf_space_quirks.h"
18 static int permissive;
19 module_param(permissive, bool, 0644);
21 #define DEFINE_PCI_CONFIG(op, size, type) \
22 int pciback_##op##_config_##size \
23 (struct pci_dev *dev, int offset, type value, void *data) \
25 return pci_##op##_config_##size(dev, offset, value); \
28 DEFINE_PCI_CONFIG(read, byte, u8 *)
29 DEFINE_PCI_CONFIG(read, word, u16 *)
30 DEFINE_PCI_CONFIG(read, dword, u32 *)
32 DEFINE_PCI_CONFIG(write, byte, u8)
33 DEFINE_PCI_CONFIG(write, word, u16)
34 DEFINE_PCI_CONFIG(write, dword, u32)
36 static int conf_space_read(struct pci_dev *dev,
37 const struct config_field_entry *entry,
38 int offset, u32 *value)
41 const struct config_field *field = entry->field;
45 switch (field->size) {
48 ret = field->u.b.read(dev, offset, (u8 *) value,
53 ret = field->u.w.read(dev, offset, (u16 *) value,
58 ret = field->u.dw.read(dev, offset, value, entry->data);
64 static int conf_space_write(struct pci_dev *dev,
65 const struct config_field_entry *entry,
66 int offset, u32 value)
69 const struct config_field *field = entry->field;
71 switch (field->size) {
74 ret = field->u.b.write(dev, offset, (u8) value,
79 ret = field->u.w.write(dev, offset, (u16) value,
83 if (field->u.dw.write)
84 ret = field->u.dw.write(dev, offset, value,
91 static inline u32 get_mask(int size)
101 static inline int valid_request(int offset, int size)
103 /* Validate request (no un-aligned requests) */
104 if ((size == 1 || size == 2 || size == 4) && (offset % size) == 0)
109 static inline u32 merge_value(u32 val, u32 new_val, u32 new_val_mask,
113 new_val_mask <<= (offset * 8);
114 new_val <<= (offset * 8);
116 new_val_mask >>= (offset * -8);
117 new_val >>= (offset * -8);
119 val = (val & ~new_val_mask) | (new_val & new_val_mask);
124 static int pcibios_err_to_errno(int err)
127 case PCIBIOS_SUCCESSFUL:
128 return XEN_PCI_ERR_success;
129 case PCIBIOS_DEVICE_NOT_FOUND:
130 return XEN_PCI_ERR_dev_not_found;
131 case PCIBIOS_BAD_REGISTER_NUMBER:
132 return XEN_PCI_ERR_invalid_offset;
133 case PCIBIOS_FUNC_NOT_SUPPORTED:
134 return XEN_PCI_ERR_not_implemented;
135 case PCIBIOS_SET_FAILED:
136 return XEN_PCI_ERR_access_denied;
141 int pciback_config_read(struct pci_dev *dev, int offset, int size,
145 struct pciback_dev_data *dev_data = pci_get_drvdata(dev);
146 const struct config_field_entry *cfg_entry;
147 const struct config_field *field;
148 int req_start, req_end, field_start, field_end;
149 /* if read fails for any reason, return 0
150 * (as if device didn't respond) */
151 u32 value = 0, tmp_val;
153 if (unlikely(verbose_request))
154 printk(KERN_DEBUG "pciback: %s: read %d bytes at 0x%x\n",
155 pci_name(dev), size, offset);
157 if (!valid_request(offset, size)) {
158 err = XEN_PCI_ERR_invalid_offset;
162 /* Get the real value first, then modify as appropriate */
165 err = pci_read_config_byte(dev, offset, (u8 *) &value);
168 err = pci_read_config_word(dev, offset, (u16 *) &value);
171 err = pci_read_config_dword(dev, offset, &value);
175 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
176 field = cfg_entry->field;
179 req_end = offset + size;
180 field_start = OFFSET(cfg_entry);
181 field_end = OFFSET(cfg_entry) + field->size;
183 if ((req_start >= field_start && req_start < field_end)
184 || (req_end > field_start && req_end <= field_end)) {
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 - req_start);
197 if (unlikely(verbose_request))
198 printk(KERN_DEBUG "pciback: %s: read %d bytes at 0x%x = %x\n",
199 pci_name(dev), size, offset, value);
202 return pcibios_err_to_errno(err);
205 int pciback_config_write(struct pci_dev *dev, int offset, int size, u32 value)
207 int err = 0, handled = 0;
208 struct pciback_dev_data *dev_data = pci_get_drvdata(dev);
209 const struct config_field_entry *cfg_entry;
210 const struct config_field *field;
212 int req_start, req_end, field_start, field_end;
214 if (unlikely(verbose_request))
216 "pciback: %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;
226 req_end = offset + size;
227 field_start = OFFSET(cfg_entry);
228 field_end = OFFSET(cfg_entry) + field->size;
230 if ((req_start >= field_start && req_start < field_end)
231 || (req_end > field_start && req_end <= field_end)) {
234 err = pciback_config_read(dev, field_start,
235 field->size, &tmp_val);
239 tmp_val = merge_value(tmp_val, value, get_mask(size),
240 req_start - field_start);
242 err = conf_space_write(dev, cfg_entry, field_start,
245 /* handled is set true here, but not every byte
246 * may have been written! Properly detecting if
247 * every byte is handled is unnecessary as the
248 * flag is used to detect devices that need
249 * special helpers to work correctly.
255 if (!handled && !err) {
256 /* By default, anything not specificially handled above is
257 * read-only. The permissive flag changes this behavior so
258 * that anything not specifically handled above is writable.
259 * This means that some fields may still be read-only because
260 * they have entries in the config_field list that intercept
261 * the write and do nothing. */
262 if (dev_data->permissive || permissive) {
265 err = pci_write_config_byte(dev, offset,
269 err = pci_write_config_word(dev, offset,
273 err = pci_write_config_dword(dev, offset,
277 } else if (!dev_data->warned_on_write) {
278 dev_data->warned_on_write = 1;
279 dev_warn(&dev->dev, "Driver tried to write to a "
280 "read-only configuration space field at offset"
281 " 0x%x, size %d. This may be harmless, but if "
282 "you have problems with your device:\n"
283 "1) see permissive attribute in sysfs\n"
284 "2) report problems to the xen-devel "
285 "mailing list along with details of your "
286 "device obtained from lspci.\n", offset, size);
290 return pcibios_err_to_errno(err);
293 void pciback_config_free_dyn_fields(struct pci_dev *dev)
295 struct pciback_dev_data *dev_data = pci_get_drvdata(dev);
296 struct config_field_entry *cfg_entry, *t;
297 const struct config_field *field;
299 dev_dbg(&dev->dev, "free-ing dynamically allocated virtual "
300 "configuration space fields\n");
304 list_for_each_entry_safe(cfg_entry, t, &dev_data->config_fields, list) {
305 field = cfg_entry->field;
308 field->clean((struct config_field *)field);
310 kfree(cfg_entry->data);
312 list_del(&cfg_entry->list);
319 void pciback_config_reset_dev(struct pci_dev *dev)
321 struct pciback_dev_data *dev_data = pci_get_drvdata(dev);
322 const struct config_field_entry *cfg_entry;
323 const struct config_field *field;
325 dev_dbg(&dev->dev, "resetting virtual configuration space\n");
329 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
330 field = cfg_entry->field;
333 field->reset(dev, OFFSET(cfg_entry), cfg_entry->data);
337 void pciback_config_free_dev(struct pci_dev *dev)
339 struct pciback_dev_data *dev_data = pci_get_drvdata(dev);
340 struct config_field_entry *cfg_entry, *t;
341 const struct config_field *field;
343 dev_dbg(&dev->dev, "free-ing virtual configuration space fields\n");
347 list_for_each_entry_safe(cfg_entry, t, &dev_data->config_fields, list) {
348 list_del(&cfg_entry->list);
350 field = cfg_entry->field;
353 field->release(dev, OFFSET(cfg_entry), cfg_entry->data);
359 int pciback_config_add_field_offset(struct pci_dev *dev,
360 const struct config_field *field,
361 unsigned int base_offset)
364 struct pciback_dev_data *dev_data = pci_get_drvdata(dev);
365 struct config_field_entry *cfg_entry;
368 cfg_entry = kmalloc(sizeof(*cfg_entry), GFP_KERNEL);
374 cfg_entry->data = NULL;
375 cfg_entry->field = field;
376 cfg_entry->base_offset = base_offset;
378 /* silently ignore duplicate fields */
379 err = pciback_field_is_dup(dev, OFFSET(cfg_entry));
384 tmp = field->init(dev, OFFSET(cfg_entry));
391 cfg_entry->data = tmp;
394 dev_dbg(&dev->dev, "added config field at offset 0x%02x\n",
396 list_add_tail(&cfg_entry->list, &dev_data->config_fields);
405 /* This sets up the device's virtual configuration space to keep track of
406 * certain registers (like the base address registers (BARs) so that we can
407 * keep the client from manipulating them directly.
409 int pciback_config_init_dev(struct pci_dev *dev)
412 struct pciback_dev_data *dev_data = pci_get_drvdata(dev);
414 dev_dbg(&dev->dev, "initializing virtual configuration space\n");
416 INIT_LIST_HEAD(&dev_data->config_fields);
418 err = pciback_config_header_add_fields(dev);
422 err = pciback_config_capability_add_fields(dev);
426 err = pciback_config_quirks_init(dev);
432 int pciback_config_init(void)
434 return pciback_config_capability_init();