shpchp.h 9.9 KB

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