pciehp.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * PCI Express Hot Plug Controller Driver
  4. *
  5. * Copyright (C) 1995,2001 Compaq Computer Corporation
  6. * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
  7. * Copyright (C) 2001 IBM Corp.
  8. * Copyright (C) 2003-2004 Intel Corporation
  9. *
  10. * All rights reserved.
  11. *
  12. * Send feedback to <greg@kroah.com>, <kristen.c.accardi@intel.com>
  13. *
  14. */
  15. #ifndef _PCIEHP_H
  16. #define _PCIEHP_H
  17. #include <linux/types.h>
  18. #include <linux/pci.h>
  19. #include <linux/pci_hotplug.h>
  20. #include <linux/delay.h>
  21. #include <linux/sched/signal.h> /* signal_pending() */
  22. #include <linux/mutex.h>
  23. #include <linux/rwsem.h>
  24. #include <linux/workqueue.h>
  25. #include "../pcie/portdrv.h"
  26. #define MY_NAME "pciehp"
  27. extern bool pciehp_poll_mode;
  28. extern int pciehp_poll_time;
  29. extern bool pciehp_debug;
  30. #define dbg(format, arg...) \
  31. do { \
  32. if (pciehp_debug) \
  33. printk(KERN_DEBUG "%s: " format, MY_NAME, ## arg); \
  34. } while (0)
  35. #define err(format, arg...) \
  36. printk(KERN_ERR "%s: " format, MY_NAME, ## arg)
  37. #define info(format, arg...) \
  38. printk(KERN_INFO "%s: " format, MY_NAME, ## arg)
  39. #define warn(format, arg...) \
  40. printk(KERN_WARNING "%s: " format, MY_NAME, ## arg)
  41. #define ctrl_dbg(ctrl, format, arg...) \
  42. do { \
  43. if (pciehp_debug) \
  44. dev_printk(KERN_DEBUG, &ctrl->pcie->device, \
  45. format, ## arg); \
  46. } while (0)
  47. #define ctrl_err(ctrl, format, arg...) \
  48. dev_err(&ctrl->pcie->device, format, ## arg)
  49. #define ctrl_info(ctrl, format, arg...) \
  50. dev_info(&ctrl->pcie->device, format, ## arg)
  51. #define ctrl_warn(ctrl, format, arg...) \
  52. dev_warn(&ctrl->pcie->device, format, ## arg)
  53. #define SLOT_NAME_SIZE 10
  54. /**
  55. * struct slot - PCIe hotplug slot
  56. * @state: current state machine position
  57. * @ctrl: pointer to the slot's controller structure
  58. * @hotplug_slot: pointer to the structure registered with the PCI hotplug core
  59. * @work: work item to turn the slot on or off after 5 seconds in response to
  60. * an Attention Button press
  61. * @lock: protects reads and writes of @state;
  62. * protects scheduling, execution and cancellation of @work
  63. */
  64. struct slot {
  65. u8 state;
  66. struct controller *ctrl;
  67. struct hotplug_slot *hotplug_slot;
  68. struct delayed_work work;
  69. struct mutex lock;
  70. };
  71. /**
  72. * struct controller - PCIe hotplug controller
  73. * @ctrl_lock: serializes writes to the Slot Control register
  74. * @pcie: pointer to the controller's PCIe port service device
  75. * @reset_lock: prevents access to the Data Link Layer Link Active bit in the
  76. * Link Status register and to the Presence Detect State bit in the Slot
  77. * Status register during a slot reset which may cause them to flap
  78. * @slot: pointer to the controller's slot structure
  79. * @queue: wait queue to wake up on reception of a Command Completed event,
  80. * used for synchronous writes to the Slot Control register
  81. * @slot_cap: cached copy of the Slot Capabilities register
  82. * @slot_ctrl: cached copy of the Slot Control register
  83. * @poll_thread: thread to poll for slot events if no IRQ is available,
  84. * enabled with pciehp_poll_mode module parameter
  85. * @cmd_started: jiffies when the Slot Control register was last written;
  86. * the next write is allowed 1 second later, absent a Command Completed
  87. * interrupt (PCIe r4.0, sec 6.7.3.2)
  88. * @cmd_busy: flag set on Slot Control register write, cleared by IRQ handler
  89. * on reception of a Command Completed event
  90. * @link_active_reporting: cached copy of Data Link Layer Link Active Reporting
  91. * Capable bit in Link Capabilities register; if this bit is zero, the
  92. * Data Link Layer Link Active bit in the Link Status register will never
  93. * be set and the driver is thus confined to wait 1 second before assuming
  94. * the link to a hotplugged device is up and accessing it
  95. * @notification_enabled: whether the IRQ was requested successfully
  96. * @power_fault_detected: whether a power fault was detected by the hardware
  97. * that has not yet been cleared by the user
  98. * @pending_events: used by the IRQ handler to save events retrieved from the
  99. * Slot Status register for later consumption by the IRQ thread
  100. * @request_result: result of last user request submitted to the IRQ thread
  101. * @requester: wait queue to wake up on completion of user request,
  102. * used for synchronous slot enable/disable request via sysfs
  103. */
  104. struct controller {
  105. struct mutex ctrl_lock;
  106. struct pcie_device *pcie;
  107. struct rw_semaphore reset_lock;
  108. struct slot *slot;
  109. wait_queue_head_t queue;
  110. u32 slot_cap;
  111. u16 slot_ctrl;
  112. struct task_struct *poll_thread;
  113. unsigned long cmd_started; /* jiffies */
  114. unsigned int cmd_busy:1;
  115. unsigned int link_active_reporting:1;
  116. unsigned int notification_enabled:1;
  117. unsigned int power_fault_detected;
  118. atomic_t pending_events;
  119. int request_result;
  120. wait_queue_head_t requester;
  121. };
  122. /**
  123. * DOC: Slot state
  124. *
  125. * @OFF_STATE: slot is powered off, no subordinate devices are enumerated
  126. * @BLINKINGON_STATE: slot will be powered on after the 5 second delay,
  127. * green led is blinking
  128. * @BLINKINGOFF_STATE: slot will be powered off after the 5 second delay,
  129. * green led is blinking
  130. * @POWERON_STATE: slot is currently powering on
  131. * @POWEROFF_STATE: slot is currently powering off
  132. * @ON_STATE: slot is powered on, subordinate devices have been enumerated
  133. */
  134. #define OFF_STATE 0
  135. #define BLINKINGON_STATE 1
  136. #define BLINKINGOFF_STATE 2
  137. #define POWERON_STATE 3
  138. #define POWEROFF_STATE 4
  139. #define ON_STATE 5
  140. /**
  141. * DOC: Flags to request an action from the IRQ thread
  142. *
  143. * These are stored together with events read from the Slot Status register,
  144. * hence must be greater than its 16-bit width.
  145. *
  146. * %DISABLE_SLOT: Disable the slot in response to a user request via sysfs or
  147. * an Attention Button press after the 5 second delay
  148. * %RERUN_ISR: Used by the IRQ handler to inform the IRQ thread that the
  149. * hotplug port was inaccessible when the interrupt occurred, requiring
  150. * that the IRQ handler is rerun by the IRQ thread after it has made the
  151. * hotplug port accessible by runtime resuming its parents to D0
  152. */
  153. #define DISABLE_SLOT (1 << 16)
  154. #define RERUN_ISR (1 << 17)
  155. #define ATTN_BUTTN(ctrl) ((ctrl)->slot_cap & PCI_EXP_SLTCAP_ABP)
  156. #define POWER_CTRL(ctrl) ((ctrl)->slot_cap & PCI_EXP_SLTCAP_PCP)
  157. #define MRL_SENS(ctrl) ((ctrl)->slot_cap & PCI_EXP_SLTCAP_MRLSP)
  158. #define ATTN_LED(ctrl) ((ctrl)->slot_cap & PCI_EXP_SLTCAP_AIP)
  159. #define PWR_LED(ctrl) ((ctrl)->slot_cap & PCI_EXP_SLTCAP_PIP)
  160. #define HP_SUPR_RM(ctrl) ((ctrl)->slot_cap & PCI_EXP_SLTCAP_HPS)
  161. #define EMI(ctrl) ((ctrl)->slot_cap & PCI_EXP_SLTCAP_EIP)
  162. #define NO_CMD_CMPL(ctrl) ((ctrl)->slot_cap & PCI_EXP_SLTCAP_NCCS)
  163. #define PSN(ctrl) (((ctrl)->slot_cap & PCI_EXP_SLTCAP_PSN) >> 19)
  164. int pciehp_sysfs_enable_slot(struct slot *slot);
  165. int pciehp_sysfs_disable_slot(struct slot *slot);
  166. void pciehp_request(struct controller *ctrl, int action);
  167. void pciehp_handle_button_press(struct slot *slot);
  168. void pciehp_handle_disable_request(struct slot *slot);
  169. void pciehp_handle_presence_or_link_change(struct slot *slot, u32 events);
  170. int pciehp_configure_device(struct slot *p_slot);
  171. void pciehp_unconfigure_device(struct slot *p_slot);
  172. void pciehp_queue_pushbutton_work(struct work_struct *work);
  173. struct controller *pcie_init(struct pcie_device *dev);
  174. int pcie_init_notification(struct controller *ctrl);
  175. void pcie_shutdown_notification(struct controller *ctrl);
  176. void pcie_clear_hotplug_events(struct controller *ctrl);
  177. int pciehp_power_on_slot(struct slot *slot);
  178. void pciehp_power_off_slot(struct slot *slot);
  179. void pciehp_get_power_status(struct slot *slot, u8 *status);
  180. void pciehp_get_attention_status(struct slot *slot, u8 *status);
  181. void pciehp_set_attention_status(struct slot *slot, u8 status);
  182. void pciehp_get_latch_status(struct slot *slot, u8 *status);
  183. void pciehp_get_adapter_status(struct slot *slot, u8 *status);
  184. int pciehp_query_power_fault(struct slot *slot);
  185. void pciehp_green_led_on(struct slot *slot);
  186. void pciehp_green_led_off(struct slot *slot);
  187. void pciehp_green_led_blink(struct slot *slot);
  188. int pciehp_check_link_status(struct controller *ctrl);
  189. bool pciehp_check_link_active(struct controller *ctrl);
  190. void pciehp_release_ctrl(struct controller *ctrl);
  191. int pciehp_reset_slot(struct slot *slot, int probe);
  192. int pciehp_set_raw_indicator_status(struct hotplug_slot *h_slot, u8 status);
  193. int pciehp_get_raw_indicator_status(struct hotplug_slot *h_slot, u8 *status);
  194. static inline const char *slot_name(struct slot *slot)
  195. {
  196. return hotplug_slot_name(slot->hotplug_slot);
  197. }
  198. #endif /* _PCIEHP_H */