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