Staging: hv: TODO: add some more items
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / s390 / net / lcs.c
bloba70de9b4bf2929938fd5f081673f19a8e0d61d42
1 /*
2 * Linux for S/390 Lan Channel Station Network Driver
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>
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.
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.
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.
26 #define KMSG_COMPONENT "lcs"
27 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
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 <linux/kthread.h>
40 #include <net/arp.h>
41 #include <net/ip.h>
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>
49 #include "lcs.h"
50 #include "cu3088.h"
53 #if !defined(CONFIG_NET_ETHERNET) && \
54 !defined(CONFIG_TR) && !defined(CONFIG_FDDI)
55 #error Cannot compile lcs.c without some net devices switched on.
56 #endif
58 /**
59 * initialization string for output
62 static char version[] __initdata = "LCS driver";
63 static char debug_buffer[255];
65 /**
66 * Some prototypes.
68 static void lcs_tasklet(unsigned long);
69 static void lcs_start_kernel_thread(struct work_struct *);
70 static void lcs_get_frames_cb(struct lcs_channel *, struct lcs_buffer *);
71 #ifdef CONFIG_IP_MULTICAST
72 static int lcs_send_delipm(struct lcs_card *, struct lcs_ipm_list *);
73 #endif /* CONFIG_IP_MULTICAST */
74 static int lcs_recovery(void *ptr);
76 /**
77 * Debug Facility Stuff
79 static debug_info_t *lcs_dbf_setup;
80 static debug_info_t *lcs_dbf_trace;
82 /**
83 * LCS Debug Facility functions
85 static void
86 lcs_unregister_debug_facility(void)
88 if (lcs_dbf_setup)
89 debug_unregister(lcs_dbf_setup);
90 if (lcs_dbf_trace)
91 debug_unregister(lcs_dbf_trace);
94 static int
95 lcs_register_debug_facility(void)
97 lcs_dbf_setup = debug_register("lcs_setup", 2, 1, 8);
98 lcs_dbf_trace = debug_register("lcs_trace", 4, 1, 8);
99 if (lcs_dbf_setup == NULL || lcs_dbf_trace == NULL) {
100 pr_err("Not enough memory for debug facility.\n");
101 lcs_unregister_debug_facility();
102 return -ENOMEM;
104 debug_register_view(lcs_dbf_setup, &debug_hex_ascii_view);
105 debug_set_level(lcs_dbf_setup, 2);
106 debug_register_view(lcs_dbf_trace, &debug_hex_ascii_view);
107 debug_set_level(lcs_dbf_trace, 2);
108 return 0;
112 * Allocate io buffers.
114 static int
115 lcs_alloc_channel(struct lcs_channel *channel)
117 int cnt;
119 LCS_DBF_TEXT(2, setup, "ichalloc");
120 for (cnt = 0; cnt < LCS_NUM_BUFFS; cnt++) {
121 /* alloc memory fo iobuffer */
122 channel->iob[cnt].data =
123 kzalloc(LCS_IOBUFFERSIZE, GFP_DMA | GFP_KERNEL);
124 if (channel->iob[cnt].data == NULL)
125 break;
126 channel->iob[cnt].state = LCS_BUF_STATE_EMPTY;
128 if (cnt < LCS_NUM_BUFFS) {
129 /* Not all io buffers could be allocated. */
130 LCS_DBF_TEXT(2, setup, "echalloc");
131 while (cnt-- > 0)
132 kfree(channel->iob[cnt].data);
133 return -ENOMEM;
135 return 0;
139 * Free io buffers.
141 static void
142 lcs_free_channel(struct lcs_channel *channel)
144 int cnt;
146 LCS_DBF_TEXT(2, setup, "ichfree");
147 for (cnt = 0; cnt < LCS_NUM_BUFFS; cnt++) {
148 kfree(channel->iob[cnt].data);
149 channel->iob[cnt].data = NULL;
154 * Cleanup channel.
156 static void
157 lcs_cleanup_channel(struct lcs_channel *channel)
159 LCS_DBF_TEXT(3, setup, "cleanch");
160 /* Kill write channel tasklets. */
161 tasklet_kill(&channel->irq_tasklet);
162 /* Free channel buffers. */
163 lcs_free_channel(channel);
167 * LCS free memory for card and channels.
169 static void
170 lcs_free_card(struct lcs_card *card)
172 LCS_DBF_TEXT(2, setup, "remcard");
173 LCS_DBF_HEX(2, setup, &card, sizeof(void*));
174 kfree(card);
178 * LCS alloc memory for card and channels
180 static struct lcs_card *
181 lcs_alloc_card(void)
183 struct lcs_card *card;
184 int rc;
186 LCS_DBF_TEXT(2, setup, "alloclcs");
188 card = kzalloc(sizeof(struct lcs_card), GFP_KERNEL | GFP_DMA);
189 if (card == NULL)
190 return NULL;
191 card->lan_type = LCS_FRAME_TYPE_AUTO;
192 card->pkt_seq = 0;
193 card->lancmd_timeout = LCS_LANCMD_TIMEOUT_DEFAULT;
194 /* Allocate io buffers for the read channel. */
195 rc = lcs_alloc_channel(&card->read);
196 if (rc){
197 LCS_DBF_TEXT(2, setup, "iccwerr");
198 lcs_free_card(card);
199 return NULL;
201 /* Allocate io buffers for the write channel. */
202 rc = lcs_alloc_channel(&card->write);
203 if (rc) {
204 LCS_DBF_TEXT(2, setup, "iccwerr");
205 lcs_cleanup_channel(&card->read);
206 lcs_free_card(card);
207 return NULL;
210 #ifdef CONFIG_IP_MULTICAST
211 INIT_LIST_HEAD(&card->ipm_list);
212 #endif
213 LCS_DBF_HEX(2, setup, &card, sizeof(void*));
214 return card;
218 * Setup read channel.
220 static void
221 lcs_setup_read_ccws(struct lcs_card *card)
223 int cnt;
225 LCS_DBF_TEXT(2, setup, "ireadccw");
226 /* Setup read ccws. */
227 memset(card->read.ccws, 0, sizeof (struct ccw1) * (LCS_NUM_BUFFS + 1));
228 for (cnt = 0; cnt < LCS_NUM_BUFFS; cnt++) {
229 card->read.ccws[cnt].cmd_code = LCS_CCW_READ;
230 card->read.ccws[cnt].count = LCS_IOBUFFERSIZE;
231 card->read.ccws[cnt].flags =
232 CCW_FLAG_CC | CCW_FLAG_SLI | CCW_FLAG_PCI;
234 * Note: we have allocated the buffer with GFP_DMA, so
235 * we do not need to do set_normalized_cda.
237 card->read.ccws[cnt].cda =
238 (__u32) __pa(card->read.iob[cnt].data);
239 ((struct lcs_header *)
240 card->read.iob[cnt].data)->offset = LCS_ILLEGAL_OFFSET;
241 card->read.iob[cnt].callback = lcs_get_frames_cb;
242 card->read.iob[cnt].state = LCS_BUF_STATE_READY;
243 card->read.iob[cnt].count = LCS_IOBUFFERSIZE;
245 card->read.ccws[0].flags &= ~CCW_FLAG_PCI;
246 card->read.ccws[LCS_NUM_BUFFS - 1].flags &= ~CCW_FLAG_PCI;
247 card->read.ccws[LCS_NUM_BUFFS - 1].flags |= CCW_FLAG_SUSPEND;
248 /* Last ccw is a tic (transfer in channel). */
249 card->read.ccws[LCS_NUM_BUFFS].cmd_code = LCS_CCW_TRANSFER;
250 card->read.ccws[LCS_NUM_BUFFS].cda =
251 (__u32) __pa(card->read.ccws);
252 /* Setg initial state of the read channel. */
253 card->read.state = LCS_CH_STATE_INIT;
255 card->read.io_idx = 0;
256 card->read.buf_idx = 0;
259 static void
260 lcs_setup_read(struct lcs_card *card)
262 LCS_DBF_TEXT(3, setup, "initread");
264 lcs_setup_read_ccws(card);
265 /* Initialize read channel tasklet. */
266 card->read.irq_tasklet.data = (unsigned long) &card->read;
267 card->read.irq_tasklet.func = lcs_tasklet;
268 /* Initialize waitqueue. */
269 init_waitqueue_head(&card->read.wait_q);
273 * Setup write channel.
275 static void
276 lcs_setup_write_ccws(struct lcs_card *card)
278 int cnt;
280 LCS_DBF_TEXT(3, setup, "iwritccw");
281 /* Setup write ccws. */
282 memset(card->write.ccws, 0, sizeof(struct ccw1) * LCS_NUM_BUFFS + 1);
283 for (cnt = 0; cnt < LCS_NUM_BUFFS; cnt++) {
284 card->write.ccws[cnt].cmd_code = LCS_CCW_WRITE;
285 card->write.ccws[cnt].count = 0;
286 card->write.ccws[cnt].flags =
287 CCW_FLAG_SUSPEND | CCW_FLAG_CC | CCW_FLAG_SLI;
289 * Note: we have allocated the buffer with GFP_DMA, so
290 * we do not need to do set_normalized_cda.
292 card->write.ccws[cnt].cda =
293 (__u32) __pa(card->write.iob[cnt].data);
295 /* Last ccw is a tic (transfer in channel). */
296 card->write.ccws[LCS_NUM_BUFFS].cmd_code = LCS_CCW_TRANSFER;
297 card->write.ccws[LCS_NUM_BUFFS].cda =
298 (__u32) __pa(card->write.ccws);
299 /* Set initial state of the write channel. */
300 card->read.state = LCS_CH_STATE_INIT;
302 card->write.io_idx = 0;
303 card->write.buf_idx = 0;
306 static void
307 lcs_setup_write(struct lcs_card *card)
309 LCS_DBF_TEXT(3, setup, "initwrit");
311 lcs_setup_write_ccws(card);
312 /* Initialize write channel tasklet. */
313 card->write.irq_tasklet.data = (unsigned long) &card->write;
314 card->write.irq_tasklet.func = lcs_tasklet;
315 /* Initialize waitqueue. */
316 init_waitqueue_head(&card->write.wait_q);
319 static void
320 lcs_set_allowed_threads(struct lcs_card *card, unsigned long threads)
322 unsigned long flags;
324 spin_lock_irqsave(&card->mask_lock, flags);
325 card->thread_allowed_mask = threads;
326 spin_unlock_irqrestore(&card->mask_lock, flags);
327 wake_up(&card->wait_q);
329 static inline int
330 lcs_threads_running(struct lcs_card *card, unsigned long threads)
332 unsigned long flags;
333 int rc = 0;
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;
341 static int
342 lcs_wait_for_threads(struct lcs_card *card, unsigned long threads)
344 return wait_event_interruptible(card->wait_q,
345 lcs_threads_running(card, threads) == 0);
348 static inline int
349 lcs_set_thread_start_bit(struct lcs_card *card, unsigned long thread)
351 unsigned long flags;
353 spin_lock_irqsave(&card->mask_lock, flags);
354 if ( !(card->thread_allowed_mask & thread) ||
355 (card->thread_start_mask & thread) ) {
356 spin_unlock_irqrestore(&card->mask_lock, flags);
357 return -EPERM;
359 card->thread_start_mask |= thread;
360 spin_unlock_irqrestore(&card->mask_lock, flags);
361 return 0;
364 static void
365 lcs_clear_thread_running_bit(struct lcs_card *card, unsigned long thread)
367 unsigned long flags;
369 spin_lock_irqsave(&card->mask_lock, flags);
370 card->thread_running_mask &= ~thread;
371 spin_unlock_irqrestore(&card->mask_lock, flags);
372 wake_up(&card->wait_q);
375 static inline int
376 __lcs_do_run_thread(struct lcs_card *card, unsigned long thread)
378 unsigned long flags;
379 int rc = 0;
381 spin_lock_irqsave(&card->mask_lock, flags);
382 if (card->thread_start_mask & thread){
383 if ((card->thread_allowed_mask & thread) &&
384 !(card->thread_running_mask & thread)){
385 rc = 1;
386 card->thread_start_mask &= ~thread;
387 card->thread_running_mask |= thread;
388 } else
389 rc = -EPERM;
391 spin_unlock_irqrestore(&card->mask_lock, flags);
392 return rc;
395 static int
396 lcs_do_run_thread(struct lcs_card *card, unsigned long thread)
398 int rc = 0;
399 wait_event(card->wait_q,
400 (rc = __lcs_do_run_thread(card, thread)) >= 0);
401 return rc;
404 static int
405 lcs_do_start_thread(struct lcs_card *card, unsigned long thread)
407 unsigned long flags;
408 int rc = 0;
410 spin_lock_irqsave(&card->mask_lock, flags);
411 LCS_DBF_TEXT_(4, trace, " %02x%02x%02x",
412 (u8) card->thread_start_mask,
413 (u8) card->thread_allowed_mask,
414 (u8) card->thread_running_mask);
415 rc = (card->thread_start_mask & thread);
416 spin_unlock_irqrestore(&card->mask_lock, flags);
417 return rc;
421 * Initialize channels,card and state machines.
423 static void
424 lcs_setup_card(struct lcs_card *card)
426 LCS_DBF_TEXT(2, setup, "initcard");
427 LCS_DBF_HEX(2, setup, &card, sizeof(void*));
429 lcs_setup_read(card);
430 lcs_setup_write(card);
431 /* Set cards initial state. */
432 card->state = DEV_STATE_DOWN;
433 card->tx_buffer = NULL;
434 card->tx_emitted = 0;
436 init_waitqueue_head(&card->wait_q);
437 spin_lock_init(&card->lock);
438 spin_lock_init(&card->ipm_lock);
439 spin_lock_init(&card->mask_lock);
440 #ifdef CONFIG_IP_MULTICAST
441 INIT_LIST_HEAD(&card->ipm_list);
442 #endif
443 INIT_LIST_HEAD(&card->lancmd_waiters);
446 static inline void
447 lcs_clear_multicast_list(struct lcs_card *card)
449 #ifdef CONFIG_IP_MULTICAST
450 struct lcs_ipm_list *ipm;
451 unsigned long flags;
453 /* Free multicast list. */
454 LCS_DBF_TEXT(3, setup, "clmclist");
455 spin_lock_irqsave(&card->ipm_lock, flags);
456 while (!list_empty(&card->ipm_list)){
457 ipm = list_entry(card->ipm_list.next,
458 struct lcs_ipm_list, list);
459 list_del(&ipm->list);
460 if (ipm->ipm_state != LCS_IPM_STATE_SET_REQUIRED){
461 spin_unlock_irqrestore(&card->ipm_lock, flags);
462 lcs_send_delipm(card, ipm);
463 spin_lock_irqsave(&card->ipm_lock, flags);
465 kfree(ipm);
467 spin_unlock_irqrestore(&card->ipm_lock, flags);
468 #endif
471 * Cleanup channels,card and state machines.
473 static void
474 lcs_cleanup_card(struct lcs_card *card)
477 LCS_DBF_TEXT(3, setup, "cleancrd");
478 LCS_DBF_HEX(2,setup,&card,sizeof(void*));
480 if (card->dev != NULL)
481 free_netdev(card->dev);
482 /* Cleanup channels. */
483 lcs_cleanup_channel(&card->write);
484 lcs_cleanup_channel(&card->read);
488 * Start channel.
490 static int
491 lcs_start_channel(struct lcs_channel *channel)
493 unsigned long flags;
494 int rc;
496 LCS_DBF_TEXT_(4, trace,"ssch%s", dev_name(&channel->ccwdev->dev));
497 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
498 rc = ccw_device_start(channel->ccwdev,
499 channel->ccws + channel->io_idx, 0, 0,
500 DOIO_DENY_PREFETCH | DOIO_ALLOW_SUSPEND);
501 if (rc == 0)
502 channel->state = LCS_CH_STATE_RUNNING;
503 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
504 if (rc) {
505 LCS_DBF_TEXT_(4,trace,"essh%s",
506 dev_name(&channel->ccwdev->dev));
507 dev_err(&channel->ccwdev->dev,
508 "Starting an LCS device resulted in an error,"
509 " rc=%d!\n", rc);
511 return rc;
514 static int
515 lcs_clear_channel(struct lcs_channel *channel)
517 unsigned long flags;
518 int rc;
520 LCS_DBF_TEXT(4,trace,"clearch");
521 LCS_DBF_TEXT_(4, trace, "%s", dev_name(&channel->ccwdev->dev));
522 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
523 rc = ccw_device_clear(channel->ccwdev, (addr_t) channel);
524 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
525 if (rc) {
526 LCS_DBF_TEXT_(4, trace, "ecsc%s",
527 dev_name(&channel->ccwdev->dev));
528 return rc;
530 wait_event(channel->wait_q, (channel->state == LCS_CH_STATE_CLEARED));
531 channel->state = LCS_CH_STATE_STOPPED;
532 return rc;
537 * Stop channel.
539 static int
540 lcs_stop_channel(struct lcs_channel *channel)
542 unsigned long flags;
543 int rc;
545 if (channel->state == LCS_CH_STATE_STOPPED)
546 return 0;
547 LCS_DBF_TEXT(4,trace,"haltsch");
548 LCS_DBF_TEXT_(4, trace, "%s", dev_name(&channel->ccwdev->dev));
549 channel->state = LCS_CH_STATE_INIT;
550 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
551 rc = ccw_device_halt(channel->ccwdev, (addr_t) channel);
552 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
553 if (rc) {
554 LCS_DBF_TEXT_(4, trace, "ehsc%s",
555 dev_name(&channel->ccwdev->dev));
556 return rc;
558 /* Asynchronous halt initialted. Wait for its completion. */
559 wait_event(channel->wait_q, (channel->state == LCS_CH_STATE_HALTED));
560 lcs_clear_channel(channel);
561 return 0;
565 * start read and write channel
567 static int
568 lcs_start_channels(struct lcs_card *card)
570 int rc;
572 LCS_DBF_TEXT(2, trace, "chstart");
573 /* start read channel */
574 rc = lcs_start_channel(&card->read);
575 if (rc)
576 return rc;
577 /* start write channel */
578 rc = lcs_start_channel(&card->write);
579 if (rc)
580 lcs_stop_channel(&card->read);
581 return rc;
585 * stop read and write channel
587 static int
588 lcs_stop_channels(struct lcs_card *card)
590 LCS_DBF_TEXT(2, trace, "chhalt");
591 lcs_stop_channel(&card->read);
592 lcs_stop_channel(&card->write);
593 return 0;
597 * Get empty buffer.
599 static struct lcs_buffer *
600 __lcs_get_buffer(struct lcs_channel *channel)
602 int index;
604 LCS_DBF_TEXT(5, trace, "_getbuff");
605 index = channel->io_idx;
606 do {
607 if (channel->iob[index].state == LCS_BUF_STATE_EMPTY) {
608 channel->iob[index].state = LCS_BUF_STATE_LOCKED;
609 return channel->iob + index;
611 index = (index + 1) & (LCS_NUM_BUFFS - 1);
612 } while (index != channel->io_idx);
613 return NULL;
616 static struct lcs_buffer *
617 lcs_get_buffer(struct lcs_channel *channel)
619 struct lcs_buffer *buffer;
620 unsigned long flags;
622 LCS_DBF_TEXT(5, trace, "getbuff");
623 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
624 buffer = __lcs_get_buffer(channel);
625 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
626 return buffer;
630 * Resume channel program if the channel is suspended.
632 static int
633 __lcs_resume_channel(struct lcs_channel *channel)
635 int rc;
637 if (channel->state != LCS_CH_STATE_SUSPENDED)
638 return 0;
639 if (channel->ccws[channel->io_idx].flags & CCW_FLAG_SUSPEND)
640 return 0;
641 LCS_DBF_TEXT_(5, trace, "rsch%s", dev_name(&channel->ccwdev->dev));
642 rc = ccw_device_resume(channel->ccwdev);
643 if (rc) {
644 LCS_DBF_TEXT_(4, trace, "ersc%s",
645 dev_name(&channel->ccwdev->dev));
646 dev_err(&channel->ccwdev->dev,
647 "Sending data from the LCS device to the LAN failed"
648 " with rc=%d\n",rc);
649 } else
650 channel->state = LCS_CH_STATE_RUNNING;
651 return rc;
656 * Make a buffer ready for processing.
658 static inline void
659 __lcs_ready_buffer_bits(struct lcs_channel *channel, int index)
661 int prev, next;
663 LCS_DBF_TEXT(5, trace, "rdybits");
664 prev = (index - 1) & (LCS_NUM_BUFFS - 1);
665 next = (index + 1) & (LCS_NUM_BUFFS - 1);
666 /* Check if we may clear the suspend bit of this buffer. */
667 if (channel->ccws[next].flags & CCW_FLAG_SUSPEND) {
668 /* Check if we have to set the PCI bit. */
669 if (!(channel->ccws[prev].flags & CCW_FLAG_SUSPEND))
670 /* Suspend bit of the previous buffer is not set. */
671 channel->ccws[index].flags |= CCW_FLAG_PCI;
672 /* Suspend bit of the next buffer is set. */
673 channel->ccws[index].flags &= ~CCW_FLAG_SUSPEND;
677 static int
678 lcs_ready_buffer(struct lcs_channel *channel, struct lcs_buffer *buffer)
680 unsigned long flags;
681 int index, rc;
683 LCS_DBF_TEXT(5, trace, "rdybuff");
684 BUG_ON(buffer->state != LCS_BUF_STATE_LOCKED &&
685 buffer->state != LCS_BUF_STATE_PROCESSED);
686 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
687 buffer->state = LCS_BUF_STATE_READY;
688 index = buffer - channel->iob;
689 /* Set length. */
690 channel->ccws[index].count = buffer->count;
691 /* Check relevant PCI/suspend bits. */
692 __lcs_ready_buffer_bits(channel, index);
693 rc = __lcs_resume_channel(channel);
694 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
695 return rc;
699 * Mark the buffer as processed. Take care of the suspend bit
700 * of the previous buffer. This function is called from
701 * interrupt context, so the lock must not be taken.
703 static int
704 __lcs_processed_buffer(struct lcs_channel *channel, struct lcs_buffer *buffer)
706 int index, prev, next;
708 LCS_DBF_TEXT(5, trace, "prcsbuff");
709 BUG_ON(buffer->state != LCS_BUF_STATE_READY);
710 buffer->state = LCS_BUF_STATE_PROCESSED;
711 index = buffer - channel->iob;
712 prev = (index - 1) & (LCS_NUM_BUFFS - 1);
713 next = (index + 1) & (LCS_NUM_BUFFS - 1);
714 /* Set the suspend bit and clear the PCI bit of this buffer. */
715 channel->ccws[index].flags |= CCW_FLAG_SUSPEND;
716 channel->ccws[index].flags &= ~CCW_FLAG_PCI;
717 /* Check the suspend bit of the previous buffer. */
718 if (channel->iob[prev].state == LCS_BUF_STATE_READY) {
720 * Previous buffer is in state ready. It might have
721 * happened in lcs_ready_buffer that the suspend bit
722 * has not been cleared to avoid an endless loop.
723 * Do it now.
725 __lcs_ready_buffer_bits(channel, prev);
727 /* Clear PCI bit of next buffer. */
728 channel->ccws[next].flags &= ~CCW_FLAG_PCI;
729 return __lcs_resume_channel(channel);
733 * Put a processed buffer back to state empty.
735 static void
736 lcs_release_buffer(struct lcs_channel *channel, struct lcs_buffer *buffer)
738 unsigned long flags;
740 LCS_DBF_TEXT(5, trace, "relbuff");
741 BUG_ON(buffer->state != LCS_BUF_STATE_LOCKED &&
742 buffer->state != LCS_BUF_STATE_PROCESSED);
743 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
744 buffer->state = LCS_BUF_STATE_EMPTY;
745 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
749 * Get buffer for a lan command.
751 static struct lcs_buffer *
752 lcs_get_lancmd(struct lcs_card *card, int count)
754 struct lcs_buffer *buffer;
755 struct lcs_cmd *cmd;
757 LCS_DBF_TEXT(4, trace, "getlncmd");
758 /* Get buffer and wait if none is available. */
759 wait_event(card->write.wait_q,
760 ((buffer = lcs_get_buffer(&card->write)) != NULL));
761 count += sizeof(struct lcs_header);
762 *(__u16 *)(buffer->data + count) = 0;
763 buffer->count = count + sizeof(__u16);
764 buffer->callback = lcs_release_buffer;
765 cmd = (struct lcs_cmd *) buffer->data;
766 cmd->offset = count;
767 cmd->type = LCS_FRAME_TYPE_CONTROL;
768 cmd->slot = 0;
769 return buffer;
773 static void
774 lcs_get_reply(struct lcs_reply *reply)
776 WARN_ON(atomic_read(&reply->refcnt) <= 0);
777 atomic_inc(&reply->refcnt);
780 static void
781 lcs_put_reply(struct lcs_reply *reply)
783 WARN_ON(atomic_read(&reply->refcnt) <= 0);
784 if (atomic_dec_and_test(&reply->refcnt)) {
785 kfree(reply);
790 static struct lcs_reply *
791 lcs_alloc_reply(struct lcs_cmd *cmd)
793 struct lcs_reply *reply;
795 LCS_DBF_TEXT(4, trace, "getreply");
797 reply = kzalloc(sizeof(struct lcs_reply), GFP_ATOMIC);
798 if (!reply)
799 return NULL;
800 atomic_set(&reply->refcnt,1);
801 reply->sequence_no = cmd->sequence_no;
802 reply->received = 0;
803 reply->rc = 0;
804 init_waitqueue_head(&reply->wait_q);
806 return reply;
810 * Notifier function for lancmd replies. Called from read irq.
812 static void
813 lcs_notify_lancmd_waiters(struct lcs_card *card, struct lcs_cmd *cmd)
815 struct list_head *l, *n;
816 struct lcs_reply *reply;
818 LCS_DBF_TEXT(4, trace, "notiwait");
819 spin_lock(&card->lock);
820 list_for_each_safe(l, n, &card->lancmd_waiters) {
821 reply = list_entry(l, struct lcs_reply, list);
822 if (reply->sequence_no == cmd->sequence_no) {
823 lcs_get_reply(reply);
824 list_del_init(&reply->list);
825 if (reply->callback != NULL)
826 reply->callback(card, cmd);
827 reply->received = 1;
828 reply->rc = cmd->return_code;
829 wake_up(&reply->wait_q);
830 lcs_put_reply(reply);
831 break;
834 spin_unlock(&card->lock);
838 * Emit buffer of a lan comand.
840 static void
841 lcs_lancmd_timeout(unsigned long data)
843 struct lcs_reply *reply, *list_reply, *r;
844 unsigned long flags;
846 LCS_DBF_TEXT(4, trace, "timeout");
847 reply = (struct lcs_reply *) data;
848 spin_lock_irqsave(&reply->card->lock, flags);
849 list_for_each_entry_safe(list_reply, r,
850 &reply->card->lancmd_waiters,list) {
851 if (reply == list_reply) {
852 lcs_get_reply(reply);
853 list_del_init(&reply->list);
854 spin_unlock_irqrestore(&reply->card->lock, flags);
855 reply->received = 1;
856 reply->rc = -ETIME;
857 wake_up(&reply->wait_q);
858 lcs_put_reply(reply);
859 return;
862 spin_unlock_irqrestore(&reply->card->lock, flags);
865 static int
866 lcs_send_lancmd(struct lcs_card *card, struct lcs_buffer *buffer,
867 void (*reply_callback)(struct lcs_card *, struct lcs_cmd *))
869 struct lcs_reply *reply;
870 struct lcs_cmd *cmd;
871 struct timer_list timer;
872 unsigned long flags;
873 int rc;
875 LCS_DBF_TEXT(4, trace, "sendcmd");
876 cmd = (struct lcs_cmd *) buffer->data;
877 cmd->return_code = 0;
878 cmd->sequence_no = card->sequence_no++;
879 reply = lcs_alloc_reply(cmd);
880 if (!reply)
881 return -ENOMEM;
882 reply->callback = reply_callback;
883 reply->card = card;
884 spin_lock_irqsave(&card->lock, flags);
885 list_add_tail(&reply->list, &card->lancmd_waiters);
886 spin_unlock_irqrestore(&card->lock, flags);
888 buffer->callback = lcs_release_buffer;
889 rc = lcs_ready_buffer(&card->write, buffer);
890 if (rc)
891 return rc;
892 init_timer(&timer);
893 timer.function = lcs_lancmd_timeout;
894 timer.data = (unsigned long) reply;
895 timer.expires = jiffies + HZ*card->lancmd_timeout;
896 add_timer(&timer);
897 wait_event(reply->wait_q, reply->received);
898 del_timer_sync(&timer);
899 LCS_DBF_TEXT_(4, trace, "rc:%d",reply->rc);
900 rc = reply->rc;
901 lcs_put_reply(reply);
902 return rc ? -EIO : 0;
906 * LCS startup command
908 static int
909 lcs_send_startup(struct lcs_card *card, __u8 initiator)
911 struct lcs_buffer *buffer;
912 struct lcs_cmd *cmd;
914 LCS_DBF_TEXT(2, trace, "startup");
915 buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
916 cmd = (struct lcs_cmd *) buffer->data;
917 cmd->cmd_code = LCS_CMD_STARTUP;
918 cmd->initiator = initiator;
919 cmd->cmd.lcs_startup.buff_size = LCS_IOBUFFERSIZE;
920 return lcs_send_lancmd(card, buffer, NULL);
924 * LCS shutdown command
926 static int
927 lcs_send_shutdown(struct lcs_card *card)
929 struct lcs_buffer *buffer;
930 struct lcs_cmd *cmd;
932 LCS_DBF_TEXT(2, trace, "shutdown");
933 buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
934 cmd = (struct lcs_cmd *) buffer->data;
935 cmd->cmd_code = LCS_CMD_SHUTDOWN;
936 cmd->initiator = LCS_INITIATOR_TCPIP;
937 return lcs_send_lancmd(card, buffer, NULL);
941 * LCS lanstat command
943 static void
944 __lcs_lanstat_cb(struct lcs_card *card, struct lcs_cmd *cmd)
946 LCS_DBF_TEXT(2, trace, "statcb");
947 memcpy(card->mac, cmd->cmd.lcs_lanstat_cmd.mac_addr, LCS_MAC_LENGTH);
950 static int
951 lcs_send_lanstat(struct lcs_card *card)
953 struct lcs_buffer *buffer;
954 struct lcs_cmd *cmd;
956 LCS_DBF_TEXT(2,trace, "cmdstat");
957 buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
958 cmd = (struct lcs_cmd *) buffer->data;
959 /* Setup lanstat command. */
960 cmd->cmd_code = LCS_CMD_LANSTAT;
961 cmd->initiator = LCS_INITIATOR_TCPIP;
962 cmd->cmd.lcs_std_cmd.lan_type = card->lan_type;
963 cmd->cmd.lcs_std_cmd.portno = card->portno;
964 return lcs_send_lancmd(card, buffer, __lcs_lanstat_cb);
968 * send stoplan command
970 static int
971 lcs_send_stoplan(struct lcs_card *card, __u8 initiator)
973 struct lcs_buffer *buffer;
974 struct lcs_cmd *cmd;
976 LCS_DBF_TEXT(2, trace, "cmdstpln");
977 buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
978 cmd = (struct lcs_cmd *) buffer->data;
979 cmd->cmd_code = LCS_CMD_STOPLAN;
980 cmd->initiator = initiator;
981 cmd->cmd.lcs_std_cmd.lan_type = card->lan_type;
982 cmd->cmd.lcs_std_cmd.portno = card->portno;
983 return lcs_send_lancmd(card, buffer, NULL);
987 * send startlan command
989 static void
990 __lcs_send_startlan_cb(struct lcs_card *card, struct lcs_cmd *cmd)
992 LCS_DBF_TEXT(2, trace, "srtlancb");
993 card->lan_type = cmd->cmd.lcs_std_cmd.lan_type;
994 card->portno = cmd->cmd.lcs_std_cmd.portno;
997 static int
998 lcs_send_startlan(struct lcs_card *card, __u8 initiator)
1000 struct lcs_buffer *buffer;
1001 struct lcs_cmd *cmd;
1003 LCS_DBF_TEXT(2, trace, "cmdstaln");
1004 buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
1005 cmd = (struct lcs_cmd *) buffer->data;
1006 cmd->cmd_code = LCS_CMD_STARTLAN;
1007 cmd->initiator = initiator;
1008 cmd->cmd.lcs_std_cmd.lan_type = card->lan_type;
1009 cmd->cmd.lcs_std_cmd.portno = card->portno;
1010 return lcs_send_lancmd(card, buffer, __lcs_send_startlan_cb);
1013 #ifdef CONFIG_IP_MULTICAST
1015 * send setipm command (Multicast)
1017 static int
1018 lcs_send_setipm(struct lcs_card *card,struct lcs_ipm_list *ipm_list)
1020 struct lcs_buffer *buffer;
1021 struct lcs_cmd *cmd;
1023 LCS_DBF_TEXT(2, trace, "cmdsetim");
1024 buffer = lcs_get_lancmd(card, LCS_MULTICAST_CMD_SIZE);
1025 cmd = (struct lcs_cmd *) buffer->data;
1026 cmd->cmd_code = LCS_CMD_SETIPM;
1027 cmd->initiator = LCS_INITIATOR_TCPIP;
1028 cmd->cmd.lcs_qipassist.lan_type = card->lan_type;
1029 cmd->cmd.lcs_qipassist.portno = card->portno;
1030 cmd->cmd.lcs_qipassist.version = 4;
1031 cmd->cmd.lcs_qipassist.num_ip_pairs = 1;
1032 memcpy(cmd->cmd.lcs_qipassist.lcs_ipass_ctlmsg.ip_mac_pair,
1033 &ipm_list->ipm, sizeof (struct lcs_ip_mac_pair));
1034 LCS_DBF_TEXT_(2, trace, "%x",ipm_list->ipm.ip_addr);
1035 return lcs_send_lancmd(card, buffer, NULL);
1039 * send delipm command (Multicast)
1041 static int
1042 lcs_send_delipm(struct lcs_card *card,struct lcs_ipm_list *ipm_list)
1044 struct lcs_buffer *buffer;
1045 struct lcs_cmd *cmd;
1047 LCS_DBF_TEXT(2, trace, "cmddelim");
1048 buffer = lcs_get_lancmd(card, LCS_MULTICAST_CMD_SIZE);
1049 cmd = (struct lcs_cmd *) buffer->data;
1050 cmd->cmd_code = LCS_CMD_DELIPM;
1051 cmd->initiator = LCS_INITIATOR_TCPIP;
1052 cmd->cmd.lcs_qipassist.lan_type = card->lan_type;
1053 cmd->cmd.lcs_qipassist.portno = card->portno;
1054 cmd->cmd.lcs_qipassist.version = 4;
1055 cmd->cmd.lcs_qipassist.num_ip_pairs = 1;
1056 memcpy(cmd->cmd.lcs_qipassist.lcs_ipass_ctlmsg.ip_mac_pair,
1057 &ipm_list->ipm, sizeof (struct lcs_ip_mac_pair));
1058 LCS_DBF_TEXT_(2, trace, "%x",ipm_list->ipm.ip_addr);
1059 return lcs_send_lancmd(card, buffer, NULL);
1063 * check if multicast is supported by LCS
1065 static void
1066 __lcs_check_multicast_cb(struct lcs_card *card, struct lcs_cmd *cmd)
1068 LCS_DBF_TEXT(2, trace, "chkmccb");
1069 card->ip_assists_supported =
1070 cmd->cmd.lcs_qipassist.ip_assists_supported;
1071 card->ip_assists_enabled =
1072 cmd->cmd.lcs_qipassist.ip_assists_enabled;
1075 static int
1076 lcs_check_multicast_support(struct lcs_card *card)
1078 struct lcs_buffer *buffer;
1079 struct lcs_cmd *cmd;
1080 int rc;
1082 LCS_DBF_TEXT(2, trace, "cmdqipa");
1083 /* Send query ipassist. */
1084 buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
1085 cmd = (struct lcs_cmd *) buffer->data;
1086 cmd->cmd_code = LCS_CMD_QIPASSIST;
1087 cmd->initiator = LCS_INITIATOR_TCPIP;
1088 cmd->cmd.lcs_qipassist.lan_type = card->lan_type;
1089 cmd->cmd.lcs_qipassist.portno = card->portno;
1090 cmd->cmd.lcs_qipassist.version = 4;
1091 cmd->cmd.lcs_qipassist.num_ip_pairs = 1;
1092 rc = lcs_send_lancmd(card, buffer, __lcs_check_multicast_cb);
1093 if (rc != 0) {
1094 pr_err("Query IPAssist failed. Assuming unsupported!\n");
1095 return -EOPNOTSUPP;
1097 if (card->ip_assists_supported & LCS_IPASS_MULTICAST_SUPPORT)
1098 return 0;
1099 return -EOPNOTSUPP;
1103 * set or del multicast address on LCS card
1105 static void
1106 lcs_fix_multicast_list(struct lcs_card *card)
1108 struct list_head failed_list;
1109 struct lcs_ipm_list *ipm, *tmp;
1110 unsigned long flags;
1111 int rc;
1113 LCS_DBF_TEXT(4,trace, "fixipm");
1114 INIT_LIST_HEAD(&failed_list);
1115 spin_lock_irqsave(&card->ipm_lock, flags);
1116 list_modified:
1117 list_for_each_entry_safe(ipm, tmp, &card->ipm_list, list){
1118 switch (ipm->ipm_state) {
1119 case LCS_IPM_STATE_SET_REQUIRED:
1120 /* del from ipm_list so noone else can tamper with
1121 * this entry */
1122 list_del_init(&ipm->list);
1123 spin_unlock_irqrestore(&card->ipm_lock, flags);
1124 rc = lcs_send_setipm(card, ipm);
1125 spin_lock_irqsave(&card->ipm_lock, flags);
1126 if (rc) {
1127 pr_info("Adding multicast address failed."
1128 " Table possibly full!\n");
1129 /* store ipm in failed list -> will be added
1130 * to ipm_list again, so a retry will be done
1131 * during the next call of this function */
1132 list_add_tail(&ipm->list, &failed_list);
1133 } else {
1134 ipm->ipm_state = LCS_IPM_STATE_ON_CARD;
1135 /* re-insert into ipm_list */
1136 list_add_tail(&ipm->list, &card->ipm_list);
1138 goto list_modified;
1139 case LCS_IPM_STATE_DEL_REQUIRED:
1140 list_del(&ipm->list);
1141 spin_unlock_irqrestore(&card->ipm_lock, flags);
1142 lcs_send_delipm(card, ipm);
1143 spin_lock_irqsave(&card->ipm_lock, flags);
1144 kfree(ipm);
1145 goto list_modified;
1146 case LCS_IPM_STATE_ON_CARD:
1147 break;
1150 /* re-insert all entries from the failed_list into ipm_list */
1151 list_for_each_entry_safe(ipm, tmp, &failed_list, list)
1152 list_move_tail(&ipm->list, &card->ipm_list);
1154 spin_unlock_irqrestore(&card->ipm_lock, flags);
1158 * get mac address for the relevant Multicast address
1160 static void
1161 lcs_get_mac_for_ipm(__be32 ipm, char *mac, struct net_device *dev)
1163 LCS_DBF_TEXT(4,trace, "getmac");
1164 if (dev->type == ARPHRD_IEEE802_TR)
1165 ip_tr_mc_map(ipm, mac);
1166 else
1167 ip_eth_mc_map(ipm, mac);
1171 * function called by net device to handle multicast address relevant things
1173 static inline void
1174 lcs_remove_mc_addresses(struct lcs_card *card, struct in_device *in4_dev)
1176 struct ip_mc_list *im4;
1177 struct list_head *l;
1178 struct lcs_ipm_list *ipm;
1179 unsigned long flags;
1180 char buf[MAX_ADDR_LEN];
1182 LCS_DBF_TEXT(4, trace, "remmclst");
1183 spin_lock_irqsave(&card->ipm_lock, flags);
1184 list_for_each(l, &card->ipm_list) {
1185 ipm = list_entry(l, struct lcs_ipm_list, list);
1186 for (im4 = in4_dev->mc_list; im4 != NULL; im4 = im4->next) {
1187 lcs_get_mac_for_ipm(im4->multiaddr, buf, card->dev);
1188 if ( (ipm->ipm.ip_addr == im4->multiaddr) &&
1189 (memcmp(buf, &ipm->ipm.mac_addr,
1190 LCS_MAC_LENGTH) == 0) )
1191 break;
1193 if (im4 == NULL)
1194 ipm->ipm_state = LCS_IPM_STATE_DEL_REQUIRED;
1196 spin_unlock_irqrestore(&card->ipm_lock, flags);
1199 static inline struct lcs_ipm_list *
1200 lcs_check_addr_entry(struct lcs_card *card, struct ip_mc_list *im4, char *buf)
1202 struct lcs_ipm_list *tmp, *ipm = NULL;
1203 struct list_head *l;
1204 unsigned long flags;
1206 LCS_DBF_TEXT(4, trace, "chkmcent");
1207 spin_lock_irqsave(&card->ipm_lock, flags);
1208 list_for_each(l, &card->ipm_list) {
1209 tmp = list_entry(l, struct lcs_ipm_list, list);
1210 if ( (tmp->ipm.ip_addr == im4->multiaddr) &&
1211 (memcmp(buf, &tmp->ipm.mac_addr,
1212 LCS_MAC_LENGTH) == 0) ) {
1213 ipm = tmp;
1214 break;
1217 spin_unlock_irqrestore(&card->ipm_lock, flags);
1218 return ipm;
1221 static inline void
1222 lcs_set_mc_addresses(struct lcs_card *card, struct in_device *in4_dev)
1225 struct ip_mc_list *im4;
1226 struct lcs_ipm_list *ipm;
1227 char buf[MAX_ADDR_LEN];
1228 unsigned long flags;
1230 LCS_DBF_TEXT(4, trace, "setmclst");
1231 for (im4 = in4_dev->mc_list; im4; im4 = im4->next) {
1232 lcs_get_mac_for_ipm(im4->multiaddr, buf, card->dev);
1233 ipm = lcs_check_addr_entry(card, im4, buf);
1234 if (ipm != NULL)
1235 continue; /* Address already in list. */
1236 ipm = (struct lcs_ipm_list *)
1237 kzalloc(sizeof(struct lcs_ipm_list), GFP_ATOMIC);
1238 if (ipm == NULL) {
1239 pr_info("Not enough memory to add"
1240 " new multicast entry!\n");
1241 break;
1243 memcpy(&ipm->ipm.mac_addr, buf, LCS_MAC_LENGTH);
1244 ipm->ipm.ip_addr = im4->multiaddr;
1245 ipm->ipm_state = LCS_IPM_STATE_SET_REQUIRED;
1246 spin_lock_irqsave(&card->ipm_lock, flags);
1247 LCS_DBF_HEX(2,trace,&ipm->ipm.ip_addr,4);
1248 list_add(&ipm->list, &card->ipm_list);
1249 spin_unlock_irqrestore(&card->ipm_lock, flags);
1253 static int
1254 lcs_register_mc_addresses(void *data)
1256 struct lcs_card *card;
1257 struct in_device *in4_dev;
1259 card = (struct lcs_card *) data;
1261 if (!lcs_do_run_thread(card, LCS_SET_MC_THREAD))
1262 return 0;
1263 LCS_DBF_TEXT(4, trace, "regmulti");
1265 in4_dev = in_dev_get(card->dev);
1266 if (in4_dev == NULL)
1267 goto out;
1268 read_lock(&in4_dev->mc_list_lock);
1269 lcs_remove_mc_addresses(card,in4_dev);
1270 lcs_set_mc_addresses(card, in4_dev);
1271 read_unlock(&in4_dev->mc_list_lock);
1272 in_dev_put(in4_dev);
1274 netif_carrier_off(card->dev);
1275 netif_tx_disable(card->dev);
1276 wait_event(card->write.wait_q,
1277 (card->write.state != LCS_CH_STATE_RUNNING));
1278 lcs_fix_multicast_list(card);
1279 if (card->state == DEV_STATE_UP) {
1280 netif_carrier_on(card->dev);
1281 netif_wake_queue(card->dev);
1283 out:
1284 lcs_clear_thread_running_bit(card, LCS_SET_MC_THREAD);
1285 return 0;
1287 #endif /* CONFIG_IP_MULTICAST */
1290 * function called by net device to
1291 * handle multicast address relevant things
1293 static void
1294 lcs_set_multicast_list(struct net_device *dev)
1296 #ifdef CONFIG_IP_MULTICAST
1297 struct lcs_card *card;
1299 LCS_DBF_TEXT(4, trace, "setmulti");
1300 card = (struct lcs_card *) dev->ml_priv;
1302 if (!lcs_set_thread_start_bit(card, LCS_SET_MC_THREAD))
1303 schedule_work(&card->kernel_thread_starter);
1304 #endif /* CONFIG_IP_MULTICAST */
1307 static long
1308 lcs_check_irb_error(struct ccw_device *cdev, struct irb *irb)
1310 if (!IS_ERR(irb))
1311 return 0;
1313 switch (PTR_ERR(irb)) {
1314 case -EIO:
1315 dev_warn(&cdev->dev,
1316 "An I/O-error occurred on the LCS device\n");
1317 LCS_DBF_TEXT(2, trace, "ckirberr");
1318 LCS_DBF_TEXT_(2, trace, " rc%d", -EIO);
1319 break;
1320 case -ETIMEDOUT:
1321 dev_warn(&cdev->dev,
1322 "A command timed out on the LCS device\n");
1323 LCS_DBF_TEXT(2, trace, "ckirberr");
1324 LCS_DBF_TEXT_(2, trace, " rc%d", -ETIMEDOUT);
1325 break;
1326 default:
1327 dev_warn(&cdev->dev,
1328 "An error occurred on the LCS device, rc=%ld\n",
1329 PTR_ERR(irb));
1330 LCS_DBF_TEXT(2, trace, "ckirberr");
1331 LCS_DBF_TEXT(2, trace, " rc???");
1333 return PTR_ERR(irb);
1336 static int
1337 lcs_get_problem(struct ccw_device *cdev, struct irb *irb)
1339 int dstat, cstat;
1340 char *sense;
1342 sense = (char *) irb->ecw;
1343 cstat = irb->scsw.cmd.cstat;
1344 dstat = irb->scsw.cmd.dstat;
1346 if (cstat & (SCHN_STAT_CHN_CTRL_CHK | SCHN_STAT_INTF_CTRL_CHK |
1347 SCHN_STAT_CHN_DATA_CHK | SCHN_STAT_CHAIN_CHECK |
1348 SCHN_STAT_PROT_CHECK | SCHN_STAT_PROG_CHECK)) {
1349 LCS_DBF_TEXT(2, trace, "CGENCHK");
1350 return 1;
1352 if (dstat & DEV_STAT_UNIT_CHECK) {
1353 if (sense[LCS_SENSE_BYTE_1] &
1354 LCS_SENSE_RESETTING_EVENT) {
1355 LCS_DBF_TEXT(2, trace, "REVIND");
1356 return 1;
1358 if (sense[LCS_SENSE_BYTE_0] &
1359 LCS_SENSE_CMD_REJECT) {
1360 LCS_DBF_TEXT(2, trace, "CMDREJ");
1361 return 0;
1363 if ((!sense[LCS_SENSE_BYTE_0]) &&
1364 (!sense[LCS_SENSE_BYTE_1]) &&
1365 (!sense[LCS_SENSE_BYTE_2]) &&
1366 (!sense[LCS_SENSE_BYTE_3])) {
1367 LCS_DBF_TEXT(2, trace, "ZEROSEN");
1368 return 0;
1370 LCS_DBF_TEXT(2, trace, "DGENCHK");
1371 return 1;
1373 return 0;
1376 static void
1377 lcs_schedule_recovery(struct lcs_card *card)
1379 LCS_DBF_TEXT(2, trace, "startrec");
1380 if (!lcs_set_thread_start_bit(card, LCS_RECOVERY_THREAD))
1381 schedule_work(&card->kernel_thread_starter);
1385 * IRQ Handler for LCS channels
1387 static void
1388 lcs_irq(struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
1390 struct lcs_card *card;
1391 struct lcs_channel *channel;
1392 int rc, index;
1393 int cstat, dstat;
1395 if (lcs_check_irb_error(cdev, irb))
1396 return;
1398 card = CARD_FROM_DEV(cdev);
1399 if (card->read.ccwdev == cdev)
1400 channel = &card->read;
1401 else
1402 channel = &card->write;
1404 cstat = irb->scsw.cmd.cstat;
1405 dstat = irb->scsw.cmd.dstat;
1406 LCS_DBF_TEXT_(5, trace, "Rint%s", dev_name(&cdev->dev));
1407 LCS_DBF_TEXT_(5, trace, "%4x%4x", irb->scsw.cmd.cstat,
1408 irb->scsw.cmd.dstat);
1409 LCS_DBF_TEXT_(5, trace, "%4x%4x", irb->scsw.cmd.fctl,
1410 irb->scsw.cmd.actl);
1412 /* Check for channel and device errors presented */
1413 rc = lcs_get_problem(cdev, irb);
1414 if (rc || (dstat & DEV_STAT_UNIT_EXCEP)) {
1415 dev_warn(&cdev->dev,
1416 "The LCS device stopped because of an error,"
1417 " dstat=0x%X, cstat=0x%X \n",
1418 dstat, cstat);
1419 if (rc) {
1420 channel->state = LCS_CH_STATE_ERROR;
1423 if (channel->state == LCS_CH_STATE_ERROR) {
1424 lcs_schedule_recovery(card);
1425 wake_up(&card->wait_q);
1426 return;
1428 /* How far in the ccw chain have we processed? */
1429 if ((channel->state != LCS_CH_STATE_INIT) &&
1430 (irb->scsw.cmd.fctl & SCSW_FCTL_START_FUNC) &&
1431 (irb->scsw.cmd.cpa != 0)) {
1432 index = (struct ccw1 *) __va((addr_t) irb->scsw.cmd.cpa)
1433 - channel->ccws;
1434 if ((irb->scsw.cmd.actl & SCSW_ACTL_SUSPENDED) ||
1435 (irb->scsw.cmd.cstat & SCHN_STAT_PCI))
1436 /* Bloody io subsystem tells us lies about cpa... */
1437 index = (index - 1) & (LCS_NUM_BUFFS - 1);
1438 while (channel->io_idx != index) {
1439 __lcs_processed_buffer(channel,
1440 channel->iob + channel->io_idx);
1441 channel->io_idx =
1442 (channel->io_idx + 1) & (LCS_NUM_BUFFS - 1);
1446 if ((irb->scsw.cmd.dstat & DEV_STAT_DEV_END) ||
1447 (irb->scsw.cmd.dstat & DEV_STAT_CHN_END) ||
1448 (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK))
1449 /* Mark channel as stopped. */
1450 channel->state = LCS_CH_STATE_STOPPED;
1451 else if (irb->scsw.cmd.actl & SCSW_ACTL_SUSPENDED)
1452 /* CCW execution stopped on a suspend bit. */
1453 channel->state = LCS_CH_STATE_SUSPENDED;
1454 if (irb->scsw.cmd.fctl & SCSW_FCTL_HALT_FUNC) {
1455 if (irb->scsw.cmd.cc != 0) {
1456 ccw_device_halt(channel->ccwdev, (addr_t) channel);
1457 return;
1459 /* The channel has been stopped by halt_IO. */
1460 channel->state = LCS_CH_STATE_HALTED;
1462 if (irb->scsw.cmd.fctl & SCSW_FCTL_CLEAR_FUNC)
1463 channel->state = LCS_CH_STATE_CLEARED;
1464 /* Do the rest in the tasklet. */
1465 tasklet_schedule(&channel->irq_tasklet);
1469 * Tasklet for IRQ handler
1471 static void
1472 lcs_tasklet(unsigned long data)
1474 unsigned long flags;
1475 struct lcs_channel *channel;
1476 struct lcs_buffer *iob;
1477 int buf_idx;
1478 int rc;
1480 channel = (struct lcs_channel *) data;
1481 LCS_DBF_TEXT_(5, trace, "tlet%s", dev_name(&channel->ccwdev->dev));
1483 /* Check for processed buffers. */
1484 iob = channel->iob;
1485 buf_idx = channel->buf_idx;
1486 while (iob[buf_idx].state == LCS_BUF_STATE_PROCESSED) {
1487 /* Do the callback thing. */
1488 if (iob[buf_idx].callback != NULL)
1489 iob[buf_idx].callback(channel, iob + buf_idx);
1490 buf_idx = (buf_idx + 1) & (LCS_NUM_BUFFS - 1);
1492 channel->buf_idx = buf_idx;
1494 if (channel->state == LCS_CH_STATE_STOPPED)
1495 // FIXME: what if rc != 0 ??
1496 rc = lcs_start_channel(channel);
1497 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1498 if (channel->state == LCS_CH_STATE_SUSPENDED &&
1499 channel->iob[channel->io_idx].state == LCS_BUF_STATE_READY) {
1500 // FIXME: what if rc != 0 ??
1501 rc = __lcs_resume_channel(channel);
1503 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1505 /* Something happened on the channel. Wake up waiters. */
1506 wake_up(&channel->wait_q);
1510 * Finish current tx buffer and make it ready for transmit.
1512 static void
1513 __lcs_emit_txbuffer(struct lcs_card *card)
1515 LCS_DBF_TEXT(5, trace, "emittx");
1516 *(__u16 *)(card->tx_buffer->data + card->tx_buffer->count) = 0;
1517 card->tx_buffer->count += 2;
1518 lcs_ready_buffer(&card->write, card->tx_buffer);
1519 card->tx_buffer = NULL;
1520 card->tx_emitted++;
1524 * Callback for finished tx buffers.
1526 static void
1527 lcs_txbuffer_cb(struct lcs_channel *channel, struct lcs_buffer *buffer)
1529 struct lcs_card *card;
1531 LCS_DBF_TEXT(5, trace, "txbuffcb");
1532 /* Put buffer back to pool. */
1533 lcs_release_buffer(channel, buffer);
1534 card = container_of(channel, struct lcs_card, write);
1535 if (netif_queue_stopped(card->dev) && netif_carrier_ok(card->dev))
1536 netif_wake_queue(card->dev);
1537 spin_lock(&card->lock);
1538 card->tx_emitted--;
1539 if (card->tx_emitted <= 0 && card->tx_buffer != NULL)
1541 * Last running tx buffer has finished. Submit partially
1542 * filled current buffer.
1544 __lcs_emit_txbuffer(card);
1545 spin_unlock(&card->lock);
1549 * Packet transmit function called by network stack
1551 static int
1552 __lcs_start_xmit(struct lcs_card *card, struct sk_buff *skb,
1553 struct net_device *dev)
1555 struct lcs_header *header;
1556 int rc = NETDEV_TX_OK;
1558 LCS_DBF_TEXT(5, trace, "hardxmit");
1559 if (skb == NULL) {
1560 card->stats.tx_dropped++;
1561 card->stats.tx_errors++;
1562 return NETDEV_TX_OK;
1564 if (card->state != DEV_STATE_UP) {
1565 dev_kfree_skb(skb);
1566 card->stats.tx_dropped++;
1567 card->stats.tx_errors++;
1568 card->stats.tx_carrier_errors++;
1569 return NETDEV_TX_OK;
1571 if (skb->protocol == htons(ETH_P_IPV6)) {
1572 dev_kfree_skb(skb);
1573 return NETDEV_TX_OK;
1575 netif_stop_queue(card->dev);
1576 spin_lock(&card->lock);
1577 if (card->tx_buffer != NULL &&
1578 card->tx_buffer->count + sizeof(struct lcs_header) +
1579 skb->len + sizeof(u16) > LCS_IOBUFFERSIZE)
1580 /* skb too big for current tx buffer. */
1581 __lcs_emit_txbuffer(card);
1582 if (card->tx_buffer == NULL) {
1583 /* Get new tx buffer */
1584 card->tx_buffer = lcs_get_buffer(&card->write);
1585 if (card->tx_buffer == NULL) {
1586 card->stats.tx_dropped++;
1587 rc = NETDEV_TX_BUSY;
1588 goto out;
1590 card->tx_buffer->callback = lcs_txbuffer_cb;
1591 card->tx_buffer->count = 0;
1593 header = (struct lcs_header *)
1594 (card->tx_buffer->data + card->tx_buffer->count);
1595 card->tx_buffer->count += skb->len + sizeof(struct lcs_header);
1596 header->offset = card->tx_buffer->count;
1597 header->type = card->lan_type;
1598 header->slot = card->portno;
1599 skb_copy_from_linear_data(skb, header + 1, skb->len);
1600 spin_unlock(&card->lock);
1601 card->stats.tx_bytes += skb->len;
1602 card->stats.tx_packets++;
1603 dev_kfree_skb(skb);
1604 netif_wake_queue(card->dev);
1605 spin_lock(&card->lock);
1606 if (card->tx_emitted <= 0 && card->tx_buffer != NULL)
1607 /* If this is the first tx buffer emit it immediately. */
1608 __lcs_emit_txbuffer(card);
1609 out:
1610 spin_unlock(&card->lock);
1611 return rc;
1614 static int
1615 lcs_start_xmit(struct sk_buff *skb, struct net_device *dev)
1617 struct lcs_card *card;
1618 int rc;
1620 LCS_DBF_TEXT(5, trace, "pktxmit");
1621 card = (struct lcs_card *) dev->ml_priv;
1622 rc = __lcs_start_xmit(card, skb, dev);
1623 return rc;
1627 * send startlan and lanstat command to make LCS device ready
1629 static int
1630 lcs_startlan_auto(struct lcs_card *card)
1632 int rc;
1634 LCS_DBF_TEXT(2, trace, "strtauto");
1635 #ifdef CONFIG_NET_ETHERNET
1636 card->lan_type = LCS_FRAME_TYPE_ENET;
1637 rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
1638 if (rc == 0)
1639 return 0;
1641 #endif
1642 #ifdef CONFIG_TR
1643 card->lan_type = LCS_FRAME_TYPE_TR;
1644 rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
1645 if (rc == 0)
1646 return 0;
1647 #endif
1648 #ifdef CONFIG_FDDI
1649 card->lan_type = LCS_FRAME_TYPE_FDDI;
1650 rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
1651 if (rc == 0)
1652 return 0;
1653 #endif
1654 return -EIO;
1657 static int
1658 lcs_startlan(struct lcs_card *card)
1660 int rc, i;
1662 LCS_DBF_TEXT(2, trace, "startlan");
1663 rc = 0;
1664 if (card->portno != LCS_INVALID_PORT_NO) {
1665 if (card->lan_type == LCS_FRAME_TYPE_AUTO)
1666 rc = lcs_startlan_auto(card);
1667 else
1668 rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
1669 } else {
1670 for (i = 0; i <= 16; i++) {
1671 card->portno = i;
1672 if (card->lan_type != LCS_FRAME_TYPE_AUTO)
1673 rc = lcs_send_startlan(card,
1674 LCS_INITIATOR_TCPIP);
1675 else
1676 /* autodetecting lan type */
1677 rc = lcs_startlan_auto(card);
1678 if (rc == 0)
1679 break;
1682 if (rc == 0)
1683 return lcs_send_lanstat(card);
1684 return rc;
1688 * LCS detect function
1689 * setup channels and make them I/O ready
1691 static int
1692 lcs_detect(struct lcs_card *card)
1694 int rc = 0;
1696 LCS_DBF_TEXT(2, setup, "lcsdetct");
1697 /* start/reset card */
1698 if (card->dev)
1699 netif_stop_queue(card->dev);
1700 rc = lcs_stop_channels(card);
1701 if (rc == 0) {
1702 rc = lcs_start_channels(card);
1703 if (rc == 0) {
1704 rc = lcs_send_startup(card, LCS_INITIATOR_TCPIP);
1705 if (rc == 0)
1706 rc = lcs_startlan(card);
1709 if (rc == 0) {
1710 card->state = DEV_STATE_UP;
1711 } else {
1712 card->state = DEV_STATE_DOWN;
1713 card->write.state = LCS_CH_STATE_INIT;
1714 card->read.state = LCS_CH_STATE_INIT;
1716 return rc;
1720 * LCS Stop card
1722 static int
1723 lcs_stopcard(struct lcs_card *card)
1725 int rc;
1727 LCS_DBF_TEXT(3, setup, "stopcard");
1729 if (card->read.state != LCS_CH_STATE_STOPPED &&
1730 card->write.state != LCS_CH_STATE_STOPPED &&
1731 card->read.state != LCS_CH_STATE_ERROR &&
1732 card->write.state != LCS_CH_STATE_ERROR &&
1733 card->state == DEV_STATE_UP) {
1734 lcs_clear_multicast_list(card);
1735 rc = lcs_send_stoplan(card,LCS_INITIATOR_TCPIP);
1736 rc = lcs_send_shutdown(card);
1738 rc = lcs_stop_channels(card);
1739 card->state = DEV_STATE_DOWN;
1741 return rc;
1745 * Kernel Thread helper functions for LGW initiated commands
1747 static void
1748 lcs_start_kernel_thread(struct work_struct *work)
1750 struct lcs_card *card = container_of(work, struct lcs_card, kernel_thread_starter);
1751 LCS_DBF_TEXT(5, trace, "krnthrd");
1752 if (lcs_do_start_thread(card, LCS_RECOVERY_THREAD))
1753 kthread_run(lcs_recovery, card, "lcs_recover");
1754 #ifdef CONFIG_IP_MULTICAST
1755 if (lcs_do_start_thread(card, LCS_SET_MC_THREAD))
1756 kthread_run(lcs_register_mc_addresses, card, "regipm");
1757 #endif
1761 * Process control frames.
1763 static void
1764 lcs_get_control(struct lcs_card *card, struct lcs_cmd *cmd)
1766 LCS_DBF_TEXT(5, trace, "getctrl");
1767 if (cmd->initiator == LCS_INITIATOR_LGW) {
1768 switch(cmd->cmd_code) {
1769 case LCS_CMD_STARTUP:
1770 case LCS_CMD_STARTLAN:
1771 lcs_schedule_recovery(card);
1772 break;
1773 case LCS_CMD_STOPLAN:
1774 pr_warning("Stoplan for %s initiated by LGW.\n",
1775 card->dev->name);
1776 if (card->dev)
1777 netif_carrier_off(card->dev);
1778 break;
1779 default:
1780 LCS_DBF_TEXT(5, trace, "noLGWcmd");
1781 break;
1783 } else
1784 lcs_notify_lancmd_waiters(card, cmd);
1788 * Unpack network packet.
1790 static void
1791 lcs_get_skb(struct lcs_card *card, char *skb_data, unsigned int skb_len)
1793 struct sk_buff *skb;
1795 LCS_DBF_TEXT(5, trace, "getskb");
1796 if (card->dev == NULL ||
1797 card->state != DEV_STATE_UP)
1798 /* The card isn't up. Ignore the packet. */
1799 return;
1801 skb = dev_alloc_skb(skb_len);
1802 if (skb == NULL) {
1803 dev_err(&card->dev->dev,
1804 " Allocating a socket buffer to interface %s failed\n",
1805 card->dev->name);
1806 card->stats.rx_dropped++;
1807 return;
1809 memcpy(skb_put(skb, skb_len), skb_data, skb_len);
1810 skb->protocol = card->lan_type_trans(skb, card->dev);
1811 card->stats.rx_bytes += skb_len;
1812 card->stats.rx_packets++;
1813 if (skb->protocol == htons(ETH_P_802_2))
1814 *((__u32 *)skb->cb) = ++card->pkt_seq;
1815 netif_rx(skb);
1819 * LCS main routine to get packets and lancmd replies from the buffers
1821 static void
1822 lcs_get_frames_cb(struct lcs_channel *channel, struct lcs_buffer *buffer)
1824 struct lcs_card *card;
1825 struct lcs_header *lcs_hdr;
1826 __u16 offset;
1828 LCS_DBF_TEXT(5, trace, "lcsgtpkt");
1829 lcs_hdr = (struct lcs_header *) buffer->data;
1830 if (lcs_hdr->offset == LCS_ILLEGAL_OFFSET) {
1831 LCS_DBF_TEXT(4, trace, "-eiogpkt");
1832 return;
1834 card = container_of(channel, struct lcs_card, read);
1835 offset = 0;
1836 while (lcs_hdr->offset != 0) {
1837 if (lcs_hdr->offset <= 0 ||
1838 lcs_hdr->offset > LCS_IOBUFFERSIZE ||
1839 lcs_hdr->offset < offset) {
1840 /* Offset invalid. */
1841 card->stats.rx_length_errors++;
1842 card->stats.rx_errors++;
1843 return;
1845 /* What kind of frame is it? */
1846 if (lcs_hdr->type == LCS_FRAME_TYPE_CONTROL)
1847 /* Control frame. */
1848 lcs_get_control(card, (struct lcs_cmd *) lcs_hdr);
1849 else if (lcs_hdr->type == LCS_FRAME_TYPE_ENET ||
1850 lcs_hdr->type == LCS_FRAME_TYPE_TR ||
1851 lcs_hdr->type == LCS_FRAME_TYPE_FDDI)
1852 /* Normal network packet. */
1853 lcs_get_skb(card, (char *)(lcs_hdr + 1),
1854 lcs_hdr->offset - offset -
1855 sizeof(struct lcs_header));
1856 else
1857 /* Unknown frame type. */
1858 ; // FIXME: error message ?
1859 /* Proceed to next frame. */
1860 offset = lcs_hdr->offset;
1861 lcs_hdr->offset = LCS_ILLEGAL_OFFSET;
1862 lcs_hdr = (struct lcs_header *) (buffer->data + offset);
1864 /* The buffer is now empty. Make it ready again. */
1865 lcs_ready_buffer(&card->read, buffer);
1869 * get network statistics for ifconfig and other user programs
1871 static struct net_device_stats *
1872 lcs_getstats(struct net_device *dev)
1874 struct lcs_card *card;
1876 LCS_DBF_TEXT(4, trace, "netstats");
1877 card = (struct lcs_card *) dev->ml_priv;
1878 return &card->stats;
1882 * stop lcs device
1883 * This function will be called by user doing ifconfig xxx down
1885 static int
1886 lcs_stop_device(struct net_device *dev)
1888 struct lcs_card *card;
1889 int rc;
1891 LCS_DBF_TEXT(2, trace, "stopdev");
1892 card = (struct lcs_card *) dev->ml_priv;
1893 netif_carrier_off(dev);
1894 netif_tx_disable(dev);
1895 dev->flags &= ~IFF_UP;
1896 wait_event(card->write.wait_q,
1897 (card->write.state != LCS_CH_STATE_RUNNING));
1898 rc = lcs_stopcard(card);
1899 if (rc)
1900 dev_err(&card->dev->dev,
1901 " Shutting down the LCS device failed\n ");
1902 return rc;
1906 * start lcs device and make it runnable
1907 * This function will be called by user doing ifconfig xxx up
1909 static int
1910 lcs_open_device(struct net_device *dev)
1912 struct lcs_card *card;
1913 int rc;
1915 LCS_DBF_TEXT(2, trace, "opendev");
1916 card = (struct lcs_card *) dev->ml_priv;
1917 /* initialize statistics */
1918 rc = lcs_detect(card);
1919 if (rc) {
1920 pr_err("Error in opening device!\n");
1922 } else {
1923 dev->flags |= IFF_UP;
1924 netif_carrier_on(dev);
1925 netif_wake_queue(dev);
1926 card->state = DEV_STATE_UP;
1928 return rc;
1932 * show function for portno called by cat or similar things
1934 static ssize_t
1935 lcs_portno_show (struct device *dev, struct device_attribute *attr, char *buf)
1937 struct lcs_card *card;
1939 card = dev_get_drvdata(dev);
1941 if (!card)
1942 return 0;
1944 return sprintf(buf, "%d\n", card->portno);
1948 * store the value which is piped to file portno
1950 static ssize_t
1951 lcs_portno_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1953 struct lcs_card *card;
1954 int value;
1956 card = dev_get_drvdata(dev);
1958 if (!card)
1959 return 0;
1961 sscanf(buf, "%u", &value);
1962 /* TODO: sanity checks */
1963 card->portno = value;
1965 return count;
1969 static DEVICE_ATTR(portno, 0644, lcs_portno_show, lcs_portno_store);
1971 static ssize_t
1972 lcs_type_show(struct device *dev, struct device_attribute *attr, char *buf)
1974 struct ccwgroup_device *cgdev;
1976 cgdev = to_ccwgroupdev(dev);
1977 if (!cgdev)
1978 return -ENODEV;
1980 return sprintf(buf, "%s\n", cu3088_type[cgdev->cdev[0]->id.driver_info]);
1983 static DEVICE_ATTR(type, 0444, lcs_type_show, NULL);
1985 static ssize_t
1986 lcs_timeout_show(struct device *dev, struct device_attribute *attr, char *buf)
1988 struct lcs_card *card;
1990 card = dev_get_drvdata(dev);
1992 return card ? sprintf(buf, "%u\n", card->lancmd_timeout) : 0;
1995 static ssize_t
1996 lcs_timeout_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1998 struct lcs_card *card;
1999 int value;
2001 card = dev_get_drvdata(dev);
2003 if (!card)
2004 return 0;
2006 sscanf(buf, "%u", &value);
2007 /* TODO: sanity checks */
2008 card->lancmd_timeout = value;
2010 return count;
2014 static DEVICE_ATTR(lancmd_timeout, 0644, lcs_timeout_show, lcs_timeout_store);
2016 static ssize_t
2017 lcs_dev_recover_store(struct device *dev, struct device_attribute *attr,
2018 const char *buf, size_t count)
2020 struct lcs_card *card = dev_get_drvdata(dev);
2021 char *tmp;
2022 int i;
2024 if (!card)
2025 return -EINVAL;
2026 if (card->state != DEV_STATE_UP)
2027 return -EPERM;
2028 i = simple_strtoul(buf, &tmp, 16);
2029 if (i == 1)
2030 lcs_schedule_recovery(card);
2031 return count;
2034 static DEVICE_ATTR(recover, 0200, NULL, lcs_dev_recover_store);
2036 static struct attribute * lcs_attrs[] = {
2037 &dev_attr_portno.attr,
2038 &dev_attr_type.attr,
2039 &dev_attr_lancmd_timeout.attr,
2040 &dev_attr_recover.attr,
2041 NULL,
2044 static struct attribute_group lcs_attr_group = {
2045 .attrs = lcs_attrs,
2049 * lcs_probe_device is called on establishing a new ccwgroup_device.
2051 static int
2052 lcs_probe_device(struct ccwgroup_device *ccwgdev)
2054 struct lcs_card *card;
2055 int ret;
2057 if (!get_device(&ccwgdev->dev))
2058 return -ENODEV;
2060 LCS_DBF_TEXT(2, setup, "add_dev");
2061 card = lcs_alloc_card();
2062 if (!card) {
2063 LCS_DBF_TEXT_(2, setup, " rc%d", -ENOMEM);
2064 put_device(&ccwgdev->dev);
2065 return -ENOMEM;
2067 ret = sysfs_create_group(&ccwgdev->dev.kobj, &lcs_attr_group);
2068 if (ret) {
2069 lcs_free_card(card);
2070 put_device(&ccwgdev->dev);
2071 return ret;
2073 dev_set_drvdata(&ccwgdev->dev, card);
2074 ccwgdev->cdev[0]->handler = lcs_irq;
2075 ccwgdev->cdev[1]->handler = lcs_irq;
2076 card->gdev = ccwgdev;
2077 INIT_WORK(&card->kernel_thread_starter, lcs_start_kernel_thread);
2078 card->thread_start_mask = 0;
2079 card->thread_allowed_mask = 0;
2080 card->thread_running_mask = 0;
2081 return 0;
2084 static int
2085 lcs_register_netdev(struct ccwgroup_device *ccwgdev)
2087 struct lcs_card *card;
2089 LCS_DBF_TEXT(2, setup, "regnetdv");
2090 card = dev_get_drvdata(&ccwgdev->dev);
2091 if (card->dev->reg_state != NETREG_UNINITIALIZED)
2092 return 0;
2093 SET_NETDEV_DEV(card->dev, &ccwgdev->dev);
2094 return register_netdev(card->dev);
2098 * lcs_new_device will be called by setting the group device online.
2100 static const struct net_device_ops lcs_netdev_ops = {
2101 .ndo_open = lcs_open_device,
2102 .ndo_stop = lcs_stop_device,
2103 .ndo_get_stats = lcs_getstats,
2104 .ndo_start_xmit = lcs_start_xmit,
2107 static const struct net_device_ops lcs_mc_netdev_ops = {
2108 .ndo_open = lcs_open_device,
2109 .ndo_stop = lcs_stop_device,
2110 .ndo_get_stats = lcs_getstats,
2111 .ndo_start_xmit = lcs_start_xmit,
2112 .ndo_set_multicast_list = lcs_set_multicast_list,
2115 static int
2116 lcs_new_device(struct ccwgroup_device *ccwgdev)
2118 struct lcs_card *card;
2119 struct net_device *dev=NULL;
2120 enum lcs_dev_states recover_state;
2121 int rc;
2123 card = dev_get_drvdata(&ccwgdev->dev);
2124 if (!card)
2125 return -ENODEV;
2127 LCS_DBF_TEXT(2, setup, "newdev");
2128 LCS_DBF_HEX(3, setup, &card, sizeof(void*));
2129 card->read.ccwdev = ccwgdev->cdev[0];
2130 card->write.ccwdev = ccwgdev->cdev[1];
2132 recover_state = card->state;
2133 ccw_device_set_online(card->read.ccwdev);
2134 ccw_device_set_online(card->write.ccwdev);
2136 LCS_DBF_TEXT(3, setup, "lcsnewdv");
2138 lcs_setup_card(card);
2139 rc = lcs_detect(card);
2140 if (rc) {
2141 LCS_DBF_TEXT(2, setup, "dtctfail");
2142 dev_err(&card->dev->dev,
2143 "Detecting a network adapter for LCS devices"
2144 " failed with rc=%d (0x%x)\n", rc, rc);
2145 lcs_stopcard(card);
2146 goto out;
2148 if (card->dev) {
2149 LCS_DBF_TEXT(2, setup, "samedev");
2150 LCS_DBF_HEX(3, setup, &card, sizeof(void*));
2151 goto netdev_out;
2153 switch (card->lan_type) {
2154 #ifdef CONFIG_NET_ETHERNET
2155 case LCS_FRAME_TYPE_ENET:
2156 card->lan_type_trans = eth_type_trans;
2157 dev = alloc_etherdev(0);
2158 break;
2159 #endif
2160 #ifdef CONFIG_TR
2161 case LCS_FRAME_TYPE_TR:
2162 card->lan_type_trans = tr_type_trans;
2163 dev = alloc_trdev(0);
2164 break;
2165 #endif
2166 #ifdef CONFIG_FDDI
2167 case LCS_FRAME_TYPE_FDDI:
2168 card->lan_type_trans = fddi_type_trans;
2169 dev = alloc_fddidev(0);
2170 break;
2171 #endif
2172 default:
2173 LCS_DBF_TEXT(3, setup, "errinit");
2174 pr_err(" Initialization failed\n");
2175 goto out;
2177 if (!dev)
2178 goto out;
2179 card->dev = dev;
2180 card->dev->ml_priv = card;
2181 card->dev->netdev_ops = &lcs_netdev_ops;
2182 memcpy(card->dev->dev_addr, card->mac, LCS_MAC_LENGTH);
2183 #ifdef CONFIG_IP_MULTICAST
2184 if (!lcs_check_multicast_support(card))
2185 card->dev->netdev_ops = &lcs_mc_netdev_ops;
2186 #endif
2187 netdev_out:
2188 lcs_set_allowed_threads(card,0xffffffff);
2189 if (recover_state == DEV_STATE_RECOVER) {
2190 lcs_set_multicast_list(card->dev);
2191 card->dev->flags |= IFF_UP;
2192 netif_carrier_on(card->dev);
2193 netif_wake_queue(card->dev);
2194 card->state = DEV_STATE_UP;
2195 } else {
2196 lcs_stopcard(card);
2199 if (lcs_register_netdev(ccwgdev) != 0)
2200 goto out;
2202 /* Print out supported assists: IPv6 */
2203 pr_info("LCS device %s %s IPv6 support\n", card->dev->name,
2204 (card->ip_assists_supported & LCS_IPASS_IPV6_SUPPORT) ?
2205 "with" : "without");
2206 /* Print out supported assist: Multicast */
2207 pr_info("LCS device %s %s Multicast support\n", card->dev->name,
2208 (card->ip_assists_supported & LCS_IPASS_MULTICAST_SUPPORT) ?
2209 "with" : "without");
2210 return 0;
2211 out:
2213 ccw_device_set_offline(card->read.ccwdev);
2214 ccw_device_set_offline(card->write.ccwdev);
2215 return -ENODEV;
2219 * lcs_shutdown_device, called when setting the group device offline.
2221 static int
2222 __lcs_shutdown_device(struct ccwgroup_device *ccwgdev, int recovery_mode)
2224 struct lcs_card *card;
2225 enum lcs_dev_states recover_state;
2226 int ret;
2228 LCS_DBF_TEXT(3, setup, "shtdndev");
2229 card = dev_get_drvdata(&ccwgdev->dev);
2230 if (!card)
2231 return -ENODEV;
2232 if (recovery_mode == 0) {
2233 lcs_set_allowed_threads(card, 0);
2234 if (lcs_wait_for_threads(card, LCS_SET_MC_THREAD))
2235 return -ERESTARTSYS;
2237 LCS_DBF_HEX(3, setup, &card, sizeof(void*));
2238 recover_state = card->state;
2240 ret = lcs_stop_device(card->dev);
2241 ret = ccw_device_set_offline(card->read.ccwdev);
2242 ret = ccw_device_set_offline(card->write.ccwdev);
2243 if (recover_state == DEV_STATE_UP) {
2244 card->state = DEV_STATE_RECOVER;
2246 if (ret)
2247 return ret;
2248 return 0;
2251 static int
2252 lcs_shutdown_device(struct ccwgroup_device *ccwgdev)
2254 return __lcs_shutdown_device(ccwgdev, 0);
2258 * drive lcs recovery after startup and startlan initiated by Lan Gateway
2260 static int
2261 lcs_recovery(void *ptr)
2263 struct lcs_card *card;
2264 struct ccwgroup_device *gdev;
2265 int rc;
2267 card = (struct lcs_card *) ptr;
2269 LCS_DBF_TEXT(4, trace, "recover1");
2270 if (!lcs_do_run_thread(card, LCS_RECOVERY_THREAD))
2271 return 0;
2272 LCS_DBF_TEXT(4, trace, "recover2");
2273 gdev = card->gdev;
2274 dev_warn(&gdev->dev,
2275 "A recovery process has been started for the LCS device\n");
2276 rc = __lcs_shutdown_device(gdev, 1);
2277 rc = lcs_new_device(gdev);
2278 if (!rc)
2279 pr_info("Device %s successfully recovered!\n",
2280 card->dev->name);
2281 else
2282 pr_info("Device %s could not be recovered!\n",
2283 card->dev->name);
2284 lcs_clear_thread_running_bit(card, LCS_RECOVERY_THREAD);
2285 return 0;
2289 * lcs_remove_device, free buffers and card
2291 static void
2292 lcs_remove_device(struct ccwgroup_device *ccwgdev)
2294 struct lcs_card *card;
2296 card = dev_get_drvdata(&ccwgdev->dev);
2297 if (!card)
2298 return;
2300 LCS_DBF_TEXT(3, setup, "remdev");
2301 LCS_DBF_HEX(3, setup, &card, sizeof(void*));
2302 if (ccwgdev->state == CCWGROUP_ONLINE) {
2303 lcs_shutdown_device(ccwgdev);
2305 if (card->dev)
2306 unregister_netdev(card->dev);
2307 sysfs_remove_group(&ccwgdev->dev.kobj, &lcs_attr_group);
2308 lcs_cleanup_card(card);
2309 lcs_free_card(card);
2310 put_device(&ccwgdev->dev);
2313 static int lcs_pm_suspend(struct lcs_card *card)
2315 if (card->dev)
2316 netif_device_detach(card->dev);
2317 lcs_set_allowed_threads(card, 0);
2318 lcs_wait_for_threads(card, 0xffffffff);
2319 if (card->state != DEV_STATE_DOWN)
2320 __lcs_shutdown_device(card->gdev, 1);
2321 return 0;
2324 static int lcs_pm_resume(struct lcs_card *card)
2326 int rc = 0;
2328 if (card->state == DEV_STATE_RECOVER)
2329 rc = lcs_new_device(card->gdev);
2330 if (card->dev)
2331 netif_device_attach(card->dev);
2332 if (rc) {
2333 dev_warn(&card->gdev->dev, "The lcs device driver "
2334 "failed to recover the device\n");
2336 return rc;
2339 static int lcs_prepare(struct ccwgroup_device *gdev)
2341 return 0;
2344 static void lcs_complete(struct ccwgroup_device *gdev)
2346 return;
2349 static int lcs_freeze(struct ccwgroup_device *gdev)
2351 struct lcs_card *card = dev_get_drvdata(&gdev->dev);
2352 return lcs_pm_suspend(card);
2355 static int lcs_thaw(struct ccwgroup_device *gdev)
2357 struct lcs_card *card = dev_get_drvdata(&gdev->dev);
2358 return lcs_pm_resume(card);
2361 static int lcs_restore(struct ccwgroup_device *gdev)
2363 struct lcs_card *card = dev_get_drvdata(&gdev->dev);
2364 return lcs_pm_resume(card);
2368 * LCS ccwgroup driver registration
2370 static struct ccwgroup_driver lcs_group_driver = {
2371 .owner = THIS_MODULE,
2372 .name = "lcs",
2373 .max_slaves = 2,
2374 .driver_id = 0xD3C3E2,
2375 .probe = lcs_probe_device,
2376 .remove = lcs_remove_device,
2377 .set_online = lcs_new_device,
2378 .set_offline = lcs_shutdown_device,
2379 .prepare = lcs_prepare,
2380 .complete = lcs_complete,
2381 .freeze = lcs_freeze,
2382 .thaw = lcs_thaw,
2383 .restore = lcs_restore,
2387 * LCS Module/Kernel initialization function
2389 static int
2390 __init lcs_init_module(void)
2392 int rc;
2394 pr_info("Loading %s\n", version);
2395 rc = lcs_register_debug_facility();
2396 LCS_DBF_TEXT(0, setup, "lcsinit");
2397 if (rc) {
2398 pr_err("Initialization failed\n");
2399 return rc;
2402 rc = register_cu3088_discipline(&lcs_group_driver);
2403 if (rc) {
2404 pr_err("Initialization failed\n");
2405 return rc;
2407 return 0;
2412 * LCS module cleanup function
2414 static void
2415 __exit lcs_cleanup_module(void)
2417 pr_info("Terminating lcs module.\n");
2418 LCS_DBF_TEXT(0, trace, "cleanup");
2419 unregister_cu3088_discipline(&lcs_group_driver);
2420 lcs_unregister_debug_facility();
2423 module_init(lcs_init_module);
2424 module_exit(lcs_cleanup_module);
2426 MODULE_AUTHOR("Frank Pavlic <fpavlic@de.ibm.com>");
2427 MODULE_LICENSE("GPL");