access.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. #include <linux/delay.h>
  2. #include <linux/pci.h>
  3. #include <linux/module.h>
  4. #include <linux/sched.h>
  5. #include <linux/ioport.h>
  6. #include <linux/wait.h>
  7. #include "pci.h"
  8. /*
  9. * This interrupt-safe spinlock protects all accesses to PCI
  10. * configuration space.
  11. */
  12. static DEFINE_SPINLOCK(pci_lock);
  13. /*
  14. * Wrappers for all PCI configuration access functions. They just check
  15. * alignment, do locking and call the low-level functions pointed to
  16. * by pci_dev->ops.
  17. */
  18. #define PCI_byte_BAD 0
  19. #define PCI_word_BAD (pos & 1)
  20. #define PCI_dword_BAD (pos & 3)
  21. #define PCI_OP_READ(size,type,len) \
  22. int pci_bus_read_config_##size \
  23. (struct pci_bus *bus, unsigned int devfn, int pos, type *value) \
  24. { \
  25. int res; \
  26. unsigned long flags; \
  27. u32 data = 0; \
  28. if (PCI_##size##_BAD) return PCIBIOS_BAD_REGISTER_NUMBER; \
  29. spin_lock_irqsave(&pci_lock, flags); \
  30. res = bus->ops->read(bus, devfn, pos, len, &data); \
  31. *value = (type)data; \
  32. spin_unlock_irqrestore(&pci_lock, flags); \
  33. return res; \
  34. }
  35. #define PCI_OP_WRITE(size,type,len) \
  36. int pci_bus_write_config_##size \
  37. (struct pci_bus *bus, unsigned int devfn, int pos, type value) \
  38. { \
  39. int res; \
  40. unsigned long flags; \
  41. if (PCI_##size##_BAD) return PCIBIOS_BAD_REGISTER_NUMBER; \
  42. spin_lock_irqsave(&pci_lock, flags); \
  43. res = bus->ops->write(bus, devfn, pos, len, value); \
  44. spin_unlock_irqrestore(&pci_lock, flags); \
  45. return res; \
  46. }
  47. PCI_OP_READ(byte, u8, 1)
  48. PCI_OP_READ(word, u16, 2)
  49. PCI_OP_READ(dword, u32, 4)
  50. PCI_OP_WRITE(byte, u8, 1)
  51. PCI_OP_WRITE(word, u16, 2)
  52. PCI_OP_WRITE(dword, u32, 4)
  53. EXPORT_SYMBOL(pci_bus_read_config_byte);
  54. EXPORT_SYMBOL(pci_bus_read_config_word);
  55. EXPORT_SYMBOL(pci_bus_read_config_dword);
  56. EXPORT_SYMBOL(pci_bus_write_config_byte);
  57. EXPORT_SYMBOL(pci_bus_write_config_word);
  58. EXPORT_SYMBOL(pci_bus_write_config_dword);
  59. /**
  60. * pci_read_vpd - Read one entry from Vital Product Data
  61. * @dev: pci device struct
  62. * @pos: offset in vpd space
  63. * @count: number of bytes to read
  64. * @buf: pointer to where to store result
  65. *
  66. */
  67. ssize_t pci_read_vpd(struct pci_dev *dev, loff_t pos, size_t count, void *buf)
  68. {
  69. if (!dev->vpd || !dev->vpd->ops)
  70. return -ENODEV;
  71. return dev->vpd->ops->read(dev, pos, count, buf);
  72. }
  73. EXPORT_SYMBOL(pci_read_vpd);
  74. /**
  75. * pci_write_vpd - Write entry to Vital Product Data
  76. * @dev: pci device struct
  77. * @pos: offset in vpd space
  78. * @count: number of bytes to read
  79. * @val: value to write
  80. *
  81. */
  82. ssize_t pci_write_vpd(struct pci_dev *dev, loff_t pos, size_t count, const void *buf)
  83. {
  84. if (!dev->vpd || !dev->vpd->ops)
  85. return -ENODEV;
  86. return dev->vpd->ops->write(dev, pos, count, buf);
  87. }
  88. EXPORT_SYMBOL(pci_write_vpd);
  89. /*
  90. * The following routines are to prevent the user from accessing PCI config
  91. * space when it's unsafe to do so. Some devices require this during BIST and
  92. * we're required to prevent it during D-state transitions.
  93. *
  94. * We have a bit per device to indicate it's blocked and a global wait queue
  95. * for callers to sleep on until devices are unblocked.
  96. */
  97. static DECLARE_WAIT_QUEUE_HEAD(pci_ucfg_wait);
  98. static noinline void pci_wait_ucfg(struct pci_dev *dev)
  99. {
  100. DECLARE_WAITQUEUE(wait, current);
  101. __add_wait_queue(&pci_ucfg_wait, &wait);
  102. do {
  103. set_current_state(TASK_UNINTERRUPTIBLE);
  104. spin_unlock_irq(&pci_lock);
  105. schedule();
  106. spin_lock_irq(&pci_lock);
  107. } while (dev->block_ucfg_access);
  108. __remove_wait_queue(&pci_ucfg_wait, &wait);
  109. }
  110. #define PCI_USER_READ_CONFIG(size,type) \
  111. int pci_user_read_config_##size \
  112. (struct pci_dev *dev, int pos, type *val) \
  113. { \
  114. int ret = 0; \
  115. u32 data = -1; \
  116. if (PCI_##size##_BAD) return PCIBIOS_BAD_REGISTER_NUMBER; \
  117. spin_lock_irq(&pci_lock); \
  118. if (unlikely(dev->block_ucfg_access)) pci_wait_ucfg(dev); \
  119. ret = dev->bus->ops->read(dev->bus, dev->devfn, \
  120. pos, sizeof(type), &data); \
  121. spin_unlock_irq(&pci_lock); \
  122. *val = (type)data; \
  123. return ret; \
  124. }
  125. #define PCI_USER_WRITE_CONFIG(size,type) \
  126. int pci_user_write_config_##size \
  127. (struct pci_dev *dev, int pos, type val) \
  128. { \
  129. int ret = -EIO; \
  130. if (PCI_##size##_BAD) return PCIBIOS_BAD_REGISTER_NUMBER; \
  131. spin_lock_irq(&pci_lock); \
  132. if (unlikely(dev->block_ucfg_access)) pci_wait_ucfg(dev); \
  133. ret = dev->bus->ops->write(dev->bus, dev->devfn, \
  134. pos, sizeof(type), val); \
  135. spin_unlock_irq(&pci_lock); \
  136. return ret; \
  137. }
  138. PCI_USER_READ_CONFIG(byte, u8)
  139. PCI_USER_READ_CONFIG(word, u16)
  140. PCI_USER_READ_CONFIG(dword, u32)
  141. PCI_USER_WRITE_CONFIG(byte, u8)
  142. PCI_USER_WRITE_CONFIG(word, u16)
  143. PCI_USER_WRITE_CONFIG(dword, u32)
  144. /* VPD access through PCI 2.2+ VPD capability */
  145. #define PCI_VPD_PCI22_SIZE (PCI_VPD_ADDR_MASK + 1)
  146. struct pci_vpd_pci22 {
  147. struct pci_vpd base;
  148. struct mutex lock;
  149. u16 flag;
  150. bool busy;
  151. u8 cap;
  152. };
  153. /*
  154. * Wait for last operation to complete.
  155. * This code has to spin since there is no other notification from the PCI
  156. * hardware. Since the VPD is often implemented by serial attachment to an
  157. * EEPROM, it may take many milliseconds to complete.
  158. */
  159. static int pci_vpd_pci22_wait(struct pci_dev *dev)
  160. {
  161. struct pci_vpd_pci22 *vpd =
  162. container_of(dev->vpd, struct pci_vpd_pci22, base);
  163. unsigned long timeout = jiffies + HZ/20 + 2;
  164. u16 status;
  165. int ret;
  166. if (!vpd->busy)
  167. return 0;
  168. for (;;) {
  169. ret = pci_user_read_config_word(dev, vpd->cap + PCI_VPD_ADDR,
  170. &status);
  171. if (ret)
  172. return ret;
  173. if ((status & PCI_VPD_ADDR_F) == vpd->flag) {
  174. vpd->busy = false;
  175. return 0;
  176. }
  177. if (time_after(jiffies, timeout))
  178. return -ETIMEDOUT;
  179. if (fatal_signal_pending(current))
  180. return -EINTR;
  181. if (!cond_resched())
  182. udelay(10);
  183. }
  184. }
  185. static ssize_t pci_vpd_pci22_read(struct pci_dev *dev, loff_t pos, size_t count,
  186. void *arg)
  187. {
  188. struct pci_vpd_pci22 *vpd =
  189. container_of(dev->vpd, struct pci_vpd_pci22, base);
  190. int ret;
  191. loff_t end = pos + count;
  192. u8 *buf = arg;
  193. if (pos < 0 || pos > vpd->base.len || end > vpd->base.len)
  194. return -EINVAL;
  195. if (mutex_lock_killable(&vpd->lock))
  196. return -EINTR;
  197. ret = pci_vpd_pci22_wait(dev);
  198. if (ret < 0)
  199. goto out;
  200. while (pos < end) {
  201. u32 val;
  202. unsigned int i, skip;
  203. ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR,
  204. pos & ~3);
  205. if (ret < 0)
  206. break;
  207. vpd->busy = true;
  208. vpd->flag = PCI_VPD_ADDR_F;
  209. ret = pci_vpd_pci22_wait(dev);
  210. if (ret < 0)
  211. break;
  212. ret = pci_user_read_config_dword(dev, vpd->cap + PCI_VPD_DATA, &val);
  213. if (ret < 0)
  214. break;
  215. skip = pos & 3;
  216. for (i = 0; i < sizeof(u32); i++) {
  217. if (i >= skip) {
  218. *buf++ = val;
  219. if (++pos == end)
  220. break;
  221. }
  222. val >>= 8;
  223. }
  224. }
  225. out:
  226. mutex_unlock(&vpd->lock);
  227. return ret ? ret : count;
  228. }
  229. static ssize_t pci_vpd_pci22_write(struct pci_dev *dev, loff_t pos, size_t count,
  230. const void *arg)
  231. {
  232. struct pci_vpd_pci22 *vpd =
  233. container_of(dev->vpd, struct pci_vpd_pci22, base);
  234. const u8 *buf = arg;
  235. loff_t end = pos + count;
  236. int ret = 0;
  237. if (pos < 0 || (pos & 3) || (count & 3) || end > vpd->base.len)
  238. return -EINVAL;
  239. if (mutex_lock_killable(&vpd->lock))
  240. return -EINTR;
  241. ret = pci_vpd_pci22_wait(dev);
  242. if (ret < 0)
  243. goto out;
  244. while (pos < end) {
  245. u32 val;
  246. val = *buf++;
  247. val |= *buf++ << 8;
  248. val |= *buf++ << 16;
  249. val |= *buf++ << 24;
  250. ret = pci_user_write_config_dword(dev, vpd->cap + PCI_VPD_DATA, val);
  251. if (ret < 0)
  252. break;
  253. ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR,
  254. pos | PCI_VPD_ADDR_F);
  255. if (ret < 0)
  256. break;
  257. vpd->busy = true;
  258. vpd->flag = 0;
  259. ret = pci_vpd_pci22_wait(dev);
  260. pos += sizeof(u32);
  261. }
  262. out:
  263. mutex_unlock(&vpd->lock);
  264. return ret ? ret : count;
  265. }
  266. static void pci_vpd_pci22_release(struct pci_dev *dev)
  267. {
  268. kfree(container_of(dev->vpd, struct pci_vpd_pci22, base));
  269. }
  270. static const struct pci_vpd_ops pci_vpd_pci22_ops = {
  271. .read = pci_vpd_pci22_read,
  272. .write = pci_vpd_pci22_write,
  273. .release = pci_vpd_pci22_release,
  274. };
  275. int pci_vpd_pci22_init(struct pci_dev *dev)
  276. {
  277. struct pci_vpd_pci22 *vpd;
  278. u8 cap;
  279. cap = pci_find_capability(dev, PCI_CAP_ID_VPD);
  280. if (!cap)
  281. return -ENODEV;
  282. vpd = kzalloc(sizeof(*vpd), GFP_ATOMIC);
  283. if (!vpd)
  284. return -ENOMEM;
  285. vpd->base.len = PCI_VPD_PCI22_SIZE;
  286. vpd->base.ops = &pci_vpd_pci22_ops;
  287. mutex_init(&vpd->lock);
  288. vpd->cap = cap;
  289. vpd->busy = false;
  290. dev->vpd = &vpd->base;
  291. return 0;
  292. }
  293. /**
  294. * pci_vpd_truncate - Set available Vital Product Data size
  295. * @dev: pci device struct
  296. * @size: available memory in bytes
  297. *
  298. * Adjust size of available VPD area.
  299. */
  300. int pci_vpd_truncate(struct pci_dev *dev, size_t size)
  301. {
  302. if (!dev->vpd)
  303. return -EINVAL;
  304. /* limited by the access method */
  305. if (size > dev->vpd->len)
  306. return -EINVAL;
  307. dev->vpd->len = size;
  308. dev->vpd->attr->size = size;
  309. return 0;
  310. }
  311. EXPORT_SYMBOL(pci_vpd_truncate);
  312. /**
  313. * pci_block_user_cfg_access - Block userspace PCI config reads/writes
  314. * @dev: pci device struct
  315. *
  316. * When user access is blocked, any reads or writes to config space will
  317. * sleep until access is unblocked again. We don't allow nesting of
  318. * block/unblock calls.
  319. */
  320. void pci_block_user_cfg_access(struct pci_dev *dev)
  321. {
  322. unsigned long flags;
  323. int was_blocked;
  324. spin_lock_irqsave(&pci_lock, flags);
  325. was_blocked = dev->block_ucfg_access;
  326. dev->block_ucfg_access = 1;
  327. spin_unlock_irqrestore(&pci_lock, flags);
  328. /* If we BUG() inside the pci_lock, we're guaranteed to hose
  329. * the machine */
  330. BUG_ON(was_blocked);
  331. }
  332. EXPORT_SYMBOL_GPL(pci_block_user_cfg_access);
  333. /**
  334. * pci_unblock_user_cfg_access - Unblock userspace PCI config reads/writes
  335. * @dev: pci device struct
  336. *
  337. * This function allows userspace PCI config accesses to resume.
  338. */
  339. void pci_unblock_user_cfg_access(struct pci_dev *dev)
  340. {
  341. unsigned long flags;
  342. spin_lock_irqsave(&pci_lock, flags);
  343. /* This indicates a problem in the caller, but we don't need
  344. * to kill them, unlike a double-block above. */
  345. WARN_ON(!dev->block_ucfg_access);
  346. dev->block_ucfg_access = 0;
  347. wake_up_all(&pci_ucfg_wait);
  348. spin_unlock_irqrestore(&pci_lock, flags);
  349. }
  350. EXPORT_SYMBOL_GPL(pci_unblock_user_cfg_access);