[PATCH] s390: TSO related fixes in qeth driver
[linux-2.6] / drivers / s390 / net / qeth_sys.c
1 /*
2  *
3  * linux/drivers/s390/net/qeth_sys.c ($Revision: 1.53 $)
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.53 $";
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
726         if (((card->state != CARD_STATE_DOWN) &&
727              (card->state != CARD_STATE_RECOVER)) ||
728             (card->info.type != QETH_CARD_TYPE_OSAE))
729                 return -EPERM;
730
731         i = simple_strtoul(buf, &tmp, 16);
732         if ((i == 0) || (i == 1))
733                 card->options.layer2 = i;
734         else {
735                 PRINT_WARN("layer2: write 0 or 1 to this file!\n");
736                 return -EINVAL;
737         }
738         return count;
739 }
740
741 static DEVICE_ATTR(layer2, 0644, qeth_dev_layer2_show,
742                    qeth_dev_layer2_store);
743
744 static ssize_t
745 qeth_dev_large_send_show(struct device *dev, struct device_attribute *attr, char *buf)
746 {
747         struct qeth_card *card = dev->driver_data;
748
749         if (!card)
750                 return -EINVAL;
751
752         switch (card->options.large_send) {
753         case QETH_LARGE_SEND_NO:
754                 return sprintf(buf, "%s\n", "no");
755         case QETH_LARGE_SEND_EDDP:
756                 return sprintf(buf, "%s\n", "EDDP");
757         case QETH_LARGE_SEND_TSO:
758                 return sprintf(buf, "%s\n", "TSO");
759         default:
760                 return sprintf(buf, "%s\n", "N/A");
761         }
762 }
763
764 static ssize_t
765 qeth_dev_large_send_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
766 {
767         struct qeth_card *card = dev->driver_data;
768         enum qeth_large_send_types type;
769         int rc = 0;
770         char *tmp;
771
772         if (!card)
773                 return -EINVAL;
774         tmp = strsep((char **) &buf, "\n");
775         if (!strcmp(tmp, "no")){
776                 type = QETH_LARGE_SEND_NO;
777         } else if (!strcmp(tmp, "EDDP")) {
778                 type = QETH_LARGE_SEND_EDDP;
779         } else if (!strcmp(tmp, "TSO")) {
780                 type = QETH_LARGE_SEND_TSO;
781         } else {
782                 PRINT_WARN("large_send: invalid mode %s!\n", tmp);
783                 return -EINVAL;
784         }
785         if (card->options.large_send == type)
786                 return count;
787         if ((rc = qeth_set_large_send(card, type)))     
788                 return rc;
789         return count;
790 }
791
792 static DEVICE_ATTR(large_send, 0644, qeth_dev_large_send_show,
793                    qeth_dev_large_send_store);
794
795 static ssize_t
796 qeth_dev_blkt_show(char *buf, struct qeth_card *card, int value )
797 {
798
799         if (!card)
800                 return -EINVAL;
801
802         return sprintf(buf, "%i\n", value);
803 }
804
805 static ssize_t
806 qeth_dev_blkt_store(struct qeth_card *card, const char *buf, size_t count,
807                           int *value, int max_value)
808 {
809         char *tmp;
810         int i;
811
812         if (!card)
813                 return -EINVAL;
814
815         if ((card->state != CARD_STATE_DOWN) &&
816             (card->state != CARD_STATE_RECOVER))
817                 return -EPERM;
818
819         i = simple_strtoul(buf, &tmp, 10);
820         if (i <= max_value) {
821                 *value = i;
822         } else {
823                 PRINT_WARN("blkt total time: write values between"
824                            " 0 and %d to this file!\n", max_value);
825                 return -EINVAL;
826         }
827         return count;
828 }
829
830 static ssize_t
831 qeth_dev_blkt_total_show(struct device *dev, struct device_attribute *attr, char *buf)
832 {
833         struct qeth_card *card = dev->driver_data;
834
835         return qeth_dev_blkt_show(buf, card, card->info.blkt.time_total);
836 }
837
838
839 static ssize_t
840 qeth_dev_blkt_total_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
841 {
842         struct qeth_card *card = dev->driver_data;
843
844         return qeth_dev_blkt_store(card, buf, count,
845                                    &card->info.blkt.time_total,1000);
846 }
847
848
849
850 static DEVICE_ATTR(total, 0644, qeth_dev_blkt_total_show,
851                    qeth_dev_blkt_total_store);
852
853 static ssize_t
854 qeth_dev_blkt_inter_show(struct device *dev, struct device_attribute *attr, char *buf)
855 {
856         struct qeth_card *card = dev->driver_data;
857
858         return qeth_dev_blkt_show(buf, card, card->info.blkt.inter_packet);
859 }
860
861
862 static ssize_t
863 qeth_dev_blkt_inter_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
864 {
865         struct qeth_card *card = dev->driver_data;
866
867         return qeth_dev_blkt_store(card, buf, count,
868                                    &card->info.blkt.inter_packet,100);
869 }
870
871 static DEVICE_ATTR(inter, 0644, qeth_dev_blkt_inter_show,
872                    qeth_dev_blkt_inter_store);
873
874 static ssize_t
875 qeth_dev_blkt_inter_jumbo_show(struct device *dev, struct device_attribute *attr, char *buf)
876 {
877         struct qeth_card *card = dev->driver_data;
878
879         return qeth_dev_blkt_show(buf, card,
880                                   card->info.blkt.inter_packet_jumbo);
881 }
882
883
884 static ssize_t
885 qeth_dev_blkt_inter_jumbo_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
886 {
887         struct qeth_card *card = dev->driver_data;
888
889         return qeth_dev_blkt_store(card, buf, count,
890                                    &card->info.blkt.inter_packet_jumbo,100);
891 }
892
893 static DEVICE_ATTR(inter_jumbo, 0644, qeth_dev_blkt_inter_jumbo_show,
894                    qeth_dev_blkt_inter_jumbo_store);
895
896 static struct device_attribute * qeth_blkt_device_attrs[] = {
897         &dev_attr_total,
898         &dev_attr_inter,
899         &dev_attr_inter_jumbo,
900         NULL,
901 };
902
903 static struct attribute_group qeth_device_blkt_group = {
904         .name = "blkt",
905         .attrs = (struct attribute **)qeth_blkt_device_attrs,
906 };
907
908 static struct device_attribute * qeth_device_attrs[] = {
909         &dev_attr_state,
910         &dev_attr_chpid,
911         &dev_attr_if_name,
912         &dev_attr_card_type,
913         &dev_attr_portno,
914         &dev_attr_portname,
915         &dev_attr_checksumming,
916         &dev_attr_priority_queueing,
917         &dev_attr_buffer_count,
918         &dev_attr_route4,
919 #ifdef CONFIG_QETH_IPV6
920         &dev_attr_route6,
921 #endif
922         &dev_attr_add_hhlen,
923         &dev_attr_fake_ll,
924         &dev_attr_fake_broadcast,
925         &dev_attr_recover,
926         &dev_attr_broadcast_mode,
927         &dev_attr_canonical_macaddr,
928         &dev_attr_layer2,
929         &dev_attr_large_send,
930         NULL,
931 };
932
933 static struct attribute_group qeth_device_attr_group = {
934         .attrs = (struct attribute **)qeth_device_attrs,
935 };
936
937
938 #define QETH_DEVICE_ATTR(_id,_name,_mode,_show,_store)                       \
939 struct device_attribute dev_attr_##_id = {                                   \
940         .attr = {.name=__stringify(_name), .mode=_mode, .owner=THIS_MODULE },\
941         .show   = _show,                                                     \
942         .store  = _store,                                                    \
943 };
944
945 int
946 qeth_check_layer2(struct qeth_card *card)
947 {
948         if (card->options.layer2)
949                 return -EPERM;
950         return 0;
951 }
952
953
954 static ssize_t
955 qeth_dev_ipato_enable_show(struct device *dev, struct device_attribute *attr, char *buf)
956 {
957         struct qeth_card *card = dev->driver_data;
958
959         if (!card)
960                 return -EINVAL;
961
962         if (qeth_check_layer2(card))
963                 return -EPERM;
964         return sprintf(buf, "%i\n", card->ipato.enabled? 1:0);
965 }
966
967 static ssize_t
968 qeth_dev_ipato_enable_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
969 {
970         struct qeth_card *card = dev->driver_data;
971         char *tmp;
972
973         if (!card)
974                 return -EINVAL;
975
976         if ((card->state != CARD_STATE_DOWN) &&
977             (card->state != CARD_STATE_RECOVER))
978                 return -EPERM;
979
980         if (qeth_check_layer2(card))
981                 return -EPERM;
982
983         tmp = strsep((char **) &buf, "\n");
984         if (!strcmp(tmp, "toggle")){
985                 card->ipato.enabled = (card->ipato.enabled)? 0 : 1;
986         } else if (!strcmp(tmp, "1")){
987                 card->ipato.enabled = 1;
988         } else if (!strcmp(tmp, "0")){
989                 card->ipato.enabled = 0;
990         } else {
991                 PRINT_WARN("ipato_enable: write 0, 1 or 'toggle' to "
992                            "this file\n");
993                 return -EINVAL;
994         }
995         return count;
996 }
997
998 static QETH_DEVICE_ATTR(ipato_enable, enable, 0644,
999                         qeth_dev_ipato_enable_show,
1000                         qeth_dev_ipato_enable_store);
1001
1002 static ssize_t
1003 qeth_dev_ipato_invert4_show(struct device *dev, struct device_attribute *attr, char *buf)
1004 {
1005         struct qeth_card *card = dev->driver_data;
1006
1007         if (!card)
1008                 return -EINVAL;
1009
1010         if (qeth_check_layer2(card))
1011                 return -EPERM;
1012
1013         return sprintf(buf, "%i\n", card->ipato.invert4? 1:0);
1014 }
1015
1016 static ssize_t
1017 qeth_dev_ipato_invert4_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1018 {
1019         struct qeth_card *card = dev->driver_data;
1020         char *tmp;
1021
1022         if (!card)
1023                 return -EINVAL;
1024
1025         if (qeth_check_layer2(card))
1026                 return -EPERM;
1027
1028         tmp = strsep((char **) &buf, "\n");
1029         if (!strcmp(tmp, "toggle")){
1030                 card->ipato.invert4 = (card->ipato.invert4)? 0 : 1;
1031         } else if (!strcmp(tmp, "1")){
1032                 card->ipato.invert4 = 1;
1033         } else if (!strcmp(tmp, "0")){
1034                 card->ipato.invert4 = 0;
1035         } else {
1036                 PRINT_WARN("ipato_invert4: write 0, 1 or 'toggle' to "
1037                            "this file\n");
1038                 return -EINVAL;
1039         }
1040         return count;
1041 }
1042
1043 static QETH_DEVICE_ATTR(ipato_invert4, invert4, 0644,
1044                         qeth_dev_ipato_invert4_show,
1045                         qeth_dev_ipato_invert4_store);
1046
1047 static inline ssize_t
1048 qeth_dev_ipato_add_show(char *buf, struct qeth_card *card,
1049                         enum qeth_prot_versions proto)
1050 {
1051         struct qeth_ipato_entry *ipatoe;
1052         unsigned long flags;
1053         char addr_str[40];
1054         int entry_len; /* length of 1 entry string, differs between v4 and v6 */
1055         int i = 0;
1056
1057         if (qeth_check_layer2(card))
1058                 return -EPERM;
1059
1060         entry_len = (proto == QETH_PROT_IPV4)? 12 : 40;
1061         /* add strlen for "/<mask>\n" */
1062         entry_len += (proto == QETH_PROT_IPV4)? 5 : 6;
1063         spin_lock_irqsave(&card->ip_lock, flags);
1064         list_for_each_entry(ipatoe, &card->ipato.entries, entry){
1065                 if (ipatoe->proto != proto)
1066                         continue;
1067                 /* String must not be longer than PAGE_SIZE. So we check if
1068                  * string length gets near PAGE_SIZE. Then we can savely display
1069                  * the next IPv6 address (worst case, compared to IPv4) */
1070                 if ((PAGE_SIZE - i) <= entry_len)
1071                         break;
1072                 qeth_ipaddr_to_string(proto, ipatoe->addr, addr_str);
1073                 i += snprintf(buf + i, PAGE_SIZE - i,
1074                               "%s/%i\n", addr_str, ipatoe->mask_bits);
1075         }
1076         spin_unlock_irqrestore(&card->ip_lock, flags);
1077         i += snprintf(buf + i, PAGE_SIZE - i, "\n");
1078
1079         return i;
1080 }
1081
1082 static ssize_t
1083 qeth_dev_ipato_add4_show(struct device *dev, struct device_attribute *attr, char *buf)
1084 {
1085         struct qeth_card *card = dev->driver_data;
1086
1087         if (!card)
1088                 return -EINVAL;
1089
1090         return qeth_dev_ipato_add_show(buf, card, QETH_PROT_IPV4);
1091 }
1092
1093 static inline int
1094 qeth_parse_ipatoe(const char* buf, enum qeth_prot_versions proto,
1095                   u8 *addr, int *mask_bits)
1096 {
1097         const char *start, *end;
1098         char *tmp;
1099         char buffer[49] = {0, };
1100
1101         start = buf;
1102         /* get address string */
1103         end = strchr(start, '/');
1104         if (!end){
1105                 PRINT_WARN("Invalid format for ipato_addx/delx. "
1106                            "Use <ip addr>/<mask bits>\n");
1107                 return -EINVAL;
1108         }
1109         strncpy(buffer, start, end - start);
1110         if (qeth_string_to_ipaddr(buffer, proto, addr)){
1111                 PRINT_WARN("Invalid IP address format!\n");
1112                 return -EINVAL;
1113         }
1114         start = end + 1;
1115         *mask_bits = simple_strtoul(start, &tmp, 10);
1116
1117         return 0;
1118 }
1119
1120 static inline ssize_t
1121 qeth_dev_ipato_add_store(const char *buf, size_t count,
1122                          struct qeth_card *card, enum qeth_prot_versions proto)
1123 {
1124         struct qeth_ipato_entry *ipatoe;
1125         u8 addr[16];
1126         int mask_bits;
1127         int rc;
1128
1129         if (qeth_check_layer2(card))
1130                 return -EPERM;
1131         if ((rc = qeth_parse_ipatoe(buf, proto, addr, &mask_bits)))
1132                 return rc;
1133
1134         if (!(ipatoe = kmalloc(sizeof(struct qeth_ipato_entry), GFP_KERNEL))){
1135                 PRINT_WARN("No memory to allocate ipato entry\n");
1136                 return -ENOMEM;
1137         }
1138         memset(ipatoe, 0, sizeof(struct qeth_ipato_entry));
1139         ipatoe->proto = proto;
1140         memcpy(ipatoe->addr, addr, (proto == QETH_PROT_IPV4)? 4:16);
1141         ipatoe->mask_bits = mask_bits;
1142
1143         if ((rc = qeth_add_ipato_entry(card, ipatoe))){
1144                 kfree(ipatoe);
1145                 return rc;
1146         }
1147
1148         return count;
1149 }
1150
1151 static ssize_t
1152 qeth_dev_ipato_add4_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1153 {
1154         struct qeth_card *card = dev->driver_data;
1155
1156         if (!card)
1157                 return -EINVAL;
1158
1159         return qeth_dev_ipato_add_store(buf, count, card, QETH_PROT_IPV4);
1160 }
1161
1162 static QETH_DEVICE_ATTR(ipato_add4, add4, 0644,
1163                         qeth_dev_ipato_add4_show,
1164                         qeth_dev_ipato_add4_store);
1165
1166 static inline ssize_t
1167 qeth_dev_ipato_del_store(const char *buf, size_t count,
1168                          struct qeth_card *card, enum qeth_prot_versions proto)
1169 {
1170         u8 addr[16];
1171         int mask_bits;
1172         int rc;
1173
1174         if (qeth_check_layer2(card))
1175                 return -EPERM;
1176         if ((rc = qeth_parse_ipatoe(buf, proto, addr, &mask_bits)))
1177                 return rc;
1178
1179         qeth_del_ipato_entry(card, proto, addr, mask_bits);
1180
1181         return count;
1182 }
1183
1184 static ssize_t
1185 qeth_dev_ipato_del4_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1186 {
1187         struct qeth_card *card = dev->driver_data;
1188
1189         if (!card)
1190                 return -EINVAL;
1191
1192         return qeth_dev_ipato_del_store(buf, count, card, QETH_PROT_IPV4);
1193 }
1194
1195 static QETH_DEVICE_ATTR(ipato_del4, del4, 0200, NULL,
1196                         qeth_dev_ipato_del4_store);
1197
1198 #ifdef CONFIG_QETH_IPV6
1199 static ssize_t
1200 qeth_dev_ipato_invert6_show(struct device *dev, struct device_attribute *attr, char *buf)
1201 {
1202         struct qeth_card *card = dev->driver_data;
1203
1204         if (!card)
1205                 return -EINVAL;
1206
1207         if (qeth_check_layer2(card))
1208                 return -EPERM;
1209
1210         return sprintf(buf, "%i\n", card->ipato.invert6? 1:0);
1211 }
1212
1213 static ssize_t
1214 qeth_dev_ipato_invert6_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1215 {
1216         struct qeth_card *card = dev->driver_data;
1217         char *tmp;
1218
1219         if (!card)
1220                 return -EINVAL;
1221
1222         if (qeth_check_layer2(card))
1223                 return -EPERM;
1224
1225         tmp = strsep((char **) &buf, "\n");
1226         if (!strcmp(tmp, "toggle")){
1227                 card->ipato.invert6 = (card->ipato.invert6)? 0 : 1;
1228         } else if (!strcmp(tmp, "1")){
1229                 card->ipato.invert6 = 1;
1230         } else if (!strcmp(tmp, "0")){
1231                 card->ipato.invert6 = 0;
1232         } else {
1233                 PRINT_WARN("ipato_invert6: write 0, 1 or 'toggle' to "
1234                            "this file\n");
1235                 return -EINVAL;
1236         }
1237         return count;
1238 }
1239
1240 static QETH_DEVICE_ATTR(ipato_invert6, invert6, 0644,
1241                         qeth_dev_ipato_invert6_show,
1242                         qeth_dev_ipato_invert6_store);
1243
1244
1245 static ssize_t
1246 qeth_dev_ipato_add6_show(struct device *dev, struct device_attribute *attr, char *buf)
1247 {
1248         struct qeth_card *card = dev->driver_data;
1249
1250         if (!card)
1251                 return -EINVAL;
1252
1253         return qeth_dev_ipato_add_show(buf, card, QETH_PROT_IPV6);
1254 }
1255
1256 static ssize_t
1257 qeth_dev_ipato_add6_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1258 {
1259         struct qeth_card *card = dev->driver_data;
1260
1261         if (!card)
1262                 return -EINVAL;
1263
1264         return qeth_dev_ipato_add_store(buf, count, card, QETH_PROT_IPV6);
1265 }
1266
1267 static QETH_DEVICE_ATTR(ipato_add6, add6, 0644,
1268                         qeth_dev_ipato_add6_show,
1269                         qeth_dev_ipato_add6_store);
1270
1271 static ssize_t
1272 qeth_dev_ipato_del6_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1273 {
1274         struct qeth_card *card = dev->driver_data;
1275
1276         if (!card)
1277                 return -EINVAL;
1278
1279         return qeth_dev_ipato_del_store(buf, count, card, QETH_PROT_IPV6);
1280 }
1281
1282 static QETH_DEVICE_ATTR(ipato_del6, del6, 0200, NULL,
1283                         qeth_dev_ipato_del6_store);
1284 #endif /* CONFIG_QETH_IPV6 */
1285
1286 static struct device_attribute * qeth_ipato_device_attrs[] = {
1287         &dev_attr_ipato_enable,
1288         &dev_attr_ipato_invert4,
1289         &dev_attr_ipato_add4,
1290         &dev_attr_ipato_del4,
1291 #ifdef CONFIG_QETH_IPV6
1292         &dev_attr_ipato_invert6,
1293         &dev_attr_ipato_add6,
1294         &dev_attr_ipato_del6,
1295 #endif
1296         NULL,
1297 };
1298
1299 static struct attribute_group qeth_device_ipato_group = {
1300         .name = "ipa_takeover",
1301         .attrs = (struct attribute **)qeth_ipato_device_attrs,
1302 };
1303
1304 static inline ssize_t
1305 qeth_dev_vipa_add_show(char *buf, struct qeth_card *card,
1306                         enum qeth_prot_versions proto)
1307 {
1308         struct qeth_ipaddr *ipaddr;
1309         char addr_str[40];
1310         int entry_len; /* length of 1 entry string, differs between v4 and v6 */
1311         unsigned long flags;
1312         int i = 0;
1313
1314         if (qeth_check_layer2(card))
1315                 return -EPERM;
1316
1317         entry_len = (proto == QETH_PROT_IPV4)? 12 : 40;
1318         entry_len += 2; /* \n + terminator */
1319         spin_lock_irqsave(&card->ip_lock, flags);
1320         list_for_each_entry(ipaddr, &card->ip_list, entry){
1321                 if (ipaddr->proto != proto)
1322                         continue;
1323                 if (ipaddr->type != QETH_IP_TYPE_VIPA)
1324                         continue;
1325                 /* String must not be longer than PAGE_SIZE. So we check if
1326                  * string length gets near PAGE_SIZE. Then we can savely display
1327                  * the next IPv6 address (worst case, compared to IPv4) */
1328                 if ((PAGE_SIZE - i) <= entry_len)
1329                         break;
1330                 qeth_ipaddr_to_string(proto, (const u8 *)&ipaddr->u, addr_str);
1331                 i += snprintf(buf + i, PAGE_SIZE - i, "%s\n", addr_str);
1332         }
1333         spin_unlock_irqrestore(&card->ip_lock, flags);
1334         i += snprintf(buf + i, PAGE_SIZE - i, "\n");
1335
1336         return i;
1337 }
1338
1339 static ssize_t
1340 qeth_dev_vipa_add4_show(struct device *dev, struct device_attribute *attr, char *buf)
1341 {
1342         struct qeth_card *card = dev->driver_data;
1343
1344         if (!card)
1345                 return -EINVAL;
1346
1347         return qeth_dev_vipa_add_show(buf, card, QETH_PROT_IPV4);
1348 }
1349
1350 static inline int
1351 qeth_parse_vipae(const char* buf, enum qeth_prot_versions proto,
1352                  u8 *addr)
1353 {
1354         if (qeth_string_to_ipaddr(buf, proto, addr)){
1355                 PRINT_WARN("Invalid IP address format!\n");
1356                 return -EINVAL;
1357         }
1358         return 0;
1359 }
1360
1361 static inline ssize_t
1362 qeth_dev_vipa_add_store(const char *buf, size_t count,
1363                          struct qeth_card *card, enum qeth_prot_versions proto)
1364 {
1365         u8 addr[16] = {0, };
1366         int rc;
1367
1368         if (qeth_check_layer2(card))
1369                 return -EPERM;
1370         if ((rc = qeth_parse_vipae(buf, proto, addr)))
1371                 return rc;
1372
1373         if ((rc = qeth_add_vipa(card, proto, addr)))
1374                 return rc;
1375
1376         return count;
1377 }
1378
1379 static ssize_t
1380 qeth_dev_vipa_add4_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1381 {
1382         struct qeth_card *card = dev->driver_data;
1383
1384         if (!card)
1385                 return -EINVAL;
1386
1387         return qeth_dev_vipa_add_store(buf, count, card, QETH_PROT_IPV4);
1388 }
1389
1390 static QETH_DEVICE_ATTR(vipa_add4, add4, 0644,
1391                         qeth_dev_vipa_add4_show,
1392                         qeth_dev_vipa_add4_store);
1393
1394 static inline ssize_t
1395 qeth_dev_vipa_del_store(const char *buf, size_t count,
1396                          struct qeth_card *card, enum qeth_prot_versions proto)
1397 {
1398         u8 addr[16];
1399         int rc;
1400
1401         if (qeth_check_layer2(card))
1402                 return -EPERM;
1403         if ((rc = qeth_parse_vipae(buf, proto, addr)))
1404                 return rc;
1405
1406         qeth_del_vipa(card, proto, addr);
1407
1408         return count;
1409 }
1410
1411 static ssize_t
1412 qeth_dev_vipa_del4_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1413 {
1414         struct qeth_card *card = dev->driver_data;
1415
1416         if (!card)
1417                 return -EINVAL;
1418
1419         return qeth_dev_vipa_del_store(buf, count, card, QETH_PROT_IPV4);
1420 }
1421
1422 static QETH_DEVICE_ATTR(vipa_del4, del4, 0200, NULL,
1423                         qeth_dev_vipa_del4_store);
1424
1425 #ifdef CONFIG_QETH_IPV6
1426 static ssize_t
1427 qeth_dev_vipa_add6_show(struct device *dev, struct device_attribute *attr, char *buf)
1428 {
1429         struct qeth_card *card = dev->driver_data;
1430
1431         if (!card)
1432                 return -EINVAL;
1433
1434         return qeth_dev_vipa_add_show(buf, card, QETH_PROT_IPV6);
1435 }
1436
1437 static ssize_t
1438 qeth_dev_vipa_add6_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1439 {
1440         struct qeth_card *card = dev->driver_data;
1441
1442         if (!card)
1443                 return -EINVAL;
1444
1445         return qeth_dev_vipa_add_store(buf, count, card, QETH_PROT_IPV6);
1446 }
1447
1448 static QETH_DEVICE_ATTR(vipa_add6, add6, 0644,
1449                         qeth_dev_vipa_add6_show,
1450                         qeth_dev_vipa_add6_store);
1451
1452 static ssize_t
1453 qeth_dev_vipa_del6_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1454 {
1455         struct qeth_card *card = dev->driver_data;
1456
1457         if (!card)
1458                 return -EINVAL;
1459
1460         if (qeth_check_layer2(card))
1461                 return -EPERM;
1462
1463         return qeth_dev_vipa_del_store(buf, count, card, QETH_PROT_IPV6);
1464 }
1465
1466 static QETH_DEVICE_ATTR(vipa_del6, del6, 0200, NULL,
1467                         qeth_dev_vipa_del6_store);
1468 #endif /* CONFIG_QETH_IPV6 */
1469
1470 static struct device_attribute * qeth_vipa_device_attrs[] = {
1471         &dev_attr_vipa_add4,
1472         &dev_attr_vipa_del4,
1473 #ifdef CONFIG_QETH_IPV6
1474         &dev_attr_vipa_add6,
1475         &dev_attr_vipa_del6,
1476 #endif
1477         NULL,
1478 };
1479
1480 static struct attribute_group qeth_device_vipa_group = {
1481         .name = "vipa",
1482         .attrs = (struct attribute **)qeth_vipa_device_attrs,
1483 };
1484
1485 static inline ssize_t
1486 qeth_dev_rxip_add_show(char *buf, struct qeth_card *card,
1487                        enum qeth_prot_versions proto)
1488 {
1489         struct qeth_ipaddr *ipaddr;
1490         char addr_str[40];
1491         int entry_len; /* length of 1 entry string, differs between v4 and v6 */
1492         unsigned long flags;
1493         int i = 0;
1494
1495         if (qeth_check_layer2(card))
1496                 return -EPERM;
1497
1498         entry_len = (proto == QETH_PROT_IPV4)? 12 : 40;
1499         entry_len += 2; /* \n + terminator */
1500         spin_lock_irqsave(&card->ip_lock, flags);
1501         list_for_each_entry(ipaddr, &card->ip_list, entry){
1502                 if (ipaddr->proto != proto)
1503                         continue;
1504                 if (ipaddr->type != QETH_IP_TYPE_RXIP)
1505                         continue;
1506                 /* String must not be longer than PAGE_SIZE. So we check if
1507                  * string length gets near PAGE_SIZE. Then we can savely display
1508                  * the next IPv6 address (worst case, compared to IPv4) */
1509                 if ((PAGE_SIZE - i) <= entry_len)
1510                         break;
1511                 qeth_ipaddr_to_string(proto, (const u8 *)&ipaddr->u, addr_str);
1512                 i += snprintf(buf + i, PAGE_SIZE - i, "%s\n", addr_str);
1513         }
1514         spin_unlock_irqrestore(&card->ip_lock, flags);
1515         i += snprintf(buf + i, PAGE_SIZE - i, "\n");
1516
1517         return i;
1518 }
1519
1520 static ssize_t
1521 qeth_dev_rxip_add4_show(struct device *dev, struct device_attribute *attr, char *buf)
1522 {
1523         struct qeth_card *card = dev->driver_data;
1524
1525         if (!card)
1526                 return -EINVAL;
1527
1528         return qeth_dev_rxip_add_show(buf, card, QETH_PROT_IPV4);
1529 }
1530
1531 static inline int
1532 qeth_parse_rxipe(const char* buf, enum qeth_prot_versions proto,
1533                  u8 *addr)
1534 {
1535         if (qeth_string_to_ipaddr(buf, proto, addr)){
1536                 PRINT_WARN("Invalid IP address format!\n");
1537                 return -EINVAL;
1538         }
1539         return 0;
1540 }
1541
1542 static inline ssize_t
1543 qeth_dev_rxip_add_store(const char *buf, size_t count,
1544                         struct qeth_card *card, enum qeth_prot_versions proto)
1545 {
1546         u8 addr[16] = {0, };
1547         int rc;
1548
1549         if (qeth_check_layer2(card))
1550                 return -EPERM;
1551         if ((rc = qeth_parse_rxipe(buf, proto, addr)))
1552                 return rc;
1553
1554         if ((rc = qeth_add_rxip(card, proto, addr)))
1555                 return rc;
1556
1557         return count;
1558 }
1559
1560 static ssize_t
1561 qeth_dev_rxip_add4_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1562 {
1563         struct qeth_card *card = dev->driver_data;
1564
1565         if (!card)
1566                 return -EINVAL;
1567
1568         return qeth_dev_rxip_add_store(buf, count, card, QETH_PROT_IPV4);
1569 }
1570
1571 static QETH_DEVICE_ATTR(rxip_add4, add4, 0644,
1572                         qeth_dev_rxip_add4_show,
1573                         qeth_dev_rxip_add4_store);
1574
1575 static inline ssize_t
1576 qeth_dev_rxip_del_store(const char *buf, size_t count,
1577                         struct qeth_card *card, enum qeth_prot_versions proto)
1578 {
1579         u8 addr[16];
1580         int rc;
1581
1582         if (qeth_check_layer2(card))
1583                 return -EPERM;
1584         if ((rc = qeth_parse_rxipe(buf, proto, addr)))
1585                 return rc;
1586
1587         qeth_del_rxip(card, proto, addr);
1588
1589         return count;
1590 }
1591
1592 static ssize_t
1593 qeth_dev_rxip_del4_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1594 {
1595         struct qeth_card *card = dev->driver_data;
1596
1597         if (!card)
1598                 return -EINVAL;
1599
1600         return qeth_dev_rxip_del_store(buf, count, card, QETH_PROT_IPV4);
1601 }
1602
1603 static QETH_DEVICE_ATTR(rxip_del4, del4, 0200, NULL,
1604                         qeth_dev_rxip_del4_store);
1605
1606 #ifdef CONFIG_QETH_IPV6
1607 static ssize_t
1608 qeth_dev_rxip_add6_show(struct device *dev, struct device_attribute *attr, char *buf)
1609 {
1610         struct qeth_card *card = dev->driver_data;
1611
1612         if (!card)
1613                 return -EINVAL;
1614
1615         return qeth_dev_rxip_add_show(buf, card, QETH_PROT_IPV6);
1616 }
1617
1618 static ssize_t
1619 qeth_dev_rxip_add6_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1620 {
1621         struct qeth_card *card = dev->driver_data;
1622
1623         if (!card)
1624                 return -EINVAL;
1625
1626         return qeth_dev_rxip_add_store(buf, count, card, QETH_PROT_IPV6);
1627 }
1628
1629 static QETH_DEVICE_ATTR(rxip_add6, add6, 0644,
1630                         qeth_dev_rxip_add6_show,
1631                         qeth_dev_rxip_add6_store);
1632
1633 static ssize_t
1634 qeth_dev_rxip_del6_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1635 {
1636         struct qeth_card *card = dev->driver_data;
1637
1638         if (!card)
1639                 return -EINVAL;
1640
1641         return qeth_dev_rxip_del_store(buf, count, card, QETH_PROT_IPV6);
1642 }
1643
1644 static QETH_DEVICE_ATTR(rxip_del6, del6, 0200, NULL,
1645                         qeth_dev_rxip_del6_store);
1646 #endif /* CONFIG_QETH_IPV6 */
1647
1648 static struct device_attribute * qeth_rxip_device_attrs[] = {
1649         &dev_attr_rxip_add4,
1650         &dev_attr_rxip_del4,
1651 #ifdef CONFIG_QETH_IPV6
1652         &dev_attr_rxip_add6,
1653         &dev_attr_rxip_del6,
1654 #endif
1655         NULL,
1656 };
1657
1658 static struct attribute_group qeth_device_rxip_group = {
1659         .name = "rxip",
1660         .attrs = (struct attribute **)qeth_rxip_device_attrs,
1661 };
1662
1663 int
1664 qeth_create_device_attributes(struct device *dev)
1665 {
1666         int ret;
1667
1668         if ((ret = sysfs_create_group(&dev->kobj, &qeth_device_attr_group)))
1669                 return ret;
1670         if ((ret = sysfs_create_group(&dev->kobj, &qeth_device_ipato_group))){
1671                 sysfs_remove_group(&dev->kobj, &qeth_device_attr_group);
1672                 return ret;
1673         }
1674         if ((ret = sysfs_create_group(&dev->kobj, &qeth_device_vipa_group))){
1675                 sysfs_remove_group(&dev->kobj, &qeth_device_attr_group);
1676                 sysfs_remove_group(&dev->kobj, &qeth_device_ipato_group);
1677                 return ret;
1678         }
1679         if ((ret = sysfs_create_group(&dev->kobj, &qeth_device_rxip_group))){
1680                 sysfs_remove_group(&dev->kobj, &qeth_device_attr_group);
1681                 sysfs_remove_group(&dev->kobj, &qeth_device_ipato_group);
1682                 sysfs_remove_group(&dev->kobj, &qeth_device_vipa_group);
1683         }
1684         if ((ret = sysfs_create_group(&dev->kobj, &qeth_device_blkt_group)))
1685                 return ret;
1686
1687         return ret;
1688 }
1689
1690 void
1691 qeth_remove_device_attributes(struct device *dev)
1692 {
1693         sysfs_remove_group(&dev->kobj, &qeth_device_attr_group);
1694         sysfs_remove_group(&dev->kobj, &qeth_device_ipato_group);
1695         sysfs_remove_group(&dev->kobj, &qeth_device_vipa_group);
1696         sysfs_remove_group(&dev->kobj, &qeth_device_rxip_group);
1697         sysfs_remove_group(&dev->kobj, &qeth_device_blkt_group);
1698 }
1699
1700 /**********************/
1701 /* DRIVER ATTRIBUTES  */
1702 /**********************/
1703 static ssize_t
1704 qeth_driver_group_store(struct device_driver *ddrv, const char *buf,
1705                         size_t count)
1706 {
1707         const char *start, *end;
1708         char bus_ids[3][BUS_ID_SIZE], *argv[3];
1709         int i;
1710         int err;
1711
1712         start = buf;
1713         for (i = 0; i < 3; i++) {
1714                 static const char delim[] = { ',', ',', '\n' };
1715                 int len;
1716
1717                 if (!(end = strchr(start, delim[i])))
1718                         return -EINVAL;
1719                 len = min_t(ptrdiff_t, BUS_ID_SIZE, end - start);
1720                 strncpy(bus_ids[i], start, len);
1721                 bus_ids[i][len] = '\0';
1722                 start = end + 1;
1723                 argv[i] = bus_ids[i];
1724         }
1725         err = ccwgroup_create(qeth_root_dev, qeth_ccwgroup_driver.driver_id,
1726                         &qeth_ccw_driver, 3, argv);
1727         if (err)
1728                 return err;
1729         else
1730                 return count;
1731 }
1732
1733
1734 static DRIVER_ATTR(group, 0200, 0, qeth_driver_group_store);
1735
1736 static ssize_t
1737 qeth_driver_notifier_register_store(struct device_driver *ddrv, const char *buf,
1738                                 size_t count)
1739 {
1740         int rc;
1741         int signum;
1742         char *tmp, *tmp2;
1743
1744         tmp = strsep((char **) &buf, "\n");
1745         if (!strncmp(tmp, "unregister", 10)){
1746                 if ((rc = qeth_notifier_unregister(current)))
1747                         return rc;
1748                 return count;
1749         }
1750
1751         signum = simple_strtoul(tmp, &tmp2, 10);
1752         if ((signum < 0) || (signum > 32)){
1753                 PRINT_WARN("Signal number %d is out of range\n", signum);
1754                 return -EINVAL;
1755         }
1756         if ((rc = qeth_notifier_register(current, signum)))
1757                 return rc;
1758
1759         return count;
1760 }
1761
1762 static DRIVER_ATTR(notifier_register, 0200, 0,
1763                    qeth_driver_notifier_register_store);
1764
1765 int
1766 qeth_create_driver_attributes(void)
1767 {
1768         int rc;
1769
1770         if ((rc = driver_create_file(&qeth_ccwgroup_driver.driver,
1771                                      &driver_attr_group)))
1772                 return rc;
1773         return driver_create_file(&qeth_ccwgroup_driver.driver,
1774                                   &driver_attr_notifier_register);
1775 }
1776
1777 void
1778 qeth_remove_driver_attributes(void)
1779 {
1780         driver_remove_file(&qeth_ccwgroup_driver.driver,
1781                         &driver_attr_group);
1782         driver_remove_file(&qeth_ccwgroup_driver.driver,
1783                         &driver_attr_notifier_register);
1784 }