ia64/linux-2.6.18-xen.hg

view drivers/xen/pciback/conf_space.c @ 859:35b981251e10

linux/pci_back: fix NULL pointer ref.

pcistub_device_release() can be called during
initialization. Thus pci_get_drvdata() can return NULL.
Fix it by inserting NULL check.

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
author Keir Fraser <keir.fraser@citrix.com>
date Tue Apr 14 11:16:08 2009 +0100 (2009-04-14)
parents 0fadef4d5c46
children
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");
300 if (!dev_data)
301 return;
303 list_for_each_entry_safe(cfg_entry, t, &dev_data->config_fields, list) {
304 field = cfg_entry->field;
306 if (field->clean) {
307 field->clean((struct config_field *)field);
309 if (cfg_entry->data)
310 kfree(cfg_entry->data);
312 list_del(&cfg_entry->list);
313 kfree(cfg_entry);
314 }
316 }
317 }
319 void pciback_config_reset_dev(struct pci_dev *dev)
320 {
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");
326 if (!dev_data)
327 return;
329 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
330 field = cfg_entry->field;
332 if (field->reset)
333 field->reset(dev, OFFSET(cfg_entry), cfg_entry->data);
334 }
335 }
337 void pciback_config_free_dev(struct pci_dev *dev)
338 {
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");
344 if (!dev_data)
345 return;
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;
352 if (field->release)
353 field->release(dev, OFFSET(cfg_entry), cfg_entry->data);
355 kfree(cfg_entry);
356 }
357 }
359 int pciback_config_add_field_offset(struct pci_dev *dev,
360 const struct config_field *field,
361 unsigned int base_offset)
362 {
363 int err = 0;
364 struct pciback_dev_data *dev_data = pci_get_drvdata(dev);
365 struct config_field_entry *cfg_entry;
366 void *tmp;
368 cfg_entry = kmalloc(sizeof(*cfg_entry), GFP_KERNEL);
369 if (!cfg_entry) {
370 err = -ENOMEM;
371 goto out;
372 }
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));
380 if (err)
381 goto out;
383 if (field->init) {
384 tmp = field->init(dev, OFFSET(cfg_entry));
386 if (IS_ERR(tmp)) {
387 err = PTR_ERR(tmp);
388 goto out;
389 }
391 cfg_entry->data = tmp;
392 }
394 dev_dbg(&dev->dev, "added config field at offset 0x%02x\n",
395 OFFSET(cfg_entry));
396 list_add_tail(&cfg_entry->list, &dev_data->config_fields);
398 out:
399 if (err)
400 kfree(cfg_entry);
402 return err;
403 }
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.
408 */
409 int pciback_config_init_dev(struct pci_dev *dev)
410 {
411 int err = 0;
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);
419 if (err)
420 goto out;
422 err = pciback_config_capability_add_fields(dev);
423 if (err)
424 goto out;
426 err = pciback_config_quirks_init(dev);
428 out:
429 return err;
430 }
432 int pciback_config_init(void)
433 {
434 return pciback_config_capability_init();
435 }