access.c 19 KB

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