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