pvrusb2: reduce stack usage pvr2_eeprom_analyze()
[linux/fpc-iii.git] / drivers / net / bonding / bond_sysfs_slave.c
blob7d16c51e69131186ebe1dc5b656efb25c96e3096
1 /* Sysfs attributes of bond slaves
3 * Copyright (c) 2014 Scott Feldman <sfeldma@cumulusnetworks.com>
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 */
11 #include <linux/capability.h>
12 #include <linux/kernel.h>
13 #include <linux/netdevice.h>
15 #include <net/bonding.h>
17 struct slave_attribute {
18 struct attribute attr;
19 ssize_t (*show)(struct slave *, char *);
22 #define SLAVE_ATTR(_name, _mode, _show) \
23 const struct slave_attribute slave_attr_##_name = { \
24 .attr = {.name = __stringify(_name), \
25 .mode = _mode }, \
26 .show = _show, \
28 #define SLAVE_ATTR_RO(_name) \
29 SLAVE_ATTR(_name, S_IRUGO, _name##_show)
31 static ssize_t state_show(struct slave *slave, char *buf)
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:
39 return sprintf(buf, "UNKNOWN\n");
42 static SLAVE_ATTR_RO(state);
44 static ssize_t mii_status_show(struct slave *slave, char *buf)
46 return sprintf(buf, "%s\n", bond_slave_link_status(slave->link));
48 static SLAVE_ATTR_RO(mii_status);
50 static ssize_t link_failure_count_show(struct slave *slave, char *buf)
52 return sprintf(buf, "%d\n", slave->link_failure_count);
54 static SLAVE_ATTR_RO(link_failure_count);
56 static ssize_t perm_hwaddr_show(struct slave *slave, char *buf)
58 return sprintf(buf, "%pM\n", slave->perm_hwaddr);
60 static SLAVE_ATTR_RO(perm_hwaddr);
62 static ssize_t queue_id_show(struct slave *slave, char *buf)
64 return sprintf(buf, "%d\n", slave->queue_id);
66 static SLAVE_ATTR_RO(queue_id);
68 static ssize_t ad_aggregator_id_show(struct slave *slave, char *buf)
70 const struct aggregator *agg;
72 if (BOND_MODE(slave->bond) == BOND_MODE_8023AD) {
73 agg = SLAVE_AD_INFO(slave)->port.aggregator;
74 if (agg)
75 return sprintf(buf, "%d\n",
76 agg->aggregator_identifier);
79 return sprintf(buf, "N/A\n");
81 static SLAVE_ATTR_RO(ad_aggregator_id);
83 static ssize_t ad_actor_oper_port_state_show(struct slave *slave, char *buf)
85 const struct port *ad_port;
87 if (BOND_MODE(slave->bond) == BOND_MODE_8023AD) {
88 ad_port = &SLAVE_AD_INFO(slave)->port;
89 if (ad_port->aggregator)
90 return sprintf(buf, "%u\n",
91 ad_port->actor_oper_port_state);
94 return sprintf(buf, "N/A\n");
96 static SLAVE_ATTR_RO(ad_actor_oper_port_state);
98 static ssize_t ad_partner_oper_port_state_show(struct slave *slave, char *buf)
100 const struct port *ad_port;
102 if (BOND_MODE(slave->bond) == BOND_MODE_8023AD) {
103 ad_port = &SLAVE_AD_INFO(slave)->port;
104 if (ad_port->aggregator)
105 return sprintf(buf, "%u\n",
106 ad_port->partner_oper.port_state);
109 return sprintf(buf, "N/A\n");
111 static SLAVE_ATTR_RO(ad_partner_oper_port_state);
113 static const struct slave_attribute *slave_attrs[] = {
114 &slave_attr_state,
115 &slave_attr_mii_status,
116 &slave_attr_link_failure_count,
117 &slave_attr_perm_hwaddr,
118 &slave_attr_queue_id,
119 &slave_attr_ad_aggregator_id,
120 &slave_attr_ad_actor_oper_port_state,
121 &slave_attr_ad_partner_oper_port_state,
122 NULL
125 #define to_slave_attr(_at) container_of(_at, struct slave_attribute, attr)
126 #define to_slave(obj) container_of(obj, struct slave, kobj)
128 static ssize_t slave_show(struct kobject *kobj,
129 struct attribute *attr, char *buf)
131 struct slave_attribute *slave_attr = to_slave_attr(attr);
132 struct slave *slave = to_slave(kobj);
134 return slave_attr->show(slave, buf);
137 static const struct sysfs_ops slave_sysfs_ops = {
138 .show = slave_show,
141 static struct kobj_type slave_ktype = {
142 #ifdef CONFIG_SYSFS
143 .sysfs_ops = &slave_sysfs_ops,
144 #endif
147 int bond_sysfs_slave_add(struct slave *slave)
149 const struct slave_attribute **a;
150 int err;
152 err = kobject_init_and_add(&slave->kobj, &slave_ktype,
153 &(slave->dev->dev.kobj), "bonding_slave");
154 if (err)
155 return err;
157 for (a = slave_attrs; *a; ++a) {
158 err = sysfs_create_file(&slave->kobj, &((*a)->attr));
159 if (err) {
160 kobject_put(&slave->kobj);
161 return err;
165 return 0;
168 void bond_sysfs_slave_del(struct slave *slave)
170 const struct slave_attribute **a;
172 for (a = slave_attrs; *a; ++a)
173 sysfs_remove_file(&slave->kobj, &((*a)->attr));
175 kobject_put(&slave->kobj);