]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/pci/hotplug/sgi_hotplug.c
PCI: cpcihp: Iterate over all devices in slot, not functions 0-7
[mirror_ubuntu-bionic-kernel.git] / drivers / pci / hotplug / sgi_hotplug.c
CommitLineData
6f354b01
PB
1/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
8e77af6a 6 * Copyright (C) 2005-2006 Silicon Graphics, Inc. All rights reserved.
6f354b01
PB
7 *
8 * This work was based on the 2.4/2.6 kernel development by Dick Reigner.
9 * Work to add BIOS PROM support was completed by Mike Habeck.
10 */
11
12#include <linux/init.h>
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/pci.h>
7a54f25c 16#include <linux/pci_hotplug.h>
6f354b01 17#include <linux/proc_fs.h>
5a0e3ad6 18#include <linux/slab.h>
6f354b01 19#include <linux/types.h>
14cc3e2b 20#include <linux/mutex.h>
6f354b01
PB
21
22#include <asm/sn/addrs.h>
e55dea58 23#include <asm/sn/geo.h>
6f354b01
PB
24#include <asm/sn/l1.h>
25#include <asm/sn/module.h>
26#include <asm/sn/pcibr_provider.h>
27#include <asm/sn/pcibus_provider_defs.h>
28#include <asm/sn/pcidev.h>
e55dea58 29#include <asm/sn/sn_feature_sets.h>
6f354b01
PB
30#include <asm/sn/sn_sal.h>
31#include <asm/sn/types.h>
3e643e77
JK
32#include <linux/acpi.h>
33#include <asm/sn/acpi.h>
6f354b01
PB
34
35#include "../pci.h"
6f354b01
PB
36
37MODULE_LICENSE("GPL");
38MODULE_AUTHOR("SGI (prarit@sgi.com, dickie@sgi.com, habeck@sgi.com)");
39MODULE_DESCRIPTION("SGI Altix Hot Plug PCI Controller Driver");
40
3e643e77
JK
41
42/* SAL call error codes. Keep in sync with prom header io/include/pcibr.h */
1d2450a4
PB
43#define PCI_SLOT_ALREADY_UP 2 /* slot already up */
44#define PCI_SLOT_ALREADY_DOWN 3 /* slot already down */
45#define PCI_L1_ERR 7 /* L1 console command error */
46#define PCI_EMPTY_33MHZ 15 /* empty 33 MHz bus */
3e643e77
JK
47
48
49#define PCIIO_ASIC_TYPE_TIOCA 4
1d2450a4
PB
50#define PCI_L1_QSIZE 128 /* our L1 message buffer size */
51#define SN_MAX_HP_SLOTS 32 /* max hotplug slots */
1d2450a4 52#define SN_SLOT_NAME_SIZE 33 /* size of name string */
6f354b01
PB
53
54/* internal list head */
55static struct list_head sn_hp_list;
56
57/* hotplug_slot struct's private pointer */
58struct slot {
59 int device_num;
60 struct pci_bus *pci_bus;
61 /* this struct for glue internal only */
62 struct hotplug_slot *hotplug_slot;
63 struct list_head hp_list;
1d2450a4 64 char physical_path[SN_SLOT_NAME_SIZE];
6f354b01
PB
65};
66
67struct pcibr_slot_enable_resp {
68 int resp_sub_errno;
69 char resp_l1_msg[PCI_L1_QSIZE + 1];
70};
71
72struct pcibr_slot_disable_resp {
73 int resp_sub_errno;
74 char resp_l1_msg[PCI_L1_QSIZE + 1];
75};
76
77enum sn_pci_req_e {
78 PCI_REQ_SLOT_ELIGIBLE,
79 PCI_REQ_SLOT_DISABLE
80};
81
82static int enable_slot(struct hotplug_slot *slot);
83static int disable_slot(struct hotplug_slot *slot);
1d2450a4 84static inline int get_power_status(struct hotplug_slot *slot, u8 *value);
6f354b01
PB
85
86static struct hotplug_slot_ops sn_hotplug_slot_ops = {
6f354b01
PB
87 .enable_slot = enable_slot,
88 .disable_slot = disable_slot,
89 .get_power_status = get_power_status,
90};
91
14cc3e2b 92static DEFINE_MUTEX(sn_hotplug_mutex);
6f354b01 93
94f81a47 94static ssize_t path_show(struct pci_slot *pci_slot, char *buf)
1d2450a4
PB
95{
96 int retval = -ENOENT;
94f81a47 97 struct slot *slot = pci_slot->hotplug->private;
1d2450a4
PB
98
99 if (!slot)
100 return retval;
101
102 retval = sprintf (buf, "%s\n", slot->physical_path);
103 return retval;
104}
105
94f81a47 106static struct pci_slot_attribute sn_slot_path_attr = __ATTR_RO(path);
1d2450a4 107
6f354b01
PB
108static int sn_pci_slot_valid(struct pci_bus *pci_bus, int device)
109{
110 struct pcibus_info *pcibus_info;
e55dea58 111 u16 busnum, segment, ioboard_type;
6f354b01
PB
112
113 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(pci_bus);
114
115 /* Check to see if this is a valid slot on 'pci_bus' */
116 if (!(pcibus_info->pbi_valid_devices & (1 << device)))
117 return -EPERM;
118
e55dea58
PB
119 ioboard_type = sn_ioboard_to_pci_bus(pci_bus);
120 busnum = pcibus_info->pbi_buscommon.bs_persist_busnum;
121 segment = pci_domain_nr(pci_bus) & 0xf;
6f354b01
PB
122
123 /* Do not allow hotplug operations on base I/O cards */
e55dea58
PB
124 if ((ioboard_type == L1_BRICKTYPE_IX ||
125 ioboard_type == L1_BRICKTYPE_IA) &&
126 (segment == 1 && busnum == 0 && device != 1))
6f354b01
PB
127 return -EPERM;
128
129 return 1;
130}
131
132static int sn_pci_bus_valid(struct pci_bus *pci_bus)
133{
134 struct pcibus_info *pcibus_info;
e55dea58
PB
135 u32 asic_type;
136 u16 ioboard_type;
6f354b01
PB
137
138 /* Don't register slots hanging off the TIOCA bus */
e55dea58 139 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(pci_bus);
6f354b01
PB
140 asic_type = pcibus_info->pbi_buscommon.bs_asic_type;
141 if (asic_type == PCIIO_ASIC_TYPE_TIOCA)
142 return -EPERM;
143
144 /* Only register slots in I/O Bricks that support hotplug */
e55dea58
PB
145 ioboard_type = sn_ioboard_to_pci_bus(pci_bus);
146 switch (ioboard_type) {
1d2450a4
PB
147 case L1_BRICKTYPE_IX:
148 case L1_BRICKTYPE_PX:
149 case L1_BRICKTYPE_IA:
150 case L1_BRICKTYPE_PA:
e55dea58 151 case L1_BOARDTYPE_PCIX3SLOT:
1d2450a4
PB
152 return 1;
153 break;
154 default:
155 return -EPERM;
156 break;
6f354b01
PB
157 }
158
159 return -EIO;
160}
161
162static int sn_hp_slot_private_alloc(struct hotplug_slot *bss_hotplug_slot,
85234ce8
AC
163 struct pci_bus *pci_bus, int device,
164 char *name)
6f354b01
PB
165{
166 struct pcibus_info *pcibus_info;
167 struct slot *slot;
168
169 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(pci_bus);
170
656da9da 171 slot = kzalloc(sizeof(*slot), GFP_KERNEL);
1d2450a4 172 if (!slot)
6f354b01 173 return -ENOMEM;
1d2450a4 174 bss_hotplug_slot->private = slot;
6f354b01 175
6f354b01
PB
176 slot->device_num = device;
177 slot->pci_bus = pci_bus;
85234ce8 178 sprintf(name, "%04x:%02x:%02x",
1d2450a4 179 pci_domain_nr(pci_bus),
e55dea58 180 ((u16)pcibus_info->pbi_buscommon.bs_persist_busnum),
1d2450a4 181 device + 1);
e55dea58
PB
182
183 sn_generate_path(pci_bus, slot->physical_path);
184
6f354b01
PB
185 slot->hotplug_slot = bss_hotplug_slot;
186 list_add(&slot->hp_list, &sn_hp_list);
187
188 return 0;
189}
190
191static struct hotplug_slot * sn_hp_destroy(void)
192{
193 struct slot *slot;
f46753c5 194 struct pci_slot *pci_slot;
6f354b01
PB
195 struct hotplug_slot *bss_hotplug_slot = NULL;
196
1d2450a4 197 list_for_each_entry(slot, &sn_hp_list, hp_list) {
6f354b01 198 bss_hotplug_slot = slot->hotplug_slot;
f46753c5 199 pci_slot = bss_hotplug_slot->pci_slot;
6f354b01
PB
200 list_del(&((struct slot *)bss_hotplug_slot->private)->
201 hp_list);
f46753c5 202 sysfs_remove_file(&pci_slot->kobj,
1d2450a4 203 &sn_slot_path_attr.attr);
6f354b01
PB
204 break;
205 }
206 return bss_hotplug_slot;
207}
208
6f354b01
PB
209static void sn_bus_free_data(struct pci_dev *dev)
210{
6f354b01
PB
211 struct pci_bus *subordinate_bus;
212 struct pci_dev *child;
213
214 /* Recursively clean up sn_irq_info structs */
215 if (dev->subordinate) {
216 subordinate_bus = dev->subordinate;
1d2450a4 217 list_for_each_entry(child, &subordinate_bus->devices, bus_list)
6f354b01 218 sn_bus_free_data(child);
6f354b01 219 }
8e77af6a
JK
220 /*
221 * Some drivers may use dma accesses during the
222 * driver remove function. We release the sysdata
223 * areas after the driver remove functions have
224 * been called.
225 */
226 sn_bus_store_sysdata(dev);
6f354b01
PB
227 sn_pci_unfixup_slot(dev);
228}
229
6f354b01 230static int sn_slot_enable(struct hotplug_slot *bss_hotplug_slot,
3e643e77 231 int device_num, char **ssdt)
6f354b01 232{
1d2450a4 233 struct slot *slot = bss_hotplug_slot->private;
6f354b01
PB
234 struct pcibus_info *pcibus_info;
235 struct pcibr_slot_enable_resp resp;
236 int rc;
237
238 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
239
240 /*
241 * Power-on and initialize the slot in the SN
242 * PCI infrastructure.
243 */
3e643e77
JK
244 rc = sal_pcibr_slot_enable(pcibus_info, device_num, &resp, ssdt);
245
6f354b01
PB
246
247 if (rc == PCI_SLOT_ALREADY_UP) {
34ef30ca 248 dev_dbg(&slot->pci_bus->self->dev, "is already active\n");
1d2450a4 249 return 1; /* return 1 to user */
6f354b01
PB
250 }
251
252 if (rc == PCI_L1_ERR) {
34ef30ca 253 dev_dbg(&slot->pci_bus->self->dev,
6f354b01
PB
254 "L1 failure %d with message: %s",
255 resp.resp_sub_errno, resp.resp_l1_msg);
256 return -EPERM;
257 }
258
259 if (rc) {
34ef30ca 260 dev_dbg(&slot->pci_bus->self->dev,
6f354b01
PB
261 "insert failed with error %d sub-error %d\n",
262 rc, resp.resp_sub_errno);
263 return -EIO;
264 }
265
1d2450a4
PB
266 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
267 pcibus_info->pbi_enabled_devices |= (1 << device_num);
6f354b01
PB
268
269 return 0;
270}
271
272static int sn_slot_disable(struct hotplug_slot *bss_hotplug_slot,
273 int device_num, int action)
274{
1d2450a4 275 struct slot *slot = bss_hotplug_slot->private;
6f354b01
PB
276 struct pcibus_info *pcibus_info;
277 struct pcibr_slot_disable_resp resp;
278 int rc;
279
280 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
281
282 rc = sal_pcibr_slot_disable(pcibus_info, device_num, action, &resp);
283
1d2450a4
PB
284 if ((action == PCI_REQ_SLOT_ELIGIBLE) &&
285 (rc == PCI_SLOT_ALREADY_DOWN)) {
34ef30ca 286 dev_dbg(&slot->pci_bus->self->dev, "Slot %s already inactive\n", slot->physical_path);
1d2450a4 287 return 1; /* return 1 to user */
6f354b01
PB
288 }
289
1d2450a4 290 if ((action == PCI_REQ_SLOT_ELIGIBLE) && (rc == PCI_EMPTY_33MHZ)) {
34ef30ca 291 dev_dbg(&slot->pci_bus->self->dev,
6f354b01
PB
292 "Cannot remove last 33MHz card\n");
293 return -EPERM;
294 }
295
1d2450a4 296 if ((action == PCI_REQ_SLOT_ELIGIBLE) && (rc == PCI_L1_ERR)) {
34ef30ca 297 dev_dbg(&slot->pci_bus->self->dev,
6f354b01
PB
298 "L1 failure %d with message \n%s\n",
299 resp.resp_sub_errno, resp.resp_l1_msg);
300 return -EPERM;
301 }
302
1d2450a4 303 if ((action == PCI_REQ_SLOT_ELIGIBLE) && rc) {
34ef30ca 304 dev_dbg(&slot->pci_bus->self->dev,
6f354b01
PB
305 "remove failed with error %d sub-error %d\n",
306 rc, resp.resp_sub_errno);
307 return -EIO;
308 }
309
1d2450a4 310 if ((action == PCI_REQ_SLOT_ELIGIBLE) && !rc)
6f354b01
PB
311 return 0;
312
1d2450a4
PB
313 if ((action == PCI_REQ_SLOT_DISABLE) && !rc) {
314 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
315 pcibus_info->pbi_enabled_devices &= ~(1 << device_num);
34ef30ca 316 dev_dbg(&slot->pci_bus->self->dev, "remove successful\n");
6f354b01
PB
317 return 0;
318 }
319
1d2450a4 320 if ((action == PCI_REQ_SLOT_DISABLE) && rc) {
34ef30ca 321 dev_dbg(&slot->pci_bus->self->dev,"remove failed rc = %d\n", rc);
6f354b01
PB
322 }
323
324 return rc;
325}
326
9f581f16
JK
327/*
328 * Power up and configure the slot via a SAL call to PROM.
329 * Scan slot (and any children), do any platform specific fixup,
330 * and find device driver.
331 */
6f354b01
PB
332static int enable_slot(struct hotplug_slot *bss_hotplug_slot)
333{
1d2450a4 334 struct slot *slot = bss_hotplug_slot->private;
6f354b01
PB
335 struct pci_bus *new_bus = NULL;
336 struct pci_dev *dev;
337 int func, num_funcs;
338 int new_ppb = 0;
339 int rc;
3e643e77 340 char *ssdt = NULL;
9f581f16 341 void pcibios_fixup_device_resources(struct pci_dev *);
6f354b01
PB
342
343 /* Serialize the Linux PCI infrastructure */
14cc3e2b 344 mutex_lock(&sn_hotplug_mutex);
6f354b01
PB
345
346 /*
347 * Power-on and initialize the slot in the SN
3e643e77
JK
348 * PCI infrastructure. Also, retrieve the ACPI SSDT
349 * table for the slot (if ACPI capable PROM).
6f354b01 350 */
3e643e77 351 rc = sn_slot_enable(bss_hotplug_slot, slot->device_num, &ssdt);
6f354b01 352 if (rc) {
14cc3e2b 353 mutex_unlock(&sn_hotplug_mutex);
6f354b01
PB
354 return rc;
355 }
356
3e643e77
JK
357 if (ssdt)
358 ssdt = __va(ssdt);
359 /* Add the new SSDT for the slot to the ACPI namespace */
360 if (SN_ACPI_BASE_SUPPORT() && ssdt) {
361 acpi_status ret;
362
363 ret = acpi_load_table((struct acpi_table_header *)ssdt);
364 if (ACPI_FAILURE(ret)) {
365 printk(KERN_ERR "%s: acpi_load_table failed (0x%x)\n",
66bef8c0 366 __func__, ret);
3e643e77
JK
367 /* try to continue on */
368 }
369 }
370
1d2450a4
PB
371 num_funcs = pci_scan_slot(slot->pci_bus,
372 PCI_DEVFN(slot->device_num + 1, 0));
6f354b01 373 if (!num_funcs) {
34ef30ca 374 dev_dbg(&slot->pci_bus->self->dev, "no device in slot\n");
14cc3e2b 375 mutex_unlock(&sn_hotplug_mutex);
6f354b01
PB
376 return -ENODEV;
377 }
378
6f354b01
PB
379 /*
380 * Map SN resources for all functions on the card
381 * to the Linux PCI interface and tell the drivers
382 * about them.
383 */
384 for (func = 0; func < num_funcs; func++) {
385 dev = pci_get_slot(slot->pci_bus,
386 PCI_DEVFN(slot->device_num + 1,
387 PCI_FUNC(func)));
6f354b01 388 if (dev) {
9f581f16
JK
389 /* Need to do slot fixup on PPB before fixup of children
390 * (PPB's pcidev_info needs to be in pcidev_info list
391 * before child's SN_PCIDEV_INFO() call to setup
392 * pdi_host_pcidev_info).
393 */
394 pcibios_fixup_device_resources(dev);
3e643e77
JK
395 if (SN_ACPI_BASE_SUPPORT())
396 sn_acpi_slot_fixup(dev);
397 else
398 sn_io_slot_fixup(dev);
6f354b01 399 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
69ba29b9
YL
400 pci_hp_add_bridge(dev);
401 if (dev->subordinate) {
402 new_bus = dev->subordinate;
403 new_ppb = 1;
404 }
6f354b01 405 }
6f354b01
PB
406 pci_dev_put(dev);
407 }
408 }
409
3e643e77
JK
410 /*
411 * Add the slot's devices to the ACPI infrastructure */
412 if (SN_ACPI_BASE_SUPPORT() && ssdt) {
27663c58 413 unsigned long long adr;
3e643e77
JK
414 struct acpi_device *pdevice;
415 struct acpi_device *device;
416 acpi_handle phandle;
417 acpi_handle chandle = NULL;
418 acpi_handle rethandle;
419 acpi_status ret;
420
421 phandle = PCI_CONTROLLER(slot->pci_bus)->acpi_handle;
422
423 if (acpi_bus_get_device(phandle, &pdevice)) {
34ef30ca 424 dev_dbg(&slot->pci_bus->self->dev,
3e643e77
JK
425 "no parent device, assuming NULL\n");
426 pdevice = NULL;
427 }
428
429 /*
430 * Walk the rootbus node's immediate children looking for
431 * the slot's device node(s). There can be more than
432 * one for multifunction devices.
433 */
434 for (;;) {
435 rethandle = NULL;
436 ret = acpi_get_next_object(ACPI_TYPE_DEVICE,
437 phandle, chandle,
438 &rethandle);
439
440 if (ret == AE_NOT_FOUND || rethandle == NULL)
441 break;
442
443 chandle = rethandle;
444
445 ret = acpi_evaluate_integer(chandle, METHOD_NAME__ADR,
446 NULL, &adr);
447
448 if (ACPI_SUCCESS(ret) &&
449 (adr>>16) == (slot->device_num + 1)) {
450
451 ret = acpi_bus_add(&device, pdevice, chandle,
452 ACPI_BUS_TYPE_DEVICE);
453 if (ACPI_FAILURE(ret)) {
454 printk(KERN_ERR "%s: acpi_bus_add "
455 "failed (0x%x) for slot %d "
66bef8c0 456 "func %d\n", __func__,
3e643e77
JK
457 ret, (int)(adr>>16),
458 (int)(adr&0xffff));
459 /* try to continue on */
460 } else {
461 acpi_bus_start(device);
462 }
463 }
464 }
465 }
466
6f354b01
PB
467 /* Call the driver for the new device */
468 pci_bus_add_devices(slot->pci_bus);
469 /* Call the drivers for the new devices subordinate to PPB */
470 if (new_ppb)
471 pci_bus_add_devices(new_bus);
472
14cc3e2b 473 mutex_unlock(&sn_hotplug_mutex);
6f354b01
PB
474
475 if (rc == 0)
34ef30ca 476 dev_dbg(&slot->pci_bus->self->dev,
6f354b01
PB
477 "insert operation successful\n");
478 else
34ef30ca 479 dev_dbg(&slot->pci_bus->self->dev,
6f354b01
PB
480 "insert operation failed rc = %d\n", rc);
481
482 return rc;
483}
484
485static int disable_slot(struct hotplug_slot *bss_hotplug_slot)
486{
1d2450a4 487 struct slot *slot = bss_hotplug_slot->private;
6f354b01
PB
488 struct pci_dev *dev;
489 int func;
490 int rc;
3e643e77 491 acpi_owner_id ssdt_id = 0;
6f354b01
PB
492
493 /* Acquire update access to the bus */
14cc3e2b 494 mutex_lock(&sn_hotplug_mutex);
6f354b01
PB
495
496 /* is it okay to bring this slot down? */
497 rc = sn_slot_disable(bss_hotplug_slot, slot->device_num,
498 PCI_REQ_SLOT_ELIGIBLE);
499 if (rc)
500 goto leaving;
501
3e643e77
JK
502 /* free the ACPI resources for the slot */
503 if (SN_ACPI_BASE_SUPPORT() &&
504 PCI_CONTROLLER(slot->pci_bus)->acpi_handle) {
27663c58 505 unsigned long long adr;
3e643e77
JK
506 struct acpi_device *device;
507 acpi_handle phandle;
508 acpi_handle chandle = NULL;
509 acpi_handle rethandle;
510 acpi_status ret;
511
512 /* Get the rootbus node pointer */
513 phandle = PCI_CONTROLLER(slot->pci_bus)->acpi_handle;
514
515 /*
516 * Walk the rootbus node's immediate children looking for
517 * the slot's device node(s). There can be more than
518 * one for multifunction devices.
519 */
520 for (;;) {
521 rethandle = NULL;
522 ret = acpi_get_next_object(ACPI_TYPE_DEVICE,
523 phandle, chandle,
524 &rethandle);
525
526 if (ret == AE_NOT_FOUND || rethandle == NULL)
527 break;
528
529 chandle = rethandle;
530
531 ret = acpi_evaluate_integer(chandle,
532 METHOD_NAME__ADR,
533 NULL, &adr);
534 if (ACPI_SUCCESS(ret) &&
535 (adr>>16) == (slot->device_num + 1)) {
536 /* retain the owner id */
537 acpi_get_id(chandle, &ssdt_id);
538
539 ret = acpi_bus_get_device(chandle,
540 &device);
541 if (ACPI_SUCCESS(ret))
542 acpi_bus_trim(device, 1);
543 }
544 }
545
546 }
547
6f354b01
PB
548 /* Free the SN resources assigned to the Linux device.*/
549 for (func = 0; func < 8; func++) {
550 dev = pci_get_slot(slot->pci_bus,
1d2450a4 551 PCI_DEVFN(slot->device_num + 1,
6f354b01
PB
552 PCI_FUNC(func)));
553 if (dev) {
6f354b01 554 sn_bus_free_data(dev);
210647af 555 pci_stop_and_remove_bus_device(dev);
6f354b01
PB
556 pci_dev_put(dev);
557 }
558 }
559
3e643e77
JK
560 /* Remove the SSDT for the slot from the ACPI namespace */
561 if (SN_ACPI_BASE_SUPPORT() && ssdt_id) {
562 acpi_status ret;
563 ret = acpi_unload_table_id(ssdt_id);
564 if (ACPI_FAILURE(ret)) {
565 printk(KERN_ERR "%s: acpi_unload_table_id "
566 "failed (0x%x) for id %d\n",
66bef8c0 567 __func__, ret, ssdt_id);
3e643e77
JK
568 /* try to continue on */
569 }
570 }
571
6f354b01
PB
572 /* free the collected sysdata pointers */
573 sn_bus_free_sysdata();
574
575 /* Deactivate slot */
576 rc = sn_slot_disable(bss_hotplug_slot, slot->device_num,
577 PCI_REQ_SLOT_DISABLE);
578 leaving:
579 /* Release the bus lock */
14cc3e2b 580 mutex_unlock(&sn_hotplug_mutex);
6f354b01
PB
581
582 return rc;
583}
584
1d2450a4
PB
585static inline int get_power_status(struct hotplug_slot *bss_hotplug_slot,
586 u8 *value)
6f354b01 587{
1d2450a4
PB
588 struct slot *slot = bss_hotplug_slot->private;
589 struct pcibus_info *pcibus_info;
466ee36b 590 u32 power;
1d2450a4
PB
591
592 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
14cc3e2b 593 mutex_lock(&sn_hotplug_mutex);
466ee36b
MH
594 power = pcibus_info->pbi_enabled_devices & (1 << slot->device_num);
595 *value = power ? 1 : 0;
14cc3e2b 596 mutex_unlock(&sn_hotplug_mutex);
6f354b01
PB
597 return 0;
598}
599
600static void sn_release_slot(struct hotplug_slot *bss_hotplug_slot)
601{
602 kfree(bss_hotplug_slot->info);
6f354b01
PB
603 kfree(bss_hotplug_slot->private);
604 kfree(bss_hotplug_slot);
605}
606
607static int sn_hotplug_slot_register(struct pci_bus *pci_bus)
608{
609 int device;
f46753c5 610 struct pci_slot *pci_slot;
6f354b01 611 struct hotplug_slot *bss_hotplug_slot;
85234ce8 612 char name[SN_SLOT_NAME_SIZE];
6f354b01
PB
613 int rc = 0;
614
615 /*
616 * Currently only four devices are supported,
617 * in the future there maybe more -- up to 32.
618 */
619
620 for (device = 0; device < SN_MAX_HP_SLOTS ; device++) {
621 if (sn_pci_slot_valid(pci_bus, device) != 1)
622 continue;
623
656da9da 624 bss_hotplug_slot = kzalloc(sizeof(*bss_hotplug_slot),
6f354b01
PB
625 GFP_KERNEL);
626 if (!bss_hotplug_slot) {
627 rc = -ENOMEM;
628 goto alloc_err;
629 }
630
631 bss_hotplug_slot->info =
656da9da 632 kzalloc(sizeof(struct hotplug_slot_info),
6f354b01
PB
633 GFP_KERNEL);
634 if (!bss_hotplug_slot->info) {
635 rc = -ENOMEM;
636 goto alloc_err;
637 }
638
639 if (sn_hp_slot_private_alloc(bss_hotplug_slot,
85234ce8 640 pci_bus, device, name)) {
6f354b01
PB
641 rc = -ENOMEM;
642 goto alloc_err;
643 }
6f354b01
PB
644 bss_hotplug_slot->ops = &sn_hotplug_slot_ops;
645 bss_hotplug_slot->release = &sn_release_slot;
646
85234ce8 647 rc = pci_hp_register(bss_hotplug_slot, pci_bus, device, name);
6f354b01
PB
648 if (rc)
649 goto register_err;
1d2450a4 650
f46753c5
AC
651 pci_slot = bss_hotplug_slot->pci_slot;
652 rc = sysfs_create_file(&pci_slot->kobj,
1d2450a4
PB
653 &sn_slot_path_attr.attr);
654 if (rc)
655 goto register_err;
6f354b01 656 }
34ef30ca 657 dev_dbg(&pci_bus->self->dev, "Registered bus with hotplug\n");
6f354b01
PB
658 return rc;
659
660register_err:
34ef30ca 661 dev_dbg(&pci_bus->self->dev, "bus failed to register with err = %d\n",
8344b568 662 rc);
6f354b01
PB
663
664alloc_err:
665 if (rc == -ENOMEM)
34ef30ca 666 dev_dbg(&pci_bus->self->dev, "Memory allocation error\n");
6f354b01
PB
667
668 /* destroy THIS element */
669 if (bss_hotplug_slot)
670 sn_release_slot(bss_hotplug_slot);
671
672 /* destroy anything else on the list */
673 while ((bss_hotplug_slot = sn_hp_destroy()))
674 pci_hp_deregister(bss_hotplug_slot);
675
676 return rc;
677}
678
db5ed9be 679static int __init sn_pci_hotplug_init(void)
6f354b01
PB
680{
681 struct pci_bus *pci_bus = NULL;
682 int rc;
683 int registered = 0;
684
e55dea58
PB
685 if (!sn_prom_feature_available(PRF_HOTPLUG_SUPPORT)) {
686 printk(KERN_ERR "%s: PROM version does not support hotplug.\n",
66bef8c0 687 __func__);
6f354b01
PB
688 return -EPERM;
689 }
690
1d2450a4
PB
691 INIT_LIST_HEAD(&sn_hp_list);
692
6f354b01
PB
693 while ((pci_bus = pci_find_next_bus(pci_bus))) {
694 if (!pci_bus->sysdata)
695 continue;
696
697 rc = sn_pci_bus_valid(pci_bus);
698 if (rc != 1) {
34ef30ca 699 dev_dbg(&pci_bus->self->dev, "not a valid hotplug bus\n");
6f354b01
PB
700 continue;
701 }
34ef30ca 702 dev_dbg(&pci_bus->self->dev, "valid hotplug bus\n");
6f354b01
PB
703
704 rc = sn_hotplug_slot_register(pci_bus);
1d2450a4 705 if (!rc) {
6f354b01 706 registered = 1;
1d2450a4 707 } else {
6f354b01
PB
708 registered = 0;
709 break;
710 }
711 }
712
713 return registered == 1 ? 0 : -ENODEV;
714}
715
db5ed9be 716static void __exit sn_pci_hotplug_exit(void)
6f354b01
PB
717{
718 struct hotplug_slot *bss_hotplug_slot;
719
1d2450a4 720 while ((bss_hotplug_slot = sn_hp_destroy()))
6f354b01 721 pci_hp_deregister(bss_hotplug_slot);
6f354b01
PB
722
723 if (!list_empty(&sn_hp_list))
724 printk(KERN_ERR "%s: internal list is not empty\n", __FILE__);
725}
726
727module_init(sn_pci_hotplug_init);
728module_exit(sn_pci_hotplug_exit);