Merge branch 'for-linus' of master.kernel.org:/pub/scm/linux/kernel/git/shaggy/jfs-2.6
[linux-2.6] / drivers / s390 / net / qeth_sys.c
1 /*
2  *
3  * linux/drivers/s390/net/qeth_sys.c ($Revision: 1.54 $)
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.54 $";
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
941 #define QETH_DEVICE_ATTR(_id,_name,_mode,_show,_store)                       \
942 struct device_attribute dev_attr_##_id = {                                   \
943         .attr = {.name=__stringify(_name), .mode=_mode, .owner=THIS_MODULE },\
944         .show   = _show,                                                     \
945         .store  = _store,                                                    \
946 };
947
948 int
949 qeth_check_layer2(struct qeth_card *card)
950 {
951         if (card->options.layer2)
952                 return -EPERM;
953         return 0;
954 }
955
956
957 static ssize_t
958 qeth_dev_ipato_enable_show(struct device *dev, struct device_attribute *attr, char *buf)
959 {
960         struct qeth_card *card = dev->driver_data;
961
962         if (!card)
963                 return -EINVAL;
964
965         if (qeth_check_layer2(card))
966                 return -EPERM;
967         return sprintf(buf, "%i\n", card->ipato.enabled? 1:0);
968 }
969
970 static ssize_t
971 qeth_dev_ipato_enable_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
972 {
973         struct qeth_card *card = dev->driver_data;
974         char *tmp;
975
976         if (!card)
977                 return -EINVAL;
978
979         if ((card->state != CARD_STATE_DOWN) &&
980             (card->state != CARD_STATE_RECOVER))
981                 return -EPERM;
982
983         if (qeth_check_layer2(card))
984                 return -EPERM;
985
986         tmp = strsep((char **) &buf, "\n");
987         if (!strcmp(tmp, "toggle")){
988                 card->ipato.enabled = (card->ipato.enabled)? 0 : 1;
989         } else if (!strcmp(tmp, "1")){
990                 card->ipato.enabled = 1;
991         } else if (!strcmp(tmp, "0")){
992                 card->ipato.enabled = 0;
993         } else {
994                 PRINT_WARN("ipato_enable: write 0, 1 or 'toggle' to "
995                            "this file\n");
996                 return -EINVAL;
997         }
998         return count;
999 }
1000
1001 static QETH_DEVICE_ATTR(ipato_enable, enable, 0644,
1002                         qeth_dev_ipato_enable_show,
1003                         qeth_dev_ipato_enable_store);
1004
1005 static ssize_t
1006 qeth_dev_ipato_invert4_show(struct device *dev, struct device_attribute *attr, char *buf)
1007 {
1008         struct qeth_card *card = dev->driver_data;
1009
1010         if (!card)
1011                 return -EINVAL;
1012
1013         if (qeth_check_layer2(card))
1014                 return -EPERM;
1015
1016         return sprintf(buf, "%i\n", card->ipato.invert4? 1:0);
1017 }
1018
1019 static ssize_t
1020 qeth_dev_ipato_invert4_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1021 {
1022         struct qeth_card *card = dev->driver_data;
1023         char *tmp;
1024
1025         if (!card)
1026                 return -EINVAL;
1027
1028         if (qeth_check_layer2(card))
1029                 return -EPERM;
1030
1031         tmp = strsep((char **) &buf, "\n");
1032         if (!strcmp(tmp, "toggle")){
1033                 card->ipato.invert4 = (card->ipato.invert4)? 0 : 1;
1034         } else if (!strcmp(tmp, "1")){
1035                 card->ipato.invert4 = 1;
1036         } else if (!strcmp(tmp, "0")){
1037                 card->ipato.invert4 = 0;
1038         } else {
1039                 PRINT_WARN("ipato_invert4: write 0, 1 or 'toggle' to "
1040                            "this file\n");
1041                 return -EINVAL;
1042         }
1043         return count;
1044 }
1045
1046 static QETH_DEVICE_ATTR(ipato_invert4, invert4, 0644,
1047                         qeth_dev_ipato_invert4_show,
1048                         qeth_dev_ipato_invert4_store);
1049
1050 static inline ssize_t
1051 qeth_dev_ipato_add_show(char *buf, struct qeth_card *card,
1052                         enum qeth_prot_versions proto)
1053 {
1054         struct qeth_ipato_entry *ipatoe;
1055         unsigned long flags;
1056         char addr_str[40];
1057         int entry_len; /* length of 1 entry string, differs between v4 and v6 */
1058         int i = 0;
1059
1060         if (qeth_check_layer2(card))
1061                 return -EPERM;
1062
1063         entry_len = (proto == QETH_PROT_IPV4)? 12 : 40;
1064         /* add strlen for "/<mask>\n" */
1065         entry_len += (proto == QETH_PROT_IPV4)? 5 : 6;
1066         spin_lock_irqsave(&card->ip_lock, flags);
1067         list_for_each_entry(ipatoe, &card->ipato.entries, entry){
1068                 if (ipatoe->proto != proto)
1069                         continue;
1070                 /* String must not be longer than PAGE_SIZE. So we check if
1071                  * string length gets near PAGE_SIZE. Then we can savely display
1072                  * the next IPv6 address (worst case, compared to IPv4) */
1073                 if ((PAGE_SIZE - i) <= entry_len)
1074                         break;
1075                 qeth_ipaddr_to_string(proto, ipatoe->addr, addr_str);
1076                 i += snprintf(buf + i, PAGE_SIZE - i,
1077                               "%s/%i\n", addr_str, ipatoe->mask_bits);
1078         }
1079         spin_unlock_irqrestore(&card->ip_lock, flags);
1080         i += snprintf(buf + i, PAGE_SIZE - i, "\n");
1081
1082         return i;
1083 }
1084
1085 static ssize_t
1086 qeth_dev_ipato_add4_show(struct device *dev, struct device_attribute *attr, char *buf)
1087 {
1088         struct qeth_card *card = dev->driver_data;
1089
1090         if (!card)
1091                 return -EINVAL;
1092
1093         return qeth_dev_ipato_add_show(buf, card, QETH_PROT_IPV4);
1094 }
1095
1096 static inline int
1097 qeth_parse_ipatoe(const char* buf, enum qeth_prot_versions proto,
1098                   u8 *addr, int *mask_bits)
1099 {
1100         const char *start, *end;
1101         char *tmp;
1102         char buffer[49] = {0, };
1103
1104         start = buf;
1105         /* get address string */
1106         end = strchr(start, '/');
1107         if (!end){
1108                 PRINT_WARN("Invalid format for ipato_addx/delx. "
1109                            "Use <ip addr>/<mask bits>\n");
1110                 return -EINVAL;
1111         }
1112         strncpy(buffer, start, end - start);
1113         if (qeth_string_to_ipaddr(buffer, proto, addr)){
1114                 PRINT_WARN("Invalid IP address format!\n");
1115                 return -EINVAL;
1116         }
1117         start = end + 1;
1118         *mask_bits = simple_strtoul(start, &tmp, 10);
1119
1120         return 0;
1121 }
1122
1123 static inline ssize_t
1124 qeth_dev_ipato_add_store(const char *buf, size_t count,
1125                          struct qeth_card *card, enum qeth_prot_versions proto)
1126 {
1127         struct qeth_ipato_entry *ipatoe;
1128         u8 addr[16];
1129         int mask_bits;
1130         int rc;
1131
1132         if (qeth_check_layer2(card))
1133                 return -EPERM;
1134         if ((rc = qeth_parse_ipatoe(buf, proto, addr, &mask_bits)))
1135                 return rc;
1136
1137         if (!(ipatoe = kmalloc(sizeof(struct qeth_ipato_entry), GFP_KERNEL))){
1138                 PRINT_WARN("No memory to allocate ipato entry\n");
1139                 return -ENOMEM;
1140         }
1141         memset(ipatoe, 0, sizeof(struct qeth_ipato_entry));
1142         ipatoe->proto = proto;
1143         memcpy(ipatoe->addr, addr, (proto == QETH_PROT_IPV4)? 4:16);
1144         ipatoe->mask_bits = mask_bits;
1145
1146         if ((rc = qeth_add_ipato_entry(card, ipatoe))){
1147                 kfree(ipatoe);
1148                 return rc;
1149         }
1150
1151         return count;
1152 }
1153
1154 static ssize_t
1155 qeth_dev_ipato_add4_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1156 {
1157         struct qeth_card *card = dev->driver_data;
1158
1159         if (!card)
1160                 return -EINVAL;
1161
1162         return qeth_dev_ipato_add_store(buf, count, card, QETH_PROT_IPV4);
1163 }
1164
1165 static QETH_DEVICE_ATTR(ipato_add4, add4, 0644,
1166                         qeth_dev_ipato_add4_show,
1167                         qeth_dev_ipato_add4_store);
1168
1169 static inline ssize_t
1170 qeth_dev_ipato_del_store(const char *buf, size_t count,
1171                          struct qeth_card *card, enum qeth_prot_versions proto)
1172 {
1173         u8 addr[16];
1174         int mask_bits;
1175         int rc;
1176
1177         if (qeth_check_layer2(card))
1178                 return -EPERM;
1179         if ((rc = qeth_parse_ipatoe(buf, proto, addr, &mask_bits)))
1180                 return rc;
1181
1182         qeth_del_ipato_entry(card, proto, addr, mask_bits);
1183
1184         return count;
1185 }
1186
1187 static ssize_t
1188 qeth_dev_ipato_del4_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1189 {
1190         struct qeth_card *card = dev->driver_data;
1191
1192         if (!card)
1193                 return -EINVAL;
1194
1195         return qeth_dev_ipato_del_store(buf, count, card, QETH_PROT_IPV4);
1196 }
1197
1198 static QETH_DEVICE_ATTR(ipato_del4, del4, 0200, NULL,
1199                         qeth_dev_ipato_del4_store);
1200
1201 #ifdef CONFIG_QETH_IPV6
1202 static ssize_t
1203 qeth_dev_ipato_invert6_show(struct device *dev, struct device_attribute *attr, char *buf)
1204 {
1205         struct qeth_card *card = dev->driver_data;
1206
1207         if (!card)
1208                 return -EINVAL;
1209
1210         if (qeth_check_layer2(card))
1211                 return -EPERM;
1212
1213         return sprintf(buf, "%i\n", card->ipato.invert6? 1:0);
1214 }
1215
1216 static ssize_t
1217 qeth_dev_ipato_invert6_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1218 {
1219         struct qeth_card *card = dev->driver_data;
1220         char *tmp;
1221
1222         if (!card)
1223                 return -EINVAL;
1224
1225         if (qeth_check_layer2(card))
1226                 return -EPERM;
1227
1228         tmp = strsep((char **) &buf, "\n");
1229         if (!strcmp(tmp, "toggle")){
1230                 card->ipato.invert6 = (card->ipato.invert6)? 0 : 1;
1231         } else if (!strcmp(tmp, "1")){
1232                 card->ipato.invert6 = 1;
1233         } else if (!strcmp(tmp, "0")){
1234                 card->ipato.invert6 = 0;
1235         } else {
1236                 PRINT_WARN("ipato_invert6: write 0, 1 or 'toggle' to "
1237                            "this file\n");
1238                 return -EINVAL;
1239         }
1240         return count;
1241 }
1242
1243 static QETH_DEVICE_ATTR(ipato_invert6, invert6, 0644,
1244                         qeth_dev_ipato_invert6_show,
1245                         qeth_dev_ipato_invert6_store);
1246
1247
1248 static ssize_t
1249 qeth_dev_ipato_add6_show(struct device *dev, struct device_attribute *attr, char *buf)
1250 {
1251         struct qeth_card *card = dev->driver_data;
1252
1253         if (!card)
1254                 return -EINVAL;
1255
1256         return qeth_dev_ipato_add_show(buf, card, QETH_PROT_IPV6);
1257 }
1258
1259 static ssize_t
1260 qeth_dev_ipato_add6_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1261 {
1262         struct qeth_card *card = dev->driver_data;
1263
1264         if (!card)
1265                 return -EINVAL;
1266
1267         return qeth_dev_ipato_add_store(buf, count, card, QETH_PROT_IPV6);
1268 }
1269
1270 static QETH_DEVICE_ATTR(ipato_add6, add6, 0644,
1271                         qeth_dev_ipato_add6_show,
1272                         qeth_dev_ipato_add6_store);
1273
1274 static ssize_t
1275 qeth_dev_ipato_del6_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1276 {
1277         struct qeth_card *card = dev->driver_data;
1278
1279         if (!card)
1280                 return -EINVAL;
1281
1282         return qeth_dev_ipato_del_store(buf, count, card, QETH_PROT_IPV6);
1283 }
1284
1285 static QETH_DEVICE_ATTR(ipato_del6, del6, 0200, NULL,
1286                         qeth_dev_ipato_del6_store);
1287 #endif /* CONFIG_QETH_IPV6 */
1288
1289 static struct device_attribute * qeth_ipato_device_attrs[] = {
1290         &dev_attr_ipato_enable,
1291         &dev_attr_ipato_invert4,
1292         &dev_attr_ipato_add4,
1293         &dev_attr_ipato_del4,
1294 #ifdef CONFIG_QETH_IPV6
1295         &dev_attr_ipato_invert6,
1296         &dev_attr_ipato_add6,
1297         &dev_attr_ipato_del6,
1298 #endif
1299         NULL,
1300 };
1301
1302 static struct attribute_group qeth_device_ipato_group = {
1303         .name = "ipa_takeover",
1304         .attrs = (struct attribute **)qeth_ipato_device_attrs,
1305 };
1306
1307 static inline ssize_t
1308 qeth_dev_vipa_add_show(char *buf, struct qeth_card *card,
1309                         enum qeth_prot_versions proto)
1310 {
1311         struct qeth_ipaddr *ipaddr;
1312         char addr_str[40];
1313         int entry_len; /* length of 1 entry string, differs between v4 and v6 */
1314         unsigned long flags;
1315         int i = 0;
1316
1317         if (qeth_check_layer2(card))
1318                 return -EPERM;
1319
1320         entry_len = (proto == QETH_PROT_IPV4)? 12 : 40;
1321         entry_len += 2; /* \n + terminator */
1322         spin_lock_irqsave(&card->ip_lock, flags);
1323         list_for_each_entry(ipaddr, &card->ip_list, entry){
1324                 if (ipaddr->proto != proto)
1325                         continue;
1326                 if (ipaddr->type != QETH_IP_TYPE_VIPA)
1327                         continue;
1328                 /* String must not be longer than PAGE_SIZE. So we check if
1329                  * string length gets near PAGE_SIZE. Then we can savely display
1330                  * the next IPv6 address (worst case, compared to IPv4) */
1331                 if ((PAGE_SIZE - i) <= entry_len)
1332                         break;
1333                 qeth_ipaddr_to_string(proto, (const u8 *)&ipaddr->u, addr_str);
1334                 i += snprintf(buf + i, PAGE_SIZE - i, "%s\n", addr_str);
1335         }
1336         spin_unlock_irqrestore(&card->ip_lock, flags);
1337         i += snprintf(buf + i, PAGE_SIZE - i, "\n");
1338
1339         return i;
1340 }
1341
1342 static ssize_t
1343 qeth_dev_vipa_add4_show(struct device *dev, struct device_attribute *attr, char *buf)
1344 {
1345         struct qeth_card *card = dev->driver_data;
1346
1347         if (!card)
1348                 return -EINVAL;
1349
1350         return qeth_dev_vipa_add_show(buf, card, QETH_PROT_IPV4);
1351 }
1352
1353 static inline int
1354 qeth_parse_vipae(const char* buf, enum qeth_prot_versions proto,
1355                  u8 *addr)
1356 {
1357         if (qeth_string_to_ipaddr(buf, proto, addr)){
1358                 PRINT_WARN("Invalid IP address format!\n");
1359                 return -EINVAL;
1360         }
1361         return 0;
1362 }
1363
1364 static inline ssize_t
1365 qeth_dev_vipa_add_store(const char *buf, size_t count,
1366                          struct qeth_card *card, enum qeth_prot_versions proto)
1367 {
1368         u8 addr[16] = {0, };
1369         int rc;
1370
1371         if (qeth_check_layer2(card))
1372                 return -EPERM;
1373         if ((rc = qeth_parse_vipae(buf, proto, addr)))
1374                 return rc;
1375
1376         if ((rc = qeth_add_vipa(card, proto, addr)))
1377                 return rc;
1378
1379         return count;
1380 }
1381
1382 static ssize_t
1383 qeth_dev_vipa_add4_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1384 {
1385         struct qeth_card *card = dev->driver_data;
1386
1387         if (!card)
1388                 return -EINVAL;
1389
1390         return qeth_dev_vipa_add_store(buf, count, card, QETH_PROT_IPV4);
1391 }
1392
1393 static QETH_DEVICE_ATTR(vipa_add4, add4, 0644,
1394                         qeth_dev_vipa_add4_show,
1395                         qeth_dev_vipa_add4_store);
1396
1397 static inline ssize_t
1398 qeth_dev_vipa_del_store(const char *buf, size_t count,
1399                          struct qeth_card *card, enum qeth_prot_versions proto)
1400 {
1401         u8 addr[16];
1402         int rc;
1403
1404         if (qeth_check_layer2(card))
1405                 return -EPERM;
1406         if ((rc = qeth_parse_vipae(buf, proto, addr)))
1407                 return rc;
1408
1409         qeth_del_vipa(card, proto, addr);
1410
1411         return count;
1412 }
1413
1414 static ssize_t
1415 qeth_dev_vipa_del4_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1416 {
1417         struct qeth_card *card = dev->driver_data;
1418
1419         if (!card)
1420                 return -EINVAL;
1421
1422         return qeth_dev_vipa_del_store(buf, count, card, QETH_PROT_IPV4);
1423 }
1424
1425 static QETH_DEVICE_ATTR(vipa_del4, del4, 0200, NULL,
1426                         qeth_dev_vipa_del4_store);
1427
1428 #ifdef CONFIG_QETH_IPV6
1429 static ssize_t
1430 qeth_dev_vipa_add6_show(struct device *dev, struct device_attribute *attr, char *buf)
1431 {
1432         struct qeth_card *card = dev->driver_data;
1433
1434         if (!card)
1435                 return -EINVAL;
1436
1437         return qeth_dev_vipa_add_show(buf, card, QETH_PROT_IPV6);
1438 }
1439
1440 static ssize_t
1441 qeth_dev_vipa_add6_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1442 {
1443         struct qeth_card *card = dev->driver_data;
1444
1445         if (!card)
1446                 return -EINVAL;
1447
1448         return qeth_dev_vipa_add_store(buf, count, card, QETH_PROT_IPV6);
1449 }
1450
1451 static QETH_DEVICE_ATTR(vipa_add6, add6, 0644,
1452                         qeth_dev_vipa_add6_show,
1453                         qeth_dev_vipa_add6_store);
1454
1455 static ssize_t
1456 qeth_dev_vipa_del6_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1457 {
1458         struct qeth_card *card = dev->driver_data;
1459
1460         if (!card)
1461                 return -EINVAL;
1462
1463         if (qeth_check_layer2(card))
1464                 return -EPERM;
1465
1466         return qeth_dev_vipa_del_store(buf, count, card, QETH_PROT_IPV6);
1467 }
1468
1469 static QETH_DEVICE_ATTR(vipa_del6, del6, 0200, NULL,
1470                         qeth_dev_vipa_del6_store);
1471 #endif /* CONFIG_QETH_IPV6 */
1472
1473 static struct device_attribute * qeth_vipa_device_attrs[] = {
1474         &dev_attr_vipa_add4,
1475         &dev_attr_vipa_del4,
1476 #ifdef CONFIG_QETH_IPV6
1477         &dev_attr_vipa_add6,
1478         &dev_attr_vipa_del6,
1479 #endif
1480         NULL,
1481 };
1482
1483 static struct attribute_group qeth_device_vipa_group = {
1484         .name = "vipa",
1485         .attrs = (struct attribute **)qeth_vipa_device_attrs,
1486 };
1487
1488 static inline ssize_t
1489 qeth_dev_rxip_add_show(char *buf, struct qeth_card *card,
1490                        enum qeth_prot_versions proto)
1491 {
1492         struct qeth_ipaddr *ipaddr;
1493         char addr_str[40];
1494         int entry_len; /* length of 1 entry string, differs between v4 and v6 */
1495         unsigned long flags;
1496         int i = 0;
1497
1498         if (qeth_check_layer2(card))
1499                 return -EPERM;
1500
1501         entry_len = (proto == QETH_PROT_IPV4)? 12 : 40;
1502         entry_len += 2; /* \n + terminator */
1503         spin_lock_irqsave(&card->ip_lock, flags);
1504         list_for_each_entry(ipaddr, &card->ip_list, entry){
1505                 if (ipaddr->proto != proto)
1506                         continue;
1507                 if (ipaddr->type != QETH_IP_TYPE_RXIP)
1508                         continue;
1509                 /* String must not be longer than PAGE_SIZE. So we check if
1510                  * string length gets near PAGE_SIZE. Then we can savely display
1511                  * the next IPv6 address (worst case, compared to IPv4) */
1512                 if ((PAGE_SIZE - i) <= entry_len)
1513                         break;
1514                 qeth_ipaddr_to_string(proto, (const u8 *)&ipaddr->u, addr_str);
1515                 i += snprintf(buf + i, PAGE_SIZE - i, "%s\n", addr_str);
1516         }
1517         spin_unlock_irqrestore(&card->ip_lock, flags);
1518         i += snprintf(buf + i, PAGE_SIZE - i, "\n");
1519
1520         return i;
1521 }
1522
1523 static ssize_t
1524 qeth_dev_rxip_add4_show(struct device *dev, struct device_attribute *attr, char *buf)
1525 {
1526         struct qeth_card *card = dev->driver_data;
1527
1528         if (!card)
1529                 return -EINVAL;
1530
1531         return qeth_dev_rxip_add_show(buf, card, QETH_PROT_IPV4);
1532 }
1533
1534 static inline int
1535 qeth_parse_rxipe(const char* buf, enum qeth_prot_versions proto,
1536                  u8 *addr)
1537 {
1538         if (qeth_string_to_ipaddr(buf, proto, addr)){
1539                 PRINT_WARN("Invalid IP address format!\n");
1540                 return -EINVAL;
1541         }
1542         return 0;
1543 }
1544
1545 static inline ssize_t
1546 qeth_dev_rxip_add_store(const char *buf, size_t count,
1547                         struct qeth_card *card, enum qeth_prot_versions proto)
1548 {
1549         u8 addr[16] = {0, };
1550         int rc;
1551
1552         if (qeth_check_layer2(card))
1553                 return -EPERM;
1554         if ((rc = qeth_parse_rxipe(buf, proto, addr)))
1555                 return rc;
1556
1557         if ((rc = qeth_add_rxip(card, proto, addr)))
1558                 return rc;
1559
1560         return count;
1561 }
1562
1563 static ssize_t
1564 qeth_dev_rxip_add4_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1565 {
1566         struct qeth_card *card = dev->driver_data;
1567
1568         if (!card)
1569                 return -EINVAL;
1570
1571         return qeth_dev_rxip_add_store(buf, count, card, QETH_PROT_IPV4);
1572 }
1573
1574 static QETH_DEVICE_ATTR(rxip_add4, add4, 0644,
1575                         qeth_dev_rxip_add4_show,
1576                         qeth_dev_rxip_add4_store);
1577
1578 static inline ssize_t
1579 qeth_dev_rxip_del_store(const char *buf, size_t count,
1580                         struct qeth_card *card, enum qeth_prot_versions proto)
1581 {
1582         u8 addr[16];
1583         int rc;
1584
1585         if (qeth_check_layer2(card))
1586                 return -EPERM;
1587         if ((rc = qeth_parse_rxipe(buf, proto, addr)))
1588                 return rc;
1589
1590         qeth_del_rxip(card, proto, addr);
1591
1592         return count;
1593 }
1594
1595 static ssize_t
1596 qeth_dev_rxip_del4_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1597 {
1598         struct qeth_card *card = dev->driver_data;
1599
1600         if (!card)
1601                 return -EINVAL;
1602
1603         return qeth_dev_rxip_del_store(buf, count, card, QETH_PROT_IPV4);
1604 }
1605
1606 static QETH_DEVICE_ATTR(rxip_del4, del4, 0200, NULL,
1607                         qeth_dev_rxip_del4_store);
1608
1609 #ifdef CONFIG_QETH_IPV6
1610 static ssize_t
1611 qeth_dev_rxip_add6_show(struct device *dev, struct device_attribute *attr, char *buf)
1612 {
1613         struct qeth_card *card = dev->driver_data;
1614
1615         if (!card)
1616                 return -EINVAL;
1617
1618         return qeth_dev_rxip_add_show(buf, card, QETH_PROT_IPV6);
1619 }
1620
1621 static ssize_t
1622 qeth_dev_rxip_add6_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1623 {
1624         struct qeth_card *card = dev->driver_data;
1625
1626         if (!card)
1627                 return -EINVAL;
1628
1629         return qeth_dev_rxip_add_store(buf, count, card, QETH_PROT_IPV6);
1630 }
1631
1632 static QETH_DEVICE_ATTR(rxip_add6, add6, 0644,
1633                         qeth_dev_rxip_add6_show,
1634                         qeth_dev_rxip_add6_store);
1635
1636 static ssize_t
1637 qeth_dev_rxip_del6_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1638 {
1639         struct qeth_card *card = dev->driver_data;
1640
1641         if (!card)
1642                 return -EINVAL;
1643
1644         return qeth_dev_rxip_del_store(buf, count, card, QETH_PROT_IPV6);
1645 }
1646
1647 static QETH_DEVICE_ATTR(rxip_del6, del6, 0200, NULL,
1648                         qeth_dev_rxip_del6_store);
1649 #endif /* CONFIG_QETH_IPV6 */
1650
1651 static struct device_attribute * qeth_rxip_device_attrs[] = {
1652         &dev_attr_rxip_add4,
1653         &dev_attr_rxip_del4,
1654 #ifdef CONFIG_QETH_IPV6
1655         &dev_attr_rxip_add6,
1656         &dev_attr_rxip_del6,
1657 #endif
1658         NULL,
1659 };
1660
1661 static struct attribute_group qeth_device_rxip_group = {
1662         .name = "rxip",
1663         .attrs = (struct attribute **)qeth_rxip_device_attrs,
1664 };
1665
1666 int
1667 qeth_create_device_attributes(struct device *dev)
1668 {
1669         int ret;
1670
1671         if ((ret = sysfs_create_group(&dev->kobj, &qeth_device_attr_group)))
1672                 return ret;
1673         if ((ret = sysfs_create_group(&dev->kobj, &qeth_device_ipato_group))){
1674                 sysfs_remove_group(&dev->kobj, &qeth_device_attr_group);
1675                 return ret;
1676         }
1677         if ((ret = sysfs_create_group(&dev->kobj, &qeth_device_vipa_group))){
1678                 sysfs_remove_group(&dev->kobj, &qeth_device_attr_group);
1679                 sysfs_remove_group(&dev->kobj, &qeth_device_ipato_group);
1680                 return ret;
1681         }
1682         if ((ret = sysfs_create_group(&dev->kobj, &qeth_device_rxip_group))){
1683                 sysfs_remove_group(&dev->kobj, &qeth_device_attr_group);
1684                 sysfs_remove_group(&dev->kobj, &qeth_device_ipato_group);
1685                 sysfs_remove_group(&dev->kobj, &qeth_device_vipa_group);
1686         }
1687         if ((ret = sysfs_create_group(&dev->kobj, &qeth_device_blkt_group)))
1688                 return ret;
1689
1690         return ret;
1691 }
1692
1693 void
1694 qeth_remove_device_attributes(struct device *dev)
1695 {
1696         sysfs_remove_group(&dev->kobj, &qeth_device_attr_group);
1697         sysfs_remove_group(&dev->kobj, &qeth_device_ipato_group);
1698         sysfs_remove_group(&dev->kobj, &qeth_device_vipa_group);
1699         sysfs_remove_group(&dev->kobj, &qeth_device_rxip_group);
1700         sysfs_remove_group(&dev->kobj, &qeth_device_blkt_group);
1701 }
1702
1703 /**********************/
1704 /* DRIVER ATTRIBUTES  */
1705 /**********************/
1706 static ssize_t
1707 qeth_driver_group_store(struct device_driver *ddrv, const char *buf,
1708                         size_t count)
1709 {
1710         const char *start, *end;
1711         char bus_ids[3][BUS_ID_SIZE], *argv[3];
1712         int i;
1713         int err;
1714
1715         start = buf;
1716         for (i = 0; i < 3; i++) {
1717                 static const char delim[] = { ',', ',', '\n' };
1718                 int len;
1719
1720                 if (!(end = strchr(start, delim[i])))
1721                         return -EINVAL;
1722                 len = min_t(ptrdiff_t, BUS_ID_SIZE, end - start);
1723                 strncpy(bus_ids[i], start, len);
1724                 bus_ids[i][len] = '\0';
1725                 start = end + 1;
1726                 argv[i] = bus_ids[i];
1727         }
1728         err = ccwgroup_create(qeth_root_dev, qeth_ccwgroup_driver.driver_id,
1729                         &qeth_ccw_driver, 3, argv);
1730         if (err)
1731                 return err;
1732         else
1733                 return count;
1734 }
1735
1736
1737 static DRIVER_ATTR(group, 0200, 0, qeth_driver_group_store);
1738
1739 static ssize_t
1740 qeth_driver_notifier_register_store(struct device_driver *ddrv, const char *buf,
1741                                 size_t count)
1742 {
1743         int rc;
1744         int signum;
1745         char *tmp, *tmp2;
1746
1747         tmp = strsep((char **) &buf, "\n");
1748         if (!strncmp(tmp, "unregister", 10)){
1749                 if ((rc = qeth_notifier_unregister(current)))
1750                         return rc;
1751                 return count;
1752         }
1753
1754         signum = simple_strtoul(tmp, &tmp2, 10);
1755         if ((signum < 0) || (signum > 32)){
1756                 PRINT_WARN("Signal number %d is out of range\n", signum);
1757                 return -EINVAL;
1758         }
1759         if ((rc = qeth_notifier_register(current, signum)))
1760                 return rc;
1761
1762         return count;
1763 }
1764
1765 static DRIVER_ATTR(notifier_register, 0200, 0,
1766                    qeth_driver_notifier_register_store);
1767
1768 int
1769 qeth_create_driver_attributes(void)
1770 {
1771         int rc;
1772
1773         if ((rc = driver_create_file(&qeth_ccwgroup_driver.driver,
1774                                      &driver_attr_group)))
1775                 return rc;
1776         return driver_create_file(&qeth_ccwgroup_driver.driver,
1777                                   &driver_attr_notifier_register);
1778 }
1779
1780 void
1781 qeth_remove_driver_attributes(void)
1782 {
1783         driver_remove_file(&qeth_ccwgroup_driver.driver,
1784                         &driver_attr_group);
1785         driver_remove_file(&qeth_ccwgroup_driver.driver,
1786                         &driver_attr_notifier_register);
1787 }