Merge with Linux 2.5.59.
[linux-2.6/linux-mips.git] / drivers / s390 / net / netiucv.c
blob98c83403cb7ebd4d855d4b5b086850609fa8985b
1 /*
2 * $Id: netiucv.c,v 1.12 2002/11/26 16:00:54 mschwide Exp $
4 * IUCV network driver
6 * Copyright (C) 2001 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Author(s): Fritz Elfert (elfert@de.ibm.com, felfert@millenux.com)
9 * Driverfs integration and all bugs therein by Cornelia Huck(cohuck@de.ibm.com)
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
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.
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.
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.
33 * RELEASE-TAG: IUCV network driver $Revision: 1.12 $
37 #include <linux/version.h>
38 #include <linux/module.h>
39 #include <linux/init.h>
40 #include <linux/kernel.h>
41 #include <linux/slab.h>
42 #include <linux/errno.h>
43 #include <linux/types.h>
44 #include <linux/interrupt.h>
45 #include <linux/timer.h>
46 #include <linux/sched.h>
48 #include <linux/signal.h>
49 #include <linux/string.h>
50 #include <linux/device.h>
52 #include <linux/ip.h>
53 #include <linux/if_arp.h>
54 #include <linux/tcp.h>
55 #include <linux/skbuff.h>
56 #include <linux/ctype.h>
57 #include <net/dst.h>
59 #include <asm/io.h>
60 #include <asm/bitops.h>
61 #include <asm/uaccess.h>
63 #include "iucv.h"
64 #include "fsm.h"
66 #undef DEBUG
68 #ifdef MODULE
69 MODULE_AUTHOR
70 ("(C) 2001 IBM Corporation by Fritz Elfert (felfert@millenux.com)");
71 MODULE_DESCRIPTION ("Linux for S/390 IUCV network driver");
72 MODULE_PARM (iucv, "1s");
73 MODULE_PARM_DESC (iucv,
74 "Specify the initial remote userids for iucv0 .. iucvn:\n"
75 "iucv=userid0:userid1:...:useridN");
76 #endif
78 static char *iucv = "";
80 typedef struct net_device net_device;
83 /**
84 * Per connection profiling data
86 typedef struct connection_profile_t {
87 unsigned long maxmulti;
88 unsigned long maxcqueue;
89 unsigned long doios_single;
90 unsigned long doios_multi;
91 unsigned long txlen;
92 unsigned long tx_time;
93 struct timespec send_stamp;
94 } connection_profile;
96 /**
97 * Representation of one iucv connection
99 typedef struct iucv_connection_t {
100 struct iucv_connection_t *next;
101 iucv_handle_t handle;
102 __u16 pathid;
103 struct sk_buff *rx_buff;
104 struct sk_buff *tx_buff;
105 struct sk_buff_head collect_queue;
106 spinlock_t collect_lock;
107 int collect_len;
108 int max_buffsize;
109 int flags;
110 fsm_timer timer;
111 int retry;
112 fsm_instance *fsm;
113 net_device *netdev;
114 connection_profile prof;
115 char userid[9];
116 } iucv_connection;
118 #define CONN_FLAGS_BUFSIZE_CHANGED 1
121 * Linked list of all connection structs.
123 static iucv_connection *connections;
126 * Representation of event-data for the
127 * connection state machine.
129 typedef struct iucv_event_t {
130 iucv_connection *conn;
131 void *data;
132 } iucv_event;
135 * Private part of the network device structure
137 typedef struct netiucv_priv_t {
138 struct net_device_stats stats;
139 unsigned long tbusy;
140 fsm_instance *fsm;
141 iucv_connection *conn;
142 struct platform_device pldev;
143 } netiucv_priv;
146 * Link level header for a packet.
148 typedef struct ll_header_t {
149 __u16 next;
150 } ll_header;
152 #define NETIUCV_HDRLEN (sizeof(ll_header))
153 #define NETIUCV_BUFSIZE_MAX 32768
154 #define NETIUCV_BUFSIZE_DEFAULT NETIUCV_BUFSIZE_MAX
155 #define NETIUCV_MTU_MAX (NETIUCV_BUFSIZE_MAX - NETIUCV_HDRLEN)
156 #define NETIUCV_MTU_DEFAULT 9216
157 #define NETIUCV_QUEUELEN_DEFAULT 50
158 #define NETIUCV_TIMEOUT_5SEC 5000
161 * Compatibility macros for busy handling
162 * of network devices.
164 static __inline__ void netiucv_clear_busy(net_device *dev)
166 clear_bit(0, &(((netiucv_priv *)dev->priv)->tbusy));
167 netif_wake_queue(dev);
170 static __inline__ int netiucv_test_and_set_busy(net_device *dev)
172 netif_stop_queue(dev);
173 return test_and_set_bit(0, &((netiucv_priv *)dev->priv)->tbusy);
176 #define SET_DEVICE_START(device, value)
178 static __u8 iucv_host[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
179 static __u8 iucvMagic[16] = {
180 0xF0, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
181 0xF0, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40
185 * This mask means the 16-byte IUCV "magic" and the origin userid must
186 * match exactly as specified in order to give connection_pending()
187 * control.
189 static __u8 mask[] = {
190 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
191 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
192 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
196 * Convert an iucv userId to its printable
197 * form (strip whitespace at end).
199 * @param An iucv userId
201 * @returns The printable string (static data!!)
203 static __inline__ char *
204 netiucv_printname(char *name)
206 static char tmp[9];
207 char *p = tmp;
208 memcpy(tmp, name, 8);
209 tmp[8] = '\0';
210 while (*p && (!isspace(*p)))
211 p++;
212 *p = '\0';
213 return tmp;
217 * States of the interface statemachine.
219 enum dev_states {
220 DEV_STATE_STOPPED,
221 DEV_STATE_STARTWAIT,
222 DEV_STATE_STOPWAIT,
223 DEV_STATE_RUNNING,
225 * MUST be always the last element!!
227 NR_DEV_STATES
230 static const char *dev_state_names[] = {
231 "Stopped",
232 "StartWait",
233 "StopWait",
234 "Running",
238 * Events of the interface statemachine.
240 enum dev_events {
241 DEV_EVENT_START,
242 DEV_EVENT_STOP,
243 DEV_EVENT_CONUP,
244 DEV_EVENT_CONDOWN,
246 * MUST be always the last element!!
248 NR_DEV_EVENTS
251 static const char *dev_event_names[] = {
252 "Start",
253 "Stop",
254 "Connection up",
255 "Connection down",
259 * Events of the connection statemachine
261 enum conn_events {
263 * Events, representing callbacks from
264 * lowlevel iucv layer)
266 CONN_EVENT_CONN_REQ,
267 CONN_EVENT_CONN_ACK,
268 CONN_EVENT_CONN_REJ,
269 CONN_EVENT_CONN_SUS,
270 CONN_EVENT_CONN_RES,
271 CONN_EVENT_RX,
272 CONN_EVENT_TXDONE,
275 * Events, representing errors return codes from
276 * calls to lowlevel iucv layer
280 * Event, representing timer expiry.
282 CONN_EVENT_TIMER,
285 * Events, representing commands from upper levels.
287 CONN_EVENT_START,
288 CONN_EVENT_STOP,
291 * MUST be always the last element!!
293 NR_CONN_EVENTS,
296 static const char *conn_event_names[] = {
297 "Remote connection request",
298 "Remote connection acknowledge",
299 "Remote connection reject",
300 "Connection suspended",
301 "Connection resumed",
302 "Data received",
303 "Data sent",
305 "Timer",
307 "Start",
308 "Stop",
312 * States of the connection statemachine.
314 enum conn_states {
316 * Connection not assigned to any device,
317 * initial state, invalid
319 CONN_STATE_INVALID,
322 * Userid assigned but not operating
324 CONN_STATE_STOPPED,
327 * Connection registered,
328 * no connection request sent yet,
329 * no connection request received
331 CONN_STATE_STARTWAIT,
334 * Connection registered and connection request sent,
335 * no acknowledge and no connection request received yet.
337 CONN_STATE_SETUPWAIT,
340 * Connection up and running idle
342 CONN_STATE_IDLE,
345 * Data sent, awaiting CONN_EVENT_TXDONE
347 CONN_STATE_TX,
350 * Terminating
352 CONN_STATE_TERM,
355 * Error during registration.
357 CONN_STATE_REGERR,
360 * Error during registration.
362 CONN_STATE_CONNERR,
365 * MUST be always the last element!!
367 NR_CONN_STATES,
370 static const char *conn_state_names[] = {
371 "Invalid",
372 "Stopped",
373 "StartWait",
374 "SetupWait",
375 "Idle",
376 "TX",
377 "Terminating",
378 "Registration error",
379 "Connect error",
384 * Callback-wrappers, called from lowlevel iucv layer.
385 *****************************************************************************/
387 static void
388 netiucv_callback_rx(iucv_MessagePending *eib, void *pgm_data)
390 iucv_connection *conn = (iucv_connection *)pgm_data;
391 iucv_event ev;
393 ev.conn = conn;
394 ev.data = (void *)eib;
396 fsm_event(conn->fsm, CONN_EVENT_RX, &ev);
399 static void
400 netiucv_callback_txdone(iucv_MessageComplete *eib, void *pgm_data)
402 iucv_connection *conn = (iucv_connection *)pgm_data;
403 iucv_event ev;
405 ev.conn = conn;
406 ev.data = (void *)eib;
407 fsm_event(conn->fsm, CONN_EVENT_TXDONE, &ev);
410 static void
411 netiucv_callback_connack(iucv_ConnectionComplete *eib, void *pgm_data)
413 iucv_connection *conn = (iucv_connection *)pgm_data;
414 iucv_event ev;
416 ev.conn = conn;
417 ev.data = (void *)eib;
418 fsm_event(conn->fsm, CONN_EVENT_CONN_ACK, &ev);
421 static void
422 netiucv_callback_connreq(iucv_ConnectionPending *eib, void *pgm_data)
424 iucv_connection *conn = (iucv_connection *)pgm_data;
425 iucv_event ev;
427 ev.conn = conn;
428 ev.data = (void *)eib;
429 fsm_event(conn->fsm, CONN_EVENT_CONN_REQ, &ev);
432 static void
433 netiucv_callback_connrej(iucv_ConnectionSevered *eib, void *pgm_data)
435 iucv_connection *conn = (iucv_connection *)pgm_data;
436 iucv_event ev;
438 ev.conn = conn;
439 ev.data = (void *)eib;
440 fsm_event(conn->fsm, CONN_EVENT_CONN_REJ, &ev);
443 static void
444 netiucv_callback_connsusp(iucv_ConnectionQuiesced *eib, void *pgm_data)
446 iucv_connection *conn = (iucv_connection *)pgm_data;
447 iucv_event ev;
449 ev.conn = conn;
450 ev.data = (void *)eib;
451 fsm_event(conn->fsm, CONN_EVENT_CONN_SUS, &ev);
454 static void
455 netiucv_callback_connres(iucv_ConnectionResumed *eib, void *pgm_data)
457 iucv_connection *conn = (iucv_connection *)pgm_data;
458 iucv_event ev;
460 ev.conn = conn;
461 ev.data = (void *)eib;
462 fsm_event(conn->fsm, CONN_EVENT_CONN_RES, &ev);
465 static iucv_interrupt_ops_t netiucv_ops = {
466 .ConnectionPending = netiucv_callback_connreq,
467 .ConnectionComplete = netiucv_callback_connack,
468 .ConnectionSevered = netiucv_callback_connrej,
469 .ConnectionQuiesced = netiucv_callback_connsusp,
470 .ConnectionResumed = netiucv_callback_connres,
471 .MessagePending = netiucv_callback_rx,
472 .MessageComplete = netiucv_callback_txdone
476 * Dummy NOP action for all statemachines
478 static void
479 fsm_action_nop(fsm_instance *fi, int event, void *arg)
484 * Actions of the connection statemachine
485 *****************************************************************************/
488 * Helper function for conn_action_rx()
489 * Unpack a just received skb and hand it over to
490 * upper layers.
492 * @param conn The connection where this skb has been received.
493 * @param pskb The received skb.
495 //static __inline__ void
496 static void
497 netiucv_unpack_skb(iucv_connection *conn, struct sk_buff *pskb)
499 net_device *dev = conn->netdev;
500 netiucv_priv *privptr = (netiucv_priv *)dev->priv;
501 __u16 offset = 0;
503 skb_put(pskb, NETIUCV_HDRLEN);
504 pskb->dev = dev;
505 pskb->ip_summed = CHECKSUM_NONE;
506 pskb->protocol = ntohs(ETH_P_IP);
508 while (1) {
509 struct sk_buff *skb;
510 ll_header *header = (ll_header *)pskb->data;
512 if (header->next == 0)
513 break;
515 skb_pull(pskb, NETIUCV_HDRLEN);
516 header->next -= offset;
517 offset += header->next;
518 header->next -= NETIUCV_HDRLEN;
519 if (skb_tailroom(pskb) < header->next) {
520 printk(KERN_WARNING
521 "%s: Illegal next field in iucv header: %d > %d\n",
522 dev->name, header->next, skb_tailroom(pskb));
523 return;
525 skb_put(pskb, header->next);
526 pskb->mac.raw = pskb->data;
527 skb = dev_alloc_skb(pskb->len);
528 if (!skb) {
529 printk(KERN_WARNING
530 "%s Out of memory in netiucv_unpack_skb\n",
531 dev->name);
532 privptr->stats.rx_dropped++;
533 return;
535 memcpy(skb_put(skb, pskb->len), pskb->data, pskb->len);
536 skb->mac.raw = skb->data;
537 skb->dev = pskb->dev;
538 skb->protocol = pskb->protocol;
539 pskb->ip_summed = CHECKSUM_UNNECESSARY;
540 netif_rx(skb);
541 dev->last_rx = jiffies;
542 privptr->stats.rx_packets++;
543 privptr->stats.rx_bytes += skb->len;
544 skb_pull(pskb, header->next);
545 skb_put(pskb, NETIUCV_HDRLEN);
549 static void
550 conn_action_rx(fsm_instance *fi, int event, void *arg)
552 iucv_event *ev = (iucv_event *)arg;
553 iucv_connection *conn = ev->conn;
554 iucv_MessagePending *eib = (iucv_MessagePending *)ev->data;
555 netiucv_priv *privptr = (netiucv_priv *)conn->netdev->priv;
557 __u16 msglen = eib->ln1msg2.ipbfln1f;
558 int rc;
560 #ifdef DEBUG
561 printk(KERN_DEBUG "%s() called\n", __FUNCTION__);
562 #endif
563 if (!conn->netdev) {
564 /* FRITZ: How to tell iucv LL to drop the msg? */
565 printk(KERN_WARNING
566 "Received data for unlinked connection\n");
567 return;
569 if (msglen > conn->max_buffsize) {
570 /* FRITZ: How to tell iucv LL to drop the msg? */
571 privptr->stats.rx_dropped++;
572 return;
574 conn->rx_buff->data = conn->rx_buff->tail = conn->rx_buff->head;
575 conn->rx_buff->len = 0;
576 rc = iucv_receive(conn->pathid, eib->ipmsgid, eib->iptrgcls,
577 conn->rx_buff->data, msglen, NULL, NULL, NULL);
578 if (rc != 0 || msglen < 5) {
579 privptr->stats.rx_errors++;
580 return;
582 netiucv_unpack_skb(conn, conn->rx_buff);
585 static void
586 conn_action_txdone(fsm_instance *fi, int event, void *arg)
588 iucv_event *ev = (iucv_event *)arg;
589 iucv_connection *conn = ev->conn;
590 iucv_MessageComplete *eib = (iucv_MessageComplete *)ev->data;
591 netiucv_priv *privptr = NULL;
592 /* Shut up, gcc! skb is always below 2G. */
593 struct sk_buff *skb = (struct sk_buff *)(unsigned long)eib->ipmsgtag;
594 __u32 txbytes = 0;
595 __u32 txpackets = 0;
596 __u32 stat_maxcq = 0;
597 unsigned long saveflags;
598 ll_header header;
600 #ifdef DEBUG
601 printk(KERN_DEBUG "%s() called\n", __FUNCTION__);
602 #endif
603 fsm_deltimer(&conn->timer);
604 if (conn && conn->netdev && conn->netdev->priv)
605 privptr = (netiucv_priv *)conn->netdev->priv;
606 if (skb) {
607 if (privptr) {
608 privptr->stats.tx_packets++;
609 privptr->stats.tx_bytes +=
610 (skb->len - NETIUCV_HDRLEN - NETIUCV_HDRLEN);
612 dev_kfree_skb_any(skb);
614 conn->tx_buff->data = conn->tx_buff->tail = conn->tx_buff->head;
615 conn->tx_buff->len = 0;
616 spin_lock_irqsave(&conn->collect_lock, saveflags);
617 while ((skb = skb_dequeue(&conn->collect_queue))) {
618 header.next = conn->tx_buff->len + skb->len + NETIUCV_HDRLEN;
619 memcpy(skb_put(conn->tx_buff, NETIUCV_HDRLEN), &header,
620 NETIUCV_HDRLEN);
621 memcpy(skb_put(conn->tx_buff, skb->len), skb->data, skb->len);
622 txbytes += skb->len;
623 txpackets++;
624 stat_maxcq++;
625 atomic_dec(&skb->users);
626 dev_kfree_skb_any(skb);
628 if (conn->collect_len > conn->prof.maxmulti)
629 conn->prof.maxmulti = conn->collect_len;
630 conn->collect_len = 0;
631 spin_unlock_irqrestore(&conn->collect_lock, saveflags);
632 if (conn->tx_buff->len) {
633 int rc;
635 header.next = 0;
636 memcpy(skb_put(conn->tx_buff, NETIUCV_HDRLEN), &header,
637 NETIUCV_HDRLEN);
639 fsm_addtimer(&conn->timer, NETIUCV_TIMEOUT_5SEC,
640 CONN_EVENT_TIMER, conn);
641 conn->prof.send_stamp = xtime;
642 rc = iucv_send(conn->pathid, NULL, 0, 0, 0, 0,
643 conn->tx_buff->data, conn->tx_buff->len);
644 conn->prof.doios_multi++;
645 conn->prof.txlen += conn->tx_buff->len;
646 if (rc != 0) {
647 fsm_deltimer(&conn->timer);
648 fsm_newstate(fi, CONN_STATE_IDLE);
649 if (privptr)
650 privptr->stats.tx_errors += txpackets;
651 } else {
652 if (privptr) {
653 privptr->stats.tx_packets += txpackets;
654 privptr->stats.tx_bytes += txbytes;
656 if (stat_maxcq > conn->prof.maxcqueue)
657 conn->prof.maxcqueue = stat_maxcq;
659 } else
660 fsm_newstate(fi, CONN_STATE_IDLE);
663 static void
664 conn_action_connaccept(fsm_instance *fi, int event, void *arg)
666 iucv_event *ev = (iucv_event *)arg;
667 iucv_connection *conn = ev->conn;
668 iucv_ConnectionPending *eib = (iucv_ConnectionPending *)ev->data;
669 net_device *netdev = conn->netdev;
670 netiucv_priv *privptr = (netiucv_priv *)netdev->priv;
671 int rc;
672 __u16 msglimit;
673 __u8 udata[16];
675 #ifdef DEBUG
676 printk(KERN_DEBUG "%s() called\n", __FUNCTION__);
677 #endif
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;
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);
692 static void
693 conn_action_connreject(fsm_instance *fi, int event, void *arg)
695 iucv_event *ev = (iucv_event *)arg;
696 // iucv_connection *conn = ev->conn;
697 iucv_ConnectionPending *eib = (iucv_ConnectionPending *)ev->data;
698 __u8 udata[16];
700 #ifdef DEBUG
701 printk(KERN_DEBUG "%s() called\n", __FUNCTION__);
702 #endif
703 iucv_sever(eib->ippathid, udata);
706 static void
707 conn_action_connack(fsm_instance *fi, int event, void *arg)
709 iucv_event *ev = (iucv_event *)arg;
710 iucv_connection *conn = ev->conn;
711 iucv_ConnectionComplete *eib = (iucv_ConnectionComplete *)ev->data;
712 net_device *netdev = conn->netdev;
713 netiucv_priv *privptr = (netiucv_priv *)netdev->priv;
715 #ifdef DEBUG
716 printk(KERN_DEBUG "%s() called\n", __FUNCTION__);
717 #endif
718 fsm_newstate(fi, CONN_STATE_IDLE);
719 conn->pathid = eib->ippathid;
720 netdev->tx_queue_len = eib->ipmsglim;
721 fsm_event(privptr->fsm, DEV_EVENT_CONUP, netdev);
724 static void
725 conn_action_connsever(fsm_instance *fi, int event, void *arg)
727 iucv_event *ev = (iucv_event *)arg;
728 iucv_connection *conn = ev->conn;
729 // iucv_ConnectionSevered *eib = (iucv_ConnectionSevered *)ev->data;
730 net_device *netdev = conn->netdev;
731 netiucv_priv *privptr = (netiucv_priv *)netdev->priv;
732 int state = fsm_getstate(fi);
734 #ifdef DEBUG
735 printk(KERN_DEBUG "%s() called\n", __FUNCTION__);
736 #endif
737 switch (state) {
738 case CONN_STATE_IDLE:
739 case CONN_STATE_TX:
740 printk(KERN_INFO "%s: Remote dropped connection\n",
741 netdev->name);
742 if (conn->handle)
743 iucv_unregister_program(conn->handle);
744 conn->handle = 0;
745 fsm_newstate(fi, CONN_STATE_STOPPED);
746 fsm_event(privptr->fsm, DEV_EVENT_CONDOWN, netdev);
747 break;
751 static void
752 conn_action_start(fsm_instance *fi, int event, void *arg)
754 iucv_event *ev = (iucv_event *)arg;
755 iucv_connection *conn = ev->conn;
757 int rc;
759 #ifdef DEBUG
760 printk(KERN_DEBUG "%s() called\n", __FUNCTION__);
761 #endif
762 if (conn->handle == 0) {
763 conn->handle =
764 iucv_register_program(iucvMagic, conn->userid, mask,
765 &netiucv_ops, conn);
766 fsm_newstate(fi, CONN_STATE_STARTWAIT);
767 if (conn->handle <= 0) {
768 fsm_newstate(fi, CONN_STATE_REGERR);
769 conn->handle = 0;
770 return;
772 #ifdef DEBUG
773 printk(KERN_DEBUG "%s('%s'): registered successfully\n",
774 conn->netdev->name, conn->userid);
775 #endif
777 #ifdef DEBUG
778 printk(KERN_DEBUG "%s('%s'): connecting ...\n",
779 conn->netdev->name, conn->userid);
780 #endif
781 rc = iucv_connect(&(conn->pathid), NETIUCV_QUEUELEN_DEFAULT, iucvMagic,
782 conn->userid, iucv_host, 0, NULL, NULL, conn->handle,
783 conn);
784 fsm_newstate(fi, CONN_STATE_SETUPWAIT);
785 switch (rc) {
786 case 0:
787 return;
788 case 11:
789 printk(KERN_NOTICE
790 "%s: User %s is currently not available.\n",
791 conn->netdev->name,
792 netiucv_printname(conn->userid));
793 fsm_newstate(fi, CONN_STATE_STARTWAIT);
794 return;
795 case 12:
796 printk(KERN_NOTICE
797 "%s: User %s is currently not ready.\n",
798 conn->netdev->name,
799 netiucv_printname(conn->userid));
800 fsm_newstate(fi, CONN_STATE_STARTWAIT);
801 return;
802 case 13:
803 printk(KERN_WARNING
804 "%s: Too many IUCV connections.\n",
805 conn->netdev->name);
806 fsm_newstate(fi, CONN_STATE_CONNERR);
807 break;
808 case 14:
809 printk(KERN_WARNING
810 "%s: User %s has too many IUCV connections.\n",
811 conn->netdev->name,
812 netiucv_printname(conn->userid));
813 fsm_newstate(fi, CONN_STATE_CONNERR);
814 break;
815 case 15:
816 printk(KERN_WARNING
817 "%s: No IUCV authorization in CP directory.\n",
818 conn->netdev->name);
819 fsm_newstate(fi, CONN_STATE_CONNERR);
820 break;
821 default:
822 printk(KERN_WARNING
823 "%s: iucv_connect returned error %d\n",
824 conn->netdev->name, rc);
825 fsm_newstate(fi, CONN_STATE_CONNERR);
826 break;
828 iucv_unregister_program(conn->handle);
829 conn->handle = 0;
832 static void
833 netiucv_purge_skb_queue(struct sk_buff_head *q)
835 struct sk_buff *skb;
837 while ((skb = skb_dequeue(q))) {
838 atomic_dec(&skb->users);
839 dev_kfree_skb_any(skb);
843 static void
844 conn_action_stop(fsm_instance *fi, int event, void *arg)
846 iucv_event *ev = (iucv_event *)arg;
847 iucv_connection *conn = ev->conn;
848 net_device *netdev = conn->netdev;
849 netiucv_priv *privptr = (netiucv_priv *)netdev->priv;
851 #ifdef DEBUG
852 printk(KERN_DEBUG "%s() called\n", __FUNCTION__);
853 #endif
854 fsm_newstate(fi, CONN_STATE_STOPPED);
855 netiucv_purge_skb_queue(&conn->collect_queue);
856 if (conn->handle)
857 iucv_unregister_program(conn->handle);
858 conn->handle = 0;
859 fsm_event(privptr->fsm, DEV_EVENT_CONDOWN, netdev);
862 static void
863 conn_action_inval(fsm_instance *fi, int event, void *arg)
865 iucv_event *ev = (iucv_event *)arg;
866 iucv_connection *conn = ev->conn;
867 net_device *netdev = conn->netdev;
869 printk(KERN_WARNING
870 "%s: Cannot connect without username\n",
871 netdev->name);
874 static const fsm_node conn_fsm[] = {
875 { CONN_STATE_INVALID, CONN_EVENT_START, conn_action_inval },
876 { CONN_STATE_STOPPED, CONN_EVENT_START, conn_action_start },
877 { CONN_STATE_STARTWAIT, CONN_EVENT_START, conn_action_start },
879 { CONN_STATE_STARTWAIT, CONN_EVENT_STOP, conn_action_stop },
880 { CONN_STATE_SETUPWAIT, CONN_EVENT_STOP, conn_action_stop },
881 { CONN_STATE_IDLE, CONN_EVENT_STOP, conn_action_stop },
882 { CONN_STATE_TX, CONN_EVENT_STOP, conn_action_stop },
883 { CONN_STATE_REGERR, CONN_EVENT_STOP, conn_action_stop },
884 { CONN_STATE_CONNERR, CONN_EVENT_STOP, conn_action_stop },
886 { CONN_STATE_STOPPED, CONN_EVENT_CONN_REQ, conn_action_connreject },
887 { CONN_STATE_STARTWAIT, CONN_EVENT_CONN_REQ, conn_action_connaccept },
888 { CONN_STATE_SETUPWAIT, CONN_EVENT_CONN_REQ, conn_action_connaccept },
889 { CONN_STATE_IDLE, CONN_EVENT_CONN_REQ, conn_action_connreject },
890 { CONN_STATE_TX, CONN_EVENT_CONN_REQ, conn_action_connreject },
892 { CONN_STATE_SETUPWAIT, CONN_EVENT_CONN_ACK, conn_action_connack },
894 { CONN_STATE_SETUPWAIT, CONN_EVENT_CONN_REJ, conn_action_connsever },
895 { CONN_STATE_IDLE, CONN_EVENT_CONN_REJ, conn_action_connsever },
896 { CONN_STATE_TX, CONN_EVENT_CONN_REJ, conn_action_connsever },
898 { CONN_STATE_IDLE, CONN_EVENT_RX, conn_action_rx },
899 { CONN_STATE_TX, CONN_EVENT_RX, conn_action_rx },
901 { CONN_STATE_TX, CONN_EVENT_TXDONE, conn_action_txdone },
904 static const int CONN_FSM_LEN = sizeof(conn_fsm) / sizeof(fsm_node);
908 * Actions for interface - statemachine.
909 *****************************************************************************/
912 * Startup connection by sending CONN_EVENT_START to it.
914 * @param fi An instance of an interface statemachine.
915 * @param event The event, just happened.
916 * @param arg Generic pointer, casted from net_device * upon call.
918 static void
919 dev_action_start(fsm_instance *fi, int event, void *arg)
921 net_device *dev = (net_device *)arg;
922 netiucv_priv *privptr = dev->priv;
923 iucv_event ev;
925 #ifdef DEBUG
926 printk(KERN_DEBUG "%s() called\n", __FUNCTION__);
927 #endif
928 ev.conn = privptr->conn;
929 fsm_newstate(fi, DEV_STATE_STARTWAIT);
930 fsm_event(privptr->conn->fsm, CONN_EVENT_START, &ev);
934 * Shutdown connection by sending CONN_EVENT_STOP to it.
936 * @param fi An instance of an interface statemachine.
937 * @param event The event, just happened.
938 * @param arg Generic pointer, casted from net_device * upon call.
940 static void
941 dev_action_stop(fsm_instance *fi, int event, void *arg)
943 net_device *dev = (net_device *)arg;
944 netiucv_priv *privptr = dev->priv;
945 iucv_event ev;
947 #ifdef DEBUG
948 printk(KERN_DEBUG "%s() called\n", __FUNCTION__);
949 #endif
950 ev.conn = privptr->conn;
952 fsm_newstate(fi, DEV_STATE_STOPWAIT);
953 fsm_event(privptr->conn->fsm, CONN_EVENT_STOP, &ev);
957 * Called from connection statemachine
958 * when a connection is up and running.
960 * @param fi An instance of an interface statemachine.
961 * @param event The event, just happened.
962 * @param arg Generic pointer, casted from net_device * upon call.
964 static void
965 dev_action_connup(fsm_instance *fi, int event, void *arg)
967 net_device *dev = (net_device *)arg;
969 #ifdef DEBUG
970 printk(KERN_DEBUG "%s() called\n", __FUNCTION__);
971 #endif
972 switch (fsm_getstate(fi)) {
973 case DEV_STATE_STARTWAIT:
974 fsm_newstate(fi, DEV_STATE_RUNNING);
975 printk(KERN_INFO
976 "%s: connected with remote side\n",
977 dev->name);
978 break;
979 case DEV_STATE_STOPWAIT:
980 printk(KERN_INFO
981 "%s: got connection UP event during shutdown!!\n",
982 dev->name);
983 break;
988 * Called from connection statemachine
989 * when a connection has been shutdown.
991 * @param fi An instance of an interface statemachine.
992 * @param event The event, just happened.
993 * @param arg Generic pointer, casted from net_device * upon call.
995 static void
996 dev_action_conndown(fsm_instance *fi, int event, void *arg)
998 net_device *dev = (net_device *)arg;
999 netiucv_priv *privptr = dev->priv;
1000 iucv_event ev;
1002 #ifdef DEBUG
1003 printk(KERN_DEBUG "%s() called\n", __FUNCTION__);
1004 #endif
1005 switch (fsm_getstate(fi)) {
1006 case DEV_STATE_RUNNING:
1007 fsm_newstate(fi, DEV_STATE_STARTWAIT);
1008 ev.conn = privptr->conn;
1009 fsm_event(privptr->conn->fsm, CONN_EVENT_START, &ev);
1010 break;
1011 case DEV_STATE_STARTWAIT:
1012 break;
1013 case DEV_STATE_STOPWAIT:
1014 fsm_newstate(fi, DEV_STATE_STOPPED);
1015 break;
1019 static const fsm_node dev_fsm[] = {
1020 { DEV_STATE_STOPPED, DEV_EVENT_START, dev_action_start },
1022 { DEV_STATE_STOPWAIT, DEV_EVENT_START, dev_action_start },
1023 { DEV_STATE_STOPWAIT, DEV_EVENT_CONDOWN, dev_action_conndown },
1025 { DEV_STATE_STARTWAIT, DEV_EVENT_STOP, dev_action_stop },
1026 { DEV_STATE_STARTWAIT, DEV_EVENT_CONUP, dev_action_connup },
1027 { DEV_STATE_STARTWAIT, DEV_EVENT_CONDOWN, dev_action_conndown },
1029 { DEV_STATE_RUNNING, DEV_EVENT_STOP, dev_action_stop },
1030 { DEV_STATE_RUNNING, DEV_EVENT_CONDOWN, dev_action_conndown },
1031 { DEV_STATE_RUNNING, DEV_EVENT_CONUP, fsm_action_nop },
1034 static const int DEV_FSM_LEN = sizeof(dev_fsm) / sizeof(fsm_node);
1037 * Transmit a packet.
1038 * This is a helper function for netiucv_tx().
1040 * @param conn Connection to be used for sending.
1041 * @param skb Pointer to struct sk_buff of packet to send.
1042 * The linklevel header has already been set up
1043 * by netiucv_tx().
1045 * @return 0 on success, -ERRNO on failure. (Never fails.)
1047 static int
1048 netiucv_transmit_skb(iucv_connection *conn, struct sk_buff *skb) {
1049 unsigned long saveflags;
1050 ll_header header;
1051 int rc = 0;
1053 if (fsm_getstate(conn->fsm) != CONN_STATE_IDLE) {
1054 int l = skb->len + NETIUCV_HDRLEN;
1056 spin_lock_irqsave(&conn->collect_lock, saveflags);
1057 if (conn->collect_len + l >
1058 (conn->max_buffsize - NETIUCV_HDRLEN))
1059 rc = -EBUSY;
1060 else {
1061 atomic_inc(&skb->users);
1062 skb_queue_tail(&conn->collect_queue, skb);
1063 conn->collect_len += l;
1065 spin_unlock_irqrestore(&conn->collect_lock, saveflags);
1066 } else {
1067 struct sk_buff *nskb = skb;
1069 * Copy the skb to a new allocated skb in lowmem only if the
1070 * data is located above 2G in memory or tailroom is < 2.
1072 unsigned long hi =
1073 ((unsigned long)(skb->tail + NETIUCV_HDRLEN)) >> 31;
1074 int copied = 0;
1075 if (hi || (skb_tailroom(skb) < 2)) {
1076 nskb = alloc_skb(skb->len + NETIUCV_HDRLEN +
1077 NETIUCV_HDRLEN, GFP_ATOMIC | GFP_DMA);
1078 if (!nskb) {
1079 printk(KERN_WARNING
1080 "%s: Could not allocate tx_skb\n",
1081 conn->netdev->name);
1082 rc = -ENOMEM;
1083 } else {
1084 skb_reserve(nskb, NETIUCV_HDRLEN);
1085 memcpy(skb_put(nskb, skb->len),
1086 skb->data, skb->len);
1088 copied = 1;
1091 * skb now is below 2G and has enough room. Add headers.
1093 header.next = nskb->len + NETIUCV_HDRLEN;
1094 memcpy(skb_push(nskb, NETIUCV_HDRLEN), &header, NETIUCV_HDRLEN);
1095 header.next = 0;
1096 memcpy(skb_put(nskb, NETIUCV_HDRLEN), &header, NETIUCV_HDRLEN);
1098 conn->retry = 0;
1099 fsm_newstate(conn->fsm, CONN_STATE_TX);
1100 fsm_addtimer(&conn->timer, NETIUCV_TIMEOUT_5SEC,
1101 CONN_EVENT_TIMER, conn);
1102 conn->prof.send_stamp = xtime;
1104 rc = iucv_send(conn->pathid, NULL, 0, 0,
1105 /* Shut up, gcc! nskb is always below 2G. */
1106 (__u32)(((unsigned long)nskb)&0xffffffff), 0,
1107 nskb->data, nskb->len);
1108 conn->prof.doios_single++;
1109 conn->prof.txlen += skb->len;
1110 if (rc != 0) {
1111 fsm_deltimer(&conn->timer);
1112 if (copied)
1113 dev_kfree_skb(nskb);
1114 else {
1116 * Remove our headers. They get added
1117 * again on retransmit.
1119 skb_pull(skb, NETIUCV_HDRLEN);
1120 skb_trim(skb, skb->len - NETIUCV_HDRLEN);
1122 } else {
1123 if (copied)
1124 dev_kfree_skb(skb);
1128 return rc;
1132 * Interface API for upper network layers
1133 *****************************************************************************/
1136 * Open an interface.
1137 * Called from generic network layer when ifconfig up is run.
1139 * @param dev Pointer to interface struct.
1141 * @return 0 on success, -ERRNO on failure. (Never fails.)
1143 static int
1144 netiucv_open(net_device *dev) {
1145 MOD_INC_USE_COUNT;
1146 SET_DEVICE_START(dev, 1);
1147 fsm_event(((netiucv_priv *)dev->priv)->fsm, DEV_EVENT_START, dev);
1148 return 0;
1152 * Close an interface.
1153 * Called from generic network layer when ifconfig down is run.
1155 * @param dev Pointer to interface struct.
1157 * @return 0 on success, -ERRNO on failure. (Never fails.)
1159 static int
1160 netiucv_close(net_device *dev) {
1161 SET_DEVICE_START(dev, 0);
1162 fsm_event(((netiucv_priv *)dev->priv)->fsm, DEV_EVENT_STOP, dev);
1163 MOD_DEC_USE_COUNT;
1164 return 0;
1168 * Start transmission of a packet.
1169 * Called from generic network device layer.
1171 * @param skb Pointer to buffer containing the packet.
1172 * @param dev Pointer to interface struct.
1174 * @return 0 if packet consumed, !0 if packet rejected.
1175 * Note: If we return !0, then the packet is free'd by
1176 * the generic network layer.
1178 static int netiucv_tx(struct sk_buff *skb, net_device *dev)
1180 int rc = 0;
1181 netiucv_priv *privptr = (netiucv_priv *)dev->priv;
1184 * Some sanity checks ...
1186 if (skb == NULL) {
1187 printk(KERN_WARNING "%s: NULL sk_buff passed\n", dev->name);
1188 privptr->stats.tx_dropped++;
1189 return 0;
1191 if (skb_headroom(skb) < (NETIUCV_HDRLEN)) {
1192 printk(KERN_WARNING
1193 "%s: Got sk_buff with head room < %ld bytes\n",
1194 dev->name, NETIUCV_HDRLEN);
1195 dev_kfree_skb(skb);
1196 privptr->stats.tx_dropped++;
1197 return 0;
1201 * If connection is not running, try to restart it
1202 * notify anybody about a link failure and throw
1203 * away packet.
1205 if (fsm_getstate(privptr->fsm) != DEV_STATE_RUNNING) {
1206 fsm_event(privptr->fsm, DEV_EVENT_START, dev);
1207 dst_link_failure(skb);
1208 dev_kfree_skb(skb);
1209 privptr->stats.tx_dropped++;
1210 privptr->stats.tx_errors++;
1211 privptr->stats.tx_carrier_errors++;
1212 return 0;
1215 if (netiucv_test_and_set_busy(dev))
1216 return -EBUSY;
1218 dev->trans_start = jiffies;
1219 if (netiucv_transmit_skb(privptr->conn, skb) != 0)
1220 rc = 1;
1221 netiucv_clear_busy(dev);
1222 return rc;
1226 * Returns interface statistics of a device.
1228 * @param dev Pointer to interface struct.
1230 * @return Pointer to stats struct of this interface.
1232 static struct net_device_stats *
1233 netiucv_stats (net_device * dev)
1235 return &((netiucv_priv *)dev->priv)->stats;
1239 * Sets MTU of an interface.
1241 * @param dev Pointer to interface struct.
1242 * @param new_mtu The new MTU to use for this interface.
1244 * @return 0 on success, -EINVAL if MTU is out of valid range.
1245 * (valid range is 576 .. NETIUCV_MTU_MAX).
1247 static int
1248 netiucv_change_mtu (net_device * dev, int new_mtu)
1250 if ((new_mtu < 576) || (new_mtu > NETIUCV_MTU_MAX))
1251 return -EINVAL;
1252 dev->mtu = new_mtu;
1253 return 0;
1257 * attributes in sysfs
1258 *****************************************************************************/
1259 #define CTRL_BUFSIZE 40
1261 static ssize_t
1262 user_show (struct device *dev, char *buf)
1264 netiucv_priv *priv = dev->driver_data;
1266 return snprintf(buf, PAGE_SIZE, "%s\n",
1267 netiucv_printname(priv->conn->userid));
1270 static ssize_t
1271 user_write (struct device *dev, const char *buf, size_t count)
1273 netiucv_priv *priv = dev->driver_data;
1274 struct net_device *ndev = container_of((void *)priv, struct net_device, priv);
1275 int i;
1276 char *p;
1277 char tmp[CTRL_BUFSIZE];
1278 char user[9];
1280 if (count >= 39)
1281 return -EINVAL;
1283 if (copy_from_user(tmp, buf, count))
1284 return -EFAULT;
1285 tmp[count+1] = '\0';
1287 memset(user, ' ', sizeof(user));
1288 user[8] = '\0';
1289 for (p = tmp, i = 0; *p && (!isspace(*p)); p++) {
1290 if (i > 7)
1291 return -EINVAL;
1292 user[i++] = *p;
1295 if (memcmp(user, priv->conn->userid, 8) != 0) {
1296 /* username changed */
1297 if (ndev->flags & IFF_RUNNING)
1298 return -EBUSY;
1300 memcpy(priv->conn->userid, user, 9);
1301 return count;
1305 static DEVICE_ATTR(user, 0644, user_show, user_write);
1307 static ssize_t
1308 buffer_show (struct device *dev, char *buf)
1310 netiucv_priv *priv = dev->driver_data;
1312 return sprintf(buf, "%d\n",
1313 priv->conn->max_buffsize);
1316 static ssize_t
1317 buffer_write (struct device *dev, const char *buf, size_t count)
1319 netiucv_priv *priv = dev->driver_data;
1320 struct net_device *ndev = container_of((void *)priv, struct net_device, priv);
1321 char *e;
1322 int bs1;
1323 char tmp[CTRL_BUFSIZE];
1325 if (count >= 39)
1326 return -EINVAL;
1328 if (copy_from_user(tmp, buf, count))
1329 return -EFAULT;
1330 tmp[count+1] = '\0';
1331 bs1 = simple_strtoul(tmp, &e, 0);
1333 if ((bs1 > NETIUCV_BUFSIZE_MAX) ||
1334 (e && (!isspace(*e))))
1335 return -EINVAL;
1336 if ((ndev->flags & IFF_RUNNING) &&
1337 (bs1 < (ndev->mtu + NETIUCV_HDRLEN + 2)))
1338 return -EINVAL;
1339 if (bs1 < (576 + NETIUCV_HDRLEN + NETIUCV_HDRLEN))
1340 return -EINVAL;
1343 priv->conn->max_buffsize = bs1;
1344 if (!(ndev->flags & IFF_RUNNING))
1345 ndev->mtu = bs1 - NETIUCV_HDRLEN - NETIUCV_HDRLEN;
1346 priv->conn->flags |= CONN_FLAGS_BUFSIZE_CHANGED;
1348 return count;
1352 static DEVICE_ATTR(buffer, 0644, buffer_show, buffer_write);
1354 static ssize_t
1355 dev_fsm_show (struct device *dev, char *buf)
1357 netiucv_priv *priv = dev->driver_data;
1359 return snprintf(buf, PAGE_SIZE, "%s\n",
1360 fsm_getstate_str(priv->fsm));
1363 static DEVICE_ATTR(device_fsm_state, 0444, dev_fsm_show, NULL);
1365 static ssize_t
1366 conn_fsm_show (struct device *dev, char *buf)
1368 netiucv_priv *priv = dev->driver_data;
1370 return snprintf(buf, PAGE_SIZE, "%s\n",
1371 fsm_getstate_str(priv->conn->fsm));
1374 static DEVICE_ATTR(connection_fsm_state, 0444, conn_fsm_show, NULL);
1376 static ssize_t
1377 maxmulti_show (struct device *dev, char *buf)
1379 netiucv_priv *priv = dev->driver_data;
1381 return sprintf(buf, "%ld\n", priv->conn->prof.maxmulti);
1384 static ssize_t
1385 maxmulti_write (struct device *dev, const char *buf, size_t count)
1387 netiucv_priv *priv = dev->driver_data;
1389 priv->conn->prof.maxmulti = 0;
1390 return count;
1393 static DEVICE_ATTR(max_tx_buffer_used, 0644, maxmulti_show, maxmulti_write);
1395 static ssize_t
1396 maxcq_show (struct device *dev, char *buf)
1398 netiucv_priv *priv = dev->driver_data;
1400 return sprintf(buf, "%ld\n", priv->conn->prof.maxcqueue);
1403 static ssize_t
1404 maxcq_write (struct device *dev, const char *buf, size_t count)
1406 netiucv_priv *priv = dev->driver_data;
1408 priv->conn->prof.maxcqueue = 0;
1409 return count;
1412 static DEVICE_ATTR(max_chained_skbs, 0644, maxcq_show, maxcq_write);
1414 static ssize_t
1415 sdoio_show (struct device *dev, char *buf)
1417 netiucv_priv *priv = dev->driver_data;
1419 return sprintf(buf, "%ld\n", priv->conn->prof.doios_single);
1422 static ssize_t
1423 sdoio_write (struct device *dev, const char *buf, size_t count)
1425 netiucv_priv *priv = dev->driver_data;
1427 priv->conn->prof.doios_single = 0;
1428 return count;
1431 static DEVICE_ATTR(tx_single_write_ops, 0644, sdoio_show, sdoio_write);
1433 static ssize_t
1434 mdoio_show (struct device *dev, char *buf)
1436 netiucv_priv *priv = dev->driver_data;
1438 return sprintf(buf, "%ld\n", priv->conn->prof.doios_multi);
1441 static ssize_t
1442 mdoio_write (struct device *dev, const char *buf, size_t count)
1444 netiucv_priv *priv = dev->driver_data;
1446 priv->conn->prof.doios_multi = 0;
1447 return count;
1450 static DEVICE_ATTR(tx_multi_write_ops, 0644, mdoio_show, mdoio_write);
1452 static ssize_t
1453 txlen_show (struct device *dev, char *buf)
1455 netiucv_priv *priv = dev->driver_data;
1457 return sprintf(buf, "%ld\n", priv->conn->prof.txlen);
1460 static ssize_t
1461 txlen_write (struct device *dev, const char *buf, size_t count)
1463 netiucv_priv *priv = dev->driver_data;
1465 priv->conn->prof.txlen = 0;
1466 return count;
1469 static DEVICE_ATTR(netto_bytes, 0644, txlen_show, txlen_write);
1471 static ssize_t
1472 txtime_show (struct device *dev, char *buf)
1474 netiucv_priv *priv = dev->driver_data;
1476 return sprintf(buf, "%ld\n", priv->conn->prof.tx_time);
1479 static ssize_t
1480 txtime_write (struct device *dev, const char *buf, size_t count)
1482 netiucv_priv *priv = dev->driver_data;
1484 priv->conn->prof.tx_time = 0;
1485 return count;
1488 static DEVICE_ATTR(max_tx_io_time, 0644, txtime_show, txtime_write);
1490 static inline int
1491 netiucv_add_files(struct device *dev)
1493 int ret = 0;
1495 if ((ret = device_create_file(dev, &dev_attr_user)) ||
1496 (ret = device_create_file(dev, &dev_attr_buffer)) ||
1497 (ret = device_create_file(dev, &dev_attr_device_fsm_state)) ||
1498 (ret = device_create_file(dev, &dev_attr_connection_fsm_state)) ||
1499 (ret = device_create_file(dev, &dev_attr_max_tx_buffer_used)) ||
1500 (ret = device_create_file(dev, &dev_attr_max_chained_skbs)) ||
1501 (ret = device_create_file(dev, &dev_attr_tx_single_write_ops)) ||
1502 (ret = device_create_file(dev, &dev_attr_tx_multi_write_ops)) ||
1503 (ret = device_create_file(dev, &dev_attr_netto_bytes)) ||
1504 (ret = device_create_file(dev, &dev_attr_max_tx_io_time))) {
1505 device_remove_file(dev, &dev_attr_netto_bytes);
1506 device_remove_file(dev, &dev_attr_tx_multi_write_ops);
1507 device_remove_file(dev, &dev_attr_tx_single_write_ops);
1508 device_remove_file(dev, &dev_attr_max_chained_skbs);
1509 device_remove_file(dev, &dev_attr_max_tx_buffer_used);
1510 device_remove_file(dev, &dev_attr_connection_fsm_state);
1511 device_remove_file(dev, &dev_attr_device_fsm_state);
1512 device_remove_file(dev, &dev_attr_buffer);
1513 device_remove_file(dev, &dev_attr_user);
1515 return ret;
1518 static int
1519 netiucv_register_device(struct net_device *ndev, int ifno)
1521 netiucv_priv *priv = ndev->priv;
1522 struct platform_device *pldev = &priv->pldev;
1523 int ret;
1524 char *str = "netiucv";
1526 snprintf(pldev->dev.name, DEVICE_NAME_SIZE,
1527 "%s%x", str, ifno);
1528 pldev->name = str;
1529 pldev->id = ifno;
1531 ret = platform_device_register(pldev);
1533 if (ret)
1534 return ret;
1536 ret = netiucv_add_files(&pldev->dev);
1538 if (ret)
1539 platform_device_unregister(pldev);
1540 else
1541 pldev->dev.driver_data = priv;
1542 return ret;
1545 #ifdef MODULE
1546 static void
1547 netiucv_unregister_device(struct net_device *ndev)
1549 netiucv_priv *priv = (netiucv_priv*)ndev->priv;
1550 struct platform_device *pldev = &priv->pldev;
1552 platform_device_unregister(pldev);
1554 #endif
1557 * Allocate and initialize a new connection structure.
1558 * Add it to the list of connections;
1560 static iucv_connection *
1561 netiucv_new_connection(net_device *dev, char *username)
1563 iucv_connection **clist = &connections;
1564 iucv_connection *conn =
1565 (iucv_connection *)kmalloc(sizeof(iucv_connection), GFP_KERNEL);
1566 if (conn) {
1567 memset(conn, 0, sizeof(iucv_connection));
1568 skb_queue_head_init(&conn->collect_queue);
1569 conn->max_buffsize = NETIUCV_BUFSIZE_DEFAULT;
1570 conn->netdev = dev;
1572 conn->rx_buff = alloc_skb(NETIUCV_BUFSIZE_DEFAULT, GFP_DMA);
1573 if (!conn->rx_buff) {
1574 kfree(conn);
1575 return NULL;
1577 conn->tx_buff = alloc_skb(NETIUCV_BUFSIZE_DEFAULT, GFP_DMA);
1578 if (!conn->tx_buff) {
1579 kfree_skb(conn->rx_buff);
1580 kfree(conn);
1581 return NULL;
1583 conn->fsm = init_fsm("netiucvconn", conn_state_names,
1584 conn_event_names, NR_CONN_STATES,
1585 NR_CONN_EVENTS, conn_fsm, CONN_FSM_LEN,
1586 GFP_KERNEL);
1587 if (!conn->fsm) {
1588 kfree_skb(conn->tx_buff);
1589 kfree_skb(conn->rx_buff);
1590 kfree(conn);
1591 return NULL;
1593 fsm_settimer(conn->fsm, &conn->timer);
1594 fsm_newstate(conn->fsm, CONN_STATE_INVALID);
1596 if (username) {
1597 memcpy(conn->userid, username, 9);
1598 fsm_newstate(conn->fsm, CONN_STATE_STOPPED);
1601 conn->next = *clist;
1602 *clist = conn;
1604 return conn;
1608 * Release a connection structure and remove it from the
1609 * list of connections.
1611 static void
1612 netiucv_remove_connection(iucv_connection *conn)
1614 iucv_connection **clist = &connections;
1616 if (conn == NULL)
1617 return;
1618 while (*clist) {
1619 if (*clist == conn) {
1620 *clist = conn->next;
1621 if (conn->handle != 0) {
1622 iucv_unregister_program(conn->handle);
1623 conn->handle = 0;
1625 fsm_deltimer(&conn->timer);
1626 kfree_fsm(conn->fsm);
1627 kfree_skb(conn->rx_buff);
1628 kfree_skb(conn->tx_buff);
1629 return;
1631 clist = &((*clist)->next);
1636 * Allocate and initialize everything of a net device.
1638 static net_device *
1639 netiucv_init_netdevice(int ifno, char *username)
1641 netiucv_priv *privptr;
1642 int priv_size;
1644 net_device *dev = kmalloc(sizeof(net_device), GFP_KERNEL);
1645 if (!dev)
1646 return NULL;
1647 memset(dev, 0, sizeof(net_device));
1648 sprintf(dev->name, "iucv%d", ifno);
1650 priv_size = sizeof(netiucv_priv);
1651 dev->priv = kmalloc(priv_size, GFP_KERNEL);
1652 if (dev->priv == NULL) {
1653 kfree(dev);
1654 return NULL;
1656 memset(dev->priv, 0, priv_size);
1657 privptr = (netiucv_priv *)dev->priv;
1658 privptr->fsm = init_fsm("netiucvdev", dev_state_names,
1659 dev_event_names, NR_DEV_STATES, NR_DEV_EVENTS,
1660 dev_fsm, DEV_FSM_LEN, GFP_KERNEL);
1661 if (privptr->fsm == NULL) {
1662 kfree(privptr);
1663 kfree(dev);
1664 return NULL;
1666 privptr->conn = netiucv_new_connection(dev, username);
1667 if (!privptr->conn) {
1668 kfree_fsm(privptr->fsm);
1669 kfree(privptr);
1670 kfree(dev);
1671 return NULL;
1674 fsm_newstate(privptr->fsm, DEV_STATE_STOPPED);
1675 dev->mtu = NETIUCV_MTU_DEFAULT;
1676 dev->hard_start_xmit = netiucv_tx;
1677 dev->open = netiucv_open;
1678 dev->stop = netiucv_close;
1679 dev->get_stats = netiucv_stats;
1680 dev->change_mtu = netiucv_change_mtu;
1681 dev->hard_header_len = NETIUCV_HDRLEN;
1682 dev->addr_len = 0;
1683 dev->type = ARPHRD_SLIP;
1684 dev->tx_queue_len = NETIUCV_QUEUELEN_DEFAULT;
1685 dev_init_buffers(dev);
1686 dev->flags = IFF_POINTOPOINT | IFF_NOARP;
1687 return dev;
1691 * Allocate and initialize everything of a net device.
1693 static void
1694 netiucv_free_netdevice(net_device *dev)
1696 netiucv_priv *privptr;
1698 if (!dev)
1699 return;
1701 privptr = (netiucv_priv *)dev->priv;
1702 if (privptr) {
1703 if (privptr->conn)
1704 netiucv_remove_connection(privptr->conn);
1705 if (privptr->fsm)
1706 kfree_fsm(privptr->fsm);
1707 kfree(privptr);
1709 kfree(dev);
1712 static void
1713 netiucv_banner(void)
1715 char vbuf[] = "$Revision: 1.12 $";
1716 char *version = vbuf;
1718 if ((version = strchr(version, ':'))) {
1719 char *p = strchr(version + 1, '$');
1720 if (p)
1721 *p = '\0';
1722 } else
1723 version = " ??? ";
1724 printk(KERN_INFO "NETIUCV driver Version%s initialized\n", version);
1727 #ifndef MODULE
1728 static int __init
1729 iucv_setup(char *param)
1732 * We do not parse parameters here because at the time of
1733 * calling iucv_setup(), the kernel does not yet have
1734 * memory management running. We delay this until probing
1735 * is called.
1737 iucv = param;
1738 return 1;
1741 __setup ("iucv=", iucv_setup);
1743 #else
1745 static void __exit
1746 netiucv_exit(void)
1748 while (connections) {
1749 net_device *dev = connections->netdev;
1750 unregister_netdev(dev);
1751 netiucv_unregister_device(dev);
1752 netiucv_free_netdevice(dev);
1755 printk(KERN_INFO "NETIUCV driver unloaded\n");
1756 return;
1759 module_exit(netiucv_exit);
1760 #endif
1762 static int __init
1763 netiucv_init(void)
1765 char *p = iucv;
1766 int ifno = 0;
1767 int i = 0;
1768 char username[10];
1770 while (p) {
1771 if (isalnum(*p)) {
1772 username[i++] = *p++;
1773 username[i] = '\0';
1774 if (i > 8) {
1775 printk(KERN_WARNING
1776 "netiucv: Invalid user name '%s'\n",
1777 username);
1778 while (*p && (*p != ':') && (*p != ','))
1779 p++;
1781 } else {
1782 if (*p && (*p != ':') && (*p != ',')) {
1783 printk(KERN_WARNING
1784 "netiucv: Invalid delimiter '%c'\n",
1785 *p);
1786 while (*p && (*p != ':') && (*p != ','))
1787 p++;
1788 } else {
1789 if (i) {
1790 net_device *dev;
1792 while (i < 9)
1793 username[i++] = ' ';
1794 username[9] = '\0';
1795 dev = netiucv_init_netdevice(ifno,
1796 username);
1797 if (!dev)
1798 printk(KERN_WARNING
1799 "netiucv: Could not allocate network device structure for user '%s'\n", netiucv_printname(username));
1800 else {
1801 if (register_netdev(dev)) {
1802 printk(KERN_WARNING
1803 "netiucv: Could not register '%s'\n", dev->name);
1804 netiucv_free_netdevice(dev);
1805 } else {
1806 printk(KERN_INFO "%s: '%s'\n", dev->name, netiucv_printname(username));
1807 netiucv_register_device(dev, ifno);
1808 ifno++;
1812 if (!(*p))
1813 break;
1814 i = 0;
1815 p++;
1819 netiucv_banner();
1820 return 0;
1823 module_init(netiucv_init);
1824 MODULE_LICENSE("GPL");