shpchp.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Standard 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
  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 _SHPCHP_H
  16. #define _SHPCHP_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(), struct timer_list */
  22. #include <linux/mutex.h>
  23. #include <linux/workqueue.h>
  24. #if !defined(MODULE)
  25. #define MY_NAME "shpchp"
  26. #else
  27. #define MY_NAME THIS_MODULE->name
  28. #endif
  29. extern bool shpchp_poll_mode;
  30. extern int shpchp_poll_time;
  31. extern bool shpchp_debug;
  32. #define ctrl_dbg(ctrl, format, arg...) \
  33. pci_dbg(ctrl->pci_dev, format, ## arg)
  34. #define ctrl_err(ctrl, format, arg...) \
  35. pci_err(ctrl->pci_dev, format, ## arg)
  36. #define ctrl_info(ctrl, format, arg...) \
  37. pci_info(ctrl->pci_dev, format, ## arg)
  38. #define ctrl_warn(ctrl, format, arg...) \
  39. pci_warn(ctrl->pci_dev, format, ## arg)
  40. #define SLOT_NAME_SIZE 10
  41. struct slot {
  42. u8 bus;
  43. u8 device;
  44. u16 status;
  45. u32 number;
  46. u8 is_a_board;
  47. u8 state;
  48. u8 attention_save;
  49. u8 presence_save;
  50. u8 latch_save;
  51. u8 pwr_save;
  52. struct controller *ctrl;
  53. struct hotplug_slot hotplug_slot;
  54. struct list_head slot_list;
  55. struct delayed_work work; /* work for button event */
  56. struct mutex lock;
  57. struct workqueue_struct *wq;
  58. u8 hp_slot;
  59. };
  60. struct event_info {
  61. u32 event_type;
  62. struct slot *p_slot;
  63. struct work_struct work;
  64. };
  65. struct controller {
  66. struct mutex crit_sect; /* critical section mutex */
  67. struct mutex cmd_lock; /* command lock */
  68. int num_slots; /* Number of slots on ctlr */
  69. int slot_num_inc; /* 1 or -1 */
  70. struct pci_dev *pci_dev;
  71. struct list_head slot_list;
  72. wait_queue_head_t queue; /* sleep & wake process */
  73. u8 slot_device_offset;
  74. u32 pcix_misc2_reg; /* for amd pogo errata */
  75. u32 first_slot; /* First physical slot number */
  76. u32 cap_offset;
  77. unsigned long mmio_base;
  78. unsigned long mmio_size;
  79. void __iomem *creg;
  80. struct timer_list poll_timer;
  81. };
  82. /* Define AMD SHPC ID */
  83. #define PCI_DEVICE_ID_AMD_POGO_7458 0x7458
  84. /* AMD PCI-X bridge registers */
  85. #define PCIX_MEM_BASE_LIMIT_OFFSET 0x1C
  86. #define PCIX_MISCII_OFFSET 0x48
  87. #define PCIX_MISC_BRIDGE_ERRORS_OFFSET 0x80
  88. /* AMD PCIX_MISCII masks and offsets */
  89. #define PERRNONFATALENABLE_MASK 0x00040000
  90. #define PERRFATALENABLE_MASK 0x00080000
  91. #define PERRFLOODENABLE_MASK 0x00100000
  92. #define SERRNONFATALENABLE_MASK 0x00200000
  93. #define SERRFATALENABLE_MASK 0x00400000
  94. /* AMD PCIX_MISC_BRIDGE_ERRORS masks and offsets */
  95. #define PERR_OBSERVED_MASK 0x00000001
  96. /* AMD PCIX_MEM_BASE_LIMIT masks */
  97. #define RSE_MASK 0x40000000
  98. #define INT_BUTTON_IGNORE 0
  99. #define INT_PRESENCE_ON 1
  100. #define INT_PRESENCE_OFF 2
  101. #define INT_SWITCH_CLOSE 3
  102. #define INT_SWITCH_OPEN 4
  103. #define INT_POWER_FAULT 5
  104. #define INT_POWER_FAULT_CLEAR 6
  105. #define INT_BUTTON_PRESS 7
  106. #define INT_BUTTON_RELEASE 8
  107. #define INT_BUTTON_CANCEL 9
  108. #define STATIC_STATE 0
  109. #define BLINKINGON_STATE 1
  110. #define BLINKINGOFF_STATE 2
  111. #define POWERON_STATE 3
  112. #define POWEROFF_STATE 4
  113. /* Error messages */
  114. #define INTERLOCK_OPEN 0x00000002
  115. #define ADD_NOT_SUPPORTED 0x00000003
  116. #define CARD_FUNCTIONING 0x00000005
  117. #define ADAPTER_NOT_SAME 0x00000006
  118. #define NO_ADAPTER_PRESENT 0x00000009
  119. #define NOT_ENOUGH_RESOURCES 0x0000000B
  120. #define DEVICE_TYPE_NOT_SUPPORTED 0x0000000C
  121. #define WRONG_BUS_FREQUENCY 0x0000000D
  122. #define POWER_FAILURE 0x0000000E
  123. int __must_check shpchp_create_ctrl_files(struct controller *ctrl);
  124. void shpchp_remove_ctrl_files(struct controller *ctrl);
  125. int shpchp_sysfs_enable_slot(struct slot *slot);
  126. int shpchp_sysfs_disable_slot(struct slot *slot);
  127. u8 shpchp_handle_attention_button(u8 hp_slot, struct controller *ctrl);
  128. u8 shpchp_handle_switch_change(u8 hp_slot, struct controller *ctrl);
  129. u8 shpchp_handle_presence_change(u8 hp_slot, struct controller *ctrl);
  130. u8 shpchp_handle_power_fault(u8 hp_slot, struct controller *ctrl);
  131. int shpchp_configure_device(struct slot *p_slot);
  132. void shpchp_unconfigure_device(struct slot *p_slot);
  133. void cleanup_slots(struct controller *ctrl);
  134. void shpchp_queue_pushbutton_work(struct work_struct *work);
  135. int shpc_init(struct controller *ctrl, struct pci_dev *pdev);
  136. static inline const char *slot_name(struct slot *slot)
  137. {
  138. return hotplug_slot_name(&slot->hotplug_slot);
  139. }
  140. struct ctrl_reg {
  141. volatile u32 base_offset;
  142. volatile u32 slot_avail1;
  143. volatile u32 slot_avail2;
  144. volatile u32 slot_config;
  145. volatile u16 sec_bus_config;
  146. volatile u8 msi_ctrl;
  147. volatile u8 prog_interface;
  148. volatile u16 cmd;
  149. volatile u16 cmd_status;
  150. volatile u32 intr_loc;
  151. volatile u32 serr_loc;
  152. volatile u32 serr_intr_enable;
  153. volatile u32 slot1;
  154. } __attribute__ ((packed));
  155. /* offsets to the controller registers based on the above structure layout */
  156. enum ctrl_offsets {
  157. BASE_OFFSET = offsetof(struct ctrl_reg, base_offset),
  158. SLOT_AVAIL1 = offsetof(struct ctrl_reg, slot_avail1),
  159. SLOT_AVAIL2 = offsetof(struct ctrl_reg, slot_avail2),
  160. SLOT_CONFIG = offsetof(struct ctrl_reg, slot_config),
  161. SEC_BUS_CONFIG = offsetof(struct ctrl_reg, sec_bus_config),
  162. MSI_CTRL = offsetof(struct ctrl_reg, msi_ctrl),
  163. PROG_INTERFACE = offsetof(struct ctrl_reg, prog_interface),
  164. CMD = offsetof(struct ctrl_reg, cmd),
  165. CMD_STATUS = offsetof(struct ctrl_reg, cmd_status),
  166. INTR_LOC = offsetof(struct ctrl_reg, intr_loc),
  167. SERR_LOC = offsetof(struct ctrl_reg, serr_loc),
  168. SERR_INTR_ENABLE = offsetof(struct ctrl_reg, serr_intr_enable),
  169. SLOT1 = offsetof(struct ctrl_reg, slot1),
  170. };
  171. static inline struct slot *get_slot(struct hotplug_slot *hotplug_slot)
  172. {
  173. return container_of(hotplug_slot, struct slot, hotplug_slot);
  174. }
  175. static inline struct slot *shpchp_find_slot(struct controller *ctrl, u8 device)
  176. {
  177. struct slot *slot;
  178. list_for_each_entry(slot, &ctrl->slot_list, slot_list) {
  179. if (slot->device == device)
  180. return slot;
  181. }
  182. ctrl_err(ctrl, "Slot (device=0x%02x) not found\n", device);
  183. return NULL;
  184. }
  185. static inline void amd_pogo_errata_save_misc_reg(struct slot *p_slot)
  186. {
  187. u32 pcix_misc2_temp;
  188. /* save MiscII register */
  189. pci_read_config_dword(p_slot->ctrl->pci_dev, PCIX_MISCII_OFFSET, &pcix_misc2_temp);
  190. p_slot->ctrl->pcix_misc2_reg = pcix_misc2_temp;
  191. /* clear SERR/PERR enable bits */
  192. pcix_misc2_temp &= ~SERRFATALENABLE_MASK;
  193. pcix_misc2_temp &= ~SERRNONFATALENABLE_MASK;
  194. pcix_misc2_temp &= ~PERRFLOODENABLE_MASK;
  195. pcix_misc2_temp &= ~PERRFATALENABLE_MASK;
  196. pcix_misc2_temp &= ~PERRNONFATALENABLE_MASK;
  197. pci_write_config_dword(p_slot->ctrl->pci_dev, PCIX_MISCII_OFFSET, pcix_misc2_temp);
  198. }
  199. static inline void amd_pogo_errata_restore_misc_reg(struct slot *p_slot)
  200. {
  201. u32 pcix_misc2_temp;
  202. u32 pcix_bridge_errors_reg;
  203. u32 pcix_mem_base_reg;
  204. u8 perr_set;
  205. u8 rse_set;
  206. /* write-one-to-clear Bridge_Errors[ PERR_OBSERVED ] */
  207. pci_read_config_dword(p_slot->ctrl->pci_dev, PCIX_MISC_BRIDGE_ERRORS_OFFSET, &pcix_bridge_errors_reg);
  208. perr_set = pcix_bridge_errors_reg & PERR_OBSERVED_MASK;
  209. if (perr_set) {
  210. ctrl_dbg(p_slot->ctrl,
  211. "Bridge_Errors[ PERR_OBSERVED = %08X] (W1C)\n",
  212. perr_set);
  213. pci_write_config_dword(p_slot->ctrl->pci_dev, PCIX_MISC_BRIDGE_ERRORS_OFFSET, perr_set);
  214. }
  215. /* write-one-to-clear Memory_Base_Limit[ RSE ] */
  216. pci_read_config_dword(p_slot->ctrl->pci_dev, PCIX_MEM_BASE_LIMIT_OFFSET, &pcix_mem_base_reg);
  217. rse_set = pcix_mem_base_reg & RSE_MASK;
  218. if (rse_set) {
  219. ctrl_dbg(p_slot->ctrl, "Memory_Base_Limit[ RSE ] (W1C)\n");
  220. pci_write_config_dword(p_slot->ctrl->pci_dev, PCIX_MEM_BASE_LIMIT_OFFSET, rse_set);
  221. }
  222. /* restore MiscII register */
  223. pci_read_config_dword(p_slot->ctrl->pci_dev, PCIX_MISCII_OFFSET, &pcix_misc2_temp);
  224. if (p_slot->ctrl->pcix_misc2_reg & SERRFATALENABLE_MASK)
  225. pcix_misc2_temp |= SERRFATALENABLE_MASK;
  226. else
  227. pcix_misc2_temp &= ~SERRFATALENABLE_MASK;
  228. if (p_slot->ctrl->pcix_misc2_reg & SERRNONFATALENABLE_MASK)
  229. pcix_misc2_temp |= SERRNONFATALENABLE_MASK;
  230. else
  231. pcix_misc2_temp &= ~SERRNONFATALENABLE_MASK;
  232. if (p_slot->ctrl->pcix_misc2_reg & PERRFLOODENABLE_MASK)
  233. pcix_misc2_temp |= PERRFLOODENABLE_MASK;
  234. else
  235. pcix_misc2_temp &= ~PERRFLOODENABLE_MASK;
  236. if (p_slot->ctrl->pcix_misc2_reg & PERRFATALENABLE_MASK)
  237. pcix_misc2_temp |= PERRFATALENABLE_MASK;
  238. else
  239. pcix_misc2_temp &= ~PERRFATALENABLE_MASK;
  240. if (p_slot->ctrl->pcix_misc2_reg & PERRNONFATALENABLE_MASK)
  241. pcix_misc2_temp |= PERRNONFATALENABLE_MASK;
  242. else
  243. pcix_misc2_temp &= ~PERRNONFATALENABLE_MASK;
  244. pci_write_config_dword(p_slot->ctrl->pci_dev, PCIX_MISCII_OFFSET, pcix_misc2_temp);
  245. }
  246. int shpchp_power_on_slot(struct slot *slot);
  247. int shpchp_slot_enable(struct slot *slot);
  248. int shpchp_slot_disable(struct slot *slot);
  249. int shpchp_set_bus_speed_mode(struct slot *slot, enum pci_bus_speed speed);
  250. int shpchp_get_power_status(struct slot *slot, u8 *status);
  251. int shpchp_get_attention_status(struct slot *slot, u8 *status);
  252. int shpchp_set_attention_status(struct slot *slot, u8 status);
  253. int shpchp_get_latch_status(struct slot *slot, u8 *status);
  254. int shpchp_get_adapter_status(struct slot *slot, u8 *status);
  255. int shpchp_get_adapter_speed(struct slot *slot, enum pci_bus_speed *speed);
  256. int shpchp_get_prog_int(struct slot *slot, u8 *prog_int);
  257. int shpchp_query_power_fault(struct slot *slot);
  258. void shpchp_green_led_on(struct slot *slot);
  259. void shpchp_green_led_off(struct slot *slot);
  260. void shpchp_green_led_blink(struct slot *slot);
  261. void shpchp_release_ctlr(struct controller *ctrl);
  262. int shpchp_check_cmd_status(struct controller *ctrl);
  263. #endif /* _SHPCHP_H */