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