conf_space.c 11 KB

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