access.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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. static ssize_t pci_vpd_f0_read(struct pci_dev *dev, loff_t pos, size_t count,
  378. void *arg)
  379. {
  380. struct pci_dev *tdev = pci_get_slot(dev->bus,
  381. PCI_DEVFN(PCI_SLOT(dev->devfn), 0));
  382. ssize_t ret;
  383. if (!tdev)
  384. return -ENODEV;
  385. ret = pci_read_vpd(tdev, pos, count, arg);
  386. pci_dev_put(tdev);
  387. return ret;
  388. }
  389. static ssize_t pci_vpd_f0_write(struct pci_dev *dev, loff_t pos, size_t count,
  390. const void *arg)
  391. {
  392. struct pci_dev *tdev = pci_get_slot(dev->bus,
  393. PCI_DEVFN(PCI_SLOT(dev->devfn), 0));
  394. ssize_t ret;
  395. if (!tdev)
  396. return -ENODEV;
  397. ret = pci_write_vpd(tdev, pos, count, arg);
  398. pci_dev_put(tdev);
  399. return ret;
  400. }
  401. static const struct pci_vpd_ops pci_vpd_f0_ops = {
  402. .read = pci_vpd_f0_read,
  403. .write = pci_vpd_f0_write,
  404. .release = pci_vpd_pci22_release,
  405. };
  406. int pci_vpd_pci22_init(struct pci_dev *dev)
  407. {
  408. struct pci_vpd_pci22 *vpd;
  409. u8 cap;
  410. cap = pci_find_capability(dev, PCI_CAP_ID_VPD);
  411. if (!cap)
  412. return -ENODEV;
  413. vpd = kzalloc(sizeof(*vpd), GFP_ATOMIC);
  414. if (!vpd)
  415. return -ENOMEM;
  416. vpd->base.len = PCI_VPD_PCI22_SIZE;
  417. if (dev->dev_flags & PCI_DEV_FLAGS_VPD_REF_F0)
  418. vpd->base.ops = &pci_vpd_f0_ops;
  419. else
  420. vpd->base.ops = &pci_vpd_pci22_ops;
  421. mutex_init(&vpd->lock);
  422. vpd->cap = cap;
  423. vpd->busy = false;
  424. dev->vpd = &vpd->base;
  425. return 0;
  426. }
  427. /**
  428. * pci_cfg_access_lock - Lock PCI config reads/writes
  429. * @dev: pci device struct
  430. *
  431. * When access is locked, any userspace reads or writes to config
  432. * space and concurrent lock requests will sleep until access is
  433. * allowed via pci_cfg_access_unlocked again.
  434. */
  435. void pci_cfg_access_lock(struct pci_dev *dev)
  436. {
  437. might_sleep();
  438. raw_spin_lock_irq(&pci_lock);
  439. if (dev->block_cfg_access)
  440. pci_wait_cfg(dev);
  441. dev->block_cfg_access = 1;
  442. raw_spin_unlock_irq(&pci_lock);
  443. }
  444. EXPORT_SYMBOL_GPL(pci_cfg_access_lock);
  445. /**
  446. * pci_cfg_access_trylock - try to lock PCI config reads/writes
  447. * @dev: pci device struct
  448. *
  449. * Same as pci_cfg_access_lock, but will return 0 if access is
  450. * already locked, 1 otherwise. This function can be used from
  451. * atomic contexts.
  452. */
  453. bool pci_cfg_access_trylock(struct pci_dev *dev)
  454. {
  455. unsigned long flags;
  456. bool locked = true;
  457. raw_spin_lock_irqsave(&pci_lock, flags);
  458. if (dev->block_cfg_access)
  459. locked = false;
  460. else
  461. dev->block_cfg_access = 1;
  462. raw_spin_unlock_irqrestore(&pci_lock, flags);
  463. return locked;
  464. }
  465. EXPORT_SYMBOL_GPL(pci_cfg_access_trylock);
  466. /**
  467. * pci_cfg_access_unlock - Unlock PCI config reads/writes
  468. * @dev: pci device struct
  469. *
  470. * This function allows PCI config accesses to resume.
  471. */
  472. void pci_cfg_access_unlock(struct pci_dev *dev)
  473. {
  474. unsigned long flags;
  475. raw_spin_lock_irqsave(&pci_lock, flags);
  476. /* This indicates a problem in the caller, but we don't need
  477. * to kill them, unlike a double-block above. */
  478. WARN_ON(!dev->block_cfg_access);
  479. dev->block_cfg_access = 0;
  480. wake_up_all(&pci_cfg_wait);
  481. raw_spin_unlock_irqrestore(&pci_lock, flags);
  482. }
  483. EXPORT_SYMBOL_GPL(pci_cfg_access_unlock);
  484. static inline int pcie_cap_version(const struct pci_dev *dev)
  485. {
  486. return pcie_caps_reg(dev) & PCI_EXP_FLAGS_VERS;
  487. }
  488. static bool pcie_downstream_port(const struct pci_dev *dev)
  489. {
  490. int type = pci_pcie_type(dev);
  491. return type == PCI_EXP_TYPE_ROOT_PORT ||
  492. type == PCI_EXP_TYPE_DOWNSTREAM;
  493. }
  494. bool pcie_cap_has_lnkctl(const struct pci_dev *dev)
  495. {
  496. int type = pci_pcie_type(dev);
  497. return type == PCI_EXP_TYPE_ENDPOINT ||
  498. type == PCI_EXP_TYPE_LEG_END ||
  499. type == PCI_EXP_TYPE_ROOT_PORT ||
  500. type == PCI_EXP_TYPE_UPSTREAM ||
  501. type == PCI_EXP_TYPE_DOWNSTREAM ||
  502. type == PCI_EXP_TYPE_PCI_BRIDGE ||
  503. type == PCI_EXP_TYPE_PCIE_BRIDGE;
  504. }
  505. static inline bool pcie_cap_has_sltctl(const struct pci_dev *dev)
  506. {
  507. return pcie_downstream_port(dev) &&
  508. pcie_caps_reg(dev) & PCI_EXP_FLAGS_SLOT;
  509. }
  510. static inline bool pcie_cap_has_rtctl(const struct pci_dev *dev)
  511. {
  512. int type = pci_pcie_type(dev);
  513. return type == PCI_EXP_TYPE_ROOT_PORT ||
  514. type == PCI_EXP_TYPE_RC_EC;
  515. }
  516. static bool pcie_capability_reg_implemented(struct pci_dev *dev, int pos)
  517. {
  518. if (!pci_is_pcie(dev))
  519. return false;
  520. switch (pos) {
  521. case PCI_EXP_FLAGS:
  522. return true;
  523. case PCI_EXP_DEVCAP:
  524. case PCI_EXP_DEVCTL:
  525. case PCI_EXP_DEVSTA:
  526. return true;
  527. case PCI_EXP_LNKCAP:
  528. case PCI_EXP_LNKCTL:
  529. case PCI_EXP_LNKSTA:
  530. return pcie_cap_has_lnkctl(dev);
  531. case PCI_EXP_SLTCAP:
  532. case PCI_EXP_SLTCTL:
  533. case PCI_EXP_SLTSTA:
  534. return pcie_cap_has_sltctl(dev);
  535. case PCI_EXP_RTCTL:
  536. case PCI_EXP_RTCAP:
  537. case PCI_EXP_RTSTA:
  538. return pcie_cap_has_rtctl(dev);
  539. case PCI_EXP_DEVCAP2:
  540. case PCI_EXP_DEVCTL2:
  541. case PCI_EXP_LNKCAP2:
  542. case PCI_EXP_LNKCTL2:
  543. case PCI_EXP_LNKSTA2:
  544. return pcie_cap_version(dev) > 1;
  545. default:
  546. return false;
  547. }
  548. }
  549. /*
  550. * Note that these accessor functions are only for the "PCI Express
  551. * Capability" (see PCIe spec r3.0, sec 7.8). They do not apply to the
  552. * other "PCI Express Extended Capabilities" (AER, VC, ACS, MFVC, etc.)
  553. */
  554. int pcie_capability_read_word(struct pci_dev *dev, int pos, u16 *val)
  555. {
  556. int ret;
  557. *val = 0;
  558. if (pos & 1)
  559. return -EINVAL;
  560. if (pcie_capability_reg_implemented(dev, pos)) {
  561. ret = pci_read_config_word(dev, pci_pcie_cap(dev) + pos, val);
  562. /*
  563. * Reset *val to 0 if pci_read_config_word() fails, it may
  564. * have been written as 0xFFFF if hardware error happens
  565. * during pci_read_config_word().
  566. */
  567. if (ret)
  568. *val = 0;
  569. return ret;
  570. }
  571. /*
  572. * For Functions that do not implement the Slot Capabilities,
  573. * Slot Status, and Slot Control registers, these spaces must
  574. * be hardwired to 0b, with the exception of the Presence Detect
  575. * State bit in the Slot Status register of Downstream Ports,
  576. * which must be hardwired to 1b. (PCIe Base Spec 3.0, sec 7.8)
  577. */
  578. if (pci_is_pcie(dev) && pcie_downstream_port(dev) &&
  579. pos == PCI_EXP_SLTSTA)
  580. *val = PCI_EXP_SLTSTA_PDS;
  581. return 0;
  582. }
  583. EXPORT_SYMBOL(pcie_capability_read_word);
  584. int pcie_capability_read_dword(struct pci_dev *dev, int pos, u32 *val)
  585. {
  586. int ret;
  587. *val = 0;
  588. if (pos & 3)
  589. return -EINVAL;
  590. if (pcie_capability_reg_implemented(dev, pos)) {
  591. ret = pci_read_config_dword(dev, pci_pcie_cap(dev) + pos, val);
  592. /*
  593. * Reset *val to 0 if pci_read_config_dword() fails, it may
  594. * have been written as 0xFFFFFFFF if hardware error happens
  595. * during pci_read_config_dword().
  596. */
  597. if (ret)
  598. *val = 0;
  599. return ret;
  600. }
  601. if (pci_is_pcie(dev) && pcie_downstream_port(dev) &&
  602. pos == PCI_EXP_SLTSTA)
  603. *val = PCI_EXP_SLTSTA_PDS;
  604. return 0;
  605. }
  606. EXPORT_SYMBOL(pcie_capability_read_dword);
  607. int pcie_capability_write_word(struct pci_dev *dev, int pos, u16 val)
  608. {
  609. if (pos & 1)
  610. return -EINVAL;
  611. if (!pcie_capability_reg_implemented(dev, pos))
  612. return 0;
  613. return pci_write_config_word(dev, pci_pcie_cap(dev) + pos, val);
  614. }
  615. EXPORT_SYMBOL(pcie_capability_write_word);
  616. int pcie_capability_write_dword(struct pci_dev *dev, int pos, u32 val)
  617. {
  618. if (pos & 3)
  619. return -EINVAL;
  620. if (!pcie_capability_reg_implemented(dev, pos))
  621. return 0;
  622. return pci_write_config_dword(dev, pci_pcie_cap(dev) + pos, val);
  623. }
  624. EXPORT_SYMBOL(pcie_capability_write_dword);
  625. int pcie_capability_clear_and_set_word(struct pci_dev *dev, int pos,
  626. u16 clear, u16 set)
  627. {
  628. int ret;
  629. u16 val;
  630. ret = pcie_capability_read_word(dev, pos, &val);
  631. if (!ret) {
  632. val &= ~clear;
  633. val |= set;
  634. ret = pcie_capability_write_word(dev, pos, val);
  635. }
  636. return ret;
  637. }
  638. EXPORT_SYMBOL(pcie_capability_clear_and_set_word);
  639. int pcie_capability_clear_and_set_dword(struct pci_dev *dev, int pos,
  640. u32 clear, u32 set)
  641. {
  642. int ret;
  643. u32 val;
  644. ret = pcie_capability_read_dword(dev, pos, &val);
  645. if (!ret) {
  646. val &= ~clear;
  647. val |= set;
  648. ret = pcie_capability_write_dword(dev, pos, val);
  649. }
  650. return ret;
  651. }
  652. EXPORT_SYMBOL(pcie_capability_clear_and_set_dword);