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