conf_space.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/pci.h>
  14. #include "pciback.h"
  15. #include "conf_space.h"
  16. #include "conf_space_quirks.h"
  17. bool xen_pcibk_permissive;
  18. module_param_named(permissive, xen_pcibk_permissive, bool, 0644);
  19. /* This is where xen_pcibk_read_config_byte, xen_pcibk_read_config_word,
  20. * xen_pcibk_write_config_word, and xen_pcibk_write_config_byte are created. */
  21. #define DEFINE_PCI_CONFIG(op, size, type) \
  22. int xen_pcibk_##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. }
  27. DEFINE_PCI_CONFIG(read, byte, u8 *)
  28. DEFINE_PCI_CONFIG(read, word, u16 *)
  29. DEFINE_PCI_CONFIG(read, dword, u32 *)
  30. DEFINE_PCI_CONFIG(write, byte, u8)
  31. DEFINE_PCI_CONFIG(write, word, u16)
  32. DEFINE_PCI_CONFIG(write, dword, u32)
  33. static int conf_space_read(struct pci_dev *dev,
  34. const struct config_field_entry *entry,
  35. int offset, u32 *value)
  36. {
  37. int ret = 0;
  38. const struct config_field *field = entry->field;
  39. *value = 0;
  40. switch (field->size) {
  41. case 1:
  42. if (field->u.b.read)
  43. ret = field->u.b.read(dev, offset, (u8 *) value,
  44. entry->data);
  45. break;
  46. case 2:
  47. if (field->u.w.read)
  48. ret = field->u.w.read(dev, offset, (u16 *) value,
  49. entry->data);
  50. break;
  51. case 4:
  52. if (field->u.dw.read)
  53. ret = field->u.dw.read(dev, offset, value, entry->data);
  54. break;
  55. }
  56. return ret;
  57. }
  58. static int conf_space_write(struct pci_dev *dev,
  59. const struct config_field_entry *entry,
  60. int offset, u32 value)
  61. {
  62. int ret = 0;
  63. const struct config_field *field = entry->field;
  64. switch (field->size) {
  65. case 1:
  66. if (field->u.b.write)
  67. ret = field->u.b.write(dev, offset, (u8) value,
  68. entry->data);
  69. break;
  70. case 2:
  71. if (field->u.w.write)
  72. ret = field->u.w.write(dev, offset, (u16) value,
  73. entry->data);
  74. break;
  75. case 4:
  76. if (field->u.dw.write)
  77. ret = field->u.dw.write(dev, offset, value,
  78. entry->data);
  79. break;
  80. }
  81. return ret;
  82. }
  83. static inline u32 get_mask(int size)
  84. {
  85. if (size == 1)
  86. return 0xff;
  87. else if (size == 2)
  88. return 0xffff;
  89. else
  90. return 0xffffffff;
  91. }
  92. static inline int valid_request(int offset, int size)
  93. {
  94. /* Validate request (no un-aligned requests) */
  95. if ((size == 1 || size == 2 || size == 4) && (offset % size) == 0)
  96. return 1;
  97. return 0;
  98. }
  99. static inline u32 merge_value(u32 val, u32 new_val, u32 new_val_mask,
  100. int offset)
  101. {
  102. if (offset >= 0) {
  103. new_val_mask <<= (offset * 8);
  104. new_val <<= (offset * 8);
  105. } else {
  106. new_val_mask >>= (offset * -8);
  107. new_val >>= (offset * -8);
  108. }
  109. val = (val & ~new_val_mask) | (new_val & new_val_mask);
  110. return val;
  111. }
  112. static int pcibios_err_to_errno(int err)
  113. {
  114. switch (err) {
  115. case PCIBIOS_SUCCESSFUL:
  116. return XEN_PCI_ERR_success;
  117. case PCIBIOS_DEVICE_NOT_FOUND:
  118. return XEN_PCI_ERR_dev_not_found;
  119. case PCIBIOS_BAD_REGISTER_NUMBER:
  120. return XEN_PCI_ERR_invalid_offset;
  121. case PCIBIOS_FUNC_NOT_SUPPORTED:
  122. return XEN_PCI_ERR_not_implemented;
  123. case PCIBIOS_SET_FAILED:
  124. return XEN_PCI_ERR_access_denied;
  125. }
  126. return err;
  127. }
  128. int xen_pcibk_config_read(struct pci_dev *dev, int offset, int size,
  129. u32 *ret_val)
  130. {
  131. int err = 0;
  132. struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
  133. const struct config_field_entry *cfg_entry;
  134. const struct config_field *field;
  135. int req_start, req_end, field_start, field_end;
  136. /* if read fails for any reason, return 0
  137. * (as if device didn't respond) */
  138. u32 value = 0, tmp_val;
  139. if (unlikely(verbose_request))
  140. printk(KERN_DEBUG DRV_NAME ": %s: read %d bytes at 0x%x\n",
  141. pci_name(dev), size, offset);
  142. if (!valid_request(offset, size)) {
  143. err = XEN_PCI_ERR_invalid_offset;
  144. goto out;
  145. }
  146. /* Get the real value first, then modify as appropriate */
  147. switch (size) {
  148. case 1:
  149. err = pci_read_config_byte(dev, offset, (u8 *) &value);
  150. break;
  151. case 2:
  152. err = pci_read_config_word(dev, offset, (u16 *) &value);
  153. break;
  154. case 4:
  155. err = pci_read_config_dword(dev, offset, &value);
  156. break;
  157. }
  158. list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
  159. field = cfg_entry->field;
  160. req_start = offset;
  161. req_end = offset + size;
  162. field_start = OFFSET(cfg_entry);
  163. field_end = OFFSET(cfg_entry) + field->size;
  164. if ((req_start >= field_start && req_start < field_end)
  165. || (req_end > field_start && req_end <= field_end)) {
  166. err = conf_space_read(dev, cfg_entry, field_start,
  167. &tmp_val);
  168. if (err)
  169. goto out;
  170. value = merge_value(value, tmp_val,
  171. get_mask(field->size),
  172. field_start - req_start);
  173. }
  174. }
  175. out:
  176. if (unlikely(verbose_request))
  177. printk(KERN_DEBUG DRV_NAME ": %s: read %d bytes at 0x%x = %x\n",
  178. pci_name(dev), size, offset, value);
  179. *ret_val = value;
  180. return pcibios_err_to_errno(err);
  181. }
  182. int xen_pcibk_config_write(struct pci_dev *dev, int offset, int size, u32 value)
  183. {
  184. int err = 0, handled = 0;
  185. struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
  186. const struct config_field_entry *cfg_entry;
  187. const struct config_field *field;
  188. u32 tmp_val;
  189. int req_start, req_end, field_start, field_end;
  190. if (unlikely(verbose_request))
  191. printk(KERN_DEBUG
  192. DRV_NAME ": %s: write request %d bytes at 0x%x = %x\n",
  193. pci_name(dev), size, offset, value);
  194. if (!valid_request(offset, size))
  195. return XEN_PCI_ERR_invalid_offset;
  196. list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
  197. field = cfg_entry->field;
  198. req_start = offset;
  199. req_end = offset + size;
  200. field_start = OFFSET(cfg_entry);
  201. field_end = OFFSET(cfg_entry) + field->size;
  202. if ((req_start >= field_start && req_start < field_end)
  203. || (req_end > field_start && req_end <= field_end)) {
  204. tmp_val = 0;
  205. err = xen_pcibk_config_read(dev, field_start,
  206. field->size, &tmp_val);
  207. if (err)
  208. break;
  209. tmp_val = merge_value(tmp_val, value, get_mask(size),
  210. req_start - field_start);
  211. err = conf_space_write(dev, cfg_entry, field_start,
  212. tmp_val);
  213. /* handled is set true here, but not every byte
  214. * may have been written! Properly detecting if
  215. * every byte is handled is unnecessary as the
  216. * flag is used to detect devices that need
  217. * special helpers to work correctly.
  218. */
  219. handled = 1;
  220. }
  221. }
  222. if (!handled && !err) {
  223. /* By default, anything not specificially handled above is
  224. * read-only. The permissive flag changes this behavior so
  225. * that anything not specifically handled above is writable.
  226. * This means that some fields may still be read-only because
  227. * they have entries in the config_field list that intercept
  228. * the write and do nothing. */
  229. if (dev_data->permissive || xen_pcibk_permissive) {
  230. switch (size) {
  231. case 1:
  232. err = pci_write_config_byte(dev, offset,
  233. (u8) value);
  234. break;
  235. case 2:
  236. err = pci_write_config_word(dev, offset,
  237. (u16) value);
  238. break;
  239. case 4:
  240. err = pci_write_config_dword(dev, offset,
  241. (u32) value);
  242. break;
  243. }
  244. } else if (!dev_data->warned_on_write) {
  245. dev_data->warned_on_write = 1;
  246. dev_warn(&dev->dev, "Driver tried to write to a "
  247. "read-only configuration space field at offset"
  248. " 0x%x, size %d. This may be harmless, but if "
  249. "you have problems with your device:\n"
  250. "1) see permissive attribute in sysfs\n"
  251. "2) report problems to the xen-devel "
  252. "mailing list along with details of your "
  253. "device obtained from lspci.\n", offset, size);
  254. }
  255. }
  256. return pcibios_err_to_errno(err);
  257. }
  258. void xen_pcibk_config_free_dyn_fields(struct pci_dev *dev)
  259. {
  260. struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
  261. struct config_field_entry *cfg_entry, *t;
  262. const struct config_field *field;
  263. dev_dbg(&dev->dev, "free-ing dynamically allocated virtual "
  264. "configuration space fields\n");
  265. if (!dev_data)
  266. return;
  267. list_for_each_entry_safe(cfg_entry, t, &dev_data->config_fields, list) {
  268. field = cfg_entry->field;
  269. if (field->clean) {
  270. field->clean((struct config_field *)field);
  271. kfree(cfg_entry->data);
  272. list_del(&cfg_entry->list);
  273. kfree(cfg_entry);
  274. }
  275. }
  276. }
  277. void xen_pcibk_config_reset_dev(struct pci_dev *dev)
  278. {
  279. struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
  280. const struct config_field_entry *cfg_entry;
  281. const struct config_field *field;
  282. dev_dbg(&dev->dev, "resetting virtual configuration space\n");
  283. if (!dev_data)
  284. return;
  285. list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
  286. field = cfg_entry->field;
  287. if (field->reset)
  288. field->reset(dev, OFFSET(cfg_entry), cfg_entry->data);
  289. }
  290. }
  291. void xen_pcibk_config_free_dev(struct pci_dev *dev)
  292. {
  293. struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
  294. struct config_field_entry *cfg_entry, *t;
  295. const struct config_field *field;
  296. dev_dbg(&dev->dev, "free-ing virtual configuration space fields\n");
  297. if (!dev_data)
  298. return;
  299. list_for_each_entry_safe(cfg_entry, t, &dev_data->config_fields, list) {
  300. list_del(&cfg_entry->list);
  301. field = cfg_entry->field;
  302. if (field->release)
  303. field->release(dev, OFFSET(cfg_entry), cfg_entry->data);
  304. kfree(cfg_entry);
  305. }
  306. }
  307. int xen_pcibk_config_add_field_offset(struct pci_dev *dev,
  308. const struct config_field *field,
  309. unsigned int base_offset)
  310. {
  311. int err = 0;
  312. struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
  313. struct config_field_entry *cfg_entry;
  314. void *tmp;
  315. cfg_entry = kmalloc(sizeof(*cfg_entry), GFP_KERNEL);
  316. if (!cfg_entry) {
  317. err = -ENOMEM;
  318. goto out;
  319. }
  320. cfg_entry->data = NULL;
  321. cfg_entry->field = field;
  322. cfg_entry->base_offset = base_offset;
  323. /* silently ignore duplicate fields */
  324. err = xen_pcibk_field_is_dup(dev, OFFSET(cfg_entry));
  325. if (err)
  326. goto out;
  327. if (field->init) {
  328. tmp = field->init(dev, OFFSET(cfg_entry));
  329. if (IS_ERR(tmp)) {
  330. err = PTR_ERR(tmp);
  331. goto out;
  332. }
  333. cfg_entry->data = tmp;
  334. }
  335. dev_dbg(&dev->dev, "added config field at offset 0x%02x\n",
  336. OFFSET(cfg_entry));
  337. list_add_tail(&cfg_entry->list, &dev_data->config_fields);
  338. out:
  339. if (err)
  340. kfree(cfg_entry);
  341. return err;
  342. }
  343. /* This sets up the device's virtual configuration space to keep track of
  344. * certain registers (like the base address registers (BARs) so that we can
  345. * keep the client from manipulating them directly.
  346. */
  347. int xen_pcibk_config_init_dev(struct pci_dev *dev)
  348. {
  349. int err = 0;
  350. struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
  351. dev_dbg(&dev->dev, "initializing virtual configuration space\n");
  352. INIT_LIST_HEAD(&dev_data->config_fields);
  353. err = xen_pcibk_config_header_add_fields(dev);
  354. if (err)
  355. goto out;
  356. err = xen_pcibk_config_capability_add_fields(dev);
  357. if (err)
  358. goto out;
  359. err = xen_pcibk_config_quirks_init(dev);
  360. out:
  361. return err;
  362. }
  363. int xen_pcibk_config_init(void)
  364. {
  365. return xen_pcibk_config_capability_init();
  366. }