^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * RPA Virtual I/O device functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2004 Linda Xie <lxie@us.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Send feedback to <lxie@us.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/pci.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <asm/rtas.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include "rpaphp.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) /* free up the memory used by a slot */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) void dealloc_slot_struct(struct slot *slot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) of_node_put(slot->dn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) kfree(slot->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) kfree(slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct slot *alloc_slot_struct(struct device_node *dn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) int drc_index, char *drc_name, int power_domain)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct slot *slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) slot = kzalloc(sizeof(struct slot), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) if (!slot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) goto error_nomem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) slot->name = kstrdup(drc_name, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (!slot->name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) goto error_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) slot->dn = of_node_get(dn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) slot->index = drc_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) slot->power_domain = power_domain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) slot->hotplug_slot.ops = &rpaphp_hotplug_slot_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return (slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) error_slot:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) kfree(slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) error_nomem:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) static int is_registered(struct slot *slot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) struct slot *tmp_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) list_for_each_entry(tmp_slot, &rpaphp_slot_head, rpaphp_slot_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (!strcmp(tmp_slot->name, slot->name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) int rpaphp_deregister_slot(struct slot *slot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) int retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct hotplug_slot *php_slot = &slot->hotplug_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) dbg("%s - Entry: deregistering slot=%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) __func__, slot->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) list_del(&slot->rpaphp_slot_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) pci_hp_deregister(php_slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) dealloc_slot_struct(slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) dbg("%s - Exit: rc[%d]\n", __func__, retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) EXPORT_SYMBOL_GPL(rpaphp_deregister_slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) int rpaphp_register_slot(struct slot *slot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) struct hotplug_slot *php_slot = &slot->hotplug_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) struct device_node *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) u32 my_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) int slotno = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) dbg("%s registering slot:path[%pOF] index[%x], name[%s] pdomain[%x] type[%d]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) __func__, slot->dn, slot->index, slot->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) slot->power_domain, slot->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) /* should not try to register the same slot twice */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (is_registered(slot)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) err("rpaphp_register_slot: slot[%s] is already registered\n", slot->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) for_each_child_of_node(slot->dn, child) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) retval = of_property_read_u32(child, "ibm,my-drc-index", &my_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (my_index == slot->index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) slotno = PCI_SLOT(PCI_DN(child)->devfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) of_node_put(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) retval = pci_hp_register(php_slot, slot->bus, slotno, slot->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) err("pci_hp_register failed with error %d\n", retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) /* add slot to our internal list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) list_add(&slot->rpaphp_slot_list, &rpaphp_slot_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) info("Slot [%s] registered\n", slot->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }