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