Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * IUCV network driver
0004  *
0005  * Copyright IBM Corp. 2001, 2009
0006  *
0007  * Author(s):
0008  *  Original netiucv driver:
0009  *      Fritz Elfert (elfert@de.ibm.com, felfert@millenux.com)
0010  *  Sysfs integration and all bugs therein:
0011  *      Cornelia Huck (cornelia.huck@de.ibm.com)
0012  *  PM functions:
0013  *      Ursula Braun (ursula.braun@de.ibm.com)
0014  *
0015  * Documentation used:
0016  *  the source of the original IUCV driver by:
0017  *    Stefan Hegewald <hegewald@de.ibm.com>
0018  *    Hartmut Penner <hpenner@de.ibm.com>
0019  *    Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
0020  *    Martin Schwidefsky (schwidefsky@de.ibm.com)
0021  *    Alan Altmark (Alan_Altmark@us.ibm.com)  Sept. 2000
0022  */
0023 
0024 #define KMSG_COMPONENT "netiucv"
0025 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
0026 
0027 #undef DEBUG
0028 
0029 #include <linux/module.h>
0030 #include <linux/init.h>
0031 #include <linux/kernel.h>
0032 #include <linux/slab.h>
0033 #include <linux/errno.h>
0034 #include <linux/types.h>
0035 #include <linux/interrupt.h>
0036 #include <linux/timer.h>
0037 #include <linux/bitops.h>
0038 
0039 #include <linux/signal.h>
0040 #include <linux/string.h>
0041 #include <linux/device.h>
0042 
0043 #include <linux/ip.h>
0044 #include <linux/if_arp.h>
0045 #include <linux/tcp.h>
0046 #include <linux/skbuff.h>
0047 #include <linux/ctype.h>
0048 #include <net/dst.h>
0049 
0050 #include <asm/io.h>
0051 #include <linux/uaccess.h>
0052 #include <asm/ebcdic.h>
0053 
0054 #include <net/iucv/iucv.h>
0055 #include "fsm.h"
0056 
0057 MODULE_AUTHOR
0058     ("(C) 2001 IBM Corporation by Fritz Elfert (felfert@millenux.com)");
0059 MODULE_DESCRIPTION ("Linux for S/390 IUCV network driver");
0060 
0061 /*
0062  * Debug Facility stuff
0063  */
0064 #define IUCV_DBF_SETUP_NAME "iucv_setup"
0065 #define IUCV_DBF_SETUP_LEN 64
0066 #define IUCV_DBF_SETUP_PAGES 2
0067 #define IUCV_DBF_SETUP_NR_AREAS 1
0068 #define IUCV_DBF_SETUP_LEVEL 3
0069 
0070 #define IUCV_DBF_DATA_NAME "iucv_data"
0071 #define IUCV_DBF_DATA_LEN 128
0072 #define IUCV_DBF_DATA_PAGES 2
0073 #define IUCV_DBF_DATA_NR_AREAS 1
0074 #define IUCV_DBF_DATA_LEVEL 2
0075 
0076 #define IUCV_DBF_TRACE_NAME "iucv_trace"
0077 #define IUCV_DBF_TRACE_LEN 16
0078 #define IUCV_DBF_TRACE_PAGES 4
0079 #define IUCV_DBF_TRACE_NR_AREAS 1
0080 #define IUCV_DBF_TRACE_LEVEL 3
0081 
0082 #define IUCV_DBF_TEXT(name,level,text) \
0083     do { \
0084         debug_text_event(iucv_dbf_##name,level,text); \
0085     } while (0)
0086 
0087 #define IUCV_DBF_HEX(name,level,addr,len) \
0088     do { \
0089         debug_event(iucv_dbf_##name,level,(void*)(addr),len); \
0090     } while (0)
0091 
0092 DECLARE_PER_CPU(char[256], iucv_dbf_txt_buf);
0093 
0094 #define IUCV_DBF_TEXT_(name, level, text...) \
0095     do { \
0096         if (debug_level_enabled(iucv_dbf_##name, level)) { \
0097             char* __buf = get_cpu_var(iucv_dbf_txt_buf); \
0098             sprintf(__buf, text); \
0099             debug_text_event(iucv_dbf_##name, level, __buf); \
0100             put_cpu_var(iucv_dbf_txt_buf); \
0101         } \
0102     } while (0)
0103 
0104 #define IUCV_DBF_SPRINTF(name,level,text...) \
0105     do { \
0106         debug_sprintf_event(iucv_dbf_trace, level, ##text ); \
0107         debug_sprintf_event(iucv_dbf_trace, level, text ); \
0108     } while (0)
0109 
0110 /*
0111  * some more debug stuff
0112  */
0113 #define PRINTK_HEADER " iucv: "       /* for debugging */
0114 
0115 static struct device_driver netiucv_driver = {
0116     .owner = THIS_MODULE,
0117     .name = "netiucv",
0118     .bus  = &iucv_bus,
0119 };
0120 
0121 /*
0122  * Per connection profiling data
0123  */
0124 struct connection_profile {
0125     unsigned long maxmulti;
0126     unsigned long maxcqueue;
0127     unsigned long doios_single;
0128     unsigned long doios_multi;
0129     unsigned long txlen;
0130     unsigned long tx_time;
0131     unsigned long send_stamp;
0132     unsigned long tx_pending;
0133     unsigned long tx_max_pending;
0134 };
0135 
0136 /*
0137  * Representation of one iucv connection
0138  */
0139 struct iucv_connection {
0140     struct list_head      list;
0141     struct iucv_path      *path;
0142     struct sk_buff            *rx_buff;
0143     struct sk_buff            *tx_buff;
0144     struct sk_buff_head       collect_queue;
0145     struct sk_buff_head   commit_queue;
0146     spinlock_t                collect_lock;
0147     int                       collect_len;
0148     int                       max_buffsize;
0149     fsm_timer                 timer;
0150     fsm_instance              *fsm;
0151     struct net_device         *netdev;
0152     struct connection_profile prof;
0153     char                      userid[9];
0154     char              userdata[17];
0155 };
0156 
0157 /*
0158  * Linked list of all connection structs.
0159  */
0160 static LIST_HEAD(iucv_connection_list);
0161 static DEFINE_RWLOCK(iucv_connection_rwlock);
0162 
0163 /*
0164  * Representation of event-data for the
0165  * connection state machine.
0166  */
0167 struct iucv_event {
0168     struct iucv_connection *conn;
0169     void                   *data;
0170 };
0171 
0172 /*
0173  * Private part of the network device structure
0174  */
0175 struct netiucv_priv {
0176     struct net_device_stats stats;
0177     unsigned long           tbusy;
0178     fsm_instance            *fsm;
0179         struct iucv_connection  *conn;
0180     struct device           *dev;
0181 };
0182 
0183 /*
0184  * Link level header for a packet.
0185  */
0186 struct ll_header {
0187     u16 next;
0188 };
0189 
0190 #define NETIUCV_HDRLEN       (sizeof(struct ll_header))
0191 #define NETIUCV_BUFSIZE_MAX  65537
0192 #define NETIUCV_BUFSIZE_DEFAULT  NETIUCV_BUFSIZE_MAX
0193 #define NETIUCV_MTU_MAX          (NETIUCV_BUFSIZE_MAX - NETIUCV_HDRLEN)
0194 #define NETIUCV_MTU_DEFAULT      9216
0195 #define NETIUCV_QUEUELEN_DEFAULT 50
0196 #define NETIUCV_TIMEOUT_5SEC     5000
0197 
0198 /*
0199  * Compatibility macros for busy handling
0200  * of network devices.
0201  */
0202 static void netiucv_clear_busy(struct net_device *dev)
0203 {
0204     struct netiucv_priv *priv = netdev_priv(dev);
0205     clear_bit(0, &priv->tbusy);
0206     netif_wake_queue(dev);
0207 }
0208 
0209 static int netiucv_test_and_set_busy(struct net_device *dev)
0210 {
0211     struct netiucv_priv *priv = netdev_priv(dev);
0212     netif_stop_queue(dev);
0213     return test_and_set_bit(0, &priv->tbusy);
0214 }
0215 
0216 static u8 iucvMagic_ascii[16] = {
0217     0x30, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0218     0x30, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20
0219 };
0220 
0221 static u8 iucvMagic_ebcdic[16] = {
0222     0xF0, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
0223     0xF0, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40
0224 };
0225 
0226 /*
0227  * Convert an iucv userId to its printable
0228  * form (strip whitespace at end).
0229  *
0230  * @param An iucv userId
0231  *
0232  * @returns The printable string (static data!!)
0233  */
0234 static char *netiucv_printname(char *name, int len)
0235 {
0236     static char tmp[17];
0237     char *p = tmp;
0238     memcpy(tmp, name, len);
0239     tmp[len] = '\0';
0240     while (*p && ((p - tmp) < len) && (!isspace(*p)))
0241         p++;
0242     *p = '\0';
0243     return tmp;
0244 }
0245 
0246 static char *netiucv_printuser(struct iucv_connection *conn)
0247 {
0248     static char tmp_uid[9];
0249     static char tmp_udat[17];
0250     static char buf[100];
0251 
0252     if (memcmp(conn->userdata, iucvMagic_ebcdic, 16)) {
0253         tmp_uid[8] = '\0';
0254         tmp_udat[16] = '\0';
0255         memcpy(tmp_uid, netiucv_printname(conn->userid, 8), 8);
0256         memcpy(tmp_udat, conn->userdata, 16);
0257         EBCASC(tmp_udat, 16);
0258         memcpy(tmp_udat, netiucv_printname(tmp_udat, 16), 16);
0259         sprintf(buf, "%s.%s", tmp_uid, tmp_udat);
0260         return buf;
0261     } else
0262         return netiucv_printname(conn->userid, 8);
0263 }
0264 
0265 /*
0266  * States of the interface statemachine.
0267  */
0268 enum dev_states {
0269     DEV_STATE_STOPPED,
0270     DEV_STATE_STARTWAIT,
0271     DEV_STATE_STOPWAIT,
0272     DEV_STATE_RUNNING,
0273     /*
0274      * MUST be always the last element!!
0275      */
0276     NR_DEV_STATES
0277 };
0278 
0279 static const char *dev_state_names[] = {
0280     "Stopped",
0281     "StartWait",
0282     "StopWait",
0283     "Running",
0284 };
0285 
0286 /*
0287  * Events of the interface statemachine.
0288  */
0289 enum dev_events {
0290     DEV_EVENT_START,
0291     DEV_EVENT_STOP,
0292     DEV_EVENT_CONUP,
0293     DEV_EVENT_CONDOWN,
0294     /*
0295      * MUST be always the last element!!
0296      */
0297     NR_DEV_EVENTS
0298 };
0299 
0300 static const char *dev_event_names[] = {
0301     "Start",
0302     "Stop",
0303     "Connection up",
0304     "Connection down",
0305 };
0306 
0307 /*
0308  * Events of the connection statemachine
0309  */
0310 enum conn_events {
0311     /*
0312      * Events, representing callbacks from
0313      * lowlevel iucv layer)
0314      */
0315     CONN_EVENT_CONN_REQ,
0316     CONN_EVENT_CONN_ACK,
0317     CONN_EVENT_CONN_REJ,
0318     CONN_EVENT_CONN_SUS,
0319     CONN_EVENT_CONN_RES,
0320     CONN_EVENT_RX,
0321     CONN_EVENT_TXDONE,
0322 
0323     /*
0324      * Events, representing errors return codes from
0325      * calls to lowlevel iucv layer
0326      */
0327 
0328     /*
0329      * Event, representing timer expiry.
0330      */
0331     CONN_EVENT_TIMER,
0332 
0333     /*
0334      * Events, representing commands from upper levels.
0335      */
0336     CONN_EVENT_START,
0337     CONN_EVENT_STOP,
0338 
0339     /*
0340      * MUST be always the last element!!
0341      */
0342     NR_CONN_EVENTS,
0343 };
0344 
0345 static const char *conn_event_names[] = {
0346     "Remote connection request",
0347     "Remote connection acknowledge",
0348     "Remote connection reject",
0349     "Connection suspended",
0350     "Connection resumed",
0351     "Data received",
0352     "Data sent",
0353 
0354     "Timer",
0355 
0356     "Start",
0357     "Stop",
0358 };
0359 
0360 /*
0361  * States of the connection statemachine.
0362  */
0363 enum conn_states {
0364     /*
0365      * Connection not assigned to any device,
0366      * initial state, invalid
0367      */
0368     CONN_STATE_INVALID,
0369 
0370     /*
0371      * Userid assigned but not operating
0372      */
0373     CONN_STATE_STOPPED,
0374 
0375     /*
0376      * Connection registered,
0377      * no connection request sent yet,
0378      * no connection request received
0379      */
0380     CONN_STATE_STARTWAIT,
0381 
0382     /*
0383      * Connection registered and connection request sent,
0384      * no acknowledge and no connection request received yet.
0385      */
0386     CONN_STATE_SETUPWAIT,
0387 
0388     /*
0389      * Connection up and running idle
0390      */
0391     CONN_STATE_IDLE,
0392 
0393     /*
0394      * Data sent, awaiting CONN_EVENT_TXDONE
0395      */
0396     CONN_STATE_TX,
0397 
0398     /*
0399      * Error during registration.
0400      */
0401     CONN_STATE_REGERR,
0402 
0403     /*
0404      * Error during registration.
0405      */
0406     CONN_STATE_CONNERR,
0407 
0408     /*
0409      * MUST be always the last element!!
0410      */
0411     NR_CONN_STATES,
0412 };
0413 
0414 static const char *conn_state_names[] = {
0415     "Invalid",
0416     "Stopped",
0417     "StartWait",
0418     "SetupWait",
0419     "Idle",
0420     "TX",
0421     "Terminating",
0422     "Registration error",
0423     "Connect error",
0424 };
0425 
0426 
0427 /*
0428  * Debug Facility Stuff
0429  */
0430 static debug_info_t *iucv_dbf_setup = NULL;
0431 static debug_info_t *iucv_dbf_data = NULL;
0432 static debug_info_t *iucv_dbf_trace = NULL;
0433 
0434 DEFINE_PER_CPU(char[256], iucv_dbf_txt_buf);
0435 
0436 static void iucv_unregister_dbf_views(void)
0437 {
0438     debug_unregister(iucv_dbf_setup);
0439     debug_unregister(iucv_dbf_data);
0440     debug_unregister(iucv_dbf_trace);
0441 }
0442 static int iucv_register_dbf_views(void)
0443 {
0444     iucv_dbf_setup = debug_register(IUCV_DBF_SETUP_NAME,
0445                     IUCV_DBF_SETUP_PAGES,
0446                     IUCV_DBF_SETUP_NR_AREAS,
0447                     IUCV_DBF_SETUP_LEN);
0448     iucv_dbf_data = debug_register(IUCV_DBF_DATA_NAME,
0449                        IUCV_DBF_DATA_PAGES,
0450                        IUCV_DBF_DATA_NR_AREAS,
0451                        IUCV_DBF_DATA_LEN);
0452     iucv_dbf_trace = debug_register(IUCV_DBF_TRACE_NAME,
0453                     IUCV_DBF_TRACE_PAGES,
0454                     IUCV_DBF_TRACE_NR_AREAS,
0455                     IUCV_DBF_TRACE_LEN);
0456 
0457     if ((iucv_dbf_setup == NULL) || (iucv_dbf_data == NULL) ||
0458         (iucv_dbf_trace == NULL)) {
0459         iucv_unregister_dbf_views();
0460         return -ENOMEM;
0461     }
0462     debug_register_view(iucv_dbf_setup, &debug_hex_ascii_view);
0463     debug_set_level(iucv_dbf_setup, IUCV_DBF_SETUP_LEVEL);
0464 
0465     debug_register_view(iucv_dbf_data, &debug_hex_ascii_view);
0466     debug_set_level(iucv_dbf_data, IUCV_DBF_DATA_LEVEL);
0467 
0468     debug_register_view(iucv_dbf_trace, &debug_hex_ascii_view);
0469     debug_set_level(iucv_dbf_trace, IUCV_DBF_TRACE_LEVEL);
0470 
0471     return 0;
0472 }
0473 
0474 /*
0475  * Callback-wrappers, called from lowlevel iucv layer.
0476  */
0477 
0478 static void netiucv_callback_rx(struct iucv_path *path,
0479                 struct iucv_message *msg)
0480 {
0481     struct iucv_connection *conn = path->private;
0482     struct iucv_event ev;
0483 
0484     ev.conn = conn;
0485     ev.data = msg;
0486     fsm_event(conn->fsm, CONN_EVENT_RX, &ev);
0487 }
0488 
0489 static void netiucv_callback_txdone(struct iucv_path *path,
0490                     struct iucv_message *msg)
0491 {
0492     struct iucv_connection *conn = path->private;
0493     struct iucv_event ev;
0494 
0495     ev.conn = conn;
0496     ev.data = msg;
0497     fsm_event(conn->fsm, CONN_EVENT_TXDONE, &ev);
0498 }
0499 
0500 static void netiucv_callback_connack(struct iucv_path *path, u8 ipuser[16])
0501 {
0502     struct iucv_connection *conn = path->private;
0503 
0504     fsm_event(conn->fsm, CONN_EVENT_CONN_ACK, conn);
0505 }
0506 
0507 static int netiucv_callback_connreq(struct iucv_path *path, u8 *ipvmid,
0508                     u8 *ipuser)
0509 {
0510     struct iucv_connection *conn = path->private;
0511     struct iucv_event ev;
0512     static char tmp_user[9];
0513     static char tmp_udat[17];
0514     int rc;
0515 
0516     rc = -EINVAL;
0517     memcpy(tmp_user, netiucv_printname(ipvmid, 8), 8);
0518     memcpy(tmp_udat, ipuser, 16);
0519     EBCASC(tmp_udat, 16);
0520     read_lock_bh(&iucv_connection_rwlock);
0521     list_for_each_entry(conn, &iucv_connection_list, list) {
0522         if (strncmp(ipvmid, conn->userid, 8) ||
0523             strncmp(ipuser, conn->userdata, 16))
0524             continue;
0525         /* Found a matching connection for this path. */
0526         conn->path = path;
0527         ev.conn = conn;
0528         ev.data = path;
0529         fsm_event(conn->fsm, CONN_EVENT_CONN_REQ, &ev);
0530         rc = 0;
0531     }
0532     IUCV_DBF_TEXT_(setup, 2, "Connection requested for %s.%s\n",
0533                tmp_user, netiucv_printname(tmp_udat, 16));
0534     read_unlock_bh(&iucv_connection_rwlock);
0535     return rc;
0536 }
0537 
0538 static void netiucv_callback_connrej(struct iucv_path *path, u8 *ipuser)
0539 {
0540     struct iucv_connection *conn = path->private;
0541 
0542     fsm_event(conn->fsm, CONN_EVENT_CONN_REJ, conn);
0543 }
0544 
0545 static void netiucv_callback_connsusp(struct iucv_path *path, u8 *ipuser)
0546 {
0547     struct iucv_connection *conn = path->private;
0548 
0549     fsm_event(conn->fsm, CONN_EVENT_CONN_SUS, conn);
0550 }
0551 
0552 static void netiucv_callback_connres(struct iucv_path *path, u8 *ipuser)
0553 {
0554     struct iucv_connection *conn = path->private;
0555 
0556     fsm_event(conn->fsm, CONN_EVENT_CONN_RES, conn);
0557 }
0558 
0559 /*
0560  * NOP action for statemachines
0561  */
0562 static void netiucv_action_nop(fsm_instance *fi, int event, void *arg)
0563 {
0564 }
0565 
0566 /*
0567  * Actions of the connection statemachine
0568  */
0569 
0570 /*
0571  * netiucv_unpack_skb
0572  * @conn: The connection where this skb has been received.
0573  * @pskb: The received skb.
0574  *
0575  * Unpack a just received skb and hand it over to upper layers.
0576  * Helper function for conn_action_rx.
0577  */
0578 static void netiucv_unpack_skb(struct iucv_connection *conn,
0579                    struct sk_buff *pskb)
0580 {
0581     struct net_device     *dev = conn->netdev;
0582     struct netiucv_priv   *privptr = netdev_priv(dev);
0583     u16 offset = 0;
0584 
0585     skb_put(pskb, NETIUCV_HDRLEN);
0586     pskb->dev = dev;
0587     pskb->ip_summed = CHECKSUM_NONE;
0588     pskb->protocol = cpu_to_be16(ETH_P_IP);
0589 
0590     while (1) {
0591         struct sk_buff *skb;
0592         struct ll_header *header = (struct ll_header *) pskb->data;
0593 
0594         if (!header->next)
0595             break;
0596 
0597         skb_pull(pskb, NETIUCV_HDRLEN);
0598         header->next -= offset;
0599         offset += header->next;
0600         header->next -= NETIUCV_HDRLEN;
0601         if (skb_tailroom(pskb) < header->next) {
0602             IUCV_DBF_TEXT_(data, 2, "Illegal next field: %d > %d\n",
0603                 header->next, skb_tailroom(pskb));
0604             return;
0605         }
0606         skb_put(pskb, header->next);
0607         skb_reset_mac_header(pskb);
0608         skb = dev_alloc_skb(pskb->len);
0609         if (!skb) {
0610             IUCV_DBF_TEXT(data, 2,
0611                 "Out of memory in netiucv_unpack_skb\n");
0612             privptr->stats.rx_dropped++;
0613             return;
0614         }
0615         skb_copy_from_linear_data(pskb, skb_put(skb, pskb->len),
0616                       pskb->len);
0617         skb_reset_mac_header(skb);
0618         skb->dev = pskb->dev;
0619         skb->protocol = pskb->protocol;
0620         pskb->ip_summed = CHECKSUM_UNNECESSARY;
0621         privptr->stats.rx_packets++;
0622         privptr->stats.rx_bytes += skb->len;
0623         netif_rx(skb);
0624         skb_pull(pskb, header->next);
0625         skb_put(pskb, NETIUCV_HDRLEN);
0626     }
0627 }
0628 
0629 static void conn_action_rx(fsm_instance *fi, int event, void *arg)
0630 {
0631     struct iucv_event *ev = arg;
0632     struct iucv_connection *conn = ev->conn;
0633     struct iucv_message *msg = ev->data;
0634     struct netiucv_priv *privptr = netdev_priv(conn->netdev);
0635     int rc;
0636 
0637     IUCV_DBF_TEXT(trace, 4, __func__);
0638 
0639     if (!conn->netdev) {
0640         iucv_message_reject(conn->path, msg);
0641         IUCV_DBF_TEXT(data, 2,
0642                   "Received data for unlinked connection\n");
0643         return;
0644     }
0645     if (msg->length > conn->max_buffsize) {
0646         iucv_message_reject(conn->path, msg);
0647         privptr->stats.rx_dropped++;
0648         IUCV_DBF_TEXT_(data, 2, "msglen %d > max_buffsize %d\n",
0649                    msg->length, conn->max_buffsize);
0650         return;
0651     }
0652     conn->rx_buff->data = conn->rx_buff->head;
0653     skb_reset_tail_pointer(conn->rx_buff);
0654     conn->rx_buff->len = 0;
0655     rc = iucv_message_receive(conn->path, msg, 0, conn->rx_buff->data,
0656                   msg->length, NULL);
0657     if (rc || msg->length < 5) {
0658         privptr->stats.rx_errors++;
0659         IUCV_DBF_TEXT_(data, 2, "rc %d from iucv_receive\n", rc);
0660         return;
0661     }
0662     netiucv_unpack_skb(conn, conn->rx_buff);
0663 }
0664 
0665 static void conn_action_txdone(fsm_instance *fi, int event, void *arg)
0666 {
0667     struct iucv_event *ev = arg;
0668     struct iucv_connection *conn = ev->conn;
0669     struct iucv_message *msg = ev->data;
0670     struct iucv_message txmsg;
0671     struct netiucv_priv *privptr = NULL;
0672     u32 single_flag = msg->tag;
0673     u32 txbytes = 0;
0674     u32 txpackets = 0;
0675     u32 stat_maxcq = 0;
0676     struct sk_buff *skb;
0677     unsigned long saveflags;
0678     struct ll_header header;
0679     int rc;
0680 
0681     IUCV_DBF_TEXT(trace, 4, __func__);
0682 
0683     if (!conn || !conn->netdev) {
0684         IUCV_DBF_TEXT(data, 2,
0685                   "Send confirmation for unlinked connection\n");
0686         return;
0687     }
0688     privptr = netdev_priv(conn->netdev);
0689     conn->prof.tx_pending--;
0690     if (single_flag) {
0691         if ((skb = skb_dequeue(&conn->commit_queue))) {
0692             refcount_dec(&skb->users);
0693             if (privptr) {
0694                 privptr->stats.tx_packets++;
0695                 privptr->stats.tx_bytes +=
0696                     (skb->len - NETIUCV_HDRLEN
0697                           - NETIUCV_HDRLEN);
0698             }
0699             dev_kfree_skb_any(skb);
0700         }
0701     }
0702     conn->tx_buff->data = conn->tx_buff->head;
0703     skb_reset_tail_pointer(conn->tx_buff);
0704     conn->tx_buff->len = 0;
0705     spin_lock_irqsave(&conn->collect_lock, saveflags);
0706     while ((skb = skb_dequeue(&conn->collect_queue))) {
0707         header.next = conn->tx_buff->len + skb->len + NETIUCV_HDRLEN;
0708         skb_put_data(conn->tx_buff, &header, NETIUCV_HDRLEN);
0709         skb_copy_from_linear_data(skb,
0710                       skb_put(conn->tx_buff, skb->len),
0711                       skb->len);
0712         txbytes += skb->len;
0713         txpackets++;
0714         stat_maxcq++;
0715         refcount_dec(&skb->users);
0716         dev_kfree_skb_any(skb);
0717     }
0718     if (conn->collect_len > conn->prof.maxmulti)
0719         conn->prof.maxmulti = conn->collect_len;
0720     conn->collect_len = 0;
0721     spin_unlock_irqrestore(&conn->collect_lock, saveflags);
0722     if (conn->tx_buff->len == 0) {
0723         fsm_newstate(fi, CONN_STATE_IDLE);
0724         return;
0725     }
0726 
0727     header.next = 0;
0728     skb_put_data(conn->tx_buff, &header, NETIUCV_HDRLEN);
0729     conn->prof.send_stamp = jiffies;
0730     txmsg.class = 0;
0731     txmsg.tag = 0;
0732     rc = iucv_message_send(conn->path, &txmsg, 0, 0,
0733                    conn->tx_buff->data, conn->tx_buff->len);
0734     conn->prof.doios_multi++;
0735     conn->prof.txlen += conn->tx_buff->len;
0736     conn->prof.tx_pending++;
0737     if (conn->prof.tx_pending > conn->prof.tx_max_pending)
0738         conn->prof.tx_max_pending = conn->prof.tx_pending;
0739     if (rc) {
0740         conn->prof.tx_pending--;
0741         fsm_newstate(fi, CONN_STATE_IDLE);
0742         if (privptr)
0743             privptr->stats.tx_errors += txpackets;
0744         IUCV_DBF_TEXT_(data, 2, "rc %d from iucv_send\n", rc);
0745     } else {
0746         if (privptr) {
0747             privptr->stats.tx_packets += txpackets;
0748             privptr->stats.tx_bytes += txbytes;
0749         }
0750         if (stat_maxcq > conn->prof.maxcqueue)
0751             conn->prof.maxcqueue = stat_maxcq;
0752     }
0753 }
0754 
0755 static struct iucv_handler netiucv_handler = {
0756     .path_pending     = netiucv_callback_connreq,
0757     .path_complete    = netiucv_callback_connack,
0758     .path_severed     = netiucv_callback_connrej,
0759     .path_quiesced    = netiucv_callback_connsusp,
0760     .path_resumed     = netiucv_callback_connres,
0761     .message_pending  = netiucv_callback_rx,
0762     .message_complete = netiucv_callback_txdone,
0763 };
0764 
0765 static void conn_action_connaccept(fsm_instance *fi, int event, void *arg)
0766 {
0767     struct iucv_event *ev = arg;
0768     struct iucv_connection *conn = ev->conn;
0769     struct iucv_path *path = ev->data;
0770     struct net_device *netdev = conn->netdev;
0771     struct netiucv_priv *privptr = netdev_priv(netdev);
0772     int rc;
0773 
0774     IUCV_DBF_TEXT(trace, 3, __func__);
0775 
0776     conn->path = path;
0777     path->msglim = NETIUCV_QUEUELEN_DEFAULT;
0778     path->flags = 0;
0779     rc = iucv_path_accept(path, &netiucv_handler, conn->userdata , conn);
0780     if (rc) {
0781         IUCV_DBF_TEXT_(setup, 2, "rc %d from iucv_accept", rc);
0782         return;
0783     }
0784     fsm_newstate(fi, CONN_STATE_IDLE);
0785     netdev->tx_queue_len = conn->path->msglim;
0786     fsm_event(privptr->fsm, DEV_EVENT_CONUP, netdev);
0787 }
0788 
0789 static void conn_action_connreject(fsm_instance *fi, int event, void *arg)
0790 {
0791     struct iucv_event *ev = arg;
0792     struct iucv_path *path = ev->data;
0793 
0794     IUCV_DBF_TEXT(trace, 3, __func__);
0795     iucv_path_sever(path, NULL);
0796 }
0797 
0798 static void conn_action_connack(fsm_instance *fi, int event, void *arg)
0799 {
0800     struct iucv_connection *conn = arg;
0801     struct net_device *netdev = conn->netdev;
0802     struct netiucv_priv *privptr = netdev_priv(netdev);
0803 
0804     IUCV_DBF_TEXT(trace, 3, __func__);
0805     fsm_deltimer(&conn->timer);
0806     fsm_newstate(fi, CONN_STATE_IDLE);
0807     netdev->tx_queue_len = conn->path->msglim;
0808     fsm_event(privptr->fsm, DEV_EVENT_CONUP, netdev);
0809 }
0810 
0811 static void conn_action_conntimsev(fsm_instance *fi, int event, void *arg)
0812 {
0813     struct iucv_connection *conn = arg;
0814 
0815     IUCV_DBF_TEXT(trace, 3, __func__);
0816     fsm_deltimer(&conn->timer);
0817     iucv_path_sever(conn->path, conn->userdata);
0818     fsm_newstate(fi, CONN_STATE_STARTWAIT);
0819 }
0820 
0821 static void conn_action_connsever(fsm_instance *fi, int event, void *arg)
0822 {
0823     struct iucv_connection *conn = arg;
0824     struct net_device *netdev = conn->netdev;
0825     struct netiucv_priv *privptr = netdev_priv(netdev);
0826 
0827     IUCV_DBF_TEXT(trace, 3, __func__);
0828 
0829     fsm_deltimer(&conn->timer);
0830     iucv_path_sever(conn->path, conn->userdata);
0831     dev_info(privptr->dev, "The peer z/VM guest %s has closed the "
0832                    "connection\n", netiucv_printuser(conn));
0833     IUCV_DBF_TEXT(data, 2,
0834               "conn_action_connsever: Remote dropped connection\n");
0835     fsm_newstate(fi, CONN_STATE_STARTWAIT);
0836     fsm_event(privptr->fsm, DEV_EVENT_CONDOWN, netdev);
0837 }
0838 
0839 static void conn_action_start(fsm_instance *fi, int event, void *arg)
0840 {
0841     struct iucv_connection *conn = arg;
0842     struct net_device *netdev = conn->netdev;
0843     struct netiucv_priv *privptr = netdev_priv(netdev);
0844     int rc;
0845 
0846     IUCV_DBF_TEXT(trace, 3, __func__);
0847 
0848     fsm_newstate(fi, CONN_STATE_STARTWAIT);
0849 
0850     /*
0851      * We must set the state before calling iucv_connect because the
0852      * callback handler could be called at any point after the connection
0853      * request is sent
0854      */
0855 
0856     fsm_newstate(fi, CONN_STATE_SETUPWAIT);
0857     conn->path = iucv_path_alloc(NETIUCV_QUEUELEN_DEFAULT, 0, GFP_KERNEL);
0858     IUCV_DBF_TEXT_(setup, 2, "%s: connecting to %s ...\n",
0859         netdev->name, netiucv_printuser(conn));
0860 
0861     rc = iucv_path_connect(conn->path, &netiucv_handler, conn->userid,
0862                    NULL, conn->userdata, conn);
0863     switch (rc) {
0864     case 0:
0865         netdev->tx_queue_len = conn->path->msglim;
0866         fsm_addtimer(&conn->timer, NETIUCV_TIMEOUT_5SEC,
0867                  CONN_EVENT_TIMER, conn);
0868         return;
0869     case 11:
0870         dev_warn(privptr->dev,
0871             "The IUCV device failed to connect to z/VM guest %s\n",
0872             netiucv_printname(conn->userid, 8));
0873         fsm_newstate(fi, CONN_STATE_STARTWAIT);
0874         break;
0875     case 12:
0876         dev_warn(privptr->dev,
0877             "The IUCV device failed to connect to the peer on z/VM"
0878             " guest %s\n", netiucv_printname(conn->userid, 8));
0879         fsm_newstate(fi, CONN_STATE_STARTWAIT);
0880         break;
0881     case 13:
0882         dev_err(privptr->dev,
0883             "Connecting the IUCV device would exceed the maximum"
0884             " number of IUCV connections\n");
0885         fsm_newstate(fi, CONN_STATE_CONNERR);
0886         break;
0887     case 14:
0888         dev_err(privptr->dev,
0889             "z/VM guest %s has too many IUCV connections"
0890             " to connect with the IUCV device\n",
0891             netiucv_printname(conn->userid, 8));
0892         fsm_newstate(fi, CONN_STATE_CONNERR);
0893         break;
0894     case 15:
0895         dev_err(privptr->dev,
0896             "The IUCV device cannot connect to a z/VM guest with no"
0897             " IUCV authorization\n");
0898         fsm_newstate(fi, CONN_STATE_CONNERR);
0899         break;
0900     default:
0901         dev_err(privptr->dev,
0902             "Connecting the IUCV device failed with error %d\n",
0903             rc);
0904         fsm_newstate(fi, CONN_STATE_CONNERR);
0905         break;
0906     }
0907     IUCV_DBF_TEXT_(setup, 5, "iucv_connect rc is %d\n", rc);
0908     kfree(conn->path);
0909     conn->path = NULL;
0910 }
0911 
0912 static void netiucv_purge_skb_queue(struct sk_buff_head *q)
0913 {
0914     struct sk_buff *skb;
0915 
0916     while ((skb = skb_dequeue(q))) {
0917         refcount_dec(&skb->users);
0918         dev_kfree_skb_any(skb);
0919     }
0920 }
0921 
0922 static void conn_action_stop(fsm_instance *fi, int event, void *arg)
0923 {
0924     struct iucv_event *ev = arg;
0925     struct iucv_connection *conn = ev->conn;
0926     struct net_device *netdev = conn->netdev;
0927     struct netiucv_priv *privptr = netdev_priv(netdev);
0928 
0929     IUCV_DBF_TEXT(trace, 3, __func__);
0930 
0931     fsm_deltimer(&conn->timer);
0932     fsm_newstate(fi, CONN_STATE_STOPPED);
0933     netiucv_purge_skb_queue(&conn->collect_queue);
0934     if (conn->path) {
0935         IUCV_DBF_TEXT(trace, 5, "calling iucv_path_sever\n");
0936         iucv_path_sever(conn->path, conn->userdata);
0937         kfree(conn->path);
0938         conn->path = NULL;
0939     }
0940     netiucv_purge_skb_queue(&conn->commit_queue);
0941     fsm_event(privptr->fsm, DEV_EVENT_CONDOWN, netdev);
0942 }
0943 
0944 static void conn_action_inval(fsm_instance *fi, int event, void *arg)
0945 {
0946     struct iucv_connection *conn = arg;
0947     struct net_device *netdev = conn->netdev;
0948 
0949     IUCV_DBF_TEXT_(data, 2, "%s('%s'): conn_action_inval called\n",
0950         netdev->name, conn->userid);
0951 }
0952 
0953 static const fsm_node conn_fsm[] = {
0954     { CONN_STATE_INVALID,   CONN_EVENT_START,    conn_action_inval      },
0955     { CONN_STATE_STOPPED,   CONN_EVENT_START,    conn_action_start      },
0956 
0957     { CONN_STATE_STOPPED,   CONN_EVENT_STOP,     conn_action_stop       },
0958     { CONN_STATE_STARTWAIT, CONN_EVENT_STOP,     conn_action_stop       },
0959     { CONN_STATE_SETUPWAIT, CONN_EVENT_STOP,     conn_action_stop       },
0960     { CONN_STATE_IDLE,      CONN_EVENT_STOP,     conn_action_stop       },
0961     { CONN_STATE_TX,        CONN_EVENT_STOP,     conn_action_stop       },
0962     { CONN_STATE_REGERR,    CONN_EVENT_STOP,     conn_action_stop       },
0963     { CONN_STATE_CONNERR,   CONN_EVENT_STOP,     conn_action_stop       },
0964 
0965     { CONN_STATE_STOPPED,   CONN_EVENT_CONN_REQ, conn_action_connreject },
0966         { CONN_STATE_STARTWAIT, CONN_EVENT_CONN_REQ, conn_action_connaccept },
0967     { CONN_STATE_SETUPWAIT, CONN_EVENT_CONN_REQ, conn_action_connaccept },
0968     { CONN_STATE_IDLE,      CONN_EVENT_CONN_REQ, conn_action_connreject },
0969     { CONN_STATE_TX,        CONN_EVENT_CONN_REQ, conn_action_connreject },
0970 
0971     { CONN_STATE_SETUPWAIT, CONN_EVENT_CONN_ACK, conn_action_connack    },
0972     { CONN_STATE_SETUPWAIT, CONN_EVENT_TIMER,    conn_action_conntimsev },
0973 
0974     { CONN_STATE_SETUPWAIT, CONN_EVENT_CONN_REJ, conn_action_connsever  },
0975     { CONN_STATE_IDLE,      CONN_EVENT_CONN_REJ, conn_action_connsever  },
0976     { CONN_STATE_TX,        CONN_EVENT_CONN_REJ, conn_action_connsever  },
0977 
0978     { CONN_STATE_IDLE,      CONN_EVENT_RX,       conn_action_rx         },
0979     { CONN_STATE_TX,        CONN_EVENT_RX,       conn_action_rx         },
0980 
0981     { CONN_STATE_TX,        CONN_EVENT_TXDONE,   conn_action_txdone     },
0982     { CONN_STATE_IDLE,      CONN_EVENT_TXDONE,   conn_action_txdone     },
0983 };
0984 
0985 static const int CONN_FSM_LEN = sizeof(conn_fsm) / sizeof(fsm_node);
0986 
0987 
0988 /*
0989  * Actions for interface - statemachine.
0990  */
0991 
0992 /*
0993  * dev_action_start
0994  * @fi: An instance of an interface statemachine.
0995  * @event: The event, just happened.
0996  * @arg: Generic pointer, casted from struct net_device * upon call.
0997  *
0998  * Startup connection by sending CONN_EVENT_START to it.
0999  */
1000 static void dev_action_start(fsm_instance *fi, int event, void *arg)
1001 {
1002     struct net_device   *dev = arg;
1003     struct netiucv_priv *privptr = netdev_priv(dev);
1004 
1005     IUCV_DBF_TEXT(trace, 3, __func__);
1006 
1007     fsm_newstate(fi, DEV_STATE_STARTWAIT);
1008     fsm_event(privptr->conn->fsm, CONN_EVENT_START, privptr->conn);
1009 }
1010 
1011 /*
1012  * Shutdown connection by sending CONN_EVENT_STOP to it.
1013  *
1014  * @param fi    An instance of an interface statemachine.
1015  * @param event The event, just happened.
1016  * @param arg   Generic pointer, casted from struct net_device * upon call.
1017  */
1018 static void
1019 dev_action_stop(fsm_instance *fi, int event, void *arg)
1020 {
1021     struct net_device   *dev = arg;
1022     struct netiucv_priv *privptr = netdev_priv(dev);
1023     struct iucv_event   ev;
1024 
1025     IUCV_DBF_TEXT(trace, 3, __func__);
1026 
1027     ev.conn = privptr->conn;
1028 
1029     fsm_newstate(fi, DEV_STATE_STOPWAIT);
1030     fsm_event(privptr->conn->fsm, CONN_EVENT_STOP, &ev);
1031 }
1032 
1033 /*
1034  * Called from connection statemachine
1035  * when a connection is up and running.
1036  *
1037  * @param fi    An instance of an interface statemachine.
1038  * @param event The event, just happened.
1039  * @param arg   Generic pointer, casted from struct net_device * upon call.
1040  */
1041 static void
1042 dev_action_connup(fsm_instance *fi, int event, void *arg)
1043 {
1044     struct net_device   *dev = arg;
1045     struct netiucv_priv *privptr = netdev_priv(dev);
1046 
1047     IUCV_DBF_TEXT(trace, 3, __func__);
1048 
1049     switch (fsm_getstate(fi)) {
1050         case DEV_STATE_STARTWAIT:
1051             fsm_newstate(fi, DEV_STATE_RUNNING);
1052             dev_info(privptr->dev,
1053                 "The IUCV device has been connected"
1054                 " successfully to %s\n",
1055                 netiucv_printuser(privptr->conn));
1056             IUCV_DBF_TEXT(setup, 3,
1057                 "connection is up and running\n");
1058             break;
1059         case DEV_STATE_STOPWAIT:
1060             IUCV_DBF_TEXT(data, 2,
1061                 "dev_action_connup: in DEV_STATE_STOPWAIT\n");
1062             break;
1063     }
1064 }
1065 
1066 /*
1067  * Called from connection statemachine
1068  * when a connection has been shutdown.
1069  *
1070  * @param fi    An instance of an interface statemachine.
1071  * @param event The event, just happened.
1072  * @param arg   Generic pointer, casted from struct net_device * upon call.
1073  */
1074 static void
1075 dev_action_conndown(fsm_instance *fi, int event, void *arg)
1076 {
1077     IUCV_DBF_TEXT(trace, 3, __func__);
1078 
1079     switch (fsm_getstate(fi)) {
1080         case DEV_STATE_RUNNING:
1081             fsm_newstate(fi, DEV_STATE_STARTWAIT);
1082             break;
1083         case DEV_STATE_STOPWAIT:
1084             fsm_newstate(fi, DEV_STATE_STOPPED);
1085             IUCV_DBF_TEXT(setup, 3, "connection is down\n");
1086             break;
1087     }
1088 }
1089 
1090 static const fsm_node dev_fsm[] = {
1091     { DEV_STATE_STOPPED,    DEV_EVENT_START,   dev_action_start    },
1092 
1093     { DEV_STATE_STOPWAIT,   DEV_EVENT_START,   dev_action_start    },
1094     { DEV_STATE_STOPWAIT,   DEV_EVENT_CONDOWN, dev_action_conndown },
1095 
1096     { DEV_STATE_STARTWAIT,  DEV_EVENT_STOP,    dev_action_stop     },
1097     { DEV_STATE_STARTWAIT,  DEV_EVENT_CONUP,   dev_action_connup   },
1098 
1099     { DEV_STATE_RUNNING,    DEV_EVENT_STOP,    dev_action_stop     },
1100     { DEV_STATE_RUNNING,    DEV_EVENT_CONDOWN, dev_action_conndown },
1101     { DEV_STATE_RUNNING,    DEV_EVENT_CONUP,   netiucv_action_nop  },
1102 };
1103 
1104 static const int DEV_FSM_LEN = sizeof(dev_fsm) / sizeof(fsm_node);
1105 
1106 /*
1107  * Transmit a packet.
1108  * This is a helper function for netiucv_tx().
1109  *
1110  * @param conn Connection to be used for sending.
1111  * @param skb Pointer to struct sk_buff of packet to send.
1112  *            The linklevel header has already been set up
1113  *            by netiucv_tx().
1114  *
1115  * @return 0 on success, -ERRNO on failure. (Never fails.)
1116  */
1117 static int netiucv_transmit_skb(struct iucv_connection *conn,
1118                 struct sk_buff *skb)
1119 {
1120     struct iucv_message msg;
1121     unsigned long saveflags;
1122     struct ll_header header;
1123     int rc;
1124 
1125     if (fsm_getstate(conn->fsm) != CONN_STATE_IDLE) {
1126         int l = skb->len + NETIUCV_HDRLEN;
1127 
1128         spin_lock_irqsave(&conn->collect_lock, saveflags);
1129         if (conn->collect_len + l >
1130             (conn->max_buffsize - NETIUCV_HDRLEN)) {
1131             rc = -EBUSY;
1132             IUCV_DBF_TEXT(data, 2,
1133                       "EBUSY from netiucv_transmit_skb\n");
1134         } else {
1135             refcount_inc(&skb->users);
1136             skb_queue_tail(&conn->collect_queue, skb);
1137             conn->collect_len += l;
1138             rc = 0;
1139         }
1140         spin_unlock_irqrestore(&conn->collect_lock, saveflags);
1141     } else {
1142         struct sk_buff *nskb = skb;
1143         /*
1144          * Copy the skb to a new allocated skb in lowmem only if the
1145          * data is located above 2G in memory or tailroom is < 2.
1146          */
1147         unsigned long hi = ((unsigned long)(skb_tail_pointer(skb) +
1148                     NETIUCV_HDRLEN)) >> 31;
1149         int copied = 0;
1150         if (hi || (skb_tailroom(skb) < 2)) {
1151             nskb = alloc_skb(skb->len + NETIUCV_HDRLEN +
1152                      NETIUCV_HDRLEN, GFP_ATOMIC | GFP_DMA);
1153             if (!nskb) {
1154                 IUCV_DBF_TEXT(data, 2, "alloc_skb failed\n");
1155                 rc = -ENOMEM;
1156                 return rc;
1157             } else {
1158                 skb_reserve(nskb, NETIUCV_HDRLEN);
1159                 skb_put_data(nskb, skb->data, skb->len);
1160             }
1161             copied = 1;
1162         }
1163         /*
1164          * skb now is below 2G and has enough room. Add headers.
1165          */
1166         header.next = nskb->len + NETIUCV_HDRLEN;
1167         memcpy(skb_push(nskb, NETIUCV_HDRLEN), &header, NETIUCV_HDRLEN);
1168         header.next = 0;
1169         skb_put_data(nskb, &header, NETIUCV_HDRLEN);
1170 
1171         fsm_newstate(conn->fsm, CONN_STATE_TX);
1172         conn->prof.send_stamp = jiffies;
1173 
1174         msg.tag = 1;
1175         msg.class = 0;
1176         rc = iucv_message_send(conn->path, &msg, 0, 0,
1177                        nskb->data, nskb->len);
1178         conn->prof.doios_single++;
1179         conn->prof.txlen += skb->len;
1180         conn->prof.tx_pending++;
1181         if (conn->prof.tx_pending > conn->prof.tx_max_pending)
1182             conn->prof.tx_max_pending = conn->prof.tx_pending;
1183         if (rc) {
1184             struct netiucv_priv *privptr;
1185             fsm_newstate(conn->fsm, CONN_STATE_IDLE);
1186             conn->prof.tx_pending--;
1187             privptr = netdev_priv(conn->netdev);
1188             if (privptr)
1189                 privptr->stats.tx_errors++;
1190             if (copied)
1191                 dev_kfree_skb(nskb);
1192             else {
1193                 /*
1194                  * Remove our headers. They get added
1195                  * again on retransmit.
1196                  */
1197                 skb_pull(skb, NETIUCV_HDRLEN);
1198                 skb_trim(skb, skb->len - NETIUCV_HDRLEN);
1199             }
1200             IUCV_DBF_TEXT_(data, 2, "rc %d from iucv_send\n", rc);
1201         } else {
1202             if (copied)
1203                 dev_kfree_skb(skb);
1204             refcount_inc(&nskb->users);
1205             skb_queue_tail(&conn->commit_queue, nskb);
1206         }
1207     }
1208 
1209     return rc;
1210 }
1211 
1212 /*
1213  * Interface API for upper network layers
1214  */
1215 
1216 /*
1217  * Open an interface.
1218  * Called from generic network layer when ifconfig up is run.
1219  *
1220  * @param dev Pointer to interface struct.
1221  *
1222  * @return 0 on success, -ERRNO on failure. (Never fails.)
1223  */
1224 static int netiucv_open(struct net_device *dev)
1225 {
1226     struct netiucv_priv *priv = netdev_priv(dev);
1227 
1228     fsm_event(priv->fsm, DEV_EVENT_START, dev);
1229     return 0;
1230 }
1231 
1232 /*
1233  * Close an interface.
1234  * Called from generic network layer when ifconfig down is run.
1235  *
1236  * @param dev Pointer to interface struct.
1237  *
1238  * @return 0 on success, -ERRNO on failure. (Never fails.)
1239  */
1240 static int netiucv_close(struct net_device *dev)
1241 {
1242     struct netiucv_priv *priv = netdev_priv(dev);
1243 
1244     fsm_event(priv->fsm, DEV_EVENT_STOP, dev);
1245     return 0;
1246 }
1247 
1248 /*
1249  * Start transmission of a packet.
1250  * Called from generic network device layer.
1251  *
1252  * @param skb Pointer to buffer containing the packet.
1253  * @param dev Pointer to interface struct.
1254  *
1255  * @return 0 if packet consumed, !0 if packet rejected.
1256  *         Note: If we return !0, then the packet is free'd by
1257  *               the generic network layer.
1258  */
1259 static int netiucv_tx(struct sk_buff *skb, struct net_device *dev)
1260 {
1261     struct netiucv_priv *privptr = netdev_priv(dev);
1262     int rc;
1263 
1264     IUCV_DBF_TEXT(trace, 4, __func__);
1265     /*
1266      * Some sanity checks ...
1267      */
1268     if (skb == NULL) {
1269         IUCV_DBF_TEXT(data, 2, "netiucv_tx: skb is NULL\n");
1270         privptr->stats.tx_dropped++;
1271         return NETDEV_TX_OK;
1272     }
1273     if (skb_headroom(skb) < NETIUCV_HDRLEN) {
1274         IUCV_DBF_TEXT(data, 2,
1275             "netiucv_tx: skb_headroom < NETIUCV_HDRLEN\n");
1276         dev_kfree_skb(skb);
1277         privptr->stats.tx_dropped++;
1278         return NETDEV_TX_OK;
1279     }
1280 
1281     /*
1282      * If connection is not running, try to restart it
1283      * and throw away packet.
1284      */
1285     if (fsm_getstate(privptr->fsm) != DEV_STATE_RUNNING) {
1286         dev_kfree_skb(skb);
1287         privptr->stats.tx_dropped++;
1288         privptr->stats.tx_errors++;
1289         privptr->stats.tx_carrier_errors++;
1290         return NETDEV_TX_OK;
1291     }
1292 
1293     if (netiucv_test_and_set_busy(dev)) {
1294         IUCV_DBF_TEXT(data, 2, "EBUSY from netiucv_tx\n");
1295         return NETDEV_TX_BUSY;
1296     }
1297     netif_trans_update(dev);
1298     rc = netiucv_transmit_skb(privptr->conn, skb);
1299     netiucv_clear_busy(dev);
1300     return rc ? NETDEV_TX_BUSY : NETDEV_TX_OK;
1301 }
1302 
1303 /*
1304  * netiucv_stats
1305  * @dev: Pointer to interface struct.
1306  *
1307  * Returns interface statistics of a device.
1308  *
1309  * Returns pointer to stats struct of this interface.
1310  */
1311 static struct net_device_stats *netiucv_stats (struct net_device * dev)
1312 {
1313     struct netiucv_priv *priv = netdev_priv(dev);
1314 
1315     IUCV_DBF_TEXT(trace, 5, __func__);
1316     return &priv->stats;
1317 }
1318 
1319 /*
1320  * attributes in sysfs
1321  */
1322 
1323 static ssize_t user_show(struct device *dev, struct device_attribute *attr,
1324              char *buf)
1325 {
1326     struct netiucv_priv *priv = dev_get_drvdata(dev);
1327 
1328     IUCV_DBF_TEXT(trace, 5, __func__);
1329     return sprintf(buf, "%s\n", netiucv_printuser(priv->conn));
1330 }
1331 
1332 static int netiucv_check_user(const char *buf, size_t count, char *username,
1333                   char *userdata)
1334 {
1335     const char *p;
1336     int i;
1337 
1338     p = strchr(buf, '.');
1339     if ((p && ((count > 26) ||
1340            ((p - buf) > 8) ||
1341            (buf + count - p > 18))) ||
1342         (!p && (count > 9))) {
1343         IUCV_DBF_TEXT(setup, 2, "conn_write: too long\n");
1344         return -EINVAL;
1345     }
1346 
1347     for (i = 0, p = buf; i < 8 && *p && *p != '.'; i++, p++) {
1348         if (isalnum(*p) || *p == '$') {
1349             username[i] = toupper(*p);
1350             continue;
1351         }
1352         if (*p == '\n')
1353             /* trailing lf, grr */
1354             break;
1355         IUCV_DBF_TEXT_(setup, 2,
1356                    "conn_write: invalid character %02x\n", *p);
1357         return -EINVAL;
1358     }
1359     while (i < 8)
1360         username[i++] = ' ';
1361     username[8] = '\0';
1362 
1363     if (*p == '.') {
1364         p++;
1365         for (i = 0; i < 16 && *p; i++, p++) {
1366             if (*p == '\n')
1367                 break;
1368             userdata[i] = toupper(*p);
1369         }
1370         while (i > 0 && i < 16)
1371             userdata[i++] = ' ';
1372     } else
1373         memcpy(userdata, iucvMagic_ascii, 16);
1374     userdata[16] = '\0';
1375     ASCEBC(userdata, 16);
1376 
1377     return 0;
1378 }
1379 
1380 static ssize_t user_write(struct device *dev, struct device_attribute *attr,
1381               const char *buf, size_t count)
1382 {
1383     struct netiucv_priv *priv = dev_get_drvdata(dev);
1384     struct net_device *ndev = priv->conn->netdev;
1385     char    username[9];
1386     char    userdata[17];
1387     int rc;
1388     struct iucv_connection *cp;
1389 
1390     IUCV_DBF_TEXT(trace, 3, __func__);
1391     rc = netiucv_check_user(buf, count, username, userdata);
1392     if (rc)
1393         return rc;
1394 
1395     if (memcmp(username, priv->conn->userid, 9) &&
1396         (ndev->flags & (IFF_UP | IFF_RUNNING))) {
1397         /* username changed while the interface is active. */
1398         IUCV_DBF_TEXT(setup, 2, "user_write: device active\n");
1399         return -EPERM;
1400     }
1401     read_lock_bh(&iucv_connection_rwlock);
1402     list_for_each_entry(cp, &iucv_connection_list, list) {
1403         if (!strncmp(username, cp->userid, 9) &&
1404            !strncmp(userdata, cp->userdata, 17) && cp->netdev != ndev) {
1405             read_unlock_bh(&iucv_connection_rwlock);
1406             IUCV_DBF_TEXT_(setup, 2, "user_write: Connection to %s "
1407                 "already exists\n", netiucv_printuser(cp));
1408             return -EEXIST;
1409         }
1410     }
1411     read_unlock_bh(&iucv_connection_rwlock);
1412     memcpy(priv->conn->userid, username, 9);
1413     memcpy(priv->conn->userdata, userdata, 17);
1414     return count;
1415 }
1416 
1417 static DEVICE_ATTR(user, 0644, user_show, user_write);
1418 
1419 static ssize_t buffer_show (struct device *dev, struct device_attribute *attr,
1420                 char *buf)
1421 {
1422     struct netiucv_priv *priv = dev_get_drvdata(dev);
1423 
1424     IUCV_DBF_TEXT(trace, 5, __func__);
1425     return sprintf(buf, "%d\n", priv->conn->max_buffsize);
1426 }
1427 
1428 static ssize_t buffer_write (struct device *dev, struct device_attribute *attr,
1429                  const char *buf, size_t count)
1430 {
1431     struct netiucv_priv *priv = dev_get_drvdata(dev);
1432     struct net_device *ndev = priv->conn->netdev;
1433     unsigned int bs1;
1434     int rc;
1435 
1436     IUCV_DBF_TEXT(trace, 3, __func__);
1437     if (count >= 39)
1438         return -EINVAL;
1439 
1440     rc = kstrtouint(buf, 0, &bs1);
1441 
1442     if (rc == -EINVAL) {
1443         IUCV_DBF_TEXT_(setup, 2, "buffer_write: invalid char %s\n",
1444             buf);
1445         return -EINVAL;
1446     }
1447     if ((rc == -ERANGE) || (bs1 > NETIUCV_BUFSIZE_MAX)) {
1448         IUCV_DBF_TEXT_(setup, 2,
1449             "buffer_write: buffer size %d too large\n",
1450             bs1);
1451         return -EINVAL;
1452     }
1453     if ((ndev->flags & IFF_RUNNING) &&
1454         (bs1 < (ndev->mtu + NETIUCV_HDRLEN + 2))) {
1455         IUCV_DBF_TEXT_(setup, 2,
1456             "buffer_write: buffer size %d too small\n",
1457             bs1);
1458         return -EINVAL;
1459     }
1460     if (bs1 < (576 + NETIUCV_HDRLEN + NETIUCV_HDRLEN)) {
1461         IUCV_DBF_TEXT_(setup, 2,
1462             "buffer_write: buffer size %d too small\n",
1463             bs1);
1464         return -EINVAL;
1465     }
1466 
1467     priv->conn->max_buffsize = bs1;
1468     if (!(ndev->flags & IFF_RUNNING))
1469         ndev->mtu = bs1 - NETIUCV_HDRLEN - NETIUCV_HDRLEN;
1470 
1471     return count;
1472 
1473 }
1474 
1475 static DEVICE_ATTR(buffer, 0644, buffer_show, buffer_write);
1476 
1477 static ssize_t dev_fsm_show (struct device *dev, struct device_attribute *attr,
1478                  char *buf)
1479 {
1480     struct netiucv_priv *priv = dev_get_drvdata(dev);
1481 
1482     IUCV_DBF_TEXT(trace, 5, __func__);
1483     return sprintf(buf, "%s\n", fsm_getstate_str(priv->fsm));
1484 }
1485 
1486 static DEVICE_ATTR(device_fsm_state, 0444, dev_fsm_show, NULL);
1487 
1488 static ssize_t conn_fsm_show (struct device *dev,
1489                   struct device_attribute *attr, char *buf)
1490 {
1491     struct netiucv_priv *priv = dev_get_drvdata(dev);
1492 
1493     IUCV_DBF_TEXT(trace, 5, __func__);
1494     return sprintf(buf, "%s\n", fsm_getstate_str(priv->conn->fsm));
1495 }
1496 
1497 static DEVICE_ATTR(connection_fsm_state, 0444, conn_fsm_show, NULL);
1498 
1499 static ssize_t maxmulti_show (struct device *dev,
1500                   struct device_attribute *attr, char *buf)
1501 {
1502     struct netiucv_priv *priv = dev_get_drvdata(dev);
1503 
1504     IUCV_DBF_TEXT(trace, 5, __func__);
1505     return sprintf(buf, "%ld\n", priv->conn->prof.maxmulti);
1506 }
1507 
1508 static ssize_t maxmulti_write (struct device *dev,
1509                    struct device_attribute *attr,
1510                    const char *buf, size_t count)
1511 {
1512     struct netiucv_priv *priv = dev_get_drvdata(dev);
1513 
1514     IUCV_DBF_TEXT(trace, 4, __func__);
1515     priv->conn->prof.maxmulti = 0;
1516     return count;
1517 }
1518 
1519 static DEVICE_ATTR(max_tx_buffer_used, 0644, maxmulti_show, maxmulti_write);
1520 
1521 static ssize_t maxcq_show (struct device *dev, struct device_attribute *attr,
1522                char *buf)
1523 {
1524     struct netiucv_priv *priv = dev_get_drvdata(dev);
1525 
1526     IUCV_DBF_TEXT(trace, 5, __func__);
1527     return sprintf(buf, "%ld\n", priv->conn->prof.maxcqueue);
1528 }
1529 
1530 static ssize_t maxcq_write (struct device *dev, struct device_attribute *attr,
1531                 const char *buf, size_t count)
1532 {
1533     struct netiucv_priv *priv = dev_get_drvdata(dev);
1534 
1535     IUCV_DBF_TEXT(trace, 4, __func__);
1536     priv->conn->prof.maxcqueue = 0;
1537     return count;
1538 }
1539 
1540 static DEVICE_ATTR(max_chained_skbs, 0644, maxcq_show, maxcq_write);
1541 
1542 static ssize_t sdoio_show (struct device *dev, struct device_attribute *attr,
1543                char *buf)
1544 {
1545     struct netiucv_priv *priv = dev_get_drvdata(dev);
1546 
1547     IUCV_DBF_TEXT(trace, 5, __func__);
1548     return sprintf(buf, "%ld\n", priv->conn->prof.doios_single);
1549 }
1550 
1551 static ssize_t sdoio_write (struct device *dev, struct device_attribute *attr,
1552                 const char *buf, size_t count)
1553 {
1554     struct netiucv_priv *priv = dev_get_drvdata(dev);
1555 
1556     IUCV_DBF_TEXT(trace, 4, __func__);
1557     priv->conn->prof.doios_single = 0;
1558     return count;
1559 }
1560 
1561 static DEVICE_ATTR(tx_single_write_ops, 0644, sdoio_show, sdoio_write);
1562 
1563 static ssize_t mdoio_show (struct device *dev, struct device_attribute *attr,
1564                char *buf)
1565 {
1566     struct netiucv_priv *priv = dev_get_drvdata(dev);
1567 
1568     IUCV_DBF_TEXT(trace, 5, __func__);
1569     return sprintf(buf, "%ld\n", priv->conn->prof.doios_multi);
1570 }
1571 
1572 static ssize_t mdoio_write (struct device *dev, struct device_attribute *attr,
1573                 const char *buf, size_t count)
1574 {
1575     struct netiucv_priv *priv = dev_get_drvdata(dev);
1576 
1577     IUCV_DBF_TEXT(trace, 5, __func__);
1578     priv->conn->prof.doios_multi = 0;
1579     return count;
1580 }
1581 
1582 static DEVICE_ATTR(tx_multi_write_ops, 0644, mdoio_show, mdoio_write);
1583 
1584 static ssize_t txlen_show (struct device *dev, struct device_attribute *attr,
1585                char *buf)
1586 {
1587     struct netiucv_priv *priv = dev_get_drvdata(dev);
1588 
1589     IUCV_DBF_TEXT(trace, 5, __func__);
1590     return sprintf(buf, "%ld\n", priv->conn->prof.txlen);
1591 }
1592 
1593 static ssize_t txlen_write (struct device *dev, struct device_attribute *attr,
1594                 const char *buf, size_t count)
1595 {
1596     struct netiucv_priv *priv = dev_get_drvdata(dev);
1597 
1598     IUCV_DBF_TEXT(trace, 4, __func__);
1599     priv->conn->prof.txlen = 0;
1600     return count;
1601 }
1602 
1603 static DEVICE_ATTR(netto_bytes, 0644, txlen_show, txlen_write);
1604 
1605 static ssize_t txtime_show (struct device *dev, struct device_attribute *attr,
1606                 char *buf)
1607 {
1608     struct netiucv_priv *priv = dev_get_drvdata(dev);
1609 
1610     IUCV_DBF_TEXT(trace, 5, __func__);
1611     return sprintf(buf, "%ld\n", priv->conn->prof.tx_time);
1612 }
1613 
1614 static ssize_t txtime_write (struct device *dev, struct device_attribute *attr,
1615                  const char *buf, size_t count)
1616 {
1617     struct netiucv_priv *priv = dev_get_drvdata(dev);
1618 
1619     IUCV_DBF_TEXT(trace, 4, __func__);
1620     priv->conn->prof.tx_time = 0;
1621     return count;
1622 }
1623 
1624 static DEVICE_ATTR(max_tx_io_time, 0644, txtime_show, txtime_write);
1625 
1626 static ssize_t txpend_show (struct device *dev, struct device_attribute *attr,
1627                 char *buf)
1628 {
1629     struct netiucv_priv *priv = dev_get_drvdata(dev);
1630 
1631     IUCV_DBF_TEXT(trace, 5, __func__);
1632     return sprintf(buf, "%ld\n", priv->conn->prof.tx_pending);
1633 }
1634 
1635 static ssize_t txpend_write (struct device *dev, struct device_attribute *attr,
1636                  const char *buf, size_t count)
1637 {
1638     struct netiucv_priv *priv = dev_get_drvdata(dev);
1639 
1640     IUCV_DBF_TEXT(trace, 4, __func__);
1641     priv->conn->prof.tx_pending = 0;
1642     return count;
1643 }
1644 
1645 static DEVICE_ATTR(tx_pending, 0644, txpend_show, txpend_write);
1646 
1647 static ssize_t txmpnd_show (struct device *dev, struct device_attribute *attr,
1648                 char *buf)
1649 {
1650     struct netiucv_priv *priv = dev_get_drvdata(dev);
1651 
1652     IUCV_DBF_TEXT(trace, 5, __func__);
1653     return sprintf(buf, "%ld\n", priv->conn->prof.tx_max_pending);
1654 }
1655 
1656 static ssize_t txmpnd_write (struct device *dev, struct device_attribute *attr,
1657                  const char *buf, size_t count)
1658 {
1659     struct netiucv_priv *priv = dev_get_drvdata(dev);
1660 
1661     IUCV_DBF_TEXT(trace, 4, __func__);
1662     priv->conn->prof.tx_max_pending = 0;
1663     return count;
1664 }
1665 
1666 static DEVICE_ATTR(tx_max_pending, 0644, txmpnd_show, txmpnd_write);
1667 
1668 static struct attribute *netiucv_attrs[] = {
1669     &dev_attr_buffer.attr,
1670     &dev_attr_user.attr,
1671     NULL,
1672 };
1673 
1674 static struct attribute_group netiucv_attr_group = {
1675     .attrs = netiucv_attrs,
1676 };
1677 
1678 static struct attribute *netiucv_stat_attrs[] = {
1679     &dev_attr_device_fsm_state.attr,
1680     &dev_attr_connection_fsm_state.attr,
1681     &dev_attr_max_tx_buffer_used.attr,
1682     &dev_attr_max_chained_skbs.attr,
1683     &dev_attr_tx_single_write_ops.attr,
1684     &dev_attr_tx_multi_write_ops.attr,
1685     &dev_attr_netto_bytes.attr,
1686     &dev_attr_max_tx_io_time.attr,
1687     &dev_attr_tx_pending.attr,
1688     &dev_attr_tx_max_pending.attr,
1689     NULL,
1690 };
1691 
1692 static struct attribute_group netiucv_stat_attr_group = {
1693     .name  = "stats",
1694     .attrs = netiucv_stat_attrs,
1695 };
1696 
1697 static const struct attribute_group *netiucv_attr_groups[] = {
1698     &netiucv_stat_attr_group,
1699     &netiucv_attr_group,
1700     NULL,
1701 };
1702 
1703 static int netiucv_register_device(struct net_device *ndev)
1704 {
1705     struct netiucv_priv *priv = netdev_priv(ndev);
1706     struct device *dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1707     int ret;
1708 
1709     IUCV_DBF_TEXT(trace, 3, __func__);
1710 
1711     if (dev) {
1712         dev_set_name(dev, "net%s", ndev->name);
1713         dev->bus = &iucv_bus;
1714         dev->parent = iucv_root;
1715         dev->groups = netiucv_attr_groups;
1716         /*
1717          * The release function could be called after the
1718          * module has been unloaded. It's _only_ task is to
1719          * free the struct. Therefore, we specify kfree()
1720          * directly here. (Probably a little bit obfuscating
1721          * but legitime ...).
1722          */
1723         dev->release = (void (*)(struct device *))kfree;
1724         dev->driver = &netiucv_driver;
1725     } else
1726         return -ENOMEM;
1727 
1728     ret = device_register(dev);
1729     if (ret) {
1730         put_device(dev);
1731         return ret;
1732     }
1733     priv->dev = dev;
1734     dev_set_drvdata(dev, priv);
1735     return 0;
1736 }
1737 
1738 static void netiucv_unregister_device(struct device *dev)
1739 {
1740     IUCV_DBF_TEXT(trace, 3, __func__);
1741     device_unregister(dev);
1742 }
1743 
1744 /*
1745  * Allocate and initialize a new connection structure.
1746  * Add it to the list of netiucv connections;
1747  */
1748 static struct iucv_connection *netiucv_new_connection(struct net_device *dev,
1749                               char *username,
1750                               char *userdata)
1751 {
1752     struct iucv_connection *conn;
1753 
1754     conn = kzalloc(sizeof(*conn), GFP_KERNEL);
1755     if (!conn)
1756         goto out;
1757     skb_queue_head_init(&conn->collect_queue);
1758     skb_queue_head_init(&conn->commit_queue);
1759     spin_lock_init(&conn->collect_lock);
1760     conn->max_buffsize = NETIUCV_BUFSIZE_DEFAULT;
1761     conn->netdev = dev;
1762 
1763     conn->rx_buff = alloc_skb(conn->max_buffsize, GFP_KERNEL | GFP_DMA);
1764     if (!conn->rx_buff)
1765         goto out_conn;
1766     conn->tx_buff = alloc_skb(conn->max_buffsize, GFP_KERNEL | GFP_DMA);
1767     if (!conn->tx_buff)
1768         goto out_rx;
1769     conn->fsm = init_fsm("netiucvconn", conn_state_names,
1770                  conn_event_names, NR_CONN_STATES,
1771                  NR_CONN_EVENTS, conn_fsm, CONN_FSM_LEN,
1772                  GFP_KERNEL);
1773     if (!conn->fsm)
1774         goto out_tx;
1775 
1776     fsm_settimer(conn->fsm, &conn->timer);
1777     fsm_newstate(conn->fsm, CONN_STATE_INVALID);
1778 
1779     if (userdata)
1780         memcpy(conn->userdata, userdata, 17);
1781     if (username) {
1782         memcpy(conn->userid, username, 9);
1783         fsm_newstate(conn->fsm, CONN_STATE_STOPPED);
1784     }
1785 
1786     write_lock_bh(&iucv_connection_rwlock);
1787     list_add_tail(&conn->list, &iucv_connection_list);
1788     write_unlock_bh(&iucv_connection_rwlock);
1789     return conn;
1790 
1791 out_tx:
1792     kfree_skb(conn->tx_buff);
1793 out_rx:
1794     kfree_skb(conn->rx_buff);
1795 out_conn:
1796     kfree(conn);
1797 out:
1798     return NULL;
1799 }
1800 
1801 /*
1802  * Release a connection structure and remove it from the
1803  * list of netiucv connections.
1804  */
1805 static void netiucv_remove_connection(struct iucv_connection *conn)
1806 {
1807 
1808     IUCV_DBF_TEXT(trace, 3, __func__);
1809     write_lock_bh(&iucv_connection_rwlock);
1810     list_del_init(&conn->list);
1811     write_unlock_bh(&iucv_connection_rwlock);
1812     fsm_deltimer(&conn->timer);
1813     netiucv_purge_skb_queue(&conn->collect_queue);
1814     if (conn->path) {
1815         iucv_path_sever(conn->path, conn->userdata);
1816         kfree(conn->path);
1817         conn->path = NULL;
1818     }
1819     netiucv_purge_skb_queue(&conn->commit_queue);
1820     kfree_fsm(conn->fsm);
1821     kfree_skb(conn->rx_buff);
1822     kfree_skb(conn->tx_buff);
1823 }
1824 
1825 /*
1826  * Release everything of a net device.
1827  */
1828 static void netiucv_free_netdevice(struct net_device *dev)
1829 {
1830     struct netiucv_priv *privptr = netdev_priv(dev);
1831 
1832     IUCV_DBF_TEXT(trace, 3, __func__);
1833 
1834     if (!dev)
1835         return;
1836 
1837     if (privptr) {
1838         if (privptr->conn)
1839             netiucv_remove_connection(privptr->conn);
1840         if (privptr->fsm)
1841             kfree_fsm(privptr->fsm);
1842         privptr->conn = NULL; privptr->fsm = NULL;
1843         /* privptr gets freed by free_netdev() */
1844     }
1845 }
1846 
1847 /*
1848  * Initialize a net device. (Called from kernel in alloc_netdev())
1849  */
1850 static const struct net_device_ops netiucv_netdev_ops = {
1851     .ndo_open       = netiucv_open,
1852     .ndo_stop       = netiucv_close,
1853     .ndo_get_stats      = netiucv_stats,
1854     .ndo_start_xmit     = netiucv_tx,
1855 };
1856 
1857 static void netiucv_setup_netdevice(struct net_device *dev)
1858 {
1859     dev->mtu             = NETIUCV_MTU_DEFAULT;
1860     dev->min_mtu         = 576;
1861     dev->max_mtu         = NETIUCV_MTU_MAX;
1862     dev->needs_free_netdev   = true;
1863     dev->priv_destructor     = netiucv_free_netdevice;
1864     dev->hard_header_len     = NETIUCV_HDRLEN;
1865     dev->addr_len            = 0;
1866     dev->type                = ARPHRD_SLIP;
1867     dev->tx_queue_len        = NETIUCV_QUEUELEN_DEFAULT;
1868     dev->flags           = IFF_POINTOPOINT | IFF_NOARP;
1869     dev->netdev_ops      = &netiucv_netdev_ops;
1870 }
1871 
1872 /*
1873  * Allocate and initialize everything of a net device.
1874  */
1875 static struct net_device *netiucv_init_netdevice(char *username, char *userdata)
1876 {
1877     struct netiucv_priv *privptr;
1878     struct net_device *dev;
1879 
1880     dev = alloc_netdev(sizeof(struct netiucv_priv), "iucv%d",
1881                NET_NAME_UNKNOWN, netiucv_setup_netdevice);
1882     if (!dev)
1883         return NULL;
1884     rtnl_lock();
1885     if (dev_alloc_name(dev, dev->name) < 0)
1886         goto out_netdev;
1887 
1888     privptr = netdev_priv(dev);
1889     privptr->fsm = init_fsm("netiucvdev", dev_state_names,
1890                 dev_event_names, NR_DEV_STATES, NR_DEV_EVENTS,
1891                 dev_fsm, DEV_FSM_LEN, GFP_KERNEL);
1892     if (!privptr->fsm)
1893         goto out_netdev;
1894 
1895     privptr->conn = netiucv_new_connection(dev, username, userdata);
1896     if (!privptr->conn) {
1897         IUCV_DBF_TEXT(setup, 2, "NULL from netiucv_new_connection\n");
1898         goto out_fsm;
1899     }
1900     fsm_newstate(privptr->fsm, DEV_STATE_STOPPED);
1901     return dev;
1902 
1903 out_fsm:
1904     kfree_fsm(privptr->fsm);
1905 out_netdev:
1906     rtnl_unlock();
1907     free_netdev(dev);
1908     return NULL;
1909 }
1910 
1911 static ssize_t connection_store(struct device_driver *drv, const char *buf,
1912                 size_t count)
1913 {
1914     char username[9];
1915     char userdata[17];
1916     int rc;
1917     struct net_device *dev;
1918     struct netiucv_priv *priv;
1919     struct iucv_connection *cp;
1920 
1921     IUCV_DBF_TEXT(trace, 3, __func__);
1922     rc = netiucv_check_user(buf, count, username, userdata);
1923     if (rc)
1924         return rc;
1925 
1926     read_lock_bh(&iucv_connection_rwlock);
1927     list_for_each_entry(cp, &iucv_connection_list, list) {
1928         if (!strncmp(username, cp->userid, 9) &&
1929             !strncmp(userdata, cp->userdata, 17)) {
1930             read_unlock_bh(&iucv_connection_rwlock);
1931             IUCV_DBF_TEXT_(setup, 2, "conn_write: Connection to %s "
1932                 "already exists\n", netiucv_printuser(cp));
1933             return -EEXIST;
1934         }
1935     }
1936     read_unlock_bh(&iucv_connection_rwlock);
1937 
1938     dev = netiucv_init_netdevice(username, userdata);
1939     if (!dev) {
1940         IUCV_DBF_TEXT(setup, 2, "NULL from netiucv_init_netdevice\n");
1941         return -ENODEV;
1942     }
1943 
1944     rc = netiucv_register_device(dev);
1945     if (rc) {
1946         rtnl_unlock();
1947         IUCV_DBF_TEXT_(setup, 2,
1948             "ret %d from netiucv_register_device\n", rc);
1949         goto out_free_ndev;
1950     }
1951 
1952     /* sysfs magic */
1953     priv = netdev_priv(dev);
1954     SET_NETDEV_DEV(dev, priv->dev);
1955 
1956     rc = register_netdevice(dev);
1957     rtnl_unlock();
1958     if (rc)
1959         goto out_unreg;
1960 
1961     dev_info(priv->dev, "The IUCV interface to %s has been established "
1962                 "successfully\n",
1963         netiucv_printuser(priv->conn));
1964 
1965     return count;
1966 
1967 out_unreg:
1968     netiucv_unregister_device(priv->dev);
1969 out_free_ndev:
1970     netiucv_free_netdevice(dev);
1971     return rc;
1972 }
1973 static DRIVER_ATTR_WO(connection);
1974 
1975 static ssize_t remove_store(struct device_driver *drv, const char *buf,
1976                 size_t count)
1977 {
1978     struct iucv_connection *cp;
1979         struct net_device *ndev;
1980         struct netiucv_priv *priv;
1981         struct device *dev;
1982         char name[IFNAMSIZ];
1983     const char *p;
1984         int i;
1985 
1986     IUCV_DBF_TEXT(trace, 3, __func__);
1987 
1988         if (count >= IFNAMSIZ)
1989                 count = IFNAMSIZ - 1;
1990 
1991     for (i = 0, p = buf; i < count && *p; i++, p++) {
1992         if (*p == '\n' || *p == ' ')
1993                         /* trailing lf, grr */
1994                         break;
1995         name[i] = *p;
1996         }
1997         name[i] = '\0';
1998 
1999     read_lock_bh(&iucv_connection_rwlock);
2000     list_for_each_entry(cp, &iucv_connection_list, list) {
2001         ndev = cp->netdev;
2002         priv = netdev_priv(ndev);
2003                 dev = priv->dev;
2004         if (strncmp(name, ndev->name, count))
2005             continue;
2006         read_unlock_bh(&iucv_connection_rwlock);
2007                 if (ndev->flags & (IFF_UP | IFF_RUNNING)) {
2008             dev_warn(dev, "The IUCV device is connected"
2009                 " to %s and cannot be removed\n",
2010                 priv->conn->userid);
2011             IUCV_DBF_TEXT(data, 2, "remove_write: still active\n");
2012             return -EPERM;
2013                 }
2014                 unregister_netdev(ndev);
2015                 netiucv_unregister_device(dev);
2016                 return count;
2017         }
2018     read_unlock_bh(&iucv_connection_rwlock);
2019     IUCV_DBF_TEXT(data, 2, "remove_write: unknown device\n");
2020         return -EINVAL;
2021 }
2022 static DRIVER_ATTR_WO(remove);
2023 
2024 static struct attribute * netiucv_drv_attrs[] = {
2025     &driver_attr_connection.attr,
2026     &driver_attr_remove.attr,
2027     NULL,
2028 };
2029 
2030 static struct attribute_group netiucv_drv_attr_group = {
2031     .attrs = netiucv_drv_attrs,
2032 };
2033 
2034 static const struct attribute_group *netiucv_drv_attr_groups[] = {
2035     &netiucv_drv_attr_group,
2036     NULL,
2037 };
2038 
2039 static void netiucv_banner(void)
2040 {
2041     pr_info("driver initialized\n");
2042 }
2043 
2044 static void __exit netiucv_exit(void)
2045 {
2046     struct iucv_connection *cp;
2047     struct net_device *ndev;
2048     struct netiucv_priv *priv;
2049     struct device *dev;
2050 
2051     IUCV_DBF_TEXT(trace, 3, __func__);
2052     while (!list_empty(&iucv_connection_list)) {
2053         cp = list_entry(iucv_connection_list.next,
2054                 struct iucv_connection, list);
2055         ndev = cp->netdev;
2056         priv = netdev_priv(ndev);
2057         dev = priv->dev;
2058 
2059         unregister_netdev(ndev);
2060         netiucv_unregister_device(dev);
2061     }
2062 
2063     driver_unregister(&netiucv_driver);
2064     iucv_unregister(&netiucv_handler, 1);
2065     iucv_unregister_dbf_views();
2066 
2067     pr_info("driver unloaded\n");
2068     return;
2069 }
2070 
2071 static int __init netiucv_init(void)
2072 {
2073     int rc;
2074 
2075     rc = iucv_register_dbf_views();
2076     if (rc)
2077         goto out;
2078     rc = iucv_register(&netiucv_handler, 1);
2079     if (rc)
2080         goto out_dbf;
2081     IUCV_DBF_TEXT(trace, 3, __func__);
2082     netiucv_driver.groups = netiucv_drv_attr_groups;
2083     rc = driver_register(&netiucv_driver);
2084     if (rc) {
2085         IUCV_DBF_TEXT_(setup, 2, "ret %d from driver_register\n", rc);
2086         goto out_iucv;
2087     }
2088 
2089     netiucv_banner();
2090     return rc;
2091 
2092 out_iucv:
2093     iucv_unregister(&netiucv_handler, 1);
2094 out_dbf:
2095     iucv_unregister_dbf_views();
2096 out:
2097     return rc;
2098 }
2099 
2100 module_init(netiucv_init);
2101 module_exit(netiucv_exit);
2102 MODULE_LICENSE("GPL");