access.c 10 KB

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