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