shpchp.h 10.0 KB

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