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