access.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  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. int pci_generic_config_read(struct pci_bus *bus, unsigned int devfn,
  61. int where, int size, u32 *val)
  62. {
  63. void __iomem *addr;
  64. addr = bus->ops->map_bus(bus, devfn, where);
  65. if (!addr) {
  66. *val = ~0;
  67. return PCIBIOS_DEVICE_NOT_FOUND;
  68. }
  69. if (size == 1)
  70. *val = readb(addr);
  71. else if (size == 2)
  72. *val = readw(addr);
  73. else
  74. *val = readl(addr);
  75. return PCIBIOS_SUCCESSFUL;
  76. }
  77. EXPORT_SYMBOL_GPL(pci_generic_config_read);
  78. int pci_generic_config_write(struct pci_bus *bus, unsigned int devfn,
  79. int where, int size, u32 val)
  80. {
  81. void __iomem *addr;
  82. addr = bus->ops->map_bus(bus, devfn, where);
  83. if (!addr)
  84. return PCIBIOS_DEVICE_NOT_FOUND;
  85. if (size == 1)
  86. writeb(val, addr);
  87. else if (size == 2)
  88. writew(val, addr);
  89. else
  90. writel(val, addr);
  91. return PCIBIOS_SUCCESSFUL;
  92. }
  93. EXPORT_SYMBOL_GPL(pci_generic_config_write);
  94. int pci_generic_config_read32(struct pci_bus *bus, unsigned int devfn,
  95. int where, int size, u32 *val)
  96. {
  97. void __iomem *addr;
  98. addr = bus->ops->map_bus(bus, devfn, where & ~0x3);
  99. if (!addr) {
  100. *val = ~0;
  101. return PCIBIOS_DEVICE_NOT_FOUND;
  102. }
  103. *val = readl(addr);
  104. if (size <= 2)
  105. *val = (*val >> (8 * (where & 3))) & ((1 << (size * 8)) - 1);
  106. return PCIBIOS_SUCCESSFUL;
  107. }
  108. EXPORT_SYMBOL_GPL(pci_generic_config_read32);
  109. int pci_generic_config_write32(struct pci_bus *bus, unsigned int devfn,
  110. int where, int size, u32 val)
  111. {
  112. void __iomem *addr;
  113. u32 mask, tmp;
  114. addr = bus->ops->map_bus(bus, devfn, where & ~0x3);
  115. if (!addr)
  116. return PCIBIOS_DEVICE_NOT_FOUND;
  117. if (size == 4) {
  118. writel(val, addr);
  119. return PCIBIOS_SUCCESSFUL;
  120. } else {
  121. mask = ~(((1 << (size * 8)) - 1) << ((where & 0x3) * 8));
  122. }
  123. tmp = readl(addr) & mask;
  124. tmp |= val << ((where & 0x3) * 8);
  125. writel(tmp, addr);
  126. return PCIBIOS_SUCCESSFUL;
  127. }
  128. EXPORT_SYMBOL_GPL(pci_generic_config_write32);
  129. /**
  130. * pci_bus_set_ops - Set raw operations of pci bus
  131. * @bus: pci bus struct
  132. * @ops: new raw operations
  133. *
  134. * Return previous raw operations
  135. */
  136. struct pci_ops *pci_bus_set_ops(struct pci_bus *bus, struct pci_ops *ops)
  137. {
  138. struct pci_ops *old_ops;
  139. unsigned long flags;
  140. raw_spin_lock_irqsave(&pci_lock, flags);
  141. old_ops = bus->ops;
  142. bus->ops = ops;
  143. raw_spin_unlock_irqrestore(&pci_lock, flags);
  144. return old_ops;
  145. }
  146. EXPORT_SYMBOL(pci_bus_set_ops);
  147. /*
  148. * The following routines are to prevent the user from accessing PCI config
  149. * space when it's unsafe to do so. Some devices require this during BIST and
  150. * we're required to prevent it during D-state transitions.
  151. *
  152. * We have a bit per device to indicate it's blocked and a global wait queue
  153. * for callers to sleep on until devices are unblocked.
  154. */
  155. static DECLARE_WAIT_QUEUE_HEAD(pci_cfg_wait);
  156. static noinline void pci_wait_cfg(struct pci_dev *dev)
  157. {
  158. DECLARE_WAITQUEUE(wait, current);
  159. __add_wait_queue(&pci_cfg_wait, &wait);
  160. do {
  161. set_current_state(TASK_UNINTERRUPTIBLE);
  162. raw_spin_unlock_irq(&pci_lock);
  163. schedule();
  164. raw_spin_lock_irq(&pci_lock);
  165. } while (dev->block_cfg_access);
  166. __remove_wait_queue(&pci_cfg_wait, &wait);
  167. }
  168. /* Returns 0 on success, negative values indicate error. */
  169. #define PCI_USER_READ_CONFIG(size, type) \
  170. int pci_user_read_config_##size \
  171. (struct pci_dev *dev, int pos, type *val) \
  172. { \
  173. int ret = PCIBIOS_SUCCESSFUL; \
  174. u32 data = -1; \
  175. if (PCI_##size##_BAD) \
  176. return -EINVAL; \
  177. raw_spin_lock_irq(&pci_lock); \
  178. if (unlikely(dev->block_cfg_access)) \
  179. pci_wait_cfg(dev); \
  180. ret = dev->bus->ops->read(dev->bus, dev->devfn, \
  181. pos, sizeof(type), &data); \
  182. raw_spin_unlock_irq(&pci_lock); \
  183. *val = (type)data; \
  184. return pcibios_err_to_errno(ret); \
  185. } \
  186. EXPORT_SYMBOL_GPL(pci_user_read_config_##size);
  187. /* Returns 0 on success, negative values indicate error. */
  188. #define PCI_USER_WRITE_CONFIG(size, type) \
  189. int pci_user_write_config_##size \
  190. (struct pci_dev *dev, int pos, type val) \
  191. { \
  192. int ret = PCIBIOS_SUCCESSFUL; \
  193. if (PCI_##size##_BAD) \
  194. return -EINVAL; \
  195. raw_spin_lock_irq(&pci_lock); \
  196. if (unlikely(dev->block_cfg_access)) \
  197. pci_wait_cfg(dev); \
  198. ret = dev->bus->ops->write(dev->bus, dev->devfn, \
  199. pos, sizeof(type), val); \
  200. raw_spin_unlock_irq(&pci_lock); \
  201. return pcibios_err_to_errno(ret); \
  202. } \
  203. EXPORT_SYMBOL_GPL(pci_user_write_config_##size);
  204. PCI_USER_READ_CONFIG(byte, u8)
  205. PCI_USER_READ_CONFIG(word, u16)
  206. PCI_USER_READ_CONFIG(dword, u32)
  207. PCI_USER_WRITE_CONFIG(byte, u8)
  208. PCI_USER_WRITE_CONFIG(word, u16)
  209. PCI_USER_WRITE_CONFIG(dword, u32)
  210. /* VPD access through PCI 2.2+ VPD capability */
  211. /**
  212. * pci_read_vpd - Read one entry from Vital Product Data
  213. * @dev: pci device struct
  214. * @pos: offset in vpd space
  215. * @count: number of bytes to read
  216. * @buf: pointer to where to store result
  217. */
  218. ssize_t pci_read_vpd(struct pci_dev *dev, loff_t pos, size_t count, void *buf)
  219. {
  220. if (!dev->vpd || !dev->vpd->ops)
  221. return -ENODEV;
  222. return dev->vpd->ops->read(dev, pos, count, buf);
  223. }
  224. EXPORT_SYMBOL(pci_read_vpd);
  225. /**
  226. * pci_write_vpd - Write entry to Vital Product Data
  227. * @dev: pci device struct
  228. * @pos: offset in vpd space
  229. * @count: number of bytes to write
  230. * @buf: buffer containing write data
  231. */
  232. ssize_t pci_write_vpd(struct pci_dev *dev, loff_t pos, size_t count, const void *buf)
  233. {
  234. if (!dev->vpd || !dev->vpd->ops)
  235. return -ENODEV;
  236. return dev->vpd->ops->write(dev, pos, count, buf);
  237. }
  238. EXPORT_SYMBOL(pci_write_vpd);
  239. /**
  240. * pci_set_vpd_size - Set size of Vital Product Data space
  241. * @dev: pci device struct
  242. * @len: size of vpd space
  243. */
  244. int pci_set_vpd_size(struct pci_dev *dev, size_t len)
  245. {
  246. if (!dev->vpd || !dev->vpd->ops)
  247. return -ENODEV;
  248. return dev->vpd->ops->set_size(dev, len);
  249. }
  250. EXPORT_SYMBOL(pci_set_vpd_size);
  251. #define PCI_VPD_MAX_SIZE (PCI_VPD_ADDR_MASK + 1)
  252. /**
  253. * pci_vpd_size - determine actual size of Vital Product Data
  254. * @dev: pci device struct
  255. * @old_size: current assumed size, also maximum allowed size
  256. */
  257. static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size)
  258. {
  259. size_t off = 0;
  260. unsigned char header[1+2]; /* 1 byte tag, 2 bytes length */
  261. while (off < old_size &&
  262. pci_read_vpd(dev, off, 1, header) == 1) {
  263. unsigned char tag;
  264. if (header[0] & PCI_VPD_LRDT) {
  265. /* Large Resource Data Type Tag */
  266. tag = pci_vpd_lrdt_tag(header);
  267. /* Only read length from known tag items */
  268. if ((tag == PCI_VPD_LTIN_ID_STRING) ||
  269. (tag == PCI_VPD_LTIN_RO_DATA) ||
  270. (tag == PCI_VPD_LTIN_RW_DATA)) {
  271. if (pci_read_vpd(dev, off+1, 2,
  272. &header[1]) != 2) {
  273. dev_warn(&dev->dev,
  274. "invalid large VPD tag %02x size at offset %zu",
  275. tag, off + 1);
  276. return 0;
  277. }
  278. off += PCI_VPD_LRDT_TAG_SIZE +
  279. pci_vpd_lrdt_size(header);
  280. }
  281. } else {
  282. /* Short Resource Data Type Tag */
  283. off += PCI_VPD_SRDT_TAG_SIZE +
  284. pci_vpd_srdt_size(header);
  285. tag = pci_vpd_srdt_tag(header);
  286. }
  287. if (tag == PCI_VPD_STIN_END) /* End tag descriptor */
  288. return off;
  289. if ((tag != PCI_VPD_LTIN_ID_STRING) &&
  290. (tag != PCI_VPD_LTIN_RO_DATA) &&
  291. (tag != PCI_VPD_LTIN_RW_DATA)) {
  292. dev_warn(&dev->dev,
  293. "invalid %s VPD tag %02x at offset %zu",
  294. (header[0] & PCI_VPD_LRDT) ? "large" : "short",
  295. tag, off);
  296. return 0;
  297. }
  298. }
  299. return 0;
  300. }
  301. /*
  302. * Wait for last operation to complete.
  303. * This code has to spin since there is no other notification from the PCI
  304. * hardware. Since the VPD is often implemented by serial attachment to an
  305. * EEPROM, it may take many milliseconds to complete.
  306. *
  307. * Returns 0 on success, negative values indicate error.
  308. */
  309. static int pci_vpd_wait(struct pci_dev *dev)
  310. {
  311. struct pci_vpd *vpd = dev->vpd;
  312. unsigned long timeout = jiffies + msecs_to_jiffies(50);
  313. unsigned long max_sleep = 16;
  314. u16 status;
  315. int ret;
  316. if (!vpd->busy)
  317. return 0;
  318. while (time_before(jiffies, timeout)) {
  319. ret = pci_user_read_config_word(dev, vpd->cap + PCI_VPD_ADDR,
  320. &status);
  321. if (ret < 0)
  322. return ret;
  323. if ((status & PCI_VPD_ADDR_F) == vpd->flag) {
  324. vpd->busy = 0;
  325. return 0;
  326. }
  327. if (fatal_signal_pending(current))
  328. return -EINTR;
  329. usleep_range(10, max_sleep);
  330. if (max_sleep < 1024)
  331. max_sleep *= 2;
  332. }
  333. dev_warn(&dev->dev, "VPD access failed. This is likely a firmware bug on this device. Contact the card vendor for a firmware update\n");
  334. return -ETIMEDOUT;
  335. }
  336. static ssize_t pci_vpd_read(struct pci_dev *dev, loff_t pos, size_t count,
  337. void *arg)
  338. {
  339. struct pci_vpd *vpd = dev->vpd;
  340. int ret;
  341. loff_t end = pos + count;
  342. u8 *buf = arg;
  343. if (pos < 0)
  344. return -EINVAL;
  345. if (!vpd->valid) {
  346. vpd->valid = 1;
  347. vpd->len = pci_vpd_size(dev, vpd->len);
  348. }
  349. if (vpd->len == 0)
  350. return -EIO;
  351. if (pos > vpd->len)
  352. return 0;
  353. if (end > vpd->len) {
  354. end = vpd->len;
  355. count = end - pos;
  356. }
  357. if (mutex_lock_killable(&vpd->lock))
  358. return -EINTR;
  359. ret = pci_vpd_wait(dev);
  360. if (ret < 0)
  361. goto out;
  362. while (pos < end) {
  363. u32 val;
  364. unsigned int i, skip;
  365. ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR,
  366. pos & ~3);
  367. if (ret < 0)
  368. break;
  369. vpd->busy = 1;
  370. vpd->flag = PCI_VPD_ADDR_F;
  371. ret = pci_vpd_wait(dev);
  372. if (ret < 0)
  373. break;
  374. ret = pci_user_read_config_dword(dev, vpd->cap + PCI_VPD_DATA, &val);
  375. if (ret < 0)
  376. break;
  377. skip = pos & 3;
  378. for (i = 0; i < sizeof(u32); i++) {
  379. if (i >= skip) {
  380. *buf++ = val;
  381. if (++pos == end)
  382. break;
  383. }
  384. val >>= 8;
  385. }
  386. }
  387. out:
  388. mutex_unlock(&vpd->lock);
  389. return ret ? ret : count;
  390. }
  391. static ssize_t pci_vpd_write(struct pci_dev *dev, loff_t pos, size_t count,
  392. const void *arg)
  393. {
  394. struct pci_vpd *vpd = dev->vpd;
  395. const u8 *buf = arg;
  396. loff_t end = pos + count;
  397. int ret = 0;
  398. if (pos < 0 || (pos & 3) || (count & 3))
  399. return -EINVAL;
  400. if (!vpd->valid) {
  401. vpd->valid = 1;
  402. vpd->len = pci_vpd_size(dev, vpd->len);
  403. }
  404. if (vpd->len == 0)
  405. return -EIO;
  406. if (end > vpd->len)
  407. return -EINVAL;
  408. if (mutex_lock_killable(&vpd->lock))
  409. return -EINTR;
  410. ret = pci_vpd_wait(dev);
  411. if (ret < 0)
  412. goto out;
  413. while (pos < end) {
  414. u32 val;
  415. val = *buf++;
  416. val |= *buf++ << 8;
  417. val |= *buf++ << 16;
  418. val |= *buf++ << 24;
  419. ret = pci_user_write_config_dword(dev, vpd->cap + PCI_VPD_DATA, val);
  420. if (ret < 0)
  421. break;
  422. ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR,
  423. pos | PCI_VPD_ADDR_F);
  424. if (ret < 0)
  425. break;
  426. vpd->busy = 1;
  427. vpd->flag = 0;
  428. ret = pci_vpd_wait(dev);
  429. if (ret < 0)
  430. break;
  431. pos += sizeof(u32);
  432. }
  433. out:
  434. mutex_unlock(&vpd->lock);
  435. return ret ? ret : count;
  436. }
  437. static int pci_vpd_set_size(struct pci_dev *dev, size_t len)
  438. {
  439. struct pci_vpd *vpd = dev->vpd;
  440. if (len == 0 || len > PCI_VPD_MAX_SIZE)
  441. return -EIO;
  442. vpd->valid = 1;
  443. vpd->len = len;
  444. return 0;
  445. }
  446. static const struct pci_vpd_ops pci_vpd_ops = {
  447. .read = pci_vpd_read,
  448. .write = pci_vpd_write,
  449. .set_size = pci_vpd_set_size,
  450. };
  451. static ssize_t pci_vpd_f0_read(struct pci_dev *dev, loff_t pos, size_t count,
  452. void *arg)
  453. {
  454. struct pci_dev *tdev = pci_get_slot(dev->bus,
  455. PCI_DEVFN(PCI_SLOT(dev->devfn), 0));
  456. ssize_t ret;
  457. if (!tdev)
  458. return -ENODEV;
  459. ret = pci_read_vpd(tdev, pos, count, arg);
  460. pci_dev_put(tdev);
  461. return ret;
  462. }
  463. static ssize_t pci_vpd_f0_write(struct pci_dev *dev, loff_t pos, size_t count,
  464. const void *arg)
  465. {
  466. struct pci_dev *tdev = pci_get_slot(dev->bus,
  467. PCI_DEVFN(PCI_SLOT(dev->devfn), 0));
  468. ssize_t ret;
  469. if (!tdev)
  470. return -ENODEV;
  471. ret = pci_write_vpd(tdev, pos, count, arg);
  472. pci_dev_put(tdev);
  473. return ret;
  474. }
  475. static int pci_vpd_f0_set_size(struct pci_dev *dev, size_t len)
  476. {
  477. struct pci_dev *tdev = pci_get_slot(dev->bus,
  478. PCI_DEVFN(PCI_SLOT(dev->devfn), 0));
  479. int ret;
  480. if (!tdev)
  481. return -ENODEV;
  482. ret = pci_set_vpd_size(tdev, len);
  483. pci_dev_put(tdev);
  484. return ret;
  485. }
  486. static const struct pci_vpd_ops pci_vpd_f0_ops = {
  487. .read = pci_vpd_f0_read,
  488. .write = pci_vpd_f0_write,
  489. .set_size = pci_vpd_f0_set_size,
  490. };
  491. int pci_vpd_init(struct pci_dev *dev)
  492. {
  493. struct pci_vpd *vpd;
  494. u8 cap;
  495. cap = pci_find_capability(dev, PCI_CAP_ID_VPD);
  496. if (!cap)
  497. return -ENODEV;
  498. vpd = kzalloc(sizeof(*vpd), GFP_ATOMIC);
  499. if (!vpd)
  500. return -ENOMEM;
  501. vpd->len = PCI_VPD_MAX_SIZE;
  502. if (dev->dev_flags & PCI_DEV_FLAGS_VPD_REF_F0)
  503. vpd->ops = &pci_vpd_f0_ops;
  504. else
  505. vpd->ops = &pci_vpd_ops;
  506. mutex_init(&vpd->lock);
  507. vpd->cap = cap;
  508. vpd->busy = 0;
  509. vpd->valid = 0;
  510. dev->vpd = vpd;
  511. return 0;
  512. }
  513. void pci_vpd_release(struct pci_dev *dev)
  514. {
  515. kfree(dev->vpd);
  516. }
  517. /**
  518. * pci_cfg_access_lock - Lock PCI config reads/writes
  519. * @dev: pci device struct
  520. *
  521. * When access is locked, any userspace reads or writes to config
  522. * space and concurrent lock requests will sleep until access is
  523. * allowed via pci_cfg_access_unlocked again.
  524. */
  525. void pci_cfg_access_lock(struct pci_dev *dev)
  526. {
  527. might_sleep();
  528. raw_spin_lock_irq(&pci_lock);
  529. if (dev->block_cfg_access)
  530. pci_wait_cfg(dev);
  531. dev->block_cfg_access = 1;
  532. raw_spin_unlock_irq(&pci_lock);
  533. }
  534. EXPORT_SYMBOL_GPL(pci_cfg_access_lock);
  535. /**
  536. * pci_cfg_access_trylock - try to lock PCI config reads/writes
  537. * @dev: pci device struct
  538. *
  539. * Same as pci_cfg_access_lock, but will return 0 if access is
  540. * already locked, 1 otherwise. This function can be used from
  541. * atomic contexts.
  542. */
  543. bool pci_cfg_access_trylock(struct pci_dev *dev)
  544. {
  545. unsigned long flags;
  546. bool locked = true;
  547. raw_spin_lock_irqsave(&pci_lock, flags);
  548. if (dev->block_cfg_access)
  549. locked = false;
  550. else
  551. dev->block_cfg_access = 1;
  552. raw_spin_unlock_irqrestore(&pci_lock, flags);
  553. return locked;
  554. }
  555. EXPORT_SYMBOL_GPL(pci_cfg_access_trylock);
  556. /**
  557. * pci_cfg_access_unlock - Unlock PCI config reads/writes
  558. * @dev: pci device struct
  559. *
  560. * This function allows PCI config accesses to resume.
  561. */
  562. void pci_cfg_access_unlock(struct pci_dev *dev)
  563. {
  564. unsigned long flags;
  565. raw_spin_lock_irqsave(&pci_lock, flags);
  566. /* This indicates a problem in the caller, but we don't need
  567. * to kill them, unlike a double-block above. */
  568. WARN_ON(!dev->block_cfg_access);
  569. dev->block_cfg_access = 0;
  570. raw_spin_unlock_irqrestore(&pci_lock, flags);
  571. wake_up_all(&pci_cfg_wait);
  572. }
  573. EXPORT_SYMBOL_GPL(pci_cfg_access_unlock);
  574. static inline int pcie_cap_version(const struct pci_dev *dev)
  575. {
  576. return pcie_caps_reg(dev) & PCI_EXP_FLAGS_VERS;
  577. }
  578. static bool pcie_downstream_port(const struct pci_dev *dev)
  579. {
  580. int type = pci_pcie_type(dev);
  581. return type == PCI_EXP_TYPE_ROOT_PORT ||
  582. type == PCI_EXP_TYPE_DOWNSTREAM;
  583. }
  584. bool pcie_cap_has_lnkctl(const struct pci_dev *dev)
  585. {
  586. int type = pci_pcie_type(dev);
  587. return type == PCI_EXP_TYPE_ENDPOINT ||
  588. type == PCI_EXP_TYPE_LEG_END ||
  589. type == PCI_EXP_TYPE_ROOT_PORT ||
  590. type == PCI_EXP_TYPE_UPSTREAM ||
  591. type == PCI_EXP_TYPE_DOWNSTREAM ||
  592. type == PCI_EXP_TYPE_PCI_BRIDGE ||
  593. type == PCI_EXP_TYPE_PCIE_BRIDGE;
  594. }
  595. static inline bool pcie_cap_has_sltctl(const struct pci_dev *dev)
  596. {
  597. return pcie_downstream_port(dev) &&
  598. pcie_caps_reg(dev) & PCI_EXP_FLAGS_SLOT;
  599. }
  600. static inline bool pcie_cap_has_rtctl(const struct pci_dev *dev)
  601. {
  602. int type = pci_pcie_type(dev);
  603. return type == PCI_EXP_TYPE_ROOT_PORT ||
  604. type == PCI_EXP_TYPE_RC_EC;
  605. }
  606. static bool pcie_capability_reg_implemented(struct pci_dev *dev, int pos)
  607. {
  608. if (!pci_is_pcie(dev))
  609. return false;
  610. switch (pos) {
  611. case PCI_EXP_FLAGS:
  612. return true;
  613. case PCI_EXP_DEVCAP:
  614. case PCI_EXP_DEVCTL:
  615. case PCI_EXP_DEVSTA:
  616. return true;
  617. case PCI_EXP_LNKCAP:
  618. case PCI_EXP_LNKCTL:
  619. case PCI_EXP_LNKSTA:
  620. return pcie_cap_has_lnkctl(dev);
  621. case PCI_EXP_SLTCAP:
  622. case PCI_EXP_SLTCTL:
  623. case PCI_EXP_SLTSTA:
  624. return pcie_cap_has_sltctl(dev);
  625. case PCI_EXP_RTCTL:
  626. case PCI_EXP_RTCAP:
  627. case PCI_EXP_RTSTA:
  628. return pcie_cap_has_rtctl(dev);
  629. case PCI_EXP_DEVCAP2:
  630. case PCI_EXP_DEVCTL2:
  631. case PCI_EXP_LNKCAP2:
  632. case PCI_EXP_LNKCTL2:
  633. case PCI_EXP_LNKSTA2:
  634. return pcie_cap_version(dev) > 1;
  635. default:
  636. return false;
  637. }
  638. }
  639. /*
  640. * Note that these accessor functions are only for the "PCI Express
  641. * Capability" (see PCIe spec r3.0, sec 7.8). They do not apply to the
  642. * other "PCI Express Extended Capabilities" (AER, VC, ACS, MFVC, etc.)
  643. */
  644. int pcie_capability_read_word(struct pci_dev *dev, int pos, u16 *val)
  645. {
  646. int ret;
  647. *val = 0;
  648. if (pos & 1)
  649. return -EINVAL;
  650. if (pcie_capability_reg_implemented(dev, pos)) {
  651. ret = pci_read_config_word(dev, pci_pcie_cap(dev) + pos, val);
  652. /*
  653. * Reset *val to 0 if pci_read_config_word() fails, it may
  654. * have been written as 0xFFFF if hardware error happens
  655. * during pci_read_config_word().
  656. */
  657. if (ret)
  658. *val = 0;
  659. return ret;
  660. }
  661. /*
  662. * For Functions that do not implement the Slot Capabilities,
  663. * Slot Status, and Slot Control registers, these spaces must
  664. * be hardwired to 0b, with the exception of the Presence Detect
  665. * State bit in the Slot Status register of Downstream Ports,
  666. * which must be hardwired to 1b. (PCIe Base Spec 3.0, sec 7.8)
  667. */
  668. if (pci_is_pcie(dev) && pcie_downstream_port(dev) &&
  669. pos == PCI_EXP_SLTSTA)
  670. *val = PCI_EXP_SLTSTA_PDS;
  671. return 0;
  672. }
  673. EXPORT_SYMBOL(pcie_capability_read_word);
  674. int pcie_capability_read_dword(struct pci_dev *dev, int pos, u32 *val)
  675. {
  676. int ret;
  677. *val = 0;
  678. if (pos & 3)
  679. return -EINVAL;
  680. if (pcie_capability_reg_implemented(dev, pos)) {
  681. ret = pci_read_config_dword(dev, pci_pcie_cap(dev) + pos, val);
  682. /*
  683. * Reset *val to 0 if pci_read_config_dword() fails, it may
  684. * have been written as 0xFFFFFFFF if hardware error happens
  685. * during pci_read_config_dword().
  686. */
  687. if (ret)
  688. *val = 0;
  689. return ret;
  690. }
  691. if (pci_is_pcie(dev) && pcie_downstream_port(dev) &&
  692. pos == PCI_EXP_SLTSTA)
  693. *val = PCI_EXP_SLTSTA_PDS;
  694. return 0;
  695. }
  696. EXPORT_SYMBOL(pcie_capability_read_dword);
  697. int pcie_capability_write_word(struct pci_dev *dev, int pos, u16 val)
  698. {
  699. if (pos & 1)
  700. return -EINVAL;
  701. if (!pcie_capability_reg_implemented(dev, pos))
  702. return 0;
  703. return pci_write_config_word(dev, pci_pcie_cap(dev) + pos, val);
  704. }
  705. EXPORT_SYMBOL(pcie_capability_write_word);
  706. int pcie_capability_write_dword(struct pci_dev *dev, int pos, u32 val)
  707. {
  708. if (pos & 3)
  709. return -EINVAL;
  710. if (!pcie_capability_reg_implemented(dev, pos))
  711. return 0;
  712. return pci_write_config_dword(dev, pci_pcie_cap(dev) + pos, val);
  713. }
  714. EXPORT_SYMBOL(pcie_capability_write_dword);
  715. int pcie_capability_clear_and_set_word(struct pci_dev *dev, int pos,
  716. u16 clear, u16 set)
  717. {
  718. int ret;
  719. u16 val;
  720. ret = pcie_capability_read_word(dev, pos, &val);
  721. if (!ret) {
  722. val &= ~clear;
  723. val |= set;
  724. ret = pcie_capability_write_word(dev, pos, val);
  725. }
  726. return ret;
  727. }
  728. EXPORT_SYMBOL(pcie_capability_clear_and_set_word);
  729. int pcie_capability_clear_and_set_dword(struct pci_dev *dev, int pos,
  730. u32 clear, u32 set)
  731. {
  732. int ret;
  733. u32 val;
  734. ret = pcie_capability_read_dword(dev, pos, &val);
  735. if (!ret) {
  736. val &= ~clear;
  737. val |= set;
  738. ret = pcie_capability_write_dword(dev, pos, val);
  739. }
  740. return ret;
  741. }
  742. EXPORT_SYMBOL(pcie_capability_clear_and_set_dword);