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