Bjorn Helgaas | 736759e | 2018-01-26 14:22:04 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Bjorn Helgaas | f762598 | 2013-11-14 11:28:18 -0700 | [diff] [blame] | 3 | * RPA Virtual I/O device functions |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * Copyright (C) 2004 Linda Xie <lxie@us.ibm.com> |
| 5 | * |
| 6 | * All rights reserved. |
| 7 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * Send feedback to <lxie@us.ibm.com> |
| 9 | * |
| 10 | */ |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/sysfs.h> |
| 14 | #include <linux/pci.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 15 | #include <linux/string.h> |
| 16 | #include <linux/slab.h> |
| 17 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <asm/rtas.h> |
| 19 | #include "rpaphp.h" |
| 20 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | /* free up the memory used by a slot */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | void dealloc_slot_struct(struct slot *slot) |
| 23 | { |
Alex Chiang | b2132fe | 2008-10-20 17:41:43 -0600 | [diff] [blame] | 24 | kfree(slot->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | kfree(slot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | } |
| 27 | |
Linas Vepstas | 5fd39c3 | 2007-04-13 15:34:10 -0700 | [diff] [blame] | 28 | struct slot *alloc_slot_struct(struct device_node *dn, |
Bogicevic Sasa | ff3ce48 | 2015-12-27 13:21:11 -0800 | [diff] [blame] | 29 | int drc_index, char *drc_name, int power_domain) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | { |
| 31 | struct slot *slot; |
Bjorn Helgaas | f762598 | 2013-11-14 11:28:18 -0700 | [diff] [blame] | 32 | |
Eric Sesterhenn | f5afe80 | 2006-02-28 15:34:49 +0100 | [diff] [blame] | 33 | slot = kzalloc(sizeof(struct slot), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | if (!slot) |
| 35 | goto error_nomem; |
Alex Chiang | b2132fe | 2008-10-20 17:41:43 -0600 | [diff] [blame] | 36 | slot->name = kstrdup(drc_name, GFP_KERNEL); |
| 37 | if (!slot->name) |
Lukas Wunner | 125450f | 2018-09-08 09:59:01 +0200 | [diff] [blame] | 38 | goto error_slot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | slot->dn = dn; |
| 40 | slot->index = drc_index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | slot->power_domain = power_domain; |
Lukas Wunner | 125450f | 2018-09-08 09:59:01 +0200 | [diff] [blame] | 42 | slot->hotplug_slot.ops = &rpaphp_hotplug_slot_ops; |
Bjorn Helgaas | f762598 | 2013-11-14 11:28:18 -0700 | [diff] [blame] | 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | return (slot); |
| 45 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | error_slot: |
| 47 | kfree(slot); |
| 48 | error_nomem: |
| 49 | return NULL; |
| 50 | } |
| 51 | |
| 52 | static int is_registered(struct slot *slot) |
| 53 | { |
Linas Vepstas | 5fd39c3 | 2007-04-13 15:34:10 -0700 | [diff] [blame] | 54 | struct slot *tmp_slot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
| 56 | list_for_each_entry(tmp_slot, &rpaphp_slot_head, rpaphp_slot_list) { |
| 57 | if (!strcmp(tmp_slot->name, slot->name)) |
| 58 | return 1; |
Bjorn Helgaas | f762598 | 2013-11-14 11:28:18 -0700 | [diff] [blame] | 59 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | return 0; |
| 61 | } |
| 62 | |
linas@austin.ibm.com | f6afbad | 2006-01-12 18:31:01 -0600 | [diff] [blame] | 63 | int rpaphp_deregister_slot(struct slot *slot) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | { |
| 65 | int retval = 0; |
Lukas Wunner | 125450f | 2018-09-08 09:59:01 +0200 | [diff] [blame] | 66 | struct hotplug_slot *php_slot = &slot->hotplug_slot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
| 68 | dbg("%s - Entry: deregistering slot=%s\n", |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 69 | __func__, slot->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
| 71 | list_del(&slot->rpaphp_slot_list); |
Lukas Wunner | 51bbf9b | 2018-07-19 17:27:43 -0500 | [diff] [blame] | 72 | pci_hp_deregister(php_slot); |
| 73 | dealloc_slot_struct(slot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 75 | dbg("%s - Exit: rc[%d]\n", __func__, retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | return retval; |
| 77 | } |
Linas Vepstas | 61ee9cd | 2006-02-01 18:21:09 -0600 | [diff] [blame] | 78 | EXPORT_SYMBOL_GPL(rpaphp_deregister_slot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
linas@austin.ibm.com | f6afbad | 2006-01-12 18:31:01 -0600 | [diff] [blame] | 80 | int rpaphp_register_slot(struct slot *slot) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | { |
Lukas Wunner | 125450f | 2018-09-08 09:59:01 +0200 | [diff] [blame] | 82 | struct hotplug_slot *php_slot = &slot->hotplug_slot; |
Tyrel Datwyler | e2413a7 | 2016-07-11 17:16:27 -0500 | [diff] [blame] | 83 | struct device_node *child; |
| 84 | u32 my_index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | int retval; |
Tyrel Datwyler | e2413a7 | 2016-07-11 17:16:27 -0500 | [diff] [blame] | 86 | int slotno = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
Rob Herring | b63773a | 2017-07-18 16:43:21 -0500 | [diff] [blame] | 88 | dbg("%s registering slot:path[%pOF] index[%x], name[%s] pdomain[%x] type[%d]\n", |
| 89 | __func__, slot->dn, slot->index, slot->name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | slot->power_domain, slot->type); |
Linas Vepstas | f1e7909 | 2006-12-01 16:31:27 -0800 | [diff] [blame] | 91 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | /* should not try to register the same slot twice */ |
Linas Vepstas | f1e7909 | 2006-12-01 16:31:27 -0800 | [diff] [blame] | 93 | if (is_registered(slot)) { |
linas@austin.ibm.com | f6afbad | 2006-01-12 18:31:01 -0600 | [diff] [blame] | 94 | err("rpaphp_register_slot: slot[%s] is already registered\n", slot->name); |
Linas Vepstas | 3499f072 | 2007-04-13 15:34:11 -0700 | [diff] [blame] | 95 | return -EAGAIN; |
Bjorn Helgaas | f762598 | 2013-11-14 11:28:18 -0700 | [diff] [blame] | 96 | } |
Linas Vepstas | f1e7909 | 2006-12-01 16:31:27 -0800 | [diff] [blame] | 97 | |
Tyrel Datwyler | e2413a7 | 2016-07-11 17:16:27 -0500 | [diff] [blame] | 98 | for_each_child_of_node(slot->dn, child) { |
| 99 | retval = of_property_read_u32(child, "ibm,my-drc-index", &my_index); |
| 100 | if (my_index == slot->index) { |
| 101 | slotno = PCI_SLOT(PCI_DN(child)->devfn); |
| 102 | of_node_put(child); |
| 103 | break; |
| 104 | } |
| 105 | } |
| 106 | |
Alex Chiang | 1359f27 | 2008-10-20 17:40:42 -0600 | [diff] [blame] | 107 | retval = pci_hp_register(php_slot, slot->bus, slotno, slot->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | if (retval) { |
| 109 | err("pci_hp_register failed with error %d\n", retval); |
Linas Vepstas | 3499f072 | 2007-04-13 15:34:11 -0700 | [diff] [blame] | 110 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | } |
Linas Vepstas | f1e7909 | 2006-12-01 16:31:27 -0800 | [diff] [blame] | 112 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | /* add slot to our internal list */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | list_add(&slot->rpaphp_slot_list, &rpaphp_slot_head); |
Linas Vepstas | 1b7c9fc | 2007-11-25 23:51:37 -0800 | [diff] [blame] | 115 | info("Slot [%s] registered\n", slot->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | return 0; |
| 117 | } |