ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / s390 / net / ctcmain.c
1 /*
2  * $Id: ctcmain.c,v 1.59 2004/04/21 17:10:13 ptiedem 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  * Driver Model stuff by : Cornelia Huck <cohuck@de.ibm.com>
11  *
12  * Documentation used:
13  *  - Principles of Operation (IBM doc#: SA22-7201-06)
14  *  - Common IO/-Device Commands and Self Description (IBM doc#: SA22-7204-02)
15  *  - Common IO/-Device Commands and Self Description (IBM doc#: SN22-5535)
16  *  - ESCON Channel-to-Channel Adapter (IBM doc#: SA22-7203-00)
17  *  - ESCON I/O Interface (IBM doc#: SA22-7202-029
18  *
19  * and the source of the original CTC driver by:
20  *  Dieter Wellerdiek (wel@de.ibm.com)
21  *  Martin Schwidefsky (schwidefsky@de.ibm.com)
22  *  Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
23  *  Jochen Röhrig (roehrig@de.ibm.com)
24  *
25  * This program is free software; you can redistribute it and/or modify
26  * it under the terms of the GNU General Public License as published by
27  * the Free Software Foundation; either version 2, or (at your option)
28  * any later version.
29  *
30  * This program is distributed in the hope that it will be useful,
31  * but WITHOUT ANY WARRANTY; without even the implied warranty of
32  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33  * GNU General Public License for more details.
34  *
35  * You should have received a copy of the GNU General Public License
36  * along with this program; if not, write to the Free Software
37  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
38  *
39  * RELEASE-TAG: CTC/ESCON network driver $Revision: 1.59 $
40  *
41  */
42 \f
43 #undef DEBUG
44
45 #include <linux/module.h>
46 #include <linux/init.h>
47 #include <linux/kernel.h>
48 #include <linux/slab.h>
49 #include <linux/errno.h>
50 #include <linux/types.h>
51 #include <linux/interrupt.h>
52 #include <linux/timer.h>
53 #include <linux/sched.h>
54
55 #include <linux/signal.h>
56 #include <linux/string.h>
57
58 #include <linux/ip.h>
59 #include <linux/if_arp.h>
60 #include <linux/tcp.h>
61 #include <linux/skbuff.h>
62 #include <linux/ctype.h>
63 #include <net/dst.h>
64
65 #include <asm/io.h>
66 #include <asm/ccwdev.h>
67 #include <asm/ccwgroup.h>
68 #include <asm/bitops.h>
69 #include <asm/uaccess.h>
70
71 #include <asm/idals.h>
72
73 #include "ctctty.h"
74 #include "fsm.h"
75 #include "cu3088.h"
76
77 MODULE_AUTHOR("(C) 2000 IBM Corp. by Fritz Elfert (felfert@millenux.com)");
78 MODULE_DESCRIPTION("Linux for S/390 CTC/Escon Driver");
79 MODULE_LICENSE("GPL");
80
81 /**
82  * CCW commands, used in this driver.
83  */
84 #define CCW_CMD_WRITE           0x01
85 #define CCW_CMD_READ            0x02
86 #define CCW_CMD_SET_EXTENDED    0xc3
87 #define CCW_CMD_PREPARE         0xe3
88
89 #define CTC_PROTO_S390          0
90 #define CTC_PROTO_LINUX         1
91 #define CTC_PROTO_LINUX_TTY     2
92 #define CTC_PROTO_OS390         3
93 #define CTC_PROTO_MAX           3
94
95 #define CTC_BUFSIZE_LIMIT       65535
96 #define CTC_BUFSIZE_DEFAULT     32768
97
98 #define CTC_TIMEOUT_5SEC        5000
99
100 #define CTC_INITIAL_BLOCKLEN    2
101
102 #define READ                    0
103 #define WRITE                   1
104
105 #define CTC_ID_SIZE             BUS_ID_SIZE+3
106 \f
107
108 struct ctc_profile {
109         unsigned long maxmulti;
110         unsigned long maxcqueue;
111         unsigned long doios_single;
112         unsigned long doios_multi;
113         unsigned long txlen;
114         unsigned long tx_time;
115         struct timespec send_stamp;
116 };
117
118 /**
119  * Definition of one channel
120  */
121 struct channel {
122
123         /**
124          * Pointer to next channel in list.
125          */
126         struct channel *next;
127         char id[CTC_ID_SIZE];
128         struct ccw_device *cdev;
129
130         /**
131          * Type of this channel.
132          * CTC/A or Escon for valid channels.
133          */
134         enum channel_types type;
135
136         /**
137          * Misc. flags. See CHANNEL_FLAGS_... below
138          */
139         __u32 flags;
140
141         /**
142          * The protocol of this channel
143          */
144         __u16 protocol;
145
146         /**
147          * I/O and irq related stuff
148          */
149         struct ccw1 *ccw;
150         struct irb *irb;
151
152         /**
153          * RX/TX buffer size
154          */
155         int max_bufsize;
156
157         /**
158          * Transmit/Receive buffer.
159          */
160         struct sk_buff *trans_skb;
161
162         /**
163          * Universal I/O queue.
164          */
165         struct sk_buff_head io_queue;
166
167         /**
168          * TX queue for collecting skb's during busy.
169          */
170         struct sk_buff_head collect_queue;
171
172         /**
173          * Amount of data in collect_queue.
174          */
175         int collect_len;
176
177         /**
178          * spinlock for collect_queue and collect_len
179          */
180         spinlock_t collect_lock;
181
182         /**
183          * Timer for detecting unresposive
184          * I/O operations.
185          */
186         fsm_timer timer;
187
188         /**
189          * Retry counter for misc. operations.
190          */
191         int retry;
192
193         /**
194          * The finite state machine of this channel
195          */
196         fsm_instance *fsm;
197
198         /**
199          * The corresponding net_device this channel
200          * belongs to.
201          */
202         struct net_device *netdev;
203
204         struct ctc_profile prof;
205
206         unsigned char *trans_skb_data;
207
208         __u16 logflags;
209 };
210
211 #define CHANNEL_FLAGS_READ            0
212 #define CHANNEL_FLAGS_WRITE           1
213 #define CHANNEL_FLAGS_INUSE           2
214 #define CHANNEL_FLAGS_BUFSIZE_CHANGED 4
215 #define CHANNEL_FLAGS_FAILED          8
216 #define CHANNEL_FLAGS_WAITIRQ        16
217 #define CHANNEL_FLAGS_RWMASK 1
218 #define CHANNEL_DIRECTION(f) (f & CHANNEL_FLAGS_RWMASK)
219
220 #define LOG_FLAG_ILLEGALPKT  1
221 #define LOG_FLAG_ILLEGALSIZE 2
222 #define LOG_FLAG_OVERRUN     4
223 #define LOG_FLAG_NOMEM       8
224
225 #define CTC_LOGLEVEL_INFO     1
226 #define CTC_LOGLEVEL_NOTICE   2
227 #define CTC_LOGLEVEL_WARN     4
228 #define CTC_LOGLEVEL_EMERG    8
229 #define CTC_LOGLEVEL_ERR     16
230 #define CTC_LOGLEVEL_DEBUG   32
231 #define CTC_LOGLEVEL_CRIT    64
232
233 #define CTC_LOGLEVEL_DEFAULT \
234 (CTC_LOGLEVEL_INFO | CTC_LOGLEVEL_NOTICE | CTC_LOGLEVEL_WARN | CTC_LOGLEVEL_CRIT)
235
236 #define CTC_LOGLEVEL_MAX     ((CTC_LOGLEVEL_CRIT<<1)-1)
237
238 static int loglevel = CTC_LOGLEVEL_DEFAULT;
239
240 #define ctc_pr_debug(fmt, arg...) \
241 do { if (loglevel & CTC_LOGLEVEL_DEBUG) printk(KERN_DEBUG fmt,##arg); } while (0)
242
243 #define ctc_pr_info(fmt, arg...) \
244 do { if (loglevel & CTC_LOGLEVEL_INFO) printk(KERN_INFO fmt,##arg); } while (0)
245
246 #define ctc_pr_notice(fmt, arg...) \
247 do { if (loglevel & CTC_LOGLEVEL_NOTICE) printk(KERN_NOTICE fmt,##arg); } while (0)
248
249 #define ctc_pr_warn(fmt, arg...) \
250 do { if (loglevel & CTC_LOGLEVEL_WARN) printk(KERN_WARNING fmt,##arg); } while (0)
251
252 #define ctc_pr_emerg(fmt, arg...) \
253 do { if (loglevel & CTC_LOGLEVEL_EMERG) printk(KERN_EMERG fmt,##arg); } while (0)
254
255 #define ctc_pr_err(fmt, arg...) \
256 do { if (loglevel & CTC_LOGLEVEL_ERR) printk(KERN_ERR fmt,##arg); } while (0)
257
258 #define ctc_pr_crit(fmt, arg...) \
259 do { if (loglevel & CTC_LOGLEVEL_CRIT) printk(KERN_CRIT fmt,##arg); } while (0)
260
261 /**
262  * Linked list of all detected channels.
263  */
264 static struct channel *channels = NULL;
265
266 struct ctc_priv {
267         struct net_device_stats stats;
268         unsigned long tbusy;
269         /**
270          * The finite state machine of this interface.
271          */
272         fsm_instance *fsm;
273         /**
274          * The protocol of this device
275          */
276         __u16 protocol;
277         /**
278          * Timer for restarting after I/O Errors
279          */
280         fsm_timer               restart_timer;
281
282         struct channel *channel[2];
283 };
284
285 /**
286  * Definition of our link level header.
287  */
288 struct ll_header {
289         __u16 length;
290         __u16 type;
291         __u16 unused;
292 };
293 #define LL_HEADER_LENGTH (sizeof(struct ll_header))
294
295 /**
296  * Compatibility macros for busy handling
297  * of network devices.
298  */
299 static __inline__ void
300 ctc_clear_busy(struct net_device * dev)
301 {
302         clear_bit(0, &(((struct ctc_priv *) dev->priv)->tbusy));
303         if (((struct ctc_priv *)dev->priv)->protocol != CTC_PROTO_LINUX_TTY)
304                 netif_wake_queue(dev);
305 }
306
307 static __inline__ int
308 ctc_test_and_set_busy(struct net_device * dev)
309 {
310         if (((struct ctc_priv *)dev->priv)->protocol != CTC_PROTO_LINUX_TTY)
311                 netif_stop_queue(dev);
312         return test_and_set_bit(0, &((struct ctc_priv *) dev->priv)->tbusy);
313 }
314
315 /**
316  * Print Banner.
317  */
318 static void
319 print_banner(void)
320 {
321         static int printed = 0;
322         char vbuf[] = "$Revision: 1.59 $";
323         char *version = vbuf;
324
325         if (printed)
326                 return;
327         if ((version = strchr(version, ':'))) {
328                 char *p = strchr(version + 1, '$');
329                 if (p)
330                         *p = '\0';
331         } else
332                 version = " ??? ";
333         printk(KERN_INFO "CTC driver Version%s"
334 #ifdef DEBUG
335                     " (DEBUG-VERSION, " __DATE__ __TIME__ ")"
336 #endif
337                     " initialized\n", version);
338         printed = 1;
339 }
340 \f
341 /**
342  * Return type of a detected device.
343  */
344 static enum channel_types
345 get_channel_type(struct ccw_device_id *id)
346 {
347         enum channel_types type = (enum channel_types) id->driver_info;
348
349         if (type == channel_type_ficon)
350                 type = channel_type_escon;
351
352         return type;
353 }
354 \f
355 /**
356  * States of the interface statemachine.
357  */
358 enum dev_states {
359         DEV_STATE_STOPPED,
360         DEV_STATE_STARTWAIT_RXTX,
361         DEV_STATE_STARTWAIT_RX,
362         DEV_STATE_STARTWAIT_TX,
363         DEV_STATE_STOPWAIT_RXTX,
364         DEV_STATE_STOPWAIT_RX,
365         DEV_STATE_STOPWAIT_TX,
366         DEV_STATE_RUNNING,
367         /**
368          * MUST be always the last element!!
369          */
370         NR_DEV_STATES
371 };
372
373 static const char *dev_state_names[] = {
374         "Stopped",
375         "StartWait RXTX",
376         "StartWait RX",
377         "StartWait TX",
378         "StopWait RXTX",
379         "StopWait RX",
380         "StopWait TX",
381         "Running",
382 };
383
384 /**
385  * Events of the interface statemachine.
386  */
387 enum dev_events {
388         DEV_EVENT_START,
389         DEV_EVENT_STOP,
390         DEV_EVENT_RXUP,
391         DEV_EVENT_TXUP,
392         DEV_EVENT_RXDOWN,
393         DEV_EVENT_TXDOWN,
394         DEV_EVENT_RESTART,
395         /**
396          * MUST be always the last element!!
397          */
398         NR_DEV_EVENTS
399 };
400
401 static const char *dev_event_names[] = {
402         "Start",
403         "Stop",
404         "RX up",
405         "TX up",
406         "RX down",
407         "TX down",
408         "Restart",
409 };
410 \f
411 /**
412  * Events of the channel statemachine
413  */
414 enum ch_events {
415         /**
416          * Events, representing return code of
417          * I/O operations (ccw_device_start, ccw_device_halt et al.)
418          */
419         CH_EVENT_IO_SUCCESS,
420         CH_EVENT_IO_EBUSY,
421         CH_EVENT_IO_ENODEV,
422         CH_EVENT_IO_EIO,
423         CH_EVENT_IO_UNKNOWN,
424
425         CH_EVENT_ATTNBUSY,
426         CH_EVENT_ATTN,
427         CH_EVENT_BUSY,
428
429         /**
430          * Events, representing unit-check
431          */
432         CH_EVENT_UC_RCRESET,
433         CH_EVENT_UC_RSRESET,
434         CH_EVENT_UC_TXTIMEOUT,
435         CH_EVENT_UC_TXPARITY,
436         CH_EVENT_UC_HWFAIL,
437         CH_EVENT_UC_RXPARITY,
438         CH_EVENT_UC_ZERO,
439         CH_EVENT_UC_UNKNOWN,
440
441         /**
442          * Events, representing subchannel-check
443          */
444         CH_EVENT_SC_UNKNOWN,
445
446         /**
447          * Events, representing machine checks
448          */
449         CH_EVENT_MC_FAIL,
450         CH_EVENT_MC_GOOD,
451
452         /**
453          * Event, representing normal IRQ
454          */
455         CH_EVENT_IRQ,
456         CH_EVENT_FINSTAT,
457
458         /**
459          * Event, representing timer expiry.
460          */
461         CH_EVENT_TIMER,
462
463         /**
464          * Events, representing commands from upper levels.
465          */
466         CH_EVENT_START,
467         CH_EVENT_STOP,
468
469         /**
470          * MUST be always the last element!!
471          */
472         NR_CH_EVENTS,
473 };
474
475 static const char *ch_event_names[] = {
476         "ccw_device success",
477         "ccw_device busy",
478         "ccw_device enodev",
479         "ccw_device ioerr",
480         "ccw_device unknown",
481
482         "Status ATTN & BUSY",
483         "Status ATTN",
484         "Status BUSY",
485
486         "Unit check remote reset",
487         "Unit check remote system reset",
488         "Unit check TX timeout",
489         "Unit check TX parity",
490         "Unit check Hardware failure",
491         "Unit check RX parity",
492         "Unit check ZERO",
493         "Unit check Unknown",
494
495         "SubChannel check Unknown",
496
497         "Machine check failure",
498         "Machine check operational",
499
500         "IRQ normal",
501         "IRQ final",
502
503         "Timer",
504
505         "Start",
506         "Stop",
507 };
508
509 /**
510  * States of the channel statemachine.
511  */
512 enum ch_states {
513         /**
514          * Channel not assigned to any device,
515          * initial state, direction invalid
516          */
517         CH_STATE_IDLE,
518
519         /**
520          * Channel assigned but not operating
521          */
522         CH_STATE_STOPPED,
523         CH_STATE_STARTWAIT,
524         CH_STATE_STARTRETRY,
525         CH_STATE_SETUPWAIT,
526         CH_STATE_RXINIT,
527         CH_STATE_TXINIT,
528         CH_STATE_RX,
529         CH_STATE_TX,
530         CH_STATE_RXIDLE,
531         CH_STATE_TXIDLE,
532         CH_STATE_RXERR,
533         CH_STATE_TXERR,
534         CH_STATE_TERM,
535         CH_STATE_DTERM,
536         CH_STATE_NOTOP,
537
538         /**
539          * MUST be always the last element!!
540          */
541         NR_CH_STATES,
542 };
543
544 static const char *ch_state_names[] = {
545         "Idle",
546         "Stopped",
547         "StartWait",
548         "StartRetry",
549         "SetupWait",
550         "RX init",
551         "TX init",
552         "RX",
553         "TX",
554         "RX idle",
555         "TX idle",
556         "RX error",
557         "TX error",
558         "Terminating",
559         "Restarting",
560         "Not operational",
561 };
562 \f
563 #ifdef DEBUG
564 /**
565  * Dump header and first 16 bytes of an sk_buff for debugging purposes.
566  *
567  * @param skb    The sk_buff to dump.
568  * @param offset Offset relative to skb-data, where to start the dump.
569  */
570 static void
571 ctc_dump_skb(struct sk_buff *skb, int offset)
572 {
573         unsigned char *p = skb->data;
574         __u16 bl;
575         struct ll_header *header;
576         int i;
577
578         if (!(loglevel & CTC_LOGLEVEL_DEBUG))
579                 return;
580         p += offset;
581         bl = *((__u16 *) p);
582         p += 2;
583         header = (struct ll_header *) p;
584         p -= 2;
585
586         printk(KERN_DEBUG "dump:\n");
587         printk(KERN_DEBUG "blocklen=%d %04x\n", bl, bl);
588
589         printk(KERN_DEBUG "h->length=%d %04x\n", header->length,
590                header->length);
591         printk(KERN_DEBUG "h->type=%04x\n", header->type);
592         printk(KERN_DEBUG "h->unused=%04x\n", header->unused);
593         if (bl > 16)
594                 bl = 16;
595         printk(KERN_DEBUG "data: ");
596         for (i = 0; i < bl; i++)
597                 printk("%02x%s", *p++, (i % 16) ? " " : "\n<7>");
598         printk("\n");
599 }
600 #else
601 static inline void
602 ctc_dump_skb(struct sk_buff *skb, int offset)
603 {
604 }
605 #endif
606
607 /**
608  * Unpack a just received skb and hand it over to
609  * upper layers.
610  *
611  * @param ch The channel where this skb has been received.
612  * @param pskb The received skb.
613  */
614 static __inline__ void
615 ctc_unpack_skb(struct channel *ch, struct sk_buff *pskb)
616 {
617         struct net_device *dev = ch->netdev;
618         struct ctc_priv *privptr = (struct ctc_priv *) dev->priv;
619
620         __u16 len = *((__u16 *) pskb->data);
621         skb_put(pskb, 2 + LL_HEADER_LENGTH);
622         skb_pull(pskb, 2);
623         pskb->dev = dev;
624         pskb->ip_summed = CHECKSUM_UNNECESSARY;
625         while (len > 0) {
626                 struct sk_buff *skb;
627                 struct ll_header *header = (struct ll_header *) pskb->data;
628
629                 skb_pull(pskb, LL_HEADER_LENGTH);
630                 if ((ch->protocol == CTC_PROTO_S390) &&
631                     (header->type != ETH_P_IP)) {
632
633 #ifndef DEBUG
634                         if (!(ch->logflags & LOG_FLAG_ILLEGALPKT)) {
635 #endif
636                                 /**
637                                  * Check packet type only if we stick strictly
638                                  * to S/390's protocol of OS390. This only
639                                  * supports IP. Otherwise allow any packet
640                                  * type.
641                                  */
642                                 ctc_pr_warn(
643                                         "%s Illegal packet type 0x%04x received, dropping\n",
644                                         dev->name, header->type);
645                                 ch->logflags |= LOG_FLAG_ILLEGALPKT;
646 #ifndef DEBUG
647                         }
648 #endif
649 #ifdef DEBUG
650                         ctc_dump_skb(pskb, -6);
651 #endif
652                         privptr->stats.rx_dropped++;
653                         privptr->stats.rx_frame_errors++;
654                         return;
655                 }
656                 pskb->protocol = ntohs(header->type);
657                 if (header->length <= LL_HEADER_LENGTH) {
658 #ifndef DEBUG
659                         if (!(ch->logflags & LOG_FLAG_ILLEGALSIZE)) {
660 #endif
661                                 ctc_pr_warn(
662                                        "%s Illegal packet size %d "
663                                        "received (MTU=%d blocklen=%d), "
664                                        "dropping\n", dev->name, header->length,
665                                        dev->mtu, len);
666                                 ch->logflags |= LOG_FLAG_ILLEGALSIZE;
667 #ifndef DEBUG
668                         }
669 #endif
670 #ifdef DEBUG
671                         ctc_dump_skb(pskb, -6);
672 #endif
673                         privptr->stats.rx_dropped++;
674                         privptr->stats.rx_length_errors++;
675                         return;
676                 }
677                 header->length -= LL_HEADER_LENGTH;
678                 len -= LL_HEADER_LENGTH;
679                 if ((header->length > skb_tailroom(pskb)) ||
680                     (header->length > len)) {
681 #ifndef DEBUG
682                         if (!(ch->logflags & LOG_FLAG_OVERRUN)) {
683 #endif
684                                 ctc_pr_warn(
685                                         "%s Illegal packet size %d "
686                                         "(beyond the end of received data), "
687                                         "dropping\n", dev->name, header->length);
688                                 ch->logflags |= LOG_FLAG_OVERRUN;
689 #ifndef DEBUG
690                         }
691 #endif
692 #ifdef DEBUG
693                         ctc_dump_skb(pskb, -6);
694 #endif
695                         privptr->stats.rx_dropped++;
696                         privptr->stats.rx_length_errors++;
697                         return;
698                 }
699                 skb_put(pskb, header->length);
700                 pskb->mac.raw = pskb->data;
701                 len -= header->length;
702                 skb = dev_alloc_skb(pskb->len);
703                 if (!skb) {
704 #ifndef DEBUG
705                         if (!(ch->logflags & LOG_FLAG_NOMEM)) {
706 #endif
707                                 ctc_pr_warn(
708                                         "%s Out of memory in ctc_unpack_skb\n",
709                                         dev->name);
710                                 ch->logflags |= LOG_FLAG_NOMEM;
711 #ifndef DEBUG
712                         }
713 #endif
714                         privptr->stats.rx_dropped++;
715                         return;
716                 }
717                 memcpy(skb_put(skb, pskb->len), pskb->data, pskb->len);
718                 skb->mac.raw = skb->data;
719                 skb->dev = pskb->dev;
720                 skb->protocol = pskb->protocol;
721                 pskb->ip_summed = CHECKSUM_UNNECESSARY;
722                 if (ch->protocol == CTC_PROTO_LINUX_TTY)
723                         ctc_tty_netif_rx(skb);
724                 else
725                         netif_rx(skb);
726                 /**
727                  * Successful rx; reset logflags
728                  */
729                 ch->logflags = 0;
730                 dev->last_rx = jiffies;
731                 privptr->stats.rx_packets++;
732                 privptr->stats.rx_bytes += skb->len;
733                 if (len > 0) {
734                         skb_pull(pskb, header->length);
735                         if (skb_tailroom(pskb) < LL_HEADER_LENGTH) {
736 #ifndef DEBUG
737                                 if (!(ch->logflags & LOG_FLAG_OVERRUN)) {
738 #endif
739                                         ctc_pr_warn(
740                                                 "%s Overrun in ctc_unpack_skb\n",
741                                                 dev->name);
742                                         ch->logflags |= LOG_FLAG_OVERRUN;
743 #ifndef DEBUG
744                                 }
745 #endif
746                                 return;
747                         }
748                         skb_put(pskb, LL_HEADER_LENGTH);
749                 }
750         }
751 }
752
753 /**
754  * Check return code of a preceeding ccw_device call, halt_IO etc...
755  *
756  * @param ch          The channel, the error belongs to.
757  * @param return_code The error code to inspect.
758  */
759 static void inline
760 ccw_check_return_code(struct channel *ch, int return_code, char *msg)
761 {
762         switch (return_code) {
763                 case 0:
764                         fsm_event(ch->fsm, CH_EVENT_IO_SUCCESS, ch);
765                         break;
766                 case -EBUSY:
767                         ctc_pr_warn("%s (%s): Busy !\n", ch->id, msg);
768                         fsm_event(ch->fsm, CH_EVENT_IO_EBUSY, ch);
769                         break;
770                 case -ENODEV:
771                         ctc_pr_emerg("%s (%s): Invalid device called for IO\n",
772                                      ch->id, msg);
773                         fsm_event(ch->fsm, CH_EVENT_IO_ENODEV, ch);
774                         break;
775                 case -EIO:
776                         ctc_pr_emerg("%s (%s): Status pending... \n",
777                                      ch->id, msg);
778                         fsm_event(ch->fsm, CH_EVENT_IO_EIO, ch);
779                         break;
780                 default:
781                         ctc_pr_emerg("%s (%s): Unknown error in do_IO %04x\n",
782                                      ch->id, msg, return_code);
783                         fsm_event(ch->fsm, CH_EVENT_IO_UNKNOWN, ch);
784         }
785 }
786
787 /**
788  * Check sense of a unit check.
789  *
790  * @param ch    The channel, the sense code belongs to.
791  * @param sense The sense code to inspect.
792  */
793 static void inline
794 ccw_unit_check(struct channel *ch, unsigned char sense)
795 {
796         if (sense & SNS0_INTERVENTION_REQ) {
797                 if (sense & 0x01) {
798                         if (ch->protocol != CTC_PROTO_LINUX_TTY)
799                                 ctc_pr_debug("%s: Interface disc. or Sel. reset "
800                                         "(remote)\n", ch->id);
801                         fsm_event(ch->fsm, CH_EVENT_UC_RCRESET, ch);
802                 } else {
803                         ctc_pr_debug("%s: System reset (remote)\n", ch->id);
804                         fsm_event(ch->fsm, CH_EVENT_UC_RSRESET, ch);
805                 }
806         } else if (sense & SNS0_EQUIPMENT_CHECK) {
807                 if (sense & SNS0_BUS_OUT_CHECK) {
808                         ctc_pr_warn("%s: Hardware malfunction (remote)\n",
809                                     ch->id);
810                         fsm_event(ch->fsm, CH_EVENT_UC_HWFAIL, ch);
811                 } else {
812                         ctc_pr_warn("%s: Read-data parity error (remote)\n",
813                                     ch->id);
814                         fsm_event(ch->fsm, CH_EVENT_UC_RXPARITY, ch);
815                 }
816         } else if (sense & SNS0_BUS_OUT_CHECK) {
817                 if (sense & 0x04) {
818                         ctc_pr_warn("%s: Data-streaming timeout)\n", ch->id);
819                         fsm_event(ch->fsm, CH_EVENT_UC_TXTIMEOUT, ch);
820                 } else {
821                         ctc_pr_warn("%s: Data-transfer parity error\n", ch->id);
822                         fsm_event(ch->fsm, CH_EVENT_UC_TXPARITY, ch);
823                 }
824         } else if (sense & SNS0_CMD_REJECT) {
825                 ctc_pr_warn("%s: Command reject\n", ch->id);
826         } else if (sense == 0) {
827                 ctc_pr_debug("%s: Unit check ZERO\n", ch->id);
828                 fsm_event(ch->fsm, CH_EVENT_UC_ZERO, ch);
829         } else {
830                 ctc_pr_warn("%s: Unit Check with sense code: %02x\n",
831                             ch->id, sense);
832                 fsm_event(ch->fsm, CH_EVENT_UC_UNKNOWN, ch);
833         }
834 }
835
836 static void
837 ctc_purge_skb_queue(struct sk_buff_head *q)
838 {
839         struct sk_buff *skb;
840
841         while ((skb = skb_dequeue(q))) {
842                 atomic_dec(&skb->users);
843                 dev_kfree_skb_irq(skb);
844         }
845 }
846
847 static __inline__ int
848 ctc_checkalloc_buffer(struct channel *ch, int warn)
849 {
850         if ((ch->trans_skb == NULL) ||
851             (ch->flags & CHANNEL_FLAGS_BUFSIZE_CHANGED)) {
852                 if (ch->trans_skb != NULL)
853                         dev_kfree_skb(ch->trans_skb);
854                 clear_normalized_cda(&ch->ccw[1]);
855                 ch->trans_skb = __dev_alloc_skb(ch->max_bufsize,
856                                                 GFP_ATOMIC | GFP_DMA);
857                 if (ch->trans_skb == NULL) {
858                         if (warn)
859                                 ctc_pr_warn(
860                                         "%s: Couldn't alloc %s trans_skb\n",
861                                         ch->id,
862                                         (CHANNEL_DIRECTION(ch->flags) == READ) ?
863                                         "RX" : "TX");
864                         return -ENOMEM;
865                 }
866                 ch->ccw[1].count = ch->max_bufsize;
867                 if (set_normalized_cda(&ch->ccw[1], ch->trans_skb->data)) {
868                         dev_kfree_skb(ch->trans_skb);
869                         ch->trans_skb = NULL;
870                         if (warn)
871                                 ctc_pr_warn(
872                                         "%s: set_normalized_cda for %s "
873                                         "trans_skb failed, dropping packets\n",
874                                         ch->id,
875                                         (CHANNEL_DIRECTION(ch->flags) == READ) ?
876                                         "RX" : "TX");
877                         return -ENOMEM;
878                 }
879                 ch->ccw[1].count = 0;
880                 ch->trans_skb_data = ch->trans_skb->data;
881                 ch->flags &= ~CHANNEL_FLAGS_BUFSIZE_CHANGED;
882         }
883         return 0;
884 }
885
886 /**
887  * Dummy NOP action for statemachines
888  */
889 static void
890 fsm_action_nop(fsm_instance * fi, int event, void *arg)
891 {
892 }
893 \f
894 /**
895  * Actions for channel - statemachines.
896  *****************************************************************************/
897
898 /**
899  * Normal data has been send. Free the corresponding
900  * skb (it's in io_queue), reset dev->tbusy and
901  * revert to idle state.
902  *
903  * @param fi    An instance of a channel statemachine.
904  * @param event The event, just happened.
905  * @param arg   Generic pointer, casted from channel * upon call.
906  */
907 static void
908 ch_action_txdone(fsm_instance * fi, int event, void *arg)
909 {
910         struct channel *ch = (struct channel *) arg;
911         struct net_device *dev = ch->netdev;
912         struct ctc_priv *privptr = dev->priv;
913         struct sk_buff *skb;
914         int first = 1;
915         int i;
916
917         struct timespec done_stamp = xtime;
918         unsigned long duration =
919             (done_stamp.tv_sec - ch->prof.send_stamp.tv_sec) * 1000000 +
920             (done_stamp.tv_nsec - ch->prof.send_stamp.tv_nsec) / 1000;
921         if (duration > ch->prof.tx_time)
922                 ch->prof.tx_time = duration;
923
924         if (ch->irb->scsw.count != 0)
925                 ctc_pr_debug("%s: TX not complete, remaining %d bytes\n",
926                              dev->name, ch->irb->scsw.count);
927         fsm_deltimer(&ch->timer);
928         while ((skb = skb_dequeue(&ch->io_queue))) {
929                 privptr->stats.tx_packets++;
930                 privptr->stats.tx_bytes += skb->len - LL_HEADER_LENGTH;
931                 if (first) {
932                         privptr->stats.tx_bytes += 2;
933                         first = 0;
934                 }
935                 atomic_dec(&skb->users);
936                 dev_kfree_skb_irq(skb);
937         }
938         spin_lock(&ch->collect_lock);
939         clear_normalized_cda(&ch->ccw[4]);
940         if (ch->collect_len > 0) {
941                 int rc;
942
943                 if (ctc_checkalloc_buffer(ch, 1)) {
944                         spin_unlock(&ch->collect_lock);
945                         return;
946                 }
947                 ch->trans_skb->tail = ch->trans_skb->data = ch->trans_skb_data;
948                 ch->trans_skb->len = 0;
949                 if (ch->prof.maxmulti < (ch->collect_len + 2))
950                         ch->prof.maxmulti = ch->collect_len + 2;
951                 if (ch->prof.maxcqueue < skb_queue_len(&ch->collect_queue))
952                         ch->prof.maxcqueue = skb_queue_len(&ch->collect_queue);
953                 *((__u16 *) skb_put(ch->trans_skb, 2)) = ch->collect_len + 2;
954                 i = 0;
955                 while ((skb = skb_dequeue(&ch->collect_queue))) {
956                         memcpy(skb_put(ch->trans_skb, skb->len), skb->data,
957                                skb->len);
958                         privptr->stats.tx_packets++;
959                         privptr->stats.tx_bytes += skb->len - LL_HEADER_LENGTH;
960                         atomic_dec(&skb->users);
961                         dev_kfree_skb_irq(skb);
962                         i++;
963                 }
964                 ch->collect_len = 0;
965                 spin_unlock(&ch->collect_lock);
966                 ch->ccw[1].count = ch->trans_skb->len;
967                 fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC, CH_EVENT_TIMER, ch);
968                 ch->prof.send_stamp = xtime;
969                 rc = ccw_device_start(ch->cdev, &ch->ccw[0],
970                                       (unsigned long) ch, 0xff, 0);
971                 ch->prof.doios_multi++;
972                 if (rc != 0) {
973                         privptr->stats.tx_dropped += i;
974                         privptr->stats.tx_errors += i;
975                         fsm_deltimer(&ch->timer);
976                         ccw_check_return_code(ch, rc, "chained TX");
977                 }
978         } else {
979                 spin_unlock(&ch->collect_lock);
980                 fsm_newstate(fi, CH_STATE_TXIDLE);
981         }
982         ctc_clear_busy(dev);
983 }
984
985 /**
986  * Initial data is sent.
987  * Notify device statemachine that we are up and
988  * running.
989  *
990  * @param fi    An instance of a channel statemachine.
991  * @param event The event, just happened.
992  * @param arg   Generic pointer, casted from channel * upon call.
993  */
994 static void
995 ch_action_txidle(fsm_instance * fi, int event, void *arg)
996 {
997         struct channel *ch = (struct channel *) arg;
998
999         fsm_deltimer(&ch->timer);
1000         fsm_newstate(fi, CH_STATE_TXIDLE);
1001         fsm_event(((struct ctc_priv *) ch->netdev->priv)->fsm, DEV_EVENT_TXUP,
1002                   ch->netdev);
1003 }
1004
1005 /**
1006  * Got normal data, check for sanity, queue it up, allocate new buffer
1007  * trigger bottom half, and initiate next read.
1008  *
1009  * @param fi    An instance of a channel statemachine.
1010  * @param event The event, just happened.
1011  * @param arg   Generic pointer, casted from channel * upon call.
1012  */
1013 static void
1014 ch_action_rx(fsm_instance * fi, int event, void *arg)
1015 {
1016         struct channel *ch = (struct channel *) arg;
1017         struct net_device *dev = ch->netdev;
1018         struct ctc_priv *privptr = dev->priv;
1019         int len = ch->max_bufsize - ch->irb->scsw.count;
1020         struct sk_buff *skb = ch->trans_skb;
1021         __u16 block_len = *((__u16 *) skb->data);
1022         int check_len;
1023         int rc;
1024
1025         fsm_deltimer(&ch->timer);
1026         if (len < 8) {
1027                 ctc_pr_debug("%s: got packet with length %d < 8\n",
1028                              dev->name, len);
1029                 privptr->stats.rx_dropped++;
1030                 privptr->stats.rx_length_errors++;
1031                 goto again;
1032         }
1033         if (len > ch->max_bufsize) {
1034                 ctc_pr_debug("%s: got packet with length %d > %d\n",
1035                              dev->name, len, ch->max_bufsize);
1036                 privptr->stats.rx_dropped++;
1037                 privptr->stats.rx_length_errors++;
1038                 goto again;
1039         }
1040
1041         /**
1042          * VM TCP seems to have a bug sending 2 trailing bytes of garbage.
1043          */
1044         switch (ch->protocol) {
1045                 case CTC_PROTO_S390:
1046                 case CTC_PROTO_OS390:
1047                         check_len = block_len + 2;
1048                         break;
1049                 default:
1050                         check_len = block_len;
1051                         break;
1052         }
1053         if ((len < block_len) || (len > check_len)) {
1054                 ctc_pr_debug("%s: got block length %d != rx length %d\n",
1055                              dev->name, block_len, len);
1056 #ifdef DEBUG
1057                 ctc_dump_skb(skb, 0);
1058 #endif
1059                 *((__u16 *) skb->data) = len;
1060                 privptr->stats.rx_dropped++;
1061                 privptr->stats.rx_length_errors++;
1062                 goto again;
1063         }
1064         block_len -= 2;
1065         if (block_len > 0) {
1066                 *((__u16 *) skb->data) = block_len;
1067                 ctc_unpack_skb(ch, skb);
1068         }
1069  again:
1070         skb->data = skb->tail = ch->trans_skb_data;
1071         skb->len = 0;
1072         if (ctc_checkalloc_buffer(ch, 1))
1073                 return;
1074         ch->ccw[1].count = ch->max_bufsize;
1075         rc = ccw_device_start(ch->cdev, &ch->ccw[0], (unsigned long) ch, 0xff, 0);
1076         if (rc != 0)
1077                 ccw_check_return_code(ch, rc, "normal RX");
1078 }
1079
1080 static void ch_action_rxidle(fsm_instance * fi, int event, void *arg);
1081
1082 /**
1083  * Initialize connection by sending a __u16 of value 0.
1084  *
1085  * @param fi    An instance of a channel statemachine.
1086  * @param event The event, just happened.
1087  * @param arg   Generic pointer, casted from channel * upon call.
1088  */
1089 static void
1090 ch_action_firstio(fsm_instance * fi, int event, void *arg)
1091 {
1092         struct channel *ch = (struct channel *) arg;
1093         int rc;
1094
1095         if (fsm_getstate(fi) == CH_STATE_TXIDLE)
1096                 ctc_pr_debug("%s: remote side issued READ?, init ...\n", ch->id);
1097         fsm_deltimer(&ch->timer);
1098         if (ctc_checkalloc_buffer(ch, 1))
1099                 return;
1100         if ((fsm_getstate(fi) == CH_STATE_SETUPWAIT) &&
1101             (ch->protocol == CTC_PROTO_OS390)) {
1102                 /* OS/390 resp. z/OS */
1103                 if (CHANNEL_DIRECTION(ch->flags) == READ) {
1104                         *((__u16 *) ch->trans_skb->data) = CTC_INITIAL_BLOCKLEN;
1105                         fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC,
1106                                      CH_EVENT_TIMER, ch);
1107                         ch_action_rxidle(fi, event, arg);
1108                 } else {
1109                         struct net_device *dev = ch->netdev;
1110                         fsm_newstate(fi, CH_STATE_TXIDLE);
1111                         fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1112                                   DEV_EVENT_TXUP, dev);
1113                 }
1114                 return;
1115         }
1116
1117         /**
1118          * Don´t setup a timer for receiving the initial RX frame
1119          * if in compatibility mode, since VM TCP delays the initial
1120          * frame until it has some data to send.
1121          */
1122         if ((CHANNEL_DIRECTION(ch->flags) == WRITE) ||
1123             (ch->protocol != CTC_PROTO_S390))
1124                 fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC, CH_EVENT_TIMER, ch);
1125
1126         *((__u16 *) ch->trans_skb->data) = CTC_INITIAL_BLOCKLEN;
1127         ch->ccw[1].count = 2;   /* Transfer only length */
1128
1129         fsm_newstate(fi, (CHANNEL_DIRECTION(ch->flags) == READ)
1130                      ? CH_STATE_RXINIT : CH_STATE_TXINIT);
1131         rc = ccw_device_start(ch->cdev, &ch->ccw[0], (unsigned long) ch, 0xff, 0);
1132         if (rc != 0) {
1133                 fsm_deltimer(&ch->timer);
1134                 fsm_newstate(fi, CH_STATE_SETUPWAIT);
1135                 ccw_check_return_code(ch, rc, "init IO");
1136         }
1137         /**
1138          * If in compatibility mode since we don´t setup a timer, we
1139          * also signal RX channel up immediately. This enables us
1140          * to send packets early which in turn usually triggers some
1141          * reply from VM TCP which brings up the RX channel to it´s
1142          * final state.
1143          */
1144         if ((CHANNEL_DIRECTION(ch->flags) == READ) &&
1145             (ch->protocol == CTC_PROTO_S390)) {
1146                 struct net_device *dev = ch->netdev;
1147                 fsm_event(((struct ctc_priv *) dev->priv)->fsm, DEV_EVENT_RXUP,
1148                           dev);
1149         }
1150 }
1151
1152 /**
1153  * Got initial data, check it. If OK,
1154  * notify device statemachine that we are up and
1155  * running.
1156  *
1157  * @param fi    An instance of a channel statemachine.
1158  * @param event The event, just happened.
1159  * @param arg   Generic pointer, casted from channel * upon call.
1160  */
1161 static void
1162 ch_action_rxidle(fsm_instance * fi, int event, void *arg)
1163 {
1164         struct channel *ch = (struct channel *) arg;
1165         struct net_device *dev = ch->netdev;
1166         __u16 buflen;
1167         int rc;
1168
1169         fsm_deltimer(&ch->timer);
1170         buflen = *((__u16 *) ch->trans_skb->data);
1171 #ifdef DEBUG
1172         ctc_pr_debug("%s: Initial RX count %d\n", dev->name, buflen);
1173 #endif
1174         if (buflen >= CTC_INITIAL_BLOCKLEN) {
1175                 if (ctc_checkalloc_buffer(ch, 1))
1176                         return;
1177                 ch->ccw[1].count = ch->max_bufsize;
1178                 fsm_newstate(fi, CH_STATE_RXIDLE);
1179                 rc = ccw_device_start(ch->cdev, &ch->ccw[0],
1180                                       (unsigned long) ch, 0xff, 0);
1181                 if (rc != 0) {
1182                         fsm_newstate(fi, CH_STATE_RXINIT);
1183                         ccw_check_return_code(ch, rc, "initial RX");
1184                 } else
1185                         fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1186                                   DEV_EVENT_RXUP, dev);
1187         } else {
1188                 ctc_pr_debug("%s: Initial RX count %d not %d\n",
1189                              dev->name, buflen, CTC_INITIAL_BLOCKLEN);
1190                 ch_action_firstio(fi, event, arg);
1191         }
1192 }
1193
1194 /**
1195  * Set channel into extended mode.
1196  *
1197  * @param fi    An instance of a channel statemachine.
1198  * @param event The event, just happened.
1199  * @param arg   Generic pointer, casted from channel * upon call.
1200  */
1201 static void
1202 ch_action_setmode(fsm_instance * fi, int event, void *arg)
1203 {
1204         struct channel *ch = (struct channel *) arg;
1205         int rc;
1206         unsigned long saveflags;
1207
1208         fsm_deltimer(&ch->timer);
1209         fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC, CH_EVENT_TIMER, ch);
1210         fsm_newstate(fi, CH_STATE_SETUPWAIT);
1211         if (event == CH_EVENT_TIMER)
1212                 spin_lock_irqsave(get_ccwdev_lock(ch->cdev), saveflags);
1213         rc = ccw_device_start(ch->cdev, &ch->ccw[6], (unsigned long) ch, 0xff, 0);
1214         if (event == CH_EVENT_TIMER)
1215                 spin_unlock_irqrestore(get_ccwdev_lock(ch->cdev), saveflags);
1216         if (rc != 0) {
1217                 fsm_deltimer(&ch->timer);
1218                 fsm_newstate(fi, CH_STATE_STARTWAIT);
1219                 ccw_check_return_code(ch, rc, "set Mode");
1220         } else
1221                 ch->retry = 0;
1222 }
1223
1224 /**
1225  * Setup channel.
1226  *
1227  * @param fi    An instance of a channel statemachine.
1228  * @param event The event, just happened.
1229  * @param arg   Generic pointer, casted from channel * upon call.
1230  */
1231 static void
1232 ch_action_start(fsm_instance * fi, int event, void *arg)
1233 {
1234         struct channel *ch = (struct channel *) arg;
1235         unsigned long saveflags;
1236         int rc;
1237         struct net_device *dev;
1238
1239         if (ch == NULL) {
1240                 ctc_pr_warn("ch_action_start ch=NULL\n");
1241                 return;
1242         }
1243         if (ch->netdev == NULL) {
1244                 ctc_pr_warn("ch_action_start dev=NULL, id=%s\n", ch->id);
1245                 return;
1246         }
1247         dev = ch->netdev;
1248
1249 #ifdef DEBUG
1250         ctc_pr_debug("%s: %s channel start\n", dev->name,
1251                      (CHANNEL_DIRECTION(ch->flags) == READ) ? "RX" : "TX");
1252 #endif
1253
1254         if (ch->trans_skb != NULL) {
1255                 clear_normalized_cda(&ch->ccw[1]);
1256                 dev_kfree_skb(ch->trans_skb);
1257                 ch->trans_skb = NULL;
1258         }
1259         if (CHANNEL_DIRECTION(ch->flags) == READ) {
1260                 ch->ccw[1].cmd_code = CCW_CMD_READ;
1261                 ch->ccw[1].flags = CCW_FLAG_SLI;
1262                 ch->ccw[1].count = 0;
1263         } else {
1264                 ch->ccw[1].cmd_code = CCW_CMD_WRITE;
1265                 ch->ccw[1].flags = CCW_FLAG_SLI | CCW_FLAG_CC;
1266                 ch->ccw[1].count = 0;
1267         }
1268         if (ctc_checkalloc_buffer(ch, 0)) {
1269                 ctc_pr_notice(
1270                         "%s: Could not allocate %s trans_skb, delaying "
1271                         "allocation until first transfer\n",
1272                         dev->name,
1273                         (CHANNEL_DIRECTION(ch->flags) == READ) ? "RX" : "TX");
1274         }
1275
1276         ch->ccw[0].cmd_code = CCW_CMD_PREPARE;
1277         ch->ccw[0].flags = CCW_FLAG_SLI | CCW_FLAG_CC;
1278         ch->ccw[0].count = 0;
1279         ch->ccw[0].cda = 0;
1280         ch->ccw[2].cmd_code = CCW_CMD_NOOP;     /* jointed CE + DE */
1281         ch->ccw[2].flags = CCW_FLAG_SLI;
1282         ch->ccw[2].count = 0;
1283         ch->ccw[2].cda = 0;
1284         memcpy(&ch->ccw[3], &ch->ccw[0], sizeof (struct ccw1) * 3);
1285         ch->ccw[4].cda = 0;
1286         ch->ccw[4].flags &= ~CCW_FLAG_IDA;
1287
1288         fsm_newstate(fi, CH_STATE_STARTWAIT);
1289         fsm_addtimer(&ch->timer, 1000, CH_EVENT_TIMER, ch);
1290         spin_lock_irqsave(get_ccwdev_lock(ch->cdev), saveflags);
1291         rc = ccw_device_halt(ch->cdev, (unsigned long) ch);
1292         spin_unlock_irqrestore(get_ccwdev_lock(ch->cdev), saveflags);
1293         if (rc != 0) {
1294                 if (rc != -EBUSY)
1295                     fsm_deltimer(&ch->timer);
1296                 ccw_check_return_code(ch, rc, "initial HaltIO");
1297         }
1298 #ifdef DEBUG
1299         ctc_pr_debug("ctc: %s(): leaving\n", __func__);
1300 #endif
1301 }
1302
1303 /**
1304  * Shutdown a channel.
1305  *
1306  * @param fi    An instance of a channel statemachine.
1307  * @param event The event, just happened.
1308  * @param arg   Generic pointer, casted from channel * upon call.
1309  */
1310 static void
1311 ch_action_haltio(fsm_instance * fi, int event, void *arg)
1312 {
1313         struct channel *ch = (struct channel *) arg;
1314         unsigned long saveflags;
1315         int rc;
1316         int oldstate;
1317
1318         fsm_deltimer(&ch->timer);
1319         fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC, CH_EVENT_TIMER, ch);
1320         if (event == CH_EVENT_STOP)
1321                 spin_lock_irqsave(get_ccwdev_lock(ch->cdev), saveflags);
1322         oldstate = fsm_getstate(fi);
1323         fsm_newstate(fi, CH_STATE_TERM);
1324         rc = ccw_device_halt(ch->cdev, (unsigned long) ch);
1325         if (event == CH_EVENT_STOP)
1326                 spin_unlock_irqrestore(get_ccwdev_lock(ch->cdev), saveflags);
1327         if (rc != 0) {
1328                 if (rc != -EBUSY) {
1329                     fsm_deltimer(&ch->timer);
1330                     fsm_newstate(fi, oldstate);
1331                 }
1332                 ccw_check_return_code(ch, rc, "HaltIO in ch_action_haltio");
1333         }
1334 }
1335
1336 /**
1337  * A channel has successfully been halted.
1338  * Cleanup it's queue and notify interface statemachine.
1339  *
1340  * @param fi    An instance of a channel statemachine.
1341  * @param event The event, just happened.
1342  * @param arg   Generic pointer, casted from channel * upon call.
1343  */
1344 static void
1345 ch_action_stopped(fsm_instance * fi, int event, void *arg)
1346 {
1347         struct channel *ch = (struct channel *) arg;
1348         struct net_device *dev = ch->netdev;
1349
1350         fsm_deltimer(&ch->timer);
1351         fsm_newstate(fi, CH_STATE_STOPPED);
1352         if (ch->trans_skb != NULL) {
1353                 clear_normalized_cda(&ch->ccw[1]);
1354                 dev_kfree_skb(ch->trans_skb);
1355                 ch->trans_skb = NULL;
1356         }
1357         if (CHANNEL_DIRECTION(ch->flags) == READ) {
1358                 skb_queue_purge(&ch->io_queue);
1359                 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1360                           DEV_EVENT_RXDOWN, dev);
1361         } else {
1362                 ctc_purge_skb_queue(&ch->io_queue);
1363                 spin_lock(&ch->collect_lock);
1364                 ctc_purge_skb_queue(&ch->collect_queue);
1365                 ch->collect_len = 0;
1366                 spin_unlock(&ch->collect_lock);
1367                 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1368                           DEV_EVENT_TXDOWN, dev);
1369         }
1370 }
1371
1372 /**
1373  * A stop command from device statemachine arrived and we are in
1374  * not operational mode. Set state to stopped.
1375  *
1376  * @param fi    An instance of a channel statemachine.
1377  * @param event The event, just happened.
1378  * @param arg   Generic pointer, casted from channel * upon call.
1379  */
1380 static void
1381 ch_action_stop(fsm_instance * fi, int event, void *arg)
1382 {
1383         fsm_newstate(fi, CH_STATE_STOPPED);
1384 }
1385
1386 /**
1387  * A machine check for no path, not operational status or gone device has
1388  * happened.
1389  * Cleanup queue and notify interface statemachine.
1390  *
1391  * @param fi    An instance of a channel statemachine.
1392  * @param event The event, just happened.
1393  * @param arg   Generic pointer, casted from channel * upon call.
1394  */
1395 static void
1396 ch_action_fail(fsm_instance * fi, int event, void *arg)
1397 {
1398         struct channel *ch = (struct channel *) arg;
1399         struct net_device *dev = ch->netdev;
1400
1401         fsm_deltimer(&ch->timer);
1402         fsm_newstate(fi, CH_STATE_NOTOP);
1403         if (CHANNEL_DIRECTION(ch->flags) == READ) {
1404                 skb_queue_purge(&ch->io_queue);
1405                 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1406                           DEV_EVENT_RXDOWN, dev);
1407         } else {
1408                 ctc_purge_skb_queue(&ch->io_queue);
1409                 spin_lock(&ch->collect_lock);
1410                 ctc_purge_skb_queue(&ch->collect_queue);
1411                 ch->collect_len = 0;
1412                 spin_unlock(&ch->collect_lock);
1413                 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1414                           DEV_EVENT_TXDOWN, dev);
1415         }
1416 }
1417
1418 /**
1419  * Handle error during setup of channel.
1420  *
1421  * @param fi    An instance of a channel statemachine.
1422  * @param event The event, just happened.
1423  * @param arg   Generic pointer, casted from channel * upon call.
1424  */
1425 static void
1426 ch_action_setuperr(fsm_instance * fi, int event, void *arg)
1427 {
1428         struct channel *ch = (struct channel *) arg;
1429         struct net_device *dev = ch->netdev;
1430
1431         /**
1432          * Special case: Got UC_RCRESET on setmode.
1433          * This means that remote side isn't setup. In this case
1434          * simply retry after some 10 secs...
1435          */
1436         if ((fsm_getstate(fi) == CH_STATE_SETUPWAIT) &&
1437             ((event == CH_EVENT_UC_RCRESET) ||
1438              (event == CH_EVENT_UC_RSRESET))) {
1439                 fsm_newstate(fi, CH_STATE_STARTRETRY);
1440                 fsm_deltimer(&ch->timer);
1441                 fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC, CH_EVENT_TIMER, ch);
1442                 if (CHANNEL_DIRECTION(ch->flags) == READ) {
1443                         int rc = ccw_device_halt(ch->cdev, (unsigned long) ch);
1444                         if (rc != 0)
1445                                 ccw_check_return_code(
1446                                         ch, rc, "HaltIO in ch_action_setuperr");
1447                 }
1448                 return;
1449         }
1450
1451         ctc_pr_debug("%s: Error %s during %s channel setup state=%s\n",
1452                      dev->name, ch_event_names[event],
1453                      (CHANNEL_DIRECTION(ch->flags) == READ) ? "RX" : "TX",
1454                      fsm_getstate_str(fi));
1455         if (CHANNEL_DIRECTION(ch->flags) == READ) {
1456                 fsm_newstate(fi, CH_STATE_RXERR);
1457                 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1458                           DEV_EVENT_RXDOWN, dev);
1459         } else {
1460                 fsm_newstate(fi, CH_STATE_TXERR);
1461                 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1462                           DEV_EVENT_TXDOWN, dev);
1463         }
1464 }
1465
1466 /**
1467  * Restart a channel after an error.
1468  *
1469  * @param fi    An instance of a channel statemachine.
1470  * @param event The event, just happened.
1471  * @param arg   Generic pointer, casted from channel * upon call.
1472  */
1473 static void
1474 ch_action_restart(fsm_instance * fi, int event, void *arg)
1475 {
1476         unsigned long saveflags;
1477         int oldstate;
1478         int rc;
1479
1480         struct channel *ch = (struct channel *) arg;
1481         struct net_device *dev = ch->netdev;
1482
1483         fsm_deltimer(&ch->timer);
1484         ctc_pr_debug("%s: %s channel restart\n", dev->name,
1485                      (CHANNEL_DIRECTION(ch->flags) == READ) ? "RX" : "TX");
1486         fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC, CH_EVENT_TIMER, ch);
1487         oldstate = fsm_getstate(fi);
1488         fsm_newstate(fi, CH_STATE_STARTWAIT);
1489         if (event == CH_EVENT_TIMER)
1490                 spin_lock_irqsave(get_ccwdev_lock(ch->cdev), saveflags);
1491         rc = ccw_device_halt(ch->cdev, (unsigned long) ch);
1492         if (event == CH_EVENT_TIMER)
1493                 spin_unlock_irqrestore(get_ccwdev_lock(ch->cdev), saveflags);
1494         if (rc != 0) {
1495                 if (rc != -EBUSY) {
1496                     fsm_deltimer(&ch->timer);
1497                     fsm_newstate(fi, oldstate);
1498                 }
1499                 ccw_check_return_code(ch, rc, "HaltIO in ch_action_restart");
1500         }
1501 }
1502
1503 /**
1504  * Handle error during RX initial handshake (exchange of
1505  * 0-length block header)
1506  *
1507  * @param fi    An instance of a channel statemachine.
1508  * @param event The event, just happened.
1509  * @param arg   Generic pointer, casted from channel * upon call.
1510  */
1511 static void
1512 ch_action_rxiniterr(fsm_instance * fi, int event, void *arg)
1513 {
1514         struct channel *ch = (struct channel *) arg;
1515         struct net_device *dev = ch->netdev;
1516
1517         if (event == CH_EVENT_TIMER) {
1518                 fsm_deltimer(&ch->timer);
1519                 ctc_pr_debug("%s: Timeout during RX init handshake\n", dev->name);
1520                 if (ch->retry++ < 3)
1521                         ch_action_restart(fi, event, arg);
1522                 else {
1523                         fsm_newstate(fi, CH_STATE_RXERR);
1524                         fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1525                                   DEV_EVENT_RXDOWN, dev);
1526                 }
1527         } else
1528                 ctc_pr_warn("%s: Error during RX init handshake\n", dev->name);
1529 }
1530
1531 /**
1532  * Notify device statemachine if we gave up initialization
1533  * of RX channel.
1534  *
1535  * @param fi    An instance of a channel statemachine.
1536  * @param event The event, just happened.
1537  * @param arg   Generic pointer, casted from channel * upon call.
1538  */
1539 static void
1540 ch_action_rxinitfail(fsm_instance * fi, int event, void *arg)
1541 {
1542         struct channel *ch = (struct channel *) arg;
1543         struct net_device *dev = ch->netdev;
1544
1545         fsm_newstate(fi, CH_STATE_RXERR);
1546         ctc_pr_warn("%s: RX initialization failed\n", dev->name);
1547         ctc_pr_warn("%s: RX <-> RX connection detected\n", dev->name);
1548         fsm_event(((struct ctc_priv *) dev->priv)->fsm, DEV_EVENT_RXDOWN, dev);
1549 }
1550
1551 /**
1552  * Handle RX Unit check remote reset (remote disconnected)
1553  *
1554  * @param fi    An instance of a channel statemachine.
1555  * @param event The event, just happened.
1556  * @param arg   Generic pointer, casted from channel * upon call.
1557  */
1558 static void
1559 ch_action_rxdisc(fsm_instance * fi, int event, void *arg)
1560 {
1561         struct channel *ch = (struct channel *) arg;
1562         struct channel *ch2;
1563         struct net_device *dev = ch->netdev;
1564
1565         fsm_deltimer(&ch->timer);
1566         ctc_pr_debug("%s: Got remote disconnect, re-initializing ...\n",
1567                      dev->name);
1568
1569         /**
1570          * Notify device statemachine
1571          */
1572         fsm_event(((struct ctc_priv *) dev->priv)->fsm, DEV_EVENT_RXDOWN, dev);
1573         fsm_event(((struct ctc_priv *) dev->priv)->fsm, DEV_EVENT_TXDOWN, dev);
1574
1575         fsm_newstate(fi, CH_STATE_DTERM);
1576         ch2 = ((struct ctc_priv *) dev->priv)->channel[WRITE];
1577         fsm_newstate(ch2->fsm, CH_STATE_DTERM);
1578
1579         ccw_device_halt(ch->cdev, (unsigned long) ch);
1580         ccw_device_halt(ch2->cdev, (unsigned long) ch2);
1581 }
1582
1583 /**
1584  * Handle error during TX channel initialization.
1585  *
1586  * @param fi    An instance of a channel statemachine.
1587  * @param event The event, just happened.
1588  * @param arg   Generic pointer, casted from channel * upon call.
1589  */
1590 static void
1591 ch_action_txiniterr(fsm_instance * fi, int event, void *arg)
1592 {
1593         struct channel *ch = (struct channel *) arg;
1594         struct net_device *dev = ch->netdev;
1595
1596         if (event == CH_EVENT_TIMER) {
1597                 fsm_deltimer(&ch->timer);
1598                 ctc_pr_debug("%s: Timeout during TX init handshake\n", dev->name);
1599                 if (ch->retry++ < 3)
1600                         ch_action_restart(fi, event, arg);
1601                 else {
1602                         fsm_newstate(fi, CH_STATE_TXERR);
1603                         fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1604                                   DEV_EVENT_TXDOWN, dev);
1605                 }
1606         } else
1607                 ctc_pr_warn("%s: Error during TX init handshake\n", dev->name);
1608 }
1609
1610 /**
1611  * Handle TX timeout by retrying operation.
1612  *
1613  * @param fi    An instance of a channel statemachine.
1614  * @param event The event, just happened.
1615  * @param arg   Generic pointer, casted from channel * upon call.
1616  */
1617 static void
1618 ch_action_txretry(fsm_instance * fi, int event, void *arg)
1619 {
1620         struct channel *ch = (struct channel *) arg;
1621         struct net_device *dev = ch->netdev;
1622         unsigned long saveflags;
1623
1624         fsm_deltimer(&ch->timer);
1625         if (ch->retry++ > 3) {
1626                 ctc_pr_debug("%s: TX retry failed, restarting channel\n",
1627                              dev->name);
1628                 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1629                           DEV_EVENT_TXDOWN, dev);
1630                 ch_action_restart(fi, event, arg);
1631         } else {
1632                 struct sk_buff *skb;
1633
1634                 ctc_pr_debug("%s: TX retry %d\n", dev->name, ch->retry);
1635                 if ((skb = skb_peek(&ch->io_queue))) {
1636                         int rc = 0;
1637
1638                         clear_normalized_cda(&ch->ccw[4]);
1639                         ch->ccw[4].count = skb->len;
1640                         if (set_normalized_cda(&ch->ccw[4], skb->data)) {
1641                                 ctc_pr_debug(
1642                                         "%s: IDAL alloc failed, chan restart\n",
1643                                         dev->name);
1644                                 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1645                                           DEV_EVENT_TXDOWN, dev);
1646                                 ch_action_restart(fi, event, arg);
1647                                 return;
1648                         }
1649                         fsm_addtimer(&ch->timer, 1000, CH_EVENT_TIMER, ch);
1650                         if (event == CH_EVENT_TIMER)
1651                                 spin_lock_irqsave(get_ccwdev_lock(ch->cdev),
1652                                                   saveflags);
1653                         rc = ccw_device_start(ch->cdev, &ch->ccw[3],
1654                                               (unsigned long) ch, 0xff, 0);
1655                         if (event == CH_EVENT_TIMER)
1656                                 spin_unlock_irqrestore(get_ccwdev_lock(ch->cdev),
1657                                                        saveflags);
1658                         if (rc != 0) {
1659                                 fsm_deltimer(&ch->timer);
1660                                 ccw_check_return_code(ch, rc, "TX in ch_action_txretry");
1661                                 ctc_purge_skb_queue(&ch->io_queue);
1662                         }
1663                 }
1664         }
1665
1666 }
1667
1668 /**
1669  * Handle fatal errors during an I/O command.
1670  *
1671  * @param fi    An instance of a channel statemachine.
1672  * @param event The event, just happened.
1673  * @param arg   Generic pointer, casted from channel * upon call.
1674  */
1675 static void
1676 ch_action_iofatal(fsm_instance * fi, int event, void *arg)
1677 {
1678         struct channel *ch = (struct channel *) arg;
1679         struct net_device *dev = ch->netdev;
1680
1681         fsm_deltimer(&ch->timer);
1682         if (CHANNEL_DIRECTION(ch->flags) == READ) {
1683                 ctc_pr_debug("%s: RX I/O error\n", dev->name);
1684                 fsm_newstate(fi, CH_STATE_RXERR);
1685                 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1686                           DEV_EVENT_RXDOWN, dev);
1687         } else {
1688                 ctc_pr_debug("%s: TX I/O error\n", dev->name);
1689                 fsm_newstate(fi, CH_STATE_TXERR);
1690                 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1691                           DEV_EVENT_TXDOWN, dev);
1692         }
1693 }
1694
1695 static void 
1696 ch_action_reinit(fsm_instance *fi, int event, void *arg)
1697 {
1698         struct channel *ch = (struct channel *)arg;
1699         struct net_device *dev = ch->netdev;
1700         struct ctc_priv *privptr = dev->priv;
1701  
1702         ch_action_iofatal(fi, event, arg);
1703         fsm_addtimer(&privptr->restart_timer, 1000, DEV_EVENT_RESTART, dev);
1704 }
1705
1706 \f
1707 /**
1708  * The statemachine for a channel.
1709  */
1710 static const fsm_node ch_fsm[] = {
1711         {CH_STATE_STOPPED,    CH_EVENT_STOP,       fsm_action_nop       },
1712         {CH_STATE_STOPPED,    CH_EVENT_START,      ch_action_start      },
1713         {CH_STATE_STOPPED,    CH_EVENT_FINSTAT,    fsm_action_nop       },
1714         {CH_STATE_STOPPED,    CH_EVENT_MC_FAIL,    fsm_action_nop       },
1715
1716         {CH_STATE_NOTOP,      CH_EVENT_STOP,       ch_action_stop       },
1717         {CH_STATE_NOTOP,      CH_EVENT_START,      fsm_action_nop       },
1718         {CH_STATE_NOTOP,      CH_EVENT_FINSTAT,    fsm_action_nop       },
1719         {CH_STATE_NOTOP,      CH_EVENT_MC_FAIL,    fsm_action_nop       },
1720         {CH_STATE_NOTOP,      CH_EVENT_MC_GOOD,    ch_action_start      },
1721
1722         {CH_STATE_STARTWAIT,  CH_EVENT_STOP,       ch_action_haltio     },
1723         {CH_STATE_STARTWAIT,  CH_EVENT_START,      fsm_action_nop       },
1724         {CH_STATE_STARTWAIT,  CH_EVENT_FINSTAT,    ch_action_setmode    },
1725         {CH_STATE_STARTWAIT,  CH_EVENT_TIMER,      ch_action_setuperr   },
1726         {CH_STATE_STARTWAIT,  CH_EVENT_IO_ENODEV,  ch_action_iofatal    },
1727         {CH_STATE_STARTWAIT,  CH_EVENT_IO_EIO,     ch_action_reinit     },
1728         {CH_STATE_STARTWAIT,  CH_EVENT_MC_FAIL,    ch_action_fail       },
1729
1730         {CH_STATE_STARTRETRY, CH_EVENT_STOP,       ch_action_haltio     },
1731         {CH_STATE_STARTRETRY, CH_EVENT_TIMER,      ch_action_setmode    },
1732         {CH_STATE_STARTRETRY, CH_EVENT_FINSTAT,    fsm_action_nop       },
1733         {CH_STATE_STARTRETRY, CH_EVENT_MC_FAIL,    ch_action_fail       },
1734
1735         {CH_STATE_SETUPWAIT,  CH_EVENT_STOP,       ch_action_haltio     },
1736         {CH_STATE_SETUPWAIT,  CH_EVENT_START,      fsm_action_nop       },
1737         {CH_STATE_SETUPWAIT,  CH_EVENT_FINSTAT,    ch_action_firstio    },
1738         {CH_STATE_SETUPWAIT,  CH_EVENT_UC_RCRESET, ch_action_setuperr   },
1739         {CH_STATE_SETUPWAIT,  CH_EVENT_UC_RSRESET, ch_action_setuperr   },
1740         {CH_STATE_SETUPWAIT,  CH_EVENT_TIMER,      ch_action_setmode    },
1741         {CH_STATE_SETUPWAIT,  CH_EVENT_IO_ENODEV,  ch_action_iofatal    },
1742         {CH_STATE_SETUPWAIT,  CH_EVENT_IO_EIO,     ch_action_reinit     },
1743         {CH_STATE_SETUPWAIT,  CH_EVENT_MC_FAIL,    ch_action_fail       },
1744
1745         {CH_STATE_RXINIT,     CH_EVENT_STOP,       ch_action_haltio     },
1746         {CH_STATE_RXINIT,     CH_EVENT_START,      fsm_action_nop       },
1747         {CH_STATE_RXINIT,     CH_EVENT_FINSTAT,    ch_action_rxidle     },
1748         {CH_STATE_RXINIT,     CH_EVENT_UC_RCRESET, ch_action_rxiniterr  },
1749         {CH_STATE_RXINIT,     CH_EVENT_UC_RSRESET, ch_action_rxiniterr  },
1750         {CH_STATE_RXINIT,     CH_EVENT_TIMER,      ch_action_rxiniterr  },
1751         {CH_STATE_RXINIT,     CH_EVENT_ATTNBUSY,   ch_action_rxinitfail },
1752         {CH_STATE_RXINIT,     CH_EVENT_IO_ENODEV,  ch_action_iofatal    },
1753         {CH_STATE_RXINIT,     CH_EVENT_IO_EIO,     ch_action_reinit     },
1754         {CH_STATE_RXINIT,     CH_EVENT_UC_ZERO,    ch_action_firstio    },
1755         {CH_STATE_RXINIT,     CH_EVENT_MC_FAIL,    ch_action_fail       },
1756
1757         {CH_STATE_RXIDLE,     CH_EVENT_STOP,       ch_action_haltio     },
1758         {CH_STATE_RXIDLE,     CH_EVENT_START,      fsm_action_nop       },
1759         {CH_STATE_RXIDLE,     CH_EVENT_FINSTAT,    ch_action_rx         },
1760         {CH_STATE_RXIDLE,     CH_EVENT_UC_RCRESET, ch_action_rxdisc     },
1761 //      {CH_STATE_RXIDLE,     CH_EVENT_UC_RSRESET, ch_action_rxretry    },
1762         {CH_STATE_RXIDLE,     CH_EVENT_IO_ENODEV,  ch_action_iofatal    },
1763         {CH_STATE_RXIDLE,     CH_EVENT_IO_EIO,     ch_action_reinit     },
1764         {CH_STATE_RXIDLE,     CH_EVENT_MC_FAIL,    ch_action_fail       },
1765         {CH_STATE_RXIDLE,     CH_EVENT_UC_ZERO,    ch_action_rx         },
1766
1767         {CH_STATE_TXINIT,     CH_EVENT_STOP,       ch_action_haltio     },
1768         {CH_STATE_TXINIT,     CH_EVENT_START,      fsm_action_nop       },
1769         {CH_STATE_TXINIT,     CH_EVENT_FINSTAT,    ch_action_txidle     },
1770         {CH_STATE_TXINIT,     CH_EVENT_UC_RCRESET, ch_action_txiniterr  },
1771         {CH_STATE_TXINIT,     CH_EVENT_UC_RSRESET, ch_action_txiniterr  },
1772         {CH_STATE_TXINIT,     CH_EVENT_TIMER,      ch_action_txiniterr  },
1773         {CH_STATE_TXINIT,     CH_EVENT_IO_ENODEV,  ch_action_iofatal    },
1774         {CH_STATE_TXINIT,     CH_EVENT_IO_EIO,     ch_action_reinit     },
1775         {CH_STATE_TXINIT,     CH_EVENT_MC_FAIL,    ch_action_fail       },
1776
1777         {CH_STATE_TXIDLE,     CH_EVENT_STOP,       ch_action_haltio     },
1778         {CH_STATE_TXIDLE,     CH_EVENT_START,      fsm_action_nop       },
1779         {CH_STATE_TXIDLE,     CH_EVENT_FINSTAT,    ch_action_firstio    },
1780         {CH_STATE_TXIDLE,     CH_EVENT_UC_RCRESET, fsm_action_nop       },
1781         {CH_STATE_TXIDLE,     CH_EVENT_UC_RSRESET, fsm_action_nop       },
1782         {CH_STATE_TXIDLE,     CH_EVENT_IO_ENODEV,  ch_action_iofatal    },
1783         {CH_STATE_TXIDLE,     CH_EVENT_IO_EIO,     ch_action_reinit     },
1784         {CH_STATE_TXIDLE,     CH_EVENT_MC_FAIL,    ch_action_fail       },
1785
1786         {CH_STATE_TERM,       CH_EVENT_STOP,       fsm_action_nop       },
1787         {CH_STATE_TERM,       CH_EVENT_START,      ch_action_restart    },
1788         {CH_STATE_TERM,       CH_EVENT_FINSTAT,    ch_action_stopped    },
1789         {CH_STATE_TERM,       CH_EVENT_UC_RCRESET, fsm_action_nop       },
1790         {CH_STATE_TERM,       CH_EVENT_UC_RSRESET, fsm_action_nop       },
1791         {CH_STATE_TERM,       CH_EVENT_MC_FAIL,    ch_action_fail       },
1792
1793         {CH_STATE_DTERM,      CH_EVENT_STOP,       ch_action_haltio     },
1794         {CH_STATE_DTERM,      CH_EVENT_START,      ch_action_restart    },
1795         {CH_STATE_DTERM,      CH_EVENT_FINSTAT,    ch_action_setmode    },
1796         {CH_STATE_DTERM,      CH_EVENT_UC_RCRESET, fsm_action_nop       },
1797         {CH_STATE_DTERM,      CH_EVENT_UC_RSRESET, fsm_action_nop       },
1798         {CH_STATE_DTERM,      CH_EVENT_MC_FAIL,    ch_action_fail       },
1799
1800         {CH_STATE_TX,         CH_EVENT_STOP,       ch_action_haltio     },
1801         {CH_STATE_TX,         CH_EVENT_START,      fsm_action_nop       },
1802         {CH_STATE_TX,         CH_EVENT_FINSTAT,    ch_action_txdone     },
1803         {CH_STATE_TX,         CH_EVENT_UC_RCRESET, ch_action_txretry    },
1804         {CH_STATE_TX,         CH_EVENT_UC_RSRESET, ch_action_txretry    },
1805         {CH_STATE_TX,         CH_EVENT_TIMER,      ch_action_txretry    },
1806         {CH_STATE_TX,         CH_EVENT_IO_ENODEV,  ch_action_iofatal    },
1807         {CH_STATE_TX,         CH_EVENT_IO_EIO,     ch_action_reinit     },
1808         {CH_STATE_TX,         CH_EVENT_MC_FAIL,    ch_action_fail       },
1809
1810         {CH_STATE_RXERR,      CH_EVENT_STOP,       ch_action_haltio     },
1811         {CH_STATE_TXERR,      CH_EVENT_STOP,       ch_action_haltio     },
1812         {CH_STATE_TXERR,      CH_EVENT_MC_FAIL,    ch_action_fail       },
1813         {CH_STATE_RXERR,      CH_EVENT_MC_FAIL,    ch_action_fail       },
1814 };
1815
1816 static const int CH_FSM_LEN = sizeof (ch_fsm) / sizeof (fsm_node);
1817 \f
1818 /**
1819  * Functions related to setup and device detection.
1820  *****************************************************************************/
1821
1822 static inline int
1823 less_than(char *id1, char *id2)
1824 {
1825         int dev1, dev2, i;
1826
1827         for (i = 0; i < 5; i++) {
1828                 id1++;
1829                 id2++;
1830         }
1831         dev1 = simple_strtoul(id1, &id1, 16);
1832         dev2 = simple_strtoul(id2, &id2, 16);
1833         
1834         return (dev1 < dev2);
1835 }
1836
1837 /**
1838  * Add a new channel to the list of channels.
1839  * Keeps the channel list sorted.
1840  *
1841  * @param cdev  The ccw_device to be added.
1842  * @param type  The type class of the new channel.
1843  *
1844  * @return 0 on success, !0 on error.
1845  */
1846 static int
1847 add_channel(struct ccw_device *cdev, enum channel_types type)
1848 {
1849         struct channel **c = &channels;
1850         struct channel *ch;
1851
1852         if ((ch =
1853              (struct channel *) kmalloc(sizeof (struct channel),
1854                                         GFP_KERNEL)) == NULL) {
1855                 ctc_pr_warn("ctc: Out of memory in add_channel\n");
1856                 return -1;
1857         }
1858         memset(ch, 0, sizeof (struct channel));
1859         if ((ch->ccw = (struct ccw1 *) kmalloc(sizeof (struct ccw1) * 8,
1860                                                GFP_KERNEL | GFP_DMA)) == NULL) {
1861                 kfree(ch);
1862                 ctc_pr_warn("ctc: Out of memory in add_channel\n");
1863                 return -1;
1864         }
1865
1866         /**
1867          * "static" ccws are used in the following way:
1868          *
1869          * ccw[0..2] (Channel program for generic I/O):
1870          *           0: prepare
1871          *           1: read or write (depending on direction) with fixed
1872          *              buffer (idal allocated once when buffer is allocated)
1873          *           2: nop
1874          * ccw[3..5] (Channel program for direct write of packets)
1875          *           3: prepare
1876          *           4: write (idal allocated on every write).
1877          *           5: nop
1878          * ccw[6..7] (Channel program for initial channel setup):
1879          *           3: set extended mode
1880          *           4: nop
1881          *
1882          * ch->ccw[0..5] are initialized in ch_action_start because
1883          * the channel's direction is yet unknown here.
1884          */
1885         ch->ccw[6].cmd_code = CCW_CMD_SET_EXTENDED;
1886         ch->ccw[6].flags = CCW_FLAG_SLI;
1887         ch->ccw[6].count = 0;
1888         ch->ccw[6].cda = 0;
1889
1890         ch->ccw[7].cmd_code = CCW_CMD_NOOP;
1891         ch->ccw[7].flags = CCW_FLAG_SLI;
1892         ch->ccw[7].count = 0;
1893         ch->ccw[7].cda = 0;
1894
1895         ch->cdev = cdev;
1896         snprintf(ch->id, CTC_ID_SIZE, "ch-%s", cdev->dev.bus_id);
1897         ch->type = type;
1898         loglevel = CTC_LOGLEVEL_DEFAULT;
1899         ch->fsm = init_fsm(ch->id, ch_state_names,
1900                            ch_event_names, NR_CH_STATES, NR_CH_EVENTS,
1901                            ch_fsm, CH_FSM_LEN, GFP_KERNEL);
1902         if (ch->fsm == NULL) {
1903                 ctc_pr_warn("ctc: Could not create FSM in add_channel\n");
1904                 kfree(ch);
1905                 return -1;
1906         }
1907         fsm_newstate(ch->fsm, CH_STATE_IDLE);
1908         if ((ch->irb = (struct irb *) kmalloc(sizeof (struct irb),
1909                                               GFP_KERNEL)) == NULL) {
1910                 ctc_pr_warn("ctc: Out of memory in add_channel\n");
1911                 kfree_fsm(ch->fsm);
1912                 kfree(ch);
1913                 return -1;
1914         }
1915         memset(ch->irb, 0, sizeof (struct irb));
1916         while (*c && less_than((*c)->id, ch->id))
1917                 c = &(*c)->next;
1918         if (!strncmp((*c)->id, ch->id, CTC_ID_SIZE)) {
1919                 ctc_pr_debug(
1920                         "ctc: add_channel: device %s already in list, "
1921                         "using old entry\n", (*c)->id);
1922                 kfree(ch->irb);
1923                 kfree_fsm(ch->fsm);
1924                 kfree(ch);
1925                 return 0;
1926         }
1927         fsm_settimer(ch->fsm, &ch->timer);
1928         skb_queue_head_init(&ch->io_queue);
1929         skb_queue_head_init(&ch->collect_queue);
1930         ch->next = *c;
1931         *c = ch;
1932         return 0;
1933 }
1934
1935 /**
1936  * Release a specific channel in the channel list.
1937  *
1938  * @param ch Pointer to channel struct to be released.
1939  */
1940 static void
1941 channel_free(struct channel *ch)
1942 {
1943         ch->flags &= ~CHANNEL_FLAGS_INUSE;
1944         fsm_newstate(ch->fsm, CH_STATE_IDLE);
1945 }
1946
1947 /**
1948  * Remove a specific channel in the channel list.
1949  *
1950  * @param ch Pointer to channel struct to be released.
1951  */
1952 static void
1953 channel_remove(struct channel *ch)
1954 {
1955         struct channel **c = &channels;
1956
1957         if (ch == NULL)
1958                 return;
1959
1960         channel_free(ch);
1961         while (*c) {
1962                 if (*c == ch) {
1963                         *c = ch->next;
1964                         fsm_deltimer(&ch->timer);
1965                         kfree_fsm(ch->fsm);
1966                         clear_normalized_cda(&ch->ccw[4]);
1967                         if (ch->trans_skb != NULL) {
1968                                 clear_normalized_cda(&ch->ccw[1]);
1969                                 dev_kfree_skb(ch->trans_skb);
1970                         }
1971                         kfree(ch->ccw);
1972                         return;
1973                 }
1974                 c = &((*c)->next);
1975         }
1976 }
1977
1978 /**
1979  * Get a specific channel from the channel list.
1980  *
1981  * @param type Type of channel we are interested in.
1982  * @param id Id of channel we are interested in.
1983  * @param direction Direction we want to use this channel for.
1984  *
1985  * @return Pointer to a channel or NULL if no matching channel available.
1986  */
1987 static struct channel
1988 *
1989 channel_get(enum channel_types type, char *id, int direction)
1990 {
1991         struct channel *ch = channels;
1992
1993 #ifdef DEBUG
1994         ctc_pr_debug("ctc: %s(): searching for ch with id %s and type %d\n",
1995                      __func__, id, type);
1996 #endif
1997
1998         while (ch && ((strncmp(ch->id, id, CTC_ID_SIZE)) || (ch->type != type))) {
1999 #ifdef DEBUG
2000                 ctc_pr_debug("ctc: %s(): ch=0x%p (id=%s, type=%d\n",
2001                              __func__, ch, ch->id, ch->type);
2002 #endif
2003                 ch = ch->next;
2004         }
2005 #ifdef DEBUG
2006         ctc_pr_debug("ctc: %s(): ch=0x%pq (id=%s, type=%d\n",
2007                      __func__, ch, ch->id, ch->type);
2008 #endif
2009         if (!ch) {
2010                 ctc_pr_warn("ctc: %s(): channel with id %s "
2011                             "and type %d not found in channel list\n",
2012                             __func__, id, type);
2013         } else {
2014                 if (ch->flags & CHANNEL_FLAGS_INUSE)
2015                         ch = NULL;
2016                 else {
2017                         ch->flags |= CHANNEL_FLAGS_INUSE;
2018                         ch->flags &= ~CHANNEL_FLAGS_RWMASK;
2019                         ch->flags |= (direction == WRITE)
2020                             ? CHANNEL_FLAGS_WRITE : CHANNEL_FLAGS_READ;
2021                         fsm_newstate(ch->fsm, CH_STATE_STOPPED);
2022                 }
2023         }
2024         return ch;
2025 }
2026
2027 /**
2028  * Return the channel type by name.
2029  *
2030  * @param name Name of network interface.
2031  *
2032  * @return Type class of channel to be used for that interface.
2033  */
2034 static enum channel_types inline
2035 extract_channel_media(char *name)
2036 {
2037         enum channel_types ret = channel_type_unknown;
2038
2039         if (name != NULL) {
2040                 if (strncmp(name, "ctc", 3) == 0)
2041                         ret = channel_type_parallel;
2042                 if (strncmp(name, "escon", 5) == 0)
2043                         ret = channel_type_escon;
2044         }
2045         return ret;
2046 }
2047
2048 static long
2049 __ctc_check_irb_error(struct ccw_device *cdev, struct irb *irb)
2050 {
2051         if (!IS_ERR(irb))
2052                 return 0;
2053
2054         switch (PTR_ERR(irb)) {
2055         case -EIO:
2056                 ctc_pr_warn("i/o-error on device %s\n", cdev->dev.bus_id);
2057 //              CTC_DBF_TEXT(trace, 2, "ckirberr");
2058 //              CTC_DBF_TEXT_(trace, 2, "  rc%d", -EIO);
2059                 break;
2060         case -ETIMEDOUT:
2061                 ctc_pr_warn("timeout on device %s\n", cdev->dev.bus_id);
2062 //              CTC_DBF_TEXT(trace, 2, "ckirberr");
2063 //              CTC_DBF_TEXT_(trace, 2, "  rc%d", -ETIMEDOUT);
2064                 break;
2065         default:
2066                 ctc_pr_warn("unknown error %ld on device %s\n", PTR_ERR(irb),
2067                            cdev->dev.bus_id);
2068 //              CTC_DBF_TEXT(trace, 2, "ckirberr");
2069 //              CTC_DBF_TEXT(trace, 2, "  rc???");
2070         }
2071         return PTR_ERR(irb);
2072 }
2073
2074 /**
2075  * Main IRQ handler.
2076  *
2077  * @param cdev    The ccw_device the interrupt is for.
2078  * @param intparm interruption parameter.
2079  * @param irb     interruption response block.
2080  */
2081 static void
2082 ctc_irq_handler(struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
2083 {
2084         struct channel *ch;
2085         struct net_device *dev;
2086         struct ctc_priv *priv;
2087
2088         if (__ctc_check_irb_error(cdev, irb))
2089                 return;
2090
2091         /* Check for unsolicited interrupts. */
2092         if (!cdev->dev.driver_data) {
2093                 ctc_pr_warn("ctc: Got unsolicited irq: %s c-%02x d-%02x\n",
2094                             cdev->dev.bus_id, irb->scsw.cstat,
2095                             irb->scsw.dstat);
2096                 return;
2097         }
2098         
2099         priv = ((struct ccwgroup_device *)cdev->dev.driver_data)
2100                 ->dev.driver_data;
2101
2102         /* Try to extract channel from driver data. */
2103         if (priv->channel[READ]->cdev == cdev)
2104                 ch = priv->channel[READ];
2105         else if (priv->channel[WRITE]->cdev == cdev)
2106                 ch = priv->channel[WRITE];
2107         else {
2108                 ctc_pr_err("ctc: Can't determine channel for interrupt, "
2109                            "device %s\n", cdev->dev.bus_id);
2110                 return;
2111         }
2112         
2113         dev = (struct net_device *) (ch->netdev);
2114         if (dev == NULL) {
2115                 ctc_pr_crit("ctc: ctc_irq_handler dev=NULL bus_id=%s, ch=0x%p\n",
2116                             cdev->dev.bus_id, ch);
2117                 return;
2118         }
2119
2120 #ifdef DEBUG
2121         ctc_pr_debug("%s: interrupt for device: %s received c-%02x d-%02x\n",
2122                      dev->name, ch->id, irb->scsw.cstat, irb->scsw.dstat);
2123 #endif
2124
2125         /* Copy interruption response block. */
2126         memcpy(ch->irb, irb, sizeof(struct irb));
2127
2128         /* Check for good subchannel return code, otherwise error message */
2129         if (ch->irb->scsw.cstat) {
2130                 fsm_event(ch->fsm, CH_EVENT_SC_UNKNOWN, ch);
2131                 ctc_pr_warn("%s: subchannel check for device: %s - %02x %02x\n",
2132                             dev->name, ch->id, ch->irb->scsw.cstat,
2133                             ch->irb->scsw.dstat);
2134                 return;
2135         }
2136
2137         /* Check the reason-code of a unit check */
2138         if (ch->irb->scsw.dstat & DEV_STAT_UNIT_CHECK) {
2139                 ccw_unit_check(ch, ch->irb->ecw[0]);
2140                 return;
2141         }
2142         if (ch->irb->scsw.dstat & DEV_STAT_BUSY) {
2143                 if (ch->irb->scsw.dstat & DEV_STAT_ATTENTION)
2144                         fsm_event(ch->fsm, CH_EVENT_ATTNBUSY, ch);
2145                 else
2146                         fsm_event(ch->fsm, CH_EVENT_BUSY, ch);
2147                 return;
2148         }
2149         if (ch->irb->scsw.dstat & DEV_STAT_ATTENTION) {
2150                 fsm_event(ch->fsm, CH_EVENT_ATTN, ch);
2151                 return;
2152         }
2153         if ((ch->irb->scsw.stctl & SCSW_STCTL_SEC_STATUS) ||
2154             (ch->irb->scsw.stctl == SCSW_STCTL_STATUS_PEND) ||
2155             (ch->irb->scsw.stctl ==
2156              (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND)))
2157                 fsm_event(ch->fsm, CH_EVENT_FINSTAT, ch);
2158         else
2159                 fsm_event(ch->fsm, CH_EVENT_IRQ, ch);
2160
2161 }
2162 \f
2163 /**
2164  * Actions for interface - statemachine.
2165  *****************************************************************************/
2166
2167 /**
2168  * Startup channels by sending CH_EVENT_START to each channel.
2169  *
2170  * @param fi    An instance of an interface statemachine.
2171  * @param event The event, just happened.
2172  * @param arg   Generic pointer, casted from struct net_device * upon call.
2173  */
2174 static void
2175 dev_action_start(fsm_instance * fi, int event, void *arg)
2176 {
2177         struct net_device *dev = (struct net_device *) arg;
2178         struct ctc_priv *privptr = dev->priv;
2179         int direction;
2180
2181         fsm_deltimer(&privptr->restart_timer);
2182         fsm_newstate(fi, DEV_STATE_STARTWAIT_RXTX);
2183         for (direction = READ; direction <= WRITE; direction++) {
2184                 struct channel *ch = privptr->channel[direction];
2185                 fsm_event(ch->fsm, CH_EVENT_START, ch);
2186         }
2187 }
2188
2189 /**
2190  * Shutdown channels by sending CH_EVENT_STOP to each channel.
2191  *
2192  * @param fi    An instance of an interface statemachine.
2193  * @param event The event, just happened.
2194  * @param arg   Generic pointer, casted from struct net_device * upon call.
2195  */
2196 static void
2197 dev_action_stop(fsm_instance * fi, int event, void *arg)
2198 {
2199         struct net_device *dev = (struct net_device *) arg;
2200         struct ctc_priv *privptr = dev->priv;
2201         int direction;
2202
2203         fsm_newstate(fi, DEV_STATE_STOPWAIT_RXTX);
2204         for (direction = READ; direction <= WRITE; direction++) {
2205                 struct channel *ch = privptr->channel[direction];
2206                 fsm_event(ch->fsm, CH_EVENT_STOP, ch);
2207         }
2208 }
2209 static void 
2210 dev_action_restart(fsm_instance *fi, int event, void *arg)
2211 {
2212         struct net_device *dev = (struct net_device *)arg;
2213         struct ctc_priv *privptr = dev->priv;
2214         
2215         ctc_pr_debug("%s: Restarting\n", dev->name);
2216         dev_action_stop(fi, event, arg);
2217         fsm_event(privptr->fsm, DEV_EVENT_STOP, dev);
2218         fsm_addtimer(&privptr->restart_timer, CTC_TIMEOUT_5SEC,
2219                      DEV_EVENT_START, dev);
2220 }
2221
2222 /**
2223  * Called from channel statemachine
2224  * when a channel is up and running.
2225  *
2226  * @param fi    An instance of an interface statemachine.
2227  * @param event The event, just happened.
2228  * @param arg   Generic pointer, casted from struct net_device * upon call.
2229  */
2230 static void
2231 dev_action_chup(fsm_instance * fi, int event, void *arg)
2232 {
2233         struct net_device *dev = (struct net_device *) arg;
2234         struct ctc_priv *privptr = dev->priv;
2235
2236         switch (fsm_getstate(fi)) {
2237                 case DEV_STATE_STARTWAIT_RXTX:
2238                         if (event == DEV_EVENT_RXUP)
2239                                 fsm_newstate(fi, DEV_STATE_STARTWAIT_TX);
2240                         else
2241                                 fsm_newstate(fi, DEV_STATE_STARTWAIT_RX);
2242                         break;
2243                 case DEV_STATE_STARTWAIT_RX:
2244                         if (event == DEV_EVENT_RXUP) {
2245                                 fsm_newstate(fi, DEV_STATE_RUNNING);
2246                                 ctc_pr_info("%s: connected with remote side\n",
2247                                             dev->name);
2248                                 if (privptr->protocol == CTC_PROTO_LINUX_TTY)
2249                                         ctc_tty_setcarrier(dev, 1);
2250                                 ctc_clear_busy(dev);
2251                         }
2252                         break;
2253                 case DEV_STATE_STARTWAIT_TX:
2254                         if (event == DEV_EVENT_TXUP) {
2255                                 fsm_newstate(fi, DEV_STATE_RUNNING);
2256                                 ctc_pr_info("%s: connected with remote side\n",
2257                                             dev->name);
2258                                 if (privptr->protocol == CTC_PROTO_LINUX_TTY)
2259                                         ctc_tty_setcarrier(dev, 1);
2260                                 ctc_clear_busy(dev);
2261                         }
2262                         break;
2263                 case DEV_STATE_STOPWAIT_TX:
2264                         if (event == DEV_EVENT_RXUP)
2265                                 fsm_newstate(fi, DEV_STATE_STOPWAIT_RXTX);
2266                         break;
2267                 case DEV_STATE_STOPWAIT_RX:
2268                         if (event == DEV_EVENT_TXUP)
2269                                 fsm_newstate(fi, DEV_STATE_STOPWAIT_RXTX);
2270                         break;
2271         }
2272 }
2273
2274 /**
2275  * Called from channel statemachine
2276  * when a channel has been shutdown.
2277  *
2278  * @param fi    An instance of an interface statemachine.
2279  * @param event The event, just happened.
2280  * @param arg   Generic pointer, casted from struct net_device * upon call.
2281  */
2282 static void
2283 dev_action_chdown(fsm_instance * fi, int event, void *arg)
2284 {
2285         struct net_device *dev = (struct net_device *) arg;
2286         struct ctc_priv *privptr = dev->priv;
2287
2288         switch (fsm_getstate(fi)) {
2289                 case DEV_STATE_RUNNING:
2290                         if (privptr->protocol == CTC_PROTO_LINUX_TTY)
2291                                 ctc_tty_setcarrier(dev, 0);
2292                         if (event == DEV_EVENT_TXDOWN)
2293                                 fsm_newstate(fi, DEV_STATE_STARTWAIT_TX);
2294                         else
2295                                 fsm_newstate(fi, DEV_STATE_STARTWAIT_RX);
2296                         break;
2297                 case DEV_STATE_STARTWAIT_RX:
2298                         if (event == DEV_EVENT_TXDOWN)
2299                                 fsm_newstate(fi, DEV_STATE_STARTWAIT_RXTX);
2300                         break;
2301                 case DEV_STATE_STARTWAIT_TX:
2302                         if (event == DEV_EVENT_RXDOWN)
2303                                 fsm_newstate(fi, DEV_STATE_STARTWAIT_RXTX);
2304                         break;
2305                 case DEV_STATE_STOPWAIT_RXTX:
2306                         if (event == DEV_EVENT_TXDOWN)
2307                                 fsm_newstate(fi, DEV_STATE_STOPWAIT_RX);
2308                         else
2309                                 fsm_newstate(fi, DEV_STATE_STOPWAIT_TX);
2310                         break;
2311                 case DEV_STATE_STOPWAIT_RX:
2312                         if (event == DEV_EVENT_RXDOWN)
2313                                 fsm_newstate(fi, DEV_STATE_STOPPED);
2314                         break;
2315                 case DEV_STATE_STOPWAIT_TX:
2316                         if (event == DEV_EVENT_TXDOWN)
2317                                 fsm_newstate(fi, DEV_STATE_STOPPED);
2318                         break;
2319         }
2320 }
2321
2322 static const fsm_node dev_fsm[] = {
2323         {DEV_STATE_STOPPED, DEV_EVENT_START, dev_action_start},
2324
2325         {DEV_STATE_STOPWAIT_RXTX,  DEV_EVENT_START,   dev_action_start   },
2326         {DEV_STATE_STOPWAIT_RXTX,  DEV_EVENT_RXDOWN,  dev_action_chdown  },
2327         {DEV_STATE_STOPWAIT_RXTX,  DEV_EVENT_TXDOWN,  dev_action_chdown  },
2328         {DEV_STATE_STOPWAIT_RXTX,  DEV_EVENT_RESTART, dev_action_restart },
2329
2330         {DEV_STATE_STOPWAIT_RX,    DEV_EVENT_START,   dev_action_start   },
2331         {DEV_STATE_STOPWAIT_RX,    DEV_EVENT_RXUP,    dev_action_chup    },
2332         {DEV_STATE_STOPWAIT_RX,    DEV_EVENT_TXUP,    dev_action_chup    },
2333         {DEV_STATE_STOPWAIT_RX,    DEV_EVENT_RXDOWN,  dev_action_chdown  },
2334         {DEV_STATE_STOPWAIT_RX,    DEV_EVENT_RESTART, dev_action_restart },
2335
2336         {DEV_STATE_STOPWAIT_TX,    DEV_EVENT_START,   dev_action_start   },
2337         {DEV_STATE_STOPWAIT_TX,    DEV_EVENT_RXUP,    dev_action_chup    },
2338         {DEV_STATE_STOPWAIT_TX,    DEV_EVENT_TXUP,    dev_action_chup    },
2339         {DEV_STATE_STOPWAIT_TX,    DEV_EVENT_TXDOWN,  dev_action_chdown  },
2340         {DEV_STATE_STOPWAIT_TX,    DEV_EVENT_RESTART, dev_action_restart },
2341
2342         {DEV_STATE_STARTWAIT_RXTX, DEV_EVENT_STOP,    dev_action_stop    },
2343         {DEV_STATE_STARTWAIT_RXTX, DEV_EVENT_RXUP,    dev_action_chup    },
2344         {DEV_STATE_STARTWAIT_RXTX, DEV_EVENT_TXUP,    dev_action_chup    },
2345         {DEV_STATE_STARTWAIT_RXTX, DEV_EVENT_RXDOWN,  dev_action_chdown  },
2346         {DEV_STATE_STARTWAIT_RXTX, DEV_EVENT_TXDOWN,  dev_action_chdown  },
2347         {DEV_STATE_STARTWAIT_RXTX, DEV_EVENT_RESTART, dev_action_restart },
2348
2349         {DEV_STATE_STARTWAIT_TX,   DEV_EVENT_STOP,    dev_action_stop    },
2350         {DEV_STATE_STARTWAIT_TX,   DEV_EVENT_RXUP,    dev_action_chup    },
2351         {DEV_STATE_STARTWAIT_TX,   DEV_EVENT_TXUP,    dev_action_chup    },
2352         {DEV_STATE_STARTWAIT_TX,   DEV_EVENT_RXDOWN,  dev_action_chdown  },
2353         {DEV_STATE_STARTWAIT_TX,   DEV_EVENT_RESTART, dev_action_restart },
2354
2355         {DEV_STATE_STARTWAIT_RX,   DEV_EVENT_STOP,    dev_action_stop    },
2356         {DEV_STATE_STARTWAIT_RX,   DEV_EVENT_RXUP,    dev_action_chup    },
2357         {DEV_STATE_STARTWAIT_RX,   DEV_EVENT_TXUP,    dev_action_chup    },
2358         {DEV_STATE_STARTWAIT_RX,   DEV_EVENT_TXDOWN,  dev_action_chdown  },
2359         {DEV_STATE_STARTWAIT_RX,   DEV_EVENT_RESTART, dev_action_restart },
2360
2361         {DEV_STATE_RUNNING,        DEV_EVENT_STOP,    dev_action_stop    },
2362         {DEV_STATE_RUNNING,        DEV_EVENT_RXDOWN,  dev_action_chdown  },
2363         {DEV_STATE_RUNNING,        DEV_EVENT_TXDOWN,  dev_action_chdown  },
2364         {DEV_STATE_RUNNING,        DEV_EVENT_TXUP,    fsm_action_nop     },
2365         {DEV_STATE_RUNNING,        DEV_EVENT_RXUP,    fsm_action_nop     },
2366         {DEV_STATE_RUNNING,        DEV_EVENT_RESTART, dev_action_restart },
2367 };
2368
2369 static const int DEV_FSM_LEN = sizeof (dev_fsm) / sizeof (fsm_node);
2370
2371 /**
2372  * Transmit a packet.
2373  * This is a helper function for ctc_tx().
2374  *
2375  * @param ch Channel to be used for sending.
2376  * @param skb Pointer to struct sk_buff of packet to send.
2377  *            The linklevel header has already been set up
2378  *            by ctc_tx().
2379  *
2380  * @return 0 on success, -ERRNO on failure. (Never fails.)
2381  */
2382 static int
2383 transmit_skb(struct channel *ch, struct sk_buff *skb)
2384 {
2385         unsigned long saveflags;
2386         struct ll_header header;
2387         int rc = 0;
2388
2389         if (fsm_getstate(ch->fsm) != CH_STATE_TXIDLE) {
2390                 int l = skb->len + LL_HEADER_LENGTH;
2391
2392                 spin_lock_irqsave(&ch->collect_lock, saveflags);
2393                 if (ch->collect_len + l > ch->max_bufsize - 2)
2394                         rc = -EBUSY;
2395                 else {
2396                         atomic_inc(&skb->users);
2397                         header.length = l;
2398                         header.type = skb->protocol;
2399                         header.unused = 0;
2400                         memcpy(skb_push(skb, LL_HEADER_LENGTH), &header,
2401                                LL_HEADER_LENGTH);
2402                         skb_queue_tail(&ch->collect_queue, skb);
2403                         ch->collect_len += l;
2404                 }
2405                 spin_unlock_irqrestore(&ch->collect_lock, saveflags);
2406         } else {
2407                 __u16 block_len;
2408                 int ccw_idx;
2409                 struct sk_buff *nskb;
2410                 unsigned long hi;
2411
2412                 /**
2413                  * Protect skb against beeing free'd by upper
2414                  * layers.
2415                  */
2416                 atomic_inc(&skb->users);
2417                 ch->prof.txlen += skb->len;
2418                 header.length = skb->len + LL_HEADER_LENGTH;
2419                 header.type = skb->protocol;
2420                 header.unused = 0;
2421                 memcpy(skb_push(skb, LL_HEADER_LENGTH), &header,
2422                        LL_HEADER_LENGTH);
2423                 block_len = skb->len + 2;
2424                 *((__u16 *) skb_push(skb, 2)) = block_len;
2425
2426                 /**
2427                  * IDAL support in CTC is broken, so we have to
2428                  * care about skb's above 2G ourselves.
2429                  */
2430                 hi = ((unsigned long) skb->tail + LL_HEADER_LENGTH) >> 31;
2431                 if (hi) {
2432                         nskb = alloc_skb(skb->len, GFP_ATOMIC | GFP_DMA);
2433                         if (!nskb) {
2434                                 atomic_dec(&skb->users);
2435                                 skb_pull(skb, LL_HEADER_LENGTH + 2);
2436                                 return -ENOMEM;
2437                         } else {
2438                                 memcpy(skb_put(nskb, skb->len),
2439                                        skb->data, skb->len);
2440                                 atomic_inc(&nskb->users);
2441                                 atomic_dec(&skb->users);
2442                                 dev_kfree_skb_irq(skb);
2443                                 skb = nskb;
2444                         }
2445                 }
2446
2447                 ch->ccw[4].count = block_len;
2448                 if (set_normalized_cda(&ch->ccw[4], skb->data)) {
2449                         /**
2450                          * idal allocation failed, try via copying to
2451                          * trans_skb. trans_skb usually has a pre-allocated
2452                          * idal.
2453                          */
2454                         if (ctc_checkalloc_buffer(ch, 1)) {
2455                                 /**
2456                                  * Remove our header. It gets added
2457                                  * again on retransmit.
2458                                  */
2459                                 atomic_dec(&skb->users);
2460                                 skb_pull(skb, LL_HEADER_LENGTH + 2);
2461                                 return -EBUSY;
2462                         }
2463
2464                         ch->trans_skb->tail = ch->trans_skb->data;
2465                         ch->trans_skb->len = 0;
2466                         ch->ccw[1].count = skb->len;
2467                         memcpy(skb_put(ch->trans_skb, skb->len), skb->data,
2468                                skb->len);
2469                         atomic_dec(&skb->users);
2470                         dev_kfree_skb_irq(skb);
2471                         ccw_idx = 0;
2472                 } else {
2473                         skb_queue_tail(&ch->io_queue, skb);
2474                         ccw_idx = 3;
2475                 }
2476                 ch->retry = 0;
2477                 fsm_newstate(ch->fsm, CH_STATE_TX);
2478                 fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC, CH_EVENT_TIMER, ch);
2479                 spin_lock_irqsave(get_ccwdev_lock(ch->cdev), saveflags);
2480                 ch->prof.send_stamp = xtime;
2481                 rc = ccw_device_start(ch->cdev, &ch->ccw[ccw_idx],
2482                                       (unsigned long) ch, 0xff, 0);
2483                 spin_unlock_irqrestore(get_ccwdev_lock(ch->cdev), saveflags);
2484                 if (ccw_idx == 3)
2485                         ch->prof.doios_single++;
2486                 if (rc != 0) {
2487                         fsm_deltimer(&ch->timer);
2488                         ccw_check_return_code(ch, rc, "single skb TX");
2489                         if (ccw_idx == 3)
2490                                 skb_dequeue_tail(&ch->io_queue);
2491                         /**
2492                          * Remove our header. It gets added
2493                          * again on retransmit.
2494                          */
2495                         skb_pull(skb, LL_HEADER_LENGTH + 2);
2496                 } else {
2497                         if (ccw_idx == 0) {
2498                                 struct net_device *dev = ch->netdev;
2499                                 struct ctc_priv *privptr = dev->priv;
2500                                 privptr->stats.tx_packets++;
2501                                 privptr->stats.tx_bytes +=
2502                                     skb->len - LL_HEADER_LENGTH;
2503                         }
2504                 }
2505         }
2506
2507         return rc;
2508 }
2509 \f
2510 /**
2511  * Interface API for upper network layers
2512  *****************************************************************************/
2513
2514 /**
2515  * Open an interface.
2516  * Called from generic network layer when ifconfig up is run.
2517  *
2518  * @param dev Pointer to interface struct.
2519  *
2520  * @return 0 on success, -ERRNO on failure. (Never fails.)
2521  */
2522 static int
2523 ctc_open(struct net_device * dev)
2524 {
2525         fsm_event(((struct ctc_priv *) dev->priv)->fsm, DEV_EVENT_START, dev);
2526         return 0;
2527 }
2528
2529 /**
2530  * Close an interface.
2531  * Called from generic network layer when ifconfig down is run.
2532  *
2533  * @param dev Pointer to interface struct.
2534  *
2535  * @return 0 on success, -ERRNO on failure. (Never fails.)
2536  */
2537 static int
2538 ctc_close(struct net_device * dev)
2539 {
2540         fsm_event(((struct ctc_priv *) dev->priv)->fsm, DEV_EVENT_STOP, dev);
2541         return 0;
2542 }
2543
2544 /**
2545  * Start transmission of a packet.
2546  * Called from generic network device layer.
2547  *
2548  * @param skb Pointer to buffer containing the packet.
2549  * @param dev Pointer to interface struct.
2550  *
2551  * @return 0 if packet consumed, !0 if packet rejected.
2552  *         Note: If we return !0, then the packet is free'd by
2553  *               the generic network layer.
2554  */
2555 static int
2556 ctc_tx(struct sk_buff *skb, struct net_device * dev)
2557 {
2558         int rc = 0;
2559         struct ctc_priv *privptr = (struct ctc_priv *) dev->priv;
2560
2561         /**
2562          * Some sanity checks ...
2563          */
2564         if (skb == NULL) {
2565                 ctc_pr_warn("%s: NULL sk_buff passed\n", dev->name);
2566                 privptr->stats.tx_dropped++;
2567                 return 0;
2568         }
2569         if (skb_headroom(skb) < (LL_HEADER_LENGTH + 2)) {
2570                 ctc_pr_warn("%s: Got sk_buff with head room < %ld bytes\n",
2571                             dev->name, LL_HEADER_LENGTH + 2);
2572                 dev_kfree_skb(skb);
2573                 privptr->stats.tx_dropped++;
2574                 return 0;
2575         }
2576
2577         /**
2578          * If channels are not running, try to restart them
2579          * and throw away packet. 
2580          */
2581         if (fsm_getstate(privptr->fsm) != DEV_STATE_RUNNING) {
2582                 fsm_event(privptr->fsm, DEV_EVENT_START, dev);
2583                 if (privptr->protocol == CTC_PROTO_LINUX_TTY)
2584                         return -EBUSY;
2585                 dev_kfree_skb(skb);
2586                 privptr->stats.tx_dropped++;
2587                 privptr->stats.tx_errors++;
2588                 privptr->stats.tx_carrier_errors++;
2589                 return 0;
2590         }
2591
2592         if (ctc_test_and_set_busy(dev))
2593                 return -EBUSY;
2594
2595         dev->trans_start = jiffies;
2596         if (transmit_skb(privptr->channel[WRITE], skb) != 0)
2597                 rc = 1;
2598         ctc_clear_busy(dev);
2599         return rc;
2600 }
2601
2602 /**
2603  * Sets MTU of an interface.
2604  *
2605  * @param dev     Pointer to interface struct.
2606  * @param new_mtu The new MTU to use for this interface.
2607  *
2608  * @return 0 on success, -EINVAL if MTU is out of valid range.
2609  *         (valid range is 576 .. 65527). If VM is on the
2610  *         remote side, maximum MTU is 32760, however this is
2611  *         <em>not</em> checked here.
2612  */
2613 static int
2614 ctc_change_mtu(struct net_device * dev, int new_mtu)
2615 {
2616         struct ctc_priv *privptr = (struct ctc_priv *) dev->priv;
2617
2618         if ((new_mtu < 576) || (new_mtu > 65527) ||
2619             (new_mtu > (privptr->channel[READ]->max_bufsize -
2620                         LL_HEADER_LENGTH - 2)))
2621                 return -EINVAL;
2622         dev->mtu = new_mtu;
2623         dev->hard_header_len = LL_HEADER_LENGTH + 2;
2624         return 0;
2625 }
2626
2627 /**
2628  * Returns interface statistics of a device.
2629  *
2630  * @param dev Pointer to interface struct.
2631  *
2632  * @return Pointer to stats struct of this interface.
2633  */
2634 static struct net_device_stats *
2635 ctc_stats(struct net_device * dev)
2636 {
2637         return &((struct ctc_priv *) dev->priv)->stats;
2638 }
2639
2640 /*
2641  * sysfs attributes
2642  */
2643 static ssize_t
2644 buffer_show(struct device *dev, char *buf)
2645 {
2646         struct ctc_priv *priv;
2647
2648         priv = dev->driver_data;
2649         if (!priv)
2650                 return -ENODEV;
2651         return sprintf(buf, "%d\n",
2652                        priv->channel[READ]->max_bufsize);
2653 }
2654
2655 static ssize_t
2656 buffer_write(struct device *dev, const char *buf, size_t count)
2657 {
2658         struct ctc_priv *priv;
2659         struct net_device *ndev;
2660         int bs1;
2661
2662         priv = dev->driver_data;
2663         if (!priv)
2664                 return -ENODEV;
2665         ndev = priv->channel[READ]->netdev;
2666         if (!ndev)
2667                 return -ENODEV;
2668         sscanf(buf, "%u", &bs1);
2669
2670         if (bs1 > CTC_BUFSIZE_LIMIT)
2671                 return -EINVAL;
2672         if ((ndev->flags & IFF_RUNNING) &&
2673             (bs1 < (ndev->mtu + LL_HEADER_LENGTH + 2)))
2674                 return -EINVAL;
2675         if (bs1 < (576 + LL_HEADER_LENGTH + 2))
2676                 return -EINVAL;
2677
2678         priv->channel[READ]->max_bufsize =
2679             priv->channel[WRITE]->max_bufsize = bs1;
2680         if (!(ndev->flags & IFF_RUNNING))
2681                 ndev->mtu = bs1 - LL_HEADER_LENGTH - 2;
2682         priv->channel[READ]->flags |= CHANNEL_FLAGS_BUFSIZE_CHANGED;
2683         priv->channel[WRITE]->flags |= CHANNEL_FLAGS_BUFSIZE_CHANGED;
2684
2685         return count;
2686
2687 }
2688
2689 static ssize_t
2690 loglevel_show(struct device *dev, char *buf)
2691 {
2692         struct ctc_priv *priv;
2693
2694         priv = dev->driver_data;
2695         if (!priv)
2696                 return -ENODEV;
2697         return sprintf(buf, "%d\n", loglevel);
2698 }
2699
2700 static ssize_t
2701 loglevel_write(struct device *dev, const char *buf, size_t count)
2702 {
2703         struct ctc_priv *priv;
2704         int ll1;
2705
2706         priv = dev->driver_data;
2707         if (!priv)
2708                 return -ENODEV;
2709         sscanf(buf, "%i", &ll1);
2710
2711         if ((ll1 > CTC_LOGLEVEL_MAX) || (ll1 < 0))
2712                 return -EINVAL;
2713         loglevel = ll1;
2714         return count;
2715 }
2716
2717 static void
2718 ctc_print_statistics(struct ctc_priv *priv)
2719 {
2720         char *sbuf;
2721         char *p;
2722
2723         if (!priv)
2724                 return;
2725         sbuf = (char *)kmalloc(2048, GFP_KERNEL);
2726         if (sbuf == NULL)
2727                 return;
2728         p = sbuf;
2729
2730         p += sprintf(p, "  Device FSM state: %s\n",
2731                      fsm_getstate_str(priv->fsm));
2732         p += sprintf(p, "  RX channel FSM state: %s\n",
2733                      fsm_getstate_str(priv->channel[READ]->fsm));
2734         p += sprintf(p, "  TX channel FSM state: %s\n",
2735                      fsm_getstate_str(priv->channel[WRITE]->fsm));
2736         p += sprintf(p, "  Max. TX buffer used: %ld\n",
2737                      priv->channel[WRITE]->prof.maxmulti);
2738         p += sprintf(p, "  Max. chained SKBs: %ld\n",
2739                      priv->channel[WRITE]->prof.maxcqueue);
2740         p += sprintf(p, "  TX single write ops: %ld\n",
2741                      priv->channel[WRITE]->prof.doios_single);
2742         p += sprintf(p, "  TX multi write ops: %ld\n",
2743                      priv->channel[WRITE]->prof.doios_multi);
2744         p += sprintf(p, "  Netto bytes written: %ld\n",
2745                      priv->channel[WRITE]->prof.txlen);
2746         p += sprintf(p, "  Max. TX IO-time: %ld\n",
2747                      priv->channel[WRITE]->prof.tx_time);
2748
2749         ctc_pr_debug("Statistics for %s:\n%s",
2750                      priv->channel[WRITE]->netdev->name, sbuf);
2751         kfree(sbuf);
2752         return;
2753 }
2754
2755 static ssize_t
2756 stats_show(struct device *dev, char *buf)
2757 {
2758         struct ctc_priv *priv = dev->driver_data;
2759         if (!priv)
2760                 return -ENODEV;
2761         ctc_print_statistics(priv);
2762         return sprintf(buf, "0\n");
2763 }
2764
2765 static ssize_t
2766 stats_write(struct device *dev, const char *buf, size_t count)
2767 {
2768         struct ctc_priv *priv = dev->driver_data;
2769         if (!priv)
2770                 return -ENODEV;
2771         /* Reset statistics */
2772         memset(&priv->channel[WRITE]->prof, 0,
2773                         sizeof(priv->channel[WRITE]->prof));
2774         return count;
2775 }
2776
2777 static DEVICE_ATTR(buffer, 0644, buffer_show, buffer_write);
2778 static DEVICE_ATTR(loglevel, 0644, loglevel_show, loglevel_write);
2779 static DEVICE_ATTR(stats, 0644, stats_show, stats_write);
2780
2781 static int
2782 ctc_add_attributes(struct device *dev)
2783 {
2784         device_create_file(dev, &dev_attr_buffer);
2785         device_create_file(dev, &dev_attr_loglevel);
2786         device_create_file(dev, &dev_attr_stats);
2787         return 0;
2788 }
2789
2790 static void
2791 ctc_remove_attributes(struct device *dev)
2792 {
2793         device_remove_file(dev, &dev_attr_stats);
2794         device_remove_file(dev, &dev_attr_loglevel);
2795         device_remove_file(dev, &dev_attr_buffer);
2796 }
2797
2798 \f
2799 static void
2800 ctc_netdev_unregister(struct net_device * dev)
2801 {
2802         struct ctc_priv *privptr;
2803
2804         if (!dev)
2805                 return;
2806         privptr = (struct ctc_priv *) dev->priv;
2807         if (privptr->protocol != CTC_PROTO_LINUX_TTY)
2808                 unregister_netdev(dev);
2809         else
2810                 ctc_tty_unregister_netdev(dev);
2811 }
2812
2813 static int
2814 ctc_netdev_register(struct net_device * dev)
2815 {
2816         struct ctc_priv *privptr = (struct ctc_priv *) dev->priv;
2817         if (privptr->protocol != CTC_PROTO_LINUX_TTY)
2818                 return register_netdev(dev);
2819         else
2820                 return ctc_tty_register_netdev(dev);
2821 }
2822
2823 static void
2824 ctc_free_netdevice(struct net_device * dev, int free_dev)
2825 {
2826         struct ctc_priv *privptr;
2827         if (!dev)
2828                 return;
2829         privptr = dev->priv;
2830         if (privptr) {
2831                 if (privptr->fsm)
2832                         kfree_fsm(privptr->fsm);
2833                 kfree(privptr);
2834         }
2835 #ifdef MODULE
2836         if (free_dev)
2837                 free_netdev(dev);
2838 #endif
2839 }
2840
2841 /**
2842  * Initialize everything of the net device except the name and the
2843  * channel structs.
2844  */
2845 static struct net_device *
2846 ctc_init_netdevice(struct net_device * dev, int alloc_device, 
2847                    struct ctc_priv *privptr)
2848 {
2849         if (!privptr)
2850                 return NULL;
2851
2852         if (alloc_device) {
2853                 dev = kmalloc(sizeof (struct net_device), GFP_KERNEL);
2854                 if (!dev)
2855                         return NULL;
2856                 memset(dev, 0, sizeof (struct net_device));
2857         }
2858
2859         dev->priv = privptr;
2860         privptr->fsm = init_fsm("ctcdev", dev_state_names,
2861                                 dev_event_names, NR_DEV_STATES, NR_DEV_EVENTS,
2862                                 dev_fsm, DEV_FSM_LEN, GFP_KERNEL);
2863         if (privptr->fsm == NULL) {
2864                 if (alloc_device)
2865                         kfree(dev);
2866                 return NULL;
2867         }
2868         fsm_newstate(privptr->fsm, DEV_STATE_STOPPED);
2869         fsm_settimer(privptr->fsm, &privptr->restart_timer);
2870         dev->mtu = CTC_BUFSIZE_DEFAULT - LL_HEADER_LENGTH - 2;
2871         dev->hard_start_xmit = ctc_tx;
2872         dev->open = ctc_open;
2873         dev->stop = ctc_close;
2874         dev->get_stats = ctc_stats;
2875         dev->change_mtu = ctc_change_mtu;
2876         dev->hard_header_len = LL_HEADER_LENGTH + 2;
2877         dev->addr_len = 0;
2878         dev->type = ARPHRD_SLIP;
2879         dev->tx_queue_len = 100;
2880         dev->flags = IFF_POINTOPOINT | IFF_NOARP;
2881         SET_MODULE_OWNER(dev);
2882         return dev;
2883 }
2884
2885 static ssize_t
2886 ctc_proto_show(struct device *dev, char *buf)
2887 {
2888         struct ctc_priv *priv;
2889
2890         priv = dev->driver_data;
2891         if (!priv)
2892                 return -ENODEV;
2893
2894         return sprintf(buf, "%d\n", priv->protocol);
2895 }
2896
2897 static ssize_t
2898 ctc_proto_store(struct device *dev, const char *buf, size_t count)
2899 {
2900         struct ctc_priv *priv;
2901         int value;
2902
2903         pr_debug("%s() called\n", __FUNCTION__);
2904
2905         priv = dev->driver_data;
2906         if (!priv)
2907                 return -ENODEV;
2908         sscanf(buf, "%u", &value);
2909         if ((value < 0) || (value > CTC_PROTO_MAX))
2910                 return -EINVAL;
2911         priv->protocol = value;
2912
2913         return count;
2914 }
2915
2916 static DEVICE_ATTR(protocol, 0644, ctc_proto_show, ctc_proto_store);
2917
2918 static ssize_t
2919 ctc_type_show(struct device *dev, char *buf)
2920 {
2921         struct ccwgroup_device *cgdev;
2922
2923         cgdev = to_ccwgroupdev(dev);
2924         if (!cgdev)
2925                 return -ENODEV;
2926
2927         return sprintf(buf, "%s\n", cu3088_type[cgdev->cdev[0]->id.driver_info]);
2928 }
2929
2930 static DEVICE_ATTR(type, 0444, ctc_type_show, NULL);
2931
2932 static struct attribute *ctc_attr[] = {
2933         &dev_attr_protocol.attr,
2934         &dev_attr_type.attr,
2935         NULL,
2936 };
2937
2938 static struct attribute_group ctc_attr_group = {
2939         .attrs = ctc_attr,
2940 };
2941
2942 static int
2943 ctc_add_files(struct device *dev)
2944 {
2945         pr_debug("%s() called\n", __FUNCTION__);
2946
2947         return sysfs_create_group(&dev->kobj, &ctc_attr_group);
2948 }
2949
2950 static void
2951 ctc_remove_files(struct device *dev)
2952 {
2953         pr_debug("%s() called\n", __FUNCTION__);
2954
2955         sysfs_remove_group(&dev->kobj, &ctc_attr_group);
2956 }
2957
2958 /**
2959  * Add ctc specific attributes.
2960  * Add ctc private data.
2961  * 
2962  * @param cgdev pointer to ccwgroup_device just added
2963  *
2964  * @returns 0 on success, !0 on failure.
2965  */
2966
2967 static int
2968 ctc_probe_device(struct ccwgroup_device *cgdev)
2969 {
2970         struct ctc_priv *priv;
2971         int rc;
2972
2973         pr_debug("%s() called\n", __FUNCTION__);
2974
2975         if (!get_device(&cgdev->dev))
2976                 return -ENODEV;
2977
2978         priv = kmalloc(sizeof (struct ctc_priv), GFP_KERNEL);
2979         if (!priv) {
2980                 ctc_pr_err("%s: Out of memory\n", __func__);
2981                 put_device(&cgdev->dev);
2982                 return -ENOMEM;
2983         }
2984
2985         memset(priv, 0, sizeof (struct ctc_priv));
2986         rc = ctc_add_files(&cgdev->dev);
2987         if (rc) {
2988                 kfree(priv);
2989                 put_device(&cgdev->dev);
2990                 return rc;
2991         }
2992
2993         cgdev->cdev[0]->handler = ctc_irq_handler;
2994         cgdev->cdev[1]->handler = ctc_irq_handler;
2995         cgdev->dev.driver_data = priv;
2996
2997         return 0;
2998 }
2999
3000 /**
3001  *
3002  * Setup an interface.
3003  *
3004  * @param cgdev  Device to be setup.
3005  *
3006  * @returns 0 on success, !0 on failure.
3007  */
3008 static int
3009 ctc_new_device(struct ccwgroup_device *cgdev)
3010 {
3011         char read_id[CTC_ID_SIZE];
3012         char write_id[CTC_ID_SIZE];
3013         int direction;
3014         enum channel_types type;
3015         struct ctc_priv *privptr;
3016         struct net_device *dev;
3017         int ret;
3018
3019         pr_debug("%s() called\n", __FUNCTION__);
3020
3021         privptr = cgdev->dev.driver_data;
3022         if (!privptr)
3023                 return -ENODEV;
3024
3025         type = get_channel_type(&cgdev->cdev[0]->id);
3026         
3027         snprintf(read_id, CTC_ID_SIZE, "ch-%s", cgdev->cdev[0]->dev.bus_id);
3028         snprintf(write_id, CTC_ID_SIZE, "ch-%s", cgdev->cdev[1]->dev.bus_id);
3029
3030         if (add_channel(cgdev->cdev[0], type))
3031                 return -ENOMEM;
3032         if (add_channel(cgdev->cdev[1], type))
3033                 return -ENOMEM;
3034
3035         ret = ccw_device_set_online(cgdev->cdev[0]);
3036         if (ret != 0) {
3037                         printk(KERN_WARNING
3038                         "ccw_device_set_online (cdev[0]) failed with ret = %d\n", ret);
3039         }
3040
3041         ret = ccw_device_set_online(cgdev->cdev[1]);
3042         if (ret != 0) {
3043                         printk(KERN_WARNING
3044                         "ccw_device_set_online (cdev[1]) failed with ret = %d\n", ret);
3045         }
3046
3047         dev = ctc_init_netdevice(NULL, 1, privptr);
3048
3049         if (!dev) {
3050                 ctc_pr_warn("ctc_init_netdevice failed\n");
3051                 goto out;
3052         }
3053
3054         if (privptr->protocol == CTC_PROTO_LINUX_TTY)
3055                 snprintf(dev->name, 8, "ctctty%%d");
3056         else
3057                 snprintf(dev->name, 8, "ctc%%d");
3058
3059         for (direction = READ; direction <= WRITE; direction++) {
3060                 privptr->channel[direction] =
3061                     channel_get(type, direction == READ ? read_id : write_id,
3062                                 direction);
3063                 if (privptr->channel[direction] == NULL) {
3064                         if (direction == WRITE) 
3065                                 channel_free(privptr->channel[READ]);
3066
3067                         ctc_free_netdevice(dev, 1);
3068                         goto out;
3069                 }
3070                 privptr->channel[direction]->netdev = dev;
3071                 privptr->channel[direction]->protocol = privptr->protocol;
3072                 privptr->channel[direction]->max_bufsize = CTC_BUFSIZE_DEFAULT;
3073         }
3074         /* sysfs magic */
3075         SET_NETDEV_DEV(dev, &cgdev->dev);
3076
3077         if (ctc_netdev_register(dev) != 0) {
3078                 ctc_free_netdevice(dev, 1);
3079                 goto out;
3080         }
3081
3082         ctc_add_attributes(&cgdev->dev);
3083
3084         strlcpy(privptr->fsm->name, dev->name, sizeof (privptr->fsm->name));
3085
3086         print_banner();
3087
3088         ctc_pr_info("%s: read: %s, write: %s, proto: %d\n",
3089                     dev->name, privptr->channel[READ]->id,
3090                     privptr->channel[WRITE]->id, privptr->protocol);
3091
3092         return 0;
3093 out:
3094         ccw_device_set_offline(cgdev->cdev[1]);
3095         ccw_device_set_offline(cgdev->cdev[0]);
3096
3097         return -ENODEV;
3098 }
3099
3100 /**
3101  * Shutdown an interface.
3102  *
3103  * @param cgdev  Device to be shut down.
3104  *
3105  * @returns 0 on success, !0 on failure.
3106  */
3107 static int
3108 ctc_shutdown_device(struct ccwgroup_device *cgdev)
3109 {
3110         struct ctc_priv *priv;
3111         struct net_device *ndev;
3112                 
3113
3114         pr_debug("%s() called\n", __FUNCTION__);
3115
3116         priv = cgdev->dev.driver_data;
3117         ndev = NULL;
3118         if (!priv)
3119                 return -ENODEV;
3120
3121         if (priv->channel[READ]) {
3122                 ndev = priv->channel[READ]->netdev;
3123
3124                 /* Close the device */
3125                 ctc_close(ndev);
3126                 ndev->flags &=~IFF_RUNNING;
3127
3128                 ctc_remove_attributes(&cgdev->dev);
3129
3130                 channel_free(priv->channel[READ]);
3131         }
3132         if (priv->channel[WRITE])
3133                 channel_free(priv->channel[WRITE]);
3134
3135         if (ndev) {
3136                 ctc_netdev_unregister(ndev);
3137                 ndev->priv = NULL;
3138                 ctc_free_netdevice(ndev, 1);
3139         }
3140
3141         if (priv->fsm)
3142                 kfree_fsm(priv->fsm);
3143
3144         ccw_device_set_offline(cgdev->cdev[1]);
3145         ccw_device_set_offline(cgdev->cdev[0]);
3146
3147         if (priv->channel[READ])
3148                 channel_remove(priv->channel[READ]);
3149         if (priv->channel[WRITE])
3150                 channel_remove(priv->channel[WRITE]);
3151         
3152         priv->channel[READ] = priv->channel[WRITE] = NULL;
3153
3154         return 0;
3155
3156 }
3157
3158 static void
3159 ctc_remove_device(struct ccwgroup_device *cgdev)
3160 {
3161         struct ctc_priv *priv;
3162
3163         pr_debug("%s() called\n", __FUNCTION__);
3164
3165         priv = cgdev->dev.driver_data;
3166         if (!priv)
3167                 return;
3168         if (cgdev->state == CCWGROUP_ONLINE)
3169                 ctc_shutdown_device(cgdev);
3170         ctc_remove_files(&cgdev->dev);
3171         cgdev->dev.driver_data = NULL;
3172         kfree(priv);
3173         put_device(&cgdev->dev);
3174 }
3175
3176 static struct ccwgroup_driver ctc_group_driver = {
3177         .owner       = THIS_MODULE,
3178         .name        = "ctc",
3179         .max_slaves  = 2,
3180         .driver_id   = 0xC3E3C3,
3181         .probe       = ctc_probe_device,
3182         .remove      = ctc_remove_device,
3183         .set_online  = ctc_new_device,
3184         .set_offline = ctc_shutdown_device,
3185 };
3186
3187 /**
3188  * Module related routines
3189  *****************************************************************************/
3190
3191 /**
3192  * Prepare to be unloaded. Free IRQ's and release all resources.
3193  * This is called just before this module is unloaded. It is
3194  * <em>not</em> called, if the usage count is !0, so we don't need to check
3195  * for that.
3196  */
3197 static void __exit
3198 ctc_exit(void)
3199 {
3200         unregister_cu3088_discipline(&ctc_group_driver);
3201         ctc_tty_cleanup();
3202         ctc_pr_info("CTC driver unloaded\n");
3203 }
3204
3205 /**
3206  * Initialize module.
3207  * This is called just after the module is loaded.
3208  *
3209  * @return 0 on success, !0 on error.
3210  */
3211 static int __init
3212 ctc_init(void)
3213 {
3214         int ret = 0;
3215
3216         print_banner();
3217
3218         ctc_tty_init();
3219         ret = register_cu3088_discipline(&ctc_group_driver);
3220         if (ret) 
3221                 ctc_tty_cleanup();
3222         return ret;
3223 }
3224
3225 module_init(ctc_init);
3226 module_exit(ctc_exit);
3227
3228 /* --- This is the END my friend --- */