patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / s390 / net / netiucv.c
1 /*
2  * $Id: netiucv.c,v 1.54 2004/05/28 08:04:14 braunu Exp $
3  *
4  * IUCV network driver
5  *
6  * Copyright (C) 2001 IBM Deutschland Entwicklung GmbH, IBM Corporation
7  * Author(s): Fritz Elfert (elfert@de.ibm.com, felfert@millenux.com)
8  *
9  * Driverfs integration and all bugs therein by Cornelia Huck(cohuck@de.ibm.com)
10  *
11  * Documentation used:
12  *  the source of the original IUCV driver by:
13  *    Stefan Hegewald <hegewald@de.ibm.com>
14  *    Hartmut Penner <hpenner@de.ibm.com>
15  *    Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
16  *    Martin Schwidefsky (schwidefsky@de.ibm.com)
17  *    Alan Altmark (Alan_Altmark@us.ibm.com)  Sept. 2000
18  *
19  * This program is free software; you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation; either version 2, or (at your option)
22  * any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program; if not, write to the Free Software
31  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32  *
33  * RELEASE-TAG: IUCV network driver $Revision: 1.54 $
34  *
35  */
36 \f
37 #undef DEBUG
38
39 #include <linux/module.h>
40 #include <linux/init.h>
41 #include <linux/kernel.h>
42 #include <linux/slab.h>
43 #include <linux/errno.h>
44 #include <linux/types.h>
45 #include <linux/interrupt.h>
46 #include <linux/timer.h>
47 #include <linux/sched.h>
48
49 #include <linux/signal.h>
50 #include <linux/string.h>
51 #include <linux/device.h>
52
53 #include <linux/ip.h>
54 #include <linux/if_arp.h>
55 #include <linux/tcp.h>
56 #include <linux/skbuff.h>
57 #include <linux/ctype.h>
58 #include <net/dst.h>
59
60 #include <asm/io.h>
61 #include <asm/bitops.h>
62 #include <asm/uaccess.h>
63
64 #include "iucv.h"
65 #include "fsm.h"
66
67 MODULE_AUTHOR
68     ("(C) 2001 IBM Corporation by Fritz Elfert (felfert@millenux.com)");
69 MODULE_DESCRIPTION ("Linux for S/390 IUCV network driver");
70
71 \f
72 /**
73  * Per connection profiling data
74  */
75 struct connection_profile {
76         unsigned long maxmulti;
77         unsigned long maxcqueue;
78         unsigned long doios_single;
79         unsigned long doios_multi;
80         unsigned long txlen;
81         unsigned long tx_time;
82         struct timespec send_stamp;
83         unsigned long tx_pending;
84         unsigned long tx_max_pending;
85 };
86
87 /**
88  * Representation of one iucv connection
89  */
90 struct iucv_connection {
91         struct iucv_connection    *next;
92         iucv_handle_t             handle;
93         __u16                     pathid;
94         struct sk_buff            *rx_buff;
95         struct sk_buff            *tx_buff;
96         struct sk_buff_head       collect_queue;
97         struct sk_buff_head       commit_queue;
98         spinlock_t                collect_lock;
99         int                       collect_len;
100         int                       max_buffsize;
101         int                       flags;
102         fsm_timer                 timer;
103         fsm_instance              *fsm;
104         struct net_device         *netdev;
105         struct connection_profile prof;
106         char                      userid[9];
107 };
108
109 #define CONN_FLAGS_BUFSIZE_CHANGED 1
110
111 /**
112  * Linked list of all connection structs.
113  */
114 static struct iucv_connection *connections;
115
116 /**
117  * Representation of event-data for the
118  * connection state machine.
119  */
120 struct iucv_event {
121         struct iucv_connection *conn;
122         void                   *data;
123 };
124
125 /**
126  * Private part of the network device structure
127  */
128 struct netiucv_priv {
129         struct net_device_stats stats;
130         unsigned long           tbusy;
131         fsm_instance            *fsm;
132         struct iucv_connection  *conn;
133         struct device           *dev;
134         fsm_timer               timer;
135 };
136
137 /**
138  * Link level header for a packet.
139  */
140 typedef struct ll_header_t {
141         __u16 next;
142 } ll_header;
143
144 #define NETIUCV_HDRLEN           (sizeof(ll_header))
145 #define NETIUCV_BUFSIZE_MAX      32768
146 #define NETIUCV_BUFSIZE_DEFAULT  NETIUCV_BUFSIZE_MAX
147 #define NETIUCV_MTU_MAX          (NETIUCV_BUFSIZE_MAX - NETIUCV_HDRLEN)
148 #define NETIUCV_MTU_DEFAULT      9216
149 #define NETIUCV_QUEUELEN_DEFAULT 50
150 #define NETIUCV_TIMEOUT_5SEC     5000
151
152 /**
153  * Compatibility macros for busy handling
154  * of network devices.
155  */
156 static __inline__ void netiucv_clear_busy(struct net_device *dev)
157 {
158         clear_bit(0, &(((struct netiucv_priv *)dev->priv)->tbusy));
159         netif_wake_queue(dev);
160 }
161
162 static __inline__ int netiucv_test_and_set_busy(struct net_device *dev)
163 {
164         netif_stop_queue(dev);
165         return test_and_set_bit(0, &((struct netiucv_priv *)dev->priv)->tbusy);
166 }
167
168 static __u8 iucv_host[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
169 static __u8 iucvMagic[16] = {
170         0xF0, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
171         0xF0, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40
172 };
173
174 /**
175  * This mask means the 16-byte IUCV "magic" and the origin userid must
176  * match exactly as specified in order to give connection_pending()
177  * control.
178  */
179 static __u8 mask[] = {
180         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
181         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
182         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
183 };
184
185 /**
186  * Convert an iucv userId to its printable
187  * form (strip whitespace at end).
188  *
189  * @param An iucv userId
190  *
191  * @returns The printable string (static data!!)
192  */
193 static __inline__ char *
194 netiucv_printname(char *name)
195 {
196         static char tmp[9];
197         char *p = tmp;
198         memcpy(tmp, name, 8);
199         tmp[8] = '\0';
200         while (*p && (!isspace(*p)))
201                 p++;
202         *p = '\0';
203         return tmp;
204 }
205 \f
206 /**
207  * States of the interface statemachine.
208  */
209 enum dev_states {
210         DEV_STATE_STOPPED,
211         DEV_STATE_STARTWAIT,
212         DEV_STATE_STOPWAIT,
213         DEV_STATE_RUNNING,
214         /**
215          * MUST be always the last element!!
216          */
217         NR_DEV_STATES
218 };
219
220 static const char *dev_state_names[] = {
221         "Stopped",
222         "StartWait",
223         "StopWait",
224         "Running",
225 };
226
227 /**
228  * Events of the interface statemachine.
229  */
230 enum dev_events {
231         DEV_EVENT_START,
232         DEV_EVENT_STOP,
233         DEV_EVENT_CONUP,
234         DEV_EVENT_CONDOWN,
235         DEV_EVENT_TIMER,
236         /**
237          * MUST be always the last element!!
238          */
239         NR_DEV_EVENTS
240 };
241
242 static const char *dev_event_names[] = {
243         "Start",
244         "Stop",
245         "Connection up",
246         "Connection down",
247         "Timer",
248 };
249 \f
250 /**
251  * Events of the connection statemachine
252  */
253 enum conn_events {
254         /**
255          * Events, representing callbacks from
256          * lowlevel iucv layer)
257          */
258         CONN_EVENT_CONN_REQ,
259         CONN_EVENT_CONN_ACK,
260         CONN_EVENT_CONN_REJ,
261         CONN_EVENT_CONN_SUS,
262         CONN_EVENT_CONN_RES,
263         CONN_EVENT_RX,
264         CONN_EVENT_TXDONE,
265
266         /**
267          * Events, representing errors return codes from
268          * calls to lowlevel iucv layer
269          */
270
271         /**
272          * Event, representing timer expiry.
273          */
274         CONN_EVENT_TIMER,
275
276         /**
277          * Events, representing commands from upper levels.
278          */
279         CONN_EVENT_START,
280         CONN_EVENT_STOP,
281
282         /**
283          * MUST be always the last element!!
284          */
285         NR_CONN_EVENTS,
286 };
287
288 static const char *conn_event_names[] = {
289         "Remote connection request",
290         "Remote connection acknowledge",
291         "Remote connection reject",
292         "Connection suspended",
293         "Connection resumed",
294         "Data received",
295         "Data sent",
296
297         "Timer",
298
299         "Start",
300         "Stop",
301 };
302
303 /**
304  * States of the connection statemachine.
305  */
306 enum conn_states {
307         /**
308          * Connection not assigned to any device,
309          * initial state, invalid
310          */
311         CONN_STATE_INVALID,
312
313         /**
314          * Userid assigned but not operating
315          */
316         CONN_STATE_STOPPED,
317
318         /**
319          * Connection registered,
320          * no connection request sent yet,
321          * no connection request received
322          */
323         CONN_STATE_STARTWAIT,
324
325         /**
326          * Connection registered and connection request sent,
327          * no acknowledge and no connection request received yet.
328          */
329         CONN_STATE_SETUPWAIT,
330
331         /**
332          * Connection up and running idle
333          */
334         CONN_STATE_IDLE,
335
336         /**
337          * Data sent, awaiting CONN_EVENT_TXDONE
338          */
339         CONN_STATE_TX,
340
341         /**
342          * Error during registration.
343          */
344         CONN_STATE_REGERR,
345
346         /**
347          * Error during registration.
348          */
349         CONN_STATE_CONNERR,
350
351         /**
352          * MUST be always the last element!!
353          */
354         NR_CONN_STATES,
355 };
356
357 static const char *conn_state_names[] = {
358         "Invalid",
359         "Stopped",
360         "StartWait",
361         "SetupWait",
362         "Idle",
363         "TX",
364         "Terminating",
365         "Registration error",
366         "Connect error",
367 };
368
369 \f
370 /**
371  * Callback-wrappers, called from lowlevel iucv layer.
372  *****************************************************************************/
373
374 static void
375 netiucv_callback_rx(iucv_MessagePending *eib, void *pgm_data)
376 {
377         struct iucv_connection *conn = (struct iucv_connection *)pgm_data;
378         struct iucv_event ev;
379
380         ev.conn = conn;
381         ev.data = (void *)eib;
382
383         fsm_event(conn->fsm, CONN_EVENT_RX, &ev);
384 }
385
386 static void
387 netiucv_callback_txdone(iucv_MessageComplete *eib, void *pgm_data)
388 {
389         struct iucv_connection *conn = (struct iucv_connection *)pgm_data;
390         struct iucv_event ev;
391
392         ev.conn = conn;
393         ev.data = (void *)eib;
394         fsm_event(conn->fsm, CONN_EVENT_TXDONE, &ev);
395 }
396
397 static void
398 netiucv_callback_connack(iucv_ConnectionComplete *eib, void *pgm_data)
399 {
400         struct iucv_connection *conn = (struct iucv_connection *)pgm_data;
401         struct iucv_event ev;
402
403         ev.conn = conn;
404         ev.data = (void *)eib;
405         fsm_event(conn->fsm, CONN_EVENT_CONN_ACK, &ev);
406 }
407
408 static void
409 netiucv_callback_connreq(iucv_ConnectionPending *eib, void *pgm_data)
410 {
411         struct iucv_connection *conn = (struct iucv_connection *)pgm_data;
412         struct iucv_event ev;
413
414         ev.conn = conn;
415         ev.data = (void *)eib;
416         fsm_event(conn->fsm, CONN_EVENT_CONN_REQ, &ev);
417 }
418
419 static void
420 netiucv_callback_connrej(iucv_ConnectionSevered *eib, void *pgm_data)
421 {
422         struct iucv_connection *conn = (struct iucv_connection *)pgm_data;
423         struct iucv_event ev;
424
425         ev.conn = conn;
426         ev.data = (void *)eib;
427         fsm_event(conn->fsm, CONN_EVENT_CONN_REJ, &ev);
428 }
429
430 static void
431 netiucv_callback_connsusp(iucv_ConnectionQuiesced *eib, void *pgm_data)
432 {
433         struct iucv_connection *conn = (struct iucv_connection *)pgm_data;
434         struct iucv_event ev;
435
436         ev.conn = conn;
437         ev.data = (void *)eib;
438         fsm_event(conn->fsm, CONN_EVENT_CONN_SUS, &ev);
439 }
440
441 static void
442 netiucv_callback_connres(iucv_ConnectionResumed *eib, void *pgm_data)
443 {
444         struct iucv_connection *conn = (struct iucv_connection *)pgm_data;
445         struct iucv_event ev;
446
447         ev.conn = conn;
448         ev.data = (void *)eib;
449         fsm_event(conn->fsm, CONN_EVENT_CONN_RES, &ev);
450 }
451
452 static iucv_interrupt_ops_t netiucv_ops = {
453         .ConnectionPending  = netiucv_callback_connreq,
454         .ConnectionComplete = netiucv_callback_connack,
455         .ConnectionSevered  = netiucv_callback_connrej,
456         .ConnectionQuiesced = netiucv_callback_connsusp,
457         .ConnectionResumed  = netiucv_callback_connres,
458         .MessagePending     = netiucv_callback_rx,
459         .MessageComplete    = netiucv_callback_txdone
460 };
461
462 /**
463  * Dummy NOP action for all statemachines
464  */
465 static void
466 fsm_action_nop(fsm_instance *fi, int event, void *arg)
467 {
468 }
469 \f
470 /**
471  * Actions of the connection statemachine
472  *****************************************************************************/
473
474 /**
475  * Helper function for conn_action_rx()
476  * Unpack a just received skb and hand it over to
477  * upper layers.
478  *
479  * @param conn The connection where this skb has been received.
480  * @param pskb The received skb.
481  */
482 //static __inline__ void
483 static void
484 netiucv_unpack_skb(struct iucv_connection *conn, struct sk_buff *pskb)
485 {
486         struct net_device     *dev = conn->netdev;
487         struct netiucv_priv   *privptr = dev->priv;
488         __u16          offset = 0;
489
490         skb_put(pskb, NETIUCV_HDRLEN);
491         pskb->dev = dev;
492         pskb->ip_summed = CHECKSUM_NONE;
493         pskb->protocol = ntohs(ETH_P_IP);
494
495         while (1) {
496                 struct sk_buff *skb;
497                 ll_header *header = (ll_header *)pskb->data;
498
499                 if (header->next == 0)
500                         break;
501
502                 skb_pull(pskb, NETIUCV_HDRLEN);
503                 header->next -= offset;
504                 offset += header->next;
505                 header->next -= NETIUCV_HDRLEN;
506                 if (skb_tailroom(pskb) < header->next) {
507                         printk(KERN_WARNING
508                                "%s: Illegal next field in iucv header: "
509                                "%d > %d\n",
510                                dev->name, header->next, skb_tailroom(pskb));
511                         return;
512                 }
513                 skb_put(pskb, header->next);
514                 pskb->mac.raw = pskb->data;
515                 skb = dev_alloc_skb(pskb->len);
516                 if (!skb) {
517                         printk(KERN_WARNING
518                                "%s Out of memory in netiucv_unpack_skb\n",
519                                dev->name);
520                         privptr->stats.rx_dropped++;
521                         return;
522                 }
523                 memcpy(skb_put(skb, pskb->len), pskb->data, pskb->len);
524                 skb->mac.raw = skb->data;
525                 skb->dev = pskb->dev;
526                 skb->protocol = pskb->protocol;
527                 pskb->ip_summed = CHECKSUM_UNNECESSARY;
528                 /*
529                  * Since receiving is always initiated from a tasklet (in iucv.c),
530                  * we must use netif_rx_ni() instead of netif_rx()
531                  */
532                 netif_rx_ni(skb);
533                 dev->last_rx = jiffies;
534                 privptr->stats.rx_packets++;
535                 privptr->stats.rx_bytes += skb->len;
536                 skb_pull(pskb, header->next);
537                 skb_put(pskb, NETIUCV_HDRLEN);
538         }
539 }
540
541 static void
542 conn_action_rx(fsm_instance *fi, int event, void *arg)
543 {
544         struct iucv_event *ev = (struct iucv_event *)arg;
545         struct iucv_connection *conn = ev->conn;
546         iucv_MessagePending *eib = (iucv_MessagePending *)ev->data;
547         struct netiucv_priv *privptr = (struct netiucv_priv *)conn->netdev->priv;
548
549         __u32 msglen = eib->ln1msg2.ipbfln1f;
550         int rc;
551
552         pr_debug("%s() called\n", __FUNCTION__);
553
554         if (!conn->netdev) {
555                 /* FRITZ: How to tell iucv LL to drop the msg? */
556                 printk(KERN_WARNING
557                        "Received data for unlinked connection\n"); 
558                 return;
559         }
560         if (msglen > conn->max_buffsize) {
561                 /* FRITZ: How to tell iucv LL to drop the msg? */
562                 privptr->stats.rx_dropped++;
563                 return;
564         }
565         conn->rx_buff->data = conn->rx_buff->tail = conn->rx_buff->head;
566         conn->rx_buff->len = 0;
567         rc = iucv_receive(conn->pathid, eib->ipmsgid, eib->iptrgcls,
568                           conn->rx_buff->data, msglen, NULL, NULL, NULL);
569         if (rc != 0 || msglen < 5) {
570                 privptr->stats.rx_errors++;
571                 printk(KERN_INFO "iucv_receive returned %08x\n", rc);
572                 return;
573         }
574         netiucv_unpack_skb(conn, conn->rx_buff);
575 }
576
577 static void
578 conn_action_txdone(fsm_instance *fi, int event, void *arg)
579 {
580         struct iucv_event *ev = (struct iucv_event *)arg;
581         struct iucv_connection *conn = ev->conn;
582         iucv_MessageComplete *eib = (iucv_MessageComplete *)ev->data;
583         struct netiucv_priv *privptr = NULL;
584                                  /* Shut up, gcc! skb is always below 2G. */
585         __u32 single_flag = eib->ipmsgtag;
586         __u32 txbytes = 0;
587         __u32 txpackets = 0;
588         __u32 stat_maxcq = 0;
589         struct sk_buff *skb;
590         unsigned long saveflags;
591         ll_header header;
592
593         pr_debug("%s() called\n", __FUNCTION__);
594
595         if (conn && conn->netdev && conn->netdev->priv)
596                 privptr = (struct netiucv_priv *)conn->netdev->priv;
597         conn->prof.tx_pending--;
598         if (single_flag) {
599                 if ((skb = skb_dequeue(&conn->commit_queue))) {
600                         atomic_dec(&skb->users);
601                         dev_kfree_skb_any(skb);
602                         if (privptr) {
603                                 privptr->stats.tx_packets++;
604                                 privptr->stats.tx_bytes +=
605                                         (skb->len - NETIUCV_HDRLEN
606                                                   - NETIUCV_HDRLEN);
607                         }
608                 }
609         }
610         conn->tx_buff->data = conn->tx_buff->tail = conn->tx_buff->head;
611         conn->tx_buff->len = 0;
612         spin_lock_irqsave(&conn->collect_lock, saveflags);
613         while ((skb = skb_dequeue(&conn->collect_queue))) {
614                 header.next = conn->tx_buff->len + skb->len + NETIUCV_HDRLEN;
615                 memcpy(skb_put(conn->tx_buff, NETIUCV_HDRLEN), &header,
616                        NETIUCV_HDRLEN);
617                 memcpy(skb_put(conn->tx_buff, skb->len), skb->data, skb->len);
618                 txbytes += skb->len;
619                 txpackets++;
620                 stat_maxcq++;
621                 atomic_dec(&skb->users);
622                 dev_kfree_skb_any(skb);
623         }
624         if (conn->collect_len > conn->prof.maxmulti)
625                 conn->prof.maxmulti = conn->collect_len;
626         conn->collect_len = 0;
627         spin_unlock_irqrestore(&conn->collect_lock, saveflags);
628         if (conn->tx_buff->len) {
629                 int rc;
630
631                 header.next = 0;
632                 memcpy(skb_put(conn->tx_buff, NETIUCV_HDRLEN), &header,
633                        NETIUCV_HDRLEN);
634
635                 conn->prof.send_stamp = xtime;
636                 rc = iucv_send(conn->pathid, NULL, 0, 0, 0, 0,
637                                conn->tx_buff->data, conn->tx_buff->len);
638                 conn->prof.doios_multi++;
639                 conn->prof.txlen += conn->tx_buff->len;
640                 conn->prof.tx_pending++;
641                 if (conn->prof.tx_pending > conn->prof.tx_max_pending)
642                         conn->prof.tx_max_pending = conn->prof.tx_pending;
643                 if (rc != 0) {
644                         conn->prof.tx_pending--;
645                         fsm_newstate(fi, CONN_STATE_IDLE);
646                         if (privptr)
647                                 privptr->stats.tx_errors += txpackets;
648                         printk(KERN_INFO "iucv_send returned %08x\n",
649                                 rc);
650                 } else {
651                         if (privptr) {
652                                 privptr->stats.tx_packets += txpackets;
653                                 privptr->stats.tx_bytes += txbytes;
654                         }
655                         if (stat_maxcq > conn->prof.maxcqueue)
656                                 conn->prof.maxcqueue = stat_maxcq;
657                 }
658         } else
659                 fsm_newstate(fi, CONN_STATE_IDLE);
660 }
661
662 static void
663 conn_action_connaccept(fsm_instance *fi, int event, void *arg)
664 {
665         struct iucv_event *ev = (struct iucv_event *)arg;
666         struct iucv_connection *conn = ev->conn;
667         iucv_ConnectionPending *eib = (iucv_ConnectionPending *)ev->data;
668         struct net_device *netdev = conn->netdev;
669         struct netiucv_priv *privptr = (struct netiucv_priv *)netdev->priv;
670         int rc;
671         __u16 msglimit;
672         __u8 udata[16];
673
674         pr_debug("%s() called\n", __FUNCTION__);
675
676         rc = iucv_accept(eib->ippathid, NETIUCV_QUEUELEN_DEFAULT, udata, 0,
677                          conn->handle, conn, NULL, &msglimit);
678         if (rc != 0) {
679                 printk(KERN_WARNING
680                        "%s: IUCV accept failed with error %d\n",
681                        netdev->name, rc);
682                 return;
683         }
684         fsm_newstate(fi, CONN_STATE_IDLE);
685         conn->pathid = eib->ippathid;
686         netdev->tx_queue_len = msglimit;
687         fsm_event(privptr->fsm, DEV_EVENT_CONUP, netdev);
688 }
689
690 static void
691 conn_action_connreject(fsm_instance *fi, int event, void *arg)
692 {
693         struct iucv_event *ev = (struct iucv_event *)arg;
694         struct iucv_connection *conn = ev->conn;
695         struct net_device *netdev = conn->netdev;
696         iucv_ConnectionPending *eib = (iucv_ConnectionPending *)ev->data;
697         __u8 udata[16];
698
699         pr_debug("%s() called\n", __FUNCTION__);
700
701         iucv_sever(eib->ippathid, udata);
702         if (eib->ippathid != conn->pathid) {
703                 printk(KERN_INFO
704                         "%s: IR pathid %d does not match original pathid %d\n",
705                         netdev->name, eib->ippathid, conn->pathid);
706                 iucv_sever(conn->pathid, udata);
707         }
708 }
709
710 static void
711 conn_action_connack(fsm_instance *fi, int event, void *arg)
712 {
713         struct iucv_event *ev = (struct iucv_event *)arg;
714         struct iucv_connection *conn = ev->conn;
715         iucv_ConnectionComplete *eib = (iucv_ConnectionComplete *)ev->data;
716         struct net_device *netdev = conn->netdev;
717         struct netiucv_priv *privptr = (struct netiucv_priv *)netdev->priv;
718
719         pr_debug("%s() called\n", __FUNCTION__);
720
721         fsm_deltimer(&conn->timer);
722         fsm_newstate(fi, CONN_STATE_IDLE);
723         if (eib->ippathid != conn->pathid) {
724                 printk(KERN_INFO
725                         "%s: IR pathid %d does not match original pathid %d\n",
726                         netdev->name, eib->ippathid, conn->pathid);
727                 conn->pathid = eib->ippathid;
728         }
729         netdev->tx_queue_len = eib->ipmsglim;
730         fsm_event(privptr->fsm, DEV_EVENT_CONUP, netdev);
731 }
732
733 static void
734 conn_action_conntimsev(fsm_instance *fi, int event, void *arg)
735 {
736         struct iucv_connection *conn = (struct iucv_connection *)arg;
737         __u8 udata[16];
738
739         pr_debug("%s() called\n", __FUNCTION__);
740
741         fsm_deltimer(&conn->timer);
742         iucv_sever(conn->pathid, udata);
743         fsm_newstate(fi, CONN_STATE_STARTWAIT);
744 }
745
746 static void
747 conn_action_connsever(fsm_instance *fi, int event, void *arg)
748 {
749         struct iucv_event *ev = (struct iucv_event *)arg;
750         struct iucv_connection *conn = ev->conn;
751         struct net_device *netdev = conn->netdev;
752         struct netiucv_priv *privptr = (struct netiucv_priv *)netdev->priv;
753         __u8 udata[16];
754
755         pr_debug("%s() called\n", __FUNCTION__);
756
757         fsm_deltimer(&conn->timer);
758         iucv_sever(conn->pathid, udata);
759         printk(KERN_INFO "%s: Remote dropped connection\n",
760                netdev->name);
761         fsm_newstate(fi, CONN_STATE_STARTWAIT);
762         fsm_event(privptr->fsm, DEV_EVENT_CONDOWN, netdev);
763 }
764
765 static void
766 conn_action_start(fsm_instance *fi, int event, void *arg)
767 {
768         struct iucv_event *ev = (struct iucv_event *)arg;
769         struct iucv_connection *conn = ev->conn;
770         __u16 msglimit;
771         int rc;
772
773         pr_debug("%s() called\n", __FUNCTION__);
774
775         if (conn->handle == 0) {
776                 conn->handle =
777                         iucv_register_program(iucvMagic, conn->userid, mask,
778                                               &netiucv_ops, conn);
779                 fsm_newstate(fi, CONN_STATE_STARTWAIT);
780                 if (conn->handle <= 0) {
781                         fsm_newstate(fi, CONN_STATE_REGERR);
782                         conn->handle = 0;
783                         return;
784                 }
785
786                 pr_debug("%s('%s'): registered successfully\n",
787                          conn->netdev->name, conn->userid);
788         }
789
790         pr_debug("%s('%s'): connecting ...\n",
791                  conn->netdev->name, conn->userid);
792
793         /* We must set the state before calling iucv_connect because the callback
794          * handler could be called at any point after the connection request is
795          * sent */
796
797         fsm_newstate(fi, CONN_STATE_SETUPWAIT);
798         rc = iucv_connect(&(conn->pathid), NETIUCV_QUEUELEN_DEFAULT, iucvMagic,
799                           conn->userid, iucv_host, 0, NULL, &msglimit, conn->handle,
800                           conn);
801         switch (rc) {
802                 case 0:
803                         conn->netdev->tx_queue_len = msglimit;
804                         fsm_addtimer(&conn->timer, NETIUCV_TIMEOUT_5SEC,
805                                 CONN_EVENT_TIMER, conn);
806                         return;
807                 case 11:
808                         printk(KERN_NOTICE
809                                "%s: User %s is currently not available.\n",
810                                conn->netdev->name,
811                                netiucv_printname(conn->userid));
812                         fsm_newstate(fi, CONN_STATE_STARTWAIT);
813                         return;
814                 case 12:
815                         printk(KERN_NOTICE
816                                "%s: User %s is currently not ready.\n",
817                                conn->netdev->name,
818                                netiucv_printname(conn->userid));
819                         fsm_newstate(fi, CONN_STATE_STARTWAIT);
820                         return;
821                 case 13:
822                         printk(KERN_WARNING
823                                "%s: Too many IUCV connections.\n",
824                                conn->netdev->name);
825                         fsm_newstate(fi, CONN_STATE_CONNERR);
826                         break;
827                 case 14:
828                         printk(KERN_WARNING
829                                "%s: User %s has too many IUCV connections.\n",
830                                conn->netdev->name,
831                                netiucv_printname(conn->userid));
832                         fsm_newstate(fi, CONN_STATE_CONNERR);
833                         break;
834                 case 15:
835                         printk(KERN_WARNING
836                                "%s: No IUCV authorization in CP directory.\n",
837                                conn->netdev->name);
838                         fsm_newstate(fi, CONN_STATE_CONNERR);
839                         break;
840                 default:
841                         printk(KERN_WARNING
842                                "%s: iucv_connect returned error %d\n",
843                                conn->netdev->name, rc);
844                         fsm_newstate(fi, CONN_STATE_CONNERR);
845                         break;
846         }
847         iucv_unregister_program(conn->handle);
848         conn->handle = 0;
849 }
850
851 static void
852 netiucv_purge_skb_queue(struct sk_buff_head *q)
853 {
854         struct sk_buff *skb;
855
856         while ((skb = skb_dequeue(q))) {
857                 atomic_dec(&skb->users);
858                 dev_kfree_skb_any(skb);
859         }
860 }
861
862 static void
863 conn_action_stop(fsm_instance *fi, int event, void *arg)
864 {
865         struct iucv_event *ev = (struct iucv_event *)arg;
866         struct iucv_connection *conn = ev->conn;
867         struct net_device *netdev = conn->netdev;
868         struct netiucv_priv *privptr = (struct netiucv_priv *)netdev->priv;
869
870         pr_debug("%s() called\n", __FUNCTION__);
871
872         fsm_deltimer(&conn->timer);
873         fsm_newstate(fi, CONN_STATE_STOPPED);
874         netiucv_purge_skb_queue(&conn->collect_queue);
875         if (conn->handle)
876                 iucv_unregister_program(conn->handle);
877         conn->handle = 0;
878         netiucv_purge_skb_queue(&conn->commit_queue);
879         fsm_event(privptr->fsm, DEV_EVENT_CONDOWN, netdev);
880 }
881
882 static void
883 conn_action_inval(fsm_instance *fi, int event, void *arg)
884 {
885         struct iucv_event *ev = (struct iucv_event *)arg;
886         struct iucv_connection *conn = ev->conn;
887         struct net_device *netdev = conn->netdev;
888
889         printk(KERN_WARNING
890                "%s: Cannot connect without username\n",
891                netdev->name);
892 }
893
894 static const fsm_node conn_fsm[] = {
895         { CONN_STATE_INVALID,   CONN_EVENT_START,    conn_action_inval      },
896         { CONN_STATE_STOPPED,   CONN_EVENT_START,    conn_action_start      },
897
898         { CONN_STATE_STOPPED,   CONN_EVENT_STOP,     conn_action_stop       },
899         { CONN_STATE_STARTWAIT, CONN_EVENT_STOP,     conn_action_stop       },
900         { CONN_STATE_SETUPWAIT, CONN_EVENT_STOP,     conn_action_stop       },
901         { CONN_STATE_IDLE,      CONN_EVENT_STOP,     conn_action_stop       },
902         { CONN_STATE_TX,        CONN_EVENT_STOP,     conn_action_stop       },
903         { CONN_STATE_REGERR,    CONN_EVENT_STOP,     conn_action_stop       },
904         { CONN_STATE_CONNERR,   CONN_EVENT_STOP,     conn_action_stop       },
905
906         { CONN_STATE_STOPPED,   CONN_EVENT_CONN_REQ, conn_action_connreject },
907         { CONN_STATE_STARTWAIT, CONN_EVENT_CONN_REQ, conn_action_connaccept },
908         { CONN_STATE_SETUPWAIT, CONN_EVENT_CONN_REQ, conn_action_connaccept },
909         { CONN_STATE_IDLE,      CONN_EVENT_CONN_REQ, conn_action_connreject },
910         { CONN_STATE_TX,        CONN_EVENT_CONN_REQ, conn_action_connreject },
911
912         { CONN_STATE_SETUPWAIT, CONN_EVENT_CONN_ACK, conn_action_connack    },
913         { CONN_STATE_SETUPWAIT, CONN_EVENT_TIMER,    conn_action_conntimsev },
914
915         { CONN_STATE_SETUPWAIT, CONN_EVENT_CONN_REJ, conn_action_connsever  },
916         { CONN_STATE_IDLE,      CONN_EVENT_CONN_REJ, conn_action_connsever  },
917         { CONN_STATE_TX,        CONN_EVENT_CONN_REJ, conn_action_connsever  },
918
919         { CONN_STATE_IDLE,      CONN_EVENT_RX,       conn_action_rx         },
920         { CONN_STATE_TX,        CONN_EVENT_RX,       conn_action_rx         },
921
922         { CONN_STATE_TX,        CONN_EVENT_TXDONE,   conn_action_txdone     },
923         { CONN_STATE_IDLE,      CONN_EVENT_TXDONE,   conn_action_txdone     },
924 };
925
926 static const int CONN_FSM_LEN = sizeof(conn_fsm) / sizeof(fsm_node);
927
928 \f
929 /**
930  * Actions for interface - statemachine.
931  *****************************************************************************/
932
933 /**
934  * Startup connection by sending CONN_EVENT_START to it.
935  *
936  * @param fi    An instance of an interface statemachine.
937  * @param event The event, just happened.
938  * @param arg   Generic pointer, casted from struct net_device * upon call.
939  */
940 static void
941 dev_action_start(fsm_instance *fi, int event, void *arg)
942 {
943         struct net_device   *dev = (struct net_device *)arg;
944         struct netiucv_priv *privptr = dev->priv;
945         struct iucv_event   ev;
946
947         pr_debug("%s() called\n", __FUNCTION__);
948
949         ev.conn = privptr->conn;
950         fsm_newstate(fi, DEV_STATE_STARTWAIT);
951         fsm_event(privptr->conn->fsm, CONN_EVENT_START, &ev);
952 }
953
954 /**
955  * Shutdown connection by sending CONN_EVENT_STOP to it.
956  *
957  * @param fi    An instance of an interface statemachine.
958  * @param event The event, just happened.
959  * @param arg   Generic pointer, casted from struct net_device * upon call.
960  */
961 static void
962 dev_action_stop(fsm_instance *fi, int event, void *arg)
963 {
964         struct net_device   *dev = (struct net_device *)arg;
965         struct netiucv_priv *privptr = dev->priv;
966         struct iucv_event   ev;
967
968         pr_debug("%s() called\n", __FUNCTION__);
969
970         ev.conn = privptr->conn;
971
972         fsm_newstate(fi, DEV_STATE_STOPWAIT);
973         fsm_event(privptr->conn->fsm, CONN_EVENT_STOP, &ev);
974 }
975
976 /**
977  * Called from connection statemachine
978  * when a connection is up and running.
979  *
980  * @param fi    An instance of an interface statemachine.
981  * @param event The event, just happened.
982  * @param arg   Generic pointer, casted from struct net_device * upon call.
983  */
984 static void
985 dev_action_connup(fsm_instance *fi, int event, void *arg)
986 {
987         struct net_device   *dev = (struct net_device *)arg;
988         struct netiucv_priv *privptr = dev->priv;
989
990         pr_debug("%s() called\n", __FUNCTION__);
991
992         switch (fsm_getstate(fi)) {
993                 case DEV_STATE_STARTWAIT:
994                         fsm_newstate(fi, DEV_STATE_RUNNING);
995                         printk(KERN_INFO
996                                "%s: connected with remote side %s\n",
997                                dev->name, privptr->conn->userid);
998                         break;
999                 case DEV_STATE_STOPWAIT:
1000                         printk(KERN_INFO
1001                                "%s: got connection UP event during shutdown!!\n",
1002                                dev->name);
1003                         break;
1004         }
1005 }
1006
1007 /**
1008  * Called from connection statemachine
1009  * when a connection has been shutdown.
1010  *
1011  * @param fi    An instance of an interface statemachine.
1012  * @param event The event, just happened.
1013  * @param arg   Generic pointer, casted from struct net_device * upon call.
1014  */
1015 static void
1016 dev_action_conndown(fsm_instance *fi, int event, void *arg)
1017 {
1018         pr_debug("%s() called\n", __FUNCTION__);
1019
1020         switch (fsm_getstate(fi)) {
1021                 case DEV_STATE_RUNNING:
1022                         fsm_newstate(fi, DEV_STATE_STARTWAIT);
1023                         break;
1024                 case DEV_STATE_STOPWAIT:
1025                         fsm_newstate(fi, DEV_STATE_STOPPED);
1026                         break;
1027         }
1028 }
1029
1030 static const fsm_node dev_fsm[] = {
1031         { DEV_STATE_STOPPED,    DEV_EVENT_START,   dev_action_start    },
1032
1033         { DEV_STATE_STOPWAIT,   DEV_EVENT_START,   dev_action_start    },
1034         { DEV_STATE_STOPWAIT,   DEV_EVENT_CONDOWN, dev_action_conndown },
1035
1036         { DEV_STATE_STARTWAIT,  DEV_EVENT_STOP,    dev_action_stop     },
1037         { DEV_STATE_STARTWAIT,  DEV_EVENT_CONUP,   dev_action_connup   },
1038
1039         { DEV_STATE_RUNNING,    DEV_EVENT_STOP,    dev_action_stop     },
1040         { DEV_STATE_RUNNING,    DEV_EVENT_CONDOWN, dev_action_conndown },
1041         { DEV_STATE_RUNNING,    DEV_EVENT_CONUP,   fsm_action_nop      },
1042 };
1043
1044 static const int DEV_FSM_LEN = sizeof(dev_fsm) / sizeof(fsm_node);
1045
1046 /**
1047  * Transmit a packet.
1048  * This is a helper function for netiucv_tx().
1049  *
1050  * @param conn Connection to be used for sending.
1051  * @param skb Pointer to struct sk_buff of packet to send.
1052  *            The linklevel header has already been set up
1053  *            by netiucv_tx().
1054  *
1055  * @return 0 on success, -ERRNO on failure. (Never fails.)
1056  */
1057 static int
1058 netiucv_transmit_skb(struct iucv_connection *conn, struct sk_buff *skb) {
1059         unsigned long saveflags;
1060         ll_header header;
1061         int       rc = 0;
1062
1063         if (fsm_getstate(conn->fsm) != CONN_STATE_IDLE) {
1064                 int l = skb->len + NETIUCV_HDRLEN;
1065
1066                 spin_lock_irqsave(&conn->collect_lock, saveflags);
1067                 if (conn->collect_len + l >
1068                     (conn->max_buffsize - NETIUCV_HDRLEN))
1069                         rc = -EBUSY;
1070                 else {
1071                         atomic_inc(&skb->users);
1072                         skb_queue_tail(&conn->collect_queue, skb);
1073                         conn->collect_len += l;
1074                 }
1075                 spin_unlock_irqrestore(&conn->collect_lock, saveflags);
1076         } else {
1077                 struct sk_buff *nskb = skb;
1078                 /**
1079                  * Copy the skb to a new allocated skb in lowmem only if the
1080                  * data is located above 2G in memory or tailroom is < 2.
1081                  */
1082                 unsigned long hi =
1083                         ((unsigned long)(skb->tail + NETIUCV_HDRLEN)) >> 31;
1084                 int copied = 0;
1085                 if (hi || (skb_tailroom(skb) < 2)) {
1086                         nskb = alloc_skb(skb->len + NETIUCV_HDRLEN +
1087                                          NETIUCV_HDRLEN, GFP_ATOMIC | GFP_DMA);
1088                         if (!nskb) {
1089                                 printk(KERN_WARNING
1090                                        "%s: Could not allocate tx_skb\n",
1091                                        conn->netdev->name);
1092                                 rc = -ENOMEM;
1093                                 return rc;
1094                         } else {
1095                                 skb_reserve(nskb, NETIUCV_HDRLEN);
1096                                 memcpy(skb_put(nskb, skb->len),
1097                                        skb->data, skb->len);
1098                         }
1099                         copied = 1;
1100                 }
1101                 /**
1102                  * skb now is below 2G and has enough room. Add headers.
1103                  */
1104                 header.next = nskb->len + NETIUCV_HDRLEN;
1105                 memcpy(skb_push(nskb, NETIUCV_HDRLEN), &header, NETIUCV_HDRLEN);
1106                 header.next = 0;
1107                 memcpy(skb_put(nskb, NETIUCV_HDRLEN), &header,  NETIUCV_HDRLEN);
1108
1109                 fsm_newstate(conn->fsm, CONN_STATE_TX);
1110                 conn->prof.send_stamp = xtime;
1111                 
1112                 rc = iucv_send(conn->pathid, NULL, 0, 0, 1 /* single_flag */,
1113                         0, nskb->data, nskb->len);
1114                                /* Shut up, gcc! nskb is always below 2G. */
1115                 conn->prof.doios_single++;
1116                 conn->prof.txlen += skb->len;
1117                 conn->prof.tx_pending++;
1118                 if (conn->prof.tx_pending > conn->prof.tx_max_pending)
1119                         conn->prof.tx_max_pending = conn->prof.tx_pending;
1120                 if (rc != 0) {
1121                         struct netiucv_priv *privptr;
1122                         fsm_newstate(conn->fsm, CONN_STATE_IDLE);
1123                         conn->prof.tx_pending--;
1124                         privptr = (struct netiucv_priv *)conn->netdev->priv;
1125                         if (privptr)
1126                                 privptr->stats.tx_errors++;
1127                         if (copied)
1128                                 dev_kfree_skb(nskb);
1129                         else {
1130                                 /**
1131                                  * Remove our headers. They get added
1132                                  * again on retransmit.
1133                                  */
1134                                 skb_pull(skb, NETIUCV_HDRLEN);
1135                                 skb_trim(skb, skb->len - NETIUCV_HDRLEN);
1136                         }
1137                         printk(KERN_INFO "iucv_send returned %08x\n",
1138                                 rc);
1139                 } else {
1140                         if (copied)
1141                                 dev_kfree_skb(skb);
1142                         atomic_inc(&nskb->users);
1143                         skb_queue_tail(&conn->commit_queue, nskb);
1144                 }
1145         }
1146
1147         return rc;
1148 }
1149 \f
1150 /**
1151  * Interface API for upper network layers
1152  *****************************************************************************/
1153
1154 /**
1155  * Open an interface.
1156  * Called from generic network layer when ifconfig up is run.
1157  *
1158  * @param dev Pointer to interface struct.
1159  *
1160  * @return 0 on success, -ERRNO on failure. (Never fails.)
1161  */
1162 static int
1163 netiucv_open(struct net_device *dev) {
1164         fsm_event(((struct netiucv_priv *)dev->priv)->fsm, DEV_EVENT_START, dev);
1165         return 0;
1166 }
1167
1168 /**
1169  * Close an interface.
1170  * Called from generic network layer when ifconfig down is run.
1171  *
1172  * @param dev Pointer to interface struct.
1173  *
1174  * @return 0 on success, -ERRNO on failure. (Never fails.)
1175  */
1176 static int
1177 netiucv_close(struct net_device *dev) {
1178         fsm_event(((struct netiucv_priv *)dev->priv)->fsm, DEV_EVENT_STOP, dev);
1179         return 0;
1180 }
1181
1182 /**
1183  * Start transmission of a packet.
1184  * Called from generic network device layer.
1185  *
1186  * @param skb Pointer to buffer containing the packet.
1187  * @param dev Pointer to interface struct.
1188  *
1189  * @return 0 if packet consumed, !0 if packet rejected.
1190  *         Note: If we return !0, then the packet is free'd by
1191  *               the generic network layer.
1192  */
1193 static int netiucv_tx(struct sk_buff *skb, struct net_device *dev)
1194 {
1195         int          rc = 0;
1196         struct netiucv_priv *privptr = dev->priv;
1197
1198         /**
1199          * Some sanity checks ...
1200          */
1201         if (skb == NULL) {
1202                 printk(KERN_WARNING "%s: NULL sk_buff passed\n", dev->name);
1203                 privptr->stats.tx_dropped++;
1204                 return 0;
1205         }
1206         if (skb_headroom(skb) < (NETIUCV_HDRLEN)) {
1207                 printk(KERN_WARNING
1208                        "%s: Got sk_buff with head room < %ld bytes\n",
1209                        dev->name, NETIUCV_HDRLEN);
1210                 dev_kfree_skb(skb);
1211                 privptr->stats.tx_dropped++;
1212                 return 0;
1213         }
1214
1215         /**
1216          * If connection is not running, try to restart it
1217          * and throw away packet. 
1218          */
1219         if (fsm_getstate(privptr->fsm) != DEV_STATE_RUNNING) {
1220                 fsm_event(privptr->fsm, DEV_EVENT_START, dev);
1221                 dev_kfree_skb(skb);
1222                 privptr->stats.tx_dropped++;
1223                 privptr->stats.tx_errors++;
1224                 privptr->stats.tx_carrier_errors++;
1225                 return 0;
1226         }
1227
1228         if (netiucv_test_and_set_busy(dev))
1229                 return -EBUSY;
1230
1231         dev->trans_start = jiffies;
1232         if (netiucv_transmit_skb(privptr->conn, skb) != 0)
1233                 rc = 1;
1234         netiucv_clear_busy(dev);
1235         return rc;
1236 }
1237
1238 /**
1239  * Returns interface statistics of a device.
1240  *
1241  * @param dev Pointer to interface struct.
1242  *
1243  * @return Pointer to stats struct of this interface.
1244  */
1245 static struct net_device_stats *
1246 netiucv_stats (struct net_device * dev)
1247 {
1248         return &((struct netiucv_priv *)dev->priv)->stats;
1249 }
1250
1251 /**
1252  * Sets MTU of an interface.
1253  *
1254  * @param dev     Pointer to interface struct.
1255  * @param new_mtu The new MTU to use for this interface.
1256  *
1257  * @return 0 on success, -EINVAL if MTU is out of valid range.
1258  *         (valid range is 576 .. NETIUCV_MTU_MAX).
1259  */
1260 static int
1261 netiucv_change_mtu (struct net_device * dev, int new_mtu)
1262 {
1263         if ((new_mtu < 576) || (new_mtu > NETIUCV_MTU_MAX))
1264                 return -EINVAL;
1265         dev->mtu = new_mtu;
1266         return 0;
1267 }
1268
1269 /**
1270  * attributes in sysfs
1271  *****************************************************************************/
1272
1273 static ssize_t
1274 user_show (struct device *dev, char *buf)
1275 {
1276         struct netiucv_priv *priv = dev->driver_data;
1277
1278         return sprintf(buf, "%s\n", netiucv_printname(priv->conn->userid));
1279 }
1280
1281 static ssize_t
1282 user_write (struct device *dev, const char *buf, size_t count)
1283 {
1284         struct netiucv_priv *priv = dev->driver_data;
1285         struct net_device *ndev = priv->conn->netdev;
1286         char    *p;
1287         char    *tmp;
1288         char    username[10];
1289         int     i;
1290
1291         if (count>9) {
1292                 printk(KERN_WARNING
1293                         "netiucv: username too long (%d)!\n", (int)count);
1294                 return -EINVAL;
1295         }
1296
1297         tmp = strsep((char **) &buf, "\n");
1298         for (i=0, p=tmp; i<8 && *p; i++, p++) {
1299                 if (isalnum(*p) || (*p == '$'))
1300                         username[i]= *p;
1301                 else if (*p == '\n') {
1302                         /* trailing lf, grr */
1303                         break;
1304                 } else {
1305                         printk(KERN_WARNING
1306                                 "netiucv: Invalid character in username!\n");
1307                         return -EINVAL;
1308                 }
1309         }
1310         while (i<9)
1311                 username[i++] = ' ';
1312         username[9] = '\0';
1313
1314         if (memcmp(username, priv->conn->userid, 8) != 0) {
1315                 /* username changed */
1316                 if (ndev->flags & (IFF_UP | IFF_RUNNING)) {
1317                         printk(KERN_WARNING
1318                                 "netiucv: device %s active, connected to %s\n",
1319                                 dev->bus_id, priv->conn->userid);
1320                         printk(KERN_WARNING
1321                                 "netiucv: user cannot be updated\n");
1322                         return -EBUSY;
1323                 }
1324         }
1325         memcpy(priv->conn->userid, username, 9);
1326
1327         return count;
1328
1329 }
1330
1331 static DEVICE_ATTR(user, 0644, user_show, user_write);
1332
1333 static ssize_t
1334 buffer_show (struct device *dev, char *buf)
1335 {
1336         struct netiucv_priv *priv = dev->driver_data;
1337
1338         return sprintf(buf, "%d\n", priv->conn->max_buffsize);
1339 }
1340
1341 static ssize_t
1342 buffer_write (struct device *dev, const char *buf, size_t count)
1343 {
1344         struct netiucv_priv *priv = dev->driver_data;
1345         struct net_device *ndev = priv->conn->netdev;
1346         char         *e;
1347         int          bs1;
1348
1349         if (count >= 39)
1350                 return -EINVAL;
1351
1352         bs1 = simple_strtoul(buf, &e, 0);
1353
1354         if (e && (!isspace(*e))) {
1355                 printk(KERN_WARNING
1356                         "netiucv: Invalid character in buffer!\n");
1357                 return -EINVAL;
1358         }
1359         if (bs1 > NETIUCV_BUFSIZE_MAX) {
1360                 printk(KERN_WARNING
1361                         "netiucv: Given buffer size %d too large.\n",
1362                         bs1);
1363
1364                 return -EINVAL;
1365         }
1366         if ((ndev->flags & IFF_RUNNING) &&
1367             (bs1 < (ndev->mtu + NETIUCV_HDRLEN + 2)))
1368                 return -EINVAL;
1369         if (bs1 < (576 + NETIUCV_HDRLEN + NETIUCV_HDRLEN))
1370                 return -EINVAL;
1371
1372         priv->conn->max_buffsize = bs1;
1373         if (!(ndev->flags & IFF_RUNNING))
1374                 ndev->mtu = bs1 - NETIUCV_HDRLEN - NETIUCV_HDRLEN;
1375         priv->conn->flags |= CONN_FLAGS_BUFSIZE_CHANGED;
1376
1377         return count;
1378
1379 }
1380
1381 static DEVICE_ATTR(buffer, 0644, buffer_show, buffer_write);
1382
1383 static ssize_t
1384 dev_fsm_show (struct device *dev, char *buf)
1385 {
1386         struct netiucv_priv *priv = dev->driver_data;
1387         
1388         return sprintf(buf, "%s\n", fsm_getstate_str(priv->fsm));
1389 }
1390
1391 static DEVICE_ATTR(device_fsm_state, 0444, dev_fsm_show, NULL);
1392
1393 static ssize_t
1394 conn_fsm_show (struct device *dev, char *buf)
1395 {
1396         struct netiucv_priv *priv = dev->driver_data;
1397         
1398         return sprintf(buf, "%s\n", fsm_getstate_str(priv->conn->fsm));
1399 }
1400
1401 static DEVICE_ATTR(connection_fsm_state, 0444, conn_fsm_show, NULL);
1402
1403 static ssize_t
1404 maxmulti_show (struct device *dev, char *buf)
1405 {
1406         struct netiucv_priv *priv = dev->driver_data;
1407         
1408         return sprintf(buf, "%ld\n", priv->conn->prof.maxmulti);
1409 }
1410
1411 static ssize_t
1412 maxmulti_write (struct device *dev, const char *buf, size_t count)
1413 {
1414         struct netiucv_priv *priv = dev->driver_data;
1415         
1416         priv->conn->prof.maxmulti = 0;
1417         return count;
1418 }
1419
1420 static DEVICE_ATTR(max_tx_buffer_used, 0644, maxmulti_show, maxmulti_write);
1421
1422 static ssize_t
1423 maxcq_show (struct device *dev, char *buf)
1424 {
1425         struct netiucv_priv *priv = dev->driver_data;
1426         
1427         return sprintf(buf, "%ld\n", priv->conn->prof.maxcqueue);
1428 }
1429
1430 static ssize_t
1431 maxcq_write (struct device *dev, const char *buf, size_t count)
1432 {
1433         struct netiucv_priv *priv = dev->driver_data;
1434         
1435         priv->conn->prof.maxcqueue = 0;
1436         return count;
1437 }
1438
1439 static DEVICE_ATTR(max_chained_skbs, 0644, maxcq_show, maxcq_write);
1440
1441 static ssize_t
1442 sdoio_show (struct device *dev, char *buf)
1443 {
1444         struct netiucv_priv *priv = dev->driver_data;
1445         
1446         return sprintf(buf, "%ld\n", priv->conn->prof.doios_single);
1447 }
1448
1449 static ssize_t
1450 sdoio_write (struct device *dev, const char *buf, size_t count)
1451 {
1452         struct netiucv_priv *priv = dev->driver_data;
1453         
1454         priv->conn->prof.doios_single = 0;
1455         return count;
1456 }
1457
1458 static DEVICE_ATTR(tx_single_write_ops, 0644, sdoio_show, sdoio_write);
1459
1460 static ssize_t
1461 mdoio_show (struct device *dev, char *buf)
1462 {
1463         struct netiucv_priv *priv = dev->driver_data;
1464         
1465         return sprintf(buf, "%ld\n", priv->conn->prof.doios_multi);
1466 }
1467
1468 static ssize_t
1469 mdoio_write (struct device *dev, const char *buf, size_t count)
1470 {
1471         struct netiucv_priv *priv = dev->driver_data;
1472         
1473         priv->conn->prof.doios_multi = 0;
1474         return count;
1475 }
1476
1477 static DEVICE_ATTR(tx_multi_write_ops, 0644, mdoio_show, mdoio_write);
1478
1479 static ssize_t
1480 txlen_show (struct device *dev, char *buf)
1481 {
1482         struct netiucv_priv *priv = dev->driver_data;
1483         
1484         return sprintf(buf, "%ld\n", priv->conn->prof.txlen);
1485 }
1486
1487 static ssize_t
1488 txlen_write (struct device *dev, const char *buf, size_t count)
1489 {
1490         struct netiucv_priv *priv = dev->driver_data;
1491         
1492         priv->conn->prof.txlen = 0;
1493         return count;
1494 }
1495
1496 static DEVICE_ATTR(netto_bytes, 0644, txlen_show, txlen_write);
1497
1498 static ssize_t
1499 txtime_show (struct device *dev, char *buf)
1500 {
1501         struct netiucv_priv *priv = dev->driver_data;
1502         
1503         return sprintf(buf, "%ld\n", priv->conn->prof.tx_time);
1504 }
1505
1506 static ssize_t
1507 txtime_write (struct device *dev, const char *buf, size_t count)
1508 {
1509         struct netiucv_priv *priv = dev->driver_data;
1510         
1511         priv->conn->prof.tx_time = 0;
1512         return count;
1513 }
1514
1515 static DEVICE_ATTR(max_tx_io_time, 0644, txtime_show, txtime_write);
1516
1517 static ssize_t
1518 txpend_show (struct device *dev, char *buf)
1519 {
1520         struct netiucv_priv *priv = dev->driver_data;
1521
1522         return sprintf(buf, "%ld\n", priv->conn->prof.tx_pending);
1523 }
1524
1525 static ssize_t
1526 txpend_write (struct device *dev, const char *buf, size_t count)
1527 {
1528         struct netiucv_priv *priv = dev->driver_data;
1529
1530         priv->conn->prof.tx_pending = 0;
1531         return count;
1532 }
1533
1534 static DEVICE_ATTR(tx_pending, 0644, txpend_show, txpend_write);
1535
1536 static ssize_t
1537 txmpnd_show (struct device *dev, char *buf)
1538 {
1539         struct netiucv_priv *priv = dev->driver_data;
1540
1541         return sprintf(buf, "%ld\n", priv->conn->prof.tx_max_pending);
1542 }
1543
1544 static ssize_t
1545 txmpnd_write (struct device *dev, const char *buf, size_t count)
1546 {
1547         struct netiucv_priv *priv = dev->driver_data;
1548
1549         priv->conn->prof.tx_max_pending = 0;
1550         return count;
1551 }
1552
1553 static DEVICE_ATTR(tx_max_pending, 0644, txmpnd_show, txmpnd_write);
1554
1555 static struct attribute *netiucv_attrs[] = {
1556         &dev_attr_buffer.attr,
1557         &dev_attr_user.attr,
1558         NULL,
1559 };
1560
1561 static struct attribute_group netiucv_attr_group = {
1562         .attrs = netiucv_attrs,
1563 };
1564
1565 static struct attribute *netiucv_stat_attrs[] = {
1566         &dev_attr_device_fsm_state.attr,
1567         &dev_attr_connection_fsm_state.attr,
1568         &dev_attr_max_tx_buffer_used.attr,
1569         &dev_attr_max_chained_skbs.attr,
1570         &dev_attr_tx_single_write_ops.attr,
1571         &dev_attr_tx_multi_write_ops.attr,
1572         &dev_attr_netto_bytes.attr,
1573         &dev_attr_max_tx_io_time.attr,
1574         &dev_attr_tx_pending.attr,
1575         &dev_attr_tx_max_pending.attr,
1576         NULL,
1577 };
1578
1579 static struct attribute_group netiucv_stat_attr_group = {
1580         .name  = "stats",
1581         .attrs = netiucv_stat_attrs,
1582 };
1583
1584 static inline int
1585 netiucv_add_files(struct device *dev)
1586 {
1587         int ret;
1588
1589         pr_debug("%s() called\n", __FUNCTION__);
1590
1591         ret = sysfs_create_group(&dev->kobj, &netiucv_attr_group);
1592         if (ret)
1593                 return ret;
1594         ret = sysfs_create_group(&dev->kobj, &netiucv_stat_attr_group);
1595         if (ret)
1596                 sysfs_remove_group(&dev->kobj, &netiucv_attr_group);
1597         return ret;
1598 }
1599
1600 static inline void
1601 netiucv_remove_files(struct device *dev)
1602 {
1603         pr_debug("%s() called\n", __FUNCTION__);
1604         sysfs_remove_group(&dev->kobj, &netiucv_stat_attr_group);
1605         sysfs_remove_group(&dev->kobj, &netiucv_attr_group);
1606 }
1607
1608 static int
1609 netiucv_register_device(struct net_device *ndev)
1610 {
1611         struct netiucv_priv *priv = ndev->priv;
1612         struct device *dev = kmalloc(sizeof(struct device), GFP_KERNEL);
1613         int ret;
1614
1615
1616         pr_debug("%s() called\n", __FUNCTION__);
1617
1618         if (dev) {
1619                 memset(dev, 0, sizeof(struct device));
1620                 snprintf(dev->bus_id, BUS_ID_SIZE, "net%s", ndev->name);
1621                 dev->bus = &iucv_bus;
1622                 dev->parent = iucv_root;
1623                 /*
1624                  * The release function could be called after the
1625                  * module has been unloaded. It's _only_ task is to
1626                  * free the struct. Therefore, we specify kfree()
1627                  * directly here. (Probably a little bit obfuscating
1628                  * but legitime ...).
1629                  */
1630                 dev->release = (void (*)(struct device *))kfree;
1631         } else
1632                 return -ENOMEM;
1633
1634         ret = device_register(dev);
1635
1636         if (ret)
1637                 return ret;
1638         ret = netiucv_add_files(dev);
1639         if (ret)
1640                 goto out_unreg;
1641         dev->driver_data = priv;
1642         priv->dev = dev;
1643         return 0;
1644
1645 out_unreg:
1646         device_unregister(dev);
1647         return ret;
1648 }
1649
1650 static void
1651 netiucv_unregister_device(struct device *dev)
1652 {
1653         pr_debug("%s() called\n", __FUNCTION__);
1654         netiucv_remove_files(dev);
1655         device_unregister(dev);
1656 }
1657
1658 /**
1659  * Allocate and initialize a new connection structure.
1660  * Add it to the list of connections;
1661  */
1662 static struct iucv_connection *
1663 netiucv_new_connection(struct net_device *dev, char *username)
1664 {
1665         struct iucv_connection **clist = &connections;
1666         struct iucv_connection *conn =
1667                 (struct iucv_connection *)
1668                 kmalloc(sizeof(struct iucv_connection), GFP_KERNEL);
1669
1670         if (conn) {
1671                 memset(conn, 0, sizeof(struct iucv_connection));
1672                 skb_queue_head_init(&conn->collect_queue);
1673                 skb_queue_head_init(&conn->commit_queue);
1674                 conn->max_buffsize = NETIUCV_BUFSIZE_DEFAULT;
1675                 conn->netdev = dev;
1676
1677                 conn->rx_buff = alloc_skb(NETIUCV_BUFSIZE_DEFAULT,
1678                                           GFP_KERNEL | GFP_DMA);
1679                 if (!conn->rx_buff) {
1680                         kfree(conn);
1681                         return NULL;
1682                 }
1683                 conn->tx_buff = alloc_skb(NETIUCV_BUFSIZE_DEFAULT,
1684                                           GFP_KERNEL | GFP_DMA);
1685                 if (!conn->tx_buff) {
1686                         kfree_skb(conn->rx_buff);
1687                         kfree(conn);
1688                         return NULL;
1689                 }
1690                 conn->fsm = init_fsm("netiucvconn", conn_state_names,
1691                                      conn_event_names, NR_CONN_STATES,
1692                                      NR_CONN_EVENTS, conn_fsm, CONN_FSM_LEN,
1693                                      GFP_KERNEL);
1694                 if (!conn->fsm) {
1695                         kfree_skb(conn->tx_buff);
1696                         kfree_skb(conn->rx_buff);
1697                         kfree(conn);
1698                         return NULL;
1699                 }
1700                 fsm_settimer(conn->fsm, &conn->timer);
1701                 fsm_newstate(conn->fsm, CONN_STATE_INVALID);
1702
1703                 if (username) {
1704                         memcpy(conn->userid, username, 9);
1705                         fsm_newstate(conn->fsm, CONN_STATE_STOPPED);
1706                 }
1707
1708                 conn->next = *clist;
1709                 *clist = conn;
1710         }
1711         return conn;
1712 }
1713
1714 /**
1715  * Release a connection structure and remove it from the
1716  * list of connections.
1717  */
1718 static void
1719 netiucv_remove_connection(struct iucv_connection *conn)
1720 {
1721         struct iucv_connection **clist = &connections;
1722
1723         pr_debug("%s() called\n", __FUNCTION__);
1724
1725         if (conn == NULL)
1726                 return;
1727         while (*clist) {
1728                 if (*clist == conn) {
1729                         *clist = conn->next;
1730                         if (conn->handle != 0) {
1731                                 iucv_unregister_program(conn->handle);
1732                                 conn->handle = 0;
1733                         }
1734                         fsm_deltimer(&conn->timer);
1735                         kfree_fsm(conn->fsm);
1736                         kfree_skb(conn->rx_buff);
1737                         kfree_skb(conn->tx_buff);
1738                         return;
1739                 }
1740                 clist = &((*clist)->next);
1741         }
1742 }
1743
1744 /**
1745  * Release everything of a net device.
1746  */
1747 static void
1748 netiucv_free_netdevice(struct net_device *dev)
1749 {
1750         struct netiucv_priv *privptr;
1751
1752         pr_debug("%s() called\n", __FUNCTION__);
1753
1754         if (!dev)
1755                 return;
1756
1757         privptr = (struct netiucv_priv *)dev->priv;
1758         if (privptr) {
1759                 if (privptr->fsm)
1760                         fsm_deltimer(&privptr->timer);
1761                 if (privptr->conn)
1762                         netiucv_remove_connection(privptr->conn);
1763                 if (privptr->fsm)
1764                         kfree_fsm(privptr->fsm);
1765                 privptr->conn = 0; privptr->fsm = 0;
1766                 /* privptr gets freed by free_netdev() */
1767         }
1768         free_netdev(dev);
1769 }
1770
1771 /**
1772  * Initialize a net device. (Called from kernel in alloc_netdev())
1773  */
1774 static void
1775 netiucv_setup_netdevice(struct net_device *dev)
1776 {
1777         memset(dev->priv, 0, sizeof(struct netiucv_priv));
1778
1779         dev->mtu                 = NETIUCV_MTU_DEFAULT;
1780         dev->hard_start_xmit     = netiucv_tx;
1781         dev->open                = netiucv_open;
1782         dev->stop                = netiucv_close;
1783         dev->get_stats           = netiucv_stats;
1784         dev->change_mtu          = netiucv_change_mtu;
1785         dev->destructor          = netiucv_free_netdevice;
1786         dev->hard_header_len     = NETIUCV_HDRLEN;
1787         dev->addr_len            = 0;
1788         dev->type                = ARPHRD_SLIP;
1789         dev->tx_queue_len        = NETIUCV_QUEUELEN_DEFAULT;
1790         dev->flags               = IFF_POINTOPOINT | IFF_NOARP;
1791         SET_MODULE_OWNER(dev);
1792 }
1793
1794 /**
1795  * Allocate and initialize everything of a net device.
1796  */
1797 static struct net_device *
1798 netiucv_init_netdevice(char *username)
1799 {
1800         struct netiucv_priv *privptr;
1801         struct net_device *dev;
1802
1803         dev = alloc_netdev(sizeof(struct netiucv_priv), "iucv%d",
1804                            netiucv_setup_netdevice);
1805         if (!dev)
1806                 return NULL;
1807
1808         privptr = (struct netiucv_priv *)dev->priv;
1809         privptr->fsm = init_fsm("netiucvdev", dev_state_names,
1810                                 dev_event_names, NR_DEV_STATES, NR_DEV_EVENTS,
1811                                 dev_fsm, DEV_FSM_LEN, GFP_KERNEL);
1812         if (privptr->fsm == NULL) {
1813                 free_netdev(dev);
1814                 return NULL;
1815         }
1816         privptr->conn = netiucv_new_connection(dev, username);
1817         if (!privptr->conn) {
1818                 kfree_fsm(privptr->fsm);
1819                 free_netdev(dev);
1820                 return NULL;
1821         }
1822         fsm_settimer(privptr->fsm, &privptr->timer);
1823         fsm_newstate(privptr->fsm, DEV_STATE_STOPPED);
1824
1825         return dev;
1826 }
1827
1828 static ssize_t
1829 conn_write(struct device_driver *drv, const char *buf, size_t count)
1830 {
1831         char *p;
1832         char username[10];
1833         int i, ret;
1834         struct net_device *dev;
1835
1836         if (count>9) {
1837                 printk(KERN_WARNING
1838                        "netiucv: username too long (%d)!\n", (int)count);
1839                 return -EINVAL;
1840         }
1841
1842         for (i=0, p=(char *)buf; i<8 && *p; i++, p++) {
1843                 if (isalnum(*p) || (*p == '$'))
1844                         username[i]= *p;
1845                 else if (*p == '\n') {
1846                         /* trailing lf, grr */
1847                         break;
1848                 } else {
1849                         printk(KERN_WARNING
1850                                "netiucv: Invalid character in username!\n");
1851                         return -EINVAL;
1852                 }
1853         }
1854         while (i<9)
1855                 username[i++] = ' ';
1856         username[9] = '\0';
1857         dev = netiucv_init_netdevice(username);
1858         if (!dev) {
1859                 printk(KERN_WARNING
1860                        "netiucv: Could not allocate network device structure "
1861                        "for user '%s'\n", netiucv_printname(username));
1862                 return -ENODEV;
1863         }
1864         
1865         if ((ret = register_netdev(dev))) {
1866                 goto out_free_ndev;
1867         }
1868
1869         if ((ret = netiucv_register_device(dev))) {
1870                 unregister_netdev(dev);
1871                 goto out_free_ndev;
1872         }
1873
1874         /* sysfs magic */
1875         SET_NETDEV_DEV(dev, (struct device*)((struct netiucv_priv*)dev->priv)->dev);
1876         printk(KERN_INFO "%s: '%s'\n", dev->name, netiucv_printname(username));
1877         
1878         return count;
1879
1880 out_free_ndev:
1881         printk(KERN_WARNING
1882                        "netiucv: Could not register '%s'\n", dev->name);
1883         netiucv_free_netdevice(dev);
1884         return ret;
1885 }
1886
1887 DRIVER_ATTR(connection, 0200, NULL, conn_write);
1888
1889 static ssize_t
1890 remove_write (struct device_driver *drv, const char *buf, size_t count)
1891 {
1892         struct iucv_connection **clist = &connections;
1893         struct net_device *ndev;
1894         struct netiucv_priv *priv;
1895         struct device *dev;
1896         char name[IFNAMSIZ];
1897         char *p;
1898         int i;
1899
1900         pr_debug("%s() called\n", __FUNCTION__);
1901
1902         if (count >= IFNAMSIZ)
1903                 count = IFNAMSIZ-1;
1904
1905         for (i=0, p=(char *)buf; i<count && *p; i++, p++) {
1906                 if ((*p == '\n') | (*p == ' ')) {
1907                         /* trailing lf, grr */
1908                         break;
1909                 } else {
1910                         name[i]=*p;
1911                 }
1912         }
1913         name[i] = '\0';
1914
1915         while (*clist) {
1916                 ndev = (*clist)->netdev;
1917                 priv = (struct netiucv_priv*)ndev->priv;
1918                 dev = priv->dev;
1919
1920                 if (strncmp(name, ndev->name, count)) {
1921                         clist = &((*clist)->next);
1922                         continue;
1923                 }
1924                 if (ndev->flags & (IFF_UP | IFF_RUNNING)) {
1925                         printk(KERN_WARNING
1926                                 "netiucv: net device %s active with peer %s\n",
1927                                 ndev->name, priv->conn->userid);
1928                         printk(KERN_WARNING
1929                                 "netiucv: %s cannot be removed\n",
1930                                 ndev->name);
1931                         return -EBUSY;
1932                 }
1933                 unregister_netdev(ndev);
1934                 netiucv_unregister_device(dev);
1935                 return count;
1936         }
1937         printk(KERN_WARNING
1938                 "netiucv: net device %s unknown\n", name);
1939         return -EINVAL;
1940 }
1941
1942 DRIVER_ATTR(remove, 0200, NULL, remove_write);
1943
1944 static struct device_driver netiucv_driver = {
1945         .name = "netiucv",
1946         .bus  = &iucv_bus,
1947 };
1948
1949 static void
1950 netiucv_banner(void)
1951 {
1952         char vbuf[] = "$Revision: 1.54 $";
1953         char *version = vbuf;
1954
1955         if ((version = strchr(version, ':'))) {
1956                 char *p = strchr(version + 1, '$');
1957                 if (p)
1958                         *p = '\0';
1959         } else
1960                 version = " ??? ";
1961         printk(KERN_INFO "NETIUCV driver Version%s initialized\n", version);
1962 }
1963
1964 static void __exit
1965 netiucv_exit(void)
1966 {
1967         while (connections) {
1968                 struct net_device *ndev = connections->netdev;
1969                 struct netiucv_priv *priv = (struct netiucv_priv*)ndev->priv;
1970                 struct device *dev = priv->dev;
1971
1972                 unregister_netdev(ndev);
1973                 netiucv_unregister_device(dev);
1974         }
1975
1976         driver_remove_file(&netiucv_driver, &driver_attr_connection);
1977         driver_remove_file(&netiucv_driver, &driver_attr_remove);
1978         driver_unregister(&netiucv_driver);
1979
1980         printk(KERN_INFO "NETIUCV driver unloaded\n");
1981         return;
1982 }
1983
1984 static int __init
1985 netiucv_init(void)
1986 {
1987         int ret;
1988         
1989         ret = driver_register(&netiucv_driver);
1990         if (ret != 0) {
1991                 printk(KERN_ERR "NETIUCV: failed to register driver.\n");
1992                 return ret;
1993         }
1994
1995         /* Add entry for specifying connections. */
1996         ret = driver_create_file(&netiucv_driver, &driver_attr_connection);
1997         if (ret == 0) {
1998                 ret = driver_create_file(&netiucv_driver, &driver_attr_remove);
1999                 netiucv_banner();
2000         } else {
2001                 printk(KERN_ERR "NETIUCV: failed to add driver attribute.\n");
2002                 driver_unregister(&netiucv_driver);
2003         }
2004         return ret;
2005 }
2006         
2007 module_init(netiucv_init);
2008 module_exit(netiucv_exit);
2009 MODULE_LICENSE("GPL");