Merge branch 'for-davem' of git://git.kernel.org/pub/scm/linux/kernel/git/linville...
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / xen / xen-pciback / conf_space.c
blob444345afbd5cfa92b8a1b3b9877ab14108bbf73c
1 /*
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>
14 #include "pciback.h"
15 #include "conf_space.h"
16 #include "conf_space_quirks.h"
18 static int permissive;
19 module_param(permissive, bool, 0644);
21 /* This is where xen_pcibk_read_config_byte, xen_pcibk_read_config_word,
22 * xen_pcibk_write_config_word, and xen_pcibk_write_config_byte are created. */
23 #define DEFINE_PCI_CONFIG(op, size, type) \
24 int xen_pcibk_##op##_config_##size \
25 (struct pci_dev *dev, int offset, type value, void *data) \
26 { \
27 return pci_##op##_config_##size(dev, offset, value); \
30 DEFINE_PCI_CONFIG(read, byte, u8 *)
31 DEFINE_PCI_CONFIG(read, word, u16 *)
32 DEFINE_PCI_CONFIG(read, dword, u32 *)
34 DEFINE_PCI_CONFIG(write, byte, u8)
35 DEFINE_PCI_CONFIG(write, word, u16)
36 DEFINE_PCI_CONFIG(write, dword, u32)
38 static int conf_space_read(struct pci_dev *dev,
39 const struct config_field_entry *entry,
40 int offset, u32 *value)
42 int ret = 0;
43 const struct config_field *field = entry->field;
45 *value = 0;
47 switch (field->size) {
48 case 1:
49 if (field->u.b.read)
50 ret = field->u.b.read(dev, offset, (u8 *) value,
51 entry->data);
52 break;
53 case 2:
54 if (field->u.w.read)
55 ret = field->u.w.read(dev, offset, (u16 *) value,
56 entry->data);
57 break;
58 case 4:
59 if (field->u.dw.read)
60 ret = field->u.dw.read(dev, offset, value, entry->data);
61 break;
63 return ret;
66 static int conf_space_write(struct pci_dev *dev,
67 const struct config_field_entry *entry,
68 int offset, u32 value)
70 int ret = 0;
71 const struct config_field *field = entry->field;
73 switch (field->size) {
74 case 1:
75 if (field->u.b.write)
76 ret = field->u.b.write(dev, offset, (u8) value,
77 entry->data);
78 break;
79 case 2:
80 if (field->u.w.write)
81 ret = field->u.w.write(dev, offset, (u16) value,
82 entry->data);
83 break;
84 case 4:
85 if (field->u.dw.write)
86 ret = field->u.dw.write(dev, offset, value,
87 entry->data);
88 break;
90 return ret;
93 static inline u32 get_mask(int size)
95 if (size == 1)
96 return 0xff;
97 else if (size == 2)
98 return 0xffff;
99 else
100 return 0xffffffff;
103 static inline int valid_request(int offset, int size)
105 /* Validate request (no un-aligned requests) */
106 if ((size == 1 || size == 2 || size == 4) && (offset % size) == 0)
107 return 1;
108 return 0;
111 static inline u32 merge_value(u32 val, u32 new_val, u32 new_val_mask,
112 int offset)
114 if (offset >= 0) {
115 new_val_mask <<= (offset * 8);
116 new_val <<= (offset * 8);
117 } else {
118 new_val_mask >>= (offset * -8);
119 new_val >>= (offset * -8);
121 val = (val & ~new_val_mask) | (new_val & new_val_mask);
123 return val;
126 static int pcibios_err_to_errno(int err)
128 switch (err) {
129 case PCIBIOS_SUCCESSFUL:
130 return XEN_PCI_ERR_success;
131 case PCIBIOS_DEVICE_NOT_FOUND:
132 return XEN_PCI_ERR_dev_not_found;
133 case PCIBIOS_BAD_REGISTER_NUMBER:
134 return XEN_PCI_ERR_invalid_offset;
135 case PCIBIOS_FUNC_NOT_SUPPORTED:
136 return XEN_PCI_ERR_not_implemented;
137 case PCIBIOS_SET_FAILED:
138 return XEN_PCI_ERR_access_denied;
140 return err;
143 int xen_pcibk_config_read(struct pci_dev *dev, int offset, int size,
144 u32 *ret_val)
146 int err = 0;
147 struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
148 const struct config_field_entry *cfg_entry;
149 const struct config_field *field;
150 int req_start, req_end, field_start, field_end;
151 /* if read fails for any reason, return 0
152 * (as if device didn't respond) */
153 u32 value = 0, tmp_val;
155 if (unlikely(verbose_request))
156 printk(KERN_DEBUG DRV_NAME ": %s: read %d bytes at 0x%x\n",
157 pci_name(dev), size, offset);
159 if (!valid_request(offset, size)) {
160 err = XEN_PCI_ERR_invalid_offset;
161 goto out;
164 /* Get the real value first, then modify as appropriate */
165 switch (size) {
166 case 1:
167 err = pci_read_config_byte(dev, offset, (u8 *) &value);
168 break;
169 case 2:
170 err = pci_read_config_word(dev, offset, (u16 *) &value);
171 break;
172 case 4:
173 err = pci_read_config_dword(dev, offset, &value);
174 break;
177 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
178 field = cfg_entry->field;
180 req_start = offset;
181 req_end = offset + size;
182 field_start = OFFSET(cfg_entry);
183 field_end = OFFSET(cfg_entry) + field->size;
185 if ((req_start >= field_start && req_start < field_end)
186 || (req_end > field_start && req_end <= field_end)) {
187 err = conf_space_read(dev, cfg_entry, field_start,
188 &tmp_val);
189 if (err)
190 goto out;
192 value = merge_value(value, tmp_val,
193 get_mask(field->size),
194 field_start - req_start);
198 out:
199 if (unlikely(verbose_request))
200 printk(KERN_DEBUG DRV_NAME ": %s: read %d bytes at 0x%x = %x\n",
201 pci_name(dev), size, offset, value);
203 *ret_val = value;
204 return pcibios_err_to_errno(err);
207 int xen_pcibk_config_write(struct pci_dev *dev, int offset, int size, u32 value)
209 int err = 0, handled = 0;
210 struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
211 const struct config_field_entry *cfg_entry;
212 const struct config_field *field;
213 u32 tmp_val;
214 int req_start, req_end, field_start, field_end;
216 if (unlikely(verbose_request))
217 printk(KERN_DEBUG
218 DRV_NAME ": %s: write request %d bytes at 0x%x = %x\n",
219 pci_name(dev), size, offset, value);
221 if (!valid_request(offset, size))
222 return XEN_PCI_ERR_invalid_offset;
224 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
225 field = cfg_entry->field;
227 req_start = offset;
228 req_end = offset + size;
229 field_start = OFFSET(cfg_entry);
230 field_end = OFFSET(cfg_entry) + field->size;
232 if ((req_start >= field_start && req_start < field_end)
233 || (req_end > field_start && req_end <= field_end)) {
234 tmp_val = 0;
236 err = xen_pcibk_config_read(dev, field_start,
237 field->size, &tmp_val);
238 if (err)
239 break;
241 tmp_val = merge_value(tmp_val, value, get_mask(size),
242 req_start - field_start);
244 err = conf_space_write(dev, cfg_entry, field_start,
245 tmp_val);
247 /* handled is set true here, but not every byte
248 * may have been written! Properly detecting if
249 * every byte is handled is unnecessary as the
250 * flag is used to detect devices that need
251 * special helpers to work correctly.
253 handled = 1;
257 if (!handled && !err) {
258 /* By default, anything not specificially handled above is
259 * read-only. The permissive flag changes this behavior so
260 * that anything not specifically handled above is writable.
261 * This means that some fields may still be read-only because
262 * they have entries in the config_field list that intercept
263 * the write and do nothing. */
264 if (dev_data->permissive || permissive) {
265 switch (size) {
266 case 1:
267 err = pci_write_config_byte(dev, offset,
268 (u8) value);
269 break;
270 case 2:
271 err = pci_write_config_word(dev, offset,
272 (u16) value);
273 break;
274 case 4:
275 err = pci_write_config_dword(dev, offset,
276 (u32) value);
277 break;
279 } else if (!dev_data->warned_on_write) {
280 dev_data->warned_on_write = 1;
281 dev_warn(&dev->dev, "Driver tried to write to a "
282 "read-only configuration space field at offset"
283 " 0x%x, size %d. This may be harmless, but if "
284 "you have problems with your device:\n"
285 "1) see permissive attribute in sysfs\n"
286 "2) report problems to the xen-devel "
287 "mailing list along with details of your "
288 "device obtained from lspci.\n", offset, size);
292 return pcibios_err_to_errno(err);
295 void xen_pcibk_config_free_dyn_fields(struct pci_dev *dev)
297 struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
298 struct config_field_entry *cfg_entry, *t;
299 const struct config_field *field;
301 dev_dbg(&dev->dev, "free-ing dynamically allocated virtual "
302 "configuration space fields\n");
303 if (!dev_data)
304 return;
306 list_for_each_entry_safe(cfg_entry, t, &dev_data->config_fields, list) {
307 field = cfg_entry->field;
309 if (field->clean) {
310 field->clean((struct config_field *)field);
312 kfree(cfg_entry->data);
314 list_del(&cfg_entry->list);
315 kfree(cfg_entry);
321 void xen_pcibk_config_reset_dev(struct pci_dev *dev)
323 struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
324 const struct config_field_entry *cfg_entry;
325 const struct config_field *field;
327 dev_dbg(&dev->dev, "resetting virtual configuration space\n");
328 if (!dev_data)
329 return;
331 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
332 field = cfg_entry->field;
334 if (field->reset)
335 field->reset(dev, OFFSET(cfg_entry), cfg_entry->data);
339 void xen_pcibk_config_free_dev(struct pci_dev *dev)
341 struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
342 struct config_field_entry *cfg_entry, *t;
343 const struct config_field *field;
345 dev_dbg(&dev->dev, "free-ing virtual configuration space fields\n");
346 if (!dev_data)
347 return;
349 list_for_each_entry_safe(cfg_entry, t, &dev_data->config_fields, list) {
350 list_del(&cfg_entry->list);
352 field = cfg_entry->field;
354 if (field->release)
355 field->release(dev, OFFSET(cfg_entry), cfg_entry->data);
357 kfree(cfg_entry);
361 int xen_pcibk_config_add_field_offset(struct pci_dev *dev,
362 const struct config_field *field,
363 unsigned int base_offset)
365 int err = 0;
366 struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
367 struct config_field_entry *cfg_entry;
368 void *tmp;
370 cfg_entry = kmalloc(sizeof(*cfg_entry), GFP_KERNEL);
371 if (!cfg_entry) {
372 err = -ENOMEM;
373 goto out;
376 cfg_entry->data = NULL;
377 cfg_entry->field = field;
378 cfg_entry->base_offset = base_offset;
380 /* silently ignore duplicate fields */
381 err = xen_pcibk_field_is_dup(dev, OFFSET(cfg_entry));
382 if (err)
383 goto out;
385 if (field->init) {
386 tmp = field->init(dev, OFFSET(cfg_entry));
388 if (IS_ERR(tmp)) {
389 err = PTR_ERR(tmp);
390 goto out;
393 cfg_entry->data = tmp;
396 dev_dbg(&dev->dev, "added config field at offset 0x%02x\n",
397 OFFSET(cfg_entry));
398 list_add_tail(&cfg_entry->list, &dev_data->config_fields);
400 out:
401 if (err)
402 kfree(cfg_entry);
404 return err;
407 /* This sets up the device's virtual configuration space to keep track of
408 * certain registers (like the base address registers (BARs) so that we can
409 * keep the client from manipulating them directly.
411 int xen_pcibk_config_init_dev(struct pci_dev *dev)
413 int err = 0;
414 struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
416 dev_dbg(&dev->dev, "initializing virtual configuration space\n");
418 INIT_LIST_HEAD(&dev_data->config_fields);
420 err = xen_pcibk_config_header_add_fields(dev);
421 if (err)
422 goto out;
424 err = xen_pcibk_config_capability_add_fields(dev);
425 if (err)
426 goto out;
428 err = xen_pcibk_config_quirks_init(dev);
430 out:
431 return err;
434 int xen_pcibk_config_init(void)
436 return xen_pcibk_config_capability_init();