Merge branch 'linus' into x86/xsave
[linux-2.6] / drivers / s390 / net / lcs.c
1 /*
2  *  linux/drivers/s390/net/lcs.c
3  *
4  *  Linux for S/390 Lan Channel Station Network Driver
5  *
6  *  Copyright (C)  1999-2001 IBM Deutschland Entwicklung GmbH,
7  *                           IBM Corporation
8  *    Author(s): Original Code written by
9  *                        DJ Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
10  *               Rewritten by
11  *                        Frank Pavlic (fpavlic@de.ibm.com) and
12  *                        Martin Schwidefsky <schwidefsky@de.ibm.com>
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2, or (at your option)
17  * any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27  */
28
29 #include <linux/module.h>
30 #include <linux/if.h>
31 #include <linux/netdevice.h>
32 #include <linux/etherdevice.h>
33 #include <linux/trdevice.h>
34 #include <linux/fddidevice.h>
35 #include <linux/inetdevice.h>
36 #include <linux/in.h>
37 #include <linux/igmp.h>
38 #include <linux/delay.h>
39 #include <net/arp.h>
40 #include <net/ip.h>
41
42 #include <asm/debug.h>
43 #include <asm/idals.h>
44 #include <asm/timex.h>
45 #include <linux/device.h>
46 #include <asm/ccwgroup.h>
47
48 #include "lcs.h"
49 #include "cu3088.h"
50
51
52 #if !defined(CONFIG_NET_ETHERNET) && \
53     !defined(CONFIG_TR) && !defined(CONFIG_FDDI)
54 #error Cannot compile lcs.c without some net devices switched on.
55 #endif
56
57 #define PRINTK_HEADER           " lcs: "
58
59 /**
60  * initialization string for output
61  */
62
63 static char version[] __initdata = "LCS driver";
64 static char debug_buffer[255];
65
66 /**
67  * Some prototypes.
68  */
69 static void lcs_tasklet(unsigned long);
70 static void lcs_start_kernel_thread(struct work_struct *);
71 static void lcs_get_frames_cb(struct lcs_channel *, struct lcs_buffer *);
72 static int lcs_send_delipm(struct lcs_card *, struct lcs_ipm_list *);
73 static int lcs_recovery(void *ptr);
74
75 /**
76  * Debug Facility Stuff
77  */
78 static debug_info_t *lcs_dbf_setup;
79 static debug_info_t *lcs_dbf_trace;
80
81 /**
82  *  LCS Debug Facility functions
83  */
84 static void
85 lcs_unregister_debug_facility(void)
86 {
87         if (lcs_dbf_setup)
88                 debug_unregister(lcs_dbf_setup);
89         if (lcs_dbf_trace)
90                 debug_unregister(lcs_dbf_trace);
91 }
92
93 static int
94 lcs_register_debug_facility(void)
95 {
96         lcs_dbf_setup = debug_register("lcs_setup", 2, 1, 8);
97         lcs_dbf_trace = debug_register("lcs_trace", 4, 1, 8);
98         if (lcs_dbf_setup == NULL || lcs_dbf_trace == NULL) {
99                 PRINT_ERR("Not enough memory for debug facility.\n");
100                 lcs_unregister_debug_facility();
101                 return -ENOMEM;
102         }
103         debug_register_view(lcs_dbf_setup, &debug_hex_ascii_view);
104         debug_set_level(lcs_dbf_setup, 2);
105         debug_register_view(lcs_dbf_trace, &debug_hex_ascii_view);
106         debug_set_level(lcs_dbf_trace, 2);
107         return 0;
108 }
109
110 /**
111  * Allocate io buffers.
112  */
113 static int
114 lcs_alloc_channel(struct lcs_channel *channel)
115 {
116         int cnt;
117
118         LCS_DBF_TEXT(2, setup, "ichalloc");
119         for (cnt = 0; cnt < LCS_NUM_BUFFS; cnt++) {
120                 /* alloc memory fo iobuffer */
121                 channel->iob[cnt].data =
122                         kzalloc(LCS_IOBUFFERSIZE, GFP_DMA | GFP_KERNEL);
123                 if (channel->iob[cnt].data == NULL)
124                         break;
125                 channel->iob[cnt].state = LCS_BUF_STATE_EMPTY;
126         }
127         if (cnt < LCS_NUM_BUFFS) {
128                 /* Not all io buffers could be allocated. */
129                 LCS_DBF_TEXT(2, setup, "echalloc");
130                 while (cnt-- > 0)
131                         kfree(channel->iob[cnt].data);
132                 return -ENOMEM;
133         }
134         return 0;
135 }
136
137 /**
138  * Free io buffers.
139  */
140 static void
141 lcs_free_channel(struct lcs_channel *channel)
142 {
143         int cnt;
144
145         LCS_DBF_TEXT(2, setup, "ichfree");
146         for (cnt = 0; cnt < LCS_NUM_BUFFS; cnt++) {
147                 kfree(channel->iob[cnt].data);
148                 channel->iob[cnt].data = NULL;
149         }
150 }
151
152 /*
153  * Cleanup channel.
154  */
155 static void
156 lcs_cleanup_channel(struct lcs_channel *channel)
157 {
158         LCS_DBF_TEXT(3, setup, "cleanch");
159         /* Kill write channel tasklets. */
160         tasklet_kill(&channel->irq_tasklet);
161         /* Free channel buffers. */
162         lcs_free_channel(channel);
163 }
164
165 /**
166  * LCS free memory for card and channels.
167  */
168 static void
169 lcs_free_card(struct lcs_card *card)
170 {
171         LCS_DBF_TEXT(2, setup, "remcard");
172         LCS_DBF_HEX(2, setup, &card, sizeof(void*));
173         kfree(card);
174 }
175
176 /**
177  * LCS alloc memory for card and channels
178  */
179 static struct lcs_card *
180 lcs_alloc_card(void)
181 {
182         struct lcs_card *card;
183         int rc;
184
185         LCS_DBF_TEXT(2, setup, "alloclcs");
186
187         card = kzalloc(sizeof(struct lcs_card), GFP_KERNEL | GFP_DMA);
188         if (card == NULL)
189                 return NULL;
190         card->lan_type = LCS_FRAME_TYPE_AUTO;
191         card->pkt_seq = 0;
192         card->lancmd_timeout = LCS_LANCMD_TIMEOUT_DEFAULT;
193         /* Allocate io buffers for the read channel. */
194         rc = lcs_alloc_channel(&card->read);
195         if (rc){
196                 LCS_DBF_TEXT(2, setup, "iccwerr");
197                 lcs_free_card(card);
198                 return NULL;
199         }
200         /* Allocate io buffers for the write channel. */
201         rc = lcs_alloc_channel(&card->write);
202         if (rc) {
203                 LCS_DBF_TEXT(2, setup, "iccwerr");
204                 lcs_cleanup_channel(&card->read);
205                 lcs_free_card(card);
206                 return NULL;
207         }
208
209 #ifdef CONFIG_IP_MULTICAST
210         INIT_LIST_HEAD(&card->ipm_list);
211 #endif
212         LCS_DBF_HEX(2, setup, &card, sizeof(void*));
213         return card;
214 }
215
216 /*
217  * Setup read channel.
218  */
219 static void
220 lcs_setup_read_ccws(struct lcs_card *card)
221 {
222         int cnt;
223
224         LCS_DBF_TEXT(2, setup, "ireadccw");
225         /* Setup read ccws. */
226         memset(card->read.ccws, 0, sizeof (struct ccw1) * (LCS_NUM_BUFFS + 1));
227         for (cnt = 0; cnt < LCS_NUM_BUFFS; cnt++) {
228                 card->read.ccws[cnt].cmd_code = LCS_CCW_READ;
229                 card->read.ccws[cnt].count = LCS_IOBUFFERSIZE;
230                 card->read.ccws[cnt].flags =
231                         CCW_FLAG_CC | CCW_FLAG_SLI | CCW_FLAG_PCI;
232                 /*
233                  * Note: we have allocated the buffer with GFP_DMA, so
234                  * we do not need to do set_normalized_cda.
235                  */
236                 card->read.ccws[cnt].cda =
237                         (__u32) __pa(card->read.iob[cnt].data);
238                 ((struct lcs_header *)
239                  card->read.iob[cnt].data)->offset = LCS_ILLEGAL_OFFSET;
240                 card->read.iob[cnt].callback = lcs_get_frames_cb;
241                 card->read.iob[cnt].state = LCS_BUF_STATE_READY;
242                 card->read.iob[cnt].count = LCS_IOBUFFERSIZE;
243         }
244         card->read.ccws[0].flags &= ~CCW_FLAG_PCI;
245         card->read.ccws[LCS_NUM_BUFFS - 1].flags &= ~CCW_FLAG_PCI;
246         card->read.ccws[LCS_NUM_BUFFS - 1].flags |= CCW_FLAG_SUSPEND;
247         /* Last ccw is a tic (transfer in channel). */
248         card->read.ccws[LCS_NUM_BUFFS].cmd_code = LCS_CCW_TRANSFER;
249         card->read.ccws[LCS_NUM_BUFFS].cda =
250                 (__u32) __pa(card->read.ccws);
251         /* Setg initial state of the read channel. */
252         card->read.state = LCS_CH_STATE_INIT;
253
254         card->read.io_idx = 0;
255         card->read.buf_idx = 0;
256 }
257
258 static void
259 lcs_setup_read(struct lcs_card *card)
260 {
261         LCS_DBF_TEXT(3, setup, "initread");
262
263         lcs_setup_read_ccws(card);
264         /* Initialize read channel tasklet. */
265         card->read.irq_tasklet.data = (unsigned long) &card->read;
266         card->read.irq_tasklet.func = lcs_tasklet;
267         /* Initialize waitqueue. */
268         init_waitqueue_head(&card->read.wait_q);
269 }
270
271 /*
272  * Setup write channel.
273  */
274 static void
275 lcs_setup_write_ccws(struct lcs_card *card)
276 {
277         int cnt;
278
279         LCS_DBF_TEXT(3, setup, "iwritccw");
280         /* Setup write ccws. */
281         memset(card->write.ccws, 0, sizeof(struct ccw1) * LCS_NUM_BUFFS + 1);
282         for (cnt = 0; cnt < LCS_NUM_BUFFS; cnt++) {
283                 card->write.ccws[cnt].cmd_code = LCS_CCW_WRITE;
284                 card->write.ccws[cnt].count = 0;
285                 card->write.ccws[cnt].flags =
286                         CCW_FLAG_SUSPEND | CCW_FLAG_CC | CCW_FLAG_SLI;
287                 /*
288                  * Note: we have allocated the buffer with GFP_DMA, so
289                  * we do not need to do set_normalized_cda.
290                  */
291                 card->write.ccws[cnt].cda =
292                         (__u32) __pa(card->write.iob[cnt].data);
293         }
294         /* Last ccw is a tic (transfer in channel). */
295         card->write.ccws[LCS_NUM_BUFFS].cmd_code = LCS_CCW_TRANSFER;
296         card->write.ccws[LCS_NUM_BUFFS].cda =
297                 (__u32) __pa(card->write.ccws);
298         /* Set initial state of the write channel. */
299         card->read.state = LCS_CH_STATE_INIT;
300
301         card->write.io_idx = 0;
302         card->write.buf_idx = 0;
303 }
304
305 static void
306 lcs_setup_write(struct lcs_card *card)
307 {
308         LCS_DBF_TEXT(3, setup, "initwrit");
309
310         lcs_setup_write_ccws(card);
311         /* Initialize write channel tasklet. */
312         card->write.irq_tasklet.data = (unsigned long) &card->write;
313         card->write.irq_tasklet.func = lcs_tasklet;
314         /* Initialize waitqueue. */
315         init_waitqueue_head(&card->write.wait_q);
316 }
317
318 static void
319 lcs_set_allowed_threads(struct lcs_card *card, unsigned long threads)
320 {
321         unsigned long flags;
322
323         spin_lock_irqsave(&card->mask_lock, flags);
324         card->thread_allowed_mask = threads;
325         spin_unlock_irqrestore(&card->mask_lock, flags);
326         wake_up(&card->wait_q);
327 }
328 static inline int
329 lcs_threads_running(struct lcs_card *card, unsigned long threads)
330 {
331         unsigned long flags;
332         int rc = 0;
333
334         spin_lock_irqsave(&card->mask_lock, flags);
335         rc = (card->thread_running_mask & threads);
336         spin_unlock_irqrestore(&card->mask_lock, flags);
337         return rc;
338 }
339
340 static int
341 lcs_wait_for_threads(struct lcs_card *card, unsigned long threads)
342 {
343         return wait_event_interruptible(card->wait_q,
344                         lcs_threads_running(card, threads) == 0);
345 }
346
347 static inline int
348 lcs_set_thread_start_bit(struct lcs_card *card, unsigned long thread)
349 {
350         unsigned long flags;
351
352         spin_lock_irqsave(&card->mask_lock, flags);
353         if ( !(card->thread_allowed_mask & thread) ||
354               (card->thread_start_mask & thread) ) {
355                 spin_unlock_irqrestore(&card->mask_lock, flags);
356                 return -EPERM;
357         }
358         card->thread_start_mask |= thread;
359         spin_unlock_irqrestore(&card->mask_lock, flags);
360         return 0;
361 }
362
363 static void
364 lcs_clear_thread_running_bit(struct lcs_card *card, unsigned long thread)
365 {
366         unsigned long flags;
367
368         spin_lock_irqsave(&card->mask_lock, flags);
369         card->thread_running_mask &= ~thread;
370         spin_unlock_irqrestore(&card->mask_lock, flags);
371         wake_up(&card->wait_q);
372 }
373
374 static inline int
375 __lcs_do_run_thread(struct lcs_card *card, unsigned long thread)
376 {
377         unsigned long flags;
378         int rc = 0;
379
380         spin_lock_irqsave(&card->mask_lock, flags);
381         if (card->thread_start_mask & thread){
382                 if ((card->thread_allowed_mask & thread) &&
383                     !(card->thread_running_mask & thread)){
384                         rc = 1;
385                         card->thread_start_mask &= ~thread;
386                         card->thread_running_mask |= thread;
387                 } else
388                         rc = -EPERM;
389         }
390         spin_unlock_irqrestore(&card->mask_lock, flags);
391         return rc;
392 }
393
394 static int
395 lcs_do_run_thread(struct lcs_card *card, unsigned long thread)
396 {
397         int rc = 0;
398         wait_event(card->wait_q,
399                    (rc = __lcs_do_run_thread(card, thread)) >= 0);
400         return rc;
401 }
402
403 static int
404 lcs_do_start_thread(struct lcs_card *card, unsigned long thread)
405 {
406         unsigned long flags;
407         int rc = 0;
408
409         spin_lock_irqsave(&card->mask_lock, flags);
410         LCS_DBF_TEXT_(4, trace, "  %02x%02x%02x",
411                         (u8) card->thread_start_mask,
412                         (u8) card->thread_allowed_mask,
413                         (u8) card->thread_running_mask);
414         rc = (card->thread_start_mask & thread);
415         spin_unlock_irqrestore(&card->mask_lock, flags);
416         return rc;
417 }
418
419 /**
420  * Initialize channels,card and state machines.
421  */
422 static void
423 lcs_setup_card(struct lcs_card *card)
424 {
425         LCS_DBF_TEXT(2, setup, "initcard");
426         LCS_DBF_HEX(2, setup, &card, sizeof(void*));
427
428         lcs_setup_read(card);
429         lcs_setup_write(card);
430         /* Set cards initial state. */
431         card->state = DEV_STATE_DOWN;
432         card->tx_buffer = NULL;
433         card->tx_emitted = 0;
434
435         init_waitqueue_head(&card->wait_q);
436         spin_lock_init(&card->lock);
437         spin_lock_init(&card->ipm_lock);
438         spin_lock_init(&card->mask_lock);
439 #ifdef CONFIG_IP_MULTICAST
440         INIT_LIST_HEAD(&card->ipm_list);
441 #endif
442         INIT_LIST_HEAD(&card->lancmd_waiters);
443 }
444
445 static inline void
446 lcs_clear_multicast_list(struct lcs_card *card)
447 {
448 #ifdef  CONFIG_IP_MULTICAST
449         struct lcs_ipm_list *ipm;
450         unsigned long flags;
451
452         /* Free multicast list. */
453         LCS_DBF_TEXT(3, setup, "clmclist");
454         spin_lock_irqsave(&card->ipm_lock, flags);
455         while (!list_empty(&card->ipm_list)){
456                 ipm = list_entry(card->ipm_list.next,
457                                  struct lcs_ipm_list, list);
458                 list_del(&ipm->list);
459                 if (ipm->ipm_state != LCS_IPM_STATE_SET_REQUIRED){
460                         spin_unlock_irqrestore(&card->ipm_lock, flags);
461                         lcs_send_delipm(card, ipm);
462                         spin_lock_irqsave(&card->ipm_lock, flags);
463                 }
464                 kfree(ipm);
465         }
466         spin_unlock_irqrestore(&card->ipm_lock, flags);
467 #endif
468 }
469 /**
470  * Cleanup channels,card and state machines.
471  */
472 static void
473 lcs_cleanup_card(struct lcs_card *card)
474 {
475
476         LCS_DBF_TEXT(3, setup, "cleancrd");
477         LCS_DBF_HEX(2,setup,&card,sizeof(void*));
478
479         if (card->dev != NULL)
480                 free_netdev(card->dev);
481         /* Cleanup channels. */
482         lcs_cleanup_channel(&card->write);
483         lcs_cleanup_channel(&card->read);
484 }
485
486 /**
487  * Start channel.
488  */
489 static int
490 lcs_start_channel(struct lcs_channel *channel)
491 {
492         unsigned long flags;
493         int rc;
494
495         LCS_DBF_TEXT_(4, trace,"ssch%s", dev_name(&channel->ccwdev->dev));
496         spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
497         rc = ccw_device_start(channel->ccwdev,
498                               channel->ccws + channel->io_idx, 0, 0,
499                               DOIO_DENY_PREFETCH | DOIO_ALLOW_SUSPEND);
500         if (rc == 0)
501                 channel->state = LCS_CH_STATE_RUNNING;
502         spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
503         if (rc) {
504                 LCS_DBF_TEXT_(4,trace,"essh%s",
505                               dev_name(&channel->ccwdev->dev));
506                 PRINT_ERR("Error in starting channel, rc=%d!\n", rc);
507         }
508         return rc;
509 }
510
511 static int
512 lcs_clear_channel(struct lcs_channel *channel)
513 {
514         unsigned long flags;
515         int rc;
516
517         LCS_DBF_TEXT(4,trace,"clearch");
518         LCS_DBF_TEXT_(4, trace, "%s", dev_name(&channel->ccwdev->dev));
519         spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
520         rc = ccw_device_clear(channel->ccwdev, (addr_t) channel);
521         spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
522         if (rc) {
523                 LCS_DBF_TEXT_(4, trace, "ecsc%s",
524                               dev_name(&channel->ccwdev->dev));
525                 return rc;
526         }
527         wait_event(channel->wait_q, (channel->state == LCS_CH_STATE_CLEARED));
528         channel->state = LCS_CH_STATE_STOPPED;
529         return rc;
530 }
531
532
533 /**
534  * Stop channel.
535  */
536 static int
537 lcs_stop_channel(struct lcs_channel *channel)
538 {
539         unsigned long flags;
540         int rc;
541
542         if (channel->state == LCS_CH_STATE_STOPPED)
543                 return 0;
544         LCS_DBF_TEXT(4,trace,"haltsch");
545         LCS_DBF_TEXT_(4, trace, "%s", dev_name(&channel->ccwdev->dev));
546         channel->state = LCS_CH_STATE_INIT;
547         spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
548         rc = ccw_device_halt(channel->ccwdev, (addr_t) channel);
549         spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
550         if (rc) {
551                 LCS_DBF_TEXT_(4, trace, "ehsc%s",
552                               dev_name(&channel->ccwdev->dev));
553                 return rc;
554         }
555         /* Asynchronous halt initialted. Wait for its completion. */
556         wait_event(channel->wait_q, (channel->state == LCS_CH_STATE_HALTED));
557         lcs_clear_channel(channel);
558         return 0;
559 }
560
561 /**
562  * start read and write channel
563  */
564 static int
565 lcs_start_channels(struct lcs_card *card)
566 {
567         int rc;
568
569         LCS_DBF_TEXT(2, trace, "chstart");
570         /* start read channel */
571         rc = lcs_start_channel(&card->read);
572         if (rc)
573                 return rc;
574         /* start write channel */
575         rc = lcs_start_channel(&card->write);
576         if (rc)
577                 lcs_stop_channel(&card->read);
578         return rc;
579 }
580
581 /**
582  * stop read and write channel
583  */
584 static int
585 lcs_stop_channels(struct lcs_card *card)
586 {
587         LCS_DBF_TEXT(2, trace, "chhalt");
588         lcs_stop_channel(&card->read);
589         lcs_stop_channel(&card->write);
590         return 0;
591 }
592
593 /**
594  * Get empty buffer.
595  */
596 static struct lcs_buffer *
597 __lcs_get_buffer(struct lcs_channel *channel)
598 {
599         int index;
600
601         LCS_DBF_TEXT(5, trace, "_getbuff");
602         index = channel->io_idx;
603         do {
604                 if (channel->iob[index].state == LCS_BUF_STATE_EMPTY) {
605                         channel->iob[index].state = LCS_BUF_STATE_LOCKED;
606                         return channel->iob + index;
607                 }
608                 index = (index + 1) & (LCS_NUM_BUFFS - 1);
609         } while (index != channel->io_idx);
610         return NULL;
611 }
612
613 static struct lcs_buffer *
614 lcs_get_buffer(struct lcs_channel *channel)
615 {
616         struct lcs_buffer *buffer;
617         unsigned long flags;
618
619         LCS_DBF_TEXT(5, trace, "getbuff");
620         spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
621         buffer = __lcs_get_buffer(channel);
622         spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
623         return buffer;
624 }
625
626 /**
627  * Resume channel program if the channel is suspended.
628  */
629 static int
630 __lcs_resume_channel(struct lcs_channel *channel)
631 {
632         int rc;
633
634         if (channel->state != LCS_CH_STATE_SUSPENDED)
635                 return 0;
636         if (channel->ccws[channel->io_idx].flags & CCW_FLAG_SUSPEND)
637                 return 0;
638         LCS_DBF_TEXT_(5, trace, "rsch%s", dev_name(&channel->ccwdev->dev));
639         rc = ccw_device_resume(channel->ccwdev);
640         if (rc) {
641                 LCS_DBF_TEXT_(4, trace, "ersc%s",
642                               dev_name(&channel->ccwdev->dev));
643                 PRINT_ERR("Error in lcs_resume_channel: rc=%d\n",rc);
644         } else
645                 channel->state = LCS_CH_STATE_RUNNING;
646         return rc;
647
648 }
649
650 /**
651  * Make a buffer ready for processing.
652  */
653 static inline void
654 __lcs_ready_buffer_bits(struct lcs_channel *channel, int index)
655 {
656         int prev, next;
657
658         LCS_DBF_TEXT(5, trace, "rdybits");
659         prev = (index - 1) & (LCS_NUM_BUFFS - 1);
660         next = (index + 1) & (LCS_NUM_BUFFS - 1);
661         /* Check if we may clear the suspend bit of this buffer. */
662         if (channel->ccws[next].flags & CCW_FLAG_SUSPEND) {
663                 /* Check if we have to set the PCI bit. */
664                 if (!(channel->ccws[prev].flags & CCW_FLAG_SUSPEND))
665                         /* Suspend bit of the previous buffer is not set. */
666                         channel->ccws[index].flags |= CCW_FLAG_PCI;
667                 /* Suspend bit of the next buffer is set. */
668                 channel->ccws[index].flags &= ~CCW_FLAG_SUSPEND;
669         }
670 }
671
672 static int
673 lcs_ready_buffer(struct lcs_channel *channel, struct lcs_buffer *buffer)
674 {
675         unsigned long flags;
676         int index, rc;
677
678         LCS_DBF_TEXT(5, trace, "rdybuff");
679         BUG_ON(buffer->state != LCS_BUF_STATE_LOCKED &&
680                buffer->state != LCS_BUF_STATE_PROCESSED);
681         spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
682         buffer->state = LCS_BUF_STATE_READY;
683         index = buffer - channel->iob;
684         /* Set length. */
685         channel->ccws[index].count = buffer->count;
686         /* Check relevant PCI/suspend bits. */
687         __lcs_ready_buffer_bits(channel, index);
688         rc = __lcs_resume_channel(channel);
689         spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
690         return rc;
691 }
692
693 /**
694  * Mark the buffer as processed. Take care of the suspend bit
695  * of the previous buffer. This function is called from
696  * interrupt context, so the lock must not be taken.
697  */
698 static int
699 __lcs_processed_buffer(struct lcs_channel *channel, struct lcs_buffer *buffer)
700 {
701         int index, prev, next;
702
703         LCS_DBF_TEXT(5, trace, "prcsbuff");
704         BUG_ON(buffer->state != LCS_BUF_STATE_READY);
705         buffer->state = LCS_BUF_STATE_PROCESSED;
706         index = buffer - channel->iob;
707         prev = (index - 1) & (LCS_NUM_BUFFS - 1);
708         next = (index + 1) & (LCS_NUM_BUFFS - 1);
709         /* Set the suspend bit and clear the PCI bit of this buffer. */
710         channel->ccws[index].flags |= CCW_FLAG_SUSPEND;
711         channel->ccws[index].flags &= ~CCW_FLAG_PCI;
712         /* Check the suspend bit of the previous buffer. */
713         if (channel->iob[prev].state == LCS_BUF_STATE_READY) {
714                 /*
715                  * Previous buffer is in state ready. It might have
716                  * happened in lcs_ready_buffer that the suspend bit
717                  * has not been cleared to avoid an endless loop.
718                  * Do it now.
719                  */
720                 __lcs_ready_buffer_bits(channel, prev);
721         }
722         /* Clear PCI bit of next buffer. */
723         channel->ccws[next].flags &= ~CCW_FLAG_PCI;
724         return __lcs_resume_channel(channel);
725 }
726
727 /**
728  * Put a processed buffer back to state empty.
729  */
730 static void
731 lcs_release_buffer(struct lcs_channel *channel, struct lcs_buffer *buffer)
732 {
733         unsigned long flags;
734
735         LCS_DBF_TEXT(5, trace, "relbuff");
736         BUG_ON(buffer->state != LCS_BUF_STATE_LOCKED &&
737                buffer->state != LCS_BUF_STATE_PROCESSED);
738         spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
739         buffer->state = LCS_BUF_STATE_EMPTY;
740         spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
741 }
742
743 /**
744  * Get buffer for a lan command.
745  */
746 static struct lcs_buffer *
747 lcs_get_lancmd(struct lcs_card *card, int count)
748 {
749         struct lcs_buffer *buffer;
750         struct lcs_cmd *cmd;
751
752         LCS_DBF_TEXT(4, trace, "getlncmd");
753         /* Get buffer and wait if none is available. */
754         wait_event(card->write.wait_q,
755                    ((buffer = lcs_get_buffer(&card->write)) != NULL));
756         count += sizeof(struct lcs_header);
757         *(__u16 *)(buffer->data + count) = 0;
758         buffer->count = count + sizeof(__u16);
759         buffer->callback = lcs_release_buffer;
760         cmd = (struct lcs_cmd *) buffer->data;
761         cmd->offset = count;
762         cmd->type = LCS_FRAME_TYPE_CONTROL;
763         cmd->slot = 0;
764         return buffer;
765 }
766
767
768 static void
769 lcs_get_reply(struct lcs_reply *reply)
770 {
771         WARN_ON(atomic_read(&reply->refcnt) <= 0);
772         atomic_inc(&reply->refcnt);
773 }
774
775 static void
776 lcs_put_reply(struct lcs_reply *reply)
777 {
778         WARN_ON(atomic_read(&reply->refcnt) <= 0);
779         if (atomic_dec_and_test(&reply->refcnt)) {
780                 kfree(reply);
781         }
782
783 }
784
785 static struct lcs_reply *
786 lcs_alloc_reply(struct lcs_cmd *cmd)
787 {
788         struct lcs_reply *reply;
789
790         LCS_DBF_TEXT(4, trace, "getreply");
791
792         reply = kzalloc(sizeof(struct lcs_reply), GFP_ATOMIC);
793         if (!reply)
794                 return NULL;
795         atomic_set(&reply->refcnt,1);
796         reply->sequence_no = cmd->sequence_no;
797         reply->received = 0;
798         reply->rc = 0;
799         init_waitqueue_head(&reply->wait_q);
800
801         return reply;
802 }
803
804 /**
805  * Notifier function for lancmd replies. Called from read irq.
806  */
807 static void
808 lcs_notify_lancmd_waiters(struct lcs_card *card, struct lcs_cmd *cmd)
809 {
810         struct list_head *l, *n;
811         struct lcs_reply *reply;
812
813         LCS_DBF_TEXT(4, trace, "notiwait");
814         spin_lock(&card->lock);
815         list_for_each_safe(l, n, &card->lancmd_waiters) {
816                 reply = list_entry(l, struct lcs_reply, list);
817                 if (reply->sequence_no == cmd->sequence_no) {
818                         lcs_get_reply(reply);
819                         list_del_init(&reply->list);
820                         if (reply->callback != NULL)
821                                 reply->callback(card, cmd);
822                         reply->received = 1;
823                         reply->rc = cmd->return_code;
824                         wake_up(&reply->wait_q);
825                         lcs_put_reply(reply);
826                         break;
827                 }
828         }
829         spin_unlock(&card->lock);
830 }
831
832 /**
833  * Emit buffer of a lan comand.
834  */
835 static void
836 lcs_lancmd_timeout(unsigned long data)
837 {
838         struct lcs_reply *reply, *list_reply, *r;
839         unsigned long flags;
840
841         LCS_DBF_TEXT(4, trace, "timeout");
842         reply = (struct lcs_reply *) data;
843         spin_lock_irqsave(&reply->card->lock, flags);
844         list_for_each_entry_safe(list_reply, r,
845                                  &reply->card->lancmd_waiters,list) {
846                 if (reply == list_reply) {
847                         lcs_get_reply(reply);
848                         list_del_init(&reply->list);
849                         spin_unlock_irqrestore(&reply->card->lock, flags);
850                         reply->received = 1;
851                         reply->rc = -ETIME;
852                         wake_up(&reply->wait_q);
853                         lcs_put_reply(reply);
854                         return;
855                 }
856         }
857         spin_unlock_irqrestore(&reply->card->lock, flags);
858 }
859
860 static int
861 lcs_send_lancmd(struct lcs_card *card, struct lcs_buffer *buffer,
862                 void (*reply_callback)(struct lcs_card *, struct lcs_cmd *))
863 {
864         struct lcs_reply *reply;
865         struct lcs_cmd *cmd;
866         struct timer_list timer;
867         unsigned long flags;
868         int rc;
869
870         LCS_DBF_TEXT(4, trace, "sendcmd");
871         cmd = (struct lcs_cmd *) buffer->data;
872         cmd->return_code = 0;
873         cmd->sequence_no = card->sequence_no++;
874         reply = lcs_alloc_reply(cmd);
875         if (!reply)
876                 return -ENOMEM;
877         reply->callback = reply_callback;
878         reply->card = card;
879         spin_lock_irqsave(&card->lock, flags);
880         list_add_tail(&reply->list, &card->lancmd_waiters);
881         spin_unlock_irqrestore(&card->lock, flags);
882
883         buffer->callback = lcs_release_buffer;
884         rc = lcs_ready_buffer(&card->write, buffer);
885         if (rc)
886                 return rc;
887         init_timer(&timer);
888         timer.function = lcs_lancmd_timeout;
889         timer.data = (unsigned long) reply;
890         timer.expires = jiffies + HZ*card->lancmd_timeout;
891         add_timer(&timer);
892         wait_event(reply->wait_q, reply->received);
893         del_timer_sync(&timer);
894         LCS_DBF_TEXT_(4, trace, "rc:%d",reply->rc);
895         rc = reply->rc;
896         lcs_put_reply(reply);
897         return rc ? -EIO : 0;
898 }
899
900 /**
901  * LCS startup command
902  */
903 static int
904 lcs_send_startup(struct lcs_card *card, __u8 initiator)
905 {
906         struct lcs_buffer *buffer;
907         struct lcs_cmd *cmd;
908
909         LCS_DBF_TEXT(2, trace, "startup");
910         buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
911         cmd = (struct lcs_cmd *) buffer->data;
912         cmd->cmd_code = LCS_CMD_STARTUP;
913         cmd->initiator = initiator;
914         cmd->cmd.lcs_startup.buff_size = LCS_IOBUFFERSIZE;
915         return lcs_send_lancmd(card, buffer, NULL);
916 }
917
918 /**
919  * LCS shutdown command
920  */
921 static int
922 lcs_send_shutdown(struct lcs_card *card)
923 {
924         struct lcs_buffer *buffer;
925         struct lcs_cmd *cmd;
926
927         LCS_DBF_TEXT(2, trace, "shutdown");
928         buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
929         cmd = (struct lcs_cmd *) buffer->data;
930         cmd->cmd_code = LCS_CMD_SHUTDOWN;
931         cmd->initiator = LCS_INITIATOR_TCPIP;
932         return lcs_send_lancmd(card, buffer, NULL);
933 }
934
935 /**
936  * LCS lanstat command
937  */
938 static void
939 __lcs_lanstat_cb(struct lcs_card *card, struct lcs_cmd *cmd)
940 {
941         LCS_DBF_TEXT(2, trace, "statcb");
942         memcpy(card->mac, cmd->cmd.lcs_lanstat_cmd.mac_addr, LCS_MAC_LENGTH);
943 }
944
945 static int
946 lcs_send_lanstat(struct lcs_card *card)
947 {
948         struct lcs_buffer *buffer;
949         struct lcs_cmd *cmd;
950
951         LCS_DBF_TEXT(2,trace, "cmdstat");
952         buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
953         cmd = (struct lcs_cmd *) buffer->data;
954         /* Setup lanstat command. */
955         cmd->cmd_code = LCS_CMD_LANSTAT;
956         cmd->initiator = LCS_INITIATOR_TCPIP;
957         cmd->cmd.lcs_std_cmd.lan_type = card->lan_type;
958         cmd->cmd.lcs_std_cmd.portno = card->portno;
959         return lcs_send_lancmd(card, buffer, __lcs_lanstat_cb);
960 }
961
962 /**
963  * send stoplan command
964  */
965 static int
966 lcs_send_stoplan(struct lcs_card *card, __u8 initiator)
967 {
968         struct lcs_buffer *buffer;
969         struct lcs_cmd *cmd;
970
971         LCS_DBF_TEXT(2, trace, "cmdstpln");
972         buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
973         cmd = (struct lcs_cmd *) buffer->data;
974         cmd->cmd_code = LCS_CMD_STOPLAN;
975         cmd->initiator = initiator;
976         cmd->cmd.lcs_std_cmd.lan_type = card->lan_type;
977         cmd->cmd.lcs_std_cmd.portno = card->portno;
978         return lcs_send_lancmd(card, buffer, NULL);
979 }
980
981 /**
982  * send startlan command
983  */
984 static void
985 __lcs_send_startlan_cb(struct lcs_card *card, struct lcs_cmd *cmd)
986 {
987         LCS_DBF_TEXT(2, trace, "srtlancb");
988         card->lan_type = cmd->cmd.lcs_std_cmd.lan_type;
989         card->portno = cmd->cmd.lcs_std_cmd.portno;
990 }
991
992 static int
993 lcs_send_startlan(struct lcs_card *card, __u8 initiator)
994 {
995         struct lcs_buffer *buffer;
996         struct lcs_cmd *cmd;
997
998         LCS_DBF_TEXT(2, trace, "cmdstaln");
999         buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
1000         cmd = (struct lcs_cmd *) buffer->data;
1001         cmd->cmd_code = LCS_CMD_STARTLAN;
1002         cmd->initiator = initiator;
1003         cmd->cmd.lcs_std_cmd.lan_type = card->lan_type;
1004         cmd->cmd.lcs_std_cmd.portno = card->portno;
1005         return lcs_send_lancmd(card, buffer, __lcs_send_startlan_cb);
1006 }
1007
1008 #ifdef CONFIG_IP_MULTICAST
1009 /**
1010  * send setipm command (Multicast)
1011  */
1012 static int
1013 lcs_send_setipm(struct lcs_card *card,struct lcs_ipm_list *ipm_list)
1014 {
1015         struct lcs_buffer *buffer;
1016         struct lcs_cmd *cmd;
1017
1018         LCS_DBF_TEXT(2, trace, "cmdsetim");
1019         buffer = lcs_get_lancmd(card, LCS_MULTICAST_CMD_SIZE);
1020         cmd = (struct lcs_cmd *) buffer->data;
1021         cmd->cmd_code = LCS_CMD_SETIPM;
1022         cmd->initiator = LCS_INITIATOR_TCPIP;
1023         cmd->cmd.lcs_qipassist.lan_type = card->lan_type;
1024         cmd->cmd.lcs_qipassist.portno = card->portno;
1025         cmd->cmd.lcs_qipassist.version = 4;
1026         cmd->cmd.lcs_qipassist.num_ip_pairs = 1;
1027         memcpy(cmd->cmd.lcs_qipassist.lcs_ipass_ctlmsg.ip_mac_pair,
1028                &ipm_list->ipm, sizeof (struct lcs_ip_mac_pair));
1029         LCS_DBF_TEXT_(2, trace, "%x",ipm_list->ipm.ip_addr);
1030         return lcs_send_lancmd(card, buffer, NULL);
1031 }
1032
1033 /**
1034  * send delipm command (Multicast)
1035  */
1036 static int
1037 lcs_send_delipm(struct lcs_card *card,struct lcs_ipm_list *ipm_list)
1038 {
1039         struct lcs_buffer *buffer;
1040         struct lcs_cmd *cmd;
1041
1042         LCS_DBF_TEXT(2, trace, "cmddelim");
1043         buffer = lcs_get_lancmd(card, LCS_MULTICAST_CMD_SIZE);
1044         cmd = (struct lcs_cmd *) buffer->data;
1045         cmd->cmd_code = LCS_CMD_DELIPM;
1046         cmd->initiator = LCS_INITIATOR_TCPIP;
1047         cmd->cmd.lcs_qipassist.lan_type = card->lan_type;
1048         cmd->cmd.lcs_qipassist.portno = card->portno;
1049         cmd->cmd.lcs_qipassist.version = 4;
1050         cmd->cmd.lcs_qipassist.num_ip_pairs = 1;
1051         memcpy(cmd->cmd.lcs_qipassist.lcs_ipass_ctlmsg.ip_mac_pair,
1052                &ipm_list->ipm, sizeof (struct lcs_ip_mac_pair));
1053         LCS_DBF_TEXT_(2, trace, "%x",ipm_list->ipm.ip_addr);
1054         return lcs_send_lancmd(card, buffer, NULL);
1055 }
1056
1057 /**
1058  * check if multicast is supported by LCS
1059  */
1060 static void
1061 __lcs_check_multicast_cb(struct lcs_card *card, struct lcs_cmd *cmd)
1062 {
1063         LCS_DBF_TEXT(2, trace, "chkmccb");
1064         card->ip_assists_supported =
1065                 cmd->cmd.lcs_qipassist.ip_assists_supported;
1066         card->ip_assists_enabled =
1067                 cmd->cmd.lcs_qipassist.ip_assists_enabled;
1068 }
1069
1070 static int
1071 lcs_check_multicast_support(struct lcs_card *card)
1072 {
1073         struct lcs_buffer *buffer;
1074         struct lcs_cmd *cmd;
1075         int rc;
1076
1077         LCS_DBF_TEXT(2, trace, "cmdqipa");
1078         /* Send query ipassist. */
1079         buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
1080         cmd = (struct lcs_cmd *) buffer->data;
1081         cmd->cmd_code = LCS_CMD_QIPASSIST;
1082         cmd->initiator = LCS_INITIATOR_TCPIP;
1083         cmd->cmd.lcs_qipassist.lan_type = card->lan_type;
1084         cmd->cmd.lcs_qipassist.portno = card->portno;
1085         cmd->cmd.lcs_qipassist.version = 4;
1086         cmd->cmd.lcs_qipassist.num_ip_pairs = 1;
1087         rc = lcs_send_lancmd(card, buffer, __lcs_check_multicast_cb);
1088         if (rc != 0) {
1089                 PRINT_ERR("Query IPAssist failed. Assuming unsupported!\n");
1090                 return -EOPNOTSUPP;
1091         }
1092         if (card->ip_assists_supported & LCS_IPASS_MULTICAST_SUPPORT)
1093                 return 0;
1094         return -EOPNOTSUPP;
1095 }
1096
1097 /**
1098  * set or del multicast address on LCS card
1099  */
1100 static void
1101 lcs_fix_multicast_list(struct lcs_card *card)
1102 {
1103         struct list_head failed_list;
1104         struct lcs_ipm_list *ipm, *tmp;
1105         unsigned long flags;
1106         int rc;
1107
1108         LCS_DBF_TEXT(4,trace, "fixipm");
1109         INIT_LIST_HEAD(&failed_list);
1110         spin_lock_irqsave(&card->ipm_lock, flags);
1111 list_modified:
1112         list_for_each_entry_safe(ipm, tmp, &card->ipm_list, list){
1113                 switch (ipm->ipm_state) {
1114                 case LCS_IPM_STATE_SET_REQUIRED:
1115                         /* del from ipm_list so noone else can tamper with
1116                          * this entry */
1117                         list_del_init(&ipm->list);
1118                         spin_unlock_irqrestore(&card->ipm_lock, flags);
1119                         rc = lcs_send_setipm(card, ipm);
1120                         spin_lock_irqsave(&card->ipm_lock, flags);
1121                         if (rc) {
1122                                 PRINT_INFO("Adding multicast address failed. "
1123                                            "Table possibly full!\n");
1124                                 /* store ipm in failed list -> will be added
1125                                  * to ipm_list again, so a retry will be done
1126                                  * during the next call of this function */
1127                                 list_add_tail(&ipm->list, &failed_list);
1128                         } else {
1129                                 ipm->ipm_state = LCS_IPM_STATE_ON_CARD;
1130                                 /* re-insert into ipm_list */
1131                                 list_add_tail(&ipm->list, &card->ipm_list);
1132                         }
1133                         goto list_modified;
1134                 case LCS_IPM_STATE_DEL_REQUIRED:
1135                         list_del(&ipm->list);
1136                         spin_unlock_irqrestore(&card->ipm_lock, flags);
1137                         lcs_send_delipm(card, ipm);
1138                         spin_lock_irqsave(&card->ipm_lock, flags);
1139                         kfree(ipm);
1140                         goto list_modified;
1141                 case LCS_IPM_STATE_ON_CARD:
1142                         break;
1143                 }
1144         }
1145         /* re-insert all entries from the failed_list into ipm_list */
1146         list_for_each_entry_safe(ipm, tmp, &failed_list, list)
1147                 list_move_tail(&ipm->list, &card->ipm_list);
1148
1149         spin_unlock_irqrestore(&card->ipm_lock, flags);
1150 }
1151
1152 /**
1153  * get mac address for the relevant Multicast address
1154  */
1155 static void
1156 lcs_get_mac_for_ipm(__be32 ipm, char *mac, struct net_device *dev)
1157 {
1158         LCS_DBF_TEXT(4,trace, "getmac");
1159         if (dev->type == ARPHRD_IEEE802_TR)
1160                 ip_tr_mc_map(ipm, mac);
1161         else
1162                 ip_eth_mc_map(ipm, mac);
1163 }
1164
1165 /**
1166  * function called by net device to handle multicast address relevant things
1167  */
1168 static inline void
1169 lcs_remove_mc_addresses(struct lcs_card *card, struct in_device *in4_dev)
1170 {
1171         struct ip_mc_list *im4;
1172         struct list_head *l;
1173         struct lcs_ipm_list *ipm;
1174         unsigned long flags;
1175         char buf[MAX_ADDR_LEN];
1176
1177         LCS_DBF_TEXT(4, trace, "remmclst");
1178         spin_lock_irqsave(&card->ipm_lock, flags);
1179         list_for_each(l, &card->ipm_list) {
1180                 ipm = list_entry(l, struct lcs_ipm_list, list);
1181                 for (im4 = in4_dev->mc_list; im4 != NULL; im4 = im4->next) {
1182                         lcs_get_mac_for_ipm(im4->multiaddr, buf, card->dev);
1183                         if ( (ipm->ipm.ip_addr == im4->multiaddr) &&
1184                              (memcmp(buf, &ipm->ipm.mac_addr,
1185                                      LCS_MAC_LENGTH) == 0) )
1186                                 break;
1187                 }
1188                 if (im4 == NULL)
1189                         ipm->ipm_state = LCS_IPM_STATE_DEL_REQUIRED;
1190         }
1191         spin_unlock_irqrestore(&card->ipm_lock, flags);
1192 }
1193
1194 static inline struct lcs_ipm_list *
1195 lcs_check_addr_entry(struct lcs_card *card, struct ip_mc_list *im4, char *buf)
1196 {
1197         struct lcs_ipm_list *tmp, *ipm = NULL;
1198         struct list_head *l;
1199         unsigned long flags;
1200
1201         LCS_DBF_TEXT(4, trace, "chkmcent");
1202         spin_lock_irqsave(&card->ipm_lock, flags);
1203         list_for_each(l, &card->ipm_list) {
1204                 tmp = list_entry(l, struct lcs_ipm_list, list);
1205                 if ( (tmp->ipm.ip_addr == im4->multiaddr) &&
1206                      (memcmp(buf, &tmp->ipm.mac_addr,
1207                              LCS_MAC_LENGTH) == 0) ) {
1208                         ipm = tmp;
1209                         break;
1210                 }
1211         }
1212         spin_unlock_irqrestore(&card->ipm_lock, flags);
1213         return ipm;
1214 }
1215
1216 static inline void
1217 lcs_set_mc_addresses(struct lcs_card *card, struct in_device *in4_dev)
1218 {
1219
1220         struct ip_mc_list *im4;
1221         struct lcs_ipm_list *ipm;
1222         char buf[MAX_ADDR_LEN];
1223         unsigned long flags;
1224
1225         LCS_DBF_TEXT(4, trace, "setmclst");
1226         for (im4 = in4_dev->mc_list; im4; im4 = im4->next) {
1227                 lcs_get_mac_for_ipm(im4->multiaddr, buf, card->dev);
1228                 ipm = lcs_check_addr_entry(card, im4, buf);
1229                 if (ipm != NULL)
1230                         continue;       /* Address already in list. */
1231                 ipm = (struct lcs_ipm_list *)
1232                         kzalloc(sizeof(struct lcs_ipm_list), GFP_ATOMIC);
1233                 if (ipm == NULL) {
1234                         PRINT_INFO("Not enough memory to add "
1235                                    "new multicast entry!\n");
1236                         break;
1237                 }
1238                 memcpy(&ipm->ipm.mac_addr, buf, LCS_MAC_LENGTH);
1239                 ipm->ipm.ip_addr = im4->multiaddr;
1240                 ipm->ipm_state = LCS_IPM_STATE_SET_REQUIRED;
1241                 spin_lock_irqsave(&card->ipm_lock, flags);
1242                 LCS_DBF_HEX(2,trace,&ipm->ipm.ip_addr,4);
1243                 list_add(&ipm->list, &card->ipm_list);
1244                 spin_unlock_irqrestore(&card->ipm_lock, flags);
1245         }
1246 }
1247
1248 static int
1249 lcs_register_mc_addresses(void *data)
1250 {
1251         struct lcs_card *card;
1252         struct in_device *in4_dev;
1253
1254         card = (struct lcs_card *) data;
1255         daemonize("regipm");
1256
1257         if (!lcs_do_run_thread(card, LCS_SET_MC_THREAD))
1258                 return 0;
1259         LCS_DBF_TEXT(4, trace, "regmulti");
1260
1261         in4_dev = in_dev_get(card->dev);
1262         if (in4_dev == NULL)
1263                 goto out;
1264         read_lock(&in4_dev->mc_list_lock);
1265         lcs_remove_mc_addresses(card,in4_dev);
1266         lcs_set_mc_addresses(card, in4_dev);
1267         read_unlock(&in4_dev->mc_list_lock);
1268         in_dev_put(in4_dev);
1269
1270         netif_carrier_off(card->dev);
1271         netif_tx_disable(card->dev);
1272         wait_event(card->write.wait_q,
1273                         (card->write.state != LCS_CH_STATE_RUNNING));
1274         lcs_fix_multicast_list(card);
1275         if (card->state == DEV_STATE_UP) {
1276                 netif_carrier_on(card->dev);
1277                 netif_wake_queue(card->dev);
1278         }
1279 out:
1280         lcs_clear_thread_running_bit(card, LCS_SET_MC_THREAD);
1281         return 0;
1282 }
1283 /**
1284  * function called by net device to
1285  * handle multicast address relevant things
1286  */
1287 static void
1288 lcs_set_multicast_list(struct net_device *dev)
1289 {
1290         struct lcs_card *card;
1291
1292         LCS_DBF_TEXT(4, trace, "setmulti");
1293         card = (struct lcs_card *) dev->priv;
1294
1295         if (!lcs_set_thread_start_bit(card, LCS_SET_MC_THREAD))
1296                 schedule_work(&card->kernel_thread_starter);
1297 }
1298
1299 #endif /* CONFIG_IP_MULTICAST */
1300
1301 static long
1302 lcs_check_irb_error(struct ccw_device *cdev, struct irb *irb)
1303 {
1304         if (!IS_ERR(irb))
1305                 return 0;
1306
1307         switch (PTR_ERR(irb)) {
1308         case -EIO:
1309                 PRINT_WARN("i/o-error on device %s\n", dev_name(&cdev->dev));
1310                 LCS_DBF_TEXT(2, trace, "ckirberr");
1311                 LCS_DBF_TEXT_(2, trace, "  rc%d", -EIO);
1312                 break;
1313         case -ETIMEDOUT:
1314                 PRINT_WARN("timeout on device %s\n", dev_name(&cdev->dev));
1315                 LCS_DBF_TEXT(2, trace, "ckirberr");
1316                 LCS_DBF_TEXT_(2, trace, "  rc%d", -ETIMEDOUT);
1317                 break;
1318         default:
1319                 PRINT_WARN("unknown error %ld on device %s\n", PTR_ERR(irb),
1320                            dev_name(&cdev->dev));
1321                 LCS_DBF_TEXT(2, trace, "ckirberr");
1322                 LCS_DBF_TEXT(2, trace, "  rc???");
1323         }
1324         return PTR_ERR(irb);
1325 }
1326
1327 static int
1328 lcs_get_problem(struct ccw_device *cdev, struct irb *irb)
1329 {
1330         int dstat, cstat;
1331         char *sense;
1332
1333         sense = (char *) irb->ecw;
1334         cstat = irb->scsw.cmd.cstat;
1335         dstat = irb->scsw.cmd.dstat;
1336
1337         if (cstat & (SCHN_STAT_CHN_CTRL_CHK | SCHN_STAT_INTF_CTRL_CHK |
1338                      SCHN_STAT_CHN_DATA_CHK | SCHN_STAT_CHAIN_CHECK |
1339                      SCHN_STAT_PROT_CHECK   | SCHN_STAT_PROG_CHECK)) {
1340                 LCS_DBF_TEXT(2, trace, "CGENCHK");
1341                 return 1;
1342         }
1343         if (dstat & DEV_STAT_UNIT_CHECK) {
1344                 if (sense[LCS_SENSE_BYTE_1] &
1345                     LCS_SENSE_RESETTING_EVENT) {
1346                         LCS_DBF_TEXT(2, trace, "REVIND");
1347                         return 1;
1348                 }
1349                 if (sense[LCS_SENSE_BYTE_0] &
1350                     LCS_SENSE_CMD_REJECT) {
1351                         LCS_DBF_TEXT(2, trace, "CMDREJ");
1352                         return 0;
1353                 }
1354                 if ((!sense[LCS_SENSE_BYTE_0]) &&
1355                     (!sense[LCS_SENSE_BYTE_1]) &&
1356                     (!sense[LCS_SENSE_BYTE_2]) &&
1357                     (!sense[LCS_SENSE_BYTE_3])) {
1358                         LCS_DBF_TEXT(2, trace, "ZEROSEN");
1359                         return 0;
1360                 }
1361                 LCS_DBF_TEXT(2, trace, "DGENCHK");
1362                 return 1;
1363         }
1364         return 0;
1365 }
1366
1367 static void
1368 lcs_schedule_recovery(struct lcs_card *card)
1369 {
1370         LCS_DBF_TEXT(2, trace, "startrec");
1371         if (!lcs_set_thread_start_bit(card, LCS_RECOVERY_THREAD))
1372                 schedule_work(&card->kernel_thread_starter);
1373 }
1374
1375 /**
1376  * IRQ Handler for LCS channels
1377  */
1378 static void
1379 lcs_irq(struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
1380 {
1381         struct lcs_card *card;
1382         struct lcs_channel *channel;
1383         int rc, index;
1384         int cstat, dstat;
1385
1386         if (lcs_check_irb_error(cdev, irb))
1387                 return;
1388
1389         card = CARD_FROM_DEV(cdev);
1390         if (card->read.ccwdev == cdev)
1391                 channel = &card->read;
1392         else
1393                 channel = &card->write;
1394
1395         cstat = irb->scsw.cmd.cstat;
1396         dstat = irb->scsw.cmd.dstat;
1397         LCS_DBF_TEXT_(5, trace, "Rint%s", dev_name(&cdev->dev));
1398         LCS_DBF_TEXT_(5, trace, "%4x%4x", irb->scsw.cmd.cstat,
1399                       irb->scsw.cmd.dstat);
1400         LCS_DBF_TEXT_(5, trace, "%4x%4x", irb->scsw.cmd.fctl,
1401                       irb->scsw.cmd.actl);
1402
1403         /* Check for channel and device errors presented */
1404         rc = lcs_get_problem(cdev, irb);
1405         if (rc || (dstat & DEV_STAT_UNIT_EXCEP)) {
1406                 PRINT_WARN("check on device %s, dstat=0x%X, cstat=0x%X \n",
1407                             dev_name(&cdev->dev), dstat, cstat);
1408                 if (rc) {
1409                         channel->state = LCS_CH_STATE_ERROR;
1410                 }
1411         }
1412         if (channel->state == LCS_CH_STATE_ERROR) {
1413                 lcs_schedule_recovery(card);
1414                 wake_up(&card->wait_q);
1415                 return;
1416         }
1417         /* How far in the ccw chain have we processed? */
1418         if ((channel->state != LCS_CH_STATE_INIT) &&
1419             (irb->scsw.cmd.fctl & SCSW_FCTL_START_FUNC) &&
1420             (irb->scsw.cmd.cpa != 0)) {
1421                 index = (struct ccw1 *) __va((addr_t) irb->scsw.cmd.cpa)
1422                         - channel->ccws;
1423                 if ((irb->scsw.cmd.actl & SCSW_ACTL_SUSPENDED) ||
1424                     (irb->scsw.cmd.cstat & SCHN_STAT_PCI))
1425                         /* Bloody io subsystem tells us lies about cpa... */
1426                         index = (index - 1) & (LCS_NUM_BUFFS - 1);
1427                 while (channel->io_idx != index) {
1428                         __lcs_processed_buffer(channel,
1429                                                channel->iob + channel->io_idx);
1430                         channel->io_idx =
1431                                 (channel->io_idx + 1) & (LCS_NUM_BUFFS - 1);
1432                 }
1433         }
1434
1435         if ((irb->scsw.cmd.dstat & DEV_STAT_DEV_END) ||
1436             (irb->scsw.cmd.dstat & DEV_STAT_CHN_END) ||
1437             (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK))
1438                 /* Mark channel as stopped. */
1439                 channel->state = LCS_CH_STATE_STOPPED;
1440         else if (irb->scsw.cmd.actl & SCSW_ACTL_SUSPENDED)
1441                 /* CCW execution stopped on a suspend bit. */
1442                 channel->state = LCS_CH_STATE_SUSPENDED;
1443         if (irb->scsw.cmd.fctl & SCSW_FCTL_HALT_FUNC) {
1444                 if (irb->scsw.cmd.cc != 0) {
1445                         ccw_device_halt(channel->ccwdev, (addr_t) channel);
1446                         return;
1447                 }
1448                 /* The channel has been stopped by halt_IO. */
1449                 channel->state = LCS_CH_STATE_HALTED;
1450         }
1451         if (irb->scsw.cmd.fctl & SCSW_FCTL_CLEAR_FUNC)
1452                 channel->state = LCS_CH_STATE_CLEARED;
1453         /* Do the rest in the tasklet. */
1454         tasklet_schedule(&channel->irq_tasklet);
1455 }
1456
1457 /**
1458  * Tasklet for IRQ handler
1459  */
1460 static void
1461 lcs_tasklet(unsigned long data)
1462 {
1463         unsigned long flags;
1464         struct lcs_channel *channel;
1465         struct lcs_buffer *iob;
1466         int buf_idx;
1467         int rc;
1468
1469         channel = (struct lcs_channel *) data;
1470         LCS_DBF_TEXT_(5, trace, "tlet%s", dev_name(&channel->ccwdev->dev));
1471
1472         /* Check for processed buffers. */
1473         iob = channel->iob;
1474         buf_idx = channel->buf_idx;
1475         while (iob[buf_idx].state == LCS_BUF_STATE_PROCESSED) {
1476                 /* Do the callback thing. */
1477                 if (iob[buf_idx].callback != NULL)
1478                         iob[buf_idx].callback(channel, iob + buf_idx);
1479                 buf_idx = (buf_idx + 1) & (LCS_NUM_BUFFS - 1);
1480         }
1481         channel->buf_idx = buf_idx;
1482
1483         if (channel->state == LCS_CH_STATE_STOPPED)
1484                 // FIXME: what if rc != 0 ??
1485                 rc = lcs_start_channel(channel);
1486         spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1487         if (channel->state == LCS_CH_STATE_SUSPENDED &&
1488             channel->iob[channel->io_idx].state == LCS_BUF_STATE_READY) {
1489                 // FIXME: what if rc != 0 ??
1490                 rc = __lcs_resume_channel(channel);
1491         }
1492         spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1493
1494         /* Something happened on the channel. Wake up waiters. */
1495         wake_up(&channel->wait_q);
1496 }
1497
1498 /**
1499  * Finish current tx buffer and make it ready for transmit.
1500  */
1501 static void
1502 __lcs_emit_txbuffer(struct lcs_card *card)
1503 {
1504         LCS_DBF_TEXT(5, trace, "emittx");
1505         *(__u16 *)(card->tx_buffer->data + card->tx_buffer->count) = 0;
1506         card->tx_buffer->count += 2;
1507         lcs_ready_buffer(&card->write, card->tx_buffer);
1508         card->tx_buffer = NULL;
1509         card->tx_emitted++;
1510 }
1511
1512 /**
1513  * Callback for finished tx buffers.
1514  */
1515 static void
1516 lcs_txbuffer_cb(struct lcs_channel *channel, struct lcs_buffer *buffer)
1517 {
1518         struct lcs_card *card;
1519
1520         LCS_DBF_TEXT(5, trace, "txbuffcb");
1521         /* Put buffer back to pool. */
1522         lcs_release_buffer(channel, buffer);
1523         card = container_of(channel, struct lcs_card, write);
1524         if (netif_queue_stopped(card->dev) && netif_carrier_ok(card->dev))
1525                 netif_wake_queue(card->dev);
1526         spin_lock(&card->lock);
1527         card->tx_emitted--;
1528         if (card->tx_emitted <= 0 && card->tx_buffer != NULL)
1529                 /*
1530                  * Last running tx buffer has finished. Submit partially
1531                  * filled current buffer.
1532                  */
1533                 __lcs_emit_txbuffer(card);
1534         spin_unlock(&card->lock);
1535 }
1536
1537 /**
1538  * Packet transmit function called by network stack
1539  */
1540 static int
1541 __lcs_start_xmit(struct lcs_card *card, struct sk_buff *skb,
1542                  struct net_device *dev)
1543 {
1544         struct lcs_header *header;
1545         int rc = 0;
1546
1547         LCS_DBF_TEXT(5, trace, "hardxmit");
1548         if (skb == NULL) {
1549                 card->stats.tx_dropped++;
1550                 card->stats.tx_errors++;
1551                 return -EIO;
1552         }
1553         if (card->state != DEV_STATE_UP) {
1554                 dev_kfree_skb(skb);
1555                 card->stats.tx_dropped++;
1556                 card->stats.tx_errors++;
1557                 card->stats.tx_carrier_errors++;
1558                 return 0;
1559         }
1560         if (skb->protocol == htons(ETH_P_IPV6)) {
1561                 dev_kfree_skb(skb);
1562                 return 0;
1563         }
1564         netif_stop_queue(card->dev);
1565         spin_lock(&card->lock);
1566         if (card->tx_buffer != NULL &&
1567             card->tx_buffer->count + sizeof(struct lcs_header) +
1568             skb->len + sizeof(u16) > LCS_IOBUFFERSIZE)
1569                 /* skb too big for current tx buffer. */
1570                 __lcs_emit_txbuffer(card);
1571         if (card->tx_buffer == NULL) {
1572                 /* Get new tx buffer */
1573                 card->tx_buffer = lcs_get_buffer(&card->write);
1574                 if (card->tx_buffer == NULL) {
1575                         card->stats.tx_dropped++;
1576                         rc = -EBUSY;
1577                         goto out;
1578                 }
1579                 card->tx_buffer->callback = lcs_txbuffer_cb;
1580                 card->tx_buffer->count = 0;
1581         }
1582         header = (struct lcs_header *)
1583                 (card->tx_buffer->data + card->tx_buffer->count);
1584         card->tx_buffer->count += skb->len + sizeof(struct lcs_header);
1585         header->offset = card->tx_buffer->count;
1586         header->type = card->lan_type;
1587         header->slot = card->portno;
1588         skb_copy_from_linear_data(skb, header + 1, skb->len);
1589         spin_unlock(&card->lock);
1590         card->stats.tx_bytes += skb->len;
1591         card->stats.tx_packets++;
1592         dev_kfree_skb(skb);
1593         netif_wake_queue(card->dev);
1594         spin_lock(&card->lock);
1595         if (card->tx_emitted <= 0 && card->tx_buffer != NULL)
1596                 /* If this is the first tx buffer emit it immediately. */
1597                 __lcs_emit_txbuffer(card);
1598 out:
1599         spin_unlock(&card->lock);
1600         return rc;
1601 }
1602
1603 static int
1604 lcs_start_xmit(struct sk_buff *skb, struct net_device *dev)
1605 {
1606         struct lcs_card *card;
1607         int rc;
1608
1609         LCS_DBF_TEXT(5, trace, "pktxmit");
1610         card = (struct lcs_card *) dev->priv;
1611         rc = __lcs_start_xmit(card, skb, dev);
1612         return rc;
1613 }
1614
1615 /**
1616  * send startlan and lanstat command to make LCS device ready
1617  */
1618 static int
1619 lcs_startlan_auto(struct lcs_card *card)
1620 {
1621         int rc;
1622
1623         LCS_DBF_TEXT(2, trace, "strtauto");
1624 #ifdef CONFIG_NET_ETHERNET
1625         card->lan_type = LCS_FRAME_TYPE_ENET;
1626         rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
1627         if (rc == 0)
1628                 return 0;
1629
1630 #endif
1631 #ifdef CONFIG_TR
1632         card->lan_type = LCS_FRAME_TYPE_TR;
1633         rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
1634         if (rc == 0)
1635                 return 0;
1636 #endif
1637 #ifdef CONFIG_FDDI
1638         card->lan_type = LCS_FRAME_TYPE_FDDI;
1639         rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
1640         if (rc == 0)
1641                 return 0;
1642 #endif
1643         return -EIO;
1644 }
1645
1646 static int
1647 lcs_startlan(struct lcs_card *card)
1648 {
1649         int rc, i;
1650
1651         LCS_DBF_TEXT(2, trace, "startlan");
1652         rc = 0;
1653         if (card->portno != LCS_INVALID_PORT_NO) {
1654                 if (card->lan_type == LCS_FRAME_TYPE_AUTO)
1655                         rc = lcs_startlan_auto(card);
1656                 else
1657                         rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
1658         } else {
1659                 for (i = 0; i <= 16; i++) {
1660                         card->portno = i;
1661                         if (card->lan_type != LCS_FRAME_TYPE_AUTO)
1662                                 rc = lcs_send_startlan(card,
1663                                                        LCS_INITIATOR_TCPIP);
1664                         else
1665                                 /* autodetecting lan type */
1666                                 rc = lcs_startlan_auto(card);
1667                         if (rc == 0)
1668                                 break;
1669                 }
1670         }
1671         if (rc == 0)
1672                 return lcs_send_lanstat(card);
1673         return rc;
1674 }
1675
1676 /**
1677  * LCS detect function
1678  * setup channels and make them I/O ready
1679  */
1680 static int
1681 lcs_detect(struct lcs_card *card)
1682 {
1683         int rc = 0;
1684
1685         LCS_DBF_TEXT(2, setup, "lcsdetct");
1686         /* start/reset card */
1687         if (card->dev)
1688                 netif_stop_queue(card->dev);
1689         rc = lcs_stop_channels(card);
1690         if (rc == 0) {
1691                 rc = lcs_start_channels(card);
1692                 if (rc == 0) {
1693                         rc = lcs_send_startup(card, LCS_INITIATOR_TCPIP);
1694                         if (rc == 0)
1695                                 rc = lcs_startlan(card);
1696                 }
1697         }
1698         if (rc == 0) {
1699                 card->state = DEV_STATE_UP;
1700         } else {
1701                 card->state = DEV_STATE_DOWN;
1702                 card->write.state = LCS_CH_STATE_INIT;
1703                 card->read.state =  LCS_CH_STATE_INIT;
1704         }
1705         return rc;
1706 }
1707
1708 /**
1709  * LCS Stop card
1710  */
1711 static int
1712 lcs_stopcard(struct lcs_card *card)
1713 {
1714         int rc;
1715
1716         LCS_DBF_TEXT(3, setup, "stopcard");
1717
1718         if (card->read.state != LCS_CH_STATE_STOPPED &&
1719             card->write.state != LCS_CH_STATE_STOPPED &&
1720             card->read.state != LCS_CH_STATE_ERROR &&
1721             card->write.state != LCS_CH_STATE_ERROR &&
1722             card->state == DEV_STATE_UP) {
1723                 lcs_clear_multicast_list(card);
1724                 rc = lcs_send_stoplan(card,LCS_INITIATOR_TCPIP);
1725                 rc = lcs_send_shutdown(card);
1726         }
1727         rc = lcs_stop_channels(card);
1728         card->state = DEV_STATE_DOWN;
1729
1730         return rc;
1731 }
1732
1733 /**
1734  * Kernel Thread helper functions for LGW initiated commands
1735  */
1736 static void
1737 lcs_start_kernel_thread(struct work_struct *work)
1738 {
1739         struct lcs_card *card = container_of(work, struct lcs_card, kernel_thread_starter);
1740         LCS_DBF_TEXT(5, trace, "krnthrd");
1741         if (lcs_do_start_thread(card, LCS_RECOVERY_THREAD))
1742                 kernel_thread(lcs_recovery, (void *) card, SIGCHLD);
1743 #ifdef CONFIG_IP_MULTICAST
1744         if (lcs_do_start_thread(card, LCS_SET_MC_THREAD))
1745                 kernel_thread(lcs_register_mc_addresses,
1746                                 (void *) card, SIGCHLD);
1747 #endif
1748 }
1749
1750 /**
1751  * Process control frames.
1752  */
1753 static void
1754 lcs_get_control(struct lcs_card *card, struct lcs_cmd *cmd)
1755 {
1756         LCS_DBF_TEXT(5, trace, "getctrl");
1757         if (cmd->initiator == LCS_INITIATOR_LGW) {
1758                 switch(cmd->cmd_code) {
1759                 case LCS_CMD_STARTUP:
1760                 case LCS_CMD_STARTLAN:
1761                         lcs_schedule_recovery(card);
1762                         break;
1763                 case LCS_CMD_STOPLAN:
1764                         PRINT_WARN("Stoplan for %s initiated by LGW.\n",
1765                                         card->dev->name);
1766                         if (card->dev)
1767                                 netif_carrier_off(card->dev);
1768                         break;
1769                 default:
1770                         LCS_DBF_TEXT(5, trace, "noLGWcmd");
1771                         break;
1772                 }
1773         } else
1774                 lcs_notify_lancmd_waiters(card, cmd);
1775 }
1776
1777 /**
1778  * Unpack network packet.
1779  */
1780 static void
1781 lcs_get_skb(struct lcs_card *card, char *skb_data, unsigned int skb_len)
1782 {
1783         struct sk_buff *skb;
1784
1785         LCS_DBF_TEXT(5, trace, "getskb");
1786         if (card->dev == NULL ||
1787             card->state != DEV_STATE_UP)
1788                 /* The card isn't up. Ignore the packet. */
1789                 return;
1790
1791         skb = dev_alloc_skb(skb_len);
1792         if (skb == NULL) {
1793                 PRINT_ERR("LCS: alloc_skb failed for device=%s\n",
1794                           card->dev->name);
1795                 card->stats.rx_dropped++;
1796                 return;
1797         }
1798         memcpy(skb_put(skb, skb_len), skb_data, skb_len);
1799         skb->protocol = card->lan_type_trans(skb, card->dev);
1800         card->stats.rx_bytes += skb_len;
1801         card->stats.rx_packets++;
1802         if (skb->protocol == htons(ETH_P_802_2))
1803                 *((__u32 *)skb->cb) = ++card->pkt_seq;
1804         netif_rx(skb);
1805 }
1806
1807 /**
1808  * LCS main routine to get packets and lancmd replies from the buffers
1809  */
1810 static void
1811 lcs_get_frames_cb(struct lcs_channel *channel, struct lcs_buffer *buffer)
1812 {
1813         struct lcs_card *card;
1814         struct lcs_header *lcs_hdr;
1815         __u16 offset;
1816
1817         LCS_DBF_TEXT(5, trace, "lcsgtpkt");
1818         lcs_hdr = (struct lcs_header *) buffer->data;
1819         if (lcs_hdr->offset == LCS_ILLEGAL_OFFSET) {
1820                 LCS_DBF_TEXT(4, trace, "-eiogpkt");
1821                 return;
1822         }
1823         card = container_of(channel, struct lcs_card, read);
1824         offset = 0;
1825         while (lcs_hdr->offset != 0) {
1826                 if (lcs_hdr->offset <= 0 ||
1827                     lcs_hdr->offset > LCS_IOBUFFERSIZE ||
1828                     lcs_hdr->offset < offset) {
1829                         /* Offset invalid. */
1830                         card->stats.rx_length_errors++;
1831                         card->stats.rx_errors++;
1832                         return;
1833                 }
1834                 /* What kind of frame is it? */
1835                 if (lcs_hdr->type == LCS_FRAME_TYPE_CONTROL)
1836                         /* Control frame. */
1837                         lcs_get_control(card, (struct lcs_cmd *) lcs_hdr);
1838                 else if (lcs_hdr->type == LCS_FRAME_TYPE_ENET ||
1839                          lcs_hdr->type == LCS_FRAME_TYPE_TR ||
1840                          lcs_hdr->type == LCS_FRAME_TYPE_FDDI)
1841                         /* Normal network packet. */
1842                         lcs_get_skb(card, (char *)(lcs_hdr + 1),
1843                                     lcs_hdr->offset - offset -
1844                                     sizeof(struct lcs_header));
1845                 else
1846                         /* Unknown frame type. */
1847                         ; // FIXME: error message ?
1848                 /* Proceed to next frame. */
1849                 offset = lcs_hdr->offset;
1850                 lcs_hdr->offset = LCS_ILLEGAL_OFFSET;
1851                 lcs_hdr = (struct lcs_header *) (buffer->data + offset);
1852         }
1853         /* The buffer is now empty. Make it ready again. */
1854         lcs_ready_buffer(&card->read, buffer);
1855 }
1856
1857 /**
1858  * get network statistics for ifconfig and other user programs
1859  */
1860 static struct net_device_stats *
1861 lcs_getstats(struct net_device *dev)
1862 {
1863         struct lcs_card *card;
1864
1865         LCS_DBF_TEXT(4, trace, "netstats");
1866         card = (struct lcs_card *) dev->priv;
1867         return &card->stats;
1868 }
1869
1870 /**
1871  * stop lcs device
1872  * This function will be called by user doing ifconfig xxx down
1873  */
1874 static int
1875 lcs_stop_device(struct net_device *dev)
1876 {
1877         struct lcs_card *card;
1878         int rc;
1879
1880         LCS_DBF_TEXT(2, trace, "stopdev");
1881         card   = (struct lcs_card *) dev->priv;
1882         netif_carrier_off(dev);
1883         netif_tx_disable(dev);
1884         dev->flags &= ~IFF_UP;
1885         wait_event(card->write.wait_q,
1886                 (card->write.state != LCS_CH_STATE_RUNNING));
1887         rc = lcs_stopcard(card);
1888         if (rc)
1889                 PRINT_ERR("Try it again!\n ");
1890         return rc;
1891 }
1892
1893 /**
1894  * start lcs device and make it runnable
1895  * This function will be called by user doing ifconfig xxx up
1896  */
1897 static int
1898 lcs_open_device(struct net_device *dev)
1899 {
1900         struct lcs_card *card;
1901         int rc;
1902
1903         LCS_DBF_TEXT(2, trace, "opendev");
1904         card = (struct lcs_card *) dev->priv;
1905         /* initialize statistics */
1906         rc = lcs_detect(card);
1907         if (rc) {
1908                 PRINT_ERR("LCS:Error in opening device!\n");
1909
1910         } else {
1911                 dev->flags |= IFF_UP;
1912                 netif_carrier_on(dev);
1913                 netif_wake_queue(dev);
1914                 card->state = DEV_STATE_UP;
1915         }
1916         return rc;
1917 }
1918
1919 /**
1920  * show function for portno called by cat or similar things
1921  */
1922 static ssize_t
1923 lcs_portno_show (struct device *dev, struct device_attribute *attr, char *buf)
1924 {
1925         struct lcs_card *card;
1926
1927         card = (struct lcs_card *)dev->driver_data;
1928
1929         if (!card)
1930                 return 0;
1931
1932         return sprintf(buf, "%d\n", card->portno);
1933 }
1934
1935 /**
1936  * store the value which is piped to file portno
1937  */
1938 static ssize_t
1939 lcs_portno_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1940 {
1941         struct lcs_card *card;
1942         int value;
1943
1944         card = (struct lcs_card *)dev->driver_data;
1945
1946         if (!card)
1947                 return 0;
1948
1949         sscanf(buf, "%u", &value);
1950         /* TODO: sanity checks */
1951         card->portno = value;
1952
1953         return count;
1954
1955 }
1956
1957 static DEVICE_ATTR(portno, 0644, lcs_portno_show, lcs_portno_store);
1958
1959 static ssize_t
1960 lcs_type_show(struct device *dev, struct device_attribute *attr, char *buf)
1961 {
1962         struct ccwgroup_device *cgdev;
1963
1964         cgdev = to_ccwgroupdev(dev);
1965         if (!cgdev)
1966                 return -ENODEV;
1967
1968         return sprintf(buf, "%s\n", cu3088_type[cgdev->cdev[0]->id.driver_info]);
1969 }
1970
1971 static DEVICE_ATTR(type, 0444, lcs_type_show, NULL);
1972
1973 static ssize_t
1974 lcs_timeout_show(struct device *dev, struct device_attribute *attr, char *buf)
1975 {
1976         struct lcs_card *card;
1977
1978         card = (struct lcs_card *)dev->driver_data;
1979
1980         return card ? sprintf(buf, "%u\n", card->lancmd_timeout) : 0;
1981 }
1982
1983 static ssize_t
1984 lcs_timeout_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1985 {
1986         struct lcs_card *card;
1987         int value;
1988
1989         card = (struct lcs_card *)dev->driver_data;
1990
1991         if (!card)
1992                 return 0;
1993
1994         sscanf(buf, "%u", &value);
1995         /* TODO: sanity checks */
1996         card->lancmd_timeout = value;
1997
1998         return count;
1999
2000 }
2001
2002 static DEVICE_ATTR(lancmd_timeout, 0644, lcs_timeout_show, lcs_timeout_store);
2003
2004 static ssize_t
2005 lcs_dev_recover_store(struct device *dev, struct device_attribute *attr,
2006                       const char *buf, size_t count)
2007 {
2008         struct lcs_card *card = dev->driver_data;
2009         char *tmp;
2010         int i;
2011
2012         if (!card)
2013                 return -EINVAL;
2014         if (card->state != DEV_STATE_UP)
2015                 return -EPERM;
2016         i = simple_strtoul(buf, &tmp, 16);
2017         if (i == 1)
2018                 lcs_schedule_recovery(card);
2019         return count;
2020 }
2021
2022 static DEVICE_ATTR(recover, 0200, NULL, lcs_dev_recover_store);
2023
2024 static struct attribute * lcs_attrs[] = {
2025         &dev_attr_portno.attr,
2026         &dev_attr_type.attr,
2027         &dev_attr_lancmd_timeout.attr,
2028         &dev_attr_recover.attr,
2029         NULL,
2030 };
2031
2032 static struct attribute_group lcs_attr_group = {
2033         .attrs = lcs_attrs,
2034 };
2035
2036 /**
2037  * lcs_probe_device is called on establishing a new ccwgroup_device.
2038  */
2039 static int
2040 lcs_probe_device(struct ccwgroup_device *ccwgdev)
2041 {
2042         struct lcs_card *card;
2043         int ret;
2044
2045         if (!get_device(&ccwgdev->dev))
2046                 return -ENODEV;
2047
2048         LCS_DBF_TEXT(2, setup, "add_dev");
2049         card = lcs_alloc_card();
2050         if (!card) {
2051                 LCS_DBF_TEXT_(2, setup, "  rc%d", -ENOMEM);
2052                 put_device(&ccwgdev->dev);
2053                 return -ENOMEM;
2054         }
2055         ret = sysfs_create_group(&ccwgdev->dev.kobj, &lcs_attr_group);
2056         if (ret) {
2057                 lcs_free_card(card);
2058                 put_device(&ccwgdev->dev);
2059                 return ret;
2060         }
2061         ccwgdev->dev.driver_data = card;
2062         ccwgdev->cdev[0]->handler = lcs_irq;
2063         ccwgdev->cdev[1]->handler = lcs_irq;
2064         card->gdev = ccwgdev;
2065         INIT_WORK(&card->kernel_thread_starter, lcs_start_kernel_thread);
2066         card->thread_start_mask = 0;
2067         card->thread_allowed_mask = 0;
2068         card->thread_running_mask = 0;
2069         return 0;
2070 }
2071
2072 static int
2073 lcs_register_netdev(struct ccwgroup_device *ccwgdev)
2074 {
2075         struct lcs_card *card;
2076
2077         LCS_DBF_TEXT(2, setup, "regnetdv");
2078         card = (struct lcs_card *)ccwgdev->dev.driver_data;
2079         if (card->dev->reg_state != NETREG_UNINITIALIZED)
2080                 return 0;
2081         SET_NETDEV_DEV(card->dev, &ccwgdev->dev);
2082         return register_netdev(card->dev);
2083 }
2084
2085 /**
2086  * lcs_new_device will be called by setting the group device online.
2087  */
2088
2089 static int
2090 lcs_new_device(struct ccwgroup_device *ccwgdev)
2091 {
2092         struct  lcs_card *card;
2093         struct net_device *dev=NULL;
2094         enum lcs_dev_states recover_state;
2095         int rc;
2096
2097         card = (struct lcs_card *)ccwgdev->dev.driver_data;
2098         if (!card)
2099                 return -ENODEV;
2100
2101         LCS_DBF_TEXT(2, setup, "newdev");
2102         LCS_DBF_HEX(3, setup, &card, sizeof(void*));
2103         card->read.ccwdev  = ccwgdev->cdev[0];
2104         card->write.ccwdev = ccwgdev->cdev[1];
2105
2106         recover_state = card->state;
2107         ccw_device_set_online(card->read.ccwdev);
2108         ccw_device_set_online(card->write.ccwdev);
2109
2110         LCS_DBF_TEXT(3, setup, "lcsnewdv");
2111
2112         lcs_setup_card(card);
2113         rc = lcs_detect(card);
2114         if (rc) {
2115                 LCS_DBF_TEXT(2, setup, "dtctfail");
2116                 PRINT_WARN("Detection of LCS card failed with return code "
2117                            "%d (0x%x)\n", rc, rc);
2118                 lcs_stopcard(card);
2119                 goto out;
2120         }
2121         if (card->dev) {
2122                 LCS_DBF_TEXT(2, setup, "samedev");
2123                 LCS_DBF_HEX(3, setup, &card, sizeof(void*));
2124                 goto netdev_out;
2125         }
2126         switch (card->lan_type) {
2127 #ifdef CONFIG_NET_ETHERNET
2128         case LCS_FRAME_TYPE_ENET:
2129                 card->lan_type_trans = eth_type_trans;
2130                 dev = alloc_etherdev(0);
2131                 break;
2132 #endif
2133 #ifdef CONFIG_TR
2134         case LCS_FRAME_TYPE_TR:
2135                 card->lan_type_trans = tr_type_trans;
2136                 dev = alloc_trdev(0);
2137                 break;
2138 #endif
2139 #ifdef CONFIG_FDDI
2140         case LCS_FRAME_TYPE_FDDI:
2141                 card->lan_type_trans = fddi_type_trans;
2142                 dev = alloc_fddidev(0);
2143                 break;
2144 #endif
2145         default:
2146                 LCS_DBF_TEXT(3, setup, "errinit");
2147                 PRINT_ERR("LCS: Initialization failed\n");
2148                 goto out;
2149         }
2150         if (!dev)
2151                 goto out;
2152         card->dev = dev;
2153         card->dev->priv = card;
2154         card->dev->open = lcs_open_device;
2155         card->dev->stop = lcs_stop_device;
2156         card->dev->hard_start_xmit = lcs_start_xmit;
2157         card->dev->get_stats = lcs_getstats;
2158         memcpy(card->dev->dev_addr, card->mac, LCS_MAC_LENGTH);
2159 #ifdef CONFIG_IP_MULTICAST
2160         if (!lcs_check_multicast_support(card))
2161                 card->dev->set_multicast_list = lcs_set_multicast_list;
2162 #endif
2163 netdev_out:
2164         lcs_set_allowed_threads(card,0xffffffff);
2165         if (recover_state == DEV_STATE_RECOVER) {
2166                 lcs_set_multicast_list(card->dev);
2167                 card->dev->flags |= IFF_UP;
2168                 netif_carrier_on(card->dev);
2169                 netif_wake_queue(card->dev);
2170                 card->state = DEV_STATE_UP;
2171         } else {
2172                 lcs_stopcard(card);
2173         }
2174
2175         if (lcs_register_netdev(ccwgdev) != 0)
2176                 goto out;
2177
2178         /* Print out supported assists: IPv6 */
2179         PRINT_INFO("LCS device %s %s IPv6 support\n", card->dev->name,
2180                    (card->ip_assists_supported & LCS_IPASS_IPV6_SUPPORT) ?
2181                    "with" : "without");
2182         /* Print out supported assist: Multicast */
2183         PRINT_INFO("LCS device %s %s Multicast support\n", card->dev->name,
2184                    (card->ip_assists_supported & LCS_IPASS_MULTICAST_SUPPORT) ?
2185                    "with" : "without");
2186         return 0;
2187 out:
2188
2189         ccw_device_set_offline(card->read.ccwdev);
2190         ccw_device_set_offline(card->write.ccwdev);
2191         return -ENODEV;
2192 }
2193
2194 /**
2195  * lcs_shutdown_device, called when setting the group device offline.
2196  */
2197 static int
2198 __lcs_shutdown_device(struct ccwgroup_device *ccwgdev, int recovery_mode)
2199 {
2200         struct lcs_card *card;
2201         enum lcs_dev_states recover_state;
2202         int ret;
2203
2204         LCS_DBF_TEXT(3, setup, "shtdndev");
2205         card = (struct lcs_card *)ccwgdev->dev.driver_data;
2206         if (!card)
2207                 return -ENODEV;
2208         if (recovery_mode == 0) {
2209                 lcs_set_allowed_threads(card, 0);
2210                 if (lcs_wait_for_threads(card, LCS_SET_MC_THREAD))
2211                         return -ERESTARTSYS;
2212         }
2213         LCS_DBF_HEX(3, setup, &card, sizeof(void*));
2214         recover_state = card->state;
2215
2216         ret = lcs_stop_device(card->dev);
2217         ret = ccw_device_set_offline(card->read.ccwdev);
2218         ret = ccw_device_set_offline(card->write.ccwdev);
2219         if (recover_state == DEV_STATE_UP) {
2220                 card->state = DEV_STATE_RECOVER;
2221         }
2222         if (ret)
2223                 return ret;
2224         return 0;
2225 }
2226
2227 static int
2228 lcs_shutdown_device(struct ccwgroup_device *ccwgdev)
2229 {
2230         return __lcs_shutdown_device(ccwgdev, 0);
2231 }
2232
2233 /**
2234  * drive lcs recovery after startup and startlan initiated by Lan Gateway
2235  */
2236 static int
2237 lcs_recovery(void *ptr)
2238 {
2239         struct lcs_card *card;
2240         struct ccwgroup_device *gdev;
2241         int rc;
2242
2243         card = (struct lcs_card *) ptr;
2244         daemonize("lcs_recover");
2245
2246         LCS_DBF_TEXT(4, trace, "recover1");
2247         if (!lcs_do_run_thread(card, LCS_RECOVERY_THREAD))
2248                 return 0;
2249         LCS_DBF_TEXT(4, trace, "recover2");
2250         gdev = card->gdev;
2251         PRINT_WARN("Recovery of device %s started...\n", dev_name(&gdev->dev));
2252         rc = __lcs_shutdown_device(gdev, 1);
2253         rc = lcs_new_device(gdev);
2254         if (!rc)
2255                 PRINT_INFO("Device %s successfully recovered!\n",
2256                                 card->dev->name);
2257         else
2258                 PRINT_INFO("Device %s could not be recovered!\n",
2259                                 card->dev->name);
2260         lcs_clear_thread_running_bit(card, LCS_RECOVERY_THREAD);
2261         return 0;
2262 }
2263
2264 /**
2265  * lcs_remove_device, free buffers and card
2266  */
2267 static void
2268 lcs_remove_device(struct ccwgroup_device *ccwgdev)
2269 {
2270         struct lcs_card *card;
2271
2272         card = (struct lcs_card *)ccwgdev->dev.driver_data;
2273         if (!card)
2274                 return;
2275
2276         LCS_DBF_TEXT(3, setup, "remdev");
2277         LCS_DBF_HEX(3, setup, &card, sizeof(void*));
2278         if (ccwgdev->state == CCWGROUP_ONLINE) {
2279                 lcs_shutdown_device(ccwgdev);
2280         }
2281         if (card->dev)
2282                 unregister_netdev(card->dev);
2283         sysfs_remove_group(&ccwgdev->dev.kobj, &lcs_attr_group);
2284         lcs_cleanup_card(card);
2285         lcs_free_card(card);
2286         put_device(&ccwgdev->dev);
2287 }
2288
2289 /**
2290  * LCS ccwgroup driver registration
2291  */
2292 static struct ccwgroup_driver lcs_group_driver = {
2293         .owner       = THIS_MODULE,
2294         .name        = "lcs",
2295         .max_slaves  = 2,
2296         .driver_id   = 0xD3C3E2,
2297         .probe       = lcs_probe_device,
2298         .remove      = lcs_remove_device,
2299         .set_online  = lcs_new_device,
2300         .set_offline = lcs_shutdown_device,
2301 };
2302
2303 /**
2304  *  LCS Module/Kernel initialization function
2305  */
2306 static int
2307 __init lcs_init_module(void)
2308 {
2309         int rc;
2310
2311         PRINT_INFO("Loading %s\n",version);
2312         rc = lcs_register_debug_facility();
2313         LCS_DBF_TEXT(0, setup, "lcsinit");
2314         if (rc) {
2315                 PRINT_ERR("Initialization failed\n");
2316                 return rc;
2317         }
2318
2319         rc = register_cu3088_discipline(&lcs_group_driver);
2320         if (rc) {
2321                 PRINT_ERR("Initialization failed\n");
2322                 return rc;
2323         }
2324         return 0;
2325 }
2326
2327
2328 /**
2329  *  LCS module cleanup function
2330  */
2331 static void
2332 __exit lcs_cleanup_module(void)
2333 {
2334         PRINT_INFO("Terminating lcs module.\n");
2335         LCS_DBF_TEXT(0, trace, "cleanup");
2336         unregister_cu3088_discipline(&lcs_group_driver);
2337         lcs_unregister_debug_facility();
2338 }
2339
2340 module_init(lcs_init_module);
2341 module_exit(lcs_cleanup_module);
2342
2343 MODULE_AUTHOR("Frank Pavlic <fpavlic@de.ibm.com>");
2344 MODULE_LICENSE("GPL");
2345