summaryrefslogtreecommitdiffstats
path: root/drivers/pci/hotplug/rpadlpar_sysfs.c
blob: 3285b822478da67bed4fe5672ae44a5939bc0e4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * Interface for Dynamic Logical Partitioning of I/O Slots on
 * RPA-compliant PPC64 platform.
 *
 * John Rose <johnrose@austin.ibm.com>
 * October 2003
 *
 * Copyright (C) 2003 IBM.
 *
 *      This program is free software; you can redistribute it and/or
 *      modify it under the terms of the GNU General Public License
 *      as published by the Free Software Foundation; either version
 *      2 of the License, or (at your option) any later version.
 */
#include <linux/kobject.h>
#include <linux/string.h>
#include "pci_hotplug.h"
#include "rpadlpar.h"

#define DLPAR_KOBJ_NAME       "control"
#define ADD_SLOT_ATTR_NAME    "add_slot"
#define REMOVE_SLOT_ATTR_NAME "remove_slot"

#define MAX_DRC_NAME_LEN 64

/* Store return code of dlpar operation in attribute struct */
struct dlpar_io_attr {
	int rc;
	struct attribute attr;
	ssize_t (*store)(struct dlpar_io_attr *dlpar_attr, const char *buf,
		size_t nbytes);
};

/* Common show callback for all attrs, display the return code
 * of the dlpar op */
static ssize_t
dlpar_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
{
	struct dlpar_io_attr *dlpar_attr = container_of(attr,
						struct dlpar_io_attr, attr);
	return sprintf(buf, "%d\n", dlpar_attr->rc);
}

static ssize_t
dlpar_attr_store(struct kobject * kobj, struct attribute * attr,
		 const char *buf, size_t nbytes)
{
	struct dlpar_io_attr *dlpar_attr = container_of(attr,
						struct dlpar_io_attr, attr);
	return dlpar_attr->store ?
		dlpar_attr->store(dlpar_attr, buf, nbytes) : 0;
}

static struct sysfs_ops dlpar_attr_sysfs_ops = {
	.show = dlpar_attr_show,
	.store = dlpar_attr_store,
};

static ssize_t add_slot_store(struct dlpar_io_attr *dlpar_attr,
				const char *buf, size_t nbytes)
{
	char drc_name[MAX_DRC_NAME_LEN];
	char *end;

	if (nbytes > MAX_DRC_NAME_LEN)
		return 0;

	memcpy(drc_name, buf, nbytes);

	end = strchr(drc_name, '\n');
	if (!end)
		end = &drc_name[nbytes];
	*end = '\0';

	dlpar_attr->rc = dlpar_add_slot(drc_name);

	return nbytes;
}

static ssize_t remove_slot_store(struct dlpar_io_attr *dlpar_attr,
		 		const char *buf, size_t nbytes)
{
	char drc_name[MAX_DRC_NAME_LEN];
	char *end;

	if (nbytes > MAX_DRC_NAME_LEN)
		return 0;

	memcpy(drc_name, buf, nbytes);

	end = strchr(drc_name, '\n');
	if (!end)
		end = &drc_name[nbytes];
	*end = '\0';

	dlpar_attr->rc = dlpar_remove_slot(drc_name);

	return nbytes;
}

static struct dlpar_io_attr add_slot_attr = {
	.rc = 0,
	.attr = { .name = ADD_SLOT_ATTR_NAME, .mode = 0644, },
	.store = add_slot_store,
};

static struct dlpar_io_attr remove_slot_attr = {
	.rc = 0,
	.attr = { .name = REMOVE_SLOT_ATTR_NAME, .mode = 0644},
	.store = remove_slot_store,
};

static struct attribute *default_attrs[] = {
	&add_slot_attr.attr,
	&remove_slot_attr.attr,
	NULL,
};

static void dlpar_io_release(struct kobject *kobj)
{
	/* noop */
	return;
}

struct kobj_type ktype_dlpar_io = {
	.release = dlpar_io_release,
	.sysfs_ops = &dlpar_attr_sysfs_ops,
	.default_attrs = default_attrs,
};

struct kset dlpar_io_kset = {
	.subsys = &pci_hotplug_slots_subsys,
	.kobj = {.name = DLPAR_KOBJ_NAME, .ktype=&ktype_dlpar_io,},
	.ktype = &ktype_dlpar_io,
};

int dlpar_sysfs_init(void)
{
	if (kset_register(&dlpar_io_kset)) {
		printk(KERN_ERR "rpadlpar_io: cannot register kset for %s\n",
				dlpar_io_kset.kobj.name);
		return -EINVAL;
	}

	return 0;
}

void dlpar_sysfs_exit(void)
{
	kset_unregister(&dlpar_io_kset);
}