shpchp.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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_GOLAM_7450 0x7450
  99. #define PCI_DEVICE_ID_AMD_POGO_7458 0x7458
  100. /* AMD PCI-X bridge registers */
  101. #define PCIX_MEM_BASE_LIMIT_OFFSET 0x1C
  102. #define PCIX_MISCII_OFFSET 0x48
  103. #define PCIX_MISC_BRIDGE_ERRORS_OFFSET 0x80
  104. /* AMD PCIX_MISCII masks and offsets */
  105. #define PERRNONFATALENABLE_MASK 0x00040000
  106. #define PERRFATALENABLE_MASK 0x00080000
  107. #define PERRFLOODENABLE_MASK 0x00100000
  108. #define SERRNONFATALENABLE_MASK 0x00200000
  109. #define SERRFATALENABLE_MASK 0x00400000
  110. /* AMD PCIX_MISC_BRIDGE_ERRORS masks and offsets */
  111. #define PERR_OBSERVED_MASK 0x00000001
  112. /* AMD PCIX_MEM_BASE_LIMIT masks */
  113. #define RSE_MASK 0x40000000
  114. #define INT_BUTTON_IGNORE 0
  115. #define INT_PRESENCE_ON 1
  116. #define INT_PRESENCE_OFF 2
  117. #define INT_SWITCH_CLOSE 3
  118. #define INT_SWITCH_OPEN 4
  119. #define INT_POWER_FAULT 5
  120. #define INT_POWER_FAULT_CLEAR 6
  121. #define INT_BUTTON_PRESS 7
  122. #define INT_BUTTON_RELEASE 8
  123. #define INT_BUTTON_CANCEL 9
  124. #define STATIC_STATE 0
  125. #define BLINKINGON_STATE 1
  126. #define BLINKINGOFF_STATE 2
  127. #define POWERON_STATE 3
  128. #define POWEROFF_STATE 4
  129. /* Error messages */
  130. #define INTERLOCK_OPEN 0x00000002
  131. #define ADD_NOT_SUPPORTED 0x00000003
  132. #define CARD_FUNCTIONING 0x00000005
  133. #define ADAPTER_NOT_SAME 0x00000006
  134. #define NO_ADAPTER_PRESENT 0x00000009
  135. #define NOT_ENOUGH_RESOURCES 0x0000000B
  136. #define DEVICE_TYPE_NOT_SUPPORTED 0x0000000C
  137. #define WRONG_BUS_FREQUENCY 0x0000000D
  138. #define POWER_FAILURE 0x0000000E
  139. int __must_check shpchp_create_ctrl_files(struct controller *ctrl);
  140. void shpchp_remove_ctrl_files(struct controller *ctrl);
  141. int shpchp_sysfs_enable_slot(struct slot *slot);
  142. int shpchp_sysfs_disable_slot(struct slot *slot);
  143. u8 shpchp_handle_attention_button(u8 hp_slot, struct controller *ctrl);
  144. u8 shpchp_handle_switch_change(u8 hp_slot, struct controller *ctrl);
  145. u8 shpchp_handle_presence_change(u8 hp_slot, struct controller *ctrl);
  146. u8 shpchp_handle_power_fault(u8 hp_slot, struct controller *ctrl);
  147. int shpchp_configure_device(struct slot *p_slot);
  148. int shpchp_unconfigure_device(struct slot *p_slot);
  149. void cleanup_slots(struct controller *ctrl);
  150. void shpchp_queue_pushbutton_work(struct work_struct *work);
  151. int shpc_init(struct controller *ctrl, struct pci_dev *pdev);
  152. static inline const char *slot_name(struct slot *slot)
  153. {
  154. return hotplug_slot_name(slot->hotplug_slot);
  155. }
  156. #ifdef CONFIG_ACPI
  157. #include <linux/pci-acpi.h>
  158. static inline int get_hp_hw_control_from_firmware(struct pci_dev *dev)
  159. {
  160. u32 flags = OSC_PCI_SHPC_NATIVE_HP_CONTROL;
  161. return acpi_get_hp_hw_control_from_firmware(dev, flags);
  162. }
  163. #else
  164. #define get_hp_hw_control_from_firmware(dev) (0)
  165. #endif
  166. struct ctrl_reg {
  167. volatile u32 base_offset;
  168. volatile u32 slot_avail1;
  169. volatile u32 slot_avail2;
  170. volatile u32 slot_config;
  171. volatile u16 sec_bus_config;
  172. volatile u8 msi_ctrl;
  173. volatile u8 prog_interface;
  174. volatile u16 cmd;
  175. volatile u16 cmd_status;
  176. volatile u32 intr_loc;
  177. volatile u32 serr_loc;
  178. volatile u32 serr_intr_enable;
  179. volatile u32 slot1;
  180. } __attribute__ ((packed));
  181. /* offsets to the controller registers based on the above structure layout */
  182. enum ctrl_offsets {
  183. BASE_OFFSET = offsetof(struct ctrl_reg, base_offset),
  184. SLOT_AVAIL1 = offsetof(struct ctrl_reg, slot_avail1),
  185. SLOT_AVAIL2 = offsetof(struct ctrl_reg, slot_avail2),
  186. SLOT_CONFIG = offsetof(struct ctrl_reg, slot_config),
  187. SEC_BUS_CONFIG = offsetof(struct ctrl_reg, sec_bus_config),
  188. MSI_CTRL = offsetof(struct ctrl_reg, msi_ctrl),
  189. PROG_INTERFACE = offsetof(struct ctrl_reg, prog_interface),
  190. CMD = offsetof(struct ctrl_reg, cmd),
  191. CMD_STATUS = offsetof(struct ctrl_reg, cmd_status),
  192. INTR_LOC = offsetof(struct ctrl_reg, intr_loc),
  193. SERR_LOC = offsetof(struct ctrl_reg, serr_loc),
  194. SERR_INTR_ENABLE = offsetof(struct ctrl_reg, serr_intr_enable),
  195. SLOT1 = offsetof(struct ctrl_reg, slot1),
  196. };
  197. static inline struct slot *get_slot(struct hotplug_slot *hotplug_slot)
  198. {
  199. return hotplug_slot->private;
  200. }
  201. static inline struct slot *shpchp_find_slot(struct controller *ctrl, u8 device)
  202. {
  203. struct slot *slot;
  204. list_for_each_entry(slot, &ctrl->slot_list, slot_list) {
  205. if (slot->device == device)
  206. return slot;
  207. }
  208. ctrl_err(ctrl, "Slot (device=0x%02x) not found\n", device);
  209. return NULL;
  210. }
  211. static inline void amd_pogo_errata_save_misc_reg(struct slot *p_slot)
  212. {
  213. u32 pcix_misc2_temp;
  214. /* save MiscII register */
  215. pci_read_config_dword(p_slot->ctrl->pci_dev, PCIX_MISCII_OFFSET, &pcix_misc2_temp);
  216. p_slot->ctrl->pcix_misc2_reg = pcix_misc2_temp;
  217. /* clear SERR/PERR enable bits */
  218. pcix_misc2_temp &= ~SERRFATALENABLE_MASK;
  219. pcix_misc2_temp &= ~SERRNONFATALENABLE_MASK;
  220. pcix_misc2_temp &= ~PERRFLOODENABLE_MASK;
  221. pcix_misc2_temp &= ~PERRFATALENABLE_MASK;
  222. pcix_misc2_temp &= ~PERRNONFATALENABLE_MASK;
  223. pci_write_config_dword(p_slot->ctrl->pci_dev, PCIX_MISCII_OFFSET, pcix_misc2_temp);
  224. }
  225. static inline void amd_pogo_errata_restore_misc_reg(struct slot *p_slot)
  226. {
  227. u32 pcix_misc2_temp;
  228. u32 pcix_bridge_errors_reg;
  229. u32 pcix_mem_base_reg;
  230. u8 perr_set;
  231. u8 rse_set;
  232. /* write-one-to-clear Bridge_Errors[ PERR_OBSERVED ] */
  233. pci_read_config_dword(p_slot->ctrl->pci_dev, PCIX_MISC_BRIDGE_ERRORS_OFFSET, &pcix_bridge_errors_reg);
  234. perr_set = pcix_bridge_errors_reg & PERR_OBSERVED_MASK;
  235. if (perr_set) {
  236. ctrl_dbg(p_slot->ctrl,
  237. "Bridge_Errors[ PERR_OBSERVED = %08X] (W1C)\n",
  238. perr_set);
  239. pci_write_config_dword(p_slot->ctrl->pci_dev, PCIX_MISC_BRIDGE_ERRORS_OFFSET, perr_set);
  240. }
  241. /* write-one-to-clear Memory_Base_Limit[ RSE ] */
  242. pci_read_config_dword(p_slot->ctrl->pci_dev, PCIX_MEM_BASE_LIMIT_OFFSET, &pcix_mem_base_reg);
  243. rse_set = pcix_mem_base_reg & RSE_MASK;
  244. if (rse_set) {
  245. ctrl_dbg(p_slot->ctrl, "Memory_Base_Limit[ RSE ] (W1C)\n");
  246. pci_write_config_dword(p_slot->ctrl->pci_dev, PCIX_MEM_BASE_LIMIT_OFFSET, rse_set);
  247. }
  248. /* restore MiscII register */
  249. pci_read_config_dword(p_slot->ctrl->pci_dev, PCIX_MISCII_OFFSET, &pcix_misc2_temp);
  250. if (p_slot->ctrl->pcix_misc2_reg & SERRFATALENABLE_MASK)
  251. pcix_misc2_temp |= SERRFATALENABLE_MASK;
  252. else
  253. pcix_misc2_temp &= ~SERRFATALENABLE_MASK;
  254. if (p_slot->ctrl->pcix_misc2_reg & SERRNONFATALENABLE_MASK)
  255. pcix_misc2_temp |= SERRNONFATALENABLE_MASK;
  256. else
  257. pcix_misc2_temp &= ~SERRNONFATALENABLE_MASK;
  258. if (p_slot->ctrl->pcix_misc2_reg & PERRFLOODENABLE_MASK)
  259. pcix_misc2_temp |= PERRFLOODENABLE_MASK;
  260. else
  261. pcix_misc2_temp &= ~PERRFLOODENABLE_MASK;
  262. if (p_slot->ctrl->pcix_misc2_reg & PERRFATALENABLE_MASK)
  263. pcix_misc2_temp |= PERRFATALENABLE_MASK;
  264. else
  265. pcix_misc2_temp &= ~PERRFATALENABLE_MASK;
  266. if (p_slot->ctrl->pcix_misc2_reg & PERRNONFATALENABLE_MASK)
  267. pcix_misc2_temp |= PERRNONFATALENABLE_MASK;
  268. else
  269. pcix_misc2_temp &= ~PERRNONFATALENABLE_MASK;
  270. pci_write_config_dword(p_slot->ctrl->pci_dev, PCIX_MISCII_OFFSET, pcix_misc2_temp);
  271. }
  272. struct hpc_ops {
  273. int (*power_on_slot)(struct slot *slot);
  274. int (*slot_enable)(struct slot *slot);
  275. int (*slot_disable)(struct slot *slot);
  276. int (*set_bus_speed_mode)(struct slot *slot, enum pci_bus_speed speed);
  277. int (*get_power_status)(struct slot *slot, u8 *status);
  278. int (*get_attention_status)(struct slot *slot, u8 *status);
  279. int (*set_attention_status)(struct slot *slot, u8 status);
  280. int (*get_latch_status)(struct slot *slot, u8 *status);
  281. int (*get_adapter_status)(struct slot *slot, u8 *status);
  282. int (*get_adapter_speed)(struct slot *slot, enum pci_bus_speed *speed);
  283. int (*get_mode1_ECC_cap)(struct slot *slot, u8 *mode);
  284. int (*get_prog_int)(struct slot *slot, u8 *prog_int);
  285. int (*query_power_fault)(struct slot *slot);
  286. void (*green_led_on)(struct slot *slot);
  287. void (*green_led_off)(struct slot *slot);
  288. void (*green_led_blink)(struct slot *slot);
  289. void (*release_ctlr)(struct controller *ctrl);
  290. int (*check_cmd_status)(struct controller *ctrl);
  291. };
  292. #endif /* _SHPCHP_H */