1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright IBM Corp. 2013
4 * Author(s): Eugene Crosser <eugene.crosser@ru.ibm.com>
7 #include <linux/slab.h>
8 #include <asm/ebcdic.h>
12 static ssize_t qeth_bridge_port_role_state_show(struct device *dev,
13 struct device_attribute *attr, char *buf,
16 struct qeth_card *card = dev_get_drvdata(dev);
17 enum qeth_sbp_states state = QETH_SBP_STATE_INACTIVE;
21 if (qeth_l2_vnicc_is_in_use(card))
22 return sprintf(buf, "n/a (VNIC characteristics)\n");
24 mutex_lock(&card->sbp_lock);
25 if (qeth_card_hw_is_reachable(card) &&
26 card->options.sbp.supported_funcs)
27 rc = qeth_bridgeport_query_ports(card,
28 &card->options.sbp.role, &state);
32 case QETH_SBP_STATE_INACTIVE:
33 word = "inactive"; break;
34 case QETH_SBP_STATE_STANDBY:
35 word = "standby"; break;
36 case QETH_SBP_STATE_ACTIVE:
37 word = "active"; break;
42 switch (card->options.sbp.role) {
43 case QETH_SBP_ROLE_NONE:
45 case QETH_SBP_ROLE_PRIMARY:
46 word = "primary"; break;
47 case QETH_SBP_ROLE_SECONDARY:
48 word = "secondary"; break;
53 QETH_CARD_TEXT_(card, 2, "SBP%02x:%02x",
54 card->options.sbp.role, state);
56 rc = sprintf(buf, "%s\n", word);
58 mutex_unlock(&card->sbp_lock);
63 static ssize_t qeth_bridge_port_role_show(struct device *dev,
64 struct device_attribute *attr, char *buf)
66 struct qeth_card *card = dev_get_drvdata(dev);
68 if (qeth_l2_vnicc_is_in_use(card))
69 return sprintf(buf, "n/a (VNIC characteristics)\n");
71 return qeth_bridge_port_role_state_show(dev, attr, buf, 0);
74 static ssize_t qeth_bridge_port_role_store(struct device *dev,
75 struct device_attribute *attr, const char *buf, size_t count)
77 struct qeth_card *card = dev_get_drvdata(dev);
79 enum qeth_sbp_roles role;
81 if (sysfs_streq(buf, "primary"))
82 role = QETH_SBP_ROLE_PRIMARY;
83 else if (sysfs_streq(buf, "secondary"))
84 role = QETH_SBP_ROLE_SECONDARY;
85 else if (sysfs_streq(buf, "none"))
86 role = QETH_SBP_ROLE_NONE;
90 mutex_lock(&card->conf_mutex);
91 mutex_lock(&card->sbp_lock);
93 if (qeth_l2_vnicc_is_in_use(card))
95 else if (card->options.sbp.reflect_promisc)
96 /* Forbid direct manipulation */
98 else if (qeth_card_hw_is_reachable(card)) {
99 rc = qeth_bridgeport_setrole(card, role);
101 card->options.sbp.role = role;
103 card->options.sbp.role = role;
105 mutex_unlock(&card->sbp_lock);
106 mutex_unlock(&card->conf_mutex);
108 return rc ? rc : count;
111 static DEVICE_ATTR(bridge_role, 0644, qeth_bridge_port_role_show,
112 qeth_bridge_port_role_store);
114 static ssize_t qeth_bridge_port_state_show(struct device *dev,
115 struct device_attribute *attr, char *buf)
117 struct qeth_card *card = dev_get_drvdata(dev);
119 if (qeth_l2_vnicc_is_in_use(card))
120 return sprintf(buf, "n/a (VNIC characteristics)\n");
122 return qeth_bridge_port_role_state_show(dev, attr, buf, 1);
125 static DEVICE_ATTR(bridge_state, 0444, qeth_bridge_port_state_show,
128 static ssize_t qeth_bridgeport_hostnotification_show(struct device *dev,
129 struct device_attribute *attr, char *buf)
131 struct qeth_card *card = dev_get_drvdata(dev);
134 if (qeth_l2_vnicc_is_in_use(card))
135 return sprintf(buf, "n/a (VNIC characteristics)\n");
137 enabled = card->options.sbp.hostnotification;
139 return sprintf(buf, "%d\n", enabled);
142 static ssize_t qeth_bridgeport_hostnotification_store(struct device *dev,
143 struct device_attribute *attr, const char *buf, size_t count)
145 struct qeth_card *card = dev_get_drvdata(dev);
149 rc = kstrtobool(buf, &enable);
153 mutex_lock(&card->conf_mutex);
154 mutex_lock(&card->sbp_lock);
156 if (qeth_l2_vnicc_is_in_use(card))
158 else if (qeth_card_hw_is_reachable(card)) {
159 rc = qeth_bridgeport_an_set(card, enable);
161 card->options.sbp.hostnotification = enable;
163 card->options.sbp.hostnotification = enable;
165 mutex_unlock(&card->sbp_lock);
166 mutex_unlock(&card->conf_mutex);
168 return rc ? rc : count;
171 static DEVICE_ATTR(bridge_hostnotify, 0644,
172 qeth_bridgeport_hostnotification_show,
173 qeth_bridgeport_hostnotification_store);
175 static ssize_t qeth_bridgeport_reflect_show(struct device *dev,
176 struct device_attribute *attr, char *buf)
178 struct qeth_card *card = dev_get_drvdata(dev);
181 if (qeth_l2_vnicc_is_in_use(card))
182 return sprintf(buf, "n/a (VNIC characteristics)\n");
184 if (card->options.sbp.reflect_promisc) {
185 if (card->options.sbp.reflect_promisc_primary)
192 return sprintf(buf, "%s\n", state);
195 static ssize_t qeth_bridgeport_reflect_store(struct device *dev,
196 struct device_attribute *attr, const char *buf, size_t count)
198 struct qeth_card *card = dev_get_drvdata(dev);
202 if (sysfs_streq(buf, "none")) {
205 } else if (sysfs_streq(buf, "primary")) {
208 } else if (sysfs_streq(buf, "secondary")) {
214 mutex_lock(&card->conf_mutex);
215 mutex_lock(&card->sbp_lock);
217 if (qeth_l2_vnicc_is_in_use(card))
219 else if (card->options.sbp.role != QETH_SBP_ROLE_NONE)
222 card->options.sbp.reflect_promisc = enable;
223 card->options.sbp.reflect_promisc_primary = primary;
227 mutex_unlock(&card->sbp_lock);
228 mutex_unlock(&card->conf_mutex);
230 return rc ? rc : count;
233 static DEVICE_ATTR(bridge_reflect_promisc, 0644,
234 qeth_bridgeport_reflect_show,
235 qeth_bridgeport_reflect_store);
237 static struct attribute *qeth_l2_bridgeport_attrs[] = {
238 &dev_attr_bridge_role.attr,
239 &dev_attr_bridge_state.attr,
240 &dev_attr_bridge_hostnotify.attr,
241 &dev_attr_bridge_reflect_promisc.attr,
245 static struct attribute_group qeth_l2_bridgeport_attr_group = {
246 .attrs = qeth_l2_bridgeport_attrs,
250 * qeth_l2_setup_bridgeport_attrs() - set/restore attrs when turning online.
251 * @card: qeth_card structure pointer
253 * Note: this function is called with conf_mutex held by the caller
255 void qeth_l2_setup_bridgeport_attrs(struct qeth_card *card)
261 if (!card->options.sbp.supported_funcs)
264 mutex_lock(&card->sbp_lock);
265 if (card->options.sbp.role != QETH_SBP_ROLE_NONE) {
266 /* Conditional to avoid spurious error messages */
267 qeth_bridgeport_setrole(card, card->options.sbp.role);
268 /* Let the callback function refresh the stored role value. */
269 qeth_bridgeport_query_ports(card,
270 &card->options.sbp.role, NULL);
272 if (card->options.sbp.hostnotification) {
273 rc = qeth_bridgeport_an_set(card, 1);
275 card->options.sbp.hostnotification = 0;
277 qeth_bridgeport_an_set(card, 0);
279 mutex_unlock(&card->sbp_lock);
282 /* VNIC CHARS support */
284 /* convert sysfs attr name to VNIC characteristic */
285 static u32 qeth_l2_vnicc_sysfs_attr_to_char(const char *attr_name)
287 if (sysfs_streq(attr_name, "flooding"))
288 return QETH_VNICC_FLOODING;
289 else if (sysfs_streq(attr_name, "mcast_flooding"))
290 return QETH_VNICC_MCAST_FLOODING;
291 else if (sysfs_streq(attr_name, "learning"))
292 return QETH_VNICC_LEARNING;
293 else if (sysfs_streq(attr_name, "takeover_setvmac"))
294 return QETH_VNICC_TAKEOVER_SETVMAC;
295 else if (sysfs_streq(attr_name, "takeover_learning"))
296 return QETH_VNICC_TAKEOVER_LEARNING;
297 else if (sysfs_streq(attr_name, "bridge_invisible"))
298 return QETH_VNICC_BRIDGE_INVISIBLE;
299 else if (sysfs_streq(attr_name, "rx_bcast"))
300 return QETH_VNICC_RX_BCAST;
305 /* get current timeout setting */
306 static ssize_t qeth_vnicc_timeout_show(struct device *dev,
307 struct device_attribute *attr, char *buf)
309 struct qeth_card *card = dev_get_drvdata(dev);
313 rc = qeth_l2_vnicc_get_timeout(card, &timeout);
315 return sprintf(buf, "n/a (BridgePort)\n");
316 if (rc == -EOPNOTSUPP)
317 return sprintf(buf, "n/a\n");
318 return rc ? rc : sprintf(buf, "%d\n", timeout);
321 /* change timeout setting */
322 static ssize_t qeth_vnicc_timeout_store(struct device *dev,
323 struct device_attribute *attr,
324 const char *buf, size_t count)
326 struct qeth_card *card = dev_get_drvdata(dev);
330 rc = kstrtou32(buf, 10, &timeout);
334 mutex_lock(&card->conf_mutex);
335 rc = qeth_l2_vnicc_set_timeout(card, timeout);
336 mutex_unlock(&card->conf_mutex);
337 return rc ? rc : count;
340 /* get current setting of characteristic */
341 static ssize_t qeth_vnicc_char_show(struct device *dev,
342 struct device_attribute *attr, char *buf)
344 struct qeth_card *card = dev_get_drvdata(dev);
349 vnicc = qeth_l2_vnicc_sysfs_attr_to_char(attr->attr.name);
350 rc = qeth_l2_vnicc_get_state(card, vnicc, &state);
353 return sprintf(buf, "n/a (BridgePort)\n");
354 if (rc == -EOPNOTSUPP)
355 return sprintf(buf, "n/a\n");
356 return rc ? rc : sprintf(buf, "%d\n", state);
359 /* change setting of characteristic */
360 static ssize_t qeth_vnicc_char_store(struct device *dev,
361 struct device_attribute *attr,
362 const char *buf, size_t count)
364 struct qeth_card *card = dev_get_drvdata(dev);
369 if (kstrtobool(buf, &state))
372 vnicc = qeth_l2_vnicc_sysfs_attr_to_char(attr->attr.name);
373 mutex_lock(&card->conf_mutex);
374 rc = qeth_l2_vnicc_set_state(card, vnicc, state);
375 mutex_unlock(&card->conf_mutex);
377 return rc ? rc : count;
380 static DEVICE_ATTR(flooding, 0644, qeth_vnicc_char_show, qeth_vnicc_char_store);
381 static DEVICE_ATTR(mcast_flooding, 0644, qeth_vnicc_char_show,
382 qeth_vnicc_char_store);
383 static DEVICE_ATTR(learning, 0644, qeth_vnicc_char_show, qeth_vnicc_char_store);
384 static DEVICE_ATTR(learning_timeout, 0644, qeth_vnicc_timeout_show,
385 qeth_vnicc_timeout_store);
386 static DEVICE_ATTR(takeover_setvmac, 0644, qeth_vnicc_char_show,
387 qeth_vnicc_char_store);
388 static DEVICE_ATTR(takeover_learning, 0644, qeth_vnicc_char_show,
389 qeth_vnicc_char_store);
390 static DEVICE_ATTR(bridge_invisible, 0644, qeth_vnicc_char_show,
391 qeth_vnicc_char_store);
392 static DEVICE_ATTR(rx_bcast, 0644, qeth_vnicc_char_show, qeth_vnicc_char_store);
394 static struct attribute *qeth_l2_vnicc_attrs[] = {
395 &dev_attr_flooding.attr,
396 &dev_attr_mcast_flooding.attr,
397 &dev_attr_learning.attr,
398 &dev_attr_learning_timeout.attr,
399 &dev_attr_takeover_setvmac.attr,
400 &dev_attr_takeover_learning.attr,
401 &dev_attr_bridge_invisible.attr,
402 &dev_attr_rx_bcast.attr,
406 static struct attribute_group qeth_l2_vnicc_attr_group = {
407 .attrs = qeth_l2_vnicc_attrs,
411 static const struct attribute_group *qeth_l2_only_attr_groups[] = {
412 &qeth_l2_bridgeport_attr_group,
413 &qeth_l2_vnicc_attr_group,
417 int qeth_l2_create_device_attributes(struct device *dev)
419 return sysfs_create_groups(&dev->kobj, qeth_l2_only_attr_groups);
422 void qeth_l2_remove_device_attributes(struct device *dev)
424 sysfs_remove_groups(&dev->kobj, qeth_l2_only_attr_groups);
427 const struct attribute_group *qeth_l2_attr_groups[] = {
428 &qeth_device_attr_group,
429 &qeth_device_blkt_group,
430 /* l2 specific, see qeth_l2_only_attr_groups: */
431 &qeth_l2_bridgeport_attr_group,
432 &qeth_l2_vnicc_attr_group,