ctcm_mpc.c 58.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
/*
 *	drivers/s390/net/ctcm_mpc.c
 *
 *	Copyright IBM Corp. 2004, 2007
 *	Authors:	Belinda Thompson (belindat@us.ibm.com)
 *			Andy Richter (richtera@us.ibm.com)
 *			Peter Tiedemann (ptiedem@de.ibm.com)
 */

/*
	This module exports functions to be used by CCS:
	EXPORT_SYMBOL(ctc_mpc_alloc_channel);
	EXPORT_SYMBOL(ctc_mpc_establish_connectivity);
	EXPORT_SYMBOL(ctc_mpc_dealloc_ch);
	EXPORT_SYMBOL(ctc_mpc_flow_control);
*/

#undef DEBUG
#undef DEBUGDATA
#undef DEBUGCCW

#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/sched.h>

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

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

#include <linux/io.h>		/* instead of <asm/io.h> ok ? */
#include <asm/ccwdev.h>
#include <asm/ccwgroup.h>
#include <linux/bitops.h>	/* instead of <asm/bitops.h> ok ? */
#include <linux/uaccess.h>	/* instead of <asm/uaccess.h> ok ? */
#include <linux/wait.h>
#include <linux/moduleparam.h>
#include <asm/idals.h>

#include "cu3088.h"
#include "ctcm_mpc.h"
#include "ctcm_main.h"
#include "ctcm_fsms.h"

static const struct xid2 init_xid = {
	.xid2_type_id	=	XID_FM2,
	.xid2_len	=	0x45,
	.xid2_adj_id	=	0,
	.xid2_rlen	=	0x31,
	.xid2_resv1	=	0,
	.xid2_flag1	=	0,
	.xid2_fmtt	=	0,
	.xid2_flag4	=	0x80,
	.xid2_resv2	=	0,
	.xid2_tgnum	=	0,
	.xid2_sender_id	=	0,
	.xid2_flag2	=	0,
	.xid2_option	=	XID2_0,
	.xid2_resv3	=	"\x00",
	.xid2_resv4	=	0,
	.xid2_dlc_type	=	XID2_READ_SIDE,
	.xid2_resv5	=	0,
	.xid2_mpc_flag	=	0,
	.xid2_resv6	=	0,
	.xid2_buf_len	=	(MPC_BUFSIZE_DEFAULT - 35),
};

static const struct th_header thnorm = {
	.th_seg		=	0x00,
	.th_ch_flag	=	TH_IS_XID,
	.th_blk_flag	=	TH_DATA_IS_XID,
	.th_is_xid	=	0x01,
	.th_seq_num	=	0x00000000,
};

static const struct th_header thdummy = {
	.th_seg		=	0x00,
	.th_ch_flag	=	0x00,
	.th_blk_flag	=	TH_DATA_IS_XID,
	.th_is_xid	=	0x01,
	.th_seq_num	=	0x00000000,
};

/*
 * Definition of one MPC group
 */

/*
 * Compatibility macros for busy handling
 * of network devices.
 */

static void ctcmpc_unpack_skb(struct channel *ch, struct sk_buff *pskb);

/*
 * MPC Group state machine actions (static prototypes)
 */
static void mpc_action_nop(fsm_instance *fsm, int event, void *arg);
static void mpc_action_go_ready(fsm_instance *fsm, int event, void *arg);
static void mpc_action_go_inop(fsm_instance *fi, int event, void *arg);
static void mpc_action_timeout(fsm_instance *fi, int event, void *arg);
static int  mpc_validate_xid(struct mpcg_info *mpcginfo);
static void mpc_action_yside_xid(fsm_instance *fsm, int event, void *arg);
static void mpc_action_doxid0(fsm_instance *fsm, int event, void *arg);
static void mpc_action_doxid7(fsm_instance *fsm, int event, void *arg);
static void mpc_action_xside_xid(fsm_instance *fsm, int event, void *arg);
static void mpc_action_rcvd_xid0(fsm_instance *fsm, int event, void *arg);
static void mpc_action_rcvd_xid7(fsm_instance *fsm, int event, void *arg);

#ifdef DEBUGDATA
/*-------------------------------------------------------------------*
* Dump buffer format						     *
*								     *
*--------------------------------------------------------------------*/
void ctcmpc_dumpit(char *buf, int len)
{
	__u32	ct, sw, rm, dup;
	char	*ptr, *rptr;
	char	tbuf[82], tdup[82];
	#if (UTS_MACHINE == s390x)
	char	addr[22];
	#else
	char	addr[12];
	#endif
	char	boff[12];
	char	bhex[82], duphex[82];
	char	basc[40];

	sw  = 0;
	rptr = ptr = buf;
	rm  = 16;
	duphex[0] = 0x00;
	dup = 0;

	for (ct = 0; ct < len; ct++, ptr++, rptr++) {
		if (sw == 0) {
			#if (UTS_MACHINE == s390x)
P
Peter Tiedemann 已提交
151
			sprintf(addr, "%16.16lx", (__u64)rptr);
152 153 154 155 156 157 158 159 160 161 162 163 164 165
			#else
			sprintf(addr, "%8.8X", (__u32)rptr);
			#endif

			sprintf(boff, "%4.4X", (__u32)ct);
			bhex[0] = '\0';
			basc[0] = '\0';
		}
		if ((sw == 4) || (sw == 12))
			strcat(bhex, " ");
		if (sw == 8)
			strcat(bhex, "	");

		#if (UTS_MACHINE == s390x)
P
Peter Tiedemann 已提交
166
		sprintf(tbuf, "%2.2lX", (__u64)*ptr);
167 168 169 170 171 172 173 174 175 176 177 178 179 180
		#else
		sprintf(tbuf, "%2.2X", (__u32)*ptr);
		#endif

		tbuf[2] = '\0';
		strcat(bhex, tbuf);
		if ((0 != isprint(*ptr)) && (*ptr >= 0x20))
			basc[sw] = *ptr;
		else
			basc[sw] = '.';

		basc[sw+1] = '\0';
		sw++;
		rm--;
P
Peter Tiedemann 已提交
181 182 183 184 185 186 187 188 189 190
		if (sw != 16)
			continue;
		if ((strcmp(duphex, bhex)) != 0) {
			if (dup != 0) {
				sprintf(tdup,
					"Duplicate as above to %s", addr);
				ctcm_pr_debug("		       --- %s ---\n",
						tdup);
			}
			ctcm_pr_debug("   %s (+%s) : %s  [%s]\n",
191
					addr, boff, bhex, basc);
P
Peter Tiedemann 已提交
192 193 194 195
			dup = 0;
			strcpy(duphex, bhex);
		} else
			dup++;
196

P
Peter Tiedemann 已提交
197 198
		sw = 0;
		rm = 16;
199 200 201 202 203 204 205 206 207 208 209 210 211
	}  /* endfor */

	if (sw != 0) {
		for ( ; rm > 0; rm--, sw++) {
			if ((sw == 4) || (sw == 12))
				strcat(bhex, " ");
			if (sw == 8)
				strcat(bhex, "	");
			strcat(bhex, "	");
			strcat(basc, " ");
		}
		if (dup != 0) {
			sprintf(tdup, "Duplicate as above to %s", addr);
P
Peter Tiedemann 已提交
212
			ctcm_pr_debug("		       --- %s ---\n", tdup);
213
		}
P
Peter Tiedemann 已提交
214 215
		ctcm_pr_debug("   %s (+%s) : %s  [%s]\n",
					addr, boff, bhex, basc);
216 217 218
	} else {
		if (dup >= 1) {
			sprintf(tdup, "Duplicate as above to %s", addr);
P
Peter Tiedemann 已提交
219
			ctcm_pr_debug("		       --- %s ---\n", tdup);
220 221
		}
		if (dup != 0) {
P
Peter Tiedemann 已提交
222
			ctcm_pr_debug("   %s (+%s) : %s  [%s]\n",
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
				addr, boff, bhex, basc);
		}
	}

	return;

}   /*	 end of ctcmpc_dumpit  */
#endif

#ifdef DEBUGDATA
/*
 * Dump header and first 16 bytes of an sk_buff for debugging purposes.
 *
 * skb		The sk_buff to dump.
 * offset	Offset relative to skb-data, where to start the dump.
 */
void ctcmpc_dump_skb(struct sk_buff *skb, int offset)
{
P
Peter Tiedemann 已提交
241
	__u8 *p = skb->data;
242 243 244 245 246 247 248 249 250 251 252
	struct th_header *header;
	struct pdu *pheader;
	int bl = skb->len;
	int i;

	if (p == NULL)
		return;

	p += offset;
	header = (struct th_header *)p;

P
Peter Tiedemann 已提交
253 254
	ctcm_pr_debug("dump:\n");
	ctcm_pr_debug("skb len=%d \n", skb->len);
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
	if (skb->len > 2) {
		switch (header->th_ch_flag) {
		case TH_HAS_PDU:
			break;
		case 0x00:
		case TH_IS_XID:
			if ((header->th_blk_flag == TH_DATA_IS_XID) &&
			   (header->th_is_xid == 0x01))
				goto dumpth;
		case TH_SWEEP_REQ:
				goto dumpth;
		case TH_SWEEP_RESP:
				goto dumpth;
		default:
			break;
		}

		pheader = (struct pdu *)p;
P
Peter Tiedemann 已提交
273 274 275 276 277
		ctcm_pr_debug("pdu->offset: %d hex: %04x\n",
			       pheader->pdu_offset, pheader->pdu_offset);
		ctcm_pr_debug("pdu->flag  : %02x\n", pheader->pdu_flag);
		ctcm_pr_debug("pdu->proto : %02x\n", pheader->pdu_proto);
		ctcm_pr_debug("pdu->seq   : %02x\n", pheader->pdu_seq);
278 279 280
					goto dumpdata;

dumpth:
P
Peter Tiedemann 已提交
281 282 283 284 285 286
		ctcm_pr_debug("th->seg     : %02x\n", header->th_seg);
		ctcm_pr_debug("th->ch      : %02x\n", header->th_ch_flag);
		ctcm_pr_debug("th->blk_flag: %02x\n", header->th_blk_flag);
		ctcm_pr_debug("th->type    : %s\n",
			       (header->th_is_xid) ? "DATA" : "XID");
		ctcm_pr_debug("th->seqnum  : %04x\n", header->th_seq_num);
287 288 289 290 291

	}
dumpdata:
	if (bl > 32)
		bl = 32;
P
Peter Tiedemann 已提交
292
	ctcm_pr_debug("data: ");
293
	for (i = 0; i < bl; i++)
P
Peter Tiedemann 已提交
294 295
		ctcm_pr_debug("%02x%s", *p++, (i % 16) ? " " : "\n");
	ctcm_pr_debug("\n");
296 297 298
}
#endif

P
Peter Tiedemann 已提交
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
static struct net_device *ctcmpc_get_dev(int port_num)
{
	char device[20];
	struct net_device *dev;
	struct ctcm_priv *priv;

	sprintf(device, "%s%i", MPC_DEVICE_NAME, port_num);

	dev = __dev_get_by_name(&init_net, device);

	if (dev == NULL) {
		CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
			"%s: Device not found by name: %s",
					CTCM_FUNTAIL, device);
		return NULL;
	}
	priv = dev->priv;
	if (priv == NULL) {
		CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
			"%s(%s): dev->priv is NULL",
					CTCM_FUNTAIL, device);
		return NULL;
	}
	if (priv->mpcg == NULL) {
		CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
			"%s(%s): priv->mpcg is NULL",
					CTCM_FUNTAIL, device);
		return NULL;
	}
	return dev;
}

331 332 333 334 335 336 337 338 339 340 341 342 343
/*
 * ctc_mpc_alloc_channel
 *	(exported interface)
 *
 * Device Initialization :
 *	ACTPATH  driven IO operations
 */
int ctc_mpc_alloc_channel(int port_num, void (*callback)(int, int))
{
	struct net_device *dev;
	struct mpc_group *grp;
	struct ctcm_priv *priv;

P
Peter Tiedemann 已提交
344 345
	dev = ctcmpc_get_dev(port_num);
	if (dev == NULL)
346 347 348 349 350 351 352 353
		return 1;
	priv = dev->priv;
	grp = priv->mpcg;

	grp->allochanfunc = callback;
	grp->port_num = port_num;
	grp->port_persist = 1;

P
Peter Tiedemann 已提交
354 355 356
	CTCM_DBF_TEXT_(MPC_SETUP, CTC_DBF_INFO,
			"%s(%s): state=%s",
			CTCM_FUNTAIL, dev->name, fsm_getstate_str(grp->fsm));
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415

	switch (fsm_getstate(grp->fsm)) {
	case MPCG_STATE_INOP:
		/* Group is in the process of terminating */
		grp->alloc_called = 1;
		break;
	case MPCG_STATE_RESET:
		/* MPC Group will transition to state		  */
		/* MPCG_STATE_XID2INITW iff the minimum number	  */
		/* of 1 read and 1 write channel have successfully*/
		/* activated					  */
		/*fsm_newstate(grp->fsm, MPCG_STATE_XID2INITW);*/
		if (callback)
			grp->send_qllc_disc = 1;
	case MPCG_STATE_XID0IOWAIT:
		fsm_deltimer(&grp->timer);
		grp->outstanding_xid2 = 0;
		grp->outstanding_xid7 = 0;
		grp->outstanding_xid7_p2 = 0;
		grp->saved_xid2 = NULL;
		if (callback)
			ctcm_open(dev);
		fsm_event(priv->fsm, DEV_EVENT_START, dev);
		break;
	case MPCG_STATE_READY:
		/* XID exchanges completed after PORT was activated */
		/* Link station already active			    */
		/* Maybe timing issue...retry callback		    */
		grp->allocchan_callback_retries++;
		if (grp->allocchan_callback_retries < 4) {
			if (grp->allochanfunc)
				grp->allochanfunc(grp->port_num,
					      grp->group_max_buflen);
		} else {
			/* there are problems...bail out	    */
			/* there may be a state mismatch so restart */
			grp->port_persist = 1;
			fsm_event(grp->fsm, MPCG_EVENT_INOP, dev);
			grp->allocchan_callback_retries = 0;
		}
		break;
	}

	return 0;
}
EXPORT_SYMBOL(ctc_mpc_alloc_channel);

/*
 * ctc_mpc_establish_connectivity
 *	(exported interface)
 */
void ctc_mpc_establish_connectivity(int port_num,
				void (*callback)(int, int, int))
{
	struct net_device *dev;
	struct mpc_group *grp;
	struct ctcm_priv *priv;
	struct channel *rch, *wch;

P
Peter Tiedemann 已提交
416 417
	dev = ctcmpc_get_dev(port_num);
	if (dev == NULL)
418 419
		return;
	priv = dev->priv;
P
Peter Tiedemann 已提交
420
	grp = priv->mpcg;
421 422 423
	rch = priv->channel[READ];
	wch = priv->channel[WRITE];

P
Peter Tiedemann 已提交
424 425 426
	CTCM_DBF_TEXT_(MPC_SETUP, CTC_DBF_INFO,
			"%s(%s): state=%s",
			CTCM_FUNTAIL, dev->name, fsm_getstate_str(grp->fsm));
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453

	grp->estconnfunc = callback;
	grp->port_num = port_num;

	switch (fsm_getstate(grp->fsm)) {
	case MPCG_STATE_READY:
		/* XID exchanges completed after PORT was activated */
		/* Link station already active			    */
		/* Maybe timing issue...retry callback		    */
		fsm_deltimer(&grp->timer);
		grp->estconn_callback_retries++;
		if (grp->estconn_callback_retries < 4) {
			if (grp->estconnfunc) {
				grp->estconnfunc(grp->port_num, 0,
						grp->group_max_buflen);
				grp->estconnfunc = NULL;
			}
		} else {
			/* there are problems...bail out	 */
			fsm_event(grp->fsm, MPCG_EVENT_INOP, dev);
			grp->estconn_callback_retries = 0;
		}
		break;
	case MPCG_STATE_INOP:
	case MPCG_STATE_RESET:
		/* MPC Group is not ready to start XID - min num of */
		/* 1 read and 1 write channel have not been acquired*/
P
Peter Tiedemann 已提交
454 455 456 457

		CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
			"%s(%s): REJECTED - inactive channels",
					CTCM_FUNTAIL, dev->name);
458 459 460 461 462 463 464 465 466
		if (grp->estconnfunc) {
			grp->estconnfunc(grp->port_num, -1, 0);
			grp->estconnfunc = NULL;
		}
		break;
	case MPCG_STATE_XID2INITW:
		/* alloc channel was called but no XID exchange    */
		/* has occurred. initiate xside XID exchange	   */
		/* make sure yside XID0 processing has not started */
P
Peter Tiedemann 已提交
467

468 469
		if ((fsm_getstate(rch->fsm) > CH_XID0_PENDING) ||
			(fsm_getstate(wch->fsm) > CH_XID0_PENDING)) {
P
Peter Tiedemann 已提交
470 471 472
			CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
				"%s(%s): ABORT - PASSIVE XID",
					CTCM_FUNTAIL, dev->name);
473 474 475 476 477 478 479 480 481 482 483 484 485 486
			break;
		}
		grp->send_qllc_disc = 1;
		fsm_newstate(grp->fsm, MPCG_STATE_XID0IOWAIT);
		fsm_deltimer(&grp->timer);
		fsm_addtimer(&grp->timer, MPC_XID_TIMEOUT_VALUE,
						MPCG_EVENT_TIMER, dev);
		grp->outstanding_xid7 = 0;
		grp->outstanding_xid7_p2 = 0;
		grp->saved_xid2 = NULL;
		if ((rch->in_mpcgroup) &&
				(fsm_getstate(rch->fsm) == CH_XID0_PENDING))
			fsm_event(grp->fsm, MPCG_EVENT_XID0DO, rch);
		else {
P
Peter Tiedemann 已提交
487 488 489
			CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
				"%s(%s): RX-%s not ready for ACTIVE XID0",
					CTCM_FUNTAIL, dev->name, rch->id);
490 491 492 493 494 495 496 497 498 499 500
			if (grp->estconnfunc) {
				grp->estconnfunc(grp->port_num, -1, 0);
				grp->estconnfunc = NULL;
			}
			fsm_deltimer(&grp->timer);
				goto done;
		}
		if ((wch->in_mpcgroup) &&
				(fsm_getstate(wch->fsm) == CH_XID0_PENDING))
			fsm_event(grp->fsm, MPCG_EVENT_XID0DO, wch);
		else {
P
Peter Tiedemann 已提交
501 502 503
			CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
				"%s(%s): WX-%s not ready for ACTIVE XID0",
					CTCM_FUNTAIL, dev->name, wch->id);
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
			if (grp->estconnfunc) {
				grp->estconnfunc(grp->port_num, -1, 0);
				grp->estconnfunc = NULL;
			}
			fsm_deltimer(&grp->timer);
				goto done;
			}
		break;
	case MPCG_STATE_XID0IOWAIT:
		/* already in active XID negotiations */
	default:
		break;
	}

done:
P
Peter Tiedemann 已提交
519
	CTCM_PR_DEBUG("Exit %s()\n", __func__);
520 521 522 523 524 525 526 527 528 529 530 531 532 533
	return;
}
EXPORT_SYMBOL(ctc_mpc_establish_connectivity);

/*
 * ctc_mpc_dealloc_ch
 *	(exported interface)
 */
void ctc_mpc_dealloc_ch(int port_num)
{
	struct net_device *dev;
	struct ctcm_priv *priv;
	struct mpc_group *grp;

P
Peter Tiedemann 已提交
534 535 536 537 538
	dev = ctcmpc_get_dev(port_num);
	if (dev == NULL)
		return;
	priv = dev->priv;
	grp = priv->mpcg;
539

P
Peter Tiedemann 已提交
540 541 542
	CTCM_DBF_TEXT_(MPC_SETUP, CTC_DBF_DEBUG,
			"%s: %s: refcount = %d\n",
			CTCM_FUNTAIL, dev->name, atomic_read(&dev->refcnt));
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569

	fsm_deltimer(&priv->restart_timer);
	grp->channels_terminating = 0;
	fsm_deltimer(&grp->timer);
	grp->allochanfunc = NULL;
	grp->estconnfunc = NULL;
	grp->port_persist = 0;
	grp->send_qllc_disc = 0;
	fsm_event(grp->fsm, MPCG_EVENT_INOP, dev);

	ctcm_close(dev);
	return;
}
EXPORT_SYMBOL(ctc_mpc_dealloc_ch);

/*
 * ctc_mpc_flow_control
 *	(exported interface)
 */
void ctc_mpc_flow_control(int port_num, int flowc)
{
	struct ctcm_priv *priv;
	struct mpc_group *grp;
	struct net_device *dev;
	struct channel *rch;
	int mpcg_state;

P
Peter Tiedemann 已提交
570 571
	dev = ctcmpc_get_dev(port_num);
	if (dev == NULL)
572
		return;
P
Peter Tiedemann 已提交
573 574
	priv = dev->priv;
	grp = priv->mpcg;
575

P
Peter Tiedemann 已提交
576 577 578
	CTCM_DBF_TEXT_(MPC_TRACE, CTC_DBF_DEBUG,
			"%s: %s: flowc = %d",
				CTCM_FUNTAIL, dev->name, flowc);
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625

	rch = priv->channel[READ];

	mpcg_state = fsm_getstate(grp->fsm);
	switch (flowc) {
	case 1:
		if (mpcg_state == MPCG_STATE_FLOWC)
			break;
		if (mpcg_state == MPCG_STATE_READY) {
			if (grp->flow_off_called == 1)
				grp->flow_off_called = 0;
			else
				fsm_newstate(grp->fsm, MPCG_STATE_FLOWC);
			break;
		}
		break;
	case 0:
		if (mpcg_state == MPCG_STATE_FLOWC) {
			fsm_newstate(grp->fsm, MPCG_STATE_READY);
			/* ensure any data that has accumulated */
			/* on the io_queue will now be sen t	*/
			tasklet_schedule(&rch->ch_tasklet);
		}
		/* possible race condition			*/
		if (mpcg_state == MPCG_STATE_READY) {
			grp->flow_off_called = 1;
			break;
		}
		break;
	}

}
EXPORT_SYMBOL(ctc_mpc_flow_control);

static int mpc_send_qllc_discontact(struct net_device *);

/*
 * helper function of ctcmpc_unpack_skb
*/
static void mpc_rcvd_sweep_resp(struct mpcg_info *mpcginfo)
{
	struct channel	  *rch = mpcginfo->ch;
	struct net_device *dev = rch->netdev;
	struct ctcm_priv   *priv = dev->priv;
	struct mpc_group  *grp = priv->mpcg;
	struct channel	  *ch = priv->channel[WRITE];

P
Peter Tiedemann 已提交
626 627
	CTCM_PR_DEBUG("%s: ch=0x%p id=%s\n", __func__, ch, ch->id);
	CTCM_D3_DUMP((char *)mpcginfo->sweep, TH_SWEEP_LENGTH);
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659

	grp->sweep_rsp_pend_num--;

	if ((grp->sweep_req_pend_num == 0) &&
			(grp->sweep_rsp_pend_num == 0)) {
		fsm_deltimer(&ch->sweep_timer);
		grp->in_sweep = 0;
		rch->th_seq_num = 0x00;
		ch->th_seq_num = 0x00;
		ctcm_clear_busy_do(dev);
	}

	kfree(mpcginfo);

	return;

}

/*
 * helper function of mpc_rcvd_sweep_req
 * which is a helper of ctcmpc_unpack_skb
 */
static void ctcmpc_send_sweep_resp(struct channel *rch)
{
	struct net_device *dev = rch->netdev;
	struct ctcm_priv *priv = dev->priv;
	struct mpc_group *grp = priv->mpcg;
	int rc = 0;
	struct th_sweep *header;
	struct sk_buff *sweep_skb;
	struct channel *ch  = priv->channel[WRITE];

P
Peter Tiedemann 已提交
660
	CTCM_PR_DEBUG("%s: ch=0x%p id=%s\n", __func__, rch, rch->id);
661

P
Peter Tiedemann 已提交
662
	sweep_skb = __dev_alloc_skb(MPC_BUFSIZE_DEFAULT, GFP_ATOMIC | GFP_DMA);
663
	if (sweep_skb == NULL) {
P
Peter Tiedemann 已提交
664 665 666
		CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
			"%s(%s): sweep_skb allocation ERROR\n",
			CTCM_FUNTAIL, rch->id);
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
		rc = -ENOMEM;
				goto done;
	}

	header = (struct th_sweep *)
			kmalloc(sizeof(struct th_sweep), gfp_type());

	if (!header) {
		dev_kfree_skb_any(sweep_skb);
		rc = -ENOMEM;
				goto done;
	}

	header->th.th_seg	= 0x00 ;
	header->th.th_ch_flag	= TH_SWEEP_RESP;
	header->th.th_blk_flag	= 0x00;
	header->th.th_is_xid	= 0x00;
	header->th.th_seq_num	= 0x00;
	header->sw.th_last_seq	= ch->th_seq_num;

	memcpy(skb_put(sweep_skb, TH_SWEEP_LENGTH), header, TH_SWEEP_LENGTH);

	kfree(header);

	dev->trans_start = jiffies;
	skb_queue_tail(&ch->sweep_queue, sweep_skb);

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

	return;

done:
	if (rc != 0) {
		grp->in_sweep = 0;
		ctcm_clear_busy_do(dev);
		fsm_event(grp->fsm, MPCG_EVENT_INOP, dev);
	}

	return;
}

/*
 * helper function of ctcmpc_unpack_skb
 */
static void mpc_rcvd_sweep_req(struct mpcg_info *mpcginfo)
{
	struct channel	  *rch     = mpcginfo->ch;
	struct net_device *dev     = rch->netdev;
	struct ctcm_priv  *priv = dev->priv;
	struct mpc_group  *grp  = priv->mpcg;
	struct channel	  *ch	   = priv->channel[WRITE];

	if (do_debug)
		CTCM_DBF_TEXT_(MPC_TRACE, CTC_DBF_DEBUG,
P
Peter Tiedemann 已提交
721
			" %s(): ch=0x%p id=%s\n", __func__, ch, ch->id);
722 723 724 725 726 727 728 729

	if (grp->in_sweep == 0) {
		grp->in_sweep = 1;
		ctcm_test_and_set_busy(dev);
		grp->sweep_req_pend_num = grp->active_channels[READ];
		grp->sweep_rsp_pend_num = grp->active_channels[READ];
	}

P
Peter Tiedemann 已提交
730
	CTCM_D3_DUMP((char *)mpcginfo->sweep, TH_SWEEP_LENGTH);
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848

	grp->sweep_req_pend_num--;
	ctcmpc_send_sweep_resp(ch);
	kfree(mpcginfo);
	return;
}

/*
  * MPC Group Station FSM definitions
 */
static const char *mpcg_event_names[] = {
	[MPCG_EVENT_INOP]	= "INOP Condition",
	[MPCG_EVENT_DISCONC]	= "Discontact Received",
	[MPCG_EVENT_XID0DO]	= "Channel Active - Start XID",
	[MPCG_EVENT_XID2]	= "XID2 Received",
	[MPCG_EVENT_XID2DONE]	= "XID0 Complete",
	[MPCG_EVENT_XID7DONE]	= "XID7 Complete",
	[MPCG_EVENT_TIMER]	= "XID Setup Timer",
	[MPCG_EVENT_DOIO]	= "XID DoIO",
};

static const char *mpcg_state_names[] = {
	[MPCG_STATE_RESET]	= "Reset",
	[MPCG_STATE_INOP]	= "INOP",
	[MPCG_STATE_XID2INITW]	= "Passive XID- XID0 Pending Start",
	[MPCG_STATE_XID2INITX]	= "Passive XID- XID0 Pending Complete",
	[MPCG_STATE_XID7INITW]	= "Passive XID- XID7 Pending P1 Start",
	[MPCG_STATE_XID7INITX]	= "Passive XID- XID7 Pending P2 Complete",
	[MPCG_STATE_XID0IOWAIT]	= "Active  XID- XID0 Pending Start",
	[MPCG_STATE_XID0IOWAIX]	= "Active  XID- XID0 Pending Complete",
	[MPCG_STATE_XID7INITI]	= "Active  XID- XID7 Pending Start",
	[MPCG_STATE_XID7INITZ]	= "Active  XID- XID7 Pending Complete ",
	[MPCG_STATE_XID7INITF]	= "XID        - XID7 Complete ",
	[MPCG_STATE_FLOWC]	= "FLOW CONTROL ON",
	[MPCG_STATE_READY]	= "READY",
};

/*
 * The MPC Group Station FSM
 *   22 events
 */
static const fsm_node mpcg_fsm[] = {
	{ MPCG_STATE_RESET,	MPCG_EVENT_INOP,	mpc_action_go_inop    },
	{ MPCG_STATE_INOP,	MPCG_EVENT_INOP,	mpc_action_nop        },
	{ MPCG_STATE_FLOWC,	MPCG_EVENT_INOP,	mpc_action_go_inop    },

	{ MPCG_STATE_READY,	MPCG_EVENT_DISCONC,	mpc_action_discontact },
	{ MPCG_STATE_READY,	MPCG_EVENT_INOP,	mpc_action_go_inop    },

	{ MPCG_STATE_XID2INITW,	MPCG_EVENT_XID0DO,	mpc_action_doxid0     },
	{ MPCG_STATE_XID2INITW,	MPCG_EVENT_XID2,	mpc_action_rcvd_xid0  },
	{ MPCG_STATE_XID2INITW,	MPCG_EVENT_INOP,	mpc_action_go_inop    },
	{ MPCG_STATE_XID2INITW,	MPCG_EVENT_TIMER,	mpc_action_timeout    },
	{ MPCG_STATE_XID2INITW,	MPCG_EVENT_DOIO,	mpc_action_yside_xid  },

	{ MPCG_STATE_XID2INITX,	MPCG_EVENT_XID0DO,	mpc_action_doxid0     },
	{ MPCG_STATE_XID2INITX,	MPCG_EVENT_XID2,	mpc_action_rcvd_xid0  },
	{ MPCG_STATE_XID2INITX,	MPCG_EVENT_INOP,	mpc_action_go_inop    },
	{ MPCG_STATE_XID2INITX,	MPCG_EVENT_TIMER,	mpc_action_timeout    },
	{ MPCG_STATE_XID2INITX,	MPCG_EVENT_DOIO,	mpc_action_yside_xid  },

	{ MPCG_STATE_XID7INITW,	MPCG_EVENT_XID2DONE,	mpc_action_doxid7     },
	{ MPCG_STATE_XID7INITW,	MPCG_EVENT_DISCONC,	mpc_action_discontact },
	{ MPCG_STATE_XID7INITW,	MPCG_EVENT_XID2,	mpc_action_rcvd_xid7  },
	{ MPCG_STATE_XID7INITW,	MPCG_EVENT_INOP,	mpc_action_go_inop    },
	{ MPCG_STATE_XID7INITW,	MPCG_EVENT_TIMER,	mpc_action_timeout    },
	{ MPCG_STATE_XID7INITW,	MPCG_EVENT_XID7DONE,	mpc_action_doxid7     },
	{ MPCG_STATE_XID7INITW,	MPCG_EVENT_DOIO,	mpc_action_yside_xid  },

	{ MPCG_STATE_XID7INITX,	MPCG_EVENT_DISCONC,	mpc_action_discontact },
	{ MPCG_STATE_XID7INITX,	MPCG_EVENT_XID2,	mpc_action_rcvd_xid7  },
	{ MPCG_STATE_XID7INITX,	MPCG_EVENT_INOP,	mpc_action_go_inop    },
	{ MPCG_STATE_XID7INITX,	MPCG_EVENT_XID7DONE,	mpc_action_doxid7     },
	{ MPCG_STATE_XID7INITX,	MPCG_EVENT_TIMER,	mpc_action_timeout    },
	{ MPCG_STATE_XID7INITX,	MPCG_EVENT_DOIO,	mpc_action_yside_xid  },

	{ MPCG_STATE_XID0IOWAIT, MPCG_EVENT_XID0DO,	mpc_action_doxid0     },
	{ MPCG_STATE_XID0IOWAIT, MPCG_EVENT_DISCONC,	mpc_action_discontact },
	{ MPCG_STATE_XID0IOWAIT, MPCG_EVENT_XID2,	mpc_action_rcvd_xid0  },
	{ MPCG_STATE_XID0IOWAIT, MPCG_EVENT_INOP,	mpc_action_go_inop    },
	{ MPCG_STATE_XID0IOWAIT, MPCG_EVENT_TIMER,	mpc_action_timeout    },
	{ MPCG_STATE_XID0IOWAIT, MPCG_EVENT_DOIO,	mpc_action_xside_xid  },

	{ MPCG_STATE_XID0IOWAIX, MPCG_EVENT_XID0DO,	mpc_action_doxid0     },
	{ MPCG_STATE_XID0IOWAIX, MPCG_EVENT_DISCONC,	mpc_action_discontact },
	{ MPCG_STATE_XID0IOWAIX, MPCG_EVENT_XID2,	mpc_action_rcvd_xid0  },
	{ MPCG_STATE_XID0IOWAIX, MPCG_EVENT_INOP,	mpc_action_go_inop    },
	{ MPCG_STATE_XID0IOWAIX, MPCG_EVENT_TIMER,	mpc_action_timeout    },
	{ MPCG_STATE_XID0IOWAIX, MPCG_EVENT_DOIO,	mpc_action_xside_xid  },

	{ MPCG_STATE_XID7INITI,	MPCG_EVENT_XID2DONE,	mpc_action_doxid7     },
	{ MPCG_STATE_XID7INITI,	MPCG_EVENT_XID2,	mpc_action_rcvd_xid7  },
	{ MPCG_STATE_XID7INITI,	MPCG_EVENT_DISCONC,	mpc_action_discontact },
	{ MPCG_STATE_XID7INITI,	MPCG_EVENT_INOP,	mpc_action_go_inop    },
	{ MPCG_STATE_XID7INITI,	MPCG_EVENT_TIMER,	mpc_action_timeout    },
	{ MPCG_STATE_XID7INITI,	MPCG_EVENT_XID7DONE,	mpc_action_doxid7     },
	{ MPCG_STATE_XID7INITI,	MPCG_EVENT_DOIO,	mpc_action_xside_xid  },

	{ MPCG_STATE_XID7INITZ,	MPCG_EVENT_XID2,	mpc_action_rcvd_xid7  },
	{ MPCG_STATE_XID7INITZ,	MPCG_EVENT_XID7DONE,	mpc_action_doxid7     },
	{ MPCG_STATE_XID7INITZ,	MPCG_EVENT_DISCONC,	mpc_action_discontact },
	{ MPCG_STATE_XID7INITZ,	MPCG_EVENT_INOP,	mpc_action_go_inop    },
	{ MPCG_STATE_XID7INITZ,	MPCG_EVENT_TIMER,	mpc_action_timeout    },
	{ MPCG_STATE_XID7INITZ,	MPCG_EVENT_DOIO,	mpc_action_xside_xid  },

	{ MPCG_STATE_XID7INITF,	MPCG_EVENT_INOP,	mpc_action_go_inop    },
	{ MPCG_STATE_XID7INITF,	MPCG_EVENT_XID7DONE,	mpc_action_go_ready   },
};

static int mpcg_fsm_len = ARRAY_SIZE(mpcg_fsm);

/*
 * MPC Group Station FSM action
 * CTCM_PROTO_MPC only
 */
static void mpc_action_go_ready(fsm_instance *fsm, int event, void *arg)
{
	struct net_device *dev = arg;
P
Peter Tiedemann 已提交
849 850
	struct ctcm_priv *priv = dev->priv;
	struct mpc_group *grp = priv->mpcg;
851 852

	if (grp == NULL) {
P
Peter Tiedemann 已提交
853 854 855
		CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
			"%s(%s): No MPC group",
				CTCM_FUNTAIL, dev->name);
856 857 858 859 860 861 862 863 864 865 866 867 868
		return;
	}

	fsm_deltimer(&grp->timer);

	if (grp->saved_xid2->xid2_flag2 == 0x40) {
		priv->xid->xid2_flag2 = 0x00;
		if (grp->estconnfunc) {
			grp->estconnfunc(grp->port_num, 1,
					grp->group_max_buflen);
			grp->estconnfunc = NULL;
		} else if (grp->allochanfunc)
			grp->send_qllc_disc = 1;
P
Peter Tiedemann 已提交
869 870 871 872 873 874

		fsm_event(grp->fsm, MPCG_EVENT_INOP, dev);
		CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
				"%s(%s): fails",
					CTCM_FUNTAIL, dev->name);
		return;
875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
	}

	grp->port_persist = 1;
	grp->out_of_sequence = 0;
	grp->estconn_called = 0;

	tasklet_hi_schedule(&grp->mpc_tasklet2);

	return;
}

/*
 * helper of ctcm_init_netdevice
 * CTCM_PROTO_MPC only
 */
void mpc_group_ready(unsigned long adev)
{
	struct net_device *dev = (struct net_device *)adev;
P
Peter Tiedemann 已提交
893 894
	struct ctcm_priv *priv = dev->priv;
	struct mpc_group *grp = priv->mpcg;
895 896 897
	struct channel *ch = NULL;

	if (grp == NULL) {
P
Peter Tiedemann 已提交
898 899 900
		CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
			"%s(%s): No MPC group",
				CTCM_FUNTAIL, dev->name);
901 902 903
		return;
	}

P
Peter Tiedemann 已提交
904 905 906
	CTCM_DBF_TEXT_(MPC_SETUP, CTC_DBF_NOTICE,
		"%s: %s: GROUP TRANSITIONED TO READY, maxbuf = %d\n",
			CTCM_FUNTAIL, dev->name, grp->group_max_buflen);
907 908 909 910 911 912

	fsm_newstate(grp->fsm, MPCG_STATE_READY);

	/* Put up a read on the channel */
	ch = priv->channel[READ];
	ch->pdu_seq = 0;
P
Peter Tiedemann 已提交
913 914
	CTCM_PR_DBGDATA("ctcmpc: %s() ToDCM_pdu_seq= %08x\n" ,
			__func__, ch->pdu_seq);
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931

	ctcmpc_chx_rxidle(ch->fsm, CTC_EVENT_START, ch);
	/* Put the write channel in idle state */
	ch = priv->channel[WRITE];
	if (ch->collect_len > 0) {
		spin_lock(&ch->collect_lock);
		ctcm_purge_skb_queue(&ch->collect_queue);
		ch->collect_len = 0;
		spin_unlock(&ch->collect_lock);
	}
	ctcm_chx_txidle(ch->fsm, CTC_EVENT_START, ch);
	ctcm_clear_busy(dev);

	if (grp->estconnfunc) {
		grp->estconnfunc(grp->port_num, 0,
				    grp->group_max_buflen);
		grp->estconnfunc = NULL;
P
Peter Tiedemann 已提交
932 933
	} else 	if (grp->allochanfunc)
		grp->allochanfunc(grp->port_num, grp->group_max_buflen);
934 935 936 937 938 939 940 941 942 943 944 945

	grp->send_qllc_disc = 1;
	grp->changed_side = 0;

	return;

}

/*
 * Increment the MPC Group Active Channel Counts
 * helper of dev_action (called from channel fsm)
 */
P
Peter Tiedemann 已提交
946
void mpc_channel_action(struct channel *ch, int direction, int action)
947
{
P
Peter Tiedemann 已提交
948 949 950
	struct net_device  *dev  = ch->netdev;
	struct ctcm_priv   *priv = dev->priv;
	struct mpc_group   *grp  = priv->mpcg;
951 952

	if (grp == NULL) {
P
Peter Tiedemann 已提交
953 954 955 956
		CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
			"%s(%s): No MPC group",
				CTCM_FUNTAIL, dev->name);
		return;
957 958
	}

P
Peter Tiedemann 已提交
959 960 961 962 963 964 965
	CTCM_PR_DEBUG("enter %s: ch=0x%p id=%s\n", __func__, ch, ch->id);

	CTCM_DBF_TEXT_(MPC_TRACE, CTC_DBF_NOTICE,
		"%s: %i / Grp:%s total_channels=%i, active_channels: "
		"read=%i, write=%i\n", __func__, action,
		fsm_getstate_str(grp->fsm), grp->num_channel_paths,
		grp->active_channels[READ], grp->active_channels[WRITE]);
966 967 968 969 970 971 972 973 974 975 976 977 978

	if ((action == MPC_CHANNEL_ADD) && (ch->in_mpcgroup == 0)) {
		grp->num_channel_paths++;
		grp->active_channels[direction]++;
		grp->outstanding_xid2++;
		ch->in_mpcgroup = 1;

		if (ch->xid_skb != NULL)
			dev_kfree_skb_any(ch->xid_skb);

		ch->xid_skb = __dev_alloc_skb(MPC_BUFSIZE_DEFAULT,
					GFP_ATOMIC | GFP_DMA);
		if (ch->xid_skb == NULL) {
P
Peter Tiedemann 已提交
979 980 981
			CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
				"%s(%s): Couldn't alloc ch xid_skb\n",
				CTCM_FUNTAIL, dev->name);
982
			fsm_event(grp->fsm, MPCG_EVENT_INOP, dev);
P
Peter Tiedemann 已提交
983
			return;
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
		}
		ch->xid_skb_data = ch->xid_skb->data;
		ch->xid_th = (struct th_header *)ch->xid_skb->data;
		skb_put(ch->xid_skb, TH_HEADER_LENGTH);
		ch->xid = (struct xid2 *)skb_tail_pointer(ch->xid_skb);
		skb_put(ch->xid_skb, XID2_LENGTH);
		ch->xid_id = skb_tail_pointer(ch->xid_skb);
		ch->xid_skb->data = ch->xid_skb_data;
		skb_reset_tail_pointer(ch->xid_skb);
		ch->xid_skb->len = 0;

		memcpy(skb_put(ch->xid_skb, grp->xid_skb->len),
				grp->xid_skb->data,
				grp->xid_skb->len);

		ch->xid->xid2_dlc_type = ((CHANNEL_DIRECTION(ch->flags) == READ)
				? XID2_READ_SIDE : XID2_WRITE_SIDE);

		if (CHANNEL_DIRECTION(ch->flags) == WRITE)
			ch->xid->xid2_buf_len = 0x00;

		ch->xid_skb->data = ch->xid_skb_data;
		skb_reset_tail_pointer(ch->xid_skb);
		ch->xid_skb->len = 0;

		fsm_newstate(ch->fsm, CH_XID0_PENDING);

		if ((grp->active_channels[READ]  > 0) &&
		    (grp->active_channels[WRITE] > 0) &&
			(fsm_getstate(grp->fsm) < MPCG_STATE_XID2INITW)) {
			fsm_newstate(grp->fsm, MPCG_STATE_XID2INITW);
P
Peter Tiedemann 已提交
1015 1016 1017
			CTCM_DBF_TEXT_(MPC_SETUP, CTC_DBF_NOTICE,
				"%s: %s: MPC GROUP CHANNELS ACTIVE\n",
						__func__, dev->name);
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
		}
	} else if ((action == MPC_CHANNEL_REMOVE) &&
			(ch->in_mpcgroup == 1)) {
		ch->in_mpcgroup = 0;
		grp->num_channel_paths--;
		grp->active_channels[direction]--;

		if (ch->xid_skb != NULL)
			dev_kfree_skb_any(ch->xid_skb);
		ch->xid_skb = NULL;

		if (grp->channels_terminating)
					goto done;

		if (((grp->active_channels[READ] == 0) &&
					(grp->active_channels[WRITE] > 0))
			|| ((grp->active_channels[WRITE] == 0) &&
					(grp->active_channels[READ] > 0)))
			fsm_event(grp->fsm, MPCG_EVENT_INOP, dev);
	}
done:
P
Peter Tiedemann 已提交
1039 1040 1041 1042 1043
	CTCM_DBF_TEXT_(MPC_TRACE, CTC_DBF_DEBUG,
		"exit %s: %i / Grp:%s total_channels=%i, active_channels: "
		"read=%i, write=%i\n", __func__, action,
		fsm_getstate_str(grp->fsm), grp->num_channel_paths,
		grp->active_channels[READ], grp->active_channels[WRITE]);
1044

P
Peter Tiedemann 已提交
1045
	CTCM_PR_DEBUG("exit %s: ch=0x%p id=%s\n", __func__, ch, ch->id);
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
}

/**
 * Unpack a just received skb and hand it over to
 * upper layers.
 * special MPC version of unpack_skb.
 *
 * ch		The channel where this skb has been received.
 * pskb		The received skb.
 */
static void ctcmpc_unpack_skb(struct channel *ch, struct sk_buff *pskb)
{
	struct net_device *dev	= ch->netdev;
	struct ctcm_priv *priv = dev->priv;
	struct mpc_group *grp = priv->mpcg;
	struct pdu *curr_pdu;
	struct mpcg_info *mpcginfo;
	struct th_header *header = NULL;
	struct th_sweep *sweep = NULL;
	int pdu_last_seen = 0;
	__u32 new_len;
	struct sk_buff *skb;
	int skblen;
	int sendrc = 0;

P
Peter Tiedemann 已提交
1071 1072
	CTCM_PR_DEBUG("ctcmpc enter: %s() %s cp:%i ch:%s\n",
			__func__, dev->name, smp_processor_id(), ch->id);
1073 1074 1075 1076 1077 1078 1079 1080

	header = (struct th_header *)pskb->data;
	if ((header->th_seg == 0) &&
		(header->th_ch_flag == 0) &&
		(header->th_blk_flag == 0) &&
		(header->th_seq_num == 0))
		/* nothing for us */	goto done;

P
Peter Tiedemann 已提交
1081 1082 1083
	CTCM_PR_DBGDATA("%s: th_header\n", __func__);
	CTCM_D3_DUMP((char *)header, TH_HEADER_LENGTH);
	CTCM_PR_DBGDATA("%s: pskb len: %04x \n", __func__, pskb->len);
1084 1085 1086 1087 1088 1089

	pskb->dev = dev;
	pskb->ip_summed = CHECKSUM_UNNECESSARY;
	skb_pull(pskb, TH_HEADER_LENGTH);

	if (likely(header->th_ch_flag == TH_HAS_PDU)) {
P
Peter Tiedemann 已提交
1090
		CTCM_PR_DBGDATA("%s: came into th_has_pdu\n", __func__);
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
		if ((fsm_getstate(grp->fsm) == MPCG_STATE_FLOWC) ||
		   ((fsm_getstate(grp->fsm) == MPCG_STATE_READY) &&
		    (header->th_seq_num != ch->th_seq_num + 1) &&
		    (ch->th_seq_num != 0))) {
			/* This is NOT the next segment		*
			 * we are not the correct race winner	*
			 * go away and let someone else win	*
			 * BUT..this only applies if xid negot	*
			 * is done				*
			*/
			grp->out_of_sequence += 1;
			__skb_push(pskb, TH_HEADER_LENGTH);
			skb_queue_tail(&ch->io_queue, pskb);
P
Peter Tiedemann 已提交
1104 1105 1106
			CTCM_PR_DBGDATA("%s: th_seq_num expect:%08x "
					"got:%08x\n", __func__,
				ch->th_seq_num + 1, header->th_seq_num);
1107 1108 1109 1110 1111 1112

			return;
		}
		grp->out_of_sequence = 0;
		ch->th_seq_num = header->th_seq_num;

P
Peter Tiedemann 已提交
1113 1114
		CTCM_PR_DBGDATA("ctcmpc: %s() FromVTAM_th_seq=%08x\n",
					__func__, ch->th_seq_num);
1115 1116 1117 1118 1119 1120

		if (unlikely(fsm_getstate(grp->fsm) != MPCG_STATE_READY))
					goto done;
		pdu_last_seen = 0;
		while ((pskb->len > 0) && !pdu_last_seen) {
			curr_pdu = (struct pdu *)pskb->data;
P
Peter Tiedemann 已提交
1121 1122 1123 1124 1125 1126

			CTCM_PR_DBGDATA("%s: pdu_header\n", __func__);
			CTCM_D3_DUMP((char *)pskb->data, PDU_HEADER_LENGTH);
			CTCM_PR_DBGDATA("%s: pskb len: %04x \n",
						__func__, pskb->len);

1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
			skb_pull(pskb, PDU_HEADER_LENGTH);

			if (curr_pdu->pdu_flag & PDU_LAST)
				pdu_last_seen = 1;
			if (curr_pdu->pdu_flag & PDU_CNTL)
				pskb->protocol = htons(ETH_P_SNAP);
			else
				pskb->protocol = htons(ETH_P_SNA_DIX);

			if ((pskb->len <= 0) || (pskb->len > ch->max_bufsize)) {
P
Peter Tiedemann 已提交
1137 1138 1139 1140 1141
				CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
					"%s(%s): Dropping packet with "
					"illegal siize %d",
					CTCM_FUNTAIL, dev->name, pskb->len);

1142 1143 1144 1145 1146 1147
				priv->stats.rx_dropped++;
				priv->stats.rx_length_errors++;
					goto done;
			}
			skb_reset_mac_header(pskb);
			new_len = curr_pdu->pdu_offset;
P
Peter Tiedemann 已提交
1148 1149
			CTCM_PR_DBGDATA("%s: new_len: %04x \n",
						__func__, new_len);
1150 1151 1152
			if ((new_len == 0) || (new_len > pskb->len)) {
				/* should never happen		    */
				/* pskb len must be hosed...bail out */
P
Peter Tiedemann 已提交
1153 1154 1155 1156 1157
				CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
					"%s(%s): non valid pdu_offset: %04x",
					/* "data may be lost", */
					CTCM_FUNTAIL, dev->name, new_len);
				goto done;
1158 1159 1160 1161
			}
			skb = __dev_alloc_skb(new_len+4, GFP_ATOMIC);

			if (!skb) {
P
Peter Tiedemann 已提交
1162 1163 1164
				CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
					"%s(%s): MEMORY allocation error",
						CTCM_FUNTAIL, dev->name);
1165
				priv->stats.rx_dropped++;
P
Peter Tiedemann 已提交
1166
				fsm_event(grp->fsm, MPCG_EVENT_INOP, dev);
1167 1168
						goto done;
			}
P
Peter Tiedemann 已提交
1169
			memcpy(skb_put(skb, new_len), pskb->data, new_len);
1170 1171 1172 1173 1174 1175 1176 1177 1178

			skb_reset_mac_header(skb);
			skb->dev = pskb->dev;
			skb->protocol = pskb->protocol;
			skb->ip_summed = CHECKSUM_UNNECESSARY;
			*((__u32 *) skb_push(skb, 4)) = ch->pdu_seq;
			ch->pdu_seq++;

			if (do_debug_data) {
P
Peter Tiedemann 已提交
1179 1180 1181 1182 1183 1184 1185
				ctcm_pr_debug("%s: ToDCM_pdu_seq= %08x\n",
						__func__, ch->pdu_seq);
				ctcm_pr_debug("%s: skb:%0lx "
					"skb len: %d \n", __func__,
					(unsigned long)skb, skb->len);
				ctcm_pr_debug("%s: up to 32 bytes "
					"of pdu_data sent\n", __func__);
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
				ctcmpc_dump32((char *)skb->data, skb->len);
			}

			skblen = skb->len;
			sendrc = netif_rx(skb);
			priv->stats.rx_packets++;
			priv->stats.rx_bytes += skblen;
			skb_pull(pskb, new_len); /* point to next PDU */
		}
	} else {
		mpcginfo = (struct mpcg_info *)
				kmalloc(sizeof(struct mpcg_info), gfp_type());
		if (mpcginfo == NULL)
					goto done;

		mpcginfo->ch = ch;
		mpcginfo->th = header;
		mpcginfo->skb = pskb;
P
Peter Tiedemann 已提交
1204 1205
		CTCM_PR_DEBUG("%s: Not PDU - may be control pkt\n",
					__func__);
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
		/*  it's a sweep?   */
		sweep = (struct th_sweep *)pskb->data;
		mpcginfo->sweep = sweep;
		if (header->th_ch_flag == TH_SWEEP_REQ)
			mpc_rcvd_sweep_req(mpcginfo);
		else if (header->th_ch_flag == TH_SWEEP_RESP)
			mpc_rcvd_sweep_resp(mpcginfo);
		else if (header->th_blk_flag == TH_DATA_IS_XID) {
			struct xid2 *thisxid = (struct xid2 *)pskb->data;
			skb_pull(pskb, XID2_LENGTH);
			mpcginfo->xid = thisxid;
			fsm_event(grp->fsm, MPCG_EVENT_XID2, mpcginfo);
		} else if (header->th_blk_flag == TH_DISCONTACT)
			fsm_event(grp->fsm, MPCG_EVENT_DISCONC, mpcginfo);
		else if (header->th_seq_num != 0) {
P
Peter Tiedemann 已提交
1221 1222 1223
			CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
				"%s(%s): control pkt expected\n",
						CTCM_FUNTAIL, dev->name);
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
			priv->stats.rx_dropped++;
			/* mpcginfo only used for non-data transfers */
			kfree(mpcginfo);
			if (do_debug_data)
				ctcmpc_dump_skb(pskb, -8);
		}
	}
done:

	dev_kfree_skb_any(pskb);
	if (sendrc == NET_RX_DROP) {
		printk(KERN_WARNING "%s %s() NETWORK BACKLOG EXCEEDED"
P
Peter Tiedemann 已提交
1236
		       " - PACKET DROPPED\n", dev->name, __func__);
1237 1238 1239
		fsm_event(grp->fsm, MPCG_EVENT_INOP, dev);
	}

P
Peter Tiedemann 已提交
1240 1241
	CTCM_PR_DEBUG("exit %s: %s: ch=0x%p id=%s\n",
			__func__, dev->name, ch, ch->id);
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
}

/**
 * tasklet helper for mpc's skb unpacking.
 *
 * ch		The channel to work on.
 * Allow flow control back pressure to occur here.
 * Throttling back channel can result in excessive
 * channel inactivity and system deact of channel
 */
void ctcmpc_bh(unsigned long thischan)
{
P
Peter Tiedemann 已提交
1254
	struct channel	  *ch	= (struct channel *)thischan;
1255
	struct sk_buff	  *skb;
P
Peter Tiedemann 已提交
1256 1257 1258
	struct net_device *dev	= ch->netdev;
	struct ctcm_priv  *priv	= dev->priv;
	struct mpc_group  *grp	= priv->mpcg;
1259

P
Peter Tiedemann 已提交
1260 1261
	CTCM_PR_DEBUG("%s cp:%i enter:  %s() %s\n",
	       dev->name, smp_processor_id(), __func__, ch->id);
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276
	/* caller has requested driver to throttle back */
	while ((fsm_getstate(grp->fsm) != MPCG_STATE_FLOWC) &&
			(skb = skb_dequeue(&ch->io_queue))) {
		ctcmpc_unpack_skb(ch, skb);
		if (grp->out_of_sequence > 20) {
			/* assume data loss has occurred if */
			/* missing seq_num for extended     */
			/* period of time		    */
			grp->out_of_sequence = 0;
			fsm_event(grp->fsm, MPCG_EVENT_INOP, dev);
			break;
		}
		if (skb == skb_peek(&ch->io_queue))
			break;
	}
P
Peter Tiedemann 已提交
1277 1278
	CTCM_PR_DEBUG("exit %s: %s: ch=0x%p id=%s\n",
			__func__, dev->name, ch, ch->id);
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288
	return;
}

/*
 *  MPC Group Initializations
 */
struct mpc_group *ctcmpc_init_mpc_group(struct ctcm_priv *priv)
{
	struct mpc_group *grp;

P
Peter Tiedemann 已提交
1289 1290
	CTCM_DBF_TEXT_(MPC_SETUP, CTC_DBF_INFO,
			"Enter %s(%p)", CTCM_FUNTAIL, priv);
1291 1292 1293 1294 1295

	grp = kzalloc(sizeof(struct mpc_group), GFP_KERNEL);
	if (grp == NULL)
		return NULL;

P
Peter Tiedemann 已提交
1296 1297 1298
	grp->fsm = init_fsm("mpcg", mpcg_state_names, mpcg_event_names,
			MPCG_NR_STATES, MPCG_NR_EVENTS, mpcg_fsm,
			mpcg_fsm_len, GFP_KERNEL);
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
	if (grp->fsm == NULL) {
		kfree(grp);
		return NULL;
	}

	fsm_newstate(grp->fsm, MPCG_STATE_RESET);
	fsm_settimer(grp->fsm, &grp->timer);

	grp->xid_skb =
		 __dev_alloc_skb(MPC_BUFSIZE_DEFAULT, GFP_ATOMIC | GFP_DMA);
	if (grp->xid_skb == NULL) {
		kfree_fsm(grp->fsm);
		kfree(grp);
		return NULL;
	}
	/*  base xid for all channels in group  */
	grp->xid_skb_data = grp->xid_skb->data;
	grp->xid_th = (struct th_header *)grp->xid_skb->data;
	memcpy(skb_put(grp->xid_skb, TH_HEADER_LENGTH),
			&thnorm, TH_HEADER_LENGTH);

P
Peter Tiedemann 已提交
1320
	grp->xid = (struct xid2 *)skb_tail_pointer(grp->xid_skb);
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 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
	memcpy(skb_put(grp->xid_skb, XID2_LENGTH), &init_xid, XID2_LENGTH);
	grp->xid->xid2_adj_id = jiffies | 0xfff00000;
	grp->xid->xid2_sender_id = jiffies;

	grp->xid_id = skb_tail_pointer(grp->xid_skb);
	memcpy(skb_put(grp->xid_skb, 4), "VTAM", 4);

	grp->rcvd_xid_skb =
		__dev_alloc_skb(MPC_BUFSIZE_DEFAULT, GFP_ATOMIC|GFP_DMA);
	if (grp->rcvd_xid_skb == NULL) {
		kfree_fsm(grp->fsm);
		dev_kfree_skb(grp->xid_skb);
		kfree(grp);
		return NULL;
	}
	grp->rcvd_xid_data = grp->rcvd_xid_skb->data;
	grp->rcvd_xid_th = (struct th_header *)grp->rcvd_xid_skb->data;
	memcpy(skb_put(grp->rcvd_xid_skb, TH_HEADER_LENGTH),
			&thnorm, TH_HEADER_LENGTH);
	grp->saved_xid2 = NULL;
	priv->xid = grp->xid;
	priv->mpcg = grp;
	return grp;
}

/*
 * The MPC Group Station FSM
 */

/*
 * MPC Group Station FSM actions
 * CTCM_PROTO_MPC only
 */

/**
 * NOP action for statemachines
 */
static void mpc_action_nop(fsm_instance *fi, int event, void *arg)
{
}

/*
 * invoked when the device transitions to dev_stopped
 * MPC will stop each individual channel if a single XID failure
 * occurs, or will intitiate all channels be stopped if a GROUP
 * level failure occurs.
 */
static void mpc_action_go_inop(fsm_instance *fi, int event, void *arg)
{
	struct net_device  *dev = arg;
	struct ctcm_priv    *priv;
	struct mpc_group *grp;
	int rc = 0;
	struct channel *wch, *rch;

P
Peter Tiedemann 已提交
1376 1377
	BUG_ON(dev == NULL);
	CTCM_PR_DEBUG("Enter %s: %s\n",	__func__, dev->name);
1378 1379 1380 1381 1382 1383

	priv  = dev->priv;
	grp =  priv->mpcg;
	grp->flow_off_called = 0;
	fsm_deltimer(&grp->timer);
	if (grp->channels_terminating)
P
Peter Tiedemann 已提交
1384
			return;
1385 1386 1387 1388 1389

	grp->channels_terminating = 1;
	grp->saved_state = fsm_getstate(grp->fsm);
	fsm_newstate(grp->fsm, MPCG_STATE_INOP);
	if (grp->saved_state > MPCG_STATE_XID7INITF)
P
Peter Tiedemann 已提交
1390 1391 1392
		CTCM_DBF_TEXT_(MPC_TRACE, CTC_DBF_NOTICE,
			"%s(%s): MPC GROUP INOPERATIVE",
				CTCM_FUNTAIL, dev->name);
1393 1394
	if ((grp->saved_state != MPCG_STATE_RESET) ||
		/* dealloc_channel has been called */
P
Peter Tiedemann 已提交
1395 1396
			((grp->saved_state == MPCG_STATE_RESET) &&
						(grp->port_persist == 0)))
1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445
		fsm_deltimer(&priv->restart_timer);

	wch = priv->channel[WRITE];
	rch = priv->channel[READ];

	switch (grp->saved_state) {
	case MPCG_STATE_RESET:
	case MPCG_STATE_INOP:
	case MPCG_STATE_XID2INITW:
	case MPCG_STATE_XID0IOWAIT:
	case MPCG_STATE_XID2INITX:
	case MPCG_STATE_XID7INITW:
	case MPCG_STATE_XID7INITX:
	case MPCG_STATE_XID0IOWAIX:
	case MPCG_STATE_XID7INITI:
	case MPCG_STATE_XID7INITZ:
	case MPCG_STATE_XID7INITF:
		break;
	case MPCG_STATE_FLOWC:
	case MPCG_STATE_READY:
	default:
		tasklet_hi_schedule(&wch->ch_disc_tasklet);
	}

	grp->xid2_tgnum = 0;
	grp->group_max_buflen = 0;  /*min of all received */
	grp->outstanding_xid2 = 0;
	grp->outstanding_xid7 = 0;
	grp->outstanding_xid7_p2 = 0;
	grp->saved_xid2 = NULL;
	grp->xidnogood = 0;
	grp->changed_side = 0;

	grp->rcvd_xid_skb->data = grp->rcvd_xid_data;
	skb_reset_tail_pointer(grp->rcvd_xid_skb);
	grp->rcvd_xid_skb->len = 0;
	grp->rcvd_xid_th = (struct th_header *)grp->rcvd_xid_skb->data;
	memcpy(skb_put(grp->rcvd_xid_skb, TH_HEADER_LENGTH), &thnorm,
	       TH_HEADER_LENGTH);

	if (grp->send_qllc_disc == 1) {
		grp->send_qllc_disc = 0;
		rc = mpc_send_qllc_discontact(dev);
	}

	/* DO NOT issue DEV_EVENT_STOP directly out of this code */
	/* This can result in INOP of VTAM PU due to halting of  */
	/* outstanding IO which causes a sense to be returned	 */
	/* Only about 3 senses are allowed and then IOS/VTAM will*/
P
Peter Tiedemann 已提交
1446 1447
	/* become unreachable without manual intervention	 */
	if ((grp->port_persist == 1) || (grp->alloc_called)) {
1448 1449
		grp->alloc_called = 0;
		fsm_deltimer(&priv->restart_timer);
P
Peter Tiedemann 已提交
1450
		fsm_addtimer(&priv->restart_timer, 500, DEV_EVENT_RESTART, dev);
1451 1452
		fsm_newstate(grp->fsm, MPCG_STATE_RESET);
		if (grp->saved_state > MPCG_STATE_XID7INITF)
P
Peter Tiedemann 已提交
1453 1454 1455
			CTCM_DBF_TEXT_(MPC_TRACE, CTC_DBF_ALWAYS,
				"%s(%s): MPC GROUP RECOVERY SCHEDULED",
					CTCM_FUNTAIL, dev->name);
1456 1457 1458 1459
	} else {
		fsm_deltimer(&priv->restart_timer);
		fsm_addtimer(&priv->restart_timer, 500, DEV_EVENT_STOP, dev);
		fsm_newstate(grp->fsm, MPCG_STATE_RESET);
P
Peter Tiedemann 已提交
1460 1461 1462
		CTCM_DBF_TEXT_(MPC_TRACE, CTC_DBF_ALWAYS,
			"%s(%s): NO MPC GROUP RECOVERY ATTEMPTED",
						CTCM_FUNTAIL, dev->name);
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482
	}
}

/**
 * Handle mpc group  action timeout.
 * MPC Group Station FSM action
 * CTCM_PROTO_MPC only
 *
 * fi		An instance of an mpc_group fsm.
 * event	The event, just happened.
 * arg		Generic pointer, casted from net_device * upon call.
 */
static void mpc_action_timeout(fsm_instance *fi, int event, void *arg)
{
	struct net_device *dev = arg;
	struct ctcm_priv *priv;
	struct mpc_group *grp;
	struct channel *wch;
	struct channel *rch;

P
Peter Tiedemann 已提交
1483
	BUG_ON(dev == NULL);
1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501

	priv = dev->priv;
	grp = priv->mpcg;
	wch = priv->channel[WRITE];
	rch = priv->channel[READ];

	switch (fsm_getstate(grp->fsm)) {
	case MPCG_STATE_XID2INITW:
		/* Unless there is outstanding IO on the  */
		/* channel just return and wait for ATTN  */
		/* interrupt to begin XID negotiations	  */
		if ((fsm_getstate(rch->fsm) == CH_XID0_PENDING) &&
		   (fsm_getstate(wch->fsm) == CH_XID0_PENDING))
			break;
	default:
		fsm_event(grp->fsm, MPCG_EVENT_INOP, dev);
	}

P
Peter Tiedemann 已提交
1502 1503 1504
	CTCM_DBF_TEXT_(MPC_TRACE, CTC_DBF_DEBUG,
			"%s: dev=%s exit",
			CTCM_FUNTAIL, dev->name);
1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515
	return;
}

/*
 * MPC Group Station FSM action
 * CTCM_PROTO_MPC only
 */
void mpc_action_discontact(fsm_instance *fi, int event, void *arg)
{
	struct mpcg_info   *mpcginfo   = arg;
	struct channel	   *ch	       = mpcginfo->ch;
P
Peter Tiedemann 已提交
1516 1517 1518
	struct net_device  *dev;
	struct ctcm_priv   *priv;
	struct mpc_group   *grp;
1519

P
Peter Tiedemann 已提交
1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532
	if (ch) {
		dev = ch->netdev;
		if (dev) {
			priv = dev->priv;
			if (priv) {
				CTCM_DBF_TEXT_(MPC_TRACE, CTC_DBF_NOTICE,
					"%s: %s: %s\n",
					CTCM_FUNTAIL, dev->name, ch->id);
				grp = priv->mpcg;
				grp->send_qllc_disc = 1;
				fsm_event(grp->fsm, MPCG_EVENT_INOP, dev);
			}
		}
1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544
	}

	return;
}

/*
 * MPC Group Station - not part of FSM
 * CTCM_PROTO_MPC only
 * called from add_channel in ctcm_main.c
 */
void mpc_action_send_discontact(unsigned long thischan)
{
P
Peter Tiedemann 已提交
1545 1546 1547
	int rc;
	struct channel	*ch = (struct channel *)thischan;
	unsigned long	saveflags = 0;
1548 1549 1550 1551 1552 1553 1554

	spin_lock_irqsave(get_ccwdev_lock(ch->cdev), saveflags);
	rc = ccw_device_start(ch->cdev, &ch->ccw[15],
					(unsigned long)ch, 0xff, 0);
	spin_unlock_irqrestore(get_ccwdev_lock(ch->cdev), saveflags);

	if (rc != 0) {
P
Peter Tiedemann 已提交
1555
		ctcm_ccw_check_rc(ch, rc, (char *)__func__);
1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568
	}

	return;
}


/*
 * helper function of mpc FSM
 * CTCM_PROTO_MPC only
 * mpc_action_rcvd_xid7
*/
static int mpc_validate_xid(struct mpcg_info *mpcginfo)
{
P
Peter Tiedemann 已提交
1569 1570
	struct channel	   *ch	 = mpcginfo->ch;
	struct net_device  *dev  = ch->netdev;
1571 1572
	struct ctcm_priv   *priv = dev->priv;
	struct mpc_group   *grp  = priv->mpcg;
P
Peter Tiedemann 已提交
1573 1574 1575 1576 1577
	struct xid2	   *xid  = mpcginfo->xid;
	int	rc	 = 0;
	__u64	our_id   = 0;
	__u64   their_id = 0;
	int	len = TH_HEADER_LENGTH + PDU_HEADER_LENGTH;
1578

P
Peter Tiedemann 已提交
1579
	CTCM_PR_DEBUG("Enter %s: xid=%p\n", __func__, xid);
1580

P
Peter Tiedemann 已提交
1581
	if (xid == NULL) {
1582
		rc = 1;
P
Peter Tiedemann 已提交
1583 1584 1585 1586 1587
		/* XID REJECTED: xid == NULL */
		CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
			"%s(%s): xid = NULL",
				CTCM_FUNTAIL, ch->id);
			goto done;
1588 1589
	}

P
Peter Tiedemann 已提交
1590
	CTCM_D3_DUMP((char *)xid, XID2_LENGTH);
1591 1592 1593 1594

	/*the received direction should be the opposite of ours  */
	if (((CHANNEL_DIRECTION(ch->flags) == READ) ? XID2_WRITE_SIDE :
				XID2_READ_SIDE) != xid->xid2_dlc_type) {
P
Peter Tiedemann 已提交
1595 1596 1597 1598 1599 1600
		rc = 2;
		/* XID REJECTED: r/w channel pairing mismatch */
		CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
			"%s(%s): r/w channel pairing mismatch",
				CTCM_FUNTAIL, ch->id);
			goto done;
1601 1602 1603
	}

	if (xid->xid2_dlc_type == XID2_READ_SIDE) {
P
Peter Tiedemann 已提交
1604 1605
		CTCM_PR_DEBUG("%s: grpmaxbuf:%d xid2buflen:%d\n", __func__,
				grp->group_max_buflen, xid->xid2_buf_len);
1606

P
Peter Tiedemann 已提交
1607 1608
		if (grp->group_max_buflen == 0 || grp->group_max_buflen >
						xid->xid2_buf_len - len)
1609 1610 1611
			grp->group_max_buflen = xid->xid2_buf_len - len;
	}

P
Peter Tiedemann 已提交
1612
	if (grp->saved_xid2 == NULL) {
1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632
		grp->saved_xid2 =
			(struct xid2 *)skb_tail_pointer(grp->rcvd_xid_skb);

		memcpy(skb_put(grp->rcvd_xid_skb,
					XID2_LENGTH), xid, XID2_LENGTH);
		grp->rcvd_xid_skb->data = grp->rcvd_xid_data;

		skb_reset_tail_pointer(grp->rcvd_xid_skb);
		grp->rcvd_xid_skb->len = 0;

		/* convert two 32 bit numbers into 1 64 bit for id compare */
		our_id = (__u64)priv->xid->xid2_adj_id;
		our_id = our_id << 32;
		our_id = our_id + priv->xid->xid2_sender_id;
		their_id = (__u64)xid->xid2_adj_id;
		their_id = their_id << 32;
		their_id = their_id + xid->xid2_sender_id;
		/* lower id assume the xside role */
		if (our_id < their_id) {
			grp->roll = XSIDE;
P
Peter Tiedemann 已提交
1633 1634 1635
			CTCM_DBF_TEXT_(MPC_TRACE, CTC_DBF_NOTICE,
				"%s(%s): WE HAVE LOW ID - TAKE XSIDE",
					CTCM_FUNTAIL, ch->id);
1636 1637
		} else {
			grp->roll = YSIDE;
P
Peter Tiedemann 已提交
1638 1639 1640
			CTCM_DBF_TEXT_(MPC_TRACE, CTC_DBF_NOTICE,
				"%s(%s): WE HAVE HIGH ID - TAKE YSIDE",
					CTCM_FUNTAIL, ch->id);
1641 1642 1643 1644
		}

	} else {
		if (xid->xid2_flag4 != grp->saved_xid2->xid2_flag4) {
P
Peter Tiedemann 已提交
1645 1646 1647 1648 1649
			rc = 3;
			/* XID REJECTED: xid flag byte4 mismatch */
			CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
				"%s(%s): xid flag byte4 mismatch",
					CTCM_FUNTAIL, ch->id);
1650 1651
		}
		if (xid->xid2_flag2 == 0x40) {
P
Peter Tiedemann 已提交
1652 1653 1654 1655 1656
			rc = 4;
			/* XID REJECTED - xid NOGOOD */
			CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
				"%s(%s): xid NOGOOD",
					CTCM_FUNTAIL, ch->id);
1657 1658
		}
		if (xid->xid2_adj_id != grp->saved_xid2->xid2_adj_id) {
P
Peter Tiedemann 已提交
1659 1660 1661 1662 1663
			rc = 5;
			/* XID REJECTED - Adjacent Station ID Mismatch */
			CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
				"%s(%s): Adjacent Station ID Mismatch",
					CTCM_FUNTAIL, ch->id);
1664 1665
		}
		if (xid->xid2_sender_id != grp->saved_xid2->xid2_sender_id) {
P
Peter Tiedemann 已提交
1666 1667 1668 1669 1670
			rc = 6;
			/* XID REJECTED - Sender Address Mismatch */
			CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
				"%s(%s): Sender Address Mismatch",
					CTCM_FUNTAIL, ch->id);
1671 1672 1673
		}
	}

P
Peter Tiedemann 已提交
1674 1675
done:
	if (rc) {
1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693
		ctcm_pr_info("ctcmpc	   :  %s() failed\n", __FUNCTION__);
		priv->xid->xid2_flag2 = 0x40;
		grp->saved_xid2->xid2_flag2 = 0x40;
	}

	return rc;
}

/*
 * MPC Group Station FSM action
 * CTCM_PROTO_MPC only
 */
static void mpc_action_side_xid(fsm_instance *fsm, void *arg, int side)
{
	struct channel *ch = arg;
	int rc = 0;
	int gotlock = 0;
	unsigned long saveflags = 0;	/* avoids compiler warning with
P
Peter Tiedemann 已提交
1694
					   spin_unlock_irqrestore */
1695

P
Peter Tiedemann 已提交
1696 1697
	CTCM_PR_DEBUG("Enter %s: cp=%i ch=0x%p id=%s\n",
			__func__, smp_processor_id(), ch, ch->id);
1698 1699 1700 1701

	if (ctcm_checkalloc_buffer(ch))
					goto done;

P
Peter Tiedemann 已提交
1702 1703 1704
	/*
	 * skb data-buffer referencing:
	 */
1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736
	ch->trans_skb->data = ch->trans_skb_data;
	skb_reset_tail_pointer(ch->trans_skb);
	ch->trans_skb->len = 0;
	/* result of the previous 3 statements is NOT always
	 * already set after ctcm_checkalloc_buffer
	 * because of possible reuse of the trans_skb
	 */
	memset(ch->trans_skb->data, 0, 16);
	ch->rcvd_xid_th =  (struct th_header *)ch->trans_skb_data;
	/* check is main purpose here: */
	skb_put(ch->trans_skb, TH_HEADER_LENGTH);
	ch->rcvd_xid = (struct xid2 *)skb_tail_pointer(ch->trans_skb);
	/* check is main purpose here: */
	skb_put(ch->trans_skb, XID2_LENGTH);
	ch->rcvd_xid_id = skb_tail_pointer(ch->trans_skb);
	/* cleanup back to startpoint */
	ch->trans_skb->data = ch->trans_skb_data;
	skb_reset_tail_pointer(ch->trans_skb);
	ch->trans_skb->len = 0;

	/* non-checking rewrite of above skb data-buffer referencing: */
	/*
	memset(ch->trans_skb->data, 0, 16);
	ch->rcvd_xid_th =  (struct th_header *)ch->trans_skb_data;
	ch->rcvd_xid = (struct xid2 *)(ch->trans_skb_data + TH_HEADER_LENGTH);
	ch->rcvd_xid_id = ch->trans_skb_data + TH_HEADER_LENGTH + XID2_LENGTH;
	 */

	ch->ccw[8].flags	= CCW_FLAG_SLI | CCW_FLAG_CC;
	ch->ccw[8].count	= 0;
	ch->ccw[8].cda		= 0x00;

P
Peter Tiedemann 已提交
1737 1738 1739 1740 1741
	if (!(ch->xid_th && ch->xid && ch->xid_id))
		CTCM_DBF_TEXT_(MPC_TRACE, CTC_DBF_INFO,
			"%s(%s): xid_th=%p, xid=%p, xid_id=%p",
			CTCM_FUNTAIL, ch->id, ch->xid_th, ch->xid, ch->xid_id);

1742 1743
	if (side == XSIDE) {
		/* mpc_action_xside_xid */
P
Peter Tiedemann 已提交
1744 1745
		if (ch->xid_th == NULL)
				goto done;
1746 1747 1748 1749 1750
		ch->ccw[9].cmd_code	= CCW_CMD_WRITE;
		ch->ccw[9].flags	= CCW_FLAG_SLI | CCW_FLAG_CC;
		ch->ccw[9].count	= TH_HEADER_LENGTH;
		ch->ccw[9].cda		= virt_to_phys(ch->xid_th);

P
Peter Tiedemann 已提交
1751 1752
		if (ch->xid == NULL)
				goto done;
1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781
		ch->ccw[10].cmd_code	= CCW_CMD_WRITE;
		ch->ccw[10].flags	= CCW_FLAG_SLI | CCW_FLAG_CC;
		ch->ccw[10].count	= XID2_LENGTH;
		ch->ccw[10].cda		= virt_to_phys(ch->xid);

		ch->ccw[11].cmd_code	= CCW_CMD_READ;
		ch->ccw[11].flags	= CCW_FLAG_SLI | CCW_FLAG_CC;
		ch->ccw[11].count	= TH_HEADER_LENGTH;
		ch->ccw[11].cda		= virt_to_phys(ch->rcvd_xid_th);

		ch->ccw[12].cmd_code	= CCW_CMD_READ;
		ch->ccw[12].flags	= CCW_FLAG_SLI | CCW_FLAG_CC;
		ch->ccw[12].count	= XID2_LENGTH;
		ch->ccw[12].cda		= virt_to_phys(ch->rcvd_xid);

		ch->ccw[13].cmd_code	= CCW_CMD_READ;
		ch->ccw[13].cda		= virt_to_phys(ch->rcvd_xid_id);

	} else { /* side == YSIDE : mpc_action_yside_xid */
		ch->ccw[9].cmd_code	= CCW_CMD_READ;
		ch->ccw[9].flags	= CCW_FLAG_SLI | CCW_FLAG_CC;
		ch->ccw[9].count	= TH_HEADER_LENGTH;
		ch->ccw[9].cda		= virt_to_phys(ch->rcvd_xid_th);

		ch->ccw[10].cmd_code	= CCW_CMD_READ;
		ch->ccw[10].flags	= CCW_FLAG_SLI | CCW_FLAG_CC;
		ch->ccw[10].count	= XID2_LENGTH;
		ch->ccw[10].cda		= virt_to_phys(ch->rcvd_xid);

P
Peter Tiedemann 已提交
1782 1783
		if (ch->xid_th == NULL)
				goto done;
1784 1785 1786 1787 1788
		ch->ccw[11].cmd_code	= CCW_CMD_WRITE;
		ch->ccw[11].flags	= CCW_FLAG_SLI | CCW_FLAG_CC;
		ch->ccw[11].count	= TH_HEADER_LENGTH;
		ch->ccw[11].cda		= virt_to_phys(ch->xid_th);

P
Peter Tiedemann 已提交
1789 1790
		if (ch->xid == NULL)
				goto done;
1791 1792 1793 1794 1795
		ch->ccw[12].cmd_code	= CCW_CMD_WRITE;
		ch->ccw[12].flags	= CCW_FLAG_SLI | CCW_FLAG_CC;
		ch->ccw[12].count	= XID2_LENGTH;
		ch->ccw[12].cda		= virt_to_phys(ch->xid);

P
Peter Tiedemann 已提交
1796 1797
		if (ch->xid_id == NULL)
				goto done;
1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809
		ch->ccw[13].cmd_code	= CCW_CMD_WRITE;
		ch->ccw[13].cda		= virt_to_phys(ch->xid_id);

	}
	ch->ccw[13].flags	= CCW_FLAG_SLI | CCW_FLAG_CC;
	ch->ccw[13].count	= 4;

	ch->ccw[14].cmd_code	= CCW_CMD_NOOP;
	ch->ccw[14].flags	= CCW_FLAG_SLI;
	ch->ccw[14].count	= 0;
	ch->ccw[14].cda		= 0;

P
Peter Tiedemann 已提交
1810 1811 1812 1813
	CTCM_CCW_DUMP((char *)&ch->ccw[8], sizeof(struct ccw1) * 7);
	CTCM_D3_DUMP((char *)ch->xid_th, TH_HEADER_LENGTH);
	CTCM_D3_DUMP((char *)ch->xid, XID2_LENGTH);
	CTCM_D3_DUMP((char *)ch->xid_id, 4);
1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835

	if (!in_irq()) {
			 /* Such conditional locking is a known problem for
			  * sparse because its static undeterministic.
			  * Warnings should be ignored here. */
		spin_lock_irqsave(get_ccwdev_lock(ch->cdev), saveflags);
		gotlock = 1;
	}

	fsm_addtimer(&ch->timer, 5000 , CTC_EVENT_TIMER, ch);
	rc = ccw_device_start(ch->cdev, &ch->ccw[8],
				(unsigned long)ch, 0xff, 0);

	if (gotlock)	/* see remark above about conditional locking */
		spin_unlock_irqrestore(get_ccwdev_lock(ch->cdev), saveflags);

	if (rc != 0) {
		ctcm_ccw_check_rc(ch, rc,
				(side == XSIDE) ? "x-side XID" : "y-side XID");
	}

done:
P
Peter Tiedemann 已提交
1836 1837
	CTCM_PR_DEBUG("Exit %s: ch=0x%p id=%s\n",
				__func__, ch, ch->id);
1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865
	return;

}

/*
 * MPC Group Station FSM action
 * CTCM_PROTO_MPC only
 */
static void mpc_action_xside_xid(fsm_instance *fsm, int event, void *arg)
{
	mpc_action_side_xid(fsm, arg, XSIDE);
}

/*
 * MPC Group Station FSM action
 * CTCM_PROTO_MPC only
 */
static void mpc_action_yside_xid(fsm_instance *fsm, int event, void *arg)
{
	mpc_action_side_xid(fsm, arg, YSIDE);
}

/*
 * MPC Group Station FSM action
 * CTCM_PROTO_MPC only
 */
static void mpc_action_doxid0(fsm_instance *fsm, int event, void *arg)
{
P
Peter Tiedemann 已提交
1866 1867 1868 1869
	struct channel	   *ch   = arg;
	struct net_device  *dev  = ch->netdev;
	struct ctcm_priv   *priv = dev->priv;
	struct mpc_group   *grp  = priv->mpcg;
1870

P
Peter Tiedemann 已提交
1871 1872
	CTCM_PR_DEBUG("Enter %s: cp=%i ch=0x%p id=%s\n",
			__func__, smp_processor_id(), ch, ch->id);
1873 1874

	if (ch->xid == NULL) {
P
Peter Tiedemann 已提交
1875 1876 1877 1878
		CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
			"%s(%s): ch->xid == NULL",
				CTCM_FUNTAIL, dev->name);
		return;
1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907
	}

	fsm_newstate(ch->fsm, CH_XID0_INPROGRESS);

	ch->xid->xid2_option =	XID2_0;

	switch (fsm_getstate(grp->fsm)) {
	case MPCG_STATE_XID2INITW:
	case MPCG_STATE_XID2INITX:
		ch->ccw[8].cmd_code = CCW_CMD_SENSE_CMD;
		break;
	case MPCG_STATE_XID0IOWAIT:
	case MPCG_STATE_XID0IOWAIX:
		ch->ccw[8].cmd_code = CCW_CMD_WRITE_CTL;
		break;
	}

	fsm_event(grp->fsm, MPCG_EVENT_DOIO, ch);

	return;
}

/*
 * MPC Group Station FSM action
 * CTCM_PROTO_MPC only
*/
static void mpc_action_doxid7(fsm_instance *fsm, int event, void *arg)
{
	struct net_device *dev = arg;
P
Peter Tiedemann 已提交
1908 1909
	struct ctcm_priv  *priv = dev->priv;
	struct mpc_group  *grp  = NULL;
1910 1911 1912
	int direction;
	int send = 0;

P
Peter Tiedemann 已提交
1913 1914
	if (priv)
		grp = priv->mpcg;
1915
	if (grp == NULL) {
P
Peter Tiedemann 已提交
1916 1917
		fsm_event(grp->fsm, MPCG_EVENT_INOP, dev);
		return;
1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981
	}

	for (direction = READ; direction <= WRITE; direction++)	{
		struct channel *ch = priv->channel[direction];
		struct xid2 *thisxid = ch->xid;
		ch->xid_skb->data = ch->xid_skb_data;
		skb_reset_tail_pointer(ch->xid_skb);
		ch->xid_skb->len = 0;
		thisxid->xid2_option = XID2_7;
		send = 0;

		/* xid7 phase 1 */
		if (grp->outstanding_xid7_p2 > 0) {
			if (grp->roll == YSIDE) {
				if (fsm_getstate(ch->fsm) == CH_XID7_PENDING1) {
					fsm_newstate(ch->fsm, CH_XID7_PENDING2);
					ch->ccw[8].cmd_code = CCW_CMD_SENSE_CMD;
					memcpy(skb_put(ch->xid_skb,
							TH_HEADER_LENGTH),
					       &thdummy, TH_HEADER_LENGTH);
					send = 1;
				}
			} else if (fsm_getstate(ch->fsm) < CH_XID7_PENDING2) {
					fsm_newstate(ch->fsm, CH_XID7_PENDING2);
					ch->ccw[8].cmd_code = CCW_CMD_WRITE_CTL;
					memcpy(skb_put(ch->xid_skb,
						       TH_HEADER_LENGTH),
					       &thnorm, TH_HEADER_LENGTH);
					send = 1;
			}
		} else {
			/* xid7 phase 2 */
			if (grp->roll == YSIDE) {
				if (fsm_getstate(ch->fsm) < CH_XID7_PENDING4) {
					fsm_newstate(ch->fsm, CH_XID7_PENDING4);
					memcpy(skb_put(ch->xid_skb,
						       TH_HEADER_LENGTH),
					       &thnorm, TH_HEADER_LENGTH);
					ch->ccw[8].cmd_code = CCW_CMD_WRITE_CTL;
					send = 1;
				}
			} else if (fsm_getstate(ch->fsm) == CH_XID7_PENDING3) {
				fsm_newstate(ch->fsm, CH_XID7_PENDING4);
				ch->ccw[8].cmd_code = CCW_CMD_SENSE_CMD;
				memcpy(skb_put(ch->xid_skb, TH_HEADER_LENGTH),
						&thdummy, TH_HEADER_LENGTH);
				send = 1;
			}
		}

		if (send)
			fsm_event(grp->fsm, MPCG_EVENT_DOIO, ch);
	}

	return;
}

/*
 * MPC Group Station FSM action
 * CTCM_PROTO_MPC only
 */
static void mpc_action_rcvd_xid0(fsm_instance *fsm, int event, void *arg)
{

P
Peter Tiedemann 已提交
1982 1983 1984 1985 1986
	struct mpcg_info   *mpcginfo  = arg;
	struct channel	   *ch   = mpcginfo->ch;
	struct net_device  *dev  = ch->netdev;
	struct ctcm_priv   *priv = dev->priv;
	struct mpc_group   *grp  = priv->mpcg;
1987

P
Peter Tiedemann 已提交
1988 1989 1990
	CTCM_PR_DEBUG("%s: ch-id:%s xid2:%i xid7:%i xidt_p2:%i \n",
			__func__, ch->id, grp->outstanding_xid2,
			grp->outstanding_xid7, grp->outstanding_xid7_p2);
1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026

	if (fsm_getstate(ch->fsm) < CH_XID7_PENDING)
		fsm_newstate(ch->fsm, CH_XID7_PENDING);

	grp->outstanding_xid2--;
	grp->outstanding_xid7++;
	grp->outstanding_xid7_p2++;

	/* must change state before validating xid to */
	/* properly handle interim interrupts received*/
	switch (fsm_getstate(grp->fsm)) {
	case MPCG_STATE_XID2INITW:
		fsm_newstate(grp->fsm, MPCG_STATE_XID2INITX);
		mpc_validate_xid(mpcginfo);
		break;
	case MPCG_STATE_XID0IOWAIT:
		fsm_newstate(grp->fsm, MPCG_STATE_XID0IOWAIX);
		mpc_validate_xid(mpcginfo);
		break;
	case MPCG_STATE_XID2INITX:
		if (grp->outstanding_xid2 == 0) {
			fsm_newstate(grp->fsm, MPCG_STATE_XID7INITW);
			mpc_validate_xid(mpcginfo);
			fsm_event(grp->fsm, MPCG_EVENT_XID2DONE, dev);
		}
		break;
	case MPCG_STATE_XID0IOWAIX:
		if (grp->outstanding_xid2 == 0) {
			fsm_newstate(grp->fsm, MPCG_STATE_XID7INITI);
			mpc_validate_xid(mpcginfo);
			fsm_event(grp->fsm, MPCG_EVENT_XID2DONE, dev);
		}
		break;
	}
	kfree(mpcginfo);

P
Peter Tiedemann 已提交
2027 2028 2029 2030 2031 2032
	CTCM_PR_DEBUG("ctcmpc:%s() %s xid2:%i xid7:%i xidt_p2:%i \n",
		__func__, ch->id, grp->outstanding_xid2,
		grp->outstanding_xid7, grp->outstanding_xid7_p2);
	CTCM_PR_DEBUG("ctcmpc:%s() %s grpstate: %s chanstate: %s \n",
		__func__, ch->id,
		fsm_getstate_str(grp->fsm), fsm_getstate_str(ch->fsm));
2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049
	return;

}


/*
 * MPC Group Station FSM action
 * CTCM_PROTO_MPC only
 */
static void mpc_action_rcvd_xid7(fsm_instance *fsm, int event, void *arg)
{
	struct mpcg_info   *mpcginfo   = arg;
	struct channel	   *ch	       = mpcginfo->ch;
	struct net_device  *dev        = ch->netdev;
	struct ctcm_priv   *priv    = dev->priv;
	struct mpc_group   *grp     = priv->mpcg;

P
Peter Tiedemann 已提交
2050 2051 2052 2053
	CTCM_PR_DEBUG("Enter %s: cp=%i ch=0x%p id=%s\n",
		__func__, smp_processor_id(), ch, ch->id);
	CTCM_PR_DEBUG("%s: outstanding_xid7: %i, outstanding_xid7_p2: %i\n",
		__func__, grp->outstanding_xid7, grp->outstanding_xid7_p2);
2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098

	grp->outstanding_xid7--;
	ch->xid_skb->data = ch->xid_skb_data;
	skb_reset_tail_pointer(ch->xid_skb);
	ch->xid_skb->len = 0;

	switch (fsm_getstate(grp->fsm)) {
	case MPCG_STATE_XID7INITI:
		fsm_newstate(grp->fsm, MPCG_STATE_XID7INITZ);
		mpc_validate_xid(mpcginfo);
		break;
	case MPCG_STATE_XID7INITW:
		fsm_newstate(grp->fsm, MPCG_STATE_XID7INITX);
		mpc_validate_xid(mpcginfo);
		break;
	case MPCG_STATE_XID7INITZ:
	case MPCG_STATE_XID7INITX:
		if (grp->outstanding_xid7 == 0) {
			if (grp->outstanding_xid7_p2 > 0) {
				grp->outstanding_xid7 =
					grp->outstanding_xid7_p2;
				grp->outstanding_xid7_p2 = 0;
			} else
				fsm_newstate(grp->fsm, MPCG_STATE_XID7INITF);

			mpc_validate_xid(mpcginfo);
			fsm_event(grp->fsm, MPCG_EVENT_XID7DONE, dev);
			break;
		}
		mpc_validate_xid(mpcginfo);
		break;
	}
	kfree(mpcginfo);
	return;
}

/*
 * mpc_action helper of an MPC Group Station FSM action
 * CTCM_PROTO_MPC only
 */
static int mpc_send_qllc_discontact(struct net_device *dev)
{
	__u32	new_len	= 0;
	struct sk_buff   *skb;
	struct qllc      *qllcptr;
P
Peter Tiedemann 已提交
2099 2100
	struct ctcm_priv *priv = dev->priv;
	struct mpc_group *grp = priv->mpcg;
2101

P
Peter Tiedemann 已提交
2102 2103
	CTCM_PR_DEBUG("%s: GROUP STATE: %s\n",
		__func__, mpcg_state_names[grp->saved_state]);
2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128

	switch (grp->saved_state) {
	/*
	 * establish conn callback function is
	 * preferred method to report failure
	 */
	case MPCG_STATE_XID0IOWAIT:
	case MPCG_STATE_XID0IOWAIX:
	case MPCG_STATE_XID7INITI:
	case MPCG_STATE_XID7INITZ:
	case MPCG_STATE_XID2INITW:
	case MPCG_STATE_XID2INITX:
	case MPCG_STATE_XID7INITW:
	case MPCG_STATE_XID7INITX:
		if (grp->estconnfunc) {
			grp->estconnfunc(grp->port_num, -1, 0);
			grp->estconnfunc = NULL;
			break;
		}
	case MPCG_STATE_FLOWC:
	case MPCG_STATE_READY:
		grp->send_qllc_disc = 2;
		new_len = sizeof(struct qllc);
		qllcptr = kzalloc(new_len, gfp_type() | GFP_DMA);
		if (qllcptr == NULL) {
P
Peter Tiedemann 已提交
2129 2130 2131 2132
			CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
				"%s(%s): qllcptr allocation error",
						CTCM_FUNTAIL, dev->name);
			return -ENOMEM;
2133 2134 2135 2136 2137 2138 2139 2140
		}

		qllcptr->qllc_address = 0xcc;
		qllcptr->qllc_commands = 0x03;

		skb = __dev_alloc_skb(new_len, GFP_ATOMIC);

		if (skb == NULL) {
P
Peter Tiedemann 已提交
2141 2142 2143
			CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
				"%s(%s): skb allocation error",
						CTCM_FUNTAIL, dev->name);
2144 2145
			priv->stats.rx_dropped++;
			kfree(qllcptr);
P
Peter Tiedemann 已提交
2146
			return -ENOMEM;
2147 2148 2149 2150 2151 2152
		}

		memcpy(skb_put(skb, new_len), qllcptr, new_len);
		kfree(qllcptr);

		if (skb_headroom(skb) < 4) {
P
Peter Tiedemann 已提交
2153 2154 2155
			CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
				"%s(%s): skb_headroom error",
						CTCM_FUNTAIL, dev->name);
2156
			dev_kfree_skb_any(skb);
P
Peter Tiedemann 已提交
2157
			return -ENOMEM;
2158 2159 2160 2161
		}

		*((__u32 *)skb_push(skb, 4)) = priv->channel[READ]->pdu_seq;
		priv->channel[READ]->pdu_seq++;
P
Peter Tiedemann 已提交
2162 2163
		CTCM_PR_DBGDATA("ctcmpc: %s ToDCM_pdu_seq= %08x\n",
				__func__, priv->channel[READ]->pdu_seq);
2164 2165 2166 2167 2168 2169 2170 2171 2172

		/* receipt of CC03 resets anticipated sequence number on
		      receiving side */
		priv->channel[READ]->pdu_seq = 0x00;
		skb_reset_mac_header(skb);
		skb->dev = dev;
		skb->protocol = htons(ETH_P_SNAP);
		skb->ip_summed = CHECKSUM_UNNECESSARY;

P
Peter Tiedemann 已提交
2173
		CTCM_D3_DUMP(skb->data, (sizeof(struct qllc) + 4));
2174 2175 2176 2177 2178 2179 2180 2181

		netif_rx(skb);
		break;
	default:
		break;

	}

P
Peter Tiedemann 已提交
2182
	return 0;
2183 2184 2185
}
/* --- This is the END my friend --- */