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