blob: b0f813e6f48ee1fa79aa7ace887e5db340a05eff [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 }
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300475 skb_copy_from_linear_data(pskb, skb_put(skb, pskb->len),
476 pskb->len);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700477 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 skb->dev = pskb->dev;
479 skb->protocol = pskb->protocol;
480 pskb->ip_summed = CHECKSUM_UNNECESSARY;
Frank Pavlic56347a22006-04-13 20:19:12 +0200481 netif_rx_ni(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 /**
483 * Successful rx; reset logflags
484 */
485 ch->logflags = 0;
486 dev->last_rx = jiffies;
487 privptr->stats.rx_packets++;
488 privptr->stats.rx_bytes += skb->len;
489 if (len > 0) {
490 skb_pull(pskb, header->length);
491 if (skb_tailroom(pskb) < LL_HEADER_LENGTH) {
492#ifndef DEBUG
493 if (!(ch->logflags & LOG_FLAG_OVERRUN)) {
494#endif
495 ctc_pr_warn(
496 "%s Overrun in ctc_unpack_skb\n",
497 dev->name);
498 ch->logflags |= LOG_FLAG_OVERRUN;
499#ifndef DEBUG
500 }
501#endif
502 return;
503 }
504 skb_put(pskb, LL_HEADER_LENGTH);
505 }
506 }
507}
508
509/**
510 * Check return code of a preceeding ccw_device call, halt_IO etc...
511 *
512 * @param ch The channel, the error belongs to.
513 * @param return_code The error code to inspect.
514 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100515static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516ccw_check_return_code(struct channel *ch, int return_code, char *msg)
517{
518 DBF_TEXT(trace, 5, __FUNCTION__);
519 switch (return_code) {
520 case 0:
521 fsm_event(ch->fsm, CH_EVENT_IO_SUCCESS, ch);
522 break;
523 case -EBUSY:
524 ctc_pr_warn("%s (%s): Busy !\n", ch->id, msg);
525 fsm_event(ch->fsm, CH_EVENT_IO_EBUSY, ch);
526 break;
527 case -ENODEV:
528 ctc_pr_emerg("%s (%s): Invalid device called for IO\n",
529 ch->id, msg);
530 fsm_event(ch->fsm, CH_EVENT_IO_ENODEV, ch);
531 break;
532 case -EIO:
533 ctc_pr_emerg("%s (%s): Status pending... \n",
534 ch->id, msg);
535 fsm_event(ch->fsm, CH_EVENT_IO_EIO, ch);
536 break;
537 default:
538 ctc_pr_emerg("%s (%s): Unknown error in do_IO %04x\n",
539 ch->id, msg, return_code);
540 fsm_event(ch->fsm, CH_EVENT_IO_UNKNOWN, ch);
541 }
542}
543
544/**
545 * Check sense of a unit check.
546 *
547 * @param ch The channel, the sense code belongs to.
548 * @param sense The sense code to inspect.
549 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100550static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551ccw_unit_check(struct channel *ch, unsigned char sense)
552{
553 DBF_TEXT(trace, 5, __FUNCTION__);
554 if (sense & SNS0_INTERVENTION_REQ) {
555 if (sense & 0x01) {
Frank Pavlic56347a22006-04-13 20:19:12 +0200556 ctc_pr_debug("%s: Interface disc. or Sel. reset "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 "(remote)\n", ch->id);
558 fsm_event(ch->fsm, CH_EVENT_UC_RCRESET, ch);
559 } else {
560 ctc_pr_debug("%s: System reset (remote)\n", ch->id);
561 fsm_event(ch->fsm, CH_EVENT_UC_RSRESET, ch);
562 }
563 } else if (sense & SNS0_EQUIPMENT_CHECK) {
564 if (sense & SNS0_BUS_OUT_CHECK) {
565 ctc_pr_warn("%s: Hardware malfunction (remote)\n",
566 ch->id);
567 fsm_event(ch->fsm, CH_EVENT_UC_HWFAIL, ch);
568 } else {
569 ctc_pr_warn("%s: Read-data parity error (remote)\n",
570 ch->id);
571 fsm_event(ch->fsm, CH_EVENT_UC_RXPARITY, ch);
572 }
573 } else if (sense & SNS0_BUS_OUT_CHECK) {
574 if (sense & 0x04) {
575 ctc_pr_warn("%s: Data-streaming timeout)\n", ch->id);
576 fsm_event(ch->fsm, CH_EVENT_UC_TXTIMEOUT, ch);
577 } else {
578 ctc_pr_warn("%s: Data-transfer parity error\n", ch->id);
579 fsm_event(ch->fsm, CH_EVENT_UC_TXPARITY, ch);
580 }
581 } else if (sense & SNS0_CMD_REJECT) {
582 ctc_pr_warn("%s: Command reject\n", ch->id);
583 } else if (sense == 0) {
584 ctc_pr_debug("%s: Unit check ZERO\n", ch->id);
585 fsm_event(ch->fsm, CH_EVENT_UC_ZERO, ch);
586 } else {
587 ctc_pr_warn("%s: Unit Check with sense code: %02x\n",
588 ch->id, sense);
589 fsm_event(ch->fsm, CH_EVENT_UC_UNKNOWN, ch);
590 }
591}
592
593static void
594ctc_purge_skb_queue(struct sk_buff_head *q)
595{
596 struct sk_buff *skb;
597
598 DBF_TEXT(trace, 5, __FUNCTION__);
599
600 while ((skb = skb_dequeue(q))) {
601 atomic_dec(&skb->users);
602 dev_kfree_skb_irq(skb);
603 }
604}
605
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100606static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607ctc_checkalloc_buffer(struct channel *ch, int warn)
608{
609 DBF_TEXT(trace, 5, __FUNCTION__);
610 if ((ch->trans_skb == NULL) ||
611 (ch->flags & CHANNEL_FLAGS_BUFSIZE_CHANGED)) {
612 if (ch->trans_skb != NULL)
613 dev_kfree_skb(ch->trans_skb);
614 clear_normalized_cda(&ch->ccw[1]);
615 ch->trans_skb = __dev_alloc_skb(ch->max_bufsize,
616 GFP_ATOMIC | GFP_DMA);
617 if (ch->trans_skb == NULL) {
618 if (warn)
619 ctc_pr_warn(
620 "%s: Couldn't alloc %s trans_skb\n",
621 ch->id,
622 (CHANNEL_DIRECTION(ch->flags) == READ) ?
623 "RX" : "TX");
624 return -ENOMEM;
625 }
626 ch->ccw[1].count = ch->max_bufsize;
627 if (set_normalized_cda(&ch->ccw[1], ch->trans_skb->data)) {
628 dev_kfree_skb(ch->trans_skb);
629 ch->trans_skb = NULL;
630 if (warn)
631 ctc_pr_warn(
632 "%s: set_normalized_cda for %s "
633 "trans_skb failed, dropping packets\n",
634 ch->id,
635 (CHANNEL_DIRECTION(ch->flags) == READ) ?
636 "RX" : "TX");
637 return -ENOMEM;
638 }
639 ch->ccw[1].count = 0;
640 ch->trans_skb_data = ch->trans_skb->data;
641 ch->flags &= ~CHANNEL_FLAGS_BUFSIZE_CHANGED;
642 }
643 return 0;
644}
645
646/**
647 * Dummy NOP action for statemachines
648 */
649static void
650fsm_action_nop(fsm_instance * fi, int event, void *arg)
651{
652}
Frank Pavlice172577d2005-09-08 09:50:06 +0200653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654/**
655 * Actions for channel - statemachines.
656 *****************************************************************************/
657
658/**
659 * Normal data has been send. Free the corresponding
660 * skb (it's in io_queue), reset dev->tbusy and
661 * revert to idle state.
662 *
663 * @param fi An instance of a channel statemachine.
664 * @param event The event, just happened.
665 * @param arg Generic pointer, casted from channel * upon call.
666 */
667static void
668ch_action_txdone(fsm_instance * fi, int event, void *arg)
669{
670 struct channel *ch = (struct channel *) arg;
671 struct net_device *dev = ch->netdev;
672 struct ctc_priv *privptr = dev->priv;
673 struct sk_buff *skb;
674 int first = 1;
675 int i;
676 unsigned long duration;
677 struct timespec done_stamp = xtime;
678
679 DBF_TEXT(trace, 4, __FUNCTION__);
680
681 duration =
682 (done_stamp.tv_sec - ch->prof.send_stamp.tv_sec) * 1000000 +
683 (done_stamp.tv_nsec - ch->prof.send_stamp.tv_nsec) / 1000;
684 if (duration > ch->prof.tx_time)
685 ch->prof.tx_time = duration;
686
687 if (ch->irb->scsw.count != 0)
688 ctc_pr_debug("%s: TX not complete, remaining %d bytes\n",
689 dev->name, ch->irb->scsw.count);
690 fsm_deltimer(&ch->timer);
691 while ((skb = skb_dequeue(&ch->io_queue))) {
692 privptr->stats.tx_packets++;
693 privptr->stats.tx_bytes += skb->len - LL_HEADER_LENGTH;
694 if (first) {
695 privptr->stats.tx_bytes += 2;
696 first = 0;
697 }
698 atomic_dec(&skb->users);
699 dev_kfree_skb_irq(skb);
700 }
701 spin_lock(&ch->collect_lock);
702 clear_normalized_cda(&ch->ccw[4]);
703 if (ch->collect_len > 0) {
704 int rc;
705
706 if (ctc_checkalloc_buffer(ch, 1)) {
707 spin_unlock(&ch->collect_lock);
708 return;
709 }
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700710 ch->trans_skb->data = ch->trans_skb_data;
711 skb_reset_tail_pointer(ch->trans_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 ch->trans_skb->len = 0;
713 if (ch->prof.maxmulti < (ch->collect_len + 2))
714 ch->prof.maxmulti = ch->collect_len + 2;
715 if (ch->prof.maxcqueue < skb_queue_len(&ch->collect_queue))
716 ch->prof.maxcqueue = skb_queue_len(&ch->collect_queue);
717 *((__u16 *) skb_put(ch->trans_skb, 2)) = ch->collect_len + 2;
718 i = 0;
719 while ((skb = skb_dequeue(&ch->collect_queue))) {
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300720 skb_copy_from_linear_data(skb, skb_put(ch->trans_skb,
721 skb->len),
722 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 privptr->stats.tx_packets++;
724 privptr->stats.tx_bytes += skb->len - LL_HEADER_LENGTH;
725 atomic_dec(&skb->users);
726 dev_kfree_skb_irq(skb);
727 i++;
728 }
729 ch->collect_len = 0;
730 spin_unlock(&ch->collect_lock);
731 ch->ccw[1].count = ch->trans_skb->len;
732 fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC, CH_EVENT_TIMER, ch);
733 ch->prof.send_stamp = xtime;
734 rc = ccw_device_start(ch->cdev, &ch->ccw[0],
735 (unsigned long) ch, 0xff, 0);
736 ch->prof.doios_multi++;
737 if (rc != 0) {
738 privptr->stats.tx_dropped += i;
739 privptr->stats.tx_errors += i;
740 fsm_deltimer(&ch->timer);
741 ccw_check_return_code(ch, rc, "chained TX");
742 }
743 } else {
744 spin_unlock(&ch->collect_lock);
745 fsm_newstate(fi, CH_STATE_TXIDLE);
746 }
747 ctc_clear_busy(dev);
748}
749
750/**
751 * Initial data is sent.
752 * Notify device statemachine that we are up and
753 * running.
754 *
755 * @param fi An instance of a channel statemachine.
756 * @param event The event, just happened.
757 * @param arg Generic pointer, casted from channel * upon call.
758 */
759static void
760ch_action_txidle(fsm_instance * fi, int event, void *arg)
761{
762 struct channel *ch = (struct channel *) arg;
763
764 DBF_TEXT(trace, 4, __FUNCTION__);
765 fsm_deltimer(&ch->timer);
766 fsm_newstate(fi, CH_STATE_TXIDLE);
767 fsm_event(((struct ctc_priv *) ch->netdev->priv)->fsm, DEV_EVENT_TXUP,
768 ch->netdev);
769}
770
771/**
772 * Got normal data, check for sanity, queue it up, allocate new buffer
773 * trigger bottom half, and initiate next read.
774 *
775 * @param fi An instance of a channel statemachine.
776 * @param event The event, just happened.
777 * @param arg Generic pointer, casted from channel * upon call.
778 */
779static void
780ch_action_rx(fsm_instance * fi, int event, void *arg)
781{
782 struct channel *ch = (struct channel *) arg;
783 struct net_device *dev = ch->netdev;
784 struct ctc_priv *privptr = dev->priv;
785 int len = ch->max_bufsize - ch->irb->scsw.count;
786 struct sk_buff *skb = ch->trans_skb;
787 __u16 block_len = *((__u16 *) skb->data);
788 int check_len;
789 int rc;
790
791 DBF_TEXT(trace, 4, __FUNCTION__);
792 fsm_deltimer(&ch->timer);
793 if (len < 8) {
794 ctc_pr_debug("%s: got packet with length %d < 8\n",
795 dev->name, len);
796 privptr->stats.rx_dropped++;
797 privptr->stats.rx_length_errors++;
798 goto again;
799 }
800 if (len > ch->max_bufsize) {
801 ctc_pr_debug("%s: got packet with length %d > %d\n",
802 dev->name, len, ch->max_bufsize);
803 privptr->stats.rx_dropped++;
804 privptr->stats.rx_length_errors++;
805 goto again;
806 }
807
808 /**
809 * VM TCP seems to have a bug sending 2 trailing bytes of garbage.
810 */
811 switch (ch->protocol) {
812 case CTC_PROTO_S390:
813 case CTC_PROTO_OS390:
814 check_len = block_len + 2;
815 break;
816 default:
817 check_len = block_len;
818 break;
819 }
820 if ((len < block_len) || (len > check_len)) {
821 ctc_pr_debug("%s: got block length %d != rx length %d\n",
822 dev->name, block_len, len);
823#ifdef DEBUG
824 ctc_dump_skb(skb, 0);
825#endif
826 *((__u16 *) skb->data) = len;
827 privptr->stats.rx_dropped++;
828 privptr->stats.rx_length_errors++;
829 goto again;
830 }
831 block_len -= 2;
832 if (block_len > 0) {
833 *((__u16 *) skb->data) = block_len;
834 ctc_unpack_skb(ch, skb);
835 }
836 again:
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700837 skb->data = ch->trans_skb_data;
838 skb_reset_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 skb->len = 0;
840 if (ctc_checkalloc_buffer(ch, 1))
841 return;
842 ch->ccw[1].count = ch->max_bufsize;
843 rc = ccw_device_start(ch->cdev, &ch->ccw[0], (unsigned long) ch, 0xff, 0);
844 if (rc != 0)
845 ccw_check_return_code(ch, rc, "normal RX");
846}
847
848static void ch_action_rxidle(fsm_instance * fi, int event, void *arg);
849
850/**
851 * Initialize connection by sending a __u16 of value 0.
852 *
853 * @param fi An instance of a channel statemachine.
854 * @param event The event, just happened.
855 * @param arg Generic pointer, casted from channel * upon call.
856 */
857static void
858ch_action_firstio(fsm_instance * fi, int event, void *arg)
859{
860 struct channel *ch = (struct channel *) arg;
861 int rc;
862
863 DBF_TEXT(trace, 4, __FUNCTION__);
864
865 if (fsm_getstate(fi) == CH_STATE_TXIDLE)
866 ctc_pr_debug("%s: remote side issued READ?, init ...\n", ch->id);
867 fsm_deltimer(&ch->timer);
868 if (ctc_checkalloc_buffer(ch, 1))
869 return;
870 if ((fsm_getstate(fi) == CH_STATE_SETUPWAIT) &&
871 (ch->protocol == CTC_PROTO_OS390)) {
872 /* OS/390 resp. z/OS */
873 if (CHANNEL_DIRECTION(ch->flags) == READ) {
874 *((__u16 *) ch->trans_skb->data) = CTC_INITIAL_BLOCKLEN;
875 fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC,
876 CH_EVENT_TIMER, ch);
877 ch_action_rxidle(fi, event, arg);
878 } else {
879 struct net_device *dev = ch->netdev;
880 fsm_newstate(fi, CH_STATE_TXIDLE);
881 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
882 DEV_EVENT_TXUP, dev);
883 }
884 return;
885 }
886
887 /**
888 * Don´t setup a timer for receiving the initial RX frame
889 * if in compatibility mode, since VM TCP delays the initial
890 * frame until it has some data to send.
891 */
892 if ((CHANNEL_DIRECTION(ch->flags) == WRITE) ||
893 (ch->protocol != CTC_PROTO_S390))
894 fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC, CH_EVENT_TIMER, ch);
895
896 *((__u16 *) ch->trans_skb->data) = CTC_INITIAL_BLOCKLEN;
897 ch->ccw[1].count = 2; /* Transfer only length */
898
899 fsm_newstate(fi, (CHANNEL_DIRECTION(ch->flags) == READ)
900 ? CH_STATE_RXINIT : CH_STATE_TXINIT);
901 rc = ccw_device_start(ch->cdev, &ch->ccw[0], (unsigned long) ch, 0xff, 0);
902 if (rc != 0) {
903 fsm_deltimer(&ch->timer);
904 fsm_newstate(fi, CH_STATE_SETUPWAIT);
905 ccw_check_return_code(ch, rc, "init IO");
906 }
907 /**
908 * If in compatibility mode since we don´t setup a timer, we
909 * also signal RX channel up immediately. This enables us
910 * to send packets early which in turn usually triggers some
911 * reply from VM TCP which brings up the RX channel to it´s
912 * final state.
913 */
914 if ((CHANNEL_DIRECTION(ch->flags) == READ) &&
915 (ch->protocol == CTC_PROTO_S390)) {
916 struct net_device *dev = ch->netdev;
917 fsm_event(((struct ctc_priv *) dev->priv)->fsm, DEV_EVENT_RXUP,
918 dev);
919 }
920}
921
922/**
923 * Got initial data, check it. If OK,
924 * notify device statemachine that we are up and
925 * running.
926 *
927 * @param fi An instance of a channel statemachine.
928 * @param event The event, just happened.
929 * @param arg Generic pointer, casted from channel * upon call.
930 */
931static void
932ch_action_rxidle(fsm_instance * fi, int event, void *arg)
933{
934 struct channel *ch = (struct channel *) arg;
935 struct net_device *dev = ch->netdev;
936 __u16 buflen;
937 int rc;
938
939 DBF_TEXT(trace, 4, __FUNCTION__);
940 fsm_deltimer(&ch->timer);
941 buflen = *((__u16 *) ch->trans_skb->data);
942#ifdef DEBUG
943 ctc_pr_debug("%s: Initial RX count %d\n", dev->name, buflen);
944#endif
945 if (buflen >= CTC_INITIAL_BLOCKLEN) {
946 if (ctc_checkalloc_buffer(ch, 1))
947 return;
948 ch->ccw[1].count = ch->max_bufsize;
949 fsm_newstate(fi, CH_STATE_RXIDLE);
950 rc = ccw_device_start(ch->cdev, &ch->ccw[0],
951 (unsigned long) ch, 0xff, 0);
952 if (rc != 0) {
953 fsm_newstate(fi, CH_STATE_RXINIT);
954 ccw_check_return_code(ch, rc, "initial RX");
955 } else
956 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
957 DEV_EVENT_RXUP, dev);
958 } else {
959 ctc_pr_debug("%s: Initial RX count %d not %d\n",
960 dev->name, buflen, CTC_INITIAL_BLOCKLEN);
961 ch_action_firstio(fi, event, arg);
962 }
963}
964
965/**
966 * Set channel into extended mode.
967 *
968 * @param fi An instance of a channel statemachine.
969 * @param event The event, just happened.
970 * @param arg Generic pointer, casted from channel * upon call.
971 */
972static void
973ch_action_setmode(fsm_instance * fi, int event, void *arg)
974{
975 struct channel *ch = (struct channel *) arg;
976 int rc;
977 unsigned long saveflags;
978
979 DBF_TEXT(trace, 4, __FUNCTION__);
980 fsm_deltimer(&ch->timer);
981 fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC, CH_EVENT_TIMER, ch);
982 fsm_newstate(fi, CH_STATE_SETUPWAIT);
983 saveflags = 0; /* avoids compiler warning with
984 spin_unlock_irqrestore */
985 if (event == CH_EVENT_TIMER) // only for timer not yet locked
986 spin_lock_irqsave(get_ccwdev_lock(ch->cdev), saveflags);
987 rc = ccw_device_start(ch->cdev, &ch->ccw[6], (unsigned long) ch, 0xff, 0);
988 if (event == CH_EVENT_TIMER)
989 spin_unlock_irqrestore(get_ccwdev_lock(ch->cdev), saveflags);
990 if (rc != 0) {
991 fsm_deltimer(&ch->timer);
992 fsm_newstate(fi, CH_STATE_STARTWAIT);
993 ccw_check_return_code(ch, rc, "set Mode");
994 } else
995 ch->retry = 0;
996}
997
998/**
999 * Setup channel.
1000 *
1001 * @param fi An instance of a channel statemachine.
1002 * @param event The event, just happened.
1003 * @param arg Generic pointer, casted from channel * upon call.
1004 */
1005static void
1006ch_action_start(fsm_instance * fi, int event, void *arg)
1007{
1008 struct channel *ch = (struct channel *) arg;
1009 unsigned long saveflags;
1010 int rc;
1011 struct net_device *dev;
1012
1013 DBF_TEXT(trace, 4, __FUNCTION__);
1014 if (ch == NULL) {
1015 ctc_pr_warn("ch_action_start ch=NULL\n");
1016 return;
1017 }
1018 if (ch->netdev == NULL) {
1019 ctc_pr_warn("ch_action_start dev=NULL, id=%s\n", ch->id);
1020 return;
1021 }
1022 dev = ch->netdev;
1023
1024#ifdef DEBUG
1025 ctc_pr_debug("%s: %s channel start\n", dev->name,
1026 (CHANNEL_DIRECTION(ch->flags) == READ) ? "RX" : "TX");
1027#endif
1028
1029 if (ch->trans_skb != NULL) {
1030 clear_normalized_cda(&ch->ccw[1]);
1031 dev_kfree_skb(ch->trans_skb);
1032 ch->trans_skb = NULL;
1033 }
1034 if (CHANNEL_DIRECTION(ch->flags) == READ) {
1035 ch->ccw[1].cmd_code = CCW_CMD_READ;
1036 ch->ccw[1].flags = CCW_FLAG_SLI;
1037 ch->ccw[1].count = 0;
1038 } else {
1039 ch->ccw[1].cmd_code = CCW_CMD_WRITE;
1040 ch->ccw[1].flags = CCW_FLAG_SLI | CCW_FLAG_CC;
1041 ch->ccw[1].count = 0;
1042 }
1043 if (ctc_checkalloc_buffer(ch, 0)) {
1044 ctc_pr_notice(
1045 "%s: Could not allocate %s trans_skb, delaying "
1046 "allocation until first transfer\n",
1047 dev->name,
1048 (CHANNEL_DIRECTION(ch->flags) == READ) ? "RX" : "TX");
1049 }
1050
1051 ch->ccw[0].cmd_code = CCW_CMD_PREPARE;
1052 ch->ccw[0].flags = CCW_FLAG_SLI | CCW_FLAG_CC;
1053 ch->ccw[0].count = 0;
1054 ch->ccw[0].cda = 0;
1055 ch->ccw[2].cmd_code = CCW_CMD_NOOP; /* jointed CE + DE */
1056 ch->ccw[2].flags = CCW_FLAG_SLI;
1057 ch->ccw[2].count = 0;
1058 ch->ccw[2].cda = 0;
1059 memcpy(&ch->ccw[3], &ch->ccw[0], sizeof (struct ccw1) * 3);
1060 ch->ccw[4].cda = 0;
1061 ch->ccw[4].flags &= ~CCW_FLAG_IDA;
1062
1063 fsm_newstate(fi, CH_STATE_STARTWAIT);
1064 fsm_addtimer(&ch->timer, 1000, CH_EVENT_TIMER, ch);
1065 spin_lock_irqsave(get_ccwdev_lock(ch->cdev), saveflags);
1066 rc = ccw_device_halt(ch->cdev, (unsigned long) ch);
1067 spin_unlock_irqrestore(get_ccwdev_lock(ch->cdev), saveflags);
1068 if (rc != 0) {
1069 if (rc != -EBUSY)
1070 fsm_deltimer(&ch->timer);
1071 ccw_check_return_code(ch, rc, "initial HaltIO");
1072 }
1073#ifdef DEBUG
1074 ctc_pr_debug("ctc: %s(): leaving\n", __func__);
1075#endif
1076}
1077
1078/**
1079 * Shutdown a channel.
1080 *
1081 * @param fi An instance of a channel statemachine.
1082 * @param event The event, just happened.
1083 * @param arg Generic pointer, casted from channel * upon call.
1084 */
1085static void
1086ch_action_haltio(fsm_instance * fi, int event, void *arg)
1087{
1088 struct channel *ch = (struct channel *) arg;
1089 unsigned long saveflags;
1090 int rc;
1091 int oldstate;
1092
1093 DBF_TEXT(trace, 3, __FUNCTION__);
1094 fsm_deltimer(&ch->timer);
1095 fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC, CH_EVENT_TIMER, ch);
1096 saveflags = 0; /* avoids comp warning with
1097 spin_unlock_irqrestore */
1098 if (event == CH_EVENT_STOP) // only for STOP not yet locked
1099 spin_lock_irqsave(get_ccwdev_lock(ch->cdev), saveflags);
1100 oldstate = fsm_getstate(fi);
1101 fsm_newstate(fi, CH_STATE_TERM);
1102 rc = ccw_device_halt(ch->cdev, (unsigned long) ch);
1103 if (event == CH_EVENT_STOP)
1104 spin_unlock_irqrestore(get_ccwdev_lock(ch->cdev), saveflags);
1105 if (rc != 0) {
1106 if (rc != -EBUSY) {
1107 fsm_deltimer(&ch->timer);
1108 fsm_newstate(fi, oldstate);
1109 }
1110 ccw_check_return_code(ch, rc, "HaltIO in ch_action_haltio");
1111 }
1112}
1113
1114/**
1115 * A channel has successfully been halted.
1116 * Cleanup it's queue and notify interface statemachine.
1117 *
1118 * @param fi An instance of a channel statemachine.
1119 * @param event The event, just happened.
1120 * @param arg Generic pointer, casted from channel * upon call.
1121 */
1122static void
1123ch_action_stopped(fsm_instance * fi, int event, void *arg)
1124{
1125 struct channel *ch = (struct channel *) arg;
1126 struct net_device *dev = ch->netdev;
1127
1128 DBF_TEXT(trace, 3, __FUNCTION__);
1129 fsm_deltimer(&ch->timer);
1130 fsm_newstate(fi, CH_STATE_STOPPED);
1131 if (ch->trans_skb != NULL) {
1132 clear_normalized_cda(&ch->ccw[1]);
1133 dev_kfree_skb(ch->trans_skb);
1134 ch->trans_skb = NULL;
1135 }
1136 if (CHANNEL_DIRECTION(ch->flags) == READ) {
1137 skb_queue_purge(&ch->io_queue);
1138 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1139 DEV_EVENT_RXDOWN, dev);
1140 } else {
1141 ctc_purge_skb_queue(&ch->io_queue);
1142 spin_lock(&ch->collect_lock);
1143 ctc_purge_skb_queue(&ch->collect_queue);
1144 ch->collect_len = 0;
1145 spin_unlock(&ch->collect_lock);
1146 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1147 DEV_EVENT_TXDOWN, dev);
1148 }
1149}
1150
1151/**
1152 * A stop command from device statemachine arrived and we are in
1153 * not operational mode. Set state to stopped.
1154 *
1155 * @param fi An instance of a channel statemachine.
1156 * @param event The event, just happened.
1157 * @param arg Generic pointer, casted from channel * upon call.
1158 */
1159static void
1160ch_action_stop(fsm_instance * fi, int event, void *arg)
1161{
1162 fsm_newstate(fi, CH_STATE_STOPPED);
1163}
1164
1165/**
1166 * A machine check for no path, not operational status or gone device has
1167 * happened.
1168 * Cleanup queue and notify interface statemachine.
1169 *
1170 * @param fi An instance of a channel statemachine.
1171 * @param event The event, just happened.
1172 * @param arg Generic pointer, casted from channel * upon call.
1173 */
1174static void
1175ch_action_fail(fsm_instance * fi, int event, void *arg)
1176{
1177 struct channel *ch = (struct channel *) arg;
1178 struct net_device *dev = ch->netdev;
1179
1180 DBF_TEXT(trace, 3, __FUNCTION__);
1181 fsm_deltimer(&ch->timer);
1182 fsm_newstate(fi, CH_STATE_NOTOP);
1183 if (CHANNEL_DIRECTION(ch->flags) == READ) {
1184 skb_queue_purge(&ch->io_queue);
1185 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1186 DEV_EVENT_RXDOWN, dev);
1187 } else {
1188 ctc_purge_skb_queue(&ch->io_queue);
1189 spin_lock(&ch->collect_lock);
1190 ctc_purge_skb_queue(&ch->collect_queue);
1191 ch->collect_len = 0;
1192 spin_unlock(&ch->collect_lock);
1193 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1194 DEV_EVENT_TXDOWN, dev);
1195 }
1196}
1197
1198/**
1199 * Handle error during setup of channel.
1200 *
1201 * @param fi An instance of a channel statemachine.
1202 * @param event The event, just happened.
1203 * @param arg Generic pointer, casted from channel * upon call.
1204 */
1205static void
1206ch_action_setuperr(fsm_instance * fi, int event, void *arg)
1207{
1208 struct channel *ch = (struct channel *) arg;
1209 struct net_device *dev = ch->netdev;
1210
1211 DBF_TEXT(setup, 3, __FUNCTION__);
1212 /**
1213 * Special case: Got UC_RCRESET on setmode.
1214 * This means that remote side isn't setup. In this case
1215 * simply retry after some 10 secs...
1216 */
1217 if ((fsm_getstate(fi) == CH_STATE_SETUPWAIT) &&
1218 ((event == CH_EVENT_UC_RCRESET) ||
1219 (event == CH_EVENT_UC_RSRESET))) {
1220 fsm_newstate(fi, CH_STATE_STARTRETRY);
1221 fsm_deltimer(&ch->timer);
1222 fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC, CH_EVENT_TIMER, ch);
1223 if (CHANNEL_DIRECTION(ch->flags) == READ) {
1224 int rc = ccw_device_halt(ch->cdev, (unsigned long) ch);
1225 if (rc != 0)
1226 ccw_check_return_code(
1227 ch, rc, "HaltIO in ch_action_setuperr");
1228 }
1229 return;
1230 }
1231
1232 ctc_pr_debug("%s: Error %s during %s channel setup state=%s\n",
1233 dev->name, ch_event_names[event],
1234 (CHANNEL_DIRECTION(ch->flags) == READ) ? "RX" : "TX",
1235 fsm_getstate_str(fi));
1236 if (CHANNEL_DIRECTION(ch->flags) == READ) {
1237 fsm_newstate(fi, CH_STATE_RXERR);
1238 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1239 DEV_EVENT_RXDOWN, dev);
1240 } else {
1241 fsm_newstate(fi, CH_STATE_TXERR);
1242 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1243 DEV_EVENT_TXDOWN, dev);
1244 }
1245}
1246
1247/**
1248 * Restart a channel after an error.
1249 *
1250 * @param fi An instance of a channel statemachine.
1251 * @param event The event, just happened.
1252 * @param arg Generic pointer, casted from channel * upon call.
1253 */
1254static void
1255ch_action_restart(fsm_instance * fi, int event, void *arg)
1256{
1257 unsigned long saveflags;
1258 int oldstate;
1259 int rc;
1260
1261 struct channel *ch = (struct channel *) arg;
1262 struct net_device *dev = ch->netdev;
1263
1264 DBF_TEXT(trace, 3, __FUNCTION__);
1265 fsm_deltimer(&ch->timer);
1266 ctc_pr_debug("%s: %s channel restart\n", dev->name,
1267 (CHANNEL_DIRECTION(ch->flags) == READ) ? "RX" : "TX");
1268 fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC, CH_EVENT_TIMER, ch);
1269 oldstate = fsm_getstate(fi);
1270 fsm_newstate(fi, CH_STATE_STARTWAIT);
1271 saveflags = 0; /* avoids compiler warning with
1272 spin_unlock_irqrestore */
1273 if (event == CH_EVENT_TIMER) // only for timer not yet locked
1274 spin_lock_irqsave(get_ccwdev_lock(ch->cdev), saveflags);
1275 rc = ccw_device_halt(ch->cdev, (unsigned long) ch);
1276 if (event == CH_EVENT_TIMER)
1277 spin_unlock_irqrestore(get_ccwdev_lock(ch->cdev), saveflags);
1278 if (rc != 0) {
1279 if (rc != -EBUSY) {
1280 fsm_deltimer(&ch->timer);
1281 fsm_newstate(fi, oldstate);
1282 }
1283 ccw_check_return_code(ch, rc, "HaltIO in ch_action_restart");
1284 }
1285}
1286
1287/**
1288 * Handle error during RX initial handshake (exchange of
1289 * 0-length block header)
1290 *
1291 * @param fi An instance of a channel statemachine.
1292 * @param event The event, just happened.
1293 * @param arg Generic pointer, casted from channel * upon call.
1294 */
1295static void
1296ch_action_rxiniterr(fsm_instance * fi, int event, void *arg)
1297{
1298 struct channel *ch = (struct channel *) arg;
1299 struct net_device *dev = ch->netdev;
1300
1301 DBF_TEXT(setup, 3, __FUNCTION__);
1302 if (event == CH_EVENT_TIMER) {
1303 fsm_deltimer(&ch->timer);
1304 ctc_pr_debug("%s: Timeout during RX init handshake\n", dev->name);
1305 if (ch->retry++ < 3)
1306 ch_action_restart(fi, event, arg);
1307 else {
1308 fsm_newstate(fi, CH_STATE_RXERR);
1309 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1310 DEV_EVENT_RXDOWN, dev);
1311 }
1312 } else
1313 ctc_pr_warn("%s: Error during RX init handshake\n", dev->name);
1314}
1315
1316/**
1317 * Notify device statemachine if we gave up initialization
1318 * of RX channel.
1319 *
1320 * @param fi An instance of a channel statemachine.
1321 * @param event The event, just happened.
1322 * @param arg Generic pointer, casted from channel * upon call.
1323 */
1324static void
1325ch_action_rxinitfail(fsm_instance * fi, int event, void *arg)
1326{
1327 struct channel *ch = (struct channel *) arg;
1328 struct net_device *dev = ch->netdev;
1329
1330 DBF_TEXT(setup, 3, __FUNCTION__);
1331 fsm_newstate(fi, CH_STATE_RXERR);
1332 ctc_pr_warn("%s: RX initialization failed\n", dev->name);
1333 ctc_pr_warn("%s: RX <-> RX connection detected\n", dev->name);
1334 fsm_event(((struct ctc_priv *) dev->priv)->fsm, DEV_EVENT_RXDOWN, dev);
1335}
1336
1337/**
1338 * Handle RX Unit check remote reset (remote disconnected)
1339 *
1340 * @param fi An instance of a channel statemachine.
1341 * @param event The event, just happened.
1342 * @param arg Generic pointer, casted from channel * upon call.
1343 */
1344static void
1345ch_action_rxdisc(fsm_instance * fi, int event, void *arg)
1346{
1347 struct channel *ch = (struct channel *) arg;
1348 struct channel *ch2;
1349 struct net_device *dev = ch->netdev;
1350
1351 DBF_TEXT(trace, 3, __FUNCTION__);
1352 fsm_deltimer(&ch->timer);
1353 ctc_pr_debug("%s: Got remote disconnect, re-initializing ...\n",
1354 dev->name);
1355
1356 /**
1357 * Notify device statemachine
1358 */
1359 fsm_event(((struct ctc_priv *) dev->priv)->fsm, DEV_EVENT_RXDOWN, dev);
1360 fsm_event(((struct ctc_priv *) dev->priv)->fsm, DEV_EVENT_TXDOWN, dev);
1361
1362 fsm_newstate(fi, CH_STATE_DTERM);
1363 ch2 = ((struct ctc_priv *) dev->priv)->channel[WRITE];
1364 fsm_newstate(ch2->fsm, CH_STATE_DTERM);
1365
1366 ccw_device_halt(ch->cdev, (unsigned long) ch);
1367 ccw_device_halt(ch2->cdev, (unsigned long) ch2);
1368}
1369
1370/**
1371 * Handle error during TX channel initialization.
1372 *
1373 * @param fi An instance of a channel statemachine.
1374 * @param event The event, just happened.
1375 * @param arg Generic pointer, casted from channel * upon call.
1376 */
1377static void
1378ch_action_txiniterr(fsm_instance * fi, int event, void *arg)
1379{
1380 struct channel *ch = (struct channel *) arg;
1381 struct net_device *dev = ch->netdev;
1382
1383 DBF_TEXT(setup, 2, __FUNCTION__);
1384 if (event == CH_EVENT_TIMER) {
1385 fsm_deltimer(&ch->timer);
1386 ctc_pr_debug("%s: Timeout during TX init handshake\n", dev->name);
1387 if (ch->retry++ < 3)
1388 ch_action_restart(fi, event, arg);
1389 else {
1390 fsm_newstate(fi, CH_STATE_TXERR);
1391 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1392 DEV_EVENT_TXDOWN, dev);
1393 }
1394 } else
1395 ctc_pr_warn("%s: Error during TX init handshake\n", dev->name);
1396}
1397
1398/**
1399 * Handle TX timeout by retrying operation.
1400 *
1401 * @param fi An instance of a channel statemachine.
1402 * @param event The event, just happened.
1403 * @param arg Generic pointer, casted from channel * upon call.
1404 */
1405static void
1406ch_action_txretry(fsm_instance * fi, int event, void *arg)
1407{
1408 struct channel *ch = (struct channel *) arg;
1409 struct net_device *dev = ch->netdev;
1410 unsigned long saveflags;
1411
1412 DBF_TEXT(trace, 4, __FUNCTION__);
1413 fsm_deltimer(&ch->timer);
1414 if (ch->retry++ > 3) {
1415 ctc_pr_debug("%s: TX retry failed, restarting channel\n",
1416 dev->name);
1417 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1418 DEV_EVENT_TXDOWN, dev);
1419 ch_action_restart(fi, event, arg);
1420 } else {
1421 struct sk_buff *skb;
1422
1423 ctc_pr_debug("%s: TX retry %d\n", dev->name, ch->retry);
1424 if ((skb = skb_peek(&ch->io_queue))) {
1425 int rc = 0;
1426
1427 clear_normalized_cda(&ch->ccw[4]);
1428 ch->ccw[4].count = skb->len;
1429 if (set_normalized_cda(&ch->ccw[4], skb->data)) {
1430 ctc_pr_debug(
1431 "%s: IDAL alloc failed, chan restart\n",
1432 dev->name);
1433 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1434 DEV_EVENT_TXDOWN, dev);
1435 ch_action_restart(fi, event, arg);
1436 return;
1437 }
1438 fsm_addtimer(&ch->timer, 1000, CH_EVENT_TIMER, ch);
1439 saveflags = 0; /* avoids compiler warning with
1440 spin_unlock_irqrestore */
1441 if (event == CH_EVENT_TIMER) // only for TIMER not yet locked
1442 spin_lock_irqsave(get_ccwdev_lock(ch->cdev),
1443 saveflags);
1444 rc = ccw_device_start(ch->cdev, &ch->ccw[3],
1445 (unsigned long) ch, 0xff, 0);
1446 if (event == CH_EVENT_TIMER)
1447 spin_unlock_irqrestore(get_ccwdev_lock(ch->cdev),
1448 saveflags);
1449 if (rc != 0) {
1450 fsm_deltimer(&ch->timer);
1451 ccw_check_return_code(ch, rc, "TX in ch_action_txretry");
1452 ctc_purge_skb_queue(&ch->io_queue);
1453 }
1454 }
1455 }
1456
1457}
1458
1459/**
1460 * Handle fatal errors during an I/O command.
1461 *
1462 * @param fi An instance of a channel statemachine.
1463 * @param event The event, just happened.
1464 * @param arg Generic pointer, casted from channel * upon call.
1465 */
1466static void
1467ch_action_iofatal(fsm_instance * fi, int event, void *arg)
1468{
1469 struct channel *ch = (struct channel *) arg;
1470 struct net_device *dev = ch->netdev;
1471
1472 DBF_TEXT(trace, 3, __FUNCTION__);
1473 fsm_deltimer(&ch->timer);
1474 if (CHANNEL_DIRECTION(ch->flags) == READ) {
1475 ctc_pr_debug("%s: RX I/O error\n", dev->name);
1476 fsm_newstate(fi, CH_STATE_RXERR);
1477 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1478 DEV_EVENT_RXDOWN, dev);
1479 } else {
1480 ctc_pr_debug("%s: TX I/O error\n", dev->name);
1481 fsm_newstate(fi, CH_STATE_TXERR);
1482 fsm_event(((struct ctc_priv *) dev->priv)->fsm,
1483 DEV_EVENT_TXDOWN, dev);
1484 }
1485}
1486
Jeff Garzike82b0f22006-05-26 21:58:38 -04001487static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488ch_action_reinit(fsm_instance *fi, int event, void *arg)
1489{
1490 struct channel *ch = (struct channel *)arg;
1491 struct net_device *dev = ch->netdev;
1492 struct ctc_priv *privptr = dev->priv;
Jeff Garzike82b0f22006-05-26 21:58:38 -04001493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 DBF_TEXT(trace, 4, __FUNCTION__);
1495 ch_action_iofatal(fi, event, arg);
1496 fsm_addtimer(&privptr->restart_timer, 1000, DEV_EVENT_RESTART, dev);
1497}
1498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499/**
1500 * The statemachine for a channel.
1501 */
1502static const fsm_node ch_fsm[] = {
1503 {CH_STATE_STOPPED, CH_EVENT_STOP, fsm_action_nop },
1504 {CH_STATE_STOPPED, CH_EVENT_START, ch_action_start },
1505 {CH_STATE_STOPPED, CH_EVENT_FINSTAT, fsm_action_nop },
1506 {CH_STATE_STOPPED, CH_EVENT_MC_FAIL, fsm_action_nop },
1507
1508 {CH_STATE_NOTOP, CH_EVENT_STOP, ch_action_stop },
1509 {CH_STATE_NOTOP, CH_EVENT_START, fsm_action_nop },
1510 {CH_STATE_NOTOP, CH_EVENT_FINSTAT, fsm_action_nop },
1511 {CH_STATE_NOTOP, CH_EVENT_MC_FAIL, fsm_action_nop },
1512 {CH_STATE_NOTOP, CH_EVENT_MC_GOOD, ch_action_start },
1513
1514 {CH_STATE_STARTWAIT, CH_EVENT_STOP, ch_action_haltio },
1515 {CH_STATE_STARTWAIT, CH_EVENT_START, fsm_action_nop },
1516 {CH_STATE_STARTWAIT, CH_EVENT_FINSTAT, ch_action_setmode },
1517 {CH_STATE_STARTWAIT, CH_EVENT_TIMER, ch_action_setuperr },
1518 {CH_STATE_STARTWAIT, CH_EVENT_IO_ENODEV, ch_action_iofatal },
1519 {CH_STATE_STARTWAIT, CH_EVENT_IO_EIO, ch_action_reinit },
1520 {CH_STATE_STARTWAIT, CH_EVENT_MC_FAIL, ch_action_fail },
1521
1522 {CH_STATE_STARTRETRY, CH_EVENT_STOP, ch_action_haltio },
1523 {CH_STATE_STARTRETRY, CH_EVENT_TIMER, ch_action_setmode },
1524 {CH_STATE_STARTRETRY, CH_EVENT_FINSTAT, fsm_action_nop },
1525 {CH_STATE_STARTRETRY, CH_EVENT_MC_FAIL, ch_action_fail },
1526
1527 {CH_STATE_SETUPWAIT, CH_EVENT_STOP, ch_action_haltio },
1528 {CH_STATE_SETUPWAIT, CH_EVENT_START, fsm_action_nop },
1529 {CH_STATE_SETUPWAIT, CH_EVENT_FINSTAT, ch_action_firstio },
1530 {CH_STATE_SETUPWAIT, CH_EVENT_UC_RCRESET, ch_action_setuperr },
1531 {CH_STATE_SETUPWAIT, CH_EVENT_UC_RSRESET, ch_action_setuperr },
1532 {CH_STATE_SETUPWAIT, CH_EVENT_TIMER, ch_action_setmode },
1533 {CH_STATE_SETUPWAIT, CH_EVENT_IO_ENODEV, ch_action_iofatal },
1534 {CH_STATE_SETUPWAIT, CH_EVENT_IO_EIO, ch_action_reinit },
1535 {CH_STATE_SETUPWAIT, CH_EVENT_MC_FAIL, ch_action_fail },
1536
1537 {CH_STATE_RXINIT, CH_EVENT_STOP, ch_action_haltio },
1538 {CH_STATE_RXINIT, CH_EVENT_START, fsm_action_nop },
1539 {CH_STATE_RXINIT, CH_EVENT_FINSTAT, ch_action_rxidle },
1540 {CH_STATE_RXINIT, CH_EVENT_UC_RCRESET, ch_action_rxiniterr },
1541 {CH_STATE_RXINIT, CH_EVENT_UC_RSRESET, ch_action_rxiniterr },
1542 {CH_STATE_RXINIT, CH_EVENT_TIMER, ch_action_rxiniterr },
1543 {CH_STATE_RXINIT, CH_EVENT_ATTNBUSY, ch_action_rxinitfail },
1544 {CH_STATE_RXINIT, CH_EVENT_IO_ENODEV, ch_action_iofatal },
1545 {CH_STATE_RXINIT, CH_EVENT_IO_EIO, ch_action_reinit },
1546 {CH_STATE_RXINIT, CH_EVENT_UC_ZERO, ch_action_firstio },
1547 {CH_STATE_RXINIT, CH_EVENT_MC_FAIL, ch_action_fail },
1548
1549 {CH_STATE_RXIDLE, CH_EVENT_STOP, ch_action_haltio },
1550 {CH_STATE_RXIDLE, CH_EVENT_START, fsm_action_nop },
1551 {CH_STATE_RXIDLE, CH_EVENT_FINSTAT, ch_action_rx },
1552 {CH_STATE_RXIDLE, CH_EVENT_UC_RCRESET, ch_action_rxdisc },
1553// {CH_STATE_RXIDLE, CH_EVENT_UC_RSRESET, ch_action_rxretry },
1554 {CH_STATE_RXIDLE, CH_EVENT_IO_ENODEV, ch_action_iofatal },
1555 {CH_STATE_RXIDLE, CH_EVENT_IO_EIO, ch_action_reinit },
1556 {CH_STATE_RXIDLE, CH_EVENT_MC_FAIL, ch_action_fail },
1557 {CH_STATE_RXIDLE, CH_EVENT_UC_ZERO, ch_action_rx },
1558
1559 {CH_STATE_TXINIT, CH_EVENT_STOP, ch_action_haltio },
1560 {CH_STATE_TXINIT, CH_EVENT_START, fsm_action_nop },
1561 {CH_STATE_TXINIT, CH_EVENT_FINSTAT, ch_action_txidle },
1562 {CH_STATE_TXINIT, CH_EVENT_UC_RCRESET, ch_action_txiniterr },
1563 {CH_STATE_TXINIT, CH_EVENT_UC_RSRESET, ch_action_txiniterr },
1564 {CH_STATE_TXINIT, CH_EVENT_TIMER, ch_action_txiniterr },
1565 {CH_STATE_TXINIT, CH_EVENT_IO_ENODEV, ch_action_iofatal },
1566 {CH_STATE_TXINIT, CH_EVENT_IO_EIO, ch_action_reinit },
1567 {CH_STATE_TXINIT, CH_EVENT_MC_FAIL, ch_action_fail },
1568
1569 {CH_STATE_TXIDLE, CH_EVENT_STOP, ch_action_haltio },
1570 {CH_STATE_TXIDLE, CH_EVENT_START, fsm_action_nop },
1571 {CH_STATE_TXIDLE, CH_EVENT_FINSTAT, ch_action_firstio },
1572 {CH_STATE_TXIDLE, CH_EVENT_UC_RCRESET, fsm_action_nop },
1573 {CH_STATE_TXIDLE, CH_EVENT_UC_RSRESET, fsm_action_nop },
1574 {CH_STATE_TXIDLE, CH_EVENT_IO_ENODEV, ch_action_iofatal },
1575 {CH_STATE_TXIDLE, CH_EVENT_IO_EIO, ch_action_reinit },
1576 {CH_STATE_TXIDLE, CH_EVENT_MC_FAIL, ch_action_fail },
1577
1578 {CH_STATE_TERM, CH_EVENT_STOP, fsm_action_nop },
1579 {CH_STATE_TERM, CH_EVENT_START, ch_action_restart },
1580 {CH_STATE_TERM, CH_EVENT_FINSTAT, ch_action_stopped },
1581 {CH_STATE_TERM, CH_EVENT_UC_RCRESET, fsm_action_nop },
1582 {CH_STATE_TERM, CH_EVENT_UC_RSRESET, fsm_action_nop },
1583 {CH_STATE_TERM, CH_EVENT_MC_FAIL, ch_action_fail },
1584
1585 {CH_STATE_DTERM, CH_EVENT_STOP, ch_action_haltio },
1586 {CH_STATE_DTERM, CH_EVENT_START, ch_action_restart },
1587 {CH_STATE_DTERM, CH_EVENT_FINSTAT, ch_action_setmode },
1588 {CH_STATE_DTERM, CH_EVENT_UC_RCRESET, fsm_action_nop },
1589 {CH_STATE_DTERM, CH_EVENT_UC_RSRESET, fsm_action_nop },
1590 {CH_STATE_DTERM, CH_EVENT_MC_FAIL, ch_action_fail },
1591
1592 {CH_STATE_TX, CH_EVENT_STOP, ch_action_haltio },
1593 {CH_STATE_TX, CH_EVENT_START, fsm_action_nop },
1594 {CH_STATE_TX, CH_EVENT_FINSTAT, ch_action_txdone },
1595 {CH_STATE_TX, CH_EVENT_UC_RCRESET, ch_action_txretry },
1596 {CH_STATE_TX, CH_EVENT_UC_RSRESET, ch_action_txretry },
1597 {CH_STATE_TX, CH_EVENT_TIMER, ch_action_txretry },
1598 {CH_STATE_TX, CH_EVENT_IO_ENODEV, ch_action_iofatal },
1599 {CH_STATE_TX, CH_EVENT_IO_EIO, ch_action_reinit },
1600 {CH_STATE_TX, CH_EVENT_MC_FAIL, ch_action_fail },
1601
1602 {CH_STATE_RXERR, CH_EVENT_STOP, ch_action_haltio },
1603 {CH_STATE_TXERR, CH_EVENT_STOP, ch_action_haltio },
1604 {CH_STATE_TXERR, CH_EVENT_MC_FAIL, ch_action_fail },
1605 {CH_STATE_RXERR, CH_EVENT_MC_FAIL, ch_action_fail },
1606};
1607
1608static const int CH_FSM_LEN = sizeof (ch_fsm) / sizeof (fsm_node);
Frank Pavlice172577d2005-09-08 09:50:06 +02001609
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610/**
1611 * Functions related to setup and device detection.
1612 *****************************************************************************/
1613
1614static inline int
1615less_than(char *id1, char *id2)
1616{
1617 int dev1, dev2, i;
1618
1619 for (i = 0; i < 5; i++) {
1620 id1++;
1621 id2++;
1622 }
1623 dev1 = simple_strtoul(id1, &id1, 16);
1624 dev2 = simple_strtoul(id2, &id2, 16);
Jeff Garzike82b0f22006-05-26 21:58:38 -04001625
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 return (dev1 < dev2);
1627}
1628
1629/**
1630 * Add a new channel to the list of channels.
1631 * Keeps the channel list sorted.
1632 *
1633 * @param cdev The ccw_device to be added.
1634 * @param type The type class of the new channel.
1635 *
1636 * @return 0 on success, !0 on error.
1637 */
1638static int
1639add_channel(struct ccw_device *cdev, enum channel_types type)
1640{
1641 struct channel **c = &channels;
1642 struct channel *ch;
1643
1644 DBF_TEXT(trace, 2, __FUNCTION__);
1645 if ((ch =
1646 (struct channel *) kmalloc(sizeof (struct channel),
1647 GFP_KERNEL)) == NULL) {
1648 ctc_pr_warn("ctc: Out of memory in add_channel\n");
1649 return -1;
1650 }
1651 memset(ch, 0, sizeof (struct channel));
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001652 if ((ch->ccw = kmalloc(8*sizeof(struct ccw1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 GFP_KERNEL | GFP_DMA)) == NULL) {
1654 kfree(ch);
1655 ctc_pr_warn("ctc: Out of memory in add_channel\n");
1656 return -1;
1657 }
1658
1659 memset(ch->ccw, 0, 8*sizeof(struct ccw1)); // assure all flags and counters are reset
1660
1661 /**
1662 * "static" ccws are used in the following way:
1663 *
1664 * ccw[0..2] (Channel program for generic I/O):
1665 * 0: prepare
1666 * 1: read or write (depending on direction) with fixed
1667 * buffer (idal allocated once when buffer is allocated)
1668 * 2: nop
1669 * ccw[3..5] (Channel program for direct write of packets)
1670 * 3: prepare
1671 * 4: write (idal allocated on every write).
1672 * 5: nop
1673 * ccw[6..7] (Channel program for initial channel setup):
1674 * 6: set extended mode
1675 * 7: nop
1676 *
1677 * ch->ccw[0..5] are initialized in ch_action_start because
1678 * the channel's direction is yet unknown here.
1679 */
1680 ch->ccw[6].cmd_code = CCW_CMD_SET_EXTENDED;
1681 ch->ccw[6].flags = CCW_FLAG_SLI;
1682
1683 ch->ccw[7].cmd_code = CCW_CMD_NOOP;
1684 ch->ccw[7].flags = CCW_FLAG_SLI;
1685
1686 ch->cdev = cdev;
1687 snprintf(ch->id, CTC_ID_SIZE, "ch-%s", cdev->dev.bus_id);
1688 ch->type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 ch->fsm = init_fsm(ch->id, ch_state_names,
1690 ch_event_names, NR_CH_STATES, NR_CH_EVENTS,
1691 ch_fsm, CH_FSM_LEN, GFP_KERNEL);
1692 if (ch->fsm == NULL) {
1693 ctc_pr_warn("ctc: Could not create FSM in add_channel\n");
1694 kfree(ch->ccw);
1695 kfree(ch);
1696 return -1;
1697 }
1698 fsm_newstate(ch->fsm, CH_STATE_IDLE);
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001699 if ((ch->irb = kmalloc(sizeof (struct irb),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 GFP_KERNEL)) == NULL) {
1701 ctc_pr_warn("ctc: Out of memory in add_channel\n");
1702 kfree_fsm(ch->fsm);
1703 kfree(ch->ccw);
1704 kfree(ch);
1705 return -1;
1706 }
1707 memset(ch->irb, 0, sizeof (struct irb));
1708 while (*c && less_than((*c)->id, ch->id))
1709 c = &(*c)->next;
1710 if (*c && (!strncmp((*c)->id, ch->id, CTC_ID_SIZE))) {
1711 ctc_pr_debug(
1712 "ctc: add_channel: device %s already in list, "
1713 "using old entry\n", (*c)->id);
1714 kfree(ch->irb);
1715 kfree_fsm(ch->fsm);
1716 kfree(ch->ccw);
1717 kfree(ch);
1718 return 0;
1719 }
Frank Pavlic4c7ae6e2006-09-15 16:25:03 +02001720
1721 spin_lock_init(&ch->collect_lock);
1722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 fsm_settimer(ch->fsm, &ch->timer);
1724 skb_queue_head_init(&ch->io_queue);
1725 skb_queue_head_init(&ch->collect_queue);
1726 ch->next = *c;
1727 *c = ch;
1728 return 0;
1729}
1730
1731/**
1732 * Release a specific channel in the channel list.
1733 *
1734 * @param ch Pointer to channel struct to be released.
1735 */
1736static void
1737channel_free(struct channel *ch)
1738{
1739 ch->flags &= ~CHANNEL_FLAGS_INUSE;
1740 fsm_newstate(ch->fsm, CH_STATE_IDLE);
1741}
1742
1743/**
1744 * Remove a specific channel in the channel list.
1745 *
1746 * @param ch Pointer to channel struct to be released.
1747 */
1748static void
1749channel_remove(struct channel *ch)
1750{
1751 struct channel **c = &channels;
1752
1753 DBF_TEXT(trace, 2, __FUNCTION__);
1754 if (ch == NULL)
1755 return;
1756
1757 channel_free(ch);
1758 while (*c) {
1759 if (*c == ch) {
1760 *c = ch->next;
1761 fsm_deltimer(&ch->timer);
1762 kfree_fsm(ch->fsm);
1763 clear_normalized_cda(&ch->ccw[4]);
1764 if (ch->trans_skb != NULL) {
1765 clear_normalized_cda(&ch->ccw[1]);
1766 dev_kfree_skb(ch->trans_skb);
1767 }
1768 kfree(ch->ccw);
1769 kfree(ch->irb);
1770 kfree(ch);
1771 return;
1772 }
1773 c = &((*c)->next);
1774 }
1775}
1776
1777/**
1778 * Get a specific channel from the channel list.
1779 *
1780 * @param type Type of channel we are interested in.
1781 * @param id Id of channel we are interested in.
1782 * @param direction Direction we want to use this channel for.
1783 *
1784 * @return Pointer to a channel or NULL if no matching channel available.
1785 */
1786static struct channel
1787*
1788channel_get(enum channel_types type, char *id, int direction)
1789{
1790 struct channel *ch = channels;
1791
1792 DBF_TEXT(trace, 3, __FUNCTION__);
1793#ifdef DEBUG
1794 ctc_pr_debug("ctc: %s(): searching for ch with id %s and type %d\n",
1795 __func__, id, type);
1796#endif
1797
1798 while (ch && ((strncmp(ch->id, id, CTC_ID_SIZE)) || (ch->type != type))) {
1799#ifdef DEBUG
1800 ctc_pr_debug("ctc: %s(): ch=0x%p (id=%s, type=%d\n",
1801 __func__, ch, ch->id, ch->type);
1802#endif
1803 ch = ch->next;
1804 }
1805#ifdef DEBUG
1806 ctc_pr_debug("ctc: %s(): ch=0x%pq (id=%s, type=%d\n",
1807 __func__, ch, ch->id, ch->type);
1808#endif
1809 if (!ch) {
1810 ctc_pr_warn("ctc: %s(): channel with id %s "
1811 "and type %d not found in channel list\n",
1812 __func__, id, type);
1813 } else {
1814 if (ch->flags & CHANNEL_FLAGS_INUSE)
1815 ch = NULL;
1816 else {
1817 ch->flags |= CHANNEL_FLAGS_INUSE;
1818 ch->flags &= ~CHANNEL_FLAGS_RWMASK;
1819 ch->flags |= (direction == WRITE)
1820 ? CHANNEL_FLAGS_WRITE : CHANNEL_FLAGS_READ;
1821 fsm_newstate(ch->fsm, CH_STATE_STOPPED);
1822 }
1823 }
1824 return ch;
1825}
1826
1827/**
1828 * Return the channel type by name.
1829 *
1830 * @param name Name of network interface.
1831 *
1832 * @return Type class of channel to be used for that interface.
1833 */
1834static enum channel_types inline
1835extract_channel_media(char *name)
1836{
1837 enum channel_types ret = channel_type_unknown;
1838
1839 if (name != NULL) {
1840 if (strncmp(name, "ctc", 3) == 0)
1841 ret = channel_type_parallel;
1842 if (strncmp(name, "escon", 5) == 0)
1843 ret = channel_type_escon;
1844 }
1845 return ret;
1846}
1847
1848static long
1849__ctc_check_irb_error(struct ccw_device *cdev, struct irb *irb)
1850{
1851 if (!IS_ERR(irb))
1852 return 0;
1853
1854 switch (PTR_ERR(irb)) {
1855 case -EIO:
1856 ctc_pr_warn("i/o-error on device %s\n", cdev->dev.bus_id);
1857// CTC_DBF_TEXT(trace, 2, "ckirberr");
1858// CTC_DBF_TEXT_(trace, 2, " rc%d", -EIO);
1859 break;
1860 case -ETIMEDOUT:
1861 ctc_pr_warn("timeout on device %s\n", cdev->dev.bus_id);
1862// CTC_DBF_TEXT(trace, 2, "ckirberr");
1863// CTC_DBF_TEXT_(trace, 2, " rc%d", -ETIMEDOUT);
1864 break;
1865 default:
1866 ctc_pr_warn("unknown error %ld on device %s\n", PTR_ERR(irb),
1867 cdev->dev.bus_id);
1868// CTC_DBF_TEXT(trace, 2, "ckirberr");
1869// CTC_DBF_TEXT(trace, 2, " rc???");
1870 }
1871 return PTR_ERR(irb);
1872}
1873
1874/**
1875 * Main IRQ handler.
1876 *
1877 * @param cdev The ccw_device the interrupt is for.
1878 * @param intparm interruption parameter.
1879 * @param irb interruption response block.
1880 */
1881static void
1882ctc_irq_handler(struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
1883{
1884 struct channel *ch;
1885 struct net_device *dev;
1886 struct ctc_priv *priv;
1887
1888 DBF_TEXT(trace, 5, __FUNCTION__);
1889 if (__ctc_check_irb_error(cdev, irb))
1890 return;
1891
1892 /* Check for unsolicited interrupts. */
1893 if (!cdev->dev.driver_data) {
1894 ctc_pr_warn("ctc: Got unsolicited irq: %s c-%02x d-%02x\n",
1895 cdev->dev.bus_id, irb->scsw.cstat,
1896 irb->scsw.dstat);
1897 return;
1898 }
Jeff Garzike82b0f22006-05-26 21:58:38 -04001899
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 priv = ((struct ccwgroup_device *)cdev->dev.driver_data)
1901 ->dev.driver_data;
1902
1903 /* Try to extract channel from driver data. */
1904 if (priv->channel[READ]->cdev == cdev)
1905 ch = priv->channel[READ];
1906 else if (priv->channel[WRITE]->cdev == cdev)
1907 ch = priv->channel[WRITE];
1908 else {
1909 ctc_pr_err("ctc: Can't determine channel for interrupt, "
1910 "device %s\n", cdev->dev.bus_id);
1911 return;
1912 }
Jeff Garzike82b0f22006-05-26 21:58:38 -04001913
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 dev = (struct net_device *) (ch->netdev);
1915 if (dev == NULL) {
1916 ctc_pr_crit("ctc: ctc_irq_handler dev=NULL bus_id=%s, ch=0x%p\n",
1917 cdev->dev.bus_id, ch);
1918 return;
1919 }
1920
1921#ifdef DEBUG
1922 ctc_pr_debug("%s: interrupt for device: %s received c-%02x d-%02x\n",
1923 dev->name, ch->id, irb->scsw.cstat, irb->scsw.dstat);
1924#endif
1925
1926 /* Copy interruption response block. */
1927 memcpy(ch->irb, irb, sizeof(struct irb));
1928
1929 /* Check for good subchannel return code, otherwise error message */
1930 if (ch->irb->scsw.cstat) {
1931 fsm_event(ch->fsm, CH_EVENT_SC_UNKNOWN, ch);
1932 ctc_pr_warn("%s: subchannel check for device: %s - %02x %02x\n",
1933 dev->name, ch->id, ch->irb->scsw.cstat,
1934 ch->irb->scsw.dstat);
1935 return;
1936 }
1937
1938 /* Check the reason-code of a unit check */
1939 if (ch->irb->scsw.dstat & DEV_STAT_UNIT_CHECK) {
1940 ccw_unit_check(ch, ch->irb->ecw[0]);
1941 return;
1942 }
1943 if (ch->irb->scsw.dstat & DEV_STAT_BUSY) {
1944 if (ch->irb->scsw.dstat & DEV_STAT_ATTENTION)
1945 fsm_event(ch->fsm, CH_EVENT_ATTNBUSY, ch);
1946 else
1947 fsm_event(ch->fsm, CH_EVENT_BUSY, ch);
1948 return;
1949 }
1950 if (ch->irb->scsw.dstat & DEV_STAT_ATTENTION) {
1951 fsm_event(ch->fsm, CH_EVENT_ATTN, ch);
1952 return;
1953 }
1954 if ((ch->irb->scsw.stctl & SCSW_STCTL_SEC_STATUS) ||
1955 (ch->irb->scsw.stctl == SCSW_STCTL_STATUS_PEND) ||
1956 (ch->irb->scsw.stctl ==
1957 (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND)))
1958 fsm_event(ch->fsm, CH_EVENT_FINSTAT, ch);
1959 else
1960 fsm_event(ch->fsm, CH_EVENT_IRQ, ch);
1961
1962}
Frank Pavlice172577d2005-09-08 09:50:06 +02001963
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964/**
1965 * Actions for interface - statemachine.
1966 *****************************************************************************/
1967
1968/**
1969 * Startup channels by sending CH_EVENT_START to each channel.
1970 *
1971 * @param fi An instance of an interface statemachine.
1972 * @param event The event, just happened.
1973 * @param arg Generic pointer, casted from struct net_device * upon call.
1974 */
1975static void
1976dev_action_start(fsm_instance * fi, int event, void *arg)
1977{
1978 struct net_device *dev = (struct net_device *) arg;
1979 struct ctc_priv *privptr = dev->priv;
1980 int direction;
1981
1982 DBF_TEXT(setup, 3, __FUNCTION__);
1983 fsm_deltimer(&privptr->restart_timer);
1984 fsm_newstate(fi, DEV_STATE_STARTWAIT_RXTX);
1985 for (direction = READ; direction <= WRITE; direction++) {
1986 struct channel *ch = privptr->channel[direction];
1987 fsm_event(ch->fsm, CH_EVENT_START, ch);
1988 }
1989}
1990
1991/**
1992 * Shutdown channels by sending CH_EVENT_STOP to each channel.
1993 *
1994 * @param fi An instance of an interface statemachine.
1995 * @param event The event, just happened.
1996 * @param arg Generic pointer, casted from struct net_device * upon call.
1997 */
1998static void
1999dev_action_stop(fsm_instance * fi, int event, void *arg)
2000{
2001 struct net_device *dev = (struct net_device *) arg;
2002 struct ctc_priv *privptr = dev->priv;
2003 int direction;
2004
2005 DBF_TEXT(trace, 3, __FUNCTION__);
2006 fsm_newstate(fi, DEV_STATE_STOPWAIT_RXTX);
2007 for (direction = READ; direction <= WRITE; direction++) {
2008 struct channel *ch = privptr->channel[direction];
2009 fsm_event(ch->fsm, CH_EVENT_STOP, ch);
2010 }
2011}
Jeff Garzike82b0f22006-05-26 21:58:38 -04002012static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013dev_action_restart(fsm_instance *fi, int event, void *arg)
2014{
2015 struct net_device *dev = (struct net_device *)arg;
2016 struct ctc_priv *privptr = dev->priv;
Jeff Garzike82b0f22006-05-26 21:58:38 -04002017
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 DBF_TEXT(trace, 3, __FUNCTION__);
2019 ctc_pr_debug("%s: Restarting\n", dev->name);
2020 dev_action_stop(fi, event, arg);
2021 fsm_event(privptr->fsm, DEV_EVENT_STOP, dev);
2022 fsm_addtimer(&privptr->restart_timer, CTC_TIMEOUT_5SEC,
2023 DEV_EVENT_START, dev);
2024}
2025
2026/**
2027 * Called from channel statemachine
2028 * when a channel is up and running.
2029 *
2030 * @param fi An instance of an interface statemachine.
2031 * @param event The event, just happened.
2032 * @param arg Generic pointer, casted from struct net_device * upon call.
2033 */
2034static void
2035dev_action_chup(fsm_instance * fi, int event, void *arg)
2036{
2037 struct net_device *dev = (struct net_device *) arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
2039 DBF_TEXT(trace, 3, __FUNCTION__);
2040 switch (fsm_getstate(fi)) {
2041 case DEV_STATE_STARTWAIT_RXTX:
2042 if (event == DEV_EVENT_RXUP)
2043 fsm_newstate(fi, DEV_STATE_STARTWAIT_TX);
2044 else
2045 fsm_newstate(fi, DEV_STATE_STARTWAIT_RX);
2046 break;
2047 case DEV_STATE_STARTWAIT_RX:
2048 if (event == DEV_EVENT_RXUP) {
2049 fsm_newstate(fi, DEV_STATE_RUNNING);
2050 ctc_pr_info("%s: connected with remote side\n",
2051 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 ctc_clear_busy(dev);
2053 }
2054 break;
2055 case DEV_STATE_STARTWAIT_TX:
2056 if (event == DEV_EVENT_TXUP) {
2057 fsm_newstate(fi, DEV_STATE_RUNNING);
2058 ctc_pr_info("%s: connected with remote side\n",
2059 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 ctc_clear_busy(dev);
2061 }
2062 break;
2063 case DEV_STATE_STOPWAIT_TX:
2064 if (event == DEV_EVENT_RXUP)
2065 fsm_newstate(fi, DEV_STATE_STOPWAIT_RXTX);
2066 break;
2067 case DEV_STATE_STOPWAIT_RX:
2068 if (event == DEV_EVENT_TXUP)
2069 fsm_newstate(fi, DEV_STATE_STOPWAIT_RXTX);
2070 break;
2071 }
2072}
2073
2074/**
2075 * Called from channel statemachine
2076 * when a channel has been shutdown.
2077 *
2078 * @param fi An instance of an interface statemachine.
2079 * @param event The event, just happened.
2080 * @param arg Generic pointer, casted from struct net_device * upon call.
2081 */
2082static void
2083dev_action_chdown(fsm_instance * fi, int event, void *arg)
2084{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
2086 DBF_TEXT(trace, 3, __FUNCTION__);
2087 switch (fsm_getstate(fi)) {
2088 case DEV_STATE_RUNNING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 if (event == DEV_EVENT_TXDOWN)
2090 fsm_newstate(fi, DEV_STATE_STARTWAIT_TX);
2091 else
2092 fsm_newstate(fi, DEV_STATE_STARTWAIT_RX);
2093 break;
2094 case DEV_STATE_STARTWAIT_RX:
2095 if (event == DEV_EVENT_TXDOWN)
2096 fsm_newstate(fi, DEV_STATE_STARTWAIT_RXTX);
2097 break;
2098 case DEV_STATE_STARTWAIT_TX:
2099 if (event == DEV_EVENT_RXDOWN)
2100 fsm_newstate(fi, DEV_STATE_STARTWAIT_RXTX);
2101 break;
2102 case DEV_STATE_STOPWAIT_RXTX:
2103 if (event == DEV_EVENT_TXDOWN)
2104 fsm_newstate(fi, DEV_STATE_STOPWAIT_RX);
2105 else
2106 fsm_newstate(fi, DEV_STATE_STOPWAIT_TX);
2107 break;
2108 case DEV_STATE_STOPWAIT_RX:
2109 if (event == DEV_EVENT_RXDOWN)
2110 fsm_newstate(fi, DEV_STATE_STOPPED);
2111 break;
2112 case DEV_STATE_STOPWAIT_TX:
2113 if (event == DEV_EVENT_TXDOWN)
2114 fsm_newstate(fi, DEV_STATE_STOPPED);
2115 break;
2116 }
2117}
2118
2119static const fsm_node dev_fsm[] = {
2120 {DEV_STATE_STOPPED, DEV_EVENT_START, dev_action_start},
2121
2122 {DEV_STATE_STOPWAIT_RXTX, DEV_EVENT_START, dev_action_start },
2123 {DEV_STATE_STOPWAIT_RXTX, DEV_EVENT_RXDOWN, dev_action_chdown },
2124 {DEV_STATE_STOPWAIT_RXTX, DEV_EVENT_TXDOWN, dev_action_chdown },
2125 {DEV_STATE_STOPWAIT_RXTX, DEV_EVENT_RESTART, dev_action_restart },
2126
2127 {DEV_STATE_STOPWAIT_RX, DEV_EVENT_START, dev_action_start },
2128 {DEV_STATE_STOPWAIT_RX, DEV_EVENT_RXUP, dev_action_chup },
2129 {DEV_STATE_STOPWAIT_RX, DEV_EVENT_TXUP, dev_action_chup },
2130 {DEV_STATE_STOPWAIT_RX, DEV_EVENT_RXDOWN, dev_action_chdown },
2131 {DEV_STATE_STOPWAIT_RX, DEV_EVENT_RESTART, dev_action_restart },
2132
2133 {DEV_STATE_STOPWAIT_TX, DEV_EVENT_START, dev_action_start },
2134 {DEV_STATE_STOPWAIT_TX, DEV_EVENT_RXUP, dev_action_chup },
2135 {DEV_STATE_STOPWAIT_TX, DEV_EVENT_TXUP, dev_action_chup },
2136 {DEV_STATE_STOPWAIT_TX, DEV_EVENT_TXDOWN, dev_action_chdown },
2137 {DEV_STATE_STOPWAIT_TX, DEV_EVENT_RESTART, dev_action_restart },
2138
2139 {DEV_STATE_STARTWAIT_RXTX, DEV_EVENT_STOP, dev_action_stop },
2140 {DEV_STATE_STARTWAIT_RXTX, DEV_EVENT_RXUP, dev_action_chup },
2141 {DEV_STATE_STARTWAIT_RXTX, DEV_EVENT_TXUP, dev_action_chup },
2142 {DEV_STATE_STARTWAIT_RXTX, DEV_EVENT_RXDOWN, dev_action_chdown },
2143 {DEV_STATE_STARTWAIT_RXTX, DEV_EVENT_TXDOWN, dev_action_chdown },
2144 {DEV_STATE_STARTWAIT_RXTX, DEV_EVENT_RESTART, dev_action_restart },
2145
2146 {DEV_STATE_STARTWAIT_TX, DEV_EVENT_STOP, dev_action_stop },
2147 {DEV_STATE_STARTWAIT_TX, DEV_EVENT_RXUP, dev_action_chup },
2148 {DEV_STATE_STARTWAIT_TX, DEV_EVENT_TXUP, dev_action_chup },
2149 {DEV_STATE_STARTWAIT_TX, DEV_EVENT_RXDOWN, dev_action_chdown },
2150 {DEV_STATE_STARTWAIT_TX, DEV_EVENT_RESTART, dev_action_restart },
2151
2152 {DEV_STATE_STARTWAIT_RX, DEV_EVENT_STOP, dev_action_stop },
2153 {DEV_STATE_STARTWAIT_RX, DEV_EVENT_RXUP, dev_action_chup },
2154 {DEV_STATE_STARTWAIT_RX, DEV_EVENT_TXUP, dev_action_chup },
2155 {DEV_STATE_STARTWAIT_RX, DEV_EVENT_TXDOWN, dev_action_chdown },
2156 {DEV_STATE_STARTWAIT_RX, DEV_EVENT_RESTART, dev_action_restart },
2157
2158 {DEV_STATE_RUNNING, DEV_EVENT_STOP, dev_action_stop },
2159 {DEV_STATE_RUNNING, DEV_EVENT_RXDOWN, dev_action_chdown },
2160 {DEV_STATE_RUNNING, DEV_EVENT_TXDOWN, dev_action_chdown },
2161 {DEV_STATE_RUNNING, DEV_EVENT_TXUP, fsm_action_nop },
2162 {DEV_STATE_RUNNING, DEV_EVENT_RXUP, fsm_action_nop },
2163 {DEV_STATE_RUNNING, DEV_EVENT_RESTART, dev_action_restart },
2164};
2165
2166static const int DEV_FSM_LEN = sizeof (dev_fsm) / sizeof (fsm_node);
2167
2168/**
2169 * Transmit a packet.
2170 * This is a helper function for ctc_tx().
2171 *
2172 * @param ch Channel to be used for sending.
2173 * @param skb Pointer to struct sk_buff of packet to send.
2174 * The linklevel header has already been set up
2175 * by ctc_tx().
2176 *
2177 * @return 0 on success, -ERRNO on failure. (Never fails.)
2178 */
2179static int
2180transmit_skb(struct channel *ch, struct sk_buff *skb)
2181{
2182 unsigned long saveflags;
2183 struct ll_header header;
2184 int rc = 0;
2185
2186 DBF_TEXT(trace, 5, __FUNCTION__);
Frank Pavlice172577d2005-09-08 09:50:06 +02002187 /* we need to acquire the lock for testing the state
Jeff Garzike82b0f22006-05-26 21:58:38 -04002188 * otherwise we can have an IRQ changing the state to
Frank Pavlice172577d2005-09-08 09:50:06 +02002189 * TXIDLE after the test but before acquiring the lock.
2190 */
2191 spin_lock_irqsave(&ch->collect_lock, saveflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 if (fsm_getstate(ch->fsm) != CH_STATE_TXIDLE) {
2193 int l = skb->len + LL_HEADER_LENGTH;
2194
Frank Pavlice172577d2005-09-08 09:50:06 +02002195 if (ch->collect_len + l > ch->max_bufsize - 2) {
2196 spin_unlock_irqrestore(&ch->collect_lock, saveflags);
2197 return -EBUSY;
2198 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 atomic_inc(&skb->users);
2200 header.length = l;
2201 header.type = skb->protocol;
2202 header.unused = 0;
2203 memcpy(skb_push(skb, LL_HEADER_LENGTH), &header,
2204 LL_HEADER_LENGTH);
2205 skb_queue_tail(&ch->collect_queue, skb);
2206 ch->collect_len += l;
2207 }
2208 spin_unlock_irqrestore(&ch->collect_lock, saveflags);
2209 } else {
2210 __u16 block_len;
2211 int ccw_idx;
2212 struct sk_buff *nskb;
2213 unsigned long hi;
Frank Pavlice172577d2005-09-08 09:50:06 +02002214 spin_unlock_irqrestore(&ch->collect_lock, saveflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 /**
2216 * Protect skb against beeing free'd by upper
2217 * layers.
2218 */
2219 atomic_inc(&skb->users);
2220 ch->prof.txlen += skb->len;
2221 header.length = skb->len + LL_HEADER_LENGTH;
2222 header.type = skb->protocol;
2223 header.unused = 0;
2224 memcpy(skb_push(skb, LL_HEADER_LENGTH), &header,
2225 LL_HEADER_LENGTH);
2226 block_len = skb->len + 2;
2227 *((__u16 *) skb_push(skb, 2)) = block_len;
2228
2229 /**
2230 * IDAL support in CTC is broken, so we have to
2231 * care about skb's above 2G ourselves.
2232 */
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002233 hi = ((unsigned long)skb_tail_pointer(skb) +
2234 LL_HEADER_LENGTH) >> 31;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 if (hi) {
2236 nskb = alloc_skb(skb->len, GFP_ATOMIC | GFP_DMA);
2237 if (!nskb) {
2238 atomic_dec(&skb->users);
2239 skb_pull(skb, LL_HEADER_LENGTH + 2);
Frank Pavlice172577d2005-09-08 09:50:06 +02002240 ctc_clear_busy(ch->netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 return -ENOMEM;
2242 } else {
2243 memcpy(skb_put(nskb, skb->len),
2244 skb->data, skb->len);
2245 atomic_inc(&nskb->users);
2246 atomic_dec(&skb->users);
2247 dev_kfree_skb_irq(skb);
2248 skb = nskb;
2249 }
2250 }
2251
2252 ch->ccw[4].count = block_len;
2253 if (set_normalized_cda(&ch->ccw[4], skb->data)) {
2254 /**
2255 * idal allocation failed, try via copying to
2256 * trans_skb. trans_skb usually has a pre-allocated
2257 * idal.
2258 */
2259 if (ctc_checkalloc_buffer(ch, 1)) {
2260 /**
2261 * Remove our header. It gets added
2262 * again on retransmit.
2263 */
2264 atomic_dec(&skb->users);
2265 skb_pull(skb, LL_HEADER_LENGTH + 2);
Frank Pavlice172577d2005-09-08 09:50:06 +02002266 ctc_clear_busy(ch->netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 return -EBUSY;
2268 }
2269
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002270 skb_reset_tail_pointer(ch->trans_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 ch->trans_skb->len = 0;
2272 ch->ccw[1].count = skb->len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002273 skb_copy_from_linear_data(skb, skb_put(ch->trans_skb,
2274 skb->len),
2275 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 atomic_dec(&skb->users);
2277 dev_kfree_skb_irq(skb);
2278 ccw_idx = 0;
2279 } else {
2280 skb_queue_tail(&ch->io_queue, skb);
2281 ccw_idx = 3;
2282 }
2283 ch->retry = 0;
2284 fsm_newstate(ch->fsm, CH_STATE_TX);
2285 fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC, CH_EVENT_TIMER, ch);
2286 spin_lock_irqsave(get_ccwdev_lock(ch->cdev), saveflags);
2287 ch->prof.send_stamp = xtime;
2288 rc = ccw_device_start(ch->cdev, &ch->ccw[ccw_idx],
2289 (unsigned long) ch, 0xff, 0);
2290 spin_unlock_irqrestore(get_ccwdev_lock(ch->cdev), saveflags);
2291 if (ccw_idx == 3)
2292 ch->prof.doios_single++;
2293 if (rc != 0) {
2294 fsm_deltimer(&ch->timer);
2295 ccw_check_return_code(ch, rc, "single skb TX");
2296 if (ccw_idx == 3)
2297 skb_dequeue_tail(&ch->io_queue);
2298 /**
2299 * Remove our header. It gets added
2300 * again on retransmit.
2301 */
2302 skb_pull(skb, LL_HEADER_LENGTH + 2);
2303 } else {
2304 if (ccw_idx == 0) {
2305 struct net_device *dev = ch->netdev;
2306 struct ctc_priv *privptr = dev->priv;
2307 privptr->stats.tx_packets++;
2308 privptr->stats.tx_bytes +=
2309 skb->len - LL_HEADER_LENGTH;
2310 }
2311 }
2312 }
2313
Frank Pavlice172577d2005-09-08 09:50:06 +02002314 ctc_clear_busy(ch->netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 return rc;
2316}
Frank Pavlice172577d2005-09-08 09:50:06 +02002317
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318/**
2319 * Interface API for upper network layers
2320 *****************************************************************************/
2321
2322/**
2323 * Open an interface.
2324 * Called from generic network layer when ifconfig up is run.
2325 *
2326 * @param dev Pointer to interface struct.
2327 *
2328 * @return 0 on success, -ERRNO on failure. (Never fails.)
2329 */
2330static int
2331ctc_open(struct net_device * dev)
2332{
2333 DBF_TEXT(trace, 5, __FUNCTION__);
2334 fsm_event(((struct ctc_priv *) dev->priv)->fsm, DEV_EVENT_START, dev);
2335 return 0;
2336}
2337
2338/**
2339 * Close an interface.
2340 * Called from generic network layer when ifconfig down is run.
2341 *
2342 * @param dev Pointer to interface struct.
2343 *
2344 * @return 0 on success, -ERRNO on failure. (Never fails.)
2345 */
2346static int
2347ctc_close(struct net_device * dev)
2348{
2349 DBF_TEXT(trace, 5, __FUNCTION__);
2350 fsm_event(((struct ctc_priv *) dev->priv)->fsm, DEV_EVENT_STOP, dev);
2351 return 0;
2352}
2353
2354/**
2355 * Start transmission of a packet.
2356 * Called from generic network device layer.
2357 *
2358 * @param skb Pointer to buffer containing the packet.
2359 * @param dev Pointer to interface struct.
2360 *
2361 * @return 0 if packet consumed, !0 if packet rejected.
2362 * Note: If we return !0, then the packet is free'd by
2363 * the generic network layer.
2364 */
2365static int
2366ctc_tx(struct sk_buff *skb, struct net_device * dev)
2367{
2368 int rc = 0;
2369 struct ctc_priv *privptr = (struct ctc_priv *) dev->priv;
2370
2371 DBF_TEXT(trace, 5, __FUNCTION__);
2372 /**
2373 * Some sanity checks ...
2374 */
2375 if (skb == NULL) {
2376 ctc_pr_warn("%s: NULL sk_buff passed\n", dev->name);
2377 privptr->stats.tx_dropped++;
2378 return 0;
2379 }
2380 if (skb_headroom(skb) < (LL_HEADER_LENGTH + 2)) {
2381 ctc_pr_warn("%s: Got sk_buff with head room < %ld bytes\n",
2382 dev->name, LL_HEADER_LENGTH + 2);
2383 dev_kfree_skb(skb);
2384 privptr->stats.tx_dropped++;
2385 return 0;
2386 }
2387
2388 /**
2389 * If channels are not running, try to restart them
Jeff Garzike82b0f22006-05-26 21:58:38 -04002390 * and throw away packet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 */
2392 if (fsm_getstate(privptr->fsm) != DEV_STATE_RUNNING) {
2393 fsm_event(privptr->fsm, DEV_EVENT_START, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 dev_kfree_skb(skb);
2395 privptr->stats.tx_dropped++;
2396 privptr->stats.tx_errors++;
2397 privptr->stats.tx_carrier_errors++;
2398 return 0;
2399 }
2400
2401 if (ctc_test_and_set_busy(dev))
2402 return -EBUSY;
2403
2404 dev->trans_start = jiffies;
2405 if (transmit_skb(privptr->channel[WRITE], skb) != 0)
2406 rc = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 return rc;
2408}
2409
2410/**
2411 * Sets MTU of an interface.
2412 *
2413 * @param dev Pointer to interface struct.
2414 * @param new_mtu The new MTU to use for this interface.
2415 *
2416 * @return 0 on success, -EINVAL if MTU is out of valid range.
2417 * (valid range is 576 .. 65527). If VM is on the
2418 * remote side, maximum MTU is 32760, however this is
2419 * <em>not</em> checked here.
2420 */
2421static int
2422ctc_change_mtu(struct net_device * dev, int new_mtu)
2423{
2424 struct ctc_priv *privptr = (struct ctc_priv *) dev->priv;
2425
2426 DBF_TEXT(trace, 3, __FUNCTION__);
2427 if ((new_mtu < 576) || (new_mtu > 65527) ||
2428 (new_mtu > (privptr->channel[READ]->max_bufsize -
2429 LL_HEADER_LENGTH - 2)))
2430 return -EINVAL;
2431 dev->mtu = new_mtu;
2432 dev->hard_header_len = LL_HEADER_LENGTH + 2;
2433 return 0;
2434}
2435
2436/**
2437 * Returns interface statistics of a device.
2438 *
2439 * @param dev Pointer to interface struct.
2440 *
2441 * @return Pointer to stats struct of this interface.
2442 */
2443static struct net_device_stats *
2444ctc_stats(struct net_device * dev)
2445{
2446 return &((struct ctc_priv *) dev->priv)->stats;
2447}
2448
2449/*
2450 * sysfs attributes
2451 */
Frank Pavlic7394c922005-05-12 20:36:47 +02002452
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -04002454buffer_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455{
2456 struct ctc_priv *priv;
2457
2458 priv = dev->driver_data;
2459 if (!priv)
2460 return -ENODEV;
2461 return sprintf(buf, "%d\n",
2462 priv->buffer_size);
2463}
2464
2465static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -04002466buffer_write(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467{
2468 struct ctc_priv *priv;
2469 struct net_device *ndev;
2470 int bs1;
Frank Pavlic7394c922005-05-12 20:36:47 +02002471 char buffer[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
2473 DBF_TEXT(trace, 3, __FUNCTION__);
Frank Pavlic7394c922005-05-12 20:36:47 +02002474 DBF_TEXT(trace, 3, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 priv = dev->driver_data;
Frank Pavlic7394c922005-05-12 20:36:47 +02002476 if (!priv) {
2477 DBF_TEXT(trace, 3, "bfnopriv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 return -ENODEV;
Frank Pavlic7394c922005-05-12 20:36:47 +02002479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
Frank Pavlic7394c922005-05-12 20:36:47 +02002481 sscanf(buf, "%u", &bs1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 if (bs1 > CTC_BUFSIZE_LIMIT)
Frank Pavlic7394c922005-05-12 20:36:47 +02002483 goto einval;
2484 if (bs1 < (576 + LL_HEADER_LENGTH + 2))
2485 goto einval;
2486 priv->buffer_size = bs1; // just to overwrite the default
2487
2488 ndev = priv->channel[READ]->netdev;
2489 if (!ndev) {
2490 DBF_TEXT(trace, 3, "bfnondev");
2491 return -ENODEV;
2492 }
2493
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 if ((ndev->flags & IFF_RUNNING) &&
2495 (bs1 < (ndev->mtu + LL_HEADER_LENGTH + 2)))
Frank Pavlic7394c922005-05-12 20:36:47 +02002496 goto einval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
Frank Pavlic7394c922005-05-12 20:36:47 +02002498 priv->channel[READ]->max_bufsize = bs1;
2499 priv->channel[WRITE]->max_bufsize = bs1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 if (!(ndev->flags & IFF_RUNNING))
2501 ndev->mtu = bs1 - LL_HEADER_LENGTH - 2;
2502 priv->channel[READ]->flags |= CHANNEL_FLAGS_BUFSIZE_CHANGED;
2503 priv->channel[WRITE]->flags |= CHANNEL_FLAGS_BUFSIZE_CHANGED;
2504
Frank Pavlic7394c922005-05-12 20:36:47 +02002505 sprintf(buffer, "%d",priv->buffer_size);
2506 DBF_TEXT(trace, 3, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 return count;
2508
Frank Pavlic7394c922005-05-12 20:36:47 +02002509einval:
2510 DBF_TEXT(trace, 3, "buff_err");
2511 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512}
2513
2514static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -04002515loglevel_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 return sprintf(buf, "%d\n", loglevel);
2518}
2519
2520static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -04002521loglevel_write(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 int ll1;
2524
2525 DBF_TEXT(trace, 5, __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 sscanf(buf, "%i", &ll1);
2527
2528 if ((ll1 > CTC_LOGLEVEL_MAX) || (ll1 < 0))
2529 return -EINVAL;
2530 loglevel = ll1;
2531 return count;
2532}
2533
2534static void
2535ctc_print_statistics(struct ctc_priv *priv)
2536{
2537 char *sbuf;
2538 char *p;
2539
2540 DBF_TEXT(trace, 4, __FUNCTION__);
2541 if (!priv)
2542 return;
Robert P. J. Day5cbded52006-12-13 00:35:56 -08002543 sbuf = kmalloc(2048, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 if (sbuf == NULL)
2545 return;
2546 p = sbuf;
2547
2548 p += sprintf(p, " Device FSM state: %s\n",
2549 fsm_getstate_str(priv->fsm));
2550 p += sprintf(p, " RX channel FSM state: %s\n",
2551 fsm_getstate_str(priv->channel[READ]->fsm));
2552 p += sprintf(p, " TX channel FSM state: %s\n",
2553 fsm_getstate_str(priv->channel[WRITE]->fsm));
2554 p += sprintf(p, " Max. TX buffer used: %ld\n",
2555 priv->channel[WRITE]->prof.maxmulti);
2556 p += sprintf(p, " Max. chained SKBs: %ld\n",
2557 priv->channel[WRITE]->prof.maxcqueue);
2558 p += sprintf(p, " TX single write ops: %ld\n",
2559 priv->channel[WRITE]->prof.doios_single);
2560 p += sprintf(p, " TX multi write ops: %ld\n",
2561 priv->channel[WRITE]->prof.doios_multi);
2562 p += sprintf(p, " Netto bytes written: %ld\n",
2563 priv->channel[WRITE]->prof.txlen);
2564 p += sprintf(p, " Max. TX IO-time: %ld\n",
2565 priv->channel[WRITE]->prof.tx_time);
2566
2567 ctc_pr_debug("Statistics for %s:\n%s",
2568 priv->channel[WRITE]->netdev->name, sbuf);
2569 kfree(sbuf);
2570 return;
2571}
2572
2573static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -04002574stats_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575{
2576 struct ctc_priv *priv = dev->driver_data;
2577 if (!priv)
2578 return -ENODEV;
2579 ctc_print_statistics(priv);
2580 return sprintf(buf, "0\n");
2581}
2582
2583static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -04002584stats_write(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585{
2586 struct ctc_priv *priv = dev->driver_data;
2587 if (!priv)
2588 return -ENODEV;
2589 /* Reset statistics */
2590 memset(&priv->channel[WRITE]->prof, 0,
2591 sizeof(priv->channel[WRITE]->prof));
2592 return count;
2593}
2594
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595static void
2596ctc_netdev_unregister(struct net_device * dev)
2597{
2598 struct ctc_priv *privptr;
2599
2600 if (!dev)
2601 return;
2602 privptr = (struct ctc_priv *) dev->priv;
Frank Pavlic56347a22006-04-13 20:19:12 +02002603 unregister_netdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604}
2605
2606static int
2607ctc_netdev_register(struct net_device * dev)
2608{
Frank Pavlic56347a22006-04-13 20:19:12 +02002609 return register_netdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610}
2611
2612static void
2613ctc_free_netdevice(struct net_device * dev, int free_dev)
2614{
2615 struct ctc_priv *privptr;
2616 if (!dev)
2617 return;
2618 privptr = dev->priv;
2619 if (privptr) {
2620 if (privptr->fsm)
2621 kfree_fsm(privptr->fsm);
2622 kfree(privptr);
2623 }
2624#ifdef MODULE
2625 if (free_dev)
2626 free_netdev(dev);
2627#endif
2628}
2629
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -04002631ctc_proto_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632{
2633 struct ctc_priv *priv;
2634
2635 priv = dev->driver_data;
2636 if (!priv)
2637 return -ENODEV;
2638
2639 return sprintf(buf, "%d\n", priv->protocol);
2640}
2641
2642static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -04002643ctc_proto_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644{
2645 struct ctc_priv *priv;
2646 int value;
2647
2648 DBF_TEXT(trace, 3, __FUNCTION__);
2649 pr_debug("%s() called\n", __FUNCTION__);
2650
2651 priv = dev->driver_data;
2652 if (!priv)
2653 return -ENODEV;
2654 sscanf(buf, "%u", &value);
Frank Pavlic56347a22006-04-13 20:19:12 +02002655 if (!((value == CTC_PROTO_S390) ||
2656 (value == CTC_PROTO_LINUX) ||
2657 (value == CTC_PROTO_OS390)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 return -EINVAL;
2659 priv->protocol = value;
2660
2661 return count;
2662}
2663
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -04002665ctc_type_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666{
2667 struct ccwgroup_device *cgdev;
2668
2669 cgdev = to_ccwgroupdev(dev);
2670 if (!cgdev)
2671 return -ENODEV;
2672
2673 return sprintf(buf, "%s\n", cu3088_type[cgdev->cdev[0]->id.driver_info]);
2674}
2675
Frank Pavlic7394c922005-05-12 20:36:47 +02002676static DEVICE_ATTR(buffer, 0644, buffer_show, buffer_write);
2677static DEVICE_ATTR(protocol, 0644, ctc_proto_show, ctc_proto_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678static DEVICE_ATTR(type, 0444, ctc_type_show, NULL);
2679
Frank Pavlic7394c922005-05-12 20:36:47 +02002680static DEVICE_ATTR(loglevel, 0644, loglevel_show, loglevel_write);
2681static DEVICE_ATTR(stats, 0644, stats_show, stats_write);
2682
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683static struct attribute *ctc_attr[] = {
2684 &dev_attr_protocol.attr,
2685 &dev_attr_type.attr,
2686 &dev_attr_buffer.attr,
2687 NULL,
2688};
2689
2690static struct attribute_group ctc_attr_group = {
2691 .attrs = ctc_attr,
2692};
2693
2694static int
Frank Pavlic7394c922005-05-12 20:36:47 +02002695ctc_add_attributes(struct device *dev)
2696{
Heiko Carstensd7cf0d52006-07-18 13:46:58 +02002697 int rc;
2698
2699 rc = device_create_file(dev, &dev_attr_loglevel);
2700 if (rc)
2701 goto out;
2702 rc = device_create_file(dev, &dev_attr_stats);
2703 if (!rc)
2704 goto out;
2705 device_remove_file(dev, &dev_attr_loglevel);
2706out:
2707 return rc;
Frank Pavlic7394c922005-05-12 20:36:47 +02002708}
2709
2710static void
2711ctc_remove_attributes(struct device *dev)
2712{
2713 device_remove_file(dev, &dev_attr_stats);
2714 device_remove_file(dev, &dev_attr_loglevel);
2715}
2716
2717static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718ctc_add_files(struct device *dev)
2719{
2720 pr_debug("%s() called\n", __FUNCTION__);
2721
2722 return sysfs_create_group(&dev->kobj, &ctc_attr_group);
2723}
2724
2725static void
2726ctc_remove_files(struct device *dev)
2727{
2728 pr_debug("%s() called\n", __FUNCTION__);
2729
2730 sysfs_remove_group(&dev->kobj, &ctc_attr_group);
2731}
2732
2733/**
2734 * Add ctc specific attributes.
2735 * Add ctc private data.
Jeff Garzike82b0f22006-05-26 21:58:38 -04002736 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 * @param cgdev pointer to ccwgroup_device just added
2738 *
2739 * @returns 0 on success, !0 on failure.
2740 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741static int
2742ctc_probe_device(struct ccwgroup_device *cgdev)
2743{
2744 struct ctc_priv *priv;
2745 int rc;
Frank Pavlic7394c922005-05-12 20:36:47 +02002746 char buffer[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
2748 pr_debug("%s() called\n", __FUNCTION__);
Frank Pavlic7394c922005-05-12 20:36:47 +02002749 DBF_TEXT(setup, 3, __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750
2751 if (!get_device(&cgdev->dev))
2752 return -ENODEV;
2753
2754 priv = kmalloc(sizeof (struct ctc_priv), GFP_KERNEL);
2755 if (!priv) {
2756 ctc_pr_err("%s: Out of memory\n", __func__);
2757 put_device(&cgdev->dev);
2758 return -ENOMEM;
2759 }
2760
2761 memset(priv, 0, sizeof (struct ctc_priv));
2762 rc = ctc_add_files(&cgdev->dev);
2763 if (rc) {
2764 kfree(priv);
2765 put_device(&cgdev->dev);
2766 return rc;
2767 }
2768 priv->buffer_size = CTC_BUFSIZE_DEFAULT;
2769 cgdev->cdev[0]->handler = ctc_irq_handler;
2770 cgdev->cdev[1]->handler = ctc_irq_handler;
2771 cgdev->dev.driver_data = priv;
2772
Frank Pavlic7394c922005-05-12 20:36:47 +02002773 sprintf(buffer, "%p", priv);
2774 DBF_TEXT(data, 3, buffer);
2775
2776 sprintf(buffer, "%u", (unsigned int)sizeof(struct ctc_priv));
2777 DBF_TEXT(data, 3, buffer);
2778
2779 sprintf(buffer, "%p", &channels);
2780 DBF_TEXT(data, 3, buffer);
2781
2782 sprintf(buffer, "%u", (unsigned int)sizeof(struct channel));
2783 DBF_TEXT(data, 3, buffer);
2784
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 return 0;
2786}
2787
2788/**
Frank Pavlic7394c922005-05-12 20:36:47 +02002789 * Initialize everything of the net device except the name and the
2790 * channel structs.
2791 */
2792static struct net_device *
2793ctc_init_netdevice(struct net_device * dev, int alloc_device,
2794 struct ctc_priv *privptr)
2795{
2796 if (!privptr)
2797 return NULL;
2798
2799 DBF_TEXT(setup, 3, __FUNCTION__);
2800
2801 if (alloc_device) {
2802 dev = kmalloc(sizeof (struct net_device), GFP_KERNEL);
2803 if (!dev)
2804 return NULL;
2805 memset(dev, 0, sizeof (struct net_device));
2806 }
2807
2808 dev->priv = privptr;
2809 privptr->fsm = init_fsm("ctcdev", dev_state_names,
2810 dev_event_names, CTC_NR_DEV_STATES, CTC_NR_DEV_EVENTS,
2811 dev_fsm, DEV_FSM_LEN, GFP_KERNEL);
2812 if (privptr->fsm == NULL) {
2813 if (alloc_device)
2814 kfree(dev);
2815 return NULL;
2816 }
2817 fsm_newstate(privptr->fsm, DEV_STATE_STOPPED);
2818 fsm_settimer(privptr->fsm, &privptr->restart_timer);
2819 if (dev->mtu == 0)
2820 dev->mtu = CTC_BUFSIZE_DEFAULT - LL_HEADER_LENGTH - 2;
2821 dev->hard_start_xmit = ctc_tx;
2822 dev->open = ctc_open;
2823 dev->stop = ctc_close;
2824 dev->get_stats = ctc_stats;
2825 dev->change_mtu = ctc_change_mtu;
2826 dev->hard_header_len = LL_HEADER_LENGTH + 2;
2827 dev->addr_len = 0;
2828 dev->type = ARPHRD_SLIP;
2829 dev->tx_queue_len = 100;
2830 dev->flags = IFF_POINTOPOINT | IFF_NOARP;
2831 SET_MODULE_OWNER(dev);
2832 return dev;
2833}
2834
2835
2836/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 *
2838 * Setup an interface.
2839 *
2840 * @param cgdev Device to be setup.
2841 *
2842 * @returns 0 on success, !0 on failure.
2843 */
2844static int
2845ctc_new_device(struct ccwgroup_device *cgdev)
2846{
2847 char read_id[CTC_ID_SIZE];
2848 char write_id[CTC_ID_SIZE];
2849 int direction;
2850 enum channel_types type;
2851 struct ctc_priv *privptr;
2852 struct net_device *dev;
2853 int ret;
Frank Pavlic7394c922005-05-12 20:36:47 +02002854 char buffer[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855
2856 pr_debug("%s() called\n", __FUNCTION__);
2857 DBF_TEXT(setup, 3, __FUNCTION__);
2858
2859 privptr = cgdev->dev.driver_data;
2860 if (!privptr)
2861 return -ENODEV;
2862
Frank Pavlic7394c922005-05-12 20:36:47 +02002863 sprintf(buffer, "%d", privptr->buffer_size);
2864 DBF_TEXT(setup, 3, buffer);
2865
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 type = get_channel_type(&cgdev->cdev[0]->id);
Jeff Garzike82b0f22006-05-26 21:58:38 -04002867
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 snprintf(read_id, CTC_ID_SIZE, "ch-%s", cgdev->cdev[0]->dev.bus_id);
2869 snprintf(write_id, CTC_ID_SIZE, "ch-%s", cgdev->cdev[1]->dev.bus_id);
2870
2871 if (add_channel(cgdev->cdev[0], type))
2872 return -ENOMEM;
2873 if (add_channel(cgdev->cdev[1], type))
2874 return -ENOMEM;
2875
2876 ret = ccw_device_set_online(cgdev->cdev[0]);
2877 if (ret != 0) {
2878 printk(KERN_WARNING
2879 "ccw_device_set_online (cdev[0]) failed with ret = %d\n", ret);
2880 }
2881
2882 ret = ccw_device_set_online(cgdev->cdev[1]);
2883 if (ret != 0) {
2884 printk(KERN_WARNING
2885 "ccw_device_set_online (cdev[1]) failed with ret = %d\n", ret);
2886 }
2887
2888 dev = ctc_init_netdevice(NULL, 1, privptr);
2889
2890 if (!dev) {
2891 ctc_pr_warn("ctc_init_netdevice failed\n");
2892 goto out;
2893 }
2894
Frank Pavlic56347a22006-04-13 20:19:12 +02002895 strlcpy(dev->name, "ctc%d", IFNAMSIZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896
2897 for (direction = READ; direction <= WRITE; direction++) {
2898 privptr->channel[direction] =
2899 channel_get(type, direction == READ ? read_id : write_id,
2900 direction);
2901 if (privptr->channel[direction] == NULL) {
Jeff Garzike82b0f22006-05-26 21:58:38 -04002902 if (direction == WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 channel_free(privptr->channel[READ]);
2904
2905 ctc_free_netdevice(dev, 1);
2906 goto out;
2907 }
2908 privptr->channel[direction]->netdev = dev;
2909 privptr->channel[direction]->protocol = privptr->protocol;
2910 privptr->channel[direction]->max_bufsize = privptr->buffer_size;
2911 }
2912 /* sysfs magic */
2913 SET_NETDEV_DEV(dev, &cgdev->dev);
2914
2915 if (ctc_netdev_register(dev) != 0) {
2916 ctc_free_netdevice(dev, 1);
2917 goto out;
2918 }
2919
Heiko Carstensd7cf0d52006-07-18 13:46:58 +02002920 if (ctc_add_attributes(&cgdev->dev)) {
2921 ctc_netdev_unregister(dev);
2922 dev->priv = NULL;
2923 ctc_free_netdevice(dev, 1);
2924 goto out;
2925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
2927 strlcpy(privptr->fsm->name, dev->name, sizeof (privptr->fsm->name));
2928
2929 print_banner();
2930
2931 ctc_pr_info("%s: read: %s, write: %s, proto: %d\n",
2932 dev->name, privptr->channel[READ]->id,
2933 privptr->channel[WRITE]->id, privptr->protocol);
2934
2935 return 0;
2936out:
2937 ccw_device_set_offline(cgdev->cdev[1]);
2938 ccw_device_set_offline(cgdev->cdev[0]);
2939
2940 return -ENODEV;
2941}
2942
2943/**
2944 * Shutdown an interface.
2945 *
2946 * @param cgdev Device to be shut down.
2947 *
2948 * @returns 0 on success, !0 on failure.
2949 */
2950static int
2951ctc_shutdown_device(struct ccwgroup_device *cgdev)
2952{
2953 struct ctc_priv *priv;
2954 struct net_device *ndev;
Jeff Garzike82b0f22006-05-26 21:58:38 -04002955
Frank Pavlic7394c922005-05-12 20:36:47 +02002956 DBF_TEXT(setup, 3, __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 pr_debug("%s() called\n", __FUNCTION__);
2958
Frank Pavlic7394c922005-05-12 20:36:47 +02002959
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 priv = cgdev->dev.driver_data;
2961 ndev = NULL;
2962 if (!priv)
2963 return -ENODEV;
2964
2965 if (priv->channel[READ]) {
2966 ndev = priv->channel[READ]->netdev;
2967
2968 /* Close the device */
2969 ctc_close(ndev);
2970 ndev->flags &=~IFF_RUNNING;
2971
2972 ctc_remove_attributes(&cgdev->dev);
2973
2974 channel_free(priv->channel[READ]);
2975 }
2976 if (priv->channel[WRITE])
2977 channel_free(priv->channel[WRITE]);
2978
2979 if (ndev) {
2980 ctc_netdev_unregister(ndev);
2981 ndev->priv = NULL;
2982 ctc_free_netdevice(ndev, 1);
2983 }
2984
2985 if (priv->fsm)
2986 kfree_fsm(priv->fsm);
2987
2988 ccw_device_set_offline(cgdev->cdev[1]);
2989 ccw_device_set_offline(cgdev->cdev[0]);
2990
2991 if (priv->channel[READ])
2992 channel_remove(priv->channel[READ]);
2993 if (priv->channel[WRITE])
2994 channel_remove(priv->channel[WRITE]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 priv->channel[READ] = priv->channel[WRITE] = NULL;
2996
2997 return 0;
2998
2999}
3000
3001static void
3002ctc_remove_device(struct ccwgroup_device *cgdev)
3003{
3004 struct ctc_priv *priv;
3005
3006 pr_debug("%s() called\n", __FUNCTION__);
Frank Pavlic7394c922005-05-12 20:36:47 +02003007 DBF_TEXT(setup, 3, __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008
3009 priv = cgdev->dev.driver_data;
3010 if (!priv)
3011 return;
3012 if (cgdev->state == CCWGROUP_ONLINE)
3013 ctc_shutdown_device(cgdev);
3014 ctc_remove_files(&cgdev->dev);
3015 cgdev->dev.driver_data = NULL;
3016 kfree(priv);
3017 put_device(&cgdev->dev);
3018}
3019
3020static struct ccwgroup_driver ctc_group_driver = {
3021 .owner = THIS_MODULE,
3022 .name = "ctc",
3023 .max_slaves = 2,
3024 .driver_id = 0xC3E3C3,
3025 .probe = ctc_probe_device,
3026 .remove = ctc_remove_device,
3027 .set_online = ctc_new_device,
3028 .set_offline = ctc_shutdown_device,
3029};
3030
3031/**
3032 * Module related routines
3033 *****************************************************************************/
3034
3035/**
3036 * Prepare to be unloaded. Free IRQ's and release all resources.
3037 * This is called just before this module is unloaded. It is
3038 * <em>not</em> called, if the usage count is !0, so we don't need to check
3039 * for that.
3040 */
3041static void __exit
3042ctc_exit(void)
3043{
Frank Pavlic7394c922005-05-12 20:36:47 +02003044 DBF_TEXT(setup, 3, __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 unregister_cu3088_discipline(&ctc_group_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 ctc_unregister_dbf_views();
3047 ctc_pr_info("CTC driver unloaded\n");
3048}
3049
3050/**
3051 * Initialize module.
3052 * This is called just after the module is loaded.
3053 *
3054 * @return 0 on success, !0 on error.
3055 */
3056static int __init
3057ctc_init(void)
3058{
3059 int ret = 0;
3060
Frank Pavlic7394c922005-05-12 20:36:47 +02003061 loglevel = CTC_LOGLEVEL_DEFAULT;
3062
3063 DBF_TEXT(setup, 3, __FUNCTION__);
3064
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 print_banner();
3066
3067 ret = ctc_register_dbf_views();
3068 if (ret){
3069 ctc_pr_crit("ctc_init failed with ctc_register_dbf_views rc = %d\n", ret);
3070 return ret;
3071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 ret = register_cu3088_discipline(&ctc_group_driver);
3073 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 ctc_unregister_dbf_views();
3075 }
3076 return ret;
3077}
3078
3079module_init(ctc_init);
3080module_exit(ctc_exit);
3081
3082/* --- This is the END my friend --- */