vserver 1.9.3
[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         spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
553         rc = ccw_device_halt(channel->ccwdev, (addr_t) channel);
554         spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
555         if (rc) {
556                 LCS_DBF_TEXT_(4,trace,"ehsc%s", channel->ccwdev->dev.bus_id);
557                 return rc;
558         }
559         /* Asynchronous halt initialted. Wait for its completion. */
560         wait_event(channel->wait_q, (channel->state == CH_STATE_HALTED));
561         lcs_clear_channel(channel);
562         return 0;
563 }
564
565 /**
566  * start read and write channel
567  */
568 static int
569 lcs_start_channels(struct lcs_card *card)
570 {
571         int rc;
572
573         LCS_DBF_TEXT(2, trace, "chstart");
574         /* start read channel */
575         rc = lcs_start_channel(&card->read);
576         if (rc)
577                 return rc;
578         /* start write channel */
579         rc = lcs_start_channel(&card->write);
580         if (rc)
581                 lcs_stop_channel(&card->read);
582         return rc;
583 }
584
585 /**
586  * stop read and write channel
587  */
588 static int
589 lcs_stop_channels(struct lcs_card *card)
590 {
591         LCS_DBF_TEXT(2, trace, "chhalt");
592         lcs_stop_channel(&card->read);
593         lcs_stop_channel(&card->write);
594         return 0;
595 }
596
597 /**
598  * Get empty buffer.
599  */
600 static struct lcs_buffer *
601 __lcs_get_buffer(struct lcs_channel *channel)
602 {
603         int index;
604
605         LCS_DBF_TEXT(5, trace, "_getbuff");
606         index = channel->io_idx;
607         do {
608                 if (channel->iob[index].state == BUF_STATE_EMPTY) {
609                         channel->iob[index].state = BUF_STATE_LOCKED;
610                         return channel->iob + index;
611                 }
612                 index = (index + 1) & (LCS_NUM_BUFFS - 1);
613         } while (index != channel->io_idx);
614         return NULL;
615 }
616
617 static struct lcs_buffer *
618 lcs_get_buffer(struct lcs_channel *channel)
619 {
620         struct lcs_buffer *buffer;
621         unsigned long flags;
622
623         LCS_DBF_TEXT(5, trace, "getbuff");
624         spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
625         buffer = __lcs_get_buffer(channel);
626         spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
627         return buffer;
628 }
629
630 /**
631  * Resume channel program if the channel is suspended.
632  */
633 static int
634 __lcs_resume_channel(struct lcs_channel *channel)
635 {
636         int rc;
637
638         if (channel->state != CH_STATE_SUSPENDED)
639                 return 0;
640         if (channel->ccws[channel->io_idx].flags & CCW_FLAG_SUSPEND)
641                 return 0;
642         LCS_DBF_TEXT_(5, trace, "rsch%s", channel->ccwdev->dev.bus_id);
643         rc = ccw_device_resume(channel->ccwdev);
644         if (rc) {
645                 LCS_DBF_TEXT_(4, trace, "ersc%s", channel->ccwdev->dev.bus_id);
646                 PRINT_ERR("Error in lcs_resume_channel: rc=%d\n",rc);
647         } else
648                 channel->state = CH_STATE_RUNNING;
649         return rc;
650
651 }
652
653 /**
654  * Make a buffer ready for processing.
655  */
656 static inline void
657 __lcs_ready_buffer_bits(struct lcs_channel *channel, int index)
658 {
659         int prev, next;
660
661         LCS_DBF_TEXT(5, trace, "rdybits");
662         prev = (index - 1) & (LCS_NUM_BUFFS - 1);
663         next = (index + 1) & (LCS_NUM_BUFFS - 1);
664         /* Check if we may clear the suspend bit of this buffer. */
665         if (channel->ccws[next].flags & CCW_FLAG_SUSPEND) {
666                 /* Check if we have to set the PCI bit. */
667                 if (!(channel->ccws[prev].flags & CCW_FLAG_SUSPEND))
668                         /* Suspend bit of the previous buffer is not set. */
669                         channel->ccws[index].flags |= CCW_FLAG_PCI;
670                 /* Suspend bit of the next buffer is set. */
671                 channel->ccws[index].flags &= ~CCW_FLAG_SUSPEND;
672         }
673 }
674
675 static int
676 lcs_ready_buffer(struct lcs_channel *channel, struct lcs_buffer *buffer)
677 {
678         unsigned long flags;
679         int index, rc;
680
681         LCS_DBF_TEXT(5, trace, "rdybuff");
682         if (buffer->state != BUF_STATE_LOCKED &&
683             buffer->state != BUF_STATE_PROCESSED)
684                 BUG();
685         spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
686         buffer->state = BUF_STATE_READY;
687         index = buffer - channel->iob;
688         /* Set length. */
689         channel->ccws[index].count = buffer->count;
690         /* Check relevant PCI/suspend bits. */
691         __lcs_ready_buffer_bits(channel, index);
692         rc = __lcs_resume_channel(channel);
693         spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
694         return rc;
695 }
696
697 /**
698  * Mark the buffer as processed. Take care of the suspend bit
699  * of the previous buffer. This function is called from
700  * interrupt context, so the lock must not be taken.
701  */
702 static int
703 __lcs_processed_buffer(struct lcs_channel *channel, struct lcs_buffer *buffer)
704 {
705         int index, prev, next;
706
707         LCS_DBF_TEXT(5, trace, "prcsbuff");
708         if (buffer->state != BUF_STATE_READY)
709                 BUG();
710         buffer->state = BUF_STATE_PROCESSED;
711         index = buffer - channel->iob;
712         prev = (index - 1) & (LCS_NUM_BUFFS - 1);
713         next = (index + 1) & (LCS_NUM_BUFFS - 1);
714         /* Set the suspend bit and clear the PCI bit of this buffer. */
715         channel->ccws[index].flags |= CCW_FLAG_SUSPEND;
716         channel->ccws[index].flags &= ~CCW_FLAG_PCI;
717         /* Check the suspend bit of the previous buffer. */
718         if (channel->iob[prev].state == BUF_STATE_READY) {
719                 /*
720                  * Previous buffer is in state ready. It might have
721                  * happened in lcs_ready_buffer that the suspend bit
722                  * has not been cleared to avoid an endless loop.
723                  * Do it now.
724                  */
725                 __lcs_ready_buffer_bits(channel, prev);
726         }
727         /* Clear PCI bit of next buffer. */
728         channel->ccws[next].flags &= ~CCW_FLAG_PCI;
729         return __lcs_resume_channel(channel);
730 }
731
732 /**
733  * Put a processed buffer back to state empty.
734  */
735 static void
736 lcs_release_buffer(struct lcs_channel *channel, struct lcs_buffer *buffer)
737 {
738         unsigned long flags;
739
740         LCS_DBF_TEXT(5, trace, "relbuff");
741         if (buffer->state != BUF_STATE_LOCKED &&
742             buffer->state != BUF_STATE_PROCESSED)
743                 BUG();
744         spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
745         buffer->state = BUF_STATE_EMPTY;
746         spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
747 }
748
749 /**
750  * Get buffer for a lan command.
751  */
752 static struct lcs_buffer *
753 lcs_get_lancmd(struct lcs_card *card, int count)
754 {
755         struct lcs_buffer *buffer;
756         struct lcs_cmd *cmd;
757
758         LCS_DBF_TEXT(4, trace, "getlncmd");
759         /* Get buffer and wait if none is available. */
760         wait_event(card->write.wait_q,
761                    ((buffer = lcs_get_buffer(&card->write)) != NULL));
762         count += sizeof(struct lcs_header);
763         *(__u16 *)(buffer->data + count) = 0;
764         buffer->count = count + sizeof(__u16);
765         buffer->callback = lcs_release_buffer;
766         cmd = (struct lcs_cmd *) buffer->data;
767         cmd->offset = count;
768         cmd->type = LCS_FRAME_TYPE_CONTROL;
769         cmd->slot = 0;
770         return buffer;
771 }
772
773
774 static void
775 lcs_get_reply(struct lcs_reply *reply)
776 {
777         WARN_ON(atomic_read(&reply->refcnt) <= 0);
778         atomic_inc(&reply->refcnt);
779 }
780
781 static void
782 lcs_put_reply(struct lcs_reply *reply)
783 {
784         WARN_ON(atomic_read(&reply->refcnt) <= 0);
785         if (atomic_dec_and_test(&reply->refcnt)) {
786                 kfree(reply);
787         }
788
789 }
790
791 static struct lcs_reply *
792 lcs_alloc_reply(struct lcs_cmd *cmd)
793 {
794         struct lcs_reply *reply;
795
796         LCS_DBF_TEXT(4, trace, "getreply");
797
798         reply = kmalloc(sizeof(struct lcs_reply), GFP_ATOMIC);
799         if (!reply)
800                 return NULL;
801         memset(reply,0,sizeof(struct lcs_reply));
802         atomic_set(&reply->refcnt,1);
803         reply->sequence_no = cmd->sequence_no;
804         reply->received = 0;
805         reply->rc = 0;
806         init_waitqueue_head(&reply->wait_q);
807
808         return reply;
809 }
810
811 /**
812  * Notifier function for lancmd replies. Called from read irq.
813  */
814 static void
815 lcs_notify_lancmd_waiters(struct lcs_card *card, struct lcs_cmd *cmd)
816 {
817         struct list_head *l, *n;
818         struct lcs_reply *reply;
819
820         LCS_DBF_TEXT(4, trace, "notiwait");
821         spin_lock(&card->lock);
822         list_for_each_safe(l, n, &card->lancmd_waiters) {
823                 reply = list_entry(l, struct lcs_reply, list);
824                 if (reply->sequence_no == cmd->sequence_no) {
825                         lcs_get_reply(reply);
826                         list_del_init(&reply->list);
827                         if (reply->callback != NULL)
828                                 reply->callback(card, cmd);
829                         reply->received = 1;
830                         reply->rc = cmd->return_code;
831                         wake_up(&reply->wait_q);
832                         lcs_put_reply(reply);
833                         break;
834                 }
835         }
836         spin_unlock(&card->lock);
837 }
838
839 /**
840  * Emit buffer of a lan comand.
841  */
842 void
843 lcs_lancmd_timeout(unsigned long data)
844 {
845         struct lcs_reply *reply, *list_reply, *r;
846         unsigned long flags;
847
848         LCS_DBF_TEXT(4, trace, "timeout");
849         reply = (struct lcs_reply *) data;
850         spin_lock_irqsave(&reply->card->lock, flags);
851         list_for_each_entry_safe(list_reply, r,
852                                  &reply->card->lancmd_waiters,list) {
853                 if (reply == list_reply) {
854                         lcs_get_reply(reply);
855                         list_del_init(&reply->list);
856                         spin_unlock_irqrestore(&reply->card->lock, flags);
857                         reply->received = 1;
858                         reply->rc = -ETIME;
859                         wake_up(&reply->wait_q);
860                         lcs_put_reply(reply);
861                         return;
862                 }
863         }
864         spin_unlock_irqrestore(&reply->card->lock, flags);
865 }
866
867 static int
868 lcs_send_lancmd(struct lcs_card *card, struct lcs_buffer *buffer,
869                 void (*reply_callback)(struct lcs_card *, struct lcs_cmd *))
870 {
871         struct lcs_reply *reply;
872         struct lcs_cmd *cmd;
873         struct timer_list timer;
874         unsigned long flags;
875         int rc;
876
877         LCS_DBF_TEXT(4, trace, "sendcmd");
878         cmd = (struct lcs_cmd *) buffer->data;
879         cmd->return_code = 0;
880         cmd->sequence_no = card->sequence_no++;
881         reply = lcs_alloc_reply(cmd);
882         if (!reply)
883                 return -ENOMEM;
884         reply->callback = reply_callback;
885         reply->card = card;
886         spin_lock_irqsave(&card->lock, flags);
887         list_add_tail(&reply->list, &card->lancmd_waiters);
888         spin_unlock_irqrestore(&card->lock, flags);
889
890         buffer->callback = lcs_release_buffer;
891         rc = lcs_ready_buffer(&card->write, buffer);
892         if (rc)
893                 return rc;
894         init_timer(&timer);
895         timer.function = lcs_lancmd_timeout;
896         timer.data = (unsigned long) reply;
897         timer.expires = jiffies + HZ*card->lancmd_timeout;
898         add_timer(&timer);
899         wait_event(reply->wait_q, reply->received);
900         del_timer_sync(&timer);
901         LCS_DBF_TEXT_(4, trace, "rc:%d",reply->rc);
902         rc = reply->rc;
903         lcs_put_reply(reply);
904         return rc ? -EIO : 0;
905 }
906
907 /**
908  * LCS startup command
909  */
910 static int
911 lcs_send_startup(struct lcs_card *card, __u8 initiator)
912 {
913         struct lcs_buffer *buffer;
914         struct lcs_cmd *cmd;
915
916         LCS_DBF_TEXT(2, trace, "startup");
917         buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
918         cmd = (struct lcs_cmd *) buffer->data;
919         cmd->cmd_code = LCS_CMD_STARTUP;
920         cmd->initiator = initiator;
921         cmd->cmd.lcs_startup.buff_size = LCS_IOBUFFERSIZE;
922         return lcs_send_lancmd(card, buffer, NULL);
923 }
924
925 /**
926  * LCS shutdown command
927  */
928 static int
929 lcs_send_shutdown(struct lcs_card *card)
930 {
931         struct lcs_buffer *buffer;
932         struct lcs_cmd *cmd;
933
934         LCS_DBF_TEXT(2, trace, "shutdown");
935         buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
936         cmd = (struct lcs_cmd *) buffer->data;
937         cmd->cmd_code = LCS_CMD_SHUTDOWN;
938         cmd->initiator = LCS_INITIATOR_TCPIP;
939         return lcs_send_lancmd(card, buffer, NULL);
940 }
941
942 /**
943  * LCS lanstat command
944  */
945 static void
946 __lcs_lanstat_cb(struct lcs_card *card, struct lcs_cmd *cmd)
947 {
948         LCS_DBF_TEXT(2, trace, "statcb");
949         memcpy(card->mac, cmd->cmd.lcs_lanstat_cmd.mac_addr, LCS_MAC_LENGTH);
950 }
951
952 static int
953 lcs_send_lanstat(struct lcs_card *card)
954 {
955         struct lcs_buffer *buffer;
956         struct lcs_cmd *cmd;
957
958         LCS_DBF_TEXT(2,trace, "cmdstat");
959         buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
960         cmd = (struct lcs_cmd *) buffer->data;
961         /* Setup lanstat command. */
962         cmd->cmd_code = LCS_CMD_LANSTAT;
963         cmd->initiator = LCS_INITIATOR_TCPIP;
964         cmd->cmd.lcs_std_cmd.lan_type = card->lan_type;
965         cmd->cmd.lcs_std_cmd.portno = card->portno;
966         return lcs_send_lancmd(card, buffer, __lcs_lanstat_cb);
967 }
968
969 /**
970  * send stoplan command
971  */
972 static int
973 lcs_send_stoplan(struct lcs_card *card, __u8 initiator)
974 {
975         struct lcs_buffer *buffer;
976         struct lcs_cmd *cmd;
977
978         LCS_DBF_TEXT(2, trace, "cmdstpln");
979         buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
980         cmd = (struct lcs_cmd *) buffer->data;
981         cmd->cmd_code = LCS_CMD_STOPLAN;
982         cmd->initiator = initiator;
983         cmd->cmd.lcs_std_cmd.lan_type = card->lan_type;
984         cmd->cmd.lcs_std_cmd.portno = card->portno;
985         return lcs_send_lancmd(card, buffer, NULL);
986 }
987
988 /**
989  * send startlan command
990  */
991 static void
992 __lcs_send_startlan_cb(struct lcs_card *card, struct lcs_cmd *cmd)
993 {
994         LCS_DBF_TEXT(2, trace, "srtlancb");
995         card->lan_type = cmd->cmd.lcs_std_cmd.lan_type;
996         card->portno = cmd->cmd.lcs_std_cmd.portno;
997 }
998
999 static int
1000 lcs_send_startlan(struct lcs_card *card, __u8 initiator)
1001 {
1002         struct lcs_buffer *buffer;
1003         struct lcs_cmd *cmd;
1004
1005         LCS_DBF_TEXT(2, trace, "cmdstaln");
1006         buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
1007         cmd = (struct lcs_cmd *) buffer->data;
1008         cmd->cmd_code = LCS_CMD_STARTLAN;
1009         cmd->initiator = initiator;
1010         cmd->cmd.lcs_std_cmd.lan_type = card->lan_type;
1011         cmd->cmd.lcs_std_cmd.portno = card->portno;
1012         return lcs_send_lancmd(card, buffer, __lcs_send_startlan_cb);
1013 }
1014
1015 #ifdef CONFIG_IP_MULTICAST
1016 /**
1017  * send setipm command (Multicast)
1018  */
1019 static int
1020 lcs_send_setipm(struct lcs_card *card,struct lcs_ipm_list *ipm_list)
1021 {
1022         struct lcs_buffer *buffer;
1023         struct lcs_cmd *cmd;
1024
1025         LCS_DBF_TEXT(2, trace, "cmdsetim");
1026         buffer = lcs_get_lancmd(card, LCS_MULTICAST_CMD_SIZE);
1027         cmd = (struct lcs_cmd *) buffer->data;
1028         cmd->cmd_code = LCS_CMD_SETIPM;
1029         cmd->initiator = LCS_INITIATOR_TCPIP;
1030         cmd->cmd.lcs_qipassist.lan_type = card->lan_type;
1031         cmd->cmd.lcs_qipassist.portno = card->portno;
1032         cmd->cmd.lcs_qipassist.version = 4;
1033         cmd->cmd.lcs_qipassist.num_ip_pairs = 1;
1034         memcpy(cmd->cmd.lcs_qipassist.lcs_ipass_ctlmsg.ip_mac_pair,
1035                &ipm_list->ipm, sizeof (struct lcs_ip_mac_pair));
1036         LCS_DBF_TEXT_(2, trace, "%x",ipm_list->ipm.ip_addr);
1037         return lcs_send_lancmd(card, buffer, NULL);
1038 }
1039
1040 /**
1041  * send delipm command (Multicast)
1042  */
1043 static int
1044 lcs_send_delipm(struct lcs_card *card,struct lcs_ipm_list *ipm_list)
1045 {
1046         struct lcs_buffer *buffer;
1047         struct lcs_cmd *cmd;
1048
1049         LCS_DBF_TEXT(2, trace, "cmddelim");
1050         buffer = lcs_get_lancmd(card, LCS_MULTICAST_CMD_SIZE);
1051         cmd = (struct lcs_cmd *) buffer->data;
1052         cmd->cmd_code = LCS_CMD_DELIPM;
1053         cmd->initiator = LCS_INITIATOR_TCPIP;
1054         cmd->cmd.lcs_qipassist.lan_type = card->lan_type;
1055         cmd->cmd.lcs_qipassist.portno = card->portno;
1056         cmd->cmd.lcs_qipassist.version = 4;
1057         cmd->cmd.lcs_qipassist.num_ip_pairs = 1;
1058         memcpy(cmd->cmd.lcs_qipassist.lcs_ipass_ctlmsg.ip_mac_pair,
1059                &ipm_list->ipm, sizeof (struct lcs_ip_mac_pair));
1060         LCS_DBF_TEXT_(2, trace, "%x",ipm_list->ipm.ip_addr);
1061         return lcs_send_lancmd(card, buffer, NULL);
1062 }
1063
1064 /**
1065  * check if multicast is supported by LCS
1066  */
1067 static void
1068 __lcs_check_multicast_cb(struct lcs_card *card, struct lcs_cmd *cmd)
1069 {
1070         LCS_DBF_TEXT(2, trace, "chkmccb");
1071         card->ip_assists_supported =
1072                 cmd->cmd.lcs_qipassist.ip_assists_supported;
1073         card->ip_assists_enabled =
1074                 cmd->cmd.lcs_qipassist.ip_assists_enabled;
1075 }
1076
1077 static int
1078 lcs_check_multicast_support(struct lcs_card *card)
1079 {
1080         struct lcs_buffer *buffer;
1081         struct lcs_cmd *cmd;
1082         int rc;
1083
1084         LCS_DBF_TEXT(2, trace, "cmdqipa");
1085         /* Send query ipassist. */
1086         buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
1087         cmd = (struct lcs_cmd *) buffer->data;
1088         cmd->cmd_code = LCS_CMD_QIPASSIST;
1089         cmd->initiator = LCS_INITIATOR_TCPIP;
1090         cmd->cmd.lcs_qipassist.lan_type = card->lan_type;
1091         cmd->cmd.lcs_qipassist.portno = card->portno;
1092         cmd->cmd.lcs_qipassist.version = 4;
1093         cmd->cmd.lcs_qipassist.num_ip_pairs = 1;
1094         rc = lcs_send_lancmd(card, buffer, __lcs_check_multicast_cb);
1095         if (rc != 0) {
1096                 PRINT_ERR("Query IPAssist failed. Assuming unsupported!\n");
1097                 return -EOPNOTSUPP;
1098         }
1099         /* Print out supported assists: IPv6 */
1100         PRINT_INFO("LCS device %s %s IPv6 support\n", card->dev->name,
1101                    (card->ip_assists_supported & LCS_IPASS_IPV6_SUPPORT) ?
1102                    "with" : "without");
1103         /* Print out supported assist: Multicast */
1104         PRINT_INFO("LCS device %s %s Multicast support\n", card->dev->name,
1105                    (card->ip_assists_supported & LCS_IPASS_MULTICAST_SUPPORT) ?
1106                    "with" : "without");
1107         if (card->ip_assists_supported & LCS_IPASS_MULTICAST_SUPPORT)
1108                 return 0;
1109         return -EOPNOTSUPP;
1110 }
1111
1112 /**
1113  * set or del multicast address on LCS card
1114  */
1115 static void
1116 lcs_fix_multicast_list(struct lcs_card *card)
1117 {
1118         struct list_head failed_list;
1119         struct lcs_ipm_list *ipm, *tmp;
1120         unsigned long flags;
1121         int rc;
1122
1123         LCS_DBF_TEXT(4,trace, "fixipm");
1124         INIT_LIST_HEAD(&failed_list);
1125         spin_lock_irqsave(&card->ipm_lock, flags);
1126 list_modified:
1127         list_for_each_entry_safe(ipm, tmp, &card->ipm_list, list){
1128                 switch (ipm->ipm_state) {
1129                 case LCS_IPM_STATE_SET_REQUIRED:
1130                         /* del from ipm_list so noone else can tamper with
1131                          * this entry */
1132                         list_del_init(&ipm->list);
1133                         spin_unlock_irqrestore(&card->ipm_lock, flags);
1134                         rc = lcs_send_setipm(card, ipm);
1135                         spin_lock_irqsave(&card->ipm_lock, flags);
1136                         if (rc) {
1137                                 PRINT_INFO("Adding multicast address failed."
1138                                            "Table possibly full!\n");
1139                                 /* store ipm in failed list -> will be added
1140                                  * to ipm_list again, so a retry will be done
1141                                  * during the next call of this function */
1142                                 list_add_tail(&ipm->list, &failed_list);
1143                         } else {
1144                                 ipm->ipm_state = LCS_IPM_STATE_ON_CARD;
1145                                 /* re-insert into ipm_list */
1146                                 list_add_tail(&ipm->list, &card->ipm_list);
1147                         }
1148                         goto list_modified;
1149                 case LCS_IPM_STATE_DEL_REQUIRED:
1150                         list_del(&ipm->list);
1151                         spin_unlock_irqrestore(&card->ipm_lock, flags);
1152                         lcs_send_delipm(card, ipm);
1153                         spin_lock_irqsave(&card->ipm_lock, flags);
1154                         kfree(ipm);
1155                         goto list_modified;
1156                 case LCS_IPM_STATE_ON_CARD:
1157                         break;
1158                 }
1159         }
1160         /* re-insert all entries from the failed_list into ipm_list */
1161         list_for_each_entry(ipm, &failed_list, list) {
1162                 list_del_init(&ipm->list);
1163                 list_add_tail(&ipm->list, &card->ipm_list);
1164         }
1165         spin_unlock_irqrestore(&card->ipm_lock, flags);
1166         if (card->state == DEV_STATE_UP)
1167                 netif_wake_queue(card->dev);
1168 }
1169
1170 /**
1171  * get mac address for the relevant Multicast address
1172  */
1173 static void
1174 lcs_get_mac_for_ipm(__u32 ipm, char *mac, struct net_device *dev)
1175 {
1176         LCS_DBF_TEXT(4,trace, "getmac");
1177         if (dev->type == ARPHRD_IEEE802_TR)
1178                 ip_tr_mc_map(ipm, mac);
1179         else
1180                 ip_eth_mc_map(ipm, mac);
1181 }
1182
1183 /**
1184  * function called by net device to handle multicast address relevant things
1185  */
1186 static inline void
1187 lcs_remove_mc_addresses(struct lcs_card *card, struct in_device *in4_dev)
1188 {
1189         struct ip_mc_list *im4;
1190         struct list_head *l;
1191         struct lcs_ipm_list *ipm;
1192         unsigned long flags;
1193         char buf[MAX_ADDR_LEN];
1194
1195         LCS_DBF_TEXT(4, trace, "remmclst");
1196         spin_lock_irqsave(&card->ipm_lock, flags);
1197         list_for_each(l, &card->ipm_list) {
1198                 ipm = list_entry(l, struct lcs_ipm_list, list);
1199                 for (im4 = in4_dev->mc_list; im4 != NULL; im4 = im4->next) {
1200                         lcs_get_mac_for_ipm(im4->multiaddr, buf, card->dev);
1201                         if ( (ipm->ipm.ip_addr == im4->multiaddr) &&
1202                              (memcmp(buf, &ipm->ipm.mac_addr,
1203                                      LCS_MAC_LENGTH) == 0) )
1204                                 break;
1205                 }
1206                 if (im4 == NULL)
1207                         ipm->ipm_state = LCS_IPM_STATE_DEL_REQUIRED;
1208         }
1209         spin_unlock_irqrestore(&card->ipm_lock, flags);
1210 }
1211
1212 static inline struct lcs_ipm_list *
1213 lcs_check_addr_entry(struct lcs_card *card, struct ip_mc_list *im4, char *buf)
1214 {
1215         struct lcs_ipm_list *tmp, *ipm = NULL;
1216         struct list_head *l;
1217         unsigned long flags;
1218
1219         LCS_DBF_TEXT(4, trace, "chkmcent");
1220         spin_lock_irqsave(&card->ipm_lock, flags);
1221         list_for_each(l, &card->ipm_list) {
1222                 tmp = list_entry(l, struct lcs_ipm_list, list);
1223                 if ( (tmp->ipm.ip_addr == im4->multiaddr) &&
1224                      (memcmp(buf, &tmp->ipm.mac_addr,
1225                              LCS_MAC_LENGTH) == 0) ) {
1226                         ipm = tmp;
1227                         break;
1228                 }
1229         }
1230         spin_unlock_irqrestore(&card->ipm_lock, flags);
1231         return ipm;
1232 }
1233
1234 static inline void
1235 lcs_set_mc_addresses(struct lcs_card *card, struct in_device *in4_dev)
1236 {
1237
1238         struct ip_mc_list *im4;
1239         struct lcs_ipm_list *ipm;
1240         char buf[MAX_ADDR_LEN];
1241         unsigned long flags;
1242
1243         LCS_DBF_TEXT(4, trace, "setmclst");
1244         for (im4 = in4_dev->mc_list; im4; im4 = im4->next) {
1245                 lcs_get_mac_for_ipm(im4->multiaddr, buf, card->dev);
1246                 ipm = lcs_check_addr_entry(card, im4, buf);
1247                 if (ipm != NULL)
1248                         continue;       /* Address already in list. */
1249                 ipm = (struct lcs_ipm_list *)
1250                         kmalloc(sizeof(struct lcs_ipm_list), GFP_ATOMIC);
1251                 if (ipm == NULL) {
1252                         PRINT_INFO("Not enough memory to add "
1253                                    "new multicast entry!\n");
1254                         break;
1255                 }
1256                 memset(ipm, 0, sizeof(struct lcs_ipm_list));
1257                 memcpy(&ipm->ipm.mac_addr, buf, LCS_MAC_LENGTH);
1258                 ipm->ipm.ip_addr = im4->multiaddr;
1259                 ipm->ipm_state = LCS_IPM_STATE_SET_REQUIRED;
1260                 spin_lock_irqsave(&card->ipm_lock, flags);
1261                 list_add(&ipm->list, &card->ipm_list);
1262                 spin_unlock_irqrestore(&card->ipm_lock, flags);
1263         }
1264 }
1265
1266 static int
1267 lcs_register_mc_addresses(void *data)
1268 {
1269         struct lcs_card *card;
1270         struct in_device *in4_dev;
1271
1272         card = (struct lcs_card *) data;
1273         daemonize("regipm");
1274
1275         if (!lcs_do_run_thread(card, LCS_SET_MC_THREAD))
1276                 return 0;
1277         LCS_DBF_TEXT(4, trace, "regmulti");
1278
1279         in4_dev = in_dev_get(card->dev);
1280         if (in4_dev == NULL)
1281                 goto out;
1282         read_lock(&in4_dev->mc_list_lock);
1283         lcs_remove_mc_addresses(card,in4_dev);
1284         lcs_set_mc_addresses(card, in4_dev);
1285         read_unlock(&in4_dev->mc_list_lock);
1286         in_dev_put(in4_dev);
1287
1288         lcs_fix_multicast_list(card);
1289 out:
1290         lcs_clear_thread_running_bit(card, LCS_SET_MC_THREAD);
1291         return 0;
1292 }
1293 /**
1294  * function called by net device to
1295  * handle multicast address relevant things
1296  */
1297 static void
1298 lcs_set_multicast_list(struct net_device *dev)
1299 {
1300         struct lcs_card *card;
1301
1302         LCS_DBF_TEXT(4, trace, "setmulti");
1303         card = (struct lcs_card *) dev->priv;
1304
1305         if (!lcs_set_thread_start_bit(card, LCS_SET_MC_THREAD)) {
1306                 schedule_work(&card->kernel_thread_starter);
1307         }
1308 }
1309
1310 #endif /* CONFIG_IP_MULTICAST */
1311
1312 static long
1313 lcs_check_irb_error(struct ccw_device *cdev, struct irb *irb)
1314 {
1315         if (!IS_ERR(irb))
1316                 return 0;
1317
1318         switch (PTR_ERR(irb)) {
1319         case -EIO:
1320                 PRINT_WARN("i/o-error on device %s\n", cdev->dev.bus_id);
1321                 LCS_DBF_TEXT(2, trace, "ckirberr");
1322                 LCS_DBF_TEXT_(2, trace, "  rc%d", -EIO);
1323                 break;
1324         case -ETIMEDOUT:
1325                 PRINT_WARN("timeout on device %s\n", cdev->dev.bus_id);
1326                 LCS_DBF_TEXT(2, trace, "ckirberr");
1327                 LCS_DBF_TEXT_(2, trace, "  rc%d", -ETIMEDOUT);
1328                 break;
1329         default:
1330                 PRINT_WARN("unknown error %ld on device %s\n", PTR_ERR(irb),
1331                            cdev->dev.bus_id);
1332                 LCS_DBF_TEXT(2, trace, "ckirberr");
1333                 LCS_DBF_TEXT(2, trace, "  rc???");
1334         }
1335         return PTR_ERR(irb);
1336 }
1337
1338
1339 /**
1340  * IRQ Handler for LCS channels
1341  */
1342 static void
1343 lcs_irq(struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
1344 {
1345         struct lcs_card *card;
1346         struct lcs_channel *channel;
1347         int index;
1348
1349         if (lcs_check_irb_error(cdev, irb))
1350                 return;
1351
1352         card = CARD_FROM_DEV(cdev);
1353         if (card->read.ccwdev == cdev)
1354                 channel = &card->read;
1355         else
1356                 channel = &card->write;
1357
1358         LCS_DBF_TEXT_(5, trace, "Rint%s",cdev->dev.bus_id);
1359         LCS_DBF_TEXT_(5, trace, "%4x%4x",irb->scsw.cstat, irb->scsw.dstat);
1360
1361         /* How far in the ccw chain have we processed? */
1362         if ((channel->state != CH_STATE_INIT) &&
1363             (irb->scsw.fctl & SCSW_FCTL_START_FUNC)) {
1364                 index = (struct ccw1 *) __va((addr_t) irb->scsw.cpa) 
1365                         - channel->ccws;
1366                 if ((irb->scsw.actl & SCSW_ACTL_SUSPENDED) ||
1367                     (irb->scsw.cstat | SCHN_STAT_PCI))
1368                         /* Bloody io subsystem tells us lies about cpa... */
1369                         index = (index - 1) & (LCS_NUM_BUFFS - 1);
1370                 while (channel->io_idx != index) {
1371                         __lcs_processed_buffer(channel,
1372                                                channel->iob + channel->io_idx);
1373                         channel->io_idx =
1374                                 (channel->io_idx + 1) & (LCS_NUM_BUFFS - 1);
1375                 }
1376         }
1377
1378         if ((irb->scsw.dstat & DEV_STAT_DEV_END) ||
1379             (irb->scsw.dstat & DEV_STAT_CHN_END) ||
1380             (irb->scsw.dstat & DEV_STAT_UNIT_CHECK))
1381                 /* Mark channel as stopped. */
1382                 channel->state = CH_STATE_STOPPED;
1383         else if (irb->scsw.actl & SCSW_ACTL_SUSPENDED)
1384                 /* CCW execution stopped on a suspend bit. */
1385                 channel->state = CH_STATE_SUSPENDED;
1386
1387         if (irb->scsw.fctl & SCSW_FCTL_HALT_FUNC) {
1388                 if (irb->scsw.cc != 0) {
1389                         ccw_device_halt(channel->ccwdev, (addr_t) channel);
1390                         return;
1391                 }
1392                 /* The channel has been stopped by halt_IO. */
1393                 channel->state = CH_STATE_HALTED;
1394         }
1395
1396         if (irb->scsw.fctl & SCSW_FCTL_CLEAR_FUNC) {
1397                 channel->state = CH_STATE_CLEARED;
1398         }
1399         /* Do the rest in the tasklet. */
1400         tasklet_schedule(&channel->irq_tasklet);
1401 }
1402
1403 /**
1404  * Tasklet for IRQ handler
1405  */
1406 static void
1407 lcs_tasklet(unsigned long data)
1408 {
1409         unsigned long flags;
1410         struct lcs_channel *channel;
1411         struct lcs_buffer *iob;
1412         int buf_idx;
1413         int rc;
1414
1415         channel = (struct lcs_channel *) data;
1416         LCS_DBF_TEXT_(5, trace, "tlet%s",channel->ccwdev->dev.bus_id);
1417
1418         /* Check for processed buffers. */
1419         iob = channel->iob;
1420         buf_idx = channel->buf_idx;
1421         while (iob[buf_idx].state == BUF_STATE_PROCESSED) {
1422                 /* Do the callback thing. */
1423                 if (iob[buf_idx].callback != NULL)
1424                         iob[buf_idx].callback(channel, iob + buf_idx);
1425                 buf_idx = (buf_idx + 1) & (LCS_NUM_BUFFS - 1);
1426         }
1427         channel->buf_idx = buf_idx;
1428
1429         if (channel->state == CH_STATE_STOPPED)
1430                 // FIXME: what if rc != 0 ??
1431                 rc = lcs_start_channel(channel);
1432         spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1433         if (channel->state == CH_STATE_SUSPENDED &&
1434             channel->iob[channel->io_idx].state == BUF_STATE_READY) {
1435                 // FIXME: what if rc != 0 ??
1436                 rc = __lcs_resume_channel(channel);
1437         }
1438         spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1439
1440         /* Something happened on the channel. Wake up waiters. */
1441         wake_up(&channel->wait_q);
1442 }
1443
1444 /**
1445  * Finish current tx buffer and make it ready for transmit.
1446  */
1447 static void
1448 __lcs_emit_txbuffer(struct lcs_card *card)
1449 {
1450         LCS_DBF_TEXT(5, trace, "emittx");
1451         *(__u16 *)(card->tx_buffer->data + card->tx_buffer->count) = 0;
1452         card->tx_buffer->count += 2;
1453         lcs_ready_buffer(&card->write, card->tx_buffer);
1454         card->tx_buffer = NULL;
1455         card->tx_emitted++;
1456 }
1457
1458 /**
1459  * Callback for finished tx buffers.
1460  */
1461 static void
1462 lcs_txbuffer_cb(struct lcs_channel *channel, struct lcs_buffer *buffer)
1463 {
1464         struct lcs_card *card;
1465
1466         LCS_DBF_TEXT(5, trace, "txbuffcb");
1467         /* Put buffer back to pool. */
1468         lcs_release_buffer(channel, buffer);
1469         card = (struct lcs_card *)
1470                 ((char *) channel - offsetof(struct lcs_card, write));
1471         spin_lock(&card->lock);
1472         card->tx_emitted--;
1473         if (card->tx_emitted <= 0 && card->tx_buffer != NULL)
1474                 /*
1475                  * Last running tx buffer has finished. Submit partially
1476                  * filled current buffer.
1477                  */
1478                 __lcs_emit_txbuffer(card);
1479         spin_unlock(&card->lock);
1480 }
1481
1482 /**
1483  * Packet transmit function called by network stack
1484  */
1485 static int
1486 __lcs_start_xmit(struct lcs_card *card, struct sk_buff *skb,
1487                  struct net_device *dev)
1488 {
1489         struct lcs_header *header;
1490
1491         LCS_DBF_TEXT(5, trace, "hardxmit");
1492         if (skb == NULL) {
1493                 card->stats.tx_dropped++;
1494                 card->stats.tx_errors++;
1495                 return -EIO;
1496         }
1497         if (card->state != DEV_STATE_UP) {
1498                 dev_kfree_skb(skb);
1499                 card->stats.tx_dropped++;
1500                 card->stats.tx_errors++;
1501                 card->stats.tx_carrier_errors++;
1502                 return 0;
1503         }
1504         if (netif_queue_stopped(dev) ) {
1505                 card->stats.tx_dropped++;
1506                 return -EBUSY;
1507         }
1508         if (card->tx_buffer != NULL &&
1509             card->tx_buffer->count + sizeof(struct lcs_header) +
1510             skb->len + sizeof(u16) > LCS_IOBUFFERSIZE)
1511                 /* skb too big for current tx buffer. */
1512                 __lcs_emit_txbuffer(card);
1513         if (card->tx_buffer == NULL) {
1514                 /* Get new tx buffer */
1515                 card->tx_buffer = lcs_get_buffer(&card->write);
1516                 if (card->tx_buffer == NULL) {
1517                         card->stats.tx_dropped++;
1518                         return -EBUSY;
1519                 }
1520                 card->tx_buffer->callback = lcs_txbuffer_cb;
1521                 card->tx_buffer->count = 0;
1522         }
1523         header = (struct lcs_header *)
1524                 (card->tx_buffer->data + card->tx_buffer->count);
1525         card->tx_buffer->count += skb->len + sizeof(struct lcs_header);
1526         header->offset = card->tx_buffer->count;
1527         header->type = card->lan_type;
1528         header->slot = card->portno;
1529         memcpy(header + 1, skb->data, skb->len);
1530         card->stats.tx_bytes += skb->len;
1531         card->stats.tx_packets++;
1532         dev_kfree_skb(skb);
1533         if (card->tx_emitted <= 0)
1534                 /* If this is the first tx buffer emit it immediately. */
1535                 __lcs_emit_txbuffer(card);
1536         return 0;
1537 }
1538
1539 static int
1540 lcs_start_xmit(struct sk_buff *skb, struct net_device *dev)
1541 {
1542         struct lcs_card *card;
1543         int rc;
1544
1545         LCS_DBF_TEXT(5, trace, "pktxmit");
1546         card = (struct lcs_card *) dev->priv;
1547         spin_lock(&card->lock);
1548         rc = __lcs_start_xmit(card, skb, dev);
1549         spin_unlock(&card->lock);
1550         return rc;
1551 }
1552
1553 /**
1554  * send startlan and lanstat command to make LCS device ready
1555  */
1556 static int
1557 lcs_startlan_auto(struct lcs_card *card)
1558 {
1559         int rc;
1560
1561         LCS_DBF_TEXT(2, trace, "strtauto");
1562 #ifdef CONFIG_NET_ETHERNET
1563         card->lan_type = LCS_FRAME_TYPE_ENET;
1564         rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
1565         if (rc == 0)
1566                 return 0;
1567
1568 #endif
1569 #ifdef CONFIG_TR
1570         card->lan_type = LCS_FRAME_TYPE_TR;
1571         rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
1572         if (rc == 0)
1573                 return 0;
1574 #endif
1575 #ifdef CONFIG_FDDI
1576         card->lan_type = LCS_FRAME_TYPE_FDDI;
1577         rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
1578         if (rc == 0)
1579                 return 0;
1580 #endif
1581         return -EIO;
1582 }
1583
1584 static int
1585 lcs_startlan(struct lcs_card *card)
1586 {
1587         int rc, i;
1588
1589         LCS_DBF_TEXT(2, trace, "startlan");
1590         rc = 0;
1591         if (card->portno != LCS_INVALID_PORT_NO) {
1592                 if (card->lan_type == LCS_FRAME_TYPE_AUTO)
1593                         rc = lcs_startlan_auto(card);
1594                 else
1595                         rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
1596         } else {
1597                 for (i = 0; i <= 16; i++) {
1598                         card->portno = i;
1599                         if (card->lan_type != LCS_FRAME_TYPE_AUTO)
1600                                 rc = lcs_send_startlan(card,
1601                                                        LCS_INITIATOR_TCPIP);
1602                         else
1603                                 /* autodetecting lan type */
1604                                 rc = lcs_startlan_auto(card);
1605                         if (rc == 0)
1606                                 break;
1607                 }
1608         }
1609         if (rc == 0)
1610                 return lcs_send_lanstat(card);
1611         return rc;
1612 }
1613
1614 /**
1615  * LCS detect function
1616  * setup channels and make them I/O ready
1617  */
1618 static int
1619 lcs_detect(struct lcs_card *card)
1620 {
1621         int rc = 0;
1622
1623         LCS_DBF_TEXT(2, setup, "lcsdetct");
1624         /* start/reset card */
1625         if (card->dev)
1626                 netif_stop_queue(card->dev);
1627         card->write.state = CH_STATE_INIT;
1628         card->read.state = CH_STATE_INIT;
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