Merge branch 'exynos-drm' of git://git.infradead.org/users/kmpark/linux-samsung into...
[pandora-kernel.git] / drivers / s390 / net / qeth_l3_sys.c
1 /*
2  *  drivers/s390/net/qeth_l3_sys.c
3  *
4  *    Copyright IBM Corp. 2007
5  *    Author(s): Utz Bacher <utz.bacher@de.ibm.com>,
6  *               Frank Pavlic <fpavlic@de.ibm.com>,
7  *               Thomas Spatzier <tspat@de.ibm.com>,
8  *               Frank Blaschka <frank.blaschka@de.ibm.com>
9  */
10
11 #include <linux/slab.h>
12 #include <asm/ebcdic.h>
13 #include "qeth_l3.h"
14
15 #define QETH_DEVICE_ATTR(_id, _name, _mode, _show, _store) \
16 struct device_attribute dev_attr_##_id = __ATTR(_name, _mode, _show, _store)
17
18 static ssize_t qeth_l3_dev_route_show(struct qeth_card *card,
19                         struct qeth_routing_info *route, char *buf)
20 {
21         switch (route->type) {
22         case PRIMARY_ROUTER:
23                 return sprintf(buf, "%s\n", "primary router");
24         case SECONDARY_ROUTER:
25                 return sprintf(buf, "%s\n", "secondary router");
26         case MULTICAST_ROUTER:
27                 if (card->info.broadcast_capable == QETH_BROADCAST_WITHOUT_ECHO)
28                         return sprintf(buf, "%s\n", "multicast router+");
29                 else
30                         return sprintf(buf, "%s\n", "multicast router");
31         case PRIMARY_CONNECTOR:
32                 if (card->info.broadcast_capable == QETH_BROADCAST_WITHOUT_ECHO)
33                         return sprintf(buf, "%s\n", "primary connector+");
34                 else
35                         return sprintf(buf, "%s\n", "primary connector");
36         case SECONDARY_CONNECTOR:
37                 if (card->info.broadcast_capable == QETH_BROADCAST_WITHOUT_ECHO)
38                         return sprintf(buf, "%s\n", "secondary connector+");
39                 else
40                         return sprintf(buf, "%s\n", "secondary connector");
41         default:
42                 return sprintf(buf, "%s\n", "no");
43         }
44 }
45
46 static ssize_t qeth_l3_dev_route4_show(struct device *dev,
47                         struct device_attribute *attr, char *buf)
48 {
49         struct qeth_card *card = dev_get_drvdata(dev);
50
51         if (!card)
52                 return -EINVAL;
53
54         return qeth_l3_dev_route_show(card, &card->options.route4, buf);
55 }
56
57 static ssize_t qeth_l3_dev_route_store(struct qeth_card *card,
58                 struct qeth_routing_info *route, enum qeth_prot_versions prot,
59                 const char *buf, size_t count)
60 {
61         enum qeth_routing_types old_route_type = route->type;
62         char *tmp;
63         int rc = 0;
64
65         tmp = strsep((char **) &buf, "\n");
66         mutex_lock(&card->conf_mutex);
67         if (!strcmp(tmp, "no_router")) {
68                 route->type = NO_ROUTER;
69         } else if (!strcmp(tmp, "primary_connector")) {
70                 route->type = PRIMARY_CONNECTOR;
71         } else if (!strcmp(tmp, "secondary_connector")) {
72                 route->type = SECONDARY_CONNECTOR;
73         } else if (!strcmp(tmp, "primary_router")) {
74                 route->type = PRIMARY_ROUTER;
75         } else if (!strcmp(tmp, "secondary_router")) {
76                 route->type = SECONDARY_ROUTER;
77         } else if (!strcmp(tmp, "multicast_router")) {
78                 route->type = MULTICAST_ROUTER;
79         } else {
80                 rc = -EINVAL;
81                 goto out;
82         }
83         if (((card->state == CARD_STATE_SOFTSETUP) ||
84              (card->state == CARD_STATE_UP)) &&
85             (old_route_type != route->type)) {
86                 if (prot == QETH_PROT_IPV4)
87                         rc = qeth_l3_setrouting_v4(card);
88                 else if (prot == QETH_PROT_IPV6)
89                         rc = qeth_l3_setrouting_v6(card);
90         }
91 out:
92         mutex_unlock(&card->conf_mutex);
93         return rc ? rc : count;
94 }
95
96 static ssize_t qeth_l3_dev_route4_store(struct device *dev,
97                 struct device_attribute *attr, const char *buf, size_t count)
98 {
99         struct qeth_card *card = dev_get_drvdata(dev);
100
101         if (!card)
102                 return -EINVAL;
103
104         return qeth_l3_dev_route_store(card, &card->options.route4,
105                                 QETH_PROT_IPV4, buf, count);
106 }
107
108 static DEVICE_ATTR(route4, 0644, qeth_l3_dev_route4_show,
109                         qeth_l3_dev_route4_store);
110
111 static ssize_t qeth_l3_dev_route6_show(struct device *dev,
112                         struct device_attribute *attr, char *buf)
113 {
114         struct qeth_card *card = dev_get_drvdata(dev);
115
116         if (!card)
117                 return -EINVAL;
118
119         return qeth_l3_dev_route_show(card, &card->options.route6, buf);
120 }
121
122 static ssize_t qeth_l3_dev_route6_store(struct device *dev,
123                 struct device_attribute *attr, const char *buf, size_t count)
124 {
125         struct qeth_card *card = dev_get_drvdata(dev);
126
127         if (!card)
128                 return -EINVAL;
129
130         return qeth_l3_dev_route_store(card, &card->options.route6,
131                                 QETH_PROT_IPV6, buf, count);
132 }
133
134 static DEVICE_ATTR(route6, 0644, qeth_l3_dev_route6_show,
135                         qeth_l3_dev_route6_store);
136
137 static ssize_t qeth_l3_dev_fake_broadcast_show(struct device *dev,
138                         struct device_attribute *attr, char *buf)
139 {
140         struct qeth_card *card = dev_get_drvdata(dev);
141
142         if (!card)
143                 return -EINVAL;
144
145         return sprintf(buf, "%i\n", card->options.fake_broadcast? 1:0);
146 }
147
148 static ssize_t qeth_l3_dev_fake_broadcast_store(struct device *dev,
149                 struct device_attribute *attr, const char *buf, size_t count)
150 {
151         struct qeth_card *card = dev_get_drvdata(dev);
152         char *tmp;
153         int i, rc = 0;
154
155         if (!card)
156                 return -EINVAL;
157
158         mutex_lock(&card->conf_mutex);
159         if ((card->state != CARD_STATE_DOWN) &&
160             (card->state != CARD_STATE_RECOVER)) {
161                 rc = -EPERM;
162                 goto out;
163         }
164
165         i = simple_strtoul(buf, &tmp, 16);
166         if ((i == 0) || (i == 1))
167                 card->options.fake_broadcast = i;
168         else
169                 rc = -EINVAL;
170 out:
171         mutex_unlock(&card->conf_mutex);
172         return rc ? rc : count;
173 }
174
175 static DEVICE_ATTR(fake_broadcast, 0644, qeth_l3_dev_fake_broadcast_show,
176                    qeth_l3_dev_fake_broadcast_store);
177
178 static ssize_t qeth_l3_dev_broadcast_mode_show(struct device *dev,
179                                 struct device_attribute *attr, char *buf)
180 {
181         struct qeth_card *card = dev_get_drvdata(dev);
182
183         if (!card)
184                 return -EINVAL;
185
186         if (!((card->info.link_type == QETH_LINK_TYPE_HSTR) ||
187               (card->info.link_type == QETH_LINK_TYPE_LANE_TR)))
188                 return sprintf(buf, "n/a\n");
189
190         return sprintf(buf, "%s\n", (card->options.broadcast_mode ==
191                                      QETH_TR_BROADCAST_ALLRINGS)?
192                        "all rings":"local");
193 }
194
195 static ssize_t qeth_l3_dev_broadcast_mode_store(struct device *dev,
196                 struct device_attribute *attr, const char *buf, size_t count)
197 {
198         struct qeth_card *card = dev_get_drvdata(dev);
199         char *tmp;
200         int rc = 0;
201
202         if (!card)
203                 return -EINVAL;
204
205         mutex_lock(&card->conf_mutex);
206         if ((card->state != CARD_STATE_DOWN) &&
207             (card->state != CARD_STATE_RECOVER)) {
208                 rc = -EPERM;
209                 goto out;
210         }
211
212         if (!((card->info.link_type == QETH_LINK_TYPE_HSTR) ||
213               (card->info.link_type == QETH_LINK_TYPE_LANE_TR))) {
214                 rc = -EINVAL;
215                 goto out;
216         }
217
218         tmp = strsep((char **) &buf, "\n");
219
220         if (!strcmp(tmp, "local"))
221                 card->options.broadcast_mode = QETH_TR_BROADCAST_LOCAL;
222         else if (!strcmp(tmp, "all_rings"))
223                 card->options.broadcast_mode = QETH_TR_BROADCAST_ALLRINGS;
224         else
225                 rc = -EINVAL;
226 out:
227         mutex_unlock(&card->conf_mutex);
228         return rc ? rc : count;
229 }
230
231 static DEVICE_ATTR(broadcast_mode, 0644, qeth_l3_dev_broadcast_mode_show,
232                    qeth_l3_dev_broadcast_mode_store);
233
234 static ssize_t qeth_l3_dev_canonical_macaddr_show(struct device *dev,
235                                 struct device_attribute *attr, char *buf)
236 {
237         struct qeth_card *card = dev_get_drvdata(dev);
238
239         if (!card)
240                 return -EINVAL;
241
242         if (!((card->info.link_type == QETH_LINK_TYPE_HSTR) ||
243               (card->info.link_type == QETH_LINK_TYPE_LANE_TR)))
244                 return sprintf(buf, "n/a\n");
245
246         return sprintf(buf, "%i\n", (card->options.macaddr_mode ==
247                                      QETH_TR_MACADDR_CANONICAL)? 1:0);
248 }
249
250 static ssize_t qeth_l3_dev_canonical_macaddr_store(struct device *dev,
251                 struct device_attribute *attr, const char *buf, size_t count)
252 {
253         struct qeth_card *card = dev_get_drvdata(dev);
254         char *tmp;
255         int i, rc = 0;
256
257         if (!card)
258                 return -EINVAL;
259
260         mutex_lock(&card->conf_mutex);
261         if ((card->state != CARD_STATE_DOWN) &&
262             (card->state != CARD_STATE_RECOVER)) {
263                 rc = -EPERM;
264                 goto out;
265         }
266
267         if (!((card->info.link_type == QETH_LINK_TYPE_HSTR) ||
268               (card->info.link_type == QETH_LINK_TYPE_LANE_TR))) {
269                 rc = -EINVAL;
270                 goto out;
271         }
272
273         i = simple_strtoul(buf, &tmp, 16);
274         if ((i == 0) || (i == 1))
275                 card->options.macaddr_mode = i?
276                         QETH_TR_MACADDR_CANONICAL :
277                         QETH_TR_MACADDR_NONCANONICAL;
278         else
279                 rc = -EINVAL;
280 out:
281         mutex_unlock(&card->conf_mutex);
282         return rc ? rc : count;
283 }
284
285 static DEVICE_ATTR(canonical_macaddr, 0644, qeth_l3_dev_canonical_macaddr_show,
286                    qeth_l3_dev_canonical_macaddr_store);
287
288 static ssize_t qeth_l3_dev_sniffer_show(struct device *dev,
289                 struct device_attribute *attr, char *buf)
290 {
291         struct qeth_card *card = dev_get_drvdata(dev);
292
293         if (!card)
294                 return -EINVAL;
295
296         return sprintf(buf, "%i\n", card->options.sniffer ? 1 : 0);
297 }
298
299 static ssize_t qeth_l3_dev_sniffer_store(struct device *dev,
300                 struct device_attribute *attr, const char *buf, size_t count)
301 {
302         struct qeth_card *card = dev_get_drvdata(dev);
303         int rc = 0;
304         unsigned long i;
305
306         if (!card)
307                 return -EINVAL;
308
309         if (card->info.type != QETH_CARD_TYPE_IQD)
310                 return -EPERM;
311         if (card->options.cq == QETH_CQ_ENABLED)
312                 return -EPERM;
313
314         mutex_lock(&card->conf_mutex);
315         if ((card->state != CARD_STATE_DOWN) &&
316             (card->state != CARD_STATE_RECOVER)) {
317                 rc = -EPERM;
318                 goto out;
319         }
320
321         rc = strict_strtoul(buf, 16, &i);
322         if (rc) {
323                 rc = -EINVAL;
324                 goto out;
325         }
326         switch (i) {
327         case 0:
328                 card->options.sniffer = i;
329                 break;
330         case 1:
331                 qdio_get_ssqd_desc(CARD_DDEV(card), &card->ssqd);
332                 if (card->ssqd.qdioac2 & QETH_SNIFF_AVAIL) {
333                         card->options.sniffer = i;
334                         if (card->qdio.init_pool.buf_count !=
335                                         QETH_IN_BUF_COUNT_MAX)
336                                 qeth_realloc_buffer_pool(card,
337                                         QETH_IN_BUF_COUNT_MAX);
338                 } else
339                         rc = -EPERM;
340                 break;
341         default:
342                 rc = -EINVAL;
343         }
344 out:
345         mutex_unlock(&card->conf_mutex);
346         return rc ? rc : count;
347 }
348
349 static DEVICE_ATTR(sniffer, 0644, qeth_l3_dev_sniffer_show,
350                 qeth_l3_dev_sniffer_store);
351
352
353 static ssize_t qeth_l3_dev_hsuid_show(struct device *dev,
354                 struct device_attribute *attr, char *buf)
355 {
356         struct qeth_card *card = dev_get_drvdata(dev);
357         char tmp_hsuid[9];
358
359         if (!card)
360                 return -EINVAL;
361
362         if (card->info.type != QETH_CARD_TYPE_IQD)
363                 return -EPERM;
364
365         if (card->state == CARD_STATE_DOWN)
366                 return -EPERM;
367
368         memcpy(tmp_hsuid, card->options.hsuid, sizeof(tmp_hsuid));
369         EBCASC(tmp_hsuid, 8);
370         return sprintf(buf, "%s\n", tmp_hsuid);
371 }
372
373 static ssize_t qeth_l3_dev_hsuid_store(struct device *dev,
374                 struct device_attribute *attr, const char *buf, size_t count)
375 {
376         struct qeth_card *card = dev_get_drvdata(dev);
377         struct qeth_ipaddr *addr;
378         char *tmp;
379         int i;
380
381         if (!card)
382                 return -EINVAL;
383
384         if (card->info.type != QETH_CARD_TYPE_IQD)
385                 return -EPERM;
386         if (card->state != CARD_STATE_DOWN &&
387             card->state != CARD_STATE_RECOVER)
388                 return -EPERM;
389         if (card->options.sniffer)
390                 return -EPERM;
391         if (card->options.cq == QETH_CQ_NOTAVAILABLE)
392                 return -EPERM;
393
394         tmp = strsep((char **)&buf, "\n");
395         if (strlen(tmp) > 8)
396                 return -EINVAL;
397
398         if (card->options.hsuid[0]) {
399                 /* delete old ip address */
400                 addr = qeth_l3_get_addr_buffer(QETH_PROT_IPV6);
401                 if (addr != NULL) {
402                         addr->u.a6.addr.s6_addr32[0] = 0xfe800000;
403                         addr->u.a6.addr.s6_addr32[1] = 0x00000000;
404                         for (i = 8; i < 16; i++)
405                                 addr->u.a6.addr.s6_addr[i] =
406                                         card->options.hsuid[i - 8];
407                         addr->u.a6.pfxlen = 0;
408                         addr->type = QETH_IP_TYPE_NORMAL;
409                 } else
410                         return -ENOMEM;
411                 if (!qeth_l3_delete_ip(card, addr))
412                         kfree(addr);
413                 qeth_l3_set_ip_addr_list(card);
414         }
415
416         if (strlen(tmp) == 0) {
417                 /* delete ip address only */
418                 card->options.hsuid[0] = '\0';
419                 if (card->dev)
420                         memcpy(card->dev->perm_addr, card->options.hsuid, 9);
421                 qeth_configure_cq(card, QETH_CQ_DISABLED);
422                 return count;
423         }
424
425         if (qeth_configure_cq(card, QETH_CQ_ENABLED))
426                 return -EPERM;
427
428         for (i = 0; i < 8; i++)
429                 card->options.hsuid[i] = ' ';
430         card->options.hsuid[8] = '\0';
431         strncpy(card->options.hsuid, tmp, strlen(tmp));
432         ASCEBC(card->options.hsuid, 8);
433         if (card->dev)
434                 memcpy(card->dev->perm_addr, card->options.hsuid, 9);
435
436         addr = qeth_l3_get_addr_buffer(QETH_PROT_IPV6);
437         if (addr != NULL) {
438                 addr->u.a6.addr.s6_addr32[0] = 0xfe800000;
439                 addr->u.a6.addr.s6_addr32[1] = 0x00000000;
440                 for (i = 8; i < 16; i++)
441                         addr->u.a6.addr.s6_addr[i] = card->options.hsuid[i - 8];
442                 addr->u.a6.pfxlen = 0;
443                 addr->type = QETH_IP_TYPE_NORMAL;
444         } else
445                 return -ENOMEM;
446         if (!qeth_l3_add_ip(card, addr))
447                 kfree(addr);
448         qeth_l3_set_ip_addr_list(card);
449
450         return count;
451 }
452
453 static DEVICE_ATTR(hsuid, 0644, qeth_l3_dev_hsuid_show,
454                    qeth_l3_dev_hsuid_store);
455
456
457 static struct attribute *qeth_l3_device_attrs[] = {
458         &dev_attr_route4.attr,
459         &dev_attr_route6.attr,
460         &dev_attr_fake_broadcast.attr,
461         &dev_attr_broadcast_mode.attr,
462         &dev_attr_canonical_macaddr.attr,
463         &dev_attr_sniffer.attr,
464         &dev_attr_hsuid.attr,
465         NULL,
466 };
467
468 static struct attribute_group qeth_l3_device_attr_group = {
469         .attrs = qeth_l3_device_attrs,
470 };
471
472 static ssize_t qeth_l3_dev_ipato_enable_show(struct device *dev,
473                         struct device_attribute *attr, char *buf)
474 {
475         struct qeth_card *card = dev_get_drvdata(dev);
476
477         if (!card)
478                 return -EINVAL;
479
480         return sprintf(buf, "%i\n", card->ipato.enabled? 1:0);
481 }
482
483 static ssize_t qeth_l3_dev_ipato_enable_store(struct device *dev,
484                 struct device_attribute *attr, const char *buf, size_t count)
485 {
486         struct qeth_card *card = dev_get_drvdata(dev);
487         struct qeth_ipaddr *tmpipa, *t;
488         char *tmp;
489         int rc = 0;
490
491         if (!card)
492                 return -EINVAL;
493
494         mutex_lock(&card->conf_mutex);
495         if ((card->state != CARD_STATE_DOWN) &&
496             (card->state != CARD_STATE_RECOVER)) {
497                 rc = -EPERM;
498                 goto out;
499         }
500
501         tmp = strsep((char **) &buf, "\n");
502         if (!strcmp(tmp, "toggle")) {
503                 card->ipato.enabled = (card->ipato.enabled)? 0 : 1;
504         } else if (!strcmp(tmp, "1")) {
505                 card->ipato.enabled = 1;
506                 list_for_each_entry_safe(tmpipa, t, card->ip_tbd_list, entry) {
507                         if ((tmpipa->type == QETH_IP_TYPE_NORMAL) &&
508                                 qeth_l3_is_addr_covered_by_ipato(card, tmpipa))
509                                 tmpipa->set_flags |=
510                                         QETH_IPA_SETIP_TAKEOVER_FLAG;
511                 }
512
513         } else if (!strcmp(tmp, "0")) {
514                 card->ipato.enabled = 0;
515                 list_for_each_entry_safe(tmpipa, t, card->ip_tbd_list, entry) {
516                         if (tmpipa->set_flags &
517                                 QETH_IPA_SETIP_TAKEOVER_FLAG)
518                                 tmpipa->set_flags &=
519                                         ~QETH_IPA_SETIP_TAKEOVER_FLAG;
520                 }
521         } else
522                 rc = -EINVAL;
523 out:
524         mutex_unlock(&card->conf_mutex);
525         return rc ? rc : count;
526 }
527
528 static QETH_DEVICE_ATTR(ipato_enable, enable, 0644,
529                         qeth_l3_dev_ipato_enable_show,
530                         qeth_l3_dev_ipato_enable_store);
531
532 static ssize_t qeth_l3_dev_ipato_invert4_show(struct device *dev,
533                                 struct device_attribute *attr, char *buf)
534 {
535         struct qeth_card *card = dev_get_drvdata(dev);
536
537         if (!card)
538                 return -EINVAL;
539
540         return sprintf(buf, "%i\n", card->ipato.invert4? 1:0);
541 }
542
543 static ssize_t qeth_l3_dev_ipato_invert4_store(struct device *dev,
544                                 struct device_attribute *attr,
545                                 const char *buf, size_t count)
546 {
547         struct qeth_card *card = dev_get_drvdata(dev);
548         char *tmp;
549         int rc = 0;
550
551         if (!card)
552                 return -EINVAL;
553
554         mutex_lock(&card->conf_mutex);
555         tmp = strsep((char **) &buf, "\n");
556         if (!strcmp(tmp, "toggle")) {
557                 card->ipato.invert4 = (card->ipato.invert4)? 0 : 1;
558         } else if (!strcmp(tmp, "1")) {
559                 card->ipato.invert4 = 1;
560         } else if (!strcmp(tmp, "0")) {
561                 card->ipato.invert4 = 0;
562         } else
563                 rc = -EINVAL;
564         mutex_unlock(&card->conf_mutex);
565         return rc ? rc : count;
566 }
567
568 static QETH_DEVICE_ATTR(ipato_invert4, invert4, 0644,
569                         qeth_l3_dev_ipato_invert4_show,
570                         qeth_l3_dev_ipato_invert4_store);
571
572 static ssize_t qeth_l3_dev_ipato_add_show(char *buf, struct qeth_card *card,
573                         enum qeth_prot_versions proto)
574 {
575         struct qeth_ipato_entry *ipatoe;
576         unsigned long flags;
577         char addr_str[40];
578         int entry_len; /* length of 1 entry string, differs between v4 and v6 */
579         int i = 0;
580
581         entry_len = (proto == QETH_PROT_IPV4)? 12 : 40;
582         /* add strlen for "/<mask>\n" */
583         entry_len += (proto == QETH_PROT_IPV4)? 5 : 6;
584         spin_lock_irqsave(&card->ip_lock, flags);
585         list_for_each_entry(ipatoe, &card->ipato.entries, entry) {
586                 if (ipatoe->proto != proto)
587                         continue;
588                 /* String must not be longer than PAGE_SIZE. So we check if
589                  * string length gets near PAGE_SIZE. Then we can savely display
590                  * the next IPv6 address (worst case, compared to IPv4) */
591                 if ((PAGE_SIZE - i) <= entry_len)
592                         break;
593                 qeth_l3_ipaddr_to_string(proto, ipatoe->addr, addr_str);
594                 i += snprintf(buf + i, PAGE_SIZE - i,
595                               "%s/%i\n", addr_str, ipatoe->mask_bits);
596         }
597         spin_unlock_irqrestore(&card->ip_lock, flags);
598         i += snprintf(buf + i, PAGE_SIZE - i, "\n");
599
600         return i;
601 }
602
603 static ssize_t qeth_l3_dev_ipato_add4_show(struct device *dev,
604                                 struct device_attribute *attr, char *buf)
605 {
606         struct qeth_card *card = dev_get_drvdata(dev);
607
608         if (!card)
609                 return -EINVAL;
610
611         return qeth_l3_dev_ipato_add_show(buf, card, QETH_PROT_IPV4);
612 }
613
614 static int qeth_l3_parse_ipatoe(const char *buf, enum qeth_prot_versions proto,
615                   u8 *addr, int *mask_bits)
616 {
617         const char *start, *end;
618         char *tmp;
619         char buffer[40] = {0, };
620
621         start = buf;
622         /* get address string */
623         end = strchr(start, '/');
624         if (!end || (end - start >= 40)) {
625                 return -EINVAL;
626         }
627         strncpy(buffer, start, end - start);
628         if (qeth_l3_string_to_ipaddr(buffer, proto, addr)) {
629                 return -EINVAL;
630         }
631         start = end + 1;
632         *mask_bits = simple_strtoul(start, &tmp, 10);
633         if (!strlen(start) ||
634             (tmp == start) ||
635             (*mask_bits > ((proto == QETH_PROT_IPV4) ? 32 : 128))) {
636                 return -EINVAL;
637         }
638         return 0;
639 }
640
641 static ssize_t qeth_l3_dev_ipato_add_store(const char *buf, size_t count,
642                          struct qeth_card *card, enum qeth_prot_versions proto)
643 {
644         struct qeth_ipato_entry *ipatoe;
645         u8 addr[16];
646         int mask_bits;
647         int rc = 0;
648
649         mutex_lock(&card->conf_mutex);
650         rc = qeth_l3_parse_ipatoe(buf, proto, addr, &mask_bits);
651         if (rc)
652                 goto out;
653
654         ipatoe = kzalloc(sizeof(struct qeth_ipato_entry), GFP_KERNEL);
655         if (!ipatoe) {
656                 rc = -ENOMEM;
657                 goto out;
658         }
659         ipatoe->proto = proto;
660         memcpy(ipatoe->addr, addr, (proto == QETH_PROT_IPV4)? 4:16);
661         ipatoe->mask_bits = mask_bits;
662
663         rc = qeth_l3_add_ipato_entry(card, ipatoe);
664         if (rc)
665                 kfree(ipatoe);
666 out:
667         mutex_unlock(&card->conf_mutex);
668         return rc ? rc : count;
669 }
670
671 static ssize_t qeth_l3_dev_ipato_add4_store(struct device *dev,
672                 struct device_attribute *attr, const char *buf, size_t count)
673 {
674         struct qeth_card *card = dev_get_drvdata(dev);
675
676         if (!card)
677                 return -EINVAL;
678
679         return qeth_l3_dev_ipato_add_store(buf, count, card, QETH_PROT_IPV4);
680 }
681
682 static QETH_DEVICE_ATTR(ipato_add4, add4, 0644,
683                         qeth_l3_dev_ipato_add4_show,
684                         qeth_l3_dev_ipato_add4_store);
685
686 static ssize_t qeth_l3_dev_ipato_del_store(const char *buf, size_t count,
687                          struct qeth_card *card, enum qeth_prot_versions proto)
688 {
689         u8 addr[16];
690         int mask_bits;
691         int rc = 0;
692
693         mutex_lock(&card->conf_mutex);
694         rc = qeth_l3_parse_ipatoe(buf, proto, addr, &mask_bits);
695         if (!rc)
696                 qeth_l3_del_ipato_entry(card, proto, addr, mask_bits);
697         mutex_unlock(&card->conf_mutex);
698         return rc ? rc : count;
699 }
700
701 static ssize_t qeth_l3_dev_ipato_del4_store(struct device *dev,
702                 struct device_attribute *attr, const char *buf, size_t count)
703 {
704         struct qeth_card *card = dev_get_drvdata(dev);
705
706         if (!card)
707                 return -EINVAL;
708
709         return qeth_l3_dev_ipato_del_store(buf, count, card, QETH_PROT_IPV4);
710 }
711
712 static QETH_DEVICE_ATTR(ipato_del4, del4, 0200, NULL,
713                         qeth_l3_dev_ipato_del4_store);
714
715 static ssize_t qeth_l3_dev_ipato_invert6_show(struct device *dev,
716                 struct device_attribute *attr, char *buf)
717 {
718         struct qeth_card *card = dev_get_drvdata(dev);
719
720         if (!card)
721                 return -EINVAL;
722
723         return sprintf(buf, "%i\n", card->ipato.invert6? 1:0);
724 }
725
726 static ssize_t qeth_l3_dev_ipato_invert6_store(struct device *dev,
727                 struct device_attribute *attr, const char *buf, size_t count)
728 {
729         struct qeth_card *card = dev_get_drvdata(dev);
730         char *tmp;
731         int rc = 0;
732
733         if (!card)
734                 return -EINVAL;
735
736         mutex_lock(&card->conf_mutex);
737         tmp = strsep((char **) &buf, "\n");
738         if (!strcmp(tmp, "toggle")) {
739                 card->ipato.invert6 = (card->ipato.invert6)? 0 : 1;
740         } else if (!strcmp(tmp, "1")) {
741                 card->ipato.invert6 = 1;
742         } else if (!strcmp(tmp, "0")) {
743                 card->ipato.invert6 = 0;
744         } else
745                 rc = -EINVAL;
746         mutex_unlock(&card->conf_mutex);
747         return rc ? rc : count;
748 }
749
750 static QETH_DEVICE_ATTR(ipato_invert6, invert6, 0644,
751                         qeth_l3_dev_ipato_invert6_show,
752                         qeth_l3_dev_ipato_invert6_store);
753
754
755 static ssize_t qeth_l3_dev_ipato_add6_show(struct device *dev,
756                                 struct device_attribute *attr, char *buf)
757 {
758         struct qeth_card *card = dev_get_drvdata(dev);
759
760         if (!card)
761                 return -EINVAL;
762
763         return qeth_l3_dev_ipato_add_show(buf, card, QETH_PROT_IPV6);
764 }
765
766 static ssize_t qeth_l3_dev_ipato_add6_store(struct device *dev,
767                 struct device_attribute *attr, const char *buf, size_t count)
768 {
769         struct qeth_card *card = dev_get_drvdata(dev);
770
771         if (!card)
772                 return -EINVAL;
773
774         return qeth_l3_dev_ipato_add_store(buf, count, card, QETH_PROT_IPV6);
775 }
776
777 static QETH_DEVICE_ATTR(ipato_add6, add6, 0644,
778                         qeth_l3_dev_ipato_add6_show,
779                         qeth_l3_dev_ipato_add6_store);
780
781 static ssize_t qeth_l3_dev_ipato_del6_store(struct device *dev,
782                 struct device_attribute *attr, const char *buf, size_t count)
783 {
784         struct qeth_card *card = dev_get_drvdata(dev);
785
786         if (!card)
787                 return -EINVAL;
788
789         return qeth_l3_dev_ipato_del_store(buf, count, card, QETH_PROT_IPV6);
790 }
791
792 static QETH_DEVICE_ATTR(ipato_del6, del6, 0200, NULL,
793                         qeth_l3_dev_ipato_del6_store);
794
795 static struct attribute *qeth_ipato_device_attrs[] = {
796         &dev_attr_ipato_enable.attr,
797         &dev_attr_ipato_invert4.attr,
798         &dev_attr_ipato_add4.attr,
799         &dev_attr_ipato_del4.attr,
800         &dev_attr_ipato_invert6.attr,
801         &dev_attr_ipato_add6.attr,
802         &dev_attr_ipato_del6.attr,
803         NULL,
804 };
805
806 static struct attribute_group qeth_device_ipato_group = {
807         .name = "ipa_takeover",
808         .attrs = qeth_ipato_device_attrs,
809 };
810
811 static ssize_t qeth_l3_dev_vipa_add_show(char *buf, struct qeth_card *card,
812                         enum qeth_prot_versions proto)
813 {
814         struct qeth_ipaddr *ipaddr;
815         char addr_str[40];
816         int entry_len; /* length of 1 entry string, differs between v4 and v6 */
817         unsigned long flags;
818         int i = 0;
819
820         entry_len = (proto == QETH_PROT_IPV4)? 12 : 40;
821         entry_len += 2; /* \n + terminator */
822         spin_lock_irqsave(&card->ip_lock, flags);
823         list_for_each_entry(ipaddr, &card->ip_list, entry) {
824                 if (ipaddr->proto != proto)
825                         continue;
826                 if (ipaddr->type != QETH_IP_TYPE_VIPA)
827                         continue;
828                 /* String must not be longer than PAGE_SIZE. So we check if
829                  * string length gets near PAGE_SIZE. Then we can savely display
830                  * the next IPv6 address (worst case, compared to IPv4) */
831                 if ((PAGE_SIZE - i) <= entry_len)
832                         break;
833                 qeth_l3_ipaddr_to_string(proto, (const u8 *)&ipaddr->u,
834                         addr_str);
835                 i += snprintf(buf + i, PAGE_SIZE - i, "%s\n", addr_str);
836         }
837         spin_unlock_irqrestore(&card->ip_lock, flags);
838         i += snprintf(buf + i, PAGE_SIZE - i, "\n");
839
840         return i;
841 }
842
843 static ssize_t qeth_l3_dev_vipa_add4_show(struct device *dev,
844                         struct device_attribute *attr, char *buf)
845 {
846         struct qeth_card *card = dev_get_drvdata(dev);
847
848         if (!card)
849                 return -EINVAL;
850
851         return qeth_l3_dev_vipa_add_show(buf, card, QETH_PROT_IPV4);
852 }
853
854 static int qeth_l3_parse_vipae(const char *buf, enum qeth_prot_versions proto,
855                  u8 *addr)
856 {
857         if (qeth_l3_string_to_ipaddr(buf, proto, addr)) {
858                 return -EINVAL;
859         }
860         return 0;
861 }
862
863 static ssize_t qeth_l3_dev_vipa_add_store(const char *buf, size_t count,
864                         struct qeth_card *card, enum qeth_prot_versions proto)
865 {
866         u8 addr[16] = {0, };
867         int rc;
868
869         mutex_lock(&card->conf_mutex);
870         rc = qeth_l3_parse_vipae(buf, proto, addr);
871         if (!rc)
872                 rc = qeth_l3_add_vipa(card, proto, addr);
873         mutex_unlock(&card->conf_mutex);
874         return rc ? rc : count;
875 }
876
877 static ssize_t qeth_l3_dev_vipa_add4_store(struct device *dev,
878                 struct device_attribute *attr, const char *buf, size_t count)
879 {
880         struct qeth_card *card = dev_get_drvdata(dev);
881
882         if (!card)
883                 return -EINVAL;
884
885         return qeth_l3_dev_vipa_add_store(buf, count, card, QETH_PROT_IPV4);
886 }
887
888 static QETH_DEVICE_ATTR(vipa_add4, add4, 0644,
889                         qeth_l3_dev_vipa_add4_show,
890                         qeth_l3_dev_vipa_add4_store);
891
892 static ssize_t qeth_l3_dev_vipa_del_store(const char *buf, size_t count,
893                          struct qeth_card *card, enum qeth_prot_versions proto)
894 {
895         u8 addr[16];
896         int rc;
897
898         mutex_lock(&card->conf_mutex);
899         rc = qeth_l3_parse_vipae(buf, proto, addr);
900         if (!rc)
901                 qeth_l3_del_vipa(card, proto, addr);
902         mutex_unlock(&card->conf_mutex);
903         return rc ? rc : count;
904 }
905
906 static ssize_t qeth_l3_dev_vipa_del4_store(struct device *dev,
907                 struct device_attribute *attr, const char *buf, size_t count)
908 {
909         struct qeth_card *card = dev_get_drvdata(dev);
910
911         if (!card)
912                 return -EINVAL;
913
914         return qeth_l3_dev_vipa_del_store(buf, count, card, QETH_PROT_IPV4);
915 }
916
917 static QETH_DEVICE_ATTR(vipa_del4, del4, 0200, NULL,
918                         qeth_l3_dev_vipa_del4_store);
919
920 static ssize_t qeth_l3_dev_vipa_add6_show(struct device *dev,
921                                 struct device_attribute *attr, char *buf)
922 {
923         struct qeth_card *card = dev_get_drvdata(dev);
924
925         if (!card)
926                 return -EINVAL;
927
928         return qeth_l3_dev_vipa_add_show(buf, card, QETH_PROT_IPV6);
929 }
930
931 static ssize_t qeth_l3_dev_vipa_add6_store(struct device *dev,
932                 struct device_attribute *attr, const char *buf, size_t count)
933 {
934         struct qeth_card *card = dev_get_drvdata(dev);
935
936         if (!card)
937                 return -EINVAL;
938
939         return qeth_l3_dev_vipa_add_store(buf, count, card, QETH_PROT_IPV6);
940 }
941
942 static QETH_DEVICE_ATTR(vipa_add6, add6, 0644,
943                         qeth_l3_dev_vipa_add6_show,
944                         qeth_l3_dev_vipa_add6_store);
945
946 static ssize_t qeth_l3_dev_vipa_del6_store(struct device *dev,
947                 struct device_attribute *attr, const char *buf, size_t count)
948 {
949         struct qeth_card *card = dev_get_drvdata(dev);
950
951         if (!card)
952                 return -EINVAL;
953
954         return qeth_l3_dev_vipa_del_store(buf, count, card, QETH_PROT_IPV6);
955 }
956
957 static QETH_DEVICE_ATTR(vipa_del6, del6, 0200, NULL,
958                         qeth_l3_dev_vipa_del6_store);
959
960 static struct attribute *qeth_vipa_device_attrs[] = {
961         &dev_attr_vipa_add4.attr,
962         &dev_attr_vipa_del4.attr,
963         &dev_attr_vipa_add6.attr,
964         &dev_attr_vipa_del6.attr,
965         NULL,
966 };
967
968 static struct attribute_group qeth_device_vipa_group = {
969         .name = "vipa",
970         .attrs = qeth_vipa_device_attrs,
971 };
972
973 static ssize_t qeth_l3_dev_rxip_add_show(char *buf, struct qeth_card *card,
974                        enum qeth_prot_versions proto)
975 {
976         struct qeth_ipaddr *ipaddr;
977         char addr_str[40];
978         int entry_len; /* length of 1 entry string, differs between v4 and v6 */
979         unsigned long flags;
980         int i = 0;
981
982         entry_len = (proto == QETH_PROT_IPV4)? 12 : 40;
983         entry_len += 2; /* \n + terminator */
984         spin_lock_irqsave(&card->ip_lock, flags);
985         list_for_each_entry(ipaddr, &card->ip_list, entry) {
986                 if (ipaddr->proto != proto)
987                         continue;
988                 if (ipaddr->type != QETH_IP_TYPE_RXIP)
989                         continue;
990                 /* String must not be longer than PAGE_SIZE. So we check if
991                  * string length gets near PAGE_SIZE. Then we can savely display
992                  * the next IPv6 address (worst case, compared to IPv4) */
993                 if ((PAGE_SIZE - i) <= entry_len)
994                         break;
995                 qeth_l3_ipaddr_to_string(proto, (const u8 *)&ipaddr->u,
996                         addr_str);
997                 i += snprintf(buf + i, PAGE_SIZE - i, "%s\n", addr_str);
998         }
999         spin_unlock_irqrestore(&card->ip_lock, flags);
1000         i += snprintf(buf + i, PAGE_SIZE - i, "\n");
1001
1002         return i;
1003 }
1004
1005 static ssize_t qeth_l3_dev_rxip_add4_show(struct device *dev,
1006                         struct device_attribute *attr, char *buf)
1007 {
1008         struct qeth_card *card = dev_get_drvdata(dev);
1009
1010         if (!card)
1011                 return -EINVAL;
1012
1013         return qeth_l3_dev_rxip_add_show(buf, card, QETH_PROT_IPV4);
1014 }
1015
1016 static int qeth_l3_parse_rxipe(const char *buf, enum qeth_prot_versions proto,
1017                  u8 *addr)
1018 {
1019         if (qeth_l3_string_to_ipaddr(buf, proto, addr)) {
1020                 return -EINVAL;
1021         }
1022         return 0;
1023 }
1024
1025 static ssize_t qeth_l3_dev_rxip_add_store(const char *buf, size_t count,
1026                         struct qeth_card *card, enum qeth_prot_versions proto)
1027 {
1028         u8 addr[16] = {0, };
1029         int rc;
1030
1031         mutex_lock(&card->conf_mutex);
1032         rc = qeth_l3_parse_rxipe(buf, proto, addr);
1033         if (!rc)
1034                 rc = qeth_l3_add_rxip(card, proto, addr);
1035         mutex_unlock(&card->conf_mutex);
1036         return rc ? rc : count;
1037 }
1038
1039 static ssize_t qeth_l3_dev_rxip_add4_store(struct device *dev,
1040                 struct device_attribute *attr, const char *buf, size_t count)
1041 {
1042         struct qeth_card *card = dev_get_drvdata(dev);
1043
1044         if (!card)
1045                 return -EINVAL;
1046
1047         return qeth_l3_dev_rxip_add_store(buf, count, card, QETH_PROT_IPV4);
1048 }
1049
1050 static QETH_DEVICE_ATTR(rxip_add4, add4, 0644,
1051                         qeth_l3_dev_rxip_add4_show,
1052                         qeth_l3_dev_rxip_add4_store);
1053
1054 static ssize_t qeth_l3_dev_rxip_del_store(const char *buf, size_t count,
1055                         struct qeth_card *card, enum qeth_prot_versions proto)
1056 {
1057         u8 addr[16];
1058         int rc;
1059
1060         mutex_lock(&card->conf_mutex);
1061         rc = qeth_l3_parse_rxipe(buf, proto, addr);
1062         if (!rc)
1063                 qeth_l3_del_rxip(card, proto, addr);
1064         mutex_unlock(&card->conf_mutex);
1065         return rc ? rc : count;
1066 }
1067
1068 static ssize_t qeth_l3_dev_rxip_del4_store(struct device *dev,
1069                 struct device_attribute *attr, const char *buf, size_t count)
1070 {
1071         struct qeth_card *card = dev_get_drvdata(dev);
1072
1073         if (!card)
1074                 return -EINVAL;
1075
1076         return qeth_l3_dev_rxip_del_store(buf, count, card, QETH_PROT_IPV4);
1077 }
1078
1079 static QETH_DEVICE_ATTR(rxip_del4, del4, 0200, NULL,
1080                         qeth_l3_dev_rxip_del4_store);
1081
1082 static ssize_t qeth_l3_dev_rxip_add6_show(struct device *dev,
1083                 struct device_attribute *attr, char *buf)
1084 {
1085         struct qeth_card *card = dev_get_drvdata(dev);
1086
1087         if (!card)
1088                 return -EINVAL;
1089
1090         return qeth_l3_dev_rxip_add_show(buf, card, QETH_PROT_IPV6);
1091 }
1092
1093 static ssize_t qeth_l3_dev_rxip_add6_store(struct device *dev,
1094                 struct device_attribute *attr, const char *buf, size_t count)
1095 {
1096         struct qeth_card *card = dev_get_drvdata(dev);
1097
1098         if (!card)
1099                 return -EINVAL;
1100
1101         return qeth_l3_dev_rxip_add_store(buf, count, card, QETH_PROT_IPV6);
1102 }
1103
1104 static QETH_DEVICE_ATTR(rxip_add6, add6, 0644,
1105                         qeth_l3_dev_rxip_add6_show,
1106                         qeth_l3_dev_rxip_add6_store);
1107
1108 static ssize_t qeth_l3_dev_rxip_del6_store(struct device *dev,
1109                 struct device_attribute *attr, const char *buf, size_t count)
1110 {
1111         struct qeth_card *card = dev_get_drvdata(dev);
1112
1113         if (!card)
1114                 return -EINVAL;
1115
1116         return qeth_l3_dev_rxip_del_store(buf, count, card, QETH_PROT_IPV6);
1117 }
1118
1119 static QETH_DEVICE_ATTR(rxip_del6, del6, 0200, NULL,
1120                         qeth_l3_dev_rxip_del6_store);
1121
1122 static struct attribute *qeth_rxip_device_attrs[] = {
1123         &dev_attr_rxip_add4.attr,
1124         &dev_attr_rxip_del4.attr,
1125         &dev_attr_rxip_add6.attr,
1126         &dev_attr_rxip_del6.attr,
1127         NULL,
1128 };
1129
1130 static struct attribute_group qeth_device_rxip_group = {
1131         .name = "rxip",
1132         .attrs = qeth_rxip_device_attrs,
1133 };
1134
1135 int qeth_l3_create_device_attributes(struct device *dev)
1136 {
1137         int ret;
1138
1139         ret = sysfs_create_group(&dev->kobj, &qeth_l3_device_attr_group);
1140         if (ret)
1141                 return ret;
1142
1143         ret = sysfs_create_group(&dev->kobj, &qeth_device_ipato_group);
1144         if (ret) {
1145                 sysfs_remove_group(&dev->kobj, &qeth_l3_device_attr_group);
1146                 return ret;
1147         }
1148
1149         ret = sysfs_create_group(&dev->kobj, &qeth_device_vipa_group);
1150         if (ret) {
1151                 sysfs_remove_group(&dev->kobj, &qeth_l3_device_attr_group);
1152                 sysfs_remove_group(&dev->kobj, &qeth_device_ipato_group);
1153                 return ret;
1154         }
1155
1156         ret = sysfs_create_group(&dev->kobj, &qeth_device_rxip_group);
1157         if (ret) {
1158                 sysfs_remove_group(&dev->kobj, &qeth_l3_device_attr_group);
1159                 sysfs_remove_group(&dev->kobj, &qeth_device_ipato_group);
1160                 sysfs_remove_group(&dev->kobj, &qeth_device_vipa_group);
1161                 return ret;
1162         }
1163         return 0;
1164 }
1165
1166 void qeth_l3_remove_device_attributes(struct device *dev)
1167 {
1168         sysfs_remove_group(&dev->kobj, &qeth_l3_device_attr_group);
1169         sysfs_remove_group(&dev->kobj, &qeth_device_ipato_group);
1170         sysfs_remove_group(&dev->kobj, &qeth_device_vipa_group);
1171         sysfs_remove_group(&dev->kobj, &qeth_device_rxip_group);
1172 }