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