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