access.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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. * pci_read_vpd - Read one entry from Vital Product Data
  149. * @dev: pci device struct
  150. * @pos: offset in vpd space
  151. * @count: number of bytes to read
  152. * @buf: pointer to where to store result
  153. *
  154. */
  155. ssize_t pci_read_vpd(struct pci_dev *dev, loff_t pos, size_t count, void *buf)
  156. {
  157. if (!dev->vpd || !dev->vpd->ops)
  158. return -ENODEV;
  159. return dev->vpd->ops->read(dev, pos, count, buf);
  160. }
  161. EXPORT_SYMBOL(pci_read_vpd);
  162. /**
  163. * pci_write_vpd - Write entry to Vital Product Data
  164. * @dev: pci device struct
  165. * @pos: offset in vpd space
  166. * @count: number of bytes to write
  167. * @buf: buffer containing write data
  168. *
  169. */
  170. ssize_t pci_write_vpd(struct pci_dev *dev, loff_t pos, size_t count, const void *buf)
  171. {
  172. if (!dev->vpd || !dev->vpd->ops)
  173. return -ENODEV;
  174. return dev->vpd->ops->write(dev, pos, count, buf);
  175. }
  176. EXPORT_SYMBOL(pci_write_vpd);
  177. /*
  178. * The following routines are to prevent the user from accessing PCI config
  179. * space when it's unsafe to do so. Some devices require this during BIST and
  180. * we're required to prevent it during D-state transitions.
  181. *
  182. * We have a bit per device to indicate it's blocked and a global wait queue
  183. * for callers to sleep on until devices are unblocked.
  184. */
  185. static DECLARE_WAIT_QUEUE_HEAD(pci_cfg_wait);
  186. static noinline void pci_wait_cfg(struct pci_dev *dev)
  187. {
  188. DECLARE_WAITQUEUE(wait, current);
  189. __add_wait_queue(&pci_cfg_wait, &wait);
  190. do {
  191. set_current_state(TASK_UNINTERRUPTIBLE);
  192. raw_spin_unlock_irq(&pci_lock);
  193. schedule();
  194. raw_spin_lock_irq(&pci_lock);
  195. } while (dev->block_cfg_access);
  196. __remove_wait_queue(&pci_cfg_wait, &wait);
  197. }
  198. /* Returns 0 on success, negative values indicate error. */
  199. #define PCI_USER_READ_CONFIG(size,type) \
  200. int pci_user_read_config_##size \
  201. (struct pci_dev *dev, int pos, type *val) \
  202. { \
  203. int ret = PCIBIOS_SUCCESSFUL; \
  204. u32 data = -1; \
  205. if (PCI_##size##_BAD) \
  206. return -EINVAL; \
  207. raw_spin_lock_irq(&pci_lock); \
  208. if (unlikely(dev->block_cfg_access)) \
  209. pci_wait_cfg(dev); \
  210. ret = dev->bus->ops->read(dev->bus, dev->devfn, \
  211. pos, sizeof(type), &data); \
  212. raw_spin_unlock_irq(&pci_lock); \
  213. *val = (type)data; \
  214. return pcibios_err_to_errno(ret); \
  215. } \
  216. EXPORT_SYMBOL_GPL(pci_user_read_config_##size);
  217. /* Returns 0 on success, negative values indicate error. */
  218. #define PCI_USER_WRITE_CONFIG(size,type) \
  219. int pci_user_write_config_##size \
  220. (struct pci_dev *dev, int pos, type val) \
  221. { \
  222. int ret = PCIBIOS_SUCCESSFUL; \
  223. if (PCI_##size##_BAD) \
  224. return -EINVAL; \
  225. raw_spin_lock_irq(&pci_lock); \
  226. if (unlikely(dev->block_cfg_access)) \
  227. pci_wait_cfg(dev); \
  228. ret = dev->bus->ops->write(dev->bus, dev->devfn, \
  229. pos, sizeof(type), val); \
  230. raw_spin_unlock_irq(&pci_lock); \
  231. return pcibios_err_to_errno(ret); \
  232. } \
  233. EXPORT_SYMBOL_GPL(pci_user_write_config_##size);
  234. PCI_USER_READ_CONFIG(byte, u8)
  235. PCI_USER_READ_CONFIG(word, u16)
  236. PCI_USER_READ_CONFIG(dword, u32)
  237. PCI_USER_WRITE_CONFIG(byte, u8)
  238. PCI_USER_WRITE_CONFIG(word, u16)
  239. PCI_USER_WRITE_CONFIG(dword, u32)
  240. /* VPD access through PCI 2.2+ VPD capability */
  241. #define PCI_VPD_PCI22_SIZE (PCI_VPD_ADDR_MASK + 1)
  242. struct pci_vpd_pci22 {
  243. struct pci_vpd base;
  244. struct mutex lock;
  245. u16 flag;
  246. bool busy;
  247. u8 cap;
  248. };
  249. /*
  250. * Wait for last operation to complete.
  251. * This code has to spin since there is no other notification from the PCI
  252. * hardware. Since the VPD is often implemented by serial attachment to an
  253. * EEPROM, it may take many milliseconds to complete.
  254. *
  255. * Returns 0 on success, negative values indicate error.
  256. */
  257. static int pci_vpd_pci22_wait(struct pci_dev *dev)
  258. {
  259. struct pci_vpd_pci22 *vpd =
  260. container_of(dev->vpd, struct pci_vpd_pci22, base);
  261. unsigned long timeout = jiffies + HZ/20 + 2;
  262. u16 status;
  263. int ret;
  264. if (!vpd->busy)
  265. return 0;
  266. for (;;) {
  267. ret = pci_user_read_config_word(dev, vpd->cap + PCI_VPD_ADDR,
  268. &status);
  269. if (ret < 0)
  270. return ret;
  271. if ((status & PCI_VPD_ADDR_F) == vpd->flag) {
  272. vpd->busy = false;
  273. return 0;
  274. }
  275. if (time_after(jiffies, timeout)) {
  276. 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");
  277. return -ETIMEDOUT;
  278. }
  279. if (fatal_signal_pending(current))
  280. return -EINTR;
  281. if (!cond_resched())
  282. udelay(10);
  283. }
  284. }
  285. static ssize_t pci_vpd_pci22_read(struct pci_dev *dev, loff_t pos, size_t count,
  286. void *arg)
  287. {
  288. struct pci_vpd_pci22 *vpd =
  289. container_of(dev->vpd, struct pci_vpd_pci22, base);
  290. int ret;
  291. loff_t end = pos + count;
  292. u8 *buf = arg;
  293. if (pos < 0 || pos > vpd->base.len || end > vpd->base.len)
  294. return -EINVAL;
  295. if (mutex_lock_killable(&vpd->lock))
  296. return -EINTR;
  297. ret = pci_vpd_pci22_wait(dev);
  298. if (ret < 0)
  299. goto out;
  300. while (pos < end) {
  301. u32 val;
  302. unsigned int i, skip;
  303. ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR,
  304. pos & ~3);
  305. if (ret < 0)
  306. break;
  307. vpd->busy = true;
  308. vpd->flag = PCI_VPD_ADDR_F;
  309. ret = pci_vpd_pci22_wait(dev);
  310. if (ret < 0)
  311. break;
  312. ret = pci_user_read_config_dword(dev, vpd->cap + PCI_VPD_DATA, &val);
  313. if (ret < 0)
  314. break;
  315. skip = pos & 3;
  316. for (i = 0; i < sizeof(u32); i++) {
  317. if (i >= skip) {
  318. *buf++ = val;
  319. if (++pos == end)
  320. break;
  321. }
  322. val >>= 8;
  323. }
  324. }
  325. out:
  326. mutex_unlock(&vpd->lock);
  327. return ret ? ret : count;
  328. }
  329. static ssize_t pci_vpd_pci22_write(struct pci_dev *dev, loff_t pos, size_t count,
  330. const void *arg)
  331. {
  332. struct pci_vpd_pci22 *vpd =
  333. container_of(dev->vpd, struct pci_vpd_pci22, base);
  334. const u8 *buf = arg;
  335. loff_t end = pos + count;
  336. int ret = 0;
  337. if (pos < 0 || (pos & 3) || (count & 3) || end > vpd->base.len)
  338. return -EINVAL;
  339. if (mutex_lock_killable(&vpd->lock))
  340. return -EINTR;
  341. ret = pci_vpd_pci22_wait(dev);
  342. if (ret < 0)
  343. goto out;
  344. while (pos < end) {
  345. u32 val;
  346. val = *buf++;
  347. val |= *buf++ << 8;
  348. val |= *buf++ << 16;
  349. val |= *buf++ << 24;
  350. ret = pci_user_write_config_dword(dev, vpd->cap + PCI_VPD_DATA, val);
  351. if (ret < 0)
  352. break;
  353. ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR,
  354. pos | PCI_VPD_ADDR_F);
  355. if (ret < 0)
  356. break;
  357. vpd->busy = true;
  358. vpd->flag = 0;
  359. ret = pci_vpd_pci22_wait(dev);
  360. if (ret < 0)
  361. break;
  362. pos += sizeof(u32);
  363. }
  364. out:
  365. mutex_unlock(&vpd->lock);
  366. return ret ? ret : count;
  367. }
  368. static void pci_vpd_pci22_release(struct pci_dev *dev)
  369. {
  370. kfree(container_of(dev->vpd, struct pci_vpd_pci22, base));
  371. }
  372. static const struct pci_vpd_ops pci_vpd_pci22_ops = {
  373. .read = pci_vpd_pci22_read,
  374. .write = pci_vpd_pci22_write,
  375. .release = pci_vpd_pci22_release,
  376. };
  377. int pci_vpd_pci22_init(struct pci_dev *dev)
  378. {
  379. struct pci_vpd_pci22 *vpd;
  380. u8 cap;
  381. cap = pci_find_capability(dev, PCI_CAP_ID_VPD);
  382. if (!cap)
  383. return -ENODEV;
  384. vpd = kzalloc(sizeof(*vpd), GFP_ATOMIC);
  385. if (!vpd)
  386. return -ENOMEM;
  387. vpd->base.len = PCI_VPD_PCI22_SIZE;
  388. vpd->base.ops = &pci_vpd_pci22_ops;
  389. mutex_init(&vpd->lock);
  390. vpd->cap = cap;
  391. vpd->busy = false;
  392. dev->vpd = &vpd->base;
  393. return 0;
  394. }
  395. /**
  396. * pci_cfg_access_lock - Lock PCI config reads/writes
  397. * @dev: pci device struct
  398. *
  399. * When access is locked, any userspace reads or writes to config
  400. * space and concurrent lock requests will sleep until access is
  401. * allowed via pci_cfg_access_unlocked again.
  402. */
  403. void pci_cfg_access_lock(struct pci_dev *dev)
  404. {
  405. might_sleep();
  406. raw_spin_lock_irq(&pci_lock);
  407. if (dev->block_cfg_access)
  408. pci_wait_cfg(dev);
  409. dev->block_cfg_access = 1;
  410. raw_spin_unlock_irq(&pci_lock);
  411. }
  412. EXPORT_SYMBOL_GPL(pci_cfg_access_lock);
  413. /**
  414. * pci_cfg_access_trylock - try to lock PCI config reads/writes
  415. * @dev: pci device struct
  416. *
  417. * Same as pci_cfg_access_lock, but will return 0 if access is
  418. * already locked, 1 otherwise. This function can be used from
  419. * atomic contexts.
  420. */
  421. bool pci_cfg_access_trylock(struct pci_dev *dev)
  422. {
  423. unsigned long flags;
  424. bool locked = true;
  425. raw_spin_lock_irqsave(&pci_lock, flags);
  426. if (dev->block_cfg_access)
  427. locked = false;
  428. else
  429. dev->block_cfg_access = 1;
  430. raw_spin_unlock_irqrestore(&pci_lock, flags);
  431. return locked;
  432. }
  433. EXPORT_SYMBOL_GPL(pci_cfg_access_trylock);
  434. /**
  435. * pci_cfg_access_unlock - Unlock PCI config reads/writes
  436. * @dev: pci device struct
  437. *
  438. * This function allows PCI config accesses to resume.
  439. */
  440. void pci_cfg_access_unlock(struct pci_dev *dev)
  441. {
  442. unsigned long flags;
  443. raw_spin_lock_irqsave(&pci_lock, flags);
  444. /* This indicates a problem in the caller, but we don't need
  445. * to kill them, unlike a double-block above. */
  446. WARN_ON(!dev->block_cfg_access);
  447. dev->block_cfg_access = 0;
  448. wake_up_all(&pci_cfg_wait);
  449. raw_spin_unlock_irqrestore(&pci_lock, flags);
  450. }
  451. EXPORT_SYMBOL_GPL(pci_cfg_access_unlock);
  452. static inline int pcie_cap_version(const struct pci_dev *dev)
  453. {
  454. return pcie_caps_reg(dev) & PCI_EXP_FLAGS_VERS;
  455. }
  456. bool pcie_cap_has_lnkctl(const struct pci_dev *dev)
  457. {
  458. int type = pci_pcie_type(dev);
  459. return type == PCI_EXP_TYPE_ENDPOINT ||
  460. type == PCI_EXP_TYPE_LEG_END ||
  461. type == PCI_EXP_TYPE_ROOT_PORT ||
  462. type == PCI_EXP_TYPE_UPSTREAM ||
  463. type == PCI_EXP_TYPE_DOWNSTREAM ||
  464. type == PCI_EXP_TYPE_PCI_BRIDGE ||
  465. type == PCI_EXP_TYPE_PCIE_BRIDGE;
  466. }
  467. static inline bool pcie_cap_has_sltctl(const struct pci_dev *dev)
  468. {
  469. int type = pci_pcie_type(dev);
  470. return (type == PCI_EXP_TYPE_ROOT_PORT ||
  471. type == PCI_EXP_TYPE_DOWNSTREAM) &&
  472. pcie_caps_reg(dev) & PCI_EXP_FLAGS_SLOT;
  473. }
  474. static inline bool pcie_cap_has_rtctl(const struct pci_dev *dev)
  475. {
  476. int type = pci_pcie_type(dev);
  477. return type == PCI_EXP_TYPE_ROOT_PORT ||
  478. type == PCI_EXP_TYPE_RC_EC;
  479. }
  480. static bool pcie_capability_reg_implemented(struct pci_dev *dev, int pos)
  481. {
  482. if (!pci_is_pcie(dev))
  483. return false;
  484. switch (pos) {
  485. case PCI_EXP_FLAGS:
  486. return true;
  487. case PCI_EXP_DEVCAP:
  488. case PCI_EXP_DEVCTL:
  489. case PCI_EXP_DEVSTA:
  490. return true;
  491. case PCI_EXP_LNKCAP:
  492. case PCI_EXP_LNKCTL:
  493. case PCI_EXP_LNKSTA:
  494. return pcie_cap_has_lnkctl(dev);
  495. case PCI_EXP_SLTCAP:
  496. case PCI_EXP_SLTCTL:
  497. case PCI_EXP_SLTSTA:
  498. return pcie_cap_has_sltctl(dev);
  499. case PCI_EXP_RTCTL:
  500. case PCI_EXP_RTCAP:
  501. case PCI_EXP_RTSTA:
  502. return pcie_cap_has_rtctl(dev);
  503. case PCI_EXP_DEVCAP2:
  504. case PCI_EXP_DEVCTL2:
  505. case PCI_EXP_LNKCAP2:
  506. case PCI_EXP_LNKCTL2:
  507. case PCI_EXP_LNKSTA2:
  508. return pcie_cap_version(dev) > 1;
  509. default:
  510. return false;
  511. }
  512. }
  513. /*
  514. * Note that these accessor functions are only for the "PCI Express
  515. * Capability" (see PCIe spec r3.0, sec 7.8). They do not apply to the
  516. * other "PCI Express Extended Capabilities" (AER, VC, ACS, MFVC, etc.)
  517. */
  518. int pcie_capability_read_word(struct pci_dev *dev, int pos, u16 *val)
  519. {
  520. int ret;
  521. *val = 0;
  522. if (pos & 1)
  523. return -EINVAL;
  524. if (pcie_capability_reg_implemented(dev, pos)) {
  525. ret = pci_read_config_word(dev, pci_pcie_cap(dev) + pos, val);
  526. /*
  527. * Reset *val to 0 if pci_read_config_word() fails, it may
  528. * have been written as 0xFFFF if hardware error happens
  529. * during pci_read_config_word().
  530. */
  531. if (ret)
  532. *val = 0;
  533. return ret;
  534. }
  535. /*
  536. * For Functions that do not implement the Slot Capabilities,
  537. * Slot Status, and Slot Control registers, these spaces must
  538. * be hardwired to 0b, with the exception of the Presence Detect
  539. * State bit in the Slot Status register of Downstream Ports,
  540. * which must be hardwired to 1b. (PCIe Base Spec 3.0, sec 7.8)
  541. */
  542. if (pci_is_pcie(dev) && pos == PCI_EXP_SLTSTA &&
  543. pci_pcie_type(dev) == PCI_EXP_TYPE_DOWNSTREAM) {
  544. *val = PCI_EXP_SLTSTA_PDS;
  545. }
  546. return 0;
  547. }
  548. EXPORT_SYMBOL(pcie_capability_read_word);
  549. int pcie_capability_read_dword(struct pci_dev *dev, int pos, u32 *val)
  550. {
  551. int ret;
  552. *val = 0;
  553. if (pos & 3)
  554. return -EINVAL;
  555. if (pcie_capability_reg_implemented(dev, pos)) {
  556. ret = pci_read_config_dword(dev, pci_pcie_cap(dev) + pos, val);
  557. /*
  558. * Reset *val to 0 if pci_read_config_dword() fails, it may
  559. * have been written as 0xFFFFFFFF if hardware error happens
  560. * during pci_read_config_dword().
  561. */
  562. if (ret)
  563. *val = 0;
  564. return ret;
  565. }
  566. if (pci_is_pcie(dev) && pos == PCI_EXP_SLTCTL &&
  567. pci_pcie_type(dev) == PCI_EXP_TYPE_DOWNSTREAM) {
  568. *val = PCI_EXP_SLTSTA_PDS;
  569. }
  570. return 0;
  571. }
  572. EXPORT_SYMBOL(pcie_capability_read_dword);
  573. int pcie_capability_write_word(struct pci_dev *dev, int pos, u16 val)
  574. {
  575. if (pos & 1)
  576. return -EINVAL;
  577. if (!pcie_capability_reg_implemented(dev, pos))
  578. return 0;
  579. return pci_write_config_word(dev, pci_pcie_cap(dev) + pos, val);
  580. }
  581. EXPORT_SYMBOL(pcie_capability_write_word);
  582. int pcie_capability_write_dword(struct pci_dev *dev, int pos, u32 val)
  583. {
  584. if (pos & 3)
  585. return -EINVAL;
  586. if (!pcie_capability_reg_implemented(dev, pos))
  587. return 0;
  588. return pci_write_config_dword(dev, pci_pcie_cap(dev) + pos, val);
  589. }
  590. EXPORT_SYMBOL(pcie_capability_write_dword);
  591. int pcie_capability_clear_and_set_word(struct pci_dev *dev, int pos,
  592. u16 clear, u16 set)
  593. {
  594. int ret;
  595. u16 val;
  596. ret = pcie_capability_read_word(dev, pos, &val);
  597. if (!ret) {
  598. val &= ~clear;
  599. val |= set;
  600. ret = pcie_capability_write_word(dev, pos, val);
  601. }
  602. return ret;
  603. }
  604. EXPORT_SYMBOL(pcie_capability_clear_and_set_word);
  605. int pcie_capability_clear_and_set_dword(struct pci_dev *dev, int pos,
  606. u32 clear, u32 set)
  607. {
  608. int ret;
  609. u32 val;
  610. ret = pcie_capability_read_dword(dev, pos, &val);
  611. if (!ret) {
  612. val &= ~clear;
  613. val |= set;
  614. ret = pcie_capability_write_dword(dev, pos, val);
  615. }
  616. return ret;
  617. }
  618. EXPORT_SYMBOL(pcie_capability_clear_and_set_dword);