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