access.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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 = PCIBIOS_SUCCESSFUL; \
  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. return pcibios_err_to_errno(ret); \
  146. } \
  147. EXPORT_SYMBOL_GPL(pci_user_read_config_##size);
  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 = PCIBIOS_SUCCESSFUL; \
  154. if (PCI_##size##_BAD) \
  155. return -EINVAL; \
  156. raw_spin_lock_irq(&pci_lock); \
  157. if (unlikely(dev->block_cfg_access)) \
  158. pci_wait_cfg(dev); \
  159. ret = dev->bus->ops->write(dev->bus, dev->devfn, \
  160. pos, sizeof(type), val); \
  161. raw_spin_unlock_irq(&pci_lock); \
  162. return pcibios_err_to_errno(ret); \
  163. } \
  164. EXPORT_SYMBOL_GPL(pci_user_write_config_##size);
  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, "vpd r/w failed. This is likely a firmware bug on this device. Contact the card vendor for a firmware update\n");
  208. return -ETIMEDOUT;
  209. }
  210. if (fatal_signal_pending(current))
  211. return -EINTR;
  212. if (!cond_resched())
  213. udelay(10);
  214. }
  215. }
  216. static ssize_t pci_vpd_pci22_read(struct pci_dev *dev, loff_t pos, size_t count,
  217. void *arg)
  218. {
  219. struct pci_vpd_pci22 *vpd =
  220. container_of(dev->vpd, struct pci_vpd_pci22, base);
  221. int ret;
  222. loff_t end = pos + count;
  223. u8 *buf = arg;
  224. if (pos < 0 || pos > vpd->base.len || end > vpd->base.len)
  225. return -EINVAL;
  226. if (mutex_lock_killable(&vpd->lock))
  227. return -EINTR;
  228. ret = pci_vpd_pci22_wait(dev);
  229. if (ret < 0)
  230. goto out;
  231. while (pos < end) {
  232. u32 val;
  233. unsigned int i, skip;
  234. ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR,
  235. pos & ~3);
  236. if (ret < 0)
  237. break;
  238. vpd->busy = true;
  239. vpd->flag = PCI_VPD_ADDR_F;
  240. ret = pci_vpd_pci22_wait(dev);
  241. if (ret < 0)
  242. break;
  243. ret = pci_user_read_config_dword(dev, vpd->cap + PCI_VPD_DATA, &val);
  244. if (ret < 0)
  245. break;
  246. skip = pos & 3;
  247. for (i = 0; i < sizeof(u32); i++) {
  248. if (i >= skip) {
  249. *buf++ = val;
  250. if (++pos == end)
  251. break;
  252. }
  253. val >>= 8;
  254. }
  255. }
  256. out:
  257. mutex_unlock(&vpd->lock);
  258. return ret ? ret : count;
  259. }
  260. static ssize_t pci_vpd_pci22_write(struct pci_dev *dev, loff_t pos, size_t count,
  261. const void *arg)
  262. {
  263. struct pci_vpd_pci22 *vpd =
  264. container_of(dev->vpd, struct pci_vpd_pci22, base);
  265. const u8 *buf = arg;
  266. loff_t end = pos + count;
  267. int ret = 0;
  268. if (pos < 0 || (pos & 3) || (count & 3) || end > vpd->base.len)
  269. return -EINVAL;
  270. if (mutex_lock_killable(&vpd->lock))
  271. return -EINTR;
  272. ret = pci_vpd_pci22_wait(dev);
  273. if (ret < 0)
  274. goto out;
  275. while (pos < end) {
  276. u32 val;
  277. val = *buf++;
  278. val |= *buf++ << 8;
  279. val |= *buf++ << 16;
  280. val |= *buf++ << 24;
  281. ret = pci_user_write_config_dword(dev, vpd->cap + PCI_VPD_DATA, val);
  282. if (ret < 0)
  283. break;
  284. ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR,
  285. pos | PCI_VPD_ADDR_F);
  286. if (ret < 0)
  287. break;
  288. vpd->busy = true;
  289. vpd->flag = 0;
  290. ret = pci_vpd_pci22_wait(dev);
  291. if (ret < 0)
  292. break;
  293. pos += sizeof(u32);
  294. }
  295. out:
  296. mutex_unlock(&vpd->lock);
  297. return ret ? ret : count;
  298. }
  299. static void pci_vpd_pci22_release(struct pci_dev *dev)
  300. {
  301. kfree(container_of(dev->vpd, struct pci_vpd_pci22, base));
  302. }
  303. static const struct pci_vpd_ops pci_vpd_pci22_ops = {
  304. .read = pci_vpd_pci22_read,
  305. .write = pci_vpd_pci22_write,
  306. .release = pci_vpd_pci22_release,
  307. };
  308. int pci_vpd_pci22_init(struct pci_dev *dev)
  309. {
  310. struct pci_vpd_pci22 *vpd;
  311. u8 cap;
  312. cap = pci_find_capability(dev, PCI_CAP_ID_VPD);
  313. if (!cap)
  314. return -ENODEV;
  315. vpd = kzalloc(sizeof(*vpd), GFP_ATOMIC);
  316. if (!vpd)
  317. return -ENOMEM;
  318. vpd->base.len = PCI_VPD_PCI22_SIZE;
  319. vpd->base.ops = &pci_vpd_pci22_ops;
  320. mutex_init(&vpd->lock);
  321. vpd->cap = cap;
  322. vpd->busy = false;
  323. dev->vpd = &vpd->base;
  324. return 0;
  325. }
  326. /**
  327. * pci_cfg_access_lock - Lock PCI config reads/writes
  328. * @dev: pci device struct
  329. *
  330. * When access is locked, any userspace reads or writes to config
  331. * space and concurrent lock requests will sleep until access is
  332. * allowed via pci_cfg_access_unlocked again.
  333. */
  334. void pci_cfg_access_lock(struct pci_dev *dev)
  335. {
  336. might_sleep();
  337. raw_spin_lock_irq(&pci_lock);
  338. if (dev->block_cfg_access)
  339. pci_wait_cfg(dev);
  340. dev->block_cfg_access = 1;
  341. raw_spin_unlock_irq(&pci_lock);
  342. }
  343. EXPORT_SYMBOL_GPL(pci_cfg_access_lock);
  344. /**
  345. * pci_cfg_access_trylock - try to lock PCI config reads/writes
  346. * @dev: pci device struct
  347. *
  348. * Same as pci_cfg_access_lock, but will return 0 if access is
  349. * already locked, 1 otherwise. This function can be used from
  350. * atomic contexts.
  351. */
  352. bool pci_cfg_access_trylock(struct pci_dev *dev)
  353. {
  354. unsigned long flags;
  355. bool locked = true;
  356. raw_spin_lock_irqsave(&pci_lock, flags);
  357. if (dev->block_cfg_access)
  358. locked = false;
  359. else
  360. dev->block_cfg_access = 1;
  361. raw_spin_unlock_irqrestore(&pci_lock, flags);
  362. return locked;
  363. }
  364. EXPORT_SYMBOL_GPL(pci_cfg_access_trylock);
  365. /**
  366. * pci_cfg_access_unlock - Unlock PCI config reads/writes
  367. * @dev: pci device struct
  368. *
  369. * This function allows PCI config accesses to resume.
  370. */
  371. void pci_cfg_access_unlock(struct pci_dev *dev)
  372. {
  373. unsigned long flags;
  374. raw_spin_lock_irqsave(&pci_lock, flags);
  375. /* This indicates a problem in the caller, but we don't need
  376. * to kill them, unlike a double-block above. */
  377. WARN_ON(!dev->block_cfg_access);
  378. dev->block_cfg_access = 0;
  379. wake_up_all(&pci_cfg_wait);
  380. raw_spin_unlock_irqrestore(&pci_lock, flags);
  381. }
  382. EXPORT_SYMBOL_GPL(pci_cfg_access_unlock);
  383. static inline int pcie_cap_version(const struct pci_dev *dev)
  384. {
  385. return pcie_caps_reg(dev) & PCI_EXP_FLAGS_VERS;
  386. }
  387. static inline bool pcie_cap_has_lnkctl(const struct pci_dev *dev)
  388. {
  389. int type = pci_pcie_type(dev);
  390. return type == PCI_EXP_TYPE_ENDPOINT ||
  391. type == PCI_EXP_TYPE_LEG_END ||
  392. type == PCI_EXP_TYPE_ROOT_PORT ||
  393. type == PCI_EXP_TYPE_UPSTREAM ||
  394. type == PCI_EXP_TYPE_DOWNSTREAM ||
  395. type == PCI_EXP_TYPE_PCI_BRIDGE ||
  396. type == PCI_EXP_TYPE_PCIE_BRIDGE;
  397. }
  398. static inline bool pcie_cap_has_sltctl(const struct pci_dev *dev)
  399. {
  400. int type = pci_pcie_type(dev);
  401. return (type == PCI_EXP_TYPE_ROOT_PORT ||
  402. type == PCI_EXP_TYPE_DOWNSTREAM) &&
  403. pcie_caps_reg(dev) & PCI_EXP_FLAGS_SLOT;
  404. }
  405. static inline bool pcie_cap_has_rtctl(const struct pci_dev *dev)
  406. {
  407. int type = pci_pcie_type(dev);
  408. return type == PCI_EXP_TYPE_ROOT_PORT ||
  409. type == PCI_EXP_TYPE_RC_EC;
  410. }
  411. static bool pcie_capability_reg_implemented(struct pci_dev *dev, int pos)
  412. {
  413. if (!pci_is_pcie(dev))
  414. return false;
  415. switch (pos) {
  416. case PCI_EXP_FLAGS:
  417. return true;
  418. case PCI_EXP_DEVCAP:
  419. case PCI_EXP_DEVCTL:
  420. case PCI_EXP_DEVSTA:
  421. return true;
  422. case PCI_EXP_LNKCAP:
  423. case PCI_EXP_LNKCTL:
  424. case PCI_EXP_LNKSTA:
  425. return pcie_cap_has_lnkctl(dev);
  426. case PCI_EXP_SLTCAP:
  427. case PCI_EXP_SLTCTL:
  428. case PCI_EXP_SLTSTA:
  429. return pcie_cap_has_sltctl(dev);
  430. case PCI_EXP_RTCTL:
  431. case PCI_EXP_RTCAP:
  432. case PCI_EXP_RTSTA:
  433. return pcie_cap_has_rtctl(dev);
  434. case PCI_EXP_DEVCAP2:
  435. case PCI_EXP_DEVCTL2:
  436. case PCI_EXP_LNKCAP2:
  437. case PCI_EXP_LNKCTL2:
  438. case PCI_EXP_LNKSTA2:
  439. return pcie_cap_version(dev) > 1;
  440. default:
  441. return false;
  442. }
  443. }
  444. /*
  445. * Note that these accessor functions are only for the "PCI Express
  446. * Capability" (see PCIe spec r3.0, sec 7.8). They do not apply to the
  447. * other "PCI Express Extended Capabilities" (AER, VC, ACS, MFVC, etc.)
  448. */
  449. int pcie_capability_read_word(struct pci_dev *dev, int pos, u16 *val)
  450. {
  451. int ret;
  452. *val = 0;
  453. if (pos & 1)
  454. return -EINVAL;
  455. if (pcie_capability_reg_implemented(dev, pos)) {
  456. ret = pci_read_config_word(dev, pci_pcie_cap(dev) + pos, val);
  457. /*
  458. * Reset *val to 0 if pci_read_config_word() fails, it may
  459. * have been written as 0xFFFF if hardware error happens
  460. * during pci_read_config_word().
  461. */
  462. if (ret)
  463. *val = 0;
  464. return ret;
  465. }
  466. /*
  467. * For Functions that do not implement the Slot Capabilities,
  468. * Slot Status, and Slot Control registers, these spaces must
  469. * be hardwired to 0b, with the exception of the Presence Detect
  470. * State bit in the Slot Status register of Downstream Ports,
  471. * which must be hardwired to 1b. (PCIe Base Spec 3.0, sec 7.8)
  472. */
  473. if (pci_is_pcie(dev) && pos == PCI_EXP_SLTSTA &&
  474. pci_pcie_type(dev) == PCI_EXP_TYPE_DOWNSTREAM) {
  475. *val = PCI_EXP_SLTSTA_PDS;
  476. }
  477. return 0;
  478. }
  479. EXPORT_SYMBOL(pcie_capability_read_word);
  480. int pcie_capability_read_dword(struct pci_dev *dev, int pos, u32 *val)
  481. {
  482. int ret;
  483. *val = 0;
  484. if (pos & 3)
  485. return -EINVAL;
  486. if (pcie_capability_reg_implemented(dev, pos)) {
  487. ret = pci_read_config_dword(dev, pci_pcie_cap(dev) + pos, val);
  488. /*
  489. * Reset *val to 0 if pci_read_config_dword() fails, it may
  490. * have been written as 0xFFFFFFFF if hardware error happens
  491. * during pci_read_config_dword().
  492. */
  493. if (ret)
  494. *val = 0;
  495. return ret;
  496. }
  497. if (pci_is_pcie(dev) && pos == PCI_EXP_SLTCTL &&
  498. pci_pcie_type(dev) == PCI_EXP_TYPE_DOWNSTREAM) {
  499. *val = PCI_EXP_SLTSTA_PDS;
  500. }
  501. return 0;
  502. }
  503. EXPORT_SYMBOL(pcie_capability_read_dword);
  504. int pcie_capability_write_word(struct pci_dev *dev, int pos, u16 val)
  505. {
  506. if (pos & 1)
  507. return -EINVAL;
  508. if (!pcie_capability_reg_implemented(dev, pos))
  509. return 0;
  510. return pci_write_config_word(dev, pci_pcie_cap(dev) + pos, val);
  511. }
  512. EXPORT_SYMBOL(pcie_capability_write_word);
  513. int pcie_capability_write_dword(struct pci_dev *dev, int pos, u32 val)
  514. {
  515. if (pos & 3)
  516. return -EINVAL;
  517. if (!pcie_capability_reg_implemented(dev, pos))
  518. return 0;
  519. return pci_write_config_dword(dev, pci_pcie_cap(dev) + pos, val);
  520. }
  521. EXPORT_SYMBOL(pcie_capability_write_dword);
  522. int pcie_capability_clear_and_set_word(struct pci_dev *dev, int pos,
  523. u16 clear, u16 set)
  524. {
  525. int ret;
  526. u16 val;
  527. ret = pcie_capability_read_word(dev, pos, &val);
  528. if (!ret) {
  529. val &= ~clear;
  530. val |= set;
  531. ret = pcie_capability_write_word(dev, pos, val);
  532. }
  533. return ret;
  534. }
  535. EXPORT_SYMBOL(pcie_capability_clear_and_set_word);
  536. int pcie_capability_clear_and_set_dword(struct pci_dev *dev, int pos,
  537. u32 clear, u32 set)
  538. {
  539. int ret;
  540. u32 val;
  541. ret = pcie_capability_read_dword(dev, pos, &val);
  542. if (!ret) {
  543. val &= ~clear;
  544. val |= set;
  545. ret = pcie_capability_write_dword(dev, pos, val);
  546. }
  547. return ret;
  548. }
  549. EXPORT_SYMBOL(pcie_capability_clear_and_set_dword);