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