]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/s390/net/ctcm_main.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[net-next-2.6.git] / drivers / s390 / net / ctcm_main.c
CommitLineData
293d984f
PT
1/*
2 * drivers/s390/net/ctcm_main.c
3 *
b8a2d42a 4 * Copyright IBM Corp. 2001, 2009
293d984f
PT
5 * Author(s):
6 * Original CTC driver(s):
7 * Fritz Elfert (felfert@millenux.com)
8 * Dieter Wellerdiek (wel@de.ibm.com)
9 * Martin Schwidefsky (schwidefsky@de.ibm.com)
10 * Denis Joseph Barrow (barrow_dj@yahoo.com)
11 * Jochen Roehrig (roehrig@de.ibm.com)
12 * Cornelia Huck <cornelia.huck@de.ibm.com>
13 * MPC additions:
14 * Belinda Thompson (belindat@us.ibm.com)
15 * Andy Richter (richtera@us.ibm.com)
16 * Revived by:
17 * Peter Tiedemann (ptiedem@de.ibm.com)
18 */
19
20#undef DEBUG
21#undef DEBUGDATA
22#undef DEBUGCCW
23
2a7c6f2c
PT
24#define KMSG_COMPONENT "ctcm"
25#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
26
293d984f
PT
27#include <linux/module.h>
28#include <linux/init.h>
29#include <linux/kernel.h>
30#include <linux/slab.h>
31#include <linux/errno.h>
32#include <linux/types.h>
33#include <linux/interrupt.h>
34#include <linux/timer.h>
35#include <linux/bitops.h>
36
37#include <linux/signal.h>
38#include <linux/string.h>
39
40#include <linux/ip.h>
41#include <linux/if_arp.h>
42#include <linux/tcp.h>
43#include <linux/skbuff.h>
44#include <linux/ctype.h>
45#include <net/dst.h>
46
47#include <linux/io.h>
48#include <asm/ccwdev.h>
49#include <asm/ccwgroup.h>
50#include <linux/uaccess.h>
51
52#include <asm/idals.h>
53
293d984f
PT
54#include "ctcm_fsms.h"
55#include "ctcm_main.h"
56
57/* Some common global variables */
58
0ca8cc6f
UB
59/**
60 * The root device for ctcm group devices
61 */
62static struct device *ctcm_root_dev;
63
293d984f
PT
64/*
65 * Linked list of all detected channels.
66 */
67struct channel *channels;
68
69/**
70 * Unpack a just received skb and hand it over to
71 * upper layers.
72 *
73 * ch The channel where this skb has been received.
74 * pskb The received skb.
75 */
76void ctcm_unpack_skb(struct channel *ch, struct sk_buff *pskb)
77{
78 struct net_device *dev = ch->netdev;
261893d3 79 struct ctcm_priv *priv = dev->ml_priv;
293d984f
PT
80 __u16 len = *((__u16 *) pskb->data);
81
82 skb_put(pskb, 2 + LL_HEADER_LENGTH);
83 skb_pull(pskb, 2);
84 pskb->dev = dev;
85 pskb->ip_summed = CHECKSUM_UNNECESSARY;
86 while (len > 0) {
87 struct sk_buff *skb;
88 int skblen;
89 struct ll_header *header = (struct ll_header *)pskb->data;
90
91 skb_pull(pskb, LL_HEADER_LENGTH);
92 if ((ch->protocol == CTCM_PROTO_S390) &&
93 (header->type != ETH_P_IP)) {
293d984f 94 if (!(ch->logflags & LOG_FLAG_ILLEGALPKT)) {
aa3f2cb6 95 ch->logflags |= LOG_FLAG_ILLEGALPKT;
293d984f
PT
96 /*
97 * Check packet type only if we stick strictly
98 * to S/390's protocol of OS390. This only
99 * supports IP. Otherwise allow any packet
100 * type.
101 */
aa3f2cb6
PT
102 CTCM_DBF_TEXT_(ERROR, CTC_DBF_ERROR,
103 "%s(%s): Illegal packet type 0x%04x"
104 " - dropping",
105 CTCM_FUNTAIL, dev->name, header->type);
293d984f 106 }
293d984f
PT
107 priv->stats.rx_dropped++;
108 priv->stats.rx_frame_errors++;
109 return;
110 }
111 pskb->protocol = ntohs(header->type);
fb8585fc
RK
112 if ((header->length <= LL_HEADER_LENGTH) ||
113 (len <= LL_HEADER_LENGTH)) {
293d984f 114 if (!(ch->logflags & LOG_FLAG_ILLEGALSIZE)) {
aa3f2cb6
PT
115 CTCM_DBF_TEXT_(ERROR, CTC_DBF_ERROR,
116 "%s(%s): Illegal packet size %d(%d,%d)"
117 "- dropping",
118 CTCM_FUNTAIL, dev->name,
119 header->length, dev->mtu, len);
293d984f
PT
120 ch->logflags |= LOG_FLAG_ILLEGALSIZE;
121 }
122
123 priv->stats.rx_dropped++;
124 priv->stats.rx_length_errors++;
125 return;
126 }
127 header->length -= LL_HEADER_LENGTH;
128 len -= LL_HEADER_LENGTH;
129 if ((header->length > skb_tailroom(pskb)) ||
130 (header->length > len)) {
131 if (!(ch->logflags & LOG_FLAG_OVERRUN)) {
aa3f2cb6
PT
132 CTCM_DBF_TEXT_(ERROR, CTC_DBF_ERROR,
133 "%s(%s): Packet size %d (overrun)"
134 " - dropping", CTCM_FUNTAIL,
135 dev->name, header->length);
293d984f
PT
136 ch->logflags |= LOG_FLAG_OVERRUN;
137 }
138
139 priv->stats.rx_dropped++;
140 priv->stats.rx_length_errors++;
141 return;
142 }
143 skb_put(pskb, header->length);
144 skb_reset_mac_header(pskb);
145 len -= header->length;
146 skb = dev_alloc_skb(pskb->len);
147 if (!skb) {
148 if (!(ch->logflags & LOG_FLAG_NOMEM)) {
aa3f2cb6
PT
149 CTCM_DBF_TEXT_(ERROR, CTC_DBF_ERROR,
150 "%s(%s): MEMORY allocation error",
151 CTCM_FUNTAIL, dev->name);
293d984f
PT
152 ch->logflags |= LOG_FLAG_NOMEM;
153 }
154 priv->stats.rx_dropped++;
155 return;
156 }
157 skb_copy_from_linear_data(pskb, skb_put(skb, pskb->len),
158 pskb->len);
159 skb_reset_mac_header(skb);
160 skb->dev = pskb->dev;
161 skb->protocol = pskb->protocol;
162 pskb->ip_summed = CHECKSUM_UNNECESSARY;
163 skblen = skb->len;
164 /*
165 * reset logflags
166 */
167 ch->logflags = 0;
168 priv->stats.rx_packets++;
169 priv->stats.rx_bytes += skblen;
170 netif_rx_ni(skb);
293d984f
PT
171 if (len > 0) {
172 skb_pull(pskb, header->length);
173 if (skb_tailroom(pskb) < LL_HEADER_LENGTH) {
e2fc8cb4
JF
174 CTCM_DBF_DEV_NAME(TRACE, dev,
175 "Overrun in ctcm_unpack_skb");
176 ch->logflags |= LOG_FLAG_OVERRUN;
293d984f
PT
177 return;
178 }
179 skb_put(pskb, LL_HEADER_LENGTH);
180 }
181 }
182}
183
184/**
185 * Release a specific channel in the channel list.
186 *
187 * ch Pointer to channel struct to be released.
188 */
189static void channel_free(struct channel *ch)
190{
aa3f2cb6 191 CTCM_DBF_TEXT_(SETUP, CTC_DBF_INFO, "%s(%s)", CTCM_FUNTAIL, ch->id);
293d984f
PT
192 ch->flags &= ~CHANNEL_FLAGS_INUSE;
193 fsm_newstate(ch->fsm, CTC_STATE_IDLE);
194}
195
196/**
197 * Remove a specific channel in the channel list.
198 *
199 * ch Pointer to channel struct to be released.
200 */
201static void channel_remove(struct channel *ch)
202{
203 struct channel **c = &channels;
204 char chid[CTCM_ID_SIZE+1];
205 int ok = 0;
206
207 if (ch == NULL)
208 return;
209 else
210 strncpy(chid, ch->id, CTCM_ID_SIZE);
211
212 channel_free(ch);
213 while (*c) {
214 if (*c == ch) {
215 *c = ch->next;
216 fsm_deltimer(&ch->timer);
217 if (IS_MPC(ch))
218 fsm_deltimer(&ch->sweep_timer);
219
220 kfree_fsm(ch->fsm);
221 clear_normalized_cda(&ch->ccw[4]);
222 if (ch->trans_skb != NULL) {
223 clear_normalized_cda(&ch->ccw[1]);
224 dev_kfree_skb_any(ch->trans_skb);
225 }
226 if (IS_MPC(ch)) {
227 tasklet_kill(&ch->ch_tasklet);
228 tasklet_kill(&ch->ch_disc_tasklet);
229 kfree(ch->discontact_th);
230 }
231 kfree(ch->ccw);
232 kfree(ch->irb);
233 kfree(ch);
234 ok = 1;
235 break;
236 }
237 c = &((*c)->next);
238 }
239
240 CTCM_DBF_TEXT_(SETUP, CTC_DBF_INFO, "%s(%s) %s", CTCM_FUNTAIL,
241 chid, ok ? "OK" : "failed");
242}
243
244/**
245 * Get a specific channel from the channel list.
246 *
247 * type Type of channel we are interested in.
248 * id Id of channel we are interested in.
249 * direction Direction we want to use this channel for.
250 *
251 * returns Pointer to a channel or NULL if no matching channel available.
252 */
0ca8cc6f 253static struct channel *channel_get(enum ctcm_channel_types type,
293d984f
PT
254 char *id, int direction)
255{
256 struct channel *ch = channels;
257
293d984f
PT
258 while (ch && (strncmp(ch->id, id, CTCM_ID_SIZE) || (ch->type != type)))
259 ch = ch->next;
260 if (!ch) {
aa3f2cb6
PT
261 CTCM_DBF_TEXT_(ERROR, CTC_DBF_ERROR,
262 "%s(%d, %s, %d) not found in channel list\n",
293d984f 263 CTCM_FUNTAIL, type, id, direction);
293d984f
PT
264 } else {
265 if (ch->flags & CHANNEL_FLAGS_INUSE)
266 ch = NULL;
267 else {
268 ch->flags |= CHANNEL_FLAGS_INUSE;
269 ch->flags &= ~CHANNEL_FLAGS_RWMASK;
3c09e264 270 ch->flags |= (direction == CTCM_WRITE)
293d984f
PT
271 ? CHANNEL_FLAGS_WRITE : CHANNEL_FLAGS_READ;
272 fsm_newstate(ch->fsm, CTC_STATE_STOPPED);
273 }
274 }
275 return ch;
276}
277
278static long ctcm_check_irb_error(struct ccw_device *cdev, struct irb *irb)
279{
280 if (!IS_ERR(irb))
281 return 0;
282
aa3f2cb6
PT
283 CTCM_DBF_TEXT_(ERROR, CTC_DBF_WARN,
284 "irb error %ld on device %s\n",
2a0217d5 285 PTR_ERR(irb), dev_name(&cdev->dev));
293d984f
PT
286
287 switch (PTR_ERR(irb)) {
288 case -EIO:
2a7c6f2c
PT
289 dev_err(&cdev->dev,
290 "An I/O-error occurred on the CTCM device\n");
293d984f
PT
291 break;
292 case -ETIMEDOUT:
2a7c6f2c
PT
293 dev_err(&cdev->dev,
294 "An adapter hardware operation timed out\n");
293d984f
PT
295 break;
296 default:
2a7c6f2c
PT
297 dev_err(&cdev->dev,
298 "An error occurred on the adapter hardware\n");
293d984f
PT
299 }
300 return PTR_ERR(irb);
301}
302
303
304/**
305 * Check sense of a unit check.
306 *
307 * ch The channel, the sense code belongs to.
308 * sense The sense code to inspect.
309 */
aa3f2cb6 310static inline void ccw_unit_check(struct channel *ch, __u8 sense)
293d984f 311{
aa3f2cb6
PT
312 CTCM_DBF_TEXT_(TRACE, CTC_DBF_DEBUG,
313 "%s(%s): %02x",
314 CTCM_FUNTAIL, ch->id, sense);
315
293d984f
PT
316 if (sense & SNS0_INTERVENTION_REQ) {
317 if (sense & 0x01) {
aa3f2cb6 318 if (ch->sense_rc != 0x01) {
2a7c6f2c
PT
319 pr_notice(
320 "%s: The communication peer has "
321 "disconnected\n", ch->id);
aa3f2cb6
PT
322 ch->sense_rc = 0x01;
323 }
293d984f
PT
324 fsm_event(ch->fsm, CTC_EVENT_UC_RCRESET, ch);
325 } else {
aa3f2cb6 326 if (ch->sense_rc != SNS0_INTERVENTION_REQ) {
2a7c6f2c
PT
327 pr_notice(
328 "%s: The remote operating system is "
329 "not available\n", ch->id);
aa3f2cb6
PT
330 ch->sense_rc = SNS0_INTERVENTION_REQ;
331 }
293d984f
PT
332 fsm_event(ch->fsm, CTC_EVENT_UC_RSRESET, ch);
333 }
334 } else if (sense & SNS0_EQUIPMENT_CHECK) {
335 if (sense & SNS0_BUS_OUT_CHECK) {
aa3f2cb6
PT
336 if (ch->sense_rc != SNS0_BUS_OUT_CHECK) {
337 CTCM_DBF_TEXT_(TRACE, CTC_DBF_WARN,
338 "%s(%s): remote HW error %02x",
339 CTCM_FUNTAIL, ch->id, sense);
340 ch->sense_rc = SNS0_BUS_OUT_CHECK;
341 }
293d984f
PT
342 fsm_event(ch->fsm, CTC_EVENT_UC_HWFAIL, ch);
343 } else {
aa3f2cb6
PT
344 if (ch->sense_rc != SNS0_EQUIPMENT_CHECK) {
345 CTCM_DBF_TEXT_(TRACE, CTC_DBF_WARN,
346 "%s(%s): remote read parity error %02x",
347 CTCM_FUNTAIL, ch->id, sense);
348 ch->sense_rc = SNS0_EQUIPMENT_CHECK;
349 }
293d984f
PT
350 fsm_event(ch->fsm, CTC_EVENT_UC_RXPARITY, ch);
351 }
352 } else if (sense & SNS0_BUS_OUT_CHECK) {
aa3f2cb6
PT
353 if (ch->sense_rc != SNS0_BUS_OUT_CHECK) {
354 CTCM_DBF_TEXT_(TRACE, CTC_DBF_WARN,
355 "%s(%s): BUS OUT error %02x",
356 CTCM_FUNTAIL, ch->id, sense);
357 ch->sense_rc = SNS0_BUS_OUT_CHECK;
358 }
359 if (sense & 0x04) /* data-streaming timeout */
293d984f 360 fsm_event(ch->fsm, CTC_EVENT_UC_TXTIMEOUT, ch);
aa3f2cb6 361 else /* Data-transfer parity error */
293d984f 362 fsm_event(ch->fsm, CTC_EVENT_UC_TXPARITY, ch);
293d984f 363 } else if (sense & SNS0_CMD_REJECT) {
aa3f2cb6
PT
364 if (ch->sense_rc != SNS0_CMD_REJECT) {
365 CTCM_DBF_TEXT_(TRACE, CTC_DBF_WARN,
366 "%s(%s): Command rejected",
367 CTCM_FUNTAIL, ch->id);
368 ch->sense_rc = SNS0_CMD_REJECT;
369 }
293d984f 370 } else if (sense == 0) {
aa3f2cb6
PT
371 CTCM_DBF_TEXT_(TRACE, CTC_DBF_WARN,
372 "%s(%s): Unit check ZERO",
373 CTCM_FUNTAIL, ch->id);
293d984f
PT
374 fsm_event(ch->fsm, CTC_EVENT_UC_ZERO, ch);
375 } else {
aa3f2cb6
PT
376 CTCM_DBF_TEXT_(TRACE, CTC_DBF_WARN,
377 "%s(%s): Unit check code %02x unknown",
378 CTCM_FUNTAIL, ch->id, sense);
293d984f
PT
379 fsm_event(ch->fsm, CTC_EVENT_UC_UNKNOWN, ch);
380 }
381}
382
383int ctcm_ch_alloc_buffer(struct channel *ch)
384{
293d984f
PT
385 clear_normalized_cda(&ch->ccw[1]);
386 ch->trans_skb = __dev_alloc_skb(ch->max_bufsize, GFP_ATOMIC | GFP_DMA);
387 if (ch->trans_skb == NULL) {
aa3f2cb6
PT
388 CTCM_DBF_TEXT_(ERROR, CTC_DBF_ERROR,
389 "%s(%s): %s trans_skb allocation error",
390 CTCM_FUNTAIL, ch->id,
3c09e264
UB
391 (CHANNEL_DIRECTION(ch->flags) == CTCM_READ) ?
392 "RX" : "TX");
293d984f
PT
393 return -ENOMEM;
394 }
395
396 ch->ccw[1].count = ch->max_bufsize;
397 if (set_normalized_cda(&ch->ccw[1], ch->trans_skb->data)) {
398 dev_kfree_skb(ch->trans_skb);
399 ch->trans_skb = NULL;
aa3f2cb6
PT
400 CTCM_DBF_TEXT_(ERROR, CTC_DBF_ERROR,
401 "%s(%s): %s set norm_cda failed",
402 CTCM_FUNTAIL, ch->id,
3c09e264
UB
403 (CHANNEL_DIRECTION(ch->flags) == CTCM_READ) ?
404 "RX" : "TX");
293d984f
PT
405 return -ENOMEM;
406 }
407
408 ch->ccw[1].count = 0;
409 ch->trans_skb_data = ch->trans_skb->data;
410 ch->flags &= ~CHANNEL_FLAGS_BUFSIZE_CHANGED;
411 return 0;
412}
413
414/*
415 * Interface API for upper network layers
416 */
417
418/**
419 * Open an interface.
420 * Called from generic network layer when ifconfig up is run.
421 *
422 * dev Pointer to interface struct.
423 *
424 * returns 0 on success, -ERRNO on failure. (Never fails.)
425 */
426int ctcm_open(struct net_device *dev)
427{
261893d3 428 struct ctcm_priv *priv = dev->ml_priv;
293d984f
PT
429
430 CTCMY_DBF_DEV_NAME(SETUP, dev, "");
431 if (!IS_MPC(priv))
432 fsm_event(priv->fsm, DEV_EVENT_START, dev);
433 return 0;
434}
435
436/**
437 * Close an interface.
438 * Called from generic network layer when ifconfig down is run.
439 *
440 * dev Pointer to interface struct.
441 *
442 * returns 0 on success, -ERRNO on failure. (Never fails.)
443 */
444int ctcm_close(struct net_device *dev)
445{
261893d3 446 struct ctcm_priv *priv = dev->ml_priv;
293d984f
PT
447
448 CTCMY_DBF_DEV_NAME(SETUP, dev, "");
449 if (!IS_MPC(priv))
450 fsm_event(priv->fsm, DEV_EVENT_STOP, dev);
451 return 0;
452}
453
454
455/**
456 * Transmit a packet.
457 * This is a helper function for ctcm_tx().
458 *
459 * ch Channel to be used for sending.
460 * skb Pointer to struct sk_buff of packet to send.
461 * The linklevel header has already been set up
462 * by ctcm_tx().
463 *
464 * returns 0 on success, -ERRNO on failure. (Never fails.)
465 */
466static int ctcm_transmit_skb(struct channel *ch, struct sk_buff *skb)
467{
468 unsigned long saveflags;
469 struct ll_header header;
470 int rc = 0;
471 __u16 block_len;
472 int ccw_idx;
473 struct sk_buff *nskb;
474 unsigned long hi;
475
476 /* we need to acquire the lock for testing the state
477 * otherwise we can have an IRQ changing the state to
478 * TXIDLE after the test but before acquiring the lock.
479 */
480 spin_lock_irqsave(&ch->collect_lock, saveflags);
481 if (fsm_getstate(ch->fsm) != CTC_STATE_TXIDLE) {
482 int l = skb->len + LL_HEADER_LENGTH;
483
484 if (ch->collect_len + l > ch->max_bufsize - 2) {
485 spin_unlock_irqrestore(&ch->collect_lock, saveflags);
486 return -EBUSY;
487 } else {
488 atomic_inc(&skb->users);
489 header.length = l;
490 header.type = skb->protocol;
491 header.unused = 0;
492 memcpy(skb_push(skb, LL_HEADER_LENGTH), &header,
493 LL_HEADER_LENGTH);
494 skb_queue_tail(&ch->collect_queue, skb);
495 ch->collect_len += l;
496 }
497 spin_unlock_irqrestore(&ch->collect_lock, saveflags);
498 goto done;
499 }
500 spin_unlock_irqrestore(&ch->collect_lock, saveflags);
501 /*
502 * Protect skb against beeing free'd by upper
503 * layers.
504 */
505 atomic_inc(&skb->users);
506 ch->prof.txlen += skb->len;
507 header.length = skb->len + LL_HEADER_LENGTH;
508 header.type = skb->protocol;
509 header.unused = 0;
510 memcpy(skb_push(skb, LL_HEADER_LENGTH), &header, LL_HEADER_LENGTH);
511 block_len = skb->len + 2;
512 *((__u16 *)skb_push(skb, 2)) = block_len;
513
514 /*
515 * IDAL support in CTCM is broken, so we have to
516 * care about skb's above 2G ourselves.
517 */
518 hi = ((unsigned long)skb_tail_pointer(skb) + LL_HEADER_LENGTH) >> 31;
519 if (hi) {
520 nskb = alloc_skb(skb->len, GFP_ATOMIC | GFP_DMA);
521 if (!nskb) {
522 atomic_dec(&skb->users);
523 skb_pull(skb, LL_HEADER_LENGTH + 2);
524 ctcm_clear_busy(ch->netdev);
525 return -ENOMEM;
526 } else {
527 memcpy(skb_put(nskb, skb->len), skb->data, skb->len);
528 atomic_inc(&nskb->users);
529 atomic_dec(&skb->users);
530 dev_kfree_skb_irq(skb);
531 skb = nskb;
532 }
533 }
534
535 ch->ccw[4].count = block_len;
536 if (set_normalized_cda(&ch->ccw[4], skb->data)) {
537 /*
538 * idal allocation failed, try via copying to
539 * trans_skb. trans_skb usually has a pre-allocated
540 * idal.
541 */
542 if (ctcm_checkalloc_buffer(ch)) {
543 /*
544 * Remove our header. It gets added
545 * again on retransmit.
546 */
547 atomic_dec(&skb->users);
548 skb_pull(skb, LL_HEADER_LENGTH + 2);
549 ctcm_clear_busy(ch->netdev);
aa3f2cb6 550 return -ENOMEM;
293d984f
PT
551 }
552
553 skb_reset_tail_pointer(ch->trans_skb);
554 ch->trans_skb->len = 0;
555 ch->ccw[1].count = skb->len;
556 skb_copy_from_linear_data(skb,
557 skb_put(ch->trans_skb, skb->len), skb->len);
558 atomic_dec(&skb->users);
559 dev_kfree_skb_irq(skb);
560 ccw_idx = 0;
561 } else {
562 skb_queue_tail(&ch->io_queue, skb);
563 ccw_idx = 3;
564 }
565 ch->retry = 0;
566 fsm_newstate(ch->fsm, CTC_STATE_TX);
567 fsm_addtimer(&ch->timer, CTCM_TIME_5_SEC, CTC_EVENT_TIMER, ch);
568 spin_lock_irqsave(get_ccwdev_lock(ch->cdev), saveflags);
569 ch->prof.send_stamp = current_kernel_time(); /* xtime */
570 rc = ccw_device_start(ch->cdev, &ch->ccw[ccw_idx],
571 (unsigned long)ch, 0xff, 0);
572 spin_unlock_irqrestore(get_ccwdev_lock(ch->cdev), saveflags);
573 if (ccw_idx == 3)
574 ch->prof.doios_single++;
575 if (rc != 0) {
576 fsm_deltimer(&ch->timer);
577 ctcm_ccw_check_rc(ch, rc, "single skb TX");
578 if (ccw_idx == 3)
579 skb_dequeue_tail(&ch->io_queue);
580 /*
581 * Remove our header. It gets added
582 * again on retransmit.
583 */
584 skb_pull(skb, LL_HEADER_LENGTH + 2);
585 } else if (ccw_idx == 0) {
586 struct net_device *dev = ch->netdev;
261893d3 587 struct ctcm_priv *priv = dev->ml_priv;
293d984f
PT
588 priv->stats.tx_packets++;
589 priv->stats.tx_bytes += skb->len - LL_HEADER_LENGTH;
590 }
591done:
592 ctcm_clear_busy(ch->netdev);
593 return rc;
594}
595
596static void ctcmpc_send_sweep_req(struct channel *rch)
597{
598 struct net_device *dev = rch->netdev;
599 struct ctcm_priv *priv;
600 struct mpc_group *grp;
601 struct th_sweep *header;
602 struct sk_buff *sweep_skb;
603 struct channel *ch;
aa3f2cb6 604 /* int rc = 0; */
293d984f 605
261893d3 606 priv = dev->ml_priv;
293d984f 607 grp = priv->mpcg;
3c09e264 608 ch = priv->channel[CTCM_WRITE];
293d984f 609
293d984f
PT
610 /* sweep processing is not complete until response and request */
611 /* has completed for all read channels in group */
612 if (grp->in_sweep == 0) {
613 grp->in_sweep = 1;
3c09e264
UB
614 grp->sweep_rsp_pend_num = grp->active_channels[CTCM_READ];
615 grp->sweep_req_pend_num = grp->active_channels[CTCM_READ];
293d984f
PT
616 }
617
618 sweep_skb = __dev_alloc_skb(MPC_BUFSIZE_DEFAULT, GFP_ATOMIC|GFP_DMA);
619
620 if (sweep_skb == NULL) {
aa3f2cb6
PT
621 /* rc = -ENOMEM; */
622 goto nomem;
293d984f
PT
623 }
624
625 header = kmalloc(TH_SWEEP_LENGTH, gfp_type());
626
627 if (!header) {
628 dev_kfree_skb_any(sweep_skb);
aa3f2cb6
PT
629 /* rc = -ENOMEM; */
630 goto nomem;
293d984f
PT
631 }
632
633 header->th.th_seg = 0x00 ;
634 header->th.th_ch_flag = TH_SWEEP_REQ; /* 0x0f */
635 header->th.th_blk_flag = 0x00;
636 header->th.th_is_xid = 0x00;
637 header->th.th_seq_num = 0x00;
638 header->sw.th_last_seq = ch->th_seq_num;
639
640 memcpy(skb_put(sweep_skb, TH_SWEEP_LENGTH), header, TH_SWEEP_LENGTH);
641
642 kfree(header);
643
644 dev->trans_start = jiffies;
645 skb_queue_tail(&ch->sweep_queue, sweep_skb);
646
647 fsm_addtimer(&ch->sweep_timer, 100, CTC_EVENT_RSWEEP_TIMER, ch);
648
649 return;
650
aa3f2cb6
PT
651nomem:
652 grp->in_sweep = 0;
653 ctcm_clear_busy(dev);
654 fsm_event(grp->fsm, MPCG_EVENT_INOP, dev);
293d984f
PT
655
656 return;
657}
658
659/*
660 * MPC mode version of transmit_skb
661 */
662static int ctcmpc_transmit_skb(struct channel *ch, struct sk_buff *skb)
663{
664 struct pdu *p_header;
665 struct net_device *dev = ch->netdev;
261893d3 666 struct ctcm_priv *priv = dev->ml_priv;
293d984f
PT
667 struct mpc_group *grp = priv->mpcg;
668 struct th_header *header;
669 struct sk_buff *nskb;
670 int rc = 0;
671 int ccw_idx;
672 unsigned long hi;
673 unsigned long saveflags = 0; /* avoids compiler warning */
674 __u16 block_len;
675
aa3f2cb6
PT
676 CTCM_PR_DEBUG("Enter %s: %s, cp=%i ch=0x%p id=%s state=%s\n",
677 __func__, dev->name, smp_processor_id(), ch,
678 ch->id, fsm_getstate_str(ch->fsm));
293d984f
PT
679
680 if ((fsm_getstate(ch->fsm) != CTC_STATE_TXIDLE) || grp->in_sweep) {
681 spin_lock_irqsave(&ch->collect_lock, saveflags);
682 atomic_inc(&skb->users);
683 p_header = kmalloc(PDU_HEADER_LENGTH, gfp_type());
684
685 if (!p_header) {
293d984f 686 spin_unlock_irqrestore(&ch->collect_lock, saveflags);
aa3f2cb6 687 goto nomem_exit;
293d984f
PT
688 }
689
690 p_header->pdu_offset = skb->len;
691 p_header->pdu_proto = 0x01;
692 p_header->pdu_flag = 0x00;
693 if (skb->protocol == ntohs(ETH_P_SNAP)) {
694 p_header->pdu_flag |= PDU_FIRST | PDU_CNTL;
695 } else {
696 p_header->pdu_flag |= PDU_FIRST;
697 }
698 p_header->pdu_seq = 0;
699 memcpy(skb_push(skb, PDU_HEADER_LENGTH), p_header,
700 PDU_HEADER_LENGTH);
701
aa3f2cb6
PT
702 CTCM_PR_DEBUG("%s(%s): Put on collect_q - skb len: %04x \n"
703 "pdu header and data for up to 32 bytes:\n",
704 __func__, dev->name, skb->len);
705 CTCM_D3_DUMP((char *)skb->data, min_t(int, 32, skb->len));
293d984f
PT
706
707 skb_queue_tail(&ch->collect_queue, skb);
708 ch->collect_len += skb->len;
709 kfree(p_header);
710
711 spin_unlock_irqrestore(&ch->collect_lock, saveflags);
712 goto done;
713 }
714
715 /*
716 * Protect skb against beeing free'd by upper
717 * layers.
718 */
719 atomic_inc(&skb->users);
720
721 block_len = skb->len + TH_HEADER_LENGTH + PDU_HEADER_LENGTH;
722 /*
723 * IDAL support in CTCM is broken, so we have to
724 * care about skb's above 2G ourselves.
725 */
726 hi = ((unsigned long)skb->tail + TH_HEADER_LENGTH) >> 31;
727 if (hi) {
728 nskb = __dev_alloc_skb(skb->len, GFP_ATOMIC | GFP_DMA);
729 if (!nskb) {
aa3f2cb6 730 goto nomem_exit;
293d984f
PT
731 } else {
732 memcpy(skb_put(nskb, skb->len), skb->data, skb->len);
733 atomic_inc(&nskb->users);
734 atomic_dec(&skb->users);
735 dev_kfree_skb_irq(skb);
736 skb = nskb;
737 }
738 }
739
740 p_header = kmalloc(PDU_HEADER_LENGTH, gfp_type());
741
aa3f2cb6
PT
742 if (!p_header)
743 goto nomem_exit;
293d984f
PT
744
745 p_header->pdu_offset = skb->len;
746 p_header->pdu_proto = 0x01;
747 p_header->pdu_flag = 0x00;
748 p_header->pdu_seq = 0;
749 if (skb->protocol == ntohs(ETH_P_SNAP)) {
750 p_header->pdu_flag |= PDU_FIRST | PDU_CNTL;
751 } else {
752 p_header->pdu_flag |= PDU_FIRST;
753 }
754 memcpy(skb_push(skb, PDU_HEADER_LENGTH), p_header, PDU_HEADER_LENGTH);
755
756 kfree(p_header);
757
758 if (ch->collect_len > 0) {
759 spin_lock_irqsave(&ch->collect_lock, saveflags);
760 skb_queue_tail(&ch->collect_queue, skb);
761 ch->collect_len += skb->len;
762 skb = skb_dequeue(&ch->collect_queue);
763 ch->collect_len -= skb->len;
764 spin_unlock_irqrestore(&ch->collect_lock, saveflags);
765 }
766
767 p_header = (struct pdu *)skb->data;
768 p_header->pdu_flag |= PDU_LAST;
769
770 ch->prof.txlen += skb->len - PDU_HEADER_LENGTH;
771
772 header = kmalloc(TH_HEADER_LENGTH, gfp_type());
aa3f2cb6
PT
773 if (!header)
774 goto nomem_exit;
293d984f
PT
775
776 header->th_seg = 0x00;
777 header->th_ch_flag = TH_HAS_PDU; /* Normal data */
778 header->th_blk_flag = 0x00;
779 header->th_is_xid = 0x00; /* Just data here */
780 ch->th_seq_num++;
781 header->th_seq_num = ch->th_seq_num;
782
aa3f2cb6
PT
783 CTCM_PR_DBGDATA("%s(%s) ToVTAM_th_seq= %08x\n" ,
784 __func__, dev->name, ch->th_seq_num);
293d984f
PT
785
786 /* put the TH on the packet */
787 memcpy(skb_push(skb, TH_HEADER_LENGTH), header, TH_HEADER_LENGTH);
788
789 kfree(header);
790
aa3f2cb6
PT
791 CTCM_PR_DBGDATA("%s(%s): skb len: %04x\n - pdu header and data for "
792 "up to 32 bytes sent to vtam:\n",
793 __func__, dev->name, skb->len);
794 CTCM_D3_DUMP((char *)skb->data, min_t(int, 32, skb->len));
293d984f
PT
795
796 ch->ccw[4].count = skb->len;
797 if (set_normalized_cda(&ch->ccw[4], skb->data)) {
798 /*
aa3f2cb6
PT
799 * idal allocation failed, try via copying to trans_skb.
800 * trans_skb usually has a pre-allocated idal.
293d984f
PT
801 */
802 if (ctcm_checkalloc_buffer(ch)) {
803 /*
aa3f2cb6
PT
804 * Remove our header.
805 * It gets added again on retransmit.
293d984f 806 */
aa3f2cb6 807 goto nomem_exit;
293d984f
PT
808 }
809
810 skb_reset_tail_pointer(ch->trans_skb);
811 ch->trans_skb->len = 0;
812 ch->ccw[1].count = skb->len;
813 memcpy(skb_put(ch->trans_skb, skb->len), skb->data, skb->len);
814 atomic_dec(&skb->users);
815 dev_kfree_skb_irq(skb);
816 ccw_idx = 0;
aa3f2cb6
PT
817 CTCM_PR_DBGDATA("%s(%s): trans_skb len: %04x\n"
818 "up to 32 bytes sent to vtam:\n",
819 __func__, dev->name, ch->trans_skb->len);
820 CTCM_D3_DUMP((char *)ch->trans_skb->data,
821 min_t(int, 32, ch->trans_skb->len));
293d984f
PT
822 } else {
823 skb_queue_tail(&ch->io_queue, skb);
824 ccw_idx = 3;
825 }
826 ch->retry = 0;
827 fsm_newstate(ch->fsm, CTC_STATE_TX);
828 fsm_addtimer(&ch->timer, CTCM_TIME_5_SEC, CTC_EVENT_TIMER, ch);
829
830 if (do_debug_ccw)
831 ctcmpc_dumpit((char *)&ch->ccw[ccw_idx],
832 sizeof(struct ccw1) * 3);
833
834 spin_lock_irqsave(get_ccwdev_lock(ch->cdev), saveflags);
835 ch->prof.send_stamp = current_kernel_time(); /* xtime */
836 rc = ccw_device_start(ch->cdev, &ch->ccw[ccw_idx],
837 (unsigned long)ch, 0xff, 0);
838 spin_unlock_irqrestore(get_ccwdev_lock(ch->cdev), saveflags);
839 if (ccw_idx == 3)
840 ch->prof.doios_single++;
841 if (rc != 0) {
842 fsm_deltimer(&ch->timer);
843 ctcm_ccw_check_rc(ch, rc, "single skb TX");
844 if (ccw_idx == 3)
845 skb_dequeue_tail(&ch->io_queue);
846 } else if (ccw_idx == 0) {
847 priv->stats.tx_packets++;
848 priv->stats.tx_bytes += skb->len - TH_HEADER_LENGTH;
849 }
aa3f2cb6 850 if (ch->th_seq_num > 0xf0000000) /* Chose at random. */
293d984f
PT
851 ctcmpc_send_sweep_req(ch);
852
aa3f2cb6
PT
853 goto done;
854nomem_exit:
855 CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_CRIT,
856 "%s(%s): MEMORY allocation ERROR\n",
857 CTCM_FUNTAIL, ch->id);
858 rc = -ENOMEM;
859 atomic_dec(&skb->users);
860 dev_kfree_skb_any(skb);
861 fsm_event(priv->mpcg->fsm, MPCG_EVENT_INOP, dev);
293d984f 862done:
aa3f2cb6
PT
863 CTCM_PR_DEBUG("Exit %s(%s)\n", __func__, dev->name);
864 return rc;
293d984f
PT
865}
866
867/**
868 * Start transmission of a packet.
869 * Called from generic network device layer.
870 *
871 * skb Pointer to buffer containing the packet.
872 * dev Pointer to interface struct.
873 *
874 * returns 0 if packet consumed, !0 if packet rejected.
875 * Note: If we return !0, then the packet is free'd by
876 * the generic network layer.
877 */
878/* first merge version - leaving both functions separated */
879static int ctcm_tx(struct sk_buff *skb, struct net_device *dev)
880{
261893d3 881 struct ctcm_priv *priv = dev->ml_priv;
293d984f
PT
882
883 if (skb == NULL) {
aa3f2cb6
PT
884 CTCM_DBF_TEXT_(ERROR, CTC_DBF_ERROR,
885 "%s(%s): NULL sk_buff passed",
886 CTCM_FUNTAIL, dev->name);
293d984f 887 priv->stats.tx_dropped++;
6ed10654 888 return NETDEV_TX_OK;
293d984f
PT
889 }
890 if (skb_headroom(skb) < (LL_HEADER_LENGTH + 2)) {
aa3f2cb6
PT
891 CTCM_DBF_TEXT_(ERROR, CTC_DBF_ERROR,
892 "%s(%s): Got sk_buff with head room < %ld bytes",
893 CTCM_FUNTAIL, dev->name, LL_HEADER_LENGTH + 2);
293d984f
PT
894 dev_kfree_skb(skb);
895 priv->stats.tx_dropped++;
6ed10654 896 return NETDEV_TX_OK;
293d984f
PT
897 }
898
899 /*
900 * If channels are not running, try to restart them
901 * and throw away packet.
902 */
903 if (fsm_getstate(priv->fsm) != DEV_STATE_RUNNING) {
904 fsm_event(priv->fsm, DEV_EVENT_START, dev);
905 dev_kfree_skb(skb);
906 priv->stats.tx_dropped++;
907 priv->stats.tx_errors++;
908 priv->stats.tx_carrier_errors++;
6ed10654 909 return NETDEV_TX_OK;
293d984f
PT
910 }
911
912 if (ctcm_test_and_set_busy(dev))
3a05d140 913 return NETDEV_TX_BUSY;
293d984f
PT
914
915 dev->trans_start = jiffies;
3c09e264 916 if (ctcm_transmit_skb(priv->channel[CTCM_WRITE], skb) != 0)
3a05d140 917 return NETDEV_TX_BUSY;
6ed10654 918 return NETDEV_TX_OK;
293d984f
PT
919}
920
921/* unmerged MPC variant of ctcm_tx */
922static int ctcmpc_tx(struct sk_buff *skb, struct net_device *dev)
923{
924 int len = 0;
261893d3 925 struct ctcm_priv *priv = dev->ml_priv;
aa3f2cb6 926 struct mpc_group *grp = priv->mpcg;
293d984f
PT
927 struct sk_buff *newskb = NULL;
928
293d984f
PT
929 /*
930 * Some sanity checks ...
931 */
932 if (skb == NULL) {
aa3f2cb6
PT
933 CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
934 "%s(%s): NULL sk_buff passed",
935 CTCM_FUNTAIL, dev->name);
293d984f
PT
936 priv->stats.tx_dropped++;
937 goto done;
938 }
939 if (skb_headroom(skb) < (TH_HEADER_LENGTH + PDU_HEADER_LENGTH)) {
aa3f2cb6
PT
940 CTCM_DBF_TEXT_(MPC_TRACE, CTC_DBF_ERROR,
941 "%s(%s): Got sk_buff with head room < %ld bytes",
942 CTCM_FUNTAIL, dev->name,
943 TH_HEADER_LENGTH + PDU_HEADER_LENGTH);
293d984f 944
aa3f2cb6 945 CTCM_D3_DUMP((char *)skb->data, min_t(int, 32, skb->len));
293d984f
PT
946
947 len = skb->len + TH_HEADER_LENGTH + PDU_HEADER_LENGTH;
948 newskb = __dev_alloc_skb(len, gfp_type() | GFP_DMA);
949
950 if (!newskb) {
aa3f2cb6
PT
951 CTCM_DBF_TEXT_(MPC_TRACE, CTC_DBF_ERROR,
952 "%s: %s: __dev_alloc_skb failed",
953 __func__, dev->name);
293d984f
PT
954
955 dev_kfree_skb_any(skb);
956 priv->stats.tx_dropped++;
957 priv->stats.tx_errors++;
958 priv->stats.tx_carrier_errors++;
959 fsm_event(grp->fsm, MPCG_EVENT_INOP, dev);
960 goto done;
961 }
962 newskb->protocol = skb->protocol;
963 skb_reserve(newskb, TH_HEADER_LENGTH + PDU_HEADER_LENGTH);
964 memcpy(skb_put(newskb, skb->len), skb->data, skb->len);
965 dev_kfree_skb_any(skb);
966 skb = newskb;
967 }
968
969 /*
970 * If channels are not running,
971 * notify anybody about a link failure and throw
972 * away packet.
973 */
974 if ((fsm_getstate(priv->fsm) != DEV_STATE_RUNNING) ||
975 (fsm_getstate(grp->fsm) < MPCG_STATE_XID2INITW)) {
976 dev_kfree_skb_any(skb);
aa3f2cb6
PT
977 CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
978 "%s(%s): inactive MPCGROUP - dropped",
979 CTCM_FUNTAIL, dev->name);
293d984f
PT
980 priv->stats.tx_dropped++;
981 priv->stats.tx_errors++;
982 priv->stats.tx_carrier_errors++;
983 goto done;
984 }
985
986 if (ctcm_test_and_set_busy(dev)) {
aa3f2cb6
PT
987 CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
988 "%s(%s): device busy - dropped",
989 CTCM_FUNTAIL, dev->name);
293d984f
PT
990 dev_kfree_skb_any(skb);
991 priv->stats.tx_dropped++;
992 priv->stats.tx_errors++;
993 priv->stats.tx_carrier_errors++;
994 fsm_event(grp->fsm, MPCG_EVENT_INOP, dev);
995 goto done;
996 }
997
998 dev->trans_start = jiffies;
3c09e264 999 if (ctcmpc_transmit_skb(priv->channel[CTCM_WRITE], skb) != 0) {
aa3f2cb6
PT
1000 CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
1001 "%s(%s): device error - dropped",
1002 CTCM_FUNTAIL, dev->name);
293d984f
PT
1003 dev_kfree_skb_any(skb);
1004 priv->stats.tx_dropped++;
1005 priv->stats.tx_errors++;
1006 priv->stats.tx_carrier_errors++;
1007 ctcm_clear_busy(dev);
1008 fsm_event(grp->fsm, MPCG_EVENT_INOP, dev);
1009 goto done;
1010 }
1011 ctcm_clear_busy(dev);
1012done:
1013 if (do_debug)
1014 MPC_DBF_DEV_NAME(TRACE, dev, "exit");
1015
6ed10654 1016 return NETDEV_TX_OK; /* handle freeing of skb here */
293d984f
PT
1017}
1018
1019
1020/**
1021 * Sets MTU of an interface.
1022 *
1023 * dev Pointer to interface struct.
1024 * new_mtu The new MTU to use for this interface.
1025 *
1026 * returns 0 on success, -EINVAL if MTU is out of valid range.
1027 * (valid range is 576 .. 65527). If VM is on the
1028 * remote side, maximum MTU is 32760, however this is
1029 * not checked here.
1030 */
1031static int ctcm_change_mtu(struct net_device *dev, int new_mtu)
1032{
1033 struct ctcm_priv *priv;
1034 int max_bufsize;
1035
293d984f
PT
1036 if (new_mtu < 576 || new_mtu > 65527)
1037 return -EINVAL;
1038
261893d3 1039 priv = dev->ml_priv;
3c09e264 1040 max_bufsize = priv->channel[CTCM_READ]->max_bufsize;
293d984f
PT
1041
1042 if (IS_MPC(priv)) {
1043 if (new_mtu > max_bufsize - TH_HEADER_LENGTH)
1044 return -EINVAL;
1045 dev->hard_header_len = TH_HEADER_LENGTH + PDU_HEADER_LENGTH;
1046 } else {
1047 if (new_mtu > max_bufsize - LL_HEADER_LENGTH - 2)
1048 return -EINVAL;
1049 dev->hard_header_len = LL_HEADER_LENGTH + 2;
1050 }
1051 dev->mtu = new_mtu;
1052 return 0;
1053}
1054
1055/**
1056 * Returns interface statistics of a device.
1057 *
1058 * dev Pointer to interface struct.
1059 *
1060 * returns Pointer to stats struct of this interface.
1061 */
1062static struct net_device_stats *ctcm_stats(struct net_device *dev)
1063{
261893d3 1064 return &((struct ctcm_priv *)dev->ml_priv)->stats;
293d984f
PT
1065}
1066
293d984f
PT
1067static void ctcm_free_netdevice(struct net_device *dev)
1068{
1069 struct ctcm_priv *priv;
1070 struct mpc_group *grp;
1071
aa3f2cb6
PT
1072 CTCM_DBF_TEXT_(SETUP, CTC_DBF_INFO,
1073 "%s(%s)", CTCM_FUNTAIL, dev->name);
261893d3 1074 priv = dev->ml_priv;
293d984f
PT
1075 if (priv) {
1076 grp = priv->mpcg;
1077 if (grp) {
1078 if (grp->fsm)
1079 kfree_fsm(grp->fsm);
1080 if (grp->xid_skb)
1081 dev_kfree_skb(grp->xid_skb);
1082 if (grp->rcvd_xid_skb)
1083 dev_kfree_skb(grp->rcvd_xid_skb);
1084 tasklet_kill(&grp->mpc_tasklet2);
1085 kfree(grp);
1086 priv->mpcg = NULL;
1087 }
1088 if (priv->fsm) {
1089 kfree_fsm(priv->fsm);
1090 priv->fsm = NULL;
1091 }
1092 kfree(priv->xid);
1093 priv->xid = NULL;
1094 /*
1095 * Note: kfree(priv); is done in "opposite" function of
1096 * allocator function probe_device which is remove_device.
1097 */
1098 }
1099#ifdef MODULE
1100 free_netdev(dev);
1101#endif
1102}
1103
1104struct mpc_group *ctcmpc_init_mpc_group(struct ctcm_priv *priv);
1105
69b3aa60
FB
1106static const struct net_device_ops ctcm_netdev_ops = {
1107 .ndo_open = ctcm_open,
1108 .ndo_stop = ctcm_close,
1109 .ndo_get_stats = ctcm_stats,
1110 .ndo_change_mtu = ctcm_change_mtu,
1111 .ndo_start_xmit = ctcm_tx,
1112};
1113
1114static const struct net_device_ops ctcm_mpc_netdev_ops = {
1115 .ndo_open = ctcm_open,
1116 .ndo_stop = ctcm_close,
1117 .ndo_get_stats = ctcm_stats,
1118 .ndo_change_mtu = ctcm_change_mtu,
1119 .ndo_start_xmit = ctcmpc_tx,
1120};
1121
293d984f
PT
1122void static ctcm_dev_setup(struct net_device *dev)
1123{
293d984f
PT
1124 dev->type = ARPHRD_SLIP;
1125 dev->tx_queue_len = 100;
1126 dev->flags = IFF_POINTOPOINT | IFF_NOARP;
1127}
1128
1129/*
1130 * Initialize everything of the net device except the name and the
1131 * channel structs.
1132 */
1133static struct net_device *ctcm_init_netdevice(struct ctcm_priv *priv)
1134{
1135 struct net_device *dev;
1136 struct mpc_group *grp;
1137 if (!priv)
1138 return NULL;
1139
1140 if (IS_MPC(priv))
1141 dev = alloc_netdev(0, MPC_DEVICE_GENE, ctcm_dev_setup);
1142 else
1143 dev = alloc_netdev(0, CTC_DEVICE_GENE, ctcm_dev_setup);
1144
1145 if (!dev) {
aa3f2cb6
PT
1146 CTCM_DBF_TEXT_(ERROR, CTC_DBF_CRIT,
1147 "%s: MEMORY allocation ERROR",
1148 CTCM_FUNTAIL);
293d984f
PT
1149 return NULL;
1150 }
261893d3 1151 dev->ml_priv = priv;
293d984f
PT
1152 priv->fsm = init_fsm("ctcmdev", dev_state_names, dev_event_names,
1153 CTCM_NR_DEV_STATES, CTCM_NR_DEV_EVENTS,
1154 dev_fsm, dev_fsm_len, GFP_KERNEL);
1155 if (priv->fsm == NULL) {
1156 CTCMY_DBF_DEV(SETUP, dev, "init_fsm error");
bc68580d 1157 free_netdev(dev);
293d984f
PT
1158 return NULL;
1159 }
1160 fsm_newstate(priv->fsm, DEV_STATE_STOPPED);
1161 fsm_settimer(priv->fsm, &priv->restart_timer);
1162
1163 if (IS_MPC(priv)) {
1164 /* MPC Group Initializations */
1165 grp = ctcmpc_init_mpc_group(priv);
1166 if (grp == NULL) {
1167 MPC_DBF_DEV(SETUP, dev, "init_mpc_group error");
bc68580d 1168 free_netdev(dev);
293d984f
PT
1169 return NULL;
1170 }
1171 tasklet_init(&grp->mpc_tasklet2,
1172 mpc_group_ready, (unsigned long)dev);
1173 dev->mtu = MPC_BUFSIZE_DEFAULT -
1174 TH_HEADER_LENGTH - PDU_HEADER_LENGTH;
1175
69b3aa60 1176 dev->netdev_ops = &ctcm_mpc_netdev_ops;
293d984f
PT
1177 dev->hard_header_len = TH_HEADER_LENGTH + PDU_HEADER_LENGTH;
1178 priv->buffer_size = MPC_BUFSIZE_DEFAULT;
1179 } else {
1180 dev->mtu = CTCM_BUFSIZE_DEFAULT - LL_HEADER_LENGTH - 2;
69b3aa60 1181 dev->netdev_ops = &ctcm_netdev_ops;
293d984f
PT
1182 dev->hard_header_len = LL_HEADER_LENGTH + 2;
1183 }
1184
1185 CTCMY_DBF_DEV(SETUP, dev, "finished");
aa3f2cb6 1186
293d984f
PT
1187 return dev;
1188}
1189
1190/**
1191 * Main IRQ handler.
1192 *
1193 * cdev The ccw_device the interrupt is for.
1194 * intparm interruption parameter.
1195 * irb interruption response block.
1196 */
1197static void ctcm_irq_handler(struct ccw_device *cdev,
1198 unsigned long intparm, struct irb *irb)
1199{
1200 struct channel *ch;
1201 struct net_device *dev;
1202 struct ctcm_priv *priv;
1203 struct ccwgroup_device *cgdev;
aa3f2cb6
PT
1204 int cstat;
1205 int dstat;
1206
1207 CTCM_DBF_TEXT_(TRACE, CTC_DBF_DEBUG,
2a0217d5 1208 "Enter %s(%s)", CTCM_FUNTAIL, dev_name(&cdev->dev));
293d984f 1209
293d984f
PT
1210 if (ctcm_check_irb_error(cdev, irb))
1211 return;
1212
1213 cgdev = dev_get_drvdata(&cdev->dev);
1214
aa3f2cb6
PT
1215 cstat = irb->scsw.cmd.cstat;
1216 dstat = irb->scsw.cmd.dstat;
1217
293d984f
PT
1218 /* Check for unsolicited interrupts. */
1219 if (cgdev == NULL) {
2a7c6f2c
PT
1220 CTCM_DBF_TEXT_(TRACE, CTC_DBF_ERROR,
1221 "%s(%s) unsolicited irq: c-%02x d-%02x\n",
1222 CTCM_FUNTAIL, dev_name(&cdev->dev), cstat, dstat);
1223 dev_warn(&cdev->dev,
1224 "The adapter received a non-specific IRQ\n");
293d984f
PT
1225 return;
1226 }
1227
1228 priv = dev_get_drvdata(&cgdev->dev);
1229
1230 /* Try to extract channel from driver data. */
3c09e264
UB
1231 if (priv->channel[CTCM_READ]->cdev == cdev)
1232 ch = priv->channel[CTCM_READ];
1233 else if (priv->channel[CTCM_WRITE]->cdev == cdev)
1234 ch = priv->channel[CTCM_WRITE];
293d984f 1235 else {
2a7c6f2c
PT
1236 dev_err(&cdev->dev,
1237 "%s: Internal error: Can't determine channel for "
1238 "interrupt device %s\n",
1239 __func__, dev_name(&cdev->dev));
1240 /* Explain: inconsistent internal structures */
293d984f
PT
1241 return;
1242 }
1243
aa3f2cb6 1244 dev = ch->netdev;
293d984f 1245 if (dev == NULL) {
2a7c6f2c
PT
1246 dev_err(&cdev->dev,
1247 "%s Internal error: net_device is NULL, ch = 0x%p\n",
1248 __func__, ch);
1249 /* Explain: inconsistent internal structures */
293d984f
PT
1250 return;
1251 }
1252
293d984f
PT
1253 /* Copy interruption response block. */
1254 memcpy(ch->irb, irb, sizeof(struct irb));
1255
2a7c6f2c 1256 /* Issue error message and return on subchannel error code */
23d805b6 1257 if (irb->scsw.cmd.cstat) {
293d984f 1258 fsm_event(ch->fsm, CTC_EVENT_SC_UNKNOWN, ch);
2a7c6f2c
PT
1259 CTCM_DBF_TEXT_(TRACE, CTC_DBF_WARN,
1260 "%s(%s): sub-ch check %s: cs=%02x ds=%02x",
1261 CTCM_FUNTAIL, dev->name, ch->id, cstat, dstat);
1262 dev_warn(&cdev->dev,
1263 "A check occurred on the subchannel\n");
293d984f
PT
1264 return;
1265 }
1266
1267 /* Check the reason-code of a unit check */
23d805b6 1268 if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) {
aa3f2cb6
PT
1269 if ((irb->ecw[0] & ch->sense_rc) == 0)
1270 /* print it only once */
2a7c6f2c 1271 CTCM_DBF_TEXT_(TRACE, CTC_DBF_WARN,
aa3f2cb6
PT
1272 "%s(%s): sense=%02x, ds=%02x",
1273 CTCM_FUNTAIL, ch->id, irb->ecw[0], dstat);
293d984f
PT
1274 ccw_unit_check(ch, irb->ecw[0]);
1275 return;
1276 }
23d805b6
PO
1277 if (irb->scsw.cmd.dstat & DEV_STAT_BUSY) {
1278 if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION)
293d984f
PT
1279 fsm_event(ch->fsm, CTC_EVENT_ATTNBUSY, ch);
1280 else
1281 fsm_event(ch->fsm, CTC_EVENT_BUSY, ch);
1282 return;
1283 }
23d805b6 1284 if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) {
293d984f
PT
1285 fsm_event(ch->fsm, CTC_EVENT_ATTN, ch);
1286 return;
1287 }
23d805b6
PO
1288 if ((irb->scsw.cmd.stctl & SCSW_STCTL_SEC_STATUS) ||
1289 (irb->scsw.cmd.stctl == SCSW_STCTL_STATUS_PEND) ||
1290 (irb->scsw.cmd.stctl ==
293d984f
PT
1291 (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND)))
1292 fsm_event(ch->fsm, CTC_EVENT_FINSTAT, ch);
1293 else
1294 fsm_event(ch->fsm, CTC_EVENT_IRQ, ch);
1295
1296}
1297
1298/**
1299 * Add ctcm specific attributes.
1300 * Add ctcm private data.
1301 *
1302 * cgdev pointer to ccwgroup_device just added
1303 *
1304 * returns 0 on success, !0 on failure.
1305 */
1306static int ctcm_probe_device(struct ccwgroup_device *cgdev)
1307{
1308 struct ctcm_priv *priv;
1309 int rc;
1310
aa3f2cb6
PT
1311 CTCM_DBF_TEXT_(SETUP, CTC_DBF_INFO,
1312 "%s %p",
1313 __func__, cgdev);
293d984f
PT
1314
1315 if (!get_device(&cgdev->dev))
1316 return -ENODEV;
1317
1318 priv = kzalloc(sizeof(struct ctcm_priv), GFP_KERNEL);
1319 if (!priv) {
aa3f2cb6
PT
1320 CTCM_DBF_TEXT_(ERROR, CTC_DBF_ERROR,
1321 "%s: memory allocation failure",
1322 CTCM_FUNTAIL);
293d984f
PT
1323 put_device(&cgdev->dev);
1324 return -ENOMEM;
1325 }
1326
1327 rc = ctcm_add_files(&cgdev->dev);
1328 if (rc) {
1329 kfree(priv);
1330 put_device(&cgdev->dev);
1331 return rc;
1332 }
1333 priv->buffer_size = CTCM_BUFSIZE_DEFAULT;
1334 cgdev->cdev[0]->handler = ctcm_irq_handler;
1335 cgdev->cdev[1]->handler = ctcm_irq_handler;
1336 dev_set_drvdata(&cgdev->dev, priv);
1337
1338 return 0;
1339}
1340
1341/**
1342 * Add a new channel to the list of channels.
1343 * Keeps the channel list sorted.
1344 *
1345 * cdev The ccw_device to be added.
1346 * type The type class of the new channel.
1347 * priv Points to the private data of the ccwgroup_device.
1348 *
1349 * returns 0 on success, !0 on error.
1350 */
0ca8cc6f 1351static int add_channel(struct ccw_device *cdev, enum ctcm_channel_types type,
293d984f
PT
1352 struct ctcm_priv *priv)
1353{
1354 struct channel **c = &channels;
1355 struct channel *ch;
1356 int ccw_num;
1357 int rc = 0;
1358
aa3f2cb6
PT
1359 CTCM_DBF_TEXT_(SETUP, CTC_DBF_INFO,
1360 "%s(%s), type %d, proto %d",
2a0217d5 1361 __func__, dev_name(&cdev->dev), type, priv->protocol);
aa3f2cb6 1362
293d984f
PT
1363 ch = kzalloc(sizeof(struct channel), GFP_KERNEL);
1364 if (ch == NULL)
aa3f2cb6 1365 return -ENOMEM;
293d984f
PT
1366
1367 ch->protocol = priv->protocol;
1368 if (IS_MPC(priv)) {
ae57b20a 1369 ch->discontact_th = kzalloc(TH_HEADER_LENGTH, gfp_type());
293d984f
PT
1370 if (ch->discontact_th == NULL)
1371 goto nomem_return;
1372
1373 ch->discontact_th->th_blk_flag = TH_DISCONTACT;
1374 tasklet_init(&ch->ch_disc_tasklet,
1375 mpc_action_send_discontact, (unsigned long)ch);
1376
1377 tasklet_init(&ch->ch_tasklet, ctcmpc_bh, (unsigned long)ch);
1378 ch->max_bufsize = (MPC_BUFSIZE_DEFAULT - 35);
1379 ccw_num = 17;
1380 } else
1381 ccw_num = 8;
1382
ae57b20a 1383 ch->ccw = kzalloc(ccw_num * sizeof(struct ccw1), GFP_KERNEL | GFP_DMA);
293d984f
PT
1384 if (ch->ccw == NULL)
1385 goto nomem_return;
1386
1387 ch->cdev = cdev;
2a0217d5 1388 snprintf(ch->id, CTCM_ID_SIZE, "ch-%s", dev_name(&cdev->dev));
293d984f
PT
1389 ch->type = type;
1390
1391 /**
1392 * "static" ccws are used in the following way:
1393 *
1394 * ccw[0..2] (Channel program for generic I/O):
1395 * 0: prepare
1396 * 1: read or write (depending on direction) with fixed
1397 * buffer (idal allocated once when buffer is allocated)
1398 * 2: nop
1399 * ccw[3..5] (Channel program for direct write of packets)
1400 * 3: prepare
1401 * 4: write (idal allocated on every write).
1402 * 5: nop
1403 * ccw[6..7] (Channel program for initial channel setup):
1404 * 6: set extended mode
1405 * 7: nop
1406 *
1407 * ch->ccw[0..5] are initialized in ch_action_start because
1408 * the channel's direction is yet unknown here.
1409 *
1410 * ccws used for xid2 negotiations
1411 * ch-ccw[8-14] need to be used for the XID exchange either
1412 * X side XID2 Processing
1413 * 8: write control
1414 * 9: write th
1415 * 10: write XID
1416 * 11: read th from secondary
1417 * 12: read XID from secondary
1418 * 13: read 4 byte ID
1419 * 14: nop
1420 * Y side XID Processing
1421 * 8: sense
1422 * 9: read th
1423 * 10: read XID
1424 * 11: write th
1425 * 12: write XID
1426 * 13: write 4 byte ID
1427 * 14: nop
1428 *
1429 * ccws used for double noop due to VM timing issues
1430 * which result in unrecoverable Busy on channel
1431 * 15: nop
1432 * 16: nop
1433 */
1434 ch->ccw[6].cmd_code = CCW_CMD_SET_EXTENDED;
1435 ch->ccw[6].flags = CCW_FLAG_SLI;
1436
1437 ch->ccw[7].cmd_code = CCW_CMD_NOOP;
1438 ch->ccw[7].flags = CCW_FLAG_SLI;
1439
1440 if (IS_MPC(priv)) {
1441 ch->ccw[15].cmd_code = CCW_CMD_WRITE;
1442 ch->ccw[15].flags = CCW_FLAG_SLI | CCW_FLAG_CC;
1443 ch->ccw[15].count = TH_HEADER_LENGTH;
1444 ch->ccw[15].cda = virt_to_phys(ch->discontact_th);
1445
1446 ch->ccw[16].cmd_code = CCW_CMD_NOOP;
1447 ch->ccw[16].flags = CCW_FLAG_SLI;
1448
1449 ch->fsm = init_fsm(ch->id, ctc_ch_state_names,
1450 ctc_ch_event_names, CTC_MPC_NR_STATES,
1451 CTC_MPC_NR_EVENTS, ctcmpc_ch_fsm,
1452 mpc_ch_fsm_len, GFP_KERNEL);
1453 } else {
1454 ch->fsm = init_fsm(ch->id, ctc_ch_state_names,
1455 ctc_ch_event_names, CTC_NR_STATES,
1456 CTC_NR_EVENTS, ch_fsm,
1457 ch_fsm_len, GFP_KERNEL);
1458 }
1459 if (ch->fsm == NULL)
1460 goto free_return;
1461
1462 fsm_newstate(ch->fsm, CTC_STATE_IDLE);
1463
1464 ch->irb = kzalloc(sizeof(struct irb), GFP_KERNEL);
1465 if (ch->irb == NULL)
1466 goto nomem_return;
1467
1468 while (*c && ctcm_less_than((*c)->id, ch->id))
1469 c = &(*c)->next;
1470
1471 if (*c && (!strncmp((*c)->id, ch->id, CTCM_ID_SIZE))) {
1472 CTCM_DBF_TEXT_(SETUP, CTC_DBF_INFO,
1473 "%s (%s) already in list, using old entry",
aa3f2cb6 1474 __func__, (*c)->id);
293d984f
PT
1475
1476 goto free_return;
1477 }
1478
1479 spin_lock_init(&ch->collect_lock);
1480
1481 fsm_settimer(ch->fsm, &ch->timer);
1482 skb_queue_head_init(&ch->io_queue);
1483 skb_queue_head_init(&ch->collect_queue);
1484
1485 if (IS_MPC(priv)) {
1486 fsm_settimer(ch->fsm, &ch->sweep_timer);
1487 skb_queue_head_init(&ch->sweep_queue);
1488 }
1489 ch->next = *c;
1490 *c = ch;
1491 return 0;
1492
1493nomem_return:
293d984f
PT
1494 rc = -ENOMEM;
1495
1496free_return: /* note that all channel pointers are 0 or valid */
aa3f2cb6 1497 kfree(ch->ccw);
293d984f
PT
1498 kfree(ch->discontact_th);
1499 kfree_fsm(ch->fsm);
1500 kfree(ch->irb);
1501 kfree(ch);
1502 return rc;
1503}
1504
1505/*
1506 * Return type of a detected device.
1507 */
0ca8cc6f 1508static enum ctcm_channel_types get_channel_type(struct ccw_device_id *id)
293d984f 1509{
0ca8cc6f
UB
1510 enum ctcm_channel_types type;
1511 type = (enum ctcm_channel_types)id->driver_info;
293d984f 1512
0ca8cc6f
UB
1513 if (type == ctcm_channel_type_ficon)
1514 type = ctcm_channel_type_escon;
293d984f
PT
1515
1516 return type;
1517}
1518
1519/**
1520 *
1521 * Setup an interface.
1522 *
1523 * cgdev Device to be setup.
1524 *
1525 * returns 0 on success, !0 on failure.
1526 */
1527static int ctcm_new_device(struct ccwgroup_device *cgdev)
1528{
1529 char read_id[CTCM_ID_SIZE];
1530 char write_id[CTCM_ID_SIZE];
1531 int direction;
0ca8cc6f 1532 enum ctcm_channel_types type;
293d984f
PT
1533 struct ctcm_priv *priv;
1534 struct net_device *dev;
aa3f2cb6
PT
1535 struct ccw_device *cdev0;
1536 struct ccw_device *cdev1;
a1c1f5ea
EL
1537 struct channel *readc;
1538 struct channel *writec;
293d984f 1539 int ret;
a1c1f5ea 1540 int result;
293d984f 1541
293d984f 1542 priv = dev_get_drvdata(&cgdev->dev);
a1c1f5ea
EL
1543 if (!priv) {
1544 result = -ENODEV;
1545 goto out_err_result;
1546 }
293d984f 1547
aa3f2cb6
PT
1548 cdev0 = cgdev->cdev[0];
1549 cdev1 = cgdev->cdev[1];
1550
1551 type = get_channel_type(&cdev0->id);
293d984f 1552
b9d3aed7
CH
1553 snprintf(read_id, CTCM_ID_SIZE, "ch-%s", dev_name(&cdev0->dev));
1554 snprintf(write_id, CTCM_ID_SIZE, "ch-%s", dev_name(&cdev1->dev));
293d984f 1555
aa3f2cb6 1556 ret = add_channel(cdev0, type, priv);
a1c1f5ea
EL
1557 if (ret) {
1558 result = ret;
1559 goto out_err_result;
1560 }
aa3f2cb6 1561 ret = add_channel(cdev1, type, priv);
a1c1f5ea
EL
1562 if (ret) {
1563 result = ret;
1564 goto out_remove_channel1;
1565 }
293d984f 1566
aa3f2cb6 1567 ret = ccw_device_set_online(cdev0);
293d984f 1568 if (ret != 0) {
aa3f2cb6
PT
1569 CTCM_DBF_TEXT_(TRACE, CTC_DBF_NOTICE,
1570 "%s(%s) set_online rc=%d",
1571 CTCM_FUNTAIL, read_id, ret);
a1c1f5ea
EL
1572 result = -EIO;
1573 goto out_remove_channel2;
293d984f
PT
1574 }
1575
aa3f2cb6 1576 ret = ccw_device_set_online(cdev1);
293d984f 1577 if (ret != 0) {
aa3f2cb6
PT
1578 CTCM_DBF_TEXT_(TRACE, CTC_DBF_NOTICE,
1579 "%s(%s) set_online rc=%d",
1580 CTCM_FUNTAIL, write_id, ret);
a1c1f5ea
EL
1581
1582 result = -EIO;
1583 goto out_ccw1;
293d984f
PT
1584 }
1585
1586 dev = ctcm_init_netdevice(priv);
a1c1f5ea
EL
1587 if (dev == NULL) {
1588 result = -ENODEV;
1589 goto out_ccw2;
1590 }
293d984f 1591
3c09e264 1592 for (direction = CTCM_READ; direction <= CTCM_WRITE; direction++) {
293d984f 1593 priv->channel[direction] =
3c09e264
UB
1594 channel_get(type, direction == CTCM_READ ?
1595 read_id : write_id, direction);
293d984f 1596 if (priv->channel[direction] == NULL) {
3c09e264
UB
1597 if (direction == CTCM_WRITE)
1598 channel_free(priv->channel[CTCM_READ]);
aa3f2cb6 1599 goto out_dev;
293d984f
PT
1600 }
1601 priv->channel[direction]->netdev = dev;
1602 priv->channel[direction]->protocol = priv->protocol;
1603 priv->channel[direction]->max_bufsize = priv->buffer_size;
1604 }
1605 /* sysfs magic */
1606 SET_NETDEV_DEV(dev, &cgdev->dev);
1607
a1c1f5ea
EL
1608 if (register_netdev(dev)) {
1609 result = -ENODEV;
1610 goto out_dev;
1611 }
293d984f
PT
1612
1613 if (ctcm_add_attributes(&cgdev->dev)) {
a1c1f5ea
EL
1614 result = -ENODEV;
1615 goto out_unregister;
293d984f
PT
1616 }
1617
1618 strlcpy(priv->fsm->name, dev->name, sizeof(priv->fsm->name));
1619
2a7c6f2c
PT
1620 dev_info(&dev->dev,
1621 "setup OK : r/w = %s/%s, protocol : %d\n",
3c09e264
UB
1622 priv->channel[CTCM_READ]->id,
1623 priv->channel[CTCM_WRITE]->id, priv->protocol);
2a7c6f2c 1624
293d984f 1625 CTCM_DBF_TEXT_(SETUP, CTC_DBF_INFO,
aa3f2cb6 1626 "setup(%s) OK : r/w = %s/%s, protocol : %d", dev->name,
3c09e264
UB
1627 priv->channel[CTCM_READ]->id,
1628 priv->channel[CTCM_WRITE]->id, priv->protocol);
293d984f
PT
1629
1630 return 0;
a1c1f5ea
EL
1631out_unregister:
1632 unregister_netdev(dev);
aa3f2cb6
PT
1633out_dev:
1634 ctcm_free_netdevice(dev);
a1c1f5ea 1635out_ccw2:
293d984f 1636 ccw_device_set_offline(cgdev->cdev[1]);
a1c1f5ea 1637out_ccw1:
293d984f 1638 ccw_device_set_offline(cgdev->cdev[0]);
a1c1f5ea 1639out_remove_channel2:
3c09e264 1640 readc = channel_get(type, read_id, CTCM_READ);
a1c1f5ea
EL
1641 channel_remove(readc);
1642out_remove_channel1:
3c09e264 1643 writec = channel_get(type, write_id, CTCM_WRITE);
a1c1f5ea
EL
1644 channel_remove(writec);
1645out_err_result:
1646 return result;
293d984f
PT
1647}
1648
1649/**
1650 * Shutdown an interface.
1651 *
1652 * cgdev Device to be shut down.
1653 *
1654 * returns 0 on success, !0 on failure.
1655 */
1656static int ctcm_shutdown_device(struct ccwgroup_device *cgdev)
1657{
1658 struct ctcm_priv *priv;
1659 struct net_device *dev;
1660
1661 priv = dev_get_drvdata(&cgdev->dev);
1662 if (!priv)
1663 return -ENODEV;
1664
3c09e264
UB
1665 if (priv->channel[CTCM_READ]) {
1666 dev = priv->channel[CTCM_READ]->netdev;
293d984f
PT
1667 CTCM_DBF_DEV(SETUP, dev, "");
1668 /* Close the device */
1669 ctcm_close(dev);
1670 dev->flags &= ~IFF_RUNNING;
1671 ctcm_remove_attributes(&cgdev->dev);
3c09e264 1672 channel_free(priv->channel[CTCM_READ]);
293d984f
PT
1673 } else
1674 dev = NULL;
1675
3c09e264
UB
1676 if (priv->channel[CTCM_WRITE])
1677 channel_free(priv->channel[CTCM_WRITE]);
293d984f
PT
1678
1679 if (dev) {
aa3f2cb6 1680 unregister_netdev(dev);
293d984f
PT
1681 ctcm_free_netdevice(dev);
1682 }
1683
1684 if (priv->fsm)
1685 kfree_fsm(priv->fsm);
1686
1687 ccw_device_set_offline(cgdev->cdev[1]);
1688 ccw_device_set_offline(cgdev->cdev[0]);
1689
3c09e264
UB
1690 if (priv->channel[CTCM_READ])
1691 channel_remove(priv->channel[CTCM_READ]);
1692 if (priv->channel[CTCM_WRITE])
1693 channel_remove(priv->channel[CTCM_WRITE]);
1694 priv->channel[CTCM_READ] = priv->channel[CTCM_WRITE] = NULL;
293d984f
PT
1695
1696 return 0;
1697
1698}
1699
1700
1701static void ctcm_remove_device(struct ccwgroup_device *cgdev)
1702{
aa3f2cb6 1703 struct ctcm_priv *priv = dev_get_drvdata(&cgdev->dev);
293d984f 1704
aa3f2cb6
PT
1705 BUG_ON(priv == NULL);
1706
1707 CTCM_DBF_TEXT_(SETUP, CTC_DBF_INFO,
8ac6d452
UB
1708 "removing device %p, proto : %d",
1709 cgdev, priv->protocol);
293d984f 1710
293d984f
PT
1711 if (cgdev->state == CCWGROUP_ONLINE)
1712 ctcm_shutdown_device(cgdev);
1713 ctcm_remove_files(&cgdev->dev);
1714 dev_set_drvdata(&cgdev->dev, NULL);
1715 kfree(priv);
1716 put_device(&cgdev->dev);
1717}
1718
b8a2d42a
FB
1719static int ctcm_pm_suspend(struct ccwgroup_device *gdev)
1720{
1721 struct ctcm_priv *priv = dev_get_drvdata(&gdev->dev);
1722
1723 if (gdev->state == CCWGROUP_OFFLINE)
1724 return 0;
3c09e264
UB
1725 netif_device_detach(priv->channel[CTCM_READ]->netdev);
1726 ctcm_close(priv->channel[CTCM_READ]->netdev);
1e1815be
FB
1727 if (!wait_event_timeout(priv->fsm->wait_q,
1728 fsm_getstate(priv->fsm) == DEV_STATE_STOPPED, CTCM_TIME_5_SEC)) {
3c09e264 1729 netif_device_attach(priv->channel[CTCM_READ]->netdev);
1e1815be
FB
1730 return -EBUSY;
1731 }
b8a2d42a
FB
1732 ccw_device_set_offline(gdev->cdev[1]);
1733 ccw_device_set_offline(gdev->cdev[0]);
1734 return 0;
1735}
1736
1737static int ctcm_pm_resume(struct ccwgroup_device *gdev)
1738{
1739 struct ctcm_priv *priv = dev_get_drvdata(&gdev->dev);
1740 int rc;
1741
1742 if (gdev->state == CCWGROUP_OFFLINE)
1743 return 0;
1744 rc = ccw_device_set_online(gdev->cdev[1]);
1745 if (rc)
1746 goto err_out;
1747 rc = ccw_device_set_online(gdev->cdev[0]);
1748 if (rc)
1749 goto err_out;
3c09e264 1750 ctcm_open(priv->channel[CTCM_READ]->netdev);
b8a2d42a 1751err_out:
3c09e264 1752 netif_device_attach(priv->channel[CTCM_READ]->netdev);
b8a2d42a
FB
1753 return rc;
1754}
1755
0ca8cc6f
UB
1756static struct ccw_device_id ctcm_ids[] = {
1757 {CCW_DEVICE(0x3088, 0x08), .driver_info = ctcm_channel_type_parallel},
1758 {CCW_DEVICE(0x3088, 0x1e), .driver_info = ctcm_channel_type_ficon},
1759 {CCW_DEVICE(0x3088, 0x1f), .driver_info = ctcm_channel_type_escon},
1760 {},
1761};
1762MODULE_DEVICE_TABLE(ccw, ctcm_ids);
1763
1764static struct ccw_driver ctcm_ccw_driver = {
1765 .owner = THIS_MODULE,
1766 .name = "ctcm",
1767 .ids = ctcm_ids,
1768 .probe = ccwgroup_probe_ccwdev,
1769 .remove = ccwgroup_remove_ccwdev,
1770};
1771
293d984f
PT
1772static struct ccwgroup_driver ctcm_group_driver = {
1773 .owner = THIS_MODULE,
1774 .name = CTC_DRIVER_NAME,
1775 .max_slaves = 2,
1776 .driver_id = 0xC3E3C3D4, /* CTCM */
1777 .probe = ctcm_probe_device,
1778 .remove = ctcm_remove_device,
1779 .set_online = ctcm_new_device,
1780 .set_offline = ctcm_shutdown_device,
b8a2d42a
FB
1781 .freeze = ctcm_pm_suspend,
1782 .thaw = ctcm_pm_resume,
1783 .restore = ctcm_pm_resume,
293d984f
PT
1784};
1785
0ca8cc6f
UB
1786static ssize_t
1787ctcm_driver_group_store(struct device_driver *ddrv, const char *buf,
1788 size_t count)
1789{
1790 int err;
1791
1792 err = ccwgroup_create_from_string(ctcm_root_dev,
1793 ctcm_group_driver.driver_id,
1794 &ctcm_ccw_driver, 2, buf);
1795 return err ? err : count;
1796}
1797
1798static DRIVER_ATTR(group, 0200, NULL, ctcm_driver_group_store);
1799
1800static struct attribute *ctcm_group_attrs[] = {
1801 &driver_attr_group.attr,
1802 NULL,
1803};
1804
1805static struct attribute_group ctcm_group_attr_group = {
1806 .attrs = ctcm_group_attrs,
1807};
1808
302689ac 1809static const struct attribute_group *ctcm_group_attr_groups[] = {
0ca8cc6f
UB
1810 &ctcm_group_attr_group,
1811 NULL,
1812};
293d984f
PT
1813
1814/*
1815 * Module related routines
1816 */
1817
1818/*
1819 * Prepare to be unloaded. Free IRQ's and release all resources.
1820 * This is called just before this module is unloaded. It is
1821 * not called, if the usage count is !0, so we don't need to check
1822 * for that.
1823 */
1824static void __exit ctcm_exit(void)
1825{
0ca8cc6f
UB
1826 driver_remove_file(&ctcm_group_driver.driver, &driver_attr_group);
1827 ccwgroup_driver_unregister(&ctcm_group_driver);
1828 ccw_driver_unregister(&ctcm_ccw_driver);
1829 root_device_unregister(ctcm_root_dev);
293d984f 1830 ctcm_unregister_dbf_views();
2a7c6f2c 1831 pr_info("CTCM driver unloaded\n");
293d984f
PT
1832}
1833
1834/*
1835 * Print Banner.
1836 */
1837static void print_banner(void)
1838{
2a7c6f2c 1839 pr_info("CTCM driver initialized\n");
293d984f
PT
1840}
1841
1842/**
1843 * Initialize module.
1844 * This is called just after the module is loaded.
1845 *
1846 * returns 0 on success, !0 on error.
1847 */
1848static int __init ctcm_init(void)
1849{
1850 int ret;
1851
1852 channels = NULL;
1853
1854 ret = ctcm_register_dbf_views();
0ca8cc6f
UB
1855 if (ret)
1856 goto out_err;
1857 ctcm_root_dev = root_device_register("ctcm");
1858 ret = IS_ERR(ctcm_root_dev) ? PTR_ERR(ctcm_root_dev) : 0;
1859 if (ret)
1860 goto register_err;
1861 ret = ccw_driver_register(&ctcm_ccw_driver);
1862 if (ret)
1863 goto ccw_err;
1864 ctcm_group_driver.driver.groups = ctcm_group_attr_groups;
1865 ret = ccwgroup_driver_register(&ctcm_group_driver);
1866 if (ret)
1867 goto ccwgroup_err;
293d984f 1868 print_banner();
0ca8cc6f
UB
1869 return 0;
1870
1871ccwgroup_err:
1872 ccw_driver_unregister(&ctcm_ccw_driver);
1873ccw_err:
1874 root_device_unregister(ctcm_root_dev);
1875register_err:
1876 ctcm_unregister_dbf_views();
1877out_err:
1878 pr_err("%s / Initializing the ctcm device driver failed, ret = %d\n",
1879 __func__, ret);
293d984f
PT
1880 return ret;
1881}
1882
1883module_init(ctcm_init);
1884module_exit(ctcm_exit);
1885
1886MODULE_AUTHOR("Peter Tiedemann <ptiedem@de.ibm.com>");
1887MODULE_DESCRIPTION("Network driver for S/390 CTC + CTCMPC (SNA)");
1888MODULE_LICENSE("GPL");
1889