port.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright(c) 2022 Intel Corporation. All rights reserved. */
  3. #include <linux/device.h>
  4. #include <linux/module.h>
  5. #include <linux/slab.h>
  6. #include "cxlmem.h"
  7. #include "cxlpci.h"
  8. /**
  9. * DOC: cxl port
  10. *
  11. * The port driver enumerates dport via PCI and scans for HDM
  12. * (Host-managed-Device-Memory) decoder resources via the
  13. * @component_reg_phys value passed in by the agent that registered the
  14. * port. All descendant ports of a CXL root port (described by platform
  15. * firmware) are managed in this drivers context. Each driver instance
  16. * is responsible for tearing down the driver context of immediate
  17. * descendant ports. The locking for this is validated by
  18. * CONFIG_PROVE_CXL_LOCKING.
  19. *
  20. * The primary service this driver provides is presenting APIs to other
  21. * drivers to utilize the decoders, and indicating to userspace (via bind
  22. * status) the connectivity of the CXL.mem protocol throughout the
  23. * PCIe topology.
  24. */
  25. static void schedule_detach(void *cxlmd)
  26. {
  27. schedule_cxl_memdev_detach(cxlmd);
  28. }
  29. static int cxl_port_probe(struct device *dev)
  30. {
  31. struct cxl_port *port = to_cxl_port(dev);
  32. struct cxl_hdm *cxlhdm;
  33. int rc;
  34. if (!is_cxl_endpoint(port)) {
  35. rc = devm_cxl_port_enumerate_dports(port);
  36. if (rc < 0)
  37. return rc;
  38. if (rc == 1)
  39. return devm_cxl_add_passthrough_decoder(port);
  40. }
  41. cxlhdm = devm_cxl_setup_hdm(port);
  42. if (IS_ERR(cxlhdm))
  43. return PTR_ERR(cxlhdm);
  44. if (is_cxl_endpoint(port)) {
  45. struct cxl_memdev *cxlmd = to_cxl_memdev(port->uport);
  46. struct cxl_dev_state *cxlds = cxlmd->cxlds;
  47. /* Cache the data early to ensure is_visible() works */
  48. read_cdat_data(port);
  49. get_device(&cxlmd->dev);
  50. rc = devm_add_action_or_reset(dev, schedule_detach, cxlmd);
  51. if (rc)
  52. return rc;
  53. rc = cxl_hdm_decode_init(cxlds, cxlhdm);
  54. if (rc)
  55. return rc;
  56. rc = cxl_await_media_ready(cxlds);
  57. if (rc) {
  58. dev_err(dev, "Media not active (%d)\n", rc);
  59. return rc;
  60. }
  61. }
  62. rc = devm_cxl_enumerate_decoders(cxlhdm);
  63. if (rc) {
  64. dev_err(dev, "Couldn't enumerate decoders (%d)\n", rc);
  65. return rc;
  66. }
  67. return 0;
  68. }
  69. static ssize_t CDAT_read(struct file *filp, struct kobject *kobj,
  70. struct bin_attribute *bin_attr, char *buf,
  71. loff_t offset, size_t count)
  72. {
  73. struct device *dev = kobj_to_dev(kobj);
  74. struct cxl_port *port = to_cxl_port(dev);
  75. if (!port->cdat_available)
  76. return -ENXIO;
  77. if (!port->cdat.table)
  78. return 0;
  79. return memory_read_from_buffer(buf, count, &offset,
  80. port->cdat.table,
  81. port->cdat.length);
  82. }
  83. static BIN_ATTR_ADMIN_RO(CDAT, 0);
  84. static umode_t cxl_port_bin_attr_is_visible(struct kobject *kobj,
  85. struct bin_attribute *attr, int i)
  86. {
  87. struct device *dev = kobj_to_dev(kobj);
  88. struct cxl_port *port = to_cxl_port(dev);
  89. if ((attr == &bin_attr_CDAT) && port->cdat_available)
  90. return attr->attr.mode;
  91. return 0;
  92. }
  93. static struct bin_attribute *cxl_cdat_bin_attributes[] = {
  94. &bin_attr_CDAT,
  95. NULL,
  96. };
  97. static struct attribute_group cxl_cdat_attribute_group = {
  98. .bin_attrs = cxl_cdat_bin_attributes,
  99. .is_bin_visible = cxl_port_bin_attr_is_visible,
  100. };
  101. static const struct attribute_group *cxl_port_attribute_groups[] = {
  102. &cxl_cdat_attribute_group,
  103. NULL,
  104. };
  105. static struct cxl_driver cxl_port_driver = {
  106. .name = "cxl_port",
  107. .probe = cxl_port_probe,
  108. .id = CXL_DEVICE_PORT,
  109. .drv = {
  110. .dev_groups = cxl_port_attribute_groups,
  111. },
  112. };
  113. module_cxl_driver(cxl_port_driver);
  114. MODULE_LICENSE("GPL v2");
  115. MODULE_IMPORT_NS(CXL);
  116. MODULE_ALIAS_CXL(CXL_DEVICE_PORT);