Merge with Linux 2.5.59.
[linux-2.6/linux-mips.git] / drivers / s390 / net / ctcmain.c
blob2926373c2f896a01d872b20631f5e3f692960988
1 /*
2 * $Id: ctcmain.c,v 1.30 2002/12/09 13:56:20 aberg 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.30 $
43 #undef DEBUG
45 #include <linux/version.h>
46 #include <linux/module.h>
47 #include <linux/init.h>
48 #include <linux/kernel.h>
49 #include <linux/slab.h>
50 #include <linux/errno.h>
51 #include <linux/types.h>
52 #include <linux/interrupt.h>
53 #include <linux/timer.h>
54 #include <linux/sched.h>
56 #include <linux/signal.h>
57 #include <linux/string.h>
59 #include <linux/ip.h>
60 #include <linux/if_arp.h>
61 #include <linux/tcp.h>
62 #include <linux/skbuff.h>
63 #include <linux/ctype.h>
64 #include <net/dst.h>
66 #include <asm/io.h>
67 #include <asm/ccwdev.h>
68 #include <asm/ccwgroup.h>
69 #include <asm/bitops.h>
70 #include <asm/uaccess.h>
72 #include <asm/idals.h>
74 #include "ctctty.h"
75 #include "fsm.h"
76 #include "cu3088.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 DEVICE_ID_SIZE+3
109 * If running on 64 bit, this must be changed. XXX Why? (bird)
111 typedef unsigned long intparm_t;
113 struct ctc_profile {
114 unsigned long maxmulti;
115 unsigned long maxcqueue;
116 unsigned long doios_single;
117 unsigned long doios_multi;
118 unsigned long txlen;
119 unsigned long tx_time;
120 struct timespec send_stamp;
124 * Definition of one channel
126 struct channel {
129 * Pointer to next channel in list.
131 struct channel *next;
132 char id[CTC_ID_SIZE];
133 struct ccw_device *cdev;
136 * Type of this channel.
137 * CTC/A or Escon for valid channels.
139 enum channel_types type;
142 * Misc. flags. See CHANNEL_FLAGS_... below
144 __u32 flags;
147 * The protocol of this channel
149 __u16 protocol;
152 * I/O and irq related stuff
154 struct ccw1 *ccw;
155 struct irb *irb;
158 * RX/TX buffer size
160 int max_bufsize;
163 * Transmit/Receive buffer.
165 struct sk_buff *trans_skb;
168 * Universal I/O queue.
170 struct sk_buff_head io_queue;
173 * TX queue for collecting skb's during busy.
175 struct sk_buff_head collect_queue;
178 * Amount of data in collect_queue.
180 int collect_len;
183 * spinlock for collect_queue and collect_len
185 spinlock_t collect_lock;
188 * Timer for detecting unresposive
189 * I/O operations.
191 fsm_timer timer;
194 * Retry counter for misc. operations.
196 int retry;
199 * The finite state machine of this channel
201 fsm_instance *fsm;
204 * The corresponding net_device this channel
205 * belongs to.
207 struct net_device *netdev;
209 struct ctc_profile prof;
211 unsigned char *trans_skb_data;
214 #define CHANNEL_FLAGS_READ 0
215 #define CHANNEL_FLAGS_WRITE 1
216 #define CHANNEL_FLAGS_INUSE 2
217 #define CHANNEL_FLAGS_BUFSIZE_CHANGED 4
218 #define CHANNEL_FLAGS_RWMASK 1
219 #define CHANNEL_DIRECTION(f) (f & CHANNEL_FLAGS_RWMASK)
222 * Linked list of all detected channels.
224 static struct channel *channels = NULL;
226 struct ctc_priv {
227 struct net_device_stats stats;
228 unsigned long tbusy;
230 * The finite state machine of this interface.
232 fsm_instance *fsm;
234 * The protocol of this device
236 __u16 protocol;
238 * Timer for restarting after I/O Errors
240 fsm_timer restart_timer;
242 struct channel *channel[2];
246 * Definition of our link level header.
248 struct ll_header {
249 __u16 length;
250 __u16 type;
251 __u16 unused;
253 #define LL_HEADER_LENGTH (sizeof(struct ll_header))
256 * Compatibility macros for busy handling
257 * of network devices.
259 static __inline__ void
260 ctc_clear_busy(struct net_device * dev)
262 clear_bit(0, &(((struct ctc_priv *) dev->priv)->tbusy));
263 netif_wake_queue(dev);
266 static __inline__ int
267 ctc_test_and_set_busy(struct net_device * dev)
269 netif_stop_queue(dev);
270 return test_and_set_bit(0, &((struct ctc_priv *) dev->priv)->tbusy);
273 #define SET_DEVICE_START(device, value)
276 * Print Banner.
278 static void
279 print_banner(void)
281 static int printed = 0;
282 char vbuf[] = "$Revision: 1.30 $";
283 char *version = vbuf;
285 if (printed)
286 return;
287 if ((version = strchr(version, ':'))) {
288 char *p = strchr(version + 1, '$');
289 if (p)
290 *p = '\0';
291 } else
292 version = " ??? ";
293 printk(KERN_INFO "CTC driver Version%s"
294 #ifdef DEBUG
295 " (DEBUG-VERSION, " __DATE__ __TIME__ ")"
296 #endif
297 " initialized\n", version);
298 printed = 1;
302 * Return type of a detected device.
304 static enum channel_types
305 get_channel_type(struct ccw_device_id *id)
307 enum channel_types type = (enum channel_types) id->driver_info;
309 if (type == channel_type_ficon)
310 type = channel_type_escon;
312 return type;
316 * States of the interface statemachine.
318 enum dev_states {
319 DEV_STATE_STOPPED,
320 DEV_STATE_STARTWAIT_RXTX,
321 DEV_STATE_STARTWAIT_RX,
322 DEV_STATE_STARTWAIT_TX,
323 DEV_STATE_STOPWAIT_RXTX,
324 DEV_STATE_STOPWAIT_RX,
325 DEV_STATE_STOPWAIT_TX,
326 DEV_STATE_RUNNING,
328 * MUST be always the last element!!
330 NR_DEV_STATES
333 static const char *dev_state_names[] = {
334 "Stopped",
335 "StartWait RXTX",
336 "StartWait RX",
337 "StartWait TX",
338 "StopWait RXTX",
339 "StopWait RX",
340 "StopWait TX",
341 "Running",
345 * Events of the interface statemachine.
347 enum dev_events {
348 DEV_EVENT_START,
349 DEV_EVENT_STOP,
350 DEV_EVENT_RXUP,
351 DEV_EVENT_TXUP,
352 DEV_EVENT_RXDOWN,
353 DEV_EVENT_TXDOWN,
354 DEV_EVENT_RESTART,
356 * MUST be always the last element!!
358 NR_DEV_EVENTS
361 static const char *dev_event_names[] = {
362 "Start",
363 "Stop",
364 "RX up",
365 "TX up",
366 "RX down",
367 "TX down",
368 "Restart",
372 * Events of the channel statemachine
374 enum ch_events {
376 * Events, representing return code of
377 * I/O operations (ccw_device_start, ccw_device_halt et al.)
379 CH_EVENT_IO_SUCCESS,
380 CH_EVENT_IO_EBUSY,
381 CH_EVENT_IO_ENODEV,
382 CH_EVENT_IO_EIO,
383 CH_EVENT_IO_UNKNOWN,
385 CH_EVENT_ATTNBUSY,
386 CH_EVENT_ATTN,
387 CH_EVENT_BUSY,
390 * Events, representing unit-check
392 CH_EVENT_UC_RCRESET,
393 CH_EVENT_UC_RSRESET,
394 CH_EVENT_UC_TXTIMEOUT,
395 CH_EVENT_UC_TXPARITY,
396 CH_EVENT_UC_HWFAIL,
397 CH_EVENT_UC_RXPARITY,
398 CH_EVENT_UC_ZERO,
399 CH_EVENT_UC_UNKNOWN,
402 * Events, representing subchannel-check
404 CH_EVENT_SC_UNKNOWN,
407 * Events, representing machine checks
409 CH_EVENT_MC_FAIL,
410 CH_EVENT_MC_GOOD,
413 * Event, representing normal IRQ
415 CH_EVENT_IRQ,
416 CH_EVENT_FINSTAT,
419 * Event, representing timer expiry.
421 CH_EVENT_TIMER,
424 * Events, representing commands from upper levels.
426 CH_EVENT_START,
427 CH_EVENT_STOP,
430 * MUST be always the last element!!
432 NR_CH_EVENTS,
435 static const char *ch_event_names[] = {
436 "do_IO success",
437 "do_IO busy",
438 "do_IO enodev",
439 "do_IO ioerr",
440 "do_IO unknown",
442 "Status ATTN & BUSY",
443 "Status ATTN",
444 "Status BUSY",
446 "Unit check remote reset",
447 "Unit check remote system reset",
448 "Unit check TX timeout",
449 "Unit check TX parity",
450 "Unit check Hardware failure",
451 "Unit check RX parity",
452 "Unit check ZERO",
453 "Unit check Unknown",
455 "SubChannel check Unknown",
457 "Machine check failure",
458 "Machine check operational",
460 "IRQ normal",
461 "IRQ final",
463 "Timer",
465 "Start",
466 "Stop",
470 * States of the channel statemachine.
472 enum ch_states {
474 * Channel not assigned to any device,
475 * initial state, direction invalid
477 CH_STATE_IDLE,
480 * Channel assigned but not operating
482 CH_STATE_STOPPED,
483 CH_STATE_STARTWAIT,
484 CH_STATE_STARTRETRY,
485 CH_STATE_SETUPWAIT,
486 CH_STATE_RXINIT,
487 CH_STATE_TXINIT,
488 CH_STATE_RX,
489 CH_STATE_TX,
490 CH_STATE_RXIDLE,
491 CH_STATE_TXIDLE,
492 CH_STATE_RXERR,
493 CH_STATE_TXERR,
494 CH_STATE_TERM,
495 CH_STATE_DTERM,
496 CH_STATE_NOTOP,
499 * MUST be always the last element!!
501 NR_CH_STATES,
504 static const char *ch_state_names[] = {
505 "Idle",
506 "Stopped",
507 "StartWait",
508 "StartRetry",
509 "SetupWait",
510 "RX init",
511 "TX init",
512 "RX",
513 "TX",
514 "RX idle",
515 "TX idle",
516 "RX error",
517 "TX error",
518 "Terminating",
519 "Restarting",
520 "Not operational",
523 #ifdef DEBUG
525 * Dump header and first 16 bytes of an sk_buff for debugging purposes.
527 * @param skb The sk_buff to dump.
528 * @param offset Offset relative to skb-data, where to start the dump.
530 static void
531 ctc_dump_skb(struct sk_buff *skb, int offset)
533 unsigned char *p = skb->data;
534 __u16 bl;
535 struct ll_header *header;
536 int i;
538 p += offset;
539 bl = *((__u16 *) p);
540 p += 2;
541 header = (struct ll_header *) p;
542 p -= 2;
544 printk(KERN_DEBUG "dump:\n");
545 printk(KERN_DEBUG "blocklen=%d %04x\n", bl, bl);
547 printk(KERN_DEBUG "h->length=%d %04x\n", header->length,
548 header->length);
549 printk(KERN_DEBUG "h->type=%04x\n", header->type);
550 printk(KERN_DEBUG "h->unused=%04x\n", header->unused);
551 if (bl > 16)
552 bl = 16;
553 printk(KERN_DEBUG "data: ");
554 for (i = 0; i < bl; i++)
555 printk("%02x%s", *p++, (i % 16) ? " " : "\n<7>");
556 printk("\n");
558 #else
559 static inline void
560 ctc_dump_skb(struct sk_buff *skb, int offset)
563 #endif
566 * Unpack a just received skb and hand it over to
567 * upper layers.
569 * @param ch The channel where this skb has been received.
570 * @param pskb The received skb.
572 static __inline__ void
573 ctc_unpack_skb(struct channel *ch, struct sk_buff *pskb)
575 struct net_device *dev = ch->netdev;
576 struct ctc_priv *privptr = (struct ctc_priv *) dev->priv;
578 __u16 len = *((__u16 *) pskb->data);
579 skb_put(pskb, 2 + LL_HEADER_LENGTH);
580 skb_pull(pskb, 2);
581 pskb->dev = dev;
582 pskb->ip_summed = CHECKSUM_UNNECESSARY;
583 while (len > 0) {
584 struct sk_buff *skb;
585 struct ll_header *header = (struct ll_header *) pskb->data;
587 skb_pull(pskb, LL_HEADER_LENGTH);
588 if ((ch->protocol == CTC_PROTO_S390) &&
589 (header->type != ETH_P_IP)) {
591 * Check packet type only if we stick strictly
592 * to S/390's protocol of OS390. This only
593 * supports IP. Otherwise allow any packet
594 * type.
596 printk(KERN_WARNING
597 "%s Illegal packet type 0x%04x "
598 "received, dropping\n", dev->name, header->type);
599 ctc_dump_skb(pskb, -6);
600 privptr->stats.rx_dropped++;
601 privptr->stats.rx_frame_errors++;
602 return;
604 pskb->protocol = ntohs(header->type);
605 header->length -= LL_HEADER_LENGTH;
606 if ((header->length == 0) ||
607 (header->length > skb_tailroom(pskb)) ||
608 (header->length > len)) {
609 printk(KERN_WARNING
610 "%s Illegal packet size %d "
611 "received (MTU=%d blocklen=%d), "
612 "dropping\n", dev->name, header->length,
613 dev->mtu, len);
614 ctc_dump_skb(pskb, -6);
615 privptr->stats.rx_dropped++;
616 privptr->stats.rx_length_errors++;
617 return;
619 if (header->length > skb_tailroom(pskb)) {
620 printk(KERN_WARNING
621 "%s Illegal packet size %d "
622 "(beyond the end of received data), "
623 "dropping\n", dev->name, header->length);
624 ctc_dump_skb(pskb, -6);
625 privptr->stats.rx_dropped++;
626 privptr->stats.rx_length_errors++;
627 return;
629 skb_put(pskb, header->length);
630 pskb->mac.raw = pskb->data;
631 len -= (LL_HEADER_LENGTH + header->length);
632 skb = dev_alloc_skb(pskb->len);
633 if (!skb) {
634 printk(KERN_WARNING
635 "%s Out of memory in ctc_unpack_skb\n",
636 dev->name);
637 privptr->stats.rx_dropped++;
638 return;
640 memcpy(skb_put(skb, pskb->len), pskb->data, pskb->len);
641 skb->mac.raw = skb->data;
642 skb->dev = pskb->dev;
643 skb->protocol = pskb->protocol;
644 pskb->ip_summed = CHECKSUM_UNNECESSARY;
645 if (ch->protocol == CTC_PROTO_LINUX_TTY)
646 ctc_tty_netif_rx(skb);
647 else
648 netif_rx(skb);
649 dev->last_rx = jiffies;
650 privptr->stats.rx_packets++;
651 privptr->stats.rx_bytes += skb->len;
652 if (len > 0) {
653 skb_pull(pskb, header->length);
654 skb_put(pskb, LL_HEADER_LENGTH);
660 * Check return code of a preceeding do_IO, halt_IO etc...
662 * @param ch The channel, the error belongs to.
663 * @param return_code The error code to inspect.
665 static void inline
666 ccw_check_return_code(struct channel *ch, int return_code)
668 switch (return_code) {
669 case 0:
670 fsm_event(ch->fsm, CH_EVENT_IO_SUCCESS, ch);
671 break;
672 case -EBUSY:
673 printk(KERN_INFO "%s: Busy !\n", ch->id);
674 fsm_event(ch->fsm, CH_EVENT_IO_EBUSY, ch);
675 break;
676 case -ENODEV:
677 printk(KERN_EMERG
678 "%s: Invalid device called for IO\n", ch->id);
679 fsm_event(ch->fsm, CH_EVENT_IO_ENODEV, ch);
680 break;
681 case -EIO:
682 printk(KERN_EMERG "%s: Status pending... \n", ch->id);
683 fsm_event(ch->fsm, CH_EVENT_IO_EIO, ch);
684 break;
685 default:
686 printk(KERN_EMERG
687 "%s: Unknown error in do_IO %04x\n",
688 ch->id, return_code);
689 fsm_event(ch->fsm, CH_EVENT_IO_UNKNOWN, ch);
694 * Check sense of a unit check.
696 * @param ch The channel, the sense code belongs to.
697 * @param sense The sense code to inspect.
699 static void inline
700 ccw_unit_check(struct channel *ch, unsigned char sense)
702 if (sense & SNS0_INTERVENTION_REQ) {
703 if (sense & 0x01) {
704 if (ch->protocol != CTC_PROTO_LINUX_TTY)
705 printk(KERN_DEBUG
706 "%s: Interface disc. or Sel. reset "
707 "(remote)\n", ch->id);
708 fsm_event(ch->fsm, CH_EVENT_UC_RCRESET, ch);
709 } else {
710 printk(KERN_DEBUG "%s: System reset (remote)\n",
711 ch->id);
712 fsm_event(ch->fsm, CH_EVENT_UC_RSRESET, ch);
714 } else if (sense & SNS0_EQUIPMENT_CHECK) {
715 if (sense & SNS0_BUS_OUT_CHECK) {
716 printk(KERN_WARNING
717 "%s: Hardware malfunction (remote)\n",
718 ch->id);
719 fsm_event(ch->fsm, CH_EVENT_UC_HWFAIL, ch);
720 } else {
721 printk(KERN_WARNING
722 "%s: Read-data parity error (remote)\n",
723 ch->id);
724 fsm_event(ch->fsm, CH_EVENT_UC_RXPARITY, ch);
726 } else if (sense & SNS0_BUS_OUT_CHECK) {
727 if (sense & 0x04) {
728 printk(KERN_WARNING
729 "%s: Data-streaming timeout)\n", ch->id);
730 fsm_event(ch->fsm, CH_EVENT_UC_TXTIMEOUT, ch);
731 } else {
732 printk(KERN_WARNING
733 "%s: Data-transfer parity error\n",
734 ch->id);
735 fsm_event(ch->fsm, CH_EVENT_UC_TXPARITY, ch);
737 } else if (sense & SNS0_CMD_REJECT) {
738 printk(KERN_WARNING "%s: Command reject\n", ch->id);
739 } else if (sense == 0) {
740 printk(KERN_DEBUG "%s: Unit check ZERO\n", ch->id);
741 fsm_event(ch->fsm, CH_EVENT_UC_ZERO, ch);
742 } else {
743 printk(KERN_WARNING
744 "%s: Unit Check with sense code: %02x\n",
745 ch->id, sense);
746 fsm_event(ch->fsm, CH_EVENT_UC_UNKNOWN, ch);
750 static void
751 ctc_purge_skb_queue(struct sk_buff_head *q)
753 struct sk_buff *skb;
755 while ((skb = skb_dequeue(q))) {
756 atomic_dec(&skb->users);
757 dev_kfree_skb_irq(skb);
761 static __inline__ int
762 ctc_checkalloc_buffer(struct channel *ch, int warn)
764 if ((ch->trans_skb == NULL) ||
765 (ch->flags & CHANNEL_FLAGS_BUFSIZE_CHANGED)) {
766 if (ch->trans_skb != NULL)
767 dev_kfree_skb(ch->trans_skb);
768 clear_normalized_cda(&ch->ccw[1]);
769 ch->trans_skb = __dev_alloc_skb(ch->max_bufsize,
770 GFP_ATOMIC | GFP_DMA);
771 if (ch->trans_skb == NULL) {
772 if (warn)
773 printk(KERN_WARNING
774 "%s: Couldn't alloc %s trans_skb\n",
775 ch->id,
776 (CHANNEL_DIRECTION(ch->flags) == READ) ?
777 "RX" : "TX");
778 return -ENOMEM;
780 ch->ccw[1].count = ch->max_bufsize;
781 if (set_normalized_cda(&ch->ccw[1], ch->trans_skb->data)) {
782 dev_kfree_skb(ch->trans_skb);
783 ch->trans_skb = NULL;
784 if (warn)
785 printk(KERN_WARNING
786 "%s: set_normalized_cda for %s "
787 "trans_skb failed, dropping packets\n",
788 ch->id,
789 (CHANNEL_DIRECTION(ch->flags) == READ) ?
790 "RX" : "TX");
791 return -ENOMEM;
793 ch->ccw[1].count = 0;
794 ch->trans_skb_data = ch->trans_skb->data;
795 ch->flags &= ~CHANNEL_FLAGS_BUFSIZE_CHANGED;
797 return 0;
801 * Dummy NOP action for statemachines
803 static void
804 fsm_action_nop(fsm_instance * fi, int event, void *arg)
809 * Actions for channel - statemachines.
810 *****************************************************************************/
813 * Normal data has been send. Free the corresponding
814 * skb (it's in io_queue), reset dev->tbusy and
815 * revert to idle state.
817 * @param fi An instance of a channel statemachine.
818 * @param event The event, just happened.
819 * @param arg Generic pointer, casted from channel * upon call.
821 static void
822 ch_action_txdone(fsm_instance * fi, int event, void *arg)
824 struct channel *ch = (struct channel *) arg;
825 struct net_device *dev = ch->netdev;
826 struct ctc_priv *privptr = dev->priv;
827 struct sk_buff *skb;
828 int first = 1;
829 int i;
831 struct timespec done_stamp = xtime;
832 unsigned long duration =
833 (done_stamp.tv_sec - ch->prof.send_stamp.tv_sec) * 1000000 +
834 (done_stamp.tv_nsec - ch->prof.send_stamp.tv_nsec) / 1000;
835 if (duration > ch->prof.tx_time)
836 ch->prof.tx_time = duration;
838 if (ch->irb->scsw.count != 0)
839 printk(KERN_DEBUG "%s: TX not complete, remaining %d bytes\n",
840 dev->name, ch->irb->scsw.count);
842 fsm_deltimer(&ch->timer);
843 while ((skb = skb_dequeue(&ch->io_queue))) {
844 privptr->stats.tx_packets++;
845 privptr->stats.tx_bytes += skb->len - LL_HEADER_LENGTH;
846 if (first) {
847 privptr->stats.tx_bytes += 2;
848 first = 0;
850 atomic_dec(&skb->users);
851 dev_kfree_skb_irq(skb);
853 spin_lock(&ch->collect_lock);
854 clear_normalized_cda(&ch->ccw[4]);
855 if (ch->collect_len > 0) {
856 int rc;
858 if (ctc_checkalloc_buffer(ch, 1)) {
859 spin_unlock(&ch->collect_lock);
860 return;
862 ch->trans_skb->tail = ch->trans_skb->data = ch->trans_skb_data;
863 ch->trans_skb->len = 0;
864 if (ch->prof.maxmulti < (ch->collect_len + 2))
865 ch->prof.maxmulti = ch->collect_len + 2;
866 if (ch->prof.maxcqueue < skb_queue_len(&ch->collect_queue))
867 ch->prof.maxcqueue = skb_queue_len(&ch->collect_queue);
868 *((__u16 *) skb_put(ch->trans_skb, 2)) = ch->collect_len + 2;
869 i = 0;
870 while ((skb = skb_dequeue(&ch->collect_queue))) {
871 memcpy(skb_put(ch->trans_skb, skb->len), skb->data,
872 skb->len);
873 privptr->stats.tx_packets++;
874 privptr->stats.tx_bytes += skb->len - LL_HEADER_LENGTH;
875 atomic_dec(&skb->users);
876 dev_kfree_skb_irq(skb);
877 i++;
879 ch->collect_len = 0;
880 spin_unlock(&ch->collect_lock);
881 ch->ccw[1].count = ch->trans_skb->len;
882 fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC, CH_EVENT_TIMER, ch);
883 ch->prof.send_stamp = xtime;
884 rc = ccw_device_start(ch->cdev, &ch->ccw[0],
885 (intparm_t) ch, 0xff, 0);
886 ch->prof.doios_multi++;
887 if (rc != 0) {
888 privptr->stats.tx_dropped += i;
889 privptr->stats.tx_errors += i;
890 fsm_deltimer(&ch->timer);
891 ccw_check_return_code(ch, rc);
893 } else {
894 spin_unlock(&ch->collect_lock);
895 fsm_newstate(fi, CH_STATE_TXIDLE);
897 ctc_clear_busy(dev);
901 * Initial data is sent.
902 * Notify device statemachine that we are up and
903 * running.
905 * @param fi An instance of a channel statemachine.
906 * @param event The event, just happened.
907 * @param arg Generic pointer, casted from channel * upon call.
909 static void
910 ch_action_txidle(fsm_instance * fi, int event, void *arg)
912 struct channel *ch = (struct channel *) arg;
914 fsm_deltimer(&ch->timer);
915 fsm_newstate(fi, CH_STATE_TXIDLE);
916 fsm_event(((struct ctc_priv *) ch->netdev->priv)->fsm, DEV_EVENT_TXUP,
917 ch->netdev);
921 * Got normal data, check for sanity, queue it up, allocate new buffer
922 * trigger bottom half, and initiate next read.
924 * @param fi An instance of a channel statemachine.
925 * @param event The event, just happened.
926 * @param arg Generic pointer, casted from channel * upon call.
928 static void
929 ch_action_rx(fsm_instance * fi, int event, void *arg)
931 struct channel *ch = (struct channel *) arg;
932 struct net_device *dev = ch->netdev;
933 struct ctc_priv *privptr = dev->priv;
934 int len = ch->max_bufsize - ch->irb->scsw.count;
935 struct sk_buff *skb = ch->trans_skb;
936 __u16 block_len = *((__u16 *) skb->data);
937 int check_len;
938 int rc;
940 fsm_deltimer(&ch->timer);
941 if (len < 8) {
942 printk(KERN_DEBUG "%s: got packet with length %d < 8\n",
943 dev->name, len);
944 privptr->stats.rx_dropped++;
945 privptr->stats.rx_length_errors++;
946 goto again;
948 if (len > ch->max_bufsize) {
949 printk(KERN_DEBUG "%s: got packet with length %d > %d\n",
950 dev->name, len, ch->max_bufsize);
951 privptr->stats.rx_dropped++;
952 privptr->stats.rx_length_errors++;
953 goto again;
957 * VM TCP seems to have a bug sending 2 trailing bytes of garbage.
959 switch (ch->protocol) {
960 case CTC_PROTO_S390:
961 case CTC_PROTO_OS390:
962 check_len = block_len + 2;
963 break;
964 default:
965 check_len = block_len;
966 break;
968 if ((len < block_len) || (len > check_len)) {
969 printk(KERN_DEBUG "%s: got block length %d != rx length %d\n",
970 dev->name, block_len, len);
971 ctc_dump_skb(skb, 0);
972 *((__u16 *) skb->data) = len;
973 privptr->stats.rx_dropped++;
974 privptr->stats.rx_length_errors++;
975 goto again;
977 block_len -= 2;
978 if (block_len > 0) {
979 *((__u16 *) skb->data) = block_len;
980 ctc_unpack_skb(ch, skb);
982 again:
983 skb->data = skb->tail = ch->trans_skb_data;
984 skb->len = 0;
985 if (ctc_checkalloc_buffer(ch, 1))
986 return;
987 ch->ccw[1].count = ch->max_bufsize;
988 rc = ccw_device_start(ch->cdev, &ch->ccw[0], (intparm_t) ch, 0xff, 0);
989 if (rc != 0)
990 ccw_check_return_code(ch, rc);
993 static void ch_action_rxidle(fsm_instance * fi, int event, void *arg);
996 * Initialize connection by sending a __u16 of value 0.
998 * @param fi An instance of a channel statemachine.
999 * @param event The event, just happened.
1000 * @param arg Generic pointer, casted from channel * upon call.
1002 static void
1003 ch_action_firstio(fsm_instance * fi, int event, void *arg)
1005 struct channel *ch = (struct channel *) arg;
1006 int rc;
1008 if (fsm_getstate(fi) == CH_STATE_TXIDLE)
1009 printk(KERN_DEBUG "%s: remote side issued READ?, "
1010 "init ...\n", ch->id);
1011 fsm_deltimer(&ch->timer);
1012 if (ctc_checkalloc_buffer(ch, 1))
1013 return;
1014 if ((fsm_getstate(fi) == CH_STATE_SETUPWAIT) &&
1015 (ch->protocol == CTC_PROTO_OS390)) {
1016 /* OS/390 resp. z/OS */
1017 if (CHANNEL_DIRECTION(ch->flags) == READ) {
1018 *((__u16 *) ch->trans_skb->data) = CTC_INITIAL_BLOCKLEN;
1019 fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC,
1020 CH_EVENT_TIMER, ch);
1021 ch_action_rxidle(fi, event, arg);
1022 } else {
1023 struct net_device *dev = ch->netdev;
1024 fsm_newstate(fi, CH_STATE_TXIDLE);
1025 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1026 DEV_EVENT_TXUP, dev);
1028 return;
1032 * Don´t setup a timer for receiving the initial RX frame
1033 * if in compatibility mode, since VM TCP delays the initial
1034 * frame until it has some data to send.
1036 if ((CHANNEL_DIRECTION(ch->flags) == WRITE) ||
1037 (ch->protocol != CTC_PROTO_S390))
1038 fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC, CH_EVENT_TIMER, ch);
1040 *((__u16 *) ch->trans_skb->data) = CTC_INITIAL_BLOCKLEN;
1041 ch->ccw[1].count = 2; /* Transfer only length */
1043 fsm_newstate(fi, (CHANNEL_DIRECTION(ch->flags) == READ)
1044 ? CH_STATE_RXINIT : CH_STATE_TXINIT);
1045 rc = ccw_device_start(ch->cdev, &ch->ccw[0], (intparm_t) ch, 0xff, 0);
1046 if (rc != 0) {
1047 fsm_deltimer(&ch->timer);
1048 fsm_newstate(fi, CH_STATE_SETUPWAIT);
1049 ccw_check_return_code(ch, rc);
1052 * If in compatibility mode since we don´t setup a timer, we
1053 * also signal RX channel up immediately. This enables us
1054 * to send packets early which in turn usually triggers some
1055 * reply from VM TCP which brings up the RX channel to it´s
1056 * final state.
1058 if ((CHANNEL_DIRECTION(ch->flags) == READ) &&
1059 (ch->protocol == CTC_PROTO_S390)) {
1060 struct net_device *dev = ch->netdev;
1061 fsm_event(((struct ctc_priv *) dev->priv)->fsm, DEV_EVENT_RXUP,
1062 dev);
1067 * Got initial data, check it. If OK,
1068 * notify device statemachine that we are up and
1069 * running.
1071 * @param fi An instance of a channel statemachine.
1072 * @param event The event, just happened.
1073 * @param arg Generic pointer, casted from channel * upon call.
1075 static void
1076 ch_action_rxidle(fsm_instance * fi, int event, void *arg)
1078 struct channel *ch = (struct channel *) arg;
1079 struct net_device *dev = ch->netdev;
1080 __u16 buflen;
1081 int rc;
1083 fsm_deltimer(&ch->timer);
1084 buflen = *((__u16 *) ch->trans_skb->data);
1085 pr_debug("%s: Initial RX count %d\n", dev->name, buflen);
1086 if (buflen >= CTC_INITIAL_BLOCKLEN) {
1087 if (ctc_checkalloc_buffer(ch, 1))
1088 return;
1089 ch->ccw[1].count = ch->max_bufsize;
1090 fsm_newstate(fi, CH_STATE_RXIDLE);
1091 rc = ccw_device_start(ch->cdev, &ch->ccw[0],
1092 (intparm_t) ch, 0xff, 0);
1093 if (rc != 0) {
1094 fsm_newstate(fi, CH_STATE_RXINIT);
1095 ccw_check_return_code(ch, rc);
1096 } else
1097 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1098 DEV_EVENT_RXUP, dev);
1099 } else {
1100 printk(KERN_DEBUG "%s: Initial RX count %d not %d\n",
1101 dev->name, buflen, CTC_INITIAL_BLOCKLEN);
1102 ch_action_firstio(fi, event, arg);
1107 * Set channel into extended mode.
1109 * @param fi An instance of a channel statemachine.
1110 * @param event The event, just happened.
1111 * @param arg Generic pointer, casted from channel * upon call.
1113 static void
1114 ch_action_setmode(fsm_instance * fi, int event, void *arg)
1116 struct channel *ch = (struct channel *) arg;
1117 int rc;
1118 unsigned long saveflags;
1120 fsm_deltimer(&ch->timer);
1121 fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC, CH_EVENT_TIMER, ch);
1122 fsm_newstate(fi, CH_STATE_SETUPWAIT);
1123 if (event == CH_EVENT_TIMER)
1124 spin_lock_irqsave(get_ccwdev_lock(ch->cdev), saveflags);
1125 rc = ccw_device_start(ch->cdev, &ch->ccw[6], (intparm_t) ch, 0xff, 0);
1126 if (event == CH_EVENT_TIMER)
1127 spin_unlock_irqrestore(get_ccwdev_lock(ch->cdev), saveflags);
1128 if (rc != 0) {
1129 fsm_deltimer(&ch->timer);
1130 fsm_newstate(fi, CH_STATE_STARTWAIT);
1131 ccw_check_return_code(ch, rc);
1132 } else
1133 ch->retry = 0;
1137 * Setup channel.
1139 * @param fi An instance of a channel statemachine.
1140 * @param event The event, just happened.
1141 * @param arg Generic pointer, casted from channel * upon call.
1143 static void
1144 ch_action_start(fsm_instance * fi, int event, void *arg)
1146 struct channel *ch = (struct channel *) arg;
1147 unsigned long saveflags;
1148 int rc;
1149 struct net_device *dev;
1151 if (ch == NULL) {
1152 printk(KERN_WARNING "ch_action_start ch=NULL\n");
1153 return;
1155 if (ch->netdev == NULL) {
1156 printk(KERN_WARNING "ch_action_start dev=NULL, id=%s\n",
1157 ch->id);
1158 return;
1160 dev = ch->netdev;
1162 pr_debug("%s: %s channel start\n", dev->name,
1163 (CHANNEL_DIRECTION(ch->flags) == READ) ? "RX" : "TX");
1165 if (ch->trans_skb != NULL) {
1166 clear_normalized_cda(&ch->ccw[1]);
1167 dev_kfree_skb(ch->trans_skb);
1168 ch->trans_skb = NULL;
1170 if (CHANNEL_DIRECTION(ch->flags) == READ) {
1171 ch->ccw[1].cmd_code = CCW_CMD_READ;
1172 ch->ccw[1].flags = CCW_FLAG_SLI;
1173 ch->ccw[1].count = 0;
1174 } else {
1175 ch->ccw[1].cmd_code = CCW_CMD_WRITE;
1176 ch->ccw[1].flags = CCW_FLAG_SLI | CCW_FLAG_CC;
1177 ch->ccw[1].count = 0;
1179 if (ctc_checkalloc_buffer(ch, 0))
1180 printk(KERN_NOTICE
1181 "%s: Could not allocate %s trans_skb, delaying "
1182 "allocation until first transfer\n",
1183 dev->name,
1184 (CHANNEL_DIRECTION(ch->flags) == READ) ? "RX" : "TX");
1186 ch->ccw[0].cmd_code = CCW_CMD_PREPARE;
1187 ch->ccw[0].flags = CCW_FLAG_SLI | CCW_FLAG_CC;
1188 ch->ccw[0].count = 0;
1189 ch->ccw[0].cda = 0;
1190 ch->ccw[2].cmd_code = CCW_CMD_NOOP; /* jointed CE + DE */
1191 ch->ccw[2].flags = CCW_FLAG_SLI;
1192 ch->ccw[2].count = 0;
1193 ch->ccw[2].cda = 0;
1194 memcpy(&ch->ccw[3], &ch->ccw[0], sizeof (struct ccw1) * 3);
1195 ch->ccw[4].cda = 0;
1196 ch->ccw[4].flags &= ~CCW_FLAG_IDA;
1198 fsm_newstate(fi, CH_STATE_STARTWAIT);
1199 fsm_addtimer(&ch->timer, 1000, CH_EVENT_TIMER, ch);
1200 spin_lock_irqsave(get_ccwdev_lock(ch->cdev), saveflags);
1201 rc = ccw_device_halt(ch->cdev, (intparm_t) ch);
1202 spin_unlock_irqrestore(get_ccwdev_lock(ch->cdev), saveflags);
1203 if (rc != 0) {
1204 fsm_deltimer(&ch->timer);
1205 ccw_check_return_code(ch, rc);
1207 pr_debug(KERN_DEBUG "ctc: %s(): leaving\n", __FUNCTION__);
1211 * Shutdown a channel.
1213 * @param fi An instance of a channel statemachine.
1214 * @param event The event, just happened.
1215 * @param arg Generic pointer, casted from channel * upon call.
1217 static void
1218 ch_action_haltio(fsm_instance * fi, int event, void *arg)
1220 struct channel *ch = (struct channel *) arg;
1221 unsigned long saveflags;
1222 int rc;
1223 int oldstate;
1225 fsm_deltimer(&ch->timer);
1226 fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC, CH_EVENT_TIMER, ch);
1227 if (event == CH_EVENT_STOP)
1228 spin_lock_irqsave(get_ccwdev_lock(ch->cdev), saveflags);
1229 oldstate = fsm_getstate(fi);
1230 fsm_newstate(fi, CH_STATE_TERM);
1231 rc = ccw_device_halt(ch->cdev, (intparm_t) ch);
1232 if (event == CH_EVENT_STOP)
1233 spin_unlock_irqrestore(get_ccwdev_lock(ch->cdev), saveflags);
1234 if (rc != 0) {
1235 fsm_deltimer(&ch->timer);
1236 fsm_newstate(fi, oldstate);
1237 ccw_check_return_code(ch, rc);
1242 * A channel has successfully been halted.
1243 * Cleanup it's queue and notify interface statemachine.
1245 * @param fi An instance of a channel statemachine.
1246 * @param event The event, just happened.
1247 * @param arg Generic pointer, casted from channel * upon call.
1249 static void
1250 ch_action_stopped(fsm_instance * fi, int event, void *arg)
1252 struct channel *ch = (struct channel *) arg;
1253 struct net_device *dev = ch->netdev;
1255 fsm_deltimer(&ch->timer);
1256 fsm_newstate(fi, CH_STATE_STOPPED);
1257 if (ch->trans_skb != NULL) {
1258 clear_normalized_cda(&ch->ccw[1]);
1259 dev_kfree_skb(ch->trans_skb);
1260 ch->trans_skb = NULL;
1262 if (CHANNEL_DIRECTION(ch->flags) == READ) {
1263 skb_queue_purge(&ch->io_queue);
1264 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1265 DEV_EVENT_RXDOWN, dev);
1266 } else {
1267 ctc_purge_skb_queue(&ch->io_queue);
1268 spin_lock(&ch->collect_lock);
1269 ctc_purge_skb_queue(&ch->collect_queue);
1270 ch->collect_len = 0;
1271 spin_unlock(&ch->collect_lock);
1272 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1273 DEV_EVENT_TXDOWN, dev);
1278 * A stop command from device statemachine arrived and we are in
1279 * not operational mode. Set state to stopped.
1281 * @param fi An instance of a channel statemachine.
1282 * @param event The event, just happened.
1283 * @param arg Generic pointer, casted from channel * upon call.
1285 static void
1286 ch_action_stop(fsm_instance * fi, int event, void *arg)
1288 fsm_newstate(fi, CH_STATE_STOPPED);
1292 * A machine check for no path, not operational status or gone device has
1293 * happened.
1294 * Cleanup queue and notify interface statemachine.
1296 * @param fi An instance of a channel statemachine.
1297 * @param event The event, just happened.
1298 * @param arg Generic pointer, casted from channel * upon call.
1300 static void
1301 ch_action_fail(fsm_instance * fi, int event, void *arg)
1303 struct channel *ch = (struct channel *) arg;
1304 struct net_device *dev = ch->netdev;
1306 fsm_deltimer(&ch->timer);
1307 fsm_newstate(fi, CH_STATE_NOTOP);
1308 if (CHANNEL_DIRECTION(ch->flags) == READ) {
1309 skb_queue_purge(&ch->io_queue);
1310 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1311 DEV_EVENT_RXDOWN, dev);
1312 } else {
1313 ctc_purge_skb_queue(&ch->io_queue);
1314 spin_lock(&ch->collect_lock);
1315 ctc_purge_skb_queue(&ch->collect_queue);
1316 ch->collect_len = 0;
1317 spin_unlock(&ch->collect_lock);
1318 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1319 DEV_EVENT_TXDOWN, dev);
1324 * Handle error during setup of channel.
1326 * @param fi An instance of a channel statemachine.
1327 * @param event The event, just happened.
1328 * @param arg Generic pointer, casted from channel * upon call.
1330 static void
1331 ch_action_setuperr(fsm_instance * fi, int event, void *arg)
1333 struct channel *ch = (struct channel *) arg;
1334 struct net_device *dev = ch->netdev;
1337 * Special case: Got UC_RCRESET on setmode.
1338 * This means that remote side isn't setup. In this case
1339 * simply retry after some 10 secs...
1341 if ((fsm_getstate(fi) == CH_STATE_SETUPWAIT) &&
1342 ((event == CH_EVENT_UC_RCRESET) ||
1343 (event == CH_EVENT_UC_RSRESET))) {
1344 fsm_newstate(fi, CH_STATE_STARTRETRY);
1345 fsm_deltimer(&ch->timer);
1346 fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC, CH_EVENT_TIMER, ch);
1347 if (CHANNEL_DIRECTION(ch->flags) == READ) {
1348 int rc = ccw_device_halt(ch->cdev, (intparm_t) ch);
1349 if (rc != 0)
1350 ccw_check_return_code(ch, rc);
1352 return;
1355 printk(KERN_DEBUG "%s: Error %s during %s channel setup state=%s\n",
1356 dev->name, ch_event_names[event],
1357 (CHANNEL_DIRECTION(ch->flags) == READ) ? "RX" : "TX",
1358 fsm_getstate_str(fi));
1359 if (CHANNEL_DIRECTION(ch->flags) == READ) {
1360 fsm_newstate(fi, CH_STATE_RXERR);
1361 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1362 DEV_EVENT_RXDOWN, dev);
1363 } else {
1364 fsm_newstate(fi, CH_STATE_TXERR);
1365 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1366 DEV_EVENT_TXDOWN, dev);
1371 * Restart a channel after an error.
1373 * @param fi An instance of a channel statemachine.
1374 * @param event The event, just happened.
1375 * @param arg Generic pointer, casted from channel * upon call.
1377 static void
1378 ch_action_restart(fsm_instance * fi, int event, void *arg)
1380 unsigned long saveflags;
1381 int oldstate;
1382 int rc;
1384 struct channel *ch = (struct channel *) arg;
1385 struct net_device *dev = ch->netdev;
1387 fsm_deltimer(&ch->timer);
1388 printk(KERN_DEBUG "%s: %s channel restart\n", dev->name,
1389 (CHANNEL_DIRECTION(ch->flags) == READ) ? "RX" : "TX");
1390 fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC, CH_EVENT_TIMER, ch);
1391 oldstate = fsm_getstate(fi);
1392 fsm_newstate(fi, CH_STATE_STARTWAIT);
1393 if (event == CH_EVENT_TIMER)
1394 spin_lock_irqsave(get_ccwdev_lock(ch->cdev), saveflags);
1395 rc = ccw_device_halt(ch->cdev, (intparm_t) ch);
1396 if (event == CH_EVENT_TIMER)
1397 spin_unlock_irqrestore(get_ccwdev_lock(ch->cdev), saveflags);
1398 if (rc != 0) {
1399 fsm_deltimer(&ch->timer);
1400 fsm_newstate(fi, oldstate);
1401 ccw_check_return_code(ch, rc);
1406 * Handle error during RX initial handshake (exchange of
1407 * 0-length block header)
1409 * @param fi An instance of a channel statemachine.
1410 * @param event The event, just happened.
1411 * @param arg Generic pointer, casted from channel * upon call.
1413 static void
1414 ch_action_rxiniterr(fsm_instance * fi, int event, void *arg)
1416 struct channel *ch = (struct channel *) arg;
1417 struct net_device *dev = ch->netdev;
1419 if (event == CH_EVENT_TIMER) {
1420 fsm_deltimer(&ch->timer);
1421 printk(KERN_DEBUG "%s: Timeout during RX init handshake\n",
1422 dev->name);
1423 if (ch->retry++ < 3)
1424 ch_action_restart(fi, event, arg);
1425 else {
1426 fsm_newstate(fi, CH_STATE_RXERR);
1427 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1428 DEV_EVENT_RXDOWN, dev);
1430 } else
1431 printk(KERN_WARNING "%s: Error during RX init handshake\n",
1432 dev->name);
1436 * Notify device statemachine if we gave up initialization
1437 * of RX channel.
1439 * @param fi An instance of a channel statemachine.
1440 * @param event The event, just happened.
1441 * @param arg Generic pointer, casted from channel * upon call.
1443 static void
1444 ch_action_rxinitfail(fsm_instance * fi, int event, void *arg)
1446 struct channel *ch = (struct channel *) arg;
1447 struct net_device *dev = ch->netdev;
1449 fsm_newstate(fi, CH_STATE_RXERR);
1450 printk(KERN_WARNING "%s: RX initialization failed\n", dev->name);
1451 printk(KERN_WARNING "%s: RX <-> RX connection detected\n", dev->name);
1452 fsm_event(((struct ctc_priv *) dev->priv)->fsm, DEV_EVENT_RXDOWN, dev);
1456 * Handle RX Unit check remote reset (remote disconnected)
1458 * @param fi An instance of a channel statemachine.
1459 * @param event The event, just happened.
1460 * @param arg Generic pointer, casted from channel * upon call.
1462 static void
1463 ch_action_rxdisc(fsm_instance * fi, int event, void *arg)
1465 struct channel *ch = (struct channel *) arg;
1466 struct channel *ch2;
1467 struct net_device *dev = ch->netdev;
1469 fsm_deltimer(&ch->timer);
1470 printk(KERN_DEBUG "%s: Got remote disconnect, re-initializing ...\n",
1471 dev->name);
1474 * Notify device statemachine
1476 fsm_event(((struct ctc_priv *) dev->priv)->fsm, DEV_EVENT_RXDOWN, dev);
1477 fsm_event(((struct ctc_priv *) dev->priv)->fsm, DEV_EVENT_TXDOWN, dev);
1479 fsm_newstate(fi, CH_STATE_DTERM);
1480 ch2 = ((struct ctc_priv *) dev->priv)->channel[WRITE];
1481 fsm_newstate(ch2->fsm, CH_STATE_DTERM);
1483 ccw_device_halt(ch->cdev, (intparm_t) ch);
1484 ccw_device_halt(ch2->cdev, (intparm_t) ch2);
1488 * Handle error during TX channel initialization.
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_txiniterr(fsm_instance * fi, int event, void *arg)
1497 struct channel *ch = (struct channel *) arg;
1498 struct net_device *dev = ch->netdev;
1500 if (event == CH_EVENT_TIMER) {
1501 fsm_deltimer(&ch->timer);
1502 printk(KERN_DEBUG "%s: Timeout during TX init handshake\n",
1503 dev->name);
1504 if (ch->retry++ < 3)
1505 ch_action_restart(fi, event, arg);
1506 else {
1507 fsm_newstate(fi, CH_STATE_TXERR);
1508 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1509 DEV_EVENT_TXDOWN, dev);
1511 } else
1512 printk(KERN_WARNING "%s: Error during TX init handshake\n",
1513 dev->name);
1517 * Handle TX timeout by retrying operation.
1519 * @param fi An instance of a channel statemachine.
1520 * @param event The event, just happened.
1521 * @param arg Generic pointer, casted from channel * upon call.
1523 static void
1524 ch_action_txretry(fsm_instance * fi, int event, void *arg)
1526 struct channel *ch = (struct channel *) arg;
1527 struct net_device *dev = ch->netdev;
1528 unsigned long saveflags;
1530 fsm_deltimer(&ch->timer);
1531 if (ch->retry++ > 3) {
1532 printk(KERN_DEBUG "%s: TX retry failed, restarting channel\n",
1533 dev->name);
1534 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1535 DEV_EVENT_TXDOWN, dev);
1536 ch_action_restart(fi, event, arg);
1537 } else {
1538 struct sk_buff *skb;
1540 printk(KERN_DEBUG "%s: TX retry %d\n", dev->name, ch->retry);
1541 if ((skb = skb_peek(&ch->io_queue))) {
1542 int rc = 0;
1544 clear_normalized_cda(&ch->ccw[4]);
1545 ch->ccw[4].count = skb->len;
1546 if (set_normalized_cda(&ch->ccw[4], skb->data)) {
1547 printk(KERN_DEBUG "%s: IDAL alloc failed, "
1548 "restarting channel\n", dev->name);
1549 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1550 DEV_EVENT_TXDOWN, dev);
1551 ch_action_restart(fi, event, arg);
1552 return;
1554 fsm_addtimer(&ch->timer, 1000, CH_EVENT_TIMER, ch);
1555 if (event == CH_EVENT_TIMER)
1556 spin_lock_irqsave(get_ccwdev_lock(ch->cdev),
1557 saveflags);
1558 rc = ccw_device_start(ch->cdev, &ch->ccw[3],
1559 (intparm_t) ch, 0xff, 0);
1560 if (event == CH_EVENT_TIMER)
1561 spin_unlock_irqrestore(get_ccwdev_lock(ch->cdev),
1562 saveflags);
1563 if (rc != 0) {
1564 fsm_deltimer(&ch->timer);
1565 ccw_check_return_code(ch, rc);
1566 ctc_purge_skb_queue(&ch->io_queue);
1574 * Handle fatal errors during an I/O command.
1576 * @param fi An instance of a channel statemachine.
1577 * @param event The event, just happened.
1578 * @param arg Generic pointer, casted from channel * upon call.
1580 static void
1581 ch_action_iofatal(fsm_instance * fi, int event, void *arg)
1583 struct channel *ch = (struct channel *) arg;
1584 struct net_device *dev = ch->netdev;
1586 fsm_deltimer(&ch->timer);
1587 if (CHANNEL_DIRECTION(ch->flags) == READ) {
1588 printk(KERN_DEBUG "%s: RX I/O error\n", dev->name);
1589 fsm_newstate(fi, CH_STATE_RXERR);
1590 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1591 DEV_EVENT_RXDOWN, dev);
1592 } else {
1593 printk(KERN_DEBUG "%s: TX I/O error\n", dev->name);
1594 fsm_newstate(fi, CH_STATE_TXERR);
1595 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1596 DEV_EVENT_TXDOWN, dev);
1600 static void
1601 ch_action_reinit(fsm_instance *fi, int event, void *arg)
1603 struct channel *ch = (struct channel *)arg;
1604 struct net_device *dev = ch->netdev;
1605 struct ctc_priv *privptr = dev->priv;
1607 ch_action_iofatal(fi, event, arg);
1608 fsm_addtimer(&privptr->restart_timer, 1000, DEV_EVENT_RESTART, dev);
1613 * The statemachine for a channel.
1615 static const fsm_node ch_fsm[] = {
1616 {CH_STATE_STOPPED, CH_EVENT_STOP, fsm_action_nop},
1617 {CH_STATE_STOPPED, CH_EVENT_START, ch_action_start},
1618 {CH_STATE_STOPPED, CH_EVENT_FINSTAT, fsm_action_nop},
1619 {CH_STATE_STOPPED, CH_EVENT_MC_FAIL, fsm_action_nop},
1621 {CH_STATE_NOTOP, CH_EVENT_STOP, ch_action_stop},
1622 {CH_STATE_NOTOP, CH_EVENT_START, fsm_action_nop},
1623 {CH_STATE_NOTOP, CH_EVENT_FINSTAT, fsm_action_nop},
1624 {CH_STATE_NOTOP, CH_EVENT_MC_FAIL, fsm_action_nop},
1625 {CH_STATE_NOTOP, CH_EVENT_MC_GOOD, ch_action_start},
1627 {CH_STATE_STARTWAIT, CH_EVENT_STOP, ch_action_haltio},
1628 {CH_STATE_STARTWAIT, CH_EVENT_START, fsm_action_nop},
1629 {CH_STATE_STARTWAIT, CH_EVENT_FINSTAT, ch_action_setmode},
1630 {CH_STATE_STARTWAIT, CH_EVENT_TIMER, ch_action_setuperr},
1631 {CH_STATE_STARTWAIT, CH_EVENT_IO_ENODEV, ch_action_iofatal},
1632 {CH_STATE_STARTWAIT, CH_EVENT_IO_EIO, ch_action_reinit},
1633 {CH_STATE_STARTWAIT, CH_EVENT_MC_FAIL, ch_action_fail},
1635 {CH_STATE_STARTRETRY, CH_EVENT_STOP, ch_action_haltio},
1636 {CH_STATE_STARTRETRY, CH_EVENT_TIMER, ch_action_setmode},
1637 {CH_STATE_STARTRETRY, CH_EVENT_FINSTAT, fsm_action_nop},
1638 {CH_STATE_STARTRETRY, CH_EVENT_MC_FAIL, ch_action_fail},
1640 {CH_STATE_SETUPWAIT, CH_EVENT_STOP, ch_action_haltio},
1641 {CH_STATE_SETUPWAIT, CH_EVENT_START, fsm_action_nop},
1642 {CH_STATE_SETUPWAIT, CH_EVENT_FINSTAT, ch_action_firstio},
1643 {CH_STATE_SETUPWAIT, CH_EVENT_UC_RCRESET, ch_action_setuperr},
1644 {CH_STATE_SETUPWAIT, CH_EVENT_UC_RSRESET, ch_action_setuperr},
1645 {CH_STATE_SETUPWAIT, CH_EVENT_TIMER, ch_action_setmode},
1646 {CH_STATE_SETUPWAIT, CH_EVENT_IO_ENODEV, ch_action_iofatal},
1647 {CH_STATE_SETUPWAIT, CH_EVENT_IO_EIO, ch_action_reinit},
1648 {CH_STATE_SETUPWAIT, CH_EVENT_MC_FAIL, ch_action_fail},
1650 {CH_STATE_RXINIT, CH_EVENT_STOP, ch_action_haltio},
1651 {CH_STATE_RXINIT, CH_EVENT_START, fsm_action_nop},
1652 {CH_STATE_RXINIT, CH_EVENT_FINSTAT, ch_action_rxidle},
1653 {CH_STATE_RXINIT, CH_EVENT_UC_RCRESET, ch_action_rxiniterr},
1654 {CH_STATE_RXINIT, CH_EVENT_UC_RSRESET, ch_action_rxiniterr},
1655 {CH_STATE_RXINIT, CH_EVENT_TIMER, ch_action_rxiniterr},
1656 {CH_STATE_RXINIT, CH_EVENT_ATTNBUSY, ch_action_rxinitfail},
1657 {CH_STATE_RXINIT, CH_EVENT_IO_ENODEV, ch_action_iofatal},
1658 {CH_STATE_RXINIT, CH_EVENT_IO_EIO, ch_action_reinit},
1659 {CH_STATE_RXINIT, CH_EVENT_UC_ZERO, ch_action_firstio},
1660 {CH_STATE_RXINIT, CH_EVENT_MC_FAIL, ch_action_fail},
1662 {CH_STATE_RXIDLE, CH_EVENT_STOP, ch_action_haltio},
1663 {CH_STATE_RXIDLE, CH_EVENT_START, fsm_action_nop},
1664 {CH_STATE_RXIDLE, CH_EVENT_FINSTAT, ch_action_rx},
1665 {CH_STATE_RXIDLE, CH_EVENT_UC_RCRESET, ch_action_rxdisc},
1666 // { CH_STATE_RXIDLE, CH_EVENT_UC_RSRESET, ch_action_rxretry },
1667 {CH_STATE_RXIDLE, CH_EVENT_IO_ENODEV, ch_action_iofatal},
1668 {CH_STATE_RXIDLE, CH_EVENT_IO_EIO, ch_action_reinit},
1669 {CH_STATE_RXIDLE, CH_EVENT_MC_FAIL, ch_action_fail},
1670 {CH_STATE_RXIDLE, CH_EVENT_UC_ZERO, ch_action_rx},
1672 {CH_STATE_TXINIT, CH_EVENT_STOP, ch_action_haltio},
1673 {CH_STATE_TXINIT, CH_EVENT_START, fsm_action_nop},
1674 {CH_STATE_TXINIT, CH_EVENT_FINSTAT, ch_action_txidle},
1675 {CH_STATE_TXINIT, CH_EVENT_UC_RCRESET, ch_action_txiniterr},
1676 {CH_STATE_TXINIT, CH_EVENT_UC_RSRESET, ch_action_txiniterr},
1677 {CH_STATE_TXINIT, CH_EVENT_TIMER, ch_action_txiniterr},
1678 {CH_STATE_TXINIT, CH_EVENT_IO_ENODEV, ch_action_iofatal},
1679 {CH_STATE_TXINIT, CH_EVENT_IO_EIO, ch_action_reinit},
1680 {CH_STATE_TXINIT, CH_EVENT_MC_FAIL, ch_action_fail},
1682 {CH_STATE_TXIDLE, CH_EVENT_STOP, ch_action_haltio},
1683 {CH_STATE_TXIDLE, CH_EVENT_START, fsm_action_nop},
1684 {CH_STATE_TXIDLE, CH_EVENT_FINSTAT, ch_action_firstio},
1685 {CH_STATE_TXIDLE, CH_EVENT_UC_RCRESET, fsm_action_nop},
1686 {CH_STATE_TXIDLE, CH_EVENT_UC_RSRESET, fsm_action_nop},
1687 {CH_STATE_TXIDLE, CH_EVENT_IO_ENODEV, ch_action_iofatal},
1688 {CH_STATE_TXIDLE, CH_EVENT_IO_EIO, ch_action_reinit},
1689 {CH_STATE_TXIDLE, CH_EVENT_MC_FAIL, ch_action_fail},
1691 {CH_STATE_TERM, CH_EVENT_STOP, fsm_action_nop},
1692 {CH_STATE_TERM, CH_EVENT_START, ch_action_restart},
1693 {CH_STATE_TERM, CH_EVENT_FINSTAT, ch_action_stopped},
1694 {CH_STATE_TERM, CH_EVENT_UC_RCRESET, fsm_action_nop},
1695 {CH_STATE_TERM, CH_EVENT_UC_RSRESET, fsm_action_nop},
1696 {CH_STATE_TERM, CH_EVENT_MC_FAIL, ch_action_fail},
1698 {CH_STATE_DTERM, CH_EVENT_STOP, ch_action_haltio},
1699 {CH_STATE_DTERM, CH_EVENT_START, ch_action_restart},
1700 {CH_STATE_DTERM, CH_EVENT_FINSTAT, ch_action_setmode},
1701 {CH_STATE_DTERM, CH_EVENT_UC_RCRESET, fsm_action_nop},
1702 {CH_STATE_DTERM, CH_EVENT_UC_RSRESET, fsm_action_nop},
1703 {CH_STATE_DTERM, CH_EVENT_MC_FAIL, ch_action_fail},
1705 {CH_STATE_TX, CH_EVENT_STOP, ch_action_haltio},
1706 {CH_STATE_TX, CH_EVENT_START, fsm_action_nop},
1707 {CH_STATE_TX, CH_EVENT_FINSTAT, ch_action_txdone},
1708 {CH_STATE_TX, CH_EVENT_UC_RCRESET, ch_action_txretry},
1709 {CH_STATE_TX, CH_EVENT_UC_RSRESET, ch_action_txretry},
1710 {CH_STATE_TX, CH_EVENT_TIMER, ch_action_txretry},
1711 {CH_STATE_TX, CH_EVENT_IO_ENODEV, ch_action_iofatal},
1712 {CH_STATE_TX, CH_EVENT_IO_EIO, ch_action_reinit},
1713 {CH_STATE_TX, CH_EVENT_MC_FAIL, ch_action_fail},
1715 {CH_STATE_RXERR, CH_EVENT_STOP, ch_action_haltio},
1716 {CH_STATE_TXERR, CH_EVENT_STOP, ch_action_haltio},
1717 {CH_STATE_TXERR, CH_EVENT_MC_FAIL, ch_action_fail},
1718 {CH_STATE_RXERR, CH_EVENT_MC_FAIL, ch_action_fail},
1721 static const int CH_FSM_LEN = sizeof (ch_fsm) / sizeof (fsm_node);
1724 * Functions related to setup and device detection.
1725 *****************************************************************************/
1727 static inline int
1728 less_than(char *id1, char *id2)
1730 int dev1,dev2,i;
1732 for (i=0;i<5;i++) {
1733 id1++;
1734 id2++;
1736 dev1 = simple_strtoul(id1, &id1, 16);
1737 dev2 = simple_strtoul(id2, &id2, 16);
1739 return (dev1<dev2);
1743 * Add a new channel to the list of channels.
1744 * Keeps the channel list sorted.
1746 * @param cdev The ccw_device to be added.
1747 * @param type The type class of the new channel.
1749 * @return 0 on success, !0 on error.
1751 static int
1752 add_channel(struct ccw_device *cdev, enum channel_types type)
1754 struct channel **c = &channels;
1755 struct channel *ch;
1757 if ((ch =
1758 (struct channel *) kmalloc(sizeof (struct channel),
1759 GFP_KERNEL)) == NULL) {
1760 printk(KERN_WARNING "ctc: Out of memory in add_channel\n");
1761 return -1;
1763 memset(ch, 0, sizeof (struct channel));
1764 if ((ch->ccw = (struct ccw1 *) kmalloc(sizeof (struct ccw1) * 8,
1765 GFP_KERNEL | GFP_DMA)) == NULL) {
1766 kfree(ch);
1767 printk(KERN_WARNING "ctc: Out of memory in add_channel\n");
1768 return -1;
1772 * "static" ccws are used in the following way:
1774 * ccw[0..2] (Channel program for generic I/O):
1775 * 0: prepare
1776 * 1: read or write (depending on direction) with fixed
1777 * buffer (idal allocated once when buffer is allocated)
1778 * 2: nop
1779 * ccw[3..5] (Channel program for direct write of packets)
1780 * 3: prepare
1781 * 4: write (idal allocated on every write).
1782 * 5: nop
1783 * ccw[6..7] (Channel program for initial channel setup):
1784 * 3: set extended mode
1785 * 4: nop
1787 * ch->ccw[0..5] are initialized in ch_action_start because
1788 * the channel's direction is yet unknown here.
1790 ch->ccw[6].cmd_code = CCW_CMD_SET_EXTENDED;
1791 ch->ccw[6].flags = CCW_FLAG_SLI;
1792 ch->ccw[6].count = 0;
1793 ch->ccw[6].cda = 0;
1795 ch->ccw[7].cmd_code = CCW_CMD_NOOP;
1796 ch->ccw[7].flags = CCW_FLAG_SLI;
1797 ch->ccw[7].count = 0;
1798 ch->ccw[7].cda = 0;
1800 ch->cdev = cdev;
1801 snprintf(ch->id, DEVICE_ID_SIZE, "ch-%s", cdev->dev.bus_id);
1802 ch->type = type;
1803 ch->fsm = init_fsm(ch->id, ch_state_names,
1804 ch_event_names, NR_CH_STATES, NR_CH_EVENTS,
1805 ch_fsm, CH_FSM_LEN, GFP_KERNEL);
1806 if (ch->fsm == NULL) {
1807 printk(KERN_WARNING
1808 "ctc: Could not create FSM in add_channel\n");
1809 kfree(ch);
1810 return -1;
1812 fsm_newstate(ch->fsm, CH_STATE_IDLE);
1813 if ((ch->irb = (struct irb *) kmalloc(sizeof (struct irb),
1814 GFP_KERNEL)) == NULL) {
1815 printk(KERN_WARNING "ctc: Out of memory in add_channel\n");
1816 kfree_fsm(ch->fsm);
1817 kfree(ch);
1818 return -1;
1820 memset(ch->irb, 0, sizeof (struct irb));
1821 while (*c && less_than((*c)->id, ch->id))
1822 c = &(*c)->next;
1823 if (!strncmp((*c)->id, ch->id, CTC_ID_SIZE)) {
1824 printk(KERN_DEBUG
1825 "ctc: add_channel: device %s already in list, "
1826 "using old entry\n", (*c)->id);
1827 kfree(ch->irb);
1828 kfree_fsm(ch->fsm);
1829 kfree(ch);
1830 return 0;
1832 fsm_settimer(ch->fsm, &ch->timer);
1833 skb_queue_head_init(&ch->io_queue);
1834 skb_queue_head_init(&ch->collect_queue);
1835 ch->next = *c;
1836 *c = ch;
1837 return 0;
1841 * Release a specific channel in the channel list.
1843 * @param ch Pointer to channel struct to be released.
1845 static void
1846 channel_free(struct channel *ch)
1848 ch->flags &= ~CHANNEL_FLAGS_INUSE;
1849 fsm_newstate(ch->fsm, CH_STATE_IDLE);
1853 * Remove a specific channel in the channel list.
1855 * @param ch Pointer to channel struct to be released.
1857 static void
1858 channel_remove(struct channel *ch)
1860 struct channel **c = &channels;
1862 if (ch == NULL)
1863 return;
1865 channel_free(ch);
1866 while (*c) {
1867 if (*c == ch) {
1868 *c = ch->next;
1869 fsm_deltimer(&ch->timer);
1870 kfree_fsm(ch->fsm);
1871 clear_normalized_cda(&ch->ccw[4]);
1872 if (ch->trans_skb != NULL) {
1873 clear_normalized_cda(&ch->ccw[1]);
1874 dev_kfree_skb(ch->trans_skb);
1876 kfree(ch->ccw);
1877 return;
1879 c = &((*c)->next);
1884 * Get a specific channel from the channel list.
1886 * @param type Type of channel we are interested in.
1887 * @param id Id of channel we are interested in.
1888 * @param direction Direction we want to use this channel for.
1890 * @return Pointer to a channel or NULL if no matching channel available.
1892 static struct channel
1894 channel_get(enum channel_types type, char *id, int direction)
1896 struct channel *ch = channels;
1898 pr_debug("ctc: %s(): searching for ch with id %d and type %d\n",
1899 __FUNCTION__, id, type);
1901 while (ch && ((strncmp(ch->id, id, CTC_ID_SIZE)) || (ch->type != type))) {
1902 pr_debug("ctc: %s(): ch=0x%p (id=%s, type=%d\n",
1903 __FUNCTION__, ch, ch->id, ch->type);
1904 ch = ch->next;
1906 pr_debug("ctc: %s(): ch=0x%pq (id=%s, type=%d\n",
1907 __FUNCTION__, ch, ch->id, ch->type);
1908 if (!ch) {
1909 printk(KERN_WARNING "ctc: %s(): channel with id %s "
1910 "and type %d not found in channel list\n",
1911 __FUNCTION__, id, type);
1912 } else {
1913 if (ch->flags & CHANNEL_FLAGS_INUSE)
1914 ch = NULL;
1915 else {
1916 ch->flags |= CHANNEL_FLAGS_INUSE;
1917 ch->flags &= ~CHANNEL_FLAGS_RWMASK;
1918 ch->flags |= (direction == WRITE)
1919 ? CHANNEL_FLAGS_WRITE : CHANNEL_FLAGS_READ;
1920 fsm_newstate(ch->fsm, CH_STATE_STOPPED);
1923 return ch;
1927 * Return the channel type by name.
1929 * @param name Name of network interface.
1931 * @return Type class of channel to be used for that interface.
1933 static enum channel_types inline
1934 extract_channel_media(char *name)
1936 enum channel_types ret = channel_type_unknown;
1938 if (name != NULL) {
1939 if (strncmp(name, "ctc", 3) == 0)
1940 ret = channel_type_parallel;
1941 if (strncmp(name, "escon", 5) == 0)
1942 ret = channel_type_escon;
1944 return ret;
1948 * Main IRQ handler.
1950 * @param cdev The ccw_device the interrupt is for.
1951 * @param intparm interruption parameter.
1952 * @param irb interruption response block.
1954 static void
1955 ctc_irq_handler(struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
1957 struct channel *ch;
1958 struct net_device *dev;
1960 ch = (struct channel *) intparm;
1962 * Check for unsolicited interrupts.
1963 * If intparm is NULL, try to get channel from ccw_device.
1965 if (ch == NULL) {
1966 ch = container_of(&cdev, struct channel, cdev);
1967 if (ch == NULL) {
1968 printk(KERN_WARNING
1969 "ctc: Got unsolicited irq: %s c-%02x d-%02x\n",
1970 cdev->dev.bus_id, irb->scsw.cstat,
1971 irb->scsw.dstat);
1972 return;
1976 dev = (struct net_device *) (ch->netdev);
1977 if (dev == NULL) {
1978 printk(KERN_CRIT
1979 "ctc: ctc_irq_handler dev = NULL bus_id=%s, ch=0x%p\n",
1980 cdev->dev.bus_id, ch);
1981 return;
1984 pr_debug("%s: interrupt for device: %s received c-%02x d-%02x\n"
1985 dev->name, ch->id, irb->scsw.cstat, irb->scsw.dstat);
1987 /* Copy interruption response block. */
1988 memcpy(ch->irb, irb, sizeof(struct irb));
1990 /* Check for good subchannel return code, otherwise error message */
1991 if (ch->irb->scsw.cstat) {
1992 fsm_event(ch->fsm, CH_EVENT_SC_UNKNOWN, ch);
1993 printk(KERN_WARNING
1994 "%s: subchannel check for device: %s - %02x %02x\n",
1995 dev->name, ch->id, ch->irb->scsw.cstat,
1996 ch->irb->scsw.dstat);
1997 return;
2000 /* Check the reason-code of a unit check */
2001 if (ch->irb->scsw.dstat & DEV_STAT_UNIT_CHECK) {
2002 ccw_unit_check(ch, ch->irb->ecw[0]);
2003 return;
2005 if (ch->irb->scsw.dstat & DEV_STAT_BUSY) {
2006 if (ch->irb->scsw.dstat & DEV_STAT_ATTENTION)
2007 fsm_event(ch->fsm, CH_EVENT_ATTNBUSY, ch);
2008 else
2009 fsm_event(ch->fsm, CH_EVENT_BUSY, ch);
2010 return;
2012 if (ch->irb->scsw.dstat & DEV_STAT_ATTENTION) {
2013 fsm_event(ch->fsm, CH_EVENT_ATTN, ch);
2014 return;
2016 if ((ch->irb->scsw.stctl & SCSW_STCTL_SEC_STATUS) ||
2017 (ch->irb->scsw.stctl == SCSW_STCTL_STATUS_PEND) ||
2018 (ch->irb->scsw.stctl ==
2019 (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND)))
2020 fsm_event(ch->fsm, CH_EVENT_FINSTAT, ch);
2021 else
2022 fsm_event(ch->fsm, CH_EVENT_IRQ, ch);
2027 * Actions for interface - statemachine.
2028 *****************************************************************************/
2031 * Startup channels by sending CH_EVENT_START to each channel.
2033 * @param fi An instance of an interface statemachine.
2034 * @param event The event, just happened.
2035 * @param arg Generic pointer, casted from struct net_device * upon call.
2037 static void
2038 dev_action_start(fsm_instance * fi, int event, void *arg)
2040 struct net_device *dev = (struct net_device *) arg;
2041 struct ctc_priv *privptr = dev->priv;
2042 int direction;
2044 fsm_deltimer(&privptr->restart_timer);
2045 fsm_newstate(fi, DEV_STATE_STARTWAIT_RXTX);
2046 for (direction = READ; direction <= WRITE; direction++) {
2047 struct channel *ch = privptr->channel[direction];
2048 fsm_event(ch->fsm, CH_EVENT_START, ch);
2053 * Shutdown channels by sending CH_EVENT_STOP to each channel.
2055 * @param fi An instance of an interface statemachine.
2056 * @param event The event, just happened.
2057 * @param arg Generic pointer, casted from struct net_device * upon call.
2059 static void
2060 dev_action_stop(fsm_instance * fi, int event, void *arg)
2062 struct net_device *dev = (struct net_device *) arg;
2063 struct ctc_priv *privptr = dev->priv;
2064 int direction;
2066 fsm_newstate(fi, DEV_STATE_STOPWAIT_RXTX);
2067 for (direction = READ; direction <= WRITE; direction++) {
2068 struct channel *ch = privptr->channel[direction];
2069 fsm_event(ch->fsm, CH_EVENT_STOP, ch);
2072 static void
2073 dev_action_restart(fsm_instance *fi, int event, void *arg)
2075 struct net_device *dev = (struct net_device *)arg;
2076 struct ctc_priv *privptr = dev->priv;
2078 printk(KERN_DEBUG "%s: Restarting\n", dev->name);
2079 dev_action_stop(fi, event, arg);
2080 fsm_event(privptr->fsm, DEV_EVENT_STOP, dev);
2081 fsm_addtimer(&privptr->restart_timer, CTC_TIMEOUT_5SEC,
2082 DEV_EVENT_START, dev);
2086 * Called from channel statemachine
2087 * when a channel is up and running.
2089 * @param fi An instance of an interface statemachine.
2090 * @param event The event, just happened.
2091 * @param arg Generic pointer, casted from struct net_device * upon call.
2093 static void
2094 dev_action_chup(fsm_instance * fi, int event, void *arg)
2096 struct net_device *dev = (struct net_device *) arg;
2097 struct ctc_priv *privptr = dev->priv;
2099 switch (fsm_getstate(fi)) {
2100 case DEV_STATE_STARTWAIT_RXTX:
2101 if (event == DEV_EVENT_RXUP)
2102 fsm_newstate(fi, DEV_STATE_STARTWAIT_TX);
2103 else
2104 fsm_newstate(fi, DEV_STATE_STARTWAIT_RX);
2105 break;
2106 case DEV_STATE_STARTWAIT_RX:
2107 if (event == DEV_EVENT_RXUP) {
2108 fsm_newstate(fi, DEV_STATE_RUNNING);
2109 printk(KERN_INFO
2110 "%s: connected with remote side\n", dev->name);
2111 if (privptr->protocol == CTC_PROTO_LINUX_TTY)
2112 ctc_tty_setcarrier(dev, 1);
2113 ctc_clear_busy(dev);
2115 break;
2116 case DEV_STATE_STARTWAIT_TX:
2117 if (event == DEV_EVENT_TXUP) {
2118 fsm_newstate(fi, DEV_STATE_RUNNING);
2119 printk(KERN_INFO
2120 "%s: connected with remote side\n", dev->name);
2121 if (privptr->protocol == CTC_PROTO_LINUX_TTY)
2122 ctc_tty_setcarrier(dev, 1);
2123 ctc_clear_busy(dev);
2125 break;
2126 case DEV_STATE_STOPWAIT_TX:
2127 if (event == DEV_EVENT_RXUP)
2128 fsm_newstate(fi, DEV_STATE_STOPWAIT_RXTX);
2129 break;
2130 case DEV_STATE_STOPWAIT_RX:
2131 if (event == DEV_EVENT_TXUP)
2132 fsm_newstate(fi, DEV_STATE_STOPWAIT_RXTX);
2133 break;
2138 * Called from channel statemachine
2139 * when a channel has been shutdown.
2141 * @param fi An instance of an interface statemachine.
2142 * @param event The event, just happened.
2143 * @param arg Generic pointer, casted from struct net_device * upon call.
2145 static void
2146 dev_action_chdown(fsm_instance * fi, int event, void *arg)
2148 struct net_device *dev = (struct net_device *) arg;
2149 struct ctc_priv *privptr = dev->priv;
2151 switch (fsm_getstate(fi)) {
2152 case DEV_STATE_RUNNING:
2153 if (privptr->protocol == CTC_PROTO_LINUX_TTY)
2154 ctc_tty_setcarrier(dev, 0);
2155 if (event == DEV_EVENT_TXDOWN)
2156 fsm_newstate(fi, DEV_STATE_STARTWAIT_TX);
2157 else
2158 fsm_newstate(fi, DEV_STATE_STARTWAIT_RX);
2159 break;
2160 case DEV_STATE_STARTWAIT_RX:
2161 if (event == DEV_EVENT_TXDOWN)
2162 fsm_newstate(fi, DEV_STATE_STARTWAIT_RXTX);
2163 break;
2164 case DEV_STATE_STARTWAIT_TX:
2165 if (event == DEV_EVENT_RXDOWN)
2166 fsm_newstate(fi, DEV_STATE_STARTWAIT_RXTX);
2167 break;
2168 case DEV_STATE_STOPWAIT_RXTX:
2169 if (event == DEV_EVENT_TXDOWN)
2170 fsm_newstate(fi, DEV_STATE_STOPWAIT_RX);
2171 else
2172 fsm_newstate(fi, DEV_STATE_STOPWAIT_TX);
2173 break;
2174 case DEV_STATE_STOPWAIT_RX:
2175 if (event == DEV_EVENT_RXDOWN)
2176 fsm_newstate(fi, DEV_STATE_STOPPED);
2177 break;
2178 case DEV_STATE_STOPWAIT_TX:
2179 if (event == DEV_EVENT_TXDOWN)
2180 fsm_newstate(fi, DEV_STATE_STOPPED);
2181 break;
2185 static const fsm_node dev_fsm[] = {
2186 {DEV_STATE_STOPPED, DEV_EVENT_START, dev_action_start},
2188 {DEV_STATE_STOPWAIT_RXTX, DEV_EVENT_START, dev_action_start},
2189 {DEV_STATE_STOPWAIT_RXTX, DEV_EVENT_RXDOWN, dev_action_chdown},
2190 {DEV_STATE_STOPWAIT_RXTX, DEV_EVENT_TXDOWN, dev_action_chdown},
2191 {DEV_STATE_STOPWAIT_RXTX, DEV_EVENT_RESTART, dev_action_restart },
2193 {DEV_STATE_STOPWAIT_RX, DEV_EVENT_START, dev_action_start},
2194 {DEV_STATE_STOPWAIT_RX, DEV_EVENT_RXUP, dev_action_chup},
2195 {DEV_STATE_STOPWAIT_RX, DEV_EVENT_TXUP, dev_action_chup},
2196 {DEV_STATE_STOPWAIT_RX, DEV_EVENT_RXDOWN, dev_action_chdown},
2197 {DEV_STATE_STOPWAIT_RX, DEV_EVENT_RESTART, dev_action_restart },
2199 {DEV_STATE_STOPWAIT_TX, DEV_EVENT_START, dev_action_start},
2200 {DEV_STATE_STOPWAIT_TX, DEV_EVENT_RXUP, dev_action_chup},
2201 {DEV_STATE_STOPWAIT_TX, DEV_EVENT_TXUP, dev_action_chup},
2202 {DEV_STATE_STOPWAIT_TX, DEV_EVENT_TXDOWN, dev_action_chdown},
2203 {DEV_STATE_STOPWAIT_TX, DEV_EVENT_RESTART, dev_action_restart },
2205 {DEV_STATE_STARTWAIT_RXTX, DEV_EVENT_STOP, dev_action_stop},
2206 {DEV_STATE_STARTWAIT_RXTX, DEV_EVENT_RXUP, dev_action_chup},
2207 {DEV_STATE_STARTWAIT_RXTX, DEV_EVENT_TXUP, dev_action_chup},
2208 {DEV_STATE_STARTWAIT_RXTX, DEV_EVENT_RXDOWN, dev_action_chdown},
2209 {DEV_STATE_STARTWAIT_RXTX, DEV_EVENT_TXDOWN, dev_action_chdown},
2210 {DEV_STATE_STARTWAIT_RXTX, DEV_EVENT_RESTART, dev_action_restart },
2212 {DEV_STATE_STARTWAIT_TX, DEV_EVENT_STOP, dev_action_stop},
2213 {DEV_STATE_STARTWAIT_TX, DEV_EVENT_RXUP, dev_action_chup},
2214 {DEV_STATE_STARTWAIT_TX, DEV_EVENT_TXUP, dev_action_chup},
2215 {DEV_STATE_STARTWAIT_TX, DEV_EVENT_RXDOWN, dev_action_chdown},
2216 {DEV_STATE_STARTWAIT_TX, DEV_EVENT_RESTART, dev_action_restart },
2218 {DEV_STATE_STARTWAIT_RX, DEV_EVENT_STOP, dev_action_stop},
2219 {DEV_STATE_STARTWAIT_RX, DEV_EVENT_RXUP, dev_action_chup},
2220 {DEV_STATE_STARTWAIT_RX, DEV_EVENT_TXUP, dev_action_chup},
2221 {DEV_STATE_STARTWAIT_RX, DEV_EVENT_TXDOWN, dev_action_chdown},
2222 {DEV_STATE_STARTWAIT_RX, DEV_EVENT_RESTART, dev_action_restart },
2224 {DEV_STATE_RUNNING, DEV_EVENT_STOP, dev_action_stop},
2225 {DEV_STATE_RUNNING, DEV_EVENT_RXDOWN, dev_action_chdown},
2226 {DEV_STATE_RUNNING, DEV_EVENT_TXDOWN, dev_action_chdown},
2227 {DEV_STATE_RUNNING, DEV_EVENT_TXUP, fsm_action_nop},
2228 {DEV_STATE_RUNNING, DEV_EVENT_RXUP, fsm_action_nop},
2229 {DEV_STATE_RUNNING, DEV_EVENT_RESTART, dev_action_restart },
2232 static const int DEV_FSM_LEN = sizeof (dev_fsm) / sizeof (fsm_node);
2235 * Transmit a packet.
2236 * This is a helper function for ctc_tx().
2238 * @param ch Channel to be used for sending.
2239 * @param skb Pointer to struct sk_buff of packet to send.
2240 * The linklevel header has already been set up
2241 * by ctc_tx().
2243 * @return 0 on success, -ERRNO on failure. (Never fails.)
2245 static int
2246 transmit_skb(struct channel *ch, struct sk_buff *skb)
2248 unsigned long saveflags;
2249 struct ll_header header;
2250 int rc = 0;
2252 if (fsm_getstate(ch->fsm) != CH_STATE_TXIDLE) {
2253 int l = skb->len + LL_HEADER_LENGTH;
2255 spin_lock_irqsave(&ch->collect_lock, saveflags);
2256 if (ch->collect_len + l > ch->max_bufsize - 2)
2257 rc = -EBUSY;
2258 else {
2259 atomic_inc(&skb->users);
2260 header.length = l;
2261 header.type = skb->protocol;
2262 header.unused = 0;
2263 memcpy(skb_push(skb, LL_HEADER_LENGTH), &header,
2264 LL_HEADER_LENGTH);
2265 skb_queue_tail(&ch->collect_queue, skb);
2266 ch->collect_len += l;
2268 spin_unlock_irqrestore(&ch->collect_lock, saveflags);
2269 } else {
2270 __u16 block_len;
2271 int ccw_idx;
2272 struct sk_buff *nskb;
2273 unsigned long hi;
2276 * Protect skb against beeing free'd by upper
2277 * layers.
2279 atomic_inc(&skb->users);
2280 ch->prof.txlen += skb->len;
2281 header.length = skb->len + LL_HEADER_LENGTH;
2282 header.type = skb->protocol;
2283 header.unused = 0;
2284 memcpy(skb_push(skb, LL_HEADER_LENGTH), &header,
2285 LL_HEADER_LENGTH);
2286 block_len = skb->len + 2;
2287 *((__u16 *) skb_push(skb, 2)) = block_len;
2290 * IDAL support in CTC is broken, so we have to
2291 * care about skb's above 2G ourselves.
2293 hi = ((unsigned long) skb->tail + LL_HEADER_LENGTH) >> 31;
2294 if (hi) {
2295 nskb = alloc_skb(skb->len, GFP_ATOMIC | GFP_DMA);
2296 if (!nskb) {
2297 atomic_dec(&skb->users);
2298 skb_pull(skb, LL_HEADER_LENGTH + 2);
2299 return -ENOMEM;
2300 } else {
2301 memcpy(skb_put(nskb, skb->len),
2302 skb->data, skb->len);
2303 atomic_inc(&nskb->users);
2304 atomic_dec(&skb->users);
2305 dev_kfree_skb_irq(skb);
2306 skb = nskb;
2310 ch->ccw[4].count = block_len;
2311 if (set_normalized_cda(&ch->ccw[4], skb->data)) {
2313 * idal allocation failed, try via copying to
2314 * trans_skb. trans_skb usually has a pre-allocated
2315 * idal.
2317 if (ctc_checkalloc_buffer(ch, 1)) {
2319 * Remove our header. It gets added
2320 * again on retransmit.
2322 atomic_dec(&skb->users);
2323 skb_pull(skb, LL_HEADER_LENGTH + 2);
2324 return -EBUSY;
2327 ch->trans_skb->tail = ch->trans_skb->data;
2328 ch->trans_skb->len = 0;
2329 ch->ccw[1].count = skb->len;
2330 memcpy(skb_put(ch->trans_skb, skb->len), skb->data,
2331 skb->len);
2332 atomic_dec(&skb->users);
2333 dev_kfree_skb_irq(skb);
2334 ccw_idx = 0;
2335 } else {
2336 skb_queue_tail(&ch->io_queue, skb);
2337 ccw_idx = 3;
2339 ch->retry = 0;
2340 fsm_newstate(ch->fsm, CH_STATE_TX);
2341 fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC, CH_EVENT_TIMER, ch);
2342 spin_lock_irqsave(get_ccwdev_lock(ch->cdev), saveflags);
2343 ch->prof.send_stamp = xtime;
2344 rc = ccw_device_start(ch->cdev, &ch->ccw[ccw_idx],
2345 (intparm_t) ch, 0xff, 0);
2346 spin_unlock_irqrestore(get_ccwdev_lock(ch->cdev), saveflags);
2347 if (ccw_idx == 3)
2348 ch->prof.doios_single++;
2349 if (rc != 0) {
2350 fsm_deltimer(&ch->timer);
2351 ccw_check_return_code(ch, rc);
2352 if (ccw_idx == 3)
2353 skb_dequeue_tail(&ch->io_queue);
2355 * Remove our header. It gets added
2356 * again on retransmit.
2358 skb_pull(skb, LL_HEADER_LENGTH + 2);
2359 } else {
2360 if (ccw_idx == 0) {
2361 struct net_device *dev = ch->netdev;
2362 struct ctc_priv *privptr = dev->priv;
2363 privptr->stats.tx_packets++;
2364 privptr->stats.tx_bytes +=
2365 skb->len - LL_HEADER_LENGTH;
2370 return rc;
2374 * Interface API for upper network layers
2375 *****************************************************************************/
2378 * Open an interface.
2379 * Called from generic network layer when ifconfig up is run.
2381 * @param dev Pointer to interface struct.
2383 * @return 0 on success, -ERRNO on failure. (Never fails.)
2385 static int
2386 ctc_open(struct net_device * dev)
2388 MOD_INC_USE_COUNT;
2389 fsm_event(((struct ctc_priv *) dev->priv)->fsm, DEV_EVENT_START, dev);
2390 return 0;
2394 * Close an interface.
2395 * Called from generic network layer when ifconfig down is run.
2397 * @param dev Pointer to interface struct.
2399 * @return 0 on success, -ERRNO on failure. (Never fails.)
2401 static int
2402 ctc_close(struct net_device * dev)
2404 SET_DEVICE_START(dev, 0);
2405 fsm_event(((struct ctc_priv *) dev->priv)->fsm, DEV_EVENT_STOP, dev);
2406 MOD_DEC_USE_COUNT;
2407 return 0;
2411 * Start transmission of a packet.
2412 * Called from generic network device layer.
2414 * @param skb Pointer to buffer containing the packet.
2415 * @param dev Pointer to interface struct.
2417 * @return 0 if packet consumed, !0 if packet rejected.
2418 * Note: If we return !0, then the packet is free'd by
2419 * the generic network layer.
2421 static int
2422 ctc_tx(struct sk_buff *skb, struct net_device * dev)
2424 int rc = 0;
2425 struct ctc_priv *privptr = (struct ctc_priv *) dev->priv;
2428 * Some sanity checks ...
2430 if (skb == NULL) {
2431 printk(KERN_WARNING "%s: NULL sk_buff passed\n", dev->name);
2432 privptr->stats.tx_dropped++;
2433 return 0;
2435 if (skb_headroom(skb) < (LL_HEADER_LENGTH + 2)) {
2436 printk(KERN_WARNING
2437 "%s: Got sk_buff with head room < %ld bytes\n",
2438 dev->name, LL_HEADER_LENGTH + 2);
2439 dev_kfree_skb(skb);
2440 privptr->stats.tx_dropped++;
2441 return 0;
2445 * If channels are not running, try to restart them
2446 * notify anybody about a link failure and throw
2447 * away packet.
2449 if (fsm_getstate(privptr->fsm) != DEV_STATE_RUNNING) {
2450 fsm_event(privptr->fsm, DEV_EVENT_START, dev);
2451 if (privptr->protocol == CTC_PROTO_LINUX_TTY)
2452 return -EBUSY;
2453 dst_link_failure(skb);
2454 dev_kfree_skb(skb);
2455 privptr->stats.tx_dropped++;
2456 privptr->stats.tx_errors++;
2457 privptr->stats.tx_carrier_errors++;
2458 return 0;
2461 if (ctc_test_and_set_busy(dev))
2462 return -EBUSY;
2464 dev->trans_start = jiffies;
2465 if (transmit_skb(privptr->channel[WRITE], skb) != 0)
2466 rc = 1;
2467 ctc_clear_busy(dev);
2468 return rc;
2472 * Sets MTU of an interface.
2474 * @param dev Pointer to interface struct.
2475 * @param new_mtu The new MTU to use for this interface.
2477 * @return 0 on success, -EINVAL if MTU is out of valid range.
2478 * (valid range is 576 .. 65527). If VM is on the
2479 * remote side, maximum MTU is 32760, however this is
2480 * <em>not</em> checked here.
2482 static int
2483 ctc_change_mtu(struct net_device * dev, int new_mtu)
2485 struct ctc_priv *privptr = (struct ctc_priv *) dev->priv;
2487 if ((new_mtu < 576) || (new_mtu > 65527) ||
2488 (new_mtu > (privptr->channel[READ]->max_bufsize -
2489 LL_HEADER_LENGTH - 2)))
2490 return -EINVAL;
2491 dev->mtu = new_mtu;
2492 dev->hard_header_len = LL_HEADER_LENGTH + 2;
2493 return 0;
2497 * Returns interface statistics of a device.
2499 * @param dev Pointer to interface struct.
2501 * @return Pointer to stats struct of this interface.
2503 static struct net_device_stats *
2504 ctc_stats(struct net_device * dev)
2506 return &((struct ctc_priv *) dev->priv)->stats;
2510 * sysfs attributes
2512 #define CTRL_BUFSIZE 40
2514 static ssize_t
2515 buffer_show(struct device *dev, char *buf)
2517 struct ctc_priv *priv;
2519 priv = dev->driver_data;
2520 if (!priv)
2521 return -ENODEV;
2522 return sprintf(buf, "%d\n",
2523 priv->channel[READ]->max_bufsize);
2526 static ssize_t
2527 buffer_write(struct device *dev, const char *buf, size_t count)
2529 struct ctc_priv *priv;
2530 struct net_device *ndev;
2531 int bs1;
2533 priv = dev->driver_data;
2534 if (!priv)
2535 return -ENODEV;
2536 ndev = container_of((void *) priv, struct net_device, priv);
2537 if (!ndev)
2538 return -ENODEV;
2539 sscanf(buf, "%u", &bs1);
2541 if (bs1 > CTC_BUFSIZE_LIMIT)
2542 return -EINVAL;
2543 if ((ndev->flags & IFF_RUNNING) &&
2544 (bs1 < (ndev->mtu + LL_HEADER_LENGTH + 2)))
2545 return -EINVAL;
2546 if (bs1 < (576 + LL_HEADER_LENGTH + 2))
2547 return -EINVAL;
2549 priv->channel[READ]->max_bufsize =
2550 priv->channel[WRITE]->max_bufsize = bs1;
2551 if (!(ndev->flags & IFF_RUNNING))
2552 ndev->mtu = bs1 - LL_HEADER_LENGTH - 2;
2553 priv->channel[READ]->flags |= CHANNEL_FLAGS_BUFSIZE_CHANGED;
2554 priv->channel[WRITE]->flags |= CHANNEL_FLAGS_BUFSIZE_CHANGED;
2556 return count;
2560 static DEVICE_ATTR(buffer, 0644, buffer_show, buffer_write);
2562 static int
2563 ctc_add_attributes(struct device *dev)
2565 return device_create_file(dev, &dev_attr_buffer);
2569 static void
2570 ctc_remove_attributes(struct device *dev)
2572 device_remove_file(dev, &dev_attr_buffer);
2576 #if 0
2577 /* FIXME: This has to be converted to another interface, as we can only have one
2578 * value per file and can't have atomicity then */
2579 #define STATS_BUFSIZE 2048
2581 static int
2582 ctc_stat_open(struct inode *inode, struct file *file)
2584 file->private_data = kmalloc(STATS_BUFSIZE, GFP_KERNEL);
2585 if (file->private_data == NULL)
2586 return -ENOMEM;
2587 MOD_INC_USE_COUNT;
2588 return 0;
2591 static int
2592 ctc_stat_close(struct inode *inode, struct file *file)
2594 kfree(file->private_data);
2595 MOD_DEC_USE_COUNT;
2596 return 0;
2599 static ssize_t
2600 ctc_stat_write(struct file *file, const char *buf, size_t count, loff_t * off)
2602 struct proc_dir_entry *pde = PDE(file->f_dentry->d_inode);
2603 struct net_device *dev;
2604 struct ctc_priv *privptr;
2606 if (!(dev = find_netdev_by_ino(pde)))
2607 return -ENODEV;
2608 privptr = (struct ctc_priv *) dev->priv;
2609 privptr->channel[WRITE]->prof.maxmulti = 0;
2610 privptr->channel[WRITE]->prof.maxcqueue = 0;
2611 privptr->channel[WRITE]->prof.doios_single = 0;
2612 privptr->channel[WRITE]->prof.doios_multi = 0;
2613 privptr->channel[WRITE]->prof.txlen = 0;
2614 privptr->channel[WRITE]->prof.tx_time = 0;
2615 return count;
2618 static ssize_t
2619 ctc_stat_read(struct file *file, char *buf, size_t count, loff_t * off)
2621 struct proc_dir_entry *pde = PDE(file->f_dentry->d_inode);
2622 char *sbuf = (char *) file->private_data;
2623 struct net_device *dev;
2624 struct ctc_priv *privptr;
2625 ssize_t ret = 0;
2626 char *p = sbuf;
2627 int l;
2629 if (!(dev = find_netdev_by_ino(pde)))
2630 return -ENODEV;
2631 if (off != &file->f_pos)
2632 return -ESPIPE;
2634 privptr = (struct ctc_priv *) dev->priv;
2636 if (file->f_pos == 0) {
2637 p += sprintf(p, "Device FSM state: %s\n",
2638 fsm_getstate_str(privptr->fsm));
2639 p += sprintf(p, "RX channel FSM state: %s\n",
2640 fsm_getstate_str(privptr->channel[READ]->fsm));
2641 p += sprintf(p, "TX channel FSM state: %s\n",
2642 fsm_getstate_str(privptr->channel[WRITE]->fsm));
2643 p += sprintf(p, "Max. TX buffer used: %ld\n",
2644 privptr->channel[WRITE]->prof.maxmulti);
2645 p += sprintf(p, "Max. chained SKBs: %ld\n",
2646 privptr->channel[WRITE]->prof.maxcqueue);
2647 p += sprintf(p, "TX single write ops: %ld\n",
2648 privptr->channel[WRITE]->prof.doios_single);
2649 p += sprintf(p, "TX multi write ops: %ld\n",
2650 privptr->channel[WRITE]->prof.doios_multi);
2651 p += sprintf(p, "Netto bytes written: %ld\n",
2652 privptr->channel[WRITE]->prof.txlen);
2653 p += sprintf(p, "Max. TX IO-time: %ld\n",
2654 privptr->channel[WRITE]->prof.tx_time);
2656 l = strlen(sbuf);
2657 p = sbuf;
2658 if (file->f_pos < l) {
2659 p += file->f_pos;
2660 l = strlen(p);
2661 ret = (count > l) ? l : count;
2662 if (copy_to_user(buf, p, ret))
2663 return -EFAULT;
2665 file->f_pos += ret;
2666 return ret;
2669 static struct file_operations ctc_stat_fops = {
2670 .read = ctc_stat_read,
2671 .write = ctc_stat_write,
2672 .open = ctc_stat_open,
2673 .release = ctc_stat_close,
2675 #endif
2677 static void
2678 ctc_netdev_unregister(struct net_device * dev)
2680 struct ctc_priv *privptr;
2682 if (!dev)
2683 return;
2684 privptr = (struct ctc_priv *) dev->priv;
2685 if (privptr->protocol != CTC_PROTO_LINUX_TTY)
2686 unregister_netdev(dev);
2687 else
2688 ctc_tty_unregister_netdev(dev);
2691 static int
2692 ctc_netdev_register(struct net_device * dev)
2694 struct ctc_priv *privptr = (struct ctc_priv *) dev->priv;
2695 if (privptr->protocol != CTC_PROTO_LINUX_TTY)
2696 return register_netdev(dev);
2697 else
2698 return ctc_tty_register_netdev(dev);
2701 static void
2702 ctc_free_netdevice(struct net_device * dev, int free_dev)
2704 struct ctc_priv *privptr;
2705 if (!dev)
2706 return;
2707 privptr = dev->priv;
2708 if (privptr) {
2709 if (privptr->fsm)
2710 kfree_fsm(privptr->fsm);
2711 kfree(privptr);
2713 #ifdef MODULE
2714 if (free_dev)
2715 kfree(dev);
2716 #endif
2720 * Initialize everything of the net device except the name and the
2721 * channel structs.
2723 static struct net_device *
2724 ctc_init_netdevice(struct net_device * dev, int alloc_device,
2725 struct ctc_priv *privptr)
2727 if (!privptr)
2728 return NULL;
2730 if (alloc_device) {
2731 dev = kmalloc(sizeof (struct net_device), GFP_KERNEL);
2732 if (!dev)
2733 return NULL;
2734 memset(dev, 0, sizeof (struct net_device));
2737 dev->priv = privptr;
2738 privptr->fsm = init_fsm("ctcdev", dev_state_names,
2739 dev_event_names, NR_DEV_STATES, NR_DEV_EVENTS,
2740 dev_fsm, DEV_FSM_LEN, GFP_KERNEL);
2741 if (privptr->fsm == NULL) {
2742 if (alloc_device)
2743 kfree(dev);
2744 return NULL;
2746 fsm_newstate(privptr->fsm, DEV_STATE_STOPPED);
2747 fsm_settimer(privptr->fsm, &privptr->restart_timer);
2748 dev->mtu = CTC_BUFSIZE_DEFAULT - LL_HEADER_LENGTH - 2;
2749 dev->hard_start_xmit = ctc_tx;
2750 dev->open = ctc_open;
2751 dev->stop = ctc_close;
2752 dev->get_stats = ctc_stats;
2753 dev->change_mtu = ctc_change_mtu;
2754 dev->hard_header_len = LL_HEADER_LENGTH + 2;
2755 dev->addr_len = 0;
2756 dev->type = ARPHRD_SLIP;
2757 dev->tx_queue_len = 100;
2758 SET_DEVICE_START(dev, 1);
2759 dev_init_buffers(dev);
2760 dev->flags = IFF_POINTOPOINT | IFF_NOARP;
2761 return dev;
2764 static ssize_t
2765 ctc_proto_show(struct device *dev, char *buf)
2767 struct ctc_priv *priv;
2769 priv = dev->driver_data;
2770 if (!priv)
2771 return -ENODEV;
2773 return sprintf(buf, "%d\n", priv->protocol);
2776 static ssize_t
2777 ctc_proto_store(struct device *dev, const char *buf, size_t count)
2779 struct ctc_priv *priv;
2780 int value;
2782 priv = dev->driver_data;
2783 if (!priv)
2784 return -ENODEV;
2785 sscanf(buf, "%u", &value);
2786 /* TODO: sanity checks */
2787 priv->protocol = value;
2789 return count;
2792 static DEVICE_ATTR(protocol, 0644, ctc_proto_show, ctc_proto_store);
2794 static int
2795 ctc_add_files(struct device *dev)
2797 return device_create_file(dev, &dev_attr_protocol);
2801 static void
2802 ctc_remove_files(struct device *dev)
2804 device_remove_file(dev, &dev_attr_protocol);
2809 * Add ctc specific attributes.
2810 * Add ctc private data.
2812 * @param cgdev pointer to ccwgroup_device just added
2814 * @returns 0 on success, !0 on failure.
2817 static int
2818 ctc_probe_device(struct ccwgroup_device *cgdev)
2820 struct ctc_priv *priv;
2821 int rc;
2823 if (!get_device(&cgdev->dev))
2824 return -ENODEV;
2826 priv = kmalloc(sizeof (struct ctc_priv), GFP_KERNEL);
2827 if (!priv) {
2828 printk(KERN_ERR "%s: Out of memory\n", __func__);
2829 put_device(&cgdev->dev);
2830 return -ENOMEM;
2833 memset(priv, 0, sizeof (struct ctc_priv));
2834 rc = ctc_add_files(&cgdev->dev);
2835 if (rc) {
2836 kfree(priv);
2837 put_device(&cgdev->dev);
2838 return rc;
2841 cgdev->cdev[0]->handler = ctc_irq_handler;
2842 cgdev->cdev[1]->handler = ctc_irq_handler;
2843 cgdev->dev.driver_data = priv;
2844 cgdev->cdev[0]->dev.driver_data = priv;
2845 cgdev->cdev[1]->dev.driver_data = priv;
2846 snprintf(cgdev->dev.name, DEVICE_NAME_SIZE, "%s",
2847 cu3088_type[cgdev->cdev[0]->id.driver_info]);
2849 return 0;
2854 * Setup an interface.
2856 * @param cgdev Device to be setup.
2858 * @returns 0 on success, !0 on failure.
2860 static int
2861 ctc_new_device(struct ccwgroup_device *cgdev)
2863 char read_id[CTC_ID_SIZE];
2864 char write_id[CTC_ID_SIZE];
2865 int direction;
2866 enum channel_types type;
2867 struct ctc_priv *privptr;
2868 struct net_device *dev;
2870 privptr = cgdev->dev.driver_data;
2871 if (!privptr)
2872 return -ENODEV;
2874 type = get_channel_type(&cgdev->cdev[0]->id);
2876 snprintf(read_id, DEVICE_ID_SIZE, "ch-%s", cgdev->cdev[0]->dev.bus_id);
2877 snprintf(write_id, DEVICE_ID_SIZE, "ch-%s", cgdev->cdev[1]->dev.bus_id);
2879 if (add_channel(cgdev->cdev[0], type))
2880 return -ENOMEM;
2881 if (add_channel(cgdev->cdev[1], type))
2882 return -ENOMEM;
2884 dev = ctc_init_netdevice(NULL, 1, privptr);
2886 if (!dev) {
2887 printk(KERN_WARNING "ctc_init_netdevice failed\n");
2888 return -ENODEV;
2891 if (privptr->protocol == CTC_PROTO_LINUX_TTY)
2892 snprintf(dev->name, 8, "ctctty%%d");
2893 else
2894 snprintf(dev->name, 8, "ctc%%d");
2896 for (direction = READ; direction <= WRITE; direction++) {
2897 privptr->channel[direction] =
2898 channel_get(type, direction == READ ? read_id : write_id,
2899 direction);
2900 if (privptr->channel[direction] == NULL) {
2901 if (direction == WRITE)
2902 channel_free(privptr->channel[READ]);
2904 ctc_free_netdevice(dev, 1);
2905 return -ENODEV;
2907 privptr->channel[direction]->netdev = dev;
2908 privptr->channel[direction]->protocol = privptr->protocol;
2909 privptr->channel[direction]->max_bufsize = CTC_BUFSIZE_DEFAULT;
2911 if (ctc_netdev_register(dev) != 0) {
2912 ctc_free_netdevice(dev, 1);
2913 return -ENODEV;
2916 ctc_add_attributes(&cgdev->dev);
2918 strncpy(privptr->fsm->name, dev->name, sizeof (privptr->fsm->name));
2920 print_banner();
2922 printk(KERN_INFO
2923 "%s: read: %s, write: %s, proto: %d\n",
2924 dev->name, privptr->channel[READ]->id,
2925 privptr->channel[WRITE]->id, privptr->protocol);
2927 return 0;
2931 * Shutdown an interface.
2933 * @param cgdev Device to be shut down.
2935 * @returns 0 on success, !0 on failure.
2937 static int
2938 ctc_shutdown_device(struct ccwgroup_device *cgdev)
2940 struct ctc_priv *priv;
2941 struct net_device *ndev;
2944 priv = cgdev->dev.driver_data;
2945 if (!priv)
2946 return -ENODEV;
2947 ndev = container_of((void *)priv, struct net_device, priv);
2949 /* Close the device */
2950 ctc_close(ndev);
2951 ndev->flags &=~IFF_RUNNING;
2953 ctc_remove_attributes(&cgdev->dev);
2955 channel_free(priv->channel[READ]);
2956 channel_free(priv->channel[WRITE]);
2958 ctc_netdev_unregister(ndev);
2959 ndev->priv = NULL;
2960 ctc_free_netdevice(ndev, 1);
2962 kfree_fsm(priv->fsm);
2964 channel_remove(priv->channel[READ]);
2965 channel_remove(priv->channel[WRITE]);
2967 return 0;
2971 static int
2972 ctc_remove_device(struct ccwgroup_device *cgdev)
2974 struct ctc_priv *priv;
2976 priv = cgdev->dev.driver_data;
2977 if (!priv)
2978 return 0;
2980 ctc_remove_files(&cgdev->dev);
2981 cgdev->dev.driver_data = NULL;
2982 kfree(priv);
2983 put_device(&cgdev->dev);
2984 return 0;
2987 static struct ccwgroup_driver ctc_group_driver = {
2988 .name = "ctc",
2989 .max_slaves = 2,
2990 .driver_id = 0xC3E3C3,
2991 .probe = ctc_probe_device,
2992 .remove = ctc_remove_device,
2993 .set_online = ctc_new_device,
2994 .set_offline = ctc_shutdown_device,
2998 * Module related routines
2999 *****************************************************************************/
3002 * Prepare to be unloaded. Free IRQ's and release all resources.
3003 * This is called just before this module is unloaded. It is
3004 * <em>not</em> called, if the usage count is !0, so we don't need to check
3005 * for that.
3007 static void __exit
3008 ctc_exit(void)
3010 unregister_cu3088_discipline(&ctc_group_driver);
3011 ctc_tty_cleanup();
3012 printk(KERN_INFO "CTC driver unloaded\n");
3016 * Initialize module.
3017 * This is called just after the module is loaded.
3019 * @return 0 on success, !0 on error.
3021 static int __init
3022 ctc_init(void)
3024 int ret = 0;
3026 print_banner();
3028 ctc_tty_init();
3029 ret = register_cu3088_discipline(&ctc_group_driver);
3030 if (ret)
3031 ctc_tty_cleanup();
3032 return ret;
3035 module_init(ctc_init);
3036 module_exit(ctc_exit);
3038 /* --- This is the END my friend --- */