conf_space.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 xen_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_end > field_start && field_end > req_start) {
  165. err = conf_space_read(dev, cfg_entry, field_start,
  166. &tmp_val);
  167. if (err)
  168. goto out;
  169. value = merge_value(value, tmp_val,
  170. get_mask(field->size),
  171. field_start - req_start);
  172. }
  173. }
  174. out:
  175. if (unlikely(verbose_request))
  176. printk(KERN_DEBUG DRV_NAME ": %s: read %d bytes at 0x%x = %x\n",
  177. pci_name(dev), size, offset, value);
  178. *ret_val = value;
  179. return xen_pcibios_err_to_errno(err);
  180. }
  181. int xen_pcibk_config_write(struct pci_dev *dev, int offset, int size, u32 value)
  182. {
  183. int err = 0, handled = 0;
  184. struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
  185. const struct config_field_entry *cfg_entry;
  186. const struct config_field *field;
  187. u32 tmp_val;
  188. int req_start, req_end, field_start, field_end;
  189. if (unlikely(verbose_request))
  190. printk(KERN_DEBUG
  191. DRV_NAME ": %s: write request %d bytes at 0x%x = %x\n",
  192. pci_name(dev), size, offset, value);
  193. if (!valid_request(offset, size))
  194. return XEN_PCI_ERR_invalid_offset;
  195. list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
  196. field = cfg_entry->field;
  197. req_start = offset;
  198. req_end = offset + size;
  199. field_start = OFFSET(cfg_entry);
  200. field_end = OFFSET(cfg_entry) + field->size;
  201. if (req_end > field_start && field_end > req_start) {
  202. tmp_val = 0;
  203. err = xen_pcibk_config_read(dev, field_start,
  204. field->size, &tmp_val);
  205. if (err)
  206. break;
  207. tmp_val = merge_value(tmp_val, value, get_mask(size),
  208. req_start - field_start);
  209. err = conf_space_write(dev, cfg_entry, field_start,
  210. tmp_val);
  211. /* handled is set true here, but not every byte
  212. * may have been written! Properly detecting if
  213. * every byte is handled is unnecessary as the
  214. * flag is used to detect devices that need
  215. * special helpers to work correctly.
  216. */
  217. handled = 1;
  218. }
  219. }
  220. if (!handled && !err) {
  221. /* By default, anything not specificially handled above is
  222. * read-only. The permissive flag changes this behavior so
  223. * that anything not specifically handled above is writable.
  224. * This means that some fields may still be read-only because
  225. * they have entries in the config_field list that intercept
  226. * the write and do nothing. */
  227. if (dev_data->permissive || xen_pcibk_permissive) {
  228. switch (size) {
  229. case 1:
  230. err = pci_write_config_byte(dev, offset,
  231. (u8) value);
  232. break;
  233. case 2:
  234. err = pci_write_config_word(dev, offset,
  235. (u16) value);
  236. break;
  237. case 4:
  238. err = pci_write_config_dword(dev, offset,
  239. (u32) value);
  240. break;
  241. }
  242. } else if (!dev_data->warned_on_write) {
  243. dev_data->warned_on_write = 1;
  244. dev_warn(&dev->dev, "Driver tried to write to a "
  245. "read-only configuration space field at offset"
  246. " 0x%x, size %d. This may be harmless, but if "
  247. "you have problems with your device:\n"
  248. "1) see permissive attribute in sysfs\n"
  249. "2) report problems to the xen-devel "
  250. "mailing list along with details of your "
  251. "device obtained from lspci.\n", offset, size);
  252. }
  253. }
  254. return xen_pcibios_err_to_errno(err);
  255. }
  256. void xen_pcibk_config_free_dyn_fields(struct pci_dev *dev)
  257. {
  258. struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
  259. struct config_field_entry *cfg_entry, *t;
  260. const struct config_field *field;
  261. dev_dbg(&dev->dev, "free-ing dynamically allocated virtual "
  262. "configuration space fields\n");
  263. if (!dev_data)
  264. return;
  265. list_for_each_entry_safe(cfg_entry, t, &dev_data->config_fields, list) {
  266. field = cfg_entry->field;
  267. if (field->clean) {
  268. field->clean((struct config_field *)field);
  269. kfree(cfg_entry->data);
  270. list_del(&cfg_entry->list);
  271. kfree(cfg_entry);
  272. }
  273. }
  274. }
  275. void xen_pcibk_config_reset_dev(struct pci_dev *dev)
  276. {
  277. struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
  278. const struct config_field_entry *cfg_entry;
  279. const struct config_field *field;
  280. dev_dbg(&dev->dev, "resetting virtual configuration space\n");
  281. if (!dev_data)
  282. return;
  283. list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
  284. field = cfg_entry->field;
  285. if (field->reset)
  286. field->reset(dev, OFFSET(cfg_entry), cfg_entry->data);
  287. }
  288. }
  289. void xen_pcibk_config_free_dev(struct pci_dev *dev)
  290. {
  291. struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
  292. struct config_field_entry *cfg_entry, *t;
  293. const struct config_field *field;
  294. dev_dbg(&dev->dev, "free-ing virtual configuration space fields\n");
  295. if (!dev_data)
  296. return;
  297. list_for_each_entry_safe(cfg_entry, t, &dev_data->config_fields, list) {
  298. list_del(&cfg_entry->list);
  299. field = cfg_entry->field;
  300. if (field->release)
  301. field->release(dev, OFFSET(cfg_entry), cfg_entry->data);
  302. kfree(cfg_entry);
  303. }
  304. }
  305. int xen_pcibk_config_add_field_offset(struct pci_dev *dev,
  306. const struct config_field *field,
  307. unsigned int base_offset)
  308. {
  309. int err = 0;
  310. struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
  311. struct config_field_entry *cfg_entry;
  312. void *tmp;
  313. cfg_entry = kmalloc(sizeof(*cfg_entry), GFP_KERNEL);
  314. if (!cfg_entry) {
  315. err = -ENOMEM;
  316. goto out;
  317. }
  318. cfg_entry->data = NULL;
  319. cfg_entry->field = field;
  320. cfg_entry->base_offset = base_offset;
  321. /* silently ignore duplicate fields */
  322. err = xen_pcibk_field_is_dup(dev, OFFSET(cfg_entry));
  323. if (err)
  324. goto out;
  325. if (field->init) {
  326. tmp = field->init(dev, OFFSET(cfg_entry));
  327. if (IS_ERR(tmp)) {
  328. err = PTR_ERR(tmp);
  329. goto out;
  330. }
  331. cfg_entry->data = tmp;
  332. }
  333. dev_dbg(&dev->dev, "added config field at offset 0x%02x\n",
  334. OFFSET(cfg_entry));
  335. list_add_tail(&cfg_entry->list, &dev_data->config_fields);
  336. out:
  337. if (err)
  338. kfree(cfg_entry);
  339. return err;
  340. }
  341. /* This sets up the device's virtual configuration space to keep track of
  342. * certain registers (like the base address registers (BARs) so that we can
  343. * keep the client from manipulating them directly.
  344. */
  345. int xen_pcibk_config_init_dev(struct pci_dev *dev)
  346. {
  347. int err = 0;
  348. struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
  349. dev_dbg(&dev->dev, "initializing virtual configuration space\n");
  350. INIT_LIST_HEAD(&dev_data->config_fields);
  351. err = xen_pcibk_config_header_add_fields(dev);
  352. if (err)
  353. goto out;
  354. err = xen_pcibk_config_capability_add_fields(dev);
  355. if (err)
  356. goto out;
  357. err = xen_pcibk_config_quirks_init(dev);
  358. out:
  359. return err;
  360. }
  361. int xen_pcibk_config_init(void)
  362. {
  363. return xen_pcibk_config_capability_init();
  364. }