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