Blame view

drivers/acpi/pci_slot.c 10.1 KB
8344b568f   Alex Chiang   PCI: ACPI PCI slo...
1
2
3
4
5
6
7
8
  /*
   *  pci_slot.c - ACPI PCI Slot Driver
   *
   *  The code here is heavily leveraged from the acpiphp module.
   *  Thanks to Matthew Wilcox <matthew@wil.cx> for much guidance.
   *  Thanks to Kenji Kaneshige <kaneshige.kenji@jp.fujitsu.com> for code
   *  review and fixes.
   *
e4268aad4   Alex Chiang   PCI hotplug: fix ...
9
10
   *  Copyright (C) 2007-2008 Hewlett-Packard Development Company, L.P.
   *  	Alex Chiang <achiang@hp.com>
8344b568f   Alex Chiang   PCI: ACPI PCI slo...
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
   *
   *  This program is free software; you can redistribute it and/or modify it
   *  under the terms and conditions of the GNU General Public License,
   *  version 2, as published by the Free Software Foundation.
   *
   *  This program is distributed in the hope that it will be useful, but
   *  WITHOUT ANY WARRANTY; without even the implied warranty of
   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   *  General Public License for more details.
   *
   *  You should have received a copy of the GNU General Public License along
   *  with this program; if not, write to the Free Software Foundation, Inc.,
   *  51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
   */
  
  #include <linux/kernel.h>
  #include <linux/module.h>
  #include <linux/init.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
29
  #include <linux/slab.h>
8344b568f   Alex Chiang   PCI: ACPI PCI slo...
30
31
32
33
34
  #include <linux/types.h>
  #include <linux/pci.h>
  #include <linux/acpi.h>
  #include <acpi/acpi_bus.h>
  #include <acpi/acpi_drivers.h>
eb27cae8a   Len Brown   ACPI: linux/acpi....
35
  #include <linux/dmi.h>
8344b568f   Alex Chiang   PCI: ACPI PCI slo...
36

90ab5ee94   Rusty Russell   module_param: mak...
37
  static bool debug;
8344b568f   Alex Chiang   PCI: ACPI PCI slo...
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
  static int check_sta_before_sun;
  
  #define DRIVER_VERSION 	"0.1"
  #define DRIVER_AUTHOR	"Alex Chiang <achiang@hp.com>"
  #define DRIVER_DESC	"ACPI PCI Slot Detection Driver"
  MODULE_AUTHOR(DRIVER_AUTHOR);
  MODULE_DESCRIPTION(DRIVER_DESC);
  MODULE_LICENSE("GPL");
  MODULE_PARM_DESC(debug, "Debugging mode enabled or not");
  module_param(debug, bool, 0644);
  
  #define _COMPONENT		ACPI_PCI_COMPONENT
  ACPI_MODULE_NAME("pci_slot");
  
  #define MY_NAME "pci_slot"
  #define err(format, arg...) printk(KERN_ERR "%s: " format , MY_NAME , ## arg)
  #define info(format, arg...) printk(KERN_INFO "%s: " format , MY_NAME , ## arg)
  #define dbg(format, arg...)					\
  	do {							\
  		if (debug)					\
  			printk(KERN_DEBUG "%s: " format,	\
  				MY_NAME , ## arg);		\
  	} while (0)
7e24bc1ce   Alex Chiang   ACPI: pci_slot.ko...
61
  #define SLOT_NAME_SIZE 21		/* Inspired by #define in acpiphp.h */
8344b568f   Alex Chiang   PCI: ACPI PCI slo...
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
  
  struct acpi_pci_slot {
  	acpi_handle root_handle;	/* handle of the root bridge */
  	struct pci_slot *pci_slot;	/* corresponding pci_slot */
  	struct list_head list;		/* node in the list of slots */
  };
  
  static int acpi_pci_slot_add(acpi_handle handle);
  static void acpi_pci_slot_remove(acpi_handle handle);
  
  static LIST_HEAD(slot_list);
  static DEFINE_MUTEX(slot_list_lock);
  static struct acpi_pci_driver acpi_pci_slot_driver = {
  	.add = acpi_pci_slot_add,
  	.remove = acpi_pci_slot_remove,
  };
  
  static int
27663c585   Matthew Wilcox   ACPI: Change acpi...
80
  check_slot(acpi_handle handle, unsigned long long *sun)
8344b568f   Alex Chiang   PCI: ACPI PCI slo...
81
  {
362b7077a   Matthew Wilcox   PCI: fix bogus "'...
82
  	int device = -1;
27663c585   Matthew Wilcox   ACPI: Change acpi...
83
  	unsigned long long adr, sta;
8344b568f   Alex Chiang   PCI: ACPI PCI slo...
84
85
86
87
88
89
90
91
92
93
  	acpi_status status;
  	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  
  	acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  	dbg("Checking slot on path: %s
  ", (char *)buffer.pointer);
  
  	if (check_sta_before_sun) {
  		/* If SxFy doesn't have _STA, we just assume it's there */
  		status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
362b7077a   Matthew Wilcox   PCI: fix bogus "'...
94
  		if (ACPI_SUCCESS(status) && !(sta & ACPI_STA_DEVICE_PRESENT))
8344b568f   Alex Chiang   PCI: ACPI PCI slo...
95
  			goto out;
8344b568f   Alex Chiang   PCI: ACPI PCI slo...
96
97
98
99
100
101
  	}
  
  	status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
  	if (ACPI_FAILURE(status)) {
  		dbg("_ADR returned %d on %s
  ", status, (char *)buffer.pointer);
8344b568f   Alex Chiang   PCI: ACPI PCI slo...
102
103
  		goto out;
  	}
8344b568f   Alex Chiang   PCI: ACPI PCI slo...
104
105
106
107
108
  	/* No _SUN == not a slot == bail */
  	status = acpi_evaluate_integer(handle, "_SUN", NULL, sun);
  	if (ACPI_FAILURE(status)) {
  		dbg("_SUN returned %d on %s
  ", status, (char *)buffer.pointer);
8344b568f   Alex Chiang   PCI: ACPI PCI slo...
109
110
  		goto out;
  	}
362b7077a   Matthew Wilcox   PCI: fix bogus "'...
111
  	device = (adr >> 16) & 0xffff;
8344b568f   Alex Chiang   PCI: ACPI PCI slo...
112
113
  out:
  	kfree(buffer.pointer);
362b7077a   Matthew Wilcox   PCI: fix bogus "'...
114
  	return device;
8344b568f   Alex Chiang   PCI: ACPI PCI slo...
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
  }
  
  struct callback_args {
  	acpi_walk_callback	user_function;	/* only for walk_p2p_bridge */
  	struct pci_bus		*pci_bus;
  	acpi_handle		root_handle;
  };
  
  /*
   * register_slot
   *
   * Called once for each SxFy object in the namespace. Don't worry about
   * calling pci_create_slot multiple times for the same pci_bus:device,
   * since each subsequent call simply bumps the refcount on the pci_slot.
   *
   * The number of calls to pci_destroy_slot from unregister_slot is
   * symmetrical.
   */
  static acpi_status
  register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
  {
  	int device;
27663c585   Matthew Wilcox   ACPI: Change acpi...
137
  	unsigned long long sun;
8344b568f   Alex Chiang   PCI: ACPI PCI slo...
138
139
140
141
142
  	char name[SLOT_NAME_SIZE];
  	struct acpi_pci_slot *slot;
  	struct pci_slot *pci_slot;
  	struct callback_args *parent_context = context;
  	struct pci_bus *pci_bus = parent_context->pci_bus;
362b7077a   Matthew Wilcox   PCI: fix bogus "'...
143
144
  	device = check_slot(handle, &sun);
  	if (device < 0)
8344b568f   Alex Chiang   PCI: ACPI PCI slo...
145
146
147
148
149
150
151
152
  		return AE_OK;
  
  	slot = kmalloc(sizeof(*slot), GFP_KERNEL);
  	if (!slot) {
  		err("%s: cannot allocate memory
  ", __func__);
  		return AE_OK;
  	}
7e24bc1ce   Alex Chiang   ACPI: pci_slot.ko...
153
  	snprintf(name, sizeof(name), "%llu", sun);
828f37683   Alex Chiang   PCI: update pci_c...
154
  	pci_slot = pci_create_slot(pci_bus, device, name, NULL);
8344b568f   Alex Chiang   PCI: ACPI PCI slo...
155
156
157
158
  	if (IS_ERR(pci_slot)) {
  		err("pci_create_slot returned %ld
  ", PTR_ERR(pci_slot));
  		kfree(slot);
e4268aad4   Alex Chiang   PCI hotplug: fix ...
159
  		return AE_OK;
8344b568f   Alex Chiang   PCI: ACPI PCI slo...
160
161
162
163
164
165
166
167
  	}
  
  	slot->root_handle = parent_context->root_handle;
  	slot->pci_slot = pci_slot;
  	INIT_LIST_HEAD(&slot->list);
  	mutex_lock(&slot_list_lock);
  	list_add(&slot->list, &slot_list);
  	mutex_unlock(&slot_list_lock);
72800360f   Alex Chiang   PCI: pci_slot: gr...
168
  	get_device(&pci_bus->dev);
8344b568f   Alex Chiang   PCI: ACPI PCI slo...
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
  	dbg("pci_slot: %p, pci_bus: %x, device: %d, name: %s
  ",
  		pci_slot, pci_bus->number, device, name);
  
  	return AE_OK;
  }
  
  /*
   * walk_p2p_bridge - discover and walk p2p bridges
   * @handle: points to an acpi_pci_root
   * @context: p2p_bridge_context pointer
   *
   * Note that when we call ourselves recursively, we pass a different
   * value of pci_bus in the child_context.
   */
  static acpi_status
  walk_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
  {
  	int device, function;
27663c585   Matthew Wilcox   ACPI: Change acpi...
188
  	unsigned long long adr;
8344b568f   Alex Chiang   PCI: ACPI PCI slo...
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
  	acpi_status status;
  	acpi_handle dummy_handle;
  	acpi_walk_callback user_function;
  
  	struct pci_dev *dev;
  	struct pci_bus *pci_bus;
  	struct callback_args child_context;
  	struct callback_args *parent_context = context;
  
  	pci_bus = parent_context->pci_bus;
  	user_function = parent_context->user_function;
  
  	status = acpi_get_handle(handle, "_ADR", &dummy_handle);
  	if (ACPI_FAILURE(status))
  		return AE_OK;
  
  	status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
  	if (ACPI_FAILURE(status))
  		return AE_OK;
  
  	device = (adr >> 16) & 0xffff;
  	function = adr & 0xffff;
  
  	dev = pci_get_slot(pci_bus, PCI_DEVFN(device, function));
  	if (!dev || !dev->subordinate)
  		goto out;
  
  	child_context.pci_bus = dev->subordinate;
  	child_context.user_function = user_function;
  	child_context.root_handle = parent_context->root_handle;
  
  	dbg("p2p bridge walk, pci_bus = %x
  ", dev->subordinate->number);
  	status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
2263576cf   Lin Ming   ACPICA: Add post-...
223
  				     user_function, NULL, &child_context, NULL);
8344b568f   Alex Chiang   PCI: ACPI PCI slo...
224
225
226
227
  	if (ACPI_FAILURE(status))
  		goto out;
  
  	status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
2263576cf   Lin Ming   ACPICA: Add post-...
228
  				     walk_p2p_bridge, NULL, &child_context, NULL);
8344b568f   Alex Chiang   PCI: ACPI PCI slo...
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
  out:
  	pci_dev_put(dev);
  	return AE_OK;
  }
  
  /*
   * walk_root_bridge - generic root bridge walker
   * @handle: points to an acpi_pci_root
   * @user_function: user callback for slot objects
   *
   * Call user_function for all objects underneath this root bridge.
   * Walk p2p bridges underneath us and call user_function on those too.
   */
  static int
  walk_root_bridge(acpi_handle handle, acpi_walk_callback user_function)
  {
  	int seg, bus;
27663c585   Matthew Wilcox   ACPI: Change acpi...
246
  	unsigned long long tmp;
8344b568f   Alex Chiang   PCI: ACPI PCI slo...
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
  	acpi_status status;
  	acpi_handle dummy_handle;
  	struct pci_bus *pci_bus;
  	struct callback_args context;
  
  	/* If the bridge doesn't have _STA, we assume it is always there */
  	status = acpi_get_handle(handle, "_STA", &dummy_handle);
  	if (ACPI_SUCCESS(status)) {
  		status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
  		if (ACPI_FAILURE(status)) {
  			info("%s: _STA evaluation failure
  ", __func__);
  			return 0;
  		}
  		if ((tmp & ACPI_STA_DEVICE_FUNCTIONING) == 0)
  			/* don't register this object */
  			return 0;
  	}
  
  	status = acpi_evaluate_integer(handle, "_SEG", NULL, &tmp);
  	seg = ACPI_SUCCESS(status) ? tmp : 0;
  
  	status = acpi_evaluate_integer(handle, "_BBN", NULL, &tmp);
  	bus = ACPI_SUCCESS(status) ? tmp : 0;
  
  	pci_bus = pci_find_bus(seg, bus);
  	if (!pci_bus)
  		return 0;
  
  	context.pci_bus = pci_bus;
  	context.user_function = user_function;
  	context.root_handle = handle;
  
  	dbg("root bridge walk, pci_bus = %x
  ", pci_bus->number);
  	status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
2263576cf   Lin Ming   ACPICA: Add post-...
283
  				     user_function, NULL, &context, NULL);
8344b568f   Alex Chiang   PCI: ACPI PCI slo...
284
285
286
287
  	if (ACPI_FAILURE(status))
  		return status;
  
  	status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
2263576cf   Lin Ming   ACPICA: Add post-...
288
  				     walk_p2p_bridge, NULL, &context, NULL);
8344b568f   Alex Chiang   PCI: ACPI PCI slo...
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
  	if (ACPI_FAILURE(status))
  		err("%s: walk_p2p_bridge failure - %d
  ", __func__, status);
  
  	return status;
  }
  
  /*
   * acpi_pci_slot_add
   * @handle: points to an acpi_pci_root
   */
  static int
  acpi_pci_slot_add(acpi_handle handle)
  {
  	acpi_status status;
  
  	status = walk_root_bridge(handle, register_slot);
  	if (ACPI_FAILURE(status))
  		err("%s: register_slot failure - %d
  ", __func__, status);
  
  	return status;
  }
  
  /*
   * acpi_pci_slot_remove
   * @handle: points to an acpi_pci_root
   */
  static void
  acpi_pci_slot_remove(acpi_handle handle)
  {
  	struct acpi_pci_slot *slot, *tmp;
72800360f   Alex Chiang   PCI: pci_slot: gr...
321
  	struct pci_bus *pbus;
8344b568f   Alex Chiang   PCI: ACPI PCI slo...
322
323
324
325
326
  
  	mutex_lock(&slot_list_lock);
  	list_for_each_entry_safe(slot, tmp, &slot_list, list) {
  		if (slot->root_handle == handle) {
  			list_del(&slot->list);
72800360f   Alex Chiang   PCI: pci_slot: gr...
327
  			pbus = slot->pci_slot->bus;
8344b568f   Alex Chiang   PCI: ACPI PCI slo...
328
  			pci_destroy_slot(slot->pci_slot);
72800360f   Alex Chiang   PCI: pci_slot: gr...
329
  			put_device(&pbus->dev);
8344b568f   Alex Chiang   PCI: ACPI PCI slo...
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
  			kfree(slot);
  		}
  	}
  	mutex_unlock(&slot_list_lock);
  }
  
  static int do_sta_before_sun(const struct dmi_system_id *d)
  {
  	info("%s detected: will evaluate _STA before calling _SUN
  ", d->ident);
  	check_sta_before_sun = 1;
  	return 0;
  }
  
  static struct dmi_system_id acpi_pci_slot_dmi_table[] __initdata = {
  	/*
  	 * Fujitsu Primequest machines will return 1023 to indicate an
  	 * error if the _SUN method is evaluated on SxFy objects that
  	 * are not present (as indicated by _STA), so for those machines,
  	 * we want to check _STA before evaluating _SUN.
  	 */
  	{
  	 .callback = do_sta_before_sun,
  	 .ident = "Fujitsu PRIMEQUEST",
  	 .matches = {
  		DMI_MATCH(DMI_BIOS_VENDOR, "FUJITSU LIMITED"),
  		DMI_MATCH(DMI_BIOS_VERSION, "PRIMEQUEST"),
  		},
  	},
  	{}
  };
  
  static int __init
  acpi_pci_slot_init(void)
  {
  	dmi_check_system(acpi_pci_slot_dmi_table);
  	acpi_pci_register_driver(&acpi_pci_slot_driver);
  	return 0;
  }
  
  static void __exit
  acpi_pci_slot_exit(void)
  {
  	acpi_pci_unregister_driver(&acpi_pci_slot_driver);
  }
  
  module_init(acpi_pci_slot_init);
  module_exit(acpi_pci_slot_exit);