1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright IBM Corp. 2001, 2009
8 * Original netiucv driver:
9 * Fritz Elfert (elfert@de.ibm.com, felfert@millenux.com)
10 * Sysfs integration and all bugs therein:
11 * Cornelia Huck (cornelia.huck@de.ibm.com)
13 * Ursula Braun (ursula.braun@de.ibm.com)
16 * the source of the original IUCV driver by:
17 * Stefan Hegewald <hegewald@de.ibm.com>
18 * Hartmut Penner <hpenner@de.ibm.com>
19 * Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
20 * Martin Schwidefsky (schwidefsky@de.ibm.com)
21 * Alan Altmark (Alan_Altmark@us.ibm.com) Sept. 2000
24 #define KMSG_COMPONENT "netiucv"
25 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/kernel.h>
32 #include <linux/slab.h>
33 #include <linux/errno.h>
34 #include <linux/types.h>
35 #include <linux/interrupt.h>
36 #include <linux/timer.h>
37 #include <linux/bitops.h>
39 #include <linux/signal.h>
40 #include <linux/string.h>
41 #include <linux/device.h>
44 #include <linux/if_arp.h>
45 #include <linux/tcp.h>
46 #include <linux/skbuff.h>
47 #include <linux/ctype.h>
51 #include <linux/uaccess.h>
52 #include <asm/ebcdic.h>
54 #include <net/iucv/iucv.h>
58 ("(C) 2001 IBM Corporation by Fritz Elfert (felfert@millenux.com)");
59 MODULE_DESCRIPTION ("Linux for S/390 IUCV network driver");
62 * Debug Facility stuff
64 #define IUCV_DBF_SETUP_NAME "iucv_setup"
65 #define IUCV_DBF_SETUP_LEN 64
66 #define IUCV_DBF_SETUP_PAGES 2
67 #define IUCV_DBF_SETUP_NR_AREAS 1
68 #define IUCV_DBF_SETUP_LEVEL 3
70 #define IUCV_DBF_DATA_NAME "iucv_data"
71 #define IUCV_DBF_DATA_LEN 128
72 #define IUCV_DBF_DATA_PAGES 2
73 #define IUCV_DBF_DATA_NR_AREAS 1
74 #define IUCV_DBF_DATA_LEVEL 2
76 #define IUCV_DBF_TRACE_NAME "iucv_trace"
77 #define IUCV_DBF_TRACE_LEN 16
78 #define IUCV_DBF_TRACE_PAGES 4
79 #define IUCV_DBF_TRACE_NR_AREAS 1
80 #define IUCV_DBF_TRACE_LEVEL 3
82 #define IUCV_DBF_TEXT(name,level,text) \
84 debug_text_event(iucv_dbf_##name,level,text); \
87 #define IUCV_DBF_HEX(name,level,addr,len) \
89 debug_event(iucv_dbf_##name,level,(void*)(addr),len); \
92 DECLARE_PER_CPU(char[256], iucv_dbf_txt_buf);
94 #define IUCV_DBF_TEXT_(name, level, text...) \
96 if (debug_level_enabled(iucv_dbf_##name, level)) { \
97 char* __buf = get_cpu_var(iucv_dbf_txt_buf); \
98 sprintf(__buf, text); \
99 debug_text_event(iucv_dbf_##name, level, __buf); \
100 put_cpu_var(iucv_dbf_txt_buf); \
104 #define IUCV_DBF_SPRINTF(name,level,text...) \
106 debug_sprintf_event(iucv_dbf_trace, level, ##text ); \
107 debug_sprintf_event(iucv_dbf_trace, level, text ); \
111 * some more debug stuff
113 #define PRINTK_HEADER " iucv: " /* for debugging */
115 static struct device_driver netiucv_driver = {
116 .owner = THIS_MODULE,
121 static int netiucv_callback_connreq(struct iucv_path *, u8 *, u8 *);
122 static void netiucv_callback_connack(struct iucv_path *, u8 *);
123 static void netiucv_callback_connrej(struct iucv_path *, u8 *);
124 static void netiucv_callback_connsusp(struct iucv_path *, u8 *);
125 static void netiucv_callback_connres(struct iucv_path *, u8 *);
126 static void netiucv_callback_rx(struct iucv_path *, struct iucv_message *);
127 static void netiucv_callback_txdone(struct iucv_path *, struct iucv_message *);
129 static struct iucv_handler netiucv_handler = {
130 .path_pending = netiucv_callback_connreq,
131 .path_complete = netiucv_callback_connack,
132 .path_severed = netiucv_callback_connrej,
133 .path_quiesced = netiucv_callback_connsusp,
134 .path_resumed = netiucv_callback_connres,
135 .message_pending = netiucv_callback_rx,
136 .message_complete = netiucv_callback_txdone
140 * Per connection profiling data
142 struct connection_profile {
143 unsigned long maxmulti;
144 unsigned long maxcqueue;
145 unsigned long doios_single;
146 unsigned long doios_multi;
148 unsigned long tx_time;
149 unsigned long send_stamp;
150 unsigned long tx_pending;
151 unsigned long tx_max_pending;
155 * Representation of one iucv connection
157 struct iucv_connection {
158 struct list_head list;
159 struct iucv_path *path;
160 struct sk_buff *rx_buff;
161 struct sk_buff *tx_buff;
162 struct sk_buff_head collect_queue;
163 struct sk_buff_head commit_queue;
164 spinlock_t collect_lock;
169 struct net_device *netdev;
170 struct connection_profile prof;
176 * Linked list of all connection structs.
178 static LIST_HEAD(iucv_connection_list);
179 static DEFINE_RWLOCK(iucv_connection_rwlock);
182 * Representation of event-data for the
183 * connection state machine.
186 struct iucv_connection *conn;
191 * Private part of the network device structure
193 struct netiucv_priv {
194 struct net_device_stats stats;
197 struct iucv_connection *conn;
202 * Link level header for a packet.
208 #define NETIUCV_HDRLEN (sizeof(struct ll_header))
209 #define NETIUCV_BUFSIZE_MAX 65537
210 #define NETIUCV_BUFSIZE_DEFAULT NETIUCV_BUFSIZE_MAX
211 #define NETIUCV_MTU_MAX (NETIUCV_BUFSIZE_MAX - NETIUCV_HDRLEN)
212 #define NETIUCV_MTU_DEFAULT 9216
213 #define NETIUCV_QUEUELEN_DEFAULT 50
214 #define NETIUCV_TIMEOUT_5SEC 5000
217 * Compatibility macros for busy handling
218 * of network devices.
220 static void netiucv_clear_busy(struct net_device *dev)
222 struct netiucv_priv *priv = netdev_priv(dev);
223 clear_bit(0, &priv->tbusy);
224 netif_wake_queue(dev);
227 static int netiucv_test_and_set_busy(struct net_device *dev)
229 struct netiucv_priv *priv = netdev_priv(dev);
230 netif_stop_queue(dev);
231 return test_and_set_bit(0, &priv->tbusy);
234 static u8 iucvMagic_ascii[16] = {
235 0x30, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
236 0x30, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20
239 static u8 iucvMagic_ebcdic[16] = {
240 0xF0, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
241 0xF0, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40
245 * Convert an iucv userId to its printable
246 * form (strip whitespace at end).
248 * @param An iucv userId
250 * @returns The printable string (static data!!)
252 static char *netiucv_printname(char *name, int len)
256 memcpy(tmp, name, len);
258 while (*p && ((p - tmp) < len) && (!isspace(*p)))
264 static char *netiucv_printuser(struct iucv_connection *conn)
266 static char tmp_uid[9];
267 static char tmp_udat[17];
268 static char buf[100];
270 if (memcmp(conn->userdata, iucvMagic_ebcdic, 16)) {
273 memcpy(tmp_uid, netiucv_printname(conn->userid, 8), 8);
274 memcpy(tmp_udat, conn->userdata, 16);
275 EBCASC(tmp_udat, 16);
276 memcpy(tmp_udat, netiucv_printname(tmp_udat, 16), 16);
277 sprintf(buf, "%s.%s", tmp_uid, tmp_udat);
280 return netiucv_printname(conn->userid, 8);
284 * States of the interface statemachine.
292 * MUST be always the last element!!
297 static const char *dev_state_names[] = {
305 * Events of the interface statemachine.
313 * MUST be always the last element!!
318 static const char *dev_event_names[] = {
326 * Events of the connection statemachine
330 * Events, representing callbacks from
331 * lowlevel iucv layer)
342 * Events, representing errors return codes from
343 * calls to lowlevel iucv layer
347 * Event, representing timer expiry.
352 * Events, representing commands from upper levels.
358 * MUST be always the last element!!
363 static const char *conn_event_names[] = {
364 "Remote connection request",
365 "Remote connection acknowledge",
366 "Remote connection reject",
367 "Connection suspended",
368 "Connection resumed",
379 * States of the connection statemachine.
383 * Connection not assigned to any device,
384 * initial state, invalid
389 * Userid assigned but not operating
394 * Connection registered,
395 * no connection request sent yet,
396 * no connection request received
398 CONN_STATE_STARTWAIT,
401 * Connection registered and connection request sent,
402 * no acknowledge and no connection request received yet.
404 CONN_STATE_SETUPWAIT,
407 * Connection up and running idle
412 * Data sent, awaiting CONN_EVENT_TXDONE
417 * Error during registration.
422 * Error during registration.
427 * MUST be always the last element!!
432 static const char *conn_state_names[] = {
440 "Registration error",
446 * Debug Facility Stuff
448 static debug_info_t *iucv_dbf_setup = NULL;
449 static debug_info_t *iucv_dbf_data = NULL;
450 static debug_info_t *iucv_dbf_trace = NULL;
452 DEFINE_PER_CPU(char[256], iucv_dbf_txt_buf);
454 static void iucv_unregister_dbf_views(void)
456 debug_unregister(iucv_dbf_setup);
457 debug_unregister(iucv_dbf_data);
458 debug_unregister(iucv_dbf_trace);
460 static int iucv_register_dbf_views(void)
462 iucv_dbf_setup = debug_register(IUCV_DBF_SETUP_NAME,
463 IUCV_DBF_SETUP_PAGES,
464 IUCV_DBF_SETUP_NR_AREAS,
466 iucv_dbf_data = debug_register(IUCV_DBF_DATA_NAME,
468 IUCV_DBF_DATA_NR_AREAS,
470 iucv_dbf_trace = debug_register(IUCV_DBF_TRACE_NAME,
471 IUCV_DBF_TRACE_PAGES,
472 IUCV_DBF_TRACE_NR_AREAS,
475 if ((iucv_dbf_setup == NULL) || (iucv_dbf_data == NULL) ||
476 (iucv_dbf_trace == NULL)) {
477 iucv_unregister_dbf_views();
480 debug_register_view(iucv_dbf_setup, &debug_hex_ascii_view);
481 debug_set_level(iucv_dbf_setup, IUCV_DBF_SETUP_LEVEL);
483 debug_register_view(iucv_dbf_data, &debug_hex_ascii_view);
484 debug_set_level(iucv_dbf_data, IUCV_DBF_DATA_LEVEL);
486 debug_register_view(iucv_dbf_trace, &debug_hex_ascii_view);
487 debug_set_level(iucv_dbf_trace, IUCV_DBF_TRACE_LEVEL);
493 * Callback-wrappers, called from lowlevel iucv layer.
496 static void netiucv_callback_rx(struct iucv_path *path,
497 struct iucv_message *msg)
499 struct iucv_connection *conn = path->private;
500 struct iucv_event ev;
504 fsm_event(conn->fsm, CONN_EVENT_RX, &ev);
507 static void netiucv_callback_txdone(struct iucv_path *path,
508 struct iucv_message *msg)
510 struct iucv_connection *conn = path->private;
511 struct iucv_event ev;
515 fsm_event(conn->fsm, CONN_EVENT_TXDONE, &ev);
518 static void netiucv_callback_connack(struct iucv_path *path, u8 ipuser[16])
520 struct iucv_connection *conn = path->private;
522 fsm_event(conn->fsm, CONN_EVENT_CONN_ACK, conn);
525 static int netiucv_callback_connreq(struct iucv_path *path, u8 *ipvmid,
528 struct iucv_connection *conn = path->private;
529 struct iucv_event ev;
530 static char tmp_user[9];
531 static char tmp_udat[17];
535 memcpy(tmp_user, netiucv_printname(ipvmid, 8), 8);
536 memcpy(tmp_udat, ipuser, 16);
537 EBCASC(tmp_udat, 16);
538 read_lock_bh(&iucv_connection_rwlock);
539 list_for_each_entry(conn, &iucv_connection_list, list) {
540 if (strncmp(ipvmid, conn->userid, 8) ||
541 strncmp(ipuser, conn->userdata, 16))
543 /* Found a matching connection for this path. */
547 fsm_event(conn->fsm, CONN_EVENT_CONN_REQ, &ev);
550 IUCV_DBF_TEXT_(setup, 2, "Connection requested for %s.%s\n",
551 tmp_user, netiucv_printname(tmp_udat, 16));
552 read_unlock_bh(&iucv_connection_rwlock);
556 static void netiucv_callback_connrej(struct iucv_path *path, u8 *ipuser)
558 struct iucv_connection *conn = path->private;
560 fsm_event(conn->fsm, CONN_EVENT_CONN_REJ, conn);
563 static void netiucv_callback_connsusp(struct iucv_path *path, u8 *ipuser)
565 struct iucv_connection *conn = path->private;
567 fsm_event(conn->fsm, CONN_EVENT_CONN_SUS, conn);
570 static void netiucv_callback_connres(struct iucv_path *path, u8 *ipuser)
572 struct iucv_connection *conn = path->private;
574 fsm_event(conn->fsm, CONN_EVENT_CONN_RES, conn);
578 * NOP action for statemachines
580 static void netiucv_action_nop(fsm_instance *fi, int event, void *arg)
585 * Actions of the connection statemachine
590 * @conn: The connection where this skb has been received.
591 * @pskb: The received skb.
593 * Unpack a just received skb and hand it over to upper layers.
594 * Helper function for conn_action_rx.
596 static void netiucv_unpack_skb(struct iucv_connection *conn,
597 struct sk_buff *pskb)
599 struct net_device *dev = conn->netdev;
600 struct netiucv_priv *privptr = netdev_priv(dev);
603 skb_put(pskb, NETIUCV_HDRLEN);
605 pskb->ip_summed = CHECKSUM_NONE;
606 pskb->protocol = cpu_to_be16(ETH_P_IP);
610 struct ll_header *header = (struct ll_header *) pskb->data;
615 skb_pull(pskb, NETIUCV_HDRLEN);
616 header->next -= offset;
617 offset += header->next;
618 header->next -= NETIUCV_HDRLEN;
619 if (skb_tailroom(pskb) < header->next) {
620 IUCV_DBF_TEXT_(data, 2, "Illegal next field: %d > %d\n",
621 header->next, skb_tailroom(pskb));
624 skb_put(pskb, header->next);
625 skb_reset_mac_header(pskb);
626 skb = dev_alloc_skb(pskb->len);
628 IUCV_DBF_TEXT(data, 2,
629 "Out of memory in netiucv_unpack_skb\n");
630 privptr->stats.rx_dropped++;
633 skb_copy_from_linear_data(pskb, skb_put(skb, pskb->len),
635 skb_reset_mac_header(skb);
636 skb->dev = pskb->dev;
637 skb->protocol = pskb->protocol;
638 pskb->ip_summed = CHECKSUM_UNNECESSARY;
639 privptr->stats.rx_packets++;
640 privptr->stats.rx_bytes += skb->len;
642 * Since receiving is always initiated from a tasklet (in iucv.c),
643 * we must use netif_rx_ni() instead of netif_rx()
646 skb_pull(pskb, header->next);
647 skb_put(pskb, NETIUCV_HDRLEN);
651 static void conn_action_rx(fsm_instance *fi, int event, void *arg)
653 struct iucv_event *ev = arg;
654 struct iucv_connection *conn = ev->conn;
655 struct iucv_message *msg = ev->data;
656 struct netiucv_priv *privptr = netdev_priv(conn->netdev);
659 IUCV_DBF_TEXT(trace, 4, __func__);
662 iucv_message_reject(conn->path, msg);
663 IUCV_DBF_TEXT(data, 2,
664 "Received data for unlinked connection\n");
667 if (msg->length > conn->max_buffsize) {
668 iucv_message_reject(conn->path, msg);
669 privptr->stats.rx_dropped++;
670 IUCV_DBF_TEXT_(data, 2, "msglen %d > max_buffsize %d\n",
671 msg->length, conn->max_buffsize);
674 conn->rx_buff->data = conn->rx_buff->head;
675 skb_reset_tail_pointer(conn->rx_buff);
676 conn->rx_buff->len = 0;
677 rc = iucv_message_receive(conn->path, msg, 0, conn->rx_buff->data,
679 if (rc || msg->length < 5) {
680 privptr->stats.rx_errors++;
681 IUCV_DBF_TEXT_(data, 2, "rc %d from iucv_receive\n", rc);
684 netiucv_unpack_skb(conn, conn->rx_buff);
687 static void conn_action_txdone(fsm_instance *fi, int event, void *arg)
689 struct iucv_event *ev = arg;
690 struct iucv_connection *conn = ev->conn;
691 struct iucv_message *msg = ev->data;
692 struct iucv_message txmsg;
693 struct netiucv_priv *privptr = NULL;
694 u32 single_flag = msg->tag;
699 unsigned long saveflags;
700 struct ll_header header;
703 IUCV_DBF_TEXT(trace, 4, __func__);
705 if (!conn || !conn->netdev) {
706 IUCV_DBF_TEXT(data, 2,
707 "Send confirmation for unlinked connection\n");
710 privptr = netdev_priv(conn->netdev);
711 conn->prof.tx_pending--;
713 if ((skb = skb_dequeue(&conn->commit_queue))) {
714 refcount_dec(&skb->users);
716 privptr->stats.tx_packets++;
717 privptr->stats.tx_bytes +=
718 (skb->len - NETIUCV_HDRLEN
721 dev_kfree_skb_any(skb);
724 conn->tx_buff->data = conn->tx_buff->head;
725 skb_reset_tail_pointer(conn->tx_buff);
726 conn->tx_buff->len = 0;
727 spin_lock_irqsave(&conn->collect_lock, saveflags);
728 while ((skb = skb_dequeue(&conn->collect_queue))) {
729 header.next = conn->tx_buff->len + skb->len + NETIUCV_HDRLEN;
730 skb_put_data(conn->tx_buff, &header, NETIUCV_HDRLEN);
731 skb_copy_from_linear_data(skb,
732 skb_put(conn->tx_buff, skb->len),
737 refcount_dec(&skb->users);
738 dev_kfree_skb_any(skb);
740 if (conn->collect_len > conn->prof.maxmulti)
741 conn->prof.maxmulti = conn->collect_len;
742 conn->collect_len = 0;
743 spin_unlock_irqrestore(&conn->collect_lock, saveflags);
744 if (conn->tx_buff->len == 0) {
745 fsm_newstate(fi, CONN_STATE_IDLE);
750 skb_put_data(conn->tx_buff, &header, NETIUCV_HDRLEN);
751 conn->prof.send_stamp = jiffies;
754 rc = iucv_message_send(conn->path, &txmsg, 0, 0,
755 conn->tx_buff->data, conn->tx_buff->len);
756 conn->prof.doios_multi++;
757 conn->prof.txlen += conn->tx_buff->len;
758 conn->prof.tx_pending++;
759 if (conn->prof.tx_pending > conn->prof.tx_max_pending)
760 conn->prof.tx_max_pending = conn->prof.tx_pending;
762 conn->prof.tx_pending--;
763 fsm_newstate(fi, CONN_STATE_IDLE);
765 privptr->stats.tx_errors += txpackets;
766 IUCV_DBF_TEXT_(data, 2, "rc %d from iucv_send\n", rc);
769 privptr->stats.tx_packets += txpackets;
770 privptr->stats.tx_bytes += txbytes;
772 if (stat_maxcq > conn->prof.maxcqueue)
773 conn->prof.maxcqueue = stat_maxcq;
777 static void conn_action_connaccept(fsm_instance *fi, int event, void *arg)
779 struct iucv_event *ev = arg;
780 struct iucv_connection *conn = ev->conn;
781 struct iucv_path *path = ev->data;
782 struct net_device *netdev = conn->netdev;
783 struct netiucv_priv *privptr = netdev_priv(netdev);
786 IUCV_DBF_TEXT(trace, 3, __func__);
789 path->msglim = NETIUCV_QUEUELEN_DEFAULT;
791 rc = iucv_path_accept(path, &netiucv_handler, conn->userdata , conn);
793 IUCV_DBF_TEXT_(setup, 2, "rc %d from iucv_accept", rc);
796 fsm_newstate(fi, CONN_STATE_IDLE);
797 netdev->tx_queue_len = conn->path->msglim;
798 fsm_event(privptr->fsm, DEV_EVENT_CONUP, netdev);
801 static void conn_action_connreject(fsm_instance *fi, int event, void *arg)
803 struct iucv_event *ev = arg;
804 struct iucv_path *path = ev->data;
806 IUCV_DBF_TEXT(trace, 3, __func__);
807 iucv_path_sever(path, NULL);
810 static void conn_action_connack(fsm_instance *fi, int event, void *arg)
812 struct iucv_connection *conn = arg;
813 struct net_device *netdev = conn->netdev;
814 struct netiucv_priv *privptr = netdev_priv(netdev);
816 IUCV_DBF_TEXT(trace, 3, __func__);
817 fsm_deltimer(&conn->timer);
818 fsm_newstate(fi, CONN_STATE_IDLE);
819 netdev->tx_queue_len = conn->path->msglim;
820 fsm_event(privptr->fsm, DEV_EVENT_CONUP, netdev);
823 static void conn_action_conntimsev(fsm_instance *fi, int event, void *arg)
825 struct iucv_connection *conn = arg;
827 IUCV_DBF_TEXT(trace, 3, __func__);
828 fsm_deltimer(&conn->timer);
829 iucv_path_sever(conn->path, conn->userdata);
830 fsm_newstate(fi, CONN_STATE_STARTWAIT);
833 static void conn_action_connsever(fsm_instance *fi, int event, void *arg)
835 struct iucv_connection *conn = arg;
836 struct net_device *netdev = conn->netdev;
837 struct netiucv_priv *privptr = netdev_priv(netdev);
839 IUCV_DBF_TEXT(trace, 3, __func__);
841 fsm_deltimer(&conn->timer);
842 iucv_path_sever(conn->path, conn->userdata);
843 dev_info(privptr->dev, "The peer z/VM guest %s has closed the "
844 "connection\n", netiucv_printuser(conn));
845 IUCV_DBF_TEXT(data, 2,
846 "conn_action_connsever: Remote dropped connection\n");
847 fsm_newstate(fi, CONN_STATE_STARTWAIT);
848 fsm_event(privptr->fsm, DEV_EVENT_CONDOWN, netdev);
851 static void conn_action_start(fsm_instance *fi, int event, void *arg)
853 struct iucv_connection *conn = arg;
854 struct net_device *netdev = conn->netdev;
855 struct netiucv_priv *privptr = netdev_priv(netdev);
858 IUCV_DBF_TEXT(trace, 3, __func__);
860 fsm_newstate(fi, CONN_STATE_STARTWAIT);
863 * We must set the state before calling iucv_connect because the
864 * callback handler could be called at any point after the connection
868 fsm_newstate(fi, CONN_STATE_SETUPWAIT);
869 conn->path = iucv_path_alloc(NETIUCV_QUEUELEN_DEFAULT, 0, GFP_KERNEL);
870 IUCV_DBF_TEXT_(setup, 2, "%s: connecting to %s ...\n",
871 netdev->name, netiucv_printuser(conn));
873 rc = iucv_path_connect(conn->path, &netiucv_handler, conn->userid,
874 NULL, conn->userdata, conn);
877 netdev->tx_queue_len = conn->path->msglim;
878 fsm_addtimer(&conn->timer, NETIUCV_TIMEOUT_5SEC,
879 CONN_EVENT_TIMER, conn);
882 dev_warn(privptr->dev,
883 "The IUCV device failed to connect to z/VM guest %s\n",
884 netiucv_printname(conn->userid, 8));
885 fsm_newstate(fi, CONN_STATE_STARTWAIT);
888 dev_warn(privptr->dev,
889 "The IUCV device failed to connect to the peer on z/VM"
890 " guest %s\n", netiucv_printname(conn->userid, 8));
891 fsm_newstate(fi, CONN_STATE_STARTWAIT);
894 dev_err(privptr->dev,
895 "Connecting the IUCV device would exceed the maximum"
896 " number of IUCV connections\n");
897 fsm_newstate(fi, CONN_STATE_CONNERR);
900 dev_err(privptr->dev,
901 "z/VM guest %s has too many IUCV connections"
902 " to connect with the IUCV device\n",
903 netiucv_printname(conn->userid, 8));
904 fsm_newstate(fi, CONN_STATE_CONNERR);
907 dev_err(privptr->dev,
908 "The IUCV device cannot connect to a z/VM guest with no"
909 " IUCV authorization\n");
910 fsm_newstate(fi, CONN_STATE_CONNERR);
913 dev_err(privptr->dev,
914 "Connecting the IUCV device failed with error %d\n",
916 fsm_newstate(fi, CONN_STATE_CONNERR);
919 IUCV_DBF_TEXT_(setup, 5, "iucv_connect rc is %d\n", rc);
924 static void netiucv_purge_skb_queue(struct sk_buff_head *q)
928 while ((skb = skb_dequeue(q))) {
929 refcount_dec(&skb->users);
930 dev_kfree_skb_any(skb);
934 static void conn_action_stop(fsm_instance *fi, int event, void *arg)
936 struct iucv_event *ev = arg;
937 struct iucv_connection *conn = ev->conn;
938 struct net_device *netdev = conn->netdev;
939 struct netiucv_priv *privptr = netdev_priv(netdev);
941 IUCV_DBF_TEXT(trace, 3, __func__);
943 fsm_deltimer(&conn->timer);
944 fsm_newstate(fi, CONN_STATE_STOPPED);
945 netiucv_purge_skb_queue(&conn->collect_queue);
947 IUCV_DBF_TEXT(trace, 5, "calling iucv_path_sever\n");
948 iucv_path_sever(conn->path, conn->userdata);
952 netiucv_purge_skb_queue(&conn->commit_queue);
953 fsm_event(privptr->fsm, DEV_EVENT_CONDOWN, netdev);
956 static void conn_action_inval(fsm_instance *fi, int event, void *arg)
958 struct iucv_connection *conn = arg;
959 struct net_device *netdev = conn->netdev;
961 IUCV_DBF_TEXT_(data, 2, "%s('%s'): conn_action_inval called\n",
962 netdev->name, conn->userid);
965 static const fsm_node conn_fsm[] = {
966 { CONN_STATE_INVALID, CONN_EVENT_START, conn_action_inval },
967 { CONN_STATE_STOPPED, CONN_EVENT_START, conn_action_start },
969 { CONN_STATE_STOPPED, CONN_EVENT_STOP, conn_action_stop },
970 { CONN_STATE_STARTWAIT, CONN_EVENT_STOP, conn_action_stop },
971 { CONN_STATE_SETUPWAIT, CONN_EVENT_STOP, conn_action_stop },
972 { CONN_STATE_IDLE, CONN_EVENT_STOP, conn_action_stop },
973 { CONN_STATE_TX, CONN_EVENT_STOP, conn_action_stop },
974 { CONN_STATE_REGERR, CONN_EVENT_STOP, conn_action_stop },
975 { CONN_STATE_CONNERR, CONN_EVENT_STOP, conn_action_stop },
977 { CONN_STATE_STOPPED, CONN_EVENT_CONN_REQ, conn_action_connreject },
978 { CONN_STATE_STARTWAIT, CONN_EVENT_CONN_REQ, conn_action_connaccept },
979 { CONN_STATE_SETUPWAIT, CONN_EVENT_CONN_REQ, conn_action_connaccept },
980 { CONN_STATE_IDLE, CONN_EVENT_CONN_REQ, conn_action_connreject },
981 { CONN_STATE_TX, CONN_EVENT_CONN_REQ, conn_action_connreject },
983 { CONN_STATE_SETUPWAIT, CONN_EVENT_CONN_ACK, conn_action_connack },
984 { CONN_STATE_SETUPWAIT, CONN_EVENT_TIMER, conn_action_conntimsev },
986 { CONN_STATE_SETUPWAIT, CONN_EVENT_CONN_REJ, conn_action_connsever },
987 { CONN_STATE_IDLE, CONN_EVENT_CONN_REJ, conn_action_connsever },
988 { CONN_STATE_TX, CONN_EVENT_CONN_REJ, conn_action_connsever },
990 { CONN_STATE_IDLE, CONN_EVENT_RX, conn_action_rx },
991 { CONN_STATE_TX, CONN_EVENT_RX, conn_action_rx },
993 { CONN_STATE_TX, CONN_EVENT_TXDONE, conn_action_txdone },
994 { CONN_STATE_IDLE, CONN_EVENT_TXDONE, conn_action_txdone },
997 static const int CONN_FSM_LEN = sizeof(conn_fsm) / sizeof(fsm_node);
1001 * Actions for interface - statemachine.
1006 * @fi: An instance of an interface statemachine.
1007 * @event: The event, just happened.
1008 * @arg: Generic pointer, casted from struct net_device * upon call.
1010 * Startup connection by sending CONN_EVENT_START to it.
1012 static void dev_action_start(fsm_instance *fi, int event, void *arg)
1014 struct net_device *dev = arg;
1015 struct netiucv_priv *privptr = netdev_priv(dev);
1017 IUCV_DBF_TEXT(trace, 3, __func__);
1019 fsm_newstate(fi, DEV_STATE_STARTWAIT);
1020 fsm_event(privptr->conn->fsm, CONN_EVENT_START, privptr->conn);
1024 * Shutdown connection by sending CONN_EVENT_STOP to it.
1026 * @param fi An instance of an interface statemachine.
1027 * @param event The event, just happened.
1028 * @param arg Generic pointer, casted from struct net_device * upon call.
1031 dev_action_stop(fsm_instance *fi, int event, void *arg)
1033 struct net_device *dev = arg;
1034 struct netiucv_priv *privptr = netdev_priv(dev);
1035 struct iucv_event ev;
1037 IUCV_DBF_TEXT(trace, 3, __func__);
1039 ev.conn = privptr->conn;
1041 fsm_newstate(fi, DEV_STATE_STOPWAIT);
1042 fsm_event(privptr->conn->fsm, CONN_EVENT_STOP, &ev);
1046 * Called from connection statemachine
1047 * when a connection is up and running.
1049 * @param fi An instance of an interface statemachine.
1050 * @param event The event, just happened.
1051 * @param arg Generic pointer, casted from struct net_device * upon call.
1054 dev_action_connup(fsm_instance *fi, int event, void *arg)
1056 struct net_device *dev = arg;
1057 struct netiucv_priv *privptr = netdev_priv(dev);
1059 IUCV_DBF_TEXT(trace, 3, __func__);
1061 switch (fsm_getstate(fi)) {
1062 case DEV_STATE_STARTWAIT:
1063 fsm_newstate(fi, DEV_STATE_RUNNING);
1064 dev_info(privptr->dev,
1065 "The IUCV device has been connected"
1066 " successfully to %s\n",
1067 netiucv_printuser(privptr->conn));
1068 IUCV_DBF_TEXT(setup, 3,
1069 "connection is up and running\n");
1071 case DEV_STATE_STOPWAIT:
1072 IUCV_DBF_TEXT(data, 2,
1073 "dev_action_connup: in DEV_STATE_STOPWAIT\n");
1079 * Called from connection statemachine
1080 * when a connection has been shutdown.
1082 * @param fi An instance of an interface statemachine.
1083 * @param event The event, just happened.
1084 * @param arg Generic pointer, casted from struct net_device * upon call.
1087 dev_action_conndown(fsm_instance *fi, int event, void *arg)
1089 IUCV_DBF_TEXT(trace, 3, __func__);
1091 switch (fsm_getstate(fi)) {
1092 case DEV_STATE_RUNNING:
1093 fsm_newstate(fi, DEV_STATE_STARTWAIT);
1095 case DEV_STATE_STOPWAIT:
1096 fsm_newstate(fi, DEV_STATE_STOPPED);
1097 IUCV_DBF_TEXT(setup, 3, "connection is down\n");
1102 static const fsm_node dev_fsm[] = {
1103 { DEV_STATE_STOPPED, DEV_EVENT_START, dev_action_start },
1105 { DEV_STATE_STOPWAIT, DEV_EVENT_START, dev_action_start },
1106 { DEV_STATE_STOPWAIT, DEV_EVENT_CONDOWN, dev_action_conndown },
1108 { DEV_STATE_STARTWAIT, DEV_EVENT_STOP, dev_action_stop },
1109 { DEV_STATE_STARTWAIT, DEV_EVENT_CONUP, dev_action_connup },
1111 { DEV_STATE_RUNNING, DEV_EVENT_STOP, dev_action_stop },
1112 { DEV_STATE_RUNNING, DEV_EVENT_CONDOWN, dev_action_conndown },
1113 { DEV_STATE_RUNNING, DEV_EVENT_CONUP, netiucv_action_nop },
1116 static const int DEV_FSM_LEN = sizeof(dev_fsm) / sizeof(fsm_node);
1119 * Transmit a packet.
1120 * This is a helper function for netiucv_tx().
1122 * @param conn Connection to be used for sending.
1123 * @param skb Pointer to struct sk_buff of packet to send.
1124 * The linklevel header has already been set up
1127 * @return 0 on success, -ERRNO on failure. (Never fails.)
1129 static int netiucv_transmit_skb(struct iucv_connection *conn,
1130 struct sk_buff *skb)
1132 struct iucv_message msg;
1133 unsigned long saveflags;
1134 struct ll_header header;
1137 if (fsm_getstate(conn->fsm) != CONN_STATE_IDLE) {
1138 int l = skb->len + NETIUCV_HDRLEN;
1140 spin_lock_irqsave(&conn->collect_lock, saveflags);
1141 if (conn->collect_len + l >
1142 (conn->max_buffsize - NETIUCV_HDRLEN)) {
1144 IUCV_DBF_TEXT(data, 2,
1145 "EBUSY from netiucv_transmit_skb\n");
1147 refcount_inc(&skb->users);
1148 skb_queue_tail(&conn->collect_queue, skb);
1149 conn->collect_len += l;
1152 spin_unlock_irqrestore(&conn->collect_lock, saveflags);
1154 struct sk_buff *nskb = skb;
1156 * Copy the skb to a new allocated skb in lowmem only if the
1157 * data is located above 2G in memory or tailroom is < 2.
1159 unsigned long hi = ((unsigned long)(skb_tail_pointer(skb) +
1160 NETIUCV_HDRLEN)) >> 31;
1162 if (hi || (skb_tailroom(skb) < 2)) {
1163 nskb = alloc_skb(skb->len + NETIUCV_HDRLEN +
1164 NETIUCV_HDRLEN, GFP_ATOMIC | GFP_DMA);
1166 IUCV_DBF_TEXT(data, 2, "alloc_skb failed\n");
1170 skb_reserve(nskb, NETIUCV_HDRLEN);
1171 skb_put_data(nskb, skb->data, skb->len);
1176 * skb now is below 2G and has enough room. Add headers.
1178 header.next = nskb->len + NETIUCV_HDRLEN;
1179 memcpy(skb_push(nskb, NETIUCV_HDRLEN), &header, NETIUCV_HDRLEN);
1181 skb_put_data(nskb, &header, NETIUCV_HDRLEN);
1183 fsm_newstate(conn->fsm, CONN_STATE_TX);
1184 conn->prof.send_stamp = jiffies;
1188 rc = iucv_message_send(conn->path, &msg, 0, 0,
1189 nskb->data, nskb->len);
1190 conn->prof.doios_single++;
1191 conn->prof.txlen += skb->len;
1192 conn->prof.tx_pending++;
1193 if (conn->prof.tx_pending > conn->prof.tx_max_pending)
1194 conn->prof.tx_max_pending = conn->prof.tx_pending;
1196 struct netiucv_priv *privptr;
1197 fsm_newstate(conn->fsm, CONN_STATE_IDLE);
1198 conn->prof.tx_pending--;
1199 privptr = netdev_priv(conn->netdev);
1201 privptr->stats.tx_errors++;
1203 dev_kfree_skb(nskb);
1206 * Remove our headers. They get added
1207 * again on retransmit.
1209 skb_pull(skb, NETIUCV_HDRLEN);
1210 skb_trim(skb, skb->len - NETIUCV_HDRLEN);
1212 IUCV_DBF_TEXT_(data, 2, "rc %d from iucv_send\n", rc);
1216 refcount_inc(&nskb->users);
1217 skb_queue_tail(&conn->commit_queue, nskb);
1225 * Interface API for upper network layers
1229 * Open an interface.
1230 * Called from generic network layer when ifconfig up is run.
1232 * @param dev Pointer to interface struct.
1234 * @return 0 on success, -ERRNO on failure. (Never fails.)
1236 static int netiucv_open(struct net_device *dev)
1238 struct netiucv_priv *priv = netdev_priv(dev);
1240 fsm_event(priv->fsm, DEV_EVENT_START, dev);
1245 * Close an interface.
1246 * Called from generic network layer when ifconfig down is run.
1248 * @param dev Pointer to interface struct.
1250 * @return 0 on success, -ERRNO on failure. (Never fails.)
1252 static int netiucv_close(struct net_device *dev)
1254 struct netiucv_priv *priv = netdev_priv(dev);
1256 fsm_event(priv->fsm, DEV_EVENT_STOP, dev);
1261 * Start transmission of a packet.
1262 * Called from generic network device layer.
1264 static netdev_tx_t netiucv_tx(struct sk_buff *skb, struct net_device *dev)
1266 struct netiucv_priv *privptr = netdev_priv(dev);
1269 IUCV_DBF_TEXT(trace, 4, __func__);
1271 * Some sanity checks ...
1274 IUCV_DBF_TEXT(data, 2, "netiucv_tx: skb is NULL\n");
1275 privptr->stats.tx_dropped++;
1276 return NETDEV_TX_OK;
1278 if (skb_headroom(skb) < NETIUCV_HDRLEN) {
1279 IUCV_DBF_TEXT(data, 2,
1280 "netiucv_tx: skb_headroom < NETIUCV_HDRLEN\n");
1282 privptr->stats.tx_dropped++;
1283 return NETDEV_TX_OK;
1287 * If connection is not running, try to restart it
1288 * and throw away packet.
1290 if (fsm_getstate(privptr->fsm) != DEV_STATE_RUNNING) {
1292 privptr->stats.tx_dropped++;
1293 privptr->stats.tx_errors++;
1294 privptr->stats.tx_carrier_errors++;
1295 return NETDEV_TX_OK;
1298 if (netiucv_test_and_set_busy(dev)) {
1299 IUCV_DBF_TEXT(data, 2, "EBUSY from netiucv_tx\n");
1300 return NETDEV_TX_BUSY;
1302 netif_trans_update(dev);
1303 rc = netiucv_transmit_skb(privptr->conn, skb);
1304 netiucv_clear_busy(dev);
1305 return rc ? NETDEV_TX_BUSY : NETDEV_TX_OK;
1310 * @dev: Pointer to interface struct.
1312 * Returns interface statistics of a device.
1314 * Returns pointer to stats struct of this interface.
1316 static struct net_device_stats *netiucv_stats (struct net_device * dev)
1318 struct netiucv_priv *priv = netdev_priv(dev);
1320 IUCV_DBF_TEXT(trace, 5, __func__);
1321 return &priv->stats;
1325 * attributes in sysfs
1328 static ssize_t user_show(struct device *dev, struct device_attribute *attr,
1331 struct netiucv_priv *priv = dev_get_drvdata(dev);
1333 IUCV_DBF_TEXT(trace, 5, __func__);
1334 return sprintf(buf, "%s\n", netiucv_printuser(priv->conn));
1337 static int netiucv_check_user(const char *buf, size_t count, char *username,
1343 p = strchr(buf, '.');
1344 if ((p && ((count > 26) ||
1346 (buf + count - p > 18))) ||
1347 (!p && (count > 9))) {
1348 IUCV_DBF_TEXT(setup, 2, "conn_write: too long\n");
1352 for (i = 0, p = buf; i < 8 && *p && *p != '.'; i++, p++) {
1353 if (isalnum(*p) || *p == '$') {
1354 username[i] = toupper(*p);
1358 /* trailing lf, grr */
1360 IUCV_DBF_TEXT_(setup, 2,
1361 "conn_write: invalid character %02x\n", *p);
1365 username[i++] = ' ';
1370 for (i = 0; i < 16 && *p; i++, p++) {
1373 userdata[i] = toupper(*p);
1375 while (i > 0 && i < 16)
1376 userdata[i++] = ' ';
1378 memcpy(userdata, iucvMagic_ascii, 16);
1379 userdata[16] = '\0';
1380 ASCEBC(userdata, 16);
1385 static ssize_t user_write(struct device *dev, struct device_attribute *attr,
1386 const char *buf, size_t count)
1388 struct netiucv_priv *priv = dev_get_drvdata(dev);
1389 struct net_device *ndev = priv->conn->netdev;
1393 struct iucv_connection *cp;
1395 IUCV_DBF_TEXT(trace, 3, __func__);
1396 rc = netiucv_check_user(buf, count, username, userdata);
1400 if (memcmp(username, priv->conn->userid, 9) &&
1401 (ndev->flags & (IFF_UP | IFF_RUNNING))) {
1402 /* username changed while the interface is active. */
1403 IUCV_DBF_TEXT(setup, 2, "user_write: device active\n");
1406 read_lock_bh(&iucv_connection_rwlock);
1407 list_for_each_entry(cp, &iucv_connection_list, list) {
1408 if (!strncmp(username, cp->userid, 9) &&
1409 !strncmp(userdata, cp->userdata, 17) && cp->netdev != ndev) {
1410 read_unlock_bh(&iucv_connection_rwlock);
1411 IUCV_DBF_TEXT_(setup, 2, "user_write: Connection to %s "
1412 "already exists\n", netiucv_printuser(cp));
1416 read_unlock_bh(&iucv_connection_rwlock);
1417 memcpy(priv->conn->userid, username, 9);
1418 memcpy(priv->conn->userdata, userdata, 17);
1422 static DEVICE_ATTR(user, 0644, user_show, user_write);
1424 static ssize_t buffer_show (struct device *dev, struct device_attribute *attr,
1427 struct netiucv_priv *priv = dev_get_drvdata(dev);
1429 IUCV_DBF_TEXT(trace, 5, __func__);
1430 return sprintf(buf, "%d\n", priv->conn->max_buffsize);
1433 static ssize_t buffer_write (struct device *dev, struct device_attribute *attr,
1434 const char *buf, size_t count)
1436 struct netiucv_priv *priv = dev_get_drvdata(dev);
1437 struct net_device *ndev = priv->conn->netdev;
1441 IUCV_DBF_TEXT(trace, 3, __func__);
1445 rc = kstrtouint(buf, 0, &bs1);
1447 if (rc == -EINVAL) {
1448 IUCV_DBF_TEXT_(setup, 2, "buffer_write: invalid char %s\n",
1452 if ((rc == -ERANGE) || (bs1 > NETIUCV_BUFSIZE_MAX)) {
1453 IUCV_DBF_TEXT_(setup, 2,
1454 "buffer_write: buffer size %d too large\n",
1458 if ((ndev->flags & IFF_RUNNING) &&
1459 (bs1 < (ndev->mtu + NETIUCV_HDRLEN + 2))) {
1460 IUCV_DBF_TEXT_(setup, 2,
1461 "buffer_write: buffer size %d too small\n",
1465 if (bs1 < (576 + NETIUCV_HDRLEN + NETIUCV_HDRLEN)) {
1466 IUCV_DBF_TEXT_(setup, 2,
1467 "buffer_write: buffer size %d too small\n",
1472 priv->conn->max_buffsize = bs1;
1473 if (!(ndev->flags & IFF_RUNNING))
1474 ndev->mtu = bs1 - NETIUCV_HDRLEN - NETIUCV_HDRLEN;
1480 static DEVICE_ATTR(buffer, 0644, buffer_show, buffer_write);
1482 static ssize_t dev_fsm_show (struct device *dev, struct device_attribute *attr,
1485 struct netiucv_priv *priv = dev_get_drvdata(dev);
1487 IUCV_DBF_TEXT(trace, 5, __func__);
1488 return sprintf(buf, "%s\n", fsm_getstate_str(priv->fsm));
1491 static DEVICE_ATTR(device_fsm_state, 0444, dev_fsm_show, NULL);
1493 static ssize_t conn_fsm_show (struct device *dev,
1494 struct device_attribute *attr, char *buf)
1496 struct netiucv_priv *priv = dev_get_drvdata(dev);
1498 IUCV_DBF_TEXT(trace, 5, __func__);
1499 return sprintf(buf, "%s\n", fsm_getstate_str(priv->conn->fsm));
1502 static DEVICE_ATTR(connection_fsm_state, 0444, conn_fsm_show, NULL);
1504 static ssize_t maxmulti_show (struct device *dev,
1505 struct device_attribute *attr, char *buf)
1507 struct netiucv_priv *priv = dev_get_drvdata(dev);
1509 IUCV_DBF_TEXT(trace, 5, __func__);
1510 return sprintf(buf, "%ld\n", priv->conn->prof.maxmulti);
1513 static ssize_t maxmulti_write (struct device *dev,
1514 struct device_attribute *attr,
1515 const char *buf, size_t count)
1517 struct netiucv_priv *priv = dev_get_drvdata(dev);
1519 IUCV_DBF_TEXT(trace, 4, __func__);
1520 priv->conn->prof.maxmulti = 0;
1524 static DEVICE_ATTR(max_tx_buffer_used, 0644, maxmulti_show, maxmulti_write);
1526 static ssize_t maxcq_show (struct device *dev, struct device_attribute *attr,
1529 struct netiucv_priv *priv = dev_get_drvdata(dev);
1531 IUCV_DBF_TEXT(trace, 5, __func__);
1532 return sprintf(buf, "%ld\n", priv->conn->prof.maxcqueue);
1535 static ssize_t maxcq_write (struct device *dev, struct device_attribute *attr,
1536 const char *buf, size_t count)
1538 struct netiucv_priv *priv = dev_get_drvdata(dev);
1540 IUCV_DBF_TEXT(trace, 4, __func__);
1541 priv->conn->prof.maxcqueue = 0;
1545 static DEVICE_ATTR(max_chained_skbs, 0644, maxcq_show, maxcq_write);
1547 static ssize_t sdoio_show (struct device *dev, struct device_attribute *attr,
1550 struct netiucv_priv *priv = dev_get_drvdata(dev);
1552 IUCV_DBF_TEXT(trace, 5, __func__);
1553 return sprintf(buf, "%ld\n", priv->conn->prof.doios_single);
1556 static ssize_t sdoio_write (struct device *dev, struct device_attribute *attr,
1557 const char *buf, size_t count)
1559 struct netiucv_priv *priv = dev_get_drvdata(dev);
1561 IUCV_DBF_TEXT(trace, 4, __func__);
1562 priv->conn->prof.doios_single = 0;
1566 static DEVICE_ATTR(tx_single_write_ops, 0644, sdoio_show, sdoio_write);
1568 static ssize_t mdoio_show (struct device *dev, struct device_attribute *attr,
1571 struct netiucv_priv *priv = dev_get_drvdata(dev);
1573 IUCV_DBF_TEXT(trace, 5, __func__);
1574 return sprintf(buf, "%ld\n", priv->conn->prof.doios_multi);
1577 static ssize_t mdoio_write (struct device *dev, struct device_attribute *attr,
1578 const char *buf, size_t count)
1580 struct netiucv_priv *priv = dev_get_drvdata(dev);
1582 IUCV_DBF_TEXT(trace, 5, __func__);
1583 priv->conn->prof.doios_multi = 0;
1587 static DEVICE_ATTR(tx_multi_write_ops, 0644, mdoio_show, mdoio_write);
1589 static ssize_t txlen_show (struct device *dev, struct device_attribute *attr,
1592 struct netiucv_priv *priv = dev_get_drvdata(dev);
1594 IUCV_DBF_TEXT(trace, 5, __func__);
1595 return sprintf(buf, "%ld\n", priv->conn->prof.txlen);
1598 static ssize_t txlen_write (struct device *dev, struct device_attribute *attr,
1599 const char *buf, size_t count)
1601 struct netiucv_priv *priv = dev_get_drvdata(dev);
1603 IUCV_DBF_TEXT(trace, 4, __func__);
1604 priv->conn->prof.txlen = 0;
1608 static DEVICE_ATTR(netto_bytes, 0644, txlen_show, txlen_write);
1610 static ssize_t txtime_show (struct device *dev, struct device_attribute *attr,
1613 struct netiucv_priv *priv = dev_get_drvdata(dev);
1615 IUCV_DBF_TEXT(trace, 5, __func__);
1616 return sprintf(buf, "%ld\n", priv->conn->prof.tx_time);
1619 static ssize_t txtime_write (struct device *dev, struct device_attribute *attr,
1620 const char *buf, size_t count)
1622 struct netiucv_priv *priv = dev_get_drvdata(dev);
1624 IUCV_DBF_TEXT(trace, 4, __func__);
1625 priv->conn->prof.tx_time = 0;
1629 static DEVICE_ATTR(max_tx_io_time, 0644, txtime_show, txtime_write);
1631 static ssize_t txpend_show (struct device *dev, struct device_attribute *attr,
1634 struct netiucv_priv *priv = dev_get_drvdata(dev);
1636 IUCV_DBF_TEXT(trace, 5, __func__);
1637 return sprintf(buf, "%ld\n", priv->conn->prof.tx_pending);
1640 static ssize_t txpend_write (struct device *dev, struct device_attribute *attr,
1641 const char *buf, size_t count)
1643 struct netiucv_priv *priv = dev_get_drvdata(dev);
1645 IUCV_DBF_TEXT(trace, 4, __func__);
1646 priv->conn->prof.tx_pending = 0;
1650 static DEVICE_ATTR(tx_pending, 0644, txpend_show, txpend_write);
1652 static ssize_t txmpnd_show (struct device *dev, struct device_attribute *attr,
1655 struct netiucv_priv *priv = dev_get_drvdata(dev);
1657 IUCV_DBF_TEXT(trace, 5, __func__);
1658 return sprintf(buf, "%ld\n", priv->conn->prof.tx_max_pending);
1661 static ssize_t txmpnd_write (struct device *dev, struct device_attribute *attr,
1662 const char *buf, size_t count)
1664 struct netiucv_priv *priv = dev_get_drvdata(dev);
1666 IUCV_DBF_TEXT(trace, 4, __func__);
1667 priv->conn->prof.tx_max_pending = 0;
1671 static DEVICE_ATTR(tx_max_pending, 0644, txmpnd_show, txmpnd_write);
1673 static struct attribute *netiucv_attrs[] = {
1674 &dev_attr_buffer.attr,
1675 &dev_attr_user.attr,
1679 static struct attribute_group netiucv_attr_group = {
1680 .attrs = netiucv_attrs,
1683 static struct attribute *netiucv_stat_attrs[] = {
1684 &dev_attr_device_fsm_state.attr,
1685 &dev_attr_connection_fsm_state.attr,
1686 &dev_attr_max_tx_buffer_used.attr,
1687 &dev_attr_max_chained_skbs.attr,
1688 &dev_attr_tx_single_write_ops.attr,
1689 &dev_attr_tx_multi_write_ops.attr,
1690 &dev_attr_netto_bytes.attr,
1691 &dev_attr_max_tx_io_time.attr,
1692 &dev_attr_tx_pending.attr,
1693 &dev_attr_tx_max_pending.attr,
1697 static struct attribute_group netiucv_stat_attr_group = {
1699 .attrs = netiucv_stat_attrs,
1702 static const struct attribute_group *netiucv_attr_groups[] = {
1703 &netiucv_stat_attr_group,
1704 &netiucv_attr_group,
1708 static int netiucv_register_device(struct net_device *ndev)
1710 struct netiucv_priv *priv = netdev_priv(ndev);
1711 struct device *dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1714 IUCV_DBF_TEXT(trace, 3, __func__);
1717 dev_set_name(dev, "net%s", ndev->name);
1718 dev->bus = &iucv_bus;
1719 dev->parent = iucv_root;
1720 dev->groups = netiucv_attr_groups;
1722 * The release function could be called after the
1723 * module has been unloaded. It's _only_ task is to
1724 * free the struct. Therefore, we specify kfree()
1725 * directly here. (Probably a little bit obfuscating
1726 * but legitime ...).
1728 dev->release = (void (*)(struct device *))kfree;
1729 dev->driver = &netiucv_driver;
1733 ret = device_register(dev);
1739 dev_set_drvdata(dev, priv);
1743 static void netiucv_unregister_device(struct device *dev)
1745 IUCV_DBF_TEXT(trace, 3, __func__);
1746 device_unregister(dev);
1750 * Allocate and initialize a new connection structure.
1751 * Add it to the list of netiucv connections;
1753 static struct iucv_connection *netiucv_new_connection(struct net_device *dev,
1757 struct iucv_connection *conn;
1759 conn = kzalloc(sizeof(*conn), GFP_KERNEL);
1762 skb_queue_head_init(&conn->collect_queue);
1763 skb_queue_head_init(&conn->commit_queue);
1764 spin_lock_init(&conn->collect_lock);
1765 conn->max_buffsize = NETIUCV_BUFSIZE_DEFAULT;
1768 conn->rx_buff = alloc_skb(conn->max_buffsize, GFP_KERNEL | GFP_DMA);
1771 conn->tx_buff = alloc_skb(conn->max_buffsize, GFP_KERNEL | GFP_DMA);
1774 conn->fsm = init_fsm("netiucvconn", conn_state_names,
1775 conn_event_names, NR_CONN_STATES,
1776 NR_CONN_EVENTS, conn_fsm, CONN_FSM_LEN,
1781 fsm_settimer(conn->fsm, &conn->timer);
1782 fsm_newstate(conn->fsm, CONN_STATE_INVALID);
1785 memcpy(conn->userdata, userdata, 17);
1787 memcpy(conn->userid, username, 9);
1788 fsm_newstate(conn->fsm, CONN_STATE_STOPPED);
1791 write_lock_bh(&iucv_connection_rwlock);
1792 list_add_tail(&conn->list, &iucv_connection_list);
1793 write_unlock_bh(&iucv_connection_rwlock);
1797 kfree_skb(conn->tx_buff);
1799 kfree_skb(conn->rx_buff);
1807 * Release a connection structure and remove it from the
1808 * list of netiucv connections.
1810 static void netiucv_remove_connection(struct iucv_connection *conn)
1813 IUCV_DBF_TEXT(trace, 3, __func__);
1814 write_lock_bh(&iucv_connection_rwlock);
1815 list_del_init(&conn->list);
1816 write_unlock_bh(&iucv_connection_rwlock);
1817 fsm_deltimer(&conn->timer);
1818 netiucv_purge_skb_queue(&conn->collect_queue);
1820 iucv_path_sever(conn->path, conn->userdata);
1824 netiucv_purge_skb_queue(&conn->commit_queue);
1825 kfree_fsm(conn->fsm);
1826 kfree_skb(conn->rx_buff);
1827 kfree_skb(conn->tx_buff);
1831 * Release everything of a net device.
1833 static void netiucv_free_netdevice(struct net_device *dev)
1835 struct netiucv_priv *privptr = netdev_priv(dev);
1837 IUCV_DBF_TEXT(trace, 3, __func__);
1844 netiucv_remove_connection(privptr->conn);
1846 kfree_fsm(privptr->fsm);
1847 privptr->conn = NULL; privptr->fsm = NULL;
1848 /* privptr gets freed by free_netdev() */
1853 * Initialize a net device. (Called from kernel in alloc_netdev())
1855 static const struct net_device_ops netiucv_netdev_ops = {
1856 .ndo_open = netiucv_open,
1857 .ndo_stop = netiucv_close,
1858 .ndo_get_stats = netiucv_stats,
1859 .ndo_start_xmit = netiucv_tx,
1862 static void netiucv_setup_netdevice(struct net_device *dev)
1864 dev->mtu = NETIUCV_MTU_DEFAULT;
1866 dev->max_mtu = NETIUCV_MTU_MAX;
1867 dev->needs_free_netdev = true;
1868 dev->priv_destructor = netiucv_free_netdevice;
1869 dev->hard_header_len = NETIUCV_HDRLEN;
1871 dev->type = ARPHRD_SLIP;
1872 dev->tx_queue_len = NETIUCV_QUEUELEN_DEFAULT;
1873 dev->flags = IFF_POINTOPOINT | IFF_NOARP;
1874 dev->netdev_ops = &netiucv_netdev_ops;
1878 * Allocate and initialize everything of a net device.
1880 static struct net_device *netiucv_init_netdevice(char *username, char *userdata)
1882 struct netiucv_priv *privptr;
1883 struct net_device *dev;
1885 dev = alloc_netdev(sizeof(struct netiucv_priv), "iucv%d",
1886 NET_NAME_UNKNOWN, netiucv_setup_netdevice);
1890 if (dev_alloc_name(dev, dev->name) < 0)
1893 privptr = netdev_priv(dev);
1894 privptr->fsm = init_fsm("netiucvdev", dev_state_names,
1895 dev_event_names, NR_DEV_STATES, NR_DEV_EVENTS,
1896 dev_fsm, DEV_FSM_LEN, GFP_KERNEL);
1900 privptr->conn = netiucv_new_connection(dev, username, userdata);
1901 if (!privptr->conn) {
1902 IUCV_DBF_TEXT(setup, 2, "NULL from netiucv_new_connection\n");
1905 fsm_newstate(privptr->fsm, DEV_STATE_STOPPED);
1909 kfree_fsm(privptr->fsm);
1916 static ssize_t connection_store(struct device_driver *drv, const char *buf,
1922 struct net_device *dev;
1923 struct netiucv_priv *priv;
1924 struct iucv_connection *cp;
1926 IUCV_DBF_TEXT(trace, 3, __func__);
1927 rc = netiucv_check_user(buf, count, username, userdata);
1931 read_lock_bh(&iucv_connection_rwlock);
1932 list_for_each_entry(cp, &iucv_connection_list, list) {
1933 if (!strncmp(username, cp->userid, 9) &&
1934 !strncmp(userdata, cp->userdata, 17)) {
1935 read_unlock_bh(&iucv_connection_rwlock);
1936 IUCV_DBF_TEXT_(setup, 2, "conn_write: Connection to %s "
1937 "already exists\n", netiucv_printuser(cp));
1941 read_unlock_bh(&iucv_connection_rwlock);
1943 dev = netiucv_init_netdevice(username, userdata);
1945 IUCV_DBF_TEXT(setup, 2, "NULL from netiucv_init_netdevice\n");
1949 rc = netiucv_register_device(dev);
1952 IUCV_DBF_TEXT_(setup, 2,
1953 "ret %d from netiucv_register_device\n", rc);
1958 priv = netdev_priv(dev);
1959 SET_NETDEV_DEV(dev, priv->dev);
1961 rc = register_netdevice(dev);
1966 dev_info(priv->dev, "The IUCV interface to %s has been established "
1968 netiucv_printuser(priv->conn));
1973 netiucv_unregister_device(priv->dev);
1975 netiucv_free_netdevice(dev);
1978 static DRIVER_ATTR_WO(connection);
1980 static ssize_t remove_store(struct device_driver *drv, const char *buf,
1983 struct iucv_connection *cp;
1984 struct net_device *ndev;
1985 struct netiucv_priv *priv;
1987 char name[IFNAMSIZ];
1991 IUCV_DBF_TEXT(trace, 3, __func__);
1993 if (count >= IFNAMSIZ)
1994 count = IFNAMSIZ - 1;
1996 for (i = 0, p = buf; i < count && *p; i++, p++) {
1997 if (*p == '\n' || *p == ' ')
1998 /* trailing lf, grr */
2004 read_lock_bh(&iucv_connection_rwlock);
2005 list_for_each_entry(cp, &iucv_connection_list, list) {
2007 priv = netdev_priv(ndev);
2009 if (strncmp(name, ndev->name, count))
2011 read_unlock_bh(&iucv_connection_rwlock);
2012 if (ndev->flags & (IFF_UP | IFF_RUNNING)) {
2013 dev_warn(dev, "The IUCV device is connected"
2014 " to %s and cannot be removed\n",
2015 priv->conn->userid);
2016 IUCV_DBF_TEXT(data, 2, "remove_write: still active\n");
2019 unregister_netdev(ndev);
2020 netiucv_unregister_device(dev);
2023 read_unlock_bh(&iucv_connection_rwlock);
2024 IUCV_DBF_TEXT(data, 2, "remove_write: unknown device\n");
2027 static DRIVER_ATTR_WO(remove);
2029 static struct attribute * netiucv_drv_attrs[] = {
2030 &driver_attr_connection.attr,
2031 &driver_attr_remove.attr,
2035 static struct attribute_group netiucv_drv_attr_group = {
2036 .attrs = netiucv_drv_attrs,
2039 static const struct attribute_group *netiucv_drv_attr_groups[] = {
2040 &netiucv_drv_attr_group,
2044 static void netiucv_banner(void)
2046 pr_info("driver initialized\n");
2049 static void __exit netiucv_exit(void)
2051 struct iucv_connection *cp;
2052 struct net_device *ndev;
2053 struct netiucv_priv *priv;
2056 IUCV_DBF_TEXT(trace, 3, __func__);
2057 while (!list_empty(&iucv_connection_list)) {
2058 cp = list_entry(iucv_connection_list.next,
2059 struct iucv_connection, list);
2061 priv = netdev_priv(ndev);
2064 unregister_netdev(ndev);
2065 netiucv_unregister_device(dev);
2068 driver_unregister(&netiucv_driver);
2069 iucv_unregister(&netiucv_handler, 1);
2070 iucv_unregister_dbf_views();
2072 pr_info("driver unloaded\n");
2076 static int __init netiucv_init(void)
2080 rc = iucv_register_dbf_views();
2083 rc = iucv_register(&netiucv_handler, 1);
2086 IUCV_DBF_TEXT(trace, 3, __func__);
2087 netiucv_driver.groups = netiucv_drv_attr_groups;
2088 rc = driver_register(&netiucv_driver);
2090 IUCV_DBF_TEXT_(setup, 2, "ret %d from driver_register\n", rc);
2098 iucv_unregister(&netiucv_handler, 1);
2100 iucv_unregister_dbf_views();
2105 module_init(netiucv_init);
2106 module_exit(netiucv_exit);
2107 MODULE_LICENSE("GPL");