access.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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. 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_cfg_wait);
  117. static noinline void pci_wait_cfg(struct pci_dev *dev)
  118. {
  119. DECLARE_WAITQUEUE(wait, current);
  120. __add_wait_queue(&pci_cfg_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_cfg_access);
  127. __remove_wait_queue(&pci_cfg_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_cfg_access)) \
  140. pci_wait_cfg(dev); \
  141. ret = dev->bus->ops->read(dev->bus, dev->devfn, \
  142. pos, sizeof(type), &data); \
  143. raw_spin_unlock_irq(&pci_lock); \
  144. *val = (type)data; \
  145. if (ret > 0) \
  146. ret = -EINVAL; \
  147. return ret; \
  148. }
  149. /* Returns 0 on success, negative values indicate error. */
  150. #define PCI_USER_WRITE_CONFIG(size,type) \
  151. int pci_user_write_config_##size \
  152. (struct pci_dev *dev, int pos, type val) \
  153. { \
  154. int ret = -EIO; \
  155. if (PCI_##size##_BAD) \
  156. return -EINVAL; \
  157. raw_spin_lock_irq(&pci_lock); \
  158. if (unlikely(dev->block_cfg_access)) \
  159. pci_wait_cfg(dev); \
  160. ret = dev->bus->ops->write(dev->bus, dev->devfn, \
  161. pos, sizeof(type), val); \
  162. raw_spin_unlock_irq(&pci_lock); \
  163. if (ret > 0) \
  164. ret = -EINVAL; \
  165. return ret; \
  166. }
  167. PCI_USER_READ_CONFIG(byte, u8)
  168. PCI_USER_READ_CONFIG(word, u16)
  169. PCI_USER_READ_CONFIG(dword, u32)
  170. PCI_USER_WRITE_CONFIG(byte, u8)
  171. PCI_USER_WRITE_CONFIG(word, u16)
  172. PCI_USER_WRITE_CONFIG(dword, u32)
  173. /* VPD access through PCI 2.2+ VPD capability */
  174. #define PCI_VPD_PCI22_SIZE (PCI_VPD_ADDR_MASK + 1)
  175. struct pci_vpd_pci22 {
  176. struct pci_vpd base;
  177. struct mutex lock;
  178. u16 flag;
  179. bool busy;
  180. u8 cap;
  181. };
  182. /*
  183. * Wait for last operation to complete.
  184. * This code has to spin since there is no other notification from the PCI
  185. * hardware. Since the VPD is often implemented by serial attachment to an
  186. * EEPROM, it may take many milliseconds to complete.
  187. *
  188. * Returns 0 on success, negative values indicate error.
  189. */
  190. static int pci_vpd_pci22_wait(struct pci_dev *dev)
  191. {
  192. struct pci_vpd_pci22 *vpd =
  193. container_of(dev->vpd, struct pci_vpd_pci22, base);
  194. unsigned long timeout = jiffies + HZ/20 + 2;
  195. u16 status;
  196. int ret;
  197. if (!vpd->busy)
  198. return 0;
  199. for (;;) {
  200. ret = pci_user_read_config_word(dev, vpd->cap + PCI_VPD_ADDR,
  201. &status);
  202. if (ret < 0)
  203. return ret;
  204. if ((status & PCI_VPD_ADDR_F) == vpd->flag) {
  205. vpd->busy = false;
  206. return 0;
  207. }
  208. if (time_after(jiffies, timeout)) {
  209. dev_printk(KERN_DEBUG, &dev->dev,
  210. "vpd r/w failed. This is likely a firmware "
  211. "bug on this device. Contact the card "
  212. "vendor for a firmware update.");
  213. return -ETIMEDOUT;
  214. }
  215. if (fatal_signal_pending(current))
  216. return -EINTR;
  217. if (!cond_resched())
  218. udelay(10);
  219. }
  220. }
  221. static ssize_t pci_vpd_pci22_read(struct pci_dev *dev, loff_t pos, size_t count,
  222. void *arg)
  223. {
  224. struct pci_vpd_pci22 *vpd =
  225. container_of(dev->vpd, struct pci_vpd_pci22, base);
  226. int ret;
  227. loff_t end = pos + count;
  228. u8 *buf = arg;
  229. if (pos < 0 || pos > vpd->base.len || end > vpd->base.len)
  230. return -EINVAL;
  231. if (mutex_lock_killable(&vpd->lock))
  232. return -EINTR;
  233. ret = pci_vpd_pci22_wait(dev);
  234. if (ret < 0)
  235. goto out;
  236. while (pos < end) {
  237. u32 val;
  238. unsigned int i, skip;
  239. ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR,
  240. pos & ~3);
  241. if (ret < 0)
  242. break;
  243. vpd->busy = true;
  244. vpd->flag = PCI_VPD_ADDR_F;
  245. ret = pci_vpd_pci22_wait(dev);
  246. if (ret < 0)
  247. break;
  248. ret = pci_user_read_config_dword(dev, vpd->cap + PCI_VPD_DATA, &val);
  249. if (ret < 0)
  250. break;
  251. skip = pos & 3;
  252. for (i = 0; i < sizeof(u32); i++) {
  253. if (i >= skip) {
  254. *buf++ = val;
  255. if (++pos == end)
  256. break;
  257. }
  258. val >>= 8;
  259. }
  260. }
  261. out:
  262. mutex_unlock(&vpd->lock);
  263. return ret ? ret : count;
  264. }
  265. static ssize_t pci_vpd_pci22_write(struct pci_dev *dev, loff_t pos, size_t count,
  266. const void *arg)
  267. {
  268. struct pci_vpd_pci22 *vpd =
  269. container_of(dev->vpd, struct pci_vpd_pci22, base);
  270. const u8 *buf = arg;
  271. loff_t end = pos + count;
  272. int ret = 0;
  273. if (pos < 0 || (pos & 3) || (count & 3) || end > vpd->base.len)
  274. return -EINVAL;
  275. if (mutex_lock_killable(&vpd->lock))
  276. return -EINTR;
  277. ret = pci_vpd_pci22_wait(dev);
  278. if (ret < 0)
  279. goto out;
  280. while (pos < end) {
  281. u32 val;
  282. val = *buf++;
  283. val |= *buf++ << 8;
  284. val |= *buf++ << 16;
  285. val |= *buf++ << 24;
  286. ret = pci_user_write_config_dword(dev, vpd->cap + PCI_VPD_DATA, val);
  287. if (ret < 0)
  288. break;
  289. ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR,
  290. pos | PCI_VPD_ADDR_F);
  291. if (ret < 0)
  292. break;
  293. vpd->busy = true;
  294. vpd->flag = 0;
  295. ret = pci_vpd_pci22_wait(dev);
  296. if (ret < 0)
  297. break;
  298. pos += sizeof(u32);
  299. }
  300. out:
  301. mutex_unlock(&vpd->lock);
  302. return ret ? ret : count;
  303. }
  304. static void pci_vpd_pci22_release(struct pci_dev *dev)
  305. {
  306. kfree(container_of(dev->vpd, struct pci_vpd_pci22, base));
  307. }
  308. static const struct pci_vpd_ops pci_vpd_pci22_ops = {
  309. .read = pci_vpd_pci22_read,
  310. .write = pci_vpd_pci22_write,
  311. .release = pci_vpd_pci22_release,
  312. };
  313. static ssize_t pci_vpd_f0_read(struct pci_dev *dev, loff_t pos, size_t count,
  314. void *arg)
  315. {
  316. struct pci_dev *tdev = pci_get_slot(dev->bus, PCI_SLOT(dev->devfn));
  317. ssize_t ret;
  318. if (!tdev)
  319. return -ENODEV;
  320. ret = pci_read_vpd(tdev, pos, count, arg);
  321. pci_dev_put(tdev);
  322. return ret;
  323. }
  324. static ssize_t pci_vpd_f0_write(struct pci_dev *dev, loff_t pos, size_t count,
  325. const void *arg)
  326. {
  327. struct pci_dev *tdev = pci_get_slot(dev->bus, PCI_SLOT(dev->devfn));
  328. ssize_t ret;
  329. if (!tdev)
  330. return -ENODEV;
  331. ret = pci_write_vpd(tdev, pos, count, arg);
  332. pci_dev_put(tdev);
  333. return ret;
  334. }
  335. static const struct pci_vpd_ops pci_vpd_f0_ops = {
  336. .read = pci_vpd_f0_read,
  337. .write = pci_vpd_f0_write,
  338. .release = pci_vpd_pci22_release,
  339. };
  340. static int pci_vpd_f0_dev_check(struct pci_dev *dev)
  341. {
  342. struct pci_dev *tdev = pci_get_slot(dev->bus, PCI_SLOT(dev->devfn));
  343. int ret = 0;
  344. if (!tdev)
  345. return -ENODEV;
  346. if (!tdev->vpd || !tdev->multifunction ||
  347. dev->class != tdev->class || dev->vendor != tdev->vendor ||
  348. dev->device != tdev->device)
  349. ret = -ENODEV;
  350. pci_dev_put(tdev);
  351. return ret;
  352. }
  353. int pci_vpd_pci22_init(struct pci_dev *dev)
  354. {
  355. struct pci_vpd_pci22 *vpd;
  356. u8 cap;
  357. cap = pci_find_capability(dev, PCI_CAP_ID_VPD);
  358. if (!cap)
  359. return -ENODEV;
  360. if (dev->dev_flags & PCI_DEV_FLAGS_VPD_REF_F0) {
  361. int ret = pci_vpd_f0_dev_check(dev);
  362. if (ret)
  363. return ret;
  364. }
  365. vpd = kzalloc(sizeof(*vpd), GFP_ATOMIC);
  366. if (!vpd)
  367. return -ENOMEM;
  368. vpd->base.len = PCI_VPD_PCI22_SIZE;
  369. if (dev->dev_flags & PCI_DEV_FLAGS_VPD_REF_F0)
  370. vpd->base.ops = &pci_vpd_f0_ops;
  371. else
  372. vpd->base.ops = &pci_vpd_pci22_ops;
  373. mutex_init(&vpd->lock);
  374. vpd->cap = cap;
  375. vpd->busy = false;
  376. dev->vpd = &vpd->base;
  377. return 0;
  378. }
  379. /**
  380. * pci_vpd_truncate - Set available Vital Product Data size
  381. * @dev: pci device struct
  382. * @size: available memory in bytes
  383. *
  384. * Adjust size of available VPD area.
  385. */
  386. int pci_vpd_truncate(struct pci_dev *dev, size_t size)
  387. {
  388. if (!dev->vpd)
  389. return -EINVAL;
  390. /* limited by the access method */
  391. if (size > dev->vpd->len)
  392. return -EINVAL;
  393. dev->vpd->len = size;
  394. if (dev->vpd->attr)
  395. dev->vpd->attr->size = size;
  396. return 0;
  397. }
  398. EXPORT_SYMBOL(pci_vpd_truncate);
  399. /**
  400. * pci_cfg_access_lock - Lock PCI config reads/writes
  401. * @dev: pci device struct
  402. *
  403. * When access is locked, any userspace reads or writes to config
  404. * space and concurrent lock requests will sleep until access is
  405. * allowed via pci_cfg_access_unlocked again.
  406. */
  407. void pci_cfg_access_lock(struct pci_dev *dev)
  408. {
  409. might_sleep();
  410. raw_spin_lock_irq(&pci_lock);
  411. if (dev->block_cfg_access)
  412. pci_wait_cfg(dev);
  413. dev->block_cfg_access = 1;
  414. raw_spin_unlock_irq(&pci_lock);
  415. }
  416. EXPORT_SYMBOL_GPL(pci_cfg_access_lock);
  417. /**
  418. * pci_cfg_access_trylock - try to lock PCI config reads/writes
  419. * @dev: pci device struct
  420. *
  421. * Same as pci_cfg_access_lock, but will return 0 if access is
  422. * already locked, 1 otherwise. This function can be used from
  423. * atomic contexts.
  424. */
  425. bool pci_cfg_access_trylock(struct pci_dev *dev)
  426. {
  427. unsigned long flags;
  428. bool locked = true;
  429. raw_spin_lock_irqsave(&pci_lock, flags);
  430. if (dev->block_cfg_access)
  431. locked = false;
  432. else
  433. dev->block_cfg_access = 1;
  434. raw_spin_unlock_irqrestore(&pci_lock, flags);
  435. return locked;
  436. }
  437. EXPORT_SYMBOL_GPL(pci_cfg_access_trylock);
  438. /**
  439. * pci_cfg_access_unlock - Unlock PCI config reads/writes
  440. * @dev: pci device struct
  441. *
  442. * This function allows PCI config accesses to resume.
  443. */
  444. void pci_cfg_access_unlock(struct pci_dev *dev)
  445. {
  446. unsigned long flags;
  447. raw_spin_lock_irqsave(&pci_lock, flags);
  448. /* This indicates a problem in the caller, but we don't need
  449. * to kill them, unlike a double-block above. */
  450. WARN_ON(!dev->block_cfg_access);
  451. dev->block_cfg_access = 0;
  452. wake_up_all(&pci_cfg_wait);
  453. raw_spin_unlock_irqrestore(&pci_lock, flags);
  454. }
  455. EXPORT_SYMBOL_GPL(pci_cfg_access_unlock);