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