xref: /linux/drivers/pci/hotplug/acpiphp_core.c (revision de2fe5e07d58424bc286fff3fd3c1b0bf933cd58)
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  * Copyright (C) 2003-2005 Matthew Wilcox (matthew.wilcox@hp.com)
11  * Copyright (C) 2003-2005 Hewlett Packard
12  *
13  * All rights reserved.
14  *
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 2 of the License, or (at
18  * your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful, but
21  * WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
23  * NON INFRINGEMENT.  See the GNU General Public License for more
24  * details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29  *
30  * Send feedback to <gregkh@us.ibm.com>,
31  *		    <t-kochi@bq.jp.nec.com>
32  *
33  */
34 
35 #include <linux/init.h>
36 #include <linux/module.h>
37 #include <linux/moduleparam.h>
38 
39 #include <linux/kernel.h>
40 #include <linux/pci.h>
41 #include <linux/slab.h>
42 #include <linux/smp.h>
43 #include <linux/smp_lock.h>
44 #include "pci_hotplug.h"
45 #include "acpiphp.h"
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.5"
57 #define DRIVER_AUTHOR	"Greg Kroah-Hartman <gregkh@us.ibm.com>, Takayoshi Kochi <t-kochi@bq.jp.nec.com>, Matthew Wilcox <willy@hp.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  * @value: pointer to struct pci_busdev (seg, bus, dev)
285  */
286 static int get_address(struct hotplug_slot *hotplug_slot, u32 *value)
287 {
288 	struct slot *slot = hotplug_slot->private;
289 
290 	dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
291 
292 	*value = acpiphp_get_address(slot->acpi_slot);
293 
294 	return 0;
295 }
296 
297 static int __init init_acpi(void)
298 {
299 	int retval;
300 
301 	/* initialize internal data structure etc. */
302 	retval = acpiphp_glue_init();
303 
304 	/* read initial number of slots */
305 	if (!retval) {
306 		num_slots = acpiphp_get_num_slots();
307 		if (num_slots == 0)
308 			retval = -ENODEV;
309 	}
310 
311 	return retval;
312 }
313 
314 
315 /**
316  * make_slot_name - make a slot name that appears in pcihpfs
317  * @slot: slot to name
318  *
319  */
320 static void make_slot_name(struct slot *slot)
321 {
322 	snprintf(slot->hotplug_slot->name, SLOT_NAME_SIZE, "%u",
323 		 slot->acpi_slot->sun);
324 }
325 
326 /**
327  * release_slot - free up the memory used by a slot
328  * @hotplug_slot: slot to free
329  */
330 static void release_slot(struct hotplug_slot *hotplug_slot)
331 {
332 	struct slot *slot = hotplug_slot->private;
333 
334 	dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
335 
336 	kfree(slot->hotplug_slot->info);
337 	kfree(slot->hotplug_slot->name);
338 	kfree(slot->hotplug_slot);
339 	kfree(slot);
340 }
341 
342 /* callback routine to initialize 'struct slot' for each slot */
343 int acpiphp_register_hotplug_slot(struct acpiphp_slot *acpiphp_slot)
344 {
345 	struct slot *slot;
346 	struct hotplug_slot *hotplug_slot;
347 	struct hotplug_slot_info *hotplug_slot_info;
348 	int retval = -ENOMEM;
349 
350 	slot = kzalloc(sizeof(*slot), GFP_KERNEL);
351 	if (!slot)
352 		goto error;
353 
354 	slot->hotplug_slot = kzalloc(sizeof(*hotplug_slot), GFP_KERNEL);
355 	if (!slot->hotplug_slot)
356 		goto error_slot;
357 
358 	slot->hotplug_slot->info = kzalloc(sizeof(*hotplug_slot_info),
359 					   GFP_KERNEL);
360 	if (!slot->hotplug_slot->info)
361 		goto error_hpslot;
362 
363 	slot->hotplug_slot->name = kzalloc(SLOT_NAME_SIZE, GFP_KERNEL);
364 	if (!slot->hotplug_slot->name)
365 		goto error_info;
366 
367 	slot->hotplug_slot->private = slot;
368 	slot->hotplug_slot->release = &release_slot;
369 	slot->hotplug_slot->ops = &acpi_hotplug_slot_ops;
370 
371 	slot->acpi_slot = acpiphp_slot;
372 	slot->hotplug_slot->info->power_status = acpiphp_get_power_status(slot->acpi_slot);
373 	slot->hotplug_slot->info->attention_status = 0;
374 	slot->hotplug_slot->info->latch_status = acpiphp_get_latch_status(slot->acpi_slot);
375 	slot->hotplug_slot->info->adapter_status = acpiphp_get_adapter_status(slot->acpi_slot);
376 	slot->hotplug_slot->info->max_bus_speed = PCI_SPEED_UNKNOWN;
377 	slot->hotplug_slot->info->cur_bus_speed = PCI_SPEED_UNKNOWN;
378 
379 	acpiphp_slot->slot = slot;
380 	make_slot_name(slot);
381 
382 	retval = pci_hp_register(slot->hotplug_slot);
383 	if (retval) {
384 		err("pci_hp_register failed with error %d\n", retval);
385 		goto error_name;
386  	}
387 
388 	info("Slot [%s] registered\n", slot->hotplug_slot->name);
389 
390 	return 0;
391 error_name:
392 	kfree(slot->hotplug_slot->name);
393 error_info:
394 	kfree(slot->hotplug_slot->info);
395 error_hpslot:
396 	kfree(slot->hotplug_slot);
397 error_slot:
398 	kfree(slot);
399 error:
400 	return retval;
401 }
402 
403 
404 void acpiphp_unregister_hotplug_slot(struct acpiphp_slot *acpiphp_slot)
405 {
406 	struct slot *slot = acpiphp_slot->slot;
407 	int retval = 0;
408 
409 	info ("Slot [%s] unregistered\n", slot->hotplug_slot->name);
410 
411 	retval = pci_hp_deregister(slot->hotplug_slot);
412 	if (retval)
413 		err("pci_hp_deregister failed with error %d\n", retval);
414 }
415 
416 
417 static int __init acpiphp_init(void)
418 {
419 	int retval;
420 	int docking_station;
421 
422 	info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
423 
424 	acpiphp_debug = debug;
425 
426 	docking_station = find_dock_station();
427 
428 	/* read all the ACPI info from the system */
429 	retval = init_acpi();
430 
431 	/* if we have found a docking station, we should
432 	 * go ahead and load even if init_acpi has found
433 	 * no slots.  This handles the case when the _DCK
434 	 * method not defined under the actual dock bridge
435 	 */
436 	if (docking_station)
437 		return 0;
438 	else
439 		return retval;
440 }
441 
442 
443 static void __exit acpiphp_exit(void)
444 {
445 	/* deallocate internal data structures etc. */
446 	acpiphp_glue_exit();
447 
448 	remove_dock_station();
449 }
450 
451 module_init(acpiphp_init);
452 module_exit(acpiphp_exit);
453