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