ctcm_main.c 44.8 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2
/*
3
 * Copyright IBM Corp. 2001, 2009
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * Author(s):
 *	Original CTC driver(s):
 *		Fritz Elfert (felfert@millenux.com)
 *		Dieter Wellerdiek (wel@de.ibm.com)
 *		Martin Schwidefsky (schwidefsky@de.ibm.com)
 *		Denis Joseph Barrow (barrow_dj@yahoo.com)
 *		Jochen Roehrig (roehrig@de.ibm.com)
 *		Cornelia Huck <cornelia.huck@de.ibm.com>
 *	MPC additions:
 *		Belinda Thompson (belindat@us.ibm.com)
 *		Andy Richter (richtera@us.ibm.com)
 *	Revived by:
 *		Peter Tiedemann (ptiedem@de.ibm.com)
 */

#undef DEBUG
#undef DEBUGDATA
#undef DEBUGCCW

23 24 25
#define KMSG_COMPONENT "ctcm"
#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/interrupt.h>
#include <linux/timer.h>
#include <linux/bitops.h>

#include <linux/signal.h>
#include <linux/string.h>

#include <linux/ip.h>
#include <linux/if_arp.h>
#include <linux/tcp.h>
#include <linux/skbuff.h>
#include <linux/ctype.h>
#include <net/dst.h>

#include <linux/io.h>
#include <asm/ccwdev.h>
#include <asm/ccwgroup.h>
#include <linux/uaccess.h>

#include <asm/idals.h>

#include "ctcm_fsms.h"
#include "ctcm_main.h"

/* Some common global variables */

58
/*
59 60 61 62
 * The root device for ctcm group devices
 */
static struct device *ctcm_root_dev;

63 64 65 66 67
/*
 * Linked list of all detected channels.
 */
struct channel *channels;

68
/*
69 70 71 72 73 74 75 76 77
 * Unpack a just received skb and hand it over to
 * upper layers.
 *
 *  ch		The channel where this skb has been received.
 *  pskb	The received skb.
 */
void ctcm_unpack_skb(struct channel *ch, struct sk_buff *pskb)
{
	struct net_device *dev = ch->netdev;
78
	struct ctcm_priv *priv = dev->ml_priv;
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
	__u16 len = *((__u16 *) pskb->data);

	skb_put(pskb, 2 + LL_HEADER_LENGTH);
	skb_pull(pskb, 2);
	pskb->dev = dev;
	pskb->ip_summed = CHECKSUM_UNNECESSARY;
	while (len > 0) {
		struct sk_buff *skb;
		int skblen;
		struct ll_header *header = (struct ll_header *)pskb->data;

		skb_pull(pskb, LL_HEADER_LENGTH);
		if ((ch->protocol == CTCM_PROTO_S390) &&
		    (header->type != ETH_P_IP)) {
			if (!(ch->logflags & LOG_FLAG_ILLEGALPKT)) {
P
Peter Tiedemann 已提交
94
				ch->logflags |= LOG_FLAG_ILLEGALPKT;
95 96 97 98 99 100
				/*
				 * Check packet type only if we stick strictly
				 * to S/390's protocol of OS390. This only
				 * supports IP. Otherwise allow any packet
				 * type.
				 */
P
Peter Tiedemann 已提交
101 102 103 104
				CTCM_DBF_TEXT_(ERROR, CTC_DBF_ERROR,
					"%s(%s): Illegal packet type 0x%04x"
					" - dropping",
					CTCM_FUNTAIL, dev->name, header->type);
105 106 107 108 109
			}
			priv->stats.rx_dropped++;
			priv->stats.rx_frame_errors++;
			return;
		}
110
		pskb->protocol = cpu_to_be16(header->type);
111 112
		if ((header->length <= LL_HEADER_LENGTH) ||
		    (len <= LL_HEADER_LENGTH)) {
113
			if (!(ch->logflags & LOG_FLAG_ILLEGALSIZE)) {
P
Peter Tiedemann 已提交
114 115 116 117 118
				CTCM_DBF_TEXT_(ERROR, CTC_DBF_ERROR,
					"%s(%s): Illegal packet size %d(%d,%d)"
					"- dropping",
					CTCM_FUNTAIL, dev->name,
					header->length, dev->mtu, len);
119 120 121 122 123 124 125 126 127 128
				ch->logflags |= LOG_FLAG_ILLEGALSIZE;
			}

			priv->stats.rx_dropped++;
			priv->stats.rx_length_errors++;
			return;
		}
		header->length -= LL_HEADER_LENGTH;
		len -= LL_HEADER_LENGTH;
		if ((header->length > skb_tailroom(pskb)) ||
129
		    (header->length > len)) {
130
			if (!(ch->logflags & LOG_FLAG_OVERRUN)) {
P
Peter Tiedemann 已提交
131 132 133 134
				CTCM_DBF_TEXT_(ERROR, CTC_DBF_ERROR,
					"%s(%s): Packet size %d (overrun)"
					" - dropping", CTCM_FUNTAIL,
						dev->name, header->length);
135 136 137 138 139 140 141 142 143 144 145 146 147
				ch->logflags |= LOG_FLAG_OVERRUN;
			}

			priv->stats.rx_dropped++;
			priv->stats.rx_length_errors++;
			return;
		}
		skb_put(pskb, header->length);
		skb_reset_mac_header(pskb);
		len -= header->length;
		skb = dev_alloc_skb(pskb->len);
		if (!skb) {
			if (!(ch->logflags & LOG_FLAG_NOMEM)) {
P
Peter Tiedemann 已提交
148 149 150
				CTCM_DBF_TEXT_(ERROR, CTC_DBF_ERROR,
					"%s(%s): MEMORY allocation error",
						CTCM_FUNTAIL, dev->name);
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
				ch->logflags |= LOG_FLAG_NOMEM;
			}
			priv->stats.rx_dropped++;
			return;
		}
		skb_copy_from_linear_data(pskb, skb_put(skb, pskb->len),
					  pskb->len);
		skb_reset_mac_header(skb);
		skb->dev = pskb->dev;
		skb->protocol = pskb->protocol;
		pskb->ip_summed = CHECKSUM_UNNECESSARY;
		skblen = skb->len;
		/*
		 * reset logflags
		 */
		ch->logflags = 0;
		priv->stats.rx_packets++;
		priv->stats.rx_bytes += skblen;
		netif_rx_ni(skb);
		if (len > 0) {
			skb_pull(pskb, header->length);
			if (skb_tailroom(pskb) < LL_HEADER_LENGTH) {
173 174 175
				CTCM_DBF_DEV_NAME(TRACE, dev,
					"Overrun in ctcm_unpack_skb");
				ch->logflags |= LOG_FLAG_OVERRUN;
176 177 178 179 180 181 182
				return;
			}
			skb_put(pskb, LL_HEADER_LENGTH);
		}
	}
}

183
/*
184 185 186 187 188 189
 * Release a specific channel in the channel list.
 *
 *  ch		Pointer to channel struct to be released.
 */
static void channel_free(struct channel *ch)
{
P
Peter Tiedemann 已提交
190
	CTCM_DBF_TEXT_(SETUP, CTC_DBF_INFO, "%s(%s)", CTCM_FUNTAIL, ch->id);
191 192 193 194
	ch->flags &= ~CHANNEL_FLAGS_INUSE;
	fsm_newstate(ch->fsm, CTC_STATE_IDLE);
}

195
/*
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
 * Remove a specific channel in the channel list.
 *
 *  ch		Pointer to channel struct to be released.
 */
static void channel_remove(struct channel *ch)
{
	struct channel **c = &channels;
	char chid[CTCM_ID_SIZE+1];
	int ok = 0;

	if (ch == NULL)
		return;
	else
		strncpy(chid, ch->id, CTCM_ID_SIZE);

	channel_free(ch);
	while (*c) {
		if (*c == ch) {
			*c = ch->next;
			fsm_deltimer(&ch->timer);
			if (IS_MPC(ch))
				fsm_deltimer(&ch->sweep_timer);

			kfree_fsm(ch->fsm);
			clear_normalized_cda(&ch->ccw[4]);
			if (ch->trans_skb != NULL) {
				clear_normalized_cda(&ch->ccw[1]);
				dev_kfree_skb_any(ch->trans_skb);
			}
			if (IS_MPC(ch)) {
				tasklet_kill(&ch->ch_tasklet);
				tasklet_kill(&ch->ch_disc_tasklet);
				kfree(ch->discontact_th);
			}
			kfree(ch->ccw);
			kfree(ch->irb);
			kfree(ch);
			ok = 1;
			break;
		}
		c = &((*c)->next);
	}

	CTCM_DBF_TEXT_(SETUP, CTC_DBF_INFO, "%s(%s) %s", CTCM_FUNTAIL,
			chid, ok ? "OK" : "failed");
}

243
/*
244 245 246 247 248 249 250 251
 * Get a specific channel from the channel list.
 *
 *  type	Type of channel we are interested in.
 *  id		Id of channel we are interested in.
 *  direction	Direction we want to use this channel for.
 *
 * returns Pointer to a channel or NULL if no matching channel available.
 */
252
static struct channel *channel_get(enum ctcm_channel_types type,
253 254 255 256 257 258 259
					char *id, int direction)
{
	struct channel *ch = channels;

	while (ch && (strncmp(ch->id, id, CTCM_ID_SIZE) || (ch->type != type)))
		ch = ch->next;
	if (!ch) {
P
Peter Tiedemann 已提交
260 261
		CTCM_DBF_TEXT_(ERROR, CTC_DBF_ERROR,
				"%s(%d, %s, %d) not found in channel list\n",
262 263 264 265 266 267 268
				CTCM_FUNTAIL, type, id, direction);
	} else {
		if (ch->flags & CHANNEL_FLAGS_INUSE)
			ch = NULL;
		else {
			ch->flags |= CHANNEL_FLAGS_INUSE;
			ch->flags &= ~CHANNEL_FLAGS_RWMASK;
269
			ch->flags |= (direction == CTCM_WRITE)
270 271 272 273 274 275 276 277 278 279 280 281
			    ? CHANNEL_FLAGS_WRITE : CHANNEL_FLAGS_READ;
			fsm_newstate(ch->fsm, CTC_STATE_STOPPED);
		}
	}
	return ch;
}

static long ctcm_check_irb_error(struct ccw_device *cdev, struct irb *irb)
{
	if (!IS_ERR(irb))
		return 0;

P
Peter Tiedemann 已提交
282 283
	CTCM_DBF_TEXT_(ERROR, CTC_DBF_WARN,
			"irb error %ld on device %s\n",
284
				PTR_ERR(irb), dev_name(&cdev->dev));
285 286 287

	switch (PTR_ERR(irb)) {
	case -EIO:
288 289
		dev_err(&cdev->dev,
			"An I/O-error occurred on the CTCM device\n");
290 291
		break;
	case -ETIMEDOUT:
292 293
		dev_err(&cdev->dev,
			"An adapter hardware operation timed out\n");
294 295
		break;
	default:
296 297
		dev_err(&cdev->dev,
			"An error occurred on the adapter hardware\n");
298 299 300 301 302
	}
	return PTR_ERR(irb);
}


303
/*
304 305 306 307 308
 * Check sense of a unit check.
 *
 *  ch		The channel, the sense code belongs to.
 *  sense	The sense code to inspect.
 */
J
Julian Wiedmann 已提交
309
static void ccw_unit_check(struct channel *ch, __u8 sense)
310
{
P
Peter Tiedemann 已提交
311 312 313 314
	CTCM_DBF_TEXT_(TRACE, CTC_DBF_DEBUG,
			"%s(%s): %02x",
				CTCM_FUNTAIL, ch->id, sense);

315 316
	if (sense & SNS0_INTERVENTION_REQ) {
		if (sense & 0x01) {
P
Peter Tiedemann 已提交
317
			if (ch->sense_rc != 0x01) {
318 319 320
				pr_notice(
					"%s: The communication peer has "
					"disconnected\n", ch->id);
P
Peter Tiedemann 已提交
321 322
				ch->sense_rc = 0x01;
			}
323 324
			fsm_event(ch->fsm, CTC_EVENT_UC_RCRESET, ch);
		} else {
P
Peter Tiedemann 已提交
325
			if (ch->sense_rc != SNS0_INTERVENTION_REQ) {
326 327 328
				pr_notice(
					"%s: The remote operating system is "
					"not available\n", ch->id);
P
Peter Tiedemann 已提交
329 330
				ch->sense_rc = SNS0_INTERVENTION_REQ;
			}
331 332 333 334
			fsm_event(ch->fsm, CTC_EVENT_UC_RSRESET, ch);
		}
	} else if (sense & SNS0_EQUIPMENT_CHECK) {
		if (sense & SNS0_BUS_OUT_CHECK) {
P
Peter Tiedemann 已提交
335 336 337 338 339 340
			if (ch->sense_rc != SNS0_BUS_OUT_CHECK) {
				CTCM_DBF_TEXT_(TRACE, CTC_DBF_WARN,
					"%s(%s): remote HW error %02x",
						CTCM_FUNTAIL, ch->id, sense);
				ch->sense_rc = SNS0_BUS_OUT_CHECK;
			}
341 342
			fsm_event(ch->fsm, CTC_EVENT_UC_HWFAIL, ch);
		} else {
P
Peter Tiedemann 已提交
343 344 345 346 347 348
			if (ch->sense_rc != SNS0_EQUIPMENT_CHECK) {
				CTCM_DBF_TEXT_(TRACE, CTC_DBF_WARN,
					"%s(%s): remote read parity error %02x",
						CTCM_FUNTAIL, ch->id, sense);
				ch->sense_rc = SNS0_EQUIPMENT_CHECK;
			}
349 350 351
			fsm_event(ch->fsm, CTC_EVENT_UC_RXPARITY, ch);
		}
	} else if (sense & SNS0_BUS_OUT_CHECK) {
P
Peter Tiedemann 已提交
352 353 354 355 356 357 358
		if (ch->sense_rc != SNS0_BUS_OUT_CHECK) {
			CTCM_DBF_TEXT_(TRACE, CTC_DBF_WARN,
				"%s(%s): BUS OUT error %02x",
					CTCM_FUNTAIL, ch->id, sense);
			ch->sense_rc = SNS0_BUS_OUT_CHECK;
		}
		if (sense & 0x04)	/* data-streaming timeout */
359
			fsm_event(ch->fsm, CTC_EVENT_UC_TXTIMEOUT, ch);
P
Peter Tiedemann 已提交
360
		else			/* Data-transfer parity error */
361 362
			fsm_event(ch->fsm, CTC_EVENT_UC_TXPARITY, ch);
	} else if (sense & SNS0_CMD_REJECT) {
P
Peter Tiedemann 已提交
363 364 365 366 367 368
		if (ch->sense_rc != SNS0_CMD_REJECT) {
			CTCM_DBF_TEXT_(TRACE, CTC_DBF_WARN,
				"%s(%s): Command rejected",
						CTCM_FUNTAIL, ch->id);
			ch->sense_rc = SNS0_CMD_REJECT;
		}
369
	} else if (sense == 0) {
P
Peter Tiedemann 已提交
370 371 372
		CTCM_DBF_TEXT_(TRACE, CTC_DBF_WARN,
			"%s(%s): Unit check ZERO",
					CTCM_FUNTAIL, ch->id);
373 374
		fsm_event(ch->fsm, CTC_EVENT_UC_ZERO, ch);
	} else {
P
Peter Tiedemann 已提交
375 376 377
		CTCM_DBF_TEXT_(TRACE, CTC_DBF_WARN,
			"%s(%s): Unit check code %02x unknown",
					CTCM_FUNTAIL, ch->id, sense);
378 379 380 381 382 383 384 385 386
		fsm_event(ch->fsm, CTC_EVENT_UC_UNKNOWN, ch);
	}
}

int ctcm_ch_alloc_buffer(struct channel *ch)
{
	clear_normalized_cda(&ch->ccw[1]);
	ch->trans_skb = __dev_alloc_skb(ch->max_bufsize, GFP_ATOMIC | GFP_DMA);
	if (ch->trans_skb == NULL) {
P
Peter Tiedemann 已提交
387 388 389
		CTCM_DBF_TEXT_(ERROR, CTC_DBF_ERROR,
			"%s(%s): %s trans_skb allocation error",
			CTCM_FUNTAIL, ch->id,
390 391
			(CHANNEL_DIRECTION(ch->flags) == CTCM_READ) ?
				"RX" : "TX");
392 393 394 395 396 397 398
		return -ENOMEM;
	}

	ch->ccw[1].count = ch->max_bufsize;
	if (set_normalized_cda(&ch->ccw[1], ch->trans_skb->data)) {
		dev_kfree_skb(ch->trans_skb);
		ch->trans_skb = NULL;
P
Peter Tiedemann 已提交
399 400 401
		CTCM_DBF_TEXT_(ERROR, CTC_DBF_ERROR,
			"%s(%s): %s set norm_cda failed",
			CTCM_FUNTAIL, ch->id,
402 403
			(CHANNEL_DIRECTION(ch->flags) == CTCM_READ) ?
				"RX" : "TX");
404 405 406 407 408 409 410 411 412 413 414 415 416
		return -ENOMEM;
	}

	ch->ccw[1].count = 0;
	ch->trans_skb_data = ch->trans_skb->data;
	ch->flags &= ~CHANNEL_FLAGS_BUFSIZE_CHANGED;
	return 0;
}

/*
 * Interface API for upper network layers
 */

417
/*
418 419 420 421 422 423 424 425 426
 * Open an interface.
 * Called from generic network layer when ifconfig up is run.
 *
 *  dev		Pointer to interface struct.
 *
 * returns 0 on success, -ERRNO on failure. (Never fails.)
 */
int ctcm_open(struct net_device *dev)
{
427
	struct ctcm_priv *priv = dev->ml_priv;
428 429 430 431 432 433 434

	CTCMY_DBF_DEV_NAME(SETUP, dev, "");
	if (!IS_MPC(priv))
		fsm_event(priv->fsm,	DEV_EVENT_START, dev);
	return 0;
}

435
/*
436 437 438 439 440 441 442 443 444
 * Close an interface.
 * Called from generic network layer when ifconfig down is run.
 *
 *  dev		Pointer to interface struct.
 *
 * returns 0 on success, -ERRNO on failure. (Never fails.)
 */
int ctcm_close(struct net_device *dev)
{
445
	struct ctcm_priv *priv = dev->ml_priv;
446 447 448 449 450 451 452 453

	CTCMY_DBF_DEV_NAME(SETUP, dev, "");
	if (!IS_MPC(priv))
		fsm_event(priv->fsm, DEV_EVENT_STOP, dev);
	return 0;
}


454
/*
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
 * Transmit a packet.
 * This is a helper function for ctcm_tx().
 *
 *  ch		Channel to be used for sending.
 *  skb		Pointer to struct sk_buff of packet to send.
 *            The linklevel header has already been set up
 *            by ctcm_tx().
 *
 * returns 0 on success, -ERRNO on failure. (Never fails.)
 */
static int ctcm_transmit_skb(struct channel *ch, struct sk_buff *skb)
{
	unsigned long saveflags;
	struct ll_header header;
	int rc = 0;
	__u16 block_len;
	int ccw_idx;
	struct sk_buff *nskb;
	unsigned long hi;

	/* we need to acquire the lock for testing the state
	 * otherwise we can have an IRQ changing the state to
	 * TXIDLE after the test but before acquiring the lock.
	 */
	spin_lock_irqsave(&ch->collect_lock, saveflags);
	if (fsm_getstate(ch->fsm) != CTC_STATE_TXIDLE) {
		int l = skb->len + LL_HEADER_LENGTH;

		if (ch->collect_len + l > ch->max_bufsize - 2) {
			spin_unlock_irqrestore(&ch->collect_lock, saveflags);
			return -EBUSY;
		} else {
487
			refcount_inc(&skb->users);
488
			header.length = l;
489
			header.type = be16_to_cpu(skb->protocol);
490 491 492 493 494 495 496 497 498 499 500 501 502 503
			header.unused = 0;
			memcpy(skb_push(skb, LL_HEADER_LENGTH), &header,
			       LL_HEADER_LENGTH);
			skb_queue_tail(&ch->collect_queue, skb);
			ch->collect_len += l;
		}
		spin_unlock_irqrestore(&ch->collect_lock, saveflags);
				goto done;
	}
	spin_unlock_irqrestore(&ch->collect_lock, saveflags);
	/*
	 * Protect skb against beeing free'd by upper
	 * layers.
	 */
504
	refcount_inc(&skb->users);
505 506
	ch->prof.txlen += skb->len;
	header.length = skb->len + LL_HEADER_LENGTH;
507
	header.type = be16_to_cpu(skb->protocol);
508 509 510 511 512 513 514 515 516 517 518 519 520
	header.unused = 0;
	memcpy(skb_push(skb, LL_HEADER_LENGTH), &header, LL_HEADER_LENGTH);
	block_len = skb->len + 2;
	*((__u16 *)skb_push(skb, 2)) = block_len;

	/*
	 * IDAL support in CTCM is broken, so we have to
	 * care about skb's above 2G ourselves.
	 */
	hi = ((unsigned long)skb_tail_pointer(skb) + LL_HEADER_LENGTH) >> 31;
	if (hi) {
		nskb = alloc_skb(skb->len, GFP_ATOMIC | GFP_DMA);
		if (!nskb) {
521
			refcount_dec(&skb->users);
522 523 524 525
			skb_pull(skb, LL_HEADER_LENGTH + 2);
			ctcm_clear_busy(ch->netdev);
			return -ENOMEM;
		} else {
526
			skb_put_data(nskb, skb->data, skb->len);
527 528
			refcount_inc(&nskb->users);
			refcount_dec(&skb->users);
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
			dev_kfree_skb_irq(skb);
			skb = nskb;
		}
	}

	ch->ccw[4].count = block_len;
	if (set_normalized_cda(&ch->ccw[4], skb->data)) {
		/*
		 * idal allocation failed, try via copying to
		 * trans_skb. trans_skb usually has a pre-allocated
		 * idal.
		 */
		if (ctcm_checkalloc_buffer(ch)) {
			/*
			 * Remove our header. It gets added
			 * again on retransmit.
			 */
546
			refcount_dec(&skb->users);
547 548
			skb_pull(skb, LL_HEADER_LENGTH + 2);
			ctcm_clear_busy(ch->netdev);
P
Peter Tiedemann 已提交
549
			return -ENOMEM;
550 551 552 553 554 555 556
		}

		skb_reset_tail_pointer(ch->trans_skb);
		ch->trans_skb->len = 0;
		ch->ccw[1].count = skb->len;
		skb_copy_from_linear_data(skb,
				skb_put(ch->trans_skb, skb->len), skb->len);
557
		refcount_dec(&skb->users);
558 559 560 561 562 563
		dev_kfree_skb_irq(skb);
		ccw_idx = 0;
	} else {
		skb_queue_tail(&ch->io_queue, skb);
		ccw_idx = 3;
	}
564 565 566
	if (do_debug_ccw)
		ctcmpc_dumpit((char *)&ch->ccw[ccw_idx],
					sizeof(struct ccw1) * 3);
567 568 569 570
	ch->retry = 0;
	fsm_newstate(ch->fsm, CTC_STATE_TX);
	fsm_addtimer(&ch->timer, CTCM_TIME_5_SEC, CTC_EVENT_TIMER, ch);
	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], 0, 0xff, 0);
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
	spin_unlock_irqrestore(get_ccwdev_lock(ch->cdev), saveflags);
	if (ccw_idx == 3)
		ch->prof.doios_single++;
	if (rc != 0) {
		fsm_deltimer(&ch->timer);
		ctcm_ccw_check_rc(ch, rc, "single skb TX");
		if (ccw_idx == 3)
			skb_dequeue_tail(&ch->io_queue);
		/*
		 * Remove our header. It gets added
		 * again on retransmit.
		 */
		skb_pull(skb, LL_HEADER_LENGTH + 2);
	} else if (ccw_idx == 0) {
		struct net_device *dev = ch->netdev;
588
		struct ctcm_priv *priv = dev->ml_priv;
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
		priv->stats.tx_packets++;
		priv->stats.tx_bytes += skb->len - LL_HEADER_LENGTH;
	}
done:
	ctcm_clear_busy(ch->netdev);
	return rc;
}

static void ctcmpc_send_sweep_req(struct channel *rch)
{
	struct net_device *dev = rch->netdev;
	struct ctcm_priv *priv;
	struct mpc_group *grp;
	struct th_sweep *header;
	struct sk_buff *sweep_skb;
	struct channel *ch;
P
Peter Tiedemann 已提交
605
	/* int rc = 0; */
606

607
	priv = dev->ml_priv;
608
	grp = priv->mpcg;
609
	ch = priv->channel[CTCM_WRITE];
610 611 612 613 614

	/* sweep processing is not complete until response and request */
	/* has completed for all read channels in group		       */
	if (grp->in_sweep == 0) {
		grp->in_sweep = 1;
615 616
		grp->sweep_rsp_pend_num = grp->active_channels[CTCM_READ];
		grp->sweep_req_pend_num = grp->active_channels[CTCM_READ];
617 618 619 620 621
	}

	sweep_skb = __dev_alloc_skb(MPC_BUFSIZE_DEFAULT, GFP_ATOMIC|GFP_DMA);

	if (sweep_skb == NULL)	{
P
Peter Tiedemann 已提交
622 623
		/* rc = -ENOMEM; */
				goto nomem;
624 625
	}

626
	header = skb_put_zero(sweep_skb, TH_SWEEP_LENGTH);
627 628 629
	header->th.th_ch_flag	= TH_SWEEP_REQ;  /* 0x0f */
	header->sw.th_last_seq	= ch->th_seq_num;

630
	netif_trans_update(dev);
631 632 633 634 635 636
	skb_queue_tail(&ch->sweep_queue, sweep_skb);

	fsm_addtimer(&ch->sweep_timer, 100, CTC_EVENT_RSWEEP_TIMER, ch);

	return;

P
Peter Tiedemann 已提交
637 638 639 640
nomem:
	grp->in_sweep = 0;
	ctcm_clear_busy(dev);
	fsm_event(grp->fsm, MPCG_EVENT_INOP, dev);
641 642 643 644 645 646 647 648 649 650 651

	return;
}

/*
 * MPC mode version of transmit_skb
 */
static int ctcmpc_transmit_skb(struct channel *ch, struct sk_buff *skb)
{
	struct pdu *p_header;
	struct net_device *dev = ch->netdev;
652
	struct ctcm_priv *priv = dev->ml_priv;
653 654 655 656 657 658 659 660
	struct mpc_group *grp = priv->mpcg;
	struct th_header *header;
	struct sk_buff *nskb;
	int rc = 0;
	int ccw_idx;
	unsigned long hi;
	unsigned long saveflags = 0;	/* avoids compiler warning */

P
Peter Tiedemann 已提交
661 662 663
	CTCM_PR_DEBUG("Enter %s: %s, cp=%i ch=0x%p id=%s state=%s\n",
			__func__, dev->name, smp_processor_id(), ch,
					ch->id, fsm_getstate_str(ch->fsm));
664 665 666

	if ((fsm_getstate(ch->fsm) != CTC_STATE_TXIDLE) || grp->in_sweep) {
		spin_lock_irqsave(&ch->collect_lock, saveflags);
667
		refcount_inc(&skb->users);
668

669 670
		p_header = skb_push(skb, PDU_HEADER_LENGTH);
		p_header->pdu_offset = skb->len - PDU_HEADER_LENGTH;
671
		p_header->pdu_proto = 0x01;
672
		if (be16_to_cpu(skb->protocol) == ETH_P_SNAP) {
673
			p_header->pdu_flag = PDU_FIRST | PDU_CNTL;
674
		} else {
675
			p_header->pdu_flag = PDU_FIRST;
676 677 678
		}
		p_header->pdu_seq = 0;

P
Peter Tiedemann 已提交
679 680 681 682
		CTCM_PR_DEBUG("%s(%s): Put on collect_q - skb len: %04x \n"
				"pdu header and data for up to 32 bytes:\n",
				__func__, dev->name, skb->len);
		CTCM_D3_DUMP((char *)skb->data, min_t(int, 32, skb->len));
683 684 685 686 687 688 689 690 691 692 693 694

		skb_queue_tail(&ch->collect_queue, skb);
		ch->collect_len += skb->len;

		spin_unlock_irqrestore(&ch->collect_lock, saveflags);
			goto done;
	}

	/*
	 * Protect skb against beeing free'd by upper
	 * layers.
	 */
695
	refcount_inc(&skb->users);
696 697 698 699 700 701 702 703 704

	/*
	 * IDAL support in CTCM is broken, so we have to
	 * care about skb's above 2G ourselves.
	 */
	hi = ((unsigned long)skb->tail + TH_HEADER_LENGTH) >> 31;
	if (hi) {
		nskb = __dev_alloc_skb(skb->len, GFP_ATOMIC | GFP_DMA);
		if (!nskb) {
P
Peter Tiedemann 已提交
705
			goto nomem_exit;
706
		} else {
707
			skb_put_data(nskb, skb->data, skb->len);
708 709
			refcount_inc(&nskb->users);
			refcount_dec(&skb->users);
710 711 712 713 714
			dev_kfree_skb_irq(skb);
			skb = nskb;
		}
	}

715 716
	p_header = skb_push(skb, PDU_HEADER_LENGTH);
	p_header->pdu_offset = skb->len - PDU_HEADER_LENGTH;
717 718
	p_header->pdu_proto = 0x01;
	p_header->pdu_seq = 0;
719
	if (be16_to_cpu(skb->protocol) == ETH_P_SNAP) {
720
		p_header->pdu_flag = PDU_FIRST | PDU_CNTL;
721
	} else {
722
		p_header->pdu_flag = PDU_FIRST;
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
	}

	if (ch->collect_len > 0) {
		spin_lock_irqsave(&ch->collect_lock, saveflags);
		skb_queue_tail(&ch->collect_queue, skb);
		ch->collect_len += skb->len;
		skb = skb_dequeue(&ch->collect_queue);
		ch->collect_len -= skb->len;
		spin_unlock_irqrestore(&ch->collect_lock, saveflags);
	}

	p_header = (struct pdu *)skb->data;
	p_header->pdu_flag |= PDU_LAST;

	ch->prof.txlen += skb->len - PDU_HEADER_LENGTH;

739 740 741
	/* put the TH on the packet */
	header = skb_push(skb, TH_HEADER_LENGTH);
	memset(header, 0, TH_HEADER_LENGTH);
742 743 744 745 746

	header->th_ch_flag = TH_HAS_PDU;  /* Normal data */
	ch->th_seq_num++;
	header->th_seq_num = ch->th_seq_num;

P
Peter Tiedemann 已提交
747 748
	CTCM_PR_DBGDATA("%s(%s) ToVTAM_th_seq= %08x\n" ,
		       __func__, dev->name, ch->th_seq_num);
749

P
Peter Tiedemann 已提交
750 751 752 753
	CTCM_PR_DBGDATA("%s(%s): skb len: %04x\n - pdu header and data for "
			"up to 32 bytes sent to vtam:\n",
				__func__, dev->name, skb->len);
	CTCM_D3_DUMP((char *)skb->data, min_t(int, 32, skb->len));
754 755 756 757

	ch->ccw[4].count = skb->len;
	if (set_normalized_cda(&ch->ccw[4], skb->data)) {
		/*
P
Peter Tiedemann 已提交
758 759
		 * idal allocation failed, try via copying to trans_skb.
		 * trans_skb usually has a pre-allocated idal.
760 761 762
		 */
		if (ctcm_checkalloc_buffer(ch)) {
			/*
P
Peter Tiedemann 已提交
763 764
			 * Remove our header.
			 * It gets added again on retransmit.
765
			 */
P
Peter Tiedemann 已提交
766
				goto nomem_exit;
767 768 769 770 771
		}

		skb_reset_tail_pointer(ch->trans_skb);
		ch->trans_skb->len = 0;
		ch->ccw[1].count = skb->len;
772
		skb_put_data(ch->trans_skb, skb->data, skb->len);
773
		refcount_dec(&skb->users);
774 775
		dev_kfree_skb_irq(skb);
		ccw_idx = 0;
P
Peter Tiedemann 已提交
776 777 778 779 780
		CTCM_PR_DBGDATA("%s(%s): trans_skb len: %04x\n"
				"up to 32 bytes sent to vtam:\n",
				__func__, dev->name, ch->trans_skb->len);
		CTCM_D3_DUMP((char *)ch->trans_skb->data,
				min_t(int, 32, ch->trans_skb->len));
781 782 783 784 785 786 787 788 789 790 791 792 793
	} else {
		skb_queue_tail(&ch->io_queue, skb);
		ccw_idx = 3;
	}
	ch->retry = 0;
	fsm_newstate(ch->fsm, CTC_STATE_TX);
	fsm_addtimer(&ch->timer, CTCM_TIME_5_SEC, CTC_EVENT_TIMER, ch);

	if (do_debug_ccw)
		ctcmpc_dumpit((char *)&ch->ccw[ccw_idx],
					sizeof(struct ccw1) * 3);

	spin_lock_irqsave(get_ccwdev_lock(ch->cdev), saveflags);
794
	ch->prof.send_stamp = jiffies;
795
	rc = ccw_device_start(ch->cdev, &ch->ccw[ccw_idx], 0, 0xff, 0);
796 797 798 799 800 801 802 803 804 805 806 807
	spin_unlock_irqrestore(get_ccwdev_lock(ch->cdev), saveflags);
	if (ccw_idx == 3)
		ch->prof.doios_single++;
	if (rc != 0) {
		fsm_deltimer(&ch->timer);
		ctcm_ccw_check_rc(ch, rc, "single skb TX");
		if (ccw_idx == 3)
			skb_dequeue_tail(&ch->io_queue);
	} else if (ccw_idx == 0) {
		priv->stats.tx_packets++;
		priv->stats.tx_bytes += skb->len - TH_HEADER_LENGTH;
	}
P
Peter Tiedemann 已提交
808
	if (ch->th_seq_num > 0xf0000000)	/* Chose at random. */
809 810
		ctcmpc_send_sweep_req(ch);

P
Peter Tiedemann 已提交
811 812 813 814 815 816
	goto done;
nomem_exit:
	CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_CRIT,
			"%s(%s): MEMORY allocation ERROR\n",
			CTCM_FUNTAIL, ch->id);
	rc = -ENOMEM;
817
	refcount_dec(&skb->users);
P
Peter Tiedemann 已提交
818 819
	dev_kfree_skb_any(skb);
	fsm_event(priv->mpcg->fsm, MPCG_EVENT_INOP, dev);
820
done:
P
Peter Tiedemann 已提交
821 822
	CTCM_PR_DEBUG("Exit %s(%s)\n", __func__, dev->name);
	return rc;
823 824
}

825
/*
826 827 828 829 830 831 832 833 834 835 836 837 838
 * Start transmission of a packet.
 * Called from generic network device layer.
 *
 *  skb		Pointer to buffer containing the packet.
 *  dev		Pointer to interface struct.
 *
 * returns 0 if packet consumed, !0 if packet rejected.
 *         Note: If we return !0, then the packet is free'd by
 *               the generic network layer.
 */
/* first merge version - leaving both functions separated */
static int ctcm_tx(struct sk_buff *skb, struct net_device *dev)
{
839
	struct ctcm_priv *priv = dev->ml_priv;
840 841

	if (skb == NULL) {
P
Peter Tiedemann 已提交
842 843 844
		CTCM_DBF_TEXT_(ERROR, CTC_DBF_ERROR,
				"%s(%s): NULL sk_buff passed",
					CTCM_FUNTAIL, dev->name);
845
		priv->stats.tx_dropped++;
846
		return NETDEV_TX_OK;
847 848
	}
	if (skb_headroom(skb) < (LL_HEADER_LENGTH + 2)) {
P
Peter Tiedemann 已提交
849 850 851
		CTCM_DBF_TEXT_(ERROR, CTC_DBF_ERROR,
			"%s(%s): Got sk_buff with head room < %ld bytes",
			CTCM_FUNTAIL, dev->name, LL_HEADER_LENGTH + 2);
852 853
		dev_kfree_skb(skb);
		priv->stats.tx_dropped++;
854
		return NETDEV_TX_OK;
855 856 857 858 859 860 861 862 863 864 865 866
	}

	/*
	 * If channels are not running, try to restart them
	 * and throw away packet.
	 */
	if (fsm_getstate(priv->fsm) != DEV_STATE_RUNNING) {
		fsm_event(priv->fsm, DEV_EVENT_START, dev);
		dev_kfree_skb(skb);
		priv->stats.tx_dropped++;
		priv->stats.tx_errors++;
		priv->stats.tx_carrier_errors++;
867
		return NETDEV_TX_OK;
868 869 870
	}

	if (ctcm_test_and_set_busy(dev))
871
		return NETDEV_TX_BUSY;
872

873
	netif_trans_update(dev);
874
	if (ctcm_transmit_skb(priv->channel[CTCM_WRITE], skb) != 0)
875
		return NETDEV_TX_BUSY;
876
	return NETDEV_TX_OK;
877 878 879 880 881 882
}

/* unmerged MPC variant of ctcm_tx */
static int ctcmpc_tx(struct sk_buff *skb, struct net_device *dev)
{
	int len = 0;
883
	struct ctcm_priv *priv = dev->ml_priv;
P
Peter Tiedemann 已提交
884
	struct mpc_group *grp  = priv->mpcg;
885 886 887 888 889 890
	struct sk_buff *newskb = NULL;

	/*
	 * Some sanity checks ...
	 */
	if (skb == NULL) {
P
Peter Tiedemann 已提交
891 892 893
		CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
			"%s(%s): NULL sk_buff passed",
					CTCM_FUNTAIL, dev->name);
894 895 896 897
		priv->stats.tx_dropped++;
					goto done;
	}
	if (skb_headroom(skb) < (TH_HEADER_LENGTH + PDU_HEADER_LENGTH)) {
P
Peter Tiedemann 已提交
898 899 900 901
		CTCM_DBF_TEXT_(MPC_TRACE, CTC_DBF_ERROR,
			"%s(%s): Got sk_buff with head room < %ld bytes",
			CTCM_FUNTAIL, dev->name,
				TH_HEADER_LENGTH + PDU_HEADER_LENGTH);
902

P
Peter Tiedemann 已提交
903
		CTCM_D3_DUMP((char *)skb->data, min_t(int, 32, skb->len));
904 905

		len =  skb->len + TH_HEADER_LENGTH + PDU_HEADER_LENGTH;
906
		newskb = __dev_alloc_skb(len, GFP_ATOMIC | GFP_DMA);
907 908

		if (!newskb) {
P
Peter Tiedemann 已提交
909 910 911
			CTCM_DBF_TEXT_(MPC_TRACE, CTC_DBF_ERROR,
				"%s: %s: __dev_alloc_skb failed",
						__func__, dev->name);
912 913 914 915 916 917 918 919 920 921

			dev_kfree_skb_any(skb);
			priv->stats.tx_dropped++;
			priv->stats.tx_errors++;
			priv->stats.tx_carrier_errors++;
			fsm_event(grp->fsm, MPCG_EVENT_INOP, dev);
					goto done;
		}
		newskb->protocol = skb->protocol;
		skb_reserve(newskb, TH_HEADER_LENGTH + PDU_HEADER_LENGTH);
922
		skb_put_data(newskb, skb->data, skb->len);
923 924 925 926 927 928 929 930 931 932 933 934
		dev_kfree_skb_any(skb);
		skb = newskb;
	}

	/*
	 * If channels are not running,
	 * notify anybody about a link failure and throw
	 * away packet.
	 */
	if ((fsm_getstate(priv->fsm) != DEV_STATE_RUNNING) ||
	   (fsm_getstate(grp->fsm) <  MPCG_STATE_XID2INITW)) {
		dev_kfree_skb_any(skb);
P
Peter Tiedemann 已提交
935 936 937
		CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
			"%s(%s): inactive MPCGROUP - dropped",
					CTCM_FUNTAIL, dev->name);
938 939 940 941 942 943 944
		priv->stats.tx_dropped++;
		priv->stats.tx_errors++;
		priv->stats.tx_carrier_errors++;
					goto done;
	}

	if (ctcm_test_and_set_busy(dev)) {
P
Peter Tiedemann 已提交
945 946 947
		CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
			"%s(%s): device busy - dropped",
					CTCM_FUNTAIL, dev->name);
948 949 950 951 952 953 954 955
		dev_kfree_skb_any(skb);
		priv->stats.tx_dropped++;
		priv->stats.tx_errors++;
		priv->stats.tx_carrier_errors++;
		fsm_event(grp->fsm, MPCG_EVENT_INOP, dev);
					goto done;
	}

956
	netif_trans_update(dev);
957
	if (ctcmpc_transmit_skb(priv->channel[CTCM_WRITE], skb) != 0) {
P
Peter Tiedemann 已提交
958 959 960
		CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
			"%s(%s): device error - dropped",
					CTCM_FUNTAIL, dev->name);
961 962 963 964 965 966 967 968 969 970 971 972 973
		dev_kfree_skb_any(skb);
		priv->stats.tx_dropped++;
		priv->stats.tx_errors++;
		priv->stats.tx_carrier_errors++;
		ctcm_clear_busy(dev);
		fsm_event(grp->fsm, MPCG_EVENT_INOP, dev);
					goto done;
	}
	ctcm_clear_busy(dev);
done:
	if (do_debug)
		MPC_DBF_DEV_NAME(TRACE, dev, "exit");

974
	return NETDEV_TX_OK;	/* handle freeing of skb here */
975 976 977
}


978
/*
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
 * Sets MTU of an interface.
 *
 *  dev		Pointer to interface struct.
 *  new_mtu	The new MTU to use for this interface.
 *
 * returns 0 on success, -EINVAL if MTU is out of valid range.
 *         (valid range is 576 .. 65527). If VM is on the
 *         remote side, maximum MTU is 32760, however this is
 *         not checked here.
 */
static int ctcm_change_mtu(struct net_device *dev, int new_mtu)
{
	struct ctcm_priv *priv;
	int max_bufsize;

994
	priv = dev->ml_priv;
995
	max_bufsize = priv->channel[CTCM_READ]->max_bufsize;
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009

	if (IS_MPC(priv)) {
		if (new_mtu > max_bufsize - TH_HEADER_LENGTH)
			return -EINVAL;
		dev->hard_header_len = TH_HEADER_LENGTH + PDU_HEADER_LENGTH;
	} else {
		if (new_mtu > max_bufsize - LL_HEADER_LENGTH - 2)
			return -EINVAL;
		dev->hard_header_len = LL_HEADER_LENGTH + 2;
	}
	dev->mtu = new_mtu;
	return 0;
}

1010
/*
1011 1012 1013 1014 1015 1016 1017 1018
 * Returns interface statistics of a device.
 *
 *  dev		Pointer to interface struct.
 *
 * returns Pointer to stats struct of this interface.
 */
static struct net_device_stats *ctcm_stats(struct net_device *dev)
{
1019
	return &((struct ctcm_priv *)dev->ml_priv)->stats;
1020 1021 1022 1023 1024 1025 1026
}

static void ctcm_free_netdevice(struct net_device *dev)
{
	struct ctcm_priv *priv;
	struct mpc_group *grp;

P
Peter Tiedemann 已提交
1027 1028
	CTCM_DBF_TEXT_(SETUP, CTC_DBF_INFO,
			"%s(%s)", CTCM_FUNTAIL, dev->name);
1029
	priv = dev->ml_priv;
1030 1031 1032 1033 1034
	if (priv) {
		grp = priv->mpcg;
		if (grp) {
			if (grp->fsm)
				kfree_fsm(grp->fsm);
1035 1036
			dev_kfree_skb(grp->xid_skb);
			dev_kfree_skb(grp->rcvd_xid_skb);
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
			tasklet_kill(&grp->mpc_tasklet2);
			kfree(grp);
			priv->mpcg = NULL;
		}
		if (priv->fsm) {
			kfree_fsm(priv->fsm);
			priv->fsm = NULL;
		}
		kfree(priv->xid);
		priv->xid = NULL;
	/*
	 * Note: kfree(priv); is done in "opposite" function of
	 * allocator function probe_device which is remove_device.
	 */
	}
#ifdef MODULE
	free_netdev(dev);
#endif
}

struct mpc_group *ctcmpc_init_mpc_group(struct ctcm_priv *priv);

F
Frank Blaschka 已提交
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
static const struct net_device_ops ctcm_netdev_ops = {
	.ndo_open		= ctcm_open,
	.ndo_stop		= ctcm_close,
	.ndo_get_stats		= ctcm_stats,
	.ndo_change_mtu	   	= ctcm_change_mtu,
	.ndo_start_xmit		= ctcm_tx,
};

static const struct net_device_ops ctcm_mpc_netdev_ops = {
	.ndo_open		= ctcm_open,
	.ndo_stop		= ctcm_close,
	.ndo_get_stats		= ctcm_stats,
	.ndo_change_mtu	   	= ctcm_change_mtu,
	.ndo_start_xmit		= ctcmpc_tx,
};

1075
static void ctcm_dev_setup(struct net_device *dev)
1076 1077 1078 1079
{
	dev->type = ARPHRD_SLIP;
	dev->tx_queue_len = 100;
	dev->flags = IFF_POINTOPOINT | IFF_NOARP;
1080 1081
	dev->min_mtu = 576;
	dev->max_mtu = 65527;
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
}

/*
 * Initialize everything of the net device except the name and the
 * channel structs.
 */
static struct net_device *ctcm_init_netdevice(struct ctcm_priv *priv)
{
	struct net_device *dev;
	struct mpc_group *grp;
	if (!priv)
		return NULL;

	if (IS_MPC(priv))
1096 1097
		dev = alloc_netdev(0, MPC_DEVICE_GENE, NET_NAME_UNKNOWN,
				   ctcm_dev_setup);
1098
	else
1099 1100
		dev = alloc_netdev(0, CTC_DEVICE_GENE, NET_NAME_UNKNOWN,
				   ctcm_dev_setup);
1101 1102

	if (!dev) {
P
Peter Tiedemann 已提交
1103 1104 1105
		CTCM_DBF_TEXT_(ERROR, CTC_DBF_CRIT,
			"%s: MEMORY allocation ERROR",
			CTCM_FUNTAIL);
1106 1107
		return NULL;
	}
1108
	dev->ml_priv = priv;
1109 1110 1111 1112 1113
	priv->fsm = init_fsm("ctcmdev", dev_state_names, dev_event_names,
				CTCM_NR_DEV_STATES, CTCM_NR_DEV_EVENTS,
				dev_fsm, dev_fsm_len, GFP_KERNEL);
	if (priv->fsm == NULL) {
		CTCMY_DBF_DEV(SETUP, dev, "init_fsm error");
1114
		free_netdev(dev);
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
		return NULL;
	}
	fsm_newstate(priv->fsm, DEV_STATE_STOPPED);
	fsm_settimer(priv->fsm, &priv->restart_timer);

	if (IS_MPC(priv)) {
		/*  MPC Group Initializations  */
		grp = ctcmpc_init_mpc_group(priv);
		if (grp == NULL) {
			MPC_DBF_DEV(SETUP, dev, "init_mpc_group error");
1125
			free_netdev(dev);
1126 1127 1128 1129 1130 1131 1132
			return NULL;
		}
		tasklet_init(&grp->mpc_tasklet2,
				mpc_group_ready, (unsigned long)dev);
		dev->mtu = MPC_BUFSIZE_DEFAULT -
				TH_HEADER_LENGTH - PDU_HEADER_LENGTH;

F
Frank Blaschka 已提交
1133
		dev->netdev_ops = &ctcm_mpc_netdev_ops;
1134 1135 1136 1137
		dev->hard_header_len = TH_HEADER_LENGTH + PDU_HEADER_LENGTH;
		priv->buffer_size = MPC_BUFSIZE_DEFAULT;
	} else {
		dev->mtu = CTCM_BUFSIZE_DEFAULT - LL_HEADER_LENGTH - 2;
F
Frank Blaschka 已提交
1138
		dev->netdev_ops = &ctcm_netdev_ops;
1139 1140 1141 1142
		dev->hard_header_len = LL_HEADER_LENGTH + 2;
	}

	CTCMY_DBF_DEV(SETUP, dev, "finished");
P
Peter Tiedemann 已提交
1143

1144 1145 1146
	return dev;
}

1147
/*
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
 * Main IRQ handler.
 *
 *  cdev	The ccw_device the interrupt is for.
 *  intparm	interruption parameter.
 *  irb		interruption response block.
 */
static void ctcm_irq_handler(struct ccw_device *cdev,
				unsigned long intparm, struct irb *irb)
{
	struct channel		*ch;
	struct net_device	*dev;
	struct ctcm_priv	*priv;
	struct ccwgroup_device	*cgdev;
P
Peter Tiedemann 已提交
1161 1162 1163 1164
	int cstat;
	int dstat;

	CTCM_DBF_TEXT_(TRACE, CTC_DBF_DEBUG,
1165
		"Enter %s(%s)", CTCM_FUNTAIL, dev_name(&cdev->dev));
1166 1167 1168 1169 1170 1171

	if (ctcm_check_irb_error(cdev, irb))
		return;

	cgdev = dev_get_drvdata(&cdev->dev);

P
Peter Tiedemann 已提交
1172 1173 1174
	cstat = irb->scsw.cmd.cstat;
	dstat = irb->scsw.cmd.dstat;

1175 1176
	/* Check for unsolicited interrupts. */
	if (cgdev == NULL) {
1177 1178 1179 1180 1181
		CTCM_DBF_TEXT_(TRACE, CTC_DBF_ERROR,
			"%s(%s) unsolicited irq: c-%02x d-%02x\n",
			CTCM_FUNTAIL, dev_name(&cdev->dev), cstat, dstat);
		dev_warn(&cdev->dev,
			"The adapter received a non-specific IRQ\n");
1182 1183 1184 1185 1186 1187
		return;
	}

	priv = dev_get_drvdata(&cgdev->dev);

	/* Try to extract channel from driver data. */
1188 1189 1190 1191
	if (priv->channel[CTCM_READ]->cdev == cdev)
		ch = priv->channel[CTCM_READ];
	else if (priv->channel[CTCM_WRITE]->cdev == cdev)
		ch = priv->channel[CTCM_WRITE];
1192
	else {
1193 1194 1195 1196 1197
		dev_err(&cdev->dev,
			"%s: Internal error: Can't determine channel for "
			"interrupt device %s\n",
			__func__, dev_name(&cdev->dev));
			/* Explain: inconsistent internal structures */
1198 1199 1200
		return;
	}

P
Peter Tiedemann 已提交
1201
	dev = ch->netdev;
1202
	if (dev == NULL) {
1203 1204 1205 1206
		dev_err(&cdev->dev,
			"%s Internal error: net_device is NULL, ch = 0x%p\n",
			__func__, ch);
			/* Explain: inconsistent internal structures */
1207 1208 1209 1210 1211 1212
		return;
	}

	/* Copy interruption response block. */
	memcpy(ch->irb, irb, sizeof(struct irb));

1213
	/* Issue error message and return on subchannel error code */
1214
	if (irb->scsw.cmd.cstat) {
1215
		fsm_event(ch->fsm, CTC_EVENT_SC_UNKNOWN, ch);
1216 1217 1218 1219 1220
		CTCM_DBF_TEXT_(TRACE, CTC_DBF_WARN,
			"%s(%s): sub-ch check %s: cs=%02x ds=%02x",
				CTCM_FUNTAIL, dev->name, ch->id, cstat, dstat);
		dev_warn(&cdev->dev,
				"A check occurred on the subchannel\n");
1221 1222 1223 1224
		return;
	}

	/* Check the reason-code of a unit check */
1225
	if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) {
P
Peter Tiedemann 已提交
1226 1227
		if ((irb->ecw[0] & ch->sense_rc) == 0)
			/* print it only once */
1228
			CTCM_DBF_TEXT_(TRACE, CTC_DBF_WARN,
P
Peter Tiedemann 已提交
1229 1230
				"%s(%s): sense=%02x, ds=%02x",
				CTCM_FUNTAIL, ch->id, irb->ecw[0], dstat);
1231 1232 1233
		ccw_unit_check(ch, irb->ecw[0]);
		return;
	}
1234 1235
	if (irb->scsw.cmd.dstat & DEV_STAT_BUSY) {
		if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION)
1236 1237 1238 1239 1240
			fsm_event(ch->fsm, CTC_EVENT_ATTNBUSY, ch);
		else
			fsm_event(ch->fsm, CTC_EVENT_BUSY, ch);
		return;
	}
1241
	if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) {
1242 1243 1244
		fsm_event(ch->fsm, CTC_EVENT_ATTN, ch);
		return;
	}
1245 1246 1247
	if ((irb->scsw.cmd.stctl & SCSW_STCTL_SEC_STATUS) ||
	    (irb->scsw.cmd.stctl == SCSW_STCTL_STATUS_PEND) ||
	    (irb->scsw.cmd.stctl ==
1248 1249 1250 1251 1252 1253 1254
	     (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND)))
		fsm_event(ch->fsm, CTC_EVENT_FINSTAT, ch);
	else
		fsm_event(ch->fsm, CTC_EVENT_IRQ, ch);

}

1255 1256 1257 1258 1259
static const struct device_type ctcm_devtype = {
	.name = "ctcm",
	.groups = ctcm_attr_groups,
};

1260
/*
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
 * Add ctcm specific attributes.
 * Add ctcm private data.
 *
 *  cgdev	pointer to ccwgroup_device just added
 *
 * returns 0 on success, !0 on failure.
 */
static int ctcm_probe_device(struct ccwgroup_device *cgdev)
{
	struct ctcm_priv *priv;

P
Peter Tiedemann 已提交
1272 1273 1274
	CTCM_DBF_TEXT_(SETUP, CTC_DBF_INFO,
			"%s %p",
			__func__, cgdev);
1275 1276 1277 1278 1279 1280

	if (!get_device(&cgdev->dev))
		return -ENODEV;

	priv = kzalloc(sizeof(struct ctcm_priv), GFP_KERNEL);
	if (!priv) {
P
Peter Tiedemann 已提交
1281 1282 1283
		CTCM_DBF_TEXT_(ERROR, CTC_DBF_ERROR,
			"%s: memory allocation failure",
			CTCM_FUNTAIL);
1284 1285 1286 1287 1288 1289 1290
		put_device(&cgdev->dev);
		return -ENOMEM;
	}
	priv->buffer_size = CTCM_BUFSIZE_DEFAULT;
	cgdev->cdev[0]->handler = ctcm_irq_handler;
	cgdev->cdev[1]->handler = ctcm_irq_handler;
	dev_set_drvdata(&cgdev->dev, priv);
1291
	cgdev->dev.type = &ctcm_devtype;
1292 1293 1294 1295

	return 0;
}

1296
/*
1297 1298 1299 1300 1301 1302 1303 1304 1305
 * Add a new channel to the list of channels.
 * Keeps the channel list sorted.
 *
 *  cdev	The ccw_device to be added.
 *  type	The type class of the new channel.
 *  priv	Points to the private data of the ccwgroup_device.
 *
 * returns 0 on success, !0 on error.
 */
1306
static int add_channel(struct ccw_device *cdev, enum ctcm_channel_types type,
1307 1308 1309 1310 1311 1312 1313
				struct ctcm_priv *priv)
{
	struct channel **c = &channels;
	struct channel *ch;
	int ccw_num;
	int rc = 0;

P
Peter Tiedemann 已提交
1314 1315
	CTCM_DBF_TEXT_(SETUP, CTC_DBF_INFO,
		"%s(%s), type %d, proto %d",
1316
			__func__, dev_name(&cdev->dev),	type, priv->protocol);
P
Peter Tiedemann 已提交
1317

1318 1319
	ch = kzalloc(sizeof(struct channel), GFP_KERNEL);
	if (ch == NULL)
P
Peter Tiedemann 已提交
1320
		return -ENOMEM;
1321 1322 1323

	ch->protocol = priv->protocol;
	if (IS_MPC(priv)) {
1324
		ch->discontact_th = kzalloc(TH_HEADER_LENGTH, GFP_KERNEL);
1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
		if (ch->discontact_th == NULL)
					goto nomem_return;

		ch->discontact_th->th_blk_flag = TH_DISCONTACT;
		tasklet_init(&ch->ch_disc_tasklet,
			mpc_action_send_discontact, (unsigned long)ch);

		tasklet_init(&ch->ch_tasklet, ctcmpc_bh, (unsigned long)ch);
		ch->max_bufsize = (MPC_BUFSIZE_DEFAULT - 35);
		ccw_num = 17;
	} else
		ccw_num = 8;

K
Kees Cook 已提交
1338
	ch->ccw = kcalloc(ccw_num, sizeof(struct ccw1), GFP_KERNEL | GFP_DMA);
1339 1340 1341 1342
	if (ch->ccw == NULL)
					goto nomem_return;

	ch->cdev = cdev;
1343
	snprintf(ch->id, CTCM_ID_SIZE, "ch-%s", dev_name(&cdev->dev));
1344 1345
	ch->type = type;

1346
	/*
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414
	 * "static" ccws are used in the following way:
	 *
	 * ccw[0..2] (Channel program for generic I/O):
	 *           0: prepare
	 *           1: read or write (depending on direction) with fixed
	 *              buffer (idal allocated once when buffer is allocated)
	 *           2: nop
	 * ccw[3..5] (Channel program for direct write of packets)
	 *           3: prepare
	 *           4: write (idal allocated on every write).
	 *           5: nop
	 * ccw[6..7] (Channel program for initial channel setup):
	 *           6: set extended mode
	 *           7: nop
	 *
	 * ch->ccw[0..5] are initialized in ch_action_start because
	 * the channel's direction is yet unknown here.
	 *
	 * ccws used for xid2 negotiations
	 *  ch-ccw[8-14] need to be used for the XID exchange either
	 *    X side XID2 Processing
	 *       8:  write control
	 *       9:  write th
	 *	     10: write XID
	 *	     11: read th from secondary
	 *	     12: read XID   from secondary
	 *	     13: read 4 byte ID
	 *	     14: nop
	 *    Y side XID Processing
	 *	     8:  sense
	 *       9:  read th
	 *	     10: read XID
	 *	     11: write th
	 *	     12: write XID
	 *	     13: write 4 byte ID
	 *	     14: nop
	 *
	 *  ccws used for double noop due to VM timing issues
	 *  which result in unrecoverable Busy on channel
	 *       15: nop
	 *       16: nop
	 */
	ch->ccw[6].cmd_code	= CCW_CMD_SET_EXTENDED;
	ch->ccw[6].flags	= CCW_FLAG_SLI;

	ch->ccw[7].cmd_code	= CCW_CMD_NOOP;
	ch->ccw[7].flags	= CCW_FLAG_SLI;

	if (IS_MPC(priv)) {
		ch->ccw[15].cmd_code = CCW_CMD_WRITE;
		ch->ccw[15].flags    = CCW_FLAG_SLI | CCW_FLAG_CC;
		ch->ccw[15].count    = TH_HEADER_LENGTH;
		ch->ccw[15].cda      = virt_to_phys(ch->discontact_th);

		ch->ccw[16].cmd_code = CCW_CMD_NOOP;
		ch->ccw[16].flags    = CCW_FLAG_SLI;

		ch->fsm = init_fsm(ch->id, ctc_ch_state_names,
				ctc_ch_event_names, CTC_MPC_NR_STATES,
				CTC_MPC_NR_EVENTS, ctcmpc_ch_fsm,
				mpc_ch_fsm_len, GFP_KERNEL);
	} else {
		ch->fsm = init_fsm(ch->id, ctc_ch_state_names,
				ctc_ch_event_names, CTC_NR_STATES,
				CTC_NR_EVENTS, ch_fsm,
				ch_fsm_len, GFP_KERNEL);
	}
	if (ch->fsm == NULL)
1415
				goto nomem_return;
1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428

	fsm_newstate(ch->fsm, CTC_STATE_IDLE);

	ch->irb = kzalloc(sizeof(struct irb), GFP_KERNEL);
	if (ch->irb == NULL)
				goto nomem_return;

	while (*c && ctcm_less_than((*c)->id, ch->id))
		c = &(*c)->next;

	if (*c && (!strncmp((*c)->id, ch->id, CTCM_ID_SIZE))) {
		CTCM_DBF_TEXT_(SETUP, CTC_DBF_INFO,
				"%s (%s) already in list, using old entry",
P
Peter Tiedemann 已提交
1429
				__func__, (*c)->id);
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451

				goto free_return;
	}

	spin_lock_init(&ch->collect_lock);

	fsm_settimer(ch->fsm, &ch->timer);
	skb_queue_head_init(&ch->io_queue);
	skb_queue_head_init(&ch->collect_queue);

	if (IS_MPC(priv)) {
		fsm_settimer(ch->fsm, &ch->sweep_timer);
		skb_queue_head_init(&ch->sweep_queue);
	}
	ch->next = *c;
	*c = ch;
	return 0;

nomem_return:
	rc = -ENOMEM;

free_return:	/* note that all channel pointers are 0 or valid */
P
Peter Tiedemann 已提交
1452
	kfree(ch->ccw);
1453 1454 1455 1456 1457 1458 1459 1460 1461 1462
	kfree(ch->discontact_th);
	kfree_fsm(ch->fsm);
	kfree(ch->irb);
	kfree(ch);
	return rc;
}

/*
 * Return type of a detected device.
 */
1463
static enum ctcm_channel_types get_channel_type(struct ccw_device_id *id)
1464
{
1465 1466
	enum ctcm_channel_types type;
	type = (enum ctcm_channel_types)id->driver_info;
1467

1468 1469
	if (type == ctcm_channel_type_ficon)
		type = ctcm_channel_type_escon;
1470 1471 1472 1473

	return type;
}

1474
/*
1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486
 *
 * Setup an interface.
 *
 *  cgdev	Device to be setup.
 *
 * returns 0 on success, !0 on failure.
 */
static int ctcm_new_device(struct ccwgroup_device *cgdev)
{
	char read_id[CTCM_ID_SIZE];
	char write_id[CTCM_ID_SIZE];
	int direction;
1487
	enum ctcm_channel_types type;
1488 1489
	struct ctcm_priv *priv;
	struct net_device *dev;
P
Peter Tiedemann 已提交
1490 1491
	struct ccw_device *cdev0;
	struct ccw_device *cdev1;
E
Einar Lueck 已提交
1492 1493
	struct channel *readc;
	struct channel *writec;
1494
	int ret;
E
Einar Lueck 已提交
1495
	int result;
1496 1497

	priv = dev_get_drvdata(&cgdev->dev);
E
Einar Lueck 已提交
1498 1499 1500 1501
	if (!priv) {
		result = -ENODEV;
		goto out_err_result;
	}
1502

P
Peter Tiedemann 已提交
1503 1504 1505 1506
	cdev0 = cgdev->cdev[0];
	cdev1 = cgdev->cdev[1];

	type = get_channel_type(&cdev0->id);
1507

1508 1509
	snprintf(read_id, CTCM_ID_SIZE, "ch-%s", dev_name(&cdev0->dev));
	snprintf(write_id, CTCM_ID_SIZE, "ch-%s", dev_name(&cdev1->dev));
1510

P
Peter Tiedemann 已提交
1511
	ret = add_channel(cdev0, type, priv);
E
Einar Lueck 已提交
1512 1513 1514 1515
	if (ret) {
		result = ret;
		goto out_err_result;
	}
P
Peter Tiedemann 已提交
1516
	ret = add_channel(cdev1, type, priv);
E
Einar Lueck 已提交
1517 1518 1519 1520
	if (ret) {
		result = ret;
		goto out_remove_channel1;
	}
1521

P
Peter Tiedemann 已提交
1522
	ret = ccw_device_set_online(cdev0);
1523
	if (ret != 0) {
P
Peter Tiedemann 已提交
1524 1525 1526
		CTCM_DBF_TEXT_(TRACE, CTC_DBF_NOTICE,
			"%s(%s) set_online rc=%d",
				CTCM_FUNTAIL, read_id, ret);
E
Einar Lueck 已提交
1527 1528
		result = -EIO;
		goto out_remove_channel2;
1529 1530
	}

P
Peter Tiedemann 已提交
1531
	ret = ccw_device_set_online(cdev1);
1532
	if (ret != 0) {
P
Peter Tiedemann 已提交
1533 1534 1535
		CTCM_DBF_TEXT_(TRACE, CTC_DBF_NOTICE,
			"%s(%s) set_online rc=%d",
				CTCM_FUNTAIL, write_id, ret);
E
Einar Lueck 已提交
1536 1537 1538

		result = -EIO;
		goto out_ccw1;
1539 1540 1541
	}

	dev = ctcm_init_netdevice(priv);
E
Einar Lueck 已提交
1542 1543 1544 1545
	if (dev == NULL) {
		result = -ENODEV;
		goto out_ccw2;
	}
1546

1547
	for (direction = CTCM_READ; direction <= CTCM_WRITE; direction++) {
1548
		priv->channel[direction] =
1549 1550
			channel_get(type, direction == CTCM_READ ?
				read_id : write_id, direction);
1551
		if (priv->channel[direction] == NULL) {
1552 1553
			if (direction == CTCM_WRITE)
				channel_free(priv->channel[CTCM_READ]);
1554
			result = -ENODEV;
P
Peter Tiedemann 已提交
1555
			goto out_dev;
1556 1557 1558 1559 1560 1561 1562 1563
		}
		priv->channel[direction]->netdev = dev;
		priv->channel[direction]->protocol = priv->protocol;
		priv->channel[direction]->max_bufsize = priv->buffer_size;
	}
	/* sysfs magic */
	SET_NETDEV_DEV(dev, &cgdev->dev);

E
Einar Lueck 已提交
1564 1565 1566 1567
	if (register_netdev(dev)) {
		result = -ENODEV;
		goto out_dev;
	}
1568 1569 1570

	strlcpy(priv->fsm->name, dev->name, sizeof(priv->fsm->name));

1571 1572
	dev_info(&dev->dev,
		"setup OK : r/w = %s/%s, protocol : %d\n",
1573 1574
			priv->channel[CTCM_READ]->id,
			priv->channel[CTCM_WRITE]->id, priv->protocol);
1575

1576
	CTCM_DBF_TEXT_(SETUP, CTC_DBF_INFO,
P
Peter Tiedemann 已提交
1577
		"setup(%s) OK : r/w = %s/%s, protocol : %d", dev->name,
1578 1579
			priv->channel[CTCM_READ]->id,
			priv->channel[CTCM_WRITE]->id, priv->protocol);
1580 1581

	return 0;
P
Peter Tiedemann 已提交
1582 1583
out_dev:
	ctcm_free_netdevice(dev);
E
Einar Lueck 已提交
1584
out_ccw2:
1585
	ccw_device_set_offline(cgdev->cdev[1]);
E
Einar Lueck 已提交
1586
out_ccw1:
1587
	ccw_device_set_offline(cgdev->cdev[0]);
E
Einar Lueck 已提交
1588
out_remove_channel2:
1589
	readc = channel_get(type, read_id, CTCM_READ);
E
Einar Lueck 已提交
1590 1591
	channel_remove(readc);
out_remove_channel1:
1592
	writec = channel_get(type, write_id, CTCM_WRITE);
E
Einar Lueck 已提交
1593 1594 1595
	channel_remove(writec);
out_err_result:
	return result;
1596 1597
}

1598
/*
1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613
 * Shutdown an interface.
 *
 *  cgdev	Device to be shut down.
 *
 * returns 0 on success, !0 on failure.
 */
static int ctcm_shutdown_device(struct ccwgroup_device *cgdev)
{
	struct ctcm_priv *priv;
	struct net_device *dev;

	priv = dev_get_drvdata(&cgdev->dev);
	if (!priv)
		return -ENODEV;

1614 1615
	if (priv->channel[CTCM_READ]) {
		dev = priv->channel[CTCM_READ]->netdev;
1616 1617 1618 1619
		CTCM_DBF_DEV(SETUP, dev, "");
		/* Close the device */
		ctcm_close(dev);
		dev->flags &= ~IFF_RUNNING;
1620
		channel_free(priv->channel[CTCM_READ]);
1621 1622 1623
	} else
		dev = NULL;

1624 1625
	if (priv->channel[CTCM_WRITE])
		channel_free(priv->channel[CTCM_WRITE]);
1626 1627

	if (dev) {
P
Peter Tiedemann 已提交
1628
		unregister_netdev(dev);
1629 1630 1631 1632 1633 1634 1635 1636
		ctcm_free_netdevice(dev);
	}

	if (priv->fsm)
		kfree_fsm(priv->fsm);

	ccw_device_set_offline(cgdev->cdev[1]);
	ccw_device_set_offline(cgdev->cdev[0]);
1637 1638
	channel_remove(priv->channel[CTCM_READ]);
	channel_remove(priv->channel[CTCM_WRITE]);
1639
	priv->channel[CTCM_READ] = priv->channel[CTCM_WRITE] = NULL;
1640 1641 1642 1643 1644 1645 1646 1647

	return 0;

}


static void ctcm_remove_device(struct ccwgroup_device *cgdev)
{
P
Peter Tiedemann 已提交
1648
	struct ctcm_priv *priv = dev_get_drvdata(&cgdev->dev);
1649

P
Peter Tiedemann 已提交
1650
	CTCM_DBF_TEXT_(SETUP, CTC_DBF_INFO,
1651 1652
			"removing device %p, proto : %d",
			cgdev, priv->protocol);
1653 1654 1655 1656 1657 1658 1659 1660

	if (cgdev->state == CCWGROUP_ONLINE)
		ctcm_shutdown_device(cgdev);
	dev_set_drvdata(&cgdev->dev, NULL);
	kfree(priv);
	put_device(&cgdev->dev);
}

1661 1662 1663 1664 1665 1666 1667 1668 1669
static struct ccw_device_id ctcm_ids[] = {
	{CCW_DEVICE(0x3088, 0x08), .driver_info = ctcm_channel_type_parallel},
	{CCW_DEVICE(0x3088, 0x1e), .driver_info = ctcm_channel_type_ficon},
	{CCW_DEVICE(0x3088, 0x1f), .driver_info = ctcm_channel_type_escon},
	{},
};
MODULE_DEVICE_TABLE(ccw, ctcm_ids);

static struct ccw_driver ctcm_ccw_driver = {
1670 1671 1672 1673
	.driver = {
		.owner	= THIS_MODULE,
		.name	= "ctcm",
	},
1674 1675 1676
	.ids	= ctcm_ids,
	.probe	= ccwgroup_probe_ccwdev,
	.remove	= ccwgroup_remove_ccwdev,
1677
	.int_class = IRQIO_CTC,
1678 1679
};

1680
static struct ccwgroup_driver ctcm_group_driver = {
1681 1682 1683 1684
	.driver = {
		.owner	= THIS_MODULE,
		.name	= CTC_DRIVER_NAME,
	},
1685
	.ccw_driver  = &ctcm_ccw_driver,
1686
	.setup	     = ctcm_probe_device,
1687 1688 1689 1690 1691
	.remove      = ctcm_remove_device,
	.set_online  = ctcm_new_device,
	.set_offline = ctcm_shutdown_device,
};

1692 1693
static ssize_t group_store(struct device_driver *ddrv, const char *buf,
			   size_t count)
1694 1695 1696
{
	int err;

1697
	err = ccwgroup_create_dev(ctcm_root_dev, &ctcm_group_driver, 2, buf);
1698 1699
	return err ? err : count;
}
1700
static DRIVER_ATTR_WO(group);
1701

S
Sebastian Ott 已提交
1702
static struct attribute *ctcm_drv_attrs[] = {
1703 1704 1705
	&driver_attr_group.attr,
	NULL,
};
S
Sebastian Ott 已提交
1706 1707
static struct attribute_group ctcm_drv_attr_group = {
	.attrs = ctcm_drv_attrs,
1708
};
S
Sebastian Ott 已提交
1709 1710
static const struct attribute_group *ctcm_drv_attr_groups[] = {
	&ctcm_drv_attr_group,
1711 1712
	NULL,
};
1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725

/*
 * Module related routines
 */

/*
 * Prepare to be unloaded. Free IRQ's and release all resources.
 * This is called just before this module is unloaded. It is
 * not called, if the usage count is !0, so we don't need to check
 * for that.
 */
static void __exit ctcm_exit(void)
{
1726 1727 1728
	ccwgroup_driver_unregister(&ctcm_group_driver);
	ccw_driver_unregister(&ctcm_ccw_driver);
	root_device_unregister(ctcm_root_dev);
1729
	ctcm_unregister_dbf_views();
1730
	pr_info("CTCM driver unloaded\n");
1731 1732 1733 1734 1735 1736 1737
}

/*
 * Print Banner.
 */
static void print_banner(void)
{
1738
	pr_info("CTCM driver initialized\n");
1739 1740
}

1741
/*
1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753
 * Initialize module.
 * This is called just after the module is loaded.
 *
 * returns 0 on success, !0 on error.
 */
static int __init ctcm_init(void)
{
	int ret;

	channels = NULL;

	ret = ctcm_register_dbf_views();
1754 1755 1756
	if (ret)
		goto out_err;
	ctcm_root_dev = root_device_register("ctcm");
1757
	ret = PTR_ERR_OR_ZERO(ctcm_root_dev);
1758 1759 1760 1761 1762
	if (ret)
		goto register_err;
	ret = ccw_driver_register(&ctcm_ccw_driver);
	if (ret)
		goto ccw_err;
S
Sebastian Ott 已提交
1763
	ctcm_group_driver.driver.groups = ctcm_drv_attr_groups;
1764 1765 1766
	ret = ccwgroup_driver_register(&ctcm_group_driver);
	if (ret)
		goto ccwgroup_err;
1767
	print_banner();
1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778
	return 0;

ccwgroup_err:
	ccw_driver_unregister(&ctcm_ccw_driver);
ccw_err:
	root_device_unregister(ctcm_root_dev);
register_err:
	ctcm_unregister_dbf_views();
out_err:
	pr_err("%s / Initializing the ctcm device driver failed, ret = %d\n",
		__func__, ret);
1779 1780 1781 1782 1783 1784 1785 1786 1787 1788
	return ret;
}

module_init(ctcm_init);
module_exit(ctcm_exit);

MODULE_AUTHOR("Peter Tiedemann <ptiedem@de.ibm.com>");
MODULE_DESCRIPTION("Network driver for S/390 CTC + CTCMPC (SNA)");
MODULE_LICENSE("GPL");