Merge with Linux 2.5.74.
[linux-2.6/linux-mips.git] / drivers / s390 / net / netiucv.c
blob89359886cfa0acfa14ae298307cacd306cf5df71
1 /*
2 * $Id: netiucv.c,v 1.20 2003/05/27 11:34:24 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.20 $
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 MODULE_AUTHOR
69 ("(C) 2001 IBM Corporation by Fritz Elfert (felfert@millenux.com)");
70 MODULE_DESCRIPTION ("Linux for S/390 IUCV network driver");
73 /**
74 * Per connection profiling data
76 struct connection_profile {
77 unsigned long maxmulti;
78 unsigned long maxcqueue;
79 unsigned long doios_single;
80 unsigned long doios_multi;
81 unsigned long txlen;
82 unsigned long tx_time;
83 struct timespec send_stamp;
86 /**
87 * Representation of one iucv connection
89 struct iucv_connection {
90 struct iucv_connection *next;
91 iucv_handle_t handle;
92 __u16 pathid;
93 struct sk_buff *rx_buff;
94 struct sk_buff *tx_buff;
95 struct sk_buff_head collect_queue;
96 spinlock_t collect_lock;
97 int collect_len;
98 int max_buffsize;
99 int flags;
100 fsm_timer timer;
101 int retry;
102 fsm_instance *fsm;
103 struct net_device *netdev;
104 struct connection_profile prof;
105 char userid[9];
108 #define CONN_FLAGS_BUFSIZE_CHANGED 1
111 * Linked list of all connection structs.
113 static struct iucv_connection *connections;
115 /* Keep track of interfaces. */
116 static int ifno;
118 static int
119 iucv_bus_match (struct device *dev, struct device_driver *drv)
121 return 0;
124 /* Hm - move to iucv.c and export? - CH */
125 static struct bus_type iucv_bus = {
126 .name = "iucv",
127 .match = iucv_bus_match,
130 static struct device iucv_root = {
131 .name = "IUCV",
132 .bus_id = "iucv",
136 * Representation of event-data for the
137 * connection state machine.
139 struct iucv_event {
140 struct iucv_connection *conn;
141 void *data;
145 * Private part of the network device structure
147 struct netiucv_priv {
148 struct net_device_stats stats;
149 unsigned long tbusy;
150 fsm_instance *fsm;
151 struct iucv_connection *conn;
152 struct device dev;
156 * Link level header for a packet.
158 typedef struct ll_header_t {
159 __u16 next;
160 } ll_header;
162 #define NETIUCV_HDRLEN (sizeof(ll_header))
163 #define NETIUCV_BUFSIZE_MAX 32768
164 #define NETIUCV_BUFSIZE_DEFAULT NETIUCV_BUFSIZE_MAX
165 #define NETIUCV_MTU_MAX (NETIUCV_BUFSIZE_MAX - NETIUCV_HDRLEN)
166 #define NETIUCV_MTU_DEFAULT 9216
167 #define NETIUCV_QUEUELEN_DEFAULT 50
168 #define NETIUCV_TIMEOUT_5SEC 5000
171 * Compatibility macros for busy handling
172 * of network devices.
174 static __inline__ void netiucv_clear_busy(struct net_device *dev)
176 clear_bit(0, &(((struct netiucv_priv *)dev->priv)->tbusy));
177 netif_wake_queue(dev);
180 static __inline__ int netiucv_test_and_set_busy(struct net_device *dev)
182 netif_stop_queue(dev);
183 return test_and_set_bit(0, &((struct netiucv_priv *)dev->priv)->tbusy);
186 #define SET_DEVICE_START(device, value)
188 static __u8 iucv_host[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
189 static __u8 iucvMagic[16] = {
190 0xF0, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
191 0xF0, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40
195 * This mask means the 16-byte IUCV "magic" and the origin userid must
196 * match exactly as specified in order to give connection_pending()
197 * control.
199 static __u8 mask[] = {
200 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
201 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
202 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
206 * Convert an iucv userId to its printable
207 * form (strip whitespace at end).
209 * @param An iucv userId
211 * @returns The printable string (static data!!)
213 static __inline__ char *
214 netiucv_printname(char *name)
216 static char tmp[9];
217 char *p = tmp;
218 memcpy(tmp, name, 8);
219 tmp[8] = '\0';
220 while (*p && (!isspace(*p)))
221 p++;
222 *p = '\0';
223 return tmp;
227 * States of the interface statemachine.
229 enum dev_states {
230 DEV_STATE_STOPPED,
231 DEV_STATE_STARTWAIT,
232 DEV_STATE_STOPWAIT,
233 DEV_STATE_RUNNING,
235 * MUST be always the last element!!
237 NR_DEV_STATES
240 static const char *dev_state_names[] = {
241 "Stopped",
242 "StartWait",
243 "StopWait",
244 "Running",
248 * Events of the interface statemachine.
250 enum dev_events {
251 DEV_EVENT_START,
252 DEV_EVENT_STOP,
253 DEV_EVENT_CONUP,
254 DEV_EVENT_CONDOWN,
256 * MUST be always the last element!!
258 NR_DEV_EVENTS
261 static const char *dev_event_names[] = {
262 "Start",
263 "Stop",
264 "Connection up",
265 "Connection down",
269 * Events of the connection statemachine
271 enum conn_events {
273 * Events, representing callbacks from
274 * lowlevel iucv layer)
276 CONN_EVENT_CONN_REQ,
277 CONN_EVENT_CONN_ACK,
278 CONN_EVENT_CONN_REJ,
279 CONN_EVENT_CONN_SUS,
280 CONN_EVENT_CONN_RES,
281 CONN_EVENT_RX,
282 CONN_EVENT_TXDONE,
285 * Events, representing errors return codes from
286 * calls to lowlevel iucv layer
290 * Event, representing timer expiry.
292 CONN_EVENT_TIMER,
295 * Events, representing commands from upper levels.
297 CONN_EVENT_START,
298 CONN_EVENT_STOP,
301 * MUST be always the last element!!
303 NR_CONN_EVENTS,
306 static const char *conn_event_names[] = {
307 "Remote connection request",
308 "Remote connection acknowledge",
309 "Remote connection reject",
310 "Connection suspended",
311 "Connection resumed",
312 "Data received",
313 "Data sent",
315 "Timer",
317 "Start",
318 "Stop",
322 * States of the connection statemachine.
324 enum conn_states {
326 * Connection not assigned to any device,
327 * initial state, invalid
329 CONN_STATE_INVALID,
332 * Userid assigned but not operating
334 CONN_STATE_STOPPED,
337 * Connection registered,
338 * no connection request sent yet,
339 * no connection request received
341 CONN_STATE_STARTWAIT,
344 * Connection registered and connection request sent,
345 * no acknowledge and no connection request received yet.
347 CONN_STATE_SETUPWAIT,
350 * Connection up and running idle
352 CONN_STATE_IDLE,
355 * Data sent, awaiting CONN_EVENT_TXDONE
357 CONN_STATE_TX,
360 * Terminating
362 CONN_STATE_TERM,
365 * Error during registration.
367 CONN_STATE_REGERR,
370 * Error during registration.
372 CONN_STATE_CONNERR,
375 * MUST be always the last element!!
377 NR_CONN_STATES,
380 static const char *conn_state_names[] = {
381 "Invalid",
382 "Stopped",
383 "StartWait",
384 "SetupWait",
385 "Idle",
386 "TX",
387 "Terminating",
388 "Registration error",
389 "Connect error",
394 * Callback-wrappers, called from lowlevel iucv layer.
395 *****************************************************************************/
397 static void
398 netiucv_callback_rx(iucv_MessagePending *eib, void *pgm_data)
400 struct iucv_connection *conn = (struct iucv_connection *)pgm_data;
401 struct iucv_event ev;
403 ev.conn = conn;
404 ev.data = (void *)eib;
406 fsm_event(conn->fsm, CONN_EVENT_RX, &ev);
409 static void
410 netiucv_callback_txdone(iucv_MessageComplete *eib, void *pgm_data)
412 struct iucv_connection *conn = (struct iucv_connection *)pgm_data;
413 struct iucv_event ev;
415 ev.conn = conn;
416 ev.data = (void *)eib;
417 fsm_event(conn->fsm, CONN_EVENT_TXDONE, &ev);
420 static void
421 netiucv_callback_connack(iucv_ConnectionComplete *eib, void *pgm_data)
423 struct iucv_connection *conn = (struct iucv_connection *)pgm_data;
424 struct iucv_event ev;
426 ev.conn = conn;
427 ev.data = (void *)eib;
428 fsm_event(conn->fsm, CONN_EVENT_CONN_ACK, &ev);
431 static void
432 netiucv_callback_connreq(iucv_ConnectionPending *eib, void *pgm_data)
434 struct iucv_connection *conn = (struct iucv_connection *)pgm_data;
435 struct iucv_event ev;
437 ev.conn = conn;
438 ev.data = (void *)eib;
439 fsm_event(conn->fsm, CONN_EVENT_CONN_REQ, &ev);
442 static void
443 netiucv_callback_connrej(iucv_ConnectionSevered *eib, void *pgm_data)
445 struct iucv_connection *conn = (struct iucv_connection *)pgm_data;
446 struct iucv_event ev;
448 ev.conn = conn;
449 ev.data = (void *)eib;
450 fsm_event(conn->fsm, CONN_EVENT_CONN_REJ, &ev);
453 static void
454 netiucv_callback_connsusp(iucv_ConnectionQuiesced *eib, void *pgm_data)
456 struct iucv_connection *conn = (struct iucv_connection *)pgm_data;
457 struct iucv_event ev;
459 ev.conn = conn;
460 ev.data = (void *)eib;
461 fsm_event(conn->fsm, CONN_EVENT_CONN_SUS, &ev);
464 static void
465 netiucv_callback_connres(iucv_ConnectionResumed *eib, void *pgm_data)
467 struct iucv_connection *conn = (struct iucv_connection *)pgm_data;
468 struct iucv_event ev;
470 ev.conn = conn;
471 ev.data = (void *)eib;
472 fsm_event(conn->fsm, CONN_EVENT_CONN_RES, &ev);
475 static iucv_interrupt_ops_t netiucv_ops = {
476 .ConnectionPending = netiucv_callback_connreq,
477 .ConnectionComplete = netiucv_callback_connack,
478 .ConnectionSevered = netiucv_callback_connrej,
479 .ConnectionQuiesced = netiucv_callback_connsusp,
480 .ConnectionResumed = netiucv_callback_connres,
481 .MessagePending = netiucv_callback_rx,
482 .MessageComplete = netiucv_callback_txdone
486 * Dummy NOP action for all statemachines
488 static void
489 fsm_action_nop(fsm_instance *fi, int event, void *arg)
494 * Actions of the connection statemachine
495 *****************************************************************************/
498 * Helper function for conn_action_rx()
499 * Unpack a just received skb and hand it over to
500 * upper layers.
502 * @param conn The connection where this skb has been received.
503 * @param pskb The received skb.
505 //static __inline__ void
506 static void
507 netiucv_unpack_skb(struct iucv_connection *conn, struct sk_buff *pskb)
509 struct net_device *dev = conn->netdev;
510 struct netiucv_priv *privptr = (struct netiucv_priv *)dev->priv;
511 __u16 offset = 0;
513 skb_put(pskb, NETIUCV_HDRLEN);
514 pskb->dev = dev;
515 pskb->ip_summed = CHECKSUM_NONE;
516 pskb->protocol = ntohs(ETH_P_IP);
518 while (1) {
519 struct sk_buff *skb;
520 ll_header *header = (ll_header *)pskb->data;
522 if (header->next == 0)
523 break;
525 skb_pull(pskb, NETIUCV_HDRLEN);
526 header->next -= offset;
527 offset += header->next;
528 header->next -= NETIUCV_HDRLEN;
529 if (skb_tailroom(pskb) < header->next) {
530 printk(KERN_WARNING
531 "%s: Illegal next field in iucv header: "
532 "%d > %d\n",
533 dev->name, header->next, skb_tailroom(pskb));
534 return;
536 skb_put(pskb, header->next);
537 pskb->mac.raw = pskb->data;
538 skb = dev_alloc_skb(pskb->len);
539 if (!skb) {
540 printk(KERN_WARNING
541 "%s Out of memory in netiucv_unpack_skb\n",
542 dev->name);
543 privptr->stats.rx_dropped++;
544 return;
546 memcpy(skb_put(skb, pskb->len), pskb->data, pskb->len);
547 skb->mac.raw = skb->data;
548 skb->dev = pskb->dev;
549 skb->protocol = pskb->protocol;
550 pskb->ip_summed = CHECKSUM_UNNECESSARY;
551 netif_rx(skb);
552 dev->last_rx = jiffies;
553 privptr->stats.rx_packets++;
554 privptr->stats.rx_bytes += skb->len;
555 skb_pull(pskb, header->next);
556 skb_put(pskb, NETIUCV_HDRLEN);
560 static void
561 conn_action_rx(fsm_instance *fi, int event, void *arg)
563 struct iucv_event *ev = (struct iucv_event *)arg;
564 struct iucv_connection *conn = ev->conn;
565 iucv_MessagePending *eib = (iucv_MessagePending *)ev->data;
566 struct netiucv_priv *privptr = (struct netiucv_priv *)conn->netdev->priv;
568 __u16 msglen = eib->ln1msg2.ipbfln1f;
569 int rc;
571 pr_debug("%s() called\n", __FUNCTION__);
573 if (!conn->netdev) {
574 /* FRITZ: How to tell iucv LL to drop the msg? */
575 printk(KERN_WARNING
576 "Received data for unlinked connection\n");
577 return;
579 if (msglen > conn->max_buffsize) {
580 /* FRITZ: How to tell iucv LL to drop the msg? */
581 privptr->stats.rx_dropped++;
582 return;
584 conn->rx_buff->data = conn->rx_buff->tail = conn->rx_buff->head;
585 conn->rx_buff->len = 0;
586 rc = iucv_receive(conn->pathid, eib->ipmsgid, eib->iptrgcls,
587 conn->rx_buff->data, msglen, NULL, NULL, NULL);
588 if (rc != 0 || msglen < 5) {
589 privptr->stats.rx_errors++;
590 return;
592 netiucv_unpack_skb(conn, conn->rx_buff);
595 static void
596 conn_action_txdone(fsm_instance *fi, int event, void *arg)
598 struct iucv_event *ev = (struct iucv_event *)arg;
599 struct iucv_connection *conn = ev->conn;
600 iucv_MessageComplete *eib = (iucv_MessageComplete *)ev->data;
601 struct netiucv_priv *privptr = NULL;
602 /* Shut up, gcc! skb is always below 2G. */
603 struct sk_buff *skb = (struct sk_buff *)(unsigned long)eib->ipmsgtag;
604 __u32 txbytes = 0;
605 __u32 txpackets = 0;
606 __u32 stat_maxcq = 0;
607 unsigned long saveflags;
608 ll_header header;
610 pr_debug("%s() called\n", __FUNCTION__);
612 fsm_deltimer(&conn->timer);
613 if (conn && conn->netdev && conn->netdev->priv)
614 privptr = (struct netiucv_priv *)conn->netdev->priv;
615 if (skb) {
616 if (privptr) {
617 privptr->stats.tx_packets++;
618 privptr->stats.tx_bytes +=
619 (skb->len - NETIUCV_HDRLEN - NETIUCV_HDRLEN);
621 dev_kfree_skb_any(skb);
623 conn->tx_buff->data = conn->tx_buff->tail = conn->tx_buff->head;
624 conn->tx_buff->len = 0;
625 spin_lock_irqsave(&conn->collect_lock, saveflags);
626 while ((skb = skb_dequeue(&conn->collect_queue))) {
627 header.next = conn->tx_buff->len + skb->len + NETIUCV_HDRLEN;
628 memcpy(skb_put(conn->tx_buff, NETIUCV_HDRLEN), &header,
629 NETIUCV_HDRLEN);
630 memcpy(skb_put(conn->tx_buff, skb->len), skb->data, skb->len);
631 txbytes += skb->len;
632 txpackets++;
633 stat_maxcq++;
634 atomic_dec(&skb->users);
635 dev_kfree_skb_any(skb);
637 if (conn->collect_len > conn->prof.maxmulti)
638 conn->prof.maxmulti = conn->collect_len;
639 conn->collect_len = 0;
640 spin_unlock_irqrestore(&conn->collect_lock, saveflags);
641 if (conn->tx_buff->len) {
642 int rc;
644 header.next = 0;
645 memcpy(skb_put(conn->tx_buff, NETIUCV_HDRLEN), &header,
646 NETIUCV_HDRLEN);
648 fsm_addtimer(&conn->timer, NETIUCV_TIMEOUT_5SEC,
649 CONN_EVENT_TIMER, conn);
650 conn->prof.send_stamp = xtime;
651 rc = iucv_send(conn->pathid, NULL, 0, 0, 0, 0,
652 conn->tx_buff->data, conn->tx_buff->len);
653 conn->prof.doios_multi++;
654 conn->prof.txlen += conn->tx_buff->len;
655 if (rc != 0) {
656 fsm_deltimer(&conn->timer);
657 fsm_newstate(fi, CONN_STATE_IDLE);
658 if (privptr)
659 privptr->stats.tx_errors += txpackets;
660 } else {
661 if (privptr) {
662 privptr->stats.tx_packets += txpackets;
663 privptr->stats.tx_bytes += txbytes;
665 if (stat_maxcq > conn->prof.maxcqueue)
666 conn->prof.maxcqueue = stat_maxcq;
668 } else
669 fsm_newstate(fi, CONN_STATE_IDLE);
672 static void
673 conn_action_connaccept(fsm_instance *fi, int event, void *arg)
675 struct iucv_event *ev = (struct iucv_event *)arg;
676 struct iucv_connection *conn = ev->conn;
677 iucv_ConnectionPending *eib = (iucv_ConnectionPending *)ev->data;
678 struct net_device *netdev = conn->netdev;
679 struct netiucv_priv *privptr = (struct netiucv_priv *)netdev->priv;
680 int rc;
681 __u16 msglimit;
682 __u8 udata[16];
684 pr_debug("%s() called\n", __FUNCTION__);
686 rc = iucv_accept(eib->ippathid, NETIUCV_QUEUELEN_DEFAULT, udata, 0,
687 conn->handle, conn, NULL, &msglimit);
688 if (rc != 0) {
689 printk(KERN_WARNING
690 "%s: IUCV accept failed with error %d\n",
691 netdev->name, rc);
692 return;
694 fsm_newstate(fi, CONN_STATE_IDLE);
695 conn->pathid = eib->ippathid;
696 netdev->tx_queue_len = msglimit;
697 fsm_event(privptr->fsm, DEV_EVENT_CONUP, netdev);
700 static void
701 conn_action_connreject(fsm_instance *fi, int event, void *arg)
703 struct iucv_event *ev = (struct iucv_event *)arg;
704 // struct iucv_connection *conn = ev->conn;
705 iucv_ConnectionPending *eib = (iucv_ConnectionPending *)ev->data;
706 __u8 udata[16];
708 pr_debug("%s() called\n", __FUNCTION__);
710 iucv_sever(eib->ippathid, udata);
713 static void
714 conn_action_connack(fsm_instance *fi, int event, void *arg)
716 struct iucv_event *ev = (struct iucv_event *)arg;
717 struct iucv_connection *conn = ev->conn;
718 iucv_ConnectionComplete *eib = (iucv_ConnectionComplete *)ev->data;
719 struct net_device *netdev = conn->netdev;
720 struct netiucv_priv *privptr = (struct netiucv_priv *)netdev->priv;
722 pr_debug("%s() called\n", __FUNCTION__);
724 fsm_newstate(fi, CONN_STATE_IDLE);
725 conn->pathid = eib->ippathid;
726 netdev->tx_queue_len = eib->ipmsglim;
727 fsm_event(privptr->fsm, DEV_EVENT_CONUP, netdev);
730 static void
731 conn_action_connsever(fsm_instance *fi, int event, void *arg)
733 struct iucv_event *ev = (struct iucv_event *)arg;
734 struct iucv_connection *conn = ev->conn;
735 // iucv_ConnectionSevered *eib = (iucv_ConnectionSevered *)ev->data;
736 struct net_device *netdev = conn->netdev;
737 struct netiucv_priv *privptr = (struct netiucv_priv *)netdev->priv;
738 int state = fsm_getstate(fi);
740 pr_debug("%s() called\n", __FUNCTION__);
742 switch (state) {
743 case CONN_STATE_IDLE:
744 case CONN_STATE_TX:
745 printk(KERN_INFO "%s: Remote dropped connection\n",
746 netdev->name);
747 if (conn->handle)
748 iucv_unregister_program(conn->handle);
749 conn->handle = 0;
750 fsm_newstate(fi, CONN_STATE_STOPPED);
751 fsm_event(privptr->fsm, DEV_EVENT_CONDOWN, netdev);
752 break;
756 static void
757 conn_action_start(fsm_instance *fi, int event, void *arg)
759 struct iucv_event *ev = (struct iucv_event *)arg;
760 struct iucv_connection *conn = ev->conn;
762 int rc;
764 pr_debug("%s() called\n", __FUNCTION__);
766 if (conn->handle == 0) {
767 conn->handle =
768 iucv_register_program(iucvMagic, conn->userid, mask,
769 &netiucv_ops, conn);
770 fsm_newstate(fi, CONN_STATE_STARTWAIT);
771 if (conn->handle <= 0) {
772 fsm_newstate(fi, CONN_STATE_REGERR);
773 conn->handle = 0;
774 return;
777 pr_debug("%s('%s'): registered successfully\n",
778 conn->netdev->name, conn->userid);
781 pr_debug("%s('%s'): connecting ...\n",
782 conn->netdev->name, conn->userid);
784 rc = iucv_connect(&(conn->pathid), NETIUCV_QUEUELEN_DEFAULT, iucvMagic,
785 conn->userid, iucv_host, 0, NULL, NULL, conn->handle,
786 conn);
787 fsm_newstate(fi, CONN_STATE_SETUPWAIT);
788 switch (rc) {
789 case 0:
790 return;
791 case 11:
792 printk(KERN_NOTICE
793 "%s: User %s is currently not available.\n",
794 conn->netdev->name,
795 netiucv_printname(conn->userid));
796 fsm_newstate(fi, CONN_STATE_STARTWAIT);
797 return;
798 case 12:
799 printk(KERN_NOTICE
800 "%s: User %s is currently not ready.\n",
801 conn->netdev->name,
802 netiucv_printname(conn->userid));
803 fsm_newstate(fi, CONN_STATE_STARTWAIT);
804 return;
805 case 13:
806 printk(KERN_WARNING
807 "%s: Too many IUCV connections.\n",
808 conn->netdev->name);
809 fsm_newstate(fi, CONN_STATE_CONNERR);
810 break;
811 case 14:
812 printk(KERN_WARNING
813 "%s: User %s has too many IUCV connections.\n",
814 conn->netdev->name,
815 netiucv_printname(conn->userid));
816 fsm_newstate(fi, CONN_STATE_CONNERR);
817 break;
818 case 15:
819 printk(KERN_WARNING
820 "%s: No IUCV authorization in CP directory.\n",
821 conn->netdev->name);
822 fsm_newstate(fi, CONN_STATE_CONNERR);
823 break;
824 default:
825 printk(KERN_WARNING
826 "%s: iucv_connect returned error %d\n",
827 conn->netdev->name, rc);
828 fsm_newstate(fi, CONN_STATE_CONNERR);
829 break;
831 iucv_unregister_program(conn->handle);
832 conn->handle = 0;
835 static void
836 netiucv_purge_skb_queue(struct sk_buff_head *q)
838 struct sk_buff *skb;
840 while ((skb = skb_dequeue(q))) {
841 atomic_dec(&skb->users);
842 dev_kfree_skb_any(skb);
846 static void
847 conn_action_stop(fsm_instance *fi, int event, void *arg)
849 struct iucv_event *ev = (struct iucv_event *)arg;
850 struct iucv_connection *conn = ev->conn;
851 struct net_device *netdev = conn->netdev;
852 struct netiucv_priv *privptr = (struct netiucv_priv *)netdev->priv;
854 pr_debug("%s() called\n", __FUNCTION__);
856 fsm_newstate(fi, CONN_STATE_STOPPED);
857 netiucv_purge_skb_queue(&conn->collect_queue);
858 if (conn->handle)
859 iucv_unregister_program(conn->handle);
860 conn->handle = 0;
861 fsm_event(privptr->fsm, DEV_EVENT_CONDOWN, netdev);
864 static void
865 conn_action_inval(fsm_instance *fi, int event, void *arg)
867 struct iucv_event *ev = (struct iucv_event *)arg;
868 struct iucv_connection *conn = ev->conn;
869 struct net_device *netdev = conn->netdev;
871 printk(KERN_WARNING
872 "%s: Cannot connect without username\n",
873 netdev->name);
876 static const fsm_node conn_fsm[] = {
877 { CONN_STATE_INVALID, CONN_EVENT_START, conn_action_inval },
878 { CONN_STATE_STOPPED, CONN_EVENT_START, conn_action_start },
879 { CONN_STATE_STARTWAIT, CONN_EVENT_START, conn_action_start },
881 { CONN_STATE_STARTWAIT, CONN_EVENT_STOP, conn_action_stop },
882 { CONN_STATE_SETUPWAIT, CONN_EVENT_STOP, conn_action_stop },
883 { CONN_STATE_IDLE, CONN_EVENT_STOP, conn_action_stop },
884 { CONN_STATE_TX, CONN_EVENT_STOP, conn_action_stop },
885 { CONN_STATE_REGERR, CONN_EVENT_STOP, conn_action_stop },
886 { CONN_STATE_CONNERR, CONN_EVENT_STOP, conn_action_stop },
888 { CONN_STATE_STOPPED, CONN_EVENT_CONN_REQ, conn_action_connreject },
889 { CONN_STATE_STARTWAIT, CONN_EVENT_CONN_REQ, conn_action_connaccept },
890 { CONN_STATE_SETUPWAIT, CONN_EVENT_CONN_REQ, conn_action_connaccept },
891 { CONN_STATE_IDLE, CONN_EVENT_CONN_REQ, conn_action_connreject },
892 { CONN_STATE_TX, CONN_EVENT_CONN_REQ, conn_action_connreject },
894 { CONN_STATE_SETUPWAIT, CONN_EVENT_CONN_ACK, conn_action_connack },
896 { CONN_STATE_SETUPWAIT, CONN_EVENT_CONN_REJ, conn_action_connsever },
897 { CONN_STATE_IDLE, CONN_EVENT_CONN_REJ, conn_action_connsever },
898 { CONN_STATE_TX, CONN_EVENT_CONN_REJ, conn_action_connsever },
900 { CONN_STATE_IDLE, CONN_EVENT_RX, conn_action_rx },
901 { CONN_STATE_TX, CONN_EVENT_RX, conn_action_rx },
903 { CONN_STATE_TX, CONN_EVENT_TXDONE, conn_action_txdone },
906 static const int CONN_FSM_LEN = sizeof(conn_fsm) / sizeof(fsm_node);
910 * Actions for interface - statemachine.
911 *****************************************************************************/
914 * Startup connection by sending CONN_EVENT_START to it.
916 * @param fi An instance of an interface statemachine.
917 * @param event The event, just happened.
918 * @param arg Generic pointer, casted from struct net_device * upon call.
920 static void
921 dev_action_start(fsm_instance *fi, int event, void *arg)
923 struct net_device *dev = (struct net_device *)arg;
924 struct netiucv_priv *privptr = dev->priv;
925 struct iucv_event ev;
927 pr_debug("%s() called\n", __FUNCTION__);
929 ev.conn = privptr->conn;
930 fsm_newstate(fi, DEV_STATE_STARTWAIT);
931 fsm_event(privptr->conn->fsm, CONN_EVENT_START, &ev);
935 * Shutdown connection by sending CONN_EVENT_STOP to it.
937 * @param fi An instance of an interface statemachine.
938 * @param event The event, just happened.
939 * @param arg Generic pointer, casted from struct net_device * upon call.
941 static void
942 dev_action_stop(fsm_instance *fi, int event, void *arg)
944 struct net_device *dev = (struct net_device *)arg;
945 struct netiucv_priv *privptr = dev->priv;
946 struct iucv_event ev;
948 pr_debug("%s() called\n", __FUNCTION__);
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 struct net_device * upon call.
964 static void
965 dev_action_connup(fsm_instance *fi, int event, void *arg)
967 struct net_device *dev = (struct net_device *)arg;
969 pr_debug("%s() called\n", __FUNCTION__);
971 switch (fsm_getstate(fi)) {
972 case DEV_STATE_STARTWAIT:
973 fsm_newstate(fi, DEV_STATE_RUNNING);
974 printk(KERN_INFO
975 "%s: connected with remote side\n",
976 dev->name);
977 break;
978 case DEV_STATE_STOPWAIT:
979 printk(KERN_INFO
980 "%s: got connection UP event during shutdown!!\n",
981 dev->name);
982 break;
987 * Called from connection statemachine
988 * when a connection has been shutdown.
990 * @param fi An instance of an interface statemachine.
991 * @param event The event, just happened.
992 * @param arg Generic pointer, casted from struct net_device * upon call.
994 static void
995 dev_action_conndown(fsm_instance *fi, int event, void *arg)
997 struct net_device *dev = (struct net_device *)arg;
998 struct netiucv_priv *privptr = dev->priv;
999 struct iucv_event ev;
1001 pr_debug("%s() called\n", __FUNCTION__);
1003 switch (fsm_getstate(fi)) {
1004 case DEV_STATE_RUNNING:
1005 fsm_newstate(fi, DEV_STATE_STARTWAIT);
1006 ev.conn = privptr->conn;
1007 fsm_event(privptr->conn->fsm, CONN_EVENT_START, &ev);
1008 break;
1009 case DEV_STATE_STARTWAIT:
1010 break;
1011 case DEV_STATE_STOPWAIT:
1012 fsm_newstate(fi, DEV_STATE_STOPPED);
1013 break;
1017 static const fsm_node dev_fsm[] = {
1018 { DEV_STATE_STOPPED, DEV_EVENT_START, dev_action_start },
1020 { DEV_STATE_STOPWAIT, DEV_EVENT_START, dev_action_start },
1021 { DEV_STATE_STOPWAIT, DEV_EVENT_CONDOWN, dev_action_conndown },
1023 { DEV_STATE_STARTWAIT, DEV_EVENT_STOP, dev_action_stop },
1024 { DEV_STATE_STARTWAIT, DEV_EVENT_CONUP, dev_action_connup },
1025 { DEV_STATE_STARTWAIT, DEV_EVENT_CONDOWN, dev_action_conndown },
1027 { DEV_STATE_RUNNING, DEV_EVENT_STOP, dev_action_stop },
1028 { DEV_STATE_RUNNING, DEV_EVENT_CONDOWN, dev_action_conndown },
1029 { DEV_STATE_RUNNING, DEV_EVENT_CONUP, fsm_action_nop },
1032 static const int DEV_FSM_LEN = sizeof(dev_fsm) / sizeof(fsm_node);
1035 * Transmit a packet.
1036 * This is a helper function for netiucv_tx().
1038 * @param conn Connection to be used for sending.
1039 * @param skb Pointer to struct sk_buff of packet to send.
1040 * The linklevel header has already been set up
1041 * by netiucv_tx().
1043 * @return 0 on success, -ERRNO on failure. (Never fails.)
1045 static int
1046 netiucv_transmit_skb(struct iucv_connection *conn, struct sk_buff *skb) {
1047 unsigned long saveflags;
1048 ll_header header;
1049 int rc = 0;
1051 if (fsm_getstate(conn->fsm) != CONN_STATE_IDLE) {
1052 int l = skb->len + NETIUCV_HDRLEN;
1054 spin_lock_irqsave(&conn->collect_lock, saveflags);
1055 if (conn->collect_len + l >
1056 (conn->max_buffsize - NETIUCV_HDRLEN))
1057 rc = -EBUSY;
1058 else {
1059 atomic_inc(&skb->users);
1060 skb_queue_tail(&conn->collect_queue, skb);
1061 conn->collect_len += l;
1063 spin_unlock_irqrestore(&conn->collect_lock, saveflags);
1064 } else {
1065 struct sk_buff *nskb = skb;
1067 * Copy the skb to a new allocated skb in lowmem only if the
1068 * data is located above 2G in memory or tailroom is < 2.
1070 unsigned long hi =
1071 ((unsigned long)(skb->tail + NETIUCV_HDRLEN)) >> 31;
1072 int copied = 0;
1073 if (hi || (skb_tailroom(skb) < 2)) {
1074 nskb = alloc_skb(skb->len + NETIUCV_HDRLEN +
1075 NETIUCV_HDRLEN, GFP_ATOMIC | GFP_DMA);
1076 if (!nskb) {
1077 printk(KERN_WARNING
1078 "%s: Could not allocate tx_skb\n",
1079 conn->netdev->name);
1080 rc = -ENOMEM;
1081 } else {
1082 skb_reserve(nskb, NETIUCV_HDRLEN);
1083 memcpy(skb_put(nskb, skb->len),
1084 skb->data, skb->len);
1086 copied = 1;
1089 * skb now is below 2G and has enough room. Add headers.
1091 header.next = nskb->len + NETIUCV_HDRLEN;
1092 memcpy(skb_push(nskb, NETIUCV_HDRLEN), &header, NETIUCV_HDRLEN);
1093 header.next = 0;
1094 memcpy(skb_put(nskb, NETIUCV_HDRLEN), &header, NETIUCV_HDRLEN);
1096 conn->retry = 0;
1097 fsm_newstate(conn->fsm, CONN_STATE_TX);
1098 fsm_addtimer(&conn->timer, NETIUCV_TIMEOUT_5SEC,
1099 CONN_EVENT_TIMER, conn);
1100 conn->prof.send_stamp = xtime;
1102 rc = iucv_send(conn->pathid, NULL, 0, 0,
1103 /* Shut up, gcc! nskb is always below 2G. */
1104 (__u32)(((unsigned long)nskb)&0xffffffff), 0,
1105 nskb->data, nskb->len);
1106 conn->prof.doios_single++;
1107 conn->prof.txlen += skb->len;
1108 if (rc != 0) {
1109 fsm_deltimer(&conn->timer);
1110 if (copied)
1111 dev_kfree_skb(nskb);
1112 else {
1114 * Remove our headers. They get added
1115 * again on retransmit.
1117 skb_pull(skb, NETIUCV_HDRLEN);
1118 skb_trim(skb, skb->len - NETIUCV_HDRLEN);
1120 } else {
1121 if (copied)
1122 dev_kfree_skb(skb);
1126 return rc;
1130 * Interface API for upper network layers
1131 *****************************************************************************/
1134 * Open an interface.
1135 * Called from generic network layer when ifconfig up is run.
1137 * @param dev Pointer to interface struct.
1139 * @return 0 on success, -ERRNO on failure. (Never fails.)
1141 static int
1142 netiucv_open(struct net_device *dev) {
1143 SET_DEVICE_START(dev, 1);
1144 fsm_event(((struct netiucv_priv *)dev->priv)->fsm, DEV_EVENT_START, dev);
1145 return 0;
1149 * Close an interface.
1150 * Called from generic network layer when ifconfig down is run.
1152 * @param dev Pointer to interface struct.
1154 * @return 0 on success, -ERRNO on failure. (Never fails.)
1156 static int
1157 netiucv_close(struct net_device *dev) {
1158 SET_DEVICE_START(dev, 0);
1159 fsm_event(((struct netiucv_priv *)dev->priv)->fsm, DEV_EVENT_STOP, dev);
1160 return 0;
1164 * Start transmission of a packet.
1165 * Called from generic network device layer.
1167 * @param skb Pointer to buffer containing the packet.
1168 * @param dev Pointer to interface struct.
1170 * @return 0 if packet consumed, !0 if packet rejected.
1171 * Note: If we return !0, then the packet is free'd by
1172 * the generic network layer.
1174 static int netiucv_tx(struct sk_buff *skb, struct net_device *dev)
1176 int rc = 0;
1177 struct netiucv_priv *privptr = (struct netiucv_priv *)dev->priv;
1180 * Some sanity checks ...
1182 if (skb == NULL) {
1183 printk(KERN_WARNING "%s: NULL sk_buff passed\n", dev->name);
1184 privptr->stats.tx_dropped++;
1185 return 0;
1187 if (skb_headroom(skb) < (NETIUCV_HDRLEN)) {
1188 printk(KERN_WARNING
1189 "%s: Got sk_buff with head room < %ld bytes\n",
1190 dev->name, NETIUCV_HDRLEN);
1191 dev_kfree_skb(skb);
1192 privptr->stats.tx_dropped++;
1193 return 0;
1197 * If connection is not running, try to restart it
1198 * notify anybody about a link failure and throw
1199 * away packet.
1201 if (fsm_getstate(privptr->fsm) != DEV_STATE_RUNNING) {
1202 fsm_event(privptr->fsm, DEV_EVENT_START, dev);
1203 dst_link_failure(skb);
1204 dev_kfree_skb(skb);
1205 privptr->stats.tx_dropped++;
1206 privptr->stats.tx_errors++;
1207 privptr->stats.tx_carrier_errors++;
1208 return 0;
1211 if (netiucv_test_and_set_busy(dev))
1212 return -EBUSY;
1214 dev->trans_start = jiffies;
1215 if (netiucv_transmit_skb(privptr->conn, skb) != 0)
1216 rc = 1;
1217 netiucv_clear_busy(dev);
1218 return rc;
1222 * Returns interface statistics of a device.
1224 * @param dev Pointer to interface struct.
1226 * @return Pointer to stats struct of this interface.
1228 static struct net_device_stats *
1229 netiucv_stats (struct net_device * dev)
1231 return &((struct netiucv_priv *)dev->priv)->stats;
1235 * Sets MTU of an interface.
1237 * @param dev Pointer to interface struct.
1238 * @param new_mtu The new MTU to use for this interface.
1240 * @return 0 on success, -EINVAL if MTU is out of valid range.
1241 * (valid range is 576 .. NETIUCV_MTU_MAX).
1243 static int
1244 netiucv_change_mtu (struct net_device * dev, int new_mtu)
1246 if ((new_mtu < 576) || (new_mtu > NETIUCV_MTU_MAX))
1247 return -EINVAL;
1248 dev->mtu = new_mtu;
1249 return 0;
1253 * attributes in sysfs
1254 *****************************************************************************/
1255 #define CTRL_BUFSIZE 40
1257 static ssize_t
1258 buffer_show (struct device *dev, char *buf)
1260 struct netiucv_priv *priv = dev->driver_data;
1262 return sprintf(buf, "%d\n", priv->conn->max_buffsize);
1265 static ssize_t
1266 buffer_write (struct device *dev, const char *buf, size_t count)
1268 struct netiucv_priv *priv = dev->driver_data;
1269 struct net_device *ndev =
1270 container_of((void *)priv, struct net_device, priv);
1271 char *e;
1272 int bs1;
1273 char tmp[CTRL_BUFSIZE];
1275 if (count >= 39)
1276 return -EINVAL;
1278 if (copy_from_user(tmp, buf, count))
1279 return -EFAULT;
1280 tmp[count+1] = '\0';
1281 bs1 = simple_strtoul(tmp, &e, 0);
1283 if ((bs1 > NETIUCV_BUFSIZE_MAX) ||
1284 (e && (!isspace(*e))))
1285 return -EINVAL;
1286 if ((ndev->flags & IFF_RUNNING) &&
1287 (bs1 < (ndev->mtu + NETIUCV_HDRLEN + 2)))
1288 return -EINVAL;
1289 if (bs1 < (576 + NETIUCV_HDRLEN + NETIUCV_HDRLEN))
1290 return -EINVAL;
1293 priv->conn->max_buffsize = bs1;
1294 if (!(ndev->flags & IFF_RUNNING))
1295 ndev->mtu = bs1 - NETIUCV_HDRLEN - NETIUCV_HDRLEN;
1296 priv->conn->flags |= CONN_FLAGS_BUFSIZE_CHANGED;
1298 return count;
1302 static DEVICE_ATTR(buffer, 0644, buffer_show, buffer_write);
1304 static ssize_t
1305 dev_fsm_show (struct device *dev, char *buf)
1307 struct netiucv_priv *priv = dev->driver_data;
1309 return sprintf(buf, "%s\n", fsm_getstate_str(priv->fsm));
1312 static DEVICE_ATTR(device_fsm_state, 0444, dev_fsm_show, NULL);
1314 static ssize_t
1315 conn_fsm_show (struct device *dev, char *buf)
1317 struct netiucv_priv *priv = dev->driver_data;
1319 return sprintf(buf, "%s\n", fsm_getstate_str(priv->conn->fsm));
1322 static DEVICE_ATTR(connection_fsm_state, 0444, conn_fsm_show, NULL);
1324 static ssize_t
1325 maxmulti_show (struct device *dev, char *buf)
1327 struct netiucv_priv *priv = dev->driver_data;
1329 return sprintf(buf, "%ld\n", priv->conn->prof.maxmulti);
1332 static ssize_t
1333 maxmulti_write (struct device *dev, const char *buf, size_t count)
1335 struct netiucv_priv *priv = dev->driver_data;
1337 priv->conn->prof.maxmulti = 0;
1338 return count;
1341 static DEVICE_ATTR(max_tx_buffer_used, 0644, maxmulti_show, maxmulti_write);
1343 static ssize_t
1344 maxcq_show (struct device *dev, char *buf)
1346 struct netiucv_priv *priv = dev->driver_data;
1348 return sprintf(buf, "%ld\n", priv->conn->prof.maxcqueue);
1351 static ssize_t
1352 maxcq_write (struct device *dev, const char *buf, size_t count)
1354 struct netiucv_priv *priv = dev->driver_data;
1356 priv->conn->prof.maxcqueue = 0;
1357 return count;
1360 static DEVICE_ATTR(max_chained_skbs, 0644, maxcq_show, maxcq_write);
1362 static ssize_t
1363 sdoio_show (struct device *dev, char *buf)
1365 struct netiucv_priv *priv = dev->driver_data;
1367 return sprintf(buf, "%ld\n", priv->conn->prof.doios_single);
1370 static ssize_t
1371 sdoio_write (struct device *dev, const char *buf, size_t count)
1373 struct netiucv_priv *priv = dev->driver_data;
1375 priv->conn->prof.doios_single = 0;
1376 return count;
1379 static DEVICE_ATTR(tx_single_write_ops, 0644, sdoio_show, sdoio_write);
1381 static ssize_t
1382 mdoio_show (struct device *dev, char *buf)
1384 struct netiucv_priv *priv = dev->driver_data;
1386 return sprintf(buf, "%ld\n", priv->conn->prof.doios_multi);
1389 static ssize_t
1390 mdoio_write (struct device *dev, const char *buf, size_t count)
1392 struct netiucv_priv *priv = dev->driver_data;
1394 priv->conn->prof.doios_multi = 0;
1395 return count;
1398 static DEVICE_ATTR(tx_multi_write_ops, 0644, mdoio_show, mdoio_write);
1400 static ssize_t
1401 txlen_show (struct device *dev, char *buf)
1403 struct netiucv_priv *priv = dev->driver_data;
1405 return sprintf(buf, "%ld\n", priv->conn->prof.txlen);
1408 static ssize_t
1409 txlen_write (struct device *dev, const char *buf, size_t count)
1411 struct netiucv_priv *priv = dev->driver_data;
1413 priv->conn->prof.txlen = 0;
1414 return count;
1417 static DEVICE_ATTR(netto_bytes, 0644, txlen_show, txlen_write);
1419 static ssize_t
1420 txtime_show (struct device *dev, char *buf)
1422 struct netiucv_priv *priv = dev->driver_data;
1424 return sprintf(buf, "%ld\n", priv->conn->prof.tx_time);
1427 static ssize_t
1428 txtime_write (struct device *dev, const char *buf, size_t count)
1430 struct netiucv_priv *priv = dev->driver_data;
1432 priv->conn->prof.tx_time = 0;
1433 return count;
1436 static DEVICE_ATTR(max_tx_io_time, 0644, txtime_show, txtime_write);
1438 static inline int
1439 netiucv_add_files(struct device *dev)
1441 int ret = 0;
1443 if ((ret = device_create_file(dev, &dev_attr_buffer)) ||
1444 (ret = device_create_file(dev, &dev_attr_device_fsm_state)) ||
1445 (ret = device_create_file(dev, &dev_attr_connection_fsm_state)) ||
1446 (ret = device_create_file(dev, &dev_attr_max_tx_buffer_used)) ||
1447 (ret = device_create_file(dev, &dev_attr_max_chained_skbs)) ||
1448 (ret = device_create_file(dev, &dev_attr_tx_single_write_ops)) ||
1449 (ret = device_create_file(dev, &dev_attr_tx_multi_write_ops)) ||
1450 (ret = device_create_file(dev, &dev_attr_netto_bytes)) ||
1451 (ret = device_create_file(dev, &dev_attr_max_tx_io_time))) {
1452 device_remove_file(dev, &dev_attr_netto_bytes);
1453 device_remove_file(dev, &dev_attr_tx_multi_write_ops);
1454 device_remove_file(dev, &dev_attr_tx_single_write_ops);
1455 device_remove_file(dev, &dev_attr_max_chained_skbs);
1456 device_remove_file(dev, &dev_attr_max_tx_buffer_used);
1457 device_remove_file(dev, &dev_attr_connection_fsm_state);
1458 device_remove_file(dev, &dev_attr_device_fsm_state);
1459 device_remove_file(dev, &dev_attr_buffer);
1461 return ret;
1464 static int
1465 netiucv_register_device(struct net_device *ndev, int ifno)
1467 struct netiucv_priv *priv = ndev->priv;
1468 struct device *dev = &priv->dev;
1469 int ret;
1470 char *str = "netiucv";
1472 snprintf(dev->name, DEVICE_NAME_SIZE, "%s", priv->conn->userid);
1473 snprintf(dev->bus_id, BUS_ID_SIZE, "%s%x", str, ifno);
1474 dev->bus = &iucv_bus;
1475 dev->parent = &iucv_root;
1477 ret = device_register(dev);
1479 if (ret)
1480 return ret;
1482 ret = netiucv_add_files(dev);
1484 if (ret)
1485 device_unregister(dev);
1486 else
1487 dev->driver_data = priv;
1488 return ret;
1491 static void
1492 netiucv_unregister_device(struct net_device *ndev)
1494 struct netiucv_priv *priv = (struct netiucv_priv*)ndev->priv;
1495 struct device *dev = &priv->dev;
1497 device_unregister(dev);
1501 * Allocate and initialize a new connection structure.
1502 * Add it to the list of connections;
1504 static struct iucv_connection *
1505 netiucv_new_connection(struct net_device *dev, char *username)
1507 struct iucv_connection **clist = &connections;
1508 struct iucv_connection *conn =
1509 (struct iucv_connection *)
1510 kmalloc(sizeof(struct iucv_connection), GFP_KERNEL);
1512 if (conn) {
1513 memset(conn, 0, sizeof(struct iucv_connection));
1514 skb_queue_head_init(&conn->collect_queue);
1515 conn->max_buffsize = NETIUCV_BUFSIZE_DEFAULT;
1516 conn->netdev = dev;
1518 conn->rx_buff = alloc_skb(NETIUCV_BUFSIZE_DEFAULT,
1519 GFP_KERNEL | GFP_DMA);
1520 if (!conn->rx_buff) {
1521 kfree(conn);
1522 return NULL;
1524 conn->tx_buff = alloc_skb(NETIUCV_BUFSIZE_DEFAULT,
1525 GFP_KERNEL | GFP_DMA);
1526 if (!conn->tx_buff) {
1527 kfree_skb(conn->rx_buff);
1528 kfree(conn);
1529 return NULL;
1531 conn->fsm = init_fsm("netiucvconn", conn_state_names,
1532 conn_event_names, NR_CONN_STATES,
1533 NR_CONN_EVENTS, conn_fsm, CONN_FSM_LEN,
1534 GFP_KERNEL);
1535 if (!conn->fsm) {
1536 kfree_skb(conn->tx_buff);
1537 kfree_skb(conn->rx_buff);
1538 kfree(conn);
1539 return NULL;
1541 fsm_settimer(conn->fsm, &conn->timer);
1542 fsm_newstate(conn->fsm, CONN_STATE_INVALID);
1544 if (username) {
1545 memcpy(conn->userid, username, 9);
1546 fsm_newstate(conn->fsm, CONN_STATE_STOPPED);
1549 conn->next = *clist;
1550 *clist = conn;
1552 return conn;
1556 * Release a connection structure and remove it from the
1557 * list of connections.
1559 static void
1560 netiucv_remove_connection(struct iucv_connection *conn)
1562 struct iucv_connection **clist = &connections;
1564 if (conn == NULL)
1565 return;
1566 while (*clist) {
1567 if (*clist == conn) {
1568 *clist = conn->next;
1569 if (conn->handle != 0) {
1570 iucv_unregister_program(conn->handle);
1571 conn->handle = 0;
1573 fsm_deltimer(&conn->timer);
1574 kfree_fsm(conn->fsm);
1575 kfree_skb(conn->rx_buff);
1576 kfree_skb(conn->tx_buff);
1577 return;
1579 clist = &((*clist)->next);
1584 * Allocate and initialize everything of a net device.
1586 static struct net_device *
1587 netiucv_init_netdevice(int ifno, char *username)
1589 struct netiucv_priv *privptr;
1590 int priv_size;
1592 struct net_device *dev = kmalloc(sizeof(struct net_device), GFP_KERNEL);
1593 if (!dev)
1594 return NULL;
1595 memset(dev, 0, sizeof(struct net_device));
1596 sprintf(dev->name, "iucv%d", ifno);
1598 priv_size = sizeof(struct netiucv_priv);
1599 dev->priv = kmalloc(priv_size, GFP_KERNEL);
1600 if (dev->priv == NULL) {
1601 kfree(dev);
1602 return NULL;
1604 memset(dev->priv, 0, priv_size);
1605 privptr = (struct netiucv_priv *)dev->priv;
1606 privptr->fsm = init_fsm("netiucvdev", dev_state_names,
1607 dev_event_names, NR_DEV_STATES, NR_DEV_EVENTS,
1608 dev_fsm, DEV_FSM_LEN, GFP_KERNEL);
1609 if (privptr->fsm == NULL) {
1610 kfree(privptr);
1611 kfree(dev);
1612 return NULL;
1614 privptr->conn = netiucv_new_connection(dev, username);
1615 if (!privptr->conn) {
1616 kfree_fsm(privptr->fsm);
1617 kfree(privptr);
1618 kfree(dev);
1619 return NULL;
1622 fsm_newstate(privptr->fsm, DEV_STATE_STOPPED);
1623 dev->mtu = NETIUCV_MTU_DEFAULT;
1624 dev->hard_start_xmit = netiucv_tx;
1625 dev->open = netiucv_open;
1626 dev->stop = netiucv_close;
1627 dev->get_stats = netiucv_stats;
1628 dev->change_mtu = netiucv_change_mtu;
1629 dev->hard_header_len = NETIUCV_HDRLEN;
1630 dev->addr_len = 0;
1631 dev->type = ARPHRD_SLIP;
1632 dev->tx_queue_len = NETIUCV_QUEUELEN_DEFAULT;
1633 dev->flags = IFF_POINTOPOINT | IFF_NOARP;
1634 SET_MODULE_OWNER(dev);
1635 return dev;
1639 * Allocate and initialize everything of a net device.
1641 static void
1642 netiucv_free_netdevice(struct net_device *dev)
1644 struct netiucv_priv *privptr;
1646 if (!dev)
1647 return;
1649 privptr = (struct netiucv_priv *)dev->priv;
1650 if (privptr) {
1651 if (privptr->conn)
1652 netiucv_remove_connection(privptr->conn);
1653 if (privptr->fsm)
1654 kfree_fsm(privptr->fsm);
1655 kfree(privptr);
1657 kfree(dev);
1660 static ssize_t
1661 conn_write(struct device_driver *drv, const char *buf, size_t count)
1663 char *p;
1664 char username[10];
1665 int i;
1666 struct net_device *dev;
1668 if (count>9) {
1669 printk(KERN_WARNING
1670 "netiucv: username too long (%d)!\n", (int)count);
1671 return -EINVAL;
1674 for (i=0, p=(char *)buf; i<8 && *p; i++, p++) {
1675 if (isalnum(*p))
1676 username[i]= *p;
1677 else if (*p == '\n') {
1678 /* trailing lf, grr */
1679 break;
1680 } else {
1681 printk(KERN_WARNING
1682 "netiucv: Invalid character in username!\n");
1683 return -EINVAL;
1686 while (i<9)
1687 username[i++] = ' ';
1688 username[9] = '\0';
1689 dev = netiucv_init_netdevice(ifno, username);
1690 if (!dev) {
1691 printk(KERN_WARNING
1692 "netiucv: Could not allocate network device structure "
1693 "for user '%s'\n", netiucv_printname(username));
1694 return -ENODEV;
1697 if (register_netdev(dev)) {
1698 printk(KERN_WARNING
1699 "netiucv: Could not register '%s'\n", dev->name);
1700 netiucv_free_netdevice(dev);
1701 return -ENODEV;
1703 printk(KERN_INFO "%s: '%s'\n", dev->name, netiucv_printname(username));
1704 netiucv_register_device(dev, ifno);
1705 ifno++;
1707 return count;
1710 DRIVER_ATTR(connection, 0200, NULL, conn_write);
1712 static struct device_driver netiucv_driver = {
1713 .name = "NETIUCV",
1714 .bus = &iucv_bus,
1717 static void
1718 netiucv_banner(void)
1720 char vbuf[] = "$Revision: 1.20 $";
1721 char *version = vbuf;
1723 if ((version = strchr(version, ':'))) {
1724 char *p = strchr(version + 1, '$');
1725 if (p)
1726 *p = '\0';
1727 } else
1728 version = " ??? ";
1729 printk(KERN_INFO "NETIUCV driver Version%s initialized\n", version);
1732 static void __exit
1733 netiucv_exit(void)
1735 while (connections) {
1736 struct net_device *dev = connections->netdev;
1737 unregister_netdev(dev);
1738 netiucv_unregister_device(dev);
1739 netiucv_free_netdevice(dev);
1742 driver_remove_file(&netiucv_driver, &driver_attr_connection);
1743 driver_unregister(&netiucv_driver);
1744 bus_unregister(&iucv_bus);
1746 printk(KERN_INFO "NETIUCV driver unloaded\n");
1747 return;
1750 static int __init
1751 netiucv_init(void)
1753 int ret;
1755 /* Move the bus stuff to iucv.c? - CH */
1756 ret = bus_register(&iucv_bus);
1757 if (ret != 0) {
1758 printk(KERN_ERR "NETIUCV: failed to register bus.\n");
1759 return ret;
1762 ret = driver_register(&netiucv_driver);
1763 if (ret != 0) {
1764 printk(KERN_ERR "NETIUCV: failed to register driver.\n");
1765 bus_unregister(&iucv_bus);
1766 return ret;
1769 ret = device_register(&iucv_root);
1770 if (ret != 0) {
1771 printk(KERN_ERR "NETIUCV: failed to register iucv root.\n");
1772 driver_unregister(&netiucv_driver);
1773 bus_unregister(&iucv_bus);
1774 return ret;
1777 /* Add entry for specifying connections. */
1778 ret = driver_create_file(&netiucv_driver, &driver_attr_connection);
1780 if (ret == 0)
1781 netiucv_banner();
1782 else {
1783 printk(KERN_ERR "NETIUCV: failed to add driver attribute.\n");
1784 device_unregister(&iucv_root);
1785 driver_unregister(&netiucv_driver);
1786 bus_unregister(&iucv_bus);
1788 return ret;
1791 module_init(netiucv_init);
1792 module_exit(netiucv_exit);
1793 MODULE_LICENSE("GPL");