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