conf_space.c 11 KB

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