ipmi_si_pci.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * ipmi_si_pci.c
  4. *
  5. * Handling for IPMI devices on the PCI bus.
  6. */
  7. #include <linux/module.h>
  8. #include <linux/pci.h>
  9. #include "ipmi_si.h"
  10. #define PFX "ipmi_pci: "
  11. static bool pci_registered;
  12. static bool si_trypci = true;
  13. module_param_named(trypci, si_trypci, bool, 0);
  14. MODULE_PARM_DESC(trypci, "Setting this to zero will disable the"
  15. " default scan of the interfaces identified via pci");
  16. #define PCI_CLASS_SERIAL_IPMI 0x0c07
  17. #define PCI_CLASS_SERIAL_IPMI_SMIC 0x0c0700
  18. #define PCI_CLASS_SERIAL_IPMI_KCS 0x0c0701
  19. #define PCI_CLASS_SERIAL_IPMI_BT 0x0c0702
  20. #define PCI_DEVICE_ID_HP_MMC 0x121A
  21. static void ipmi_pci_cleanup(struct si_sm_io *io)
  22. {
  23. struct pci_dev *pdev = io->addr_source_data;
  24. pci_disable_device(pdev);
  25. }
  26. static int ipmi_pci_probe_regspacing(struct si_sm_io *io)
  27. {
  28. if (io->si_type == SI_KCS) {
  29. unsigned char status;
  30. int regspacing;
  31. io->regsize = DEFAULT_REGSIZE;
  32. io->regshift = 0;
  33. /* detect 1, 4, 16byte spacing */
  34. for (regspacing = DEFAULT_REGSPACING; regspacing <= 16;) {
  35. io->regspacing = regspacing;
  36. if (io->io_setup(io)) {
  37. dev_err(io->dev,
  38. "Could not setup I/O space\n");
  39. return DEFAULT_REGSPACING;
  40. }
  41. /* write invalid cmd */
  42. io->outputb(io, 1, 0x10);
  43. /* read status back */
  44. status = io->inputb(io, 1);
  45. io->io_cleanup(io);
  46. if (status)
  47. return regspacing;
  48. regspacing *= 4;
  49. }
  50. }
  51. return DEFAULT_REGSPACING;
  52. }
  53. static struct pci_device_id ipmi_pci_blacklist[] = {
  54. /*
  55. * This is a "Virtual IPMI device", whatever that is. It appears
  56. * as a KCS device by the class, but it is not one.
  57. */
  58. { PCI_VDEVICE(REALTEK, 0x816c) },
  59. { 0, }
  60. };
  61. static int ipmi_pci_probe(struct pci_dev *pdev,
  62. const struct pci_device_id *ent)
  63. {
  64. int rv;
  65. struct si_sm_io io;
  66. if (pci_match_id(ipmi_pci_blacklist, pdev))
  67. return -ENODEV;
  68. memset(&io, 0, sizeof(io));
  69. io.addr_source = SI_PCI;
  70. dev_info(&pdev->dev, "probing via PCI");
  71. switch (pdev->class) {
  72. case PCI_CLASS_SERIAL_IPMI_SMIC:
  73. io.si_type = SI_SMIC;
  74. break;
  75. case PCI_CLASS_SERIAL_IPMI_KCS:
  76. io.si_type = SI_KCS;
  77. break;
  78. case PCI_CLASS_SERIAL_IPMI_BT:
  79. io.si_type = SI_BT;
  80. break;
  81. default:
  82. dev_info(&pdev->dev, "Unknown IPMI class: %x\n", pdev->class);
  83. return -ENOMEM;
  84. }
  85. rv = pci_enable_device(pdev);
  86. if (rv) {
  87. dev_err(&pdev->dev, "couldn't enable PCI device\n");
  88. return rv;
  89. }
  90. io.addr_source_cleanup = ipmi_pci_cleanup;
  91. io.addr_source_data = pdev;
  92. if (pci_resource_flags(pdev, 0) & IORESOURCE_IO) {
  93. io.addr_type = IPMI_IO_ADDR_SPACE;
  94. io.io_setup = ipmi_si_port_setup;
  95. } else {
  96. io.addr_type = IPMI_MEM_ADDR_SPACE;
  97. io.io_setup = ipmi_si_mem_setup;
  98. }
  99. io.addr_data = pci_resource_start(pdev, 0);
  100. io.regspacing = ipmi_pci_probe_regspacing(&io);
  101. io.regsize = DEFAULT_REGSIZE;
  102. io.regshift = 0;
  103. io.irq = pdev->irq;
  104. if (io.irq)
  105. io.irq_setup = ipmi_std_irq_setup;
  106. io.dev = &pdev->dev;
  107. dev_info(&pdev->dev, "%pR regsize %d spacing %d irq %d\n",
  108. &pdev->resource[0], io.regsize, io.regspacing, io.irq);
  109. rv = ipmi_si_add_smi(&io);
  110. if (rv)
  111. pci_disable_device(pdev);
  112. return rv;
  113. }
  114. static void ipmi_pci_remove(struct pci_dev *pdev)
  115. {
  116. ipmi_si_remove_by_dev(&pdev->dev);
  117. }
  118. static const struct pci_device_id ipmi_pci_devices[] = {
  119. { PCI_VDEVICE(HP, PCI_DEVICE_ID_HP_MMC) },
  120. { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_IPMI_SMIC, ~0) },
  121. { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_IPMI_KCS, ~0) },
  122. { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_IPMI_BT, ~0) },
  123. { 0, }
  124. };
  125. MODULE_DEVICE_TABLE(pci, ipmi_pci_devices);
  126. static struct pci_driver ipmi_pci_driver = {
  127. .name = DEVICE_NAME,
  128. .id_table = ipmi_pci_devices,
  129. .probe = ipmi_pci_probe,
  130. .remove = ipmi_pci_remove,
  131. };
  132. void ipmi_si_pci_init(void)
  133. {
  134. if (si_trypci) {
  135. int rv = pci_register_driver(&ipmi_pci_driver);
  136. if (rv)
  137. pr_err(PFX "Unable to register PCI driver: %d\n", rv);
  138. else
  139. pci_registered = true;
  140. }
  141. }
  142. void ipmi_si_pci_shutdown(void)
  143. {
  144. if (pci_registered)
  145. pci_unregister_driver(&ipmi_pci_driver);
  146. }