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