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