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