edcf05d5d568af56dcbcb6e85eec70bcaefacb54
[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         if (buffer->state != BUF_STATE_LOCKED &&
679             buffer->state != BUF_STATE_PROCESSED)
680                 BUG();
681         spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
682         buffer->state = BUF_STATE_READY;
683         index = buffer - channel->iob;
684         /* Set length. */
685         channel->ccws[index].count = buffer->count;
686         /* Check relevant PCI/suspend bits. */
687         __lcs_ready_buffer_bits(channel, index);
688         rc = __lcs_resume_channel(channel);
689         spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
690         return rc;
691 }
692
693 /**
694  * Mark the buffer as processed. Take care of the suspend bit
695  * of the previous buffer. This function is called from
696  * interrupt context, so the lock must not be taken.
697  */
698 static int
699 __lcs_processed_buffer(struct lcs_channel *channel, struct lcs_buffer *buffer)
700 {
701         int index, prev, next;
702
703         LCS_DBF_TEXT(5, trace, "prcsbuff");
704         if (buffer->state != BUF_STATE_READY)
705                 BUG();
706         buffer->state = BUF_STATE_PROCESSED;
707         index = buffer - channel->iob;
708         prev = (index - 1) & (LCS_NUM_BUFFS - 1);
709         next = (index + 1) & (LCS_NUM_BUFFS - 1);
710         /* Set the suspend bit and clear the PCI bit of this buffer. */
711         channel->ccws[index].flags |= CCW_FLAG_SUSPEND;
712         channel->ccws[index].flags &= ~CCW_FLAG_PCI;
713         /* Check the suspend bit of the previous buffer. */
714         if (channel->iob[prev].state == BUF_STATE_READY) {
715                 /*
716                  * Previous buffer is in state ready. It might have
717                  * happened in lcs_ready_buffer that the suspend bit
718                  * has not been cleared to avoid an endless loop.
719                  * Do it now.
720                  */
721                 __lcs_ready_buffer_bits(channel, prev);
722         }
723         /* Clear PCI bit of next buffer. */
724         channel->ccws[next].flags &= ~CCW_FLAG_PCI;
725         return __lcs_resume_channel(channel);
726 }
727
728 /**
729  * Put a processed buffer back to state empty.
730  */
731 static void
732 lcs_release_buffer(struct lcs_channel *channel, struct lcs_buffer *buffer)
733 {
734         unsigned long flags;
735
736         LCS_DBF_TEXT(5, trace, "relbuff");
737         if (buffer->state != BUF_STATE_LOCKED &&
738             buffer->state != BUF_STATE_PROCESSED)
739                 BUG();
740         spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
741         buffer->state = BUF_STATE_EMPTY;
742         spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
743 }
744
745 /**
746  * Get buffer for a lan command.
747  */
748 static struct lcs_buffer *
749 lcs_get_lancmd(struct lcs_card *card, int count)
750 {
751         struct lcs_buffer *buffer;
752         struct lcs_cmd *cmd;
753
754         LCS_DBF_TEXT(4, trace, "getlncmd");
755         /* Get buffer and wait if none is available. */
756         wait_event(card->write.wait_q,
757                    ((buffer = lcs_get_buffer(&card->write)) != NULL));
758         count += sizeof(struct lcs_header);
759         *(__u16 *)(buffer->data + count) = 0;
760         buffer->count = count + sizeof(__u16);
761         buffer->callback = lcs_release_buffer;
762         cmd = (struct lcs_cmd *) buffer->data;
763         cmd->offset = count;
764         cmd->type = LCS_FRAME_TYPE_CONTROL;
765         cmd->slot = 0;
766         return buffer;
767 }
768
769
770 static void
771 lcs_get_reply(struct lcs_reply *reply)
772 {
773         WARN_ON(atomic_read(&reply->refcnt) <= 0);
774         atomic_inc(&reply->refcnt);
775 }
776
777 static void
778 lcs_put_reply(struct lcs_reply *reply)
779 {
780         WARN_ON(atomic_read(&reply->refcnt) <= 0);
781         if (atomic_dec_and_test(&reply->refcnt)) {
782                 kfree(reply);
783         }
784
785 }
786
787 static struct lcs_reply *
788 lcs_alloc_reply(struct lcs_cmd *cmd)
789 {
790         struct lcs_reply *reply;
791
792         LCS_DBF_TEXT(4, trace, "getreply");
793
794         reply = kzalloc(sizeof(struct lcs_reply), GFP_ATOMIC);
795         if (!reply)
796                 return NULL;
797         atomic_set(&reply->refcnt,1);
798         reply->sequence_no = cmd->sequence_no;
799         reply->received = 0;
800         reply->rc = 0;
801         init_waitqueue_head(&reply->wait_q);
802
803         return reply;
804 }
805
806 /**
807  * Notifier function for lancmd replies. Called from read irq.
808  */
809 static void
810 lcs_notify_lancmd_waiters(struct lcs_card *card, struct lcs_cmd *cmd)
811 {
812         struct list_head *l, *n;
813         struct lcs_reply *reply;
814
815         LCS_DBF_TEXT(4, trace, "notiwait");
816         spin_lock(&card->lock);
817         list_for_each_safe(l, n, &card->lancmd_waiters) {
818                 reply = list_entry(l, struct lcs_reply, list);
819                 if (reply->sequence_no == cmd->sequence_no) {
820                         lcs_get_reply(reply);
821                         list_del_init(&reply->list);
822                         if (reply->callback != NULL)
823                                 reply->callback(card, cmd);
824                         reply->received = 1;
825                         reply->rc = cmd->return_code;
826                         wake_up(&reply->wait_q);
827                         lcs_put_reply(reply);
828                         break;
829                 }
830         }
831         spin_unlock(&card->lock);
832 }
833
834 /**
835  * Emit buffer of a lan comand.
836  */
837 void
838 lcs_lancmd_timeout(unsigned long data)
839 {
840         struct lcs_reply *reply, *list_reply, *r;
841         unsigned long flags;
842
843         LCS_DBF_TEXT(4, trace, "timeout");
844         reply = (struct lcs_reply *) data;
845         spin_lock_irqsave(&reply->card->lock, flags);
846         list_for_each_entry_safe(list_reply, r,
847                                  &reply->card->lancmd_waiters,list) {
848                 if (reply == list_reply) {
849                         lcs_get_reply(reply);
850                         list_del_init(&reply->list);
851                         spin_unlock_irqrestore(&reply->card->lock, flags);
852                         reply->received = 1;
853                         reply->rc = -ETIME;
854                         wake_up(&reply->wait_q);
855                         lcs_put_reply(reply);
856                         return;
857                 }
858         }
859         spin_unlock_irqrestore(&reply->card->lock, flags);
860 }
861
862 static int
863 lcs_send_lancmd(struct lcs_card *card, struct lcs_buffer *buffer,
864                 void (*reply_callback)(struct lcs_card *, struct lcs_cmd *))
865 {
866         struct lcs_reply *reply;
867         struct lcs_cmd *cmd;
868         struct timer_list timer;
869         unsigned long flags;
870         int rc;
871
872         LCS_DBF_TEXT(4, trace, "sendcmd");
873         cmd = (struct lcs_cmd *) buffer->data;
874         cmd->return_code = 0;
875         cmd->sequence_no = card->sequence_no++;
876         reply = lcs_alloc_reply(cmd);
877         if (!reply)
878                 return -ENOMEM;
879         reply->callback = reply_callback;
880         reply->card = card;
881         spin_lock_irqsave(&card->lock, flags);
882         list_add_tail(&reply->list, &card->lancmd_waiters);
883         spin_unlock_irqrestore(&card->lock, flags);
884
885         buffer->callback = lcs_release_buffer;
886         rc = lcs_ready_buffer(&card->write, buffer);
887         if (rc)
888                 return rc;
889         init_timer(&timer);
890         timer.function = lcs_lancmd_timeout;
891         timer.data = (unsigned long) reply;
892         timer.expires = jiffies + HZ*card->lancmd_timeout;
893         add_timer(&timer);
894         wait_event(reply->wait_q, reply->received);
895         del_timer_sync(&timer);
896         LCS_DBF_TEXT_(4, trace, "rc:%d",reply->rc);
897         rc = reply->rc;
898         lcs_put_reply(reply);
899         return rc ? -EIO : 0;
900 }
901
902 /**
903  * LCS startup command
904  */
905 static int
906 lcs_send_startup(struct lcs_card *card, __u8 initiator)
907 {
908         struct lcs_buffer *buffer;
909         struct lcs_cmd *cmd;
910
911         LCS_DBF_TEXT(2, trace, "startup");
912         buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
913         cmd = (struct lcs_cmd *) buffer->data;
914         cmd->cmd_code = LCS_CMD_STARTUP;
915         cmd->initiator = initiator;
916         cmd->cmd.lcs_startup.buff_size = LCS_IOBUFFERSIZE;
917         return lcs_send_lancmd(card, buffer, NULL);
918 }
919
920 /**
921  * LCS shutdown command
922  */
923 static int
924 lcs_send_shutdown(struct lcs_card *card)
925 {
926         struct lcs_buffer *buffer;
927         struct lcs_cmd *cmd;
928
929         LCS_DBF_TEXT(2, trace, "shutdown");
930         buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
931         cmd = (struct lcs_cmd *) buffer->data;
932         cmd->cmd_code = LCS_CMD_SHUTDOWN;
933         cmd->initiator = LCS_INITIATOR_TCPIP;
934         return lcs_send_lancmd(card, buffer, NULL);
935 }
936
937 /**
938  * LCS lanstat command
939  */
940 static void
941 __lcs_lanstat_cb(struct lcs_card *card, struct lcs_cmd *cmd)
942 {
943         LCS_DBF_TEXT(2, trace, "statcb");
944         memcpy(card->mac, cmd->cmd.lcs_lanstat_cmd.mac_addr, LCS_MAC_LENGTH);
945 }
946
947 static int
948 lcs_send_lanstat(struct lcs_card *card)
949 {
950         struct lcs_buffer *buffer;
951         struct lcs_cmd *cmd;
952
953         LCS_DBF_TEXT(2,trace, "cmdstat");
954         buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
955         cmd = (struct lcs_cmd *) buffer->data;
956         /* Setup lanstat command. */
957         cmd->cmd_code = LCS_CMD_LANSTAT;
958         cmd->initiator = LCS_INITIATOR_TCPIP;
959         cmd->cmd.lcs_std_cmd.lan_type = card->lan_type;
960         cmd->cmd.lcs_std_cmd.portno = card->portno;
961         return lcs_send_lancmd(card, buffer, __lcs_lanstat_cb);
962 }
963
964 /**
965  * send stoplan command
966  */
967 static int
968 lcs_send_stoplan(struct lcs_card *card, __u8 initiator)
969 {
970         struct lcs_buffer *buffer;
971         struct lcs_cmd *cmd;
972
973         LCS_DBF_TEXT(2, trace, "cmdstpln");
974         buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
975         cmd = (struct lcs_cmd *) buffer->data;
976         cmd->cmd_code = LCS_CMD_STOPLAN;
977         cmd->initiator = initiator;
978         cmd->cmd.lcs_std_cmd.lan_type = card->lan_type;
979         cmd->cmd.lcs_std_cmd.portno = card->portno;
980         return lcs_send_lancmd(card, buffer, NULL);
981 }
982
983 /**
984  * send startlan command
985  */
986 static void
987 __lcs_send_startlan_cb(struct lcs_card *card, struct lcs_cmd *cmd)
988 {
989         LCS_DBF_TEXT(2, trace, "srtlancb");
990         card->lan_type = cmd->cmd.lcs_std_cmd.lan_type;
991         card->portno = cmd->cmd.lcs_std_cmd.portno;
992 }
993
994 static int
995 lcs_send_startlan(struct lcs_card *card, __u8 initiator)
996 {
997         struct lcs_buffer *buffer;
998         struct lcs_cmd *cmd;
999
1000         LCS_DBF_TEXT(2, trace, "cmdstaln");
1001         buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
1002         cmd = (struct lcs_cmd *) buffer->data;
1003         cmd->cmd_code = LCS_CMD_STARTLAN;
1004         cmd->initiator = initiator;
1005         cmd->cmd.lcs_std_cmd.lan_type = card->lan_type;
1006         cmd->cmd.lcs_std_cmd.portno = card->portno;
1007         return lcs_send_lancmd(card, buffer, __lcs_send_startlan_cb);
1008 }
1009
1010 #ifdef CONFIG_IP_MULTICAST
1011 /**
1012  * send setipm command (Multicast)
1013  */
1014 static int
1015 lcs_send_setipm(struct lcs_card *card,struct lcs_ipm_list *ipm_list)
1016 {
1017         struct lcs_buffer *buffer;
1018         struct lcs_cmd *cmd;
1019
1020         LCS_DBF_TEXT(2, trace, "cmdsetim");
1021         buffer = lcs_get_lancmd(card, LCS_MULTICAST_CMD_SIZE);
1022         cmd = (struct lcs_cmd *) buffer->data;
1023         cmd->cmd_code = LCS_CMD_SETIPM;
1024         cmd->initiator = LCS_INITIATOR_TCPIP;
1025         cmd->cmd.lcs_qipassist.lan_type = card->lan_type;
1026         cmd->cmd.lcs_qipassist.portno = card->portno;
1027         cmd->cmd.lcs_qipassist.version = 4;
1028         cmd->cmd.lcs_qipassist.num_ip_pairs = 1;
1029         memcpy(cmd->cmd.lcs_qipassist.lcs_ipass_ctlmsg.ip_mac_pair,
1030                &ipm_list->ipm, sizeof (struct lcs_ip_mac_pair));
1031         LCS_DBF_TEXT_(2, trace, "%x",ipm_list->ipm.ip_addr);
1032         return lcs_send_lancmd(card, buffer, NULL);
1033 }
1034
1035 /**
1036  * send delipm command (Multicast)
1037  */
1038 static int
1039 lcs_send_delipm(struct lcs_card *card,struct lcs_ipm_list *ipm_list)
1040 {
1041         struct lcs_buffer *buffer;
1042         struct lcs_cmd *cmd;
1043
1044         LCS_DBF_TEXT(2, trace, "cmddelim");
1045         buffer = lcs_get_lancmd(card, LCS_MULTICAST_CMD_SIZE);
1046         cmd = (struct lcs_cmd *) buffer->data;
1047         cmd->cmd_code = LCS_CMD_DELIPM;
1048         cmd->initiator = LCS_INITIATOR_TCPIP;
1049         cmd->cmd.lcs_qipassist.lan_type = card->lan_type;
1050         cmd->cmd.lcs_qipassist.portno = card->portno;
1051         cmd->cmd.lcs_qipassist.version = 4;
1052         cmd->cmd.lcs_qipassist.num_ip_pairs = 1;
1053         memcpy(cmd->cmd.lcs_qipassist.lcs_ipass_ctlmsg.ip_mac_pair,
1054                &ipm_list->ipm, sizeof (struct lcs_ip_mac_pair));
1055         LCS_DBF_TEXT_(2, trace, "%x",ipm_list->ipm.ip_addr);
1056         return lcs_send_lancmd(card, buffer, NULL);
1057 }
1058
1059 /**
1060  * check if multicast is supported by LCS
1061  */
1062 static void
1063 __lcs_check_multicast_cb(struct lcs_card *card, struct lcs_cmd *cmd)
1064 {
1065         LCS_DBF_TEXT(2, trace, "chkmccb");
1066         card->ip_assists_supported =
1067                 cmd->cmd.lcs_qipassist.ip_assists_supported;
1068         card->ip_assists_enabled =
1069                 cmd->cmd.lcs_qipassist.ip_assists_enabled;
1070 }
1071
1072 static int
1073 lcs_check_multicast_support(struct lcs_card *card)
1074 {
1075         struct lcs_buffer *buffer;
1076         struct lcs_cmd *cmd;
1077         int rc;
1078
1079         LCS_DBF_TEXT(2, trace, "cmdqipa");
1080         /* Send query ipassist. */
1081         buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
1082         cmd = (struct lcs_cmd *) buffer->data;
1083         cmd->cmd_code = LCS_CMD_QIPASSIST;
1084         cmd->initiator = LCS_INITIATOR_TCPIP;
1085         cmd->cmd.lcs_qipassist.lan_type = card->lan_type;
1086         cmd->cmd.lcs_qipassist.portno = card->portno;
1087         cmd->cmd.lcs_qipassist.version = 4;
1088         cmd->cmd.lcs_qipassist.num_ip_pairs = 1;
1089         rc = lcs_send_lancmd(card, buffer, __lcs_check_multicast_cb);
1090         if (rc != 0) {
1091                 PRINT_ERR("Query IPAssist failed. Assuming unsupported!\n");
1092                 return -EOPNOTSUPP;
1093         }
1094         if (card->ip_assists_supported & LCS_IPASS_MULTICAST_SUPPORT)
1095                 return 0;
1096         return -EOPNOTSUPP;
1097 }
1098
1099 /**
1100  * set or del multicast address on LCS card
1101  */
1102 static void
1103 lcs_fix_multicast_list(struct lcs_card *card)
1104 {
1105         struct list_head failed_list;
1106         struct lcs_ipm_list *ipm, *tmp;
1107         unsigned long flags;
1108         int rc;
1109
1110         LCS_DBF_TEXT(4,trace, "fixipm");
1111         INIT_LIST_HEAD(&failed_list);
1112         spin_lock_irqsave(&card->ipm_lock, flags);
1113 list_modified:
1114         list_for_each_entry_safe(ipm, tmp, &card->ipm_list, list){
1115                 switch (ipm->ipm_state) {
1116                 case LCS_IPM_STATE_SET_REQUIRED:
1117                         /* del from ipm_list so noone else can tamper with
1118                          * this entry */
1119                         list_del_init(&ipm->list);
1120                         spin_unlock_irqrestore(&card->ipm_lock, flags);
1121                         rc = lcs_send_setipm(card, ipm);
1122                         spin_lock_irqsave(&card->ipm_lock, flags);
1123                         if (rc) {
1124                                 PRINT_INFO("Adding multicast address failed."
1125                                            "Table possibly full!\n");
1126                                 /* store ipm in failed list -> will be added
1127                                  * to ipm_list again, so a retry will be done
1128                                  * during the next call of this function */
1129                                 list_add_tail(&ipm->list, &failed_list);
1130                         } else {
1131                                 ipm->ipm_state = LCS_IPM_STATE_ON_CARD;
1132                                 /* re-insert into ipm_list */
1133                                 list_add_tail(&ipm->list, &card->ipm_list);
1134                         }
1135                         goto list_modified;
1136                 case LCS_IPM_STATE_DEL_REQUIRED:
1137                         list_del(&ipm->list);
1138                         spin_unlock_irqrestore(&card->ipm_lock, flags);
1139                         lcs_send_delipm(card, ipm);
1140                         spin_lock_irqsave(&card->ipm_lock, flags);
1141                         kfree(ipm);
1142                         goto list_modified;
1143                 case LCS_IPM_STATE_ON_CARD:
1144                         break;
1145                 }
1146         }
1147         /* re-insert all entries from the failed_list into ipm_list */
1148         list_for_each_entry_safe(ipm, tmp, &failed_list, list) {
1149                 list_del_init(&ipm->list);
1150                 list_add_tail(&ipm->list, &card->ipm_list);
1151         }
1152         spin_unlock_irqrestore(&card->ipm_lock, flags);
1153         if (card->state == DEV_STATE_UP)
1154                 netif_wake_queue(card->dev);
1155 }
1156
1157 /**
1158  * get mac address for the relevant Multicast address
1159  */
1160 static void
1161 lcs_get_mac_for_ipm(__u32 ipm, char *mac, struct net_device *dev)
1162 {
1163         LCS_DBF_TEXT(4,trace, "getmac");
1164         if (dev->type == ARPHRD_IEEE802_TR)
1165                 ip_tr_mc_map(ipm, mac);
1166         else
1167                 ip_eth_mc_map(ipm, mac);
1168 }
1169
1170 /**
1171  * function called by net device to handle multicast address relevant things
1172  */
1173 static inline void
1174 lcs_remove_mc_addresses(struct lcs_card *card, struct in_device *in4_dev)
1175 {
1176         struct ip_mc_list *im4;
1177         struct list_head *l;
1178         struct lcs_ipm_list *ipm;
1179         unsigned long flags;
1180         char buf[MAX_ADDR_LEN];
1181
1182         LCS_DBF_TEXT(4, trace, "remmclst");
1183         spin_lock_irqsave(&card->ipm_lock, flags);
1184         list_for_each(l, &card->ipm_list) {
1185                 ipm = list_entry(l, struct lcs_ipm_list, list);
1186                 for (im4 = in4_dev->mc_list; im4 != NULL; im4 = im4->next) {
1187                         lcs_get_mac_for_ipm(im4->multiaddr, buf, card->dev);
1188                         if ( (ipm->ipm.ip_addr == im4->multiaddr) &&
1189                              (memcmp(buf, &ipm->ipm.mac_addr,
1190                                      LCS_MAC_LENGTH) == 0) )
1191                                 break;
1192                 }
1193                 if (im4 == NULL)
1194                         ipm->ipm_state = LCS_IPM_STATE_DEL_REQUIRED;
1195         }
1196         spin_unlock_irqrestore(&card->ipm_lock, flags);
1197 }
1198
1199 static inline struct lcs_ipm_list *
1200 lcs_check_addr_entry(struct lcs_card *card, struct ip_mc_list *im4, char *buf)
1201 {
1202         struct lcs_ipm_list *tmp, *ipm = NULL;
1203         struct list_head *l;
1204         unsigned long flags;
1205
1206         LCS_DBF_TEXT(4, trace, "chkmcent");
1207         spin_lock_irqsave(&card->ipm_lock, flags);
1208         list_for_each(l, &card->ipm_list) {
1209                 tmp = list_entry(l, struct lcs_ipm_list, list);
1210                 if ( (tmp->ipm.ip_addr == im4->multiaddr) &&
1211                      (memcmp(buf, &tmp->ipm.mac_addr,
1212                              LCS_MAC_LENGTH) == 0) ) {
1213                         ipm = tmp;
1214                         break;
1215                 }
1216         }
1217         spin_unlock_irqrestore(&card->ipm_lock, flags);
1218         return ipm;
1219 }
1220
1221 static inline void
1222 lcs_set_mc_addresses(struct lcs_card *card, struct in_device *in4_dev)
1223 {
1224
1225         struct ip_mc_list *im4;
1226         struct lcs_ipm_list *ipm;
1227         char buf[MAX_ADDR_LEN];
1228         unsigned long flags;
1229
1230         LCS_DBF_TEXT(4, trace, "setmclst");
1231         for (im4 = in4_dev->mc_list; im4; im4 = im4->next) {
1232                 lcs_get_mac_for_ipm(im4->multiaddr, buf, card->dev);
1233                 ipm = lcs_check_addr_entry(card, im4, buf);
1234                 if (ipm != NULL)
1235                         continue;       /* Address already in list. */
1236                 ipm = (struct lcs_ipm_list *)
1237                         kmalloc(sizeof(struct lcs_ipm_list), GFP_ATOMIC);
1238                 if (ipm == NULL) {
1239                         PRINT_INFO("Not enough memory to add "
1240                                    "new multicast entry!\n");
1241                         break;
1242                 }
1243                 memset(ipm, 0, sizeof(struct lcs_ipm_list));
1244                 memcpy(&ipm->ipm.mac_addr, buf, LCS_MAC_LENGTH);
1245                 ipm->ipm.ip_addr = im4->multiaddr;
1246                 ipm->ipm_state = LCS_IPM_STATE_SET_REQUIRED;
1247                 spin_lock_irqsave(&card->ipm_lock, flags);
1248                 list_add(&ipm->list, &card->ipm_list);
1249                 spin_unlock_irqrestore(&card->ipm_lock, flags);
1250         }
1251 }
1252
1253 static int
1254 lcs_register_mc_addresses(void *data)
1255 {
1256         struct lcs_card *card;
1257         struct in_device *in4_dev;
1258
1259         card = (struct lcs_card *) data;
1260         daemonize("regipm");
1261
1262         if (!lcs_do_run_thread(card, LCS_SET_MC_THREAD))
1263                 return 0;
1264         LCS_DBF_TEXT(4, trace, "regmulti");
1265
1266         in4_dev = in_dev_get(card->dev);
1267         if (in4_dev == NULL)
1268                 goto out;
1269         read_lock(&in4_dev->mc_list_lock);
1270         lcs_remove_mc_addresses(card,in4_dev);
1271         lcs_set_mc_addresses(card, in4_dev);
1272         read_unlock(&in4_dev->mc_list_lock);
1273         in_dev_put(in4_dev);
1274
1275         lcs_fix_multicast_list(card);
1276 out:
1277         lcs_clear_thread_running_bit(card, LCS_SET_MC_THREAD);
1278         return 0;
1279 }
1280 /**
1281  * function called by net device to
1282  * handle multicast address relevant things
1283  */
1284 static void
1285 lcs_set_multicast_list(struct net_device *dev)
1286 {
1287         struct lcs_card *card;
1288
1289         LCS_DBF_TEXT(4, trace, "setmulti");
1290         card = (struct lcs_card *) dev->priv;
1291
1292         if (!lcs_set_thread_start_bit(card, LCS_SET_MC_THREAD)) 
1293                 schedule_work(&card->kernel_thread_starter);
1294 }
1295
1296 #endif /* CONFIG_IP_MULTICAST */
1297
1298 static long
1299 lcs_check_irb_error(struct ccw_device *cdev, struct irb *irb)
1300 {
1301         if (!IS_ERR(irb))
1302                 return 0;
1303
1304         switch (PTR_ERR(irb)) {
1305         case -EIO:
1306                 PRINT_WARN("i/o-error on device %s\n", cdev->dev.bus_id);
1307                 LCS_DBF_TEXT(2, trace, "ckirberr");
1308                 LCS_DBF_TEXT_(2, trace, "  rc%d", -EIO);
1309                 break;
1310         case -ETIMEDOUT:
1311                 PRINT_WARN("timeout on device %s\n", cdev->dev.bus_id);
1312                 LCS_DBF_TEXT(2, trace, "ckirberr");
1313                 LCS_DBF_TEXT_(2, trace, "  rc%d", -ETIMEDOUT);
1314                 break;
1315         default:
1316                 PRINT_WARN("unknown error %ld on device %s\n", PTR_ERR(irb),
1317                            cdev->dev.bus_id);
1318                 LCS_DBF_TEXT(2, trace, "ckirberr");
1319                 LCS_DBF_TEXT(2, trace, "  rc???");
1320         }
1321         return PTR_ERR(irb);
1322 }
1323
1324
1325 /**
1326  * IRQ Handler for LCS channels
1327  */
1328 static void
1329 lcs_irq(struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
1330 {
1331         struct lcs_card *card;
1332         struct lcs_channel *channel;
1333         int index;
1334
1335         if (lcs_check_irb_error(cdev, irb))
1336                 return;
1337
1338         card = CARD_FROM_DEV(cdev);
1339         if (card->read.ccwdev == cdev)
1340                 channel = &card->read;
1341         else
1342                 channel = &card->write;
1343
1344         LCS_DBF_TEXT_(5, trace, "Rint%s",cdev->dev.bus_id);
1345         LCS_DBF_TEXT_(5, trace, "%4x%4x",irb->scsw.cstat, irb->scsw.dstat);
1346         LCS_DBF_TEXT_(5, trace, "%4x%4x",irb->scsw.fctl, irb->scsw.actl);
1347
1348         /* How far in the ccw chain have we processed? */
1349         if ((channel->state != CH_STATE_INIT) &&
1350             (irb->scsw.fctl & SCSW_FCTL_START_FUNC)) {
1351                 index = (struct ccw1 *) __va((addr_t) irb->scsw.cpa) 
1352                         - channel->ccws;
1353                 if ((irb->scsw.actl & SCSW_ACTL_SUSPENDED) ||
1354                     (irb->scsw.cstat | SCHN_STAT_PCI))
1355                         /* Bloody io subsystem tells us lies about cpa... */
1356                         index = (index - 1) & (LCS_NUM_BUFFS - 1);
1357                 while (channel->io_idx != index) {
1358                         __lcs_processed_buffer(channel,
1359                                                channel->iob + channel->io_idx);
1360                         channel->io_idx =
1361                                 (channel->io_idx + 1) & (LCS_NUM_BUFFS - 1);
1362                 }
1363         }
1364
1365         if ((irb->scsw.dstat & DEV_STAT_DEV_END) ||
1366             (irb->scsw.dstat & DEV_STAT_CHN_END) ||
1367             (irb->scsw.dstat & DEV_STAT_UNIT_CHECK))
1368                 /* Mark channel as stopped. */
1369                 channel->state = CH_STATE_STOPPED;
1370         else if (irb->scsw.actl & SCSW_ACTL_SUSPENDED)
1371                 /* CCW execution stopped on a suspend bit. */
1372                 channel->state = CH_STATE_SUSPENDED;
1373
1374         if (irb->scsw.fctl & SCSW_FCTL_HALT_FUNC) {
1375                 if (irb->scsw.cc != 0) {
1376                         ccw_device_halt(channel->ccwdev, (addr_t) channel);
1377                         return;
1378                 }
1379                 /* The channel has been stopped by halt_IO. */
1380                 channel->state = CH_STATE_HALTED;
1381         }
1382
1383         if (irb->scsw.fctl & SCSW_FCTL_CLEAR_FUNC) {
1384                 channel->state = CH_STATE_CLEARED;
1385         }
1386         /* Do the rest in the tasklet. */
1387         tasklet_schedule(&channel->irq_tasklet);
1388 }
1389
1390 /**
1391  * Tasklet for IRQ handler
1392  */
1393 static void
1394 lcs_tasklet(unsigned long data)
1395 {
1396         unsigned long flags;
1397         struct lcs_channel *channel;
1398         struct lcs_buffer *iob;
1399         int buf_idx;
1400         int rc;
1401
1402         channel = (struct lcs_channel *) data;
1403         LCS_DBF_TEXT_(5, trace, "tlet%s",channel->ccwdev->dev.bus_id);
1404
1405         /* Check for processed buffers. */
1406         iob = channel->iob;
1407         buf_idx = channel->buf_idx;
1408         while (iob[buf_idx].state == BUF_STATE_PROCESSED) {
1409                 /* Do the callback thing. */
1410                 if (iob[buf_idx].callback != NULL)
1411                         iob[buf_idx].callback(channel, iob + buf_idx);
1412                 buf_idx = (buf_idx + 1) & (LCS_NUM_BUFFS - 1);
1413         }
1414         channel->buf_idx = buf_idx;
1415
1416         if (channel->state == CH_STATE_STOPPED)
1417                 // FIXME: what if rc != 0 ??
1418                 rc = lcs_start_channel(channel);
1419         spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1420         if (channel->state == CH_STATE_SUSPENDED &&
1421             channel->iob[channel->io_idx].state == BUF_STATE_READY) {
1422                 // FIXME: what if rc != 0 ??
1423                 rc = __lcs_resume_channel(channel);
1424         }
1425         spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1426
1427         /* Something happened on the channel. Wake up waiters. */
1428         wake_up(&channel->wait_q);
1429 }
1430
1431 /**
1432  * Finish current tx buffer and make it ready for transmit.
1433  */
1434 static void
1435 __lcs_emit_txbuffer(struct lcs_card *card)
1436 {
1437         LCS_DBF_TEXT(5, trace, "emittx");
1438         *(__u16 *)(card->tx_buffer->data + card->tx_buffer->count) = 0;
1439         card->tx_buffer->count += 2;
1440         lcs_ready_buffer(&card->write, card->tx_buffer);
1441         card->tx_buffer = NULL;
1442         card->tx_emitted++;
1443 }
1444
1445 /**
1446  * Callback for finished tx buffers.
1447  */
1448 static void
1449 lcs_txbuffer_cb(struct lcs_channel *channel, struct lcs_buffer *buffer)
1450 {
1451         struct lcs_card *card;
1452
1453         LCS_DBF_TEXT(5, trace, "txbuffcb");
1454         /* Put buffer back to pool. */
1455         lcs_release_buffer(channel, buffer);
1456         card = (struct lcs_card *)
1457                 ((char *) channel - offsetof(struct lcs_card, write));
1458         if (netif_queue_stopped(card->dev))
1459                 netif_wake_queue(card->dev);
1460         spin_lock(&card->lock);
1461         card->tx_emitted--;
1462         if (card->tx_emitted <= 0 && card->tx_buffer != NULL)
1463                 /*
1464                  * Last running tx buffer has finished. Submit partially
1465                  * filled current buffer.
1466                  */
1467                 __lcs_emit_txbuffer(card);
1468         spin_unlock(&card->lock);
1469 }
1470
1471 /**
1472  * Packet transmit function called by network stack
1473  */
1474 static int
1475 __lcs_start_xmit(struct lcs_card *card, struct sk_buff *skb,
1476                  struct net_device *dev)
1477 {
1478         struct lcs_header *header;
1479         int rc = 0;
1480
1481         LCS_DBF_TEXT(5, trace, "hardxmit");
1482         if (skb == NULL) {
1483                 card->stats.tx_dropped++;
1484                 card->stats.tx_errors++;
1485                 return -EIO;
1486         }
1487         if (card->state != DEV_STATE_UP) {
1488                 dev_kfree_skb(skb);
1489                 card->stats.tx_dropped++;
1490                 card->stats.tx_errors++;
1491                 card->stats.tx_carrier_errors++;
1492                 return 0;
1493         }
1494         netif_stop_queue(card->dev);
1495         spin_lock(&card->lock);
1496         if (card->tx_buffer != NULL &&
1497             card->tx_buffer->count + sizeof(struct lcs_header) +
1498             skb->len + sizeof(u16) > LCS_IOBUFFERSIZE)
1499                 /* skb too big for current tx buffer. */
1500                 __lcs_emit_txbuffer(card);
1501         if (card->tx_buffer == NULL) {
1502                 /* Get new tx buffer */
1503                 card->tx_buffer = lcs_get_buffer(&card->write);
1504                 if (card->tx_buffer == NULL) {
1505                         card->stats.tx_dropped++;
1506                         rc = -EBUSY;
1507                         goto out;
1508                 }
1509                 card->tx_buffer->callback = lcs_txbuffer_cb;
1510                 card->tx_buffer->count = 0;
1511         }
1512         header = (struct lcs_header *)
1513                 (card->tx_buffer->data + card->tx_buffer->count);
1514         card->tx_buffer->count += skb->len + sizeof(struct lcs_header);
1515         header->offset = card->tx_buffer->count;
1516         header->type = card->lan_type;
1517         header->slot = card->portno;
1518         memcpy(header + 1, skb->data, skb->len);
1519         spin_unlock(&card->lock);
1520         card->stats.tx_bytes += skb->len;
1521         card->stats.tx_packets++;
1522         dev_kfree_skb(skb);
1523         netif_wake_queue(card->dev);
1524         spin_lock(&card->lock);
1525         if (card->tx_emitted <= 0 && card->tx_buffer != NULL)
1526                 /* If this is the first tx buffer emit it immediately. */
1527                 __lcs_emit_txbuffer(card);
1528 out:
1529         spin_unlock(&card->lock);
1530         return rc;
1531 }
1532
1533 static int
1534 lcs_start_xmit(struct sk_buff *skb, struct net_device *dev)
1535 {
1536         struct lcs_card *card;
1537         int rc;
1538
1539         LCS_DBF_TEXT(5, trace, "pktxmit");
1540         card = (struct lcs_card *) dev->priv;
1541         rc = __lcs_start_xmit(card, skb, dev);
1542         return rc;
1543 }
1544
1545 /**
1546  * send startlan and lanstat command to make LCS device ready
1547  */
1548 static int
1549 lcs_startlan_auto(struct lcs_card *card)
1550 {
1551         int rc;
1552
1553         LCS_DBF_TEXT(2, trace, "strtauto");
1554 #ifdef CONFIG_NET_ETHERNET
1555         card->lan_type = LCS_FRAME_TYPE_ENET;
1556         rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
1557         if (rc == 0)
1558                 return 0;
1559
1560 #endif
1561 #ifdef CONFIG_TR
1562         card->lan_type = LCS_FRAME_TYPE_TR;
1563         rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
1564         if (rc == 0)
1565                 return 0;
1566 #endif
1567 #ifdef CONFIG_FDDI
1568         card->lan_type = LCS_FRAME_TYPE_FDDI;
1569         rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
1570         if (rc == 0)
1571                 return 0;
1572 #endif
1573         return -EIO;
1574 }
1575
1576 static int
1577 lcs_startlan(struct lcs_card *card)
1578 {
1579         int rc, i;
1580
1581         LCS_DBF_TEXT(2, trace, "startlan");
1582         rc = 0;
1583         if (card->portno != LCS_INVALID_PORT_NO) {
1584                 if (card->lan_type == LCS_FRAME_TYPE_AUTO)
1585                         rc = lcs_startlan_auto(card);
1586                 else
1587                         rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
1588         } else {
1589                 for (i = 0; i <= 16; i++) {
1590                         card->portno = i;
1591                         if (card->lan_type != LCS_FRAME_TYPE_AUTO)
1592                                 rc = lcs_send_startlan(card,
1593                                                        LCS_INITIATOR_TCPIP);
1594                         else
1595                                 /* autodetecting lan type */
1596                                 rc = lcs_startlan_auto(card);
1597                         if (rc == 0)
1598                                 break;
1599                 }
1600         }
1601         if (rc == 0)
1602                 return lcs_send_lanstat(card);
1603         return rc;
1604 }
1605
1606 /**
1607  * LCS detect function
1608  * setup channels and make them I/O ready
1609  */
1610 static int
1611 lcs_detect(struct lcs_card *card)
1612 {
1613         int rc = 0;
1614
1615         LCS_DBF_TEXT(2, setup, "lcsdetct");
1616         /* start/reset card */
1617         if (card->dev)
1618                 netif_stop_queue(card->dev);
1619         rc = lcs_stop_channels(card);
1620         if (rc == 0) {
1621                 rc = lcs_start_channels(card);
1622                 if (rc == 0) {
1623                         rc = lcs_send_startup(card, LCS_INITIATOR_TCPIP);
1624                         if (rc == 0)
1625                                 rc = lcs_startlan(card);
1626                 }
1627         }
1628         if (rc == 0) {
1629                 card->state = DEV_STATE_UP;
1630         } else {
1631                 card->state = DEV_STATE_DOWN;
1632                 card->write.state = CH_STATE_INIT;
1633                 card->read.state =  CH_STATE_INIT;
1634         }
1635         return rc;
1636 }
1637
1638 /**
1639  * reset card
1640  */
1641 static int
1642 lcs_resetcard(struct lcs_card *card)
1643 {
1644         int retries;
1645
1646         LCS_DBF_TEXT(2, trace, "rescard");
1647         for (retries = 0; retries < 10; retries++) {
1648                 if (lcs_detect(card) == 0) {
1649                         netif_wake_queue(card->dev);
1650                         card->state = DEV_STATE_UP;
1651                         PRINT_INFO("LCS device %s successfully restarted!\n",
1652                                    card->dev->name);
1653                         return 0;
1654                 }
1655                 msleep(3000);
1656         }
1657         PRINT_ERR("Error in Reseting LCS card!\n");
1658         return -EIO;
1659 }
1660
1661
1662 /**
1663  * LCS Stop card
1664  */
1665 static int
1666 lcs_stopcard(struct lcs_card *card)
1667 {
1668         int rc;
1669
1670         LCS_DBF_TEXT(3, setup, "stopcard");
1671
1672         if (card->read.state != CH_STATE_STOPPED &&
1673             card->write.state != CH_STATE_STOPPED &&
1674             card->state == DEV_STATE_UP) {
1675                 lcs_clear_multicast_list(card);
1676                 rc = lcs_send_stoplan(card,LCS_INITIATOR_TCPIP);
1677                 rc = lcs_send_shutdown(card);
1678         }
1679         rc = lcs_stop_channels(card);
1680         card->state = DEV_STATE_DOWN;
1681
1682         return rc;
1683 }
1684
1685 /**
1686  * LGW initiated commands
1687  */
1688 static int
1689 lcs_lgw_startlan_thread(void *data)
1690 {
1691         struct lcs_card *card;
1692
1693         card = (struct lcs_card *) data;
1694         daemonize("lgwstpln");
1695
1696         if (!lcs_do_run_thread(card, LCS_STARTLAN_THREAD))
1697                 return 0;
1698         LCS_DBF_TEXT(4, trace, "lgwstpln");
1699         if (card->dev)
1700                 netif_stop_queue(card->dev);
1701         if (lcs_startlan(card) == 0) {
1702                 netif_wake_queue(card->dev);
1703                 card->state = DEV_STATE_UP;
1704                 PRINT_INFO("LCS Startlan for device %s succeeded!\n",
1705                            card->dev->name);
1706
1707         } else
1708                 PRINT_ERR("LCS Startlan for device %s failed!\n",
1709                           card->dev->name);
1710         lcs_clear_thread_running_bit(card, LCS_STARTLAN_THREAD);
1711         return 0;
1712 }
1713
1714 /**
1715  * Send startup command initiated by Lan Gateway
1716  */
1717 static int
1718 lcs_lgw_startup_thread(void *data)
1719 {
1720         int rc;
1721
1722         struct lcs_card *card;
1723
1724         card = (struct lcs_card *) data;
1725         daemonize("lgwstaln");
1726
1727         if (!lcs_do_run_thread(card, LCS_STARTUP_THREAD))
1728                 return 0;
1729         LCS_DBF_TEXT(4, trace, "lgwstaln");
1730         if (card->dev)
1731                 netif_stop_queue(card->dev);
1732         rc = lcs_send_startup(card, LCS_INITIATOR_LGW);
1733         if (rc != 0) {
1734                 PRINT_ERR("Startup for LCS device %s initiated " \
1735                           "by LGW failed!\nReseting card ...\n",
1736                           card->dev->name);
1737                 /* do a card reset */
1738                 rc = lcs_resetcard(card);
1739                 if (rc == 0)
1740                         goto Done;
1741         }
1742         rc = lcs_startlan(card);
1743         if (rc == 0) {
1744                 netif_wake_queue(card->dev);
1745                 card->state = DEV_STATE_UP;
1746         }
1747 Done:
1748         if (rc == 0)
1749                 PRINT_INFO("LCS Startup for device %s succeeded!\n",
1750                            card->dev->name);
1751         else
1752                 PRINT_ERR("LCS Startup for device %s failed!\n",
1753                           card->dev->name);
1754         lcs_clear_thread_running_bit(card, LCS_STARTUP_THREAD);
1755         return 0;
1756 }
1757
1758
1759 /**
1760  * send stoplan command initiated by Lan Gateway
1761  */
1762 static int
1763 lcs_lgw_stoplan_thread(void *data)
1764 {
1765         struct lcs_card *card;
1766         int rc;
1767
1768         card = (struct lcs_card *) data;
1769         daemonize("lgwstop");
1770
1771         if (!lcs_do_run_thread(card, LCS_STOPLAN_THREAD))
1772                 return 0;
1773         LCS_DBF_TEXT(4, trace, "lgwstop");
1774         if (card->dev)
1775                 netif_stop_queue(card->dev);
1776         if (lcs_send_stoplan(card, LCS_INITIATOR_LGW) == 0)
1777                 PRINT_INFO("Stoplan for %s initiated by LGW succeeded!\n",
1778                            card->dev->name);
1779         else
1780                 PRINT_ERR("Stoplan %s initiated by LGW failed!\n",
1781                           card->dev->name);
1782         /*Try to reset the card, stop it on failure */
1783         rc = lcs_resetcard(card);
1784         if (rc != 0)
1785                 rc = lcs_stopcard(card);
1786         lcs_clear_thread_running_bit(card, LCS_STOPLAN_THREAD);
1787         return rc;
1788 }
1789
1790 /**
1791  * Kernel Thread helper functions for LGW initiated commands
1792  */
1793 static void
1794 lcs_start_kernel_thread(struct lcs_card *card)
1795 {
1796         LCS_DBF_TEXT(5, trace, "krnthrd");
1797         if (lcs_do_start_thread(card, LCS_STARTUP_THREAD))
1798                 kernel_thread(lcs_lgw_startup_thread, (void *) card, SIGCHLD);
1799         if (lcs_do_start_thread(card, LCS_STARTLAN_THREAD))
1800                 kernel_thread(lcs_lgw_startlan_thread, (void *) card, SIGCHLD);
1801         if (lcs_do_start_thread(card, LCS_STOPLAN_THREAD))
1802                 kernel_thread(lcs_lgw_stoplan_thread, (void *) card, SIGCHLD);
1803 #ifdef CONFIG_IP_MULTICAST
1804         if (lcs_do_start_thread(card, LCS_SET_MC_THREAD))
1805                 kernel_thread(lcs_register_mc_addresses, (void *) card, SIGCHLD);
1806 #endif
1807 }
1808
1809 /**
1810  * Process control frames.
1811  */
1812 static void
1813 lcs_get_control(struct lcs_card *card, struct lcs_cmd *cmd)
1814 {
1815         LCS_DBF_TEXT(5, trace, "getctrl");
1816         if (cmd->initiator == LCS_INITIATOR_LGW) {
1817                 switch(cmd->cmd_code) {
1818                 case LCS_CMD_STARTUP:
1819                         if (!lcs_set_thread_start_bit(card,
1820                                                       LCS_STARTUP_THREAD))
1821                                 schedule_work(&card->kernel_thread_starter);
1822                         break;
1823                 case LCS_CMD_STARTLAN:
1824                         if (!lcs_set_thread_start_bit(card,
1825                                                       LCS_STARTLAN_THREAD))
1826                                 schedule_work(&card->kernel_thread_starter);
1827                         break;
1828                 case LCS_CMD_STOPLAN:
1829                         if (!lcs_set_thread_start_bit(card,
1830                                                       LCS_STOPLAN_THREAD))
1831                                 schedule_work(&card->kernel_thread_starter);
1832                         break;
1833                 default:
1834                         PRINT_INFO("UNRECOGNIZED LGW COMMAND\n");
1835                         break;
1836                 }
1837         } else
1838                 lcs_notify_lancmd_waiters(card, cmd);
1839 }
1840
1841 /**
1842  * Unpack network packet.
1843  */
1844 static void
1845 lcs_get_skb(struct lcs_card *card, char *skb_data, unsigned int skb_len)
1846 {
1847         struct sk_buff *skb;
1848
1849         LCS_DBF_TEXT(5, trace, "getskb");
1850         if (card->dev == NULL ||
1851             card->state != DEV_STATE_UP)
1852                 /* The card isn't up. Ignore the packet. */
1853                 return;
1854
1855         skb = dev_alloc_skb(skb_len);
1856         if (skb == NULL) {
1857                 PRINT_ERR("LCS: alloc_skb failed for device=%s\n",
1858                           card->dev->name);
1859                 card->stats.rx_dropped++;
1860                 return;
1861         }
1862         skb->dev = card->dev;
1863         memcpy(skb_put(skb, skb_len), skb_data, skb_len);
1864         skb->protocol = card->lan_type_trans(skb, card->dev);
1865         card->stats.rx_bytes += skb_len;
1866         card->stats.rx_packets++;
1867         *((__u32 *)skb->cb) = ++card->pkt_seq;
1868         netif_rx(skb);
1869 }
1870
1871 /**
1872  * LCS main routine to get packets and lancmd replies from the buffers
1873  */
1874 static void
1875 lcs_get_frames_cb(struct lcs_channel *channel, struct lcs_buffer *buffer)
1876 {
1877         struct lcs_card *card;
1878         struct lcs_header *lcs_hdr;
1879         __u16 offset;
1880
1881         LCS_DBF_TEXT(5, trace, "lcsgtpkt");
1882         lcs_hdr = (struct lcs_header *) buffer->data;
1883         if (lcs_hdr->offset == LCS_ILLEGAL_OFFSET) {
1884                 LCS_DBF_TEXT(4, trace, "-eiogpkt");
1885                 return;
1886         }
1887         card = (struct lcs_card *)
1888                 ((char *) channel - offsetof(struct lcs_card, read));
1889         offset = 0;
1890         while (lcs_hdr->offset != 0) {
1891                 if (lcs_hdr->offset <= 0 ||
1892                     lcs_hdr->offset > LCS_IOBUFFERSIZE ||
1893                     lcs_hdr->offset < offset) {
1894                         /* Offset invalid. */
1895                         card->stats.rx_length_errors++;
1896                         card->stats.rx_errors++;
1897                         return;
1898                 }
1899                 /* What kind of frame is it? */
1900                 if (lcs_hdr->type == LCS_FRAME_TYPE_CONTROL)
1901                         /* Control frame. */
1902                         lcs_get_control(card, (struct lcs_cmd *) lcs_hdr);
1903                 else if (lcs_hdr->type == LCS_FRAME_TYPE_ENET ||
1904                          lcs_hdr->type == LCS_FRAME_TYPE_TR ||
1905                          lcs_hdr->type == LCS_FRAME_TYPE_FDDI)
1906                         /* Normal network packet. */
1907                         lcs_get_skb(card, (char *)(lcs_hdr + 1),
1908                                     lcs_hdr->offset - offset -
1909                                     sizeof(struct lcs_header));
1910                 else
1911                         /* Unknown frame type. */
1912                         ; // FIXME: error message ?
1913                 /* Proceed to next frame. */
1914                 offset = lcs_hdr->offset;
1915                 lcs_hdr->offset = LCS_ILLEGAL_OFFSET;
1916                 lcs_hdr = (struct lcs_header *) (buffer->data + offset);
1917         }
1918         /* The buffer is now empty. Make it ready again. */
1919         lcs_ready_buffer(&card->read, buffer);
1920 }
1921
1922 /**
1923  * get network statistics for ifconfig and other user programs
1924  */
1925 static struct net_device_stats *
1926 lcs_getstats(struct net_device *dev)
1927 {
1928         struct lcs_card *card;
1929
1930         LCS_DBF_TEXT(4, trace, "netstats");
1931         card = (struct lcs_card *) dev->priv;
1932         return &card->stats;
1933 }
1934
1935 /**
1936  * stop lcs device
1937  * This function will be called by user doing ifconfig xxx down
1938  */
1939 static int
1940 lcs_stop_device(struct net_device *dev)
1941 {
1942         struct lcs_card *card;
1943         int rc;
1944
1945         LCS_DBF_TEXT(2, trace, "stopdev");
1946         card   = (struct lcs_card *) dev->priv;
1947         netif_stop_queue(dev);
1948         dev->flags &= ~IFF_UP;
1949         rc = lcs_stopcard(card);
1950         if (rc)
1951                 PRINT_ERR("Try it again!\n ");
1952         return rc;
1953 }
1954
1955 /**
1956  * start lcs device and make it runnable
1957  * This function will be called by user doing ifconfig xxx up
1958  */
1959 static int
1960 lcs_open_device(struct net_device *dev)
1961 {
1962         struct lcs_card *card;
1963         int rc;
1964
1965         LCS_DBF_TEXT(2, trace, "opendev");
1966         card = (struct lcs_card *) dev->priv;
1967         /* initialize statistics */
1968         rc = lcs_detect(card);
1969         if (rc) {
1970                 PRINT_ERR("LCS:Error in opening device!\n");
1971
1972         } else {
1973                 dev->flags |= IFF_UP;
1974                 netif_wake_queue(dev);
1975                 card->state = DEV_STATE_UP;
1976         }
1977         return rc;
1978 }
1979
1980 /**
1981  * show function for portno called by cat or similar things
1982  */
1983 static ssize_t
1984 lcs_portno_show (struct device *dev, struct device_attribute *attr, char *buf)
1985 {
1986         struct lcs_card *card;
1987
1988         card = (struct lcs_card *)dev->driver_data;
1989
1990         if (!card)
1991                 return 0;
1992
1993         return sprintf(buf, "%d\n", card->portno);
1994 }
1995
1996 /**
1997  * store the value which is piped to file portno
1998  */
1999 static ssize_t
2000 lcs_portno_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
2001 {
2002         struct lcs_card *card;
2003         int value;
2004
2005         card = (struct lcs_card *)dev->driver_data;
2006
2007         if (!card)
2008                 return 0;
2009
2010         sscanf(buf, "%u", &value);
2011         /* TODO: sanity checks */
2012         card->portno = value;
2013
2014         return count;
2015
2016 }
2017
2018 static DEVICE_ATTR(portno, 0644, lcs_portno_show, lcs_portno_store);
2019
2020 static ssize_t
2021 lcs_type_show(struct device *dev, struct device_attribute *attr, char *buf)
2022 {
2023         struct ccwgroup_device *cgdev;
2024
2025         cgdev = to_ccwgroupdev(dev);
2026         if (!cgdev)
2027                 return -ENODEV;
2028
2029         return sprintf(buf, "%s\n", cu3088_type[cgdev->cdev[0]->id.driver_info]);
2030 }
2031
2032 static DEVICE_ATTR(type, 0444, lcs_type_show, NULL);
2033
2034 static ssize_t
2035 lcs_timeout_show(struct device *dev, struct device_attribute *attr, char *buf)
2036 {
2037         struct lcs_card *card;
2038
2039         card = (struct lcs_card *)dev->driver_data;
2040
2041         return card ? sprintf(buf, "%u\n", card->lancmd_timeout) : 0;
2042 }
2043
2044 static ssize_t
2045 lcs_timeout_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
2046 {
2047         struct lcs_card *card;
2048         int value;
2049
2050         card = (struct lcs_card *)dev->driver_data;
2051
2052         if (!card)
2053                 return 0;
2054
2055         sscanf(buf, "%u", &value);
2056         /* TODO: sanity checks */
2057         card->lancmd_timeout = value;
2058
2059         return count;
2060
2061 }
2062
2063 DEVICE_ATTR(lancmd_timeout, 0644, lcs_timeout_show, lcs_timeout_store);
2064
2065 static struct attribute * lcs_attrs[] = {
2066         &dev_attr_portno.attr,
2067         &dev_attr_type.attr,
2068         &dev_attr_lancmd_timeout.attr,
2069         NULL,
2070 };
2071
2072 static struct attribute_group lcs_attr_group = {
2073         .attrs = lcs_attrs,
2074 };
2075
2076 /**
2077  * lcs_probe_device is called on establishing a new ccwgroup_device.
2078  */
2079 static int
2080 lcs_probe_device(struct ccwgroup_device *ccwgdev)
2081 {
2082         struct lcs_card *card;
2083         int ret;
2084
2085         if (!get_device(&ccwgdev->dev))
2086                 return -ENODEV;
2087
2088         LCS_DBF_TEXT(2, setup, "add_dev");
2089         card = lcs_alloc_card();
2090         if (!card) {
2091                 PRINT_ERR("Allocation of lcs card failed\n");
2092                 put_device(&ccwgdev->dev);
2093                 return -ENOMEM;
2094         }
2095         ret = sysfs_create_group(&ccwgdev->dev.kobj, &lcs_attr_group);
2096         if (ret) {
2097                 PRINT_ERR("Creating attributes failed");
2098                 lcs_free_card(card);
2099                 put_device(&ccwgdev->dev);
2100                 return ret;
2101         }
2102         ccwgdev->dev.driver_data = card;
2103         ccwgdev->cdev[0]->handler = lcs_irq;
2104         ccwgdev->cdev[1]->handler = lcs_irq;
2105         return 0;
2106 }
2107
2108 static int
2109 lcs_register_netdev(struct ccwgroup_device *ccwgdev)
2110 {
2111         struct lcs_card *card;
2112
2113         LCS_DBF_TEXT(2, setup, "regnetdv");
2114         card = (struct lcs_card *)ccwgdev->dev.driver_data;
2115         if (card->dev->reg_state != NETREG_UNINITIALIZED)
2116                 return 0;
2117         SET_NETDEV_DEV(card->dev, &ccwgdev->dev);
2118         return register_netdev(card->dev);
2119 }
2120
2121 /**
2122  * lcs_new_device will be called by setting the group device online.
2123  */
2124
2125 static int
2126 lcs_new_device(struct ccwgroup_device *ccwgdev)
2127 {
2128         struct  lcs_card *card;
2129         struct net_device *dev=NULL;
2130         enum lcs_dev_states recover_state;
2131         int rc;
2132
2133         card = (struct lcs_card *)ccwgdev->dev.driver_data;
2134         if (!card)
2135                 return -ENODEV;
2136
2137         LCS_DBF_TEXT(2, setup, "newdev");
2138         LCS_DBF_HEX(3, setup, &card, sizeof(void*));
2139         card->read.ccwdev  = ccwgdev->cdev[0];
2140         card->write.ccwdev = ccwgdev->cdev[1];
2141
2142         recover_state = card->state;
2143         ccw_device_set_online(card->read.ccwdev);
2144         ccw_device_set_online(card->write.ccwdev);
2145
2146         LCS_DBF_TEXT(3, setup, "lcsnewdv");
2147
2148         lcs_setup_card(card);
2149         rc = lcs_detect(card);
2150         if (rc) {
2151                 LCS_DBF_TEXT(2, setup, "dtctfail");
2152                 PRINT_WARN("Detection of LCS card failed with return code "
2153                            "%d (0x%x)\n", rc, rc);
2154                 lcs_stopcard(card);
2155                 goto out;
2156         }
2157         if (card->dev) {
2158                 LCS_DBF_TEXT(2, setup, "samedev");
2159                 LCS_DBF_HEX(3, setup, &card, sizeof(void*));
2160                 goto netdev_out;
2161         }
2162         switch (card->lan_type) {
2163 #ifdef CONFIG_NET_ETHERNET
2164         case LCS_FRAME_TYPE_ENET:
2165                 card->lan_type_trans = eth_type_trans;
2166                 dev = alloc_etherdev(0);
2167                 break;
2168 #endif
2169 #ifdef CONFIG_TR
2170         case LCS_FRAME_TYPE_TR:
2171                 card->lan_type_trans = tr_type_trans;
2172                 dev = alloc_trdev(0);
2173                 break;
2174 #endif
2175 #ifdef CONFIG_FDDI
2176         case LCS_FRAME_TYPE_FDDI:
2177                 card->lan_type_trans = fddi_type_trans;
2178                 dev = alloc_fddidev(0);
2179                 break;
2180 #endif
2181         default:
2182                 LCS_DBF_TEXT(3, setup, "errinit");
2183                 PRINT_ERR("LCS: Initialization failed\n");
2184                 PRINT_ERR("LCS: No device found!\n");
2185                 goto out;
2186         }
2187         if (!dev)
2188                 goto out;
2189         card->dev = dev;
2190         card->dev->priv = card;
2191         card->dev->open = lcs_open_device;
2192         card->dev->stop = lcs_stop_device;
2193         card->dev->hard_start_xmit = lcs_start_xmit;
2194         card->dev->get_stats = lcs_getstats;
2195         SET_MODULE_OWNER(dev);
2196         memcpy(card->dev->dev_addr, card->mac, LCS_MAC_LENGTH);
2197 #ifdef CONFIG_IP_MULTICAST
2198         if (!lcs_check_multicast_support(card))
2199                 card->dev->set_multicast_list = lcs_set_multicast_list;
2200 #endif
2201 netdev_out:
2202         lcs_set_allowed_threads(card,0xffffffff);
2203         if (recover_state == DEV_STATE_RECOVER) {
2204                 lcs_set_multicast_list(card->dev);
2205                 card->dev->flags |= IFF_UP;
2206                 netif_wake_queue(card->dev);
2207                 card->state = DEV_STATE_UP;
2208         } else {
2209                 lcs_stopcard(card);
2210         }
2211
2212         if (lcs_register_netdev(ccwgdev) != 0)
2213                 goto out;
2214
2215         /* Print out supported assists: IPv6 */
2216         PRINT_INFO("LCS device %s %s IPv6 support\n", card->dev->name,
2217                    (card->ip_assists_supported & LCS_IPASS_IPV6_SUPPORT) ?
2218                    "with" : "without");
2219         /* Print out supported assist: Multicast */
2220         PRINT_INFO("LCS device %s %s Multicast support\n", card->dev->name,
2221                    (card->ip_assists_supported & LCS_IPASS_MULTICAST_SUPPORT) ?
2222                    "with" : "without");
2223         return 0;
2224 out:
2225
2226         ccw_device_set_offline(card->read.ccwdev);
2227         ccw_device_set_offline(card->write.ccwdev);
2228         return -ENODEV;
2229 }
2230
2231 /**
2232  * lcs_shutdown_device, called when setting the group device offline.
2233  */
2234 static int
2235 lcs_shutdown_device(struct ccwgroup_device *ccwgdev)
2236 {
2237         struct lcs_card *card;
2238         enum lcs_dev_states recover_state;
2239         int ret;
2240
2241         LCS_DBF_TEXT(3, setup, "shtdndev");
2242         card = (struct lcs_card *)ccwgdev->dev.driver_data;
2243         if (!card)
2244                 return -ENODEV;
2245         lcs_set_allowed_threads(card, 0);
2246         if (lcs_wait_for_threads(card, LCS_SET_MC_THREAD))
2247                 return -ERESTARTSYS;
2248         LCS_DBF_HEX(3, setup, &card, sizeof(void*));
2249         recover_state = card->state;
2250
2251         ret = lcs_stop_device(card->dev);
2252         ret = ccw_device_set_offline(card->read.ccwdev);
2253         ret = ccw_device_set_offline(card->write.ccwdev);
2254         if (recover_state == DEV_STATE_UP) {
2255                 card->state = DEV_STATE_RECOVER;
2256         }
2257         if (ret)
2258                 return ret;
2259         return 0;
2260 }
2261
2262 /**
2263  * lcs_remove_device, free buffers and card
2264  */
2265 static void
2266 lcs_remove_device(struct ccwgroup_device *ccwgdev)
2267 {
2268         struct lcs_card *card;
2269
2270         card = (struct lcs_card *)ccwgdev->dev.driver_data;
2271         if (!card)
2272                 return;
2273
2274         PRINT_INFO("Removing lcs group device ....\n");
2275         LCS_DBF_TEXT(3, setup, "remdev");
2276         LCS_DBF_HEX(3, setup, &card, sizeof(void*));
2277         if (ccwgdev->state == CCWGROUP_ONLINE) {
2278                 lcs_shutdown_device(ccwgdev);
2279         }
2280         if (card->dev)
2281                 unregister_netdev(card->dev);
2282         sysfs_remove_group(&ccwgdev->dev.kobj, &lcs_attr_group);
2283         lcs_cleanup_card(card);
2284         lcs_free_card(card);
2285         put_device(&ccwgdev->dev);
2286 }
2287
2288 /**
2289  * LCS ccwgroup driver registration
2290  */
2291 static struct ccwgroup_driver lcs_group_driver = {
2292         .owner       = THIS_MODULE,
2293         .name        = "lcs",
2294         .max_slaves  = 2,
2295         .driver_id   = 0xD3C3E2,
2296         .probe       = lcs_probe_device,
2297         .remove      = lcs_remove_device,
2298         .set_online  = lcs_new_device,
2299         .set_offline = lcs_shutdown_device,
2300 };
2301
2302 /**
2303  *  LCS Module/Kernel initialization function
2304  */
2305 static int
2306 __init lcs_init_module(void)
2307 {
2308         int rc;
2309
2310         PRINT_INFO("Loading %s\n",version);
2311         rc = lcs_register_debug_facility();
2312         LCS_DBF_TEXT(0, setup, "lcsinit");
2313         if (rc) {
2314                 PRINT_ERR("Initialization failed\n");
2315                 return rc;
2316         }
2317
2318         rc = register_cu3088_discipline(&lcs_group_driver);
2319         if (rc) {
2320                 PRINT_ERR("Initialization failed\n");
2321                 return rc;
2322         }
2323         return 0;
2324 }
2325
2326
2327 /**
2328  *  LCS module cleanup function
2329  */
2330 static void
2331 __exit lcs_cleanup_module(void)
2332 {
2333         PRINT_INFO("Terminating lcs module.\n");
2334         LCS_DBF_TEXT(0, trace, "cleanup");
2335         unregister_cu3088_discipline(&lcs_group_driver);
2336         lcs_unregister_debug_facility();
2337 }
2338
2339 module_init(lcs_init_module);
2340 module_exit(lcs_cleanup_module);
2341
2342 MODULE_AUTHOR("Frank Pavlic <fpavlic@de.ibm.com>");
2343 MODULE_LICENSE("GPL");
2344