Add linux-next specific files for 20110831
[linux-2.6/next.git] / drivers / xen / xen-pciback / conf_space.c
blobc1d80112891cd405875ca2a9c276f4f0bfd17113
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/module.h>
14 #include <linux/pci.h>
15 #include "pciback.h"
16 #include "conf_space.h"
17 #include "conf_space_quirks.h"
19 #define DRV_NAME "xen-pciback"
20 static int permissive;
21 module_param(permissive, bool, 0644);
23 /* This is where xen_pcibk_read_config_byte, xen_pcibk_read_config_word,
24 * xen_pcibk_write_config_word, and xen_pcibk_write_config_byte are created. */
25 #define DEFINE_PCI_CONFIG(op, size, type) \
26 int xen_pcibk_##op##_config_##size \
27 (struct pci_dev *dev, int offset, type value, void *data) \
28 { \
29 return pci_##op##_config_##size(dev, offset, value); \
32 DEFINE_PCI_CONFIG(read, byte, u8 *)
33 DEFINE_PCI_CONFIG(read, word, u16 *)
34 DEFINE_PCI_CONFIG(read, dword, u32 *)
36 DEFINE_PCI_CONFIG(write, byte, u8)
37 DEFINE_PCI_CONFIG(write, word, u16)
38 DEFINE_PCI_CONFIG(write, dword, u32)
40 static int conf_space_read(struct pci_dev *dev,
41 const struct config_field_entry *entry,
42 int offset, u32 *value)
44 int ret = 0;
45 const struct config_field *field = entry->field;
47 *value = 0;
49 switch (field->size) {
50 case 1:
51 if (field->u.b.read)
52 ret = field->u.b.read(dev, offset, (u8 *) value,
53 entry->data);
54 break;
55 case 2:
56 if (field->u.w.read)
57 ret = field->u.w.read(dev, offset, (u16 *) value,
58 entry->data);
59 break;
60 case 4:
61 if (field->u.dw.read)
62 ret = field->u.dw.read(dev, offset, value, entry->data);
63 break;
65 return ret;
68 static int conf_space_write(struct pci_dev *dev,
69 const struct config_field_entry *entry,
70 int offset, u32 value)
72 int ret = 0;
73 const struct config_field *field = entry->field;
75 switch (field->size) {
76 case 1:
77 if (field->u.b.write)
78 ret = field->u.b.write(dev, offset, (u8) value,
79 entry->data);
80 break;
81 case 2:
82 if (field->u.w.write)
83 ret = field->u.w.write(dev, offset, (u16) value,
84 entry->data);
85 break;
86 case 4:
87 if (field->u.dw.write)
88 ret = field->u.dw.write(dev, offset, value,
89 entry->data);
90 break;
92 return ret;
95 static inline u32 get_mask(int size)
97 if (size == 1)
98 return 0xff;
99 else if (size == 2)
100 return 0xffff;
101 else
102 return 0xffffffff;
105 static inline int valid_request(int offset, int size)
107 /* Validate request (no un-aligned requests) */
108 if ((size == 1 || size == 2 || size == 4) && (offset % size) == 0)
109 return 1;
110 return 0;
113 static inline u32 merge_value(u32 val, u32 new_val, u32 new_val_mask,
114 int offset)
116 if (offset >= 0) {
117 new_val_mask <<= (offset * 8);
118 new_val <<= (offset * 8);
119 } else {
120 new_val_mask >>= (offset * -8);
121 new_val >>= (offset * -8);
123 val = (val & ~new_val_mask) | (new_val & new_val_mask);
125 return val;
128 static int pcibios_err_to_errno(int err)
130 switch (err) {
131 case PCIBIOS_SUCCESSFUL:
132 return XEN_PCI_ERR_success;
133 case PCIBIOS_DEVICE_NOT_FOUND:
134 return XEN_PCI_ERR_dev_not_found;
135 case PCIBIOS_BAD_REGISTER_NUMBER:
136 return XEN_PCI_ERR_invalid_offset;
137 case PCIBIOS_FUNC_NOT_SUPPORTED:
138 return XEN_PCI_ERR_not_implemented;
139 case PCIBIOS_SET_FAILED:
140 return XEN_PCI_ERR_access_denied;
142 return err;
145 int xen_pcibk_config_read(struct pci_dev *dev, int offset, int size,
146 u32 *ret_val)
148 int err = 0;
149 struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
150 const struct config_field_entry *cfg_entry;
151 const struct config_field *field;
152 int req_start, req_end, field_start, field_end;
153 /* if read fails for any reason, return 0
154 * (as if device didn't respond) */
155 u32 value = 0, tmp_val;
157 if (unlikely(verbose_request))
158 printk(KERN_DEBUG DRV_NAME ": %s: read %d bytes at 0x%x\n",
159 pci_name(dev), size, offset);
161 if (!valid_request(offset, size)) {
162 err = XEN_PCI_ERR_invalid_offset;
163 goto out;
166 /* Get the real value first, then modify as appropriate */
167 switch (size) {
168 case 1:
169 err = pci_read_config_byte(dev, offset, (u8 *) &value);
170 break;
171 case 2:
172 err = pci_read_config_word(dev, offset, (u16 *) &value);
173 break;
174 case 4:
175 err = pci_read_config_dword(dev, offset, &value);
176 break;
179 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
180 field = cfg_entry->field;
182 req_start = offset;
183 req_end = offset + size;
184 field_start = OFFSET(cfg_entry);
185 field_end = OFFSET(cfg_entry) + field->size;
187 if ((req_start >= field_start && req_start < field_end)
188 || (req_end > field_start && req_end <= field_end)) {
189 err = conf_space_read(dev, cfg_entry, field_start,
190 &tmp_val);
191 if (err)
192 goto out;
194 value = merge_value(value, tmp_val,
195 get_mask(field->size),
196 field_start - req_start);
200 out:
201 if (unlikely(verbose_request))
202 printk(KERN_DEBUG DRV_NAME ": %s: read %d bytes at 0x%x = %x\n",
203 pci_name(dev), size, offset, value);
205 *ret_val = value;
206 return pcibios_err_to_errno(err);
209 int xen_pcibk_config_write(struct pci_dev *dev, int offset, int size, u32 value)
211 int err = 0, handled = 0;
212 struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
213 const struct config_field_entry *cfg_entry;
214 const struct config_field *field;
215 u32 tmp_val;
216 int req_start, req_end, field_start, field_end;
218 if (unlikely(verbose_request))
219 printk(KERN_DEBUG
220 DRV_NAME ": %s: write request %d bytes at 0x%x = %x\n",
221 pci_name(dev), size, offset, value);
223 if (!valid_request(offset, size))
224 return XEN_PCI_ERR_invalid_offset;
226 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
227 field = cfg_entry->field;
229 req_start = offset;
230 req_end = offset + size;
231 field_start = OFFSET(cfg_entry);
232 field_end = OFFSET(cfg_entry) + field->size;
234 if ((req_start >= field_start && req_start < field_end)
235 || (req_end > field_start && req_end <= field_end)) {
236 tmp_val = 0;
238 err = xen_pcibk_config_read(dev, field_start,
239 field->size, &tmp_val);
240 if (err)
241 break;
243 tmp_val = merge_value(tmp_val, value, get_mask(size),
244 req_start - field_start);
246 err = conf_space_write(dev, cfg_entry, field_start,
247 tmp_val);
249 /* handled is set true here, but not every byte
250 * may have been written! Properly detecting if
251 * every byte is handled is unnecessary as the
252 * flag is used to detect devices that need
253 * special helpers to work correctly.
255 handled = 1;
259 if (!handled && !err) {
260 /* By default, anything not specificially handled above is
261 * read-only. The permissive flag changes this behavior so
262 * that anything not specifically handled above is writable.
263 * This means that some fields may still be read-only because
264 * they have entries in the config_field list that intercept
265 * the write and do nothing. */
266 if (dev_data->permissive || permissive) {
267 switch (size) {
268 case 1:
269 err = pci_write_config_byte(dev, offset,
270 (u8) value);
271 break;
272 case 2:
273 err = pci_write_config_word(dev, offset,
274 (u16) value);
275 break;
276 case 4:
277 err = pci_write_config_dword(dev, offset,
278 (u32) value);
279 break;
281 } else if (!dev_data->warned_on_write) {
282 dev_data->warned_on_write = 1;
283 dev_warn(&dev->dev, "Driver tried to write to a "
284 "read-only configuration space field at offset"
285 " 0x%x, size %d. This may be harmless, but if "
286 "you have problems with your device:\n"
287 "1) see permissive attribute in sysfs\n"
288 "2) report problems to the xen-devel "
289 "mailing list along with details of your "
290 "device obtained from lspci.\n", offset, size);
294 return pcibios_err_to_errno(err);
297 void xen_pcibk_config_free_dyn_fields(struct pci_dev *dev)
299 struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
300 struct config_field_entry *cfg_entry, *t;
301 const struct config_field *field;
303 dev_dbg(&dev->dev, "free-ing dynamically allocated virtual "
304 "configuration space fields\n");
305 if (!dev_data)
306 return;
308 list_for_each_entry_safe(cfg_entry, t, &dev_data->config_fields, list) {
309 field = cfg_entry->field;
311 if (field->clean) {
312 field->clean((struct config_field *)field);
314 kfree(cfg_entry->data);
316 list_del(&cfg_entry->list);
317 kfree(cfg_entry);
323 void xen_pcibk_config_reset_dev(struct pci_dev *dev)
325 struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
326 const struct config_field_entry *cfg_entry;
327 const struct config_field *field;
329 dev_dbg(&dev->dev, "resetting virtual configuration space\n");
330 if (!dev_data)
331 return;
333 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
334 field = cfg_entry->field;
336 if (field->reset)
337 field->reset(dev, OFFSET(cfg_entry), cfg_entry->data);
341 void xen_pcibk_config_free_dev(struct pci_dev *dev)
343 struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
344 struct config_field_entry *cfg_entry, *t;
345 const struct config_field *field;
347 dev_dbg(&dev->dev, "free-ing virtual configuration space fields\n");
348 if (!dev_data)
349 return;
351 list_for_each_entry_safe(cfg_entry, t, &dev_data->config_fields, list) {
352 list_del(&cfg_entry->list);
354 field = cfg_entry->field;
356 if (field->release)
357 field->release(dev, OFFSET(cfg_entry), cfg_entry->data);
359 kfree(cfg_entry);
363 int xen_pcibk_config_add_field_offset(struct pci_dev *dev,
364 const struct config_field *field,
365 unsigned int base_offset)
367 int err = 0;
368 struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
369 struct config_field_entry *cfg_entry;
370 void *tmp;
372 cfg_entry = kmalloc(sizeof(*cfg_entry), GFP_KERNEL);
373 if (!cfg_entry) {
374 err = -ENOMEM;
375 goto out;
378 cfg_entry->data = NULL;
379 cfg_entry->field = field;
380 cfg_entry->base_offset = base_offset;
382 /* silently ignore duplicate fields */
383 err = xen_pcibk_field_is_dup(dev, OFFSET(cfg_entry));
384 if (err)
385 goto out;
387 if (field->init) {
388 tmp = field->init(dev, OFFSET(cfg_entry));
390 if (IS_ERR(tmp)) {
391 err = PTR_ERR(tmp);
392 goto out;
395 cfg_entry->data = tmp;
398 dev_dbg(&dev->dev, "added config field at offset 0x%02x\n",
399 OFFSET(cfg_entry));
400 list_add_tail(&cfg_entry->list, &dev_data->config_fields);
402 out:
403 if (err)
404 kfree(cfg_entry);
406 return err;
409 /* This sets up the device's virtual configuration space to keep track of
410 * certain registers (like the base address registers (BARs) so that we can
411 * keep the client from manipulating them directly.
413 int xen_pcibk_config_init_dev(struct pci_dev *dev)
415 int err = 0;
416 struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
418 dev_dbg(&dev->dev, "initializing virtual configuration space\n");
420 INIT_LIST_HEAD(&dev_data->config_fields);
422 err = xen_pcibk_config_header_add_fields(dev);
423 if (err)
424 goto out;
426 err = xen_pcibk_config_capability_add_fields(dev);
427 if (err)
428 goto out;
430 err = xen_pcibk_config_quirks_init(dev);
432 out:
433 return err;
436 int xen_pcibk_config_init(void)
438 return xen_pcibk_config_capability_init();