ia64/linux-2.6.18-xen.hg

view drivers/xen/pciback/conf_space.c @ 779:0fadef4d5c46

pciback: Global 'permissive' module parameter.

Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Mon Jan 19 16:25:17 2009 +0000 (2009-01-19)
parents c87adc976013
children 35b981251e10
line source
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...
8 *
9 * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
10 */
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 #define DEFINE_PCI_CONFIG(op,size,type) \
22 int pciback_##op##_config_##size \
23 (struct pci_dev *dev, int offset, type value, void *data) \
24 { \
25 return pci_##op##_config_##size (dev, offset, value); \
26 }
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)
39 {
40 int ret = 0;
41 const struct config_field *field = entry->field;
43 *value = 0;
45 switch (field->size) {
46 case 1:
47 if (field->u.b.read)
48 ret = field->u.b.read(dev, offset, (u8 *) value,
49 entry->data);
50 break;
51 case 2:
52 if (field->u.w.read)
53 ret = field->u.w.read(dev, offset, (u16 *) value,
54 entry->data);
55 break;
56 case 4:
57 if (field->u.dw.read)
58 ret = field->u.dw.read(dev, offset, value, entry->data);
59 break;
60 }
61 return ret;
62 }
64 static int conf_space_write(struct pci_dev *dev,
65 const struct config_field_entry *entry,
66 int offset, u32 value)
67 {
68 int ret = 0;
69 const struct config_field *field = entry->field;
71 switch (field->size) {
72 case 1:
73 if (field->u.b.write)
74 ret = field->u.b.write(dev, offset, (u8) value,
75 entry->data);
76 break;
77 case 2:
78 if (field->u.w.write)
79 ret = field->u.w.write(dev, offset, (u16) value,
80 entry->data);
81 break;
82 case 4:
83 if (field->u.dw.write)
84 ret = field->u.dw.write(dev, offset, value,
85 entry->data);
86 break;
87 }
88 return ret;
89 }
91 static inline u32 get_mask(int size)
92 {
93 if (size == 1)
94 return 0xff;
95 else if (size == 2)
96 return 0xffff;
97 else
98 return 0xffffffff;
99 }
101 static inline int valid_request(int offset, int size)
102 {
103 /* Validate request (no un-aligned requests) */
104 if ((size == 1 || size == 2 || size == 4) && (offset % size) == 0)
105 return 1;
106 return 0;
107 }
109 static inline u32 merge_value(u32 val, u32 new_val, u32 new_val_mask,
110 int offset)
111 {
112 if (offset >= 0) {
113 new_val_mask <<= (offset * 8);
114 new_val <<= (offset * 8);
115 } else {
116 new_val_mask >>= (offset * -8);
117 new_val >>= (offset * -8);
118 }
119 val = (val & ~new_val_mask) | (new_val & new_val_mask);
121 return val;
122 }
124 static int pcibios_err_to_errno(int err)
125 {
126 switch (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;
137 }
138 return err;
139 }
141 int pciback_config_read(struct pci_dev *dev, int offset, int size,
142 u32 * ret_val)
143 {
144 int err = 0;
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 (as if device didn't respond) */
150 u32 value = 0, tmp_val;
152 if (unlikely(verbose_request))
153 printk(KERN_DEBUG "pciback: %s: read %d bytes at 0x%x\n",
154 pci_name(dev), size, offset);
156 if (!valid_request(offset, size)) {
157 err = XEN_PCI_ERR_invalid_offset;
158 goto out;
159 }
161 /* Get the real value first, then modify as appropriate */
162 switch (size) {
163 case 1:
164 err = pci_read_config_byte(dev, offset, (u8 *) & value);
165 break;
166 case 2:
167 err = pci_read_config_word(dev, offset, (u16 *) & value);
168 break;
169 case 4:
170 err = pci_read_config_dword(dev, offset, &value);
171 break;
172 }
174 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
175 field = cfg_entry->field;
177 req_start = offset;
178 req_end = offset + size;
179 field_start = OFFSET(cfg_entry);
180 field_end = OFFSET(cfg_entry) + field->size;
182 if ((req_start >= field_start && req_start < field_end)
183 || (req_end > field_start && req_end <= field_end)) {
184 err = conf_space_read(dev, cfg_entry, field_start,
185 &tmp_val);
186 if (err)
187 goto out;
189 value = merge_value(value, tmp_val,
190 get_mask(field->size),
191 field_start - req_start);
192 }
193 }
195 out:
196 if (unlikely(verbose_request))
197 printk(KERN_DEBUG "pciback: %s: read %d bytes at 0x%x = %x\n",
198 pci_name(dev), size, offset, value);
200 *ret_val = value;
201 return pcibios_err_to_errno(err);
202 }
204 int pciback_config_write(struct pci_dev *dev, int offset, int size, u32 value)
205 {
206 int err = 0, handled = 0;
207 struct pciback_dev_data *dev_data = pci_get_drvdata(dev);
208 const struct config_field_entry *cfg_entry;
209 const struct config_field *field;
210 u32 tmp_val;
211 int req_start, req_end, field_start, field_end;
213 if (unlikely(verbose_request))
214 printk(KERN_DEBUG
215 "pciback: %s: write request %d bytes at 0x%x = %x\n",
216 pci_name(dev), size, offset, value);
218 if (!valid_request(offset, size))
219 return XEN_PCI_ERR_invalid_offset;
221 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
222 field = cfg_entry->field;
224 req_start = offset;
225 req_end = offset + size;
226 field_start = OFFSET(cfg_entry);
227 field_end = OFFSET(cfg_entry) + field->size;
229 if ((req_start >= field_start && req_start < field_end)
230 || (req_end > field_start && req_end <= field_end)) {
231 tmp_val = 0;
233 err = pciback_config_read(dev, field_start,
234 field->size, &tmp_val);
235 if (err)
236 break;
238 tmp_val = merge_value(tmp_val, value, get_mask(size),
239 req_start - field_start);
241 err = conf_space_write(dev, cfg_entry, field_start,
242 tmp_val);
244 /* handled is set true here, but not every byte
245 * may have been written! Properly detecting if
246 * every byte is handled is unnecessary as the
247 * flag is used to detect devices that need
248 * special helpers to work correctly.
249 */
250 handled = 1;
251 }
252 }
254 if (!handled && !err) {
255 /* By default, anything not specificially handled above is
256 * read-only. The permissive flag changes this behavior so
257 * that anything not specifically handled above is writable.
258 * This means that some fields may still be read-only because
259 * they have entries in the config_field list that intercept
260 * the write and do nothing. */
261 if (dev_data->permissive || permissive) {
262 switch (size) {
263 case 1:
264 err = pci_write_config_byte(dev, offset,
265 (u8) value);
266 break;
267 case 2:
268 err = pci_write_config_word(dev, offset,
269 (u16) value);
270 break;
271 case 4:
272 err = pci_write_config_dword(dev, offset,
273 (u32) value);
274 break;
275 }
276 } else if (!dev_data->warned_on_write) {
277 dev_data->warned_on_write = 1;
278 dev_warn(&dev->dev, "Driver tried to write to a "
279 "read-only configuration space field at offset "
280 "0x%x, size %d. This may be harmless, but if "
281 "you have problems with your device:\n"
282 "1) see permissive attribute in sysfs\n"
283 "2) report problems to the xen-devel "
284 "mailing list along with details of your "
285 "device obtained from lspci.\n", offset, size);
286 }
287 }
289 return pcibios_err_to_errno(err);
290 }
292 void pciback_config_free_dyn_fields(struct pci_dev *dev)
293 {
294 struct pciback_dev_data *dev_data = pci_get_drvdata(dev);
295 struct config_field_entry *cfg_entry, *t;
296 const struct config_field *field;
298 dev_dbg(&dev->dev,
299 "free-ing dynamically allocated virtual configuration space fields\n");
301 list_for_each_entry_safe(cfg_entry, t, &dev_data->config_fields, list) {
302 field = cfg_entry->field;
304 if (field->clean) {
305 field->clean((struct config_field *)field);
307 if (cfg_entry->data)
308 kfree(cfg_entry->data);
310 list_del(&cfg_entry->list);
311 kfree(cfg_entry);
312 }
314 }
315 }
317 void pciback_config_reset_dev(struct pci_dev *dev)
318 {
319 struct pciback_dev_data *dev_data = pci_get_drvdata(dev);
320 const struct config_field_entry *cfg_entry;
321 const struct config_field *field;
323 dev_dbg(&dev->dev, "resetting virtual configuration space\n");
325 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
326 field = cfg_entry->field;
328 if (field->reset)
329 field->reset(dev, OFFSET(cfg_entry), cfg_entry->data);
330 }
331 }
333 void pciback_config_free_dev(struct pci_dev *dev)
334 {
335 struct pciback_dev_data *dev_data = pci_get_drvdata(dev);
336 struct config_field_entry *cfg_entry, *t;
337 const struct config_field *field;
339 dev_dbg(&dev->dev, "free-ing virtual configuration space fields\n");
341 list_for_each_entry_safe(cfg_entry, t, &dev_data->config_fields, list) {
342 list_del(&cfg_entry->list);
344 field = cfg_entry->field;
346 if (field->release)
347 field->release(dev, OFFSET(cfg_entry), cfg_entry->data);
349 kfree(cfg_entry);
350 }
351 }
353 int pciback_config_add_field_offset(struct pci_dev *dev,
354 const struct config_field *field,
355 unsigned int base_offset)
356 {
357 int err = 0;
358 struct pciback_dev_data *dev_data = pci_get_drvdata(dev);
359 struct config_field_entry *cfg_entry;
360 void *tmp;
362 cfg_entry = kmalloc(sizeof(*cfg_entry), GFP_KERNEL);
363 if (!cfg_entry) {
364 err = -ENOMEM;
365 goto out;
366 }
368 cfg_entry->data = NULL;
369 cfg_entry->field = field;
370 cfg_entry->base_offset = base_offset;
372 /* silently ignore duplicate fields */
373 err = pciback_field_is_dup(dev,OFFSET(cfg_entry));
374 if (err)
375 goto out;
377 if (field->init) {
378 tmp = field->init(dev, OFFSET(cfg_entry));
380 if (IS_ERR(tmp)) {
381 err = PTR_ERR(tmp);
382 goto out;
383 }
385 cfg_entry->data = tmp;
386 }
388 dev_dbg(&dev->dev, "added config field at offset 0x%02x\n",
389 OFFSET(cfg_entry));
390 list_add_tail(&cfg_entry->list, &dev_data->config_fields);
392 out:
393 if (err)
394 kfree(cfg_entry);
396 return err;
397 }
399 /* This sets up the device's virtual configuration space to keep track of
400 * certain registers (like the base address registers (BARs) so that we can
401 * keep the client from manipulating them directly.
402 */
403 int pciback_config_init_dev(struct pci_dev *dev)
404 {
405 int err = 0;
406 struct pciback_dev_data *dev_data = pci_get_drvdata(dev);
408 dev_dbg(&dev->dev, "initializing virtual configuration space\n");
410 INIT_LIST_HEAD(&dev_data->config_fields);
412 err = pciback_config_header_add_fields(dev);
413 if (err)
414 goto out;
416 err = pciback_config_capability_add_fields(dev);
417 if (err)
418 goto out;
420 err = pciback_config_quirks_init(dev);
422 out:
423 return err;
424 }
426 int pciback_config_init(void)
427 {
428 return pciback_config_capability_init();
429 }