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