access.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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_RAW_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. raw_spin_lock_irqsave(&pci_lock, flags); \
  31. res = bus->ops->read(bus, devfn, pos, len, &data); \
  32. *value = (type)data; \
  33. raw_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. raw_spin_lock_irqsave(&pci_lock, flags); \
  44. res = bus->ops->write(bus, devfn, pos, len, value); \
  45. raw_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. raw_spin_lock_irqsave(&pci_lock, flags);
  72. old_ops = bus->ops;
  73. bus->ops = ops;
  74. raw_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. raw_spin_unlock_irq(&pci_lock);
  124. schedule();
  125. raw_spin_lock_irq(&pci_lock);
  126. } while (dev->block_ucfg_access);
  127. __remove_wait_queue(&pci_ucfg_wait, &wait);
  128. }
  129. /* Returns 0 on success, negative values indicate error. */
  130. #define PCI_USER_READ_CONFIG(size,type) \
  131. int pci_user_read_config_##size \
  132. (struct pci_dev *dev, int pos, type *val) \
  133. { \
  134. int ret = 0; \
  135. u32 data = -1; \
  136. if (PCI_##size##_BAD) \
  137. return -EINVAL; \
  138. raw_spin_lock_irq(&pci_lock); \
  139. if (unlikely(dev->block_ucfg_access)) pci_wait_ucfg(dev); \
  140. ret = dev->bus->ops->read(dev->bus, dev->devfn, \
  141. pos, sizeof(type), &data); \
  142. raw_spin_unlock_irq(&pci_lock); \
  143. *val = (type)data; \
  144. if (ret > 0) \
  145. ret = -EINVAL; \
  146. return ret; \
  147. }
  148. /* Returns 0 on success, negative values indicate error. */
  149. #define PCI_USER_WRITE_CONFIG(size,type) \
  150. int pci_user_write_config_##size \
  151. (struct pci_dev *dev, int pos, type val) \
  152. { \
  153. int ret = -EIO; \
  154. if (PCI_##size##_BAD) \
  155. return -EINVAL; \
  156. raw_spin_lock_irq(&pci_lock); \
  157. if (unlikely(dev->block_ucfg_access)) pci_wait_ucfg(dev); \
  158. ret = dev->bus->ops->write(dev->bus, dev->devfn, \
  159. pos, sizeof(type), val); \
  160. raw_spin_unlock_irq(&pci_lock); \
  161. if (ret > 0) \
  162. ret = -EINVAL; \
  163. return ret; \
  164. }
  165. PCI_USER_READ_CONFIG(byte, u8)
  166. PCI_USER_READ_CONFIG(word, u16)
  167. PCI_USER_READ_CONFIG(dword, u32)
  168. PCI_USER_WRITE_CONFIG(byte, u8)
  169. PCI_USER_WRITE_CONFIG(word, u16)
  170. PCI_USER_WRITE_CONFIG(dword, u32)
  171. /* VPD access through PCI 2.2+ VPD capability */
  172. #define PCI_VPD_PCI22_SIZE (PCI_VPD_ADDR_MASK + 1)
  173. struct pci_vpd_pci22 {
  174. struct pci_vpd base;
  175. struct mutex lock;
  176. u16 flag;
  177. bool busy;
  178. u8 cap;
  179. };
  180. /*
  181. * Wait for last operation to complete.
  182. * This code has to spin since there is no other notification from the PCI
  183. * hardware. Since the VPD is often implemented by serial attachment to an
  184. * EEPROM, it may take many milliseconds to complete.
  185. *
  186. * Returns 0 on success, negative values indicate error.
  187. */
  188. static int pci_vpd_pci22_wait(struct pci_dev *dev)
  189. {
  190. struct pci_vpd_pci22 *vpd =
  191. container_of(dev->vpd, struct pci_vpd_pci22, base);
  192. unsigned long timeout = jiffies + HZ/20 + 2;
  193. u16 status;
  194. int ret;
  195. if (!vpd->busy)
  196. return 0;
  197. for (;;) {
  198. ret = pci_user_read_config_word(dev, vpd->cap + PCI_VPD_ADDR,
  199. &status);
  200. if (ret < 0)
  201. return ret;
  202. if ((status & PCI_VPD_ADDR_F) == vpd->flag) {
  203. vpd->busy = false;
  204. return 0;
  205. }
  206. if (time_after(jiffies, timeout)) {
  207. dev_printk(KERN_DEBUG, &dev->dev,
  208. "vpd r/w failed. This is likely a firmware "
  209. "bug on this device. Contact the card "
  210. "vendor for a firmware update.");
  211. return -ETIMEDOUT;
  212. }
  213. if (fatal_signal_pending(current))
  214. return -EINTR;
  215. if (!cond_resched())
  216. udelay(10);
  217. }
  218. }
  219. static ssize_t pci_vpd_pci22_read(struct pci_dev *dev, loff_t pos, size_t count,
  220. void *arg)
  221. {
  222. struct pci_vpd_pci22 *vpd =
  223. container_of(dev->vpd, struct pci_vpd_pci22, base);
  224. int ret;
  225. loff_t end = pos + count;
  226. u8 *buf = arg;
  227. if (pos < 0 || pos > vpd->base.len || end > vpd->base.len)
  228. return -EINVAL;
  229. if (mutex_lock_killable(&vpd->lock))
  230. return -EINTR;
  231. ret = pci_vpd_pci22_wait(dev);
  232. if (ret < 0)
  233. goto out;
  234. while (pos < end) {
  235. u32 val;
  236. unsigned int i, skip;
  237. ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR,
  238. pos & ~3);
  239. if (ret < 0)
  240. break;
  241. vpd->busy = true;
  242. vpd->flag = PCI_VPD_ADDR_F;
  243. ret = pci_vpd_pci22_wait(dev);
  244. if (ret < 0)
  245. break;
  246. ret = pci_user_read_config_dword(dev, vpd->cap + PCI_VPD_DATA, &val);
  247. if (ret < 0)
  248. break;
  249. skip = pos & 3;
  250. for (i = 0; i < sizeof(u32); i++) {
  251. if (i >= skip) {
  252. *buf++ = val;
  253. if (++pos == end)
  254. break;
  255. }
  256. val >>= 8;
  257. }
  258. }
  259. out:
  260. mutex_unlock(&vpd->lock);
  261. return ret ? ret : count;
  262. }
  263. static ssize_t pci_vpd_pci22_write(struct pci_dev *dev, loff_t pos, size_t count,
  264. const void *arg)
  265. {
  266. struct pci_vpd_pci22 *vpd =
  267. container_of(dev->vpd, struct pci_vpd_pci22, base);
  268. const u8 *buf = arg;
  269. loff_t end = pos + count;
  270. int ret = 0;
  271. if (pos < 0 || (pos & 3) || (count & 3) || end > vpd->base.len)
  272. return -EINVAL;
  273. if (mutex_lock_killable(&vpd->lock))
  274. return -EINTR;
  275. ret = pci_vpd_pci22_wait(dev);
  276. if (ret < 0)
  277. goto out;
  278. while (pos < end) {
  279. u32 val;
  280. val = *buf++;
  281. val |= *buf++ << 8;
  282. val |= *buf++ << 16;
  283. val |= *buf++ << 24;
  284. ret = pci_user_write_config_dword(dev, vpd->cap + PCI_VPD_DATA, val);
  285. if (ret < 0)
  286. break;
  287. ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR,
  288. pos | PCI_VPD_ADDR_F);
  289. if (ret < 0)
  290. break;
  291. vpd->busy = true;
  292. vpd->flag = 0;
  293. ret = pci_vpd_pci22_wait(dev);
  294. if (ret < 0)
  295. break;
  296. pos += sizeof(u32);
  297. }
  298. out:
  299. mutex_unlock(&vpd->lock);
  300. return ret ? ret : count;
  301. }
  302. static void pci_vpd_pci22_release(struct pci_dev *dev)
  303. {
  304. kfree(container_of(dev->vpd, struct pci_vpd_pci22, base));
  305. }
  306. static const struct pci_vpd_ops pci_vpd_pci22_ops = {
  307. .read = pci_vpd_pci22_read,
  308. .write = pci_vpd_pci22_write,
  309. .release = pci_vpd_pci22_release,
  310. };
  311. int pci_vpd_pci22_init(struct pci_dev *dev)
  312. {
  313. struct pci_vpd_pci22 *vpd;
  314. u8 cap;
  315. cap = pci_find_capability(dev, PCI_CAP_ID_VPD);
  316. if (!cap)
  317. return -ENODEV;
  318. vpd = kzalloc(sizeof(*vpd), GFP_ATOMIC);
  319. if (!vpd)
  320. return -ENOMEM;
  321. vpd->base.len = PCI_VPD_PCI22_SIZE;
  322. vpd->base.ops = &pci_vpd_pci22_ops;
  323. mutex_init(&vpd->lock);
  324. vpd->cap = cap;
  325. vpd->busy = false;
  326. dev->vpd = &vpd->base;
  327. return 0;
  328. }
  329. /**
  330. * pci_vpd_truncate - Set available Vital Product Data size
  331. * @dev: pci device struct
  332. * @size: available memory in bytes
  333. *
  334. * Adjust size of available VPD area.
  335. */
  336. int pci_vpd_truncate(struct pci_dev *dev, size_t size)
  337. {
  338. if (!dev->vpd)
  339. return -EINVAL;
  340. /* limited by the access method */
  341. if (size > dev->vpd->len)
  342. return -EINVAL;
  343. dev->vpd->len = size;
  344. if (dev->vpd->attr)
  345. dev->vpd->attr->size = size;
  346. return 0;
  347. }
  348. EXPORT_SYMBOL(pci_vpd_truncate);
  349. /**
  350. * pci_block_user_cfg_access - Block userspace PCI config reads/writes
  351. * @dev: pci device struct
  352. *
  353. * When user access is blocked, any reads or writes to config space will
  354. * sleep until access is unblocked again. We don't allow nesting of
  355. * block/unblock calls.
  356. */
  357. void pci_block_user_cfg_access(struct pci_dev *dev)
  358. {
  359. unsigned long flags;
  360. int was_blocked;
  361. raw_spin_lock_irqsave(&pci_lock, flags);
  362. was_blocked = dev->block_ucfg_access;
  363. dev->block_ucfg_access = 1;
  364. raw_spin_unlock_irqrestore(&pci_lock, flags);
  365. /* If we BUG() inside the pci_lock, we're guaranteed to hose
  366. * the machine */
  367. BUG_ON(was_blocked);
  368. }
  369. EXPORT_SYMBOL_GPL(pci_block_user_cfg_access);
  370. /**
  371. * pci_unblock_user_cfg_access - Unblock userspace PCI config reads/writes
  372. * @dev: pci device struct
  373. *
  374. * This function allows userspace PCI config accesses to resume.
  375. */
  376. void pci_unblock_user_cfg_access(struct pci_dev *dev)
  377. {
  378. unsigned long flags;
  379. raw_spin_lock_irqsave(&pci_lock, flags);
  380. /* This indicates a problem in the caller, but we don't need
  381. * to kill them, unlike a double-block above. */
  382. WARN_ON(!dev->block_ucfg_access);
  383. dev->block_ucfg_access = 0;
  384. wake_up_all(&pci_ucfg_wait);
  385. raw_spin_unlock_irqrestore(&pci_lock, flags);
  386. }
  387. EXPORT_SYMBOL_GPL(pci_unblock_user_cfg_access);