1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2005-2006 Silicon Graphics, Inc. All rights reserved.
5 * This work was based on the 2.4/2.6 kernel development by Dick Reigner.
6 * Work to add BIOS PROM support was completed by Mike Habeck.
9 #include <linux/acpi.h>
10 #include <linux/init.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/pci.h>
14 #include <linux/pci_hotplug.h>
15 #include <linux/proc_fs.h>
16 #include <linux/slab.h>
17 #include <linux/types.h>
18 #include <linux/mutex.h>
20 #include <asm/sn/addrs.h>
21 #include <asm/sn/geo.h>
22 #include <asm/sn/l1.h>
23 #include <asm/sn/module.h>
24 #include <asm/sn/pcibr_provider.h>
25 #include <asm/sn/pcibus_provider_defs.h>
26 #include <asm/sn/pcidev.h>
27 #include <asm/sn/sn_feature_sets.h>
28 #include <asm/sn/sn_sal.h>
29 #include <asm/sn/types.h>
30 #include <asm/sn/acpi.h>
34 MODULE_LICENSE("GPL");
35 MODULE_AUTHOR("SGI (prarit@sgi.com, dickie@sgi.com, habeck@sgi.com)");
36 MODULE_DESCRIPTION("SGI Altix Hot Plug PCI Controller Driver");
39 /* SAL call error codes. Keep in sync with prom header io/include/pcibr.h */
40 #define PCI_SLOT_ALREADY_UP 2 /* slot already up */
41 #define PCI_SLOT_ALREADY_DOWN 3 /* slot already down */
42 #define PCI_L1_ERR 7 /* L1 console command error */
43 #define PCI_EMPTY_33MHZ 15 /* empty 33 MHz bus */
46 #define PCIIO_ASIC_TYPE_TIOCA 4
47 #define PCI_L1_QSIZE 128 /* our L1 message buffer size */
48 #define SN_MAX_HP_SLOTS 32 /* max hotplug slots */
49 #define SN_SLOT_NAME_SIZE 33 /* size of name string */
51 /* internal list head */
52 static struct list_head sn_hp_list;
54 /* hotplug_slot struct's private pointer */
57 struct pci_bus *pci_bus;
58 /* this struct for glue internal only */
59 struct hotplug_slot hotplug_slot;
60 struct list_head hp_list;
61 char physical_path[SN_SLOT_NAME_SIZE];
64 struct pcibr_slot_enable_resp {
66 char resp_l1_msg[PCI_L1_QSIZE + 1];
69 struct pcibr_slot_disable_resp {
71 char resp_l1_msg[PCI_L1_QSIZE + 1];
75 PCI_REQ_SLOT_ELIGIBLE,
79 static int enable_slot(struct hotplug_slot *slot);
80 static int disable_slot(struct hotplug_slot *slot);
81 static inline int get_power_status(struct hotplug_slot *slot, u8 *value);
83 static const struct hotplug_slot_ops sn_hotplug_slot_ops = {
84 .enable_slot = enable_slot,
85 .disable_slot = disable_slot,
86 .get_power_status = get_power_status,
89 static DEFINE_MUTEX(sn_hotplug_mutex);
91 static struct slot *to_slot(struct hotplug_slot *bss_hotplug_slot)
93 return container_of(bss_hotplug_slot, struct slot, hotplug_slot);
96 static ssize_t path_show(struct pci_slot *pci_slot, char *buf)
99 struct slot *slot = to_slot(pci_slot->hotplug);
104 retval = sprintf(buf, "%s\n", slot->physical_path);
108 static struct pci_slot_attribute sn_slot_path_attr = __ATTR_RO(path);
110 static int sn_pci_slot_valid(struct pci_bus *pci_bus, int device)
112 struct pcibus_info *pcibus_info;
113 u16 busnum, segment, ioboard_type;
115 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(pci_bus);
117 /* Check to see if this is a valid slot on 'pci_bus' */
118 if (!(pcibus_info->pbi_valid_devices & (1 << device)))
121 ioboard_type = sn_ioboard_to_pci_bus(pci_bus);
122 busnum = pcibus_info->pbi_buscommon.bs_persist_busnum;
123 segment = pci_domain_nr(pci_bus) & 0xf;
125 /* Do not allow hotplug operations on base I/O cards */
126 if ((ioboard_type == L1_BRICKTYPE_IX ||
127 ioboard_type == L1_BRICKTYPE_IA) &&
128 (segment == 1 && busnum == 0 && device != 1))
134 static int sn_pci_bus_valid(struct pci_bus *pci_bus)
136 struct pcibus_info *pcibus_info;
140 /* Don't register slots hanging off the TIOCA bus */
141 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(pci_bus);
142 asic_type = pcibus_info->pbi_buscommon.bs_asic_type;
143 if (asic_type == PCIIO_ASIC_TYPE_TIOCA)
146 /* Only register slots in I/O Bricks that support hotplug */
147 ioboard_type = sn_ioboard_to_pci_bus(pci_bus);
148 switch (ioboard_type) {
149 case L1_BRICKTYPE_IX:
150 case L1_BRICKTYPE_PX:
151 case L1_BRICKTYPE_IA:
152 case L1_BRICKTYPE_PA:
153 case L1_BOARDTYPE_PCIX3SLOT:
164 static int sn_hp_slot_private_alloc(struct hotplug_slot **bss_hotplug_slot,
165 struct pci_bus *pci_bus, int device,
168 struct pcibus_info *pcibus_info;
171 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(pci_bus);
173 slot = kzalloc(sizeof(*slot), GFP_KERNEL);
177 slot->device_num = device;
178 slot->pci_bus = pci_bus;
179 sprintf(name, "%04x:%02x:%02x",
180 pci_domain_nr(pci_bus),
181 ((u16)pcibus_info->pbi_buscommon.bs_persist_busnum),
184 sn_generate_path(pci_bus, slot->physical_path);
186 list_add(&slot->hp_list, &sn_hp_list);
187 *bss_hotplug_slot = &slot->hotplug_slot;
192 static struct hotplug_slot *sn_hp_destroy(void)
195 struct pci_slot *pci_slot;
196 struct hotplug_slot *bss_hotplug_slot = NULL;
198 list_for_each_entry(slot, &sn_hp_list, hp_list) {
199 bss_hotplug_slot = &slot->hotplug_slot;
200 pci_slot = bss_hotplug_slot->pci_slot;
201 list_del(&slot->hp_list);
202 sysfs_remove_file(&pci_slot->kobj,
203 &sn_slot_path_attr.attr);
206 return bss_hotplug_slot;
209 static void sn_bus_free_data(struct pci_dev *dev)
211 struct pci_bus *subordinate_bus;
212 struct pci_dev *child;
214 /* Recursively clean up sn_irq_info structs */
215 if (dev->subordinate) {
216 subordinate_bus = dev->subordinate;
217 list_for_each_entry(child, &subordinate_bus->devices, bus_list)
218 sn_bus_free_data(child);
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
226 sn_bus_store_sysdata(dev);
227 sn_pci_unfixup_slot(dev);
230 static int sn_slot_enable(struct hotplug_slot *bss_hotplug_slot,
231 int device_num, char **ssdt)
233 struct slot *slot = to_slot(bss_hotplug_slot);
234 struct pcibus_info *pcibus_info;
235 struct pcibr_slot_enable_resp resp;
238 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
241 * Power-on and initialize the slot in the SN
242 * PCI infrastructure.
244 rc = sal_pcibr_slot_enable(pcibus_info, device_num, &resp, ssdt);
247 if (rc == PCI_SLOT_ALREADY_UP) {
248 pci_dbg(slot->pci_bus->self, "is already active\n");
249 return 1; /* return 1 to user */
252 if (rc == PCI_L1_ERR) {
253 pci_dbg(slot->pci_bus->self, "L1 failure %d with message: %s",
254 resp.resp_sub_errno, resp.resp_l1_msg);
259 pci_dbg(slot->pci_bus->self, "insert failed with error %d sub-error %d\n",
260 rc, resp.resp_sub_errno);
264 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
265 pcibus_info->pbi_enabled_devices |= (1 << device_num);
270 static int sn_slot_disable(struct hotplug_slot *bss_hotplug_slot,
271 int device_num, int action)
273 struct slot *slot = to_slot(bss_hotplug_slot);
274 struct pcibus_info *pcibus_info;
275 struct pcibr_slot_disable_resp resp;
278 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
280 rc = sal_pcibr_slot_disable(pcibus_info, device_num, action, &resp);
282 if ((action == PCI_REQ_SLOT_ELIGIBLE) &&
283 (rc == PCI_SLOT_ALREADY_DOWN)) {
284 pci_dbg(slot->pci_bus->self, "Slot %s already inactive\n", slot->physical_path);
285 return 1; /* return 1 to user */
288 if ((action == PCI_REQ_SLOT_ELIGIBLE) && (rc == PCI_EMPTY_33MHZ)) {
289 pci_dbg(slot->pci_bus->self, "Cannot remove last 33MHz card\n");
293 if ((action == PCI_REQ_SLOT_ELIGIBLE) && (rc == PCI_L1_ERR)) {
294 pci_dbg(slot->pci_bus->self, "L1 failure %d with message \n%s\n",
295 resp.resp_sub_errno, resp.resp_l1_msg);
299 if ((action == PCI_REQ_SLOT_ELIGIBLE) && rc) {
300 pci_dbg(slot->pci_bus->self, "remove failed with error %d sub-error %d\n",
301 rc, resp.resp_sub_errno);
305 if ((action == PCI_REQ_SLOT_ELIGIBLE) && !rc)
308 if ((action == PCI_REQ_SLOT_DISABLE) && !rc) {
309 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
310 pcibus_info->pbi_enabled_devices &= ~(1 << device_num);
311 pci_dbg(slot->pci_bus->self, "remove successful\n");
315 if ((action == PCI_REQ_SLOT_DISABLE) && rc) {
316 pci_dbg(slot->pci_bus->self, "remove failed rc = %d\n", rc);
323 * Power up and configure the slot via a SAL call to PROM.
324 * Scan slot (and any children), do any platform specific fixup,
325 * and find device driver.
327 static int enable_slot(struct hotplug_slot *bss_hotplug_slot)
329 struct slot *slot = to_slot(bss_hotplug_slot);
330 struct pci_bus *new_bus = NULL;
336 void pcibios_fixup_device_resources(struct pci_dev *);
338 /* Serialize the Linux PCI infrastructure */
339 mutex_lock(&sn_hotplug_mutex);
342 * Power-on and initialize the slot in the SN
343 * PCI infrastructure. Also, retrieve the ACPI SSDT
344 * table for the slot (if ACPI capable PROM).
346 rc = sn_slot_enable(bss_hotplug_slot, slot->device_num, &ssdt);
348 mutex_unlock(&sn_hotplug_mutex);
354 /* Add the new SSDT for the slot to the ACPI namespace */
355 if (SN_ACPI_BASE_SUPPORT() && ssdt) {
358 ret = acpi_load_table((struct acpi_table_header *)ssdt);
359 if (ACPI_FAILURE(ret)) {
360 printk(KERN_ERR "%s: acpi_load_table failed (0x%x)\n",
362 /* try to continue on */
366 num_funcs = pci_scan_slot(slot->pci_bus,
367 PCI_DEVFN(slot->device_num + 1, 0));
369 pci_dbg(slot->pci_bus->self, "no device in slot\n");
370 mutex_unlock(&sn_hotplug_mutex);
375 * Map SN resources for all functions on the card
376 * to the Linux PCI interface and tell the drivers
379 list_for_each_entry(dev, &slot->pci_bus->devices, bus_list) {
380 if (PCI_SLOT(dev->devfn) != slot->device_num + 1)
383 /* Need to do slot fixup on PPB before fixup of children
384 * (PPB's pcidev_info needs to be in pcidev_info list
385 * before child's SN_PCIDEV_INFO() call to setup
386 * pdi_host_pcidev_info).
388 pcibios_fixup_device_resources(dev);
389 if (SN_ACPI_BASE_SUPPORT())
390 sn_acpi_slot_fixup(dev);
392 sn_io_slot_fixup(dev);
393 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
394 pci_hp_add_bridge(dev);
395 if (dev->subordinate) {
396 new_bus = dev->subordinate;
403 * Add the slot's devices to the ACPI infrastructure */
404 if (SN_ACPI_BASE_SUPPORT() && ssdt) {
405 unsigned long long adr;
406 struct acpi_device *pdevice;
408 acpi_handle chandle = NULL;
409 acpi_handle rethandle;
412 phandle = acpi_device_handle(PCI_CONTROLLER(slot->pci_bus)->companion);
414 if (acpi_bus_get_device(phandle, &pdevice)) {
415 pci_dbg(slot->pci_bus->self, "no parent device, assuming NULL\n");
419 acpi_scan_lock_acquire();
421 * Walk the rootbus node's immediate children looking for
422 * the slot's device node(s). There can be more than
423 * one for multifunction devices.
427 ret = acpi_get_next_object(ACPI_TYPE_DEVICE,
431 if (ret == AE_NOT_FOUND || rethandle == NULL)
436 ret = acpi_evaluate_integer(chandle, METHOD_NAME__ADR,
439 if (ACPI_SUCCESS(ret) &&
440 (adr>>16) == (slot->device_num + 1)) {
442 ret = acpi_bus_scan(chandle);
443 if (ACPI_FAILURE(ret)) {
444 printk(KERN_ERR "%s: acpi_bus_scan failed (0x%x) for slot %d func %d\n",
445 __func__, ret, (int)(adr>>16),
447 /* try to continue on */
451 acpi_scan_lock_release();
454 pci_lock_rescan_remove();
456 /* Call the driver for the new device */
457 pci_bus_add_devices(slot->pci_bus);
458 /* Call the drivers for the new devices subordinate to PPB */
460 pci_bus_add_devices(new_bus);
462 pci_unlock_rescan_remove();
463 mutex_unlock(&sn_hotplug_mutex);
466 pci_dbg(slot->pci_bus->self, "insert operation successful\n");
468 pci_dbg(slot->pci_bus->self, "insert operation failed rc = %d\n", rc);
473 static int disable_slot(struct hotplug_slot *bss_hotplug_slot)
475 struct slot *slot = to_slot(bss_hotplug_slot);
476 struct pci_dev *dev, *temp;
478 acpi_handle ssdt_hdl = NULL;
480 /* Acquire update access to the bus */
481 mutex_lock(&sn_hotplug_mutex);
483 /* is it okay to bring this slot down? */
484 rc = sn_slot_disable(bss_hotplug_slot, slot->device_num,
485 PCI_REQ_SLOT_ELIGIBLE);
489 /* free the ACPI resources for the slot */
490 if (SN_ACPI_BASE_SUPPORT() &&
491 PCI_CONTROLLER(slot->pci_bus)->companion) {
492 unsigned long long adr;
493 struct acpi_device *device;
495 acpi_handle chandle = NULL;
496 acpi_handle rethandle;
499 /* Get the rootbus node pointer */
500 phandle = acpi_device_handle(PCI_CONTROLLER(slot->pci_bus)->companion);
502 acpi_scan_lock_acquire();
504 * Walk the rootbus node's immediate children looking for
505 * the slot's device node(s). There can be more than
506 * one for multifunction devices.
510 ret = acpi_get_next_object(ACPI_TYPE_DEVICE,
514 if (ret == AE_NOT_FOUND || rethandle == NULL)
519 ret = acpi_evaluate_integer(chandle,
522 if (ACPI_SUCCESS(ret) &&
523 (adr>>16) == (slot->device_num + 1)) {
524 /* retain the owner id */
527 ret = acpi_bus_get_device(chandle,
529 if (ACPI_SUCCESS(ret))
530 acpi_bus_trim(device);
533 acpi_scan_lock_release();
536 pci_lock_rescan_remove();
537 /* Free the SN resources assigned to the Linux device.*/
538 list_for_each_entry_safe(dev, temp, &slot->pci_bus->devices, bus_list) {
539 if (PCI_SLOT(dev->devfn) != slot->device_num + 1)
543 sn_bus_free_data(dev);
544 pci_stop_and_remove_bus_device(dev);
547 pci_unlock_rescan_remove();
549 /* Remove the SSDT for the slot from the ACPI namespace */
550 if (SN_ACPI_BASE_SUPPORT() && ssdt_hdl) {
552 ret = acpi_unload_parent_table(ssdt_hdl);
553 if (ACPI_FAILURE(ret)) {
554 acpi_handle_err(ssdt_hdl,
555 "%s: acpi_unload_parent_table failed (0x%x)\n",
557 /* try to continue on */
561 /* free the collected sysdata pointers */
562 sn_bus_free_sysdata();
564 /* Deactivate slot */
565 rc = sn_slot_disable(bss_hotplug_slot, slot->device_num,
566 PCI_REQ_SLOT_DISABLE);
568 /* Release the bus lock */
569 mutex_unlock(&sn_hotplug_mutex);
574 static inline int get_power_status(struct hotplug_slot *bss_hotplug_slot,
577 struct slot *slot = to_slot(bss_hotplug_slot);
578 struct pcibus_info *pcibus_info;
581 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
582 mutex_lock(&sn_hotplug_mutex);
583 power = pcibus_info->pbi_enabled_devices & (1 << slot->device_num);
584 *value = power ? 1 : 0;
585 mutex_unlock(&sn_hotplug_mutex);
589 static void sn_release_slot(struct hotplug_slot *bss_hotplug_slot)
591 kfree(to_slot(bss_hotplug_slot));
594 static int sn_hotplug_slot_register(struct pci_bus *pci_bus)
597 struct pci_slot *pci_slot;
598 struct hotplug_slot *bss_hotplug_slot;
599 char name[SN_SLOT_NAME_SIZE];
603 * Currently only four devices are supported,
604 * in the future there maybe more -- up to 32.
607 for (device = 0; device < SN_MAX_HP_SLOTS ; device++) {
608 if (sn_pci_slot_valid(pci_bus, device) != 1)
611 if (sn_hp_slot_private_alloc(&bss_hotplug_slot,
612 pci_bus, device, name)) {
616 bss_hotplug_slot->ops = &sn_hotplug_slot_ops;
618 rc = pci_hp_register(bss_hotplug_slot, pci_bus, device, name);
622 pci_slot = bss_hotplug_slot->pci_slot;
623 rc = sysfs_create_file(&pci_slot->kobj,
624 &sn_slot_path_attr.attr);
628 pci_dbg(pci_bus->self, "Registered bus with hotplug\n");
632 pci_dbg(pci_bus->self, "bus failed to register with err = %d\n",
635 /* destroy THIS element */
637 sn_release_slot(bss_hotplug_slot);
640 /* destroy anything else on the list */
641 while ((bss_hotplug_slot = sn_hp_destroy())) {
642 pci_hp_deregister(bss_hotplug_slot);
643 sn_release_slot(bss_hotplug_slot);
649 static int __init sn_pci_hotplug_init(void)
651 struct pci_bus *pci_bus = NULL;
655 if (!sn_prom_feature_available(PRF_HOTPLUG_SUPPORT)) {
656 printk(KERN_ERR "%s: PROM version does not support hotplug.\n",
661 INIT_LIST_HEAD(&sn_hp_list);
663 while ((pci_bus = pci_find_next_bus(pci_bus))) {
664 if (!pci_bus->sysdata)
667 rc = sn_pci_bus_valid(pci_bus);
669 pci_dbg(pci_bus->self, "not a valid hotplug bus\n");
672 pci_dbg(pci_bus->self, "valid hotplug bus\n");
674 rc = sn_hotplug_slot_register(pci_bus);
683 return registered == 1 ? 0 : -ENODEV;
686 static void __exit sn_pci_hotplug_exit(void)
688 struct hotplug_slot *bss_hotplug_slot;
690 while ((bss_hotplug_slot = sn_hp_destroy())) {
691 pci_hp_deregister(bss_hotplug_slot);
692 sn_release_slot(bss_hotplug_slot);
695 if (!list_empty(&sn_hp_list))
696 printk(KERN_ERR "%s: internal list is not empty\n", __FILE__);
699 module_init(sn_pci_hotplug_init);
700 module_exit(sn_pci_hotplug_exit);