PCI hotplug: fix wrong assumption in acpi_get_hp_params_from_firmware
[deliverable/linux.git] / drivers / pci / hotplug / acpi_pcihp.c
CommitLineData
1da177e4 1/*
783c49fc 2 * Common ACPI functions for hot plug platforms
1da177e4 3 *
783c49fc 4 * Copyright (C) 2006 Intel Corporation
1da177e4
LT
5 *
6 * All rights reserved.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or (at
11 * your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
16 * NON INFRINGEMENT. See the GNU General Public License for more
17 * details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
8cf4c195 23 * Send feedback to <kristen.c.accardi@intel.com>
1da177e4
LT
24 *
25 */
26
1da177e4 27#include <linux/module.h>
aad20cab 28#include <linux/moduleparam.h>
1da177e4
LT
29#include <linux/kernel.h>
30#include <linux/types.h>
31#include <linux/pci.h>
7a54f25c 32#include <linux/pci_hotplug.h>
9f5404d8 33#include <linux/acpi.h>
ac9c052d 34#include <linux/pci-acpi.h>
1da177e4 35
aad20cab
KK
36#define MY_NAME "acpi_pcihp"
37
66bef8c0 38#define dbg(fmt, arg...) do { if (debug_acpi) printk(KERN_DEBUG "%s: %s: " fmt , MY_NAME , __func__ , ## arg); } while (0)
aad20cab
KK
39#define err(format, arg...) printk(KERN_ERR "%s: " format , MY_NAME , ## arg)
40#define info(format, arg...) printk(KERN_INFO "%s: " format , MY_NAME , ## arg)
41#define warn(format, arg...) printk(KERN_WARNING "%s: " format , MY_NAME , ## arg)
42
1da177e4
LT
43#define METHOD_NAME__SUN "_SUN"
44#define METHOD_NAME__HPP "_HPP"
45#define METHOD_NAME_OSHP "OSHP"
46
aad20cab
KK
47static int debug_acpi;
48
e22b7350
KK
49static acpi_status
50decode_type0_hpx_record(union acpi_object *record, struct hotplug_params *hpx)
51{
52 int i;
53 union acpi_object *fields = record->package.elements;
54 u32 revision = fields[1].integer.value;
55
56 switch (revision) {
57 case 1:
58 if (record->package.count != 6)
59 return AE_ERROR;
60 for (i = 2; i < 6; i++)
61 if (fields[i].type != ACPI_TYPE_INTEGER)
62 return AE_ERROR;
63 hpx->t0 = &hpx->type0_data;
64 hpx->t0->revision = revision;
65 hpx->t0->cache_line_size = fields[2].integer.value;
66 hpx->t0->latency_timer = fields[3].integer.value;
67 hpx->t0->enable_serr = fields[4].integer.value;
68 hpx->t0->enable_perr = fields[5].integer.value;
69 break;
70 default:
71 printk(KERN_WARNING
72 "%s: Type 0 Revision %d record not supported\n",
66bef8c0 73 __func__, revision);
e22b7350
KK
74 return AE_ERROR;
75 }
76 return AE_OK;
77}
78
79static acpi_status
80decode_type1_hpx_record(union acpi_object *record, struct hotplug_params *hpx)
81{
82 int i;
83 union acpi_object *fields = record->package.elements;
84 u32 revision = fields[1].integer.value;
85
86 switch (revision) {
87 case 1:
88 if (record->package.count != 5)
89 return AE_ERROR;
90 for (i = 2; i < 5; i++)
91 if (fields[i].type != ACPI_TYPE_INTEGER)
92 return AE_ERROR;
93 hpx->t1 = &hpx->type1_data;
94 hpx->t1->revision = revision;
95 hpx->t1->max_mem_read = fields[2].integer.value;
96 hpx->t1->avg_max_split = fields[3].integer.value;
97 hpx->t1->tot_max_split = fields[4].integer.value;
98 break;
99 default:
100 printk(KERN_WARNING
101 "%s: Type 1 Revision %d record not supported\n",
66bef8c0 102 __func__, revision);
e22b7350
KK
103 return AE_ERROR;
104 }
105 return AE_OK;
106}
107
108static acpi_status
109decode_type2_hpx_record(union acpi_object *record, struct hotplug_params *hpx)
110{
111 int i;
112 union acpi_object *fields = record->package.elements;
113 u32 revision = fields[1].integer.value;
114
115 switch (revision) {
116 case 1:
117 if (record->package.count != 18)
118 return AE_ERROR;
119 for (i = 2; i < 18; i++)
120 if (fields[i].type != ACPI_TYPE_INTEGER)
121 return AE_ERROR;
122 hpx->t2 = &hpx->type2_data;
123 hpx->t2->revision = revision;
124 hpx->t2->unc_err_mask_and = fields[2].integer.value;
125 hpx->t2->unc_err_mask_or = fields[3].integer.value;
126 hpx->t2->unc_err_sever_and = fields[4].integer.value;
127 hpx->t2->unc_err_sever_or = fields[5].integer.value;
128 hpx->t2->cor_err_mask_and = fields[6].integer.value;
129 hpx->t2->cor_err_mask_or = fields[7].integer.value;
130 hpx->t2->adv_err_cap_and = fields[8].integer.value;
131 hpx->t2->adv_err_cap_or = fields[9].integer.value;
132 hpx->t2->pci_exp_devctl_and = fields[10].integer.value;
133 hpx->t2->pci_exp_devctl_or = fields[11].integer.value;
134 hpx->t2->pci_exp_lnkctl_and = fields[12].integer.value;
135 hpx->t2->pci_exp_lnkctl_or = fields[13].integer.value;
136 hpx->t2->sec_unc_err_sever_and = fields[14].integer.value;
137 hpx->t2->sec_unc_err_sever_or = fields[15].integer.value;
138 hpx->t2->sec_unc_err_mask_and = fields[16].integer.value;
139 hpx->t2->sec_unc_err_mask_or = fields[17].integer.value;
140 break;
141 default:
142 printk(KERN_WARNING
143 "%s: Type 2 Revision %d record not supported\n",
66bef8c0 144 __func__, revision);
e22b7350
KK
145 return AE_ERROR;
146 }
147 return AE_OK;
148}
149
150static acpi_status
151acpi_run_hpx(acpi_handle handle, struct hotplug_params *hpx)
152{
153 acpi_status status;
154 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
155 union acpi_object *package, *record, *fields;
156 u32 type;
157 int i;
158
159 /* Clear the return buffer with zeros */
160 memset(hpx, 0, sizeof(struct hotplug_params));
161
162 status = acpi_evaluate_object(handle, "_HPX", NULL, &buffer);
163 if (ACPI_FAILURE(status))
164 return status;
165
166 package = (union acpi_object *)buffer.pointer;
167 if (package->type != ACPI_TYPE_PACKAGE) {
168 status = AE_ERROR;
169 goto exit;
170 }
171
172 for (i = 0; i < package->package.count; i++) {
173 record = &package->package.elements[i];
174 if (record->type != ACPI_TYPE_PACKAGE) {
175 status = AE_ERROR;
176 goto exit;
177 }
178
179 fields = record->package.elements;
180 if (fields[0].type != ACPI_TYPE_INTEGER ||
181 fields[1].type != ACPI_TYPE_INTEGER) {
182 status = AE_ERROR;
183 goto exit;
184 }
185
186 type = fields[0].integer.value;
187 switch (type) {
188 case 0:
189 status = decode_type0_hpx_record(record, hpx);
190 if (ACPI_FAILURE(status))
191 goto exit;
192 break;
193 case 1:
194 status = decode_type1_hpx_record(record, hpx);
195 if (ACPI_FAILURE(status))
196 goto exit;
197 break;
198 case 2:
199 status = decode_type2_hpx_record(record, hpx);
200 if (ACPI_FAILURE(status))
201 goto exit;
202 break;
203 default:
204 printk(KERN_ERR "%s: Type %d record not supported\n",
66bef8c0 205 __func__, type);
e22b7350
KK
206 status = AE_ERROR;
207 goto exit;
208 }
209 }
210 exit:
211 kfree(buffer.pointer);
212 return status;
213}
1da177e4 214
a8a2be94 215static acpi_status
216acpi_run_hpp(acpi_handle handle, struct hotplug_params *hpp)
1da177e4
LT
217{
218 acpi_status status;
219 u8 nui[4];
220 struct acpi_buffer ret_buf = { 0, NULL};
b2e6e3ba 221 struct acpi_buffer string = { ACPI_ALLOCATE_BUFFER, NULL };
1da177e4 222 union acpi_object *ext_obj, *package;
1da177e4
LT
223 int i, len = 0;
224
b2e6e3ba
MT
225 acpi_get_name(handle, ACPI_FULL_PATHNAME, &string);
226
e22b7350
KK
227 /* Clear the return buffer with zeros */
228 memset(hpp, 0, sizeof(struct hotplug_params));
229
1da177e4 230 /* get _hpp */
a8a2be94 231 status = acpi_evaluate_object(handle, METHOD_NAME__HPP, NULL, &ret_buf);
1da177e4
LT
232 switch (status) {
233 case AE_BUFFER_OVERFLOW:
234 ret_buf.pointer = kmalloc (ret_buf.length, GFP_KERNEL);
235 if (!ret_buf.pointer) {
783c49fc 236 printk(KERN_ERR "%s:%s alloc for _HPP fail\n",
66bef8c0 237 __func__, (char *)string.pointer);
81b26bca 238 kfree(string.pointer);
a8a2be94 239 return AE_NO_MEMORY;
1da177e4 240 }
a8a2be94 241 status = acpi_evaluate_object(handle, METHOD_NAME__HPP,
242 NULL, &ret_buf);
1da177e4
LT
243 if (ACPI_SUCCESS(status))
244 break;
245 default:
246 if (ACPI_FAILURE(status)) {
66bef8c0 247 pr_debug("%s:%s _HPP fail=0x%x\n", __func__,
b2e6e3ba 248 (char *)string.pointer, status);
81b26bca 249 kfree(string.pointer);
a8a2be94 250 return status;
1da177e4
LT
251 }
252 }
253
254 ext_obj = (union acpi_object *) ret_buf.pointer;
255 if (ext_obj->type != ACPI_TYPE_PACKAGE) {
66bef8c0 256 printk(KERN_ERR "%s:%s _HPP obj not a package\n", __func__,
b2e6e3ba 257 (char *)string.pointer);
a8a2be94 258 status = AE_ERROR;
1da177e4
LT
259 goto free_and_return;
260 }
261
262 len = ext_obj->package.count;
263 package = (union acpi_object *) ret_buf.pointer;
264 for ( i = 0; (i < len) || (i < 4); i++) {
265 ext_obj = (union acpi_object *) &package->package.elements[i];
266 switch (ext_obj->type) {
267 case ACPI_TYPE_INTEGER:
268 nui[i] = (u8)ext_obj->integer.value;
269 break;
270 default:
783c49fc 271 printk(KERN_ERR "%s:%s _HPP obj type incorrect\n",
66bef8c0 272 __func__, (char *)string.pointer);
a8a2be94 273 status = AE_ERROR;
1da177e4
LT
274 goto free_and_return;
275 }
276 }
277
e22b7350
KK
278 hpp->t0 = &hpp->type0_data;
279 hpp->t0->cache_line_size = nui[0];
280 hpp->t0->latency_timer = nui[1];
281 hpp->t0->enable_serr = nui[2];
282 hpp->t0->enable_perr = nui[3];
1da177e4 283
e22b7350
KK
284 pr_debug(" _HPP: cache_line_size=0x%x\n", hpp->t0->cache_line_size);
285 pr_debug(" _HPP: latency timer =0x%x\n", hpp->t0->latency_timer);
286 pr_debug(" _HPP: enable SERR =0x%x\n", hpp->t0->enable_serr);
287 pr_debug(" _HPP: enable PERR =0x%x\n", hpp->t0->enable_perr);
1da177e4
LT
288
289free_and_return:
81b26bca
KA
290 kfree(string.pointer);
291 kfree(ret_buf.pointer);
a8a2be94 292 return status;
1da177e4
LT
293}
294
783c49fc
KA
295
296
297/* acpi_run_oshp - get control of hotplug from the firmware
298 *
299 * @handle - the handle of the hotplug controller.
300 */
ac9c052d 301static acpi_status acpi_run_oshp(acpi_handle handle)
1da177e4
LT
302{
303 acpi_status status;
b2e6e3ba
MT
304 struct acpi_buffer string = { ACPI_ALLOCATE_BUFFER, NULL };
305
306 acpi_get_name(handle, ACPI_FULL_PATHNAME, &string);
1da177e4
LT
307
308 /* run OSHP */
a8a2be94 309 status = acpi_evaluate_object(handle, METHOD_NAME_OSHP, NULL, NULL);
783c49fc 310 if (ACPI_FAILURE(status))
aad20cab
KK
311 if (status != AE_NOT_FOUND)
312 printk(KERN_ERR "%s:%s OSHP fails=0x%x\n",
66bef8c0 313 __func__, (char *)string.pointer, status);
aad20cab
KK
314 else
315 dbg("%s:%s OSHP not found\n",
66bef8c0 316 __func__, (char *)string.pointer);
783c49fc 317 else
66bef8c0 318 pr_debug("%s:%s OSHP passes\n", __func__,
b2e6e3ba
MT
319 (char *)string.pointer);
320
81b26bca 321 kfree(string.pointer);
783c49fc
KA
322 return status;
323}
783c49fc
KA
324
325/* acpi_get_hp_params_from_firmware
326 *
7430e34c 327 * @bus - the pci_bus of the bus on which the device is newly added
783c49fc
KA
328 * @hpp - allocated by the caller
329 */
7430e34c 330acpi_status acpi_get_hp_params_from_firmware(struct pci_bus *bus,
783c49fc
KA
331 struct hotplug_params *hpp)
332{
333 acpi_status status = AE_NOT_FOUND;
7430e34c 334 acpi_handle handle, phandle;
267efd7e
KK
335 struct pci_bus *pbus;
336
337 handle = NULL;
338 for (pbus = bus; pbus; pbus = pbus->parent) {
339 handle = acpi_pci_get_bridge_handle(pbus);
340 if (handle)
7430e34c 341 break;
267efd7e 342 }
783c49fc
KA
343
344 /*
345 * _HPP settings apply to all child buses, until another _HPP is
346 * encountered. If we don't find an _HPP for the input pci dev,
347 * look for it in the parent device scope since that would apply to
348 * this pci dev. If we don't find any _HPP, use hardcoded defaults
349 */
7430e34c 350 while (handle) {
e22b7350
KK
351 status = acpi_run_hpx(handle, hpp);
352 if (ACPI_SUCCESS(status))
353 break;
783c49fc 354 status = acpi_run_hpp(handle, hpp);
7430e34c
KK
355 if (ACPI_SUCCESS(status))
356 break;
357 if (acpi_root_bridge(handle))
358 break;
359 status = acpi_get_parent(handle, &phandle);
360 if (ACPI_FAILURE(status))
783c49fc 361 break;
7430e34c 362 handle = phandle;
1da177e4 363 }
a8a2be94 364 return status;
1da177e4 365}
783c49fc
KA
366EXPORT_SYMBOL_GPL(acpi_get_hp_params_from_firmware);
367
ac9c052d
KK
368/**
369 * acpi_get_hp_hw_control_from_firmware
370 * @dev: the pci_dev of the bridge that has a hotplug controller
371 * @flags: requested control bits for _OSC
372 *
373 * Attempt to take hotplug control from firmware.
374 */
375int acpi_get_hp_hw_control_from_firmware(struct pci_dev *dev, u32 flags)
376{
377 acpi_status status;
e0d94bee 378 acpi_handle chandle, handle;
ac9c052d
KK
379 struct pci_dev *pdev = dev;
380 struct pci_bus *parent;
381 struct acpi_buffer string = { ACPI_ALLOCATE_BUFFER, NULL };
382
383 flags &= (OSC_PCI_EXPRESS_NATIVE_HP_CONTROL |
384 OSC_SHPC_NATIVE_HP_CONTROL |
385 OSC_PCI_EXPRESS_CAP_STRUCTURE_CONTROL);
386 if (!flags) {
387 err("Invalid flags %u specified!\n", flags);
388 return -EINVAL;
389 }
390
391 /*
392 * Per PCI firmware specification, we should run the ACPI _OSC
393 * method to get control of hotplug hardware before using it. If
394 * an _OSC is missing, we look for an OSHP to do the same thing.
e0d94bee
JS
395 * To handle different BIOS behavior, we look for _OSC on a root
396 * bridge preferentially (according to PCI fw spec). Later for
397 * OSHP within the scope of the hotplug controller and its parents,
ac9c052d
KK
398 * upto the host bridge under which this controller exists.
399 */
056c58e8 400 handle = acpi_find_root_bridge_handle(pdev);
e0d94bee
JS
401 if (handle) {
402 acpi_get_name(handle, ACPI_FULL_PATHNAME, &string);
403 dbg("Trying to get hotplug control for %s\n",
404 (char *)string.pointer);
9f5404d8 405 status = acpi_pci_osc_control_set(handle, flags);
e0d94bee
JS
406 if (ACPI_SUCCESS(status))
407 goto got_one;
408 kfree(string.pointer);
409 string = (struct acpi_buffer){ ACPI_ALLOCATE_BUFFER, NULL };
410 }
411
412 pdev = dev;
413 handle = DEVICE_ACPI_HANDLE(&dev->dev);
ac9c052d
KK
414 while (!handle) {
415 /*
416 * This hotplug controller was not listed in the ACPI name
417 * space at all. Try to get acpi handle of parent pci bus.
418 */
419 if (!pdev || !pdev->bus->parent)
420 break;
421 parent = pdev->bus->parent;
422 dbg("Could not find %s in acpi namespace, trying parent\n",
423 pci_name(pdev));
424 if (!parent->self)
425 /* Parent must be a host bridge */
426 handle = acpi_get_pci_rootbridge_handle(
427 pci_domain_nr(parent),
428 parent->number);
429 else
430 handle = DEVICE_ACPI_HANDLE(&(parent->self->dev));
431 pdev = parent->self;
432 }
433
434 while (handle) {
435 acpi_get_name(handle, ACPI_FULL_PATHNAME, &string);
436 dbg("Trying to get hotplug control for %s \n",
437 (char *)string.pointer);
e0d94bee
JS
438 status = acpi_run_oshp(handle);
439 if (ACPI_SUCCESS(status))
440 goto got_one;
ac9c052d
KK
441 if (acpi_root_bridge(handle))
442 break;
443 chandle = handle;
444 status = acpi_get_parent(chandle, &handle);
445 if (ACPI_FAILURE(status))
446 break;
447 }
448
449 dbg("Cannot get control of hotplug hardware for pci %s\n",
450 pci_name(dev));
451
452 kfree(string.pointer);
453 return -ENODEV;
e0d94bee
JS
454got_one:
455 dbg("Gained control for hotplug HW for pci %s (%s)\n", pci_name(dev),
456 (char *)string.pointer);
457 kfree(string.pointer);
458 return 0;
ac9c052d
KK
459}
460EXPORT_SYMBOL(acpi_get_hp_hw_control_from_firmware);
1da177e4 461
783c49fc
KA
462/* acpi_root_bridge - check to see if this acpi object is a root bridge
463 *
464 * @handle - the acpi object in question.
465 */
466int acpi_root_bridge(acpi_handle handle)
a3a45ec8 467{
468 acpi_status status;
469 struct acpi_device_info *info;
470 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
471 int i;
472
473 status = acpi_get_object_info(handle, &buffer);
474 if (ACPI_SUCCESS(status)) {
475 info = buffer.pointer;
476 if ((info->valid & ACPI_VALID_HID) &&
477 !strcmp(PCI_ROOT_HID_STRING,
478 info->hardware_id.value)) {
81b26bca 479 kfree(buffer.pointer);
a3a45ec8 480 return 1;
481 }
482 if (info->valid & ACPI_VALID_CID) {
483 for (i=0; i < info->compatibility_id.count; i++) {
484 if (!strcmp(PCI_ROOT_HID_STRING,
485 info->compatibility_id.id[i].value)) {
81b26bca 486 kfree(buffer.pointer);
a3a45ec8 487 return 1;
488 }
489 }
490 }
81b26bca 491 kfree(buffer.pointer);
a3a45ec8 492 }
493 return 0;
494}
783c49fc 495EXPORT_SYMBOL_GPL(acpi_root_bridge);
aad20cab 496
e8c331e9
KK
497
498static int is_ejectable(acpi_handle handle)
499{
500 acpi_status status;
501 acpi_handle tmp;
502 unsigned long long removable;
503 status = acpi_get_handle(handle, "_ADR", &tmp);
504 if (ACPI_FAILURE(status))
505 return 0;
506 status = acpi_get_handle(handle, "_EJ0", &tmp);
507 if (ACPI_SUCCESS(status))
508 return 1;
509 status = acpi_evaluate_integer(handle, "_RMV", NULL, &removable);
510 if (ACPI_SUCCESS(status) && removable)
511 return 1;
512 return 0;
513}
514
515/**
516 * acpi_pcihp_check_ejectable - check if handle is ejectable ACPI PCI slot
517 * @pbus: the PCI bus of the PCI slot corresponding to 'handle'
518 * @handle: ACPI handle to check
519 *
520 * Return 1 if handle is ejectable PCI slot, 0 otherwise.
521 */
522int acpi_pci_check_ejectable(struct pci_bus *pbus, acpi_handle handle)
523{
524 acpi_handle bridge_handle, parent_handle;
525
526 if (!(bridge_handle = acpi_pci_get_bridge_handle(pbus)))
527 return 0;
528 if ((ACPI_FAILURE(acpi_get_parent(handle, &parent_handle))))
529 return 0;
530 if (bridge_handle != parent_handle)
531 return 0;
532 return is_ejectable(handle);
533}
534EXPORT_SYMBOL_GPL(acpi_pci_check_ejectable);
535
536static acpi_status
537check_hotplug(acpi_handle handle, u32 lvl, void *context, void **rv)
538{
539 int *found = (int *)context;
540 if (is_ejectable(handle)) {
541 *found = 1;
542 return AE_CTRL_TERMINATE;
543 }
544 return AE_OK;
545}
546
547/**
548 * acpi_pci_detect_ejectable - check if the PCI bus has ejectable slots
549 * @pbus - PCI bus to scan
550 *
551 * Returns 1 if the PCI bus has ACPI based ejectable slots, 0 otherwise.
552 */
553int acpi_pci_detect_ejectable(struct pci_bus *pbus)
554{
555 acpi_handle handle;
556 int found = 0;
557
558 if (!(handle = acpi_pci_get_bridge_handle(pbus)))
559 return 0;
560 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
561 check_hotplug, (void *)&found, NULL);
562 return found;
563}
564EXPORT_SYMBOL_GPL(acpi_pci_detect_ejectable);
565
aad20cab
KK
566module_param(debug_acpi, bool, 0644);
567MODULE_PARM_DESC(debug_acpi, "Debugging mode for ACPI enabled or not");
This page took 0.48711 seconds and 5 git commands to generate.