access.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  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. #define PCI_VPD_MAX_SIZE (PCI_VPD_ADDR_MASK + 1)
  240. /**
  241. * pci_vpd_size - determine actual size of Vital Product Data
  242. * @dev: pci device struct
  243. * @old_size: current assumed size, also maximum allowed size
  244. */
  245. static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size)
  246. {
  247. size_t off = 0;
  248. unsigned char header[1+2]; /* 1 byte tag, 2 bytes length */
  249. while (off < old_size &&
  250. pci_read_vpd(dev, off, 1, header) == 1) {
  251. unsigned char tag;
  252. if (header[0] & PCI_VPD_LRDT) {
  253. /* Large Resource Data Type Tag */
  254. tag = pci_vpd_lrdt_tag(header);
  255. /* Only read length from known tag items */
  256. if ((tag == PCI_VPD_LTIN_ID_STRING) ||
  257. (tag == PCI_VPD_LTIN_RO_DATA) ||
  258. (tag == PCI_VPD_LTIN_RW_DATA)) {
  259. if (pci_read_vpd(dev, off+1, 2,
  260. &header[1]) != 2) {
  261. dev_warn(&dev->dev,
  262. "invalid large VPD tag %02x size at offset %zu",
  263. tag, off + 1);
  264. return 0;
  265. }
  266. off += PCI_VPD_LRDT_TAG_SIZE +
  267. pci_vpd_lrdt_size(header);
  268. }
  269. } else {
  270. /* Short Resource Data Type Tag */
  271. off += PCI_VPD_SRDT_TAG_SIZE +
  272. pci_vpd_srdt_size(header);
  273. tag = pci_vpd_srdt_tag(header);
  274. }
  275. if (tag == PCI_VPD_STIN_END) /* End tag descriptor */
  276. return off;
  277. if ((tag != PCI_VPD_LTIN_ID_STRING) &&
  278. (tag != PCI_VPD_LTIN_RO_DATA) &&
  279. (tag != PCI_VPD_LTIN_RW_DATA)) {
  280. dev_warn(&dev->dev,
  281. "invalid %s VPD tag %02x at offset %zu",
  282. (header[0] & PCI_VPD_LRDT) ? "large" : "short",
  283. tag, off);
  284. return 0;
  285. }
  286. }
  287. return 0;
  288. }
  289. /*
  290. * Wait for last operation to complete.
  291. * This code has to spin since there is no other notification from the PCI
  292. * hardware. Since the VPD is often implemented by serial attachment to an
  293. * EEPROM, it may take many milliseconds to complete.
  294. *
  295. * Returns 0 on success, negative values indicate error.
  296. */
  297. static int pci_vpd_wait(struct pci_dev *dev)
  298. {
  299. struct pci_vpd *vpd = dev->vpd;
  300. unsigned long timeout = jiffies + msecs_to_jiffies(50);
  301. unsigned long max_sleep = 16;
  302. u16 status;
  303. int ret;
  304. if (!vpd->busy)
  305. return 0;
  306. while (time_before(jiffies, timeout)) {
  307. ret = pci_user_read_config_word(dev, vpd->cap + PCI_VPD_ADDR,
  308. &status);
  309. if (ret < 0)
  310. return ret;
  311. if ((status & PCI_VPD_ADDR_F) == vpd->flag) {
  312. vpd->busy = 0;
  313. return 0;
  314. }
  315. if (fatal_signal_pending(current))
  316. return -EINTR;
  317. usleep_range(10, max_sleep);
  318. if (max_sleep < 1024)
  319. max_sleep *= 2;
  320. }
  321. 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");
  322. return -ETIMEDOUT;
  323. }
  324. static ssize_t pci_vpd_read(struct pci_dev *dev, loff_t pos, size_t count,
  325. void *arg)
  326. {
  327. struct pci_vpd *vpd = dev->vpd;
  328. int ret;
  329. loff_t end = pos + count;
  330. u8 *buf = arg;
  331. if (pos < 0)
  332. return -EINVAL;
  333. if (!vpd->valid) {
  334. vpd->valid = 1;
  335. vpd->len = pci_vpd_size(dev, vpd->len);
  336. }
  337. if (vpd->len == 0)
  338. return -EIO;
  339. if (pos > vpd->len)
  340. return 0;
  341. if (end > vpd->len) {
  342. end = vpd->len;
  343. count = end - pos;
  344. }
  345. if (mutex_lock_killable(&vpd->lock))
  346. return -EINTR;
  347. ret = pci_vpd_wait(dev);
  348. if (ret < 0)
  349. goto out;
  350. while (pos < end) {
  351. u32 val;
  352. unsigned int i, skip;
  353. ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR,
  354. pos & ~3);
  355. if (ret < 0)
  356. break;
  357. vpd->busy = 1;
  358. vpd->flag = PCI_VPD_ADDR_F;
  359. ret = pci_vpd_wait(dev);
  360. if (ret < 0)
  361. break;
  362. ret = pci_user_read_config_dword(dev, vpd->cap + PCI_VPD_DATA, &val);
  363. if (ret < 0)
  364. break;
  365. skip = pos & 3;
  366. for (i = 0; i < sizeof(u32); i++) {
  367. if (i >= skip) {
  368. *buf++ = val;
  369. if (++pos == end)
  370. break;
  371. }
  372. val >>= 8;
  373. }
  374. }
  375. out:
  376. mutex_unlock(&vpd->lock);
  377. return ret ? ret : count;
  378. }
  379. static ssize_t pci_vpd_write(struct pci_dev *dev, loff_t pos, size_t count,
  380. const void *arg)
  381. {
  382. struct pci_vpd *vpd = dev->vpd;
  383. const u8 *buf = arg;
  384. loff_t end = pos + count;
  385. int ret = 0;
  386. if (pos < 0 || (pos & 3) || (count & 3))
  387. return -EINVAL;
  388. if (!vpd->valid) {
  389. vpd->valid = 1;
  390. vpd->len = pci_vpd_size(dev, vpd->len);
  391. }
  392. if (vpd->len == 0)
  393. return -EIO;
  394. if (end > vpd->len)
  395. return -EINVAL;
  396. if (mutex_lock_killable(&vpd->lock))
  397. return -EINTR;
  398. ret = pci_vpd_wait(dev);
  399. if (ret < 0)
  400. goto out;
  401. while (pos < end) {
  402. u32 val;
  403. val = *buf++;
  404. val |= *buf++ << 8;
  405. val |= *buf++ << 16;
  406. val |= *buf++ << 24;
  407. ret = pci_user_write_config_dword(dev, vpd->cap + PCI_VPD_DATA, val);
  408. if (ret < 0)
  409. break;
  410. ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR,
  411. pos | PCI_VPD_ADDR_F);
  412. if (ret < 0)
  413. break;
  414. vpd->busy = 1;
  415. vpd->flag = 0;
  416. ret = pci_vpd_wait(dev);
  417. if (ret < 0)
  418. break;
  419. pos += sizeof(u32);
  420. }
  421. out:
  422. mutex_unlock(&vpd->lock);
  423. return ret ? ret : count;
  424. }
  425. static const struct pci_vpd_ops pci_vpd_ops = {
  426. .read = pci_vpd_read,
  427. .write = pci_vpd_write,
  428. };
  429. static ssize_t pci_vpd_f0_read(struct pci_dev *dev, loff_t pos, size_t count,
  430. void *arg)
  431. {
  432. struct pci_dev *tdev = pci_get_slot(dev->bus,
  433. PCI_DEVFN(PCI_SLOT(dev->devfn), 0));
  434. ssize_t ret;
  435. if (!tdev)
  436. return -ENODEV;
  437. ret = pci_read_vpd(tdev, pos, count, arg);
  438. pci_dev_put(tdev);
  439. return ret;
  440. }
  441. static ssize_t pci_vpd_f0_write(struct pci_dev *dev, loff_t pos, size_t count,
  442. const void *arg)
  443. {
  444. struct pci_dev *tdev = pci_get_slot(dev->bus,
  445. PCI_DEVFN(PCI_SLOT(dev->devfn), 0));
  446. ssize_t ret;
  447. if (!tdev)
  448. return -ENODEV;
  449. ret = pci_write_vpd(tdev, pos, count, arg);
  450. pci_dev_put(tdev);
  451. return ret;
  452. }
  453. static const struct pci_vpd_ops pci_vpd_f0_ops = {
  454. .read = pci_vpd_f0_read,
  455. .write = pci_vpd_f0_write,
  456. };
  457. int pci_vpd_init(struct pci_dev *dev)
  458. {
  459. struct pci_vpd *vpd;
  460. u8 cap;
  461. cap = pci_find_capability(dev, PCI_CAP_ID_VPD);
  462. if (!cap)
  463. return -ENODEV;
  464. vpd = kzalloc(sizeof(*vpd), GFP_ATOMIC);
  465. if (!vpd)
  466. return -ENOMEM;
  467. vpd->len = PCI_VPD_MAX_SIZE;
  468. if (dev->dev_flags & PCI_DEV_FLAGS_VPD_REF_F0)
  469. vpd->ops = &pci_vpd_f0_ops;
  470. else
  471. vpd->ops = &pci_vpd_ops;
  472. mutex_init(&vpd->lock);
  473. vpd->cap = cap;
  474. vpd->busy = 0;
  475. vpd->valid = 0;
  476. dev->vpd = vpd;
  477. return 0;
  478. }
  479. void pci_vpd_release(struct pci_dev *dev)
  480. {
  481. kfree(dev->vpd);
  482. }
  483. /**
  484. * pci_cfg_access_lock - Lock PCI config reads/writes
  485. * @dev: pci device struct
  486. *
  487. * When access is locked, any userspace reads or writes to config
  488. * space and concurrent lock requests will sleep until access is
  489. * allowed via pci_cfg_access_unlocked again.
  490. */
  491. void pci_cfg_access_lock(struct pci_dev *dev)
  492. {
  493. might_sleep();
  494. raw_spin_lock_irq(&pci_lock);
  495. if (dev->block_cfg_access)
  496. pci_wait_cfg(dev);
  497. dev->block_cfg_access = 1;
  498. raw_spin_unlock_irq(&pci_lock);
  499. }
  500. EXPORT_SYMBOL_GPL(pci_cfg_access_lock);
  501. /**
  502. * pci_cfg_access_trylock - try to lock PCI config reads/writes
  503. * @dev: pci device struct
  504. *
  505. * Same as pci_cfg_access_lock, but will return 0 if access is
  506. * already locked, 1 otherwise. This function can be used from
  507. * atomic contexts.
  508. */
  509. bool pci_cfg_access_trylock(struct pci_dev *dev)
  510. {
  511. unsigned long flags;
  512. bool locked = true;
  513. raw_spin_lock_irqsave(&pci_lock, flags);
  514. if (dev->block_cfg_access)
  515. locked = false;
  516. else
  517. dev->block_cfg_access = 1;
  518. raw_spin_unlock_irqrestore(&pci_lock, flags);
  519. return locked;
  520. }
  521. EXPORT_SYMBOL_GPL(pci_cfg_access_trylock);
  522. /**
  523. * pci_cfg_access_unlock - Unlock PCI config reads/writes
  524. * @dev: pci device struct
  525. *
  526. * This function allows PCI config accesses to resume.
  527. */
  528. void pci_cfg_access_unlock(struct pci_dev *dev)
  529. {
  530. unsigned long flags;
  531. raw_spin_lock_irqsave(&pci_lock, flags);
  532. /* This indicates a problem in the caller, but we don't need
  533. * to kill them, unlike a double-block above. */
  534. WARN_ON(!dev->block_cfg_access);
  535. dev->block_cfg_access = 0;
  536. wake_up_all(&pci_cfg_wait);
  537. raw_spin_unlock_irqrestore(&pci_lock, flags);
  538. }
  539. EXPORT_SYMBOL_GPL(pci_cfg_access_unlock);
  540. static inline int pcie_cap_version(const struct pci_dev *dev)
  541. {
  542. return pcie_caps_reg(dev) & PCI_EXP_FLAGS_VERS;
  543. }
  544. static bool pcie_downstream_port(const struct pci_dev *dev)
  545. {
  546. int type = pci_pcie_type(dev);
  547. return type == PCI_EXP_TYPE_ROOT_PORT ||
  548. type == PCI_EXP_TYPE_DOWNSTREAM;
  549. }
  550. bool pcie_cap_has_lnkctl(const struct pci_dev *dev)
  551. {
  552. int type = pci_pcie_type(dev);
  553. return type == PCI_EXP_TYPE_ENDPOINT ||
  554. type == PCI_EXP_TYPE_LEG_END ||
  555. type == PCI_EXP_TYPE_ROOT_PORT ||
  556. type == PCI_EXP_TYPE_UPSTREAM ||
  557. type == PCI_EXP_TYPE_DOWNSTREAM ||
  558. type == PCI_EXP_TYPE_PCI_BRIDGE ||
  559. type == PCI_EXP_TYPE_PCIE_BRIDGE;
  560. }
  561. static inline bool pcie_cap_has_sltctl(const struct pci_dev *dev)
  562. {
  563. return pcie_downstream_port(dev) &&
  564. pcie_caps_reg(dev) & PCI_EXP_FLAGS_SLOT;
  565. }
  566. static inline bool pcie_cap_has_rtctl(const struct pci_dev *dev)
  567. {
  568. int type = pci_pcie_type(dev);
  569. return type == PCI_EXP_TYPE_ROOT_PORT ||
  570. type == PCI_EXP_TYPE_RC_EC;
  571. }
  572. static bool pcie_capability_reg_implemented(struct pci_dev *dev, int pos)
  573. {
  574. if (!pci_is_pcie(dev))
  575. return false;
  576. switch (pos) {
  577. case PCI_EXP_FLAGS:
  578. return true;
  579. case PCI_EXP_DEVCAP:
  580. case PCI_EXP_DEVCTL:
  581. case PCI_EXP_DEVSTA:
  582. return true;
  583. case PCI_EXP_LNKCAP:
  584. case PCI_EXP_LNKCTL:
  585. case PCI_EXP_LNKSTA:
  586. return pcie_cap_has_lnkctl(dev);
  587. case PCI_EXP_SLTCAP:
  588. case PCI_EXP_SLTCTL:
  589. case PCI_EXP_SLTSTA:
  590. return pcie_cap_has_sltctl(dev);
  591. case PCI_EXP_RTCTL:
  592. case PCI_EXP_RTCAP:
  593. case PCI_EXP_RTSTA:
  594. return pcie_cap_has_rtctl(dev);
  595. case PCI_EXP_DEVCAP2:
  596. case PCI_EXP_DEVCTL2:
  597. case PCI_EXP_LNKCAP2:
  598. case PCI_EXP_LNKCTL2:
  599. case PCI_EXP_LNKSTA2:
  600. return pcie_cap_version(dev) > 1;
  601. default:
  602. return false;
  603. }
  604. }
  605. /*
  606. * Note that these accessor functions are only for the "PCI Express
  607. * Capability" (see PCIe spec r3.0, sec 7.8). They do not apply to the
  608. * other "PCI Express Extended Capabilities" (AER, VC, ACS, MFVC, etc.)
  609. */
  610. int pcie_capability_read_word(struct pci_dev *dev, int pos, u16 *val)
  611. {
  612. int ret;
  613. *val = 0;
  614. if (pos & 1)
  615. return -EINVAL;
  616. if (pcie_capability_reg_implemented(dev, pos)) {
  617. ret = pci_read_config_word(dev, pci_pcie_cap(dev) + pos, val);
  618. /*
  619. * Reset *val to 0 if pci_read_config_word() fails, it may
  620. * have been written as 0xFFFF if hardware error happens
  621. * during pci_read_config_word().
  622. */
  623. if (ret)
  624. *val = 0;
  625. return ret;
  626. }
  627. /*
  628. * For Functions that do not implement the Slot Capabilities,
  629. * Slot Status, and Slot Control registers, these spaces must
  630. * be hardwired to 0b, with the exception of the Presence Detect
  631. * State bit in the Slot Status register of Downstream Ports,
  632. * which must be hardwired to 1b. (PCIe Base Spec 3.0, sec 7.8)
  633. */
  634. if (pci_is_pcie(dev) && pcie_downstream_port(dev) &&
  635. pos == PCI_EXP_SLTSTA)
  636. *val = PCI_EXP_SLTSTA_PDS;
  637. return 0;
  638. }
  639. EXPORT_SYMBOL(pcie_capability_read_word);
  640. int pcie_capability_read_dword(struct pci_dev *dev, int pos, u32 *val)
  641. {
  642. int ret;
  643. *val = 0;
  644. if (pos & 3)
  645. return -EINVAL;
  646. if (pcie_capability_reg_implemented(dev, pos)) {
  647. ret = pci_read_config_dword(dev, pci_pcie_cap(dev) + pos, val);
  648. /*
  649. * Reset *val to 0 if pci_read_config_dword() fails, it may
  650. * have been written as 0xFFFFFFFF if hardware error happens
  651. * during pci_read_config_dword().
  652. */
  653. if (ret)
  654. *val = 0;
  655. return ret;
  656. }
  657. if (pci_is_pcie(dev) && pcie_downstream_port(dev) &&
  658. pos == PCI_EXP_SLTSTA)
  659. *val = PCI_EXP_SLTSTA_PDS;
  660. return 0;
  661. }
  662. EXPORT_SYMBOL(pcie_capability_read_dword);
  663. int pcie_capability_write_word(struct pci_dev *dev, int pos, u16 val)
  664. {
  665. if (pos & 1)
  666. return -EINVAL;
  667. if (!pcie_capability_reg_implemented(dev, pos))
  668. return 0;
  669. return pci_write_config_word(dev, pci_pcie_cap(dev) + pos, val);
  670. }
  671. EXPORT_SYMBOL(pcie_capability_write_word);
  672. int pcie_capability_write_dword(struct pci_dev *dev, int pos, u32 val)
  673. {
  674. if (pos & 3)
  675. return -EINVAL;
  676. if (!pcie_capability_reg_implemented(dev, pos))
  677. return 0;
  678. return pci_write_config_dword(dev, pci_pcie_cap(dev) + pos, val);
  679. }
  680. EXPORT_SYMBOL(pcie_capability_write_dword);
  681. int pcie_capability_clear_and_set_word(struct pci_dev *dev, int pos,
  682. u16 clear, u16 set)
  683. {
  684. int ret;
  685. u16 val;
  686. ret = pcie_capability_read_word(dev, pos, &val);
  687. if (!ret) {
  688. val &= ~clear;
  689. val |= set;
  690. ret = pcie_capability_write_word(dev, pos, val);
  691. }
  692. return ret;
  693. }
  694. EXPORT_SYMBOL(pcie_capability_clear_and_set_word);
  695. int pcie_capability_clear_and_set_dword(struct pci_dev *dev, int pos,
  696. u32 clear, u32 set)
  697. {
  698. int ret;
  699. u32 val;
  700. ret = pcie_capability_read_dword(dev, pos, &val);
  701. if (!ret) {
  702. val &= ~clear;
  703. val |= set;
  704. ret = pcie_capability_write_dword(dev, pos, val);
  705. }
  706. return ret;
  707. }
  708. EXPORT_SYMBOL(pcie_capability_clear_and_set_dword);