blob: 5282aa3e33c506aa7995cd36132545ea354a01d7 [file] [log] [blame]
Bjorn Helgaas736759e2018-01-26 14:22:04 -06001// SPDX-License-Identifier: GPL-2.0+
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Bjorn Helgaasf7625982013-11-14 11:28:18 -07003 * RPA Virtual I/O device functions
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Copyright (C) 2004 Linda Xie <lxie@us.ibm.com>
5 *
6 * All rights reserved.
7 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Send feedback to <lxie@us.ibm.com>
9 *
10 */
11#include <linux/kernel.h>
12#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/sysfs.h>
14#include <linux/pci.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080015#include <linux/string.h>
16#include <linux/slab.h>
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/rtas.h>
19#include "rpaphp.h"
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021/* free up the memory used by a slot */
Linus Torvalds1da177e2005-04-16 15:20:36 -070022void dealloc_slot_struct(struct slot *slot)
23{
Alex Chiangb2132fe2008-10-20 17:41:43 -060024 kfree(slot->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 kfree(slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -070026}
27
Linas Vepstas5fd39c32007-04-13 15:34:10 -070028struct slot *alloc_slot_struct(struct device_node *dn,
Bogicevic Sasaff3ce482015-12-27 13:21:11 -080029 int drc_index, char *drc_name, int power_domain)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
31 struct slot *slot;
Bjorn Helgaasf7625982013-11-14 11:28:18 -070032
Eric Sesterhennf5afe802006-02-28 15:34:49 +010033 slot = kzalloc(sizeof(struct slot), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 if (!slot)
35 goto error_nomem;
Alex Chiangb2132fe2008-10-20 17:41:43 -060036 slot->name = kstrdup(drc_name, GFP_KERNEL);
37 if (!slot->name)
Lukas Wunner125450f2018-09-08 09:59:01 +020038 goto error_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 slot->dn = dn;
40 slot->index = drc_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 slot->power_domain = power_domain;
Lukas Wunner125450f2018-09-08 09:59:01 +020042 slot->hotplug_slot.ops = &rpaphp_hotplug_slot_ops;
Bjorn Helgaasf7625982013-11-14 11:28:18 -070043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 return (slot);
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046error_slot:
47 kfree(slot);
48error_nomem:
49 return NULL;
50}
51
52static int is_registered(struct slot *slot)
53{
Linas Vepstas5fd39c32007-04-13 15:34:10 -070054 struct slot *tmp_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
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 Helgaasf7625982013-11-14 11:28:18 -070059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 return 0;
61}
62
linas@austin.ibm.comf6afbad2006-01-12 18:31:01 -060063int rpaphp_deregister_slot(struct slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
65 int retval = 0;
Lukas Wunner125450f2018-09-08 09:59:01 +020066 struct hotplug_slot *php_slot = &slot->hotplug_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68 dbg("%s - Entry: deregistering slot=%s\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -080069 __func__, slot->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71 list_del(&slot->rpaphp_slot_list);
Lukas Wunner51bbf9b2018-07-19 17:27:43 -050072 pci_hp_deregister(php_slot);
73 dealloc_slot_struct(slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Harvey Harrison66bef8c2008-03-03 19:09:46 -080075 dbg("%s - Exit: rc[%d]\n", __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 return retval;
77}
Linas Vepstas61ee9cd2006-02-01 18:21:09 -060078EXPORT_SYMBOL_GPL(rpaphp_deregister_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
linas@austin.ibm.comf6afbad2006-01-12 18:31:01 -060080int rpaphp_register_slot(struct slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Lukas Wunner125450f2018-09-08 09:59:01 +020082 struct hotplug_slot *php_slot = &slot->hotplug_slot;
Tyrel Datwylere2413a72016-07-11 17:16:27 -050083 struct device_node *child;
84 u32 my_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 int retval;
Tyrel Datwylere2413a72016-07-11 17:16:27 -050086 int slotno = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Rob Herringb63773a2017-07-18 16:43:21 -050088 dbg("%s registering slot:path[%pOF] index[%x], name[%s] pdomain[%x] type[%d]\n",
89 __func__, slot->dn, slot->index, slot->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 slot->power_domain, slot->type);
Linas Vepstasf1e79092006-12-01 16:31:27 -080091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 /* should not try to register the same slot twice */
Linas Vepstasf1e79092006-12-01 16:31:27 -080093 if (is_registered(slot)) {
linas@austin.ibm.comf6afbad2006-01-12 18:31:01 -060094 err("rpaphp_register_slot: slot[%s] is already registered\n", slot->name);
Linas Vepstas3499f0722007-04-13 15:34:11 -070095 return -EAGAIN;
Bjorn Helgaasf7625982013-11-14 11:28:18 -070096 }
Linas Vepstasf1e79092006-12-01 16:31:27 -080097
Tyrel Datwylere2413a72016-07-11 17:16:27 -050098 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 Chiang1359f272008-10-20 17:40:42 -0600107 retval = pci_hp_register(php_slot, slot->bus, slotno, slot->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 if (retval) {
109 err("pci_hp_register failed with error %d\n", retval);
Linas Vepstas3499f0722007-04-13 15:34:11 -0700110 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 }
Linas Vepstasf1e79092006-12-01 16:31:27 -0800112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 /* add slot to our internal list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 list_add(&slot->rpaphp_slot_list, &rpaphp_slot_head);
Linas Vepstas1b7c9fc2007-11-25 23:51:37 -0800115 info("Slot [%s] registered\n", slot->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 return 0;
117}