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