xref: /openbmc/linux/drivers/pci/hotplug/rpaphp_slot.c (revision 1359f2701b96abd9bb69c1273fb995a093b6409a)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * RPA Virtual I/O device functions
31da177e4SLinus Torvalds  * Copyright (C) 2004 Linda Xie <lxie@us.ibm.com>
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * All rights reserved.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify
81da177e4SLinus Torvalds  * it under the terms of the GNU General Public License as published by
91da177e4SLinus Torvalds  * the Free Software Foundation; either version 2 of the License, or (at
101da177e4SLinus Torvalds  * your option) any later version.
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  * This program is distributed in the hope that it will be useful, but
131da177e4SLinus Torvalds  * WITHOUT ANY WARRANTY; without even the implied warranty of
141da177e4SLinus Torvalds  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
151da177e4SLinus Torvalds  * NON INFRINGEMENT.  See the GNU General Public License for more
161da177e4SLinus Torvalds  * details.
171da177e4SLinus Torvalds  *
181da177e4SLinus Torvalds  * You should have received a copy of the GNU General Public License
191da177e4SLinus Torvalds  * along with this program; if not, write to the Free Software
201da177e4SLinus Torvalds  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
211da177e4SLinus Torvalds  *
221da177e4SLinus Torvalds  * Send feedback to <lxie@us.ibm.com>
231da177e4SLinus Torvalds  *
241da177e4SLinus Torvalds  */
251da177e4SLinus Torvalds #include <linux/kernel.h>
261da177e4SLinus Torvalds #include <linux/module.h>
271da177e4SLinus Torvalds #include <linux/kobject.h>
281da177e4SLinus Torvalds #include <linux/sysfs.h>
291da177e4SLinus Torvalds #include <linux/pci.h>
304e57b681STim Schmielau #include <linux/string.h>
314e57b681STim Schmielau #include <linux/slab.h>
324e57b681STim Schmielau 
331da177e4SLinus Torvalds #include <asm/rtas.h>
341da177e4SLinus Torvalds #include "rpaphp.h"
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds /* free up the memory used by a slot */
371da177e4SLinus Torvalds static void rpaphp_release_slot(struct hotplug_slot *hotplug_slot)
381da177e4SLinus Torvalds {
391da177e4SLinus Torvalds 	struct slot *slot = (struct slot *) hotplug_slot->private;
401da177e4SLinus Torvalds 	dealloc_slot_struct(slot);
411da177e4SLinus Torvalds }
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds void dealloc_slot_struct(struct slot *slot)
441da177e4SLinus Torvalds {
451da177e4SLinus Torvalds 	kfree(slot->hotplug_slot->info);
461da177e4SLinus Torvalds 	kfree(slot->hotplug_slot->name);
471da177e4SLinus Torvalds 	kfree(slot->hotplug_slot);
481da177e4SLinus Torvalds 	kfree(slot);
491da177e4SLinus Torvalds }
501da177e4SLinus Torvalds 
515fd39c35SLinas Vepstas struct slot *alloc_slot_struct(struct device_node *dn,
525fd39c35SLinas Vepstas                        int drc_index, char *drc_name, int power_domain)
531da177e4SLinus Torvalds {
541da177e4SLinus Torvalds 	struct slot *slot;
551da177e4SLinus Torvalds 
56f5afe806SEric Sesterhenn 	slot = kzalloc(sizeof(struct slot), GFP_KERNEL);
571da177e4SLinus Torvalds 	if (!slot)
581da177e4SLinus Torvalds 		goto error_nomem;
59f5afe806SEric Sesterhenn 	slot->hotplug_slot = kzalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
601da177e4SLinus Torvalds 	if (!slot->hotplug_slot)
611da177e4SLinus Torvalds 		goto error_slot;
62f5afe806SEric Sesterhenn 	slot->hotplug_slot->info = kzalloc(sizeof(struct hotplug_slot_info),
631da177e4SLinus Torvalds 					   GFP_KERNEL);
641da177e4SLinus Torvalds 	if (!slot->hotplug_slot->info)
651da177e4SLinus Torvalds 		goto error_hpslot;
661b7c9fcaSLinas Vepstas 	slot->hotplug_slot->name = kmalloc(strlen(drc_name) + 1, GFP_KERNEL);
671da177e4SLinus Torvalds 	if (!slot->hotplug_slot->name)
681da177e4SLinus Torvalds 		goto error_info;
691da177e4SLinus Torvalds 	slot->name = slot->hotplug_slot->name;
701b7c9fcaSLinas Vepstas 	strcpy(slot->name, drc_name);
711da177e4SLinus Torvalds 	slot->dn = dn;
721da177e4SLinus Torvalds 	slot->index = drc_index;
731da177e4SLinus Torvalds 	slot->power_domain = power_domain;
741da177e4SLinus Torvalds 	slot->hotplug_slot->private = slot;
751da177e4SLinus Torvalds 	slot->hotplug_slot->ops = &rpaphp_hotplug_slot_ops;
761da177e4SLinus Torvalds 	slot->hotplug_slot->release = &rpaphp_release_slot;
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds 	return (slot);
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds error_info:
811da177e4SLinus Torvalds 	kfree(slot->hotplug_slot->info);
821da177e4SLinus Torvalds error_hpslot:
831da177e4SLinus Torvalds 	kfree(slot->hotplug_slot);
841da177e4SLinus Torvalds error_slot:
851da177e4SLinus Torvalds 	kfree(slot);
861da177e4SLinus Torvalds error_nomem:
871da177e4SLinus Torvalds 	return NULL;
881da177e4SLinus Torvalds }
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds static int is_registered(struct slot *slot)
911da177e4SLinus Torvalds {
921da177e4SLinus Torvalds 	struct slot *tmp_slot;
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds 	list_for_each_entry(tmp_slot, &rpaphp_slot_head, rpaphp_slot_list) {
951da177e4SLinus Torvalds 		if (!strcmp(tmp_slot->name, slot->name))
961da177e4SLinus Torvalds 			return 1;
971da177e4SLinus Torvalds 	}
981da177e4SLinus Torvalds 	return 0;
991da177e4SLinus Torvalds }
1001da177e4SLinus Torvalds 
101f6afbad8Slinas@austin.ibm.com int rpaphp_deregister_slot(struct slot *slot)
1021da177e4SLinus Torvalds {
1031da177e4SLinus Torvalds 	int retval = 0;
1041da177e4SLinus Torvalds 	struct hotplug_slot *php_slot = slot->hotplug_slot;
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds 	 dbg("%s - Entry: deregistering slot=%s\n",
10766bef8c0SHarvey Harrison 		__func__, slot->name);
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds 	list_del(&slot->rpaphp_slot_list);
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds 	retval = pci_hp_deregister(php_slot);
1121da177e4SLinus Torvalds 	if (retval)
1131da177e4SLinus Torvalds 		err("Problem unregistering a slot %s\n", slot->name);
1141da177e4SLinus Torvalds 
11566bef8c0SHarvey Harrison 	dbg("%s - Exit: rc[%d]\n", __func__, retval);
1161da177e4SLinus Torvalds 	return retval;
1171da177e4SLinus Torvalds }
11861ee9cd5SLinas Vepstas EXPORT_SYMBOL_GPL(rpaphp_deregister_slot);
1191da177e4SLinus Torvalds 
120f6afbad8Slinas@austin.ibm.com int rpaphp_register_slot(struct slot *slot)
1211da177e4SLinus Torvalds {
122f1e79092SLinas Vepstas 	struct hotplug_slot *php_slot = slot->hotplug_slot;
1231da177e4SLinus Torvalds 	int retval;
124f46753c5SAlex Chiang 	int slotno;
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds 	dbg("%s registering slot:path[%s] index[%x], name[%s] pdomain[%x] type[%d]\n",
12766bef8c0SHarvey Harrison 		__func__, slot->dn->full_name, slot->index, slot->name,
1281da177e4SLinus Torvalds 		slot->power_domain, slot->type);
129f1e79092SLinas Vepstas 
1301da177e4SLinus Torvalds 	/* should not try to register the same slot twice */
131f1e79092SLinas Vepstas 	if (is_registered(slot)) {
132f6afbad8Slinas@austin.ibm.com 		err("rpaphp_register_slot: slot[%s] is already registered\n", slot->name);
1333499f072SLinas Vepstas 		return -EAGAIN;
1341da177e4SLinus Torvalds 	}
135f1e79092SLinas Vepstas 
136f46753c5SAlex Chiang 	if (slot->dn->child)
137f46753c5SAlex Chiang 		slotno = PCI_SLOT(PCI_DN(slot->dn->child)->devfn);
138f46753c5SAlex Chiang 	else
139f46753c5SAlex Chiang 		slotno = -1;
140*1359f270SAlex Chiang 	retval = pci_hp_register(php_slot, slot->bus, slotno, slot->name);
1411da177e4SLinus Torvalds 	if (retval) {
1421da177e4SLinus Torvalds 		err("pci_hp_register failed with error %d\n", retval);
1433499f072SLinas Vepstas 		return retval;
1441da177e4SLinus Torvalds 	}
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds 	/* add slot to our internal list */
1471da177e4SLinus Torvalds 	list_add(&slot->rpaphp_slot_list, &rpaphp_slot_head);
1481b7c9fcaSLinas Vepstas 	info("Slot [%s] registered\n", slot->name);
1491da177e4SLinus Torvalds 	return 0;
1501da177e4SLinus Torvalds }
1511da177e4SLinus Torvalds 
152