udf: improve error management in udf_CS0toUTF8()
[linux/fpc-iii.git] / drivers / s390 / net / qeth_l3_sys.c
blob386eb7b89b1ea23b58483284410dba10349c5dcd
1 /*
2 * Copyright IBM Corp. 2007
3 * Author(s): Utz Bacher <utz.bacher@de.ibm.com>,
4 * Frank Pavlic <fpavlic@de.ibm.com>,
5 * Thomas Spatzier <tspat@de.ibm.com>,
6 * Frank Blaschka <frank.blaschka@de.ibm.com>
7 */
9 #include <linux/slab.h>
10 #include <asm/ebcdic.h>
11 #include "qeth_l3.h"
13 #define QETH_DEVICE_ATTR(_id, _name, _mode, _show, _store) \
14 struct device_attribute dev_attr_##_id = __ATTR(_name, _mode, _show, _store)
16 static ssize_t qeth_l3_dev_route_show(struct qeth_card *card,
17 struct qeth_routing_info *route, char *buf)
19 switch (route->type) {
20 case PRIMARY_ROUTER:
21 return sprintf(buf, "%s\n", "primary router");
22 case SECONDARY_ROUTER:
23 return sprintf(buf, "%s\n", "secondary router");
24 case MULTICAST_ROUTER:
25 if (card->info.broadcast_capable == QETH_BROADCAST_WITHOUT_ECHO)
26 return sprintf(buf, "%s\n", "multicast router+");
27 else
28 return sprintf(buf, "%s\n", "multicast router");
29 case PRIMARY_CONNECTOR:
30 if (card->info.broadcast_capable == QETH_BROADCAST_WITHOUT_ECHO)
31 return sprintf(buf, "%s\n", "primary connector+");
32 else
33 return sprintf(buf, "%s\n", "primary connector");
34 case SECONDARY_CONNECTOR:
35 if (card->info.broadcast_capable == QETH_BROADCAST_WITHOUT_ECHO)
36 return sprintf(buf, "%s\n", "secondary connector+");
37 else
38 return sprintf(buf, "%s\n", "secondary connector");
39 default:
40 return sprintf(buf, "%s\n", "no");
44 static ssize_t qeth_l3_dev_route4_show(struct device *dev,
45 struct device_attribute *attr, char *buf)
47 struct qeth_card *card = dev_get_drvdata(dev);
49 if (!card)
50 return -EINVAL;
52 return qeth_l3_dev_route_show(card, &card->options.route4, buf);
55 static ssize_t qeth_l3_dev_route_store(struct qeth_card *card,
56 struct qeth_routing_info *route, enum qeth_prot_versions prot,
57 const char *buf, size_t count)
59 enum qeth_routing_types old_route_type = route->type;
60 int rc = 0;
62 mutex_lock(&card->conf_mutex);
63 if (sysfs_streq(buf, "no_router")) {
64 route->type = NO_ROUTER;
65 } else if (sysfs_streq(buf, "primary_connector")) {
66 route->type = PRIMARY_CONNECTOR;
67 } else if (sysfs_streq(buf, "secondary_connector")) {
68 route->type = SECONDARY_CONNECTOR;
69 } else if (sysfs_streq(buf, "primary_router")) {
70 route->type = PRIMARY_ROUTER;
71 } else if (sysfs_streq(buf, "secondary_router")) {
72 route->type = SECONDARY_ROUTER;
73 } else if (sysfs_streq(buf, "multicast_router")) {
74 route->type = MULTICAST_ROUTER;
75 } else {
76 rc = -EINVAL;
77 goto out;
79 if (qeth_card_hw_is_reachable(card) &&
80 (old_route_type != route->type)) {
81 if (prot == QETH_PROT_IPV4)
82 rc = qeth_l3_setrouting_v4(card);
83 else if (prot == QETH_PROT_IPV6)
84 rc = qeth_l3_setrouting_v6(card);
86 out:
87 if (rc)
88 route->type = old_route_type;
89 mutex_unlock(&card->conf_mutex);
90 return rc ? rc : count;
93 static ssize_t qeth_l3_dev_route4_store(struct device *dev,
94 struct device_attribute *attr, const char *buf, size_t count)
96 struct qeth_card *card = dev_get_drvdata(dev);
98 if (!card)
99 return -EINVAL;
101 return qeth_l3_dev_route_store(card, &card->options.route4,
102 QETH_PROT_IPV4, buf, count);
105 static DEVICE_ATTR(route4, 0644, qeth_l3_dev_route4_show,
106 qeth_l3_dev_route4_store);
108 static ssize_t qeth_l3_dev_route6_show(struct device *dev,
109 struct device_attribute *attr, char *buf)
111 struct qeth_card *card = dev_get_drvdata(dev);
113 if (!card)
114 return -EINVAL;
116 return qeth_l3_dev_route_show(card, &card->options.route6, buf);
119 static ssize_t qeth_l3_dev_route6_store(struct device *dev,
120 struct device_attribute *attr, const char *buf, size_t count)
122 struct qeth_card *card = dev_get_drvdata(dev);
124 if (!card)
125 return -EINVAL;
127 return qeth_l3_dev_route_store(card, &card->options.route6,
128 QETH_PROT_IPV6, buf, count);
131 static DEVICE_ATTR(route6, 0644, qeth_l3_dev_route6_show,
132 qeth_l3_dev_route6_store);
134 static ssize_t qeth_l3_dev_fake_broadcast_show(struct device *dev,
135 struct device_attribute *attr, char *buf)
137 struct qeth_card *card = dev_get_drvdata(dev);
139 if (!card)
140 return -EINVAL;
142 return sprintf(buf, "%i\n", card->options.fake_broadcast? 1:0);
145 static ssize_t qeth_l3_dev_fake_broadcast_store(struct device *dev,
146 struct device_attribute *attr, const char *buf, size_t count)
148 struct qeth_card *card = dev_get_drvdata(dev);
149 char *tmp;
150 int i, rc = 0;
152 if (!card)
153 return -EINVAL;
155 mutex_lock(&card->conf_mutex);
156 if ((card->state != CARD_STATE_DOWN) &&
157 (card->state != CARD_STATE_RECOVER)) {
158 rc = -EPERM;
159 goto out;
162 i = simple_strtoul(buf, &tmp, 16);
163 if ((i == 0) || (i == 1))
164 card->options.fake_broadcast = i;
165 else
166 rc = -EINVAL;
167 out:
168 mutex_unlock(&card->conf_mutex);
169 return rc ? rc : count;
172 static DEVICE_ATTR(fake_broadcast, 0644, qeth_l3_dev_fake_broadcast_show,
173 qeth_l3_dev_fake_broadcast_store);
175 static ssize_t qeth_l3_dev_sniffer_show(struct device *dev,
176 struct device_attribute *attr, char *buf)
178 struct qeth_card *card = dev_get_drvdata(dev);
180 if (!card)
181 return -EINVAL;
183 return sprintf(buf, "%i\n", card->options.sniffer ? 1 : 0);
186 static ssize_t qeth_l3_dev_sniffer_store(struct device *dev,
187 struct device_attribute *attr, const char *buf, size_t count)
189 struct qeth_card *card = dev_get_drvdata(dev);
190 int rc = 0;
191 unsigned long i;
193 if (!card)
194 return -EINVAL;
196 if (card->info.type != QETH_CARD_TYPE_IQD)
197 return -EPERM;
198 if (card->options.cq == QETH_CQ_ENABLED)
199 return -EPERM;
201 mutex_lock(&card->conf_mutex);
202 if ((card->state != CARD_STATE_DOWN) &&
203 (card->state != CARD_STATE_RECOVER)) {
204 rc = -EPERM;
205 goto out;
208 rc = kstrtoul(buf, 16, &i);
209 if (rc) {
210 rc = -EINVAL;
211 goto out;
213 switch (i) {
214 case 0:
215 card->options.sniffer = i;
216 break;
217 case 1:
218 qdio_get_ssqd_desc(CARD_DDEV(card), &card->ssqd);
219 if (card->ssqd.qdioac2 & QETH_SNIFF_AVAIL) {
220 card->options.sniffer = i;
221 if (card->qdio.init_pool.buf_count !=
222 QETH_IN_BUF_COUNT_MAX)
223 qeth_realloc_buffer_pool(card,
224 QETH_IN_BUF_COUNT_MAX);
225 } else
226 rc = -EPERM;
227 break;
228 default:
229 rc = -EINVAL;
231 out:
232 mutex_unlock(&card->conf_mutex);
233 return rc ? rc : count;
236 static DEVICE_ATTR(sniffer, 0644, qeth_l3_dev_sniffer_show,
237 qeth_l3_dev_sniffer_store);
240 static ssize_t qeth_l3_dev_hsuid_show(struct device *dev,
241 struct device_attribute *attr, char *buf)
243 struct qeth_card *card = dev_get_drvdata(dev);
244 char tmp_hsuid[9];
246 if (!card)
247 return -EINVAL;
249 if (card->info.type != QETH_CARD_TYPE_IQD)
250 return -EPERM;
252 if (card->state == CARD_STATE_DOWN)
253 return -EPERM;
255 memcpy(tmp_hsuid, card->options.hsuid, sizeof(tmp_hsuid));
256 EBCASC(tmp_hsuid, 8);
257 return sprintf(buf, "%s\n", tmp_hsuid);
260 static ssize_t qeth_l3_dev_hsuid_store(struct device *dev,
261 struct device_attribute *attr, const char *buf, size_t count)
263 struct qeth_card *card = dev_get_drvdata(dev);
264 struct qeth_ipaddr *addr;
265 char *tmp;
266 int i;
268 if (!card)
269 return -EINVAL;
271 if (card->info.type != QETH_CARD_TYPE_IQD)
272 return -EPERM;
273 if (card->state != CARD_STATE_DOWN &&
274 card->state != CARD_STATE_RECOVER)
275 return -EPERM;
276 if (card->options.sniffer)
277 return -EPERM;
278 if (card->options.cq == QETH_CQ_NOTAVAILABLE)
279 return -EPERM;
281 tmp = strsep((char **)&buf, "\n");
282 if (strlen(tmp) > 8)
283 return -EINVAL;
285 if (card->options.hsuid[0]) {
286 /* delete old ip address */
287 addr = qeth_l3_get_addr_buffer(QETH_PROT_IPV6);
288 if (addr != NULL) {
289 addr->u.a6.addr.s6_addr32[0] = 0xfe800000;
290 addr->u.a6.addr.s6_addr32[1] = 0x00000000;
291 for (i = 8; i < 16; i++)
292 addr->u.a6.addr.s6_addr[i] =
293 card->options.hsuid[i - 8];
294 addr->u.a6.pfxlen = 0;
295 addr->type = QETH_IP_TYPE_NORMAL;
296 } else
297 return -ENOMEM;
298 if (!qeth_l3_delete_ip(card, addr))
299 kfree(addr);
300 qeth_l3_set_ip_addr_list(card);
303 if (strlen(tmp) == 0) {
304 /* delete ip address only */
305 card->options.hsuid[0] = '\0';
306 if (card->dev)
307 memcpy(card->dev->perm_addr, card->options.hsuid, 9);
308 qeth_configure_cq(card, QETH_CQ_DISABLED);
309 return count;
312 if (qeth_configure_cq(card, QETH_CQ_ENABLED))
313 return -EPERM;
315 snprintf(card->options.hsuid, sizeof(card->options.hsuid),
316 "%-8s", tmp);
317 ASCEBC(card->options.hsuid, 8);
318 if (card->dev)
319 memcpy(card->dev->perm_addr, card->options.hsuid, 9);
321 addr = qeth_l3_get_addr_buffer(QETH_PROT_IPV6);
322 if (addr != NULL) {
323 addr->u.a6.addr.s6_addr32[0] = 0xfe800000;
324 addr->u.a6.addr.s6_addr32[1] = 0x00000000;
325 for (i = 8; i < 16; i++)
326 addr->u.a6.addr.s6_addr[i] = card->options.hsuid[i - 8];
327 addr->u.a6.pfxlen = 0;
328 addr->type = QETH_IP_TYPE_NORMAL;
329 } else
330 return -ENOMEM;
331 if (!qeth_l3_add_ip(card, addr))
332 kfree(addr);
333 qeth_l3_set_ip_addr_list(card);
335 return count;
338 static DEVICE_ATTR(hsuid, 0644, qeth_l3_dev_hsuid_show,
339 qeth_l3_dev_hsuid_store);
342 static struct attribute *qeth_l3_device_attrs[] = {
343 &dev_attr_route4.attr,
344 &dev_attr_route6.attr,
345 &dev_attr_fake_broadcast.attr,
346 &dev_attr_sniffer.attr,
347 &dev_attr_hsuid.attr,
348 NULL,
351 static struct attribute_group qeth_l3_device_attr_group = {
352 .attrs = qeth_l3_device_attrs,
355 static ssize_t qeth_l3_dev_ipato_enable_show(struct device *dev,
356 struct device_attribute *attr, char *buf)
358 struct qeth_card *card = dev_get_drvdata(dev);
360 if (!card)
361 return -EINVAL;
363 return sprintf(buf, "%i\n", card->ipato.enabled? 1:0);
366 static ssize_t qeth_l3_dev_ipato_enable_store(struct device *dev,
367 struct device_attribute *attr, const char *buf, size_t count)
369 struct qeth_card *card = dev_get_drvdata(dev);
370 struct qeth_ipaddr *tmpipa, *t;
371 int rc = 0;
373 if (!card)
374 return -EINVAL;
376 mutex_lock(&card->conf_mutex);
377 if ((card->state != CARD_STATE_DOWN) &&
378 (card->state != CARD_STATE_RECOVER)) {
379 rc = -EPERM;
380 goto out;
383 if (sysfs_streq(buf, "toggle")) {
384 card->ipato.enabled = (card->ipato.enabled)? 0 : 1;
385 } else if (sysfs_streq(buf, "1")) {
386 card->ipato.enabled = 1;
387 list_for_each_entry_safe(tmpipa, t, card->ip_tbd_list, entry) {
388 if ((tmpipa->type == QETH_IP_TYPE_NORMAL) &&
389 qeth_l3_is_addr_covered_by_ipato(card, tmpipa))
390 tmpipa->set_flags |=
391 QETH_IPA_SETIP_TAKEOVER_FLAG;
394 } else if (sysfs_streq(buf, "0")) {
395 card->ipato.enabled = 0;
396 list_for_each_entry_safe(tmpipa, t, card->ip_tbd_list, entry) {
397 if (tmpipa->set_flags &
398 QETH_IPA_SETIP_TAKEOVER_FLAG)
399 tmpipa->set_flags &=
400 ~QETH_IPA_SETIP_TAKEOVER_FLAG;
402 } else
403 rc = -EINVAL;
404 out:
405 mutex_unlock(&card->conf_mutex);
406 return rc ? rc : count;
409 static QETH_DEVICE_ATTR(ipato_enable, enable, 0644,
410 qeth_l3_dev_ipato_enable_show,
411 qeth_l3_dev_ipato_enable_store);
413 static ssize_t qeth_l3_dev_ipato_invert4_show(struct device *dev,
414 struct device_attribute *attr, char *buf)
416 struct qeth_card *card = dev_get_drvdata(dev);
418 if (!card)
419 return -EINVAL;
421 return sprintf(buf, "%i\n", card->ipato.invert4? 1:0);
424 static ssize_t qeth_l3_dev_ipato_invert4_store(struct device *dev,
425 struct device_attribute *attr,
426 const char *buf, size_t count)
428 struct qeth_card *card = dev_get_drvdata(dev);
429 int rc = 0;
431 if (!card)
432 return -EINVAL;
434 mutex_lock(&card->conf_mutex);
435 if (sysfs_streq(buf, "toggle"))
436 card->ipato.invert4 = (card->ipato.invert4)? 0 : 1;
437 else if (sysfs_streq(buf, "1"))
438 card->ipato.invert4 = 1;
439 else if (sysfs_streq(buf, "0"))
440 card->ipato.invert4 = 0;
441 else
442 rc = -EINVAL;
443 mutex_unlock(&card->conf_mutex);
444 return rc ? rc : count;
447 static QETH_DEVICE_ATTR(ipato_invert4, invert4, 0644,
448 qeth_l3_dev_ipato_invert4_show,
449 qeth_l3_dev_ipato_invert4_store);
451 static ssize_t qeth_l3_dev_ipato_add_show(char *buf, struct qeth_card *card,
452 enum qeth_prot_versions proto)
454 struct qeth_ipato_entry *ipatoe;
455 unsigned long flags;
456 char addr_str[40];
457 int entry_len; /* length of 1 entry string, differs between v4 and v6 */
458 int i = 0;
460 entry_len = (proto == QETH_PROT_IPV4)? 12 : 40;
461 /* add strlen for "/<mask>\n" */
462 entry_len += (proto == QETH_PROT_IPV4)? 5 : 6;
463 spin_lock_irqsave(&card->ip_lock, flags);
464 list_for_each_entry(ipatoe, &card->ipato.entries, entry) {
465 if (ipatoe->proto != proto)
466 continue;
467 /* String must not be longer than PAGE_SIZE. So we check if
468 * string length gets near PAGE_SIZE. Then we can savely display
469 * the next IPv6 address (worst case, compared to IPv4) */
470 if ((PAGE_SIZE - i) <= entry_len)
471 break;
472 qeth_l3_ipaddr_to_string(proto, ipatoe->addr, addr_str);
473 i += snprintf(buf + i, PAGE_SIZE - i,
474 "%s/%i\n", addr_str, ipatoe->mask_bits);
476 spin_unlock_irqrestore(&card->ip_lock, flags);
477 i += snprintf(buf + i, PAGE_SIZE - i, "\n");
479 return i;
482 static ssize_t qeth_l3_dev_ipato_add4_show(struct device *dev,
483 struct device_attribute *attr, char *buf)
485 struct qeth_card *card = dev_get_drvdata(dev);
487 if (!card)
488 return -EINVAL;
490 return qeth_l3_dev_ipato_add_show(buf, card, QETH_PROT_IPV4);
493 static int qeth_l3_parse_ipatoe(const char *buf, enum qeth_prot_versions proto,
494 u8 *addr, int *mask_bits)
496 const char *start, *end;
497 char *tmp;
498 char buffer[40] = {0, };
500 start = buf;
501 /* get address string */
502 end = strchr(start, '/');
503 if (!end || (end - start >= 40)) {
504 return -EINVAL;
506 strncpy(buffer, start, end - start);
507 if (qeth_l3_string_to_ipaddr(buffer, proto, addr)) {
508 return -EINVAL;
510 start = end + 1;
511 *mask_bits = simple_strtoul(start, &tmp, 10);
512 if (!strlen(start) ||
513 (tmp == start) ||
514 (*mask_bits > ((proto == QETH_PROT_IPV4) ? 32 : 128))) {
515 return -EINVAL;
517 return 0;
520 static ssize_t qeth_l3_dev_ipato_add_store(const char *buf, size_t count,
521 struct qeth_card *card, enum qeth_prot_versions proto)
523 struct qeth_ipato_entry *ipatoe;
524 u8 addr[16];
525 int mask_bits;
526 int rc = 0;
528 mutex_lock(&card->conf_mutex);
529 rc = qeth_l3_parse_ipatoe(buf, proto, addr, &mask_bits);
530 if (rc)
531 goto out;
533 ipatoe = kzalloc(sizeof(struct qeth_ipato_entry), GFP_KERNEL);
534 if (!ipatoe) {
535 rc = -ENOMEM;
536 goto out;
538 ipatoe->proto = proto;
539 memcpy(ipatoe->addr, addr, (proto == QETH_PROT_IPV4)? 4:16);
540 ipatoe->mask_bits = mask_bits;
542 rc = qeth_l3_add_ipato_entry(card, ipatoe);
543 if (rc)
544 kfree(ipatoe);
545 out:
546 mutex_unlock(&card->conf_mutex);
547 return rc ? rc : count;
550 static ssize_t qeth_l3_dev_ipato_add4_store(struct device *dev,
551 struct device_attribute *attr, const char *buf, size_t count)
553 struct qeth_card *card = dev_get_drvdata(dev);
555 if (!card)
556 return -EINVAL;
558 return qeth_l3_dev_ipato_add_store(buf, count, card, QETH_PROT_IPV4);
561 static QETH_DEVICE_ATTR(ipato_add4, add4, 0644,
562 qeth_l3_dev_ipato_add4_show,
563 qeth_l3_dev_ipato_add4_store);
565 static ssize_t qeth_l3_dev_ipato_del_store(const char *buf, size_t count,
566 struct qeth_card *card, enum qeth_prot_versions proto)
568 u8 addr[16];
569 int mask_bits;
570 int rc = 0;
572 mutex_lock(&card->conf_mutex);
573 rc = qeth_l3_parse_ipatoe(buf, proto, addr, &mask_bits);
574 if (!rc)
575 qeth_l3_del_ipato_entry(card, proto, addr, mask_bits);
576 mutex_unlock(&card->conf_mutex);
577 return rc ? rc : count;
580 static ssize_t qeth_l3_dev_ipato_del4_store(struct device *dev,
581 struct device_attribute *attr, const char *buf, size_t count)
583 struct qeth_card *card = dev_get_drvdata(dev);
585 if (!card)
586 return -EINVAL;
588 return qeth_l3_dev_ipato_del_store(buf, count, card, QETH_PROT_IPV4);
591 static QETH_DEVICE_ATTR(ipato_del4, del4, 0200, NULL,
592 qeth_l3_dev_ipato_del4_store);
594 static ssize_t qeth_l3_dev_ipato_invert6_show(struct device *dev,
595 struct device_attribute *attr, char *buf)
597 struct qeth_card *card = dev_get_drvdata(dev);
599 if (!card)
600 return -EINVAL;
602 return sprintf(buf, "%i\n", card->ipato.invert6? 1:0);
605 static ssize_t qeth_l3_dev_ipato_invert6_store(struct device *dev,
606 struct device_attribute *attr, const char *buf, size_t count)
608 struct qeth_card *card = dev_get_drvdata(dev);
609 int rc = 0;
611 if (!card)
612 return -EINVAL;
614 mutex_lock(&card->conf_mutex);
615 if (sysfs_streq(buf, "toggle"))
616 card->ipato.invert6 = (card->ipato.invert6)? 0 : 1;
617 else if (sysfs_streq(buf, "1"))
618 card->ipato.invert6 = 1;
619 else if (sysfs_streq(buf, "0"))
620 card->ipato.invert6 = 0;
621 else
622 rc = -EINVAL;
623 mutex_unlock(&card->conf_mutex);
624 return rc ? rc : count;
627 static QETH_DEVICE_ATTR(ipato_invert6, invert6, 0644,
628 qeth_l3_dev_ipato_invert6_show,
629 qeth_l3_dev_ipato_invert6_store);
632 static ssize_t qeth_l3_dev_ipato_add6_show(struct device *dev,
633 struct device_attribute *attr, char *buf)
635 struct qeth_card *card = dev_get_drvdata(dev);
637 if (!card)
638 return -EINVAL;
640 return qeth_l3_dev_ipato_add_show(buf, card, QETH_PROT_IPV6);
643 static ssize_t qeth_l3_dev_ipato_add6_store(struct device *dev,
644 struct device_attribute *attr, const char *buf, size_t count)
646 struct qeth_card *card = dev_get_drvdata(dev);
648 if (!card)
649 return -EINVAL;
651 return qeth_l3_dev_ipato_add_store(buf, count, card, QETH_PROT_IPV6);
654 static QETH_DEVICE_ATTR(ipato_add6, add6, 0644,
655 qeth_l3_dev_ipato_add6_show,
656 qeth_l3_dev_ipato_add6_store);
658 static ssize_t qeth_l3_dev_ipato_del6_store(struct device *dev,
659 struct device_attribute *attr, const char *buf, size_t count)
661 struct qeth_card *card = dev_get_drvdata(dev);
663 if (!card)
664 return -EINVAL;
666 return qeth_l3_dev_ipato_del_store(buf, count, card, QETH_PROT_IPV6);
669 static QETH_DEVICE_ATTR(ipato_del6, del6, 0200, NULL,
670 qeth_l3_dev_ipato_del6_store);
672 static struct attribute *qeth_ipato_device_attrs[] = {
673 &dev_attr_ipato_enable.attr,
674 &dev_attr_ipato_invert4.attr,
675 &dev_attr_ipato_add4.attr,
676 &dev_attr_ipato_del4.attr,
677 &dev_attr_ipato_invert6.attr,
678 &dev_attr_ipato_add6.attr,
679 &dev_attr_ipato_del6.attr,
680 NULL,
683 static struct attribute_group qeth_device_ipato_group = {
684 .name = "ipa_takeover",
685 .attrs = qeth_ipato_device_attrs,
688 static ssize_t qeth_l3_dev_vipa_add_show(char *buf, struct qeth_card *card,
689 enum qeth_prot_versions proto)
691 struct qeth_ipaddr *ipaddr;
692 char addr_str[40];
693 int entry_len; /* length of 1 entry string, differs between v4 and v6 */
694 unsigned long flags;
695 int i = 0;
697 entry_len = (proto == QETH_PROT_IPV4)? 12 : 40;
698 entry_len += 2; /* \n + terminator */
699 spin_lock_irqsave(&card->ip_lock, flags);
700 list_for_each_entry(ipaddr, &card->ip_list, entry) {
701 if (ipaddr->proto != proto)
702 continue;
703 if (ipaddr->type != QETH_IP_TYPE_VIPA)
704 continue;
705 /* String must not be longer than PAGE_SIZE. So we check if
706 * string length gets near PAGE_SIZE. Then we can savely display
707 * the next IPv6 address (worst case, compared to IPv4) */
708 if ((PAGE_SIZE - i) <= entry_len)
709 break;
710 qeth_l3_ipaddr_to_string(proto, (const u8 *)&ipaddr->u,
711 addr_str);
712 i += snprintf(buf + i, PAGE_SIZE - i, "%s\n", addr_str);
714 spin_unlock_irqrestore(&card->ip_lock, flags);
715 i += snprintf(buf + i, PAGE_SIZE - i, "\n");
717 return i;
720 static ssize_t qeth_l3_dev_vipa_add4_show(struct device *dev,
721 struct device_attribute *attr, char *buf)
723 struct qeth_card *card = dev_get_drvdata(dev);
725 if (!card)
726 return -EINVAL;
728 return qeth_l3_dev_vipa_add_show(buf, card, QETH_PROT_IPV4);
731 static int qeth_l3_parse_vipae(const char *buf, enum qeth_prot_versions proto,
732 u8 *addr)
734 if (qeth_l3_string_to_ipaddr(buf, proto, addr)) {
735 return -EINVAL;
737 return 0;
740 static ssize_t qeth_l3_dev_vipa_add_store(const char *buf, size_t count,
741 struct qeth_card *card, enum qeth_prot_versions proto)
743 u8 addr[16] = {0, };
744 int rc;
746 mutex_lock(&card->conf_mutex);
747 rc = qeth_l3_parse_vipae(buf, proto, addr);
748 if (!rc)
749 rc = qeth_l3_add_vipa(card, proto, addr);
750 mutex_unlock(&card->conf_mutex);
751 return rc ? rc : count;
754 static ssize_t qeth_l3_dev_vipa_add4_store(struct device *dev,
755 struct device_attribute *attr, const char *buf, size_t count)
757 struct qeth_card *card = dev_get_drvdata(dev);
759 if (!card)
760 return -EINVAL;
762 return qeth_l3_dev_vipa_add_store(buf, count, card, QETH_PROT_IPV4);
765 static QETH_DEVICE_ATTR(vipa_add4, add4, 0644,
766 qeth_l3_dev_vipa_add4_show,
767 qeth_l3_dev_vipa_add4_store);
769 static ssize_t qeth_l3_dev_vipa_del_store(const char *buf, size_t count,
770 struct qeth_card *card, enum qeth_prot_versions proto)
772 u8 addr[16];
773 int rc;
775 mutex_lock(&card->conf_mutex);
776 rc = qeth_l3_parse_vipae(buf, proto, addr);
777 if (!rc)
778 qeth_l3_del_vipa(card, proto, addr);
779 mutex_unlock(&card->conf_mutex);
780 return rc ? rc : count;
783 static ssize_t qeth_l3_dev_vipa_del4_store(struct device *dev,
784 struct device_attribute *attr, const char *buf, size_t count)
786 struct qeth_card *card = dev_get_drvdata(dev);
788 if (!card)
789 return -EINVAL;
791 return qeth_l3_dev_vipa_del_store(buf, count, card, QETH_PROT_IPV4);
794 static QETH_DEVICE_ATTR(vipa_del4, del4, 0200, NULL,
795 qeth_l3_dev_vipa_del4_store);
797 static ssize_t qeth_l3_dev_vipa_add6_show(struct device *dev,
798 struct device_attribute *attr, char *buf)
800 struct qeth_card *card = dev_get_drvdata(dev);
802 if (!card)
803 return -EINVAL;
805 return qeth_l3_dev_vipa_add_show(buf, card, QETH_PROT_IPV6);
808 static ssize_t qeth_l3_dev_vipa_add6_store(struct device *dev,
809 struct device_attribute *attr, const char *buf, size_t count)
811 struct qeth_card *card = dev_get_drvdata(dev);
813 if (!card)
814 return -EINVAL;
816 return qeth_l3_dev_vipa_add_store(buf, count, card, QETH_PROT_IPV6);
819 static QETH_DEVICE_ATTR(vipa_add6, add6, 0644,
820 qeth_l3_dev_vipa_add6_show,
821 qeth_l3_dev_vipa_add6_store);
823 static ssize_t qeth_l3_dev_vipa_del6_store(struct device *dev,
824 struct device_attribute *attr, const char *buf, size_t count)
826 struct qeth_card *card = dev_get_drvdata(dev);
828 if (!card)
829 return -EINVAL;
831 return qeth_l3_dev_vipa_del_store(buf, count, card, QETH_PROT_IPV6);
834 static QETH_DEVICE_ATTR(vipa_del6, del6, 0200, NULL,
835 qeth_l3_dev_vipa_del6_store);
837 static struct attribute *qeth_vipa_device_attrs[] = {
838 &dev_attr_vipa_add4.attr,
839 &dev_attr_vipa_del4.attr,
840 &dev_attr_vipa_add6.attr,
841 &dev_attr_vipa_del6.attr,
842 NULL,
845 static struct attribute_group qeth_device_vipa_group = {
846 .name = "vipa",
847 .attrs = qeth_vipa_device_attrs,
850 static ssize_t qeth_l3_dev_rxip_add_show(char *buf, struct qeth_card *card,
851 enum qeth_prot_versions proto)
853 struct qeth_ipaddr *ipaddr;
854 char addr_str[40];
855 int entry_len; /* length of 1 entry string, differs between v4 and v6 */
856 unsigned long flags;
857 int i = 0;
859 entry_len = (proto == QETH_PROT_IPV4)? 12 : 40;
860 entry_len += 2; /* \n + terminator */
861 spin_lock_irqsave(&card->ip_lock, flags);
862 list_for_each_entry(ipaddr, &card->ip_list, entry) {
863 if (ipaddr->proto != proto)
864 continue;
865 if (ipaddr->type != QETH_IP_TYPE_RXIP)
866 continue;
867 /* String must not be longer than PAGE_SIZE. So we check if
868 * string length gets near PAGE_SIZE. Then we can savely display
869 * the next IPv6 address (worst case, compared to IPv4) */
870 if ((PAGE_SIZE - i) <= entry_len)
871 break;
872 qeth_l3_ipaddr_to_string(proto, (const u8 *)&ipaddr->u,
873 addr_str);
874 i += snprintf(buf + i, PAGE_SIZE - i, "%s\n", addr_str);
876 spin_unlock_irqrestore(&card->ip_lock, flags);
877 i += snprintf(buf + i, PAGE_SIZE - i, "\n");
879 return i;
882 static ssize_t qeth_l3_dev_rxip_add4_show(struct device *dev,
883 struct device_attribute *attr, char *buf)
885 struct qeth_card *card = dev_get_drvdata(dev);
887 if (!card)
888 return -EINVAL;
890 return qeth_l3_dev_rxip_add_show(buf, card, QETH_PROT_IPV4);
893 static int qeth_l3_parse_rxipe(const char *buf, enum qeth_prot_versions proto,
894 u8 *addr)
896 if (qeth_l3_string_to_ipaddr(buf, proto, addr)) {
897 return -EINVAL;
899 return 0;
902 static ssize_t qeth_l3_dev_rxip_add_store(const char *buf, size_t count,
903 struct qeth_card *card, enum qeth_prot_versions proto)
905 u8 addr[16] = {0, };
906 int rc;
908 mutex_lock(&card->conf_mutex);
909 rc = qeth_l3_parse_rxipe(buf, proto, addr);
910 if (!rc)
911 rc = qeth_l3_add_rxip(card, proto, addr);
912 mutex_unlock(&card->conf_mutex);
913 return rc ? rc : count;
916 static ssize_t qeth_l3_dev_rxip_add4_store(struct device *dev,
917 struct device_attribute *attr, const char *buf, size_t count)
919 struct qeth_card *card = dev_get_drvdata(dev);
921 if (!card)
922 return -EINVAL;
924 return qeth_l3_dev_rxip_add_store(buf, count, card, QETH_PROT_IPV4);
927 static QETH_DEVICE_ATTR(rxip_add4, add4, 0644,
928 qeth_l3_dev_rxip_add4_show,
929 qeth_l3_dev_rxip_add4_store);
931 static ssize_t qeth_l3_dev_rxip_del_store(const char *buf, size_t count,
932 struct qeth_card *card, enum qeth_prot_versions proto)
934 u8 addr[16];
935 int rc;
937 mutex_lock(&card->conf_mutex);
938 rc = qeth_l3_parse_rxipe(buf, proto, addr);
939 if (!rc)
940 qeth_l3_del_rxip(card, proto, addr);
941 mutex_unlock(&card->conf_mutex);
942 return rc ? rc : count;
945 static ssize_t qeth_l3_dev_rxip_del4_store(struct device *dev,
946 struct device_attribute *attr, const char *buf, size_t count)
948 struct qeth_card *card = dev_get_drvdata(dev);
950 if (!card)
951 return -EINVAL;
953 return qeth_l3_dev_rxip_del_store(buf, count, card, QETH_PROT_IPV4);
956 static QETH_DEVICE_ATTR(rxip_del4, del4, 0200, NULL,
957 qeth_l3_dev_rxip_del4_store);
959 static ssize_t qeth_l3_dev_rxip_add6_show(struct device *dev,
960 struct device_attribute *attr, char *buf)
962 struct qeth_card *card = dev_get_drvdata(dev);
964 if (!card)
965 return -EINVAL;
967 return qeth_l3_dev_rxip_add_show(buf, card, QETH_PROT_IPV6);
970 static ssize_t qeth_l3_dev_rxip_add6_store(struct device *dev,
971 struct device_attribute *attr, const char *buf, size_t count)
973 struct qeth_card *card = dev_get_drvdata(dev);
975 if (!card)
976 return -EINVAL;
978 return qeth_l3_dev_rxip_add_store(buf, count, card, QETH_PROT_IPV6);
981 static QETH_DEVICE_ATTR(rxip_add6, add6, 0644,
982 qeth_l3_dev_rxip_add6_show,
983 qeth_l3_dev_rxip_add6_store);
985 static ssize_t qeth_l3_dev_rxip_del6_store(struct device *dev,
986 struct device_attribute *attr, const char *buf, size_t count)
988 struct qeth_card *card = dev_get_drvdata(dev);
990 if (!card)
991 return -EINVAL;
993 return qeth_l3_dev_rxip_del_store(buf, count, card, QETH_PROT_IPV6);
996 static QETH_DEVICE_ATTR(rxip_del6, del6, 0200, NULL,
997 qeth_l3_dev_rxip_del6_store);
999 static struct attribute *qeth_rxip_device_attrs[] = {
1000 &dev_attr_rxip_add4.attr,
1001 &dev_attr_rxip_del4.attr,
1002 &dev_attr_rxip_add6.attr,
1003 &dev_attr_rxip_del6.attr,
1004 NULL,
1007 static struct attribute_group qeth_device_rxip_group = {
1008 .name = "rxip",
1009 .attrs = qeth_rxip_device_attrs,
1012 int qeth_l3_create_device_attributes(struct device *dev)
1014 int ret;
1016 ret = sysfs_create_group(&dev->kobj, &qeth_l3_device_attr_group);
1017 if (ret)
1018 return ret;
1020 ret = sysfs_create_group(&dev->kobj, &qeth_device_ipato_group);
1021 if (ret) {
1022 sysfs_remove_group(&dev->kobj, &qeth_l3_device_attr_group);
1023 return ret;
1026 ret = sysfs_create_group(&dev->kobj, &qeth_device_vipa_group);
1027 if (ret) {
1028 sysfs_remove_group(&dev->kobj, &qeth_l3_device_attr_group);
1029 sysfs_remove_group(&dev->kobj, &qeth_device_ipato_group);
1030 return ret;
1033 ret = sysfs_create_group(&dev->kobj, &qeth_device_rxip_group);
1034 if (ret) {
1035 sysfs_remove_group(&dev->kobj, &qeth_l3_device_attr_group);
1036 sysfs_remove_group(&dev->kobj, &qeth_device_ipato_group);
1037 sysfs_remove_group(&dev->kobj, &qeth_device_vipa_group);
1038 return ret;
1040 return 0;
1043 void qeth_l3_remove_device_attributes(struct device *dev)
1045 sysfs_remove_group(&dev->kobj, &qeth_l3_device_attr_group);
1046 sysfs_remove_group(&dev->kobj, &qeth_device_ipato_group);
1047 sysfs_remove_group(&dev->kobj, &qeth_device_vipa_group);
1048 sysfs_remove_group(&dev->kobj, &qeth_device_rxip_group);