Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/rric/oprofile
[linux-2.6] / drivers / s390 / net / qeth_core_sys.c
1 /*
2  *  drivers/s390/net/qeth_core_sys.c
3  *
4  *    Copyright IBM Corp. 2007
5  *    Author(s): Utz Bacher <utz.bacher@de.ibm.com>,
6  *               Frank Pavlic <fpavlic@de.ibm.com>,
7  *               Thomas Spatzier <tspat@de.ibm.com>,
8  *               Frank Blaschka <frank.blaschka@de.ibm.com>
9  */
10
11 #include <linux/list.h>
12 #include <linux/rwsem.h>
13 #include <asm/ebcdic.h>
14
15 #include "qeth_core.h"
16
17 static ssize_t qeth_dev_state_show(struct device *dev,
18                                 struct device_attribute *attr, char *buf)
19 {
20         struct qeth_card *card = dev_get_drvdata(dev);
21         if (!card)
22                 return -EINVAL;
23
24         switch (card->state) {
25         case CARD_STATE_DOWN:
26                 return sprintf(buf, "DOWN\n");
27         case CARD_STATE_HARDSETUP:
28                 return sprintf(buf, "HARDSETUP\n");
29         case CARD_STATE_SOFTSETUP:
30                 return sprintf(buf, "SOFTSETUP\n");
31         case CARD_STATE_UP:
32                 if (card->lan_online)
33                 return sprintf(buf, "UP (LAN ONLINE)\n");
34                 else
35                         return sprintf(buf, "UP (LAN OFFLINE)\n");
36         case CARD_STATE_RECOVER:
37                 return sprintf(buf, "RECOVER\n");
38         default:
39                 return sprintf(buf, "UNKNOWN\n");
40         }
41 }
42
43 static DEVICE_ATTR(state, 0444, qeth_dev_state_show, NULL);
44
45 static ssize_t qeth_dev_chpid_show(struct device *dev,
46                                 struct device_attribute *attr, char *buf)
47 {
48         struct qeth_card *card = dev_get_drvdata(dev);
49         if (!card)
50                 return -EINVAL;
51
52         return sprintf(buf, "%02X\n", card->info.chpid);
53 }
54
55 static DEVICE_ATTR(chpid, 0444, qeth_dev_chpid_show, NULL);
56
57 static ssize_t qeth_dev_if_name_show(struct device *dev,
58                                 struct device_attribute *attr, char *buf)
59 {
60         struct qeth_card *card = dev_get_drvdata(dev);
61         if (!card)
62                 return -EINVAL;
63         return sprintf(buf, "%s\n", QETH_CARD_IFNAME(card));
64 }
65
66 static DEVICE_ATTR(if_name, 0444, qeth_dev_if_name_show, NULL);
67
68 static ssize_t qeth_dev_card_type_show(struct device *dev,
69                                 struct device_attribute *attr, char *buf)
70 {
71         struct qeth_card *card = dev_get_drvdata(dev);
72         if (!card)
73                 return -EINVAL;
74
75         return sprintf(buf, "%s\n", qeth_get_cardname_short(card));
76 }
77
78 static DEVICE_ATTR(card_type, 0444, qeth_dev_card_type_show, NULL);
79
80 static inline const char *qeth_get_bufsize_str(struct qeth_card *card)
81 {
82         if (card->qdio.in_buf_size == 16384)
83                 return "16k";
84         else if (card->qdio.in_buf_size == 24576)
85                 return "24k";
86         else if (card->qdio.in_buf_size == 32768)
87                 return "32k";
88         else if (card->qdio.in_buf_size == 40960)
89                 return "40k";
90         else
91                 return "64k";
92 }
93
94 static ssize_t qeth_dev_inbuf_size_show(struct device *dev,
95                                 struct device_attribute *attr, char *buf)
96 {
97         struct qeth_card *card = dev_get_drvdata(dev);
98         if (!card)
99                 return -EINVAL;
100
101         return sprintf(buf, "%s\n", qeth_get_bufsize_str(card));
102 }
103
104 static DEVICE_ATTR(inbuf_size, 0444, qeth_dev_inbuf_size_show, NULL);
105
106 static ssize_t qeth_dev_portno_show(struct device *dev,
107                         struct device_attribute *attr, char *buf)
108 {
109         struct qeth_card *card = dev_get_drvdata(dev);
110         if (!card)
111                 return -EINVAL;
112
113         return sprintf(buf, "%i\n", card->info.portno);
114 }
115
116 static ssize_t qeth_dev_portno_store(struct device *dev,
117                 struct device_attribute *attr, const char *buf, size_t count)
118 {
119         struct qeth_card *card = dev_get_drvdata(dev);
120         char *tmp;
121         unsigned int portno;
122
123         if (!card)
124                 return -EINVAL;
125
126         if ((card->state != CARD_STATE_DOWN) &&
127             (card->state != CARD_STATE_RECOVER))
128                 return -EPERM;
129
130         portno = simple_strtoul(buf, &tmp, 16);
131         if (portno > QETH_MAX_PORTNO) {
132                 return -EINVAL;
133         }
134
135         card->info.portno = portno;
136         return count;
137 }
138
139 static DEVICE_ATTR(portno, 0644, qeth_dev_portno_show, qeth_dev_portno_store);
140
141 static ssize_t qeth_dev_portname_show(struct device *dev,
142                                 struct device_attribute *attr, char *buf)
143 {
144         struct qeth_card *card = dev_get_drvdata(dev);
145         char portname[9] = {0, };
146
147         if (!card)
148                 return -EINVAL;
149
150         if (card->info.portname_required) {
151                 memcpy(portname, card->info.portname + 1, 8);
152                 EBCASC(portname, 8);
153                 return sprintf(buf, "%s\n", portname);
154         } else
155                 return sprintf(buf, "no portname required\n");
156 }
157
158 static ssize_t qeth_dev_portname_store(struct device *dev,
159                 struct device_attribute *attr, const char *buf, size_t count)
160 {
161         struct qeth_card *card = dev_get_drvdata(dev);
162         char *tmp;
163         int i;
164
165         if (!card)
166                 return -EINVAL;
167
168         if ((card->state != CARD_STATE_DOWN) &&
169             (card->state != CARD_STATE_RECOVER))
170                 return -EPERM;
171
172         tmp = strsep((char **) &buf, "\n");
173         if ((strlen(tmp) > 8) || (strlen(tmp) == 0))
174                 return -EINVAL;
175
176         card->info.portname[0] = strlen(tmp);
177         /* for beauty reasons */
178         for (i = 1; i < 9; i++)
179                 card->info.portname[i] = ' ';
180         strcpy(card->info.portname + 1, tmp);
181         ASCEBC(card->info.portname + 1, 8);
182
183         return count;
184 }
185
186 static DEVICE_ATTR(portname, 0644, qeth_dev_portname_show,
187                 qeth_dev_portname_store);
188
189 static ssize_t qeth_dev_prioqing_show(struct device *dev,
190                                 struct device_attribute *attr, char *buf)
191 {
192         struct qeth_card *card = dev_get_drvdata(dev);
193
194         if (!card)
195                 return -EINVAL;
196
197         switch (card->qdio.do_prio_queueing) {
198         case QETH_PRIO_Q_ING_PREC:
199                 return sprintf(buf, "%s\n", "by precedence");
200         case QETH_PRIO_Q_ING_TOS:
201                 return sprintf(buf, "%s\n", "by type of service");
202         default:
203                 return sprintf(buf, "always queue %i\n",
204                                card->qdio.default_out_queue);
205         }
206 }
207
208 static ssize_t qeth_dev_prioqing_store(struct device *dev,
209                 struct device_attribute *attr, const char *buf, size_t count)
210 {
211         struct qeth_card *card = dev_get_drvdata(dev);
212         char *tmp;
213
214         if (!card)
215                 return -EINVAL;
216
217         if ((card->state != CARD_STATE_DOWN) &&
218             (card->state != CARD_STATE_RECOVER))
219                 return -EPERM;
220
221         /* check if 1920 devices are supported ,
222          * if though we have to permit priority queueing
223          */
224         if (card->qdio.no_out_queues == 1) {
225                 card->qdio.do_prio_queueing = QETH_PRIOQ_DEFAULT;
226                 return -EPERM;
227         }
228
229         tmp = strsep((char **) &buf, "\n");
230         if (!strcmp(tmp, "prio_queueing_prec"))
231                 card->qdio.do_prio_queueing = QETH_PRIO_Q_ING_PREC;
232         else if (!strcmp(tmp, "prio_queueing_tos"))
233                 card->qdio.do_prio_queueing = QETH_PRIO_Q_ING_TOS;
234         else if (!strcmp(tmp, "no_prio_queueing:0")) {
235                 card->qdio.do_prio_queueing = QETH_NO_PRIO_QUEUEING;
236                 card->qdio.default_out_queue = 0;
237         } else if (!strcmp(tmp, "no_prio_queueing:1")) {
238                 card->qdio.do_prio_queueing = QETH_NO_PRIO_QUEUEING;
239                 card->qdio.default_out_queue = 1;
240         } else if (!strcmp(tmp, "no_prio_queueing:2")) {
241                 card->qdio.do_prio_queueing = QETH_NO_PRIO_QUEUEING;
242                 card->qdio.default_out_queue = 2;
243         } else if (!strcmp(tmp, "no_prio_queueing:3")) {
244                 card->qdio.do_prio_queueing = QETH_NO_PRIO_QUEUEING;
245                 card->qdio.default_out_queue = 3;
246         } else if (!strcmp(tmp, "no_prio_queueing")) {
247                 card->qdio.do_prio_queueing = QETH_NO_PRIO_QUEUEING;
248                 card->qdio.default_out_queue = QETH_DEFAULT_QUEUE;
249         } else {
250                 return -EINVAL;
251         }
252         return count;
253 }
254
255 static DEVICE_ATTR(priority_queueing, 0644, qeth_dev_prioqing_show,
256                 qeth_dev_prioqing_store);
257
258 static ssize_t qeth_dev_bufcnt_show(struct device *dev,
259                                 struct device_attribute *attr, char *buf)
260 {
261         struct qeth_card *card = dev_get_drvdata(dev);
262
263         if (!card)
264                 return -EINVAL;
265
266         return sprintf(buf, "%i\n", card->qdio.in_buf_pool.buf_count);
267 }
268
269 static ssize_t qeth_dev_bufcnt_store(struct device *dev,
270                 struct device_attribute *attr, const char *buf, size_t count)
271 {
272         struct qeth_card *card = dev_get_drvdata(dev);
273         char *tmp;
274         int cnt, old_cnt;
275         int rc;
276
277         if (!card)
278                 return -EINVAL;
279
280         if ((card->state != CARD_STATE_DOWN) &&
281             (card->state != CARD_STATE_RECOVER))
282                 return -EPERM;
283
284         old_cnt = card->qdio.in_buf_pool.buf_count;
285         cnt = simple_strtoul(buf, &tmp, 10);
286         cnt = (cnt < QETH_IN_BUF_COUNT_MIN) ? QETH_IN_BUF_COUNT_MIN :
287                 ((cnt > QETH_IN_BUF_COUNT_MAX) ? QETH_IN_BUF_COUNT_MAX : cnt);
288         if (old_cnt != cnt) {
289                 rc = qeth_realloc_buffer_pool(card, cnt);
290         }
291         return count;
292 }
293
294 static DEVICE_ATTR(buffer_count, 0644, qeth_dev_bufcnt_show,
295                 qeth_dev_bufcnt_store);
296
297 static ssize_t qeth_dev_recover_store(struct device *dev,
298                 struct device_attribute *attr, const char *buf, size_t count)
299 {
300         struct qeth_card *card = dev_get_drvdata(dev);
301         char *tmp;
302         int i;
303
304         if (!card)
305                 return -EINVAL;
306
307         if (card->state != CARD_STATE_UP)
308                 return -EPERM;
309
310         i = simple_strtoul(buf, &tmp, 16);
311         if (i == 1)
312                 qeth_schedule_recovery(card);
313
314         return count;
315 }
316
317 static DEVICE_ATTR(recover, 0200, NULL, qeth_dev_recover_store);
318
319 static ssize_t qeth_dev_performance_stats_show(struct device *dev,
320                                 struct device_attribute *attr, char *buf)
321 {
322         struct qeth_card *card = dev_get_drvdata(dev);
323
324         if (!card)
325                 return -EINVAL;
326
327         return sprintf(buf, "%i\n", card->options.performance_stats ? 1:0);
328 }
329
330 static ssize_t qeth_dev_performance_stats_store(struct device *dev,
331                 struct device_attribute *attr, const char *buf, size_t count)
332 {
333         struct qeth_card *card = dev_get_drvdata(dev);
334         char *tmp;
335         int i;
336
337         if (!card)
338                 return -EINVAL;
339
340         i = simple_strtoul(buf, &tmp, 16);
341         if ((i == 0) || (i == 1)) {
342                 if (i == card->options.performance_stats)
343                         return count;
344                 card->options.performance_stats = i;
345                 if (i == 0)
346                         memset(&card->perf_stats, 0,
347                                 sizeof(struct qeth_perf_stats));
348                 card->perf_stats.initial_rx_packets = card->stats.rx_packets;
349                 card->perf_stats.initial_tx_packets = card->stats.tx_packets;
350         } else {
351                 return -EINVAL;
352         }
353         return count;
354 }
355
356 static DEVICE_ATTR(performance_stats, 0644, qeth_dev_performance_stats_show,
357                    qeth_dev_performance_stats_store);
358
359 static ssize_t qeth_dev_layer2_show(struct device *dev,
360                 struct device_attribute *attr, char *buf)
361 {
362         struct qeth_card *card = dev_get_drvdata(dev);
363
364         if (!card)
365                 return -EINVAL;
366
367         return sprintf(buf, "%i\n", card->options.layer2 ? 1:0);
368 }
369
370 static ssize_t qeth_dev_layer2_store(struct device *dev,
371                 struct device_attribute *attr, const char *buf, size_t count)
372 {
373         struct qeth_card *card = dev_get_drvdata(dev);
374         char *tmp;
375         int i, rc;
376         enum qeth_discipline_id newdis;
377
378         if (!card)
379                 return -EINVAL;
380
381         if (((card->state != CARD_STATE_DOWN) &&
382              (card->state != CARD_STATE_RECOVER)))
383                 return -EPERM;
384
385         i = simple_strtoul(buf, &tmp, 16);
386         switch (i) {
387         case 0:
388                 newdis = QETH_DISCIPLINE_LAYER3;
389                 break;
390         case 1:
391                 newdis = QETH_DISCIPLINE_LAYER2;
392                 break;
393         default:
394                 return -EINVAL;
395         }
396
397         if (card->options.layer2 == newdis) {
398                 return count;
399         } else {
400                 if (card->discipline.ccwgdriver) {
401                         card->discipline.ccwgdriver->remove(card->gdev);
402                         qeth_core_free_discipline(card);
403                 }
404         }
405
406         rc = qeth_core_load_discipline(card, newdis);
407         if (rc)
408                 return rc;
409
410         rc = card->discipline.ccwgdriver->probe(card->gdev);
411         if (rc)
412                 return rc;
413         return count;
414 }
415
416 static DEVICE_ATTR(layer2, 0644, qeth_dev_layer2_show,
417                    qeth_dev_layer2_store);
418
419 static ssize_t qeth_dev_large_send_show(struct device *dev,
420                                 struct device_attribute *attr, char *buf)
421 {
422         struct qeth_card *card = dev_get_drvdata(dev);
423
424         if (!card)
425                 return -EINVAL;
426
427         switch (card->options.large_send) {
428         case QETH_LARGE_SEND_NO:
429                 return sprintf(buf, "%s\n", "no");
430         case QETH_LARGE_SEND_EDDP:
431                 return sprintf(buf, "%s\n", "EDDP");
432         case QETH_LARGE_SEND_TSO:
433                 return sprintf(buf, "%s\n", "TSO");
434         default:
435                 return sprintf(buf, "%s\n", "N/A");
436         }
437 }
438
439 static ssize_t qeth_dev_large_send_store(struct device *dev,
440                 struct device_attribute *attr, const char *buf, size_t count)
441 {
442         struct qeth_card *card = dev_get_drvdata(dev);
443         enum qeth_large_send_types type;
444         int rc = 0;
445         char *tmp;
446
447         if (!card)
448                 return -EINVAL;
449         tmp = strsep((char **) &buf, "\n");
450         if (!strcmp(tmp, "no")) {
451                 type = QETH_LARGE_SEND_NO;
452         } else if (!strcmp(tmp, "EDDP")) {
453                 type = QETH_LARGE_SEND_EDDP;
454         } else if (!strcmp(tmp, "TSO")) {
455                 type = QETH_LARGE_SEND_TSO;
456         } else {
457                 return -EINVAL;
458         }
459         if (card->options.large_send == type)
460                 return count;
461         rc = qeth_set_large_send(card, type);
462         if (rc)
463                 return rc;
464         return count;
465 }
466
467 static DEVICE_ATTR(large_send, 0644, qeth_dev_large_send_show,
468                    qeth_dev_large_send_store);
469
470 static ssize_t qeth_dev_blkt_show(char *buf, struct qeth_card *card, int value)
471 {
472
473         if (!card)
474                 return -EINVAL;
475
476         return sprintf(buf, "%i\n", value);
477 }
478
479 static ssize_t qeth_dev_blkt_store(struct qeth_card *card,
480                 const char *buf, size_t count, int *value, int max_value)
481 {
482         char *tmp;
483         int i;
484
485         if (!card)
486                 return -EINVAL;
487
488         if ((card->state != CARD_STATE_DOWN) &&
489             (card->state != CARD_STATE_RECOVER))
490                 return -EPERM;
491
492         i = simple_strtoul(buf, &tmp, 10);
493         if (i <= max_value) {
494                 *value = i;
495         } else {
496                 return -EINVAL;
497         }
498         return count;
499 }
500
501 static ssize_t qeth_dev_blkt_total_show(struct device *dev,
502                                 struct device_attribute *attr, char *buf)
503 {
504         struct qeth_card *card = dev_get_drvdata(dev);
505
506         return qeth_dev_blkt_show(buf, card, card->info.blkt.time_total);
507 }
508
509 static ssize_t qeth_dev_blkt_total_store(struct device *dev,
510                 struct device_attribute *attr, const char *buf, size_t count)
511 {
512         struct qeth_card *card = dev_get_drvdata(dev);
513
514         return qeth_dev_blkt_store(card, buf, count,
515                                    &card->info.blkt.time_total, 1000);
516 }
517
518
519
520 static DEVICE_ATTR(total, 0644, qeth_dev_blkt_total_show,
521                    qeth_dev_blkt_total_store);
522
523 static ssize_t qeth_dev_blkt_inter_show(struct device *dev,
524                                 struct device_attribute *attr, char *buf)
525 {
526         struct qeth_card *card = dev_get_drvdata(dev);
527
528         return qeth_dev_blkt_show(buf, card, card->info.blkt.inter_packet);
529 }
530
531 static ssize_t qeth_dev_blkt_inter_store(struct device *dev,
532                 struct device_attribute *attr, const char *buf, size_t count)
533 {
534         struct qeth_card *card = dev_get_drvdata(dev);
535
536         return qeth_dev_blkt_store(card, buf, count,
537                                    &card->info.blkt.inter_packet, 100);
538 }
539
540 static DEVICE_ATTR(inter, 0644, qeth_dev_blkt_inter_show,
541                    qeth_dev_blkt_inter_store);
542
543 static ssize_t qeth_dev_blkt_inter_jumbo_show(struct device *dev,
544                                 struct device_attribute *attr, char *buf)
545 {
546         struct qeth_card *card = dev_get_drvdata(dev);
547
548         return qeth_dev_blkt_show(buf, card,
549                                   card->info.blkt.inter_packet_jumbo);
550 }
551
552 static ssize_t qeth_dev_blkt_inter_jumbo_store(struct device *dev,
553                 struct device_attribute *attr, const char *buf, size_t count)
554 {
555         struct qeth_card *card = dev_get_drvdata(dev);
556
557         return qeth_dev_blkt_store(card, buf, count,
558                                    &card->info.blkt.inter_packet_jumbo, 100);
559 }
560
561 static DEVICE_ATTR(inter_jumbo, 0644, qeth_dev_blkt_inter_jumbo_show,
562                    qeth_dev_blkt_inter_jumbo_store);
563
564 static struct attribute *qeth_blkt_device_attrs[] = {
565         &dev_attr_total.attr,
566         &dev_attr_inter.attr,
567         &dev_attr_inter_jumbo.attr,
568         NULL,
569 };
570
571 static struct attribute_group qeth_device_blkt_group = {
572         .name = "blkt",
573         .attrs = qeth_blkt_device_attrs,
574 };
575
576 static struct attribute *qeth_device_attrs[] = {
577         &dev_attr_state.attr,
578         &dev_attr_chpid.attr,
579         &dev_attr_if_name.attr,
580         &dev_attr_card_type.attr,
581         &dev_attr_inbuf_size.attr,
582         &dev_attr_portno.attr,
583         &dev_attr_portname.attr,
584         &dev_attr_priority_queueing.attr,
585         &dev_attr_buffer_count.attr,
586         &dev_attr_recover.attr,
587         &dev_attr_performance_stats.attr,
588         &dev_attr_layer2.attr,
589         &dev_attr_large_send.attr,
590         NULL,
591 };
592
593 static struct attribute_group qeth_device_attr_group = {
594         .attrs = qeth_device_attrs,
595 };
596
597 static struct attribute *qeth_osn_device_attrs[] = {
598         &dev_attr_state.attr,
599         &dev_attr_chpid.attr,
600         &dev_attr_if_name.attr,
601         &dev_attr_card_type.attr,
602         &dev_attr_buffer_count.attr,
603         &dev_attr_recover.attr,
604         NULL,
605 };
606
607 static struct attribute_group qeth_osn_device_attr_group = {
608         .attrs = qeth_osn_device_attrs,
609 };
610
611 int qeth_core_create_device_attributes(struct device *dev)
612 {
613         int ret;
614         ret = sysfs_create_group(&dev->kobj, &qeth_device_attr_group);
615         if (ret)
616                 return ret;
617         ret = sysfs_create_group(&dev->kobj, &qeth_device_blkt_group);
618         if (ret)
619                 sysfs_remove_group(&dev->kobj, &qeth_device_attr_group);
620
621         return 0;
622 }
623
624 void qeth_core_remove_device_attributes(struct device *dev)
625 {
626         sysfs_remove_group(&dev->kobj, &qeth_device_attr_group);
627         sysfs_remove_group(&dev->kobj, &qeth_device_blkt_group);
628 }
629
630 int qeth_core_create_osn_attributes(struct device *dev)
631 {
632         return sysfs_create_group(&dev->kobj, &qeth_osn_device_attr_group);
633 }
634
635 void qeth_core_remove_osn_attributes(struct device *dev)
636 {
637         sysfs_remove_group(&dev->kobj, &qeth_osn_device_attr_group);
638         return;
639 }