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