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