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