xref: /openbmc/linux/drivers/pci/hotplug/shpchp.h (revision 87c2ce3b)
1 /*
2  * Standard Hot Plug Controller Driver
3  *
4  * Copyright (C) 1995,2001 Compaq Computer Corporation
5  * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
6  * Copyright (C) 2001 IBM
7  * Copyright (C) 2003-2004 Intel Corporation
8  *
9  * All rights reserved.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or (at
14  * your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
19  * NON INFRINGEMENT.  See the GNU General Public License for more
20  * details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25  *
26  * Send feedback to <greg@kroah.com>,<kristen.c.accardi@intel.com>
27  *
28  */
29 #ifndef _SHPCHP_H
30 #define _SHPCHP_H
31 
32 #include <linux/types.h>
33 #include <linux/pci.h>
34 #include <linux/delay.h>
35 #include <linux/sched.h>	/* signal_pending(), struct timer_list */
36 
37 #include "pci_hotplug.h"
38 
39 #if !defined(MODULE)
40 	#define MY_NAME	"shpchp"
41 #else
42 	#define MY_NAME	THIS_MODULE->name
43 #endif
44 
45 extern int shpchp_poll_mode;
46 extern int shpchp_poll_time;
47 extern int shpchp_debug;
48 
49 /*#define dbg(format, arg...) do { if (shpchp_debug) printk(KERN_DEBUG "%s: " format, MY_NAME , ## arg); } while (0)*/
50 #define dbg(format, arg...) do { if (shpchp_debug) printk("%s: " format, MY_NAME , ## arg); } while (0)
51 #define err(format, arg...) printk(KERN_ERR "%s: " format, MY_NAME , ## arg)
52 #define info(format, arg...) printk(KERN_INFO "%s: " format, MY_NAME , ## arg)
53 #define warn(format, arg...) printk(KERN_WARNING "%s: " format, MY_NAME , ## arg)
54 
55 #define SLOT_MAGIC	0x67267321
56 struct slot {
57 	u32 magic;
58 	struct slot *next;
59 	u8 bus;
60 	u8 device;
61 	u16 status;
62 	u32 number;
63 	u8 is_a_board;
64 	u8 state;
65 	u8 presence_save;
66 	u8 pwr_save;
67 	struct timer_list task_event;
68 	u8 hp_slot;
69 	struct controller *ctrl;
70 	struct hpc_ops *hpc_ops;
71 	struct hotplug_slot *hotplug_slot;
72 	struct list_head	slot_list;
73 };
74 
75 struct event_info {
76 	u32 event_type;
77 	u8 hp_slot;
78 };
79 
80 struct controller {
81 	struct controller *next;
82 	struct semaphore crit_sect;	/* critical section semaphore */
83 	struct php_ctlr_state_s *hpc_ctlr_handle; /* HPC controller handle */
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 pci_bus *pci_bus;
88 	struct event_info event_queue[10];
89 	struct slot *slot;
90 	struct hpc_ops *hpc_ops;
91 	wait_queue_head_t queue;	/* sleep & wake process */
92 	u8 next_event;
93 	u8 bus;
94 	u8 device;
95 	u8 function;
96 	u8 slot_device_offset;
97 	u8 add_support;
98 	enum pci_bus_speed speed;
99 	u32 first_slot;		/* First physical slot number */
100 	u8 slot_bus;		/* Bus where the slots handled by this controller sit */
101 	u32 cap_offset;
102 	unsigned long mmio_base;
103 	unsigned long mmio_size;
104 	volatile int cmd_busy;
105 };
106 
107 struct hotplug_params {
108 	u8	cache_line_size;
109 	u8	latency_timer;
110 	u8	enable_serr;
111 	u8	enable_perr;
112 };
113 
114 /* Define AMD SHPC ID  */
115 #define PCI_DEVICE_ID_AMD_GOLAM_7450	0x7450
116 
117 #define INT_BUTTON_IGNORE		0
118 #define INT_PRESENCE_ON			1
119 #define INT_PRESENCE_OFF		2
120 #define INT_SWITCH_CLOSE		3
121 #define INT_SWITCH_OPEN			4
122 #define INT_POWER_FAULT			5
123 #define INT_POWER_FAULT_CLEAR		6
124 #define INT_BUTTON_PRESS		7
125 #define INT_BUTTON_RELEASE		8
126 #define INT_BUTTON_CANCEL		9
127 
128 #define STATIC_STATE			0
129 #define BLINKINGON_STATE		1
130 #define BLINKINGOFF_STATE		2
131 #define POWERON_STATE			3
132 #define POWEROFF_STATE			4
133 
134 #define PCI_TO_PCI_BRIDGE_CLASS		0x00060400
135 
136 /* Error messages */
137 #define INTERLOCK_OPEN			0x00000002
138 #define ADD_NOT_SUPPORTED		0x00000003
139 #define CARD_FUNCTIONING		0x00000005
140 #define ADAPTER_NOT_SAME		0x00000006
141 #define NO_ADAPTER_PRESENT		0x00000009
142 #define NOT_ENOUGH_RESOURCES		0x0000000B
143 #define DEVICE_TYPE_NOT_SUPPORTED	0x0000000C
144 #define WRONG_BUS_FREQUENCY		0x0000000D
145 #define POWER_FAILURE			0x0000000E
146 
147 #define REMOVE_NOT_SUPPORTED		0x00000003
148 
149 #define DISABLE_CARD			1
150 
151 /*
152  * error Messages
153  */
154 #define msg_initialization_err	"Initialization failure, error=%d\n"
155 #define msg_button_on		"PCI slot #%d - powering on due to button press.\n"
156 #define msg_button_off		"PCI slot #%d - powering off due to button press.\n"
157 #define msg_button_cancel	"PCI slot #%d - action canceled due to button press.\n"
158 
159 /* sysfs functions for the hotplug controller info */
160 extern void shpchp_create_ctrl_files	(struct controller *ctrl);
161 
162 /* controller functions */
163 extern int	shpchp_event_start_thread(void);
164 extern void	shpchp_event_stop_thread(void);
165 extern int	shpchp_enable_slot(struct slot *slot);
166 extern int	shpchp_disable_slot(struct slot *slot);
167 
168 extern u8	shpchp_handle_attention_button(u8 hp_slot, void *inst_id);
169 extern u8	shpchp_handle_switch_change(u8 hp_slot, void *inst_id);
170 extern u8	shpchp_handle_presence_change(u8 hp_slot, void *inst_id);
171 extern u8	shpchp_handle_power_fault(u8 hp_slot, void *inst_id);
172 
173 /* pci functions */
174 extern int	shpchp_save_config(struct controller *ctrl, int busnumber, int num_ctlr_slots, int first_device_num);
175 extern int	shpchp_configure_device(struct slot *p_slot);
176 extern int	shpchp_unconfigure_device(struct slot *p_slot);
177 extern void	get_hp_hw_control_from_firmware(struct pci_dev *dev);
178 extern void	get_hp_params_from_firmware(struct pci_dev *dev,
179 		struct hotplug_params *hpp);
180 extern int	shpchprm_get_physical_slot_number(struct controller *ctrl,
181 		u32 *sun, u8 busnum, u8 devnum);
182 extern void	shpchp_remove_ctrl_files(struct controller *ctrl);
183 
184 
185 /* Global variables */
186 extern struct controller *shpchp_ctrl_list;
187 
188 struct ctrl_reg {
189 	volatile u32 base_offset;
190 	volatile u32 slot_avail1;
191 	volatile u32 slot_avail2;
192 	volatile u32 slot_config;
193 	volatile u16 sec_bus_config;
194 	volatile u8  msi_ctrl;
195 	volatile u8  prog_interface;
196 	volatile u16 cmd;
197 	volatile u16 cmd_status;
198 	volatile u32 intr_loc;
199 	volatile u32 serr_loc;
200 	volatile u32 serr_intr_enable;
201 	volatile u32 slot1;
202 	volatile u32 slot2;
203 	volatile u32 slot3;
204 	volatile u32 slot4;
205 	volatile u32 slot5;
206 	volatile u32 slot6;
207 	volatile u32 slot7;
208 	volatile u32 slot8;
209 	volatile u32 slot9;
210 	volatile u32 slot10;
211 	volatile u32 slot11;
212 	volatile u32 slot12;
213 } __attribute__ ((packed));
214 
215 /* offsets to the controller registers based on the above structure layout */
216 enum ctrl_offsets {
217 	BASE_OFFSET =	offsetof(struct ctrl_reg, base_offset),
218 	SLOT_AVAIL1 =	offsetof(struct ctrl_reg, slot_avail1),
219 	SLOT_AVAIL2	=	offsetof(struct ctrl_reg, slot_avail2),
220 	SLOT_CONFIG =	offsetof(struct ctrl_reg, slot_config),
221 	SEC_BUS_CONFIG =	offsetof(struct ctrl_reg, sec_bus_config),
222 	MSI_CTRL	=	offsetof(struct ctrl_reg, msi_ctrl),
223 	PROG_INTERFACE =	offsetof(struct ctrl_reg, prog_interface),
224 	CMD		=	offsetof(struct ctrl_reg, cmd),
225 	CMD_STATUS	=	offsetof(struct ctrl_reg, cmd_status),
226 	INTR_LOC	= 	offsetof(struct ctrl_reg, intr_loc),
227 	SERR_LOC	= 	offsetof(struct ctrl_reg, serr_loc),
228 	SERR_INTR_ENABLE =	offsetof(struct ctrl_reg, serr_intr_enable),
229 	SLOT1 =		offsetof(struct ctrl_reg, slot1),
230 	SLOT2 =		offsetof(struct ctrl_reg, slot2),
231 	SLOT3 =		offsetof(struct ctrl_reg, slot3),
232 	SLOT4 =		offsetof(struct ctrl_reg, slot4),
233 	SLOT5 =		offsetof(struct ctrl_reg, slot5),
234 	SLOT6 =		offsetof(struct ctrl_reg, slot6),
235 	SLOT7 =		offsetof(struct ctrl_reg, slot7),
236 	SLOT8 =		offsetof(struct ctrl_reg, slot8),
237 	SLOT9 =		offsetof(struct ctrl_reg, slot9),
238 	SLOT10 =	offsetof(struct ctrl_reg, slot10),
239 	SLOT11 =	offsetof(struct ctrl_reg, slot11),
240 	SLOT12 =	offsetof(struct ctrl_reg, slot12),
241 };
242 typedef u8(*php_intr_callback_t) (u8 hp_slot, void *instance_id);
243 struct php_ctlr_state_s {
244 	struct php_ctlr_state_s *pnext;
245 	struct pci_dev *pci_dev;
246 	unsigned int irq;
247 	unsigned long flags;	/* spinlock's */
248 	u32 slot_device_offset;
249 	u32 num_slots;
250     	struct timer_list	int_poll_timer;	/* Added for poll event */
251 	php_intr_callback_t attention_button_callback;
252 	php_intr_callback_t switch_change_callback;
253 	php_intr_callback_t presence_change_callback;
254 	php_intr_callback_t power_fault_callback;
255 	void *callback_instance_id;
256 	void __iomem *creg;			/* Ptr to controller register space */
257 };
258 /* Inline functions */
259 
260 
261 /* Inline functions to check the sanity of a pointer that is passed to us */
262 static inline int slot_paranoia_check (struct slot *slot, const char *function)
263 {
264 	if (!slot) {
265 		dbg("%s - slot == NULL", function);
266 		return -1;
267 	}
268 	if (slot->magic != SLOT_MAGIC) {
269 		dbg("%s - bad magic number for slot", function);
270 		return -1;
271 	}
272 	if (!slot->hotplug_slot) {
273 		dbg("%s - slot->hotplug_slot == NULL!", function);
274 		return -1;
275 	}
276 	return 0;
277 }
278 
279 static inline struct slot *get_slot (struct hotplug_slot *hotplug_slot, const char *function)
280 {
281 	struct slot *slot;
282 
283 	if (!hotplug_slot) {
284 		dbg("%s - hotplug_slot == NULL\n", function);
285 		return NULL;
286 	}
287 
288 	slot = (struct slot *)hotplug_slot->private;
289 	if (slot_paranoia_check (slot, function))
290                 return NULL;
291 	return slot;
292 }
293 
294 static inline struct slot *shpchp_find_slot (struct controller *ctrl, u8 device)
295 {
296 	struct slot *p_slot, *tmp_slot = NULL;
297 
298 	if (!ctrl)
299 		return NULL;
300 
301 	p_slot = ctrl->slot;
302 
303 	while (p_slot && (p_slot->device != device)) {
304 		tmp_slot = p_slot;
305 		p_slot = p_slot->next;
306 	}
307 	if (p_slot == NULL) {
308 		err("ERROR: shpchp_find_slot device=0x%x\n", device);
309 		p_slot = tmp_slot;
310 	}
311 
312 	return (p_slot);
313 }
314 
315 static inline int wait_for_ctrl_irq (struct controller *ctrl)
316 {
317     DECLARE_WAITQUEUE(wait, current);
318 	int retval = 0;
319 
320 	add_wait_queue(&ctrl->queue, &wait);
321 
322 	if (!shpchp_poll_mode) {
323 		/* Sleep for up to 1 second */
324 		msleep_interruptible(1000);
325 	} else {
326 		/* Sleep for up to 2 seconds */
327 		msleep_interruptible(2000);
328 	}
329 	remove_wait_queue(&ctrl->queue, &wait);
330 	if (signal_pending(current))
331 		retval =  -EINTR;
332 
333 	return retval;
334 }
335 
336 #define SLOT_NAME_SIZE 10
337 
338 static inline void make_slot_name(char *buffer, int buffer_size, struct slot *slot)
339 {
340 	snprintf(buffer, buffer_size, "%04d_%04d", slot->bus, slot->number);
341 }
342 
343 enum php_ctlr_type {
344 	PCI,
345 	ISA,
346 	ACPI
347 };
348 
349 int shpc_init( struct controller *ctrl, struct pci_dev *pdev);
350 
351 int shpc_get_ctlr_slot_config( struct controller *ctrl,
352 		int *num_ctlr_slots,
353 		int *first_device_num,
354 		int *physical_slot_num,
355 		int *updown,
356 		int *flags);
357 
358 struct hpc_ops {
359 	int	(*power_on_slot )		(struct slot *slot);
360 	int	(*slot_enable )			(struct slot *slot);
361 	int	(*slot_disable )		(struct slot *slot);
362 	int	(*set_bus_speed_mode)	(struct slot *slot, enum pci_bus_speed speed);
363 	int	(*get_power_status)		(struct slot *slot, u8 *status);
364 	int	(*get_attention_status)	(struct slot *slot, u8 *status);
365 	int	(*set_attention_status)	(struct slot *slot, u8 status);
366 	int	(*get_latch_status)		(struct slot *slot, u8 *status);
367 	int	(*get_adapter_status)	(struct slot *slot, u8 *status);
368 
369 	int	(*get_max_bus_speed)	(struct slot *slot, enum pci_bus_speed *speed);
370 	int	(*get_cur_bus_speed)	(struct slot *slot, enum pci_bus_speed *speed);
371 	int	(*get_adapter_speed)	(struct slot *slot, enum pci_bus_speed *speed);
372 	int	(*get_mode1_ECC_cap)	(struct slot *slot, u8 *mode);
373 	int	(*get_prog_int)			(struct slot *slot, u8 *prog_int);
374 
375 	int	(*query_power_fault)	(struct slot *slot);
376 	void	(*green_led_on)		(struct slot *slot);
377 	void	(*green_led_off)	(struct slot *slot);
378 	void	(*green_led_blink)	(struct slot *slot);
379 	void	(*release_ctlr)		(struct controller *ctrl);
380 	int (*check_cmd_status)		(struct controller *ctrl);
381 };
382 
383 #endif				/* _SHPCHP_H */
384