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