File: /usr/src/linux/drivers/s390/net/ctcmain.c

1     /*
2      * $Id: ctcmain.c,v 1.46 2001/07/05 17:36:41 felfert Exp $
3      *
4      * CTC / ESCON network driver
5      *
6      * Copyright (C) 2001 IBM Deutschland Entwicklung GmbH, IBM Corporation
7      * Author(s): Fritz Elfert (elfert@de.ibm.com, felfert@millenux.com)
8      * Fixes by : Jochen Röhrig (roehrig@de.ibm.com)
9      *            Arnaldo Carvalho de Melo <acme@conectiva.com.br>
10      *
11      * Documentation used:
12      *  - Principles of Operation (IBM doc#: SA22-7201-06)
13      *  - Common IO/-Device Commands and Self Description (IBM doc#: SA22-7204-02)
14      *  - Common IO/-Device Commands and Self Description (IBM doc#: SN22-5535)
15      *  - ESCON Channel-to-Channel Adapter (IBM doc#: SA22-7203-00)
16      *  - ESCON I/O Interface (IBM doc#: SA22-7202-029
17      *
18      * and the source of the original CTC driver by:
19      *  Dieter Wellerdiek (wel@de.ibm.com)
20      *  Martin Schwidefsky (schwidefsky@de.ibm.com)
21      *  Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
22      *  Jochen Röhrig (roehrig@de.ibm.com)
23      *
24      * This program is free software; you can redistribute it and/or modify
25      * it under the terms of the GNU General Public License as published by
26      * the Free Software Foundation; either version 2, or (at your option)
27      * any later version.
28      *
29      * This program is distributed in the hope that it will be useful,
30      * but WITHOUT ANY WARRANTY; without even the implied warranty of
31      * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32      * GNU General Public License for more details.
33      *
34      * You should have received a copy of the GNU General Public License
35      * along with this program; if not, write to the Free Software
36      * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
37      *
38      * RELEASE-TAG: CTC/ESCON network driver $Revision: 1.46 $
39      *
40      */
41     
42     #include <linux/version.h>
43     #include <linux/module.h>
44     #include <linux/init.h>
45     #include <linux/kernel.h>
46     #include <linux/slab.h>
47     #include <linux/errno.h>
48     #include <linux/types.h>
49     #include <linux/interrupt.h>
50     #include <linux/timer.h>
51     #include <linux/sched.h>
52     
53     #include <linux/signal.h>
54     #include <linux/string.h>
55     #include <linux/proc_fs.h>
56     
57     #include <linux/ip.h>
58     #include <linux/if_arp.h>
59     #include <linux/tcp.h>
60     #include <linux/skbuff.h>
61     #include <linux/ctype.h>
62     #include <net/dst.h>
63     
64     #include <asm/io.h>
65     #include <asm/bitops.h>
66     #include <asm/uaccess.h>
67     #ifdef CONFIG_CHANDEV
68     #define CTC_CHANDEV
69     #endif
70     
71     #ifdef CTC_CHANDEV
72     #include <asm/chandev.h>
73     #define REQUEST_IRQ chandev_request_irq
74     #define FREE_IRQ chandev_free_irq
75     #else
76     #define REQUEST_IRQ request_irq
77     #define FREE_IRQ free_irq
78     #endif
79     
80     #if LINUX_VERSION_CODE >= 0x020213
81     #  include <asm/idals.h>
82     #else
83     #  define set_normalized_cda(ccw, addr) ((ccw)->cda = (addr))
84     #  define clear_normalized_cda(ccw)
85     #endif
86     #if LINUX_VERSION_CODE < 0x020400
87     #  define s390_dev_info_t dev_info_t
88     #  define dev_kfree_skb_irq(a) dev_kfree_skb(a)
89     #endif
90     
91     #include <asm/irq.h>
92     
93     #include "ctctty.h"
94     #include "fsm.h"
95     
96     #ifdef MODULE
97     MODULE_AUTHOR("(C) 2000 IBM Corp. by Fritz Elfert (felfert@millenux.com)");
98     MODULE_DESCRIPTION("Linux for S/390 CTC/Escon Driver");
99     #ifndef CTC_CHANDEV
100     MODULE_PARM(ctc, "s");
101     MODULE_PARM_DESC(ctc,
102     "One or more definitions in the same format like the kernel param for ctc.\n"
103     "E.g.: ctc0:0x700:0x701:0:ctc1:0x702:0x703:0\n");
104     
105     char *ctc = NULL;
106     #endif
107     #else
108     /**
109      * Number of devices in monolithic (not module) driver version.
110      */
111     #define MAX_STATIC_DEVICES 16
112     #endif /* MODULE */
113     
114     #undef DEBUG
115     
116     /**
117      * CCW commands, used in this driver.
118      */
119     #define CCW_CMD_WRITE		0x01
120     #define CCW_CMD_READ		0x02
121     #define CCW_CMD_SET_EXTENDED	0xc3
122     #define CCW_CMD_PREPARE		0xe3
123     
124     #define CTC_PROTO_S390          0
125     #define CTC_PROTO_LINUX         1
126     #define CTC_PROTO_LINUX_TTY     2
127     #define CTC_PROTO_OS390         3
128     #define CTC_PROTO_MAX           3
129     
130     #define CTC_BUFSIZE_LIMIT       65535
131     #define CTC_BUFSIZE_DEFAULT     32768
132     
133     #define CTC_TIMEOUT_5SEC        5000
134     
135     #define CTC_INITIAL_BLOCKLEN    2
136     
137     #define READ			0
138     #define WRITE			1
139     
140     /**
141      * Enum for classifying detected devices.
142      */
143     enum channel_types {
144             /**
145     	 * Device is not a channel.
146     	 */
147     	channel_type_none,
148     
149     	/**
150     	 * Device is a channel, but we don't know
151     	 * anything about it.
152     	 */
153     	channel_type_unknown,
154     
155             /**
156     	 * Device is a CTC/A.
157     	 */
158     	channel_type_ctca,
159     
160     	/**
161     	 * Device is a ESCON channel.
162     	 */
163     	channel_type_escon,
164     	/**
165     	 * Device is an unsupported model.
166     	 */
167     	channel_type_unsupported
168     };
169     
170     typedef enum channel_types channel_type_t;
171     
172     #ifndef CTC_CHANDEV
173     static int ctc_no_auto = 0;
174     #endif
175     
176     /**
177      * If running on 64 bit, this must be changed. XXX Why? (bird)
178      */
179     typedef unsigned long intparm_t;
180     
181     #ifndef CTC_CHANDEV
182     /**
183      * Definition of a per device parameter block
184      */
185     #define MAX_PARAM_NAME_LEN 11
186     typedef struct param_t {
187     	struct param_t *next;
188     	int            read_dev;
189     	int            write_dev;
190     	__u16          proto;
191     	char           name[MAX_PARAM_NAME_LEN];
192     } param;
193     
194     static param *params = NULL;
195     #endif
196     
197     typedef struct {
198     	unsigned long maxmulti;
199     	unsigned long maxcqueue;
200     	unsigned long doios_single;
201     	unsigned long doios_multi;
202     	unsigned long txlen;
203     	unsigned long tx_time;
204     	struct timeval send_stamp;
205     } ctc_profile;
206     
207     /**
208      * Definition of one channel
209      */
210     typedef struct channel_t {
211     
212             /**
213     	 * Pointer to next channel in list.
214     	 */
215     	struct channel_t    *next;
216     	__u16               devno;
217     	int                 irq;
218     
219     	/**
220     	 * Type of this channel.
221     	 * CTC/A or Escon for valid channels.
222     	 */
223     	channel_type_t      type;
224     
225             /**
226     	 * Misc. flags. See CHANNEL_FLAGS_... below
227     	 */
228     	__u32               flags;
229     
230     	/**
231     	 * The protocol of this channel
232     	 */
233     	__u16               protocol;
234     
235     	/**
236     	 * I/O and irq related stuff
237     	 */
238     	ccw1_t              *ccw;
239     	devstat_t           *devstat;
240     
241     	/**
242     	 * Bottom half task queue.
243     	 */
244     	struct tq_struct    tq;
245     
246     	/**
247     	 * RX/TX buffer size
248     	 */
249     	int                 max_bufsize;
250     
251     	/**
252     	 * Transmit/Receive buffer.
253     	 */
254     	struct sk_buff      *trans_skb;
255     
256     	/**
257     	 * Universal I/O queue.
258     	 */
259     	struct sk_buff_head io_queue;
260     
261     	/**
262     	 * TX queue for collecting skb's during busy.
263     	 */
264     	struct sk_buff_head collect_queue;
265     
266     	/**
267     	 * Amount of data in collect_queue.
268     	 */
269     	int                 collect_len;
270     
271     	/**
272     	 * spinlock for collect_queue and collect_len
273     	 */
274     	spinlock_t          collect_lock;
275     
276     	/**
277     	 * Timer for detecting unresposive
278     	 * I/O operations.
279     	 */
280     	fsm_timer           timer;
281     
282     	/**
283     	 * Retry counter for misc. operations.
284     	 */
285     	int                 retry;
286     
287     	/**
288     	 * The finite state machine of this channel
289     	 */
290     	fsm_instance        *fsm;
291     
292     	/**
293     	 * The corresponding net_device this channel
294     	 * belongs to.
295     	 */
296     	net_device          *netdev;
297     
298     	ctc_profile         prof;
299     } channel;
300     
301     #define CHANNEL_FLAGS_READ            0
302     #define CHANNEL_FLAGS_WRITE           1
303     #define CHANNEL_FLAGS_INUSE           2
304     #define CHANNEL_FLAGS_BUFSIZE_CHANGED 4
305     #define CHANNEL_FLAGS_RWMASK 1
306     #define CHANNEL_DIRECTION(f) (f & CHANNEL_FLAGS_RWMASK)
307     
308     /**
309      * Linked list of all detected channels.
310      */
311     static channel *channels = NULL;
312     
313     #ifdef CTC_CHANDEV
314     static int activated;
315     #endif
316     
317     typedef struct ctc_priv_t {
318     	struct net_device_stats stats;
319     #if LINUX_VERSION_CODE >= 0x02032D
320     	unsigned long           tbusy;
321     #endif
322     	/**
323     	 * The finite state machine of this interface.
324     	 */
325     	fsm_instance            *fsm;
326     	/**
327     	 * The protocol of this device
328     	 */
329     	__u16                   protocol;
330     	channel                 *channel[2];
331     	struct proc_dir_entry   *proc_dentry;
332     	struct proc_dir_entry   *proc_stat_entry;
333     	struct proc_dir_entry   *proc_ctrl_entry;
334     	int                     proc_registered;
335     } ctc_priv;
336     
337     /**
338      * Definition of our link level header.
339      */
340     typedef struct ll_header_t {
341     	__u16	      length;
342     	__u16	      type;
343     	__u16	      unused;
344     } ll_header;
345     #define LL_HEADER_LENGTH (sizeof(ll_header))
346     
347     /**
348      * Compatibility macros for busy handling
349      * of network devices.
350      */
351     #if LINUX_VERSION_CODE < 0x02032D
352     static __inline__ void ctc_clear_busy(net_device *dev)
353     {
354     	clear_bit(0 ,(void *)&dev->tbusy);
355     	mark_bh(NET_BH);
356     }
357     
358     static __inline__ int ctc_test_and_set_busy(net_device *dev)
359     {
360     	return(test_and_set_bit(0, (void *)&dev->tbusy));
361     }
362     
363     #define SET_DEVICE_START(device, value) dev->start = value
364     #else
365     static __inline__ void ctc_clear_busy(net_device *dev)
366     {
367     	clear_bit(0, &(((ctc_priv *)dev->priv)->tbusy));
368     	netif_start_queue(dev);
369     }
370     
371     static __inline__ int ctc_test_and_set_busy(net_device *dev)
372     {
373     	netif_stop_queue(dev);
374     	return test_and_set_bit(0, &((ctc_priv *)dev->priv)->tbusy);
375     }
376     
377     #define SET_DEVICE_START(device, value)
378     #endif
379     
380     /**
381      * Print Banner.
382      */
383     static void print_banner(void) {
384     	static int printed = 0;
385     	char vbuf[] = "$Revision: 1.46 $";
386     	char *version = vbuf;
387     
388     	if (printed)
389     		return;
390     	if ((version = strchr(version, ':'))) {
391     		char *p = strchr(version + 1, '$');
392     		if (p)
393     			*p = '\0';
394     	} else
395     		version = " ??? ";
396     	printk(KERN_INFO "CTC driver Version%s initialized\n", version);
397     	printed = 1;
398     }
399     
400     
401     #ifndef CTC_CHANDEV
402     /**
403      * Return type of a detected device.
404      */
405     static channel_type_t channel_type (senseid_t *id) {
406     	channel_type_t type = channel_type_none;
407     
408     	switch (id->cu_type) {
409     		case 0x3088:
410     			switch (id->cu_model) {
411     				case 0x08:
412     					/**
413     					 * 3088-08 = CTCA
414     					 */
415     					type = channel_type_ctca;
416     					break;
417     
418     				case 0x1F:
419     					/**
420     					 * 3088-1F = ESCON channel
421     					 */
422     					type = channel_type_escon;
423     					break;
424     
425     					/**
426     					 * 3088-01 = P390 OSA emulation
427     					 */
428     				case 0x01:
429     					/* fall thru */
430     
431     					/**
432     					 * 3088-60 = OSA/2 adapter
433     					 */
434     				case 0x60:
435     					/* fall thru */
436     
437     					/**
438     					 * 3088-61 = CISCO 7206 CLAW proto
439     					 * on ESCON
440     					 */
441     				case 0x61:
442     					/* fall thru */
443     
444     					/**
445     					 * 3088-62 = OSA/D device
446     					 */
447     				case 0x62:
448     					type = channel_type_unsupported;
449     					break;
450     
451     				default:
452     					type = channel_type_unknown;
453     					printk(KERN_INFO
454     					       "channel: Unknown model found "
455     					       "3088-%02x\n", id->cu_model);
456     			}
457     			break;
458     
459     		default:
460     			type = channel_type_none;
461     	}
462     	return type;
463     }
464     #endif
465     
466     
467     /**
468      * States of the interface statemachine.
469      */
470     enum dev_states {
471     	DEV_STATE_STOPPED,
472     	DEV_STATE_STARTWAIT_RXTX,
473     	DEV_STATE_STARTWAIT_RX,
474     	DEV_STATE_STARTWAIT_TX,
475     	DEV_STATE_STOPWAIT_RXTX,
476     	DEV_STATE_STOPWAIT_RX,
477     	DEV_STATE_STOPWAIT_TX,
478     	DEV_STATE_RUNNING,
479     	/**
480     	 * MUST be always the last element!!
481     	 */
482     	NR_DEV_STATES
483     };
484     
485     static const char *dev_state_names[] = {
486     	"Stopped",
487     	"StartWait RXTX",
488     	"StartWait RX",
489     	"StartWait TX",
490     	"StopWait RXTX",
491     	"StopWait RX",
492     	"StopWait TX",
493     	"Running",
494     };
495     
496     /**
497      * Events of the interface statemachine.
498      */
499     enum dev_events {
500     	DEV_EVENT_START,
501     	DEV_EVENT_STOP,
502     	DEV_EVENT_RXUP,
503     	DEV_EVENT_TXUP,
504     	DEV_EVENT_RXDOWN,
505     	DEV_EVENT_TXDOWN,
506     	/**
507     	 * MUST be always the last element!!
508     	 */
509     	NR_DEV_EVENTS
510     };
511     
512     static const char *dev_event_names[] = {
513     	"Start",
514     	"Stop",
515     	"RX up",
516     	"TX up",
517     	"RX down",
518     	"TX down",
519     };
520     
521     /**
522      * Events of the channel statemachine
523      */
524     enum ch_events {
525     	/**
526     	 * Events, representing return code of
527     	 * I/O operations (do_IO, halt_IO et al.)
528     	 */
529     	CH_EVENT_IO_SUCCESS,
530     	CH_EVENT_IO_EBUSY,
531     	CH_EVENT_IO_ENODEV,
532     	CH_EVENT_IO_EIO,
533     	CH_EVENT_IO_UNKNOWN,
534     
535     	CH_EVENT_ATTNBUSY,
536     	CH_EVENT_ATTN,
537     	CH_EVENT_BUSY,
538     
539     	/**
540     	 * Events, representing unit-check
541     	 */
542     	CH_EVENT_UC_RCRESET,
543     	CH_EVENT_UC_RSRESET,
544     	CH_EVENT_UC_TXTIMEOUT,
545     	CH_EVENT_UC_TXPARITY,
546     	CH_EVENT_UC_HWFAIL,
547     	CH_EVENT_UC_RXPARITY,
548     	CH_EVENT_UC_ZERO,
549     	CH_EVENT_UC_UNKNOWN,
550     
551     	/**
552     	 * Events, representing subchannel-check
553     	 */
554     	CH_EVENT_SC_UNKNOWN,
555     
556     	/**
557     	 * Events, representing machine checks
558     	 */
559     	CH_EVENT_MC_FAIL,
560     	CH_EVENT_MC_GOOD,
561     
562     	/**
563     	 * Event, representing normal IRQ
564     	 */
565     	CH_EVENT_IRQ,
566     	CH_EVENT_FINSTAT,
567     
568     	/**
569     	 * Event, representing timer expiry.
570     	 */
571     	CH_EVENT_TIMER,
572     
573     	/**
574     	 * Events, representing commands from upper levels.
575     	 */
576     	CH_EVENT_START,
577     	CH_EVENT_STOP,
578     
579     	/**
580     	 * MUST be always the last element!!
581     	 */
582     	NR_CH_EVENTS,
583     };
584     
585     static const char *ch_event_names[] = {
586     	"do_IO success",
587     	"do_IO busy",
588     	"do_IO enodev",
589     	"do_IO ioerr",
590     	"do_IO unknown",
591     
592     	"Status ATTN & BUSY",
593     	"Status ATTN",
594     	"Status BUSY",
595     
596     	"Unit check remote reset",
597     	"Unit check remote system reset",
598     	"Unit check TX timeout",
599     	"Unit check TX parity",
600     	"Unit check Hardware failure",
601     	"Unit check RX parity",
602     	"Unit check ZERO",
603     	"Unit check Unknown",
604     
605     	"SubChannel check Unknown",
606     
607     	"Machine check failure",
608     	"Machine check operational",
609     
610     	"IRQ normal",
611     	"IRQ final",
612     
613     	"Timer",
614     
615     	"Start",
616     	"Stop",
617     };
618     
619     /**
620      * States of the channel statemachine.
621      */
622     enum ch_states {
623     	/**
624     	 * Channel not assigned to any device,
625     	 * initial state, direction invalid
626     	 */
627     	CH_STATE_IDLE,
628     
629     	/**
630     	 * Channel assigned but not operating
631     	 */
632     	CH_STATE_STOPPED,
633     	CH_STATE_STARTWAIT,
634     	CH_STATE_STARTRETRY,
635     	CH_STATE_SETUPWAIT,
636     	CH_STATE_RXINIT,
637     	CH_STATE_TXINIT,
638     	CH_STATE_RX,
639     	CH_STATE_TX,
640     	CH_STATE_RXIDLE,
641     	CH_STATE_TXIDLE,
642     	CH_STATE_RXERR,
643     	CH_STATE_TXERR,
644     	CH_STATE_TERM,
645     	CH_STATE_DTERM,
646     	CH_STATE_NOTOP,
647     
648     	/**
649     	 * MUST be always the last element!!
650     	 */
651     	NR_CH_STATES,
652     };
653     
654     static const char *ch_state_names[] = {
655     	"Idle",
656     	"Stopped",
657     	"StartWait",
658     	"StartRetry",
659     	"SetupWait",
660     	"RX init",
661     	"TX init",
662     	"RX",
663     	"TX",
664     	"RX idle",
665     	"TX idle",
666     	"RX error",
667     	"TX error",
668     	"Terminating",
669     	"Restarting",
670     	"Not operational",
671     };
672     
673     
674     #ifdef DEBUG
675     /**
676      * Dump header and first 16 bytes of an sk_buff for debugging purposes.
677      *
678      * @param skb    The sk_buff to dump.
679      * @param offset Offset relative to skb-data, where to start the dump.
680      */
681     static void ctc_dump_skb(struct sk_buff *skb, int offset)
682     {
683     	unsigned char *p = skb->data;
684     	__u16 bl;
685     	ll_header *header;
686     	int i;
687     
688     	p += offset;
689     	bl = *((__u16*)p);
690     	p += 2;
691     	header = (ll_header *)p;
692     	p -= 2;
693     	
694     	printk(KERN_DEBUG "dump:\n");
695     	printk(KERN_DEBUG "blocklen=%d %04x\n", bl, bl);
696     
697     	printk(KERN_DEBUG "h->length=%d %04x\n", header->length,
698     	       header->length); 
699     	printk(KERN_DEBUG "h->type=%04x\n", header->type); 
700     	printk(KERN_DEBUG "h->unused=%04x\n", header->unused);
701     	if (bl > 16)
702     		bl = 16;
703     	printk(KERN_DEBUG "data: ");
704     	for (i = 0; i < bl; i++)
705     		printk("%02x%s", *p++, (i % 16) ? " " : "\n<7>");
706     	printk("\n");
707     }
708     #endif
709     
710     /**
711      * Unpack a just received skb and hand it over to
712      * upper layers.
713      *
714      * @param ch The channel where this skb has been received.
715      * @param pskb The received skb.
716      */
717     static __inline__ void ctc_unpack_skb(channel *ch, struct sk_buff *pskb)
718     {
719     	net_device     *dev = ch->netdev;
720     	ctc_priv       *privptr = (ctc_priv *)dev->priv;
721     
722     	__u16 len = *((__u16*)pskb->data);
723     	skb_put(pskb, 2 + LL_HEADER_LENGTH);
724     	skb_pull(pskb, 2);
725     	pskb->dev = dev;
726     	pskb->ip_summed = CHECKSUM_UNNECESSARY;
727     	while (len > 0) {
728     		struct sk_buff *skb;
729     		ll_header *header = (ll_header *)pskb->data;
730     
731     		skb_pull(pskb, LL_HEADER_LENGTH);
732     		if ((ch->protocol == CTC_PROTO_S390) &&
733     		    (header->type != ETH_P_IP)) {
734     			/**
735     			 * Check packet type only if we stick strictly
736     			 * to S/390's protocol of OS390. This only
737     			 * supports IP. Otherwise allow any packet
738     			 * type.
739     			 */
740     			printk(KERN_WARNING
741     			       "%s Illegal packet type 0x%04x "
742     			       "received, dropping\n",
743     			       dev->name, header->type);
744     #ifdef DEBUG
745     			ctc_dump_skb(pskb, -6);
746     #endif
747     			privptr->stats.rx_dropped++;
748     			privptr->stats.rx_frame_errors++;
749     			return;
750     		}
751     		pskb->protocol = ntohs(header->type);
752     		header->length -= LL_HEADER_LENGTH;
753     		if ((header->length == 0) ||
754     		    (header->length > skb_tailroom(pskb))) {
755     			printk(KERN_WARNING
756     			       "%s Illegal packet size %d "
757     			       "received (MTU=%d), "
758     			       "dropping\n", dev->name, header->length,
759     			       dev->mtu);
760     #ifdef DEBUG
761     			ctc_dump_skb(pskb, -6);
762     #endif
763     			privptr->stats.rx_dropped++;
764     			privptr->stats.rx_length_errors++;
765     			return;
766     		}
767     		if (header->length > skb_tailroom(pskb)) {
768     			printk(KERN_WARNING
769     			       "%s Illegal packet size %d "
770     			       "(beyond the end of received data), "
771     			       "dropping\n", dev->name, header->length);
772     #ifdef DEBUG
773     			ctc_dump_skb(pskb, -6);
774     #endif
775     			privptr->stats.rx_dropped++;
776     			privptr->stats.rx_length_errors++;
777     			return;
778     		}
779     		skb_put(pskb, header->length);
780     		pskb->mac.raw = pskb->data;
781     		len -= (LL_HEADER_LENGTH + header->length);
782     		skb = dev_alloc_skb(pskb->len);
783     		if (!skb) {
784     			printk(KERN_WARNING
785     			       "%s Out of memory in ctc_unpack_skb\n",
786     			       dev->name);
787     			privptr->stats.rx_dropped++;
788     			return;
789     		}
790     		memcpy(skb_put(skb, pskb->len), pskb->data, pskb->len);
791     		skb->mac.raw = skb->data;
792     		skb->dev = pskb->dev;
793     		skb->protocol = pskb->protocol;
794     		pskb->ip_summed = CHECKSUM_UNNECESSARY;
795     		if (ch->protocol == CTC_PROTO_LINUX_TTY)
796     			ctc_tty_netif_rx(skb);
797     		else
798     			netif_rx(skb);
799     		privptr->stats.rx_packets++;
800     		privptr->stats.rx_bytes += skb->len;
801     		if (len > 0) {
802     			skb_pull(pskb, header->length);
803     			skb_put(pskb, LL_HEADER_LENGTH);
804     		}
805     	}
806     }
807     
808     /**
809      * Bottom half routine.
810      *
811      * @param ch The channel to work on.
812      */
813     static void ctc_bh(channel *ch)
814     {
815     	struct sk_buff *skb;
816     
817     	while ((skb = skb_dequeue(&ch->io_queue)))
818     		ctc_unpack_skb(ch, skb);
819     }
820     
821     /**
822      * Check return code of a preceeding do_IO, halt_IO etc...
823      *
824      * @param ch          The channel, the error belongs to.
825      * @param return_code The error code to inspect.
826      */
827     static void inline ccw_check_return_code (channel *ch, int return_code)
828     {
829     	switch (return_code) {
830     		case 0:
831     			fsm_event(ch->fsm, CH_EVENT_IO_SUCCESS, ch);
832     			break;
833     		case -EBUSY:
834     			printk(KERN_INFO "ch-%04x: Busy !\n", ch->devno);
835     			fsm_event(ch->fsm, CH_EVENT_IO_EBUSY, ch);
836     			break;
837     		case -ENODEV:
838     			printk(KERN_EMERG
839     			       "ch-%04x: Invalid device called for IO\n",
840     			       ch->devno);
841     			fsm_event(ch->fsm, CH_EVENT_IO_ENODEV, ch);
842     			break;
843     		case -EIO:
844     			printk(KERN_EMERG
845     			       "ch-%04x: Status pending... \n", ch->devno);
846     			fsm_event(ch->fsm, CH_EVENT_IO_EIO, ch);
847     			break;
848     		default:
849     			printk(KERN_EMERG
850     			       "ch-%04x: Unknown error in do_IO %04x\n",
851     			       ch->devno, return_code);
852     			fsm_event(ch->fsm, CH_EVENT_IO_UNKNOWN, ch);
853     	}
854     }
855     
856     /**
857      * Check sense of a unit check.
858      *
859      * @param ch    The channel, the sense code belongs to.
860      * @param sense The sense code to inspect.
861      */
862     static void inline ccw_unit_check (channel *ch, unsigned char sense) {
863     	if (sense & SNS0_INTERVENTION_REQ) {
864     		if (sense & 0x01)  {
865     			if (ch->protocol != CTC_PROTO_LINUX_TTY)
866     				printk(KERN_DEBUG
867     				       "ch-%04x: Interface disc. or Sel. reset "
868     				       "(remote)\n", ch->devno);
869     			fsm_event(ch->fsm, CH_EVENT_UC_RCRESET, ch);
870     		} else {
871     			printk(KERN_DEBUG "ch-%04x: System reset (remote)\n",
872     			       ch->devno);
873     			fsm_event(ch->fsm, CH_EVENT_UC_RSRESET, ch);
874     		}
875     	} else if (sense & SNS0_EQUIPMENT_CHECK) {
876     		if (sense & SNS0_BUS_OUT_CHECK) {
877     			printk(KERN_WARNING
878     			       "ch-%04x: Hardware malfunction (remote)\n",
879     			       ch->devno);
880     			fsm_event(ch->fsm, CH_EVENT_UC_HWFAIL, ch);
881     		} else {
882     			printk(KERN_WARNING
883     			       "ch-%04x: Read-data parity error (remote)\n",
884     			       ch->devno);
885     			fsm_event(ch->fsm, CH_EVENT_UC_RXPARITY, ch);
886     		}
887     	} else if (sense & SNS0_BUS_OUT_CHECK) {
888     		if (sense & 0x04) {
889     			printk(KERN_WARNING
890     			       "ch-%04x: Data-streaming timeout)\n",
891     			       ch->devno);
892     			fsm_event(ch->fsm, CH_EVENT_UC_TXTIMEOUT, ch);
893     		} else {
894     			printk(KERN_WARNING
895     			       "ch-%04x: Data-transfer parity error\n",
896     			       ch->devno);
897     			fsm_event(ch->fsm, CH_EVENT_UC_TXPARITY, ch);
898     		}
899     	} else if (sense & SNS0_CMD_REJECT) {
900     			printk(KERN_WARNING "ch-%04x: Command reject\n",
901     			       ch->devno);
902     	} else if (sense == 0) {
903     		printk(KERN_DEBUG "ch-%04x: Unit check ZERO\n", ch->devno);
904     		fsm_event(ch->fsm, CH_EVENT_UC_ZERO, ch);
905     	} else {
906     		printk(KERN_WARNING
907     		       "ch-%04x: Unit Check with sense code: %02x\n",
908     		       ch->devno, sense);
909     		fsm_event(ch->fsm, CH_EVENT_UC_UNKNOWN, ch);
910     	}
911     }
912     
913     static void ctc_purge_skb_queue(struct sk_buff_head *q)
914     {
915     	struct sk_buff *skb;
916     
917     	while ((skb = skb_dequeue(q))) {
918     		atomic_dec(&skb->users);
919     		dev_kfree_skb_irq(skb);
920     	}
921     }
922     
923     static __inline__ int ctc_checkalloc_buffer(channel *ch, int warn) {
924     	if ((ch->trans_skb == NULL) ||
925     	    (ch->flags & CHANNEL_FLAGS_BUFSIZE_CHANGED)) {
926     		if (ch->trans_skb != NULL)
927     			dev_kfree_skb(ch->trans_skb);
928     		ch->trans_skb = dev_alloc_skb(ch->max_bufsize);
929     		if (ch->trans_skb == NULL) {
930     			if (warn)
931     				printk(KERN_WARNING
932     				       "ch-%04x: Couldn't alloc %s trans_skb\n",
933     				       ch->devno,
934     				       (CHANNEL_DIRECTION(ch->flags) == READ) ?
935     				       "RX" : "TX");
936     			return -ENOMEM;
937     		}
938     		set_normalized_cda(&ch->ccw[1],
939     				   virt_to_phys(ch->trans_skb->data));
940     		if (ch->ccw[1].cda == 0) {
941     			dev_kfree_skb(ch->trans_skb);
942     			ch->trans_skb = NULL;
943     			if (warn)
944     				printk(KERN_WARNING
945     				       "ch-%04x: set_normalized_cda for %s "
946     				       "trans_skb failed, dropping packets\n",
947     				       ch->devno,
948     				       (CHANNEL_DIRECTION(ch->flags) == READ) ?
949     				       "RX" : "TX");
950     			return -ENOMEM;
951     		}
952     		ch->flags &= ~CHANNEL_FLAGS_BUFSIZE_CHANGED;
953     	}
954     	return 0;
955     }
956     
957     /**
958      * Dummy NOP action for statemachines
959      */
960     static void fsm_action_nop(fsm_instance *fi, int event, void *arg)
961     {
962     }
963     
964     /**
965      * Actions for channel - statemachines.
966      *****************************************************************************/
967     
968     /**
969      * Normal data has been send. Free the corresponding
970      * skb (it's in io_queue), reset dev->tbusy and
971      * revert to idle state.
972      *
973      * @param fi    An instance of a channel statemachine.
974      * @param event The event, just happened.
975      * @param arg   Generic pointer, casted from channel * upon call.
976      */
977     static void ch_action_txdone(fsm_instance *fi, int event, void *arg)
978     {
979     	channel        *ch = (channel *)arg;
980     	net_device     *dev = ch->netdev;
981     	ctc_priv       *privptr = dev->priv;
982     	struct sk_buff *skb;
983     	int            first = 1;
984     	int            i;
985     
986     	struct timeval done_stamp = xtime;
987     	unsigned long duration = 
988     		(done_stamp.tv_sec - ch->prof.send_stamp.tv_sec) * 1000000 +
989     		done_stamp.tv_usec - ch->prof.send_stamp.tv_usec;
990     	if (duration > ch->prof.tx_time)
991     		ch->prof.tx_time = duration;
992     
993     	if (ch->devstat->rescnt != 0)
994     		printk(KERN_DEBUG "%s: TX not complete, remaining %d bytes\n",
995     		       dev->name, ch->devstat->rescnt);
996     	
997     	fsm_deltimer(&ch->timer);
998     	while ((skb = skb_dequeue(&ch->io_queue))) {
999     		privptr->stats.tx_packets++;
1000     		privptr->stats.tx_bytes += skb->len - LL_HEADER_LENGTH;
1001     		if (first) {
1002     			privptr->stats.tx_bytes += 2;
1003     			first = 0;
1004     		}
1005     		atomic_dec(&skb->users);
1006     		dev_kfree_skb_irq(skb);
1007     	}
1008     	spin_lock(&ch->collect_lock);
1009     	clear_normalized_cda(&ch->ccw[4]);
1010     	if (ch->collect_len > 0) {
1011     		int rc;
1012     
1013     		if (ctc_checkalloc_buffer(ch, 1)) {
1014     			spin_unlock(&ch->collect_lock);
1015     			return;
1016     		}
1017     		ch->trans_skb->tail = ch->trans_skb->data;
1018     		ch->trans_skb->len = 0;
1019     		if (ch->prof.maxmulti < (ch->collect_len + 2))
1020     			ch->prof.maxmulti = ch->collect_len + 2;
1021     		if (ch->prof.maxcqueue < skb_queue_len(&ch->collect_queue))
1022     			ch->prof.maxcqueue = skb_queue_len(&ch->collect_queue);
1023     		ch->ccw[1].count = ch->collect_len + 2;
1024     		*((__u16 *)skb_put(ch->trans_skb, 2)) = ch->collect_len + 2;
1025     		i = 0;
1026     		while ((skb = skb_dequeue(&ch->collect_queue))) {
1027     			memcpy(skb_put(ch->trans_skb, skb->len), skb->data,
1028     			       skb->len);
1029     			privptr->stats.tx_packets++;
1030     			privptr->stats.tx_bytes += skb->len - LL_HEADER_LENGTH;
1031     			atomic_dec(&skb->users);
1032     			dev_kfree_skb_irq(skb);
1033     			i++;
1034     		}
1035     		ch->collect_len = 0;
1036     		fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC, CH_EVENT_TIMER, ch);
1037     		ch->prof.send_stamp = xtime;
1038     #ifdef DEBUG
1039     	printk(KERN_DEBUG "ccw[1].cda = %08x\n", ch->ccw[1].cda);
1040     #endif
1041     		rc = do_IO(ch->irq, &ch->ccw[0], (intparm_t)ch, 0xff, 0);
1042     		ch->prof.doios_multi++;
1043     		if (rc != 0) {
1044     			privptr->stats.tx_dropped += i;
1045     			privptr->stats.tx_errors += i;
1046     			fsm_deltimer(&ch->timer);
1047     			ccw_check_return_code(ch, rc);
1048     		}
1049     	} else
1050     		fsm_newstate(fi, CH_STATE_TXIDLE);
1051     	ctc_clear_busy(dev);
1052     	spin_unlock(&ch->collect_lock);
1053     }
1054     
1055     /**
1056      * Initial data is sent.
1057      * Notify device statemachine that we are up and
1058      * running.
1059      *
1060      * @param fi    An instance of a channel statemachine.
1061      * @param event The event, just happened.
1062      * @param arg   Generic pointer, casted from channel * upon call.
1063      */
1064     static void ch_action_txidle(fsm_instance *fi, int event, void *arg)
1065     {
1066     	channel *ch = (channel *)arg;
1067     
1068     	fsm_deltimer(&ch->timer);
1069     	fsm_newstate(fi, CH_STATE_TXIDLE);
1070     	fsm_event(((ctc_priv *)ch->netdev->priv)->fsm, DEV_EVENT_TXUP,
1071     		  ch->netdev);
1072     }
1073     
1074     /**
1075      * Got normal data, check for sanity, queue it up, allocate new buffer
1076      * trigger bottom half, and initiate next read.
1077      *
1078      * @param fi    An instance of a channel statemachine.
1079      * @param event The event, just happened.
1080      * @param arg   Generic pointer, casted from channel * upon call.
1081      */
1082     static void ch_action_rx(fsm_instance *fi, int event, void *arg)
1083     {
1084     	channel        *ch = (channel *)arg;
1085     	net_device     *dev = ch->netdev;
1086     	ctc_priv       *privptr = dev->priv;
1087     	int            len = ch->max_bufsize - ch->devstat->rescnt;
1088     	struct sk_buff *skb = ch->trans_skb;
1089     	__u16          block_len = *((__u16*)skb->data);
1090     	char           *saved_data = skb->data;
1091     	int            check_len;
1092     	int            rc;
1093     
1094     	fsm_deltimer(&ch->timer);
1095     	if (len < 8) {
1096     		printk(KERN_WARNING "%s: got packet with length %d < 8\n",
1097     		       dev->name, len);
1098     		privptr->stats.rx_dropped++;
1099     		privptr->stats.rx_length_errors++;
1100     		goto again;
1101     	}
1102     	if (len > ch->max_bufsize) {
1103     		printk(KERN_WARNING "%s: got packet with length %d > %d\n",
1104     		       dev->name, len, ch->max_bufsize);
1105     		privptr->stats.rx_dropped++;
1106     		privptr->stats.rx_length_errors++;
1107     		goto again;
1108     	}
1109     
1110     	/**
1111     	 * VM TCP seems to have a bug sending 2 trailing bytes of garbage.
1112     	 */
1113     	switch (ch->protocol) {
1114     		case CTC_PROTO_S390:
1115     		case CTC_PROTO_OS390:
1116     			check_len = block_len + 2;
1117     			break;
1118     		default:
1119     			check_len = block_len;
1120     			break;
1121     	}
1122     	if ((len < block_len) || (len > check_len)) {
1123     		printk(KERN_WARNING "%s: got block length %d != rx length %d\n",
1124     		       dev->name, block_len, len);
1125     #ifdef DEBUG
1126     		ctc_dump_skb(skb, 0);
1127     #endif
1128     		*((__u16*)skb->data) = len;
1129     		privptr->stats.rx_dropped++;
1130     		privptr->stats.rx_length_errors++;
1131     		goto again;
1132     	}
1133     	block_len -= 2;
1134     	if (block_len > 0) {
1135     		*((__u16*)skb->data) = block_len;
1136     		ctc_unpack_skb(ch, skb);
1137     	}
1138      again:
1139     	skb->data = skb->tail = saved_data;
1140     	skb->len = 0;
1141     	if (ctc_checkalloc_buffer(ch, 1))
1142     		return;
1143     	ch->ccw[1].count = ch->max_bufsize;
1144     #ifdef DEBUG
1145     	printk(KERN_DEBUG "ccw[1].cda = %08x\n", ch->ccw[1].cda);
1146     #endif
1147     	rc = do_IO(ch->irq, &ch->ccw[0], (intparm_t)ch, 0xff, 0);
1148     	if (rc != 0)
1149     		ccw_check_return_code(ch, rc);
1150     }
1151     
1152     static void ch_action_rxidle(fsm_instance *fi, int event, void *arg);
1153     
1154     /**
1155      * Initialize connection by sending a __u16 of value 0.
1156      *
1157      * @param fi    An instance of a channel statemachine.
1158      * @param event The event, just happened.
1159      * @param arg   Generic pointer, casted from channel * upon call.
1160      */
1161     static void ch_action_firstio(fsm_instance *fi, int event, void *arg)
1162     {
1163     	channel *ch = (channel *)arg;
1164     	int     rc;
1165     
1166     	if (fsm_getstate(fi) == CH_STATE_TXIDLE)
1167     		printk(KERN_DEBUG "ch-%04x: remote side issued READ?, "
1168     		       "init ...\n", ch->devno);
1169     	fsm_deltimer(&ch->timer);
1170     	if (ctc_checkalloc_buffer(ch, 1))
1171     		return;
1172     	if ((fsm_getstate(fi) == CH_STATE_SETUPWAIT) &&
1173     	    (ch->protocol == CTC_PROTO_OS390)) {
1174     		/* OS/390 resp. z/OS */
1175     		if (CHANNEL_DIRECTION(ch->flags) == READ) {
1176     			*((__u16 *)ch->trans_skb->data) = CTC_INITIAL_BLOCKLEN;
1177     			fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC,
1178     				     CH_EVENT_TIMER, ch);
1179     			ch_action_rxidle(fi, event, arg);
1180     		} else {
1181     			net_device *dev = ch->netdev;
1182     			fsm_newstate(fi, CH_STATE_TXIDLE);
1183     			fsm_event(((ctc_priv *)dev->priv)->fsm,
1184     				  DEV_EVENT_TXUP, dev);
1185     		}
1186     		return;
1187     	}
1188     
1189     	/**
1190     	 * Don´t setup a timer for receiving the initial RX frame
1191     	 * if in compatibility mode, since VM TCP delays the initial
1192     	 * frame until it has some data to send.
1193     	 */
1194     	if ((CHANNEL_DIRECTION(ch->flags) == WRITE) ||
1195     	    (ch->protocol != CTC_PROTO_S390))
1196     		fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC, CH_EVENT_TIMER, ch);
1197     
1198     	*((__u16 *)ch->trans_skb->data) = CTC_INITIAL_BLOCKLEN;
1199     	ch->ccw[1].count = 2; /* Transfer only length */
1200     
1201     #ifdef DEBUG
1202     	printk(KERN_DEBUG "ccw[1].cda = %08x\n", ch->ccw[1].cda);
1203     #endif
1204     	fsm_newstate(fi, (CHANNEL_DIRECTION(ch->flags) == READ)
1205     		     ? CH_STATE_RXINIT : CH_STATE_TXINIT);
1206     	rc = do_IO(ch->irq, &ch->ccw[0], (intparm_t)ch, 0xff, 0);
1207     	if (rc != 0) {
1208     		fsm_deltimer(&ch->timer);
1209     		fsm_newstate(fi, CH_STATE_SETUPWAIT);
1210     		ccw_check_return_code(ch, rc);
1211     	}
1212     	/**
1213     	 * If in compatibility mode since we don´t setup a timer, we
1214     	 * also signal RX channel up immediately. This enables us
1215     	 * to send packets early which in turn usually triggers some
1216     	 * reply from VM TCP which brings up the RX channel to it´s
1217     	 * final state.
1218     	 */
1219     	if ((CHANNEL_DIRECTION(ch->flags) == READ) &&
1220     	    (ch->protocol == CTC_PROTO_S390)) {
1221     		net_device *dev = ch->netdev;
1222     		fsm_event(((ctc_priv *)dev->priv)->fsm, DEV_EVENT_RXUP, dev);
1223     	}
1224     }
1225     
1226     /**
1227      * Got initial data, check it. If OK,
1228      * notify device statemachine that we are up and
1229      * running.
1230      *
1231      * @param fi    An instance of a channel statemachine.
1232      * @param event The event, just happened.
1233      * @param arg   Generic pointer, casted from channel * upon call.
1234      */
1235     static void ch_action_rxidle(fsm_instance *fi, int event, void *arg)
1236     {
1237     	channel    *ch = (channel *)arg;
1238     	net_device *dev = ch->netdev;
1239     	__u16      buflen;
1240     	int        rc;
1241     
1242     	fsm_deltimer(&ch->timer);
1243     	buflen = *((__u16 *)ch->trans_skb->data);
1244     #ifdef DEBUG
1245     	printk(KERN_DEBUG "%s: Initial RX count %d\n", dev->name, buflen);
1246     #endif
1247     	if (buflen >= CTC_INITIAL_BLOCKLEN) {
1248     		if (ctc_checkalloc_buffer(ch, 1))
1249     			return;
1250     		ch->ccw[1].count = ch->max_bufsize;
1251     		fsm_newstate(fi, CH_STATE_RXIDLE);
1252     #ifdef DEBUG
1253     	printk(KERN_DEBUG "ccw[1].cda = %08x\n", ch->ccw[1].cda);
1254     #endif
1255     		rc = do_IO(ch->irq, &ch->ccw[0], (intparm_t)ch, 0xff, 0);
1256     		if (rc != 0) {
1257     			fsm_newstate(fi, CH_STATE_RXINIT);
1258     			ccw_check_return_code(ch, rc);
1259     		} else
1260     			fsm_event(((ctc_priv *)dev->priv)->fsm,
1261     				  DEV_EVENT_RXUP, dev);
1262     	} else {
1263     		printk(KERN_DEBUG "%s: Initial RX count %d not %d\n",
1264     		       dev->name, buflen, CTC_INITIAL_BLOCKLEN);
1265     		ch_action_firstio(fi, event, arg);
1266     	}
1267     }
1268     
1269     /**
1270      * Set channel into extended mode.
1271      *
1272      * @param fi    An instance of a channel statemachine.
1273      * @param event The event, just happened.
1274      * @param arg   Generic pointer, casted from channel * upon call.
1275      */
1276     static void ch_action_setmode(fsm_instance *fi, int event, void *arg)
1277     {
1278     	channel *ch = (channel *)arg;
1279     	int     rc;
1280     	unsigned long saveflags;
1281     
1282     	fsm_deltimer(&ch->timer);
1283     	fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC, CH_EVENT_TIMER, ch);
1284     	fsm_newstate(fi, CH_STATE_SETUPWAIT);
1285     	if (event == CH_EVENT_TIMER)
1286     		s390irq_spin_lock_irqsave(ch->irq, saveflags);
1287     	rc = do_IO(ch->irq, &ch->ccw[6], (intparm_t)ch, 0xff, 0);
1288     	if (event == CH_EVENT_TIMER)
1289     		s390irq_spin_unlock_irqrestore(ch->irq, saveflags);
1290     	if (rc != 0) {
1291     		fsm_deltimer(&ch->timer);
1292     		fsm_newstate(fi, CH_STATE_STARTWAIT);
1293     		ccw_check_return_code(ch, rc);
1294     	} else
1295     		ch->retry = 0;
1296     }
1297     
1298     /**
1299      * Setup channel.
1300      *
1301      * @param fi    An instance of a channel statemachine.
1302      * @param event The event, just happened.
1303      * @param arg   Generic pointer, casted from channel * upon call.
1304      */
1305     static void ch_action_start(fsm_instance *fi, int event, void *arg)
1306     {
1307     	channel *ch = (channel *)arg;
1308     	unsigned long saveflags;
1309     	int     rc;
1310     	net_device *dev;
1311     
1312     	if (ch == NULL) {
1313     		printk(KERN_WARNING "ch_action_start ch=NULL\n");
1314     		return;
1315     	}
1316     	if (ch->netdev == NULL) {
1317     		printk(KERN_WARNING "ch_action_start dev=NULL, irq=%d\n",
1318     		       ch->irq);
1319     		return;
1320     	}
1321     	dev = ch->netdev;
1322     
1323     #ifdef DEBUG
1324     	printk(KERN_DEBUG "%s: %s channel start\n", dev->name,
1325     	       (CHANNEL_DIRECTION(ch->flags) == READ) ? "RX" : "TX");
1326     #endif
1327     
1328     	if (ch->trans_skb != NULL) {
1329     		clear_normalized_cda(&ch->ccw[1]);
1330     		dev_kfree_skb(ch->trans_skb);
1331     		ch->trans_skb = NULL;
1332     	}
1333     	if (ctc_checkalloc_buffer(ch, 0))
1334     		printk(KERN_NOTICE
1335     		       "%s: Could not allocate %s trans_skb, delaying "
1336     		       "allocation until first transfer\n",
1337     		       dev->name, 
1338     		       (CHANNEL_DIRECTION(ch->flags) == READ) ? "RX" : "TX");
1339     
1340     #if LINUX_VERSION_CODE >= 0x020400
1341     	INIT_LIST_HEAD(&ch->tq.list);
1342     #else
1343     	ch->tq.next = NULL;
1344     #endif
1345     	ch->tq.sync    = 0;
1346     	ch->tq.routine = (void *)(void *)ctc_bh;
1347     	ch->tq.data    = ch;
1348     
1349     	ch->ccw[0].cmd_code = CCW_CMD_PREPARE;
1350     	ch->ccw[0].flags    = CCW_FLAG_SLI | CCW_FLAG_CC;
1351     	ch->ccw[0].count    = 0;
1352     	ch->ccw[0].cda	    = 0;
1353     	if (CHANNEL_DIRECTION(ch->flags) == READ) {
1354     		ch->ccw[1].cmd_code = CCW_CMD_READ;
1355     		ch->ccw[1].flags    = CCW_FLAG_SLI;
1356     		ch->ccw[1].count    = 0;
1357     	} else {
1358     		ch->ccw[1].cmd_code = CCW_CMD_WRITE;
1359     		ch->ccw[1].flags    = CCW_FLAG_SLI | CCW_FLAG_CC;
1360     		ch->ccw[1].count    = 0;
1361     	}
1362     	ch->ccw[2].cmd_code = CCW_CMD_NOOP;	 /* jointed CE + DE */
1363     	ch->ccw[2].flags    = CCW_FLAG_SLI;
1364     	ch->ccw[2].count    = 0;
1365     	ch->ccw[2].cda	    = 0;
1366     	memcpy(&ch->ccw[3], &ch->ccw[0], sizeof(ccw1_t) * 3);
1367     	ch->ccw[4].cda	    = 0;
1368     
1369     	fsm_newstate(fi, CH_STATE_STARTWAIT);
1370     	fsm_addtimer(&ch->timer, 1000, CH_EVENT_TIMER, ch);
1371     	s390irq_spin_lock_irqsave(ch->irq, saveflags);
1372     	rc = halt_IO(ch->irq, (intparm_t)ch, 0);
1373     	s390irq_spin_unlock_irqrestore(ch->irq, saveflags);
1374     	if (rc != 0) {
1375     		fsm_deltimer(&ch->timer);
1376     		ccw_check_return_code(ch, rc);
1377     	}
1378     #ifdef DEBUG
1379     	printk(KERN_DEBUG "ctc: %s(): leaving\n", __FUNCTION__);
1380     #endif
1381     }
1382     
1383     /**
1384      * Shutdown a channel.
1385      *
1386      * @param fi    An instance of a channel statemachine.
1387      * @param event The event, just happened.
1388      * @param arg   Generic pointer, casted from channel * upon call.
1389      */
1390     static void ch_action_haltio(fsm_instance *fi, int event, void *arg)
1391     {
1392     	channel *ch = (channel *)arg;
1393     	unsigned long saveflags;
1394     	int     rc;
1395     	int     oldstate;
1396     
1397     	fsm_deltimer(&ch->timer);
1398     	fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC, CH_EVENT_TIMER, ch);
1399     	if (event == CH_EVENT_STOP)
1400     		s390irq_spin_lock_irqsave(ch->irq, saveflags);
1401     	oldstate = fsm_getstate(fi);
1402     	fsm_newstate(fi, CH_STATE_TERM);
1403     	rc = halt_IO (ch->irq, (intparm_t)ch, 0);
1404     	if (event == CH_EVENT_STOP)
1405     		s390irq_spin_unlock_irqrestore(ch->irq, saveflags);
1406     	if (rc != 0) {
1407     		fsm_deltimer(&ch->timer);
1408     		fsm_newstate(fi, oldstate);
1409     		ccw_check_return_code(ch, rc);
1410     	}
1411     }
1412     
1413     /**
1414      * A channel has successfully been halted.
1415      * Cleanup it's queue and notify interface statemachine.
1416      *
1417      * @param fi    An instance of a channel statemachine.
1418      * @param event The event, just happened.
1419      * @param arg   Generic pointer, casted from channel * upon call.
1420      */
1421     static void ch_action_stopped(fsm_instance *fi, int event, void *arg)
1422     {
1423     	channel *ch = (channel *)arg;
1424     	net_device *dev = ch->netdev;
1425     
1426     	fsm_deltimer(&ch->timer);
1427     	fsm_newstate(fi, CH_STATE_STOPPED);
1428     	if (ch->trans_skb != NULL) {
1429     		clear_normalized_cda(&ch->ccw[1]);
1430     		dev_kfree_skb(ch->trans_skb);
1431     		ch->trans_skb = NULL;
1432     	}
1433     	if (CHANNEL_DIRECTION(ch->flags) == READ) {
1434     		skb_queue_purge(&ch->io_queue);
1435     		fsm_event(((ctc_priv *)dev->priv)->fsm, DEV_EVENT_RXDOWN, dev);
1436     	} else {
1437     		ctc_purge_skb_queue(&ch->io_queue);
1438     		spin_lock(&ch->collect_lock);
1439     		ctc_purge_skb_queue(&ch->collect_queue);
1440     		ch->collect_len = 0;
1441     		spin_unlock(&ch->collect_lock);
1442     		fsm_event(((ctc_priv *)dev->priv)->fsm, DEV_EVENT_TXDOWN, dev);
1443     	}
1444     }
1445     
1446     /**
1447      * A stop command from device statemachine arrived and we are in
1448      * not operational mode. Set state to stopped.
1449      *
1450      * @param fi    An instance of a channel statemachine.
1451      * @param event The event, just happened.
1452      * @param arg   Generic pointer, casted from channel * upon call.
1453      */
1454     static void ch_action_stop(fsm_instance *fi, int event, void *arg)
1455     {
1456     	fsm_newstate(fi, CH_STATE_STOPPED);
1457     }
1458     
1459     /**
1460      * A machine check for no path, not operational status or gone device has
1461      * happened.
1462      * Cleanup queue and notify interface statemachine.
1463      *
1464      * @param fi    An instance of a channel statemachine.
1465      * @param event The event, just happened.
1466      * @param arg   Generic pointer, casted from channel * upon call.
1467      */
1468     static void ch_action_fail(fsm_instance *fi, int event, void *arg)
1469     {
1470     	channel *ch = (channel *)arg;
1471     	net_device *dev = ch->netdev;
1472     
1473     	fsm_deltimer(&ch->timer);
1474     	fsm_newstate(fi, CH_STATE_NOTOP);
1475     	if (CHANNEL_DIRECTION(ch->flags) == READ) {
1476     		skb_queue_purge(&ch->io_queue);
1477     		fsm_event(((ctc_priv *)dev->priv)->fsm, DEV_EVENT_RXDOWN, dev);
1478     	} else {
1479     		ctc_purge_skb_queue(&ch->io_queue);
1480     		spin_lock(&ch->collect_lock);
1481     		ctc_purge_skb_queue(&ch->collect_queue);
1482     		ch->collect_len = 0;
1483     		spin_unlock(&ch->collect_lock);
1484     		fsm_event(((ctc_priv *)dev->priv)->fsm, DEV_EVENT_TXDOWN, dev);
1485     	}
1486     }
1487     
1488     /**
1489      * Handle error during setup of channel.
1490      *
1491      * @param fi    An instance of a channel statemachine.
1492      * @param event The event, just happened.
1493      * @param arg   Generic pointer, casted from channel * upon call.
1494      */
1495     static void ch_action_setuperr(fsm_instance *fi, int event, void *arg)
1496     {
1497     	channel *ch = (channel *)arg;
1498     	net_device *dev = ch->netdev;
1499     
1500     	/**
1501     	 * Special case: Got UC_RCRESET on setmode.
1502     	 * This means that remote side isn't setup. In this case
1503     	 * simply retry after some 10 secs...
1504     	 */
1505     	if ((fsm_getstate(fi) == CH_STATE_SETUPWAIT) &&
1506     	    ((event == CH_EVENT_UC_RCRESET) ||
1507     	     (event == CH_EVENT_UC_RSRESET)   )         ) {
1508     		fsm_newstate(fi, CH_STATE_STARTRETRY);
1509     		fsm_deltimer(&ch->timer);
1510     		fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC, CH_EVENT_TIMER, ch);
1511     		if (CHANNEL_DIRECTION(ch->flags) == READ) {
1512     			int rc = halt_IO (ch->irq, (intparm_t)ch, 0);
1513     			if (rc != 0)
1514     				ccw_check_return_code(ch, rc);
1515     		}
1516     		return;
1517     	}
1518     
1519     	printk(KERN_DEBUG "%s: Error %s during %s channel setup state=%s\n",
1520     	       dev->name, ch_event_names[event],
1521     	       (CHANNEL_DIRECTION(ch->flags) == READ) ? "RX" : "TX",
1522     	       fsm_getstate_str(fi));
1523     	if (CHANNEL_DIRECTION(ch->flags) == READ) {
1524     		fsm_newstate(fi, CH_STATE_RXERR);
1525     		fsm_event(((ctc_priv *)dev->priv)->fsm, DEV_EVENT_RXDOWN, dev);
1526     	} else {
1527     		fsm_newstate(fi, CH_STATE_TXERR);
1528     		fsm_event(((ctc_priv *)dev->priv)->fsm, DEV_EVENT_TXDOWN, dev);
1529     	}
1530     }
1531     
1532     /**
1533      * Restart a channel after an error.
1534      *
1535      * @param fi    An instance of a channel statemachine.
1536      * @param event The event, just happened.
1537      * @param arg   Generic pointer, casted from channel * upon call.
1538      */
1539     static void ch_action_restart(fsm_instance *fi, int event, void *arg)
1540     {
1541     	unsigned long saveflags;
1542     	int   oldstate;
1543     	int   rc;
1544     
1545     	channel *ch = (channel *)arg;
1546     	net_device *dev = ch->netdev;
1547     
1548     	printk(KERN_DEBUG "%s: %s channel restart\n", dev->name,
1549     	       (CHANNEL_DIRECTION(ch->flags) == READ) ? "RX" : "TX");
1550     
1551     	fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC, CH_EVENT_TIMER, ch);
1552     	oldstate = fsm_getstate(fi);
1553     	fsm_newstate(fi, CH_STATE_STARTWAIT);
1554     	if (event == CH_EVENT_TIMER)
1555     		s390irq_spin_lock_irqsave(ch->irq, saveflags);
1556     	rc = halt_IO (ch->irq, (intparm_t)ch, 0);
1557     	if (event == CH_EVENT_TIMER)
1558     		s390irq_spin_unlock_irqrestore(ch->irq, saveflags);
1559     	if (rc != 0) {
1560     		fsm_deltimer(&ch->timer);
1561     		fsm_newstate(fi, oldstate);
1562     		ccw_check_return_code(ch, rc);
1563     	}
1564     }
1565     
1566     /**
1567      * Handle error during RX initial handshake (exchange of
1568      * 0-length block header)
1569      *
1570      * @param fi    An instance of a channel statemachine.
1571      * @param event The event, just happened.
1572      * @param arg   Generic pointer, casted from channel * upon call.
1573      */
1574     static void ch_action_rxiniterr(fsm_instance *fi, int event, void *arg)
1575     {
1576     	channel *ch = (channel *)arg;
1577     	net_device *dev = ch->netdev;
1578     
1579     	if (event == CH_EVENT_TIMER) {
1580     		fsm_deltimer(&ch->timer);
1581     		printk(KERN_DEBUG "%s: Timeout during RX init handshake\n",
1582     		       dev->name);
1583     		if (ch->retry++ < 3)
1584     			ch_action_restart(fi, event, arg);
1585     		else {
1586     			fsm_newstate(fi, CH_STATE_RXERR);
1587     			fsm_event(((ctc_priv *)dev->priv)->fsm,
1588     				  DEV_EVENT_RXDOWN, dev);
1589     		}
1590     	} else
1591     		printk(KERN_WARNING "%s: Error during RX init handshake\n",
1592     		       dev->name);
1593     }
1594     
1595     /**
1596      * Notify device statemachine if we gave up initialization
1597      * of RX channel.
1598      *
1599      * @param fi    An instance of a channel statemachine.
1600      * @param event The event, just happened.
1601      * @param arg   Generic pointer, casted from channel * upon call.
1602      */
1603     static void ch_action_rxinitfail(fsm_instance *fi, int event, void *arg)
1604     {
1605     	channel *ch = (channel *)arg;
1606     	net_device *dev = ch->netdev;
1607     
1608     	fsm_newstate(fi, CH_STATE_RXERR);
1609     	printk(KERN_WARNING "%s: RX initialization failed\n", dev->name);
1610     	printk(KERN_WARNING "%s: RX <-> RX connection detected\n", dev->name);
1611     	fsm_event(((ctc_priv *)dev->priv)->fsm, DEV_EVENT_RXDOWN, dev);
1612     }
1613     
1614     /**
1615      * Handle RX Unit check remote reset (remote disconnected)
1616      *
1617      * @param fi    An instance of a channel statemachine.
1618      * @param event The event, just happened.
1619      * @param arg   Generic pointer, casted from channel * upon call.
1620      */
1621     static void ch_action_rxdisc(fsm_instance *fi, int event, void *arg)
1622     {
1623     	channel    *ch = (channel *)arg;
1624     	channel    *ch2;
1625     	net_device *dev = ch->netdev;
1626     
1627     	fsm_deltimer(&ch->timer);
1628     	printk(KERN_DEBUG "%s: Got remote disconnect, re-initializing ...\n",
1629     	       dev->name);
1630     
1631     	/**
1632     	 * Notify device statemachine
1633     	 */
1634     	fsm_event(((ctc_priv *)dev->priv)->fsm, DEV_EVENT_RXDOWN, dev);
1635     	fsm_event(((ctc_priv *)dev->priv)->fsm, DEV_EVENT_TXDOWN, dev);
1636     
1637     	fsm_newstate(fi, CH_STATE_DTERM);
1638     	ch2 = ((ctc_priv *)dev->priv)->channel[WRITE];
1639     	fsm_newstate(ch2->fsm, CH_STATE_DTERM);
1640     
1641     	halt_IO(ch->irq, (intparm_t)ch, 0);
1642     	halt_IO(ch2->irq, (intparm_t)ch2, 0);
1643     }
1644     
1645     /**
1646      * Handle error during TX channel initialization.
1647      *
1648      * @param fi    An instance of a channel statemachine.
1649      * @param event The event, just happened.
1650      * @param arg   Generic pointer, casted from channel * upon call.
1651      */
1652     static void ch_action_txiniterr(fsm_instance *fi, int event, void *arg)
1653     {
1654     	channel *ch = (channel *)arg;
1655     	net_device *dev = ch->netdev;
1656     
1657     	if (event == CH_EVENT_TIMER) {
1658     		fsm_deltimer(&ch->timer);
1659     		printk(KERN_DEBUG "%s: Timeout during TX init handshake\n",
1660     		       dev->name);
1661     		if (ch->retry++ < 3)
1662     			ch_action_restart(fi, event, arg);
1663     		else {
1664     			fsm_newstate(fi, CH_STATE_TXERR);
1665     			fsm_event(((ctc_priv *)dev->priv)->fsm,
1666     				  DEV_EVENT_TXDOWN, dev);
1667     		}
1668     	} else
1669     		printk(KERN_WARNING "%s: Error during TX init handshake\n",
1670     		       dev->name);
1671     }
1672     
1673     /**
1674      * Handle TX timeout by retrying operation.
1675      *
1676      * @param fi    An instance of a channel statemachine.
1677      * @param event The event, just happened.
1678      * @param arg   Generic pointer, casted from channel * upon call.
1679      */
1680     static void ch_action_txretry(fsm_instance *fi, int event, void *arg)
1681     {
1682     	channel    *ch = (channel *)arg;
1683     	net_device *dev = ch->netdev;
1684     	unsigned long saveflags;
1685     
1686     	fsm_deltimer(&ch->timer);
1687     	if (ch->retry++ > 3) {
1688     		printk(KERN_DEBUG "%s: TX retry failed, restarting channel\n",
1689     		       dev->name);
1690     		fsm_event(((ctc_priv *)dev->priv)->fsm, DEV_EVENT_TXDOWN, dev);
1691     		ch_action_restart(fi, event, arg);
1692     	} else {
1693     		struct sk_buff *skb;
1694     
1695     		printk(KERN_DEBUG "%s: TX retry %d\n", dev->name, ch->retry);
1696     		if ((skb = skb_peek(&ch->io_queue))) {
1697     			int rc = 0;
1698     
1699     			set_normalized_cda(&ch->ccw[4],
1700     					   virt_to_phys(skb->data));
1701     			if (ch->ccw[4].cda == 0) {
1702     				printk(KERN_DEBUG "%s: IDAL alloc failed, "
1703     				       "restarting channel\n", dev->name);
1704     				fsm_event(((ctc_priv *)dev->priv)->fsm,
1705     					  DEV_EVENT_TXDOWN, dev);
1706     				ch_action_restart(fi, event, arg);
1707     				return;
1708     			}
1709     			fsm_addtimer(&ch->timer, 1000, CH_EVENT_TIMER, ch);
1710     			if (event == CH_EVENT_TIMER)
1711     				s390irq_spin_lock_irqsave(ch->irq, saveflags);
1712     			ch->ccw[4].count = skb->len;
1713     #ifdef DEBUG
1714     	printk(KERN_DEBUG "ccw[4].cda = %08x\n", ch->ccw[4].cda);
1715     #endif
1716     			rc = do_IO(ch->irq, &ch->ccw[3],
1717     				   (intparm_t)ch, 0xff, 0);
1718     			if (event == CH_EVENT_TIMER)
1719     				s390irq_spin_unlock_irqrestore(ch->irq,
1720     							       saveflags);
1721     			if (rc != 0) {
1722     				fsm_deltimer(&ch->timer);
1723     				ccw_check_return_code(ch, rc);
1724     				ctc_purge_skb_queue(&ch->io_queue);
1725     			}
1726     		}
1727     	}
1728     
1729     }
1730     
1731     /**
1732      * Handle fatal errors during an I/O command.
1733      *
1734      * @param fi    An instance of a channel statemachine.
1735      * @param event The event, just happened.
1736      * @param arg   Generic pointer, casted from channel * upon call.
1737      */
1738     static void ch_action_iofatal(fsm_instance *fi, int event, void *arg)
1739     {
1740     	channel *ch = (channel *)arg;
1741     	net_device *dev = ch->netdev;
1742     
1743     	fsm_deltimer(&ch->timer);
1744     	if (CHANNEL_DIRECTION(ch->flags) == READ) {
1745     		printk(KERN_DEBUG "%s: RX I/O error\n", dev->name);
1746     		fsm_newstate(fi, CH_STATE_RXERR);
1747     		fsm_event(((ctc_priv *)dev->priv)->fsm, DEV_EVENT_RXDOWN, dev);
1748     	} else {
1749     		printk(KERN_DEBUG "%s: TX I/O error\n", dev->name);
1750     		fsm_newstate(fi, CH_STATE_TXERR);
1751     		fsm_event(((ctc_priv *)dev->priv)->fsm, DEV_EVENT_TXDOWN, dev);
1752     	}
1753     }
1754     
1755     /**
1756      * The statemachine for a channel.
1757      */
1758     static const fsm_node ch_fsm[] = {
1759     	{ CH_STATE_STOPPED,    CH_EVENT_STOP,       fsm_action_nop       },
1760     	{ CH_STATE_STOPPED,    CH_EVENT_START,      ch_action_start      },
1761     	{ CH_STATE_STOPPED,    CH_EVENT_FINSTAT,    fsm_action_nop       },
1762     	{ CH_STATE_STOPPED,    CH_EVENT_MC_FAIL,    fsm_action_nop       },
1763     
1764     	{ CH_STATE_NOTOP,      CH_EVENT_STOP,       ch_action_stop       },
1765     	{ CH_STATE_NOTOP,      CH_EVENT_START,      fsm_action_nop       },
1766     	{ CH_STATE_NOTOP,      CH_EVENT_FINSTAT,    fsm_action_nop       },
1767     	{ CH_STATE_NOTOP,      CH_EVENT_MC_FAIL,    fsm_action_nop       },
1768     	{ CH_STATE_NOTOP,      CH_EVENT_MC_GOOD,    ch_action_start      },
1769     
1770     	{ CH_STATE_STARTWAIT,  CH_EVENT_STOP,       ch_action_haltio     },
1771     	{ CH_STATE_STARTWAIT,  CH_EVENT_START,      fsm_action_nop       },
1772     	{ CH_STATE_STARTWAIT,  CH_EVENT_FINSTAT,    ch_action_setmode    },
1773     	{ CH_STATE_STARTWAIT,  CH_EVENT_TIMER,      ch_action_setuperr   },
1774     	{ CH_STATE_STARTWAIT,  CH_EVENT_IO_ENODEV,  ch_action_iofatal    },
1775     	{ CH_STATE_STARTWAIT,  CH_EVENT_IO_EIO,     ch_action_iofatal    },
1776     	{ CH_STATE_STARTWAIT,  CH_EVENT_MC_FAIL,    ch_action_fail       },
1777     
1778     	{ CH_STATE_STARTRETRY, CH_EVENT_STOP,       ch_action_haltio     },
1779     	{ CH_STATE_STARTRETRY, CH_EVENT_TIMER,      ch_action_setmode    },
1780     	{ CH_STATE_STARTRETRY, CH_EVENT_FINSTAT,    fsm_action_nop       },
1781     	{ CH_STATE_STARTRETRY, CH_EVENT_MC_FAIL,    ch_action_fail       },
1782     
1783     	{ CH_STATE_SETUPWAIT,  CH_EVENT_STOP,       ch_action_haltio     },
1784     	{ CH_STATE_SETUPWAIT,  CH_EVENT_START,      fsm_action_nop       },
1785     	{ CH_STATE_SETUPWAIT,  CH_EVENT_FINSTAT,    ch_action_firstio    },
1786     	{ CH_STATE_SETUPWAIT,  CH_EVENT_UC_RCRESET, ch_action_setuperr   },
1787     	{ CH_STATE_SETUPWAIT,  CH_EVENT_UC_RSRESET, ch_action_setuperr   },
1788     	{ CH_STATE_SETUPWAIT,  CH_EVENT_TIMER,      ch_action_setmode    },
1789     	{ CH_STATE_SETUPWAIT,  CH_EVENT_IO_ENODEV,  ch_action_iofatal    },
1790     	{ CH_STATE_SETUPWAIT,  CH_EVENT_IO_EIO,     ch_action_iofatal    },
1791     	{ CH_STATE_SETUPWAIT,  CH_EVENT_MC_FAIL,    ch_action_fail       },
1792     
1793     	{ CH_STATE_RXINIT,     CH_EVENT_STOP,       ch_action_haltio     },
1794     	{ CH_STATE_RXINIT,     CH_EVENT_START,      fsm_action_nop       },
1795     	{ CH_STATE_RXINIT,     CH_EVENT_FINSTAT,    ch_action_rxidle     },
1796     	{ CH_STATE_RXINIT,     CH_EVENT_UC_RCRESET, ch_action_rxiniterr  },
1797     	{ CH_STATE_RXINIT,     CH_EVENT_UC_RSRESET, ch_action_rxiniterr  },
1798     	{ CH_STATE_RXINIT,     CH_EVENT_TIMER,      ch_action_rxiniterr  },
1799     	{ CH_STATE_RXINIT,     CH_EVENT_ATTNBUSY,   ch_action_rxinitfail },
1800     	{ CH_STATE_RXINIT,     CH_EVENT_IO_ENODEV,  ch_action_iofatal    },
1801     	{ CH_STATE_RXINIT,     CH_EVENT_IO_EIO,     ch_action_iofatal    },
1802     	{ CH_STATE_RXINIT,     CH_EVENT_UC_ZERO,    ch_action_firstio    },
1803     	{ CH_STATE_RXINIT,     CH_EVENT_MC_FAIL,    ch_action_fail       },
1804     
1805     	{ CH_STATE_RXIDLE,     CH_EVENT_STOP,       ch_action_haltio     },
1806     	{ CH_STATE_RXIDLE,     CH_EVENT_START,      fsm_action_nop       },
1807     	{ CH_STATE_RXIDLE,     CH_EVENT_FINSTAT,    ch_action_rx         },
1808     	{ CH_STATE_RXIDLE,     CH_EVENT_UC_RCRESET, ch_action_rxdisc     },
1809     //	{ CH_STATE_RXIDLE,     CH_EVENT_UC_RSRESET, ch_action_rxretry    },
1810     	{ CH_STATE_RXIDLE,     CH_EVENT_IO_ENODEV,  ch_action_iofatal    },
1811     	{ CH_STATE_RXIDLE,     CH_EVENT_IO_EIO,     ch_action_iofatal    },
1812     	{ CH_STATE_RXIDLE,     CH_EVENT_MC_FAIL,    ch_action_fail       },
1813     	{ CH_STATE_RXIDLE,     CH_EVENT_UC_ZERO,    ch_action_rx         },
1814     
1815     	{ CH_STATE_TXINIT,     CH_EVENT_STOP,       ch_action_haltio     },
1816     	{ CH_STATE_TXINIT,     CH_EVENT_START,      fsm_action_nop       },
1817     	{ CH_STATE_TXINIT,     CH_EVENT_FINSTAT,    ch_action_txidle     },
1818     	{ CH_STATE_TXINIT,     CH_EVENT_UC_RCRESET, ch_action_txiniterr  },
1819     	{ CH_STATE_TXINIT,     CH_EVENT_UC_RSRESET, ch_action_txiniterr  },
1820     	{ CH_STATE_TXINIT,     CH_EVENT_TIMER,      ch_action_txiniterr  },
1821     	{ CH_STATE_TXINIT,     CH_EVENT_IO_ENODEV,  ch_action_iofatal    },
1822     	{ CH_STATE_TXINIT,     CH_EVENT_IO_EIO,     ch_action_iofatal    },
1823     	{ CH_STATE_TXINIT,     CH_EVENT_MC_FAIL,    ch_action_fail       },
1824     
1825     	{ CH_STATE_TXIDLE,     CH_EVENT_STOP,       ch_action_haltio     },
1826     	{ CH_STATE_TXIDLE,     CH_EVENT_START,      fsm_action_nop       },
1827     	{ CH_STATE_TXIDLE,     CH_EVENT_FINSTAT,    ch_action_firstio    },
1828     	{ CH_STATE_TXIDLE,     CH_EVENT_UC_RCRESET, fsm_action_nop       },
1829     	{ CH_STATE_TXIDLE,     CH_EVENT_UC_RSRESET, fsm_action_nop       },
1830     	{ CH_STATE_TXIDLE,     CH_EVENT_IO_ENODEV,  ch_action_iofatal    },
1831     	{ CH_STATE_TXIDLE,     CH_EVENT_IO_EIO,     ch_action_iofatal    },
1832     	{ CH_STATE_TXIDLE,     CH_EVENT_MC_FAIL,    ch_action_fail       },
1833     
1834     	{ CH_STATE_TERM,       CH_EVENT_STOP,       fsm_action_nop       },
1835     	{ CH_STATE_TERM,       CH_EVENT_START,      ch_action_restart    },
1836     	{ CH_STATE_TERM,       CH_EVENT_FINSTAT,    ch_action_stopped    },
1837     	{ CH_STATE_TERM,       CH_EVENT_UC_RCRESET, fsm_action_nop       },
1838     	{ CH_STATE_TERM,       CH_EVENT_UC_RSRESET, fsm_action_nop       },
1839     	{ CH_STATE_TERM,       CH_EVENT_MC_FAIL,    ch_action_fail       },
1840     
1841     	{ CH_STATE_DTERM,      CH_EVENT_STOP,       ch_action_haltio     },
1842     	{ CH_STATE_DTERM,      CH_EVENT_START,      ch_action_restart    },
1843     	{ CH_STATE_DTERM,      CH_EVENT_FINSTAT,    ch_action_setmode    },
1844     	{ CH_STATE_DTERM,      CH_EVENT_UC_RCRESET, fsm_action_nop       },
1845     	{ CH_STATE_DTERM,      CH_EVENT_UC_RSRESET, fsm_action_nop       },
1846     	{ CH_STATE_DTERM,      CH_EVENT_MC_FAIL,    ch_action_fail       },
1847     
1848     	{ CH_STATE_TX,         CH_EVENT_STOP,       ch_action_haltio     },
1849     	{ CH_STATE_TX,         CH_EVENT_START,      fsm_action_nop       },
1850     	{ CH_STATE_TX,         CH_EVENT_FINSTAT,    ch_action_txdone     },
1851     	{ CH_STATE_TX,         CH_EVENT_UC_RCRESET, ch_action_txretry    },
1852     	{ CH_STATE_TX,         CH_EVENT_UC_RSRESET, ch_action_txretry    },
1853     	{ CH_STATE_TX,         CH_EVENT_TIMER,      ch_action_txretry    },
1854     	{ CH_STATE_TX,         CH_EVENT_IO_ENODEV,  ch_action_iofatal    },
1855     	{ CH_STATE_TX,         CH_EVENT_IO_EIO,     ch_action_iofatal    },
1856     	{ CH_STATE_TX,         CH_EVENT_MC_FAIL,    ch_action_fail       },
1857     
1858     	{ CH_STATE_RXERR,      CH_EVENT_STOP,       ch_action_haltio     },
1859     	{ CH_STATE_TXERR,      CH_EVENT_STOP,       ch_action_haltio     },
1860     	{ CH_STATE_TXERR,      CH_EVENT_MC_FAIL,    ch_action_fail       },
1861     	{ CH_STATE_RXERR,      CH_EVENT_MC_FAIL,    ch_action_fail       },
1862     };
1863     
1864     static const int CH_FSM_LEN = sizeof(ch_fsm) / sizeof(fsm_node);
1865     
1866     /**
1867      * Functions related to setup and device detection.
1868      *****************************************************************************/
1869     
1870     /**
1871      * Add a new channel to the list of channels.
1872      * Keeps the channel list sorted.
1873      *
1874      * @param irq   The IRQ to be used by the new channel.
1875      * @param devno The device number of the new channel.
1876      * @param type  The type class of the new channel.
1877      *
1878      * @return 0 on success, !0 on error.
1879      */
1880     static int add_channel(int irq, __u16 devno, channel_type_t type)
1881     {
1882     	channel **c = &channels;
1883     	channel *ch;
1884     	char name[10];
1885     
1886     	if ((ch = (channel *)kmalloc(sizeof(channel), GFP_KERNEL)) == NULL) {
1887     		printk(KERN_WARNING "ctc: Out of memory in add_channel\n");
1888     		return -1;
1889     	}
1890     	memset(ch, 0, sizeof(channel));
1891     	if ((ch->ccw = (ccw1_t *)kmalloc(sizeof(ccw1_t) * 8,
1892     					 GFP_KERNEL|GFP_DMA)) == NULL) {
1893     		kfree(ch);
1894     		printk(KERN_WARNING "ctc: Out of memory in add_channel\n");
1895     		return -1;
1896     	}
1897     
1898     	/**
1899     	 * "static" ccws are used in the following way:
1900     	 *
1901     	 * ccw[0..2] (Channel program for generic I/O):
1902     	 *           0: prepare
1903     	 *           1: read or write (depending on direction) with fixed
1904     	 *              buffer (idal allocated once when buffer is allocated)
1905     	 *           2: nop
1906     	 * ccw[3..5] (Channel program for direct write of packets)
1907     	 *           3: prepare
1908     	 *           4: write (idal allocated on every write).
1909     	 *           5: nop
1910     	 * ccw[6..7] (Channel program for initial channel setup):
1911     	 *           3: set extended mode
1912     	 *           4: nop
1913     	 *
1914     	 * ch->ccw[0..5] are initialized in ch_action_start because
1915     	 * the channel's direction is yet unknown here.
1916     	 */
1917     	ch->ccw[6].cmd_code = CCW_CMD_SET_EXTENDED;
1918     	ch->ccw[6].flags    = CCW_FLAG_SLI;
1919     	ch->ccw[6].count    = 0;
1920     	ch->ccw[6].cda      = 0;
1921     	
1922     	ch->ccw[7].cmd_code = CCW_CMD_NOOP;
1923     	ch->ccw[7].flags    = CCW_FLAG_SLI;
1924     	ch->ccw[7].count    = 0;
1925     	ch->ccw[7].cda      = 0;
1926     
1927     	ch->irq = irq;
1928     	ch->devno = devno;
1929     	ch->type = type;
1930     	sprintf(name, "ch-%04x", devno);
1931     	ch->fsm = init_fsm(name, ch_state_names,
1932     			ch_event_names, NR_CH_STATES, NR_CH_EVENTS,
1933     			ch_fsm, CH_FSM_LEN, GFP_KERNEL);
1934     	if (ch->fsm == NULL) {
1935     		printk(KERN_WARNING
1936     		       "ctc: Could not create FSM in add_channel\n");
1937     		kfree(ch);
1938     		return -1;
1939     	}
1940     	fsm_newstate(ch->fsm, CH_STATE_IDLE);
1941     	if ((ch->devstat = (devstat_t*)kmalloc(sizeof(devstat_t), GFP_KERNEL))
1942     	    == NULL) {
1943     		printk(KERN_WARNING "ctc: Out of memory in add_channel\n");
1944     		kfree_fsm(ch->fsm);
1945     		kfree(ch);
1946     		return -1;
1947     	}
1948     	memset(ch->devstat, 0, sizeof(devstat_t));
1949     	while (*c && ((*c)->devno < devno))
1950     		c = &(*c)->next;
1951     	if ((*c)->devno == devno) {
1952     		printk(KERN_DEBUG
1953     		       "ctc: add_channel: device %04x already in list, "
1954     		       "using old entry\n", (*c)->devno);
1955     		kfree(ch->devstat);
1956     		kfree_fsm(ch->fsm);
1957     		kfree(ch);
1958     		return 0;
1959     	}
1960     	fsm_settimer(ch->fsm, &ch->timer);
1961     	skb_queue_head_init(&ch->io_queue);
1962     	skb_queue_head_init(&ch->collect_queue);
1963     	ch->next = *c;
1964     	*c = ch;
1965     	return 0;
1966     }
1967     
1968     #ifndef CTC_CHANDEV
1969     /**
1970      * scan for all channels and create an entry in the channels list
1971      * for every supported channel.
1972      */
1973     static void channel_scan(void)
1974     {
1975     	static int      print_result = 1;
1976     	int	        irq;
1977     	int             nr_escon = 0;
1978     	int             nr_ctca  = 0;
1979     	s390_dev_info_t di;
1980     
1981     	for (irq = 0; irq < NR_IRQS; irq++) {
1982     		if (get_dev_info_by_irq(irq, &di) == 0) {
1983     			if ((di.status == DEVSTAT_NOT_OPER) ||
1984     			    (di.status == DEVSTAT_DEVICE_OWNED))
1985     				continue;
1986     			switch (channel_type(&di.sid_data)) {
1987     				case channel_type_ctca:
1988     					/* CTC/A */
1989     					if (!add_channel(irq, di.devno,
1990     							 channel_type_ctca))
1991     						nr_ctca++;
1992     					break;
1993     				case channel_type_escon:
1994     					/* ESCON */
1995     					if (!add_channel(irq, di.devno,
1996     							 channel_type_escon))
1997     						nr_escon++;
1998     					break;
1999     			default:
2000     			}
2001     		}
2002     	}
2003     	if (print_result) {
2004     		if (nr_escon + nr_ctca)
2005     			printk(KERN_INFO
2006     			       "ctc: %d CTC/A channel%s and %d ESCON "
2007     			       "channel%s found.\n",
2008     			       nr_ctca, (nr_ctca == 1) ? "s" : "",
2009     			       nr_escon, (nr_escon == 1) ? "s" : "");
2010     		else
2011     			printk(KERN_INFO "ctc: No channel devices found.\n");
2012     	}
2013     	print_result = 0;
2014     }
2015     #endif
2016     
2017     /**
2018      * Release a specific channel in the channel list.
2019      *
2020      * @param ch Pointer to channel struct to be released.
2021      */
2022     static void channel_free(channel *ch)
2023     {
2024     	ch->flags &= ~CHANNEL_FLAGS_INUSE;
2025     	fsm_newstate(ch->fsm, CH_STATE_IDLE);
2026     }
2027     
2028     /**
2029      * Remove a specific channel in the channel list.
2030      *
2031      * @param ch Pointer to channel struct to be released.
2032      */
2033     static void channel_remove(channel *ch)
2034     {
2035     	channel **c = &channels;
2036     
2037     	if (ch == NULL)
2038     		return;
2039     
2040     #ifndef CTC_CHANDEV
2041     	if (ch->flags & CHANNEL_FLAGS_INUSE)
2042     		FREE_IRQ(ch->irq, ch->devstat);
2043     #endif
2044     	channel_free(ch);
2045     	while (*c) {
2046     		if (*c == ch) {
2047     			*c = ch->next;
2048     			fsm_deltimer(&ch->timer);
2049     			kfree_fsm(ch->fsm);
2050     			clear_normalized_cda(&ch->ccw[4]);
2051     			if (ch->trans_skb != NULL) {
2052     				clear_normalized_cda(&ch->ccw[1]);
2053     				dev_kfree_skb(ch->trans_skb);
2054     			}
2055     			kfree(ch->ccw);
2056     			return;
2057     		}
2058     		c = &((*c)->next);
2059     	}
2060     }
2061     
2062     
2063     /**
2064      * Get a specific channel from the channel list.
2065      *
2066      * @param type Type of channel we are interested in.
2067      * @param devno Device number of channel we are interested in.
2068      * @param direction Direction we want to use this channel for.
2069      *
2070      * @return Pointer to a channel or NULL if no matching channel available.
2071      */
2072     static channel *channel_get(channel_type_t type, int devno, int direction)
2073     {
2074     	channel *ch = channels;
2075     
2076     #ifdef DEBUG
2077     	printk(KERN_DEBUG
2078     	       "ctc: %s(): searching for ch with devno %d and type %d\n",
2079     	       __FUNCTION__, devno, type);
2080     #endif
2081     
2082     	while (ch && ((ch->devno != devno) || (ch->type != type))) {
2083     #ifdef DEBUG
2084     		printk(KERN_DEBUG
2085     		       "ctc: %s(): ch=0x%p (devno=%d, type=%d\n",
2086     		       __FUNCTION__, ch, ch->devno, ch->type);
2087     #endif
2088     		ch = ch->next;
2089     	}
2090     #ifdef DEBUG
2091     	printk(KERN_DEBUG
2092     	       "ctc: %s(): ch=0x%pq (devno=%d, type=%d\n",
2093     	       __FUNCTION__, ch, ch->devno, ch->type);
2094     #endif
2095     	if (!ch) {
2096     		printk(KERN_WARNING "ctc: %s(): channel with devno %d "
2097     		       "and type %d not found in channel list\n",
2098     	       __FUNCTION__, devno, type);
2099     	}
2100     	else {
2101     		if (ch->flags & CHANNEL_FLAGS_INUSE)
2102     			ch = NULL;
2103     		else {
2104     			ch->flags |= CHANNEL_FLAGS_INUSE;
2105     			ch->flags &= ~CHANNEL_FLAGS_RWMASK;
2106     			ch->flags |= (direction == WRITE)
2107     				? CHANNEL_FLAGS_WRITE:CHANNEL_FLAGS_READ;
2108     			fsm_newstate(ch->fsm, CH_STATE_STOPPED);
2109     		}
2110     	}
2111     	return ch;
2112     }
2113     
2114     #ifndef CTC_CHANDEV
2115     /**
2116      * Get the next free channel from the channel list
2117      *
2118      * @param type Type of channel we are interested in.
2119      * @param direction Direction we want to use this channel for.
2120      *
2121      * @return Pointer to a channel or NULL if no matching channel available.
2122      */
2123     static channel *channel_get_next(channel_type_t type, int direction)
2124     {
2125     	channel *ch = channels;
2126     
2127     	while (ch && (ch->type != type || (ch->flags & CHANNEL_FLAGS_INUSE)))
2128     		ch = ch->next;
2129     	if (ch) {
2130     		ch->flags |= CHANNEL_FLAGS_INUSE;
2131     		ch->flags &= ~CHANNEL_FLAGS_RWMASK;
2132     		ch->flags |= (direction == WRITE)
2133     			? CHANNEL_FLAGS_WRITE:CHANNEL_FLAGS_READ;
2134     		fsm_newstate(ch->fsm, CH_STATE_STOPPED);
2135     	}
2136     	return ch;
2137     }
2138     #endif
2139     
2140     /**
2141      * Return the channel type by name.
2142      *
2143      * @param name Name of network interface.
2144      *
2145      * @return Type class of channel to be used for that interface.
2146      */
2147     static channel_type_t inline extract_channel_media(char *name)
2148     {
2149     	channel_type_t ret = channel_type_unknown;
2150     
2151     	if (name != NULL) {
2152     		if (strncmp(name, "ctc", 3) == 0)
2153     			ret = channel_type_ctca;
2154     		if (strncmp(name, "escon", 5) == 0)
2155     			ret = channel_type_escon;
2156     	}
2157     	return ret;
2158     }
2159     
2160     /**
2161      * Find a channel in the list by its IRQ.
2162      *
2163      * @param irq IRQ to search for.
2164      *
2165      * @return Pointer to channel or NULL if no matching channel found.
2166      */
2167     static channel *find_channel_by_irq(int irq)
2168     {
2169     	channel *ch = channels;
2170     	while (ch && (ch->irq != irq))
2171     		ch = ch->next;
2172     	return ch;
2173     }
2174     
2175     /**
2176      * Main IRQ handler.
2177      *
2178      * @param irq     The IRQ to handle.
2179      * @param intparm IRQ params.
2180      * @param regs    CPU registers.
2181      */
2182     static void ctc_irq_handler (int irq, void *intparm, struct pt_regs *regs)
2183     {
2184     	devstat_t  *devstat = (devstat_t *)intparm;
2185     	channel    *ch = (channel *)devstat->intparm;
2186     	net_device *dev;
2187     
2188     	/**
2189     	 * Check for unsolicited interrupts.
2190     	 * If intparm is NULL, then loop over all our known
2191     	 * channels and try matching the irq number.
2192     	 */
2193     	if (ch == NULL) {
2194     		if ((ch = find_channel_by_irq(irq)) == NULL) {
2195     			printk(KERN_WARNING
2196     			       "ctc: Got unsolicited irq: %04x c-%02x d-%02x"
2197     			       "f-%02x\n", devstat->devno, devstat->cstat,
2198     			       devstat->dstat, devstat->flag);
2199     			goto done;
2200     		}
2201     	}
2202     
2203     	dev = (net_device *)(ch->netdev);
2204     	if (dev == NULL) {
2205     		printk(KERN_CRIT
2206     		       "ctc: ctc_irq_handler dev = NULL irq=%d, ch=0x%p\n",
2207     		       irq, ch);
2208     		goto done;
2209     	}
2210     	if (intparm == NULL)
2211     		printk(KERN_DEBUG "%s: Channel %04x found by IRQ %d\n",
2212     		       dev->name, ch->devno, irq);
2213     
2214     #ifdef DEBUG
2215     	printk(KERN_DEBUG
2216     	       "%s: interrupt for device: %04x received c-%02x d-%02x "
2217     	       "f-%02x\n", dev->name, devstat->devno, devstat->cstat,
2218     	       devstat->dstat, devstat->flag);
2219     #endif
2220     
2221     	/* Check for good subchannel return code, otherwise error message */
2222     	if (devstat->cstat) {
2223     		fsm_event(ch->fsm, CH_EVENT_SC_UNKNOWN, ch);
2224     		printk(KERN_WARNING
2225     		       "%s: subchannel check for device: %04x - %02x %02x "
2226     		       "%02x\n", dev->name, ch->devno, devstat->cstat,
2227     		       devstat->dstat, devstat->flag);
2228     		goto done;
2229     	}
2230     
2231     	/* Check the reason-code of a unit check */
2232     	if (devstat->dstat & DEV_STAT_UNIT_CHECK) {
2233     		ccw_unit_check(ch, devstat->ii.sense.data[0]);
2234     		goto done;
2235     	}
2236     	if (devstat->dstat & DEV_STAT_BUSY) {
2237     		if (devstat->dstat & DEV_STAT_ATTENTION)
2238     			fsm_event(ch->fsm, CH_EVENT_ATTNBUSY, ch);
2239     		else
2240     			fsm_event(ch->fsm, CH_EVENT_BUSY, ch);
2241     		goto done;
2242     	}
2243     	if (devstat->dstat & DEV_STAT_ATTENTION) {
2244     		fsm_event(ch->fsm, CH_EVENT_ATTN, ch);
2245     		goto done;
2246     	}
2247     	if (devstat->flag & DEVSTAT_FINAL_STATUS)
2248     		fsm_event(ch->fsm, CH_EVENT_FINSTAT, ch);
2249     	else
2250     		fsm_event(ch->fsm, CH_EVENT_IRQ, ch);
2251     
2252      done:
2253     }
2254     
2255     /**
2256      * Actions for interface - statemachine.
2257      *****************************************************************************/
2258     
2259     /**
2260      * Startup channels by sending CH_EVENT_START to each channel.
2261      *
2262      * @param fi    An instance of an interface statemachine.
2263      * @param event The event, just happened.
2264      * @param arg   Generic pointer, casted from net_device * upon call.
2265      */
2266     static void dev_action_start(fsm_instance *fi, int event, void *arg)
2267     {
2268     	net_device *dev = (net_device *)arg;
2269     	ctc_priv   *privptr = dev->priv;
2270     	int        direction;
2271     
2272     	fsm_newstate(fi, DEV_STATE_STARTWAIT_RXTX);
2273     	for (direction = READ; direction <= WRITE; direction++) {
2274     		channel *ch = privptr->channel[direction];
2275     		fsm_event(ch->fsm, CH_EVENT_START, ch);
2276     	}
2277     }
2278     
2279     /**
2280      * Shutdown channels by sending CH_EVENT_STOP to each channel.
2281      *
2282      * @param fi    An instance of an interface statemachine.
2283      * @param event The event, just happened.
2284      * @param arg   Generic pointer, casted from net_device * upon call.
2285      */
2286     static void dev_action_stop(fsm_instance *fi, int event, void *arg)
2287     {
2288     	net_device *dev = (net_device *)arg;
2289     	ctc_priv   *privptr = dev->priv;
2290     	int        direction;
2291     
2292     	fsm_newstate(fi, DEV_STATE_STOPWAIT_RXTX);
2293     	for (direction = READ; direction <= WRITE; direction++) {
2294     		channel *ch = privptr->channel[direction];
2295     		fsm_event(ch->fsm, CH_EVENT_STOP, ch);
2296     	}
2297     }
2298     
2299     /**
2300      * Called from channel statemachine
2301      * when a channel is up and running.
2302      *
2303      * @param fi    An instance of an interface statemachine.
2304      * @param event The event, just happened.
2305      * @param arg   Generic pointer, casted from net_device * upon call.
2306      */
2307     static void dev_action_chup(fsm_instance *fi, int event, void *arg)
2308     {
2309     	net_device *dev = (net_device *)arg;
2310     	ctc_priv   *privptr = dev->priv;
2311     
2312     	switch (fsm_getstate(fi)) {
2313     		case DEV_STATE_STARTWAIT_RXTX:
2314     			if (event == DEV_EVENT_RXUP)
2315     				fsm_newstate(fi, DEV_STATE_STARTWAIT_TX);
2316     			else
2317     				fsm_newstate(fi, DEV_STATE_STARTWAIT_RX);
2318     			break;
2319     		case DEV_STATE_STARTWAIT_RX:
2320     			if (event == DEV_EVENT_RXUP) {
2321     				fsm_newstate(fi, DEV_STATE_RUNNING);
2322     				printk(KERN_INFO
2323     				       "%s: connected with remote side\n",
2324     				       dev->name);
2325     				if (privptr->protocol == CTC_PROTO_LINUX_TTY)
2326     					ctc_tty_setcarrier(dev, 1);
2327     				ctc_clear_busy(dev);
2328     			}
2329     			break;
2330     		case DEV_STATE_STARTWAIT_TX:
2331     			if (event == DEV_EVENT_TXUP) {
2332     				fsm_newstate(fi, DEV_STATE_RUNNING);
2333     				printk(KERN_INFO
2334     				       "%s: connected with remote side\n",
2335     				       dev->name);
2336     				if (privptr->protocol == CTC_PROTO_LINUX_TTY)
2337     					ctc_tty_setcarrier(dev, 1);
2338     				ctc_clear_busy(dev);
2339     			}
2340     			break;
2341     		case DEV_STATE_STOPWAIT_TX:
2342     			if (event == DEV_EVENT_RXUP)
2343     				fsm_newstate(fi, DEV_STATE_STOPWAIT_RXTX);
2344     			break;
2345     		case DEV_STATE_STOPWAIT_RX:
2346     			if (event == DEV_EVENT_TXUP)
2347     				fsm_newstate(fi, DEV_STATE_STOPWAIT_RXTX);
2348     			break;
2349     	}
2350     }
2351     
2352     /**
2353      * Called from channel statemachine
2354      * when a channel has been shutdown.
2355      *
2356      * @param fi    An instance of an interface statemachine.
2357      * @param event The event, just happened.
2358      * @param arg   Generic pointer, casted from net_device * upon call.
2359      */
2360     static void dev_action_chdown(fsm_instance *fi, int event, void *arg)
2361     {
2362     	net_device *dev = (net_device *)arg;
2363     	ctc_priv   *privptr = dev->priv;
2364     
2365     	switch (fsm_getstate(fi)) {
2366     		case DEV_STATE_RUNNING:
2367     			if (privptr->protocol == CTC_PROTO_LINUX_TTY)
2368     				ctc_tty_setcarrier(dev, 0);
2369     			if (event == DEV_EVENT_TXDOWN)
2370     				fsm_newstate(fi, DEV_STATE_STARTWAIT_TX);
2371     			else
2372     				fsm_newstate(fi, DEV_STATE_STARTWAIT_RX);
2373     			break;
2374     		case DEV_STATE_STARTWAIT_RX:
2375     			if (event == DEV_EVENT_TXDOWN)
2376     				fsm_newstate(fi, DEV_STATE_STARTWAIT_RXTX);
2377     			break;
2378     		case DEV_STATE_STARTWAIT_TX:
2379     			if (event == DEV_EVENT_RXDOWN)
2380     				fsm_newstate(fi, DEV_STATE_STARTWAIT_RXTX);
2381     			break;
2382     		case DEV_STATE_STOPWAIT_RXTX:
2383     			if (event == DEV_EVENT_TXDOWN)
2384     				fsm_newstate(fi, DEV_STATE_STOPWAIT_RX);
2385     			else
2386     				fsm_newstate(fi, DEV_STATE_STOPWAIT_TX);
2387     			break;
2388     		case DEV_STATE_STOPWAIT_RX:
2389     			if (event == DEV_EVENT_RXDOWN)
2390     				fsm_newstate(fi, DEV_STATE_STOPPED);
2391     			break;
2392     		case DEV_STATE_STOPWAIT_TX:
2393     			if (event == DEV_EVENT_TXDOWN)
2394     				fsm_newstate(fi, DEV_STATE_STOPPED);
2395     			break;
2396     	}
2397     }
2398     
2399     static const fsm_node dev_fsm[] = {
2400     	{ DEV_STATE_STOPPED,        DEV_EVENT_START,   dev_action_start  },
2401     
2402     	{ DEV_STATE_STOPWAIT_RXTX,  DEV_EVENT_START,   dev_action_start  },
2403     	{ DEV_STATE_STOPWAIT_RXTX,  DEV_EVENT_RXDOWN,  dev_action_chdown },
2404     	{ DEV_STATE_STOPWAIT_RXTX,  DEV_EVENT_TXDOWN,  dev_action_chdown },
2405     
2406     	{ DEV_STATE_STOPWAIT_RX,    DEV_EVENT_START,   dev_action_start  },
2407     	{ DEV_STATE_STOPWAIT_RX,    DEV_EVENT_RXUP,    dev_action_chup   },
2408     	{ DEV_STATE_STOPWAIT_RX,    DEV_EVENT_TXUP,    dev_action_chup   },
2409     	{ DEV_STATE_STOPWAIT_RX,    DEV_EVENT_RXDOWN,  dev_action_chdown },
2410     
2411     	{ DEV_STATE_STOPWAIT_TX,    DEV_EVENT_START,   dev_action_start  },
2412     	{ DEV_STATE_STOPWAIT_TX,    DEV_EVENT_RXUP,    dev_action_chup   },
2413     	{ DEV_STATE_STOPWAIT_TX,    DEV_EVENT_TXUP,    dev_action_chup   },
2414     	{ DEV_STATE_STOPWAIT_TX,    DEV_EVENT_TXDOWN,  dev_action_chdown },
2415     
2416     	{ DEV_STATE_STARTWAIT_RXTX, DEV_EVENT_STOP,    dev_action_stop   },
2417     	{ DEV_STATE_STARTWAIT_RXTX, DEV_EVENT_RXUP,    dev_action_chup   },
2418     	{ DEV_STATE_STARTWAIT_RXTX, DEV_EVENT_TXUP,    dev_action_chup   },
2419     	{ DEV_STATE_STARTWAIT_RXTX, DEV_EVENT_RXDOWN,  dev_action_chdown },
2420     	{ DEV_STATE_STARTWAIT_RXTX, DEV_EVENT_TXDOWN,  dev_action_chdown },
2421     
2422     	{ DEV_STATE_STARTWAIT_TX,   DEV_EVENT_STOP,    dev_action_stop   },
2423     	{ DEV_STATE_STARTWAIT_TX,   DEV_EVENT_RXUP,    dev_action_chup   },
2424     	{ DEV_STATE_STARTWAIT_TX,   DEV_EVENT_TXUP,    dev_action_chup   },
2425     	{ DEV_STATE_STARTWAIT_TX,   DEV_EVENT_RXDOWN,  dev_action_chdown },
2426     
2427     	{ DEV_STATE_STARTWAIT_RX,   DEV_EVENT_STOP,    dev_action_stop   },
2428     	{ DEV_STATE_STARTWAIT_RX,   DEV_EVENT_RXUP,    dev_action_chup   },
2429     	{ DEV_STATE_STARTWAIT_RX,   DEV_EVENT_TXUP,    dev_action_chup   },
2430     	{ DEV_STATE_STARTWAIT_RX,   DEV_EVENT_TXDOWN,  dev_action_chdown },
2431     
2432     	{ DEV_STATE_RUNNING,        DEV_EVENT_STOP,    dev_action_stop   },
2433     	{ DEV_STATE_RUNNING,        DEV_EVENT_RXDOWN,  dev_action_chdown },
2434     	{ DEV_STATE_RUNNING,        DEV_EVENT_TXDOWN,  dev_action_chdown },
2435     	{ DEV_STATE_RUNNING,        DEV_EVENT_TXUP,    fsm_action_nop    },
2436     	{ DEV_STATE_RUNNING,        DEV_EVENT_RXUP,    fsm_action_nop    },
2437     };
2438     
2439     static const int DEV_FSM_LEN = sizeof(dev_fsm) / sizeof(fsm_node);
2440     
2441     /**
2442      * Transmit a packet.
2443      * This is a helper function for ctc_tx().
2444      *
2445      * @param ch Channel to be used for sending.
2446      * @param skb Pointer to struct sk_buff of packet to send.
2447      *            The linklevel header has already been set up
2448      *            by ctc_tx().
2449      *
2450      * @return 0 on success, -ERRNO on failure. (Never fails.)
2451      */
2452     static int transmit_skb(channel *ch, struct sk_buff *skb) {
2453     	unsigned long saveflags;
2454     	ll_header header;
2455     	int       rc = 0;
2456     
2457     	if (fsm_getstate(ch->fsm) != CH_STATE_TXIDLE) {
2458     		int l = skb->len + LL_HEADER_LENGTH;
2459     
2460     		spin_lock_irqsave(&ch->collect_lock, saveflags);
2461     		if (ch->collect_len + l > ch->max_bufsize - 2)
2462     			rc = -EBUSY;
2463     		else {
2464     			atomic_inc(&skb->users);
2465     			header.length = l;
2466     			header.type = skb->protocol;
2467     			header.unused = 0;
2468     			memcpy(skb_push(skb, LL_HEADER_LENGTH), &header,
2469     			       LL_HEADER_LENGTH);
2470     			skb_queue_tail(&ch->collect_queue, skb);
2471     			ch->collect_len += l;
2472     		}
2473     		spin_unlock_irqrestore(&ch->collect_lock, saveflags);
2474     	} else {
2475     		__u16 block_len;
2476     		int ccw_idx;
2477     
2478     		/**
2479     		 * Protect skb against beeing free'd by upper
2480     		 * layers.
2481     		 */
2482     		atomic_inc(&skb->users);
2483     		ch->prof.txlen += skb->len;
2484     		header.length = skb->len + LL_HEADER_LENGTH;
2485     		header.type = skb->protocol;
2486     		header.unused = 0;
2487     		memcpy(skb_push(skb, LL_HEADER_LENGTH), &header,
2488     		       LL_HEADER_LENGTH);
2489     		block_len = skb->len + 2;
2490     		*((__u16 *)skb_push(skb, 2)) = block_len;
2491     		set_normalized_cda(&ch->ccw[4], virt_to_phys(skb->data));
2492     		if (ch->ccw[4].cda == 0) {
2493     			/**
2494     			 * idal allocation failed, try via copying to
2495     			 * trans_skb. trans_skb usually has a pre-allocated
2496     			 * idal.
2497     			 */
2498     			if (ctc_checkalloc_buffer(ch, 1)) {
2499     				/**
2500     				 * Remove our header. It gets added
2501     				 * again on retransmit.
2502     				 */
2503     				skb_pull(skb, LL_HEADER_LENGTH + 2);
2504     				return -EBUSY;
2505     			}
2506     
2507     			ch->trans_skb->tail = ch->trans_skb->data;
2508     			ch->trans_skb->len = 0;
2509     			ch->ccw[1].count = skb->len;
2510     			memcpy(skb_put(ch->trans_skb, skb->len), skb->data,
2511     			       skb->len);
2512     			atomic_dec(&skb->users);
2513     			dev_kfree_skb_irq(skb);
2514     			ccw_idx = 0;
2515     		} else {
2516     			ch->ccw[4].count = block_len;
2517     			skb_queue_tail(&ch->io_queue, skb);
2518     			ccw_idx = 3;
2519     		}
2520     		ch->retry = 0;
2521     #ifdef DEBUG
2522     		ctc_dump_skb(skb, 0);
2523     #endif
2524     		fsm_newstate(ch->fsm, CH_STATE_TX);
2525     		fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC,
2526     			     CH_EVENT_TIMER, ch);
2527     		s390irq_spin_lock_irqsave(ch->irq, saveflags);
2528     		ch->prof.send_stamp = xtime;
2529     #ifdef DEBUG
2530     	printk(KERN_DEBUG "ccw[%d].cda = %08x\n", ccw_idx+1,
2531     	       ch->ccw[ccw_idx+1].cda);
2532     #endif
2533     		rc = do_IO(ch->irq, &ch->ccw[ccw_idx], (intparm_t)ch, 0xff, 0);
2534     		s390irq_spin_unlock_irqrestore(ch->irq, saveflags);
2535     		if (ccw_idx == 3)
2536     			ch->prof.doios_single++;
2537     		if (rc != 0) {
2538     			fsm_deltimer(&ch->timer);
2539     			ccw_check_return_code(ch, rc);
2540     			skb_dequeue_tail(&ch->io_queue);
2541     			/**
2542     			 * Remove our header. It gets added
2543     			 * again on retransmit.
2544     			 */
2545     			skb_pull(skb, LL_HEADER_LENGTH + 2);
2546     		} else {
2547     			if (ccw_idx == 0) {
2548     				net_device *dev = ch->netdev;
2549     				ctc_priv   *privptr = dev->priv;
2550     				privptr->stats.tx_packets++;
2551     				privptr->stats.tx_bytes +=
2552     					skb->len - LL_HEADER_LENGTH;
2553     			}
2554     		}
2555     	}
2556     
2557     	return rc;
2558     }
2559     
2560     /**
2561      * Interface API for upper network layers
2562      *****************************************************************************/
2563     
2564     /**
2565      * Open an interface.
2566      * Called from generic network layer when ifconfig up is run.
2567      *
2568      * @param dev Pointer to interface struct.
2569      *
2570      * @return 0 on success, -ERRNO on failure. (Never fails.)
2571      */
2572     static int ctc_open(net_device *dev) {
2573     	MOD_INC_USE_COUNT;
2574     	fsm_event(((ctc_priv *)dev->priv)->fsm, DEV_EVENT_START, dev);
2575     	return 0;
2576     }
2577     
2578     /**
2579      * Close an interface.
2580      * Called from generic network layer when ifconfig down is run.
2581      *
2582      * @param dev Pointer to interface struct.
2583      *
2584      * @return 0 on success, -ERRNO on failure. (Never fails.)
2585      */
2586     static int ctc_close(net_device *dev) {
2587     	SET_DEVICE_START(dev, 0);
2588     	fsm_event(((ctc_priv *)dev->priv)->fsm, DEV_EVENT_STOP, dev);
2589     	MOD_DEC_USE_COUNT;
2590     	return 0;
2591     }
2592     
2593     /**
2594      * Start transmission of a packet.
2595      * Called from generic network device layer.
2596      *
2597      * @param skb Pointer to buffer containing the packet.
2598      * @param dev Pointer to interface struct.
2599      *
2600      * @return 0 if packet consumed, !0 if packet rejected.
2601      *         Note: If we return !0, then the packet is free'd by
2602      *               the generic network layer.
2603      */
2604     static int ctc_tx(struct sk_buff *skb, net_device *dev)
2605     {
2606     	int       rc = 0;
2607     	ctc_priv  *privptr = (ctc_priv *)dev->priv;
2608     
2609     	/**
2610     	 * Some sanity checks ...
2611     	 */
2612     	if (skb == NULL) {
2613     		printk(KERN_WARNING "%s: NULL sk_buff passed\n", dev->name);
2614     		privptr->stats.tx_dropped++;
2615     		return 0;
2616     	}
2617     	if (skb_headroom(skb) < (LL_HEADER_LENGTH + 2)) {
2618     		printk(KERN_WARNING
2619     		       "%s: Got sk_buff with head room < %ld bytes\n",
2620     		       dev->name, LL_HEADER_LENGTH + 2);
2621     		dev_kfree_skb(skb);
2622     		privptr->stats.tx_dropped++;
2623     		return 0;
2624     	}
2625     
2626     	/**
2627     	 * If channels are not running, try to restart them
2628     	 * notify anybody about a link failure and throw
2629     	 * away packet. 
2630     	 */
2631     	if (fsm_getstate(privptr->fsm) != DEV_STATE_RUNNING) {
2632     		fsm_event(privptr->fsm, DEV_EVENT_START, dev);
2633     		if (privptr->protocol == CTC_PROTO_LINUX_TTY)
2634     			return -EBUSY;
2635     		dst_link_failure(skb);
2636     		dev_kfree_skb(skb);
2637     		privptr->stats.tx_dropped++;
2638     		privptr->stats.tx_errors++;
2639     		privptr->stats.tx_carrier_errors++;
2640     		return 0;
2641     	}
2642     
2643     	if (ctc_test_and_set_busy(dev))
2644     		return -EBUSY;
2645     
2646     	dev->trans_start = jiffies;
2647     	if (transmit_skb(privptr->channel[WRITE], skb) != 0)
2648     		rc = 1;
2649     	ctc_clear_busy(dev);
2650     	return rc;
2651     }
2652     
2653     
2654     /**
2655      * Sets MTU of an interface.
2656      *
2657      * @param dev     Pointer to interface struct.
2658      * @param new_mtu The new MTU to use for this interface.
2659      *
2660      * @return 0 on success, -EINVAL if MTU is out of valid range.
2661      *         (valid range is 576 .. 65527). If VM is on the
2662      *         remote side, maximum MTU is 32760, however this is
2663      *         <em>not</em> checked here.
2664      */
2665     static int ctc_change_mtu(net_device *dev, int new_mtu) {
2666     	ctc_priv  *privptr = (ctc_priv *)dev->priv;
2667     
2668     	if ((new_mtu < 576) || (new_mtu > 65527) ||
2669     	    (new_mtu > (privptr->channel[READ]->max_bufsize -
2670     			LL_HEADER_LENGTH - 2)))
2671     		return -EINVAL;
2672     	dev->mtu = new_mtu;
2673     	dev->hard_header_len = LL_HEADER_LENGTH + 2;
2674     	return 0;
2675     }
2676     
2677     
2678     /**
2679      * Returns interface statistics of a device.
2680      *
2681      * @param dev Pointer to interface struct.
2682      *
2683      * @return Pointer to stats struct of this interface.
2684      */
2685     static struct net_device_stats *ctc_stats(net_device *dev) {
2686     	return &((ctc_priv *)dev->priv)->stats;
2687     }
2688     
2689     /**
2690      * procfs related structures and routines
2691      *****************************************************************************/
2692     
2693     static net_device *find_netdev_by_ino(unsigned long ino)
2694     {
2695     	channel *ch = channels;
2696     	net_device *dev = NULL;
2697     	ctc_priv *privptr;
2698     
2699     	while (ch) {
2700     		if (ch->netdev != dev) {
2701     			dev = ch->netdev;
2702     			privptr = (ctc_priv *)dev->priv;
2703     
2704     			if ((privptr->proc_ctrl_entry->low_ino == ino) ||
2705     			    (privptr->proc_stat_entry->low_ino == ino))
2706     				return dev;
2707     		}
2708     		ch = ch->next;
2709     	}
2710     	return NULL;
2711     }
2712     
2713     #if LINUX_VERSION_CODE < 0x020363
2714     /**
2715      * Lock the module, if someone changes into
2716      * our proc directory.
2717      */
2718     static void ctc_fill_inode(struct inode *inode, int fill)
2719     {
2720     	if (fill) {
2721     		MOD_INC_USE_COUNT;
2722     	} else
2723     		MOD_DEC_USE_COUNT;
2724     }
2725     #endif
2726     
2727     #define CTRL_BUFSIZE 40
2728     
2729     static int ctc_ctrl_open(struct inode *inode, struct file *file)
2730     {
2731     	file->private_data = kmalloc(CTRL_BUFSIZE, GFP_KERNEL);
2732     	if (file->private_data == NULL)
2733     		return -ENOMEM;
2734     	MOD_INC_USE_COUNT;
2735     	return 0;
2736     }
2737     
2738     static int ctc_ctrl_close(struct inode *inode, struct file *file)
2739     {
2740     	kfree(file->private_data);
2741     	MOD_DEC_USE_COUNT;
2742     	return 0;
2743     }
2744     
2745     static ssize_t ctc_ctrl_write(struct file *file, const char *buf, size_t count,
2746     			   loff_t *off)
2747     {
2748     	unsigned int ino = ((struct inode *)file->f_dentry->d_inode)->i_ino;
2749     	net_device   *dev;
2750     	ctc_priv     *privptr;
2751     	char         *e;
2752     	int          bs1;
2753     	char         tmp[40];
2754     
2755     	if (!(dev = find_netdev_by_ino(ino)))
2756     		return -ENODEV;
2757     	if (off != &file->f_pos)
2758     		return -ESPIPE;
2759     
2760     	privptr = (ctc_priv *)dev->priv;
2761     
2762     	if (count >= 39)
2763     		return -EINVAL;
2764     
2765     	if (copy_from_user(tmp, buf, count))
2766     		return -EFAULT;
2767     	tmp[count+1] = '\0';
2768     	bs1 = simple_strtoul(tmp, &e, 0);
2769     
2770     	if ((bs1 > CTC_BUFSIZE_LIMIT) ||
2771     	    (e && (!isspace(*e))))
2772     		return -EINVAL;
2773     	if ((dev->flags & IFF_RUNNING) &&
2774     	    (bs1 < (dev->mtu + LL_HEADER_LENGTH + 2)))
2775     		return -EINVAL;
2776     	if (bs1 < (576 + LL_HEADER_LENGTH + 2))
2777     		return -EINVAL;
2778     
2779     
2780     	privptr->channel[READ]->max_bufsize =
2781     		privptr->channel[WRITE]->max_bufsize = bs1;
2782     	if (!(dev->flags & IFF_RUNNING))
2783     		dev->mtu = bs1 - LL_HEADER_LENGTH - 2;
2784     	privptr->channel[READ]->flags |= CHANNEL_FLAGS_BUFSIZE_CHANGED;
2785     	privptr->channel[WRITE]->flags |= CHANNEL_FLAGS_BUFSIZE_CHANGED;
2786     
2787     	return count;
2788     }
2789     
2790     static ssize_t ctc_ctrl_read(struct file *file, char *buf, size_t count,
2791     			  loff_t *off)
2792     {
2793     	unsigned int ino = ((struct inode *)file->f_dentry->d_inode)->i_ino;
2794     	char *sbuf = (char *)file->private_data;
2795     	net_device *dev;
2796     	ctc_priv *privptr;
2797     	ssize_t ret = 0;
2798     	char *p = sbuf;
2799     	int l;
2800     
2801     	if (!(dev = find_netdev_by_ino(ino)))
2802     		return -ENODEV;
2803     	if (off != &file->f_pos)
2804     		return -ESPIPE;
2805     
2806     	privptr = (ctc_priv *)dev->priv;
2807     
2808     	if (file->f_pos == 0)
2809     		sprintf(sbuf, "%d\n", privptr->channel[READ]->max_bufsize);
2810     
2811     	l = strlen(sbuf);
2812     	p = sbuf;
2813     	if (file->f_pos < l) {
2814     		p += file->f_pos;
2815     		l = strlen(p);
2816     		ret = (count > l) ? l : count;
2817     		if (copy_to_user(buf, p, ret))
2818     			return -EFAULT;
2819     	}
2820     	file->f_pos += ret;
2821     	return ret;
2822     }
2823     
2824     #define STATS_BUFSIZE 2048
2825     
2826     static int ctc_stat_open(struct inode *inode, struct file *file)
2827     {
2828     	file->private_data = kmalloc(STATS_BUFSIZE, GFP_KERNEL);
2829     	if (file->private_data == NULL)
2830     		return -ENOMEM;
2831     	MOD_INC_USE_COUNT;
2832     	return 0;
2833     }
2834     
2835     static int ctc_stat_close(struct inode *inode, struct file *file)
2836     {
2837     	kfree(file->private_data);
2838     	MOD_DEC_USE_COUNT;
2839     	return 0;
2840     }
2841     
2842     static ssize_t ctc_stat_write(struct file *file, const char *buf, size_t count,
2843     			      loff_t *off)
2844     {
2845     	unsigned int ino = ((struct inode *)file->f_dentry->d_inode)->i_ino;
2846     	net_device *dev;
2847     	ctc_priv *privptr;
2848     
2849     	if (!(dev = find_netdev_by_ino(ino)))
2850     		return -ENODEV;
2851     	privptr = (ctc_priv *)dev->priv;
2852     	privptr->channel[WRITE]->prof.maxmulti = 0;
2853     	privptr->channel[WRITE]->prof.maxcqueue = 0;
2854     	privptr->channel[WRITE]->prof.doios_single = 0;
2855     	privptr->channel[WRITE]->prof.doios_multi = 0;
2856     	privptr->channel[WRITE]->prof.txlen = 0;
2857     	privptr->channel[WRITE]->prof.tx_time = 0;
2858     	return count;
2859     }
2860     
2861     static ssize_t ctc_stat_read(struct file *file, char *buf, size_t count,
2862     			      loff_t *off)
2863     {
2864     	unsigned int ino = ((struct inode *)file->f_dentry->d_inode)->i_ino;
2865     	char *sbuf = (char *)file->private_data;
2866     	net_device *dev;
2867     	ctc_priv *privptr;
2868     	ssize_t ret = 0;
2869     	char *p = sbuf;
2870     	int l;
2871     
2872     	if (!(dev = find_netdev_by_ino(ino)))
2873     		return -ENODEV;
2874     	if (off != &file->f_pos)
2875     		return -ESPIPE;
2876     
2877     	privptr = (ctc_priv *)dev->priv;
2878     
2879     	if (file->f_pos == 0) {
2880     		p += sprintf(p, "Device FSM state: %s\n",
2881     			     fsm_getstate_str(privptr->fsm));
2882     		p += sprintf(p, "RX channel FSM state: %s\n",
2883     			     fsm_getstate_str(privptr->channel[READ]->fsm));
2884     		p += sprintf(p, "TX channel FSM state: %s\n",
2885     			     fsm_getstate_str(privptr->channel[WRITE]->fsm));
2886     		p += sprintf(p, "Max. TX buffer used: %ld\n",
2887     			     privptr->channel[WRITE]->prof.maxmulti);
2888     		p += sprintf(p, "Max. chained SKBs: %ld\n",
2889     			     privptr->channel[WRITE]->prof.maxcqueue);
2890     		p += sprintf(p, "TX single write ops: %ld\n",
2891     			     privptr->channel[WRITE]->prof.doios_single);
2892     		p += sprintf(p, "TX multi write ops: %ld\n",
2893     			     privptr->channel[WRITE]->prof.doios_multi);
2894     		p += sprintf(p, "Netto bytes written: %ld\n",
2895     			     privptr->channel[WRITE]->prof.txlen);
2896     		p += sprintf(p, "Max. TX IO-time: %ld\n",
2897     			     privptr->channel[WRITE]->prof.tx_time);
2898     	}
2899     	l = strlen(sbuf);
2900     	p = sbuf;
2901     	if (file->f_pos < l) {
2902     		p += file->f_pos;
2903     		l = strlen(p);
2904     		ret = (count > l) ? l : count;
2905     		if (copy_to_user(buf, p, ret))
2906     			return -EFAULT;
2907     	}
2908     	file->f_pos += ret;
2909     	return ret;
2910     }
2911     
2912     static struct file_operations ctc_stat_fops = {
2913     	read:    ctc_stat_read,
2914     	write:   ctc_stat_write,
2915     	open:    ctc_stat_open,
2916     	release: ctc_stat_close,
2917     };
2918     
2919     static struct file_operations ctc_ctrl_fops = {
2920     	read:    ctc_ctrl_read,
2921     	write:   ctc_ctrl_write,
2922     	open:    ctc_ctrl_open,
2923     	release: ctc_ctrl_close,
2924     };
2925     
2926     static struct inode_operations ctc_stat_iops = {
2927     #if LINUX_VERSION_CODE < 0x020363
2928     	default_file_ops: &ctc_stat_fops
2929     #endif
2930     };
2931     static struct inode_operations ctc_ctrl_iops = {
2932     #if LINUX_VERSION_CODE < 0x020363
2933     	default_file_ops: &ctc_ctrl_fops
2934     #endif
2935     };
2936     
2937     static struct proc_dir_entry stat_entry = {
2938     	0,                           /* low_ino */
2939     	10,                          /* namelen */
2940     	"statistics",                /* name    */
2941     	S_IFREG | S_IRUGO | S_IWUSR, /* mode    */
2942     	1,                           /* nlink   */
2943     	0,                           /* uid     */
2944     	0,                           /* gid     */
2945     	0,                           /* size    */
2946     	&ctc_stat_iops               /* ops     */
2947     };
2948     
2949     static struct proc_dir_entry ctrl_entry = {
2950     	0,                           /* low_ino */
2951     	10,                          /* namelen */
2952     	"buffersize",                /* name    */
2953     	S_IFREG | S_IRUSR | S_IWUSR, /* mode    */
2954     	1,                           /* nlink   */
2955     	0,                           /* uid     */
2956     	0,                           /* gid     */
2957     	0,                           /* size    */
2958     	&ctc_ctrl_iops               /* ops     */
2959     };
2960     
2961     #if LINUX_VERSION_CODE < 0x020363
2962     static struct proc_dir_entry ctc_dir = {
2963     	0,                           /* low_ino  */
2964     	3,                           /* namelen  */
2965     	"ctc",                       /* name     */
2966     	S_IFDIR | S_IRUGO | S_IXUGO, /* mode     */
2967     	2,                           /* nlink    */
2968     	0,                           /* uid      */
2969     	0,                           /* gid      */
2970     	0,                           /* size     */
2971     	0,                           /* ops      */
2972     	0,                           /* get_info */
2973     	ctc_fill_inode               /* fill_ino (for locking) */
2974     };
2975     
2976     static struct proc_dir_entry ctc_template =
2977     {
2978     	0,                           /* low_ino  */
2979     	0,                           /* namelen  */
2980     	"",                          /* name     */
2981     	S_IFDIR | S_IRUGO | S_IXUGO, /* mode     */
2982     	2,                           /* nlink    */
2983     	0,                           /* uid      */
2984     	0,                           /* gid      */
2985     	0,                           /* size     */
2986     	0,                           /* ops      */
2987     	0,                           /* get_info */
2988     	ctc_fill_inode               /* fill_ino (for locking) */
2989     };
2990     #else
2991     static struct proc_dir_entry *ctc_dir = NULL;
2992     static struct proc_dir_entry *ctc_template = NULL;
2993     #endif
2994     
2995     /**
2996      * Create the driver's main directory /proc/net/ctc
2997      */
2998     static void ctc_proc_create_main(void) {
2999     	/**
3000     	 * If not registered, register main proc dir-entry now
3001     	 */
3002     #if LINUX_VERSION_CODE > 0x020362
3003     	if (!ctc_dir)
3004     		ctc_dir = proc_mkdir("ctc", proc_net);
3005     #else
3006     	if (ctc_dir.low_ino == 0)
3007     		proc_net_register(&ctc_dir);
3008     #endif
3009     }
3010     
3011     #ifdef MODULE
3012     /**
3013      * Destroy /proc/net/ctc
3014      */
3015     static void ctc_proc_destroy_main(void) {
3016     #if LINUX_VERSION_CODE > 0x020362
3017     	remove_proc_entry("ctc", proc_net);
3018     #else
3019     	proc_net_unregister(ctc_dir.low_ino);
3020     #endif
3021     }
3022     #endif MODULE
3023     
3024     /**
3025      * Create a device specific subdirectory in /proc/net/ctc/ with the
3026      * same name like the device. In that directory, create 2 entries
3027      * "statistics" and "buffersize".
3028      *
3029      * @param dev The device for which the subdirectory should be created.
3030      *
3031      */
3032     static void ctc_proc_create_sub(net_device *dev) {
3033     	ctc_priv *privptr = dev->priv;
3034     
3035     #if LINUX_VERSION_CODE > 0x020362
3036     	privptr->proc_dentry = proc_mkdir(dev->name, ctc_dir);
3037     	privptr->proc_stat_entry =
3038     		create_proc_entry("statistics",
3039     				  S_IFREG | S_IRUSR | S_IWUSR,
3040     				  privptr->proc_dentry);
3041     	privptr->proc_stat_entry->proc_fops = &ctc_stat_fops;
3042     	privptr->proc_stat_entry->proc_iops = &ctc_stat_iops;
3043     	privptr->proc_ctrl_entry =
3044     		create_proc_entry("buffersize",
3045     				  S_IFREG | S_IRUSR | S_IWUSR,
3046     				  privptr->proc_dentry);
3047     	privptr->proc_ctrl_entry->proc_fops = &ctc_ctrl_fops;
3048     	privptr->proc_ctrl_entry->proc_iops = &ctc_ctrl_iops;
3049     #else
3050     	privptr->proc_dentry->name = dev->name;
3051     	privptr->proc_dentry->namelen = strlen(dev->name);
3052     	proc_register(&ctc_dir, privptr->proc_dentry);
3053     	proc_register(privptr->proc_dentry, privptr->proc_stat_entry);
3054     	proc_register(privptr->proc_dentry, privptr->proc_ctrl_entry);
3055     #endif
3056     	privptr->proc_registered = 1;
3057     }
3058     
3059     
3060     /**
3061      * Destroy a device specific subdirectory.
3062      *
3063      * @param privptr Pointer to device private data.
3064      */
3065     static void ctc_proc_destroy_sub(ctc_priv *privptr) {
3066     	if (!privptr->proc_registered)
3067     		return;
3068     #if LINUX_VERSION_CODE > 0x020362
3069     	remove_proc_entry("statistics", privptr->proc_dentry);
3070     	remove_proc_entry("buffersize", privptr->proc_dentry);
3071     	remove_proc_entry(privptr->proc_dentry->name, ctc_dir);
3072     #else
3073     	proc_unregister(privptr->proc_dentry,
3074     			privptr->proc_stat_entry->low_ino);
3075     	proc_unregister(privptr->proc_dentry,
3076     			privptr->proc_ctrl_entry->low_ino);
3077     	proc_unregister(&ctc_dir,
3078     			privptr->proc_dentry->low_ino);
3079     #endif
3080     	privptr->proc_registered = 0;
3081     }
3082     
3083     
3084     
3085     #ifndef CTC_CHANDEV
3086     /**
3087      * Setup related routines
3088      *****************************************************************************/
3089     
3090     /**
3091      * Parse a portion of the setup string describing a single device or option
3092      * providing the following syntax:
3093      *
3094      * [Device/OptionName[:int1][:int2][:int3]]
3095      *
3096      *
3097      * @param setup    Pointer to a pointer to the remainder of the parameter
3098      *                 string to be parsed. On return, the content of this
3099      *                 pointer is updated to point to the first character after
3100      *                 the parsed portion (e.g. possible start of next portion)
3101      *                 NOTE: The string pointed to must be writeable, since a
3102      *                 \0 is written for termination of the device/option name.
3103      *
3104      * @param dev_name Pointer to a pointer to the name of the device whose
3105      *                 parameters are parsed. On return, this is set to the
3106      *                 name of the device/option.
3107      *
3108      * @param ints     Pointer to an array of integer parameters. On return,
3109      *                 element 0 is set to the number of parameters found.
3110      *
3111      * @param maxip    Maximum number of ints to parse.
3112      *                 (ints[] must have size maxip+1)
3113      *
3114      * @return     0 if string "setup" was empty, !=0 otherwise
3115      */
3116     static int parse_opts(char **setup, char **dev_name, int *ints, int maxip) {
3117     	char *cur = *setup;
3118     	int i = 1;
3119     	int rc = 0;
3120     	int in_name = 1;
3121     	int noauto = 0;
3122     
3123     #ifdef DEBUG
3124     	printk(KERN_DEBUG
3125     	       "ctc: parse_opts(): *setup='%s', maxip=%d\n", *setup, maxip);
3126     #endif
3127     	if (*setup) {
3128     		*dev_name = *setup;
3129     
3130     		if (strncmp(cur, "ctc", 3) && strncmp(cur, "escon", 5) &&
3131     		    strncmp(cur, "noauto", 6)) {
3132     			if ((*setup = strchr(cur, ':')))
3133     				*(*setup)++ = '\0';
3134     			printk(KERN_WARNING
3135     			       "ctc: Invalid device name or option '%s'\n",
3136     			       cur);
3137     			return 1;
3138     		}
3139     		switch (*cur) {
3140     			case 'c':
3141     				cur += 3;
3142     				break;
3143     			case 'e':
3144     				cur += 5;
3145     				break;
3146     			case 'n':
3147     				cur += 6;
3148     				*cur++ = '\0';
3149     				noauto = 1;
3150     		}
3151     		if (!noauto) {
3152     			while (cur &&
3153     			       (*cur == '-' || isdigit(*cur)) &&
3154     			       i <= maxip) {
3155     				if (in_name) {
3156     					cur++;
3157     					if (*cur == ':') {
3158     						*cur++ = '\0';
3159     						in_name = 0;
3160     					}
3161     				} else {
3162     					ints[i++] =
3163     						simple_strtoul(cur, NULL, 0);
3164     #ifdef DEBUG
3165     					printk(KERN_DEBUG
3166     					       "ctc: %s: ints[%d]=%d\n",
3167     					       __FUNCTION__,
3168     					       i-1, ints[i-1]);
3169     #endif
3170     					if ((cur = strchr(cur, ':')) != NULL)
3171     						cur++;
3172     				}
3173     			}
3174     		}
3175     		ints[0] = i - 1;
3176     		*setup = cur;
3177     		if (cur && (*cur == ':'))
3178     			(*setup)++;
3179     		rc = 1;
3180     	}
3181     	return rc;
3182     }
3183     
3184     /**
3185      *
3186      * Allocate one param struct
3187      *
3188      * If the driver is loaded as a module this functions is called during
3189      *   module set up and we can allocate the struct by using kmalloc()
3190      *
3191      * If the driver is statically linked into the kernel this function is called
3192      * when kmalloc() is not yet available so we must allocate from a static array
3193      *
3194      */
3195     #ifdef MODULE
3196     #define alloc_param() ((param *)kmalloc(sizeof(param), GFP_KERNEL));
3197     #else
3198     static param parms_array[MAX_STATIC_DEVICES];
3199     static param *next_param = parms_array;
3200     #define alloc_param() \
3201             ((next_param<parms_array+MAX_STATIC_DEVICES)?next_param++:NULL)
3202     #endif MODULE
3203     
3204     /**
3205      * Returns commandline parameter using device name as key.
3206      *
3207      * @param name Name of interface to get parameters from.
3208      *
3209      * @return Pointer to corresponting param struct, NULL if not found.
3210      */
3211     static param *find_param(char *name) {
3212     	param *p = params;
3213     
3214     	while (p && strcmp(p->name, name))
3215     		p = p->next;
3216     	return p;
3217     }
3218     
3219     /**
3220      * maximum number of integer parametes that may be specified
3221      * for one device in the setup string
3222      */
3223     #define CTC_MAX_INTPARMS 3
3224     
3225     /**
3226      * Parse configuration options for all interfaces.
3227      *
3228      * This function is called from two possible locations:
3229      *  - If built as module, this function is called from init_module().
3230      *  - If built in monolithic kernel, this function is called from within
3231      *    init/main.c.
3232      * Parsing is always done here.
3233      *
3234      * Valid parameters are:
3235      *
3236      *
3237      *   [NAME[:0xRRRR[:0xWWWW[:P]]]]
3238      *
3239      *     where P       is the channel protocol (always 0)
3240      *	      0xRRRR is the cu number for the read channel
3241      *	      0xWWWW is the cu number for the write channel
3242      *	      NAME   is either ctc0 ... ctcN for CTC/A
3243      *                      or     escon0 ... esconN for Escon.
3244      *                      or     noauto
3245      *                             which switches off auto-detection of channels.
3246      *
3247      * @param setup    The parameter string to parse. MUST be writeable!
3248      * @param ints     Pointer to an array of ints. Only for kernel 2.2,
3249      *                 builtin (not module) version. With kernel 2.2,
3250      *                 normally all integer-parameters, preceeding some
3251      *                 configuration-string are pre-parsed in init/main.c
3252      *                 and handed over here.
3253      *                 To simplify 2.2/2.4 compatibility, by definition,
3254      *                 our parameters always start with a string and ints
3255      *                 is always unset and ignored.
3256      */
3257     #ifdef MODULE
3258        static void ctc_setup(char *setup)
3259     #  define ctc_setup_return return
3260     #else MODULE
3261     #  if LINUX_VERSION_CODE < 0x020300
3262          __initfunc(void ctc_setup(char *setup, int *ints))
3263     #    define ctc_setup_return return
3264     #    define ints local_ints
3265     #  else
3266          static int __init ctc_setup(char *setup)
3267     #    define ctc_setup_return return(1)
3268     #  endif
3269     #endif MODULE
3270     {
3271     	int write_dev;
3272     	int read_dev;
3273     	int proto;
3274     	param *par;
3275     	char *dev_name;
3276     	int ints[CTC_MAX_INTPARMS+1];
3277     
3278     	while (parse_opts(&setup, &dev_name, ints, CTC_MAX_INTPARMS)) {
3279     		write_dev = -1;
3280     		read_dev = -1;
3281     		proto = CTC_PROTO_S390;
3282     #ifdef DEBUG
3283     		printk(KERN_DEBUG
3284     		       "ctc: ctc_setup(): setup='%s' dev_name='%s',"
3285     		       " ints[0]=%d)\n",
3286     		       setup, dev_name, ints[0]);
3287     #endif DEBUG
3288     		if (dev_name == NULL) {
3289     			/**
3290     			 * happens if device name is not specified in
3291     			 * parameter line (cf. init/main.c:get_options()
3292     			 */
3293     			printk(KERN_WARNING
3294     			       "ctc: %s(): Device name not specified\n",
3295     			       __FUNCTION__);
3296     			ctc_setup_return;
3297     		}
3298     
3299     #ifdef DEBUG
3300     		printk(KERN_DEBUG "name=´%s´ argc=%d\n", dev_name, ints[0]);
3301     #endif
3302     
3303     		if (strcmp(dev_name, "noauto") == 0) {
3304     			printk(KERN_INFO "ctc: autoprobing disabled\n");
3305     			ctc_no_auto = 1;
3306     			continue;
3307     		}
3308     
3309     		if (find_param(dev_name) != NULL) {
3310     			printk(KERN_WARNING
3311     			       "ctc: Definition for device %s already set. "
3312     			       "Ignoring second definition\n", dev_name);
3313     			continue;
3314     		}
3315     
3316     		switch (ints[0]) {
3317     			case 3: /* protocol type passed */
3318     				proto = ints[3];
3319     				if (proto > CTC_PROTO_MAX) {
3320     					printk(KERN_WARNING
3321     					       "%s: wrong protocol type "
3322     					       "passed\n", dev_name);
3323     					ctc_setup_return;
3324     				}
3325     			case 2: /* write channel passed */
3326     				write_dev = ints[2];
3327     			case 1: /* read channel passed */
3328     				read_dev = ints[1];
3329     				if (write_dev == -1)
3330     					write_dev = read_dev + 1;
3331     				break;
3332     			default:
3333     				printk(KERN_WARNING
3334     				       "ctc: wrong number of parameter "
3335     				       "passed (is: %d, expected: [1..3]\n",
3336     				       ints[0]);
3337     				ctc_setup_return;
3338     		}
3339     		par = alloc_param();
3340     		if (!par) {
3341     #ifdef MODULE
3342     			printk(KERN_WARNING
3343     			       "ctc: Couldn't allocate setup param block\n");
3344     #else
3345     			printk(KERN_WARNING
3346     			       "ctc: Number of device definitions in "
3347     			       " kernel commandline exceeds builtin limit "
3348     			       " of %d devices.\n", MAX_STATIC_DEVICES);
3349     #endif
3350     			ctc_setup_return;
3351     		}
3352     		par->read_dev = read_dev;
3353     		par->write_dev = write_dev;
3354     		par->proto = proto;
3355     		strncpy(par->name, dev_name, MAX_PARAM_NAME_LEN);
3356     		par->next = params;
3357     		params = par;
3358     #ifdef DEBUG
3359     		printk(KERN_DEBUG "%s: protocol=%x read=%04x write=%04x\n",
3360     		       dev_name, proto, read_dev, write_dev);
3361     #endif
3362     	}
3363     	ctc_setup_return;
3364     }
3365     
3366     #if LINUX_VERSION_CODE >= 0x020300
3367     __setup("ctc=", ctc_setup);
3368     #endif
3369     #endif /* !CTC_CHANDEV */
3370     
3371     
3372     static void
3373     ctc_netdev_unregister(net_device *dev)
3374     {
3375     	ctc_priv *privptr;
3376     
3377     	if (!dev)
3378     		return;
3379     	privptr = (ctc_priv *)dev->priv;
3380     	if (privptr->protocol != CTC_PROTO_LINUX_TTY)
3381     		unregister_netdev(dev);
3382     	else
3383     		ctc_tty_unregister_netdev(dev);
3384     }
3385     
3386     static int
3387     ctc_netdev_register(net_device *dev)
3388     {
3389     	ctc_priv *privptr = (ctc_priv *)dev->priv;
3390     	if (privptr->protocol != CTC_PROTO_LINUX_TTY)
3391     		return register_netdev(dev);
3392     	else
3393     		return ctc_tty_register_netdev(dev);
3394     }
3395     
3396     static void
3397     ctc_free_netdevice(net_device *dev, int free_dev)
3398     {
3399     	ctc_priv *privptr;
3400     	if (!dev)
3401     		return;
3402     	privptr = dev->priv;
3403     	if (privptr) {
3404     		if (privptr->fsm)
3405     			kfree_fsm(privptr->fsm);
3406     		ctc_proc_destroy_sub(privptr);
3407     		kfree(privptr);
3408     	}
3409     #ifdef MODULE
3410     	if (free_dev)
3411     		kfree(dev);
3412     #endif
3413     }
3414     
3415     #ifdef CTC_CHANDEV
3416     static int
3417     ctc_shutdown(net_device *dev)
3418     {
3419     	ctc_priv *privptr;
3420     
3421     	if (!dev)
3422     		return 0;
3423     	privptr = (ctc_priv *)dev->priv;
3424     	channel_remove(privptr->channel[READ]);
3425     	channel_remove(privptr->channel[WRITE]);
3426     	ctc_free_netdevice(dev, 0);
3427     	return 0;
3428     }
3429     #endif
3430     
3431     /**
3432      * Initialize everything of the net device except the name and the
3433      * channel structs.
3434      */
3435     static net_device *
3436     ctc_init_netdevice(net_device *dev, int alloc_device)
3437     {
3438     	ctc_priv *privptr;
3439     	int      priv_size;
3440     	if (alloc_device) {
3441     		dev = kmalloc(sizeof(net_device)
3442     #if LINUX_VERSION_CODE < 0x020300
3443     			      + 11 /* name + zero */
3444     #endif
3445     			      , GFP_KERNEL);
3446     		if (!dev)
3447     			return NULL;
3448     		memset(dev, 0, sizeof(net_device));
3449     	}
3450     	priv_size = sizeof(ctc_priv) + sizeof(ctc_template) +
3451     		sizeof(stat_entry) + sizeof(ctrl_entry);
3452     	dev->priv = kmalloc(priv_size, GFP_KERNEL);
3453     	if (dev->priv == NULL) {
3454     		if (alloc_device)
3455     			kfree(dev);
3456     		return NULL;
3457     	}
3458             memset(dev->priv, 0, priv_size);
3459             privptr = (ctc_priv *)dev->priv;
3460             privptr->proc_dentry = (struct proc_dir_entry *)
3461     		(((char *)privptr) + sizeof(ctc_priv));
3462             privptr->proc_stat_entry = (struct proc_dir_entry *)
3463     		(((char *)privptr) + sizeof(ctc_priv) +
3464     		 sizeof(ctc_template));
3465             privptr->proc_ctrl_entry = (struct proc_dir_entry *)
3466     		(((char *)privptr) + sizeof(ctc_priv) +
3467     		 sizeof(ctc_template) + sizeof(stat_entry));
3468     	memcpy(privptr->proc_dentry, &ctc_template, sizeof(ctc_template));
3469     	memcpy(privptr->proc_stat_entry, &stat_entry, sizeof(stat_entry));
3470     	memcpy(privptr->proc_ctrl_entry, &ctrl_entry, sizeof(ctrl_entry));
3471     	privptr->fsm = init_fsm("ctcdev", dev_state_names,
3472     			dev_event_names, NR_DEV_STATES, NR_DEV_EVENTS,
3473     			dev_fsm, DEV_FSM_LEN, GFP_KERNEL);
3474     	if (privptr->fsm == NULL) {
3475     		kfree(privptr);
3476     		if (alloc_device)
3477     			kfree(dev);
3478     		return NULL;
3479     	}
3480     	fsm_newstate(privptr->fsm, DEV_STATE_STOPPED);
3481     	dev->mtu	         = CTC_BUFSIZE_DEFAULT - LL_HEADER_LENGTH - 2;
3482     	dev->hard_start_xmit     = ctc_tx;
3483     	dev->open	         = ctc_open;
3484     	dev->stop	         = ctc_close;
3485     	dev->get_stats	         = ctc_stats;
3486     	dev->change_mtu          = ctc_change_mtu;
3487     	dev->hard_header_len     = LL_HEADER_LENGTH + 2;
3488     	dev->addr_len            = 0;
3489     	dev->type                = ARPHRD_SLIP;
3490     	dev->tx_queue_len        = 100;
3491     	SET_DEVICE_START(dev, 1);
3492     	dev_init_buffers(dev);
3493     	dev->flags	         = IFF_POINTOPOINT | IFF_NOARP;
3494     	return dev;
3495     }
3496     
3497     #ifdef CTC_CHANDEV
3498     static void
3499     ctc_chandev_msck_notify(void *dev, int msck_irq,
3500     			chandev_msck_status prevstatus,
3501     			chandev_msck_status newstatus)
3502     {
3503     	net_device *device = (net_device *)dev;
3504     	ctc_priv *privptr;
3505     	int direction;
3506     
3507     	if (!dev)
3508     		return;
3509     
3510     	privptr = device->priv;
3511     	if (prevstatus == chandev_status_revalidate)
3512     		for (direction = READ; direction <= WRITE; direction++) {
3513     			channel *ch = privptr->channel[direction];
3514     			if(ch->irq == msck_irq) {
3515     				s390_dev_info_t devinfo;
3516     
3517     				if (get_dev_info_by_irq(ch->irq, &devinfo))
3518     					ch->devno = devinfo.devno;
3519     				else
3520     					printk(KERN_WARNING
3521     					       "ctc_chandev_msck_notify: "
3522     					       "get_dev_info_by_irq failed for "
3523     					       "irq %d\n", ch->irq);
3524     			}
3525     		}
3526     	switch (newstatus) {
3527     		case chandev_status_not_oper:
3528     		case chandev_status_no_path:
3529     		case chandev_status_gone:
3530     			for (direction = READ; direction <= WRITE; direction++) {
3531     				channel *ch = privptr->channel[direction];
3532     				fsm_event(ch->fsm, CH_EVENT_MC_FAIL, ch);
3533     			}
3534     			printk(KERN_WARNING
3535     			       "ctc: %s channel deactivated\n", device->name);
3536     			break;
3537     		case chandev_status_all_chans_good:
3538     			for (direction = READ; direction <= WRITE; direction++) {
3539     				channel *ch = privptr->channel[direction];
3540     				fsm_event(ch->fsm, CH_EVENT_MC_GOOD, ch);
3541     			}
3542     			printk(KERN_WARNING
3543     			       "ctc: %s channel activated\n", device->name);
3544     			break;
3545     		default:
3546     			break;
3547     	}
3548     }
3549     
3550     /**
3551      *
3552      * Setup an interface.
3553      *
3554      * Like ctc_setup(), ctc_probe() can be called from two different locations:
3555      *  - If built as module, it is called from within init_module().
3556      *  - If built in monolithic kernel, it is called from within generic network
3557      *    layer during initialization for every corresponding device, declared in
3558      *    drivers/net/Space.c
3559      *
3560      * @param dev Pointer to net_device to be initialized.
3561      *
3562      * @returns 0 on success, !0 on failure.
3563      */
3564     static int ctc_chandev_probe(chandev_probeinfo *info)
3565     {
3566     	int               devno[2];
3567     	__u16             proto;
3568     	int               rc;
3569     	int               direction;
3570     	channel_type_t    type;
3571     	ctc_priv          *privptr;
3572     	net_device        *dev;
3573     
3574     	ctc_proc_create_main();
3575     
3576     
3577     	switch (info->chan_type) {
3578     		case chandev_type_ctc:
3579     			type = channel_type_ctca;
3580     			break;
3581     		case chandev_type_escon:
3582     			type = channel_type_escon;
3583     			break;
3584     		default:
3585     			printk(KERN_WARNING "ctc_chandev_probe called with "
3586     			       "unsupported channel type %d\n", info->chan_type);
3587     			return -ENODEV;
3588     	}
3589     	devno[READ]  = info->read.devno;
3590     	devno[WRITE] = info->write.devno;
3591     	proto        = info->port_protocol_no;
3592     
3593     	if (add_channel(info->read.irq, info->read.devno, type))
3594     		return -ENOMEM;
3595     	if (add_channel(info->write.irq, info->write.devno, type))
3596     		return -ENOMEM;
3597     
3598     	dev = ctc_init_netdevice(NULL, 1);
3599     
3600     	
3601     	if (!dev) {
3602     		printk(KERN_WARNING "ctc_init_netdevice failed\n");
3603     		return -ENODEV;
3604     	}
3605     	
3606     	if (proto == CTC_PROTO_LINUX_TTY)
3607     		chandev_build_device_name(info, dev->name, "ctctty", 1);
3608     	else
3609     		chandev_build_device_name(info, dev->name, "ctc", 1);
3610     
3611     	privptr = (ctc_priv *)dev->priv;
3612     	privptr->protocol = proto;
3613     	for (direction = READ; direction <= WRITE; direction++) {
3614     		privptr->channel[direction] =
3615     			channel_get(type, devno[direction], direction);
3616     		if (privptr->channel[direction] == NULL) {
3617     			if (direction == WRITE) {
3618     				FREE_IRQ(privptr->channel[READ]->irq,
3619     					 privptr->channel[READ]->devstat);
3620     				channel_free(privptr->channel[READ]);
3621     			}
3622     			ctc_free_netdevice(dev, 1);
3623     			return -ENODEV;
3624     		}
3625     		privptr->channel[direction]->netdev = dev;
3626     		privptr->channel[direction]->protocol = proto;
3627     		privptr->channel[direction]->max_bufsize = CTC_BUFSIZE_DEFAULT;
3628     		rc = REQUEST_IRQ(privptr->channel[direction]->irq,
3629     				 (void *)ctc_irq_handler, SA_INTERRUPT,
3630     				 dev->name,
3631     				 privptr->channel[direction]->devstat);
3632     		if (rc) {
3633     			printk(KERN_WARNING
3634     			       "%s: requested irq %d is busy rc=%02x\n",
3635     			       dev->name, privptr->channel[direction]->irq,
3636     			       rc);
3637     			if (direction == WRITE) {
3638     				FREE_IRQ(privptr->channel[READ]->irq,
3639     					 privptr->channel[READ]->devstat);
3640     				channel_free(privptr->channel[READ]);
3641     			}
3642     			channel_free(privptr->channel[direction]);
3643     			ctc_free_netdevice(dev, 1);
3644     			return -EBUSY;
3645     		}
3646     	}
3647     	if (ctc_netdev_register(dev) != 0) {
3648     		ctc_free_netdevice(dev, 1);
3649     		return -ENODEV;
3650     	}
3651     
3652     	/**
3653     	 * register subdir in /proc/net/ctc
3654     	 */
3655     	ctc_proc_create_sub(dev);
3656     	strncpy(privptr->fsm->name, dev->name, sizeof(privptr->fsm->name));
3657     	activated++;
3658     
3659     	print_banner();
3660     
3661     	printk(KERN_INFO
3662     	       "%s: read: ch %04x (irq %04x), "
3663     	       "write: ch %04x (irq %04x) proto: %d\n",
3664     	       dev->name, privptr->channel[READ]->devno,
3665     	       privptr->channel[READ]->irq, privptr->channel[WRITE]->devno,
3666     	       privptr->channel[WRITE]->irq, proto);
3667     
3668     	chandev_initdevice(info, dev, 0, dev->name,
3669     			   (proto == CTC_PROTO_LINUX_TTY)
3670     			   ? chandev_category_serial_device :
3671     			   chandev_category_network_device,
3672     			   (chandev_unregfunc)ctc_netdev_unregister);
3673     	return 0;
3674     }
3675     #else /* ! CHANDEV */
3676     /**
3677      *
3678      * Setup an interface.
3679      *
3680      * Like ctc_setup(), ctc_probe() can be called from two different locations:
3681      *  - If built as module, it is called from within init_module().
3682      *  - If built in monolithic kernel, it is called from within generic network
3683      *    layer during initialization for every corresponding device, declared in
3684      *    drivers/net/Space.c
3685      *
3686      * @param dev Pointer to net_device to be initialized.
3687      *
3688      * @returns 0 on success, !0 on failure.
3689      */
3690     int ctc_probe(net_device *dev)
3691     {
3692     	int            devno[2];
3693     	__u16          proto;
3694     	int            rc;
3695     	int            direction;
3696     	channel_type_t type;
3697     	ctc_priv       *privptr;
3698     	param          *par;
3699     
3700     	ctc_proc_create_main();
3701     
3702     	/**
3703     	 * Scan for available channels only the first time,
3704     	 * ctc_probe gets control.
3705     	 */
3706     	if (channels == NULL)
3707     		channel_scan();
3708     
3709     	type = extract_channel_media(dev->name);
3710     	if (type == channel_type_unknown)
3711     		return -ENODEV;
3712     
3713     	par = find_param(dev->name);
3714     	if (par) {
3715     		devno[READ] = par->read_dev;
3716     		devno[WRITE] = par->write_dev;
3717     		proto = par->proto;
3718     	} else {
3719     		if (ctc_no_auto)
3720     			return -ENODEV;
3721     		else {
3722     			devno[READ] = -1;
3723     			devno[WRITE] = -1;
3724     			proto = CTC_PROTO_S390;
3725     		}
3726     	}
3727     
3728     #ifndef MODULE
3729     	if (ctc_init_netdevice(dev, 0) == NULL)
3730     		return -ENODEV;
3731     #endif
3732     	privptr = (ctc_priv *)dev->priv;
3733     	privptr->protocol = proto;
3734     
3735     	for (direction = READ; direction <= WRITE; direction++) {
3736     		if ((ctc_no_auto == 0) || (devno[direction] == -1))
3737     			privptr->channel[direction] =
3738     				channel_get_next(type, direction);
3739     		else
3740     			privptr->channel[direction] =
3741     				channel_get(type, devno[direction], direction);
3742     		if (privptr->channel[direction] == NULL) {
3743     			if (direction == WRITE) {
3744     				FREE_IRQ(privptr->channel[READ]->irq,
3745     					 privptr->channel[READ]->devstat);
3746     				channel_free(privptr->channel[READ]);
3747     			}
3748     			ctc_free_netdevice(dev, 1);
3749     			return -ENODEV;
3750     		}
3751     		privptr->channel[direction]->netdev = dev;
3752     		privptr->channel[direction]->protocol = proto;
3753     		privptr->channel[direction]->max_bufsize = CTC_BUFSIZE_DEFAULT;
3754     		rc = REQUEST_IRQ(privptr->channel[direction]->irq,
3755     				 (void *)ctc_irq_handler, SA_INTERRUPT,
3756     				 dev->name,
3757     				 privptr->channel[direction]->devstat);
3758     		if (rc) {
3759     			printk(KERN_WARNING
3760     			       "%s: requested irq %d is busy rc=%02x\n",
3761     			       dev->name, privptr->channel[direction]->irq,
3762     			       rc);
3763     			if (direction == WRITE) {
3764     				FREE_IRQ(privptr->channel[READ]->irq,
3765     					 privptr->channel[READ]->devstat);
3766     				channel_free(privptr->channel[READ]);
3767     			}
3768     			channel_free(privptr->channel[direction]);
3769     			ctc_free_netdevice(dev, 1);
3770     			return -EBUSY;
3771     		}
3772     	}
3773     
3774     	/**
3775     	 * register subdir in /proc/net/ctc
3776     	 */
3777     	ctc_proc_create_sub(dev);
3778     
3779     	print_banner();
3780     
3781     	printk(KERN_INFO
3782     	       "%s: read: ch %04x (irq %04x), "
3783     	       "write: ch %04x (irq %04x) proto: %d\n",
3784     	       dev->name, privptr->channel[READ]->devno,
3785     	       privptr->channel[READ]->irq, privptr->channel[WRITE]->devno,
3786     	       privptr->channel[WRITE]->irq, proto);
3787     
3788     	return 0;
3789     }
3790     #endif
3791     
3792     /**
3793      * Module related routines
3794      *****************************************************************************/
3795     
3796     #ifdef MODULE
3797     /**
3798      * Prepare to be unloaded. Free IRQ's and release all resources.
3799      * This is called just before this module is unloaded. It is
3800      * <em>not</em> called, if the usage count is !0, so we don't need to check
3801      * for that.
3802      */
3803     void cleanup_module(void) {
3804     
3805     	ctc_tty_cleanup(0);
3806     	/* we are called if all interfaces are down only, so no need
3807     	 * to bother around with locking stuff
3808     	 */
3809     #ifndef CTC_CHANDEV
3810     	while (channels) {
3811     		if ((channels->flags & CHANNEL_FLAGS_INUSE) &&
3812     		    (channels->netdev != NULL)) {
3813     			net_device *dev = channels->netdev;
3814     			ctc_priv *privptr = dev->priv;
3815     
3816     			if (privptr) {
3817     				privptr->channel[READ]->netdev = NULL;
3818     				privptr->channel[WRITE]->netdev = NULL;
3819     			}
3820     			channels->netdev = NULL;
3821     			ctc_netdev_unregister(dev);
3822     			ctc_free_netdevice(dev, 1);
3823     		}
3824     		channel_remove(channels);
3825     	}
3826     	channels = NULL;
3827     #endif
3828     	ctc_tty_cleanup(1);
3829     	ctc_proc_destroy_main();
3830     #ifdef CTC_CHANDEV
3831     	chandev_unregister(ctc_chandev_probe, 1);
3832     #endif
3833     	printk(KERN_INFO "CTC driver unloaded\n");
3834     }
3835     
3836     #define ctc_init init_module
3837     #endif MODULE
3838     
3839     /**
3840      * Initialize module.
3841      * This is called just after the module is loaded.
3842      *
3843      * @return 0 on success, !0 on error.
3844      */
3845     int ctc_init(void) {
3846     #ifndef CTC_CHANDEV
3847     	int   cnt[2];
3848     	int   itype;
3849     	int   activated;
3850     	param *par;
3851     #endif
3852     	int   ret = 0;
3853     	int   probed = 0;
3854     
3855     	print_banner();
3856     
3857     #if defined(DEBUG) && !defined(CTC_CHANDEV)
3858     	printk(KERN_DEBUG
3859     	       "ctc: init_module(): got string '%s'\n", ctc);
3860     #endif
3861     
3862     #ifndef CTC_CHANDEV
3863     #ifdef MODULE
3864     	ctc_setup(ctc);
3865     #endif
3866     	par = params;
3867     #endif
3868     
3869     	activated = 0;
3870     	ctc_tty_init();
3871     #ifdef CTC_CHANDEV
3872     	chandev_register_and_probe(ctc_chandev_probe,
3873     				   (chandev_shutdownfunc)ctc_shutdown,
3874     				   ctc_chandev_msck_notify,
3875     				   chandev_type_ctc|chandev_type_escon);
3876     #else /* CTC_CHANDEV */
3877     	for (itype = 0; itype < 2; itype++) {
3878     		net_device *dev = NULL;
3879     		char       *bname = (itype) ? "escon" : "ctc";
3880     
3881     		cnt[itype] = 0;
3882     		do {
3883     			dev = ctc_init_netdevice(NULL, 1);
3884     			if (!dev) {
3885     				ret = -ENOMEM;
3886     				break;
3887     			}
3888     #if LINUX_VERSION_CODE < 0x020300
3889     			dev->name = (unsigned char *)dev + sizeof(net_device);
3890     #endif
3891     			if (par && par->name) {
3892     				char *p;
3893     				int  n;
3894     
3895     				sprintf(dev->name, "%s", par->name);
3896     				par = par->next;
3897     				for (p = dev->name; p && *p; p++)
3898     					if (isdigit(*p))
3899     						break;
3900     				if (p && *p) {
3901     					int it =
3902     						(strncmp(dev->name, "escon", 5))
3903     						? 1 : 0;
3904     					n = simple_strtoul(p, NULL, 0);
3905     					if (n >= cnt[it])
3906     						cnt[it] = n + 1;
3907     				}
3908     			} else {
3909     				if (ctc_no_auto) {
3910     					itype = 3;
3911     					ctc_free_netdevice(dev, 1);
3912     					dev = NULL;
3913     					break;
3914     				}
3915     				sprintf(dev->name, "%s%d", bname,
3916     					(cnt[itype])++);
3917     			}
3918     #ifdef DEBUG
3919     			printk(KERN_DEBUG "ctc: %s(): probing for device %s\n",
3920     						 __FUNCTION__, dev->name);
3921     #endif			
3922     			probed = 1;
3923     			if (ctc_probe(dev) == 0) {
3924     				ctc_priv *privptr = (ctc_priv *)dev->priv;
3925     #ifdef DEBUG
3926     				printk(KERN_DEBUG
3927     				       "ctc: %s(): probing succeeded\n",
3928     				       __FUNCTION__);
3929     				printk(KERN_DEBUG
3930     				       "ctc: %s(): registering device %s\n",
3931     				       __FUNCTION__, dev->name);
3932     #endif
3933     				if (ctc_netdev_register(dev) != 0) {
3934     					printk(KERN_WARNING
3935     					       "ctc: Couldn't register %s\n",
3936     					       dev->name);
3937     					FREE_IRQ(
3938     						privptr->channel[READ]->irq,
3939     						privptr->channel[READ]->devstat);
3940     					FREE_IRQ(
3941     						privptr->channel[WRITE]->irq,
3942     						privptr->channel[WRITE]->devstat);
3943     					channel_free(privptr->channel[READ]);
3944     					channel_free(privptr->channel[WRITE]);
3945     					ctc_free_netdevice(dev, 1);
3946     					dev = NULL;
3947     				} else {
3948     #ifdef DEBUG
3949     					printk(KERN_DEBUG
3950     					       "ctc: %s(): register succeed\n",
3951     					       __FUNCTION__);
3952     #endif			
3953     					activated++;
3954     				}
3955     			} else {
3956     #ifdef DEBUG
3957     				printk(KERN_DEBUG
3958     				       "ctc: %s(): probing failed\n",
3959     				       __FUNCTION__);
3960     #endif			
3961     				dev = NULL;
3962     			}
3963     		} while (dev && (ret == 0));
3964     	}
3965     #endif /* CHANDEV */
3966     #if !defined(CTC_CHANDEV) && defined(MODULE)
3967     	if (!activated) {
3968     		printk(KERN_WARNING "ctc: No devices registered\n");
3969     		ret = -ENODEV;
3970     	}
3971     #endif
3972     	if (ret) {
3973     		ctc_tty_cleanup(0);
3974     		ctc_tty_cleanup(1);
3975     #if defined(CTC_CHANDEV) && defined(MODULE)
3976     		chandev_unregister(ctc_chandev_probe, 0);
3977     #endif
3978     #ifdef MODULE
3979     		if (probed)
3980     			ctc_proc_destroy_main();
3981     #endif
3982     	}
3983     	return ret;
3984     }
3985     
3986     #ifndef MODULE
3987     #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,3,0))
3988     __initcall(ctc_init);
3989     #endif /* LINUX_VERSION_CODE */
3990     #endif /* MODULE */
3991     
3992     /* --- This is the END my friend --- */
3993