1 /*
2  * Interface for Dynamic Logical Partitioning of I/O Slots on
3  * RPA-compliant PPC64 platform.
4  *
5  * John Rose <johnrose@austin.ibm.com>
6  * October 2003
7  *
8  * Copyright (C) 2003 IBM.
9  *
10  *      This program is free software; you can redistribute it and/or
11  *      modify it under the terms of the GNU General Public License
12  *      as published by the Free Software Foundation; either version
13  *      2 of the License, or (at your option) any later version.
14  */
15 #include <linux/kobject.h>
16 #include <linux/string.h>
17 #include "pci_hotplug.h"
18 #include "rpadlpar.h"
19 
20 #define DLPAR_KOBJ_NAME       "control"
21 #define ADD_SLOT_ATTR_NAME    "add_slot"
22 #define REMOVE_SLOT_ATTR_NAME "remove_slot"
23 
24 #define MAX_DRC_NAME_LEN 64
25 
26 /* Store return code of dlpar operation in attribute struct */
27 struct dlpar_io_attr {
28 	int rc;
29 	struct attribute attr;
30 	ssize_t (*store)(struct dlpar_io_attr *dlpar_attr, const char *buf,
31 		size_t nbytes);
32 };
33 
34 /* Common show callback for all attrs, display the return code
35  * of the dlpar op */
36 static ssize_t
37 dlpar_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
38 {
39 	struct dlpar_io_attr *dlpar_attr = container_of(attr,
40 						struct dlpar_io_attr, attr);
41 	return sprintf(buf, "%d\n", dlpar_attr->rc);
42 }
43 
44 static ssize_t
45 dlpar_attr_store(struct kobject * kobj, struct attribute * attr,
46 		 const char *buf, size_t nbytes)
47 {
48 	struct dlpar_io_attr *dlpar_attr = container_of(attr,
49 						struct dlpar_io_attr, attr);
50 	return dlpar_attr->store ?
51 		dlpar_attr->store(dlpar_attr, buf, nbytes) : -EIO;
52 }
53 
54 static struct sysfs_ops dlpar_attr_sysfs_ops = {
55 	.show = dlpar_attr_show,
56 	.store = dlpar_attr_store,
57 };
58 
59 static ssize_t add_slot_store(struct dlpar_io_attr *dlpar_attr,
60 				const char *buf, size_t nbytes)
61 {
62 	char drc_name[MAX_DRC_NAME_LEN];
63 	char *end;
64 
65 	if (nbytes >= MAX_DRC_NAME_LEN)
66 		return 0;
67 
68 	memcpy(drc_name, buf, nbytes);
69 
70 	end = strchr(drc_name, '\n');
71 	if (!end)
72 		end = &drc_name[nbytes];
73 	*end = '\0';
74 
75 	dlpar_attr->rc = dlpar_add_slot(drc_name);
76 
77 	return nbytes;
78 }
79 
80 static ssize_t remove_slot_store(struct dlpar_io_attr *dlpar_attr,
81 		 		const char *buf, size_t nbytes)
82 {
83 	char drc_name[MAX_DRC_NAME_LEN];
84 	char *end;
85 
86 	if (nbytes >= MAX_DRC_NAME_LEN)
87 		return 0;
88 
89 	memcpy(drc_name, buf, nbytes);
90 
91 	end = strchr(drc_name, '\n');
92 	if (!end)
93 		end = &drc_name[nbytes];
94 	*end = '\0';
95 
96 	dlpar_attr->rc = dlpar_remove_slot(drc_name);
97 
98 	return nbytes;
99 }
100 
101 static struct dlpar_io_attr add_slot_attr = {
102 	.rc = 0,
103 	.attr = { .name = ADD_SLOT_ATTR_NAME, .mode = 0644, },
104 	.store = add_slot_store,
105 };
106 
107 static struct dlpar_io_attr remove_slot_attr = {
108 	.rc = 0,
109 	.attr = { .name = REMOVE_SLOT_ATTR_NAME, .mode = 0644},
110 	.store = remove_slot_store,
111 };
112 
113 static struct attribute *default_attrs[] = {
114 	&add_slot_attr.attr,
115 	&remove_slot_attr.attr,
116 	NULL,
117 };
118 
119 static void dlpar_io_release(struct kobject *kobj)
120 {
121 	/* noop */
122 	return;
123 }
124 
125 struct kobj_type ktype_dlpar_io = {
126 	.release = dlpar_io_release,
127 	.sysfs_ops = &dlpar_attr_sysfs_ops,
128 	.default_attrs = default_attrs,
129 };
130 
131 struct kset dlpar_io_kset = {
132 	.subsys = &pci_hotplug_slots_subsys,
133 	.kobj = {.name = DLPAR_KOBJ_NAME, .ktype=&ktype_dlpar_io,},
134 	.ktype = &ktype_dlpar_io,
135 };
136 
137 int dlpar_sysfs_init(void)
138 {
139 	if (kset_register(&dlpar_io_kset)) {
140 		printk(KERN_ERR "rpadlpar_io: cannot register kset for %s\n",
141 				dlpar_io_kset.kobj.name);
142 		return -EINVAL;
143 	}
144 
145 	return 0;
146 }
147 
148 void dlpar_sysfs_exit(void)
149 {
150 	kset_unregister(&dlpar_io_kset);
151 }
152