initial commit with v2.6.9
[linux-2.6.9-moxart.git] / drivers / s390 / net / ctcmain.c
blob6a1cb3f86be9e49561fb7ef879bb12b94402c2bb
1 /*
2 * $Id: ctcmain.c,v 1.63 2004/07/28 12:27:54 ptiedem Exp $
4 * CTC / ESCON network driver
6 * Copyright (C) 2001 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Author(s): Fritz Elfert (elfert@de.ibm.com, felfert@millenux.com)
8 * Fixes by : Jochen Röhrig (roehrig@de.ibm.com)
9 * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
10 * Driver Model stuff by : Cornelia Huck <cohuck@de.ibm.com>
12 * Documentation used:
13 * - Principles of Operation (IBM doc#: SA22-7201-06)
14 * - Common IO/-Device Commands and Self Description (IBM doc#: SA22-7204-02)
15 * - Common IO/-Device Commands and Self Description (IBM doc#: SN22-5535)
16 * - ESCON Channel-to-Channel Adapter (IBM doc#: SA22-7203-00)
17 * - ESCON I/O Interface (IBM doc#: SA22-7202-029
19 * and the source of the original CTC driver by:
20 * Dieter Wellerdiek (wel@de.ibm.com)
21 * Martin Schwidefsky (schwidefsky@de.ibm.com)
22 * Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
23 * Jochen Röhrig (roehrig@de.ibm.com)
25 * This program is free software; you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation; either version 2, or (at your option)
28 * any later version.
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public License
36 * along with this program; if not, write to the Free Software
37 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
39 * RELEASE-TAG: CTC/ESCON network driver $Revision: 1.63 $
43 #undef DEBUG
45 #include <linux/module.h>
46 #include <linux/init.h>
47 #include <linux/kernel.h>
48 #include <linux/slab.h>
49 #include <linux/errno.h>
50 #include <linux/types.h>
51 #include <linux/interrupt.h>
52 #include <linux/timer.h>
53 #include <linux/sched.h>
55 #include <linux/signal.h>
56 #include <linux/string.h>
58 #include <linux/ip.h>
59 #include <linux/if_arp.h>
60 #include <linux/tcp.h>
61 #include <linux/skbuff.h>
62 #include <linux/ctype.h>
63 #include <net/dst.h>
65 #include <asm/io.h>
66 #include <asm/ccwdev.h>
67 #include <asm/ccwgroup.h>
68 #include <asm/bitops.h>
69 #include <asm/uaccess.h>
71 #include <asm/idals.h>
73 #include "ctctty.h"
74 #include "fsm.h"
75 #include "cu3088.h"
76 #include "ctcdbug.h"
78 MODULE_AUTHOR("(C) 2000 IBM Corp. by Fritz Elfert (felfert@millenux.com)");
79 MODULE_DESCRIPTION("Linux for S/390 CTC/Escon Driver");
80 MODULE_LICENSE("GPL");
82 /**
83 * CCW commands, used in this driver.
85 #define CCW_CMD_WRITE 0x01
86 #define CCW_CMD_READ 0x02
87 #define CCW_CMD_SET_EXTENDED 0xc3
88 #define CCW_CMD_PREPARE 0xe3
90 #define CTC_PROTO_S390 0
91 #define CTC_PROTO_LINUX 1
92 #define CTC_PROTO_LINUX_TTY 2
93 #define CTC_PROTO_OS390 3
94 #define CTC_PROTO_MAX 3
96 #define CTC_BUFSIZE_LIMIT 65535
97 #define CTC_BUFSIZE_DEFAULT 32768
99 #define CTC_TIMEOUT_5SEC 5000
101 #define CTC_INITIAL_BLOCKLEN 2
103 #define READ 0
104 #define WRITE 1
106 #define CTC_ID_SIZE BUS_ID_SIZE+3
109 struct ctc_profile {
110 unsigned long maxmulti;
111 unsigned long maxcqueue;
112 unsigned long doios_single;
113 unsigned long doios_multi;
114 unsigned long txlen;
115 unsigned long tx_time;
116 struct timespec send_stamp;
120 * Definition of one channel
122 struct channel {
125 * Pointer to next channel in list.
127 struct channel *next;
128 char id[CTC_ID_SIZE];
129 struct ccw_device *cdev;
132 * Type of this channel.
133 * CTC/A or Escon for valid channels.
135 enum channel_types type;
138 * Misc. flags. See CHANNEL_FLAGS_... below
140 __u32 flags;
143 * The protocol of this channel
145 __u16 protocol;
148 * I/O and irq related stuff
150 struct ccw1 *ccw;
151 struct irb *irb;
154 * RX/TX buffer size
156 int max_bufsize;
159 * Transmit/Receive buffer.
161 struct sk_buff *trans_skb;
164 * Universal I/O queue.
166 struct sk_buff_head io_queue;
169 * TX queue for collecting skb's during busy.
171 struct sk_buff_head collect_queue;
174 * Amount of data in collect_queue.
176 int collect_len;
179 * spinlock for collect_queue and collect_len
181 spinlock_t collect_lock;
184 * Timer for detecting unresposive
185 * I/O operations.
187 fsm_timer timer;
190 * Retry counter for misc. operations.
192 int retry;
195 * The finite state machine of this channel
197 fsm_instance *fsm;
200 * The corresponding net_device this channel
201 * belongs to.
203 struct net_device *netdev;
205 struct ctc_profile prof;
207 unsigned char *trans_skb_data;
209 __u16 logflags;
212 #define CHANNEL_FLAGS_READ 0
213 #define CHANNEL_FLAGS_WRITE 1
214 #define CHANNEL_FLAGS_INUSE 2
215 #define CHANNEL_FLAGS_BUFSIZE_CHANGED 4
216 #define CHANNEL_FLAGS_FAILED 8
217 #define CHANNEL_FLAGS_WAITIRQ 16
218 #define CHANNEL_FLAGS_RWMASK 1
219 #define CHANNEL_DIRECTION(f) (f & CHANNEL_FLAGS_RWMASK)
221 #define LOG_FLAG_ILLEGALPKT 1
222 #define LOG_FLAG_ILLEGALSIZE 2
223 #define LOG_FLAG_OVERRUN 4
224 #define LOG_FLAG_NOMEM 8
226 #define CTC_LOGLEVEL_INFO 1
227 #define CTC_LOGLEVEL_NOTICE 2
228 #define CTC_LOGLEVEL_WARN 4
229 #define CTC_LOGLEVEL_EMERG 8
230 #define CTC_LOGLEVEL_ERR 16
231 #define CTC_LOGLEVEL_DEBUG 32
232 #define CTC_LOGLEVEL_CRIT 64
234 #define CTC_LOGLEVEL_DEFAULT \
235 (CTC_LOGLEVEL_INFO | CTC_LOGLEVEL_NOTICE | CTC_LOGLEVEL_WARN | CTC_LOGLEVEL_CRIT)
237 #define CTC_LOGLEVEL_MAX ((CTC_LOGLEVEL_CRIT<<1)-1)
239 static int loglevel = CTC_LOGLEVEL_DEFAULT;
241 #define ctc_pr_debug(fmt, arg...) \
242 do { if (loglevel & CTC_LOGLEVEL_DEBUG) printk(KERN_DEBUG fmt,##arg); } while (0)
244 #define ctc_pr_info(fmt, arg...) \
245 do { if (loglevel & CTC_LOGLEVEL_INFO) printk(KERN_INFO fmt,##arg); } while (0)
247 #define ctc_pr_notice(fmt, arg...) \
248 do { if (loglevel & CTC_LOGLEVEL_NOTICE) printk(KERN_NOTICE fmt,##arg); } while (0)
250 #define ctc_pr_warn(fmt, arg...) \
251 do { if (loglevel & CTC_LOGLEVEL_WARN) printk(KERN_WARNING fmt,##arg); } while (0)
253 #define ctc_pr_emerg(fmt, arg...) \
254 do { if (loglevel & CTC_LOGLEVEL_EMERG) printk(KERN_EMERG fmt,##arg); } while (0)
256 #define ctc_pr_err(fmt, arg...) \
257 do { if (loglevel & CTC_LOGLEVEL_ERR) printk(KERN_ERR fmt,##arg); } while (0)
259 #define ctc_pr_crit(fmt, arg...) \
260 do { if (loglevel & CTC_LOGLEVEL_CRIT) printk(KERN_CRIT fmt,##arg); } while (0)
263 * Linked list of all detected channels.
265 static struct channel *channels = NULL;
267 struct ctc_priv {
268 struct net_device_stats stats;
269 unsigned long tbusy;
271 * The finite state machine of this interface.
273 fsm_instance *fsm;
275 * The protocol of this device
277 __u16 protocol;
279 * Timer for restarting after I/O Errors
281 fsm_timer restart_timer;
283 struct channel *channel[2];
287 * Definition of our link level header.
289 struct ll_header {
290 __u16 length;
291 __u16 type;
292 __u16 unused;
294 #define LL_HEADER_LENGTH (sizeof(struct ll_header))
297 * Compatibility macros for busy handling
298 * of network devices.
300 static __inline__ void
301 ctc_clear_busy(struct net_device * dev)
303 clear_bit(0, &(((struct ctc_priv *) dev->priv)->tbusy));
304 if (((struct ctc_priv *)dev->priv)->protocol != CTC_PROTO_LINUX_TTY)
305 netif_wake_queue(dev);
308 static __inline__ int
309 ctc_test_and_set_busy(struct net_device * dev)
311 if (((struct ctc_priv *)dev->priv)->protocol != CTC_PROTO_LINUX_TTY)
312 netif_stop_queue(dev);
313 return test_and_set_bit(0, &((struct ctc_priv *) dev->priv)->tbusy);
317 * Print Banner.
319 static void
320 print_banner(void)
322 static int printed = 0;
323 char vbuf[] = "$Revision: 1.63 $";
324 char *version = vbuf;
326 if (printed)
327 return;
328 if ((version = strchr(version, ':'))) {
329 char *p = strchr(version + 1, '$');
330 if (p)
331 *p = '\0';
332 } else
333 version = " ??? ";
334 printk(KERN_INFO "CTC driver Version%s"
335 #ifdef DEBUG
336 " (DEBUG-VERSION, " __DATE__ __TIME__ ")"
337 #endif
338 " initialized\n", version);
339 printed = 1;
343 * Return type of a detected device.
345 static enum channel_types
346 get_channel_type(struct ccw_device_id *id)
348 enum channel_types type = (enum channel_types) id->driver_info;
350 if (type == channel_type_ficon)
351 type = channel_type_escon;
353 return type;
357 * States of the interface statemachine.
359 enum dev_states {
360 DEV_STATE_STOPPED,
361 DEV_STATE_STARTWAIT_RXTX,
362 DEV_STATE_STARTWAIT_RX,
363 DEV_STATE_STARTWAIT_TX,
364 DEV_STATE_STOPWAIT_RXTX,
365 DEV_STATE_STOPWAIT_RX,
366 DEV_STATE_STOPWAIT_TX,
367 DEV_STATE_RUNNING,
369 * MUST be always the last element!!
371 NR_DEV_STATES
374 static const char *dev_state_names[] = {
375 "Stopped",
376 "StartWait RXTX",
377 "StartWait RX",
378 "StartWait TX",
379 "StopWait RXTX",
380 "StopWait RX",
381 "StopWait TX",
382 "Running",
386 * Events of the interface statemachine.
388 enum dev_events {
389 DEV_EVENT_START,
390 DEV_EVENT_STOP,
391 DEV_EVENT_RXUP,
392 DEV_EVENT_TXUP,
393 DEV_EVENT_RXDOWN,
394 DEV_EVENT_TXDOWN,
395 DEV_EVENT_RESTART,
397 * MUST be always the last element!!
399 NR_DEV_EVENTS
402 static const char *dev_event_names[] = {
403 "Start",
404 "Stop",
405 "RX up",
406 "TX up",
407 "RX down",
408 "TX down",
409 "Restart",
413 * Events of the channel statemachine
415 enum ch_events {
417 * Events, representing return code of
418 * I/O operations (ccw_device_start, ccw_device_halt et al.)
420 CH_EVENT_IO_SUCCESS,
421 CH_EVENT_IO_EBUSY,
422 CH_EVENT_IO_ENODEV,
423 CH_EVENT_IO_EIO,
424 CH_EVENT_IO_UNKNOWN,
426 CH_EVENT_ATTNBUSY,
427 CH_EVENT_ATTN,
428 CH_EVENT_BUSY,
431 * Events, representing unit-check
433 CH_EVENT_UC_RCRESET,
434 CH_EVENT_UC_RSRESET,
435 CH_EVENT_UC_TXTIMEOUT,
436 CH_EVENT_UC_TXPARITY,
437 CH_EVENT_UC_HWFAIL,
438 CH_EVENT_UC_RXPARITY,
439 CH_EVENT_UC_ZERO,
440 CH_EVENT_UC_UNKNOWN,
443 * Events, representing subchannel-check
445 CH_EVENT_SC_UNKNOWN,
448 * Events, representing machine checks
450 CH_EVENT_MC_FAIL,
451 CH_EVENT_MC_GOOD,
454 * Event, representing normal IRQ
456 CH_EVENT_IRQ,
457 CH_EVENT_FINSTAT,
460 * Event, representing timer expiry.
462 CH_EVENT_TIMER,
465 * Events, representing commands from upper levels.
467 CH_EVENT_START,
468 CH_EVENT_STOP,
471 * MUST be always the last element!!
473 NR_CH_EVENTS,
476 static const char *ch_event_names[] = {
477 "ccw_device success",
478 "ccw_device busy",
479 "ccw_device enodev",
480 "ccw_device ioerr",
481 "ccw_device unknown",
483 "Status ATTN & BUSY",
484 "Status ATTN",
485 "Status BUSY",
487 "Unit check remote reset",
488 "Unit check remote system reset",
489 "Unit check TX timeout",
490 "Unit check TX parity",
491 "Unit check Hardware failure",
492 "Unit check RX parity",
493 "Unit check ZERO",
494 "Unit check Unknown",
496 "SubChannel check Unknown",
498 "Machine check failure",
499 "Machine check operational",
501 "IRQ normal",
502 "IRQ final",
504 "Timer",
506 "Start",
507 "Stop",
511 * States of the channel statemachine.
513 enum ch_states {
515 * Channel not assigned to any device,
516 * initial state, direction invalid
518 CH_STATE_IDLE,
521 * Channel assigned but not operating
523 CH_STATE_STOPPED,
524 CH_STATE_STARTWAIT,
525 CH_STATE_STARTRETRY,
526 CH_STATE_SETUPWAIT,
527 CH_STATE_RXINIT,
528 CH_STATE_TXINIT,
529 CH_STATE_RX,
530 CH_STATE_TX,
531 CH_STATE_RXIDLE,
532 CH_STATE_TXIDLE,
533 CH_STATE_RXERR,
534 CH_STATE_TXERR,
535 CH_STATE_TERM,
536 CH_STATE_DTERM,
537 CH_STATE_NOTOP,
540 * MUST be always the last element!!
542 NR_CH_STATES,
545 static const char *ch_state_names[] = {
546 "Idle",
547 "Stopped",
548 "StartWait",
549 "StartRetry",
550 "SetupWait",
551 "RX init",
552 "TX init",
553 "RX",
554 "TX",
555 "RX idle",
556 "TX idle",
557 "RX error",
558 "TX error",
559 "Terminating",
560 "Restarting",
561 "Not operational",
564 #ifdef DEBUG
566 * Dump header and first 16 bytes of an sk_buff for debugging purposes.
568 * @param skb The sk_buff to dump.
569 * @param offset Offset relative to skb-data, where to start the dump.
571 static void
572 ctc_dump_skb(struct sk_buff *skb, int offset)
574 unsigned char *p = skb->data;
575 __u16 bl;
576 struct ll_header *header;
577 int i;
579 if (!(loglevel & CTC_LOGLEVEL_DEBUG))
580 return;
581 p += offset;
582 bl = *((__u16 *) p);
583 p += 2;
584 header = (struct ll_header *) p;
585 p -= 2;
587 printk(KERN_DEBUG "dump:\n");
588 printk(KERN_DEBUG "blocklen=%d %04x\n", bl, bl);
590 printk(KERN_DEBUG "h->length=%d %04x\n", header->length,
591 header->length);
592 printk(KERN_DEBUG "h->type=%04x\n", header->type);
593 printk(KERN_DEBUG "h->unused=%04x\n", header->unused);
594 if (bl > 16)
595 bl = 16;
596 printk(KERN_DEBUG "data: ");
597 for (i = 0; i < bl; i++)
598 printk("%02x%s", *p++, (i % 16) ? " " : "\n<7>");
599 printk("\n");
601 #else
602 static inline void
603 ctc_dump_skb(struct sk_buff *skb, int offset)
606 #endif
609 * Unpack a just received skb and hand it over to
610 * upper layers.
612 * @param ch The channel where this skb has been received.
613 * @param pskb The received skb.
615 static __inline__ void
616 ctc_unpack_skb(struct channel *ch, struct sk_buff *pskb)
618 struct net_device *dev = ch->netdev;
619 struct ctc_priv *privptr = (struct ctc_priv *) dev->priv;
620 __u16 len = *((__u16 *) pskb->data);
622 DBF_TEXT(trace, 4, __FUNCTION__);
623 skb_put(pskb, 2 + LL_HEADER_LENGTH);
624 skb_pull(pskb, 2);
625 pskb->dev = dev;
626 pskb->ip_summed = CHECKSUM_UNNECESSARY;
627 while (len > 0) {
628 struct sk_buff *skb;
629 struct ll_header *header = (struct ll_header *) pskb->data;
631 skb_pull(pskb, LL_HEADER_LENGTH);
632 if ((ch->protocol == CTC_PROTO_S390) &&
633 (header->type != ETH_P_IP)) {
635 #ifndef DEBUG
636 if (!(ch->logflags & LOG_FLAG_ILLEGALPKT)) {
637 #endif
639 * Check packet type only if we stick strictly
640 * to S/390's protocol of OS390. This only
641 * supports IP. Otherwise allow any packet
642 * type.
644 ctc_pr_warn(
645 "%s Illegal packet type 0x%04x received, dropping\n",
646 dev->name, header->type);
647 ch->logflags |= LOG_FLAG_ILLEGALPKT;
648 #ifndef DEBUG
650 #endif
651 #ifdef DEBUG
652 ctc_dump_skb(pskb, -6);
653 #endif
654 privptr->stats.rx_dropped++;
655 privptr->stats.rx_frame_errors++;
656 return;
658 pskb->protocol = ntohs(header->type);
659 if (header->length <= LL_HEADER_LENGTH) {
660 #ifndef DEBUG
661 if (!(ch->logflags & LOG_FLAG_ILLEGALSIZE)) {
662 #endif
663 ctc_pr_warn(
664 "%s Illegal packet size %d "
665 "received (MTU=%d blocklen=%d), "
666 "dropping\n", dev->name, header->length,
667 dev->mtu, len);
668 ch->logflags |= LOG_FLAG_ILLEGALSIZE;
669 #ifndef DEBUG
671 #endif
672 #ifdef DEBUG
673 ctc_dump_skb(pskb, -6);
674 #endif
675 privptr->stats.rx_dropped++;
676 privptr->stats.rx_length_errors++;
677 return;
679 header->length -= LL_HEADER_LENGTH;
680 len -= LL_HEADER_LENGTH;
681 if ((header->length > skb_tailroom(pskb)) ||
682 (header->length > len)) {
683 #ifndef DEBUG
684 if (!(ch->logflags & LOG_FLAG_OVERRUN)) {
685 #endif
686 ctc_pr_warn(
687 "%s Illegal packet size %d "
688 "(beyond the end of received data), "
689 "dropping\n", dev->name, header->length);
690 ch->logflags |= LOG_FLAG_OVERRUN;
691 #ifndef DEBUG
693 #endif
694 #ifdef DEBUG
695 ctc_dump_skb(pskb, -6);
696 #endif
697 privptr->stats.rx_dropped++;
698 privptr->stats.rx_length_errors++;
699 return;
701 skb_put(pskb, header->length);
702 pskb->mac.raw = pskb->data;
703 len -= header->length;
704 skb = dev_alloc_skb(pskb->len);
705 if (!skb) {
706 #ifndef DEBUG
707 if (!(ch->logflags & LOG_FLAG_NOMEM)) {
708 #endif
709 ctc_pr_warn(
710 "%s Out of memory in ctc_unpack_skb\n",
711 dev->name);
712 ch->logflags |= LOG_FLAG_NOMEM;
713 #ifndef DEBUG
715 #endif
716 privptr->stats.rx_dropped++;
717 return;
719 memcpy(skb_put(skb, pskb->len), pskb->data, pskb->len);
720 skb->mac.raw = skb->data;
721 skb->dev = pskb->dev;
722 skb->protocol = pskb->protocol;
723 pskb->ip_summed = CHECKSUM_UNNECESSARY;
724 if (ch->protocol == CTC_PROTO_LINUX_TTY)
725 ctc_tty_netif_rx(skb);
726 else
727 netif_rx_ni(skb);
729 * Successful rx; reset logflags
731 ch->logflags = 0;
732 dev->last_rx = jiffies;
733 privptr->stats.rx_packets++;
734 privptr->stats.rx_bytes += skb->len;
735 if (len > 0) {
736 skb_pull(pskb, header->length);
737 if (skb_tailroom(pskb) < LL_HEADER_LENGTH) {
738 #ifndef DEBUG
739 if (!(ch->logflags & LOG_FLAG_OVERRUN)) {
740 #endif
741 ctc_pr_warn(
742 "%s Overrun in ctc_unpack_skb\n",
743 dev->name);
744 ch->logflags |= LOG_FLAG_OVERRUN;
745 #ifndef DEBUG
747 #endif
748 return;
750 skb_put(pskb, LL_HEADER_LENGTH);
756 * Check return code of a preceeding ccw_device call, halt_IO etc...
758 * @param ch The channel, the error belongs to.
759 * @param return_code The error code to inspect.
761 static void inline
762 ccw_check_return_code(struct channel *ch, int return_code, char *msg)
764 DBF_TEXT(trace, 5, __FUNCTION__);
765 switch (return_code) {
766 case 0:
767 fsm_event(ch->fsm, CH_EVENT_IO_SUCCESS, ch);
768 break;
769 case -EBUSY:
770 ctc_pr_warn("%s (%s): Busy !\n", ch->id, msg);
771 fsm_event(ch->fsm, CH_EVENT_IO_EBUSY, ch);
772 break;
773 case -ENODEV:
774 ctc_pr_emerg("%s (%s): Invalid device called for IO\n",
775 ch->id, msg);
776 fsm_event(ch->fsm, CH_EVENT_IO_ENODEV, ch);
777 break;
778 case -EIO:
779 ctc_pr_emerg("%s (%s): Status pending... \n",
780 ch->id, msg);
781 fsm_event(ch->fsm, CH_EVENT_IO_EIO, ch);
782 break;
783 default:
784 ctc_pr_emerg("%s (%s): Unknown error in do_IO %04x\n",
785 ch->id, msg, return_code);
786 fsm_event(ch->fsm, CH_EVENT_IO_UNKNOWN, ch);
791 * Check sense of a unit check.
793 * @param ch The channel, the sense code belongs to.
794 * @param sense The sense code to inspect.
796 static void inline
797 ccw_unit_check(struct channel *ch, unsigned char sense)
799 DBF_TEXT(trace, 5, __FUNCTION__);
800 if (sense & SNS0_INTERVENTION_REQ) {
801 if (sense & 0x01) {
802 if (ch->protocol != CTC_PROTO_LINUX_TTY)
803 ctc_pr_debug("%s: Interface disc. or Sel. reset "
804 "(remote)\n", ch->id);
805 fsm_event(ch->fsm, CH_EVENT_UC_RCRESET, ch);
806 } else {
807 ctc_pr_debug("%s: System reset (remote)\n", ch->id);
808 fsm_event(ch->fsm, CH_EVENT_UC_RSRESET, ch);
810 } else if (sense & SNS0_EQUIPMENT_CHECK) {
811 if (sense & SNS0_BUS_OUT_CHECK) {
812 ctc_pr_warn("%s: Hardware malfunction (remote)\n",
813 ch->id);
814 fsm_event(ch->fsm, CH_EVENT_UC_HWFAIL, ch);
815 } else {
816 ctc_pr_warn("%s: Read-data parity error (remote)\n",
817 ch->id);
818 fsm_event(ch->fsm, CH_EVENT_UC_RXPARITY, ch);
820 } else if (sense & SNS0_BUS_OUT_CHECK) {
821 if (sense & 0x04) {
822 ctc_pr_warn("%s: Data-streaming timeout)\n", ch->id);
823 fsm_event(ch->fsm, CH_EVENT_UC_TXTIMEOUT, ch);
824 } else {
825 ctc_pr_warn("%s: Data-transfer parity error\n", ch->id);
826 fsm_event(ch->fsm, CH_EVENT_UC_TXPARITY, ch);
828 } else if (sense & SNS0_CMD_REJECT) {
829 ctc_pr_warn("%s: Command reject\n", ch->id);
830 } else if (sense == 0) {
831 ctc_pr_debug("%s: Unit check ZERO\n", ch->id);
832 fsm_event(ch->fsm, CH_EVENT_UC_ZERO, ch);
833 } else {
834 ctc_pr_warn("%s: Unit Check with sense code: %02x\n",
835 ch->id, sense);
836 fsm_event(ch->fsm, CH_EVENT_UC_UNKNOWN, ch);
840 static void
841 ctc_purge_skb_queue(struct sk_buff_head *q)
843 struct sk_buff *skb;
845 DBF_TEXT(trace, 5, __FUNCTION__);
847 while ((skb = skb_dequeue(q))) {
848 atomic_dec(&skb->users);
849 dev_kfree_skb_irq(skb);
853 static __inline__ int
854 ctc_checkalloc_buffer(struct channel *ch, int warn)
856 DBF_TEXT(trace, 5, __FUNCTION__);
857 if ((ch->trans_skb == NULL) ||
858 (ch->flags & CHANNEL_FLAGS_BUFSIZE_CHANGED)) {
859 if (ch->trans_skb != NULL)
860 dev_kfree_skb(ch->trans_skb);
861 clear_normalized_cda(&ch->ccw[1]);
862 ch->trans_skb = __dev_alloc_skb(ch->max_bufsize,
863 GFP_ATOMIC | GFP_DMA);
864 if (ch->trans_skb == NULL) {
865 if (warn)
866 ctc_pr_warn(
867 "%s: Couldn't alloc %s trans_skb\n",
868 ch->id,
869 (CHANNEL_DIRECTION(ch->flags) == READ) ?
870 "RX" : "TX");
871 return -ENOMEM;
873 ch->ccw[1].count = ch->max_bufsize;
874 if (set_normalized_cda(&ch->ccw[1], ch->trans_skb->data)) {
875 dev_kfree_skb(ch->trans_skb);
876 ch->trans_skb = NULL;
877 if (warn)
878 ctc_pr_warn(
879 "%s: set_normalized_cda for %s "
880 "trans_skb failed, dropping packets\n",
881 ch->id,
882 (CHANNEL_DIRECTION(ch->flags) == READ) ?
883 "RX" : "TX");
884 return -ENOMEM;
886 ch->ccw[1].count = 0;
887 ch->trans_skb_data = ch->trans_skb->data;
888 ch->flags &= ~CHANNEL_FLAGS_BUFSIZE_CHANGED;
890 return 0;
894 * Dummy NOP action for statemachines
896 static void
897 fsm_action_nop(fsm_instance * fi, int event, void *arg)
902 * Actions for channel - statemachines.
903 *****************************************************************************/
906 * Normal data has been send. Free the corresponding
907 * skb (it's in io_queue), reset dev->tbusy and
908 * revert to idle state.
910 * @param fi An instance of a channel statemachine.
911 * @param event The event, just happened.
912 * @param arg Generic pointer, casted from channel * upon call.
914 static void
915 ch_action_txdone(fsm_instance * fi, int event, void *arg)
917 struct channel *ch = (struct channel *) arg;
918 struct net_device *dev = ch->netdev;
919 struct ctc_priv *privptr = dev->priv;
920 struct sk_buff *skb;
921 int first = 1;
922 int i;
923 unsigned long duration;
924 struct timespec done_stamp = xtime;
926 DBF_TEXT(trace, 4, __FUNCTION__);
928 duration =
929 (done_stamp.tv_sec - ch->prof.send_stamp.tv_sec) * 1000000 +
930 (done_stamp.tv_nsec - ch->prof.send_stamp.tv_nsec) / 1000;
931 if (duration > ch->prof.tx_time)
932 ch->prof.tx_time = duration;
934 if (ch->irb->scsw.count != 0)
935 ctc_pr_debug("%s: TX not complete, remaining %d bytes\n",
936 dev->name, ch->irb->scsw.count);
937 fsm_deltimer(&ch->timer);
938 while ((skb = skb_dequeue(&ch->io_queue))) {
939 privptr->stats.tx_packets++;
940 privptr->stats.tx_bytes += skb->len - LL_HEADER_LENGTH;
941 if (first) {
942 privptr->stats.tx_bytes += 2;
943 first = 0;
945 atomic_dec(&skb->users);
946 dev_kfree_skb_irq(skb);
948 spin_lock(&ch->collect_lock);
949 clear_normalized_cda(&ch->ccw[4]);
950 if (ch->collect_len > 0) {
951 int rc;
953 if (ctc_checkalloc_buffer(ch, 1)) {
954 spin_unlock(&ch->collect_lock);
955 return;
957 ch->trans_skb->tail = ch->trans_skb->data = ch->trans_skb_data;
958 ch->trans_skb->len = 0;
959 if (ch->prof.maxmulti < (ch->collect_len + 2))
960 ch->prof.maxmulti = ch->collect_len + 2;
961 if (ch->prof.maxcqueue < skb_queue_len(&ch->collect_queue))
962 ch->prof.maxcqueue = skb_queue_len(&ch->collect_queue);
963 *((__u16 *) skb_put(ch->trans_skb, 2)) = ch->collect_len + 2;
964 i = 0;
965 while ((skb = skb_dequeue(&ch->collect_queue))) {
966 memcpy(skb_put(ch->trans_skb, skb->len), skb->data,
967 skb->len);
968 privptr->stats.tx_packets++;
969 privptr->stats.tx_bytes += skb->len - LL_HEADER_LENGTH;
970 atomic_dec(&skb->users);
971 dev_kfree_skb_irq(skb);
972 i++;
974 ch->collect_len = 0;
975 spin_unlock(&ch->collect_lock);
976 ch->ccw[1].count = ch->trans_skb->len;
977 fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC, CH_EVENT_TIMER, ch);
978 ch->prof.send_stamp = xtime;
979 rc = ccw_device_start(ch->cdev, &ch->ccw[0],
980 (unsigned long) ch, 0xff, 0);
981 ch->prof.doios_multi++;
982 if (rc != 0) {
983 privptr->stats.tx_dropped += i;
984 privptr->stats.tx_errors += i;
985 fsm_deltimer(&ch->timer);
986 ccw_check_return_code(ch, rc, "chained TX");
988 } else {
989 spin_unlock(&ch->collect_lock);
990 fsm_newstate(fi, CH_STATE_TXIDLE);
992 ctc_clear_busy(dev);
996 * Initial data is sent.
997 * Notify device statemachine that we are up and
998 * running.
1000 * @param fi An instance of a channel statemachine.
1001 * @param event The event, just happened.
1002 * @param arg Generic pointer, casted from channel * upon call.
1004 static void
1005 ch_action_txidle(fsm_instance * fi, int event, void *arg)
1007 struct channel *ch = (struct channel *) arg;
1009 DBF_TEXT(trace, 4, __FUNCTION__);
1010 fsm_deltimer(&ch->timer);
1011 fsm_newstate(fi, CH_STATE_TXIDLE);
1012 fsm_event(((struct ctc_priv *) ch->netdev->priv)->fsm, DEV_EVENT_TXUP,
1013 ch->netdev);
1017 * Got normal data, check for sanity, queue it up, allocate new buffer
1018 * trigger bottom half, and initiate next read.
1020 * @param fi An instance of a channel statemachine.
1021 * @param event The event, just happened.
1022 * @param arg Generic pointer, casted from channel * upon call.
1024 static void
1025 ch_action_rx(fsm_instance * fi, int event, void *arg)
1027 struct channel *ch = (struct channel *) arg;
1028 struct net_device *dev = ch->netdev;
1029 struct ctc_priv *privptr = dev->priv;
1030 int len = ch->max_bufsize - ch->irb->scsw.count;
1031 struct sk_buff *skb = ch->trans_skb;
1032 __u16 block_len = *((__u16 *) skb->data);
1033 int check_len;
1034 int rc;
1036 DBF_TEXT(trace, 4, __FUNCTION__);
1037 fsm_deltimer(&ch->timer);
1038 if (len < 8) {
1039 ctc_pr_debug("%s: got packet with length %d < 8\n",
1040 dev->name, len);
1041 privptr->stats.rx_dropped++;
1042 privptr->stats.rx_length_errors++;
1043 goto again;
1045 if (len > ch->max_bufsize) {
1046 ctc_pr_debug("%s: got packet with length %d > %d\n",
1047 dev->name, len, ch->max_bufsize);
1048 privptr->stats.rx_dropped++;
1049 privptr->stats.rx_length_errors++;
1050 goto again;
1054 * VM TCP seems to have a bug sending 2 trailing bytes of garbage.
1056 switch (ch->protocol) {
1057 case CTC_PROTO_S390:
1058 case CTC_PROTO_OS390:
1059 check_len = block_len + 2;
1060 break;
1061 default:
1062 check_len = block_len;
1063 break;
1065 if ((len < block_len) || (len > check_len)) {
1066 ctc_pr_debug("%s: got block length %d != rx length %d\n",
1067 dev->name, block_len, len);
1068 #ifdef DEBUG
1069 ctc_dump_skb(skb, 0);
1070 #endif
1071 *((__u16 *) skb->data) = len;
1072 privptr->stats.rx_dropped++;
1073 privptr->stats.rx_length_errors++;
1074 goto again;
1076 block_len -= 2;
1077 if (block_len > 0) {
1078 *((__u16 *) skb->data) = block_len;
1079 ctc_unpack_skb(ch, skb);
1081 again:
1082 skb->data = skb->tail = ch->trans_skb_data;
1083 skb->len = 0;
1084 if (ctc_checkalloc_buffer(ch, 1))
1085 return;
1086 ch->ccw[1].count = ch->max_bufsize;
1087 rc = ccw_device_start(ch->cdev, &ch->ccw[0], (unsigned long) ch, 0xff, 0);
1088 if (rc != 0)
1089 ccw_check_return_code(ch, rc, "normal RX");
1092 static void ch_action_rxidle(fsm_instance * fi, int event, void *arg);
1095 * Initialize connection by sending a __u16 of value 0.
1097 * @param fi An instance of a channel statemachine.
1098 * @param event The event, just happened.
1099 * @param arg Generic pointer, casted from channel * upon call.
1101 static void
1102 ch_action_firstio(fsm_instance * fi, int event, void *arg)
1104 struct channel *ch = (struct channel *) arg;
1105 int rc;
1107 DBF_TEXT(trace, 4, __FUNCTION__);
1109 if (fsm_getstate(fi) == CH_STATE_TXIDLE)
1110 ctc_pr_debug("%s: remote side issued READ?, init ...\n", ch->id);
1111 fsm_deltimer(&ch->timer);
1112 if (ctc_checkalloc_buffer(ch, 1))
1113 return;
1114 if ((fsm_getstate(fi) == CH_STATE_SETUPWAIT) &&
1115 (ch->protocol == CTC_PROTO_OS390)) {
1116 /* OS/390 resp. z/OS */
1117 if (CHANNEL_DIRECTION(ch->flags) == READ) {
1118 *((__u16 *) ch->trans_skb->data) = CTC_INITIAL_BLOCKLEN;
1119 fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC,
1120 CH_EVENT_TIMER, ch);
1121 ch_action_rxidle(fi, event, arg);
1122 } else {
1123 struct net_device *dev = ch->netdev;
1124 fsm_newstate(fi, CH_STATE_TXIDLE);
1125 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1126 DEV_EVENT_TXUP, dev);
1128 return;
1132 * Don´t setup a timer for receiving the initial RX frame
1133 * if in compatibility mode, since VM TCP delays the initial
1134 * frame until it has some data to send.
1136 if ((CHANNEL_DIRECTION(ch->flags) == WRITE) ||
1137 (ch->protocol != CTC_PROTO_S390))
1138 fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC, CH_EVENT_TIMER, ch);
1140 *((__u16 *) ch->trans_skb->data) = CTC_INITIAL_BLOCKLEN;
1141 ch->ccw[1].count = 2; /* Transfer only length */
1143 fsm_newstate(fi, (CHANNEL_DIRECTION(ch->flags) == READ)
1144 ? CH_STATE_RXINIT : CH_STATE_TXINIT);
1145 rc = ccw_device_start(ch->cdev, &ch->ccw[0], (unsigned long) ch, 0xff, 0);
1146 if (rc != 0) {
1147 fsm_deltimer(&ch->timer);
1148 fsm_newstate(fi, CH_STATE_SETUPWAIT);
1149 ccw_check_return_code(ch, rc, "init IO");
1152 * If in compatibility mode since we don´t setup a timer, we
1153 * also signal RX channel up immediately. This enables us
1154 * to send packets early which in turn usually triggers some
1155 * reply from VM TCP which brings up the RX channel to it´s
1156 * final state.
1158 if ((CHANNEL_DIRECTION(ch->flags) == READ) &&
1159 (ch->protocol == CTC_PROTO_S390)) {
1160 struct net_device *dev = ch->netdev;
1161 fsm_event(((struct ctc_priv *) dev->priv)->fsm, DEV_EVENT_RXUP,
1162 dev);
1167 * Got initial data, check it. If OK,
1168 * notify device statemachine that we are up and
1169 * running.
1171 * @param fi An instance of a channel statemachine.
1172 * @param event The event, just happened.
1173 * @param arg Generic pointer, casted from channel * upon call.
1175 static void
1176 ch_action_rxidle(fsm_instance * fi, int event, void *arg)
1178 struct channel *ch = (struct channel *) arg;
1179 struct net_device *dev = ch->netdev;
1180 __u16 buflen;
1181 int rc;
1183 DBF_TEXT(trace, 4, __FUNCTION__);
1184 fsm_deltimer(&ch->timer);
1185 buflen = *((__u16 *) ch->trans_skb->data);
1186 #ifdef DEBUG
1187 ctc_pr_debug("%s: Initial RX count %d\n", dev->name, buflen);
1188 #endif
1189 if (buflen >= CTC_INITIAL_BLOCKLEN) {
1190 if (ctc_checkalloc_buffer(ch, 1))
1191 return;
1192 ch->ccw[1].count = ch->max_bufsize;
1193 fsm_newstate(fi, CH_STATE_RXIDLE);
1194 rc = ccw_device_start(ch->cdev, &ch->ccw[0],
1195 (unsigned long) ch, 0xff, 0);
1196 if (rc != 0) {
1197 fsm_newstate(fi, CH_STATE_RXINIT);
1198 ccw_check_return_code(ch, rc, "initial RX");
1199 } else
1200 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1201 DEV_EVENT_RXUP, dev);
1202 } else {
1203 ctc_pr_debug("%s: Initial RX count %d not %d\n",
1204 dev->name, buflen, CTC_INITIAL_BLOCKLEN);
1205 ch_action_firstio(fi, event, arg);
1210 * Set channel into extended mode.
1212 * @param fi An instance of a channel statemachine.
1213 * @param event The event, just happened.
1214 * @param arg Generic pointer, casted from channel * upon call.
1216 static void
1217 ch_action_setmode(fsm_instance * fi, int event, void *arg)
1219 struct channel *ch = (struct channel *) arg;
1220 int rc;
1221 unsigned long saveflags;
1223 DBF_TEXT(trace, 4, __FUNCTION__);
1224 fsm_deltimer(&ch->timer);
1225 fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC, CH_EVENT_TIMER, ch);
1226 fsm_newstate(fi, CH_STATE_SETUPWAIT);
1227 if (event == CH_EVENT_TIMER)
1228 spin_lock_irqsave(get_ccwdev_lock(ch->cdev), saveflags);
1229 rc = ccw_device_start(ch->cdev, &ch->ccw[6], (unsigned long) ch, 0xff, 0);
1230 if (event == CH_EVENT_TIMER)
1231 spin_unlock_irqrestore(get_ccwdev_lock(ch->cdev), saveflags);
1232 if (rc != 0) {
1233 fsm_deltimer(&ch->timer);
1234 fsm_newstate(fi, CH_STATE_STARTWAIT);
1235 ccw_check_return_code(ch, rc, "set Mode");
1236 } else
1237 ch->retry = 0;
1241 * Setup channel.
1243 * @param fi An instance of a channel statemachine.
1244 * @param event The event, just happened.
1245 * @param arg Generic pointer, casted from channel * upon call.
1247 static void
1248 ch_action_start(fsm_instance * fi, int event, void *arg)
1250 struct channel *ch = (struct channel *) arg;
1251 unsigned long saveflags;
1252 int rc;
1253 struct net_device *dev;
1255 DBF_TEXT(trace, 4, __FUNCTION__);
1256 if (ch == NULL) {
1257 ctc_pr_warn("ch_action_start ch=NULL\n");
1258 return;
1260 if (ch->netdev == NULL) {
1261 ctc_pr_warn("ch_action_start dev=NULL, id=%s\n", ch->id);
1262 return;
1264 dev = ch->netdev;
1266 #ifdef DEBUG
1267 ctc_pr_debug("%s: %s channel start\n", dev->name,
1268 (CHANNEL_DIRECTION(ch->flags) == READ) ? "RX" : "TX");
1269 #endif
1271 if (ch->trans_skb != NULL) {
1272 clear_normalized_cda(&ch->ccw[1]);
1273 dev_kfree_skb(ch->trans_skb);
1274 ch->trans_skb = NULL;
1276 if (CHANNEL_DIRECTION(ch->flags) == READ) {
1277 ch->ccw[1].cmd_code = CCW_CMD_READ;
1278 ch->ccw[1].flags = CCW_FLAG_SLI;
1279 ch->ccw[1].count = 0;
1280 } else {
1281 ch->ccw[1].cmd_code = CCW_CMD_WRITE;
1282 ch->ccw[1].flags = CCW_FLAG_SLI | CCW_FLAG_CC;
1283 ch->ccw[1].count = 0;
1285 if (ctc_checkalloc_buffer(ch, 0)) {
1286 ctc_pr_notice(
1287 "%s: Could not allocate %s trans_skb, delaying "
1288 "allocation until first transfer\n",
1289 dev->name,
1290 (CHANNEL_DIRECTION(ch->flags) == READ) ? "RX" : "TX");
1293 ch->ccw[0].cmd_code = CCW_CMD_PREPARE;
1294 ch->ccw[0].flags = CCW_FLAG_SLI | CCW_FLAG_CC;
1295 ch->ccw[0].count = 0;
1296 ch->ccw[0].cda = 0;
1297 ch->ccw[2].cmd_code = CCW_CMD_NOOP; /* jointed CE + DE */
1298 ch->ccw[2].flags = CCW_FLAG_SLI;
1299 ch->ccw[2].count = 0;
1300 ch->ccw[2].cda = 0;
1301 memcpy(&ch->ccw[3], &ch->ccw[0], sizeof (struct ccw1) * 3);
1302 ch->ccw[4].cda = 0;
1303 ch->ccw[4].flags &= ~CCW_FLAG_IDA;
1305 fsm_newstate(fi, CH_STATE_STARTWAIT);
1306 fsm_addtimer(&ch->timer, 1000, CH_EVENT_TIMER, ch);
1307 spin_lock_irqsave(get_ccwdev_lock(ch->cdev), saveflags);
1308 rc = ccw_device_halt(ch->cdev, (unsigned long) ch);
1309 spin_unlock_irqrestore(get_ccwdev_lock(ch->cdev), saveflags);
1310 if (rc != 0) {
1311 if (rc != -EBUSY)
1312 fsm_deltimer(&ch->timer);
1313 ccw_check_return_code(ch, rc, "initial HaltIO");
1315 #ifdef DEBUG
1316 ctc_pr_debug("ctc: %s(): leaving\n", __func__);
1317 #endif
1321 * Shutdown a channel.
1323 * @param fi An instance of a channel statemachine.
1324 * @param event The event, just happened.
1325 * @param arg Generic pointer, casted from channel * upon call.
1327 static void
1328 ch_action_haltio(fsm_instance * fi, int event, void *arg)
1330 struct channel *ch = (struct channel *) arg;
1331 unsigned long saveflags;
1332 int rc;
1333 int oldstate;
1335 DBF_TEXT(trace, 3, __FUNCTION__);
1336 fsm_deltimer(&ch->timer);
1337 fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC, CH_EVENT_TIMER, ch);
1338 if (event == CH_EVENT_STOP)
1339 spin_lock_irqsave(get_ccwdev_lock(ch->cdev), saveflags);
1340 oldstate = fsm_getstate(fi);
1341 fsm_newstate(fi, CH_STATE_TERM);
1342 rc = ccw_device_halt(ch->cdev, (unsigned long) ch);
1343 if (event == CH_EVENT_STOP)
1344 spin_unlock_irqrestore(get_ccwdev_lock(ch->cdev), saveflags);
1345 if (rc != 0) {
1346 if (rc != -EBUSY) {
1347 fsm_deltimer(&ch->timer);
1348 fsm_newstate(fi, oldstate);
1350 ccw_check_return_code(ch, rc, "HaltIO in ch_action_haltio");
1355 * A channel has successfully been halted.
1356 * Cleanup it's queue and notify interface statemachine.
1358 * @param fi An instance of a channel statemachine.
1359 * @param event The event, just happened.
1360 * @param arg Generic pointer, casted from channel * upon call.
1362 static void
1363 ch_action_stopped(fsm_instance * fi, int event, void *arg)
1365 struct channel *ch = (struct channel *) arg;
1366 struct net_device *dev = ch->netdev;
1368 DBF_TEXT(trace, 3, __FUNCTION__);
1369 fsm_deltimer(&ch->timer);
1370 fsm_newstate(fi, CH_STATE_STOPPED);
1371 if (ch->trans_skb != NULL) {
1372 clear_normalized_cda(&ch->ccw[1]);
1373 dev_kfree_skb(ch->trans_skb);
1374 ch->trans_skb = NULL;
1376 if (CHANNEL_DIRECTION(ch->flags) == READ) {
1377 skb_queue_purge(&ch->io_queue);
1378 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1379 DEV_EVENT_RXDOWN, dev);
1380 } else {
1381 ctc_purge_skb_queue(&ch->io_queue);
1382 spin_lock(&ch->collect_lock);
1383 ctc_purge_skb_queue(&ch->collect_queue);
1384 ch->collect_len = 0;
1385 spin_unlock(&ch->collect_lock);
1386 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1387 DEV_EVENT_TXDOWN, dev);
1392 * A stop command from device statemachine arrived and we are in
1393 * not operational mode. Set state to stopped.
1395 * @param fi An instance of a channel statemachine.
1396 * @param event The event, just happened.
1397 * @param arg Generic pointer, casted from channel * upon call.
1399 static void
1400 ch_action_stop(fsm_instance * fi, int event, void *arg)
1402 fsm_newstate(fi, CH_STATE_STOPPED);
1406 * A machine check for no path, not operational status or gone device has
1407 * happened.
1408 * Cleanup queue and notify interface statemachine.
1410 * @param fi An instance of a channel statemachine.
1411 * @param event The event, just happened.
1412 * @param arg Generic pointer, casted from channel * upon call.
1414 static void
1415 ch_action_fail(fsm_instance * fi, int event, void *arg)
1417 struct channel *ch = (struct channel *) arg;
1418 struct net_device *dev = ch->netdev;
1420 DBF_TEXT(trace, 3, __FUNCTION__);
1421 fsm_deltimer(&ch->timer);
1422 fsm_newstate(fi, CH_STATE_NOTOP);
1423 if (CHANNEL_DIRECTION(ch->flags) == READ) {
1424 skb_queue_purge(&ch->io_queue);
1425 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1426 DEV_EVENT_RXDOWN, dev);
1427 } else {
1428 ctc_purge_skb_queue(&ch->io_queue);
1429 spin_lock(&ch->collect_lock);
1430 ctc_purge_skb_queue(&ch->collect_queue);
1431 ch->collect_len = 0;
1432 spin_unlock(&ch->collect_lock);
1433 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1434 DEV_EVENT_TXDOWN, dev);
1439 * Handle error during setup of channel.
1441 * @param fi An instance of a channel statemachine.
1442 * @param event The event, just happened.
1443 * @param arg Generic pointer, casted from channel * upon call.
1445 static void
1446 ch_action_setuperr(fsm_instance * fi, int event, void *arg)
1448 struct channel *ch = (struct channel *) arg;
1449 struct net_device *dev = ch->netdev;
1451 DBF_TEXT(setup, 3, __FUNCTION__);
1453 * Special case: Got UC_RCRESET on setmode.
1454 * This means that remote side isn't setup. In this case
1455 * simply retry after some 10 secs...
1457 if ((fsm_getstate(fi) == CH_STATE_SETUPWAIT) &&
1458 ((event == CH_EVENT_UC_RCRESET) ||
1459 (event == CH_EVENT_UC_RSRESET))) {
1460 fsm_newstate(fi, CH_STATE_STARTRETRY);
1461 fsm_deltimer(&ch->timer);
1462 fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC, CH_EVENT_TIMER, ch);
1463 if (CHANNEL_DIRECTION(ch->flags) == READ) {
1464 int rc = ccw_device_halt(ch->cdev, (unsigned long) ch);
1465 if (rc != 0)
1466 ccw_check_return_code(
1467 ch, rc, "HaltIO in ch_action_setuperr");
1469 return;
1472 ctc_pr_debug("%s: Error %s during %s channel setup state=%s\n",
1473 dev->name, ch_event_names[event],
1474 (CHANNEL_DIRECTION(ch->flags) == READ) ? "RX" : "TX",
1475 fsm_getstate_str(fi));
1476 if (CHANNEL_DIRECTION(ch->flags) == READ) {
1477 fsm_newstate(fi, CH_STATE_RXERR);
1478 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1479 DEV_EVENT_RXDOWN, dev);
1480 } else {
1481 fsm_newstate(fi, CH_STATE_TXERR);
1482 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1483 DEV_EVENT_TXDOWN, dev);
1488 * Restart a channel after an error.
1490 * @param fi An instance of a channel statemachine.
1491 * @param event The event, just happened.
1492 * @param arg Generic pointer, casted from channel * upon call.
1494 static void
1495 ch_action_restart(fsm_instance * fi, int event, void *arg)
1497 unsigned long saveflags;
1498 int oldstate;
1499 int rc;
1501 struct channel *ch = (struct channel *) arg;
1502 struct net_device *dev = ch->netdev;
1504 DBF_TEXT(trace, 3, __FUNCTION__);
1505 fsm_deltimer(&ch->timer);
1506 ctc_pr_debug("%s: %s channel restart\n", dev->name,
1507 (CHANNEL_DIRECTION(ch->flags) == READ) ? "RX" : "TX");
1508 fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC, CH_EVENT_TIMER, ch);
1509 oldstate = fsm_getstate(fi);
1510 fsm_newstate(fi, CH_STATE_STARTWAIT);
1511 if (event == CH_EVENT_TIMER)
1512 spin_lock_irqsave(get_ccwdev_lock(ch->cdev), saveflags);
1513 rc = ccw_device_halt(ch->cdev, (unsigned long) ch);
1514 if (event == CH_EVENT_TIMER)
1515 spin_unlock_irqrestore(get_ccwdev_lock(ch->cdev), saveflags);
1516 if (rc != 0) {
1517 if (rc != -EBUSY) {
1518 fsm_deltimer(&ch->timer);
1519 fsm_newstate(fi, oldstate);
1521 ccw_check_return_code(ch, rc, "HaltIO in ch_action_restart");
1526 * Handle error during RX initial handshake (exchange of
1527 * 0-length block header)
1529 * @param fi An instance of a channel statemachine.
1530 * @param event The event, just happened.
1531 * @param arg Generic pointer, casted from channel * upon call.
1533 static void
1534 ch_action_rxiniterr(fsm_instance * fi, int event, void *arg)
1536 struct channel *ch = (struct channel *) arg;
1537 struct net_device *dev = ch->netdev;
1539 DBF_TEXT(setup, 3, __FUNCTION__);
1540 if (event == CH_EVENT_TIMER) {
1541 fsm_deltimer(&ch->timer);
1542 ctc_pr_debug("%s: Timeout during RX init handshake\n", dev->name);
1543 if (ch->retry++ < 3)
1544 ch_action_restart(fi, event, arg);
1545 else {
1546 fsm_newstate(fi, CH_STATE_RXERR);
1547 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1548 DEV_EVENT_RXDOWN, dev);
1550 } else
1551 ctc_pr_warn("%s: Error during RX init handshake\n", dev->name);
1555 * Notify device statemachine if we gave up initialization
1556 * of RX channel.
1558 * @param fi An instance of a channel statemachine.
1559 * @param event The event, just happened.
1560 * @param arg Generic pointer, casted from channel * upon call.
1562 static void
1563 ch_action_rxinitfail(fsm_instance * fi, int event, void *arg)
1565 struct channel *ch = (struct channel *) arg;
1566 struct net_device *dev = ch->netdev;
1568 DBF_TEXT(setup, 3, __FUNCTION__);
1569 fsm_newstate(fi, CH_STATE_RXERR);
1570 ctc_pr_warn("%s: RX initialization failed\n", dev->name);
1571 ctc_pr_warn("%s: RX <-> RX connection detected\n", dev->name);
1572 fsm_event(((struct ctc_priv *) dev->priv)->fsm, DEV_EVENT_RXDOWN, dev);
1576 * Handle RX Unit check remote reset (remote disconnected)
1578 * @param fi An instance of a channel statemachine.
1579 * @param event The event, just happened.
1580 * @param arg Generic pointer, casted from channel * upon call.
1582 static void
1583 ch_action_rxdisc(fsm_instance * fi, int event, void *arg)
1585 struct channel *ch = (struct channel *) arg;
1586 struct channel *ch2;
1587 struct net_device *dev = ch->netdev;
1589 DBF_TEXT(trace, 3, __FUNCTION__);
1590 fsm_deltimer(&ch->timer);
1591 ctc_pr_debug("%s: Got remote disconnect, re-initializing ...\n",
1592 dev->name);
1595 * Notify device statemachine
1597 fsm_event(((struct ctc_priv *) dev->priv)->fsm, DEV_EVENT_RXDOWN, dev);
1598 fsm_event(((struct ctc_priv *) dev->priv)->fsm, DEV_EVENT_TXDOWN, dev);
1600 fsm_newstate(fi, CH_STATE_DTERM);
1601 ch2 = ((struct ctc_priv *) dev->priv)->channel[WRITE];
1602 fsm_newstate(ch2->fsm, CH_STATE_DTERM);
1604 ccw_device_halt(ch->cdev, (unsigned long) ch);
1605 ccw_device_halt(ch2->cdev, (unsigned long) ch2);
1609 * Handle error during TX channel initialization.
1611 * @param fi An instance of a channel statemachine.
1612 * @param event The event, just happened.
1613 * @param arg Generic pointer, casted from channel * upon call.
1615 static void
1616 ch_action_txiniterr(fsm_instance * fi, int event, void *arg)
1618 struct channel *ch = (struct channel *) arg;
1619 struct net_device *dev = ch->netdev;
1621 DBF_TEXT(setup, 2, __FUNCTION__);
1622 if (event == CH_EVENT_TIMER) {
1623 fsm_deltimer(&ch->timer);
1624 ctc_pr_debug("%s: Timeout during TX init handshake\n", dev->name);
1625 if (ch->retry++ < 3)
1626 ch_action_restart(fi, event, arg);
1627 else {
1628 fsm_newstate(fi, CH_STATE_TXERR);
1629 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1630 DEV_EVENT_TXDOWN, dev);
1632 } else
1633 ctc_pr_warn("%s: Error during TX init handshake\n", dev->name);
1637 * Handle TX timeout by retrying operation.
1639 * @param fi An instance of a channel statemachine.
1640 * @param event The event, just happened.
1641 * @param arg Generic pointer, casted from channel * upon call.
1643 static void
1644 ch_action_txretry(fsm_instance * fi, int event, void *arg)
1646 struct channel *ch = (struct channel *) arg;
1647 struct net_device *dev = ch->netdev;
1648 unsigned long saveflags;
1650 DBF_TEXT(trace, 4, __FUNCTION__);
1651 fsm_deltimer(&ch->timer);
1652 if (ch->retry++ > 3) {
1653 ctc_pr_debug("%s: TX retry failed, restarting channel\n",
1654 dev->name);
1655 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1656 DEV_EVENT_TXDOWN, dev);
1657 ch_action_restart(fi, event, arg);
1658 } else {
1659 struct sk_buff *skb;
1661 ctc_pr_debug("%s: TX retry %d\n", dev->name, ch->retry);
1662 if ((skb = skb_peek(&ch->io_queue))) {
1663 int rc = 0;
1665 clear_normalized_cda(&ch->ccw[4]);
1666 ch->ccw[4].count = skb->len;
1667 if (set_normalized_cda(&ch->ccw[4], skb->data)) {
1668 ctc_pr_debug(
1669 "%s: IDAL alloc failed, chan restart\n",
1670 dev->name);
1671 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1672 DEV_EVENT_TXDOWN, dev);
1673 ch_action_restart(fi, event, arg);
1674 return;
1676 fsm_addtimer(&ch->timer, 1000, CH_EVENT_TIMER, ch);
1677 if (event == CH_EVENT_TIMER)
1678 spin_lock_irqsave(get_ccwdev_lock(ch->cdev),
1679 saveflags);
1680 rc = ccw_device_start(ch->cdev, &ch->ccw[3],
1681 (unsigned long) ch, 0xff, 0);
1682 if (event == CH_EVENT_TIMER)
1683 spin_unlock_irqrestore(get_ccwdev_lock(ch->cdev),
1684 saveflags);
1685 if (rc != 0) {
1686 fsm_deltimer(&ch->timer);
1687 ccw_check_return_code(ch, rc, "TX in ch_action_txretry");
1688 ctc_purge_skb_queue(&ch->io_queue);
1696 * Handle fatal errors during an I/O command.
1698 * @param fi An instance of a channel statemachine.
1699 * @param event The event, just happened.
1700 * @param arg Generic pointer, casted from channel * upon call.
1702 static void
1703 ch_action_iofatal(fsm_instance * fi, int event, void *arg)
1705 struct channel *ch = (struct channel *) arg;
1706 struct net_device *dev = ch->netdev;
1708 DBF_TEXT(trace, 3, __FUNCTION__);
1709 fsm_deltimer(&ch->timer);
1710 if (CHANNEL_DIRECTION(ch->flags) == READ) {
1711 ctc_pr_debug("%s: RX I/O error\n", dev->name);
1712 fsm_newstate(fi, CH_STATE_RXERR);
1713 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1714 DEV_EVENT_RXDOWN, dev);
1715 } else {
1716 ctc_pr_debug("%s: TX I/O error\n", dev->name);
1717 fsm_newstate(fi, CH_STATE_TXERR);
1718 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1719 DEV_EVENT_TXDOWN, dev);
1723 static void
1724 ch_action_reinit(fsm_instance *fi, int event, void *arg)
1726 struct channel *ch = (struct channel *)arg;
1727 struct net_device *dev = ch->netdev;
1728 struct ctc_priv *privptr = dev->priv;
1730 DBF_TEXT(trace, 4, __FUNCTION__);
1731 ch_action_iofatal(fi, event, arg);
1732 fsm_addtimer(&privptr->restart_timer, 1000, DEV_EVENT_RESTART, dev);
1737 * The statemachine for a channel.
1739 static const fsm_node ch_fsm[] = {
1740 {CH_STATE_STOPPED, CH_EVENT_STOP, fsm_action_nop },
1741 {CH_STATE_STOPPED, CH_EVENT_START, ch_action_start },
1742 {CH_STATE_STOPPED, CH_EVENT_FINSTAT, fsm_action_nop },
1743 {CH_STATE_STOPPED, CH_EVENT_MC_FAIL, fsm_action_nop },
1745 {CH_STATE_NOTOP, CH_EVENT_STOP, ch_action_stop },
1746 {CH_STATE_NOTOP, CH_EVENT_START, fsm_action_nop },
1747 {CH_STATE_NOTOP, CH_EVENT_FINSTAT, fsm_action_nop },
1748 {CH_STATE_NOTOP, CH_EVENT_MC_FAIL, fsm_action_nop },
1749 {CH_STATE_NOTOP, CH_EVENT_MC_GOOD, ch_action_start },
1751 {CH_STATE_STARTWAIT, CH_EVENT_STOP, ch_action_haltio },
1752 {CH_STATE_STARTWAIT, CH_EVENT_START, fsm_action_nop },
1753 {CH_STATE_STARTWAIT, CH_EVENT_FINSTAT, ch_action_setmode },
1754 {CH_STATE_STARTWAIT, CH_EVENT_TIMER, ch_action_setuperr },
1755 {CH_STATE_STARTWAIT, CH_EVENT_IO_ENODEV, ch_action_iofatal },
1756 {CH_STATE_STARTWAIT, CH_EVENT_IO_EIO, ch_action_reinit },
1757 {CH_STATE_STARTWAIT, CH_EVENT_MC_FAIL, ch_action_fail },
1759 {CH_STATE_STARTRETRY, CH_EVENT_STOP, ch_action_haltio },
1760 {CH_STATE_STARTRETRY, CH_EVENT_TIMER, ch_action_setmode },
1761 {CH_STATE_STARTRETRY, CH_EVENT_FINSTAT, fsm_action_nop },
1762 {CH_STATE_STARTRETRY, CH_EVENT_MC_FAIL, ch_action_fail },
1764 {CH_STATE_SETUPWAIT, CH_EVENT_STOP, ch_action_haltio },
1765 {CH_STATE_SETUPWAIT, CH_EVENT_START, fsm_action_nop },
1766 {CH_STATE_SETUPWAIT, CH_EVENT_FINSTAT, ch_action_firstio },
1767 {CH_STATE_SETUPWAIT, CH_EVENT_UC_RCRESET, ch_action_setuperr },
1768 {CH_STATE_SETUPWAIT, CH_EVENT_UC_RSRESET, ch_action_setuperr },
1769 {CH_STATE_SETUPWAIT, CH_EVENT_TIMER, ch_action_setmode },
1770 {CH_STATE_SETUPWAIT, CH_EVENT_IO_ENODEV, ch_action_iofatal },
1771 {CH_STATE_SETUPWAIT, CH_EVENT_IO_EIO, ch_action_reinit },
1772 {CH_STATE_SETUPWAIT, CH_EVENT_MC_FAIL, ch_action_fail },
1774 {CH_STATE_RXINIT, CH_EVENT_STOP, ch_action_haltio },
1775 {CH_STATE_RXINIT, CH_EVENT_START, fsm_action_nop },
1776 {CH_STATE_RXINIT, CH_EVENT_FINSTAT, ch_action_rxidle },
1777 {CH_STATE_RXINIT, CH_EVENT_UC_RCRESET, ch_action_rxiniterr },
1778 {CH_STATE_RXINIT, CH_EVENT_UC_RSRESET, ch_action_rxiniterr },
1779 {CH_STATE_RXINIT, CH_EVENT_TIMER, ch_action_rxiniterr },
1780 {CH_STATE_RXINIT, CH_EVENT_ATTNBUSY, ch_action_rxinitfail },
1781 {CH_STATE_RXINIT, CH_EVENT_IO_ENODEV, ch_action_iofatal },
1782 {CH_STATE_RXINIT, CH_EVENT_IO_EIO, ch_action_reinit },
1783 {CH_STATE_RXINIT, CH_EVENT_UC_ZERO, ch_action_firstio },
1784 {CH_STATE_RXINIT, CH_EVENT_MC_FAIL, ch_action_fail },
1786 {CH_STATE_RXIDLE, CH_EVENT_STOP, ch_action_haltio },
1787 {CH_STATE_RXIDLE, CH_EVENT_START, fsm_action_nop },
1788 {CH_STATE_RXIDLE, CH_EVENT_FINSTAT, ch_action_rx },
1789 {CH_STATE_RXIDLE, CH_EVENT_UC_RCRESET, ch_action_rxdisc },
1790 // {CH_STATE_RXIDLE, CH_EVENT_UC_RSRESET, ch_action_rxretry },
1791 {CH_STATE_RXIDLE, CH_EVENT_IO_ENODEV, ch_action_iofatal },
1792 {CH_STATE_RXIDLE, CH_EVENT_IO_EIO, ch_action_reinit },
1793 {CH_STATE_RXIDLE, CH_EVENT_MC_FAIL, ch_action_fail },
1794 {CH_STATE_RXIDLE, CH_EVENT_UC_ZERO, ch_action_rx },
1796 {CH_STATE_TXINIT, CH_EVENT_STOP, ch_action_haltio },
1797 {CH_STATE_TXINIT, CH_EVENT_START, fsm_action_nop },
1798 {CH_STATE_TXINIT, CH_EVENT_FINSTAT, ch_action_txidle },
1799 {CH_STATE_TXINIT, CH_EVENT_UC_RCRESET, ch_action_txiniterr },
1800 {CH_STATE_TXINIT, CH_EVENT_UC_RSRESET, ch_action_txiniterr },
1801 {CH_STATE_TXINIT, CH_EVENT_TIMER, ch_action_txiniterr },
1802 {CH_STATE_TXINIT, CH_EVENT_IO_ENODEV, ch_action_iofatal },
1803 {CH_STATE_TXINIT, CH_EVENT_IO_EIO, ch_action_reinit },
1804 {CH_STATE_TXINIT, CH_EVENT_MC_FAIL, ch_action_fail },
1806 {CH_STATE_TXIDLE, CH_EVENT_STOP, ch_action_haltio },
1807 {CH_STATE_TXIDLE, CH_EVENT_START, fsm_action_nop },
1808 {CH_STATE_TXIDLE, CH_EVENT_FINSTAT, ch_action_firstio },
1809 {CH_STATE_TXIDLE, CH_EVENT_UC_RCRESET, fsm_action_nop },
1810 {CH_STATE_TXIDLE, CH_EVENT_UC_RSRESET, fsm_action_nop },
1811 {CH_STATE_TXIDLE, CH_EVENT_IO_ENODEV, ch_action_iofatal },
1812 {CH_STATE_TXIDLE, CH_EVENT_IO_EIO, ch_action_reinit },
1813 {CH_STATE_TXIDLE, CH_EVENT_MC_FAIL, ch_action_fail },
1815 {CH_STATE_TERM, CH_EVENT_STOP, fsm_action_nop },
1816 {CH_STATE_TERM, CH_EVENT_START, ch_action_restart },
1817 {CH_STATE_TERM, CH_EVENT_FINSTAT, ch_action_stopped },
1818 {CH_STATE_TERM, CH_EVENT_UC_RCRESET, fsm_action_nop },
1819 {CH_STATE_TERM, CH_EVENT_UC_RSRESET, fsm_action_nop },
1820 {CH_STATE_TERM, CH_EVENT_MC_FAIL, ch_action_fail },
1822 {CH_STATE_DTERM, CH_EVENT_STOP, ch_action_haltio },
1823 {CH_STATE_DTERM, CH_EVENT_START, ch_action_restart },
1824 {CH_STATE_DTERM, CH_EVENT_FINSTAT, ch_action_setmode },
1825 {CH_STATE_DTERM, CH_EVENT_UC_RCRESET, fsm_action_nop },
1826 {CH_STATE_DTERM, CH_EVENT_UC_RSRESET, fsm_action_nop },
1827 {CH_STATE_DTERM, CH_EVENT_MC_FAIL, ch_action_fail },
1829 {CH_STATE_TX, CH_EVENT_STOP, ch_action_haltio },
1830 {CH_STATE_TX, CH_EVENT_START, fsm_action_nop },
1831 {CH_STATE_TX, CH_EVENT_FINSTAT, ch_action_txdone },
1832 {CH_STATE_TX, CH_EVENT_UC_RCRESET, ch_action_txretry },
1833 {CH_STATE_TX, CH_EVENT_UC_RSRESET, ch_action_txretry },
1834 {CH_STATE_TX, CH_EVENT_TIMER, ch_action_txretry },
1835 {CH_STATE_TX, CH_EVENT_IO_ENODEV, ch_action_iofatal },
1836 {CH_STATE_TX, CH_EVENT_IO_EIO, ch_action_reinit },
1837 {CH_STATE_TX, CH_EVENT_MC_FAIL, ch_action_fail },
1839 {CH_STATE_RXERR, CH_EVENT_STOP, ch_action_haltio },
1840 {CH_STATE_TXERR, CH_EVENT_STOP, ch_action_haltio },
1841 {CH_STATE_TXERR, CH_EVENT_MC_FAIL, ch_action_fail },
1842 {CH_STATE_RXERR, CH_EVENT_MC_FAIL, ch_action_fail },
1845 static const int CH_FSM_LEN = sizeof (ch_fsm) / sizeof (fsm_node);
1848 * Functions related to setup and device detection.
1849 *****************************************************************************/
1851 static inline int
1852 less_than(char *id1, char *id2)
1854 int dev1, dev2, i;
1856 for (i = 0; i < 5; i++) {
1857 id1++;
1858 id2++;
1860 dev1 = simple_strtoul(id1, &id1, 16);
1861 dev2 = simple_strtoul(id2, &id2, 16);
1863 return (dev1 < dev2);
1867 * Add a new channel to the list of channels.
1868 * Keeps the channel list sorted.
1870 * @param cdev The ccw_device to be added.
1871 * @param type The type class of the new channel.
1873 * @return 0 on success, !0 on error.
1875 static int
1876 add_channel(struct ccw_device *cdev, enum channel_types type)
1878 struct channel **c = &channels;
1879 struct channel *ch;
1881 DBF_TEXT(trace, 2, __FUNCTION__);
1882 if ((ch =
1883 (struct channel *) kmalloc(sizeof (struct channel),
1884 GFP_KERNEL)) == NULL) {
1885 ctc_pr_warn("ctc: Out of memory in add_channel\n");
1886 return -1;
1888 memset(ch, 0, sizeof (struct channel));
1889 if ((ch->ccw = (struct ccw1 *) kmalloc(sizeof (struct ccw1) * 8,
1890 GFP_KERNEL | GFP_DMA)) == NULL) {
1891 kfree(ch);
1892 ctc_pr_warn("ctc: Out of memory in add_channel\n");
1893 return -1;
1897 * "static" ccws are used in the following way:
1899 * ccw[0..2] (Channel program for generic I/O):
1900 * 0: prepare
1901 * 1: read or write (depending on direction) with fixed
1902 * buffer (idal allocated once when buffer is allocated)
1903 * 2: nop
1904 * ccw[3..5] (Channel program for direct write of packets)
1905 * 3: prepare
1906 * 4: write (idal allocated on every write).
1907 * 5: nop
1908 * ccw[6..7] (Channel program for initial channel setup):
1909 * 3: set extended mode
1910 * 4: nop
1912 * ch->ccw[0..5] are initialized in ch_action_start because
1913 * the channel's direction is yet unknown here.
1915 ch->ccw[6].cmd_code = CCW_CMD_SET_EXTENDED;
1916 ch->ccw[6].flags = CCW_FLAG_SLI;
1917 ch->ccw[6].count = 0;
1918 ch->ccw[6].cda = 0;
1920 ch->ccw[7].cmd_code = CCW_CMD_NOOP;
1921 ch->ccw[7].flags = CCW_FLAG_SLI;
1922 ch->ccw[7].count = 0;
1923 ch->ccw[7].cda = 0;
1925 ch->cdev = cdev;
1926 snprintf(ch->id, CTC_ID_SIZE, "ch-%s", cdev->dev.bus_id);
1927 ch->type = type;
1928 loglevel = CTC_LOGLEVEL_DEFAULT;
1929 ch->fsm = init_fsm(ch->id, ch_state_names,
1930 ch_event_names, NR_CH_STATES, NR_CH_EVENTS,
1931 ch_fsm, CH_FSM_LEN, GFP_KERNEL);
1932 if (ch->fsm == NULL) {
1933 ctc_pr_warn("ctc: Could not create FSM in add_channel\n");
1934 kfree(ch);
1935 return -1;
1937 fsm_newstate(ch->fsm, CH_STATE_IDLE);
1938 if ((ch->irb = (struct irb *) kmalloc(sizeof (struct irb),
1939 GFP_KERNEL)) == NULL) {
1940 ctc_pr_warn("ctc: Out of memory in add_channel\n");
1941 kfree_fsm(ch->fsm);
1942 kfree(ch);
1943 return -1;
1945 memset(ch->irb, 0, sizeof (struct irb));
1946 while (*c && less_than((*c)->id, ch->id))
1947 c = &(*c)->next;
1948 if (!strncmp((*c)->id, ch->id, CTC_ID_SIZE)) {
1949 ctc_pr_debug(
1950 "ctc: add_channel: device %s already in list, "
1951 "using old entry\n", (*c)->id);
1952 kfree(ch->irb);
1953 kfree_fsm(ch->fsm);
1954 kfree(ch);
1955 return 0;
1957 fsm_settimer(ch->fsm, &ch->timer);
1958 skb_queue_head_init(&ch->io_queue);
1959 skb_queue_head_init(&ch->collect_queue);
1960 ch->next = *c;
1961 *c = ch;
1962 return 0;
1966 * Release a specific channel in the channel list.
1968 * @param ch Pointer to channel struct to be released.
1970 static void
1971 channel_free(struct channel *ch)
1973 ch->flags &= ~CHANNEL_FLAGS_INUSE;
1974 fsm_newstate(ch->fsm, CH_STATE_IDLE);
1978 * Remove a specific channel in the channel list.
1980 * @param ch Pointer to channel struct to be released.
1982 static void
1983 channel_remove(struct channel *ch)
1985 struct channel **c = &channels;
1987 DBF_TEXT(trace, 2, __FUNCTION__);
1988 if (ch == NULL)
1989 return;
1991 channel_free(ch);
1992 while (*c) {
1993 if (*c == ch) {
1994 *c = ch->next;
1995 fsm_deltimer(&ch->timer);
1996 kfree_fsm(ch->fsm);
1997 clear_normalized_cda(&ch->ccw[4]);
1998 if (ch->trans_skb != NULL) {
1999 clear_normalized_cda(&ch->ccw[1]);
2000 dev_kfree_skb(ch->trans_skb);
2002 kfree(ch->ccw);
2003 return;
2005 c = &((*c)->next);
2010 * Get a specific channel from the channel list.
2012 * @param type Type of channel we are interested in.
2013 * @param id Id of channel we are interested in.
2014 * @param direction Direction we want to use this channel for.
2016 * @return Pointer to a channel or NULL if no matching channel available.
2018 static struct channel
2020 channel_get(enum channel_types type, char *id, int direction)
2022 struct channel *ch = channels;
2024 DBF_TEXT(trace, 3, __FUNCTION__);
2025 #ifdef DEBUG
2026 ctc_pr_debug("ctc: %s(): searching for ch with id %s and type %d\n",
2027 __func__, id, type);
2028 #endif
2030 while (ch && ((strncmp(ch->id, id, CTC_ID_SIZE)) || (ch->type != type))) {
2031 #ifdef DEBUG
2032 ctc_pr_debug("ctc: %s(): ch=0x%p (id=%s, type=%d\n",
2033 __func__, ch, ch->id, ch->type);
2034 #endif
2035 ch = ch->next;
2037 #ifdef DEBUG
2038 ctc_pr_debug("ctc: %s(): ch=0x%pq (id=%s, type=%d\n",
2039 __func__, ch, ch->id, ch->type);
2040 #endif
2041 if (!ch) {
2042 ctc_pr_warn("ctc: %s(): channel with id %s "
2043 "and type %d not found in channel list\n",
2044 __func__, id, type);
2045 } else {
2046 if (ch->flags & CHANNEL_FLAGS_INUSE)
2047 ch = NULL;
2048 else {
2049 ch->flags |= CHANNEL_FLAGS_INUSE;
2050 ch->flags &= ~CHANNEL_FLAGS_RWMASK;
2051 ch->flags |= (direction == WRITE)
2052 ? CHANNEL_FLAGS_WRITE : CHANNEL_FLAGS_READ;
2053 fsm_newstate(ch->fsm, CH_STATE_STOPPED);
2056 return ch;
2060 * Return the channel type by name.
2062 * @param name Name of network interface.
2064 * @return Type class of channel to be used for that interface.
2066 static enum channel_types inline
2067 extract_channel_media(char *name)
2069 enum channel_types ret = channel_type_unknown;
2071 if (name != NULL) {
2072 if (strncmp(name, "ctc", 3) == 0)
2073 ret = channel_type_parallel;
2074 if (strncmp(name, "escon", 5) == 0)
2075 ret = channel_type_escon;
2077 return ret;
2080 static long
2081 __ctc_check_irb_error(struct ccw_device *cdev, struct irb *irb)
2083 if (!IS_ERR(irb))
2084 return 0;
2086 switch (PTR_ERR(irb)) {
2087 case -EIO:
2088 ctc_pr_warn("i/o-error on device %s\n", cdev->dev.bus_id);
2089 // CTC_DBF_TEXT(trace, 2, "ckirberr");
2090 // CTC_DBF_TEXT_(trace, 2, " rc%d", -EIO);
2091 break;
2092 case -ETIMEDOUT:
2093 ctc_pr_warn("timeout on device %s\n", cdev->dev.bus_id);
2094 // CTC_DBF_TEXT(trace, 2, "ckirberr");
2095 // CTC_DBF_TEXT_(trace, 2, " rc%d", -ETIMEDOUT);
2096 break;
2097 default:
2098 ctc_pr_warn("unknown error %ld on device %s\n", PTR_ERR(irb),
2099 cdev->dev.bus_id);
2100 // CTC_DBF_TEXT(trace, 2, "ckirberr");
2101 // CTC_DBF_TEXT(trace, 2, " rc???");
2103 return PTR_ERR(irb);
2107 * Main IRQ handler.
2109 * @param cdev The ccw_device the interrupt is for.
2110 * @param intparm interruption parameter.
2111 * @param irb interruption response block.
2113 static void
2114 ctc_irq_handler(struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
2116 struct channel *ch;
2117 struct net_device *dev;
2118 struct ctc_priv *priv;
2120 DBF_TEXT(trace, 5, __FUNCTION__);
2121 if (__ctc_check_irb_error(cdev, irb))
2122 return;
2124 /* Check for unsolicited interrupts. */
2125 if (!cdev->dev.driver_data) {
2126 ctc_pr_warn("ctc: Got unsolicited irq: %s c-%02x d-%02x\n",
2127 cdev->dev.bus_id, irb->scsw.cstat,
2128 irb->scsw.dstat);
2129 return;
2132 priv = ((struct ccwgroup_device *)cdev->dev.driver_data)
2133 ->dev.driver_data;
2135 /* Try to extract channel from driver data. */
2136 if (priv->channel[READ]->cdev == cdev)
2137 ch = priv->channel[READ];
2138 else if (priv->channel[WRITE]->cdev == cdev)
2139 ch = priv->channel[WRITE];
2140 else {
2141 ctc_pr_err("ctc: Can't determine channel for interrupt, "
2142 "device %s\n", cdev->dev.bus_id);
2143 return;
2146 dev = (struct net_device *) (ch->netdev);
2147 if (dev == NULL) {
2148 ctc_pr_crit("ctc: ctc_irq_handler dev=NULL bus_id=%s, ch=0x%p\n",
2149 cdev->dev.bus_id, ch);
2150 return;
2153 #ifdef DEBUG
2154 ctc_pr_debug("%s: interrupt for device: %s received c-%02x d-%02x\n",
2155 dev->name, ch->id, irb->scsw.cstat, irb->scsw.dstat);
2156 #endif
2158 /* Copy interruption response block. */
2159 memcpy(ch->irb, irb, sizeof(struct irb));
2161 /* Check for good subchannel return code, otherwise error message */
2162 if (ch->irb->scsw.cstat) {
2163 fsm_event(ch->fsm, CH_EVENT_SC_UNKNOWN, ch);
2164 ctc_pr_warn("%s: subchannel check for device: %s - %02x %02x\n",
2165 dev->name, ch->id, ch->irb->scsw.cstat,
2166 ch->irb->scsw.dstat);
2167 return;
2170 /* Check the reason-code of a unit check */
2171 if (ch->irb->scsw.dstat & DEV_STAT_UNIT_CHECK) {
2172 ccw_unit_check(ch, ch->irb->ecw[0]);
2173 return;
2175 if (ch->irb->scsw.dstat & DEV_STAT_BUSY) {
2176 if (ch->irb->scsw.dstat & DEV_STAT_ATTENTION)
2177 fsm_event(ch->fsm, CH_EVENT_ATTNBUSY, ch);
2178 else
2179 fsm_event(ch->fsm, CH_EVENT_BUSY, ch);
2180 return;
2182 if (ch->irb->scsw.dstat & DEV_STAT_ATTENTION) {
2183 fsm_event(ch->fsm, CH_EVENT_ATTN, ch);
2184 return;
2186 if ((ch->irb->scsw.stctl & SCSW_STCTL_SEC_STATUS) ||
2187 (ch->irb->scsw.stctl == SCSW_STCTL_STATUS_PEND) ||
2188 (ch->irb->scsw.stctl ==
2189 (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND)))
2190 fsm_event(ch->fsm, CH_EVENT_FINSTAT, ch);
2191 else
2192 fsm_event(ch->fsm, CH_EVENT_IRQ, ch);
2197 * Actions for interface - statemachine.
2198 *****************************************************************************/
2201 * Startup channels by sending CH_EVENT_START to each channel.
2203 * @param fi An instance of an interface statemachine.
2204 * @param event The event, just happened.
2205 * @param arg Generic pointer, casted from struct net_device * upon call.
2207 static void
2208 dev_action_start(fsm_instance * fi, int event, void *arg)
2210 struct net_device *dev = (struct net_device *) arg;
2211 struct ctc_priv *privptr = dev->priv;
2212 int direction;
2214 DBF_TEXT(setup, 3, __FUNCTION__);
2215 fsm_deltimer(&privptr->restart_timer);
2216 fsm_newstate(fi, DEV_STATE_STARTWAIT_RXTX);
2217 for (direction = READ; direction <= WRITE; direction++) {
2218 struct channel *ch = privptr->channel[direction];
2219 fsm_event(ch->fsm, CH_EVENT_START, ch);
2224 * Shutdown channels by sending CH_EVENT_STOP to each channel.
2226 * @param fi An instance of an interface statemachine.
2227 * @param event The event, just happened.
2228 * @param arg Generic pointer, casted from struct net_device * upon call.
2230 static void
2231 dev_action_stop(fsm_instance * fi, int event, void *arg)
2233 struct net_device *dev = (struct net_device *) arg;
2234 struct ctc_priv *privptr = dev->priv;
2235 int direction;
2237 DBF_TEXT(trace, 3, __FUNCTION__);
2238 fsm_newstate(fi, DEV_STATE_STOPWAIT_RXTX);
2239 for (direction = READ; direction <= WRITE; direction++) {
2240 struct channel *ch = privptr->channel[direction];
2241 fsm_event(ch->fsm, CH_EVENT_STOP, ch);
2244 static void
2245 dev_action_restart(fsm_instance *fi, int event, void *arg)
2247 struct net_device *dev = (struct net_device *)arg;
2248 struct ctc_priv *privptr = dev->priv;
2250 DBF_TEXT(trace, 3, __FUNCTION__);
2251 ctc_pr_debug("%s: Restarting\n", dev->name);
2252 dev_action_stop(fi, event, arg);
2253 fsm_event(privptr->fsm, DEV_EVENT_STOP, dev);
2254 fsm_addtimer(&privptr->restart_timer, CTC_TIMEOUT_5SEC,
2255 DEV_EVENT_START, dev);
2259 * Called from channel statemachine
2260 * when a channel is up and running.
2262 * @param fi An instance of an interface statemachine.
2263 * @param event The event, just happened.
2264 * @param arg Generic pointer, casted from struct net_device * upon call.
2266 static void
2267 dev_action_chup(fsm_instance * fi, int event, void *arg)
2269 struct net_device *dev = (struct net_device *) arg;
2270 struct ctc_priv *privptr = dev->priv;
2272 DBF_TEXT(trace, 3, __FUNCTION__);
2273 switch (fsm_getstate(fi)) {
2274 case DEV_STATE_STARTWAIT_RXTX:
2275 if (event == DEV_EVENT_RXUP)
2276 fsm_newstate(fi, DEV_STATE_STARTWAIT_TX);
2277 else
2278 fsm_newstate(fi, DEV_STATE_STARTWAIT_RX);
2279 break;
2280 case DEV_STATE_STARTWAIT_RX:
2281 if (event == DEV_EVENT_RXUP) {
2282 fsm_newstate(fi, DEV_STATE_RUNNING);
2283 ctc_pr_info("%s: connected with remote side\n",
2284 dev->name);
2285 if (privptr->protocol == CTC_PROTO_LINUX_TTY)
2286 ctc_tty_setcarrier(dev, 1);
2287 ctc_clear_busy(dev);
2289 break;
2290 case DEV_STATE_STARTWAIT_TX:
2291 if (event == DEV_EVENT_TXUP) {
2292 fsm_newstate(fi, DEV_STATE_RUNNING);
2293 ctc_pr_info("%s: connected with remote side\n",
2294 dev->name);
2295 if (privptr->protocol == CTC_PROTO_LINUX_TTY)
2296 ctc_tty_setcarrier(dev, 1);
2297 ctc_clear_busy(dev);
2299 break;
2300 case DEV_STATE_STOPWAIT_TX:
2301 if (event == DEV_EVENT_RXUP)
2302 fsm_newstate(fi, DEV_STATE_STOPWAIT_RXTX);
2303 break;
2304 case DEV_STATE_STOPWAIT_RX:
2305 if (event == DEV_EVENT_TXUP)
2306 fsm_newstate(fi, DEV_STATE_STOPWAIT_RXTX);
2307 break;
2312 * Called from channel statemachine
2313 * when a channel has been shutdown.
2315 * @param fi An instance of an interface statemachine.
2316 * @param event The event, just happened.
2317 * @param arg Generic pointer, casted from struct net_device * upon call.
2319 static void
2320 dev_action_chdown(fsm_instance * fi, int event, void *arg)
2322 struct net_device *dev = (struct net_device *) arg;
2323 struct ctc_priv *privptr = dev->priv;
2325 DBF_TEXT(trace, 3, __FUNCTION__);
2326 switch (fsm_getstate(fi)) {
2327 case DEV_STATE_RUNNING:
2328 if (privptr->protocol == CTC_PROTO_LINUX_TTY)
2329 ctc_tty_setcarrier(dev, 0);
2330 if (event == DEV_EVENT_TXDOWN)
2331 fsm_newstate(fi, DEV_STATE_STARTWAIT_TX);
2332 else
2333 fsm_newstate(fi, DEV_STATE_STARTWAIT_RX);
2334 break;
2335 case DEV_STATE_STARTWAIT_RX:
2336 if (event == DEV_EVENT_TXDOWN)
2337 fsm_newstate(fi, DEV_STATE_STARTWAIT_RXTX);
2338 break;
2339 case DEV_STATE_STARTWAIT_TX:
2340 if (event == DEV_EVENT_RXDOWN)
2341 fsm_newstate(fi, DEV_STATE_STARTWAIT_RXTX);
2342 break;
2343 case DEV_STATE_STOPWAIT_RXTX:
2344 if (event == DEV_EVENT_TXDOWN)
2345 fsm_newstate(fi, DEV_STATE_STOPWAIT_RX);
2346 else
2347 fsm_newstate(fi, DEV_STATE_STOPWAIT_TX);
2348 break;
2349 case DEV_STATE_STOPWAIT_RX:
2350 if (event == DEV_EVENT_RXDOWN)
2351 fsm_newstate(fi, DEV_STATE_STOPPED);
2352 break;
2353 case DEV_STATE_STOPWAIT_TX:
2354 if (event == DEV_EVENT_TXDOWN)
2355 fsm_newstate(fi, DEV_STATE_STOPPED);
2356 break;
2360 static const fsm_node dev_fsm[] = {
2361 {DEV_STATE_STOPPED, DEV_EVENT_START, dev_action_start},
2363 {DEV_STATE_STOPWAIT_RXTX, DEV_EVENT_START, dev_action_start },
2364 {DEV_STATE_STOPWAIT_RXTX, DEV_EVENT_RXDOWN, dev_action_chdown },
2365 {DEV_STATE_STOPWAIT_RXTX, DEV_EVENT_TXDOWN, dev_action_chdown },
2366 {DEV_STATE_STOPWAIT_RXTX, DEV_EVENT_RESTART, dev_action_restart },
2368 {DEV_STATE_STOPWAIT_RX, DEV_EVENT_START, dev_action_start },
2369 {DEV_STATE_STOPWAIT_RX, DEV_EVENT_RXUP, dev_action_chup },
2370 {DEV_STATE_STOPWAIT_RX, DEV_EVENT_TXUP, dev_action_chup },
2371 {DEV_STATE_STOPWAIT_RX, DEV_EVENT_RXDOWN, dev_action_chdown },
2372 {DEV_STATE_STOPWAIT_RX, DEV_EVENT_RESTART, dev_action_restart },
2374 {DEV_STATE_STOPWAIT_TX, DEV_EVENT_START, dev_action_start },
2375 {DEV_STATE_STOPWAIT_TX, DEV_EVENT_RXUP, dev_action_chup },
2376 {DEV_STATE_STOPWAIT_TX, DEV_EVENT_TXUP, dev_action_chup },
2377 {DEV_STATE_STOPWAIT_TX, DEV_EVENT_TXDOWN, dev_action_chdown },
2378 {DEV_STATE_STOPWAIT_TX, DEV_EVENT_RESTART, dev_action_restart },
2380 {DEV_STATE_STARTWAIT_RXTX, DEV_EVENT_STOP, dev_action_stop },
2381 {DEV_STATE_STARTWAIT_RXTX, DEV_EVENT_RXUP, dev_action_chup },
2382 {DEV_STATE_STARTWAIT_RXTX, DEV_EVENT_TXUP, dev_action_chup },
2383 {DEV_STATE_STARTWAIT_RXTX, DEV_EVENT_RXDOWN, dev_action_chdown },
2384 {DEV_STATE_STARTWAIT_RXTX, DEV_EVENT_TXDOWN, dev_action_chdown },
2385 {DEV_STATE_STARTWAIT_RXTX, DEV_EVENT_RESTART, dev_action_restart },
2387 {DEV_STATE_STARTWAIT_TX, DEV_EVENT_STOP, dev_action_stop },
2388 {DEV_STATE_STARTWAIT_TX, DEV_EVENT_RXUP, dev_action_chup },
2389 {DEV_STATE_STARTWAIT_TX, DEV_EVENT_TXUP, dev_action_chup },
2390 {DEV_STATE_STARTWAIT_TX, DEV_EVENT_RXDOWN, dev_action_chdown },
2391 {DEV_STATE_STARTWAIT_TX, DEV_EVENT_RESTART, dev_action_restart },
2393 {DEV_STATE_STARTWAIT_RX, DEV_EVENT_STOP, dev_action_stop },
2394 {DEV_STATE_STARTWAIT_RX, DEV_EVENT_RXUP, dev_action_chup },
2395 {DEV_STATE_STARTWAIT_RX, DEV_EVENT_TXUP, dev_action_chup },
2396 {DEV_STATE_STARTWAIT_RX, DEV_EVENT_TXDOWN, dev_action_chdown },
2397 {DEV_STATE_STARTWAIT_RX, DEV_EVENT_RESTART, dev_action_restart },
2399 {DEV_STATE_RUNNING, DEV_EVENT_STOP, dev_action_stop },
2400 {DEV_STATE_RUNNING, DEV_EVENT_RXDOWN, dev_action_chdown },
2401 {DEV_STATE_RUNNING, DEV_EVENT_TXDOWN, dev_action_chdown },
2402 {DEV_STATE_RUNNING, DEV_EVENT_TXUP, fsm_action_nop },
2403 {DEV_STATE_RUNNING, DEV_EVENT_RXUP, fsm_action_nop },
2404 {DEV_STATE_RUNNING, DEV_EVENT_RESTART, dev_action_restart },
2407 static const int DEV_FSM_LEN = sizeof (dev_fsm) / sizeof (fsm_node);
2410 * Transmit a packet.
2411 * This is a helper function for ctc_tx().
2413 * @param ch Channel to be used for sending.
2414 * @param skb Pointer to struct sk_buff of packet to send.
2415 * The linklevel header has already been set up
2416 * by ctc_tx().
2418 * @return 0 on success, -ERRNO on failure. (Never fails.)
2420 static int
2421 transmit_skb(struct channel *ch, struct sk_buff *skb)
2423 unsigned long saveflags;
2424 struct ll_header header;
2425 int rc = 0;
2427 DBF_TEXT(trace, 5, __FUNCTION__);
2428 if (fsm_getstate(ch->fsm) != CH_STATE_TXIDLE) {
2429 int l = skb->len + LL_HEADER_LENGTH;
2431 spin_lock_irqsave(&ch->collect_lock, saveflags);
2432 if (ch->collect_len + l > ch->max_bufsize - 2)
2433 rc = -EBUSY;
2434 else {
2435 atomic_inc(&skb->users);
2436 header.length = l;
2437 header.type = skb->protocol;
2438 header.unused = 0;
2439 memcpy(skb_push(skb, LL_HEADER_LENGTH), &header,
2440 LL_HEADER_LENGTH);
2441 skb_queue_tail(&ch->collect_queue, skb);
2442 ch->collect_len += l;
2444 spin_unlock_irqrestore(&ch->collect_lock, saveflags);
2445 } else {
2446 __u16 block_len;
2447 int ccw_idx;
2448 struct sk_buff *nskb;
2449 unsigned long hi;
2452 * Protect skb against beeing free'd by upper
2453 * layers.
2455 atomic_inc(&skb->users);
2456 ch->prof.txlen += skb->len;
2457 header.length = skb->len + LL_HEADER_LENGTH;
2458 header.type = skb->protocol;
2459 header.unused = 0;
2460 memcpy(skb_push(skb, LL_HEADER_LENGTH), &header,
2461 LL_HEADER_LENGTH);
2462 block_len = skb->len + 2;
2463 *((__u16 *) skb_push(skb, 2)) = block_len;
2466 * IDAL support in CTC is broken, so we have to
2467 * care about skb's above 2G ourselves.
2469 hi = ((unsigned long) skb->tail + LL_HEADER_LENGTH) >> 31;
2470 if (hi) {
2471 nskb = alloc_skb(skb->len, GFP_ATOMIC | GFP_DMA);
2472 if (!nskb) {
2473 atomic_dec(&skb->users);
2474 skb_pull(skb, LL_HEADER_LENGTH + 2);
2475 return -ENOMEM;
2476 } else {
2477 memcpy(skb_put(nskb, skb->len),
2478 skb->data, skb->len);
2479 atomic_inc(&nskb->users);
2480 atomic_dec(&skb->users);
2481 dev_kfree_skb_irq(skb);
2482 skb = nskb;
2486 ch->ccw[4].count = block_len;
2487 if (set_normalized_cda(&ch->ccw[4], skb->data)) {
2489 * idal allocation failed, try via copying to
2490 * trans_skb. trans_skb usually has a pre-allocated
2491 * idal.
2493 if (ctc_checkalloc_buffer(ch, 1)) {
2495 * Remove our header. It gets added
2496 * again on retransmit.
2498 atomic_dec(&skb->users);
2499 skb_pull(skb, LL_HEADER_LENGTH + 2);
2500 return -EBUSY;
2503 ch->trans_skb->tail = ch->trans_skb->data;
2504 ch->trans_skb->len = 0;
2505 ch->ccw[1].count = skb->len;
2506 memcpy(skb_put(ch->trans_skb, skb->len), skb->data,
2507 skb->len);
2508 atomic_dec(&skb->users);
2509 dev_kfree_skb_irq(skb);
2510 ccw_idx = 0;
2511 } else {
2512 skb_queue_tail(&ch->io_queue, skb);
2513 ccw_idx = 3;
2515 ch->retry = 0;
2516 fsm_newstate(ch->fsm, CH_STATE_TX);
2517 fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC, CH_EVENT_TIMER, ch);
2518 spin_lock_irqsave(get_ccwdev_lock(ch->cdev), saveflags);
2519 ch->prof.send_stamp = xtime;
2520 rc = ccw_device_start(ch->cdev, &ch->ccw[ccw_idx],
2521 (unsigned long) ch, 0xff, 0);
2522 spin_unlock_irqrestore(get_ccwdev_lock(ch->cdev), saveflags);
2523 if (ccw_idx == 3)
2524 ch->prof.doios_single++;
2525 if (rc != 0) {
2526 fsm_deltimer(&ch->timer);
2527 ccw_check_return_code(ch, rc, "single skb TX");
2528 if (ccw_idx == 3)
2529 skb_dequeue_tail(&ch->io_queue);
2531 * Remove our header. It gets added
2532 * again on retransmit.
2534 skb_pull(skb, LL_HEADER_LENGTH + 2);
2535 } else {
2536 if (ccw_idx == 0) {
2537 struct net_device *dev = ch->netdev;
2538 struct ctc_priv *privptr = dev->priv;
2539 privptr->stats.tx_packets++;
2540 privptr->stats.tx_bytes +=
2541 skb->len - LL_HEADER_LENGTH;
2546 return rc;
2550 * Interface API for upper network layers
2551 *****************************************************************************/
2554 * Open an interface.
2555 * Called from generic network layer when ifconfig up is run.
2557 * @param dev Pointer to interface struct.
2559 * @return 0 on success, -ERRNO on failure. (Never fails.)
2561 static int
2562 ctc_open(struct net_device * dev)
2564 DBF_TEXT(trace, 5, __FUNCTION__);
2565 fsm_event(((struct ctc_priv *) dev->priv)->fsm, DEV_EVENT_START, dev);
2566 return 0;
2570 * Close an interface.
2571 * Called from generic network layer when ifconfig down is run.
2573 * @param dev Pointer to interface struct.
2575 * @return 0 on success, -ERRNO on failure. (Never fails.)
2577 static int
2578 ctc_close(struct net_device * dev)
2580 DBF_TEXT(trace, 5, __FUNCTION__);
2581 fsm_event(((struct ctc_priv *) dev->priv)->fsm, DEV_EVENT_STOP, dev);
2582 return 0;
2586 * Start transmission of a packet.
2587 * Called from generic network device layer.
2589 * @param skb Pointer to buffer containing the packet.
2590 * @param dev Pointer to interface struct.
2592 * @return 0 if packet consumed, !0 if packet rejected.
2593 * Note: If we return !0, then the packet is free'd by
2594 * the generic network layer.
2596 static int
2597 ctc_tx(struct sk_buff *skb, struct net_device * dev)
2599 int rc = 0;
2600 struct ctc_priv *privptr = (struct ctc_priv *) dev->priv;
2602 DBF_TEXT(trace, 5, __FUNCTION__);
2604 * Some sanity checks ...
2606 if (skb == NULL) {
2607 ctc_pr_warn("%s: NULL sk_buff passed\n", dev->name);
2608 privptr->stats.tx_dropped++;
2609 return 0;
2611 if (skb_headroom(skb) < (LL_HEADER_LENGTH + 2)) {
2612 ctc_pr_warn("%s: Got sk_buff with head room < %ld bytes\n",
2613 dev->name, LL_HEADER_LENGTH + 2);
2614 dev_kfree_skb(skb);
2615 privptr->stats.tx_dropped++;
2616 return 0;
2620 * If channels are not running, try to restart them
2621 * and throw away packet.
2623 if (fsm_getstate(privptr->fsm) != DEV_STATE_RUNNING) {
2624 fsm_event(privptr->fsm, DEV_EVENT_START, dev);
2625 if (privptr->protocol == CTC_PROTO_LINUX_TTY)
2626 return -EBUSY;
2627 dev_kfree_skb(skb);
2628 privptr->stats.tx_dropped++;
2629 privptr->stats.tx_errors++;
2630 privptr->stats.tx_carrier_errors++;
2631 return 0;
2634 if (ctc_test_and_set_busy(dev))
2635 return -EBUSY;
2637 dev->trans_start = jiffies;
2638 if (transmit_skb(privptr->channel[WRITE], skb) != 0)
2639 rc = 1;
2640 ctc_clear_busy(dev);
2641 return rc;
2645 * Sets MTU of an interface.
2647 * @param dev Pointer to interface struct.
2648 * @param new_mtu The new MTU to use for this interface.
2650 * @return 0 on success, -EINVAL if MTU is out of valid range.
2651 * (valid range is 576 .. 65527). If VM is on the
2652 * remote side, maximum MTU is 32760, however this is
2653 * <em>not</em> checked here.
2655 static int
2656 ctc_change_mtu(struct net_device * dev, int new_mtu)
2658 struct ctc_priv *privptr = (struct ctc_priv *) dev->priv;
2660 DBF_TEXT(trace, 3, __FUNCTION__);
2661 if ((new_mtu < 576) || (new_mtu > 65527) ||
2662 (new_mtu > (privptr->channel[READ]->max_bufsize -
2663 LL_HEADER_LENGTH - 2)))
2664 return -EINVAL;
2665 dev->mtu = new_mtu;
2666 dev->hard_header_len = LL_HEADER_LENGTH + 2;
2667 return 0;
2671 * Returns interface statistics of a device.
2673 * @param dev Pointer to interface struct.
2675 * @return Pointer to stats struct of this interface.
2677 static struct net_device_stats *
2678 ctc_stats(struct net_device * dev)
2680 return &((struct ctc_priv *) dev->priv)->stats;
2684 * sysfs attributes
2686 static ssize_t
2687 buffer_show(struct device *dev, char *buf)
2689 struct ctc_priv *priv;
2691 priv = dev->driver_data;
2692 if (!priv)
2693 return -ENODEV;
2694 return sprintf(buf, "%d\n",
2695 priv->channel[READ]->max_bufsize);
2698 static ssize_t
2699 buffer_write(struct device *dev, const char *buf, size_t count)
2701 struct ctc_priv *priv;
2702 struct net_device *ndev;
2703 int bs1;
2705 DBF_TEXT(trace, 5, __FUNCTION__);
2706 priv = dev->driver_data;
2707 if (!priv)
2708 return -ENODEV;
2709 ndev = priv->channel[READ]->netdev;
2710 if (!ndev)
2711 return -ENODEV;
2712 sscanf(buf, "%u", &bs1);
2714 if (bs1 > CTC_BUFSIZE_LIMIT)
2715 return -EINVAL;
2716 if ((ndev->flags & IFF_RUNNING) &&
2717 (bs1 < (ndev->mtu + LL_HEADER_LENGTH + 2)))
2718 return -EINVAL;
2719 if (bs1 < (576 + LL_HEADER_LENGTH + 2))
2720 return -EINVAL;
2722 priv->channel[READ]->max_bufsize =
2723 priv->channel[WRITE]->max_bufsize = bs1;
2724 if (!(ndev->flags & IFF_RUNNING))
2725 ndev->mtu = bs1 - LL_HEADER_LENGTH - 2;
2726 priv->channel[READ]->flags |= CHANNEL_FLAGS_BUFSIZE_CHANGED;
2727 priv->channel[WRITE]->flags |= CHANNEL_FLAGS_BUFSIZE_CHANGED;
2729 return count;
2733 static ssize_t
2734 loglevel_show(struct device *dev, char *buf)
2736 struct ctc_priv *priv;
2738 priv = dev->driver_data;
2739 if (!priv)
2740 return -ENODEV;
2741 return sprintf(buf, "%d\n", loglevel);
2744 static ssize_t
2745 loglevel_write(struct device *dev, const char *buf, size_t count)
2747 struct ctc_priv *priv;
2748 int ll1;
2750 DBF_TEXT(trace, 5, __FUNCTION__);
2751 priv = dev->driver_data;
2752 if (!priv)
2753 return -ENODEV;
2754 sscanf(buf, "%i", &ll1);
2756 if ((ll1 > CTC_LOGLEVEL_MAX) || (ll1 < 0))
2757 return -EINVAL;
2758 loglevel = ll1;
2759 return count;
2762 static void
2763 ctc_print_statistics(struct ctc_priv *priv)
2765 char *sbuf;
2766 char *p;
2768 DBF_TEXT(trace, 4, __FUNCTION__);
2769 if (!priv)
2770 return;
2771 sbuf = (char *)kmalloc(2048, GFP_KERNEL);
2772 if (sbuf == NULL)
2773 return;
2774 p = sbuf;
2776 p += sprintf(p, " Device FSM state: %s\n",
2777 fsm_getstate_str(priv->fsm));
2778 p += sprintf(p, " RX channel FSM state: %s\n",
2779 fsm_getstate_str(priv->channel[READ]->fsm));
2780 p += sprintf(p, " TX channel FSM state: %s\n",
2781 fsm_getstate_str(priv->channel[WRITE]->fsm));
2782 p += sprintf(p, " Max. TX buffer used: %ld\n",
2783 priv->channel[WRITE]->prof.maxmulti);
2784 p += sprintf(p, " Max. chained SKBs: %ld\n",
2785 priv->channel[WRITE]->prof.maxcqueue);
2786 p += sprintf(p, " TX single write ops: %ld\n",
2787 priv->channel[WRITE]->prof.doios_single);
2788 p += sprintf(p, " TX multi write ops: %ld\n",
2789 priv->channel[WRITE]->prof.doios_multi);
2790 p += sprintf(p, " Netto bytes written: %ld\n",
2791 priv->channel[WRITE]->prof.txlen);
2792 p += sprintf(p, " Max. TX IO-time: %ld\n",
2793 priv->channel[WRITE]->prof.tx_time);
2795 ctc_pr_debug("Statistics for %s:\n%s",
2796 priv->channel[WRITE]->netdev->name, sbuf);
2797 kfree(sbuf);
2798 return;
2801 static ssize_t
2802 stats_show(struct device *dev, char *buf)
2804 struct ctc_priv *priv = dev->driver_data;
2805 if (!priv)
2806 return -ENODEV;
2807 ctc_print_statistics(priv);
2808 return sprintf(buf, "0\n");
2811 static ssize_t
2812 stats_write(struct device *dev, const char *buf, size_t count)
2814 struct ctc_priv *priv = dev->driver_data;
2815 if (!priv)
2816 return -ENODEV;
2817 /* Reset statistics */
2818 memset(&priv->channel[WRITE]->prof, 0,
2819 sizeof(priv->channel[WRITE]->prof));
2820 return count;
2823 static DEVICE_ATTR(buffer, 0644, buffer_show, buffer_write);
2824 static DEVICE_ATTR(loglevel, 0644, loglevel_show, loglevel_write);
2825 static DEVICE_ATTR(stats, 0644, stats_show, stats_write);
2827 static int
2828 ctc_add_attributes(struct device *dev)
2830 device_create_file(dev, &dev_attr_buffer);
2831 device_create_file(dev, &dev_attr_loglevel);
2832 device_create_file(dev, &dev_attr_stats);
2833 return 0;
2836 static void
2837 ctc_remove_attributes(struct device *dev)
2839 device_remove_file(dev, &dev_attr_stats);
2840 device_remove_file(dev, &dev_attr_loglevel);
2841 device_remove_file(dev, &dev_attr_buffer);
2845 static void
2846 ctc_netdev_unregister(struct net_device * dev)
2848 struct ctc_priv *privptr;
2850 if (!dev)
2851 return;
2852 privptr = (struct ctc_priv *) dev->priv;
2853 if (privptr->protocol != CTC_PROTO_LINUX_TTY)
2854 unregister_netdev(dev);
2855 else
2856 ctc_tty_unregister_netdev(dev);
2859 static int
2860 ctc_netdev_register(struct net_device * dev)
2862 struct ctc_priv *privptr = (struct ctc_priv *) dev->priv;
2863 if (privptr->protocol != CTC_PROTO_LINUX_TTY)
2864 return register_netdev(dev);
2865 else
2866 return ctc_tty_register_netdev(dev);
2869 static void
2870 ctc_free_netdevice(struct net_device * dev, int free_dev)
2872 struct ctc_priv *privptr;
2873 if (!dev)
2874 return;
2875 privptr = dev->priv;
2876 if (privptr) {
2877 if (privptr->fsm)
2878 kfree_fsm(privptr->fsm);
2879 kfree(privptr);
2881 #ifdef MODULE
2882 if (free_dev)
2883 free_netdev(dev);
2884 #endif
2888 * Initialize everything of the net device except the name and the
2889 * channel structs.
2891 static struct net_device *
2892 ctc_init_netdevice(struct net_device * dev, int alloc_device,
2893 struct ctc_priv *privptr)
2895 if (!privptr)
2896 return NULL;
2898 DBF_TEXT(setup, 3, __FUNCTION__);
2899 if (alloc_device) {
2900 dev = kmalloc(sizeof (struct net_device), GFP_KERNEL);
2901 if (!dev)
2902 return NULL;
2903 memset(dev, 0, sizeof (struct net_device));
2906 dev->priv = privptr;
2907 privptr->fsm = init_fsm("ctcdev", dev_state_names,
2908 dev_event_names, NR_DEV_STATES, NR_DEV_EVENTS,
2909 dev_fsm, DEV_FSM_LEN, GFP_KERNEL);
2910 if (privptr->fsm == NULL) {
2911 if (alloc_device)
2912 kfree(dev);
2913 return NULL;
2915 fsm_newstate(privptr->fsm, DEV_STATE_STOPPED);
2916 fsm_settimer(privptr->fsm, &privptr->restart_timer);
2917 dev->mtu = CTC_BUFSIZE_DEFAULT - LL_HEADER_LENGTH - 2;
2918 dev->hard_start_xmit = ctc_tx;
2919 dev->open = ctc_open;
2920 dev->stop = ctc_close;
2921 dev->get_stats = ctc_stats;
2922 dev->change_mtu = ctc_change_mtu;
2923 dev->hard_header_len = LL_HEADER_LENGTH + 2;
2924 dev->addr_len = 0;
2925 dev->type = ARPHRD_SLIP;
2926 dev->tx_queue_len = 100;
2927 dev->flags = IFF_POINTOPOINT | IFF_NOARP;
2928 SET_MODULE_OWNER(dev);
2929 return dev;
2932 static ssize_t
2933 ctc_proto_show(struct device *dev, char *buf)
2935 struct ctc_priv *priv;
2937 priv = dev->driver_data;
2938 if (!priv)
2939 return -ENODEV;
2941 return sprintf(buf, "%d\n", priv->protocol);
2944 static ssize_t
2945 ctc_proto_store(struct device *dev, const char *buf, size_t count)
2947 struct ctc_priv *priv;
2948 int value;
2950 DBF_TEXT(trace, 3, __FUNCTION__);
2951 pr_debug("%s() called\n", __FUNCTION__);
2953 priv = dev->driver_data;
2954 if (!priv)
2955 return -ENODEV;
2956 sscanf(buf, "%u", &value);
2957 if ((value < 0) || (value > CTC_PROTO_MAX))
2958 return -EINVAL;
2959 priv->protocol = value;
2961 return count;
2964 static DEVICE_ATTR(protocol, 0644, ctc_proto_show, ctc_proto_store);
2966 static ssize_t
2967 ctc_type_show(struct device *dev, char *buf)
2969 struct ccwgroup_device *cgdev;
2971 cgdev = to_ccwgroupdev(dev);
2972 if (!cgdev)
2973 return -ENODEV;
2975 return sprintf(buf, "%s\n", cu3088_type[cgdev->cdev[0]->id.driver_info]);
2978 static DEVICE_ATTR(type, 0444, ctc_type_show, NULL);
2980 static struct attribute *ctc_attr[] = {
2981 &dev_attr_protocol.attr,
2982 &dev_attr_type.attr,
2983 NULL,
2986 static struct attribute_group ctc_attr_group = {
2987 .attrs = ctc_attr,
2990 static int
2991 ctc_add_files(struct device *dev)
2993 pr_debug("%s() called\n", __FUNCTION__);
2995 return sysfs_create_group(&dev->kobj, &ctc_attr_group);
2998 static void
2999 ctc_remove_files(struct device *dev)
3001 pr_debug("%s() called\n", __FUNCTION__);
3003 sysfs_remove_group(&dev->kobj, &ctc_attr_group);
3007 * Add ctc specific attributes.
3008 * Add ctc private data.
3010 * @param cgdev pointer to ccwgroup_device just added
3012 * @returns 0 on success, !0 on failure.
3015 static int
3016 ctc_probe_device(struct ccwgroup_device *cgdev)
3018 struct ctc_priv *priv;
3019 int rc;
3021 pr_debug("%s() called\n", __FUNCTION__);
3022 DBF_TEXT(trace, 3, __FUNCTION__);
3024 if (!get_device(&cgdev->dev))
3025 return -ENODEV;
3027 priv = kmalloc(sizeof (struct ctc_priv), GFP_KERNEL);
3028 if (!priv) {
3029 ctc_pr_err("%s: Out of memory\n", __func__);
3030 put_device(&cgdev->dev);
3031 return -ENOMEM;
3034 memset(priv, 0, sizeof (struct ctc_priv));
3035 rc = ctc_add_files(&cgdev->dev);
3036 if (rc) {
3037 kfree(priv);
3038 put_device(&cgdev->dev);
3039 return rc;
3042 cgdev->cdev[0]->handler = ctc_irq_handler;
3043 cgdev->cdev[1]->handler = ctc_irq_handler;
3044 cgdev->dev.driver_data = priv;
3046 return 0;
3051 * Setup an interface.
3053 * @param cgdev Device to be setup.
3055 * @returns 0 on success, !0 on failure.
3057 static int
3058 ctc_new_device(struct ccwgroup_device *cgdev)
3060 char read_id[CTC_ID_SIZE];
3061 char write_id[CTC_ID_SIZE];
3062 int direction;
3063 enum channel_types type;
3064 struct ctc_priv *privptr;
3065 struct net_device *dev;
3066 int ret;
3068 pr_debug("%s() called\n", __FUNCTION__);
3069 DBF_TEXT(setup, 3, __FUNCTION__);
3071 privptr = cgdev->dev.driver_data;
3072 if (!privptr)
3073 return -ENODEV;
3075 type = get_channel_type(&cgdev->cdev[0]->id);
3077 snprintf(read_id, CTC_ID_SIZE, "ch-%s", cgdev->cdev[0]->dev.bus_id);
3078 snprintf(write_id, CTC_ID_SIZE, "ch-%s", cgdev->cdev[1]->dev.bus_id);
3080 if (add_channel(cgdev->cdev[0], type))
3081 return -ENOMEM;
3082 if (add_channel(cgdev->cdev[1], type))
3083 return -ENOMEM;
3085 ret = ccw_device_set_online(cgdev->cdev[0]);
3086 if (ret != 0) {
3087 printk(KERN_WARNING
3088 "ccw_device_set_online (cdev[0]) failed with ret = %d\n", ret);
3091 ret = ccw_device_set_online(cgdev->cdev[1]);
3092 if (ret != 0) {
3093 printk(KERN_WARNING
3094 "ccw_device_set_online (cdev[1]) failed with ret = %d\n", ret);
3097 dev = ctc_init_netdevice(NULL, 1, privptr);
3099 if (!dev) {
3100 ctc_pr_warn("ctc_init_netdevice failed\n");
3101 goto out;
3104 if (privptr->protocol == CTC_PROTO_LINUX_TTY)
3105 strlcpy(dev->name, "ctctty%d", IFNAMSIZ);
3106 else
3107 strlcpy(dev->name, "ctc%d", IFNAMSIZ);
3109 for (direction = READ; direction <= WRITE; direction++) {
3110 privptr->channel[direction] =
3111 channel_get(type, direction == READ ? read_id : write_id,
3112 direction);
3113 if (privptr->channel[direction] == NULL) {
3114 if (direction == WRITE)
3115 channel_free(privptr->channel[READ]);
3117 ctc_free_netdevice(dev, 1);
3118 goto out;
3120 privptr->channel[direction]->netdev = dev;
3121 privptr->channel[direction]->protocol = privptr->protocol;
3122 privptr->channel[direction]->max_bufsize = CTC_BUFSIZE_DEFAULT;
3124 /* sysfs magic */
3125 SET_NETDEV_DEV(dev, &cgdev->dev);
3127 if (ctc_netdev_register(dev) != 0) {
3128 ctc_free_netdevice(dev, 1);
3129 goto out;
3132 ctc_add_attributes(&cgdev->dev);
3134 strlcpy(privptr->fsm->name, dev->name, sizeof (privptr->fsm->name));
3136 print_banner();
3138 ctc_pr_info("%s: read: %s, write: %s, proto: %d\n",
3139 dev->name, privptr->channel[READ]->id,
3140 privptr->channel[WRITE]->id, privptr->protocol);
3142 return 0;
3143 out:
3144 ccw_device_set_offline(cgdev->cdev[1]);
3145 ccw_device_set_offline(cgdev->cdev[0]);
3147 return -ENODEV;
3151 * Shutdown an interface.
3153 * @param cgdev Device to be shut down.
3155 * @returns 0 on success, !0 on failure.
3157 static int
3158 ctc_shutdown_device(struct ccwgroup_device *cgdev)
3160 struct ctc_priv *priv;
3161 struct net_device *ndev;
3163 DBF_TEXT(trace, 3, __FUNCTION__);
3164 pr_debug("%s() called\n", __FUNCTION__);
3166 priv = cgdev->dev.driver_data;
3167 ndev = NULL;
3168 if (!priv)
3169 return -ENODEV;
3171 if (priv->channel[READ]) {
3172 ndev = priv->channel[READ]->netdev;
3174 /* Close the device */
3175 ctc_close(ndev);
3176 ndev->flags &=~IFF_RUNNING;
3178 ctc_remove_attributes(&cgdev->dev);
3180 channel_free(priv->channel[READ]);
3182 if (priv->channel[WRITE])
3183 channel_free(priv->channel[WRITE]);
3185 if (ndev) {
3186 ctc_netdev_unregister(ndev);
3187 ndev->priv = NULL;
3188 ctc_free_netdevice(ndev, 1);
3191 if (priv->fsm)
3192 kfree_fsm(priv->fsm);
3194 ccw_device_set_offline(cgdev->cdev[1]);
3195 ccw_device_set_offline(cgdev->cdev[0]);
3197 if (priv->channel[READ])
3198 channel_remove(priv->channel[READ]);
3199 if (priv->channel[WRITE])
3200 channel_remove(priv->channel[WRITE]);
3202 priv->channel[READ] = priv->channel[WRITE] = NULL;
3204 return 0;
3208 static void
3209 ctc_remove_device(struct ccwgroup_device *cgdev)
3211 struct ctc_priv *priv;
3213 pr_debug("%s() called\n", __FUNCTION__);
3214 DBF_TEXT(trace, 3, __FUNCTION__);
3216 priv = cgdev->dev.driver_data;
3217 if (!priv)
3218 return;
3219 if (cgdev->state == CCWGROUP_ONLINE)
3220 ctc_shutdown_device(cgdev);
3221 ctc_remove_files(&cgdev->dev);
3222 cgdev->dev.driver_data = NULL;
3223 kfree(priv);
3224 put_device(&cgdev->dev);
3227 static struct ccwgroup_driver ctc_group_driver = {
3228 .owner = THIS_MODULE,
3229 .name = "ctc",
3230 .max_slaves = 2,
3231 .driver_id = 0xC3E3C3,
3232 .probe = ctc_probe_device,
3233 .remove = ctc_remove_device,
3234 .set_online = ctc_new_device,
3235 .set_offline = ctc_shutdown_device,
3239 * Module related routines
3240 *****************************************************************************/
3243 * Prepare to be unloaded. Free IRQ's and release all resources.
3244 * This is called just before this module is unloaded. It is
3245 * <em>not</em> called, if the usage count is !0, so we don't need to check
3246 * for that.
3248 static void __exit
3249 ctc_exit(void)
3251 unregister_cu3088_discipline(&ctc_group_driver);
3252 ctc_tty_cleanup();
3253 ctc_unregister_dbf_views();
3254 ctc_pr_info("CTC driver unloaded\n");
3258 * Initialize module.
3259 * This is called just after the module is loaded.
3261 * @return 0 on success, !0 on error.
3263 static int __init
3264 ctc_init(void)
3266 int ret = 0;
3268 print_banner();
3270 ret = ctc_register_dbf_views();
3271 if (ret){
3272 ctc_pr_crit("ctc_init failed with ctc_register_dbf_views rc = %d\n", ret);
3273 return ret;
3275 ctc_tty_init();
3276 ret = register_cu3088_discipline(&ctc_group_driver);
3277 if (ret) {
3278 ctc_tty_cleanup();
3279 ctc_unregister_dbf_views();
3281 return ret;
3284 module_init(ctc_init);
3285 module_exit(ctc_exit);
3287 /* --- This is the END my friend --- */