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