Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * PCI Backend - Functions for creating a virtual configuration space for
4 * exported PCI Devices.
5 * It's dangerous to allow PCI Driver Domains to change their
6 * device's resources (memory, i/o ports, interrupts). We need to
7 * restrict changes to certain PCI Configuration registers:
8 * BARs, INTERRUPT_PIN, most registers in the header...
9 *
10 * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
11 */
12
13#define dev_fmt(fmt) DRV_NAME ": " fmt
14
15#include <linux/kernel.h>
16#include <linux/moduleparam.h>
17#include <linux/pci.h>
18#include "pciback.h"
19#include "conf_space.h"
20#include "conf_space_quirks.h"
21
22bool xen_pcibk_permissive;
23module_param_named(permissive, xen_pcibk_permissive, bool, 0644);
24
25/* This is where xen_pcibk_read_config_byte, xen_pcibk_read_config_word,
26 * xen_pcibk_write_config_word, and xen_pcibk_write_config_byte are created. */
27#define DEFINE_PCI_CONFIG(op, size, type) \
28int xen_pcibk_##op##_config_##size \
29(struct pci_dev *dev, int offset, type value, void *data) \
30{ \
31 return pci_##op##_config_##size(dev, offset, value); \
32}
33
34DEFINE_PCI_CONFIG(read, byte, u8 *)
35DEFINE_PCI_CONFIG(read, word, u16 *)
36DEFINE_PCI_CONFIG(read, dword, u32 *)
37
38DEFINE_PCI_CONFIG(write, byte, u8)
39DEFINE_PCI_CONFIG(write, word, u16)
40DEFINE_PCI_CONFIG(write, dword, u32)
41
42static int conf_space_read(struct pci_dev *dev,
43 const struct config_field_entry *entry,
44 int offset, u32 *value)
45{
46 int ret = 0;
47 const struct config_field *field = entry->field;
48
49 *value = 0;
50
51 switch (field->size) {
52 case 1:
53 if (field->u.b.read)
54 ret = field->u.b.read(dev, offset, (u8 *) value,
55 entry->data);
56 break;
57 case 2:
58 if (field->u.w.read)
59 ret = field->u.w.read(dev, offset, (u16 *) value,
60 entry->data);
61 break;
62 case 4:
63 if (field->u.dw.read)
64 ret = field->u.dw.read(dev, offset, value, entry->data);
65 break;
66 }
67 return ret;
68}
69
70static int conf_space_write(struct pci_dev *dev,
71 const struct config_field_entry *entry,
72 int offset, u32 value)
73{
74 int ret = 0;
75 const struct config_field *field = entry->field;
76
77 switch (field->size) {
78 case 1:
79 if (field->u.b.write)
80 ret = field->u.b.write(dev, offset, (u8) value,
81 entry->data);
82 break;
83 case 2:
84 if (field->u.w.write)
85 ret = field->u.w.write(dev, offset, (u16) value,
86 entry->data);
87 break;
88 case 4:
89 if (field->u.dw.write)
90 ret = field->u.dw.write(dev, offset, value,
91 entry->data);
92 break;
93 }
94 return ret;
95}
96
97static inline u32 get_mask(int size)
98{
99 if (size == 1)
100 return 0xff;
101 else if (size == 2)
102 return 0xffff;
103 else
104 return 0xffffffff;
105}
106
107static inline int valid_request(int offset, int size)
108{
109 /* Validate request (no un-aligned requests) */
110 if ((size == 1 || size == 2 || size == 4) && (offset % size) == 0)
111 return 1;
112 return 0;
113}
114
115static inline u32 merge_value(u32 val, u32 new_val, u32 new_val_mask,
116 int offset)
117{
118 if (offset >= 0) {
119 new_val_mask <<= (offset * 8);
120 new_val <<= (offset * 8);
121 } else {
122 new_val_mask >>= (offset * -8);
123 new_val >>= (offset * -8);
124 }
125 val = (val & ~new_val_mask) | (new_val & new_val_mask);
126
127 return val;
128}
129
130static int xen_pcibios_err_to_errno(int err)
131{
132 switch (err) {
133 case PCIBIOS_SUCCESSFUL:
134 return XEN_PCI_ERR_success;
135 case PCIBIOS_DEVICE_NOT_FOUND:
136 return XEN_PCI_ERR_dev_not_found;
137 case PCIBIOS_BAD_REGISTER_NUMBER:
138 return XEN_PCI_ERR_invalid_offset;
139 case PCIBIOS_FUNC_NOT_SUPPORTED:
140 return XEN_PCI_ERR_not_implemented;
141 case PCIBIOS_SET_FAILED:
142 return XEN_PCI_ERR_access_denied;
143 }
144 return err;
145}
146
147int xen_pcibk_config_read(struct pci_dev *dev, int offset, int size,
148 u32 *ret_val)
149{
150 int err = 0;
151 struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
152 const struct config_field_entry *cfg_entry;
153 const struct config_field *field;
154 int field_start, field_end;
155 /* if read fails for any reason, return 0
156 * (as if device didn't respond) */
157 u32 value = 0, tmp_val;
158
159 dev_dbg(&dev->dev, "read %d bytes at 0x%x\n", size, offset);
160
161 if (!valid_request(offset, size)) {
162 err = XEN_PCI_ERR_invalid_offset;
163 goto out;
164 }
165
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;
177 }
178
179 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
180 field = cfg_entry->field;
181
182 field_start = OFFSET(cfg_entry);
183 field_end = OFFSET(cfg_entry) + field->size;
184
185 if (offset + size > field_start && field_end > offset) {
186 err = conf_space_read(dev, cfg_entry, field_start,
187 &tmp_val);
188 if (err)
189 goto out;
190
191 value = merge_value(value, tmp_val,
192 get_mask(field->size),
193 field_start - offset);
194 }
195 }
196
197out:
198 dev_dbg(&dev->dev, "read %d bytes at 0x%x = %x\n", size, offset, value);
199
200 *ret_val = value;
201 return xen_pcibios_err_to_errno(err);
202}
203
204int xen_pcibk_config_write(struct pci_dev *dev, int offset, int size, u32 value)
205{
206 int err = 0, handled = 0;
207 struct xen_pcibk_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 field_start, field_end;
212
213 dev_dbg(&dev->dev, "write request %d bytes at 0x%x = %x\n",
214 size, offset, value);
215
216 if (!valid_request(offset, size))
217 return XEN_PCI_ERR_invalid_offset;
218
219 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
220 field = cfg_entry->field;
221
222 field_start = OFFSET(cfg_entry);
223 field_end = OFFSET(cfg_entry) + field->size;
224
225 if (offset + size > field_start && field_end > offset) {
226 err = conf_space_read(dev, cfg_entry, field_start,
227 &tmp_val);
228 if (err)
229 break;
230
231 tmp_val = merge_value(tmp_val, value, get_mask(size),
232 offset - field_start);
233
234 err = conf_space_write(dev, cfg_entry, field_start,
235 tmp_val);
236
237 /* handled is set true here, but not every byte
238 * may have been written! Properly detecting if
239 * every byte is handled is unnecessary as the
240 * flag is used to detect devices that need
241 * special helpers to work correctly.
242 */
243 handled = 1;
244 }
245 }
246
247 if (!handled && !err) {
248 /* By default, anything not specificially handled above is
249 * read-only. The permissive flag changes this behavior so
250 * that anything not specifically handled above is writable.
251 * This means that some fields may still be read-only because
252 * they have entries in the config_field list that intercept
253 * the write and do nothing. */
254 if (dev_data->permissive || xen_pcibk_permissive) {
255 switch (size) {
256 case 1:
257 err = pci_write_config_byte(dev, offset,
258 (u8) value);
259 break;
260 case 2:
261 err = pci_write_config_word(dev, offset,
262 (u16) value);
263 break;
264 case 4:
265 err = pci_write_config_dword(dev, offset,
266 (u32) value);
267 break;
268 }
269 } else if (!dev_data->warned_on_write) {
270 dev_data->warned_on_write = 1;
271 dev_warn(&dev->dev, "Driver tried to write to a "
272 "read-only configuration space field at offset"
273 " 0x%x, size %d. This may be harmless, but if "
274 "you have problems with your device:\n"
275 "1) see permissive attribute in sysfs\n"
276 "2) report problems to the xen-devel "
277 "mailing list along with details of your "
278 "device obtained from lspci.\n", offset, size);
279 }
280 }
281
282 return xen_pcibios_err_to_errno(err);
283}
284
285int xen_pcibk_get_interrupt_type(struct pci_dev *dev)
286{
287 int err;
288 u16 val;
289 int ret = 0;
290
291 /*
292 * Do not trust dev->msi(x)_enabled here, as enabling could be done
293 * bypassing the pci_*msi* functions, by the qemu.
294 */
295 if (dev->msi_cap) {
296 err = pci_read_config_word(dev,
297 dev->msi_cap + PCI_MSI_FLAGS,
298 &val);
299 if (err)
300 return err;
301 if (val & PCI_MSI_FLAGS_ENABLE)
302 ret |= INTERRUPT_TYPE_MSI;
303 }
304 if (dev->msix_cap) {
305 err = pci_read_config_word(dev,
306 dev->msix_cap + PCI_MSIX_FLAGS,
307 &val);
308 if (err)
309 return err;
310 if (val & PCI_MSIX_FLAGS_ENABLE)
311 ret |= INTERRUPT_TYPE_MSIX;
312 }
313
314 /*
315 * PCIe spec says device cannot use INTx if MSI/MSI-X is enabled,
316 * so check for INTx only when both are disabled.
317 */
318 if (!ret) {
319 err = pci_read_config_word(dev, PCI_COMMAND, &val);
320 if (err)
321 return err;
322 if (!(val & PCI_COMMAND_INTX_DISABLE))
323 ret |= INTERRUPT_TYPE_INTX;
324 }
325
326 return ret ?: INTERRUPT_TYPE_NONE;
327}
328
329void xen_pcibk_config_free_dyn_fields(struct pci_dev *dev)
330{
331 struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
332 struct config_field_entry *cfg_entry, *t;
333 const struct config_field *field;
334
335 dev_dbg(&dev->dev, "free-ing dynamically allocated virtual "
336 "configuration space fields\n");
337 if (!dev_data)
338 return;
339
340 list_for_each_entry_safe(cfg_entry, t, &dev_data->config_fields, list) {
341 field = cfg_entry->field;
342
343 if (field->clean) {
344 field->clean((struct config_field *)field);
345
346 kfree(cfg_entry->data);
347
348 list_del(&cfg_entry->list);
349 kfree(cfg_entry);
350 }
351
352 }
353}
354
355void xen_pcibk_config_reset_dev(struct pci_dev *dev)
356{
357 struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
358 const struct config_field_entry *cfg_entry;
359 const struct config_field *field;
360
361 dev_dbg(&dev->dev, "resetting virtual configuration space\n");
362 if (!dev_data)
363 return;
364
365 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
366 field = cfg_entry->field;
367
368 if (field->reset)
369 field->reset(dev, OFFSET(cfg_entry), cfg_entry->data);
370 }
371}
372
373void xen_pcibk_config_free_dev(struct pci_dev *dev)
374{
375 struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
376 struct config_field_entry *cfg_entry, *t;
377 const struct config_field *field;
378
379 dev_dbg(&dev->dev, "free-ing virtual configuration space fields\n");
380 if (!dev_data)
381 return;
382
383 list_for_each_entry_safe(cfg_entry, t, &dev_data->config_fields, list) {
384 list_del(&cfg_entry->list);
385
386 field = cfg_entry->field;
387
388 if (field->release)
389 field->release(dev, OFFSET(cfg_entry), cfg_entry->data);
390
391 kfree(cfg_entry);
392 }
393}
394
395int xen_pcibk_config_add_field_offset(struct pci_dev *dev,
396 const struct config_field *field,
397 unsigned int base_offset)
398{
399 int err = 0;
400 struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
401 struct config_field_entry *cfg_entry;
402 void *tmp;
403
404 cfg_entry = kmalloc(sizeof(*cfg_entry), GFP_KERNEL);
405 if (!cfg_entry) {
406 err = -ENOMEM;
407 goto out;
408 }
409
410 cfg_entry->data = NULL;
411 cfg_entry->field = field;
412 cfg_entry->base_offset = base_offset;
413
414 /* silently ignore duplicate fields */
415 err = xen_pcibk_field_is_dup(dev, OFFSET(cfg_entry));
416 if (err)
417 goto out;
418
419 if (field->init) {
420 tmp = field->init(dev, OFFSET(cfg_entry));
421
422 if (IS_ERR(tmp)) {
423 err = PTR_ERR(tmp);
424 goto out;
425 }
426
427 cfg_entry->data = tmp;
428 }
429
430 dev_dbg(&dev->dev, "added config field at offset 0x%02x\n",
431 OFFSET(cfg_entry));
432 list_add_tail(&cfg_entry->list, &dev_data->config_fields);
433
434out:
435 if (err)
436 kfree(cfg_entry);
437
438 return err;
439}
440
441/* This sets up the device's virtual configuration space to keep track of
442 * certain registers (like the base address registers (BARs) so that we can
443 * keep the client from manipulating them directly.
444 */
445int xen_pcibk_config_init_dev(struct pci_dev *dev)
446{
447 int err = 0;
448 struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
449
450 dev_dbg(&dev->dev, "initializing virtual configuration space\n");
451
452 INIT_LIST_HEAD(&dev_data->config_fields);
453
454 err = xen_pcibk_config_header_add_fields(dev);
455 if (err)
456 goto out;
457
458 err = xen_pcibk_config_capability_add_fields(dev);
459 if (err)
460 goto out;
461
462 err = xen_pcibk_config_quirks_init(dev);
463
464out:
465 return err;
466}
467
468int xen_pcibk_config_init(void)
469{
470 return xen_pcibk_config_capability_init();
471}
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 */
11
12#include <linux/kernel.h>
13#include <linux/moduleparam.h>
14#include <linux/pci.h>
15#include "pciback.h"
16#include "conf_space.h"
17#include "conf_space_quirks.h"
18
19bool xen_pcibk_permissive;
20module_param_named(permissive, xen_pcibk_permissive, bool, 0644);
21
22/* This is where xen_pcibk_read_config_byte, xen_pcibk_read_config_word,
23 * xen_pcibk_write_config_word, and xen_pcibk_write_config_byte are created. */
24#define DEFINE_PCI_CONFIG(op, size, type) \
25int xen_pcibk_##op##_config_##size \
26(struct pci_dev *dev, int offset, type value, void *data) \
27{ \
28 return pci_##op##_config_##size(dev, offset, value); \
29}
30
31DEFINE_PCI_CONFIG(read, byte, u8 *)
32DEFINE_PCI_CONFIG(read, word, u16 *)
33DEFINE_PCI_CONFIG(read, dword, u32 *)
34
35DEFINE_PCI_CONFIG(write, byte, u8)
36DEFINE_PCI_CONFIG(write, word, u16)
37DEFINE_PCI_CONFIG(write, dword, u32)
38
39static int conf_space_read(struct pci_dev *dev,
40 const struct config_field_entry *entry,
41 int offset, u32 *value)
42{
43 int ret = 0;
44 const struct config_field *field = entry->field;
45
46 *value = 0;
47
48 switch (field->size) {
49 case 1:
50 if (field->u.b.read)
51 ret = field->u.b.read(dev, offset, (u8 *) value,
52 entry->data);
53 break;
54 case 2:
55 if (field->u.w.read)
56 ret = field->u.w.read(dev, offset, (u16 *) value,
57 entry->data);
58 break;
59 case 4:
60 if (field->u.dw.read)
61 ret = field->u.dw.read(dev, offset, value, entry->data);
62 break;
63 }
64 return ret;
65}
66
67static int conf_space_write(struct pci_dev *dev,
68 const struct config_field_entry *entry,
69 int offset, u32 value)
70{
71 int ret = 0;
72 const struct config_field *field = entry->field;
73
74 switch (field->size) {
75 case 1:
76 if (field->u.b.write)
77 ret = field->u.b.write(dev, offset, (u8) value,
78 entry->data);
79 break;
80 case 2:
81 if (field->u.w.write)
82 ret = field->u.w.write(dev, offset, (u16) value,
83 entry->data);
84 break;
85 case 4:
86 if (field->u.dw.write)
87 ret = field->u.dw.write(dev, offset, value,
88 entry->data);
89 break;
90 }
91 return ret;
92}
93
94static inline u32 get_mask(int size)
95{
96 if (size == 1)
97 return 0xff;
98 else if (size == 2)
99 return 0xffff;
100 else
101 return 0xffffffff;
102}
103
104static inline int valid_request(int offset, int size)
105{
106 /* Validate request (no un-aligned requests) */
107 if ((size == 1 || size == 2 || size == 4) && (offset % size) == 0)
108 return 1;
109 return 0;
110}
111
112static inline u32 merge_value(u32 val, u32 new_val, u32 new_val_mask,
113 int offset)
114{
115 if (offset >= 0) {
116 new_val_mask <<= (offset * 8);
117 new_val <<= (offset * 8);
118 } else {
119 new_val_mask >>= (offset * -8);
120 new_val >>= (offset * -8);
121 }
122 val = (val & ~new_val_mask) | (new_val & new_val_mask);
123
124 return val;
125}
126
127static int xen_pcibios_err_to_errno(int err)
128{
129 switch (err) {
130 case PCIBIOS_SUCCESSFUL:
131 return XEN_PCI_ERR_success;
132 case PCIBIOS_DEVICE_NOT_FOUND:
133 return XEN_PCI_ERR_dev_not_found;
134 case PCIBIOS_BAD_REGISTER_NUMBER:
135 return XEN_PCI_ERR_invalid_offset;
136 case PCIBIOS_FUNC_NOT_SUPPORTED:
137 return XEN_PCI_ERR_not_implemented;
138 case PCIBIOS_SET_FAILED:
139 return XEN_PCI_ERR_access_denied;
140 }
141 return err;
142}
143
144int xen_pcibk_config_read(struct pci_dev *dev, int offset, int size,
145 u32 *ret_val)
146{
147 int err = 0;
148 struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
149 const struct config_field_entry *cfg_entry;
150 const struct config_field *field;
151 int field_start, field_end;
152 /* if read fails for any reason, return 0
153 * (as if device didn't respond) */
154 u32 value = 0, tmp_val;
155
156 if (unlikely(verbose_request))
157 printk(KERN_DEBUG DRV_NAME ": %s: read %d bytes at 0x%x\n",
158 pci_name(dev), size, offset);
159
160 if (!valid_request(offset, size)) {
161 err = XEN_PCI_ERR_invalid_offset;
162 goto out;
163 }
164
165 /* Get the real value first, then modify as appropriate */
166 switch (size) {
167 case 1:
168 err = pci_read_config_byte(dev, offset, (u8 *) &value);
169 break;
170 case 2:
171 err = pci_read_config_word(dev, offset, (u16 *) &value);
172 break;
173 case 4:
174 err = pci_read_config_dword(dev, offset, &value);
175 break;
176 }
177
178 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
179 field = cfg_entry->field;
180
181 field_start = OFFSET(cfg_entry);
182 field_end = OFFSET(cfg_entry) + field->size;
183
184 if (offset + size > field_start && field_end > offset) {
185 err = conf_space_read(dev, cfg_entry, field_start,
186 &tmp_val);
187 if (err)
188 goto out;
189
190 value = merge_value(value, tmp_val,
191 get_mask(field->size),
192 field_start - offset);
193 }
194 }
195
196out:
197 if (unlikely(verbose_request))
198 printk(KERN_DEBUG DRV_NAME ": %s: read %d bytes at 0x%x = %x\n",
199 pci_name(dev), size, offset, value);
200
201 *ret_val = value;
202 return xen_pcibios_err_to_errno(err);
203}
204
205int xen_pcibk_config_write(struct pci_dev *dev, int offset, int size, u32 value)
206{
207 int err = 0, handled = 0;
208 struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
209 const struct config_field_entry *cfg_entry;
210 const struct config_field *field;
211 u32 tmp_val;
212 int field_start, field_end;
213
214 if (unlikely(verbose_request))
215 printk(KERN_DEBUG
216 DRV_NAME ": %s: write request %d bytes at 0x%x = %x\n",
217 pci_name(dev), size, offset, value);
218
219 if (!valid_request(offset, size))
220 return XEN_PCI_ERR_invalid_offset;
221
222 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
223 field = cfg_entry->field;
224
225 field_start = OFFSET(cfg_entry);
226 field_end = OFFSET(cfg_entry) + field->size;
227
228 if (offset + size > field_start && field_end > offset) {
229 err = conf_space_read(dev, cfg_entry, field_start,
230 &tmp_val);
231 if (err)
232 break;
233
234 tmp_val = merge_value(tmp_val, value, get_mask(size),
235 offset - field_start);
236
237 err = conf_space_write(dev, cfg_entry, field_start,
238 tmp_val);
239
240 /* handled is set true here, but not every byte
241 * may have been written! Properly detecting if
242 * every byte is handled is unnecessary as the
243 * flag is used to detect devices that need
244 * special helpers to work correctly.
245 */
246 handled = 1;
247 }
248 }
249
250 if (!handled && !err) {
251 /* By default, anything not specificially handled above is
252 * read-only. The permissive flag changes this behavior so
253 * that anything not specifically handled above is writable.
254 * This means that some fields may still be read-only because
255 * they have entries in the config_field list that intercept
256 * the write and do nothing. */
257 if (dev_data->permissive || xen_pcibk_permissive) {
258 switch (size) {
259 case 1:
260 err = pci_write_config_byte(dev, offset,
261 (u8) value);
262 break;
263 case 2:
264 err = pci_write_config_word(dev, offset,
265 (u16) value);
266 break;
267 case 4:
268 err = pci_write_config_dword(dev, offset,
269 (u32) value);
270 break;
271 }
272 } else if (!dev_data->warned_on_write) {
273 dev_data->warned_on_write = 1;
274 dev_warn(&dev->dev, "Driver tried to write to a "
275 "read-only configuration space field at offset"
276 " 0x%x, size %d. This may be harmless, but if "
277 "you have problems with your device:\n"
278 "1) see permissive attribute in sysfs\n"
279 "2) report problems to the xen-devel "
280 "mailing list along with details of your "
281 "device obtained from lspci.\n", offset, size);
282 }
283 }
284
285 return xen_pcibios_err_to_errno(err);
286}
287
288void xen_pcibk_config_free_dyn_fields(struct pci_dev *dev)
289{
290 struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
291 struct config_field_entry *cfg_entry, *t;
292 const struct config_field *field;
293
294 dev_dbg(&dev->dev, "free-ing dynamically allocated virtual "
295 "configuration space fields\n");
296 if (!dev_data)
297 return;
298
299 list_for_each_entry_safe(cfg_entry, t, &dev_data->config_fields, list) {
300 field = cfg_entry->field;
301
302 if (field->clean) {
303 field->clean((struct config_field *)field);
304
305 kfree(cfg_entry->data);
306
307 list_del(&cfg_entry->list);
308 kfree(cfg_entry);
309 }
310
311 }
312}
313
314void xen_pcibk_config_reset_dev(struct pci_dev *dev)
315{
316 struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
317 const struct config_field_entry *cfg_entry;
318 const struct config_field *field;
319
320 dev_dbg(&dev->dev, "resetting virtual configuration space\n");
321 if (!dev_data)
322 return;
323
324 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
325 field = cfg_entry->field;
326
327 if (field->reset)
328 field->reset(dev, OFFSET(cfg_entry), cfg_entry->data);
329 }
330}
331
332void xen_pcibk_config_free_dev(struct pci_dev *dev)
333{
334 struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
335 struct config_field_entry *cfg_entry, *t;
336 const struct config_field *field;
337
338 dev_dbg(&dev->dev, "free-ing virtual configuration space fields\n");
339 if (!dev_data)
340 return;
341
342 list_for_each_entry_safe(cfg_entry, t, &dev_data->config_fields, list) {
343 list_del(&cfg_entry->list);
344
345 field = cfg_entry->field;
346
347 if (field->release)
348 field->release(dev, OFFSET(cfg_entry), cfg_entry->data);
349
350 kfree(cfg_entry);
351 }
352}
353
354int xen_pcibk_config_add_field_offset(struct pci_dev *dev,
355 const struct config_field *field,
356 unsigned int base_offset)
357{
358 int err = 0;
359 struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
360 struct config_field_entry *cfg_entry;
361 void *tmp;
362
363 cfg_entry = kmalloc(sizeof(*cfg_entry), GFP_KERNEL);
364 if (!cfg_entry) {
365 err = -ENOMEM;
366 goto out;
367 }
368
369 cfg_entry->data = NULL;
370 cfg_entry->field = field;
371 cfg_entry->base_offset = base_offset;
372
373 /* silently ignore duplicate fields */
374 err = xen_pcibk_field_is_dup(dev, OFFSET(cfg_entry));
375 if (err)
376 goto out;
377
378 if (field->init) {
379 tmp = field->init(dev, OFFSET(cfg_entry));
380
381 if (IS_ERR(tmp)) {
382 err = PTR_ERR(tmp);
383 goto out;
384 }
385
386 cfg_entry->data = tmp;
387 }
388
389 dev_dbg(&dev->dev, "added config field at offset 0x%02x\n",
390 OFFSET(cfg_entry));
391 list_add_tail(&cfg_entry->list, &dev_data->config_fields);
392
393out:
394 if (err)
395 kfree(cfg_entry);
396
397 return err;
398}
399
400/* This sets up the device's virtual configuration space to keep track of
401 * certain registers (like the base address registers (BARs) so that we can
402 * keep the client from manipulating them directly.
403 */
404int xen_pcibk_config_init_dev(struct pci_dev *dev)
405{
406 int err = 0;
407 struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
408
409 dev_dbg(&dev->dev, "initializing virtual configuration space\n");
410
411 INIT_LIST_HEAD(&dev_data->config_fields);
412
413 err = xen_pcibk_config_header_add_fields(dev);
414 if (err)
415 goto out;
416
417 err = xen_pcibk_config_capability_add_fields(dev);
418 if (err)
419 goto out;
420
421 err = xen_pcibk_config_quirks_init(dev);
422
423out:
424 return err;
425}
426
427int xen_pcibk_config_init(void)
428{
429 return xen_pcibk_config_capability_init();
430}