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
6 * Copyright (C) 2005-2006 Silicon Graphics, Inc. All rights reserved.
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.
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/pci.h>
16 #include <linux/pci_hotplug.h>
17 #include <linux/proc_fs.h>
18 #include <linux/types.h>
19 #include <linux/mutex.h>
21 #include <asm/sn/addrs.h>
22 #include <asm/sn/geo.h>
23 #include <asm/sn/l1.h>
24 #include <asm/sn/module.h>
25 #include <asm/sn/pcibr_provider.h>
26 #include <asm/sn/pcibus_provider_defs.h>
27 #include <asm/sn/pcidev.h>
28 #include <asm/sn/sn_feature_sets.h>
29 #include <asm/sn/sn_sal.h>
30 #include <asm/sn/types.h>
31 #include <linux/acpi.h>
32 #include <asm/sn/acpi.h>
36 MODULE_LICENSE("GPL");
37 MODULE_AUTHOR("SGI (prarit@sgi.com, dickie@sgi.com, habeck@sgi.com)");
38 MODULE_DESCRIPTION("SGI Altix Hot Plug PCI Controller Driver");
41 /* SAL call error codes. Keep in sync with prom header io/include/pcibr.h */
42 #define PCI_SLOT_ALREADY_UP 2 /* slot already up */
43 #define PCI_SLOT_ALREADY_DOWN 3 /* slot already down */
44 #define PCI_L1_ERR 7 /* L1 console command error */
45 #define PCI_EMPTY_33MHZ 15 /* empty 33 MHz bus */
48 #define PCIIO_ASIC_TYPE_TIOCA 4
49 #define PCI_L1_QSIZE 128 /* our L1 message buffer size */
50 #define SN_MAX_HP_SLOTS 32 /* max hotplug slots */
51 #define SN_SLOT_NAME_SIZE 33 /* size of name string */
53 /* internal list head */
54 static struct list_head sn_hp_list;
56 /* hotplug_slot struct's private pointer */
59 struct pci_bus *pci_bus;
60 /* this struct for glue internal only */
61 struct hotplug_slot *hotplug_slot;
62 struct list_head hp_list;
63 char physical_path[SN_SLOT_NAME_SIZE];
66 struct pcibr_slot_enable_resp {
68 char resp_l1_msg[PCI_L1_QSIZE + 1];
71 struct pcibr_slot_disable_resp {
73 char resp_l1_msg[PCI_L1_QSIZE + 1];
77 PCI_REQ_SLOT_ELIGIBLE,
81 static int enable_slot(struct hotplug_slot *slot);
82 static int disable_slot(struct hotplug_slot *slot);
83 static inline int get_power_status(struct hotplug_slot *slot, u8 *value);
85 static struct hotplug_slot_ops sn_hotplug_slot_ops = {
87 .enable_slot = enable_slot,
88 .disable_slot = disable_slot,
89 .get_power_status = get_power_status,
92 static DEFINE_MUTEX(sn_hotplug_mutex);
94 static ssize_t path_show (struct hotplug_slot *bss_hotplug_slot,
98 struct slot *slot = bss_hotplug_slot->private;
103 retval = sprintf (buf, "%s\n", slot->physical_path);
107 static struct hotplug_slot_attribute sn_slot_path_attr = __ATTR_RO(path);
109 static int sn_pci_slot_valid(struct pci_bus *pci_bus, int device)
111 struct pcibus_info *pcibus_info;
112 u16 busnum, segment, ioboard_type;
114 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(pci_bus);
116 /* Check to see if this is a valid slot on 'pci_bus' */
117 if (!(pcibus_info->pbi_valid_devices & (1 << device)))
120 ioboard_type = sn_ioboard_to_pci_bus(pci_bus);
121 busnum = pcibus_info->pbi_buscommon.bs_persist_busnum;
122 segment = pci_domain_nr(pci_bus) & 0xf;
124 /* Do not allow hotplug operations on base I/O cards */
125 if ((ioboard_type == L1_BRICKTYPE_IX ||
126 ioboard_type == L1_BRICKTYPE_IA) &&
127 (segment == 1 && busnum == 0 && device != 1))
133 static int sn_pci_bus_valid(struct pci_bus *pci_bus)
135 struct pcibus_info *pcibus_info;
139 /* Don't register slots hanging off the TIOCA bus */
140 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(pci_bus);
141 asic_type = pcibus_info->pbi_buscommon.bs_asic_type;
142 if (asic_type == PCIIO_ASIC_TYPE_TIOCA)
145 /* Only register slots in I/O Bricks that support hotplug */
146 ioboard_type = sn_ioboard_to_pci_bus(pci_bus);
147 switch (ioboard_type) {
148 case L1_BRICKTYPE_IX:
149 case L1_BRICKTYPE_PX:
150 case L1_BRICKTYPE_IA:
151 case L1_BRICKTYPE_PA:
152 case L1_BOARDTYPE_PCIX3SLOT:
163 static int sn_hp_slot_private_alloc(struct hotplug_slot *bss_hotplug_slot,
164 struct pci_bus *pci_bus, int device)
166 struct pcibus_info *pcibus_info;
169 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(pci_bus);
171 slot = kzalloc(sizeof(*slot), GFP_KERNEL);
174 bss_hotplug_slot->private = slot;
176 bss_hotplug_slot->name = kmalloc(SN_SLOT_NAME_SIZE, GFP_KERNEL);
177 if (!bss_hotplug_slot->name) {
178 kfree(bss_hotplug_slot->private);
182 slot->device_num = device;
183 slot->pci_bus = pci_bus;
184 sprintf(bss_hotplug_slot->name, "%04x:%02x:%02x",
185 pci_domain_nr(pci_bus),
186 ((u16)pcibus_info->pbi_buscommon.bs_persist_busnum),
189 sn_generate_path(pci_bus, slot->physical_path);
191 slot->hotplug_slot = bss_hotplug_slot;
192 list_add(&slot->hp_list, &sn_hp_list);
197 static struct hotplug_slot * sn_hp_destroy(void)
200 struct pci_slot *pci_slot;
201 struct hotplug_slot *bss_hotplug_slot = NULL;
203 list_for_each_entry(slot, &sn_hp_list, hp_list) {
204 bss_hotplug_slot = slot->hotplug_slot;
205 pci_slot = bss_hotplug_slot->pci_slot;
206 list_del(&((struct slot *)bss_hotplug_slot->private)->
208 sysfs_remove_file(&pci_slot->kobj,
209 &sn_slot_path_attr.attr);
212 return bss_hotplug_slot;
215 static void sn_bus_free_data(struct pci_dev *dev)
217 struct pci_bus *subordinate_bus;
218 struct pci_dev *child;
220 /* Recursively clean up sn_irq_info structs */
221 if (dev->subordinate) {
222 subordinate_bus = dev->subordinate;
223 list_for_each_entry(child, &subordinate_bus->devices, bus_list)
224 sn_bus_free_data(child);
227 * Some drivers may use dma accesses during the
228 * driver remove function. We release the sysdata
229 * areas after the driver remove functions have
232 sn_bus_store_sysdata(dev);
233 sn_pci_unfixup_slot(dev);
236 static int sn_slot_enable(struct hotplug_slot *bss_hotplug_slot,
237 int device_num, char **ssdt)
239 struct slot *slot = bss_hotplug_slot->private;
240 struct pcibus_info *pcibus_info;
241 struct pcibr_slot_enable_resp resp;
244 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
247 * Power-on and initialize the slot in the SN
248 * PCI infrastructure.
250 rc = sal_pcibr_slot_enable(pcibus_info, device_num, &resp, ssdt);
253 if (rc == PCI_SLOT_ALREADY_UP) {
254 dev_dbg(&slot->pci_bus->self->dev, "is already active\n");
255 return 1; /* return 1 to user */
258 if (rc == PCI_L1_ERR) {
259 dev_dbg(&slot->pci_bus->self->dev,
260 "L1 failure %d with message: %s",
261 resp.resp_sub_errno, resp.resp_l1_msg);
266 dev_dbg(&slot->pci_bus->self->dev,
267 "insert failed with error %d sub-error %d\n",
268 rc, resp.resp_sub_errno);
272 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
273 pcibus_info->pbi_enabled_devices |= (1 << device_num);
278 static int sn_slot_disable(struct hotplug_slot *bss_hotplug_slot,
279 int device_num, int action)
281 struct slot *slot = bss_hotplug_slot->private;
282 struct pcibus_info *pcibus_info;
283 struct pcibr_slot_disable_resp resp;
286 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
288 rc = sal_pcibr_slot_disable(pcibus_info, device_num, action, &resp);
290 if ((action == PCI_REQ_SLOT_ELIGIBLE) &&
291 (rc == PCI_SLOT_ALREADY_DOWN)) {
292 dev_dbg(&slot->pci_bus->self->dev, "Slot %s already inactive\n", slot->physical_path);
293 return 1; /* return 1 to user */
296 if ((action == PCI_REQ_SLOT_ELIGIBLE) && (rc == PCI_EMPTY_33MHZ)) {
297 dev_dbg(&slot->pci_bus->self->dev,
298 "Cannot remove last 33MHz card\n");
302 if ((action == PCI_REQ_SLOT_ELIGIBLE) && (rc == PCI_L1_ERR)) {
303 dev_dbg(&slot->pci_bus->self->dev,
304 "L1 failure %d with message \n%s\n",
305 resp.resp_sub_errno, resp.resp_l1_msg);
309 if ((action == PCI_REQ_SLOT_ELIGIBLE) && rc) {
310 dev_dbg(&slot->pci_bus->self->dev,
311 "remove failed with error %d sub-error %d\n",
312 rc, resp.resp_sub_errno);
316 if ((action == PCI_REQ_SLOT_ELIGIBLE) && !rc)
319 if ((action == PCI_REQ_SLOT_DISABLE) && !rc) {
320 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
321 pcibus_info->pbi_enabled_devices &= ~(1 << device_num);
322 dev_dbg(&slot->pci_bus->self->dev, "remove successful\n");
326 if ((action == PCI_REQ_SLOT_DISABLE) && rc) {
327 dev_dbg(&slot->pci_bus->self->dev,"remove failed rc = %d\n", rc);
334 * Power up and configure the slot via a SAL call to PROM.
335 * Scan slot (and any children), do any platform specific fixup,
336 * and find device driver.
338 static int enable_slot(struct hotplug_slot *bss_hotplug_slot)
340 struct slot *slot = bss_hotplug_slot->private;
341 struct pci_bus *new_bus = NULL;
347 void pcibios_fixup_device_resources(struct pci_dev *);
349 /* Serialize the Linux PCI infrastructure */
350 mutex_lock(&sn_hotplug_mutex);
353 * Power-on and initialize the slot in the SN
354 * PCI infrastructure. Also, retrieve the ACPI SSDT
355 * table for the slot (if ACPI capable PROM).
357 rc = sn_slot_enable(bss_hotplug_slot, slot->device_num, &ssdt);
359 mutex_unlock(&sn_hotplug_mutex);
365 /* Add the new SSDT for the slot to the ACPI namespace */
366 if (SN_ACPI_BASE_SUPPORT() && ssdt) {
369 ret = acpi_load_table((struct acpi_table_header *)ssdt);
370 if (ACPI_FAILURE(ret)) {
371 printk(KERN_ERR "%s: acpi_load_table failed (0x%x)\n",
373 /* try to continue on */
377 num_funcs = pci_scan_slot(slot->pci_bus,
378 PCI_DEVFN(slot->device_num + 1, 0));
380 dev_dbg(&slot->pci_bus->self->dev, "no device in slot\n");
381 mutex_unlock(&sn_hotplug_mutex);
386 * Map SN resources for all functions on the card
387 * to the Linux PCI interface and tell the drivers
390 for (func = 0; func < num_funcs; func++) {
391 dev = pci_get_slot(slot->pci_bus,
392 PCI_DEVFN(slot->device_num + 1,
395 /* Need to do slot fixup on PPB before fixup of children
396 * (PPB's pcidev_info needs to be in pcidev_info list
397 * before child's SN_PCIDEV_INFO() call to setup
398 * pdi_host_pcidev_info).
400 pcibios_fixup_device_resources(dev);
401 if (SN_ACPI_BASE_SUPPORT())
402 sn_acpi_slot_fixup(dev);
404 sn_io_slot_fixup(dev);
405 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
406 unsigned char sec_bus;
407 pci_read_config_byte(dev, PCI_SECONDARY_BUS,
409 new_bus = pci_add_new_bus(dev->bus, dev,
411 pci_scan_child_bus(new_bus);
419 * Add the slot's devices to the ACPI infrastructure */
420 if (SN_ACPI_BASE_SUPPORT() && ssdt) {
422 struct acpi_device *pdevice;
423 struct acpi_device *device;
425 acpi_handle chandle = NULL;
426 acpi_handle rethandle;
429 phandle = PCI_CONTROLLER(slot->pci_bus)->acpi_handle;
431 if (acpi_bus_get_device(phandle, &pdevice)) {
432 dev_dbg(&slot->pci_bus->self->dev,
433 "no parent device, assuming NULL\n");
438 * Walk the rootbus node's immediate children looking for
439 * the slot's device node(s). There can be more than
440 * one for multifunction devices.
444 ret = acpi_get_next_object(ACPI_TYPE_DEVICE,
448 if (ret == AE_NOT_FOUND || rethandle == NULL)
453 ret = acpi_evaluate_integer(chandle, METHOD_NAME__ADR,
456 if (ACPI_SUCCESS(ret) &&
457 (adr>>16) == (slot->device_num + 1)) {
459 ret = acpi_bus_add(&device, pdevice, chandle,
460 ACPI_BUS_TYPE_DEVICE);
461 if (ACPI_FAILURE(ret)) {
462 printk(KERN_ERR "%s: acpi_bus_add "
463 "failed (0x%x) for slot %d "
464 "func %d\n", __func__,
467 /* try to continue on */
469 acpi_bus_start(device);
475 /* Call the driver for the new device */
476 pci_bus_add_devices(slot->pci_bus);
477 /* Call the drivers for the new devices subordinate to PPB */
479 pci_bus_add_devices(new_bus);
481 mutex_unlock(&sn_hotplug_mutex);
484 dev_dbg(&slot->pci_bus->self->dev,
485 "insert operation successful\n");
487 dev_dbg(&slot->pci_bus->self->dev,
488 "insert operation failed rc = %d\n", rc);
493 static int disable_slot(struct hotplug_slot *bss_hotplug_slot)
495 struct slot *slot = bss_hotplug_slot->private;
499 acpi_owner_id ssdt_id = 0;
501 /* Acquire update access to the bus */
502 mutex_lock(&sn_hotplug_mutex);
504 /* is it okay to bring this slot down? */
505 rc = sn_slot_disable(bss_hotplug_slot, slot->device_num,
506 PCI_REQ_SLOT_ELIGIBLE);
510 /* free the ACPI resources for the slot */
511 if (SN_ACPI_BASE_SUPPORT() &&
512 PCI_CONTROLLER(slot->pci_bus)->acpi_handle) {
514 struct acpi_device *device;
516 acpi_handle chandle = NULL;
517 acpi_handle rethandle;
520 /* Get the rootbus node pointer */
521 phandle = PCI_CONTROLLER(slot->pci_bus)->acpi_handle;
524 * Walk the rootbus node's immediate children looking for
525 * the slot's device node(s). There can be more than
526 * one for multifunction devices.
530 ret = acpi_get_next_object(ACPI_TYPE_DEVICE,
534 if (ret == AE_NOT_FOUND || rethandle == NULL)
539 ret = acpi_evaluate_integer(chandle,
542 if (ACPI_SUCCESS(ret) &&
543 (adr>>16) == (slot->device_num + 1)) {
544 /* retain the owner id */
545 acpi_get_id(chandle, &ssdt_id);
547 ret = acpi_bus_get_device(chandle,
549 if (ACPI_SUCCESS(ret))
550 acpi_bus_trim(device, 1);
556 /* Free the SN resources assigned to the Linux device.*/
557 for (func = 0; func < 8; func++) {
558 dev = pci_get_slot(slot->pci_bus,
559 PCI_DEVFN(slot->device_num + 1,
562 sn_bus_free_data(dev);
563 pci_remove_bus_device(dev);
568 /* Remove the SSDT for the slot from the ACPI namespace */
569 if (SN_ACPI_BASE_SUPPORT() && ssdt_id) {
571 ret = acpi_unload_table_id(ssdt_id);
572 if (ACPI_FAILURE(ret)) {
573 printk(KERN_ERR "%s: acpi_unload_table_id "
574 "failed (0x%x) for id %d\n",
575 __func__, ret, ssdt_id);
576 /* try to continue on */
580 /* free the collected sysdata pointers */
581 sn_bus_free_sysdata();
583 /* Deactivate slot */
584 rc = sn_slot_disable(bss_hotplug_slot, slot->device_num,
585 PCI_REQ_SLOT_DISABLE);
587 /* Release the bus lock */
588 mutex_unlock(&sn_hotplug_mutex);
593 static inline int get_power_status(struct hotplug_slot *bss_hotplug_slot,
596 struct slot *slot = bss_hotplug_slot->private;
597 struct pcibus_info *pcibus_info;
600 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
601 mutex_lock(&sn_hotplug_mutex);
602 power = pcibus_info->pbi_enabled_devices & (1 << slot->device_num);
603 *value = power ? 1 : 0;
604 mutex_unlock(&sn_hotplug_mutex);
608 static void sn_release_slot(struct hotplug_slot *bss_hotplug_slot)
610 kfree(bss_hotplug_slot->info);
611 kfree(bss_hotplug_slot->name);
612 kfree(bss_hotplug_slot->private);
613 kfree(bss_hotplug_slot);
616 static int sn_hotplug_slot_register(struct pci_bus *pci_bus)
619 struct pci_slot *pci_slot;
620 struct hotplug_slot *bss_hotplug_slot;
624 * Currently only four devices are supported,
625 * in the future there maybe more -- up to 32.
628 for (device = 0; device < SN_MAX_HP_SLOTS ; device++) {
629 if (sn_pci_slot_valid(pci_bus, device) != 1)
632 bss_hotplug_slot = kzalloc(sizeof(*bss_hotplug_slot),
634 if (!bss_hotplug_slot) {
639 bss_hotplug_slot->info =
640 kzalloc(sizeof(struct hotplug_slot_info),
642 if (!bss_hotplug_slot->info) {
647 if (sn_hp_slot_private_alloc(bss_hotplug_slot,
653 bss_hotplug_slot->ops = &sn_hotplug_slot_ops;
654 bss_hotplug_slot->release = &sn_release_slot;
656 rc = pci_hp_register(bss_hotplug_slot, pci_bus, device);
660 pci_slot = bss_hotplug_slot->pci_slot;
661 rc = sysfs_create_file(&pci_slot->kobj,
662 &sn_slot_path_attr.attr);
666 dev_dbg(&pci_bus->self->dev, "Registered bus with hotplug\n");
670 dev_dbg(&pci_bus->self->dev, "bus failed to register with err = %d\n",
675 dev_dbg(&pci_bus->self->dev, "Memory allocation error\n");
677 /* destroy THIS element */
678 if (bss_hotplug_slot)
679 sn_release_slot(bss_hotplug_slot);
681 /* destroy anything else on the list */
682 while ((bss_hotplug_slot = sn_hp_destroy()))
683 pci_hp_deregister(bss_hotplug_slot);
688 static int sn_pci_hotplug_init(void)
690 struct pci_bus *pci_bus = NULL;
694 if (!sn_prom_feature_available(PRF_HOTPLUG_SUPPORT)) {
695 printk(KERN_ERR "%s: PROM version does not support hotplug.\n",
700 INIT_LIST_HEAD(&sn_hp_list);
702 while ((pci_bus = pci_find_next_bus(pci_bus))) {
703 if (!pci_bus->sysdata)
706 rc = sn_pci_bus_valid(pci_bus);
708 dev_dbg(&pci_bus->self->dev, "not a valid hotplug bus\n");
711 dev_dbg(&pci_bus->self->dev, "valid hotplug bus\n");
713 rc = sn_hotplug_slot_register(pci_bus);
722 return registered == 1 ? 0 : -ENODEV;
725 static void sn_pci_hotplug_exit(void)
727 struct hotplug_slot *bss_hotplug_slot;
729 while ((bss_hotplug_slot = sn_hp_destroy()))
730 pci_hp_deregister(bss_hotplug_slot);
732 if (!list_empty(&sn_hp_list))
733 printk(KERN_ERR "%s: internal list is not empty\n", __FILE__);
736 module_init(sn_pci_hotplug_init);
737 module_exit(sn_pci_hotplug_exit);