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