This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / drivers / s390 / net / lcs.c
1 /*
2  *  linux/drivers/s390/net/lcs.c
3  *
4  *  Linux for S/390 Lan Channel Station Network Driver
5  *
6  *  Copyright (C)  1999-2001 IBM Deutschland Entwicklung GmbH,
7  *                           IBM Corporation
8  *    Author(s): Original Code written by
9  *                        DJ Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
10  *               Rewritten by
11  *                        Frank Pavlic (pavlic@de.ibm.com) and
12  *                        Martin Schwidefsky <schwidefsky@de.ibm.com>
13  *
14  *    $Revision: 1.92 $  $Date: 2004/09/03 08:06:11 $
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.92 $"
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", 1, 1, 8);
97         lcs_dbf_trace = debug_register("lcs_trace", 1, 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->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         /* Print out supported assists: IPv6 */
1101         PRINT_INFO("LCS device %s %s IPv6 support\n", card->dev->name,
1102                    (card->ip_assists_supported & LCS_IPASS_IPV6_SUPPORT) ?
1103                    "with" : "without");
1104         /* Print out supported assist: Multicast */
1105         PRINT_INFO("LCS device %s %s Multicast support\n", card->dev->name,
1106                    (card->ip_assists_supported & LCS_IPASS_MULTICAST_SUPPORT) ?
1107                    "with" : "without");
1108         if (card->ip_assists_supported & LCS_IPASS_MULTICAST_SUPPORT)
1109                 return 0;
1110         return -EOPNOTSUPP;
1111 }
1112
1113 /**
1114  * set or del multicast address on LCS card
1115  */
1116 static void
1117 lcs_fix_multicast_list(struct lcs_card *card)
1118 {
1119         struct list_head failed_list;
1120         struct lcs_ipm_list *ipm, *tmp;
1121         unsigned long flags;
1122         int rc;
1123
1124         LCS_DBF_TEXT(4,trace, "fixipm");
1125         INIT_LIST_HEAD(&failed_list);
1126         spin_lock_irqsave(&card->ipm_lock, flags);
1127 list_modified:
1128         list_for_each_entry_safe(ipm, tmp, &card->ipm_list, list){
1129                 switch (ipm->ipm_state) {
1130                 case LCS_IPM_STATE_SET_REQUIRED:
1131                         /* del from ipm_list so noone else can tamper with
1132                          * this entry */
1133                         list_del_init(&ipm->list);
1134                         spin_unlock_irqrestore(&card->ipm_lock, flags);
1135                         rc = lcs_send_setipm(card, ipm);
1136                         spin_lock_irqsave(&card->ipm_lock, flags);
1137                         if (rc) {
1138                                 PRINT_INFO("Adding multicast address failed."
1139                                            "Table possibly full!\n");
1140                                 /* store ipm in failed list -> will be added
1141                                  * to ipm_list again, so a retry will be done
1142                                  * during the next call of this function */
1143                                 list_add_tail(&ipm->list, &failed_list);
1144                         } else {
1145                                 ipm->ipm_state = LCS_IPM_STATE_ON_CARD;
1146                                 /* re-insert into ipm_list */
1147                                 list_add_tail(&ipm->list, &card->ipm_list);
1148                         }
1149                         goto list_modified;
1150                 case LCS_IPM_STATE_DEL_REQUIRED:
1151                         list_del(&ipm->list);
1152                         spin_unlock_irqrestore(&card->ipm_lock, flags);
1153                         lcs_send_delipm(card, ipm);
1154                         spin_lock_irqsave(&card->ipm_lock, flags);
1155                         kfree(ipm);
1156                         goto list_modified;
1157                 case LCS_IPM_STATE_ON_CARD:
1158                         break;
1159                 }
1160         }
1161         /* re-insert all entries from the failed_list into ipm_list */
1162         list_for_each_entry(ipm, &failed_list, list) {
1163                 list_del_init(&ipm->list);
1164                 list_add_tail(&ipm->list, &card->ipm_list);
1165         }
1166         spin_unlock_irqrestore(&card->ipm_lock, flags);
1167         if (card->state == DEV_STATE_UP)
1168                 netif_wake_queue(card->dev);
1169 }
1170
1171 /**
1172  * get mac address for the relevant Multicast address
1173  */
1174 static void
1175 lcs_get_mac_for_ipm(__u32 ipm, char *mac, struct net_device *dev)
1176 {
1177         LCS_DBF_TEXT(4,trace, "getmac");
1178         if (dev->type == ARPHRD_IEEE802_TR)
1179                 ip_tr_mc_map(ipm, mac);
1180         else
1181                 ip_eth_mc_map(ipm, mac);
1182 }
1183
1184 /**
1185  * function called by net device to handle multicast address relevant things
1186  */
1187 static inline void
1188 lcs_remove_mc_addresses(struct lcs_card *card, struct in_device *in4_dev)
1189 {
1190         struct ip_mc_list *im4;
1191         struct list_head *l;
1192         struct lcs_ipm_list *ipm;
1193         unsigned long flags;
1194         char buf[MAX_ADDR_LEN];
1195
1196         LCS_DBF_TEXT(4, trace, "remmclst");
1197         spin_lock_irqsave(&card->ipm_lock, flags);
1198         list_for_each(l, &card->ipm_list) {
1199                 ipm = list_entry(l, struct lcs_ipm_list, list);
1200                 for (im4 = in4_dev->mc_list; im4 != NULL; im4 = im4->next) {
1201                         lcs_get_mac_for_ipm(im4->multiaddr, buf, card->dev);
1202                         if ( (ipm->ipm.ip_addr == im4->multiaddr) &&
1203                              (memcmp(buf, &ipm->ipm.mac_addr,
1204                                      LCS_MAC_LENGTH) == 0) )
1205                                 break;
1206                 }
1207                 if (im4 == NULL)
1208                         ipm->ipm_state = LCS_IPM_STATE_DEL_REQUIRED;
1209         }
1210         spin_unlock_irqrestore(&card->ipm_lock, flags);
1211 }
1212
1213 static inline struct lcs_ipm_list *
1214 lcs_check_addr_entry(struct lcs_card *card, struct ip_mc_list *im4, char *buf)
1215 {
1216         struct lcs_ipm_list *tmp, *ipm = NULL;
1217         struct list_head *l;
1218         unsigned long flags;
1219
1220         LCS_DBF_TEXT(4, trace, "chkmcent");
1221         spin_lock_irqsave(&card->ipm_lock, flags);
1222         list_for_each(l, &card->ipm_list) {
1223                 tmp = list_entry(l, struct lcs_ipm_list, list);
1224                 if ( (tmp->ipm.ip_addr == im4->multiaddr) &&
1225                      (memcmp(buf, &tmp->ipm.mac_addr,
1226                              LCS_MAC_LENGTH) == 0) ) {
1227                         ipm = tmp;
1228                         break;
1229                 }
1230         }
1231         spin_unlock_irqrestore(&card->ipm_lock, flags);
1232         return ipm;
1233 }
1234
1235 static inline void
1236 lcs_set_mc_addresses(struct lcs_card *card, struct in_device *in4_dev)
1237 {
1238
1239         struct ip_mc_list *im4;
1240         struct lcs_ipm_list *ipm;
1241         char buf[MAX_ADDR_LEN];
1242         unsigned long flags;
1243
1244         LCS_DBF_TEXT(4, trace, "setmclst");
1245         for (im4 = in4_dev->mc_list; im4; im4 = im4->next) {
1246                 lcs_get_mac_for_ipm(im4->multiaddr, buf, card->dev);
1247                 ipm = lcs_check_addr_entry(card, im4, buf);
1248                 if (ipm != NULL)
1249                         continue;       /* Address already in list. */
1250                 ipm = (struct lcs_ipm_list *)
1251                         kmalloc(sizeof(struct lcs_ipm_list), GFP_ATOMIC);
1252                 if (ipm == NULL) {
1253                         PRINT_INFO("Not enough memory to add "
1254                                    "new multicast entry!\n");
1255                         break;
1256                 }
1257                 memset(ipm, 0, sizeof(struct lcs_ipm_list));
1258                 memcpy(&ipm->ipm.mac_addr, buf, LCS_MAC_LENGTH);
1259                 ipm->ipm.ip_addr = im4->multiaddr;
1260                 ipm->ipm_state = LCS_IPM_STATE_SET_REQUIRED;
1261                 spin_lock_irqsave(&card->ipm_lock, flags);
1262                 list_add(&ipm->list, &card->ipm_list);
1263                 spin_unlock_irqrestore(&card->ipm_lock, flags);
1264         }
1265 }
1266
1267 static int
1268 lcs_register_mc_addresses(void *data)
1269 {
1270         struct lcs_card *card;
1271         struct in_device *in4_dev;
1272
1273         card = (struct lcs_card *) data;
1274         daemonize("regipm");
1275
1276         if (!lcs_do_run_thread(card, LCS_SET_MC_THREAD))
1277                 return 0;
1278         LCS_DBF_TEXT(4, trace, "regmulti");
1279
1280         in4_dev = in_dev_get(card->dev);
1281         if (in4_dev == NULL)
1282                 goto out;
1283         read_lock(&in4_dev->mc_list_lock);
1284         lcs_remove_mc_addresses(card,in4_dev);
1285         lcs_set_mc_addresses(card, in4_dev);
1286         read_unlock(&in4_dev->mc_list_lock);
1287         in_dev_put(in4_dev);
1288
1289         lcs_fix_multicast_list(card);
1290 out:
1291         lcs_clear_thread_running_bit(card, LCS_SET_MC_THREAD);
1292         return 0;
1293 }
1294 /**
1295  * function called by net device to
1296  * handle multicast address relevant things
1297  */
1298 static void
1299 lcs_set_multicast_list(struct net_device *dev)
1300 {
1301         struct lcs_card *card;
1302
1303         LCS_DBF_TEXT(4, trace, "setmulti");
1304         card = (struct lcs_card *) dev->priv;
1305
1306         if (!lcs_set_thread_start_bit(card, LCS_SET_MC_THREAD)) {
1307                 schedule_work(&card->kernel_thread_starter);
1308         }
1309 }
1310
1311 #endif /* CONFIG_IP_MULTICAST */
1312
1313 static long
1314 lcs_check_irb_error(struct ccw_device *cdev, struct irb *irb)
1315 {
1316         if (!IS_ERR(irb))
1317                 return 0;
1318
1319         switch (PTR_ERR(irb)) {
1320         case -EIO:
1321                 PRINT_WARN("i/o-error on device %s\n", cdev->dev.bus_id);
1322                 LCS_DBF_TEXT(2, trace, "ckirberr");
1323                 LCS_DBF_TEXT_(2, trace, "  rc%d", -EIO);
1324                 break;
1325         case -ETIMEDOUT:
1326                 PRINT_WARN("timeout on device %s\n", cdev->dev.bus_id);
1327                 LCS_DBF_TEXT(2, trace, "ckirberr");
1328                 LCS_DBF_TEXT_(2, trace, "  rc%d", -ETIMEDOUT);
1329                 break;
1330         default:
1331                 PRINT_WARN("unknown error %ld on device %s\n", PTR_ERR(irb),
1332                            cdev->dev.bus_id);
1333                 LCS_DBF_TEXT(2, trace, "ckirberr");
1334                 LCS_DBF_TEXT(2, trace, "  rc???");
1335         }
1336         return PTR_ERR(irb);
1337 }
1338
1339
1340 /**
1341  * IRQ Handler for LCS channels
1342  */
1343 static void
1344 lcs_irq(struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
1345 {
1346         struct lcs_card *card;
1347         struct lcs_channel *channel;
1348         int index;
1349
1350         if (lcs_check_irb_error(cdev, irb))
1351                 return;
1352
1353         card = CARD_FROM_DEV(cdev);
1354         if (card->read.ccwdev == cdev)
1355                 channel = &card->read;
1356         else
1357                 channel = &card->write;
1358
1359         LCS_DBF_TEXT_(5, trace, "Rint%s",cdev->dev.bus_id);
1360         LCS_DBF_TEXT_(5, trace, "%4x%4x",irb->scsw.cstat, irb->scsw.dstat);
1361         LCS_DBF_TEXT_(5, trace, "%4x%4x",irb->scsw.fctl, irb->scsw.actl);
1362
1363         /* How far in the ccw chain have we processed? */
1364         if ((channel->state != CH_STATE_INIT) &&
1365             (irb->scsw.fctl & SCSW_FCTL_START_FUNC)) {
1366                 index = (struct ccw1 *) __va((addr_t) irb->scsw.cpa) 
1367                         - channel->ccws;
1368                 if ((irb->scsw.actl & SCSW_ACTL_SUSPENDED) ||
1369                     (irb->scsw.cstat | SCHN_STAT_PCI))
1370                         /* Bloody io subsystem tells us lies about cpa... */
1371                         index = (index - 1) & (LCS_NUM_BUFFS - 1);
1372                 while (channel->io_idx != index) {
1373                         __lcs_processed_buffer(channel,
1374                                                channel->iob + channel->io_idx);
1375                         channel->io_idx =
1376                                 (channel->io_idx + 1) & (LCS_NUM_BUFFS - 1);
1377                 }
1378         }
1379
1380         if ((irb->scsw.dstat & DEV_STAT_DEV_END) ||
1381             (irb->scsw.dstat & DEV_STAT_CHN_END) ||
1382             (irb->scsw.dstat & DEV_STAT_UNIT_CHECK))
1383                 /* Mark channel as stopped. */
1384                 channel->state = CH_STATE_STOPPED;
1385         else if (irb->scsw.actl & SCSW_ACTL_SUSPENDED)
1386                 /* CCW execution stopped on a suspend bit. */
1387                 channel->state = CH_STATE_SUSPENDED;
1388
1389         if (irb->scsw.fctl & SCSW_FCTL_HALT_FUNC) {
1390                 if (irb->scsw.cc != 0) {
1391                         ccw_device_halt(channel->ccwdev, (addr_t) channel);
1392                         return;
1393                 }
1394                 /* The channel has been stopped by halt_IO. */
1395                 channel->state = CH_STATE_HALTED;
1396         }
1397
1398         if (irb->scsw.fctl & SCSW_FCTL_CLEAR_FUNC) {
1399                 channel->state = CH_STATE_CLEARED;
1400         }
1401         /* Do the rest in the tasklet. */
1402         tasklet_schedule(&channel->irq_tasklet);
1403 }
1404
1405 /**
1406  * Tasklet for IRQ handler
1407  */
1408 static void
1409 lcs_tasklet(unsigned long data)
1410 {
1411         unsigned long flags;
1412         struct lcs_channel *channel;
1413         struct lcs_buffer *iob;
1414         int buf_idx;
1415         int rc;
1416
1417         channel = (struct lcs_channel *) data;
1418         LCS_DBF_TEXT_(5, trace, "tlet%s",channel->ccwdev->dev.bus_id);
1419
1420         /* Check for processed buffers. */
1421         iob = channel->iob;
1422         buf_idx = channel->buf_idx;
1423         while (iob[buf_idx].state == BUF_STATE_PROCESSED) {
1424                 /* Do the callback thing. */
1425                 if (iob[buf_idx].callback != NULL)
1426                         iob[buf_idx].callback(channel, iob + buf_idx);
1427                 buf_idx = (buf_idx + 1) & (LCS_NUM_BUFFS - 1);
1428         }
1429         channel->buf_idx = buf_idx;
1430
1431         if (channel->state == CH_STATE_STOPPED)
1432                 // FIXME: what if rc != 0 ??
1433                 rc = lcs_start_channel(channel);
1434         spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1435         if (channel->state == CH_STATE_SUSPENDED &&
1436             channel->iob[channel->io_idx].state == BUF_STATE_READY) {
1437                 // FIXME: what if rc != 0 ??
1438                 rc = __lcs_resume_channel(channel);
1439         }
1440         spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1441
1442         /* Something happened on the channel. Wake up waiters. */
1443         wake_up(&channel->wait_q);
1444 }
1445
1446 /**
1447  * Finish current tx buffer and make it ready for transmit.
1448  */
1449 static void
1450 __lcs_emit_txbuffer(struct lcs_card *card)
1451 {
1452         LCS_DBF_TEXT(5, trace, "emittx");
1453         *(__u16 *)(card->tx_buffer->data + card->tx_buffer->count) = 0;
1454         card->tx_buffer->count += 2;
1455         lcs_ready_buffer(&card->write, card->tx_buffer);
1456         card->tx_buffer = NULL;
1457         card->tx_emitted++;
1458 }
1459
1460 /**
1461  * Callback for finished tx buffers.
1462  */
1463 static void
1464 lcs_txbuffer_cb(struct lcs_channel *channel, struct lcs_buffer *buffer)
1465 {
1466         struct lcs_card *card;
1467
1468         LCS_DBF_TEXT(5, trace, "txbuffcb");
1469         /* Put buffer back to pool. */
1470         lcs_release_buffer(channel, buffer);
1471         card = (struct lcs_card *)
1472                 ((char *) channel - offsetof(struct lcs_card, write));
1473         spin_lock(&card->lock);
1474         card->tx_emitted--;
1475         if (card->tx_emitted <= 0 && card->tx_buffer != NULL)
1476                 /*
1477                  * Last running tx buffer has finished. Submit partially
1478                  * filled current buffer.
1479                  */
1480                 __lcs_emit_txbuffer(card);
1481         spin_unlock(&card->lock);
1482 }
1483
1484 /**
1485  * Packet transmit function called by network stack
1486  */
1487 static int
1488 __lcs_start_xmit(struct lcs_card *card, struct sk_buff *skb,
1489                  struct net_device *dev)
1490 {
1491         struct lcs_header *header;
1492
1493         LCS_DBF_TEXT(5, trace, "hardxmit");
1494         if (skb == NULL) {
1495                 card->stats.tx_dropped++;
1496                 card->stats.tx_errors++;
1497                 return -EIO;
1498         }
1499         if (card->state != DEV_STATE_UP) {
1500                 dev_kfree_skb(skb);
1501                 card->stats.tx_dropped++;
1502                 card->stats.tx_errors++;
1503                 card->stats.tx_carrier_errors++;
1504                 return 0;
1505         }
1506         if (netif_queue_stopped(dev) ) {
1507                 card->stats.tx_dropped++;
1508                 return -EBUSY;
1509         }
1510         if (card->tx_buffer != NULL &&
1511             card->tx_buffer->count + sizeof(struct lcs_header) +
1512             skb->len + sizeof(u16) > LCS_IOBUFFERSIZE)
1513                 /* skb too big for current tx buffer. */
1514                 __lcs_emit_txbuffer(card);
1515         if (card->tx_buffer == NULL) {
1516                 /* Get new tx buffer */
1517                 card->tx_buffer = lcs_get_buffer(&card->write);
1518                 if (card->tx_buffer == NULL) {
1519                         card->stats.tx_dropped++;
1520                         return -EBUSY;
1521                 }
1522                 card->tx_buffer->callback = lcs_txbuffer_cb;
1523                 card->tx_buffer->count = 0;
1524         }
1525         header = (struct lcs_header *)
1526                 (card->tx_buffer->data + card->tx_buffer->count);
1527         card->tx_buffer->count += skb->len + sizeof(struct lcs_header);
1528         header->offset = card->tx_buffer->count;
1529         header->type = card->lan_type;
1530         header->slot = card->portno;
1531         memcpy(header + 1, skb->data, skb->len);
1532         card->stats.tx_bytes += skb->len;
1533         card->stats.tx_packets++;
1534         dev_kfree_skb(skb);
1535         if (card->tx_emitted <= 0)
1536                 /* If this is the first tx buffer emit it immediately. */
1537                 __lcs_emit_txbuffer(card);
1538         return 0;
1539 }
1540
1541 static int
1542 lcs_start_xmit(struct sk_buff *skb, struct net_device *dev)
1543 {
1544         struct lcs_card *card;
1545         int rc;
1546
1547         LCS_DBF_TEXT(5, trace, "pktxmit");
1548         card = (struct lcs_card *) dev->priv;
1549         spin_lock(&card->lock);
1550         rc = __lcs_start_xmit(card, skb, dev);
1551         spin_unlock(&card->lock);
1552         return rc;
1553 }
1554
1555 /**
1556  * send startlan and lanstat command to make LCS device ready
1557  */
1558 static int
1559 lcs_startlan_auto(struct lcs_card *card)
1560 {
1561         int rc;
1562
1563         LCS_DBF_TEXT(2, trace, "strtauto");
1564 #ifdef CONFIG_NET_ETHERNET
1565         card->lan_type = LCS_FRAME_TYPE_ENET;
1566         rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
1567         if (rc == 0)
1568                 return 0;
1569
1570 #endif
1571 #ifdef CONFIG_TR
1572         card->lan_type = LCS_FRAME_TYPE_TR;
1573         rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
1574         if (rc == 0)
1575                 return 0;
1576 #endif
1577 #ifdef CONFIG_FDDI
1578         card->lan_type = LCS_FRAME_TYPE_FDDI;
1579         rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
1580         if (rc == 0)
1581                 return 0;
1582 #endif
1583         return -EIO;
1584 }
1585
1586 static int
1587 lcs_startlan(struct lcs_card *card)
1588 {
1589         int rc, i;
1590
1591         LCS_DBF_TEXT(2, trace, "startlan");
1592         rc = 0;
1593         if (card->portno != LCS_INVALID_PORT_NO) {
1594                 if (card->lan_type == LCS_FRAME_TYPE_AUTO)
1595                         rc = lcs_startlan_auto(card);
1596                 else
1597                         rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
1598         } else {
1599                 for (i = 0; i <= 16; i++) {
1600                         card->portno = i;
1601                         if (card->lan_type != LCS_FRAME_TYPE_AUTO)
1602                                 rc = lcs_send_startlan(card,
1603                                                        LCS_INITIATOR_TCPIP);
1604                         else
1605                                 /* autodetecting lan type */
1606                                 rc = lcs_startlan_auto(card);
1607                         if (rc == 0)
1608                                 break;
1609                 }
1610         }
1611         if (rc == 0)
1612                 return lcs_send_lanstat(card);
1613         return rc;
1614 }
1615
1616 /**
1617  * LCS detect function
1618  * setup channels and make them I/O ready
1619  */
1620 static int
1621 lcs_detect(struct lcs_card *card)
1622 {
1623         int rc = 0;
1624
1625         LCS_DBF_TEXT(2, setup, "lcsdetct");
1626         /* start/reset card */
1627         if (card->dev)
1628                 netif_stop_queue(card->dev);
1629         rc = lcs_stop_channels(card);
1630         if (rc == 0) {
1631                 rc = lcs_start_channels(card);
1632                 if (rc == 0) {
1633                         rc = lcs_send_startup(card, LCS_INITIATOR_TCPIP);
1634                         if (rc == 0)
1635                                 rc = lcs_startlan(card);
1636                 }
1637         }
1638         if (rc == 0) {
1639                 card->state = DEV_STATE_UP;
1640         } else {
1641                 card->state = DEV_STATE_DOWN;
1642                 card->write.state = CH_STATE_INIT;
1643                 card->read.state =  CH_STATE_INIT;
1644         }
1645         return rc;
1646 }
1647
1648 /**
1649  * reset card
1650  */
1651 static int
1652 lcs_resetcard(struct lcs_card *card)
1653 {
1654         int retries;
1655
1656         LCS_DBF_TEXT(2, trace, "rescard");
1657         for (retries = 0; retries < 10; retries++) {
1658                 if (lcs_detect(card) == 0) {
1659                         netif_wake_queue(card->dev);
1660                         card->state = DEV_STATE_UP;
1661                         PRINT_INFO("LCS device %s successfully restarted!\n",
1662                                    card->dev->name);
1663                         return 0;
1664                 }
1665                 msleep(3000);
1666         }
1667         PRINT_ERR("Error in Reseting LCS card!\n");
1668         return -EIO;
1669 }
1670
1671
1672 /**
1673  * LCS Stop card
1674  */
1675 static int
1676 lcs_stopcard(struct lcs_card *card)
1677 {
1678         int rc;
1679
1680         LCS_DBF_TEXT(3, setup, "stopcard");
1681
1682         if (card->read.state != CH_STATE_STOPPED &&
1683             card->write.state != CH_STATE_STOPPED &&
1684             card->state == DEV_STATE_UP) {
1685                 lcs_clear_multicast_list(card);
1686                 rc = lcs_send_stoplan(card,LCS_INITIATOR_TCPIP);
1687                 rc = lcs_send_shutdown(card);
1688         }
1689         rc = lcs_stop_channels(card);
1690         card->state = DEV_STATE_DOWN;
1691
1692         return rc;
1693 }
1694
1695 /**
1696  * LGW initiated commands
1697  */
1698 static int
1699 lcs_lgw_startlan_thread(void *data)
1700 {
1701         struct lcs_card *card;
1702
1703         card = (struct lcs_card *) data;
1704         daemonize("lgwstpln");
1705
1706         if (!lcs_do_run_thread(card, LCS_STARTLAN_THREAD))
1707                 return 0;
1708         LCS_DBF_TEXT(4, trace, "lgwstpln");
1709         if (card->dev)
1710                 netif_stop_queue(card->dev);
1711         if (lcs_startlan(card) == 0) {
1712                 netif_wake_queue(card->dev);
1713                 card->state = DEV_STATE_UP;
1714                 PRINT_INFO("LCS Startlan for device %s succeeded!\n",
1715                            card->dev->name);
1716
1717         } else
1718                 PRINT_ERR("LCS Startlan for device %s failed!\n",
1719                           card->dev->name);
1720         lcs_clear_thread_running_bit(card, LCS_STARTLAN_THREAD);
1721         return 0;
1722 }
1723
1724 /**
1725  * Send startup command initiated by Lan Gateway
1726  */
1727 static int
1728 lcs_lgw_startup_thread(void *data)
1729 {
1730         int rc;
1731
1732         struct lcs_card *card;
1733
1734         card = (struct lcs_card *) data;
1735         daemonize("lgwstaln");
1736
1737         if (!lcs_do_run_thread(card, LCS_STARTUP_THREAD))
1738                 return 0;
1739         LCS_DBF_TEXT(4, trace, "lgwstaln");
1740         if (card->dev)
1741                 netif_stop_queue(card->dev);
1742         rc = lcs_send_startup(card, LCS_INITIATOR_LGW);
1743         if (rc != 0) {
1744                 PRINT_ERR("Startup for LCS device %s initiated " \
1745                           "by LGW failed!\nReseting card ...\n",
1746                           card->dev->name);
1747                 /* do a card reset */
1748                 rc = lcs_resetcard(card);
1749                 if (rc == 0)
1750                         goto Done;
1751         }
1752         rc = lcs_startlan(card);
1753         if (rc == 0) {
1754                 netif_wake_queue(card->dev);
1755                 card->state = DEV_STATE_UP;
1756         }
1757 Done:
1758         if (rc == 0)
1759                 PRINT_INFO("LCS Startup for device %s succeeded!\n",
1760                            card->dev->name);
1761         else
1762                 PRINT_ERR("LCS Startup for device %s failed!\n",
1763                           card->dev->name);
1764         lcs_clear_thread_running_bit(card, LCS_STARTUP_THREAD);
1765         return 0;
1766 }
1767
1768
1769 /**
1770  * send stoplan command initiated by Lan Gateway
1771  */
1772 static int
1773 lcs_lgw_stoplan_thread(void *data)
1774 {
1775         struct lcs_card *card;
1776         int rc;
1777
1778         card = (struct lcs_card *) data;
1779         daemonize("lgwstop");
1780
1781         if (!lcs_do_run_thread(card, LCS_STOPLAN_THREAD))
1782                 return 0;
1783         LCS_DBF_TEXT(4, trace, "lgwstop");
1784         if (card->dev)
1785                 netif_stop_queue(card->dev);
1786         if (lcs_send_stoplan(card, LCS_INITIATOR_LGW) == 0)
1787                 PRINT_INFO("Stoplan for %s initiated by LGW succeeded!\n",
1788                            card->dev->name);
1789         else
1790                 PRINT_ERR("Stoplan %s initiated by LGW failed!\n",
1791                           card->dev->name);
1792         /*Try to reset the card, stop it on failure */
1793         rc = lcs_resetcard(card);
1794         if (rc != 0)
1795                 rc = lcs_stopcard(card);
1796         lcs_clear_thread_running_bit(card, LCS_STOPLAN_THREAD);
1797         return rc;
1798 }
1799
1800 /**
1801  * Kernel Thread helper functions for LGW initiated commands
1802  */
1803 static void
1804 lcs_start_kernel_thread(struct lcs_card *card)
1805 {
1806         LCS_DBF_TEXT(5, trace, "krnthrd");
1807         if (lcs_do_start_thread(card, LCS_STARTUP_THREAD))
1808                 kernel_thread(lcs_lgw_startup_thread, (void *) card, SIGCHLD);
1809         if (lcs_do_start_thread(card, LCS_STARTLAN_THREAD))
1810                 kernel_thread(lcs_lgw_startlan_thread, (void *) card, SIGCHLD);
1811         if (lcs_do_start_thread(card, LCS_STOPLAN_THREAD))
1812                 kernel_thread(lcs_lgw_stoplan_thread, (void *) card, SIGCHLD);
1813 #ifdef CONFIG_IP_MULTICAST
1814         if (lcs_do_start_thread(card, LCS_SET_MC_THREAD))
1815                 kernel_thread(lcs_register_mc_addresses, (void *) card, SIGCHLD);
1816 #endif
1817 }
1818
1819 /**
1820  * Process control frames.
1821  */
1822 static void
1823 lcs_get_control(struct lcs_card *card, struct lcs_cmd *cmd)
1824 {
1825         LCS_DBF_TEXT(5, trace, "getctrl");
1826         if (cmd->initiator == LCS_INITIATOR_LGW) {
1827                 switch(cmd->cmd_code) {
1828                 case LCS_CMD_STARTUP:
1829                         if (!lcs_set_thread_start_bit(card,
1830                                                       LCS_STARTUP_THREAD))
1831                                 schedule_work(&card->kernel_thread_starter);
1832                         break;
1833                 case LCS_CMD_STARTLAN:
1834                         if (!lcs_set_thread_start_bit(card,
1835                                                       LCS_STARTLAN_THREAD))
1836                                 schedule_work(&card->kernel_thread_starter);
1837                         break;
1838                 case LCS_CMD_STOPLAN:
1839                         if (!lcs_set_thread_start_bit(card,
1840                                                       LCS_STOPLAN_THREAD))
1841                                 schedule_work(&card->kernel_thread_starter);
1842                         break;
1843                 default:
1844                         PRINT_INFO("UNRECOGNIZED LGW COMMAND\n");
1845                         break;
1846                 }
1847         } else
1848                 lcs_notify_lancmd_waiters(card, cmd);
1849 }
1850
1851 /**
1852  * Unpack network packet.
1853  */
1854 static void
1855 lcs_get_skb(struct lcs_card *card, char *skb_data, unsigned int skb_len)
1856 {
1857         struct sk_buff *skb;
1858
1859         LCS_DBF_TEXT(5, trace, "getskb");
1860         if (card->dev == NULL ||
1861             card->state != DEV_STATE_UP)
1862                 /* The card isn't up. Ignore the packet. */
1863                 return;
1864
1865         skb = dev_alloc_skb(skb_len);
1866         if (skb == NULL) {
1867                 PRINT_ERR("LCS: alloc_skb failed for device=%s\n",
1868                           card->dev->name);
1869                 card->stats.rx_dropped++;
1870                 return;
1871         }
1872         skb->dev = card->dev;
1873         memcpy(skb_put(skb, skb_len), skb_data, skb_len);
1874         skb->protocol = card->lan_type_trans(skb, card->dev);
1875         card->stats.rx_bytes += skb_len;
1876         card->stats.rx_packets++;
1877         netif_rx(skb);
1878 }
1879
1880 /**
1881  * LCS main routine to get packets and lancmd replies from the buffers
1882  */
1883 static void
1884 lcs_get_frames_cb(struct lcs_channel *channel, struct lcs_buffer *buffer)
1885 {
1886         struct lcs_card *card;
1887         struct lcs_header *lcs_hdr;
1888         __u16 offset;
1889
1890         LCS_DBF_TEXT(5, trace, "lcsgtpkt");
1891         lcs_hdr = (struct lcs_header *) buffer->data;
1892         if (lcs_hdr->offset == LCS_ILLEGAL_OFFSET) {
1893                 LCS_DBF_TEXT(4, trace, "-eiogpkt");
1894                 return;
1895         }
1896         card = (struct lcs_card *)
1897                 ((char *) channel - offsetof(struct lcs_card, read));
1898         offset = 0;
1899         while (lcs_hdr->offset != 0) {
1900                 if (lcs_hdr->offset <= 0 ||
1901                     lcs_hdr->offset > LCS_IOBUFFERSIZE ||
1902                     lcs_hdr->offset < offset) {
1903                         /* Offset invalid. */
1904                         card->stats.rx_length_errors++;
1905                         card->stats.rx_errors++;
1906                         return;
1907                 }
1908                 /* What kind of frame is it? */
1909                 if (lcs_hdr->type == LCS_FRAME_TYPE_CONTROL)
1910                         /* Control frame. */
1911                         lcs_get_control(card, (struct lcs_cmd *) lcs_hdr);
1912                 else if (lcs_hdr->type == LCS_FRAME_TYPE_ENET ||
1913                          lcs_hdr->type == LCS_FRAME_TYPE_TR ||
1914                          lcs_hdr->type == LCS_FRAME_TYPE_FDDI)
1915                         /* Normal network packet. */
1916                         lcs_get_skb(card, (char *)(lcs_hdr + 1),
1917                                     lcs_hdr->offset - offset -
1918                                     sizeof(struct lcs_header));
1919                 else
1920                         /* Unknown frame type. */
1921                         ; // FIXME: error message ?
1922                 /* Proceed to next frame. */
1923                 offset = lcs_hdr->offset;
1924                 lcs_hdr->offset = LCS_ILLEGAL_OFFSET;
1925                 lcs_hdr = (struct lcs_header *) (buffer->data + offset);
1926         }
1927         /* The buffer is now empty. Make it ready again. */
1928         lcs_ready_buffer(&card->read, buffer);
1929 }
1930
1931 /**
1932  * get network statistics for ifconfig and other user programs
1933  */
1934 static struct net_device_stats *
1935 lcs_getstats(struct net_device *dev)
1936 {
1937         struct lcs_card *card;
1938
1939         LCS_DBF_TEXT(4, trace, "netstats");
1940         card = (struct lcs_card *) dev->priv;
1941         return &card->stats;
1942 }
1943
1944 /**
1945  * stop lcs device
1946  * This function will be called by user doing ifconfig xxx down
1947  */
1948 static int
1949 lcs_stop_device(struct net_device *dev)
1950 {
1951         struct lcs_card *card;
1952         int rc;
1953
1954         LCS_DBF_TEXT(2, trace, "stopdev");
1955         card   = (struct lcs_card *) dev->priv;
1956         netif_stop_queue(dev);
1957         dev->flags &= ~IFF_UP;
1958         rc = lcs_stopcard(card);
1959         if (rc)
1960                 PRINT_ERR("Try it again!\n ");
1961         return rc;
1962 }
1963
1964 /**
1965  * start lcs device and make it runnable
1966  * This function will be called by user doing ifconfig xxx up
1967  */
1968 static int
1969 lcs_open_device(struct net_device *dev)
1970 {
1971         struct lcs_card *card;
1972         int rc;
1973
1974         LCS_DBF_TEXT(2, trace, "opendev");
1975         card = (struct lcs_card *) dev->priv;
1976         /* initialize statistics */
1977         rc = lcs_detect(card);
1978         if (rc) {
1979                 PRINT_ERR("LCS:Error in opening device!\n");
1980
1981         } else {
1982                 dev->flags |= IFF_UP;
1983                 netif_wake_queue(dev);
1984                 card->state = DEV_STATE_UP;
1985         }
1986         return rc;
1987 }
1988
1989 /**
1990  * show function for portno called by cat or similar things
1991  */
1992 static ssize_t
1993 lcs_portno_show (struct device *dev, char *buf)
1994 {
1995         struct lcs_card *card;
1996
1997         card = (struct lcs_card *)dev->driver_data;
1998
1999         if (!card)
2000                 return 0;
2001
2002         return sprintf(buf, "%d\n", card->portno);
2003 }
2004
2005 /**
2006  * store the value which is piped to file portno
2007  */
2008 static ssize_t
2009 lcs_portno_store (struct device *dev, const char *buf, size_t count)
2010 {
2011         struct lcs_card *card;
2012         int value;
2013
2014         card = (struct lcs_card *)dev->driver_data;
2015
2016         if (!card)
2017                 return 0;
2018
2019         sscanf(buf, "%u", &value);
2020         /* TODO: sanity checks */
2021         card->portno = value;
2022
2023         return count;
2024
2025 }
2026
2027 static DEVICE_ATTR(portno, 0644, lcs_portno_show, lcs_portno_store);
2028
2029 static ssize_t
2030 lcs_type_show(struct device *dev, char *buf)
2031 {
2032         struct ccwgroup_device *cgdev;
2033
2034         cgdev = to_ccwgroupdev(dev);
2035         if (!cgdev)
2036                 return -ENODEV;
2037
2038         return sprintf(buf, "%s\n", cu3088_type[cgdev->cdev[0]->id.driver_info]);
2039 }
2040
2041 static DEVICE_ATTR(type, 0444, lcs_type_show, NULL);
2042
2043 static ssize_t
2044 lcs_timeout_show(struct device *dev, char *buf)
2045 {
2046         struct lcs_card *card;
2047
2048         card = (struct lcs_card *)dev->driver_data;
2049
2050         return card ? sprintf(buf, "%u\n", card->lancmd_timeout) : 0;
2051 }
2052
2053 static ssize_t
2054 lcs_timeout_store (struct device *dev, const char *buf, size_t count)
2055 {
2056         struct lcs_card *card;
2057         int value;
2058
2059         card = (struct lcs_card *)dev->driver_data;
2060
2061         if (!card)
2062                 return 0;
2063
2064         sscanf(buf, "%u", &value);
2065         /* TODO: sanity checks */
2066         card->lancmd_timeout = value;
2067
2068         return count;
2069
2070 }
2071
2072 DEVICE_ATTR(lancmd_timeout, 0644, lcs_timeout_show, lcs_timeout_store);
2073
2074 static struct attribute * lcs_attrs[] = {
2075         &dev_attr_portno.attr,
2076         &dev_attr_type.attr,
2077         &dev_attr_lancmd_timeout.attr,
2078         NULL,
2079 };
2080
2081 static struct attribute_group lcs_attr_group = {
2082         .attrs = lcs_attrs,
2083 };
2084
2085 /**
2086  * lcs_probe_device is called on establishing a new ccwgroup_device.
2087  */
2088 static int
2089 lcs_probe_device(struct ccwgroup_device *ccwgdev)
2090 {
2091         struct lcs_card *card;
2092         int ret;
2093
2094         if (!get_device(&ccwgdev->dev))
2095                 return -ENODEV;
2096
2097         LCS_DBF_TEXT(2, setup, "add_dev");
2098         card = lcs_alloc_card();
2099         if (!card) {
2100                 PRINT_ERR("Allocation of lcs card failed\n");
2101                 put_device(&ccwgdev->dev);
2102                 return -ENOMEM;
2103         }
2104         ret = sysfs_create_group(&ccwgdev->dev.kobj, &lcs_attr_group);
2105         if (ret) {
2106                 PRINT_ERR("Creating attributes failed");
2107                 lcs_free_card(card);
2108                 put_device(&ccwgdev->dev);
2109                 return ret;
2110         }
2111         ccwgdev->dev.driver_data = card;
2112         ccwgdev->cdev[0]->handler = lcs_irq;
2113         ccwgdev->cdev[1]->handler = lcs_irq;
2114         return 0;
2115 }
2116
2117 static int
2118 lcs_register_netdev(struct ccwgroup_device *ccwgdev)
2119 {
2120         struct lcs_card *card;
2121
2122         LCS_DBF_TEXT(2, setup, "regnetdv");
2123         card = (struct lcs_card *)ccwgdev->dev.driver_data;
2124         if (card->dev->reg_state != NETREG_UNINITIALIZED)
2125                 return 0;
2126         SET_NETDEV_DEV(card->dev, &ccwgdev->dev);
2127         return register_netdev(card->dev);
2128 }
2129
2130 /**
2131  * lcs_new_device will be called by setting the group device online.
2132  */
2133
2134 static int
2135 lcs_new_device(struct ccwgroup_device *ccwgdev)
2136 {
2137         struct  lcs_card *card;
2138         struct net_device *dev=NULL;
2139         enum lcs_dev_states recover_state;
2140         int rc;
2141
2142         card = (struct lcs_card *)ccwgdev->dev.driver_data;
2143         if (!card)
2144                 return -ENODEV;
2145
2146         LCS_DBF_TEXT(2, setup, "newdev");
2147         LCS_DBF_HEX(3, setup, &card, sizeof(void*));
2148         card->read.ccwdev  = ccwgdev->cdev[0];
2149         card->write.ccwdev = ccwgdev->cdev[1];
2150
2151         recover_state = card->state;
2152         ccw_device_set_online(card->read.ccwdev);
2153         ccw_device_set_online(card->write.ccwdev);
2154
2155         LCS_DBF_TEXT(3, setup, "lcsnewdv");
2156
2157         lcs_setup_card(card);
2158         rc = lcs_detect(card);
2159         if (rc) {
2160                 LCS_DBF_TEXT(2, setup, "dtctfail");
2161                 PRINT_WARN("Detection of LCS card failed with return code "
2162                            "%d (0x%x)\n", rc, rc);
2163                 lcs_stopcard(card);
2164                 goto out;
2165         }
2166         if (card->dev) {
2167                 LCS_DBF_TEXT(2, setup, "samedev");
2168                 LCS_DBF_HEX(3, setup, &card, sizeof(void*));
2169                 goto netdev_out;
2170         }
2171         switch (card->lan_type) {
2172 #ifdef CONFIG_NET_ETHERNET
2173         case LCS_FRAME_TYPE_ENET:
2174                 card->lan_type_trans = eth_type_trans;
2175                 dev = alloc_etherdev(0);
2176                 break;
2177 #endif
2178 #ifdef CONFIG_TR
2179         case LCS_FRAME_TYPE_TR:
2180                 card->lan_type_trans = tr_type_trans;
2181                 dev = alloc_trdev(0);
2182                 break;
2183 #endif
2184 #ifdef CONFIG_FDDI
2185         case LCS_FRAME_TYPE_FDDI:
2186                 card->lan_type_trans = fddi_type_trans;
2187                 dev = alloc_fddidev(0);
2188                 break;
2189 #endif
2190         default:
2191                 LCS_DBF_TEXT(3, setup, "errinit");
2192                 PRINT_ERR("LCS: Initialization failed\n");
2193                 PRINT_ERR("LCS: No device found!\n");
2194                 goto out;
2195         }
2196         if (!dev)
2197                 goto out;
2198         card->dev = dev;
2199 netdev_out:
2200         card->dev->priv = card;
2201         card->dev->open = lcs_open_device;
2202         card->dev->stop = lcs_stop_device;
2203         card->dev->hard_start_xmit = lcs_start_xmit;
2204         card->dev->get_stats = lcs_getstats;
2205         SET_MODULE_OWNER(dev);
2206         if (lcs_register_netdev(ccwgdev) != 0)
2207                 goto out;
2208         memcpy(card->dev->dev_addr, card->mac, LCS_MAC_LENGTH);
2209 #ifdef CONFIG_IP_MULTICAST
2210         if (!lcs_check_multicast_support(card))
2211                 card->dev->set_multicast_list = lcs_set_multicast_list;
2212 #endif
2213         netif_stop_queue(card->dev);
2214         lcs_set_allowed_threads(card,0xffffffff);
2215         if (recover_state == DEV_STATE_RECOVER) {
2216                 lcs_set_multicast_list(card->dev);
2217                 card->dev->flags |= IFF_UP;
2218                 netif_wake_queue(card->dev);
2219                 card->state = DEV_STATE_UP;
2220         } else
2221                 lcs_stopcard(card);
2222
2223         return 0;
2224 out:
2225
2226         ccw_device_set_offline(card->read.ccwdev);
2227         ccw_device_set_offline(card->write.ccwdev);
2228         return -ENODEV;
2229 }
2230
2231 /**
2232  * lcs_shutdown_device, called when setting the group device offline.
2233  */
2234 static int
2235 lcs_shutdown_device(struct ccwgroup_device *ccwgdev)
2236 {
2237         struct lcs_card *card;
2238         enum lcs_dev_states recover_state;
2239         int ret;
2240
2241         LCS_DBF_TEXT(3, setup, "shtdndev");
2242         card = (struct lcs_card *)ccwgdev->dev.driver_data;
2243         if (!card)
2244                 return -ENODEV;
2245         lcs_set_allowed_threads(card, 0);
2246         if (lcs_wait_for_threads(card, LCS_SET_MC_THREAD))
2247                 return -ERESTARTSYS;
2248         LCS_DBF_HEX(3, setup, &card, sizeof(void*));
2249         recover_state = card->state;
2250
2251         ret = lcs_stop_device(card->dev);
2252         ret = ccw_device_set_offline(card->read.ccwdev);
2253         ret = ccw_device_set_offline(card->write.ccwdev);
2254         if (recover_state == DEV_STATE_UP) {
2255                 card->state = DEV_STATE_RECOVER;
2256         }
2257         if (ret)
2258                 return ret;
2259         return 0;
2260 }
2261
2262 /**
2263  * lcs_remove_device, free buffers and card
2264  */
2265 static void
2266 lcs_remove_device(struct ccwgroup_device *ccwgdev)
2267 {
2268         struct lcs_card *card;
2269
2270         card = (struct lcs_card *)ccwgdev->dev.driver_data;
2271         if (!card)
2272                 return;
2273
2274         PRINT_INFO("Removing lcs group device ....\n");
2275         LCS_DBF_TEXT(3, setup, "remdev");
2276         LCS_DBF_HEX(3, setup, &card, sizeof(void*));
2277         if (ccwgdev->state == CCWGROUP_ONLINE) {
2278                 lcs_shutdown_device(ccwgdev);
2279         }
2280         if (card->dev)
2281                 unregister_netdev(card->dev);
2282         sysfs_remove_group(&ccwgdev->dev.kobj, &lcs_attr_group);
2283         lcs_cleanup_card(card);
2284         lcs_free_card(card);
2285         put_device(&ccwgdev->dev);
2286 }
2287
2288 /**
2289  * LCS ccwgroup driver registration
2290  */
2291 static struct ccwgroup_driver lcs_group_driver = {
2292         .owner       = THIS_MODULE,
2293         .name        = "lcs",
2294         .max_slaves  = 2,
2295         .driver_id   = 0xD3C3E2,
2296         .probe       = lcs_probe_device,
2297         .remove      = lcs_remove_device,
2298         .set_online  = lcs_new_device,
2299         .set_offline = lcs_shutdown_device,
2300 };
2301
2302 /**
2303  *  LCS Module/Kernel initialization function
2304  */
2305 static int
2306 __init lcs_init_module(void)
2307 {
2308         int rc;
2309
2310         PRINT_INFO("Loading %s\n",version);
2311         rc = lcs_register_debug_facility();
2312         LCS_DBF_TEXT(0, setup, "lcsinit");
2313         if (rc) {
2314                 PRINT_ERR("Initialization failed\n");
2315                 return rc;
2316         }
2317
2318         rc = register_cu3088_discipline(&lcs_group_driver);
2319         if (rc) {
2320                 PRINT_ERR("Initialization failed\n");
2321                 return rc;
2322         }
2323
2324         return 0;
2325 }
2326
2327
2328 /**
2329  *  LCS module cleanup function
2330  */
2331 static void
2332 __exit lcs_cleanup_module(void)
2333 {
2334         PRINT_INFO("Terminating lcs module.\n");
2335         LCS_DBF_TEXT(0, trace, "cleanup");
2336         unregister_cu3088_discipline(&lcs_group_driver);
2337         lcs_unregister_debug_facility();
2338 }
2339
2340 module_init(lcs_init_module);
2341 module_exit(lcs_cleanup_module);
2342
2343 MODULE_AUTHOR("Frank Pavlic <pavlic@de.ibm.com>");
2344 MODULE_LICENSE("GPL");
2345