Merge tag 'asm-generic-nommu' of git://git.kernel.org/pub/scm/linux/kernel/git/arnd...
[linux-2.6-block.git] / drivers / net / bonding / bond_sysfs_slave.c
CommitLineData
07699f9a 1/* Sysfs attributes of bond slaves
2 *
3 * Copyright (c) 2014 Scott Feldman <sfeldma@cumulusnetworks.com>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
9 */
10
11#include <linux/capability.h>
12#include <linux/kernel.h>
13#include <linux/netdevice.h>
14
1ef8019b 15#include <net/bonding.h>
07699f9a 16
17struct slave_attribute {
18 struct attribute attr;
19 ssize_t (*show)(struct slave *, char *);
20};
21
22#define SLAVE_ATTR(_name, _mode, _show) \
23const struct slave_attribute slave_attr_##_name = { \
24 .attr = {.name = __stringify(_name), \
25 .mode = _mode }, \
26 .show = _show, \
27};
d61e4038
JP
28#define SLAVE_ATTR_RO(_name) \
29 SLAVE_ATTR(_name, 0444, _name##_show)
07699f9a 30
31static ssize_t state_show(struct slave *slave, char *buf)
32{
33 switch (bond_slave_state(slave)) {
34 case BOND_STATE_ACTIVE:
35 return sprintf(buf, "active\n");
36 case BOND_STATE_BACKUP:
37 return sprintf(buf, "backup\n");
38 default:
9b13494c 39 return sprintf(buf, "UNKNOWN\n");
07699f9a 40 }
41}
42static SLAVE_ATTR_RO(state);
43
44static ssize_t mii_status_show(struct slave *slave, char *buf)
45{
46 return sprintf(buf, "%s\n", bond_slave_link_status(slave->link));
47}
48static SLAVE_ATTR_RO(mii_status);
49
50static ssize_t link_failure_count_show(struct slave *slave, char *buf)
51{
52 return sprintf(buf, "%d\n", slave->link_failure_count);
53}
54static SLAVE_ATTR_RO(link_failure_count);
55
56static ssize_t perm_hwaddr_show(struct slave *slave, char *buf)
57{
18bebc6d
KK
58 return sprintf(buf, "%*phC\n",
59 slave->dev->addr_len,
60 slave->perm_hwaddr);
07699f9a 61}
62static SLAVE_ATTR_RO(perm_hwaddr);
63
64static ssize_t queue_id_show(struct slave *slave, char *buf)
65{
66 return sprintf(buf, "%d\n", slave->queue_id);
67}
68static SLAVE_ATTR_RO(queue_id);
69
70static ssize_t ad_aggregator_id_show(struct slave *slave, char *buf)
71{
72 const struct aggregator *agg;
73
01844098 74 if (BOND_MODE(slave->bond) == BOND_MODE_8023AD) {
3fdddd85 75 agg = SLAVE_AD_INFO(slave)->port.aggregator;
07699f9a 76 if (agg)
77 return sprintf(buf, "%d\n",
78 agg->aggregator_identifier);
79 }
80
81 return sprintf(buf, "N/A\n");
82}
83static SLAVE_ATTR_RO(ad_aggregator_id);
84
254cb6db
NA
85static ssize_t ad_actor_oper_port_state_show(struct slave *slave, char *buf)
86{
87 const struct port *ad_port;
88
89 if (BOND_MODE(slave->bond) == BOND_MODE_8023AD) {
90 ad_port = &SLAVE_AD_INFO(slave)->port;
91 if (ad_port->aggregator)
92 return sprintf(buf, "%u\n",
93 ad_port->actor_oper_port_state);
94 }
95
96 return sprintf(buf, "N/A\n");
97}
98static SLAVE_ATTR_RO(ad_actor_oper_port_state);
99
46ea297e
NA
100static ssize_t ad_partner_oper_port_state_show(struct slave *slave, char *buf)
101{
102 const struct port *ad_port;
103
104 if (BOND_MODE(slave->bond) == BOND_MODE_8023AD) {
105 ad_port = &SLAVE_AD_INFO(slave)->port;
106 if (ad_port->aggregator)
107 return sprintf(buf, "%u\n",
108 ad_port->partner_oper.port_state);
109 }
110
111 return sprintf(buf, "N/A\n");
112}
113static SLAVE_ATTR_RO(ad_partner_oper_port_state);
114
07699f9a 115static const struct slave_attribute *slave_attrs[] = {
116 &slave_attr_state,
117 &slave_attr_mii_status,
118 &slave_attr_link_failure_count,
119 &slave_attr_perm_hwaddr,
120 &slave_attr_queue_id,
121 &slave_attr_ad_aggregator_id,
254cb6db 122 &slave_attr_ad_actor_oper_port_state,
46ea297e 123 &slave_attr_ad_partner_oper_port_state,
07699f9a 124 NULL
125};
126
127#define to_slave_attr(_at) container_of(_at, struct slave_attribute, attr)
128#define to_slave(obj) container_of(obj, struct slave, kobj)
129
130static ssize_t slave_show(struct kobject *kobj,
131 struct attribute *attr, char *buf)
132{
133 struct slave_attribute *slave_attr = to_slave_attr(attr);
134 struct slave *slave = to_slave(kobj);
135
136 return slave_attr->show(slave, buf);
137}
138
8d783163 139static const struct sysfs_ops slave_sysfs_ops = {
07699f9a 140 .show = slave_show,
141};
142
143static struct kobj_type slave_ktype = {
144#ifdef CONFIG_SYSFS
145 .sysfs_ops = &slave_sysfs_ops,
146#endif
147};
148
149int bond_sysfs_slave_add(struct slave *slave)
150{
151 const struct slave_attribute **a;
152 int err;
153
154 err = kobject_init_and_add(&slave->kobj, &slave_ktype,
0a9099f8 155 &(slave->dev->dev.kobj), "bonding_slave");
07699f9a 156 if (err)
157 return err;
158
159 for (a = slave_attrs; *a; ++a) {
160 err = sysfs_create_file(&slave->kobj, &((*a)->attr));
161 if (err) {
7afcaec4 162 kobject_put(&slave->kobj);
07699f9a 163 return err;
164 }
165 }
166
167 return 0;
168}
169
170void bond_sysfs_slave_del(struct slave *slave)
171{
172 const struct slave_attribute **a;
173
174 for (a = slave_attrs; *a; ++a)
175 sysfs_remove_file(&slave->kobj, &((*a)->attr));
176
7afcaec4 177 kobject_put(&slave->kobj);
07699f9a 178}