1/*
2 *	Copyright IBM Corp. 2004, 2007
3 *	Authors:	Belinda Thompson (belindat@us.ibm.com)
4 *			Andy Richter (richtera@us.ibm.com)
5 *			Peter Tiedemann (ptiedem@de.ibm.com)
6 */
7
8/*
9	This module exports functions to be used by CCS:
10	EXPORT_SYMBOL(ctc_mpc_alloc_channel);
11	EXPORT_SYMBOL(ctc_mpc_establish_connectivity);
12	EXPORT_SYMBOL(ctc_mpc_dealloc_ch);
13	EXPORT_SYMBOL(ctc_mpc_flow_control);
14*/
15
16#undef DEBUG
17#undef DEBUGDATA
18#undef DEBUGCCW
19
20#define KMSG_COMPONENT "ctcm"
21#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
22
23#include <linux/module.h>
24#include <linux/init.h>
25#include <linux/kernel.h>
26#include <linux/slab.h>
27#include <linux/errno.h>
28#include <linux/types.h>
29#include <linux/interrupt.h>
30#include <linux/timer.h>
31#include <linux/sched.h>
32
33#include <linux/signal.h>
34#include <linux/string.h>
35#include <linux/proc_fs.h>
36
37#include <linux/ip.h>
38#include <linux/if_arp.h>
39#include <linux/tcp.h>
40#include <linux/skbuff.h>
41#include <linux/ctype.h>
42#include <linux/netdevice.h>
43#include <net/dst.h>
44
45#include <linux/io.h>		/* instead of <asm/io.h> ok ? */
46#include <asm/ccwdev.h>
47#include <asm/ccwgroup.h>
48#include <linux/bitops.h>	/* instead of <asm/bitops.h> ok ? */
49#include <linux/uaccess.h>	/* instead of <asm/uaccess.h> ok ? */
50#include <linux/wait.h>
51#include <linux/moduleparam.h>
52#include <asm/idals.h>
53
54#include "ctcm_main.h"
55#include "ctcm_mpc.h"
56#include "ctcm_fsms.h"
57
58static const struct xid2 init_xid = {
59	.xid2_type_id	=	XID_FM2,
60	.xid2_len	=	0x45,
61	.xid2_adj_id	=	0,
62	.xid2_rlen	=	0x31,
63	.xid2_resv1	=	0,
64	.xid2_flag1	=	0,
65	.xid2_fmtt	=	0,
66	.xid2_flag4	=	0x80,
67	.xid2_resv2	=	0,
68	.xid2_tgnum	=	0,
69	.xid2_sender_id	=	0,
70	.xid2_flag2	=	0,
71	.xid2_option	=	XID2_0,
72	.xid2_resv3	=	"\x00",
73	.xid2_resv4	=	0,
74	.xid2_dlc_type	=	XID2_READ_SIDE,
75	.xid2_resv5	=	0,
76	.xid2_mpc_flag	=	0,
77	.xid2_resv6	=	0,
78	.xid2_buf_len	=	(MPC_BUFSIZE_DEFAULT - 35),
79};
80
81static const struct th_header thnorm = {
82	.th_seg		=	0x00,
83	.th_ch_flag	=	TH_IS_XID,
84	.th_blk_flag	=	TH_DATA_IS_XID,
85	.th_is_xid	=	0x01,
86	.th_seq_num	=	0x00000000,
87};
88
89static const struct th_header thdummy = {
90	.th_seg		=	0x00,
91	.th_ch_flag	=	0x00,
92	.th_blk_flag	=	TH_DATA_IS_XID,
93	.th_is_xid	=	0x01,
94	.th_seq_num	=	0x00000000,
95};
96
97/*
98 * Definition of one MPC group
99 */
100
101/*
102 * Compatibility macros for busy handling
103 * of network devices.
104 */
105
106static void ctcmpc_unpack_skb(struct channel *ch, struct sk_buff *pskb);
107
108/*
109 * MPC Group state machine actions (static prototypes)
110 */
111static void mpc_action_nop(fsm_instance *fsm, int event, void *arg);
112static void mpc_action_go_ready(fsm_instance *fsm, int event, void *arg);
113static void mpc_action_go_inop(fsm_instance *fi, int event, void *arg);
114static void mpc_action_timeout(fsm_instance *fi, int event, void *arg);
115static int  mpc_validate_xid(struct mpcg_info *mpcginfo);
116static void mpc_action_yside_xid(fsm_instance *fsm, int event, void *arg);
117static void mpc_action_doxid0(fsm_instance *fsm, int event, void *arg);
118static void mpc_action_doxid7(fsm_instance *fsm, int event, void *arg);
119static void mpc_action_xside_xid(fsm_instance *fsm, int event, void *arg);
120static void mpc_action_rcvd_xid0(fsm_instance *fsm, int event, void *arg);
121static void mpc_action_rcvd_xid7(fsm_instance *fsm, int event, void *arg);
122
123#ifdef DEBUGDATA
124/*-------------------------------------------------------------------*
125* Dump buffer format						     *
126*								     *
127*--------------------------------------------------------------------*/
128void ctcmpc_dumpit(char *buf, int len)
129{
130	__u32	ct, sw, rm, dup;
131	char	*ptr, *rptr;
132	char	tbuf[82], tdup[82];
133	char	addr[22];
134	char	boff[12];
135	char	bhex[82], duphex[82];
136	char	basc[40];
137
138	sw  = 0;
139	rptr = ptr = buf;
140	rm  = 16;
141	duphex[0] = 0x00;
142	dup = 0;
143
144	for (ct = 0; ct < len; ct++, ptr++, rptr++) {
145		if (sw == 0) {
146			sprintf(addr, "%16.16llx", (__u64)rptr);
147
148			sprintf(boff, "%4.4X", (__u32)ct);
149			bhex[0] = '\0';
150			basc[0] = '\0';
151		}
152		if ((sw == 4) || (sw == 12))
153			strcat(bhex, " ");
154		if (sw == 8)
155			strcat(bhex, "	");
156
157		sprintf(tbuf, "%2.2llX", (__u64)*ptr);
158
159		tbuf[2] = '\0';
160		strcat(bhex, tbuf);
161		if ((0 != isprint(*ptr)) && (*ptr >= 0x20))
162			basc[sw] = *ptr;
163		else
164			basc[sw] = '.';
165
166		basc[sw+1] = '\0';
167		sw++;
168		rm--;
169		if (sw != 16)
170			continue;
171		if ((strcmp(duphex, bhex)) != 0) {
172			if (dup != 0) {
173				sprintf(tdup,
174					"Duplicate as above to %s", addr);
175				ctcm_pr_debug("		       --- %s ---\n",
176						tdup);
177			}
178			ctcm_pr_debug("   %s (+%s) : %s  [%s]\n",
179					addr, boff, bhex, basc);
180			dup = 0;
181			strcpy(duphex, bhex);
182		} else
183			dup++;
184
185		sw = 0;
186		rm = 16;
187	}  /* endfor */
188
189	if (sw != 0) {
190		for ( ; rm > 0; rm--, sw++) {
191			if ((sw == 4) || (sw == 12))
192				strcat(bhex, " ");
193			if (sw == 8)
194				strcat(bhex, "	");
195			strcat(bhex, "	");
196			strcat(basc, " ");
197		}
198		if (dup != 0) {
199			sprintf(tdup, "Duplicate as above to %s", addr);
200			ctcm_pr_debug("		       --- %s ---\n", tdup);
201		}
202		ctcm_pr_debug("   %s (+%s) : %s  [%s]\n",
203					addr, boff, bhex, basc);
204	} else {
205		if (dup >= 1) {
206			sprintf(tdup, "Duplicate as above to %s", addr);
207			ctcm_pr_debug("		       --- %s ---\n", tdup);
208		}
209		if (dup != 0) {
210			ctcm_pr_debug("   %s (+%s) : %s  [%s]\n",
211				addr, boff, bhex, basc);
212		}
213	}
214
215	return;
216
217}   /*	 end of ctcmpc_dumpit  */
218#endif
219
220#ifdef DEBUGDATA
221/*
222 * Dump header and first 16 bytes of an sk_buff for debugging purposes.
223 *
224 * skb		The sk_buff to dump.
225 * offset	Offset relative to skb-data, where to start the dump.
226 */
227void ctcmpc_dump_skb(struct sk_buff *skb, int offset)
228{
229	__u8 *p = skb->data;
230	struct th_header *header;
231	struct pdu *pheader;
232	int bl = skb->len;
233	int i;
234
235	if (p == NULL)
236		return;
237
238	p += offset;
239	header = (struct th_header *)p;
240
241	ctcm_pr_debug("dump:\n");
242	ctcm_pr_debug("skb len=%d \n", skb->len);
243	if (skb->len > 2) {
244		switch (header->th_ch_flag) {
245		case TH_HAS_PDU:
246			break;
247		case 0x00:
248		case TH_IS_XID:
249			if ((header->th_blk_flag == TH_DATA_IS_XID) &&
250			   (header->th_is_xid == 0x01))
251				goto dumpth;
252		case TH_SWEEP_REQ:
253				goto dumpth;
254		case TH_SWEEP_RESP:
255				goto dumpth;
256		default:
257			break;
258		}
259
260		pheader = (struct pdu *)p;
261		ctcm_pr_debug("pdu->offset: %d hex: %04x\n",
262			       pheader->pdu_offset, pheader->pdu_offset);
263		ctcm_pr_debug("pdu->flag  : %02x\n", pheader->pdu_flag);
264		ctcm_pr_debug("pdu->proto : %02x\n", pheader->pdu_proto);
265		ctcm_pr_debug("pdu->seq   : %02x\n", pheader->pdu_seq);
266					goto dumpdata;
267
268dumpth:
269		ctcm_pr_debug("th->seg     : %02x\n", header->th_seg);
270		ctcm_pr_debug("th->ch      : %02x\n", header->th_ch_flag);
271		ctcm_pr_debug("th->blk_flag: %02x\n", header->th_blk_flag);
272		ctcm_pr_debug("th->type    : %s\n",
273			       (header->th_is_xid) ? "DATA" : "XID");
274		ctcm_pr_debug("th->seqnum  : %04x\n", header->th_seq_num);
275
276	}
277dumpdata:
278	if (bl > 32)
279		bl = 32;
280	ctcm_pr_debug("data: ");
281	for (i = 0; i < bl; i++)
282		ctcm_pr_debug("%02x%s", *p++, (i % 16) ? " " : "\n");
283	ctcm_pr_debug("\n");
284}
285#endif
286
287static struct net_device *ctcmpc_get_dev(int port_num)
288{
289	char device[20];
290	struct net_device *dev;
291	struct ctcm_priv *priv;
292
293	sprintf(device, "%s%i", MPC_DEVICE_NAME, port_num);
294
295	dev = __dev_get_by_name(&init_net, device);
296
297	if (dev == NULL) {
298		CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
299			"%s: Device not found by name: %s",
300					CTCM_FUNTAIL, device);
301		return NULL;
302	}
303	priv = dev->ml_priv;
304	if (priv == NULL) {
305		CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
306			"%s(%s): dev->ml_priv is NULL",
307					CTCM_FUNTAIL, device);
308		return NULL;
309	}
310	if (priv->mpcg == NULL) {
311		CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
312			"%s(%s): priv->mpcg is NULL",
313					CTCM_FUNTAIL, device);
314		return NULL;
315	}
316	return dev;
317}
318
319/*
320 * ctc_mpc_alloc_channel
321 *	(exported interface)
322 *
323 * Device Initialization :
324 *	ACTPATH  driven IO operations
325 */
326int ctc_mpc_alloc_channel(int port_num, void (*callback)(int, int))
327{
328	struct net_device *dev;
329	struct mpc_group *grp;
330	struct ctcm_priv *priv;
331
332	dev = ctcmpc_get_dev(port_num);
333	if (dev == NULL)
334		return 1;
335	priv = dev->ml_priv;
336	grp = priv->mpcg;
337
338	grp->allochanfunc = callback;
339	grp->port_num = port_num;
340	grp->port_persist = 1;
341
342	CTCM_DBF_TEXT_(MPC_SETUP, CTC_DBF_INFO,
343			"%s(%s): state=%s",
344			CTCM_FUNTAIL, dev->name, fsm_getstate_str(grp->fsm));
345
346	switch (fsm_getstate(grp->fsm)) {
347	case MPCG_STATE_INOP:
348		/* Group is in the process of terminating */
349		grp->alloc_called = 1;
350		break;
351	case MPCG_STATE_RESET:
352		/* MPC Group will transition to state		  */
353		/* MPCG_STATE_XID2INITW iff the minimum number	  */
354		/* of 1 read and 1 write channel have successfully*/
355		/* activated					  */
356		/*fsm_newstate(grp->fsm, MPCG_STATE_XID2INITW);*/
357		if (callback)
358			grp->send_qllc_disc = 1;
359	case MPCG_STATE_XID0IOWAIT:
360		fsm_deltimer(&grp->timer);
361		grp->outstanding_xid2 = 0;
362		grp->outstanding_xid7 = 0;
363		grp->outstanding_xid7_p2 = 0;
364		grp->saved_xid2 = NULL;
365		if (callback)
366			ctcm_open(dev);
367		fsm_event(priv->fsm, DEV_EVENT_START, dev);
368		break;
369	case MPCG_STATE_READY:
370		/* XID exchanges completed after PORT was activated */
371		/* Link station already active			    */
372		/* Maybe timing issue...retry callback		    */
373		grp->allocchan_callback_retries++;
374		if (grp->allocchan_callback_retries < 4) {
375			if (grp->allochanfunc)
376				grp->allochanfunc(grp->port_num,
377						  grp->group_max_buflen);
378		} else {
379			/* there are problems...bail out	    */
380			/* there may be a state mismatch so restart */
381			fsm_event(grp->fsm, MPCG_EVENT_INOP, dev);
382			grp->allocchan_callback_retries = 0;
383		}
384		break;
385	}
386
387	return 0;
388}
389EXPORT_SYMBOL(ctc_mpc_alloc_channel);
390
391/*
392 * ctc_mpc_establish_connectivity
393 *	(exported interface)
394 */
395void ctc_mpc_establish_connectivity(int port_num,
396				void (*callback)(int, int, int))
397{
398	struct net_device *dev;
399	struct mpc_group *grp;
400	struct ctcm_priv *priv;
401	struct channel *rch, *wch;
402
403	dev = ctcmpc_get_dev(port_num);
404	if (dev == NULL)
405		return;
406	priv = dev->ml_priv;
407	grp = priv->mpcg;
408	rch = priv->channel[CTCM_READ];
409	wch = priv->channel[CTCM_WRITE];
410
411	CTCM_DBF_TEXT_(MPC_SETUP, CTC_DBF_INFO,
412			"%s(%s): state=%s",
413			CTCM_FUNTAIL, dev->name, fsm_getstate_str(grp->fsm));
414
415	grp->estconnfunc = callback;
416	grp->port_num = port_num;
417
418	switch (fsm_getstate(grp->fsm)) {
419	case MPCG_STATE_READY:
420		/* XID exchanges completed after PORT was activated */
421		/* Link station already active			    */
422		/* Maybe timing issue...retry callback		    */
423		fsm_deltimer(&grp->timer);
424		grp->estconn_callback_retries++;
425		if (grp->estconn_callback_retries < 4) {
426			if (grp->estconnfunc) {
427				grp->estconnfunc(grp->port_num, 0,
428						grp->group_max_buflen);
429				grp->estconnfunc = NULL;
430			}
431		} else {
432			/* there are problems...bail out	 */
433			fsm_event(grp->fsm, MPCG_EVENT_INOP, dev);
434			grp->estconn_callback_retries = 0;
435		}
436		break;
437	case MPCG_STATE_INOP:
438	case MPCG_STATE_RESET:
439		/* MPC Group is not ready to start XID - min num of */
440		/* 1 read and 1 write channel have not been acquired*/
441
442		CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
443			"%s(%s): REJECTED - inactive channels",
444					CTCM_FUNTAIL, dev->name);
445		if (grp->estconnfunc) {
446			grp->estconnfunc(grp->port_num, -1, 0);
447			grp->estconnfunc = NULL;
448		}
449		break;
450	case MPCG_STATE_XID2INITW:
451		/* alloc channel was called but no XID exchange    */
452		/* has occurred. initiate xside XID exchange	   */
453		/* make sure yside XID0 processing has not started */
454
455		if ((fsm_getstate(rch->fsm) > CH_XID0_PENDING) ||
456			(fsm_getstate(wch->fsm) > CH_XID0_PENDING)) {
457			CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
458				"%s(%s): ABORT - PASSIVE XID",
459					CTCM_FUNTAIL, dev->name);
460			break;
461		}
462		grp->send_qllc_disc = 1;
463		fsm_newstate(grp->fsm, MPCG_STATE_XID0IOWAIT);
464		fsm_deltimer(&grp->timer);
465		fsm_addtimer(&grp->timer, MPC_XID_TIMEOUT_VALUE,
466						MPCG_EVENT_TIMER, dev);
467		grp->outstanding_xid7 = 0;
468		grp->outstanding_xid7_p2 = 0;
469		grp->saved_xid2 = NULL;
470		if ((rch->in_mpcgroup) &&
471				(fsm_getstate(rch->fsm) == CH_XID0_PENDING))
472			fsm_event(grp->fsm, MPCG_EVENT_XID0DO, rch);
473		else {
474			CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
475				"%s(%s): RX-%s not ready for ACTIVE XID0",
476					CTCM_FUNTAIL, dev->name, rch->id);
477			if (grp->estconnfunc) {
478				grp->estconnfunc(grp->port_num, -1, 0);
479				grp->estconnfunc = NULL;
480			}
481			fsm_deltimer(&grp->timer);
482				goto done;
483		}
484		if ((wch->in_mpcgroup) &&
485				(fsm_getstate(wch->fsm) == CH_XID0_PENDING))
486			fsm_event(grp->fsm, MPCG_EVENT_XID0DO, wch);
487		else {
488			CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
489				"%s(%s): WX-%s not ready for ACTIVE XID0",
490					CTCM_FUNTAIL, dev->name, wch->id);
491			if (grp->estconnfunc) {
492				grp->estconnfunc(grp->port_num, -1, 0);
493				grp->estconnfunc = NULL;
494			}
495			fsm_deltimer(&grp->timer);
496				goto done;
497			}
498		break;
499	case MPCG_STATE_XID0IOWAIT:
500		/* already in active XID negotiations */
501	default:
502		break;
503	}
504
505done:
506	CTCM_PR_DEBUG("Exit %s()\n", __func__);
507	return;
508}
509EXPORT_SYMBOL(ctc_mpc_establish_connectivity);
510
511/*
512 * ctc_mpc_dealloc_ch
513 *	(exported interface)
514 */
515void ctc_mpc_dealloc_ch(int port_num)
516{
517	struct net_device *dev;
518	struct ctcm_priv *priv;
519	struct mpc_group *grp;
520
521	dev = ctcmpc_get_dev(port_num);
522	if (dev == NULL)
523		return;
524	priv = dev->ml_priv;
525	grp = priv->mpcg;
526
527	CTCM_DBF_TEXT_(MPC_SETUP, CTC_DBF_DEBUG,
528			"%s: %s: refcount = %d\n",
529			CTCM_FUNTAIL, dev->name, netdev_refcnt_read(dev));
530
531	fsm_deltimer(&priv->restart_timer);
532	grp->channels_terminating = 0;
533	fsm_deltimer(&grp->timer);
534	grp->allochanfunc = NULL;
535	grp->estconnfunc = NULL;
536	grp->port_persist = 0;
537	grp->send_qllc_disc = 0;
538	fsm_event(grp->fsm, MPCG_EVENT_INOP, dev);
539
540	ctcm_close(dev);
541	return;
542}
543EXPORT_SYMBOL(ctc_mpc_dealloc_ch);
544
545/*
546 * ctc_mpc_flow_control
547 *	(exported interface)
548 */
549void ctc_mpc_flow_control(int port_num, int flowc)
550{
551	struct ctcm_priv *priv;
552	struct mpc_group *grp;
553	struct net_device *dev;
554	struct channel *rch;
555	int mpcg_state;
556
557	dev = ctcmpc_get_dev(port_num);
558	if (dev == NULL)
559		return;
560	priv = dev->ml_priv;
561	grp = priv->mpcg;
562
563	CTCM_DBF_TEXT_(MPC_TRACE, CTC_DBF_DEBUG,
564			"%s: %s: flowc = %d",
565				CTCM_FUNTAIL, dev->name, flowc);
566
567	rch = priv->channel[CTCM_READ];
568
569	mpcg_state = fsm_getstate(grp->fsm);
570	switch (flowc) {
571	case 1:
572		if (mpcg_state == MPCG_STATE_FLOWC)
573			break;
574		if (mpcg_state == MPCG_STATE_READY) {
575			if (grp->flow_off_called == 1)
576				grp->flow_off_called = 0;
577			else
578				fsm_newstate(grp->fsm, MPCG_STATE_FLOWC);
579			break;
580		}
581		break;
582	case 0:
583		if (mpcg_state == MPCG_STATE_FLOWC) {
584			fsm_newstate(grp->fsm, MPCG_STATE_READY);
585			/* ensure any data that has accumulated */
586			/* on the io_queue will now be sen t	*/
587			tasklet_schedule(&rch->ch_tasklet);
588		}
589		/* possible race condition			*/
590		if (mpcg_state == MPCG_STATE_READY) {
591			grp->flow_off_called = 1;
592			break;
593		}
594		break;
595	}
596
597}
598EXPORT_SYMBOL(ctc_mpc_flow_control);
599
600static int mpc_send_qllc_discontact(struct net_device *);
601
602/*
603 * helper function of ctcmpc_unpack_skb
604*/
605static void mpc_rcvd_sweep_resp(struct mpcg_info *mpcginfo)
606{
607	struct channel	  *rch = mpcginfo->ch;
608	struct net_device *dev = rch->netdev;
609	struct ctcm_priv   *priv = dev->ml_priv;
610	struct mpc_group  *grp = priv->mpcg;
611	struct channel	  *ch = priv->channel[CTCM_WRITE];
612
613	CTCM_PR_DEBUG("%s: ch=0x%p id=%s\n", __func__, ch, ch->id);
614	CTCM_D3_DUMP((char *)mpcginfo->sweep, TH_SWEEP_LENGTH);
615
616	grp->sweep_rsp_pend_num--;
617
618	if ((grp->sweep_req_pend_num == 0) &&
619			(grp->sweep_rsp_pend_num == 0)) {
620		fsm_deltimer(&ch->sweep_timer);
621		grp->in_sweep = 0;
622		rch->th_seq_num = 0x00;
623		ch->th_seq_num = 0x00;
624		ctcm_clear_busy_do(dev);
625	}
626
627	kfree(mpcginfo);
628
629	return;
630
631}
632
633/*
634 * helper function of mpc_rcvd_sweep_req
635 * which is a helper of ctcmpc_unpack_skb
636 */
637static void ctcmpc_send_sweep_resp(struct channel *rch)
638{
639	struct net_device *dev = rch->netdev;
640	struct ctcm_priv *priv = dev->ml_priv;
641	struct mpc_group *grp = priv->mpcg;
642	struct th_sweep *header;
643	struct sk_buff *sweep_skb;
644	struct channel *ch  = priv->channel[CTCM_WRITE];
645
646	CTCM_PR_DEBUG("%s: ch=0x%p id=%s\n", __func__, rch, rch->id);
647
648	sweep_skb = __dev_alloc_skb(MPC_BUFSIZE_DEFAULT, GFP_ATOMIC | GFP_DMA);
649	if (sweep_skb == NULL) {
650		CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
651			"%s(%s): sweep_skb allocation ERROR\n",
652			CTCM_FUNTAIL, rch->id);
653		goto done;
654	}
655
656	header = kmalloc(sizeof(struct th_sweep), gfp_type());
657
658	if (!header) {
659		dev_kfree_skb_any(sweep_skb);
660		goto done;
661	}
662
663	header->th.th_seg	= 0x00 ;
664	header->th.th_ch_flag	= TH_SWEEP_RESP;
665	header->th.th_blk_flag	= 0x00;
666	header->th.th_is_xid	= 0x00;
667	header->th.th_seq_num	= 0x00;
668	header->sw.th_last_seq	= ch->th_seq_num;
669
670	memcpy(skb_put(sweep_skb, TH_SWEEP_LENGTH), header, TH_SWEEP_LENGTH);
671
672	kfree(header);
673
674	dev->trans_start = jiffies;
675	skb_queue_tail(&ch->sweep_queue, sweep_skb);
676
677	fsm_addtimer(&ch->sweep_timer, 100, CTC_EVENT_RSWEEP_TIMER, ch);
678
679	return;
680
681done:
682	grp->in_sweep = 0;
683	ctcm_clear_busy_do(dev);
684	fsm_event(grp->fsm, MPCG_EVENT_INOP, dev);
685
686	return;
687}
688
689/*
690 * helper function of ctcmpc_unpack_skb
691 */
692static void mpc_rcvd_sweep_req(struct mpcg_info *mpcginfo)
693{
694	struct channel	  *rch     = mpcginfo->ch;
695	struct net_device *dev     = rch->netdev;
696	struct ctcm_priv  *priv = dev->ml_priv;
697	struct mpc_group  *grp  = priv->mpcg;
698	struct channel	  *ch	   = priv->channel[CTCM_WRITE];
699
700	if (do_debug)
701		CTCM_DBF_TEXT_(MPC_TRACE, CTC_DBF_DEBUG,
702			" %s(): ch=0x%p id=%s\n", __func__, ch, ch->id);
703
704	if (grp->in_sweep == 0) {
705		grp->in_sweep = 1;
706		ctcm_test_and_set_busy(dev);
707		grp->sweep_req_pend_num = grp->active_channels[CTCM_READ];
708		grp->sweep_rsp_pend_num = grp->active_channels[CTCM_READ];
709	}
710
711	CTCM_D3_DUMP((char *)mpcginfo->sweep, TH_SWEEP_LENGTH);
712
713	grp->sweep_req_pend_num--;
714	ctcmpc_send_sweep_resp(ch);
715	kfree(mpcginfo);
716	return;
717}
718
719/*
720  * MPC Group Station FSM definitions
721 */
722static const char *mpcg_event_names[] = {
723	[MPCG_EVENT_INOP]	= "INOP Condition",
724	[MPCG_EVENT_DISCONC]	= "Discontact Received",
725	[MPCG_EVENT_XID0DO]	= "Channel Active - Start XID",
726	[MPCG_EVENT_XID2]	= "XID2 Received",
727	[MPCG_EVENT_XID2DONE]	= "XID0 Complete",
728	[MPCG_EVENT_XID7DONE]	= "XID7 Complete",
729	[MPCG_EVENT_TIMER]	= "XID Setup Timer",
730	[MPCG_EVENT_DOIO]	= "XID DoIO",
731};
732
733static const char *mpcg_state_names[] = {
734	[MPCG_STATE_RESET]	= "Reset",
735	[MPCG_STATE_INOP]	= "INOP",
736	[MPCG_STATE_XID2INITW]	= "Passive XID- XID0 Pending Start",
737	[MPCG_STATE_XID2INITX]	= "Passive XID- XID0 Pending Complete",
738	[MPCG_STATE_XID7INITW]	= "Passive XID- XID7 Pending P1 Start",
739	[MPCG_STATE_XID7INITX]	= "Passive XID- XID7 Pending P2 Complete",
740	[MPCG_STATE_XID0IOWAIT]	= "Active  XID- XID0 Pending Start",
741	[MPCG_STATE_XID0IOWAIX]	= "Active  XID- XID0 Pending Complete",
742	[MPCG_STATE_XID7INITI]	= "Active  XID- XID7 Pending Start",
743	[MPCG_STATE_XID7INITZ]	= "Active  XID- XID7 Pending Complete ",
744	[MPCG_STATE_XID7INITF]	= "XID        - XID7 Complete ",
745	[MPCG_STATE_FLOWC]	= "FLOW CONTROL ON",
746	[MPCG_STATE_READY]	= "READY",
747};
748
749/*
750 * The MPC Group Station FSM
751 *   22 events
752 */
753static const fsm_node mpcg_fsm[] = {
754	{ MPCG_STATE_RESET,	MPCG_EVENT_INOP,	mpc_action_go_inop    },
755	{ MPCG_STATE_INOP,	MPCG_EVENT_INOP,	mpc_action_nop        },
756	{ MPCG_STATE_FLOWC,	MPCG_EVENT_INOP,	mpc_action_go_inop    },
757
758	{ MPCG_STATE_READY,	MPCG_EVENT_DISCONC,	mpc_action_discontact },
759	{ MPCG_STATE_READY,	MPCG_EVENT_INOP,	mpc_action_go_inop    },
760
761	{ MPCG_STATE_XID2INITW,	MPCG_EVENT_XID0DO,	mpc_action_doxid0     },
762	{ MPCG_STATE_XID2INITW,	MPCG_EVENT_XID2,	mpc_action_rcvd_xid0  },
763	{ MPCG_STATE_XID2INITW,	MPCG_EVENT_INOP,	mpc_action_go_inop    },
764	{ MPCG_STATE_XID2INITW,	MPCG_EVENT_TIMER,	mpc_action_timeout    },
765	{ MPCG_STATE_XID2INITW,	MPCG_EVENT_DOIO,	mpc_action_yside_xid  },
766
767	{ MPCG_STATE_XID2INITX,	MPCG_EVENT_XID0DO,	mpc_action_doxid0     },
768	{ MPCG_STATE_XID2INITX,	MPCG_EVENT_XID2,	mpc_action_rcvd_xid0  },
769	{ MPCG_STATE_XID2INITX,	MPCG_EVENT_INOP,	mpc_action_go_inop    },
770	{ MPCG_STATE_XID2INITX,	MPCG_EVENT_TIMER,	mpc_action_timeout    },
771	{ MPCG_STATE_XID2INITX,	MPCG_EVENT_DOIO,	mpc_action_yside_xid  },
772
773	{ MPCG_STATE_XID7INITW,	MPCG_EVENT_XID2DONE,	mpc_action_doxid7     },
774	{ MPCG_STATE_XID7INITW,	MPCG_EVENT_DISCONC,	mpc_action_discontact },
775	{ MPCG_STATE_XID7INITW,	MPCG_EVENT_XID2,	mpc_action_rcvd_xid7  },
776	{ MPCG_STATE_XID7INITW,	MPCG_EVENT_INOP,	mpc_action_go_inop    },
777	{ MPCG_STATE_XID7INITW,	MPCG_EVENT_TIMER,	mpc_action_timeout    },
778	{ MPCG_STATE_XID7INITW,	MPCG_EVENT_XID7DONE,	mpc_action_doxid7     },
779	{ MPCG_STATE_XID7INITW,	MPCG_EVENT_DOIO,	mpc_action_yside_xid  },
780
781	{ MPCG_STATE_XID7INITX,	MPCG_EVENT_DISCONC,	mpc_action_discontact },
782	{ MPCG_STATE_XID7INITX,	MPCG_EVENT_XID2,	mpc_action_rcvd_xid7  },
783	{ MPCG_STATE_XID7INITX,	MPCG_EVENT_INOP,	mpc_action_go_inop    },
784	{ MPCG_STATE_XID7INITX,	MPCG_EVENT_XID7DONE,	mpc_action_doxid7     },
785	{ MPCG_STATE_XID7INITX,	MPCG_EVENT_TIMER,	mpc_action_timeout    },
786	{ MPCG_STATE_XID7INITX,	MPCG_EVENT_DOIO,	mpc_action_yside_xid  },
787
788	{ MPCG_STATE_XID0IOWAIT, MPCG_EVENT_XID0DO,	mpc_action_doxid0     },
789	{ MPCG_STATE_XID0IOWAIT, MPCG_EVENT_DISCONC,	mpc_action_discontact },
790	{ MPCG_STATE_XID0IOWAIT, MPCG_EVENT_XID2,	mpc_action_rcvd_xid0  },
791	{ MPCG_STATE_XID0IOWAIT, MPCG_EVENT_INOP,	mpc_action_go_inop    },
792	{ MPCG_STATE_XID0IOWAIT, MPCG_EVENT_TIMER,	mpc_action_timeout    },
793	{ MPCG_STATE_XID0IOWAIT, MPCG_EVENT_DOIO,	mpc_action_xside_xid  },
794
795	{ MPCG_STATE_XID0IOWAIX, MPCG_EVENT_XID0DO,	mpc_action_doxid0     },
796	{ MPCG_STATE_XID0IOWAIX, MPCG_EVENT_DISCONC,	mpc_action_discontact },
797	{ MPCG_STATE_XID0IOWAIX, MPCG_EVENT_XID2,	mpc_action_rcvd_xid0  },
798	{ MPCG_STATE_XID0IOWAIX, MPCG_EVENT_INOP,	mpc_action_go_inop    },
799	{ MPCG_STATE_XID0IOWAIX, MPCG_EVENT_TIMER,	mpc_action_timeout    },
800	{ MPCG_STATE_XID0IOWAIX, MPCG_EVENT_DOIO,	mpc_action_xside_xid  },
801
802	{ MPCG_STATE_XID7INITI,	MPCG_EVENT_XID2DONE,	mpc_action_doxid7     },
803	{ MPCG_STATE_XID7INITI,	MPCG_EVENT_XID2,	mpc_action_rcvd_xid7  },
804	{ MPCG_STATE_XID7INITI,	MPCG_EVENT_DISCONC,	mpc_action_discontact },
805	{ MPCG_STATE_XID7INITI,	MPCG_EVENT_INOP,	mpc_action_go_inop    },
806	{ MPCG_STATE_XID7INITI,	MPCG_EVENT_TIMER,	mpc_action_timeout    },
807	{ MPCG_STATE_XID7INITI,	MPCG_EVENT_XID7DONE,	mpc_action_doxid7     },
808	{ MPCG_STATE_XID7INITI,	MPCG_EVENT_DOIO,	mpc_action_xside_xid  },
809
810	{ MPCG_STATE_XID7INITZ,	MPCG_EVENT_XID2,	mpc_action_rcvd_xid7  },
811	{ MPCG_STATE_XID7INITZ,	MPCG_EVENT_XID7DONE,	mpc_action_doxid7     },
812	{ MPCG_STATE_XID7INITZ,	MPCG_EVENT_DISCONC,	mpc_action_discontact },
813	{ MPCG_STATE_XID7INITZ,	MPCG_EVENT_INOP,	mpc_action_go_inop    },
814	{ MPCG_STATE_XID7INITZ,	MPCG_EVENT_TIMER,	mpc_action_timeout    },
815	{ MPCG_STATE_XID7INITZ,	MPCG_EVENT_DOIO,	mpc_action_xside_xid  },
816
817	{ MPCG_STATE_XID7INITF,	MPCG_EVENT_INOP,	mpc_action_go_inop    },
818	{ MPCG_STATE_XID7INITF,	MPCG_EVENT_XID7DONE,	mpc_action_go_ready   },
819};
820
821static int mpcg_fsm_len = ARRAY_SIZE(mpcg_fsm);
822
823/*
824 * MPC Group Station FSM action
825 * CTCM_PROTO_MPC only
826 */
827static void mpc_action_go_ready(fsm_instance *fsm, int event, void *arg)
828{
829	struct net_device *dev = arg;
830	struct ctcm_priv *priv = dev->ml_priv;
831	struct mpc_group *grp = priv->mpcg;
832
833	if (grp == NULL) {
834		CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
835			"%s(%s): No MPC group",
836				CTCM_FUNTAIL, dev->name);
837		return;
838	}
839
840	fsm_deltimer(&grp->timer);
841
842	if (grp->saved_xid2->xid2_flag2 == 0x40) {
843		priv->xid->xid2_flag2 = 0x00;
844		if (grp->estconnfunc) {
845			grp->estconnfunc(grp->port_num, 1,
846					grp->group_max_buflen);
847			grp->estconnfunc = NULL;
848		} else if (grp->allochanfunc)
849			grp->send_qllc_disc = 1;
850
851		fsm_event(grp->fsm, MPCG_EVENT_INOP, dev);
852		CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
853				"%s(%s): fails",
854					CTCM_FUNTAIL, dev->name);
855		return;
856	}
857
858	grp->port_persist = 1;
859	grp->out_of_sequence = 0;
860	grp->estconn_called = 0;
861
862	tasklet_hi_schedule(&grp->mpc_tasklet2);
863
864	return;
865}
866
867/*
868 * helper of ctcm_init_netdevice
869 * CTCM_PROTO_MPC only
870 */
871void mpc_group_ready(unsigned long adev)
872{
873	struct net_device *dev = (struct net_device *)adev;
874	struct ctcm_priv *priv = dev->ml_priv;
875	struct mpc_group *grp = priv->mpcg;
876	struct channel *ch = NULL;
877
878	if (grp == NULL) {
879		CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
880			"%s(%s): No MPC group",
881				CTCM_FUNTAIL, dev->name);
882		return;
883	}
884
885	CTCM_DBF_TEXT_(MPC_SETUP, CTC_DBF_NOTICE,
886		"%s: %s: GROUP TRANSITIONED TO READY, maxbuf = %d\n",
887			CTCM_FUNTAIL, dev->name, grp->group_max_buflen);
888
889	fsm_newstate(grp->fsm, MPCG_STATE_READY);
890
891	/* Put up a read on the channel */
892	ch = priv->channel[CTCM_READ];
893	ch->pdu_seq = 0;
894	CTCM_PR_DBGDATA("ctcmpc: %s() ToDCM_pdu_seq= %08x\n" ,
895			__func__, ch->pdu_seq);
896
897	ctcmpc_chx_rxidle(ch->fsm, CTC_EVENT_START, ch);
898	/* Put the write channel in idle state */
899	ch = priv->channel[CTCM_WRITE];
900	if (ch->collect_len > 0) {
901		spin_lock(&ch->collect_lock);
902		ctcm_purge_skb_queue(&ch->collect_queue);
903		ch->collect_len = 0;
904		spin_unlock(&ch->collect_lock);
905	}
906	ctcm_chx_txidle(ch->fsm, CTC_EVENT_START, ch);
907	ctcm_clear_busy(dev);
908
909	if (grp->estconnfunc) {
910		grp->estconnfunc(grp->port_num, 0,
911				    grp->group_max_buflen);
912		grp->estconnfunc = NULL;
913	} else 	if (grp->allochanfunc)
914		grp->allochanfunc(grp->port_num, grp->group_max_buflen);
915
916	grp->send_qllc_disc = 1;
917	grp->changed_side = 0;
918
919	return;
920
921}
922
923/*
924 * Increment the MPC Group Active Channel Counts
925 * helper of dev_action (called from channel fsm)
926 */
927void mpc_channel_action(struct channel *ch, int direction, int action)
928{
929	struct net_device  *dev  = ch->netdev;
930	struct ctcm_priv   *priv = dev->ml_priv;
931	struct mpc_group   *grp  = priv->mpcg;
932
933	if (grp == NULL) {
934		CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
935			"%s(%s): No MPC group",
936				CTCM_FUNTAIL, dev->name);
937		return;
938	}
939
940	CTCM_PR_DEBUG("enter %s: ch=0x%p id=%s\n", __func__, ch, ch->id);
941
942	CTCM_DBF_TEXT_(MPC_TRACE, CTC_DBF_NOTICE,
943		"%s: %i / Grp:%s total_channels=%i, active_channels: "
944		"read=%i, write=%i\n", __func__, action,
945		fsm_getstate_str(grp->fsm), grp->num_channel_paths,
946		grp->active_channels[CTCM_READ],
947		grp->active_channels[CTCM_WRITE]);
948
949	if ((action == MPC_CHANNEL_ADD) && (ch->in_mpcgroup == 0)) {
950		grp->num_channel_paths++;
951		grp->active_channels[direction]++;
952		grp->outstanding_xid2++;
953		ch->in_mpcgroup = 1;
954
955		if (ch->xid_skb != NULL)
956			dev_kfree_skb_any(ch->xid_skb);
957
958		ch->xid_skb = __dev_alloc_skb(MPC_BUFSIZE_DEFAULT,
959					GFP_ATOMIC | GFP_DMA);
960		if (ch->xid_skb == NULL) {
961			CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
962				"%s(%s): Couldn't alloc ch xid_skb\n",
963				CTCM_FUNTAIL, dev->name);
964			fsm_event(grp->fsm, MPCG_EVENT_INOP, dev);
965			return;
966		}
967		ch->xid_skb_data = ch->xid_skb->data;
968		ch->xid_th = (struct th_header *)ch->xid_skb->data;
969		skb_put(ch->xid_skb, TH_HEADER_LENGTH);
970		ch->xid = (struct xid2 *)skb_tail_pointer(ch->xid_skb);
971		skb_put(ch->xid_skb, XID2_LENGTH);
972		ch->xid_id = skb_tail_pointer(ch->xid_skb);
973		ch->xid_skb->data = ch->xid_skb_data;
974		skb_reset_tail_pointer(ch->xid_skb);
975		ch->xid_skb->len = 0;
976
977		memcpy(skb_put(ch->xid_skb, grp->xid_skb->len),
978				grp->xid_skb->data,
979				grp->xid_skb->len);
980
981		ch->xid->xid2_dlc_type =
982			((CHANNEL_DIRECTION(ch->flags) == CTCM_READ)
983				? XID2_READ_SIDE : XID2_WRITE_SIDE);
984
985		if (CHANNEL_DIRECTION(ch->flags) == CTCM_WRITE)
986			ch->xid->xid2_buf_len = 0x00;
987
988		ch->xid_skb->data = ch->xid_skb_data;
989		skb_reset_tail_pointer(ch->xid_skb);
990		ch->xid_skb->len = 0;
991
992		fsm_newstate(ch->fsm, CH_XID0_PENDING);
993
994		if ((grp->active_channels[CTCM_READ] > 0) &&
995		    (grp->active_channels[CTCM_WRITE] > 0) &&
996			(fsm_getstate(grp->fsm) < MPCG_STATE_XID2INITW)) {
997			fsm_newstate(grp->fsm, MPCG_STATE_XID2INITW);
998			CTCM_DBF_TEXT_(MPC_SETUP, CTC_DBF_NOTICE,
999				"%s: %s: MPC GROUP CHANNELS ACTIVE\n",
1000						__func__, dev->name);
1001		}
1002	} else if ((action == MPC_CHANNEL_REMOVE) &&
1003			(ch->in_mpcgroup == 1)) {
1004		ch->in_mpcgroup = 0;
1005		grp->num_channel_paths--;
1006		grp->active_channels[direction]--;
1007
1008		if (ch->xid_skb != NULL)
1009			dev_kfree_skb_any(ch->xid_skb);
1010		ch->xid_skb = NULL;
1011
1012		if (grp->channels_terminating)
1013					goto done;
1014
1015		if (((grp->active_channels[CTCM_READ] == 0) &&
1016					(grp->active_channels[CTCM_WRITE] > 0))
1017			|| ((grp->active_channels[CTCM_WRITE] == 0) &&
1018					(grp->active_channels[CTCM_READ] > 0)))
1019			fsm_event(grp->fsm, MPCG_EVENT_INOP, dev);
1020	}
1021done:
1022	CTCM_DBF_TEXT_(MPC_TRACE, CTC_DBF_DEBUG,
1023		"exit %s: %i / Grp:%s total_channels=%i, active_channels: "
1024		"read=%i, write=%i\n", __func__, action,
1025		fsm_getstate_str(grp->fsm), grp->num_channel_paths,
1026		grp->active_channels[CTCM_READ],
1027		grp->active_channels[CTCM_WRITE]);
1028
1029	CTCM_PR_DEBUG("exit %s: ch=0x%p id=%s\n", __func__, ch, ch->id);
1030}
1031
1032/**
1033 * Unpack a just received skb and hand it over to
1034 * upper layers.
1035 * special MPC version of unpack_skb.
1036 *
1037 * ch		The channel where this skb has been received.
1038 * pskb		The received skb.
1039 */
1040static void ctcmpc_unpack_skb(struct channel *ch, struct sk_buff *pskb)
1041{
1042	struct net_device *dev	= ch->netdev;
1043	struct ctcm_priv *priv = dev->ml_priv;
1044	struct mpc_group *grp = priv->mpcg;
1045	struct pdu *curr_pdu;
1046	struct mpcg_info *mpcginfo;
1047	struct th_header *header = NULL;
1048	struct th_sweep *sweep = NULL;
1049	int pdu_last_seen = 0;
1050	__u32 new_len;
1051	struct sk_buff *skb;
1052	int skblen;
1053	int sendrc = 0;
1054
1055	CTCM_PR_DEBUG("ctcmpc enter: %s() %s cp:%i ch:%s\n",
1056			__func__, dev->name, smp_processor_id(), ch->id);
1057
1058	header = (struct th_header *)pskb->data;
1059	if ((header->th_seg == 0) &&
1060		(header->th_ch_flag == 0) &&
1061		(header->th_blk_flag == 0) &&
1062		(header->th_seq_num == 0))
1063		/* nothing for us */	goto done;
1064
1065	CTCM_PR_DBGDATA("%s: th_header\n", __func__);
1066	CTCM_D3_DUMP((char *)header, TH_HEADER_LENGTH);
1067	CTCM_PR_DBGDATA("%s: pskb len: %04x \n", __func__, pskb->len);
1068
1069	pskb->dev = dev;
1070	pskb->ip_summed = CHECKSUM_UNNECESSARY;
1071	skb_pull(pskb, TH_HEADER_LENGTH);
1072
1073	if (likely(header->th_ch_flag == TH_HAS_PDU)) {
1074		CTCM_PR_DBGDATA("%s: came into th_has_pdu\n", __func__);
1075		if ((fsm_getstate(grp->fsm) == MPCG_STATE_FLOWC) ||
1076		   ((fsm_getstate(grp->fsm) == MPCG_STATE_READY) &&
1077		    (header->th_seq_num != ch->th_seq_num + 1) &&
1078		    (ch->th_seq_num != 0))) {
1079			/* This is NOT the next segment		*
1080			 * we are not the correct race winner	*
1081			 * go away and let someone else win	*
1082			 * BUT..this only applies if xid negot	*
1083			 * is done				*
1084			*/
1085			grp->out_of_sequence += 1;
1086			__skb_push(pskb, TH_HEADER_LENGTH);
1087			skb_queue_tail(&ch->io_queue, pskb);
1088			CTCM_PR_DBGDATA("%s: th_seq_num expect:%08x "
1089					"got:%08x\n", __func__,
1090				ch->th_seq_num + 1, header->th_seq_num);
1091
1092			return;
1093		}
1094		grp->out_of_sequence = 0;
1095		ch->th_seq_num = header->th_seq_num;
1096
1097		CTCM_PR_DBGDATA("ctcmpc: %s() FromVTAM_th_seq=%08x\n",
1098					__func__, ch->th_seq_num);
1099
1100		if (unlikely(fsm_getstate(grp->fsm) != MPCG_STATE_READY))
1101					goto done;
1102		while ((pskb->len > 0) && !pdu_last_seen) {
1103			curr_pdu = (struct pdu *)pskb->data;
1104
1105			CTCM_PR_DBGDATA("%s: pdu_header\n", __func__);
1106			CTCM_D3_DUMP((char *)pskb->data, PDU_HEADER_LENGTH);
1107			CTCM_PR_DBGDATA("%s: pskb len: %04x \n",
1108						__func__, pskb->len);
1109
1110			skb_pull(pskb, PDU_HEADER_LENGTH);
1111
1112			if (curr_pdu->pdu_flag & PDU_LAST)
1113				pdu_last_seen = 1;
1114			if (curr_pdu->pdu_flag & PDU_CNTL)
1115				pskb->protocol = htons(ETH_P_SNAP);
1116			else
1117				pskb->protocol = htons(ETH_P_SNA_DIX);
1118
1119			if ((pskb->len <= 0) || (pskb->len > ch->max_bufsize)) {
1120				CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
1121					"%s(%s): Dropping packet with "
1122					"illegal siize %d",
1123					CTCM_FUNTAIL, dev->name, pskb->len);
1124
1125				priv->stats.rx_dropped++;
1126				priv->stats.rx_length_errors++;
1127					goto done;
1128			}
1129			skb_reset_mac_header(pskb);
1130			new_len = curr_pdu->pdu_offset;
1131			CTCM_PR_DBGDATA("%s: new_len: %04x \n",
1132						__func__, new_len);
1133			if ((new_len == 0) || (new_len > pskb->len)) {
1134				/* should never happen		    */
1135				/* pskb len must be hosed...bail out */
1136				CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
1137					"%s(%s): non valid pdu_offset: %04x",
1138					/* "data may be lost", */
1139					CTCM_FUNTAIL, dev->name, new_len);
1140				goto done;
1141			}
1142			skb = __dev_alloc_skb(new_len+4, GFP_ATOMIC);
1143
1144			if (!skb) {
1145				CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
1146					"%s(%s): MEMORY allocation error",
1147						CTCM_FUNTAIL, dev->name);
1148				priv->stats.rx_dropped++;
1149				fsm_event(grp->fsm, MPCG_EVENT_INOP, dev);
1150						goto done;
1151			}
1152			memcpy(skb_put(skb, new_len), pskb->data, new_len);
1153
1154			skb_reset_mac_header(skb);
1155			skb->dev = pskb->dev;
1156			skb->protocol = pskb->protocol;
1157			skb->ip_summed = CHECKSUM_UNNECESSARY;
1158			*((__u32 *) skb_push(skb, 4)) = ch->pdu_seq;
1159			ch->pdu_seq++;
1160
1161			if (do_debug_data) {
1162				ctcm_pr_debug("%s: ToDCM_pdu_seq= %08x\n",
1163						__func__, ch->pdu_seq);
1164				ctcm_pr_debug("%s: skb:%0lx "
1165					"skb len: %d \n", __func__,
1166					(unsigned long)skb, skb->len);
1167				ctcm_pr_debug("%s: up to 32 bytes "
1168					"of pdu_data sent\n", __func__);
1169				ctcmpc_dump32((char *)skb->data, skb->len);
1170			}
1171
1172			skblen = skb->len;
1173			sendrc = netif_rx(skb);
1174			priv->stats.rx_packets++;
1175			priv->stats.rx_bytes += skblen;
1176			skb_pull(pskb, new_len); /* point to next PDU */
1177		}
1178	} else {
1179		mpcginfo = kmalloc(sizeof(struct mpcg_info), gfp_type());
1180		if (mpcginfo == NULL)
1181					goto done;
1182
1183		mpcginfo->ch = ch;
1184		mpcginfo->th = header;
1185		mpcginfo->skb = pskb;
1186		CTCM_PR_DEBUG("%s: Not PDU - may be control pkt\n",
1187					__func__);
1188		/*  it's a sweep?   */
1189		sweep = (struct th_sweep *)pskb->data;
1190		mpcginfo->sweep = sweep;
1191		if (header->th_ch_flag == TH_SWEEP_REQ)
1192			mpc_rcvd_sweep_req(mpcginfo);
1193		else if (header->th_ch_flag == TH_SWEEP_RESP)
1194			mpc_rcvd_sweep_resp(mpcginfo);
1195		else if (header->th_blk_flag == TH_DATA_IS_XID) {
1196			struct xid2 *thisxid = (struct xid2 *)pskb->data;
1197			skb_pull(pskb, XID2_LENGTH);
1198			mpcginfo->xid = thisxid;
1199			fsm_event(grp->fsm, MPCG_EVENT_XID2, mpcginfo);
1200		} else if (header->th_blk_flag == TH_DISCONTACT)
1201			fsm_event(grp->fsm, MPCG_EVENT_DISCONC, mpcginfo);
1202		else if (header->th_seq_num != 0) {
1203			CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
1204				"%s(%s): control pkt expected\n",
1205						CTCM_FUNTAIL, dev->name);
1206			priv->stats.rx_dropped++;
1207			/* mpcginfo only used for non-data transfers */
1208			kfree(mpcginfo);
1209			if (do_debug_data)
1210				ctcmpc_dump_skb(pskb, -8);
1211		}
1212	}
1213done:
1214
1215	dev_kfree_skb_any(pskb);
1216	if (sendrc == NET_RX_DROP) {
1217		dev_warn(&dev->dev,
1218			"The network backlog for %s is exceeded, "
1219			"package dropped\n", __func__);
1220		fsm_event(grp->fsm, MPCG_EVENT_INOP, dev);
1221	}
1222
1223	CTCM_PR_DEBUG("exit %s: %s: ch=0x%p id=%s\n",
1224			__func__, dev->name, ch, ch->id);
1225}
1226
1227/**
1228 * tasklet helper for mpc's skb unpacking.
1229 *
1230 * ch		The channel to work on.
1231 * Allow flow control back pressure to occur here.
1232 * Throttling back channel can result in excessive
1233 * channel inactivity and system deact of channel
1234 */
1235void ctcmpc_bh(unsigned long thischan)
1236{
1237	struct channel	  *ch	= (struct channel *)thischan;
1238	struct sk_buff	  *skb;
1239	struct net_device *dev	= ch->netdev;
1240	struct ctcm_priv  *priv	= dev->ml_priv;
1241	struct mpc_group  *grp	= priv->mpcg;
1242
1243	CTCM_PR_DEBUG("%s cp:%i enter:  %s() %s\n",
1244	       dev->name, smp_processor_id(), __func__, ch->id);
1245	/* caller has requested driver to throttle back */
1246	while ((fsm_getstate(grp->fsm) != MPCG_STATE_FLOWC) &&
1247			(skb = skb_dequeue(&ch->io_queue))) {
1248		ctcmpc_unpack_skb(ch, skb);
1249		if (grp->out_of_sequence > 20) {
1250			/* assume data loss has occurred if */
1251			/* missing seq_num for extended     */
1252			/* period of time		    */
1253			grp->out_of_sequence = 0;
1254			fsm_event(grp->fsm, MPCG_EVENT_INOP, dev);
1255			break;
1256		}
1257		if (skb == skb_peek(&ch->io_queue))
1258			break;
1259	}
1260	CTCM_PR_DEBUG("exit %s: %s: ch=0x%p id=%s\n",
1261			__func__, dev->name, ch, ch->id);
1262	return;
1263}
1264
1265/*
1266 *  MPC Group Initializations
1267 */
1268struct mpc_group *ctcmpc_init_mpc_group(struct ctcm_priv *priv)
1269{
1270	struct mpc_group *grp;
1271
1272	CTCM_DBF_TEXT_(MPC_SETUP, CTC_DBF_INFO,
1273			"Enter %s(%p)", CTCM_FUNTAIL, priv);
1274
1275	grp = kzalloc(sizeof(struct mpc_group), GFP_KERNEL);
1276	if (grp == NULL)
1277		return NULL;
1278
1279	grp->fsm = init_fsm("mpcg", mpcg_state_names, mpcg_event_names,
1280			MPCG_NR_STATES, MPCG_NR_EVENTS, mpcg_fsm,
1281			mpcg_fsm_len, GFP_KERNEL);
1282	if (grp->fsm == NULL) {
1283		kfree(grp);
1284		return NULL;
1285	}
1286
1287	fsm_newstate(grp->fsm, MPCG_STATE_RESET);
1288	fsm_settimer(grp->fsm, &grp->timer);
1289
1290	grp->xid_skb =
1291		 __dev_alloc_skb(MPC_BUFSIZE_DEFAULT, GFP_ATOMIC | GFP_DMA);
1292	if (grp->xid_skb == NULL) {
1293		kfree_fsm(grp->fsm);
1294		kfree(grp);
1295		return NULL;
1296	}
1297	/*  base xid for all channels in group  */
1298	grp->xid_skb_data = grp->xid_skb->data;
1299	grp->xid_th = (struct th_header *)grp->xid_skb->data;
1300	memcpy(skb_put(grp->xid_skb, TH_HEADER_LENGTH),
1301			&thnorm, TH_HEADER_LENGTH);
1302
1303	grp->xid = (struct xid2 *)skb_tail_pointer(grp->xid_skb);
1304	memcpy(skb_put(grp->xid_skb, XID2_LENGTH), &init_xid, XID2_LENGTH);
1305	grp->xid->xid2_adj_id = jiffies | 0xfff00000;
1306	grp->xid->xid2_sender_id = jiffies;
1307
1308	grp->xid_id = skb_tail_pointer(grp->xid_skb);
1309	memcpy(skb_put(grp->xid_skb, 4), "VTAM", 4);
1310
1311	grp->rcvd_xid_skb =
1312		__dev_alloc_skb(MPC_BUFSIZE_DEFAULT, GFP_ATOMIC|GFP_DMA);
1313	if (grp->rcvd_xid_skb == NULL) {
1314		kfree_fsm(grp->fsm);
1315		dev_kfree_skb(grp->xid_skb);
1316		kfree(grp);
1317		return NULL;
1318	}
1319	grp->rcvd_xid_data = grp->rcvd_xid_skb->data;
1320	grp->rcvd_xid_th = (struct th_header *)grp->rcvd_xid_skb->data;
1321	memcpy(skb_put(grp->rcvd_xid_skb, TH_HEADER_LENGTH),
1322			&thnorm, TH_HEADER_LENGTH);
1323	grp->saved_xid2 = NULL;
1324	priv->xid = grp->xid;
1325	priv->mpcg = grp;
1326	return grp;
1327}
1328
1329/*
1330 * The MPC Group Station FSM
1331 */
1332
1333/*
1334 * MPC Group Station FSM actions
1335 * CTCM_PROTO_MPC only
1336 */
1337
1338/**
1339 * NOP action for statemachines
1340 */
1341static void mpc_action_nop(fsm_instance *fi, int event, void *arg)
1342{
1343}
1344
1345/*
1346 * invoked when the device transitions to dev_stopped
1347 * MPC will stop each individual channel if a single XID failure
1348 * occurs, or will intitiate all channels be stopped if a GROUP
1349 * level failure occurs.
1350 */
1351static void mpc_action_go_inop(fsm_instance *fi, int event, void *arg)
1352{
1353	struct net_device  *dev = arg;
1354	struct ctcm_priv    *priv;
1355	struct mpc_group *grp;
1356	struct channel *wch;
1357
1358	CTCM_PR_DEBUG("Enter %s: %s\n",	__func__, dev->name);
1359
1360	priv  = dev->ml_priv;
1361	grp =  priv->mpcg;
1362	grp->flow_off_called = 0;
1363	fsm_deltimer(&grp->timer);
1364	if (grp->channels_terminating)
1365			return;
1366
1367	grp->channels_terminating = 1;
1368	grp->saved_state = fsm_getstate(grp->fsm);
1369	fsm_newstate(grp->fsm, MPCG_STATE_INOP);
1370	if (grp->saved_state > MPCG_STATE_XID7INITF)
1371		CTCM_DBF_TEXT_(MPC_TRACE, CTC_DBF_NOTICE,
1372			"%s(%s): MPC GROUP INOPERATIVE",
1373				CTCM_FUNTAIL, dev->name);
1374	if ((grp->saved_state != MPCG_STATE_RESET) ||
1375		/* dealloc_channel has been called */
1376		(grp->port_persist == 0))
1377		fsm_deltimer(&priv->restart_timer);
1378
1379	wch = priv->channel[CTCM_WRITE];
1380
1381	switch (grp->saved_state) {
1382	case MPCG_STATE_RESET:
1383	case MPCG_STATE_INOP:
1384	case MPCG_STATE_XID2INITW:
1385	case MPCG_STATE_XID0IOWAIT:
1386	case MPCG_STATE_XID2INITX:
1387	case MPCG_STATE_XID7INITW:
1388	case MPCG_STATE_XID7INITX:
1389	case MPCG_STATE_XID0IOWAIX:
1390	case MPCG_STATE_XID7INITI:
1391	case MPCG_STATE_XID7INITZ:
1392	case MPCG_STATE_XID7INITF:
1393		break;
1394	case MPCG_STATE_FLOWC:
1395	case MPCG_STATE_READY:
1396	default:
1397		tasklet_hi_schedule(&wch->ch_disc_tasklet);
1398	}
1399
1400	grp->xid2_tgnum = 0;
1401	grp->group_max_buflen = 0;  /*min of all received */
1402	grp->outstanding_xid2 = 0;
1403	grp->outstanding_xid7 = 0;
1404	grp->outstanding_xid7_p2 = 0;
1405	grp->saved_xid2 = NULL;
1406	grp->xidnogood = 0;
1407	grp->changed_side = 0;
1408
1409	grp->rcvd_xid_skb->data = grp->rcvd_xid_data;
1410	skb_reset_tail_pointer(grp->rcvd_xid_skb);
1411	grp->rcvd_xid_skb->len = 0;
1412	grp->rcvd_xid_th = (struct th_header *)grp->rcvd_xid_skb->data;
1413	memcpy(skb_put(grp->rcvd_xid_skb, TH_HEADER_LENGTH), &thnorm,
1414	       TH_HEADER_LENGTH);
1415
1416	if (grp->send_qllc_disc == 1) {
1417		grp->send_qllc_disc = 0;
1418		mpc_send_qllc_discontact(dev);
1419	}
1420
1421	/* DO NOT issue DEV_EVENT_STOP directly out of this code */
1422	/* This can result in INOP of VTAM PU due to halting of  */
1423	/* outstanding IO which causes a sense to be returned	 */
1424	/* Only about 3 senses are allowed and then IOS/VTAM will*/
1425	/* become unreachable without manual intervention	 */
1426	if ((grp->port_persist == 1) || (grp->alloc_called)) {
1427		grp->alloc_called = 0;
1428		fsm_deltimer(&priv->restart_timer);
1429		fsm_addtimer(&priv->restart_timer, 500, DEV_EVENT_RESTART, dev);
1430		fsm_newstate(grp->fsm, MPCG_STATE_RESET);
1431		if (grp->saved_state > MPCG_STATE_XID7INITF)
1432			CTCM_DBF_TEXT_(MPC_TRACE, CTC_DBF_ALWAYS,
1433				"%s(%s): MPC GROUP RECOVERY SCHEDULED",
1434					CTCM_FUNTAIL, dev->name);
1435	} else {
1436		fsm_deltimer(&priv->restart_timer);
1437		fsm_addtimer(&priv->restart_timer, 500, DEV_EVENT_STOP, dev);
1438		fsm_newstate(grp->fsm, MPCG_STATE_RESET);
1439		CTCM_DBF_TEXT_(MPC_TRACE, CTC_DBF_ALWAYS,
1440			"%s(%s): NO MPC GROUP RECOVERY ATTEMPTED",
1441						CTCM_FUNTAIL, dev->name);
1442	}
1443}
1444
1445/**
1446 * Handle mpc group  action timeout.
1447 * MPC Group Station FSM action
1448 * CTCM_PROTO_MPC only
1449 *
1450 * fi		An instance of an mpc_group fsm.
1451 * event	The event, just happened.
1452 * arg		Generic pointer, casted from net_device * upon call.
1453 */
1454static void mpc_action_timeout(fsm_instance *fi, int event, void *arg)
1455{
1456	struct net_device *dev = arg;
1457	struct ctcm_priv *priv;
1458	struct mpc_group *grp;
1459	struct channel *wch;
1460	struct channel *rch;
1461
1462	priv = dev->ml_priv;
1463	grp = priv->mpcg;
1464	wch = priv->channel[CTCM_WRITE];
1465	rch = priv->channel[CTCM_READ];
1466
1467	switch (fsm_getstate(grp->fsm)) {
1468	case MPCG_STATE_XID2INITW:
1469		/* Unless there is outstanding IO on the  */
1470		/* channel just return and wait for ATTN  */
1471		/* interrupt to begin XID negotiations	  */
1472		if ((fsm_getstate(rch->fsm) == CH_XID0_PENDING) &&
1473		   (fsm_getstate(wch->fsm) == CH_XID0_PENDING))
1474			break;
1475	default:
1476		fsm_event(grp->fsm, MPCG_EVENT_INOP, dev);
1477	}
1478
1479	CTCM_DBF_TEXT_(MPC_TRACE, CTC_DBF_DEBUG,
1480			"%s: dev=%s exit",
1481			CTCM_FUNTAIL, dev->name);
1482	return;
1483}
1484
1485/*
1486 * MPC Group Station FSM action
1487 * CTCM_PROTO_MPC only
1488 */
1489void mpc_action_discontact(fsm_instance *fi, int event, void *arg)
1490{
1491	struct mpcg_info   *mpcginfo   = arg;
1492	struct channel	   *ch	       = mpcginfo->ch;
1493	struct net_device  *dev;
1494	struct ctcm_priv   *priv;
1495	struct mpc_group   *grp;
1496
1497	if (ch) {
1498		dev = ch->netdev;
1499		if (dev) {
1500			priv = dev->ml_priv;
1501			if (priv) {
1502				CTCM_DBF_TEXT_(MPC_TRACE, CTC_DBF_NOTICE,
1503					"%s: %s: %s\n",
1504					CTCM_FUNTAIL, dev->name, ch->id);
1505				grp = priv->mpcg;
1506				grp->send_qllc_disc = 1;
1507				fsm_event(grp->fsm, MPCG_EVENT_INOP, dev);
1508			}
1509		}
1510	}
1511
1512	return;
1513}
1514
1515/*
1516 * MPC Group Station - not part of FSM
1517 * CTCM_PROTO_MPC only
1518 * called from add_channel in ctcm_main.c
1519 */
1520void mpc_action_send_discontact(unsigned long thischan)
1521{
1522	int rc;
1523	struct channel	*ch = (struct channel *)thischan;
1524	unsigned long	saveflags = 0;
1525
1526	spin_lock_irqsave(get_ccwdev_lock(ch->cdev), saveflags);
1527	rc = ccw_device_start(ch->cdev, &ch->ccw[15],
1528					(unsigned long)ch, 0xff, 0);
1529	spin_unlock_irqrestore(get_ccwdev_lock(ch->cdev), saveflags);
1530
1531	if (rc != 0) {
1532		ctcm_ccw_check_rc(ch, rc, (char *)__func__);
1533	}
1534
1535	return;
1536}
1537
1538
1539/*
1540 * helper function of mpc FSM
1541 * CTCM_PROTO_MPC only
1542 * mpc_action_rcvd_xid7
1543*/
1544static int mpc_validate_xid(struct mpcg_info *mpcginfo)
1545{
1546	struct channel	   *ch	 = mpcginfo->ch;
1547	struct net_device  *dev  = ch->netdev;
1548	struct ctcm_priv   *priv = dev->ml_priv;
1549	struct mpc_group   *grp  = priv->mpcg;
1550	struct xid2	   *xid  = mpcginfo->xid;
1551	int	rc	 = 0;
1552	__u64	our_id   = 0;
1553	__u64   their_id = 0;
1554	int	len = TH_HEADER_LENGTH + PDU_HEADER_LENGTH;
1555
1556	CTCM_PR_DEBUG("Enter %s: xid=%p\n", __func__, xid);
1557
1558	if (xid == NULL) {
1559		rc = 1;
1560		/* XID REJECTED: xid == NULL */
1561		CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
1562			"%s(%s): xid = NULL",
1563				CTCM_FUNTAIL, ch->id);
1564			goto done;
1565	}
1566
1567	CTCM_D3_DUMP((char *)xid, XID2_LENGTH);
1568
1569	/*the received direction should be the opposite of ours  */
1570	if (((CHANNEL_DIRECTION(ch->flags) == CTCM_READ) ? XID2_WRITE_SIDE :
1571				XID2_READ_SIDE) != xid->xid2_dlc_type) {
1572		rc = 2;
1573		/* XID REJECTED: r/w channel pairing mismatch */
1574		CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
1575			"%s(%s): r/w channel pairing mismatch",
1576				CTCM_FUNTAIL, ch->id);
1577			goto done;
1578	}
1579
1580	if (xid->xid2_dlc_type == XID2_READ_SIDE) {
1581		CTCM_PR_DEBUG("%s: grpmaxbuf:%d xid2buflen:%d\n", __func__,
1582				grp->group_max_buflen, xid->xid2_buf_len);
1583
1584		if (grp->group_max_buflen == 0 || grp->group_max_buflen >
1585						xid->xid2_buf_len - len)
1586			grp->group_max_buflen = xid->xid2_buf_len - len;
1587	}
1588
1589	if (grp->saved_xid2 == NULL) {
1590		grp->saved_xid2 =
1591			(struct xid2 *)skb_tail_pointer(grp->rcvd_xid_skb);
1592
1593		memcpy(skb_put(grp->rcvd_xid_skb,
1594					XID2_LENGTH), xid, XID2_LENGTH);
1595		grp->rcvd_xid_skb->data = grp->rcvd_xid_data;
1596
1597		skb_reset_tail_pointer(grp->rcvd_xid_skb);
1598		grp->rcvd_xid_skb->len = 0;
1599
1600		/* convert two 32 bit numbers into 1 64 bit for id compare */
1601		our_id = (__u64)priv->xid->xid2_adj_id;
1602		our_id = our_id << 32;
1603		our_id = our_id + priv->xid->xid2_sender_id;
1604		their_id = (__u64)xid->xid2_adj_id;
1605		their_id = their_id << 32;
1606		their_id = their_id + xid->xid2_sender_id;
1607		/* lower id assume the xside role */
1608		if (our_id < their_id) {
1609			grp->roll = XSIDE;
1610			CTCM_DBF_TEXT_(MPC_TRACE, CTC_DBF_NOTICE,
1611				"%s(%s): WE HAVE LOW ID - TAKE XSIDE",
1612					CTCM_FUNTAIL, ch->id);
1613		} else {
1614			grp->roll = YSIDE;
1615			CTCM_DBF_TEXT_(MPC_TRACE, CTC_DBF_NOTICE,
1616				"%s(%s): WE HAVE HIGH ID - TAKE YSIDE",
1617					CTCM_FUNTAIL, ch->id);
1618		}
1619
1620	} else {
1621		if (xid->xid2_flag4 != grp->saved_xid2->xid2_flag4) {
1622			rc = 3;
1623			/* XID REJECTED: xid flag byte4 mismatch */
1624			CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
1625				"%s(%s): xid flag byte4 mismatch",
1626					CTCM_FUNTAIL, ch->id);
1627		}
1628		if (xid->xid2_flag2 == 0x40) {
1629			rc = 4;
1630			/* XID REJECTED - xid NOGOOD */
1631			CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
1632				"%s(%s): xid NOGOOD",
1633					CTCM_FUNTAIL, ch->id);
1634		}
1635		if (xid->xid2_adj_id != grp->saved_xid2->xid2_adj_id) {
1636			rc = 5;
1637			/* XID REJECTED - Adjacent Station ID Mismatch */
1638			CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
1639				"%s(%s): Adjacent Station ID Mismatch",
1640					CTCM_FUNTAIL, ch->id);
1641		}
1642		if (xid->xid2_sender_id != grp->saved_xid2->xid2_sender_id) {
1643			rc = 6;
1644			/* XID REJECTED - Sender Address Mismatch */
1645			CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
1646				"%s(%s): Sender Address Mismatch",
1647					CTCM_FUNTAIL, ch->id);
1648		}
1649	}
1650done:
1651	if (rc) {
1652		dev_warn(&dev->dev,
1653			"The XID used in the MPC protocol is not valid, "
1654			"rc = %d\n", rc);
1655		priv->xid->xid2_flag2 = 0x40;
1656		grp->saved_xid2->xid2_flag2 = 0x40;
1657	}
1658
1659	return rc;
1660}
1661
1662/*
1663 * MPC Group Station FSM action
1664 * CTCM_PROTO_MPC only
1665 */
1666static void mpc_action_side_xid(fsm_instance *fsm, void *arg, int side)
1667{
1668	struct channel *ch = arg;
1669	int rc = 0;
1670	int gotlock = 0;
1671	unsigned long saveflags = 0;	/* avoids compiler warning with
1672					   spin_unlock_irqrestore */
1673
1674	CTCM_PR_DEBUG("Enter %s: cp=%i ch=0x%p id=%s\n",
1675			__func__, smp_processor_id(), ch, ch->id);
1676
1677	if (ctcm_checkalloc_buffer(ch))
1678					goto done;
1679
1680	/*
1681	 * skb data-buffer referencing:
1682	 */
1683	ch->trans_skb->data = ch->trans_skb_data;
1684	skb_reset_tail_pointer(ch->trans_skb);
1685	ch->trans_skb->len = 0;
1686	/* result of the previous 3 statements is NOT always
1687	 * already set after ctcm_checkalloc_buffer
1688	 * because of possible reuse of the trans_skb
1689	 */
1690	memset(ch->trans_skb->data, 0, 16);
1691	ch->rcvd_xid_th =  (struct th_header *)ch->trans_skb_data;
1692	/* check is main purpose here: */
1693	skb_put(ch->trans_skb, TH_HEADER_LENGTH);
1694	ch->rcvd_xid = (struct xid2 *)skb_tail_pointer(ch->trans_skb);
1695	/* check is main purpose here: */
1696	skb_put(ch->trans_skb, XID2_LENGTH);
1697	ch->rcvd_xid_id = skb_tail_pointer(ch->trans_skb);
1698	/* cleanup back to startpoint */
1699	ch->trans_skb->data = ch->trans_skb_data;
1700	skb_reset_tail_pointer(ch->trans_skb);
1701	ch->trans_skb->len = 0;
1702
1703	/* non-checking rewrite of above skb data-buffer referencing: */
1704	/*
1705	memset(ch->trans_skb->data, 0, 16);
1706	ch->rcvd_xid_th =  (struct th_header *)ch->trans_skb_data;
1707	ch->rcvd_xid = (struct xid2 *)(ch->trans_skb_data + TH_HEADER_LENGTH);
1708	ch->rcvd_xid_id = ch->trans_skb_data + TH_HEADER_LENGTH + XID2_LENGTH;
1709	 */
1710
1711	ch->ccw[8].flags	= CCW_FLAG_SLI | CCW_FLAG_CC;
1712	ch->ccw[8].count	= 0;
1713	ch->ccw[8].cda		= 0x00;
1714
1715	if (!(ch->xid_th && ch->xid && ch->xid_id))
1716		CTCM_DBF_TEXT_(MPC_TRACE, CTC_DBF_INFO,
1717			"%s(%s): xid_th=%p, xid=%p, xid_id=%p",
1718			CTCM_FUNTAIL, ch->id, ch->xid_th, ch->xid, ch->xid_id);
1719
1720	if (side == XSIDE) {
1721		/* mpc_action_xside_xid */
1722		if (ch->xid_th == NULL)
1723				goto done;
1724		ch->ccw[9].cmd_code	= CCW_CMD_WRITE;
1725		ch->ccw[9].flags	= CCW_FLAG_SLI | CCW_FLAG_CC;
1726		ch->ccw[9].count	= TH_HEADER_LENGTH;
1727		ch->ccw[9].cda		= virt_to_phys(ch->xid_th);
1728
1729		if (ch->xid == NULL)
1730				goto done;
1731		ch->ccw[10].cmd_code	= CCW_CMD_WRITE;
1732		ch->ccw[10].flags	= CCW_FLAG_SLI | CCW_FLAG_CC;
1733		ch->ccw[10].count	= XID2_LENGTH;
1734		ch->ccw[10].cda		= virt_to_phys(ch->xid);
1735
1736		ch->ccw[11].cmd_code	= CCW_CMD_READ;
1737		ch->ccw[11].flags	= CCW_FLAG_SLI | CCW_FLAG_CC;
1738		ch->ccw[11].count	= TH_HEADER_LENGTH;
1739		ch->ccw[11].cda		= virt_to_phys(ch->rcvd_xid_th);
1740
1741		ch->ccw[12].cmd_code	= CCW_CMD_READ;
1742		ch->ccw[12].flags	= CCW_FLAG_SLI | CCW_FLAG_CC;
1743		ch->ccw[12].count	= XID2_LENGTH;
1744		ch->ccw[12].cda		= virt_to_phys(ch->rcvd_xid);
1745
1746		ch->ccw[13].cmd_code	= CCW_CMD_READ;
1747		ch->ccw[13].cda		= virt_to_phys(ch->rcvd_xid_id);
1748
1749	} else { /* side == YSIDE : mpc_action_yside_xid */
1750		ch->ccw[9].cmd_code	= CCW_CMD_READ;
1751		ch->ccw[9].flags	= CCW_FLAG_SLI | CCW_FLAG_CC;
1752		ch->ccw[9].count	= TH_HEADER_LENGTH;
1753		ch->ccw[9].cda		= virt_to_phys(ch->rcvd_xid_th);
1754
1755		ch->ccw[10].cmd_code	= CCW_CMD_READ;
1756		ch->ccw[10].flags	= CCW_FLAG_SLI | CCW_FLAG_CC;
1757		ch->ccw[10].count	= XID2_LENGTH;
1758		ch->ccw[10].cda		= virt_to_phys(ch->rcvd_xid);
1759
1760		if (ch->xid_th == NULL)
1761				goto done;
1762		ch->ccw[11].cmd_code	= CCW_CMD_WRITE;
1763		ch->ccw[11].flags	= CCW_FLAG_SLI | CCW_FLAG_CC;
1764		ch->ccw[11].count	= TH_HEADER_LENGTH;
1765		ch->ccw[11].cda		= virt_to_phys(ch->xid_th);
1766
1767		if (ch->xid == NULL)
1768				goto done;
1769		ch->ccw[12].cmd_code	= CCW_CMD_WRITE;
1770		ch->ccw[12].flags	= CCW_FLAG_SLI | CCW_FLAG_CC;
1771		ch->ccw[12].count	= XID2_LENGTH;
1772		ch->ccw[12].cda		= virt_to_phys(ch->xid);
1773
1774		if (ch->xid_id == NULL)
1775				goto done;
1776		ch->ccw[13].cmd_code	= CCW_CMD_WRITE;
1777		ch->ccw[13].cda		= virt_to_phys(ch->xid_id);
1778
1779	}
1780	ch->ccw[13].flags	= CCW_FLAG_SLI | CCW_FLAG_CC;
1781	ch->ccw[13].count	= 4;
1782
1783	ch->ccw[14].cmd_code	= CCW_CMD_NOOP;
1784	ch->ccw[14].flags	= CCW_FLAG_SLI;
1785	ch->ccw[14].count	= 0;
1786	ch->ccw[14].cda		= 0;
1787
1788	CTCM_CCW_DUMP((char *)&ch->ccw[8], sizeof(struct ccw1) * 7);
1789	CTCM_D3_DUMP((char *)ch->xid_th, TH_HEADER_LENGTH);
1790	CTCM_D3_DUMP((char *)ch->xid, XID2_LENGTH);
1791	CTCM_D3_DUMP((char *)ch->xid_id, 4);
1792
1793	if (!in_irq()) {
1794			 /* Such conditional locking is a known problem for
1795			  * sparse because its static undeterministic.
1796			  * Warnings should be ignored here. */
1797		spin_lock_irqsave(get_ccwdev_lock(ch->cdev), saveflags);
1798		gotlock = 1;
1799	}
1800
1801	fsm_addtimer(&ch->timer, 5000 , CTC_EVENT_TIMER, ch);
1802	rc = ccw_device_start(ch->cdev, &ch->ccw[8],
1803				(unsigned long)ch, 0xff, 0);
1804
1805	if (gotlock)	/* see remark above about conditional locking */
1806		spin_unlock_irqrestore(get_ccwdev_lock(ch->cdev), saveflags);
1807
1808	if (rc != 0) {
1809		ctcm_ccw_check_rc(ch, rc,
1810				(side == XSIDE) ? "x-side XID" : "y-side XID");
1811	}
1812
1813done:
1814	CTCM_PR_DEBUG("Exit %s: ch=0x%p id=%s\n",
1815				__func__, ch, ch->id);
1816	return;
1817
1818}
1819
1820/*
1821 * MPC Group Station FSM action
1822 * CTCM_PROTO_MPC only
1823 */
1824static void mpc_action_xside_xid(fsm_instance *fsm, int event, void *arg)
1825{
1826	mpc_action_side_xid(fsm, arg, XSIDE);
1827}
1828
1829/*
1830 * MPC Group Station FSM action
1831 * CTCM_PROTO_MPC only
1832 */
1833static void mpc_action_yside_xid(fsm_instance *fsm, int event, void *arg)
1834{
1835	mpc_action_side_xid(fsm, arg, YSIDE);
1836}
1837
1838/*
1839 * MPC Group Station FSM action
1840 * CTCM_PROTO_MPC only
1841 */
1842static void mpc_action_doxid0(fsm_instance *fsm, int event, void *arg)
1843{
1844	struct channel	   *ch   = arg;
1845	struct net_device  *dev  = ch->netdev;
1846	struct ctcm_priv   *priv = dev->ml_priv;
1847	struct mpc_group   *grp  = priv->mpcg;
1848
1849	CTCM_PR_DEBUG("Enter %s: cp=%i ch=0x%p id=%s\n",
1850			__func__, smp_processor_id(), ch, ch->id);
1851
1852	if (ch->xid == NULL) {
1853		CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
1854			"%s(%s): ch->xid == NULL",
1855				CTCM_FUNTAIL, dev->name);
1856		return;
1857	}
1858
1859	fsm_newstate(ch->fsm, CH_XID0_INPROGRESS);
1860
1861	ch->xid->xid2_option =	XID2_0;
1862
1863	switch (fsm_getstate(grp->fsm)) {
1864	case MPCG_STATE_XID2INITW:
1865	case MPCG_STATE_XID2INITX:
1866		ch->ccw[8].cmd_code = CCW_CMD_SENSE_CMD;
1867		break;
1868	case MPCG_STATE_XID0IOWAIT:
1869	case MPCG_STATE_XID0IOWAIX:
1870		ch->ccw[8].cmd_code = CCW_CMD_WRITE_CTL;
1871		break;
1872	}
1873
1874	fsm_event(grp->fsm, MPCG_EVENT_DOIO, ch);
1875
1876	return;
1877}
1878
1879/*
1880 * MPC Group Station FSM action
1881 * CTCM_PROTO_MPC only
1882*/
1883static void mpc_action_doxid7(fsm_instance *fsm, int event, void *arg)
1884{
1885	struct net_device *dev = arg;
1886	struct ctcm_priv  *priv = dev->ml_priv;
1887	struct mpc_group  *grp  = NULL;
1888	int direction;
1889	int send = 0;
1890
1891	if (priv)
1892		grp = priv->mpcg;
1893	if (grp == NULL)
1894		return;
1895
1896	for (direction = CTCM_READ; direction <= CTCM_WRITE; direction++) {
1897		struct channel *ch = priv->channel[direction];
1898		struct xid2 *thisxid = ch->xid;
1899		ch->xid_skb->data = ch->xid_skb_data;
1900		skb_reset_tail_pointer(ch->xid_skb);
1901		ch->xid_skb->len = 0;
1902		thisxid->xid2_option = XID2_7;
1903		send = 0;
1904
1905		/* xid7 phase 1 */
1906		if (grp->outstanding_xid7_p2 > 0) {
1907			if (grp->roll == YSIDE) {
1908				if (fsm_getstate(ch->fsm) == CH_XID7_PENDING1) {
1909					fsm_newstate(ch->fsm, CH_XID7_PENDING2);
1910					ch->ccw[8].cmd_code = CCW_CMD_SENSE_CMD;
1911					memcpy(skb_put(ch->xid_skb,
1912							TH_HEADER_LENGTH),
1913					       &thdummy, TH_HEADER_LENGTH);
1914					send = 1;
1915				}
1916			} else if (fsm_getstate(ch->fsm) < CH_XID7_PENDING2) {
1917					fsm_newstate(ch->fsm, CH_XID7_PENDING2);
1918					ch->ccw[8].cmd_code = CCW_CMD_WRITE_CTL;
1919					memcpy(skb_put(ch->xid_skb,
1920						       TH_HEADER_LENGTH),
1921					       &thnorm, TH_HEADER_LENGTH);
1922					send = 1;
1923			}
1924		} else {
1925			/* xid7 phase 2 */
1926			if (grp->roll == YSIDE) {
1927				if (fsm_getstate(ch->fsm) < CH_XID7_PENDING4) {
1928					fsm_newstate(ch->fsm, CH_XID7_PENDING4);
1929					memcpy(skb_put(ch->xid_skb,
1930						       TH_HEADER_LENGTH),
1931					       &thnorm, TH_HEADER_LENGTH);
1932					ch->ccw[8].cmd_code = CCW_CMD_WRITE_CTL;
1933					send = 1;
1934				}
1935			} else if (fsm_getstate(ch->fsm) == CH_XID7_PENDING3) {
1936				fsm_newstate(ch->fsm, CH_XID7_PENDING4);
1937				ch->ccw[8].cmd_code = CCW_CMD_SENSE_CMD;
1938				memcpy(skb_put(ch->xid_skb, TH_HEADER_LENGTH),
1939						&thdummy, TH_HEADER_LENGTH);
1940				send = 1;
1941			}
1942		}
1943
1944		if (send)
1945			fsm_event(grp->fsm, MPCG_EVENT_DOIO, ch);
1946	}
1947
1948	return;
1949}
1950
1951/*
1952 * MPC Group Station FSM action
1953 * CTCM_PROTO_MPC only
1954 */
1955static void mpc_action_rcvd_xid0(fsm_instance *fsm, int event, void *arg)
1956{
1957
1958	struct mpcg_info   *mpcginfo  = arg;
1959	struct channel	   *ch   = mpcginfo->ch;
1960	struct net_device  *dev  = ch->netdev;
1961	struct ctcm_priv   *priv = dev->ml_priv;
1962	struct mpc_group   *grp  = priv->mpcg;
1963
1964	CTCM_PR_DEBUG("%s: ch-id:%s xid2:%i xid7:%i xidt_p2:%i \n",
1965			__func__, ch->id, grp->outstanding_xid2,
1966			grp->outstanding_xid7, grp->outstanding_xid7_p2);
1967
1968	if (fsm_getstate(ch->fsm) < CH_XID7_PENDING)
1969		fsm_newstate(ch->fsm, CH_XID7_PENDING);
1970
1971	grp->outstanding_xid2--;
1972	grp->outstanding_xid7++;
1973	grp->outstanding_xid7_p2++;
1974
1975	/* must change state before validating xid to */
1976	/* properly handle interim interrupts received*/
1977	switch (fsm_getstate(grp->fsm)) {
1978	case MPCG_STATE_XID2INITW:
1979		fsm_newstate(grp->fsm, MPCG_STATE_XID2INITX);
1980		mpc_validate_xid(mpcginfo);
1981		break;
1982	case MPCG_STATE_XID0IOWAIT:
1983		fsm_newstate(grp->fsm, MPCG_STATE_XID0IOWAIX);
1984		mpc_validate_xid(mpcginfo);
1985		break;
1986	case MPCG_STATE_XID2INITX:
1987		if (grp->outstanding_xid2 == 0) {
1988			fsm_newstate(grp->fsm, MPCG_STATE_XID7INITW);
1989			mpc_validate_xid(mpcginfo);
1990			fsm_event(grp->fsm, MPCG_EVENT_XID2DONE, dev);
1991		}
1992		break;
1993	case MPCG_STATE_XID0IOWAIX:
1994		if (grp->outstanding_xid2 == 0) {
1995			fsm_newstate(grp->fsm, MPCG_STATE_XID7INITI);
1996			mpc_validate_xid(mpcginfo);
1997			fsm_event(grp->fsm, MPCG_EVENT_XID2DONE, dev);
1998		}
1999		break;
2000	}
2001	kfree(mpcginfo);
2002
2003	CTCM_PR_DEBUG("ctcmpc:%s() %s xid2:%i xid7:%i xidt_p2:%i \n",
2004		__func__, ch->id, grp->outstanding_xid2,
2005		grp->outstanding_xid7, grp->outstanding_xid7_p2);
2006	CTCM_PR_DEBUG("ctcmpc:%s() %s grpstate: %s chanstate: %s \n",
2007		__func__, ch->id,
2008		fsm_getstate_str(grp->fsm), fsm_getstate_str(ch->fsm));
2009	return;
2010
2011}
2012
2013
2014/*
2015 * MPC Group Station FSM action
2016 * CTCM_PROTO_MPC only
2017 */
2018static void mpc_action_rcvd_xid7(fsm_instance *fsm, int event, void *arg)
2019{
2020	struct mpcg_info   *mpcginfo   = arg;
2021	struct channel	   *ch	       = mpcginfo->ch;
2022	struct net_device  *dev        = ch->netdev;
2023	struct ctcm_priv   *priv    = dev->ml_priv;
2024	struct mpc_group   *grp     = priv->mpcg;
2025
2026	CTCM_PR_DEBUG("Enter %s: cp=%i ch=0x%p id=%s\n",
2027		__func__, smp_processor_id(), ch, ch->id);
2028	CTCM_PR_DEBUG("%s: outstanding_xid7: %i, outstanding_xid7_p2: %i\n",
2029		__func__, grp->outstanding_xid7, grp->outstanding_xid7_p2);
2030
2031	grp->outstanding_xid7--;
2032	ch->xid_skb->data = ch->xid_skb_data;
2033	skb_reset_tail_pointer(ch->xid_skb);
2034	ch->xid_skb->len = 0;
2035
2036	switch (fsm_getstate(grp->fsm)) {
2037	case MPCG_STATE_XID7INITI:
2038		fsm_newstate(grp->fsm, MPCG_STATE_XID7INITZ);
2039		mpc_validate_xid(mpcginfo);
2040		break;
2041	case MPCG_STATE_XID7INITW:
2042		fsm_newstate(grp->fsm, MPCG_STATE_XID7INITX);
2043		mpc_validate_xid(mpcginfo);
2044		break;
2045	case MPCG_STATE_XID7INITZ:
2046	case MPCG_STATE_XID7INITX:
2047		if (grp->outstanding_xid7 == 0) {
2048			if (grp->outstanding_xid7_p2 > 0) {
2049				grp->outstanding_xid7 =
2050					grp->outstanding_xid7_p2;
2051				grp->outstanding_xid7_p2 = 0;
2052			} else
2053				fsm_newstate(grp->fsm, MPCG_STATE_XID7INITF);
2054
2055			mpc_validate_xid(mpcginfo);
2056			fsm_event(grp->fsm, MPCG_EVENT_XID7DONE, dev);
2057			break;
2058		}
2059		mpc_validate_xid(mpcginfo);
2060		break;
2061	}
2062	kfree(mpcginfo);
2063	return;
2064}
2065
2066/*
2067 * mpc_action helper of an MPC Group Station FSM action
2068 * CTCM_PROTO_MPC only
2069 */
2070static int mpc_send_qllc_discontact(struct net_device *dev)
2071{
2072	__u32	new_len	= 0;
2073	struct sk_buff   *skb;
2074	struct qllc      *qllcptr;
2075	struct ctcm_priv *priv = dev->ml_priv;
2076	struct mpc_group *grp = priv->mpcg;
2077
2078	CTCM_PR_DEBUG("%s: GROUP STATE: %s\n",
2079		__func__, mpcg_state_names[grp->saved_state]);
2080
2081	switch (grp->saved_state) {
2082	/*
2083	 * establish conn callback function is
2084	 * preferred method to report failure
2085	 */
2086	case MPCG_STATE_XID0IOWAIT:
2087	case MPCG_STATE_XID0IOWAIX:
2088	case MPCG_STATE_XID7INITI:
2089	case MPCG_STATE_XID7INITZ:
2090	case MPCG_STATE_XID2INITW:
2091	case MPCG_STATE_XID2INITX:
2092	case MPCG_STATE_XID7INITW:
2093	case MPCG_STATE_XID7INITX:
2094		if (grp->estconnfunc) {
2095			grp->estconnfunc(grp->port_num, -1, 0);
2096			grp->estconnfunc = NULL;
2097			break;
2098		}
2099	case MPCG_STATE_FLOWC:
2100	case MPCG_STATE_READY:
2101		grp->send_qllc_disc = 2;
2102		new_len = sizeof(struct qllc);
2103		qllcptr = kzalloc(new_len, gfp_type() | GFP_DMA);
2104		if (qllcptr == NULL) {
2105			CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
2106				"%s(%s): qllcptr allocation error",
2107						CTCM_FUNTAIL, dev->name);
2108			return -ENOMEM;
2109		}
2110
2111		qllcptr->qllc_address = 0xcc;
2112		qllcptr->qllc_commands = 0x03;
2113
2114		skb = __dev_alloc_skb(new_len, GFP_ATOMIC);
2115
2116		if (skb == NULL) {
2117			CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
2118				"%s(%s): skb allocation error",
2119						CTCM_FUNTAIL, dev->name);
2120			priv->stats.rx_dropped++;
2121			kfree(qllcptr);
2122			return -ENOMEM;
2123		}
2124
2125		memcpy(skb_put(skb, new_len), qllcptr, new_len);
2126		kfree(qllcptr);
2127
2128		if (skb_headroom(skb) < 4) {
2129			CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
2130				"%s(%s): skb_headroom error",
2131						CTCM_FUNTAIL, dev->name);
2132			dev_kfree_skb_any(skb);
2133			return -ENOMEM;
2134		}
2135
2136		*((__u32 *)skb_push(skb, 4)) =
2137			priv->channel[CTCM_READ]->pdu_seq;
2138		priv->channel[CTCM_READ]->pdu_seq++;
2139		CTCM_PR_DBGDATA("ctcmpc: %s ToDCM_pdu_seq= %08x\n",
2140				__func__, priv->channel[CTCM_READ]->pdu_seq);
2141
2142		/* receipt of CC03 resets anticipated sequence number on
2143		      receiving side */
2144		priv->channel[CTCM_READ]->pdu_seq = 0x00;
2145		skb_reset_mac_header(skb);
2146		skb->dev = dev;
2147		skb->protocol = htons(ETH_P_SNAP);
2148		skb->ip_summed = CHECKSUM_UNNECESSARY;
2149
2150		CTCM_D3_DUMP(skb->data, (sizeof(struct qllc) + 4));
2151
2152		netif_rx(skb);
2153		break;
2154	default:
2155		break;
2156
2157	}
2158
2159	return 0;
2160}
2161/* --- This is the END my friend --- */
2162
2163