OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [s390/] [net/] [ctcmain.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.