Merge master.kernel.org:/pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-2.6] / drivers / s390 / net / qeth_sys.c
1 /*
2  *
3  * linux/drivers/s390/net/qeth_sys.c ($Revision: 1.55 $)
4  *
5  * Linux on zSeries OSA Express and HiperSockets support
6  * This file contains code related to sysfs.
7  *
8  * Copyright 2000,2003 IBM Corporation
9  *
10  * Author(s): Thomas Spatzier <tspat@de.ibm.com>
11  *            Frank Pavlic <pavlic@de.ibm.com>
12  *
13  */
14 #include <linux/list.h>
15 #include <linux/rwsem.h>
16
17 #include <asm/ebcdic.h>
18
19 #include "qeth.h"
20 #include "qeth_mpc.h"
21 #include "qeth_fs.h"
22
23 const char *VERSION_QETH_SYS_C = "$Revision: 1.55 $";
24
25 /*****************************************************************************/
26 /*                                                                           */
27 /*          /sys-fs stuff UNDER DEVELOPMENT !!!                              */
28 /*                                                                           */
29 /*****************************************************************************/
30 //low/high watermark
31
32 static ssize_t
33 qeth_dev_state_show(struct device *dev, struct device_attribute *attr, char *buf)
34 {
35         struct qeth_card *card = dev->driver_data;
36         if (!card)
37                 return -EINVAL;
38
39         switch (card->state) {
40         case CARD_STATE_DOWN:
41                 return sprintf(buf, "DOWN\n");
42         case CARD_STATE_HARDSETUP:
43                 return sprintf(buf, "HARDSETUP\n");
44         case CARD_STATE_SOFTSETUP:
45                 return sprintf(buf, "SOFTSETUP\n");
46         case CARD_STATE_UP:
47                 if (card->lan_online)
48                 return sprintf(buf, "UP (LAN ONLINE)\n");
49                 else
50                         return sprintf(buf, "UP (LAN OFFLINE)\n");
51         case CARD_STATE_RECOVER:
52                 return sprintf(buf, "RECOVER\n");
53         default:
54                 return sprintf(buf, "UNKNOWN\n");
55         }
56 }
57
58 static DEVICE_ATTR(state, 0444, qeth_dev_state_show, NULL);
59
60 static ssize_t
61 qeth_dev_chpid_show(struct device *dev, struct device_attribute *attr, char *buf)
62 {
63         struct qeth_card *card = dev->driver_data;
64         if (!card)
65                 return -EINVAL;
66
67         return sprintf(buf, "%02X\n", card->info.chpid);
68 }
69
70 static DEVICE_ATTR(chpid, 0444, qeth_dev_chpid_show, NULL);
71
72 static ssize_t
73 qeth_dev_if_name_show(struct device *dev, struct device_attribute *attr, char *buf)
74 {
75         struct qeth_card *card = dev->driver_data;
76         if (!card)
77                 return -EINVAL;
78         return sprintf(buf, "%s\n", QETH_CARD_IFNAME(card));
79 }
80
81 static DEVICE_ATTR(if_name, 0444, qeth_dev_if_name_show, NULL);
82
83 static ssize_t
84 qeth_dev_card_type_show(struct device *dev, struct device_attribute *attr, char *buf)
85 {
86         struct qeth_card *card = dev->driver_data;
87         if (!card)
88                 return -EINVAL;
89
90         return sprintf(buf, "%s\n", qeth_get_cardname_short(card));
91 }
92
93 static DEVICE_ATTR(card_type, 0444, qeth_dev_card_type_show, NULL);
94
95 static ssize_t
96 qeth_dev_portno_show(struct device *dev, struct device_attribute *attr, char *buf)
97 {
98         struct qeth_card *card = dev->driver_data;
99         if (!card)
100                 return -EINVAL;
101
102         return sprintf(buf, "%i\n", card->info.portno);
103 }
104
105 static ssize_t
106 qeth_dev_portno_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
107 {
108         struct qeth_card *card = dev->driver_data;
109         char *tmp;
110         unsigned int portno;
111
112         if (!card)
113                 return -EINVAL;
114
115         if ((card->state != CARD_STATE_DOWN) &&
116             (card->state != CARD_STATE_RECOVER))
117                 return -EPERM;
118
119         portno = simple_strtoul(buf, &tmp, 16);
120         if ((portno < 0) || (portno > MAX_PORTNO)){
121                 PRINT_WARN("portno 0x%X is out of range\n", portno);
122                 return -EINVAL;
123         }
124
125         card->info.portno = portno;
126         return count;
127 }
128
129 static DEVICE_ATTR(portno, 0644, qeth_dev_portno_show, qeth_dev_portno_store);
130
131 static ssize_t
132 qeth_dev_portname_show(struct device *dev, struct device_attribute *attr, char *buf)
133 {
134         struct qeth_card *card = dev->driver_data;
135         char portname[9] = {0, };
136
137         if (!card)
138                 return -EINVAL;
139
140         if (card->info.portname_required) {
141                 memcpy(portname, card->info.portname + 1, 8);
142                 EBCASC(portname, 8);
143                 return sprintf(buf, "%s\n", portname);
144         } else
145                 return sprintf(buf, "no portname required\n");
146 }
147
148 static ssize_t
149 qeth_dev_portname_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
150 {
151         struct qeth_card *card = dev->driver_data;
152         char *tmp;
153         int i;
154
155         if (!card)
156                 return -EINVAL;
157
158         if ((card->state != CARD_STATE_DOWN) &&
159             (card->state != CARD_STATE_RECOVER))
160                 return -EPERM;
161
162         tmp = strsep((char **) &buf, "\n");
163         if ((strlen(tmp) > 8) || (strlen(tmp) < 2))
164                 return -EINVAL;
165
166         card->info.portname[0] = strlen(tmp);
167         /* for beauty reasons */
168         for (i = 1; i < 9; i++)
169                 card->info.portname[i] = ' ';
170         strcpy(card->info.portname + 1, tmp);
171         ASCEBC(card->info.portname + 1, 8);
172
173         return count;
174 }
175
176 static DEVICE_ATTR(portname, 0644, qeth_dev_portname_show,
177                 qeth_dev_portname_store);
178
179 static ssize_t
180 qeth_dev_checksum_show(struct device *dev, struct device_attribute *attr, char *buf)
181 {
182         struct qeth_card *card = dev->driver_data;
183
184         if (!card)
185                 return -EINVAL;
186
187         return sprintf(buf, "%s checksumming\n", qeth_get_checksum_str(card));
188 }
189
190 static ssize_t
191 qeth_dev_checksum_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
192 {
193         struct qeth_card *card = dev->driver_data;
194         char *tmp;
195
196         if (!card)
197                 return -EINVAL;
198
199         if ((card->state != CARD_STATE_DOWN) &&
200             (card->state != CARD_STATE_RECOVER))
201                 return -EPERM;
202
203         tmp = strsep((char **) &buf, "\n");
204         if (!strcmp(tmp, "sw_checksumming"))
205                 card->options.checksum_type = SW_CHECKSUMMING;
206         else if (!strcmp(tmp, "hw_checksumming"))
207                 card->options.checksum_type = HW_CHECKSUMMING;
208         else if (!strcmp(tmp, "no_checksumming"))
209                 card->options.checksum_type = NO_CHECKSUMMING;
210         else {
211                 PRINT_WARN("Unknown checksumming type '%s'\n", tmp);
212                 return -EINVAL;
213         }
214         return count;
215 }
216
217 static DEVICE_ATTR(checksumming, 0644, qeth_dev_checksum_show,
218                 qeth_dev_checksum_store);
219
220 static ssize_t
221 qeth_dev_prioqing_show(struct device *dev, struct device_attribute *attr, char *buf)
222 {
223         struct qeth_card *card = dev->driver_data;
224
225         if (!card)
226                 return -EINVAL;
227
228         switch (card->qdio.do_prio_queueing) {
229         case QETH_PRIO_Q_ING_PREC:
230                 return sprintf(buf, "%s\n", "by precedence");
231         case QETH_PRIO_Q_ING_TOS:
232                 return sprintf(buf, "%s\n", "by type of service");
233         default:
234                 return sprintf(buf, "always queue %i\n",
235                                card->qdio.default_out_queue);
236         }
237 }
238
239 static ssize_t
240 qeth_dev_prioqing_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
241 {
242         struct qeth_card *card = dev->driver_data;
243         char *tmp;
244
245         if (!card)
246                 return -EINVAL;
247
248         if ((card->state != CARD_STATE_DOWN) &&
249             (card->state != CARD_STATE_RECOVER))
250                 return -EPERM;
251
252         /* check if 1920 devices are supported ,
253          * if though we have to permit priority queueing
254          */
255         if (card->qdio.no_out_queues == 1) {
256                 PRINT_WARN("Priority queueing disabled due "
257                            "to hardware limitations!\n");
258                 card->qdio.do_prio_queueing = QETH_PRIOQ_DEFAULT;
259                 return -EPERM;
260         }
261
262         tmp = strsep((char **) &buf, "\n");
263         if (!strcmp(tmp, "prio_queueing_prec"))
264                 card->qdio.do_prio_queueing = QETH_PRIO_Q_ING_PREC;
265         else if (!strcmp(tmp, "prio_queueing_tos"))
266                 card->qdio.do_prio_queueing = QETH_PRIO_Q_ING_TOS;
267         else if (!strcmp(tmp, "no_prio_queueing:0")) {
268                 card->qdio.do_prio_queueing = QETH_NO_PRIO_QUEUEING;
269                 card->qdio.default_out_queue = 0;
270         } else if (!strcmp(tmp, "no_prio_queueing:1")) {
271                 card->qdio.do_prio_queueing = QETH_NO_PRIO_QUEUEING;
272                 card->qdio.default_out_queue = 1;
273         } else if (!strcmp(tmp, "no_prio_queueing:2")) {
274                 card->qdio.do_prio_queueing = QETH_NO_PRIO_QUEUEING;
275                 card->qdio.default_out_queue = 2;
276         } else if (!strcmp(tmp, "no_prio_queueing:3")) {
277                 card->qdio.do_prio_queueing = QETH_NO_PRIO_QUEUEING;
278                 card->qdio.default_out_queue = 3;
279         } else if (!strcmp(tmp, "no_prio_queueing")) {
280                 card->qdio.do_prio_queueing = QETH_NO_PRIO_QUEUEING;
281                 card->qdio.default_out_queue = QETH_DEFAULT_QUEUE;
282         } else {
283                 PRINT_WARN("Unknown queueing type '%s'\n", tmp);
284                 return -EINVAL;
285         }
286         return count;
287 }
288
289 static DEVICE_ATTR(priority_queueing, 0644, qeth_dev_prioqing_show,
290                 qeth_dev_prioqing_store);
291
292 static ssize_t
293 qeth_dev_bufcnt_show(struct device *dev, struct device_attribute *attr, char *buf)
294 {
295         struct qeth_card *card = dev->driver_data;
296
297         if (!card)
298                 return -EINVAL;
299
300         return sprintf(buf, "%i\n", card->qdio.in_buf_pool.buf_count);
301 }
302
303 static ssize_t
304 qeth_dev_bufcnt_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
305 {
306         struct qeth_card *card = dev->driver_data;
307         char *tmp;
308         int cnt, old_cnt;
309         int rc;
310
311         if (!card)
312                 return -EINVAL;
313
314         if ((card->state != CARD_STATE_DOWN) &&
315             (card->state != CARD_STATE_RECOVER))
316                 return -EPERM;
317
318         old_cnt = card->qdio.in_buf_pool.buf_count;
319         cnt = simple_strtoul(buf, &tmp, 10);
320         cnt = (cnt < QETH_IN_BUF_COUNT_MIN) ? QETH_IN_BUF_COUNT_MIN :
321                 ((cnt > QETH_IN_BUF_COUNT_MAX) ? QETH_IN_BUF_COUNT_MAX : cnt);
322         if (old_cnt != cnt) {
323                 if ((rc = qeth_realloc_buffer_pool(card, cnt)))
324                         PRINT_WARN("Error (%d) while setting "
325                                    "buffer count.\n", rc);
326         }
327         return count;
328 }
329
330 static DEVICE_ATTR(buffer_count, 0644, qeth_dev_bufcnt_show,
331                 qeth_dev_bufcnt_store);
332
333 static inline ssize_t
334 qeth_dev_route_show(struct qeth_card *card, struct qeth_routing_info *route,
335                     char *buf)
336 {
337         switch (route->type) {
338         case PRIMARY_ROUTER:
339                 return sprintf(buf, "%s\n", "primary router");
340         case SECONDARY_ROUTER:
341                 return sprintf(buf, "%s\n", "secondary router");
342         case MULTICAST_ROUTER:
343                 if (card->info.broadcast_capable == QETH_BROADCAST_WITHOUT_ECHO)
344                         return sprintf(buf, "%s\n", "multicast router+");
345                 else
346                         return sprintf(buf, "%s\n", "multicast router");
347         case PRIMARY_CONNECTOR:
348                 if (card->info.broadcast_capable == QETH_BROADCAST_WITHOUT_ECHO)
349                         return sprintf(buf, "%s\n", "primary connector+");
350                 else
351                         return sprintf(buf, "%s\n", "primary connector");
352         case SECONDARY_CONNECTOR:
353                 if (card->info.broadcast_capable == QETH_BROADCAST_WITHOUT_ECHO)
354                         return sprintf(buf, "%s\n", "secondary connector+");
355                 else
356                         return sprintf(buf, "%s\n", "secondary connector");
357         default:
358                 return sprintf(buf, "%s\n", "no");
359         }
360 }
361
362 static ssize_t
363 qeth_dev_route4_show(struct device *dev, struct device_attribute *attr, char *buf)
364 {
365         struct qeth_card *card = dev->driver_data;
366
367         if (!card)
368                 return -EINVAL;
369
370         return qeth_dev_route_show(card, &card->options.route4, buf);
371 }
372
373 static inline ssize_t
374 qeth_dev_route_store(struct qeth_card *card, struct qeth_routing_info *route,
375                 enum qeth_prot_versions prot, const char *buf, size_t count)
376 {
377         enum qeth_routing_types old_route_type = route->type;
378         char *tmp;
379         int rc;
380
381         tmp = strsep((char **) &buf, "\n");
382
383         if (!strcmp(tmp, "no_router")){
384                 route->type = NO_ROUTER;
385         } else if (!strcmp(tmp, "primary_connector")) {
386                 route->type = PRIMARY_CONNECTOR;
387         } else if (!strcmp(tmp, "secondary_connector")) {
388                 route->type = SECONDARY_CONNECTOR;
389         } else if (!strcmp(tmp, "multicast_router")) {
390                 route->type = MULTICAST_ROUTER;
391         } else if (!strcmp(tmp, "primary_router")) {
392                 route->type = PRIMARY_ROUTER;
393         } else if (!strcmp(tmp, "secondary_router")) {
394                 route->type = SECONDARY_ROUTER;
395         } else if (!strcmp(tmp, "multicast_router")) {
396                 route->type = MULTICAST_ROUTER;
397         } else {
398                 PRINT_WARN("Invalid routing type '%s'.\n", tmp);
399                 return -EINVAL;
400         }
401         if (((card->state == CARD_STATE_SOFTSETUP) ||
402              (card->state == CARD_STATE_UP)) &&
403             (old_route_type != route->type)){
404                 if (prot == QETH_PROT_IPV4)
405                         rc = qeth_setrouting_v4(card);
406                 else if (prot == QETH_PROT_IPV6)
407                         rc = qeth_setrouting_v6(card);
408         }
409         return count;
410 }
411
412 static ssize_t
413 qeth_dev_route4_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
414 {
415         struct qeth_card *card = dev->driver_data;
416
417         if (!card)
418                 return -EINVAL;
419
420         return qeth_dev_route_store(card, &card->options.route4,
421                                     QETH_PROT_IPV4, buf, count);
422 }
423
424 static DEVICE_ATTR(route4, 0644, qeth_dev_route4_show, qeth_dev_route4_store);
425
426 #ifdef CONFIG_QETH_IPV6
427 static ssize_t
428 qeth_dev_route6_show(struct device *dev, struct device_attribute *attr, char *buf)
429 {
430         struct qeth_card *card = dev->driver_data;
431
432         if (!card)
433                 return -EINVAL;
434
435         if (!qeth_is_supported(card, IPA_IPV6))
436                 return sprintf(buf, "%s\n", "n/a");
437
438         return qeth_dev_route_show(card, &card->options.route6, buf);
439 }
440
441 static ssize_t
442 qeth_dev_route6_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
443 {
444         struct qeth_card *card = dev->driver_data;
445
446         if (!card)
447                 return -EINVAL;
448
449         if (!qeth_is_supported(card, IPA_IPV6)){
450                 PRINT_WARN("IPv6 not supported for interface %s.\n"
451                            "Routing status no changed.\n",
452                            QETH_CARD_IFNAME(card));
453                 return -ENOTSUPP;
454         }
455
456         return qeth_dev_route_store(card, &card->options.route6,
457                                     QETH_PROT_IPV6, buf, count);
458 }
459
460 static DEVICE_ATTR(route6, 0644, qeth_dev_route6_show, qeth_dev_route6_store);
461 #endif
462
463 static ssize_t
464 qeth_dev_add_hhlen_show(struct device *dev, struct device_attribute *attr, char *buf)
465 {
466         struct qeth_card *card = dev->driver_data;
467
468         if (!card)
469                 return -EINVAL;
470
471         return sprintf(buf, "%i\n", card->options.add_hhlen);
472 }
473
474 static ssize_t
475 qeth_dev_add_hhlen_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
476 {
477         struct qeth_card *card = dev->driver_data;
478         char *tmp;
479         int i;
480
481         if (!card)
482                 return -EINVAL;
483
484         if ((card->state != CARD_STATE_DOWN) &&
485             (card->state != CARD_STATE_RECOVER))
486                 return -EPERM;
487
488         i = simple_strtoul(buf, &tmp, 10);
489         if ((i < 0) || (i > MAX_ADD_HHLEN)) {
490                 PRINT_WARN("add_hhlen out of range\n");
491                 return -EINVAL;
492         }
493         card->options.add_hhlen = i;
494
495         return count;
496 }
497
498 static DEVICE_ATTR(add_hhlen, 0644, qeth_dev_add_hhlen_show,
499                    qeth_dev_add_hhlen_store);
500
501 static ssize_t
502 qeth_dev_fake_ll_show(struct device *dev, struct device_attribute *attr, char *buf)
503 {
504         struct qeth_card *card = dev->driver_data;
505
506         if (!card)
507                 return -EINVAL;
508
509         return sprintf(buf, "%i\n", card->options.fake_ll? 1:0);
510 }
511
512 static ssize_t
513 qeth_dev_fake_ll_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
514 {
515         struct qeth_card *card = dev->driver_data;
516         char *tmp;
517         int i;
518
519         if (!card)
520                 return -EINVAL;
521
522         if ((card->state != CARD_STATE_DOWN) &&
523             (card->state != CARD_STATE_RECOVER))
524                 return -EPERM;
525
526         i = simple_strtoul(buf, &tmp, 16);
527         if ((i != 0) && (i != 1)) {
528                 PRINT_WARN("fake_ll: write 0 or 1 to this file!\n");
529                 return -EINVAL;
530         }
531         card->options.fake_ll = i;
532         return count;
533 }
534
535 static DEVICE_ATTR(fake_ll, 0644, qeth_dev_fake_ll_show,
536                    qeth_dev_fake_ll_store);
537
538 static ssize_t
539 qeth_dev_fake_broadcast_show(struct device *dev, struct device_attribute *attr, char *buf)
540 {
541         struct qeth_card *card = dev->driver_data;
542
543         if (!card)
544                 return -EINVAL;
545
546         return sprintf(buf, "%i\n", card->options.fake_broadcast? 1:0);
547 }
548
549 static ssize_t
550 qeth_dev_fake_broadcast_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
551 {
552         struct qeth_card *card = dev->driver_data;
553         char *tmp;
554         int i;
555
556         if (!card)
557                 return -EINVAL;
558
559         if ((card->state != CARD_STATE_DOWN) &&
560             (card->state != CARD_STATE_RECOVER))
561                 return -EPERM;
562
563         i = simple_strtoul(buf, &tmp, 16);
564         if ((i == 0) || (i == 1))
565                 card->options.fake_broadcast = i;
566         else {
567                 PRINT_WARN("fake_broadcast: write 0 or 1 to this file!\n");
568                 return -EINVAL;
569         }
570         return count;
571 }
572
573 static DEVICE_ATTR(fake_broadcast, 0644, qeth_dev_fake_broadcast_show,
574                    qeth_dev_fake_broadcast_store);
575
576 static ssize_t
577 qeth_dev_recover_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
578 {
579         struct qeth_card *card = dev->driver_data;
580         char *tmp;
581         int i;
582
583         if (!card)
584                 return -EINVAL;
585
586         if (card->state != CARD_STATE_UP)
587                 return -EPERM;
588
589         i = simple_strtoul(buf, &tmp, 16);
590         if (i == 1)
591                 qeth_schedule_recovery(card);
592
593         return count;
594 }
595
596 static DEVICE_ATTR(recover, 0200, NULL, qeth_dev_recover_store);
597
598 static ssize_t
599 qeth_dev_broadcast_mode_show(struct device *dev, struct device_attribute *attr, char *buf)
600 {
601         struct qeth_card *card = dev->driver_data;
602
603         if (!card)
604                 return -EINVAL;
605
606         if (!((card->info.link_type == QETH_LINK_TYPE_HSTR) ||
607               (card->info.link_type == QETH_LINK_TYPE_LANE_TR)))
608                 return sprintf(buf, "n/a\n");
609
610         return sprintf(buf, "%s\n", (card->options.broadcast_mode ==
611                                      QETH_TR_BROADCAST_ALLRINGS)?
612                        "all rings":"local");
613 }
614
615 static ssize_t
616 qeth_dev_broadcast_mode_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
617 {
618         struct qeth_card *card = dev->driver_data;
619         char *tmp;
620
621         if (!card)
622                 return -EINVAL;
623
624         if ((card->state != CARD_STATE_DOWN) &&
625             (card->state != CARD_STATE_RECOVER))
626                 return -EPERM;
627
628         if (!((card->info.link_type == QETH_LINK_TYPE_HSTR) ||
629               (card->info.link_type == QETH_LINK_TYPE_LANE_TR))){
630                 PRINT_WARN("Device is not a tokenring device!\n");
631                 return -EINVAL;
632         }
633
634         tmp = strsep((char **) &buf, "\n");
635
636         if (!strcmp(tmp, "local")){
637                 card->options.broadcast_mode = QETH_TR_BROADCAST_LOCAL;
638                 return count;
639         } else if (!strcmp(tmp, "all_rings")) {
640                 card->options.broadcast_mode = QETH_TR_BROADCAST_ALLRINGS;
641                 return count;
642         } else {
643                 PRINT_WARN("broadcast_mode: invalid mode %s!\n",
644                            tmp);
645                 return -EINVAL;
646         }
647         return count;
648 }
649
650 static DEVICE_ATTR(broadcast_mode, 0644, qeth_dev_broadcast_mode_show,
651                    qeth_dev_broadcast_mode_store);
652
653 static ssize_t
654 qeth_dev_canonical_macaddr_show(struct device *dev, struct device_attribute *attr, char *buf)
655 {
656         struct qeth_card *card = dev->driver_data;
657
658         if (!card)
659                 return -EINVAL;
660
661         if (!((card->info.link_type == QETH_LINK_TYPE_HSTR) ||
662               (card->info.link_type == QETH_LINK_TYPE_LANE_TR)))
663                 return sprintf(buf, "n/a\n");
664
665         return sprintf(buf, "%i\n", (card->options.macaddr_mode ==
666                                      QETH_TR_MACADDR_CANONICAL)? 1:0);
667 }
668
669 static ssize_t
670 qeth_dev_canonical_macaddr_store(struct device *dev, struct device_attribute *attr, const char *buf,
671                                   size_t count)
672 {
673         struct qeth_card *card = dev->driver_data;
674         char *tmp;
675         int i;
676
677         if (!card)
678                 return -EINVAL;
679
680         if ((card->state != CARD_STATE_DOWN) &&
681             (card->state != CARD_STATE_RECOVER))
682                 return -EPERM;
683
684         if (!((card->info.link_type == QETH_LINK_TYPE_HSTR) ||
685               (card->info.link_type == QETH_LINK_TYPE_LANE_TR))){
686                 PRINT_WARN("Device is not a tokenring device!\n");
687                 return -EINVAL;
688         }
689
690         i = simple_strtoul(buf, &tmp, 16);
691         if ((i == 0) || (i == 1))
692                 card->options.macaddr_mode = i?
693                         QETH_TR_MACADDR_CANONICAL :
694                         QETH_TR_MACADDR_NONCANONICAL;
695         else {
696                 PRINT_WARN("canonical_macaddr: write 0 or 1 to this file!\n");
697                 return -EINVAL;
698         }
699         return count;
700 }
701
702 static DEVICE_ATTR(canonical_macaddr, 0644, qeth_dev_canonical_macaddr_show,
703                    qeth_dev_canonical_macaddr_store);
704
705 static ssize_t
706 qeth_dev_layer2_show(struct device *dev, struct device_attribute *attr, char *buf)
707 {
708         struct qeth_card *card = dev->driver_data;
709
710         if (!card)
711                 return -EINVAL;
712
713         return sprintf(buf, "%i\n", card->options.layer2 ? 1:0);
714 }
715
716 static ssize_t
717 qeth_dev_layer2_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
718 {
719         struct qeth_card *card = dev->driver_data;
720         char *tmp;
721         int i;
722
723         if (!card)
724                 return -EINVAL;
725         if (card->info.type == QETH_CARD_TYPE_IQD) {
726                 PRINT_WARN("Layer2 on Hipersockets is not supported! \n");
727                 return -EPERM;
728         }
729
730         if (((card->state != CARD_STATE_DOWN) &&
731              (card->state != CARD_STATE_RECOVER)))
732                 return -EPERM;
733
734         i = simple_strtoul(buf, &tmp, 16);
735         if ((i == 0) || (i == 1))
736                 card->options.layer2 = i;
737         else {
738                 PRINT_WARN("layer2: write 0 or 1 to this file!\n");
739                 return -EINVAL;
740         }
741         return count;
742 }
743
744 static DEVICE_ATTR(layer2, 0644, qeth_dev_layer2_show,
745                    qeth_dev_layer2_store);
746
747 static ssize_t
748 qeth_dev_large_send_show(struct device *dev, struct device_attribute *attr, char *buf)
749 {
750         struct qeth_card *card = dev->driver_data;
751
752         if (!card)
753                 return -EINVAL;
754
755         switch (card->options.large_send) {
756         case QETH_LARGE_SEND_NO:
757                 return sprintf(buf, "%s\n", "no");
758         case QETH_LARGE_SEND_EDDP:
759                 return sprintf(buf, "%s\n", "EDDP");
760         case QETH_LARGE_SEND_TSO:
761                 return sprintf(buf, "%s\n", "TSO");
762         default:
763                 return sprintf(buf, "%s\n", "N/A");
764         }
765 }
766
767 static ssize_t
768 qeth_dev_large_send_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
769 {
770         struct qeth_card *card = dev->driver_data;
771         enum qeth_large_send_types type;
772         int rc = 0;
773         char *tmp;
774
775         if (!card)
776                 return -EINVAL;
777         tmp = strsep((char **) &buf, "\n");
778         if (!strcmp(tmp, "no")){
779                 type = QETH_LARGE_SEND_NO;
780         } else if (!strcmp(tmp, "EDDP")) {
781                 type = QETH_LARGE_SEND_EDDP;
782         } else if (!strcmp(tmp, "TSO")) {
783                 type = QETH_LARGE_SEND_TSO;
784         } else {
785                 PRINT_WARN("large_send: invalid mode %s!\n", tmp);
786                 return -EINVAL;
787         }
788         if (card->options.large_send == type)
789                 return count;
790         if ((rc = qeth_set_large_send(card, type)))     
791                 return rc;
792         return count;
793 }
794
795 static DEVICE_ATTR(large_send, 0644, qeth_dev_large_send_show,
796                    qeth_dev_large_send_store);
797
798 static ssize_t
799 qeth_dev_blkt_show(char *buf, struct qeth_card *card, int value )
800 {
801
802         if (!card)
803                 return -EINVAL;
804
805         return sprintf(buf, "%i\n", value);
806 }
807
808 static ssize_t
809 qeth_dev_blkt_store(struct qeth_card *card, const char *buf, size_t count,
810                           int *value, int max_value)
811 {
812         char *tmp;
813         int i;
814
815         if (!card)
816                 return -EINVAL;
817
818         if ((card->state != CARD_STATE_DOWN) &&
819             (card->state != CARD_STATE_RECOVER))
820                 return -EPERM;
821
822         i = simple_strtoul(buf, &tmp, 10);
823         if (i <= max_value) {
824                 *value = i;
825         } else {
826                 PRINT_WARN("blkt total time: write values between"
827                            " 0 and %d to this file!\n", max_value);
828                 return -EINVAL;
829         }
830         return count;
831 }
832
833 static ssize_t
834 qeth_dev_blkt_total_show(struct device *dev, struct device_attribute *attr, char *buf)
835 {
836         struct qeth_card *card = dev->driver_data;
837
838         return qeth_dev_blkt_show(buf, card, card->info.blkt.time_total);
839 }
840
841
842 static ssize_t
843 qeth_dev_blkt_total_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
844 {
845         struct qeth_card *card = dev->driver_data;
846
847         return qeth_dev_blkt_store(card, buf, count,
848                                    &card->info.blkt.time_total,1000);
849 }
850
851
852
853 static DEVICE_ATTR(total, 0644, qeth_dev_blkt_total_show,
854                    qeth_dev_blkt_total_store);
855
856 static ssize_t
857 qeth_dev_blkt_inter_show(struct device *dev, struct device_attribute *attr, char *buf)
858 {
859         struct qeth_card *card = dev->driver_data;
860
861         return qeth_dev_blkt_show(buf, card, card->info.blkt.inter_packet);
862 }
863
864
865 static ssize_t
866 qeth_dev_blkt_inter_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
867 {
868         struct qeth_card *card = dev->driver_data;
869
870         return qeth_dev_blkt_store(card, buf, count,
871                                    &card->info.blkt.inter_packet,100);
872 }
873
874 static DEVICE_ATTR(inter, 0644, qeth_dev_blkt_inter_show,
875                    qeth_dev_blkt_inter_store);
876
877 static ssize_t
878 qeth_dev_blkt_inter_jumbo_show(struct device *dev, struct device_attribute *attr, char *buf)
879 {
880         struct qeth_card *card = dev->driver_data;
881
882         return qeth_dev_blkt_show(buf, card,
883                                   card->info.blkt.inter_packet_jumbo);
884 }
885
886
887 static ssize_t
888 qeth_dev_blkt_inter_jumbo_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
889 {
890         struct qeth_card *card = dev->driver_data;
891
892         return qeth_dev_blkt_store(card, buf, count,
893                                    &card->info.blkt.inter_packet_jumbo,100);
894 }
895
896 static DEVICE_ATTR(inter_jumbo, 0644, qeth_dev_blkt_inter_jumbo_show,
897                    qeth_dev_blkt_inter_jumbo_store);
898
899 static struct device_attribute * qeth_blkt_device_attrs[] = {
900         &dev_attr_total,
901         &dev_attr_inter,
902         &dev_attr_inter_jumbo,
903         NULL,
904 };
905
906 static struct attribute_group qeth_device_blkt_group = {
907         .name = "blkt",
908         .attrs = (struct attribute **)qeth_blkt_device_attrs,
909 };
910
911 static struct device_attribute * qeth_device_attrs[] = {
912         &dev_attr_state,
913         &dev_attr_chpid,
914         &dev_attr_if_name,
915         &dev_attr_card_type,
916         &dev_attr_portno,
917         &dev_attr_portname,
918         &dev_attr_checksumming,
919         &dev_attr_priority_queueing,
920         &dev_attr_buffer_count,
921         &dev_attr_route4,
922 #ifdef CONFIG_QETH_IPV6
923         &dev_attr_route6,
924 #endif
925         &dev_attr_add_hhlen,
926         &dev_attr_fake_ll,
927         &dev_attr_fake_broadcast,
928         &dev_attr_recover,
929         &dev_attr_broadcast_mode,
930         &dev_attr_canonical_macaddr,
931         &dev_attr_layer2,
932         &dev_attr_large_send,
933         NULL,
934 };
935
936 static struct attribute_group qeth_device_attr_group = {
937         .attrs = (struct attribute **)qeth_device_attrs,
938 };
939
940 static struct device_attribute * qeth_osn_device_attrs[] = {
941         &dev_attr_state,
942         &dev_attr_chpid,
943         &dev_attr_if_name,
944         &dev_attr_card_type,
945         &dev_attr_buffer_count,
946         &dev_attr_recover,
947         NULL,
948 };
949
950 static struct attribute_group qeth_osn_device_attr_group = {
951         .attrs = (struct attribute **)qeth_osn_device_attrs,
952 };
953
954 #define QETH_DEVICE_ATTR(_id,_name,_mode,_show,_store)                       \
955 struct device_attribute dev_attr_##_id = {                                   \
956         .attr = {.name=__stringify(_name), .mode=_mode, .owner=THIS_MODULE },\
957         .show   = _show,                                                     \
958         .store  = _store,                                                    \
959 };
960
961 int
962 qeth_check_layer2(struct qeth_card *card)
963 {
964         if (card->options.layer2)
965                 return -EPERM;
966         return 0;
967 }
968
969
970 static ssize_t
971 qeth_dev_ipato_enable_show(struct device *dev, struct device_attribute *attr, char *buf)
972 {
973         struct qeth_card *card = dev->driver_data;
974
975         if (!card)
976                 return -EINVAL;
977
978         if (qeth_check_layer2(card))
979                 return -EPERM;
980         return sprintf(buf, "%i\n", card->ipato.enabled? 1:0);
981 }
982
983 static ssize_t
984 qeth_dev_ipato_enable_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
985 {
986         struct qeth_card *card = dev->driver_data;
987         char *tmp;
988
989         if (!card)
990                 return -EINVAL;
991
992         if ((card->state != CARD_STATE_DOWN) &&
993             (card->state != CARD_STATE_RECOVER))
994                 return -EPERM;
995
996         if (qeth_check_layer2(card))
997                 return -EPERM;
998
999         tmp = strsep((char **) &buf, "\n");
1000         if (!strcmp(tmp, "toggle")){
1001                 card->ipato.enabled = (card->ipato.enabled)? 0 : 1;
1002         } else if (!strcmp(tmp, "1")){
1003                 card->ipato.enabled = 1;
1004         } else if (!strcmp(tmp, "0")){
1005                 card->ipato.enabled = 0;
1006         } else {
1007                 PRINT_WARN("ipato_enable: write 0, 1 or 'toggle' to "
1008                            "this file\n");
1009                 return -EINVAL;
1010         }
1011         return count;
1012 }
1013
1014 static QETH_DEVICE_ATTR(ipato_enable, enable, 0644,
1015                         qeth_dev_ipato_enable_show,
1016                         qeth_dev_ipato_enable_store);
1017
1018 static ssize_t
1019 qeth_dev_ipato_invert4_show(struct device *dev, struct device_attribute *attr, char *buf)
1020 {
1021         struct qeth_card *card = dev->driver_data;
1022
1023         if (!card)
1024                 return -EINVAL;
1025
1026         if (qeth_check_layer2(card))
1027                 return -EPERM;
1028
1029         return sprintf(buf, "%i\n", card->ipato.invert4? 1:0);
1030 }
1031
1032 static ssize_t
1033 qeth_dev_ipato_invert4_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1034 {
1035         struct qeth_card *card = dev->driver_data;
1036         char *tmp;
1037
1038         if (!card)
1039                 return -EINVAL;
1040
1041         if (qeth_check_layer2(card))
1042                 return -EPERM;
1043
1044         tmp = strsep((char **) &buf, "\n");
1045         if (!strcmp(tmp, "toggle")){
1046                 card->ipato.invert4 = (card->ipato.invert4)? 0 : 1;
1047         } else if (!strcmp(tmp, "1")){
1048                 card->ipato.invert4 = 1;
1049         } else if (!strcmp(tmp, "0")){
1050                 card->ipato.invert4 = 0;
1051         } else {
1052                 PRINT_WARN("ipato_invert4: write 0, 1 or 'toggle' to "
1053                            "this file\n");
1054                 return -EINVAL;
1055         }
1056         return count;
1057 }
1058
1059 static QETH_DEVICE_ATTR(ipato_invert4, invert4, 0644,
1060                         qeth_dev_ipato_invert4_show,
1061                         qeth_dev_ipato_invert4_store);
1062
1063 static inline ssize_t
1064 qeth_dev_ipato_add_show(char *buf, struct qeth_card *card,
1065                         enum qeth_prot_versions proto)
1066 {
1067         struct qeth_ipato_entry *ipatoe;
1068         unsigned long flags;
1069         char addr_str[40];
1070         int entry_len; /* length of 1 entry string, differs between v4 and v6 */
1071         int i = 0;
1072
1073         if (qeth_check_layer2(card))
1074                 return -EPERM;
1075
1076         entry_len = (proto == QETH_PROT_IPV4)? 12 : 40;
1077         /* add strlen for "/<mask>\n" */
1078         entry_len += (proto == QETH_PROT_IPV4)? 5 : 6;
1079         spin_lock_irqsave(&card->ip_lock, flags);
1080         list_for_each_entry(ipatoe, &card->ipato.entries, entry){
1081                 if (ipatoe->proto != proto)
1082                         continue;
1083                 /* String must not be longer than PAGE_SIZE. So we check if
1084                  * string length gets near PAGE_SIZE. Then we can savely display
1085                  * the next IPv6 address (worst case, compared to IPv4) */
1086                 if ((PAGE_SIZE - i) <= entry_len)
1087                         break;
1088                 qeth_ipaddr_to_string(proto, ipatoe->addr, addr_str);
1089                 i += snprintf(buf + i, PAGE_SIZE - i,
1090                               "%s/%i\n", addr_str, ipatoe->mask_bits);
1091         }
1092         spin_unlock_irqrestore(&card->ip_lock, flags);
1093         i += snprintf(buf + i, PAGE_SIZE - i, "\n");
1094
1095         return i;
1096 }
1097
1098 static ssize_t
1099 qeth_dev_ipato_add4_show(struct device *dev, struct device_attribute *attr, char *buf)
1100 {
1101         struct qeth_card *card = dev->driver_data;
1102
1103         if (!card)
1104                 return -EINVAL;
1105
1106         return qeth_dev_ipato_add_show(buf, card, QETH_PROT_IPV4);
1107 }
1108
1109 static inline int
1110 qeth_parse_ipatoe(const char* buf, enum qeth_prot_versions proto,
1111                   u8 *addr, int *mask_bits)
1112 {
1113         const char *start, *end;
1114         char *tmp;
1115         char buffer[49] = {0, };
1116
1117         start = buf;
1118         /* get address string */
1119         end = strchr(start, '/');
1120         if (!end){
1121                 PRINT_WARN("Invalid format for ipato_addx/delx. "
1122                            "Use <ip addr>/<mask bits>\n");
1123                 return -EINVAL;
1124         }
1125         strncpy(buffer, start, end - start);
1126         if (qeth_string_to_ipaddr(buffer, proto, addr)){
1127                 PRINT_WARN("Invalid IP address format!\n");
1128                 return -EINVAL;
1129         }
1130         start = end + 1;
1131         *mask_bits = simple_strtoul(start, &tmp, 10);
1132
1133         return 0;
1134 }
1135
1136 static inline ssize_t
1137 qeth_dev_ipato_add_store(const char *buf, size_t count,
1138                          struct qeth_card *card, enum qeth_prot_versions proto)
1139 {
1140         struct qeth_ipato_entry *ipatoe;
1141         u8 addr[16];
1142         int mask_bits;
1143         int rc;
1144
1145         if (qeth_check_layer2(card))
1146                 return -EPERM;
1147         if ((rc = qeth_parse_ipatoe(buf, proto, addr, &mask_bits)))
1148                 return rc;
1149
1150         if (!(ipatoe = kmalloc(sizeof(struct qeth_ipato_entry), GFP_KERNEL))){
1151                 PRINT_WARN("No memory to allocate ipato entry\n");
1152                 return -ENOMEM;
1153         }
1154         memset(ipatoe, 0, sizeof(struct qeth_ipato_entry));
1155         ipatoe->proto = proto;
1156         memcpy(ipatoe->addr, addr, (proto == QETH_PROT_IPV4)? 4:16);
1157         ipatoe->mask_bits = mask_bits;
1158
1159         if ((rc = qeth_add_ipato_entry(card, ipatoe))){
1160                 kfree(ipatoe);
1161                 return rc;
1162         }
1163
1164         return count;
1165 }
1166
1167 static ssize_t
1168 qeth_dev_ipato_add4_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1169 {
1170         struct qeth_card *card = dev->driver_data;
1171
1172         if (!card)
1173                 return -EINVAL;
1174
1175         return qeth_dev_ipato_add_store(buf, count, card, QETH_PROT_IPV4);
1176 }
1177
1178 static QETH_DEVICE_ATTR(ipato_add4, add4, 0644,
1179                         qeth_dev_ipato_add4_show,
1180                         qeth_dev_ipato_add4_store);
1181
1182 static inline ssize_t
1183 qeth_dev_ipato_del_store(const char *buf, size_t count,
1184                          struct qeth_card *card, enum qeth_prot_versions proto)
1185 {
1186         u8 addr[16];
1187         int mask_bits;
1188         int rc;
1189
1190         if (qeth_check_layer2(card))
1191                 return -EPERM;
1192         if ((rc = qeth_parse_ipatoe(buf, proto, addr, &mask_bits)))
1193                 return rc;
1194
1195         qeth_del_ipato_entry(card, proto, addr, mask_bits);
1196
1197         return count;
1198 }
1199
1200 static ssize_t
1201 qeth_dev_ipato_del4_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1202 {
1203         struct qeth_card *card = dev->driver_data;
1204
1205         if (!card)
1206                 return -EINVAL;
1207
1208         return qeth_dev_ipato_del_store(buf, count, card, QETH_PROT_IPV4);
1209 }
1210
1211 static QETH_DEVICE_ATTR(ipato_del4, del4, 0200, NULL,
1212                         qeth_dev_ipato_del4_store);
1213
1214 #ifdef CONFIG_QETH_IPV6
1215 static ssize_t
1216 qeth_dev_ipato_invert6_show(struct device *dev, struct device_attribute *attr, char *buf)
1217 {
1218         struct qeth_card *card = dev->driver_data;
1219
1220         if (!card)
1221                 return -EINVAL;
1222
1223         if (qeth_check_layer2(card))
1224                 return -EPERM;
1225
1226         return sprintf(buf, "%i\n", card->ipato.invert6? 1:0);
1227 }
1228
1229 static ssize_t
1230 qeth_dev_ipato_invert6_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1231 {
1232         struct qeth_card *card = dev->driver_data;
1233         char *tmp;
1234
1235         if (!card)
1236                 return -EINVAL;
1237
1238         if (qeth_check_layer2(card))
1239                 return -EPERM;
1240
1241         tmp = strsep((char **) &buf, "\n");
1242         if (!strcmp(tmp, "toggle")){
1243                 card->ipato.invert6 = (card->ipato.invert6)? 0 : 1;
1244         } else if (!strcmp(tmp, "1")){
1245                 card->ipato.invert6 = 1;
1246         } else if (!strcmp(tmp, "0")){
1247                 card->ipato.invert6 = 0;
1248         } else {
1249                 PRINT_WARN("ipato_invert6: write 0, 1 or 'toggle' to "
1250                            "this file\n");
1251                 return -EINVAL;
1252         }
1253         return count;
1254 }
1255
1256 static QETH_DEVICE_ATTR(ipato_invert6, invert6, 0644,
1257                         qeth_dev_ipato_invert6_show,
1258                         qeth_dev_ipato_invert6_store);
1259
1260
1261 static ssize_t
1262 qeth_dev_ipato_add6_show(struct device *dev, struct device_attribute *attr, char *buf)
1263 {
1264         struct qeth_card *card = dev->driver_data;
1265
1266         if (!card)
1267                 return -EINVAL;
1268
1269         return qeth_dev_ipato_add_show(buf, card, QETH_PROT_IPV6);
1270 }
1271
1272 static ssize_t
1273 qeth_dev_ipato_add6_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1274 {
1275         struct qeth_card *card = dev->driver_data;
1276
1277         if (!card)
1278                 return -EINVAL;
1279
1280         return qeth_dev_ipato_add_store(buf, count, card, QETH_PROT_IPV6);
1281 }
1282
1283 static QETH_DEVICE_ATTR(ipato_add6, add6, 0644,
1284                         qeth_dev_ipato_add6_show,
1285                         qeth_dev_ipato_add6_store);
1286
1287 static ssize_t
1288 qeth_dev_ipato_del6_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1289 {
1290         struct qeth_card *card = dev->driver_data;
1291
1292         if (!card)
1293                 return -EINVAL;
1294
1295         return qeth_dev_ipato_del_store(buf, count, card, QETH_PROT_IPV6);
1296 }
1297
1298 static QETH_DEVICE_ATTR(ipato_del6, del6, 0200, NULL,
1299                         qeth_dev_ipato_del6_store);
1300 #endif /* CONFIG_QETH_IPV6 */
1301
1302 static struct device_attribute * qeth_ipato_device_attrs[] = {
1303         &dev_attr_ipato_enable,
1304         &dev_attr_ipato_invert4,
1305         &dev_attr_ipato_add4,
1306         &dev_attr_ipato_del4,
1307 #ifdef CONFIG_QETH_IPV6
1308         &dev_attr_ipato_invert6,
1309         &dev_attr_ipato_add6,
1310         &dev_attr_ipato_del6,
1311 #endif
1312         NULL,
1313 };
1314
1315 static struct attribute_group qeth_device_ipato_group = {
1316         .name = "ipa_takeover",
1317         .attrs = (struct attribute **)qeth_ipato_device_attrs,
1318 };
1319
1320 static inline ssize_t
1321 qeth_dev_vipa_add_show(char *buf, struct qeth_card *card,
1322                         enum qeth_prot_versions proto)
1323 {
1324         struct qeth_ipaddr *ipaddr;
1325         char addr_str[40];
1326         int entry_len; /* length of 1 entry string, differs between v4 and v6 */
1327         unsigned long flags;
1328         int i = 0;
1329
1330         if (qeth_check_layer2(card))
1331                 return -EPERM;
1332
1333         entry_len = (proto == QETH_PROT_IPV4)? 12 : 40;
1334         entry_len += 2; /* \n + terminator */
1335         spin_lock_irqsave(&card->ip_lock, flags);
1336         list_for_each_entry(ipaddr, &card->ip_list, entry){
1337                 if (ipaddr->proto != proto)
1338                         continue;
1339                 if (ipaddr->type != QETH_IP_TYPE_VIPA)
1340                         continue;
1341                 /* String must not be longer than PAGE_SIZE. So we check if
1342                  * string length gets near PAGE_SIZE. Then we can savely display
1343                  * the next IPv6 address (worst case, compared to IPv4) */
1344                 if ((PAGE_SIZE - i) <= entry_len)
1345                         break;
1346                 qeth_ipaddr_to_string(proto, (const u8 *)&ipaddr->u, addr_str);
1347                 i += snprintf(buf + i, PAGE_SIZE - i, "%s\n", addr_str);
1348         }
1349         spin_unlock_irqrestore(&card->ip_lock, flags);
1350         i += snprintf(buf + i, PAGE_SIZE - i, "\n");
1351
1352         return i;
1353 }
1354
1355 static ssize_t
1356 qeth_dev_vipa_add4_show(struct device *dev, struct device_attribute *attr, char *buf)
1357 {
1358         struct qeth_card *card = dev->driver_data;
1359
1360         if (!card)
1361                 return -EINVAL;
1362
1363         return qeth_dev_vipa_add_show(buf, card, QETH_PROT_IPV4);
1364 }
1365
1366 static inline int
1367 qeth_parse_vipae(const char* buf, enum qeth_prot_versions proto,
1368                  u8 *addr)
1369 {
1370         if (qeth_string_to_ipaddr(buf, proto, addr)){
1371                 PRINT_WARN("Invalid IP address format!\n");
1372                 return -EINVAL;
1373         }
1374         return 0;
1375 }
1376
1377 static inline ssize_t
1378 qeth_dev_vipa_add_store(const char *buf, size_t count,
1379                          struct qeth_card *card, enum qeth_prot_versions proto)
1380 {
1381         u8 addr[16] = {0, };
1382         int rc;
1383
1384         if (qeth_check_layer2(card))
1385                 return -EPERM;
1386         if ((rc = qeth_parse_vipae(buf, proto, addr)))
1387                 return rc;
1388
1389         if ((rc = qeth_add_vipa(card, proto, addr)))
1390                 return rc;
1391
1392         return count;
1393 }
1394
1395 static ssize_t
1396 qeth_dev_vipa_add4_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1397 {
1398         struct qeth_card *card = dev->driver_data;
1399
1400         if (!card)
1401                 return -EINVAL;
1402
1403         return qeth_dev_vipa_add_store(buf, count, card, QETH_PROT_IPV4);
1404 }
1405
1406 static QETH_DEVICE_ATTR(vipa_add4, add4, 0644,
1407                         qeth_dev_vipa_add4_show,
1408                         qeth_dev_vipa_add4_store);
1409
1410 static inline ssize_t
1411 qeth_dev_vipa_del_store(const char *buf, size_t count,
1412                          struct qeth_card *card, enum qeth_prot_versions proto)
1413 {
1414         u8 addr[16];
1415         int rc;
1416
1417         if (qeth_check_layer2(card))
1418                 return -EPERM;
1419         if ((rc = qeth_parse_vipae(buf, proto, addr)))
1420                 return rc;
1421
1422         qeth_del_vipa(card, proto, addr);
1423
1424         return count;
1425 }
1426
1427 static ssize_t
1428 qeth_dev_vipa_del4_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1429 {
1430         struct qeth_card *card = dev->driver_data;
1431
1432         if (!card)
1433                 return -EINVAL;
1434
1435         return qeth_dev_vipa_del_store(buf, count, card, QETH_PROT_IPV4);
1436 }
1437
1438 static QETH_DEVICE_ATTR(vipa_del4, del4, 0200, NULL,
1439                         qeth_dev_vipa_del4_store);
1440
1441 #ifdef CONFIG_QETH_IPV6
1442 static ssize_t
1443 qeth_dev_vipa_add6_show(struct device *dev, struct device_attribute *attr, char *buf)
1444 {
1445         struct qeth_card *card = dev->driver_data;
1446
1447         if (!card)
1448                 return -EINVAL;
1449
1450         return qeth_dev_vipa_add_show(buf, card, QETH_PROT_IPV6);
1451 }
1452
1453 static ssize_t
1454 qeth_dev_vipa_add6_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1455 {
1456         struct qeth_card *card = dev->driver_data;
1457
1458         if (!card)
1459                 return -EINVAL;
1460
1461         return qeth_dev_vipa_add_store(buf, count, card, QETH_PROT_IPV6);
1462 }
1463
1464 static QETH_DEVICE_ATTR(vipa_add6, add6, 0644,
1465                         qeth_dev_vipa_add6_show,
1466                         qeth_dev_vipa_add6_store);
1467
1468 static ssize_t
1469 qeth_dev_vipa_del6_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1470 {
1471         struct qeth_card *card = dev->driver_data;
1472
1473         if (!card)
1474                 return -EINVAL;
1475
1476         if (qeth_check_layer2(card))
1477                 return -EPERM;
1478
1479         return qeth_dev_vipa_del_store(buf, count, card, QETH_PROT_IPV6);
1480 }
1481
1482 static QETH_DEVICE_ATTR(vipa_del6, del6, 0200, NULL,
1483                         qeth_dev_vipa_del6_store);
1484 #endif /* CONFIG_QETH_IPV6 */
1485
1486 static struct device_attribute * qeth_vipa_device_attrs[] = {
1487         &dev_attr_vipa_add4,
1488         &dev_attr_vipa_del4,
1489 #ifdef CONFIG_QETH_IPV6
1490         &dev_attr_vipa_add6,
1491         &dev_attr_vipa_del6,
1492 #endif
1493         NULL,
1494 };
1495
1496 static struct attribute_group qeth_device_vipa_group = {
1497         .name = "vipa",
1498         .attrs = (struct attribute **)qeth_vipa_device_attrs,
1499 };
1500
1501 static inline ssize_t
1502 qeth_dev_rxip_add_show(char *buf, struct qeth_card *card,
1503                        enum qeth_prot_versions proto)
1504 {
1505         struct qeth_ipaddr *ipaddr;
1506         char addr_str[40];
1507         int entry_len; /* length of 1 entry string, differs between v4 and v6 */
1508         unsigned long flags;
1509         int i = 0;
1510
1511         if (qeth_check_layer2(card))
1512                 return -EPERM;
1513
1514         entry_len = (proto == QETH_PROT_IPV4)? 12 : 40;
1515         entry_len += 2; /* \n + terminator */
1516         spin_lock_irqsave(&card->ip_lock, flags);
1517         list_for_each_entry(ipaddr, &card->ip_list, entry){
1518                 if (ipaddr->proto != proto)
1519                         continue;
1520                 if (ipaddr->type != QETH_IP_TYPE_RXIP)
1521                         continue;
1522                 /* String must not be longer than PAGE_SIZE. So we check if
1523                  * string length gets near PAGE_SIZE. Then we can savely display
1524                  * the next IPv6 address (worst case, compared to IPv4) */
1525                 if ((PAGE_SIZE - i) <= entry_len)
1526                         break;
1527                 qeth_ipaddr_to_string(proto, (const u8 *)&ipaddr->u, addr_str);
1528                 i += snprintf(buf + i, PAGE_SIZE - i, "%s\n", addr_str);
1529         }
1530         spin_unlock_irqrestore(&card->ip_lock, flags);
1531         i += snprintf(buf + i, PAGE_SIZE - i, "\n");
1532
1533         return i;
1534 }
1535
1536 static ssize_t
1537 qeth_dev_rxip_add4_show(struct device *dev, struct device_attribute *attr, char *buf)
1538 {
1539         struct qeth_card *card = dev->driver_data;
1540
1541         if (!card)
1542                 return -EINVAL;
1543
1544         return qeth_dev_rxip_add_show(buf, card, QETH_PROT_IPV4);
1545 }
1546
1547 static inline int
1548 qeth_parse_rxipe(const char* buf, enum qeth_prot_versions proto,
1549                  u8 *addr)
1550 {
1551         if (qeth_string_to_ipaddr(buf, proto, addr)){
1552                 PRINT_WARN("Invalid IP address format!\n");
1553                 return -EINVAL;
1554         }
1555         return 0;
1556 }
1557
1558 static inline ssize_t
1559 qeth_dev_rxip_add_store(const char *buf, size_t count,
1560                         struct qeth_card *card, enum qeth_prot_versions proto)
1561 {
1562         u8 addr[16] = {0, };
1563         int rc;
1564
1565         if (qeth_check_layer2(card))
1566                 return -EPERM;
1567         if ((rc = qeth_parse_rxipe(buf, proto, addr)))
1568                 return rc;
1569
1570         if ((rc = qeth_add_rxip(card, proto, addr)))
1571                 return rc;
1572
1573         return count;
1574 }
1575
1576 static ssize_t
1577 qeth_dev_rxip_add4_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1578 {
1579         struct qeth_card *card = dev->driver_data;
1580
1581         if (!card)
1582                 return -EINVAL;
1583
1584         return qeth_dev_rxip_add_store(buf, count, card, QETH_PROT_IPV4);
1585 }
1586
1587 static QETH_DEVICE_ATTR(rxip_add4, add4, 0644,
1588                         qeth_dev_rxip_add4_show,
1589                         qeth_dev_rxip_add4_store);
1590
1591 static inline ssize_t
1592 qeth_dev_rxip_del_store(const char *buf, size_t count,
1593                         struct qeth_card *card, enum qeth_prot_versions proto)
1594 {
1595         u8 addr[16];
1596         int rc;
1597
1598         if (qeth_check_layer2(card))
1599                 return -EPERM;
1600         if ((rc = qeth_parse_rxipe(buf, proto, addr)))
1601                 return rc;
1602
1603         qeth_del_rxip(card, proto, addr);
1604
1605         return count;
1606 }
1607
1608 static ssize_t
1609 qeth_dev_rxip_del4_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1610 {
1611         struct qeth_card *card = dev->driver_data;
1612
1613         if (!card)
1614                 return -EINVAL;
1615
1616         return qeth_dev_rxip_del_store(buf, count, card, QETH_PROT_IPV4);
1617 }
1618
1619 static QETH_DEVICE_ATTR(rxip_del4, del4, 0200, NULL,
1620                         qeth_dev_rxip_del4_store);
1621
1622 #ifdef CONFIG_QETH_IPV6
1623 static ssize_t
1624 qeth_dev_rxip_add6_show(struct device *dev, struct device_attribute *attr, char *buf)
1625 {
1626         struct qeth_card *card = dev->driver_data;
1627
1628         if (!card)
1629                 return -EINVAL;
1630
1631         return qeth_dev_rxip_add_show(buf, card, QETH_PROT_IPV6);
1632 }
1633
1634 static ssize_t
1635 qeth_dev_rxip_add6_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1636 {
1637         struct qeth_card *card = dev->driver_data;
1638
1639         if (!card)
1640                 return -EINVAL;
1641
1642         return qeth_dev_rxip_add_store(buf, count, card, QETH_PROT_IPV6);
1643 }
1644
1645 static QETH_DEVICE_ATTR(rxip_add6, add6, 0644,
1646                         qeth_dev_rxip_add6_show,
1647                         qeth_dev_rxip_add6_store);
1648
1649 static ssize_t
1650 qeth_dev_rxip_del6_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1651 {
1652         struct qeth_card *card = dev->driver_data;
1653
1654         if (!card)
1655                 return -EINVAL;
1656
1657         return qeth_dev_rxip_del_store(buf, count, card, QETH_PROT_IPV6);
1658 }
1659
1660 static QETH_DEVICE_ATTR(rxip_del6, del6, 0200, NULL,
1661                         qeth_dev_rxip_del6_store);
1662 #endif /* CONFIG_QETH_IPV6 */
1663
1664 static struct device_attribute * qeth_rxip_device_attrs[] = {
1665         &dev_attr_rxip_add4,
1666         &dev_attr_rxip_del4,
1667 #ifdef CONFIG_QETH_IPV6
1668         &dev_attr_rxip_add6,
1669         &dev_attr_rxip_del6,
1670 #endif
1671         NULL,
1672 };
1673
1674 static struct attribute_group qeth_device_rxip_group = {
1675         .name = "rxip",
1676         .attrs = (struct attribute **)qeth_rxip_device_attrs,
1677 };
1678
1679 int
1680 qeth_create_device_attributes(struct device *dev)
1681 {
1682         int ret;
1683         struct qeth_card *card = dev->driver_data;
1684
1685         if (card->info.type == QETH_CARD_TYPE_OSN)
1686                 return sysfs_create_group(&dev->kobj,
1687                                           &qeth_osn_device_attr_group);
1688         
1689         if ((ret = sysfs_create_group(&dev->kobj, &qeth_device_attr_group)))
1690                 return ret;
1691         if ((ret = sysfs_create_group(&dev->kobj, &qeth_device_ipato_group))){
1692                 sysfs_remove_group(&dev->kobj, &qeth_device_attr_group);
1693                 return ret;
1694         }
1695         if ((ret = sysfs_create_group(&dev->kobj, &qeth_device_vipa_group))){
1696                 sysfs_remove_group(&dev->kobj, &qeth_device_attr_group);
1697                 sysfs_remove_group(&dev->kobj, &qeth_device_ipato_group);
1698                 return ret;
1699         }
1700         if ((ret = sysfs_create_group(&dev->kobj, &qeth_device_rxip_group))){
1701                 sysfs_remove_group(&dev->kobj, &qeth_device_attr_group);
1702                 sysfs_remove_group(&dev->kobj, &qeth_device_ipato_group);
1703                 sysfs_remove_group(&dev->kobj, &qeth_device_vipa_group);
1704         }
1705         if ((ret = sysfs_create_group(&dev->kobj, &qeth_device_blkt_group)))
1706                 return ret;
1707
1708         return ret;
1709 }
1710
1711 void
1712 qeth_remove_device_attributes(struct device *dev)
1713 {
1714         struct qeth_card *card = dev->driver_data;
1715
1716         if (card->info.type == QETH_CARD_TYPE_OSN)
1717                 return sysfs_remove_group(&dev->kobj,
1718                                           &qeth_osn_device_attr_group);
1719                       
1720         sysfs_remove_group(&dev->kobj, &qeth_device_attr_group);
1721         sysfs_remove_group(&dev->kobj, &qeth_device_ipato_group);
1722         sysfs_remove_group(&dev->kobj, &qeth_device_vipa_group);
1723         sysfs_remove_group(&dev->kobj, &qeth_device_rxip_group);
1724         sysfs_remove_group(&dev->kobj, &qeth_device_blkt_group);
1725 }
1726
1727 /**********************/
1728 /* DRIVER ATTRIBUTES  */
1729 /**********************/
1730 static ssize_t
1731 qeth_driver_group_store(struct device_driver *ddrv, const char *buf,
1732                         size_t count)
1733 {
1734         const char *start, *end;
1735         char bus_ids[3][BUS_ID_SIZE], *argv[3];
1736         int i;
1737         int err;
1738
1739         start = buf;
1740         for (i = 0; i < 3; i++) {
1741                 static const char delim[] = { ',', ',', '\n' };
1742                 int len;
1743
1744                 if (!(end = strchr(start, delim[i])))
1745                         return -EINVAL;
1746                 len = min_t(ptrdiff_t, BUS_ID_SIZE, end - start);
1747                 strncpy(bus_ids[i], start, len);
1748                 bus_ids[i][len] = '\0';
1749                 start = end + 1;
1750                 argv[i] = bus_ids[i];
1751         }
1752         err = ccwgroup_create(qeth_root_dev, qeth_ccwgroup_driver.driver_id,
1753                         &qeth_ccw_driver, 3, argv);
1754         if (err)
1755                 return err;
1756         else
1757                 return count;
1758 }
1759
1760
1761 static DRIVER_ATTR(group, 0200, 0, qeth_driver_group_store);
1762
1763 static ssize_t
1764 qeth_driver_notifier_register_store(struct device_driver *ddrv, const char *buf,
1765                                 size_t count)
1766 {
1767         int rc;
1768         int signum;
1769         char *tmp, *tmp2;
1770
1771         tmp = strsep((char **) &buf, "\n");
1772         if (!strncmp(tmp, "unregister", 10)){
1773                 if ((rc = qeth_notifier_unregister(current)))
1774                         return rc;
1775                 return count;
1776         }
1777
1778         signum = simple_strtoul(tmp, &tmp2, 10);
1779         if ((signum < 0) || (signum > 32)){
1780                 PRINT_WARN("Signal number %d is out of range\n", signum);
1781                 return -EINVAL;
1782         }
1783         if ((rc = qeth_notifier_register(current, signum)))
1784                 return rc;
1785
1786         return count;
1787 }
1788
1789 static DRIVER_ATTR(notifier_register, 0200, 0,
1790                    qeth_driver_notifier_register_store);
1791
1792 int
1793 qeth_create_driver_attributes(void)
1794 {
1795         int rc;
1796
1797         if ((rc = driver_create_file(&qeth_ccwgroup_driver.driver,
1798                                      &driver_attr_group)))
1799                 return rc;
1800         return driver_create_file(&qeth_ccwgroup_driver.driver,
1801                                   &driver_attr_notifier_register);
1802 }
1803
1804 void
1805 qeth_remove_driver_attributes(void)
1806 {
1807         driver_remove_file(&qeth_ccwgroup_driver.driver,
1808                         &driver_attr_group);
1809         driver_remove_file(&qeth_ccwgroup_driver.driver,
1810                         &driver_attr_notifier_register);
1811 }