xref: /openbmc/linux/drivers/pci/hotplug/acpiphp_core.c (revision 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2)
1 /*
2  * ACPI PCI 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 Corp.
7  * Copyright (C) 2002 Hiroshi Aono (h-aono@ap.jp.nec.com)
8  * Copyright (C) 2002,2003 Takayoshi Kochi (t-kochi@bq.jp.nec.com)
9  * Copyright (C) 2002,2003 NEC Corporation
10  *
11  * All rights reserved.
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or (at
16  * your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful, but
19  * WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
21  * NON INFRINGEMENT.  See the GNU General Public License for more
22  * details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27  *
28  * Send feedback to <gregkh@us.ibm.com>,
29  *		    <t-kochi@bq.jp.nec.com>
30  *
31  */
32 
33 #include <linux/init.h>
34 #include <linux/module.h>
35 #include <linux/moduleparam.h>
36 
37 #include <linux/kernel.h>
38 #include <linux/pci.h>
39 #include <linux/slab.h>
40 #include <linux/smp.h>
41 #include <linux/smp_lock.h>
42 #include "pci_hotplug.h"
43 #include "acpiphp.h"
44 
45 static LIST_HEAD(slot_list);
46 
47 #define MY_NAME	"acpiphp"
48 
49 static int debug;
50 int acpiphp_debug;
51 
52 /* local variables */
53 static int num_slots;
54 static struct acpiphp_attention_info *attention_info;
55 
56 #define DRIVER_VERSION	"0.4"
57 #define DRIVER_AUTHOR	"Greg Kroah-Hartman <gregkh@us.ibm.com>, Takayoshi Kochi <t-kochi@bq.jp.nec.com>"
58 #define DRIVER_DESC	"ACPI Hot Plug PCI Controller Driver"
59 
60 MODULE_AUTHOR(DRIVER_AUTHOR);
61 MODULE_DESCRIPTION(DRIVER_DESC);
62 MODULE_LICENSE("GPL");
63 MODULE_PARM_DESC(debug, "Debugging mode enabled or not");
64 module_param(debug, bool, 0644);
65 
66 /* export the attention callback registration methods */
67 EXPORT_SYMBOL_GPL(acpiphp_register_attention);
68 EXPORT_SYMBOL_GPL(acpiphp_unregister_attention);
69 
70 static int enable_slot		(struct hotplug_slot *slot);
71 static int disable_slot		(struct hotplug_slot *slot);
72 static int set_attention_status (struct hotplug_slot *slot, u8 value);
73 static int get_power_status	(struct hotplug_slot *slot, u8 *value);
74 static int get_attention_status (struct hotplug_slot *slot, u8 *value);
75 static int get_address		(struct hotplug_slot *slot, u32 *value);
76 static int get_latch_status	(struct hotplug_slot *slot, u8 *value);
77 static int get_adapter_status	(struct hotplug_slot *slot, u8 *value);
78 
79 static struct hotplug_slot_ops acpi_hotplug_slot_ops = {
80 	.owner			= THIS_MODULE,
81 	.enable_slot		= enable_slot,
82 	.disable_slot		= disable_slot,
83 	.set_attention_status	= set_attention_status,
84 	.get_power_status	= get_power_status,
85 	.get_attention_status	= get_attention_status,
86 	.get_latch_status	= get_latch_status,
87 	.get_adapter_status	= get_adapter_status,
88 	.get_address		= get_address,
89 };
90 
91 
92 /**
93  * acpiphp_register_attention - set attention LED callback
94  * @info: must be completely filled with LED callbacks
95  *
96  * Description: this is used to register a hardware specific ACPI
97  * driver that manipulates the attention LED.  All the fields in
98  * info must be set.
99  **/
100 int acpiphp_register_attention(struct acpiphp_attention_info *info)
101 {
102 	int retval = -EINVAL;
103 
104 	if (info && info->owner && info->set_attn &&
105 			info->get_attn && !attention_info) {
106 		retval = 0;
107 		attention_info = info;
108 	}
109 	return retval;
110 }
111 
112 
113 /**
114  * acpiphp_unregister_attention - unset attention LED callback
115  * @info: must match the pointer used to register
116  *
117  * Description: this is used to un-register a hardware specific acpi
118  * driver that manipulates the attention LED.  The pointer to the
119  * info struct must be the same as the one used to set it.
120  **/
121 int acpiphp_unregister_attention(struct acpiphp_attention_info *info)
122 {
123 	int retval = -EINVAL;
124 
125 	if (info && attention_info == info) {
126 		attention_info = NULL;
127 		retval = 0;
128 	}
129 	return retval;
130 }
131 
132 
133 /**
134  * enable_slot - power on and enable a slot
135  * @hotplug_slot: slot to enable
136  *
137  * Actual tasks are done in acpiphp_enable_slot()
138  *
139  */
140 static int enable_slot(struct hotplug_slot *hotplug_slot)
141 {
142 	struct slot *slot = hotplug_slot->private;
143 
144 	dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
145 
146 	/* enable the specified slot */
147 	return acpiphp_enable_slot(slot->acpi_slot);
148 }
149 
150 
151 /**
152  * disable_slot - disable and power off a slot
153  * @hotplug_slot: slot to disable
154  *
155  * Actual tasks are done in acpiphp_disable_slot()
156  *
157  */
158 static int disable_slot(struct hotplug_slot *hotplug_slot)
159 {
160 	struct slot *slot = hotplug_slot->private;
161 
162 	dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
163 
164 	/* disable the specified slot */
165 	return acpiphp_disable_slot(slot->acpi_slot);
166 }
167 
168 
169  /**
170   * set_attention_status - set attention LED
171  * @hotplug_slot: slot to set attention LED on
172  * @status: value to set attention LED to (0 or 1)
173  *
174  * attention status LED, so we use a callback that
175  * was registered with us.  This allows hardware specific
176  * ACPI implementations to blink the light for us.
177  **/
178  static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
179  {
180 	int retval = -ENODEV;
181 
182  	dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
183 
184 	if (attention_info && try_module_get(attention_info->owner)) {
185 		retval = attention_info->set_attn(hotplug_slot, status);
186 		module_put(attention_info->owner);
187 	} else
188 		attention_info = NULL;
189 	return retval;
190  }
191 
192 
193 /**
194  * get_power_status - get power status of a slot
195  * @hotplug_slot: slot to get status
196  * @value: pointer to store status
197  *
198  * Some platforms may not implement _STA method properly.
199  * In that case, the value returned may not be reliable.
200  *
201  */
202 static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
203 {
204 	struct slot *slot = hotplug_slot->private;
205 
206 	dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
207 
208 	*value = acpiphp_get_power_status(slot->acpi_slot);
209 
210 	return 0;
211 }
212 
213 
214  /**
215  * get_attention_status - get attention LED status
216  * @hotplug_slot: slot to get status from
217  * @value: returns with value of attention LED
218  *
219  * ACPI doesn't have known method to determine the state
220  * of the attention status LED, so we use a callback that
221  * was registered with us.  This allows hardware specific
222  * ACPI implementations to determine its state
223  **/
224 static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
225 {
226 	int retval = -EINVAL;
227 
228 	dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
229 
230 	if (attention_info && try_module_get(attention_info->owner)) {
231 		retval = attention_info->get_attn(hotplug_slot, value);
232 		module_put(attention_info->owner);
233 	} else
234 		attention_info = NULL;
235 	return retval;
236 }
237 
238 
239 /**
240  * get_latch_status - get latch status of a slot
241  * @hotplug_slot: slot to get status
242  * @value: pointer to store status
243  *
244  * ACPI doesn't provide any formal means to access latch status.
245  * Instead, we fake latch status from _STA
246  *
247  */
248 static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
249 {
250 	struct slot *slot = hotplug_slot->private;
251 
252 	dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
253 
254 	*value = acpiphp_get_latch_status(slot->acpi_slot);
255 
256 	return 0;
257 }
258 
259 
260 /**
261  * get_adapter_status - get adapter status of a slot
262  * @hotplug_slot: slot to get status
263  * @value: pointer to store status
264  *
265  * ACPI doesn't provide any formal means to access adapter status.
266  * Instead, we fake adapter status from _STA
267  *
268  */
269 static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
270 {
271 	struct slot *slot = hotplug_slot->private;
272 
273 	dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
274 
275 	*value = acpiphp_get_adapter_status(slot->acpi_slot);
276 
277 	return 0;
278 }
279 
280 
281 /**
282  * get_address - get pci address of a slot
283  * @hotplug_slot: slot to get status
284  * @busdev: pointer to struct pci_busdev (seg, bus, dev)
285  *
286  */
287 static int get_address(struct hotplug_slot *hotplug_slot, u32 *value)
288 {
289 	struct slot *slot = hotplug_slot->private;
290 
291 	dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
292 
293 	*value = acpiphp_get_address(slot->acpi_slot);
294 
295 	return 0;
296 }
297 
298 static int __init init_acpi(void)
299 {
300 	int retval;
301 
302 	/* initialize internal data structure etc. */
303 	retval = acpiphp_glue_init();
304 
305 	/* read initial number of slots */
306 	if (!retval) {
307 		num_slots = acpiphp_get_num_slots();
308 		if (num_slots == 0)
309 			retval = -ENODEV;
310 	}
311 
312 	return retval;
313 }
314 
315 
316 /**
317  * make_slot_name - make a slot name that appears in pcihpfs
318  * @slot: slot to name
319  *
320  */
321 static void make_slot_name(struct slot *slot)
322 {
323 	snprintf(slot->hotplug_slot->name, SLOT_NAME_SIZE, "%u",
324 		 slot->acpi_slot->sun);
325 }
326 
327 /**
328  * release_slot - free up the memory used by a slot
329  * @hotplug_slot: slot to free
330  */
331 static void release_slot(struct hotplug_slot *hotplug_slot)
332 {
333 	struct slot *slot = hotplug_slot->private;
334 
335 	dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
336 
337 	kfree(slot->hotplug_slot->info);
338 	kfree(slot->hotplug_slot->name);
339 	kfree(slot->hotplug_slot);
340 	kfree(slot);
341 }
342 
343 /**
344  * init_slots - initialize 'struct slot' structures for each slot
345  *
346  */
347 static int __init init_slots(void)
348 {
349 	struct slot *slot;
350 	int retval = -ENOMEM;
351 	int i;
352 
353 	for (i = 0; i < num_slots; ++i) {
354 		slot = kmalloc(sizeof(struct slot), GFP_KERNEL);
355 		if (!slot)
356 			goto error;
357 		memset(slot, 0, sizeof(struct slot));
358 
359 		slot->hotplug_slot = kmalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
360 		if (!slot->hotplug_slot)
361 			goto error_slot;
362 		memset(slot->hotplug_slot, 0, sizeof(struct hotplug_slot));
363 
364 		slot->hotplug_slot->info = kmalloc(sizeof(struct hotplug_slot_info), GFP_KERNEL);
365 		if (!slot->hotplug_slot->info)
366 			goto error_hpslot;
367 		memset(slot->hotplug_slot->info, 0, sizeof(struct hotplug_slot_info));
368 
369 		slot->hotplug_slot->name = kmalloc(SLOT_NAME_SIZE, GFP_KERNEL);
370 		if (!slot->hotplug_slot->name)
371 			goto error_info;
372 
373 		slot->number = i;
374 
375 		slot->hotplug_slot->private = slot;
376 		slot->hotplug_slot->release = &release_slot;
377 		slot->hotplug_slot->ops = &acpi_hotplug_slot_ops;
378 
379 		slot->acpi_slot = get_slot_from_id(i);
380 		slot->hotplug_slot->info->power_status = acpiphp_get_power_status(slot->acpi_slot);
381 		slot->hotplug_slot->info->attention_status = 0;
382 		slot->hotplug_slot->info->latch_status = acpiphp_get_latch_status(slot->acpi_slot);
383 		slot->hotplug_slot->info->adapter_status = acpiphp_get_adapter_status(slot->acpi_slot);
384 		slot->hotplug_slot->info->max_bus_speed = PCI_SPEED_UNKNOWN;
385 		slot->hotplug_slot->info->cur_bus_speed = PCI_SPEED_UNKNOWN;
386 
387 		make_slot_name(slot);
388 
389 		retval = pci_hp_register(slot->hotplug_slot);
390 		if (retval) {
391 			err("pci_hp_register failed with error %d\n", retval);
392 			goto error_name;
393 		}
394 
395 		/* add slot to our internal list */
396 		list_add(&slot->slot_list, &slot_list);
397 		info("Slot [%s] registered\n", slot->hotplug_slot->name);
398 	}
399 
400 	return 0;
401 error_name:
402 	kfree(slot->hotplug_slot->name);
403 error_info:
404 	kfree(slot->hotplug_slot->info);
405 error_hpslot:
406 	kfree(slot->hotplug_slot);
407 error_slot:
408 	kfree(slot);
409 error:
410 	return retval;
411 }
412 
413 
414 static void __exit cleanup_slots (void)
415 {
416 	struct list_head *tmp, *n;
417 	struct slot *slot;
418 
419 	list_for_each_safe (tmp, n, &slot_list) {
420 		/* memory will be freed in release_slot callback */
421 		slot = list_entry(tmp, struct slot, slot_list);
422 		list_del(&slot->slot_list);
423 		pci_hp_deregister(slot->hotplug_slot);
424 	}
425 }
426 
427 
428 static int __init acpiphp_init(void)
429 {
430 	int retval;
431 
432 	info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
433 
434 	acpiphp_debug = debug;
435 
436 	/* read all the ACPI info from the system */
437 	retval = init_acpi();
438 	if (retval)
439 		return retval;
440 
441 	return init_slots();
442 }
443 
444 
445 static void __exit acpiphp_exit(void)
446 {
447 	cleanup_slots();
448 	/* deallocate internal data structures etc. */
449 	acpiphp_glue_exit();
450 }
451 
452 module_init(acpiphp_init);
453 module_exit(acpiphp_exit);
454