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