2 * linux/drivers/s390/net/lcs.c
4 * Linux for S/390 Lan Channel Station Network Driver
6 * Copyright (C) 1999-2001 IBM Deutschland Entwicklung GmbH,
8 * Author(s): Original Code written by
9 * DJ Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
11 * Frank Pavlic (pavlic@de.ibm.com) and
12 * Martin Schwidefsky <schwidefsky@de.ibm.com>
14 * $Revision: 1.96 $ $Date: 2004/11/11 13:42:33 $
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2, or (at your option)
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 #include <linux/module.h>
33 #include <linux/netdevice.h>
34 #include <linux/etherdevice.h>
35 #include <linux/trdevice.h>
36 #include <linux/fddidevice.h>
37 #include <linux/inetdevice.h>
39 #include <linux/igmp.h>
40 #include <linux/delay.h>
44 #include <asm/debug.h>
45 #include <asm/idals.h>
46 #include <asm/timex.h>
47 #include <linux/device.h>
48 #include <asm/ccwgroup.h>
54 #if !defined(CONFIG_NET_ETHERNET) && \
55 !defined(CONFIG_TR) && !defined(CONFIG_FDDI)
56 #error Cannot compile lcs.c without some net devices switched on.
60 * initialization string for output
62 #define VERSION_LCS_C "$Revision: 1.96 $"
64 static char version[] __initdata = "LCS driver ("VERSION_LCS_C "/" VERSION_LCS_H ")";
65 static char debug_buffer[255];
70 static void lcs_tasklet(unsigned long);
71 static void lcs_start_kernel_thread(struct lcs_card *card);
72 static void lcs_get_frames_cb(struct lcs_channel *, struct lcs_buffer *);
73 static int lcs_send_delipm(struct lcs_card *, struct lcs_ipm_list *);
76 * Debug Facility Stuff
78 static debug_info_t *lcs_dbf_setup;
79 static debug_info_t *lcs_dbf_trace;
82 * LCS Debug Facility functions
85 lcs_unregister_debug_facility(void)
88 debug_unregister(lcs_dbf_setup);
90 debug_unregister(lcs_dbf_trace);
94 lcs_register_debug_facility(void)
96 lcs_dbf_setup = debug_register("lcs_setup", 1, 1, 8);
97 lcs_dbf_trace = debug_register("lcs_trace", 1, 2, 8);
98 if (lcs_dbf_setup == NULL || lcs_dbf_trace == NULL) {
99 PRINT_ERR("Not enough memory for debug facility.\n");
100 lcs_unregister_debug_facility();
103 debug_register_view(lcs_dbf_setup, &debug_hex_ascii_view);
104 debug_set_level(lcs_dbf_setup, 4);
105 debug_register_view(lcs_dbf_trace, &debug_hex_ascii_view);
106 debug_set_level(lcs_dbf_trace, 4);
111 * Allocate io buffers.
114 lcs_alloc_channel(struct lcs_channel *channel)
118 LCS_DBF_TEXT(2, setup, "ichalloc");
119 for (cnt = 0; cnt < LCS_NUM_BUFFS; cnt++) {
120 /* alloc memory fo iobuffer */
121 channel->iob[cnt].data = (void *)
122 kmalloc(LCS_IOBUFFERSIZE, GFP_DMA | GFP_KERNEL);
123 if (channel->iob[cnt].data == NULL)
125 memset(channel->iob[cnt].data, 0, LCS_IOBUFFERSIZE);
126 channel->iob[cnt].state = BUF_STATE_EMPTY;
128 if (cnt < LCS_NUM_BUFFS) {
129 /* Not all io buffers could be allocated. */
130 LCS_DBF_TEXT(2, setup, "echalloc");
132 kfree(channel->iob[cnt].data);
142 lcs_free_channel(struct lcs_channel *channel)
146 LCS_DBF_TEXT(2, setup, "ichfree");
147 for (cnt = 0; cnt < LCS_NUM_BUFFS; cnt++) {
148 if (channel->iob[cnt].data != NULL)
149 kfree(channel->iob[cnt].data);
150 channel->iob[cnt].data = NULL;
158 lcs_cleanup_channel(struct lcs_channel *channel)
160 LCS_DBF_TEXT(3, setup, "cleanch");
161 /* Kill write channel tasklets. */
162 tasklet_kill(&channel->irq_tasklet);
163 /* Free channel buffers. */
164 lcs_free_channel(channel);
168 * LCS free memory for card and channels.
171 lcs_free_card(struct lcs_card *card)
173 LCS_DBF_TEXT(2, setup, "remcard");
174 LCS_DBF_HEX(2, setup, &card, sizeof(void*));
179 * LCS alloc memory for card and channels
181 static struct lcs_card *
184 struct lcs_card *card;
187 LCS_DBF_TEXT(2, setup, "alloclcs");
189 card = kmalloc(sizeof(struct lcs_card), GFP_KERNEL | GFP_DMA);
192 memset(card, 0, sizeof(struct lcs_card));
193 card->lan_type = LCS_FRAME_TYPE_AUTO;
195 card->lancmd_timeout = LCS_LANCMD_TIMEOUT_DEFAULT;
196 /* Allocate io buffers for the read channel. */
197 rc = lcs_alloc_channel(&card->read);
199 LCS_DBF_TEXT(2, setup, "iccwerr");
203 /* Allocate io buffers for the write channel. */
204 rc = lcs_alloc_channel(&card->write);
206 LCS_DBF_TEXT(2, setup, "iccwerr");
207 lcs_cleanup_channel(&card->read);
212 #ifdef CONFIG_IP_MULTICAST
213 INIT_LIST_HEAD(&card->ipm_list);
215 LCS_DBF_HEX(2, setup, &card, sizeof(void*));
220 * Setup read channel.
223 lcs_setup_read_ccws(struct lcs_card *card)
227 LCS_DBF_TEXT(2, setup, "ireadccw");
228 /* Setup read ccws. */
229 memset(card->read.ccws, 0, sizeof (struct ccw1) * (LCS_NUM_BUFFS + 1));
230 for (cnt = 0; cnt < LCS_NUM_BUFFS; cnt++) {
231 card->read.ccws[cnt].cmd_code = LCS_CCW_READ;
232 card->read.ccws[cnt].count = LCS_IOBUFFERSIZE;
233 card->read.ccws[cnt].flags =
234 CCW_FLAG_CC | CCW_FLAG_SLI | CCW_FLAG_PCI;
236 * Note: we have allocated the buffer with GFP_DMA, so
237 * we do not need to do set_normalized_cda.
239 card->read.ccws[cnt].cda =
240 (__u32) __pa(card->read.iob[cnt].data);
241 ((struct lcs_header *)
242 card->read.iob[cnt].data)->offset = LCS_ILLEGAL_OFFSET;
243 card->read.iob[cnt].callback = lcs_get_frames_cb;
244 card->read.iob[cnt].state = BUF_STATE_READY;
245 card->read.iob[cnt].count = LCS_IOBUFFERSIZE;
247 card->read.ccws[0].flags &= ~CCW_FLAG_PCI;
248 card->read.ccws[LCS_NUM_BUFFS - 1].flags &= ~CCW_FLAG_PCI;
249 card->read.ccws[LCS_NUM_BUFFS - 1].flags |= CCW_FLAG_SUSPEND;
250 /* Last ccw is a tic (transfer in channel). */
251 card->read.ccws[LCS_NUM_BUFFS].cmd_code = LCS_CCW_TRANSFER;
252 card->read.ccws[LCS_NUM_BUFFS].cda =
253 (__u32) __pa(card->read.ccws);
254 /* Setg initial state of the read channel. */
255 card->read.state = CH_STATE_INIT;
257 card->read.io_idx = 0;
258 card->read.buf_idx = 0;
262 lcs_setup_read(struct lcs_card *card)
264 LCS_DBF_TEXT(3, setup, "initread");
266 lcs_setup_read_ccws(card);
267 /* Initialize read channel tasklet. */
268 card->read.irq_tasklet.data = (unsigned long) &card->read;
269 card->read.irq_tasklet.func = lcs_tasklet;
270 /* Initialize waitqueue. */
271 init_waitqueue_head(&card->read.wait_q);
275 * Setup write channel.
278 lcs_setup_write_ccws(struct lcs_card *card)
282 LCS_DBF_TEXT(3, setup, "iwritccw");
283 /* Setup write ccws. */
284 memset(card->write.ccws, 0, sizeof(struct ccw1) * LCS_NUM_BUFFS + 1);
285 for (cnt = 0; cnt < LCS_NUM_BUFFS; cnt++) {
286 card->write.ccws[cnt].cmd_code = LCS_CCW_WRITE;
287 card->write.ccws[cnt].count = 0;
288 card->write.ccws[cnt].flags =
289 CCW_FLAG_SUSPEND | CCW_FLAG_CC | CCW_FLAG_SLI;
291 * Note: we have allocated the buffer with GFP_DMA, so
292 * we do not need to do set_normalized_cda.
294 card->write.ccws[cnt].cda =
295 (__u32) __pa(card->write.iob[cnt].data);
297 /* Last ccw is a tic (transfer in channel). */
298 card->write.ccws[LCS_NUM_BUFFS].cmd_code = LCS_CCW_TRANSFER;
299 card->write.ccws[LCS_NUM_BUFFS].cda =
300 (__u32) __pa(card->write.ccws);
301 /* Set initial state of the write channel. */
302 card->read.state = CH_STATE_INIT;
304 card->write.io_idx = 0;
305 card->write.buf_idx = 0;
309 lcs_setup_write(struct lcs_card *card)
311 LCS_DBF_TEXT(3, setup, "initwrit");
313 lcs_setup_write_ccws(card);
314 /* Initialize write channel tasklet. */
315 card->write.irq_tasklet.data = (unsigned long) &card->write;
316 card->write.irq_tasklet.func = lcs_tasklet;
317 /* Initialize waitqueue. */
318 init_waitqueue_head(&card->write.wait_q);
322 lcs_set_allowed_threads(struct lcs_card *card, unsigned long threads)
326 spin_lock_irqsave(&card->mask_lock, flags);
327 card->thread_allowed_mask = threads;
328 spin_unlock_irqrestore(&card->mask_lock, flags);
329 wake_up(&card->wait_q);
332 lcs_threads_running(struct lcs_card *card, unsigned long threads)
337 spin_lock_irqsave(&card->mask_lock, flags);
338 rc = (card->thread_running_mask & threads);
339 spin_unlock_irqrestore(&card->mask_lock, flags);
344 lcs_wait_for_threads(struct lcs_card *card, unsigned long threads)
346 return wait_event_interruptible(card->wait_q,
347 lcs_threads_running(card, threads) == 0);
351 lcs_set_thread_start_bit(struct lcs_card *card, unsigned long thread)
355 spin_lock_irqsave(&card->mask_lock, flags);
356 if ( !(card->thread_allowed_mask & thread) ||
357 (card->thread_start_mask & thread) ) {
358 spin_unlock_irqrestore(&card->mask_lock, flags);
361 card->thread_start_mask |= thread;
362 spin_unlock_irqrestore(&card->mask_lock, flags);
367 lcs_clear_thread_running_bit(struct lcs_card *card, unsigned long thread)
371 spin_lock_irqsave(&card->mask_lock, flags);
372 card->thread_running_mask &= ~thread;
373 spin_unlock_irqrestore(&card->mask_lock, flags);
374 wake_up(&card->wait_q);
378 __lcs_do_run_thread(struct lcs_card *card, unsigned long thread)
383 spin_lock_irqsave(&card->mask_lock, flags);
384 if (card->thread_start_mask & thread){
385 if ((card->thread_allowed_mask & thread) &&
386 !(card->thread_running_mask & thread)){
388 card->thread_start_mask &= ~thread;
389 card->thread_running_mask |= thread;
393 spin_unlock_irqrestore(&card->mask_lock, flags);
398 lcs_do_run_thread(struct lcs_card *card, unsigned long thread)
401 wait_event(card->wait_q,
402 (rc = __lcs_do_run_thread(card, thread)) >= 0);
407 lcs_do_start_thread(struct lcs_card *card, unsigned long thread)
412 spin_lock_irqsave(&card->mask_lock, flags);
413 LCS_DBF_TEXT_(4, trace, " %02x%02x%02x",
414 (u8) card->thread_start_mask,
415 (u8) card->thread_allowed_mask,
416 (u8) card->thread_running_mask);
417 rc = (card->thread_start_mask & thread);
418 spin_unlock_irqrestore(&card->mask_lock, flags);
423 * Initialize channels,card and state machines.
426 lcs_setup_card(struct lcs_card *card)
428 LCS_DBF_TEXT(2, setup, "initcard");
429 LCS_DBF_HEX(2, setup, &card, sizeof(void*));
431 lcs_setup_read(card);
432 lcs_setup_write(card);
433 /* Set cards initial state. */
434 card->state = DEV_STATE_DOWN;
435 card->tx_buffer = NULL;
436 card->tx_emitted = 0;
438 /* Initialize kernel thread task used for LGW commands. */
439 INIT_WORK(&card->kernel_thread_starter,
440 (void *)lcs_start_kernel_thread,card);
441 card->thread_start_mask = 0;
442 card->thread_allowed_mask = 0;
443 card->thread_running_mask = 0;
444 init_waitqueue_head(&card->wait_q);
445 spin_lock_init(&card->lock);
446 spin_lock_init(&card->ipm_lock);
447 spin_lock_init(&card->mask_lock);
448 #ifdef CONFIG_IP_MULTICAST
449 INIT_LIST_HEAD(&card->ipm_list);
451 INIT_LIST_HEAD(&card->lancmd_waiters);
455 lcs_clear_multicast_list(struct lcs_card *card)
457 #ifdef CONFIG_IP_MULTICAST
458 struct lcs_ipm_list *ipm;
461 /* Free multicast list. */
462 LCS_DBF_TEXT(3, setup, "clmclist");
463 spin_lock_irqsave(&card->ipm_lock, flags);
464 while (!list_empty(&card->ipm_list)){
465 ipm = list_entry(card->ipm_list.next,
466 struct lcs_ipm_list, list);
467 list_del(&ipm->list);
468 if (ipm->ipm_state != LCS_IPM_STATE_SET_REQUIRED){
469 spin_unlock_irqrestore(&card->ipm_lock, flags);
470 lcs_send_delipm(card, ipm);
471 spin_lock_irqsave(&card->ipm_lock, flags);
475 spin_unlock_irqrestore(&card->ipm_lock, flags);
479 * Cleanup channels,card and state machines.
482 lcs_cleanup_card(struct lcs_card *card)
485 LCS_DBF_TEXT(3, setup, "cleancrd");
486 LCS_DBF_HEX(2,setup,&card,sizeof(void*));
488 if (card->dev != NULL)
489 free_netdev(card->dev);
490 /* Cleanup channels. */
491 lcs_cleanup_channel(&card->write);
492 lcs_cleanup_channel(&card->read);
499 lcs_start_channel(struct lcs_channel *channel)
504 LCS_DBF_TEXT_(4,trace,"ssch%s", channel->ccwdev->dev.bus_id);
505 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
506 rc = ccw_device_start(channel->ccwdev,
507 channel->ccws + channel->io_idx, 0, 0,
508 DOIO_DENY_PREFETCH | DOIO_ALLOW_SUSPEND);
510 channel->state = CH_STATE_RUNNING;
511 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
513 LCS_DBF_TEXT_(4,trace,"essh%s", channel->ccwdev->dev.bus_id);
514 PRINT_ERR("Error in starting channel, rc=%d!\n", rc);
520 lcs_clear_channel(struct lcs_channel *channel)
525 LCS_DBF_TEXT(4,trace,"clearch");
526 LCS_DBF_TEXT_(4,trace,"%s", channel->ccwdev->dev.bus_id);
527 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
528 rc = ccw_device_clear(channel->ccwdev, (addr_t) channel);
529 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
531 LCS_DBF_TEXT_(4,trace,"ecsc%s", channel->ccwdev->dev.bus_id);
534 wait_event(channel->wait_q, (channel->state == CH_STATE_CLEARED));
535 channel->state = CH_STATE_STOPPED;
544 lcs_stop_channel(struct lcs_channel *channel)
549 if (channel->state == CH_STATE_STOPPED)
551 LCS_DBF_TEXT(4,trace,"haltsch");
552 LCS_DBF_TEXT_(4,trace,"%s", channel->ccwdev->dev.bus_id);
553 channel->state = CH_STATE_INIT;
554 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
555 rc = ccw_device_halt(channel->ccwdev, (addr_t) channel);
556 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
558 LCS_DBF_TEXT_(4,trace,"ehsc%s", channel->ccwdev->dev.bus_id);
561 /* Asynchronous halt initialted. Wait for its completion. */
562 wait_event(channel->wait_q, (channel->state == CH_STATE_HALTED));
563 lcs_clear_channel(channel);
568 * start read and write channel
571 lcs_start_channels(struct lcs_card *card)
575 LCS_DBF_TEXT(2, trace, "chstart");
576 /* start read channel */
577 rc = lcs_start_channel(&card->read);
580 /* start write channel */
581 rc = lcs_start_channel(&card->write);
583 lcs_stop_channel(&card->read);
588 * stop read and write channel
591 lcs_stop_channels(struct lcs_card *card)
593 LCS_DBF_TEXT(2, trace, "chhalt");
594 lcs_stop_channel(&card->read);
595 lcs_stop_channel(&card->write);
602 static struct lcs_buffer *
603 __lcs_get_buffer(struct lcs_channel *channel)
607 LCS_DBF_TEXT(5, trace, "_getbuff");
608 index = channel->io_idx;
610 if (channel->iob[index].state == BUF_STATE_EMPTY) {
611 channel->iob[index].state = BUF_STATE_LOCKED;
612 return channel->iob + index;
614 index = (index + 1) & (LCS_NUM_BUFFS - 1);
615 } while (index != channel->io_idx);
619 static struct lcs_buffer *
620 lcs_get_buffer(struct lcs_channel *channel)
622 struct lcs_buffer *buffer;
625 LCS_DBF_TEXT(5, trace, "getbuff");
626 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
627 buffer = __lcs_get_buffer(channel);
628 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
633 * Resume channel program if the channel is suspended.
636 __lcs_resume_channel(struct lcs_channel *channel)
640 if (channel->state != CH_STATE_SUSPENDED)
642 if (channel->ccws[channel->io_idx].flags & CCW_FLAG_SUSPEND)
644 LCS_DBF_TEXT_(5, trace, "rsch%s", channel->ccwdev->dev.bus_id);
645 rc = ccw_device_resume(channel->ccwdev);
647 LCS_DBF_TEXT_(4, trace, "ersc%s", channel->ccwdev->dev.bus_id);
648 PRINT_ERR("Error in lcs_resume_channel: rc=%d\n",rc);
650 channel->state = CH_STATE_RUNNING;
656 * Make a buffer ready for processing.
659 __lcs_ready_buffer_bits(struct lcs_channel *channel, int index)
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;
678 lcs_ready_buffer(struct lcs_channel *channel, struct lcs_buffer *buffer)
683 LCS_DBF_TEXT(5, trace, "rdybuff");
684 if (buffer->state != BUF_STATE_LOCKED &&
685 buffer->state != BUF_STATE_PROCESSED)
687 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
688 buffer->state = BUF_STATE_READY;
689 index = buffer - channel->iob;
691 channel->ccws[index].count = buffer->count;
692 /* Check relevant PCI/suspend bits. */
693 __lcs_ready_buffer_bits(channel, index);
694 rc = __lcs_resume_channel(channel);
695 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
700 * Mark the buffer as processed. Take care of the suspend bit
701 * of the previous buffer. This function is called from
702 * interrupt context, so the lock must not be taken.
705 __lcs_processed_buffer(struct lcs_channel *channel, struct lcs_buffer *buffer)
707 int index, prev, next;
709 LCS_DBF_TEXT(5, trace, "prcsbuff");
710 if (buffer->state != BUF_STATE_READY)
712 buffer->state = BUF_STATE_PROCESSED;
713 index = buffer - channel->iob;
714 prev = (index - 1) & (LCS_NUM_BUFFS - 1);
715 next = (index + 1) & (LCS_NUM_BUFFS - 1);
716 /* Set the suspend bit and clear the PCI bit of this buffer. */
717 channel->ccws[index].flags |= CCW_FLAG_SUSPEND;
718 channel->ccws[index].flags &= ~CCW_FLAG_PCI;
719 /* Check the suspend bit of the previous buffer. */
720 if (channel->iob[prev].state == BUF_STATE_READY) {
722 * Previous buffer is in state ready. It might have
723 * happened in lcs_ready_buffer that the suspend bit
724 * has not been cleared to avoid an endless loop.
727 __lcs_ready_buffer_bits(channel, prev);
729 /* Clear PCI bit of next buffer. */
730 channel->ccws[next].flags &= ~CCW_FLAG_PCI;
731 return __lcs_resume_channel(channel);
735 * Put a processed buffer back to state empty.
738 lcs_release_buffer(struct lcs_channel *channel, struct lcs_buffer *buffer)
742 LCS_DBF_TEXT(5, trace, "relbuff");
743 if (buffer->state != BUF_STATE_LOCKED &&
744 buffer->state != BUF_STATE_PROCESSED)
746 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
747 buffer->state = BUF_STATE_EMPTY;
748 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
752 * Get buffer for a lan command.
754 static struct lcs_buffer *
755 lcs_get_lancmd(struct lcs_card *card, int count)
757 struct lcs_buffer *buffer;
760 LCS_DBF_TEXT(4, trace, "getlncmd");
761 /* Get buffer and wait if none is available. */
762 wait_event(card->write.wait_q,
763 ((buffer = lcs_get_buffer(&card->write)) != NULL));
764 count += sizeof(struct lcs_header);
765 *(__u16 *)(buffer->data + count) = 0;
766 buffer->count = count + sizeof(__u16);
767 buffer->callback = lcs_release_buffer;
768 cmd = (struct lcs_cmd *) buffer->data;
770 cmd->type = LCS_FRAME_TYPE_CONTROL;
777 lcs_get_reply(struct lcs_reply *reply)
779 WARN_ON(atomic_read(&reply->refcnt) <= 0);
780 atomic_inc(&reply->refcnt);
784 lcs_put_reply(struct lcs_reply *reply)
786 WARN_ON(atomic_read(&reply->refcnt) <= 0);
787 if (atomic_dec_and_test(&reply->refcnt)) {
793 static struct lcs_reply *
794 lcs_alloc_reply(struct lcs_cmd *cmd)
796 struct lcs_reply *reply;
798 LCS_DBF_TEXT(4, trace, "getreply");
800 reply = kmalloc(sizeof(struct lcs_reply), GFP_ATOMIC);
803 memset(reply,0,sizeof(struct lcs_reply));
804 atomic_set(&reply->refcnt,1);
805 reply->sequence_no = cmd->sequence_no;
808 init_waitqueue_head(&reply->wait_q);
814 * Notifier function for lancmd replies. Called from read irq.
817 lcs_notify_lancmd_waiters(struct lcs_card *card, struct lcs_cmd *cmd)
819 struct list_head *l, *n;
820 struct lcs_reply *reply;
822 LCS_DBF_TEXT(4, trace, "notiwait");
823 spin_lock(&card->lock);
824 list_for_each_safe(l, n, &card->lancmd_waiters) {
825 reply = list_entry(l, struct lcs_reply, list);
826 if (reply->sequence_no == cmd->sequence_no) {
827 lcs_get_reply(reply);
828 list_del_init(&reply->list);
829 if (reply->callback != NULL)
830 reply->callback(card, cmd);
832 reply->rc = cmd->return_code;
833 wake_up(&reply->wait_q);
834 lcs_put_reply(reply);
838 spin_unlock(&card->lock);
842 * Emit buffer of a lan comand.
845 lcs_lancmd_timeout(unsigned long data)
847 struct lcs_reply *reply, *list_reply, *r;
850 LCS_DBF_TEXT(4, trace, "timeout");
851 reply = (struct lcs_reply *) data;
852 spin_lock_irqsave(&reply->card->lock, flags);
853 list_for_each_entry_safe(list_reply, r,
854 &reply->card->lancmd_waiters,list) {
855 if (reply == list_reply) {
856 lcs_get_reply(reply);
857 list_del_init(&reply->list);
858 spin_unlock_irqrestore(&reply->card->lock, flags);
861 wake_up(&reply->wait_q);
862 lcs_put_reply(reply);
866 spin_unlock_irqrestore(&reply->card->lock, flags);
870 lcs_send_lancmd(struct lcs_card *card, struct lcs_buffer *buffer,
871 void (*reply_callback)(struct lcs_card *, struct lcs_cmd *))
873 struct lcs_reply *reply;
875 struct timer_list timer;
879 LCS_DBF_TEXT(4, trace, "sendcmd");
880 cmd = (struct lcs_cmd *) buffer->data;
881 cmd->return_code = 0;
882 cmd->sequence_no = card->sequence_no++;
883 reply = lcs_alloc_reply(cmd);
886 reply->callback = reply_callback;
888 spin_lock_irqsave(&card->lock, flags);
889 list_add_tail(&reply->list, &card->lancmd_waiters);
890 spin_unlock_irqrestore(&card->lock, flags);
892 buffer->callback = lcs_release_buffer;
893 rc = lcs_ready_buffer(&card->write, buffer);
897 timer.function = lcs_lancmd_timeout;
898 timer.data = (unsigned long) reply;
899 timer.expires = jiffies + HZ*card->lancmd_timeout;
901 wait_event(reply->wait_q, reply->received);
902 del_timer_sync(&timer);
903 LCS_DBF_TEXT_(4, trace, "rc:%d",reply->rc);
905 lcs_put_reply(reply);
906 return rc ? -EIO : 0;
910 * LCS startup command
913 lcs_send_startup(struct lcs_card *card, __u8 initiator)
915 struct lcs_buffer *buffer;
918 LCS_DBF_TEXT(2, trace, "startup");
919 buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
920 cmd = (struct lcs_cmd *) buffer->data;
921 cmd->cmd_code = LCS_CMD_STARTUP;
922 cmd->initiator = initiator;
923 cmd->cmd.lcs_startup.buff_size = LCS_IOBUFFERSIZE;
924 return lcs_send_lancmd(card, buffer, NULL);
928 * LCS shutdown command
931 lcs_send_shutdown(struct lcs_card *card)
933 struct lcs_buffer *buffer;
936 LCS_DBF_TEXT(2, trace, "shutdown");
937 buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
938 cmd = (struct lcs_cmd *) buffer->data;
939 cmd->cmd_code = LCS_CMD_SHUTDOWN;
940 cmd->initiator = LCS_INITIATOR_TCPIP;
941 return lcs_send_lancmd(card, buffer, NULL);
945 * LCS lanstat command
948 __lcs_lanstat_cb(struct lcs_card *card, struct lcs_cmd *cmd)
950 LCS_DBF_TEXT(2, trace, "statcb");
951 memcpy(card->mac, cmd->cmd.lcs_lanstat_cmd.mac_addr, LCS_MAC_LENGTH);
955 lcs_send_lanstat(struct lcs_card *card)
957 struct lcs_buffer *buffer;
960 LCS_DBF_TEXT(2,trace, "cmdstat");
961 buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
962 cmd = (struct lcs_cmd *) buffer->data;
963 /* Setup lanstat command. */
964 cmd->cmd_code = LCS_CMD_LANSTAT;
965 cmd->initiator = LCS_INITIATOR_TCPIP;
966 cmd->cmd.lcs_std_cmd.lan_type = card->lan_type;
967 cmd->cmd.lcs_std_cmd.portno = card->portno;
968 return lcs_send_lancmd(card, buffer, __lcs_lanstat_cb);
972 * send stoplan command
975 lcs_send_stoplan(struct lcs_card *card, __u8 initiator)
977 struct lcs_buffer *buffer;
980 LCS_DBF_TEXT(2, trace, "cmdstpln");
981 buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
982 cmd = (struct lcs_cmd *) buffer->data;
983 cmd->cmd_code = LCS_CMD_STOPLAN;
984 cmd->initiator = initiator;
985 cmd->cmd.lcs_std_cmd.lan_type = card->lan_type;
986 cmd->cmd.lcs_std_cmd.portno = card->portno;
987 return lcs_send_lancmd(card, buffer, NULL);
991 * send startlan command
994 __lcs_send_startlan_cb(struct lcs_card *card, struct lcs_cmd *cmd)
996 LCS_DBF_TEXT(2, trace, "srtlancb");
997 card->lan_type = cmd->cmd.lcs_std_cmd.lan_type;
998 card->portno = cmd->cmd.lcs_std_cmd.portno;
1002 lcs_send_startlan(struct lcs_card *card, __u8 initiator)
1004 struct lcs_buffer *buffer;
1005 struct lcs_cmd *cmd;
1007 LCS_DBF_TEXT(2, trace, "cmdstaln");
1008 buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
1009 cmd = (struct lcs_cmd *) buffer->data;
1010 cmd->cmd_code = LCS_CMD_STARTLAN;
1011 cmd->initiator = initiator;
1012 cmd->cmd.lcs_std_cmd.lan_type = card->lan_type;
1013 cmd->cmd.lcs_std_cmd.portno = card->portno;
1014 return lcs_send_lancmd(card, buffer, __lcs_send_startlan_cb);
1017 #ifdef CONFIG_IP_MULTICAST
1019 * send setipm command (Multicast)
1022 lcs_send_setipm(struct lcs_card *card,struct lcs_ipm_list *ipm_list)
1024 struct lcs_buffer *buffer;
1025 struct lcs_cmd *cmd;
1027 LCS_DBF_TEXT(2, trace, "cmdsetim");
1028 buffer = lcs_get_lancmd(card, LCS_MULTICAST_CMD_SIZE);
1029 cmd = (struct lcs_cmd *) buffer->data;
1030 cmd->cmd_code = LCS_CMD_SETIPM;
1031 cmd->initiator = LCS_INITIATOR_TCPIP;
1032 cmd->cmd.lcs_qipassist.lan_type = card->lan_type;
1033 cmd->cmd.lcs_qipassist.portno = card->portno;
1034 cmd->cmd.lcs_qipassist.version = 4;
1035 cmd->cmd.lcs_qipassist.num_ip_pairs = 1;
1036 memcpy(cmd->cmd.lcs_qipassist.lcs_ipass_ctlmsg.ip_mac_pair,
1037 &ipm_list->ipm, sizeof (struct lcs_ip_mac_pair));
1038 LCS_DBF_TEXT_(2, trace, "%x",ipm_list->ipm.ip_addr);
1039 return lcs_send_lancmd(card, buffer, NULL);
1043 * send delipm command (Multicast)
1046 lcs_send_delipm(struct lcs_card *card,struct lcs_ipm_list *ipm_list)
1048 struct lcs_buffer *buffer;
1049 struct lcs_cmd *cmd;
1051 LCS_DBF_TEXT(2, trace, "cmddelim");
1052 buffer = lcs_get_lancmd(card, LCS_MULTICAST_CMD_SIZE);
1053 cmd = (struct lcs_cmd *) buffer->data;
1054 cmd->cmd_code = LCS_CMD_DELIPM;
1055 cmd->initiator = LCS_INITIATOR_TCPIP;
1056 cmd->cmd.lcs_qipassist.lan_type = card->lan_type;
1057 cmd->cmd.lcs_qipassist.portno = card->portno;
1058 cmd->cmd.lcs_qipassist.version = 4;
1059 cmd->cmd.lcs_qipassist.num_ip_pairs = 1;
1060 memcpy(cmd->cmd.lcs_qipassist.lcs_ipass_ctlmsg.ip_mac_pair,
1061 &ipm_list->ipm, sizeof (struct lcs_ip_mac_pair));
1062 LCS_DBF_TEXT_(2, trace, "%x",ipm_list->ipm.ip_addr);
1063 return lcs_send_lancmd(card, buffer, NULL);
1067 * check if multicast is supported by LCS
1070 __lcs_check_multicast_cb(struct lcs_card *card, struct lcs_cmd *cmd)
1072 LCS_DBF_TEXT(2, trace, "chkmccb");
1073 card->ip_assists_supported =
1074 cmd->cmd.lcs_qipassist.ip_assists_supported;
1075 card->ip_assists_enabled =
1076 cmd->cmd.lcs_qipassist.ip_assists_enabled;
1080 lcs_check_multicast_support(struct lcs_card *card)
1082 struct lcs_buffer *buffer;
1083 struct lcs_cmd *cmd;
1086 LCS_DBF_TEXT(2, trace, "cmdqipa");
1087 /* Send query ipassist. */
1088 buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
1089 cmd = (struct lcs_cmd *) buffer->data;
1090 cmd->cmd_code = LCS_CMD_QIPASSIST;
1091 cmd->initiator = LCS_INITIATOR_TCPIP;
1092 cmd->cmd.lcs_qipassist.lan_type = card->lan_type;
1093 cmd->cmd.lcs_qipassist.portno = card->portno;
1094 cmd->cmd.lcs_qipassist.version = 4;
1095 cmd->cmd.lcs_qipassist.num_ip_pairs = 1;
1096 rc = lcs_send_lancmd(card, buffer, __lcs_check_multicast_cb);
1098 PRINT_ERR("Query IPAssist failed. Assuming unsupported!\n");
1101 /* Print out supported assists: IPv6 */
1102 PRINT_INFO("LCS device %s %s IPv6 support\n", card->dev->name,
1103 (card->ip_assists_supported & LCS_IPASS_IPV6_SUPPORT) ?
1104 "with" : "without");
1105 /* Print out supported assist: Multicast */
1106 PRINT_INFO("LCS device %s %s Multicast support\n", card->dev->name,
1107 (card->ip_assists_supported & LCS_IPASS_MULTICAST_SUPPORT) ?
1108 "with" : "without");
1109 if (card->ip_assists_supported & LCS_IPASS_MULTICAST_SUPPORT)
1115 * set or del multicast address on LCS card
1118 lcs_fix_multicast_list(struct lcs_card *card)
1120 struct list_head failed_list;
1121 struct lcs_ipm_list *ipm, *tmp;
1122 unsigned long flags;
1125 LCS_DBF_TEXT(4,trace, "fixipm");
1126 INIT_LIST_HEAD(&failed_list);
1127 spin_lock_irqsave(&card->ipm_lock, flags);
1129 list_for_each_entry_safe(ipm, tmp, &card->ipm_list, list){
1130 switch (ipm->ipm_state) {
1131 case LCS_IPM_STATE_SET_REQUIRED:
1132 /* del from ipm_list so noone else can tamper with
1134 list_del_init(&ipm->list);
1135 spin_unlock_irqrestore(&card->ipm_lock, flags);
1136 rc = lcs_send_setipm(card, ipm);
1137 spin_lock_irqsave(&card->ipm_lock, flags);
1139 PRINT_INFO("Adding multicast address failed."
1140 "Table possibly full!\n");
1141 /* store ipm in failed list -> will be added
1142 * to ipm_list again, so a retry will be done
1143 * during the next call of this function */
1144 list_add_tail(&ipm->list, &failed_list);
1146 ipm->ipm_state = LCS_IPM_STATE_ON_CARD;
1147 /* re-insert into ipm_list */
1148 list_add_tail(&ipm->list, &card->ipm_list);
1151 case LCS_IPM_STATE_DEL_REQUIRED:
1152 list_del(&ipm->list);
1153 spin_unlock_irqrestore(&card->ipm_lock, flags);
1154 lcs_send_delipm(card, ipm);
1155 spin_lock_irqsave(&card->ipm_lock, flags);
1158 case LCS_IPM_STATE_ON_CARD:
1162 /* re-insert all entries from the failed_list into ipm_list */
1163 list_for_each_entry(ipm, &failed_list, list) {
1164 list_del_init(&ipm->list);
1165 list_add_tail(&ipm->list, &card->ipm_list);
1167 spin_unlock_irqrestore(&card->ipm_lock, flags);
1168 if (card->state == DEV_STATE_UP)
1169 netif_wake_queue(card->dev);
1173 * get mac address for the relevant Multicast address
1176 lcs_get_mac_for_ipm(__u32 ipm, char *mac, struct net_device *dev)
1178 LCS_DBF_TEXT(4,trace, "getmac");
1179 if (dev->type == ARPHRD_IEEE802_TR)
1180 ip_tr_mc_map(ipm, mac);
1182 ip_eth_mc_map(ipm, mac);
1186 * function called by net device to handle multicast address relevant things
1189 lcs_remove_mc_addresses(struct lcs_card *card, struct in_device *in4_dev)
1191 struct ip_mc_list *im4;
1192 struct list_head *l;
1193 struct lcs_ipm_list *ipm;
1194 unsigned long flags;
1195 char buf[MAX_ADDR_LEN];
1197 LCS_DBF_TEXT(4, trace, "remmclst");
1198 spin_lock_irqsave(&card->ipm_lock, flags);
1199 list_for_each(l, &card->ipm_list) {
1200 ipm = list_entry(l, struct lcs_ipm_list, list);
1201 for (im4 = in4_dev->mc_list; im4 != NULL; im4 = im4->next) {
1202 lcs_get_mac_for_ipm(im4->multiaddr, buf, card->dev);
1203 if ( (ipm->ipm.ip_addr == im4->multiaddr) &&
1204 (memcmp(buf, &ipm->ipm.mac_addr,
1205 LCS_MAC_LENGTH) == 0) )
1209 ipm->ipm_state = LCS_IPM_STATE_DEL_REQUIRED;
1211 spin_unlock_irqrestore(&card->ipm_lock, flags);
1214 static inline struct lcs_ipm_list *
1215 lcs_check_addr_entry(struct lcs_card *card, struct ip_mc_list *im4, char *buf)
1217 struct lcs_ipm_list *tmp, *ipm = NULL;
1218 struct list_head *l;
1219 unsigned long flags;
1221 LCS_DBF_TEXT(4, trace, "chkmcent");
1222 spin_lock_irqsave(&card->ipm_lock, flags);
1223 list_for_each(l, &card->ipm_list) {
1224 tmp = list_entry(l, struct lcs_ipm_list, list);
1225 if ( (tmp->ipm.ip_addr == im4->multiaddr) &&
1226 (memcmp(buf, &tmp->ipm.mac_addr,
1227 LCS_MAC_LENGTH) == 0) ) {
1232 spin_unlock_irqrestore(&card->ipm_lock, flags);
1237 lcs_set_mc_addresses(struct lcs_card *card, struct in_device *in4_dev)
1240 struct ip_mc_list *im4;
1241 struct lcs_ipm_list *ipm;
1242 char buf[MAX_ADDR_LEN];
1243 unsigned long flags;
1245 LCS_DBF_TEXT(4, trace, "setmclst");
1246 for (im4 = in4_dev->mc_list; im4; im4 = im4->next) {
1247 lcs_get_mac_for_ipm(im4->multiaddr, buf, card->dev);
1248 ipm = lcs_check_addr_entry(card, im4, buf);
1250 continue; /* Address already in list. */
1251 ipm = (struct lcs_ipm_list *)
1252 kmalloc(sizeof(struct lcs_ipm_list), GFP_ATOMIC);
1254 PRINT_INFO("Not enough memory to add "
1255 "new multicast entry!\n");
1258 memset(ipm, 0, sizeof(struct lcs_ipm_list));
1259 memcpy(&ipm->ipm.mac_addr, buf, LCS_MAC_LENGTH);
1260 ipm->ipm.ip_addr = im4->multiaddr;
1261 ipm->ipm_state = LCS_IPM_STATE_SET_REQUIRED;
1262 spin_lock_irqsave(&card->ipm_lock, flags);
1263 list_add(&ipm->list, &card->ipm_list);
1264 spin_unlock_irqrestore(&card->ipm_lock, flags);
1269 lcs_register_mc_addresses(void *data)
1271 struct lcs_card *card;
1272 struct in_device *in4_dev;
1274 card = (struct lcs_card *) data;
1275 daemonize("regipm");
1277 if (!lcs_do_run_thread(card, LCS_SET_MC_THREAD))
1279 LCS_DBF_TEXT(4, trace, "regmulti");
1281 in4_dev = in_dev_get(card->dev);
1282 if (in4_dev == NULL)
1284 read_lock(&in4_dev->mc_list_lock);
1285 lcs_remove_mc_addresses(card,in4_dev);
1286 lcs_set_mc_addresses(card, in4_dev);
1287 read_unlock(&in4_dev->mc_list_lock);
1288 in_dev_put(in4_dev);
1290 lcs_fix_multicast_list(card);
1292 lcs_clear_thread_running_bit(card, LCS_SET_MC_THREAD);
1296 * function called by net device to
1297 * handle multicast address relevant things
1300 lcs_set_multicast_list(struct net_device *dev)
1302 struct lcs_card *card;
1304 LCS_DBF_TEXT(4, trace, "setmulti");
1305 card = (struct lcs_card *) dev->priv;
1307 if (!lcs_set_thread_start_bit(card, LCS_SET_MC_THREAD)) {
1308 schedule_work(&card->kernel_thread_starter);
1312 #endif /* CONFIG_IP_MULTICAST */
1315 lcs_check_irb_error(struct ccw_device *cdev, struct irb *irb)
1320 switch (PTR_ERR(irb)) {
1322 PRINT_WARN("i/o-error on device %s\n", cdev->dev.bus_id);
1323 LCS_DBF_TEXT(2, trace, "ckirberr");
1324 LCS_DBF_TEXT_(2, trace, " rc%d", -EIO);
1327 PRINT_WARN("timeout on device %s\n", cdev->dev.bus_id);
1328 LCS_DBF_TEXT(2, trace, "ckirberr");
1329 LCS_DBF_TEXT_(2, trace, " rc%d", -ETIMEDOUT);
1332 PRINT_WARN("unknown error %ld on device %s\n", PTR_ERR(irb),
1334 LCS_DBF_TEXT(2, trace, "ckirberr");
1335 LCS_DBF_TEXT(2, trace, " rc???");
1337 return PTR_ERR(irb);
1342 * IRQ Handler for LCS channels
1345 lcs_irq(struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
1347 struct lcs_card *card;
1348 struct lcs_channel *channel;
1351 if (lcs_check_irb_error(cdev, irb))
1354 card = CARD_FROM_DEV(cdev);
1355 if (card->read.ccwdev == cdev)
1356 channel = &card->read;
1358 channel = &card->write;
1360 LCS_DBF_TEXT_(5, trace, "Rint%s",cdev->dev.bus_id);
1361 LCS_DBF_TEXT_(5, trace, "%4x%4x",irb->scsw.cstat, irb->scsw.dstat);
1362 LCS_DBF_TEXT_(5, trace, "%4x%4x",irb->scsw.fctl, irb->scsw.actl);
1364 /* How far in the ccw chain have we processed? */
1365 if ((channel->state != CH_STATE_INIT) &&
1366 (irb->scsw.fctl & SCSW_FCTL_START_FUNC)) {
1367 index = (struct ccw1 *) __va((addr_t) irb->scsw.cpa)
1369 if ((irb->scsw.actl & SCSW_ACTL_SUSPENDED) ||
1370 (irb->scsw.cstat | SCHN_STAT_PCI))
1371 /* Bloody io subsystem tells us lies about cpa... */
1372 index = (index - 1) & (LCS_NUM_BUFFS - 1);
1373 while (channel->io_idx != index) {
1374 __lcs_processed_buffer(channel,
1375 channel->iob + channel->io_idx);
1377 (channel->io_idx + 1) & (LCS_NUM_BUFFS - 1);
1381 if ((irb->scsw.dstat & DEV_STAT_DEV_END) ||
1382 (irb->scsw.dstat & DEV_STAT_CHN_END) ||
1383 (irb->scsw.dstat & DEV_STAT_UNIT_CHECK))
1384 /* Mark channel as stopped. */
1385 channel->state = CH_STATE_STOPPED;
1386 else if (irb->scsw.actl & SCSW_ACTL_SUSPENDED)
1387 /* CCW execution stopped on a suspend bit. */
1388 channel->state = CH_STATE_SUSPENDED;
1390 if (irb->scsw.fctl & SCSW_FCTL_HALT_FUNC) {
1391 if (irb->scsw.cc != 0) {
1392 ccw_device_halt(channel->ccwdev, (addr_t) channel);
1395 /* The channel has been stopped by halt_IO. */
1396 channel->state = CH_STATE_HALTED;
1399 if (irb->scsw.fctl & SCSW_FCTL_CLEAR_FUNC) {
1400 channel->state = CH_STATE_CLEARED;
1402 /* Do the rest in the tasklet. */
1403 tasklet_schedule(&channel->irq_tasklet);
1407 * Tasklet for IRQ handler
1410 lcs_tasklet(unsigned long data)
1412 unsigned long flags;
1413 struct lcs_channel *channel;
1414 struct lcs_buffer *iob;
1418 channel = (struct lcs_channel *) data;
1419 LCS_DBF_TEXT_(5, trace, "tlet%s",channel->ccwdev->dev.bus_id);
1421 /* Check for processed buffers. */
1423 buf_idx = channel->buf_idx;
1424 while (iob[buf_idx].state == BUF_STATE_PROCESSED) {
1425 /* Do the callback thing. */
1426 if (iob[buf_idx].callback != NULL)
1427 iob[buf_idx].callback(channel, iob + buf_idx);
1428 buf_idx = (buf_idx + 1) & (LCS_NUM_BUFFS - 1);
1430 channel->buf_idx = buf_idx;
1432 if (channel->state == CH_STATE_STOPPED)
1433 // FIXME: what if rc != 0 ??
1434 rc = lcs_start_channel(channel);
1435 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1436 if (channel->state == CH_STATE_SUSPENDED &&
1437 channel->iob[channel->io_idx].state == BUF_STATE_READY) {
1438 // FIXME: what if rc != 0 ??
1439 rc = __lcs_resume_channel(channel);
1441 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1443 /* Something happened on the channel. Wake up waiters. */
1444 wake_up(&channel->wait_q);
1448 * Finish current tx buffer and make it ready for transmit.
1451 __lcs_emit_txbuffer(struct lcs_card *card)
1453 LCS_DBF_TEXT(5, trace, "emittx");
1454 *(__u16 *)(card->tx_buffer->data + card->tx_buffer->count) = 0;
1455 card->tx_buffer->count += 2;
1456 lcs_ready_buffer(&card->write, card->tx_buffer);
1457 card->tx_buffer = NULL;
1462 * Callback for finished tx buffers.
1465 lcs_txbuffer_cb(struct lcs_channel *channel, struct lcs_buffer *buffer)
1467 struct lcs_card *card;
1469 LCS_DBF_TEXT(5, trace, "txbuffcb");
1470 /* Put buffer back to pool. */
1471 lcs_release_buffer(channel, buffer);
1472 card = (struct lcs_card *)
1473 ((char *) channel - offsetof(struct lcs_card, write));
1474 spin_lock(&card->lock);
1476 if (card->tx_emitted <= 0 && card->tx_buffer != NULL)
1478 * Last running tx buffer has finished. Submit partially
1479 * filled current buffer.
1481 __lcs_emit_txbuffer(card);
1482 spin_unlock(&card->lock);
1486 * Packet transmit function called by network stack
1489 __lcs_start_xmit(struct lcs_card *card, struct sk_buff *skb,
1490 struct net_device *dev)
1492 struct lcs_header *header;
1494 LCS_DBF_TEXT(5, trace, "hardxmit");
1496 card->stats.tx_dropped++;
1497 card->stats.tx_errors++;
1500 if (card->state != DEV_STATE_UP) {
1502 card->stats.tx_dropped++;
1503 card->stats.tx_errors++;
1504 card->stats.tx_carrier_errors++;
1507 if (netif_queue_stopped(dev) ) {
1508 card->stats.tx_dropped++;
1511 if (card->tx_buffer != NULL &&
1512 card->tx_buffer->count + sizeof(struct lcs_header) +
1513 skb->len + sizeof(u16) > LCS_IOBUFFERSIZE)
1514 /* skb too big for current tx buffer. */
1515 __lcs_emit_txbuffer(card);
1516 if (card->tx_buffer == NULL) {
1517 /* Get new tx buffer */
1518 card->tx_buffer = lcs_get_buffer(&card->write);
1519 if (card->tx_buffer == NULL) {
1520 card->stats.tx_dropped++;
1523 card->tx_buffer->callback = lcs_txbuffer_cb;
1524 card->tx_buffer->count = 0;
1526 header = (struct lcs_header *)
1527 (card->tx_buffer->data + card->tx_buffer->count);
1528 card->tx_buffer->count += skb->len + sizeof(struct lcs_header);
1529 header->offset = card->tx_buffer->count;
1530 header->type = card->lan_type;
1531 header->slot = card->portno;
1532 memcpy(header + 1, skb->data, skb->len);
1533 card->stats.tx_bytes += skb->len;
1534 card->stats.tx_packets++;
1536 if (card->tx_emitted <= 0)
1537 /* If this is the first tx buffer emit it immediately. */
1538 __lcs_emit_txbuffer(card);
1543 lcs_start_xmit(struct sk_buff *skb, struct net_device *dev)
1545 struct lcs_card *card;
1548 LCS_DBF_TEXT(5, trace, "pktxmit");
1549 card = (struct lcs_card *) dev->priv;
1550 spin_lock(&card->lock);
1551 rc = __lcs_start_xmit(card, skb, dev);
1552 spin_unlock(&card->lock);
1557 * send startlan and lanstat command to make LCS device ready
1560 lcs_startlan_auto(struct lcs_card *card)
1564 LCS_DBF_TEXT(2, trace, "strtauto");
1565 #ifdef CONFIG_NET_ETHERNET
1566 card->lan_type = LCS_FRAME_TYPE_ENET;
1567 rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
1573 card->lan_type = LCS_FRAME_TYPE_TR;
1574 rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
1579 card->lan_type = LCS_FRAME_TYPE_FDDI;
1580 rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
1588 lcs_startlan(struct lcs_card *card)
1592 LCS_DBF_TEXT(2, trace, "startlan");
1594 if (card->portno != LCS_INVALID_PORT_NO) {
1595 if (card->lan_type == LCS_FRAME_TYPE_AUTO)
1596 rc = lcs_startlan_auto(card);
1598 rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
1600 for (i = 0; i <= 16; i++) {
1602 if (card->lan_type != LCS_FRAME_TYPE_AUTO)
1603 rc = lcs_send_startlan(card,
1604 LCS_INITIATOR_TCPIP);
1606 /* autodetecting lan type */
1607 rc = lcs_startlan_auto(card);
1613 return lcs_send_lanstat(card);
1618 * LCS detect function
1619 * setup channels and make them I/O ready
1622 lcs_detect(struct lcs_card *card)
1626 LCS_DBF_TEXT(2, setup, "lcsdetct");
1627 /* start/reset card */
1629 netif_stop_queue(card->dev);
1630 rc = lcs_stop_channels(card);
1632 rc = lcs_start_channels(card);
1634 rc = lcs_send_startup(card, LCS_INITIATOR_TCPIP);
1636 rc = lcs_startlan(card);
1640 card->state = DEV_STATE_UP;
1642 card->state = DEV_STATE_DOWN;
1643 card->write.state = CH_STATE_INIT;
1644 card->read.state = CH_STATE_INIT;
1653 lcs_resetcard(struct lcs_card *card)
1657 LCS_DBF_TEXT(2, trace, "rescard");
1658 for (retries = 0; retries < 10; retries++) {
1659 if (lcs_detect(card) == 0) {
1660 netif_wake_queue(card->dev);
1661 card->state = DEV_STATE_UP;
1662 PRINT_INFO("LCS device %s successfully restarted!\n",
1668 PRINT_ERR("Error in Reseting LCS card!\n");
1677 lcs_stopcard(struct lcs_card *card)
1681 LCS_DBF_TEXT(3, setup, "stopcard");
1683 if (card->read.state != CH_STATE_STOPPED &&
1684 card->write.state != CH_STATE_STOPPED &&
1685 card->state == DEV_STATE_UP) {
1686 lcs_clear_multicast_list(card);
1687 rc = lcs_send_stoplan(card,LCS_INITIATOR_TCPIP);
1688 rc = lcs_send_shutdown(card);
1690 rc = lcs_stop_channels(card);
1691 card->state = DEV_STATE_DOWN;
1697 * LGW initiated commands
1700 lcs_lgw_startlan_thread(void *data)
1702 struct lcs_card *card;
1704 card = (struct lcs_card *) data;
1705 daemonize("lgwstpln");
1707 if (!lcs_do_run_thread(card, LCS_STARTLAN_THREAD))
1709 LCS_DBF_TEXT(4, trace, "lgwstpln");
1711 netif_stop_queue(card->dev);
1712 if (lcs_startlan(card) == 0) {
1713 netif_wake_queue(card->dev);
1714 card->state = DEV_STATE_UP;
1715 PRINT_INFO("LCS Startlan for device %s succeeded!\n",
1719 PRINT_ERR("LCS Startlan for device %s failed!\n",
1721 lcs_clear_thread_running_bit(card, LCS_STARTLAN_THREAD);
1726 * Send startup command initiated by Lan Gateway
1729 lcs_lgw_startup_thread(void *data)
1733 struct lcs_card *card;
1735 card = (struct lcs_card *) data;
1736 daemonize("lgwstaln");
1738 if (!lcs_do_run_thread(card, LCS_STARTUP_THREAD))
1740 LCS_DBF_TEXT(4, trace, "lgwstaln");
1742 netif_stop_queue(card->dev);
1743 rc = lcs_send_startup(card, LCS_INITIATOR_LGW);
1745 PRINT_ERR("Startup for LCS device %s initiated " \
1746 "by LGW failed!\nReseting card ...\n",
1748 /* do a card reset */
1749 rc = lcs_resetcard(card);
1753 rc = lcs_startlan(card);
1755 netif_wake_queue(card->dev);
1756 card->state = DEV_STATE_UP;
1760 PRINT_INFO("LCS Startup for device %s succeeded!\n",
1763 PRINT_ERR("LCS Startup for device %s failed!\n",
1765 lcs_clear_thread_running_bit(card, LCS_STARTUP_THREAD);
1771 * send stoplan command initiated by Lan Gateway
1774 lcs_lgw_stoplan_thread(void *data)
1776 struct lcs_card *card;
1779 card = (struct lcs_card *) data;
1780 daemonize("lgwstop");
1782 if (!lcs_do_run_thread(card, LCS_STOPLAN_THREAD))
1784 LCS_DBF_TEXT(4, trace, "lgwstop");
1786 netif_stop_queue(card->dev);
1787 if (lcs_send_stoplan(card, LCS_INITIATOR_LGW) == 0)
1788 PRINT_INFO("Stoplan for %s initiated by LGW succeeded!\n",
1791 PRINT_ERR("Stoplan %s initiated by LGW failed!\n",
1793 /*Try to reset the card, stop it on failure */
1794 rc = lcs_resetcard(card);
1796 rc = lcs_stopcard(card);
1797 lcs_clear_thread_running_bit(card, LCS_STOPLAN_THREAD);
1802 * Kernel Thread helper functions for LGW initiated commands
1805 lcs_start_kernel_thread(struct lcs_card *card)
1807 LCS_DBF_TEXT(5, trace, "krnthrd");
1808 if (lcs_do_start_thread(card, LCS_STARTUP_THREAD))
1809 kernel_thread(lcs_lgw_startup_thread, (void *) card, SIGCHLD);
1810 if (lcs_do_start_thread(card, LCS_STARTLAN_THREAD))
1811 kernel_thread(lcs_lgw_startlan_thread, (void *) card, SIGCHLD);
1812 if (lcs_do_start_thread(card, LCS_STOPLAN_THREAD))
1813 kernel_thread(lcs_lgw_stoplan_thread, (void *) card, SIGCHLD);
1814 #ifdef CONFIG_IP_MULTICAST
1815 if (lcs_do_start_thread(card, LCS_SET_MC_THREAD))
1816 kernel_thread(lcs_register_mc_addresses, (void *) card, SIGCHLD);
1821 * Process control frames.
1824 lcs_get_control(struct lcs_card *card, struct lcs_cmd *cmd)
1826 LCS_DBF_TEXT(5, trace, "getctrl");
1827 if (cmd->initiator == LCS_INITIATOR_LGW) {
1828 switch(cmd->cmd_code) {
1829 case LCS_CMD_STARTUP:
1830 if (!lcs_set_thread_start_bit(card,
1831 LCS_STARTUP_THREAD))
1832 schedule_work(&card->kernel_thread_starter);
1834 case LCS_CMD_STARTLAN:
1835 if (!lcs_set_thread_start_bit(card,
1836 LCS_STARTLAN_THREAD))
1837 schedule_work(&card->kernel_thread_starter);
1839 case LCS_CMD_STOPLAN:
1840 if (!lcs_set_thread_start_bit(card,
1841 LCS_STOPLAN_THREAD))
1842 schedule_work(&card->kernel_thread_starter);
1845 PRINT_INFO("UNRECOGNIZED LGW COMMAND\n");
1849 lcs_notify_lancmd_waiters(card, cmd);
1853 * Unpack network packet.
1856 lcs_get_skb(struct lcs_card *card, char *skb_data, unsigned int skb_len)
1858 struct sk_buff *skb;
1860 LCS_DBF_TEXT(5, trace, "getskb");
1861 if (card->dev == NULL ||
1862 card->state != DEV_STATE_UP)
1863 /* The card isn't up. Ignore the packet. */
1866 skb = dev_alloc_skb(skb_len);
1868 PRINT_ERR("LCS: alloc_skb failed for device=%s\n",
1870 card->stats.rx_dropped++;
1873 skb->dev = card->dev;
1874 memcpy(skb_put(skb, skb_len), skb_data, skb_len);
1875 skb->protocol = card->lan_type_trans(skb, card->dev);
1876 card->stats.rx_bytes += skb_len;
1877 card->stats.rx_packets++;
1878 *((__u32 *)skb->cb) = ++card->pkt_seq;
1883 * LCS main routine to get packets and lancmd replies from the buffers
1886 lcs_get_frames_cb(struct lcs_channel *channel, struct lcs_buffer *buffer)
1888 struct lcs_card *card;
1889 struct lcs_header *lcs_hdr;
1892 LCS_DBF_TEXT(5, trace, "lcsgtpkt");
1893 lcs_hdr = (struct lcs_header *) buffer->data;
1894 if (lcs_hdr->offset == LCS_ILLEGAL_OFFSET) {
1895 LCS_DBF_TEXT(4, trace, "-eiogpkt");
1898 card = (struct lcs_card *)
1899 ((char *) channel - offsetof(struct lcs_card, read));
1901 while (lcs_hdr->offset != 0) {
1902 if (lcs_hdr->offset <= 0 ||
1903 lcs_hdr->offset > LCS_IOBUFFERSIZE ||
1904 lcs_hdr->offset < offset) {
1905 /* Offset invalid. */
1906 card->stats.rx_length_errors++;
1907 card->stats.rx_errors++;
1910 /* What kind of frame is it? */
1911 if (lcs_hdr->type == LCS_FRAME_TYPE_CONTROL)
1912 /* Control frame. */
1913 lcs_get_control(card, (struct lcs_cmd *) lcs_hdr);
1914 else if (lcs_hdr->type == LCS_FRAME_TYPE_ENET ||
1915 lcs_hdr->type == LCS_FRAME_TYPE_TR ||
1916 lcs_hdr->type == LCS_FRAME_TYPE_FDDI)
1917 /* Normal network packet. */
1918 lcs_get_skb(card, (char *)(lcs_hdr + 1),
1919 lcs_hdr->offset - offset -
1920 sizeof(struct lcs_header));
1922 /* Unknown frame type. */
1923 ; // FIXME: error message ?
1924 /* Proceed to next frame. */
1925 offset = lcs_hdr->offset;
1926 lcs_hdr->offset = LCS_ILLEGAL_OFFSET;
1927 lcs_hdr = (struct lcs_header *) (buffer->data + offset);
1929 /* The buffer is now empty. Make it ready again. */
1930 lcs_ready_buffer(&card->read, buffer);
1934 * get network statistics for ifconfig and other user programs
1936 static struct net_device_stats *
1937 lcs_getstats(struct net_device *dev)
1939 struct lcs_card *card;
1941 LCS_DBF_TEXT(4, trace, "netstats");
1942 card = (struct lcs_card *) dev->priv;
1943 return &card->stats;
1948 * This function will be called by user doing ifconfig xxx down
1951 lcs_stop_device(struct net_device *dev)
1953 struct lcs_card *card;
1956 LCS_DBF_TEXT(2, trace, "stopdev");
1957 card = (struct lcs_card *) dev->priv;
1958 netif_stop_queue(dev);
1959 dev->flags &= ~IFF_UP;
1960 rc = lcs_stopcard(card);
1962 PRINT_ERR("Try it again!\n ");
1967 * start lcs device and make it runnable
1968 * This function will be called by user doing ifconfig xxx up
1971 lcs_open_device(struct net_device *dev)
1973 struct lcs_card *card;
1976 LCS_DBF_TEXT(2, trace, "opendev");
1977 card = (struct lcs_card *) dev->priv;
1978 /* initialize statistics */
1979 rc = lcs_detect(card);
1981 PRINT_ERR("LCS:Error in opening device!\n");
1984 dev->flags |= IFF_UP;
1985 netif_wake_queue(dev);
1986 card->state = DEV_STATE_UP;
1992 * show function for portno called by cat or similar things
1995 lcs_portno_show (struct device *dev, char *buf)
1997 struct lcs_card *card;
1999 card = (struct lcs_card *)dev->driver_data;
2004 return sprintf(buf, "%d\n", card->portno);
2008 * store the value which is piped to file portno
2011 lcs_portno_store (struct device *dev, const char *buf, size_t count)
2013 struct lcs_card *card;
2016 card = (struct lcs_card *)dev->driver_data;
2021 sscanf(buf, "%u", &value);
2022 /* TODO: sanity checks */
2023 card->portno = value;
2029 static DEVICE_ATTR(portno, 0644, lcs_portno_show, lcs_portno_store);
2032 lcs_type_show(struct device *dev, char *buf)
2034 struct ccwgroup_device *cgdev;
2036 cgdev = to_ccwgroupdev(dev);
2040 return sprintf(buf, "%s\n", cu3088_type[cgdev->cdev[0]->id.driver_info]);
2043 static DEVICE_ATTR(type, 0444, lcs_type_show, NULL);
2046 lcs_timeout_show(struct device *dev, char *buf)
2048 struct lcs_card *card;
2050 card = (struct lcs_card *)dev->driver_data;
2052 return card ? sprintf(buf, "%u\n", card->lancmd_timeout) : 0;
2056 lcs_timeout_store (struct device *dev, const char *buf, size_t count)
2058 struct lcs_card *card;
2061 card = (struct lcs_card *)dev->driver_data;
2066 sscanf(buf, "%u", &value);
2067 /* TODO: sanity checks */
2068 card->lancmd_timeout = value;
2074 DEVICE_ATTR(lancmd_timeout, 0644, lcs_timeout_show, lcs_timeout_store);
2076 static struct attribute * lcs_attrs[] = {
2077 &dev_attr_portno.attr,
2078 &dev_attr_type.attr,
2079 &dev_attr_lancmd_timeout.attr,
2083 static struct attribute_group lcs_attr_group = {
2088 * lcs_probe_device is called on establishing a new ccwgroup_device.
2091 lcs_probe_device(struct ccwgroup_device *ccwgdev)
2093 struct lcs_card *card;
2096 if (!get_device(&ccwgdev->dev))
2099 LCS_DBF_TEXT(2, setup, "add_dev");
2100 card = lcs_alloc_card();
2102 PRINT_ERR("Allocation of lcs card failed\n");
2103 put_device(&ccwgdev->dev);
2106 ret = sysfs_create_group(&ccwgdev->dev.kobj, &lcs_attr_group);
2108 PRINT_ERR("Creating attributes failed");
2109 lcs_free_card(card);
2110 put_device(&ccwgdev->dev);
2113 ccwgdev->dev.driver_data = card;
2114 ccwgdev->cdev[0]->handler = lcs_irq;
2115 ccwgdev->cdev[1]->handler = lcs_irq;
2120 lcs_register_netdev(struct ccwgroup_device *ccwgdev)
2122 struct lcs_card *card;
2124 LCS_DBF_TEXT(2, setup, "regnetdv");
2125 card = (struct lcs_card *)ccwgdev->dev.driver_data;
2126 if (card->dev->reg_state != NETREG_UNINITIALIZED)
2128 SET_NETDEV_DEV(card->dev, &ccwgdev->dev);
2129 return register_netdev(card->dev);
2133 * lcs_new_device will be called by setting the group device online.
2137 lcs_new_device(struct ccwgroup_device *ccwgdev)
2139 struct lcs_card *card;
2140 struct net_device *dev=NULL;
2141 enum lcs_dev_states recover_state;
2144 card = (struct lcs_card *)ccwgdev->dev.driver_data;
2148 LCS_DBF_TEXT(2, setup, "newdev");
2149 LCS_DBF_HEX(3, setup, &card, sizeof(void*));
2150 card->read.ccwdev = ccwgdev->cdev[0];
2151 card->write.ccwdev = ccwgdev->cdev[1];
2153 recover_state = card->state;
2154 ccw_device_set_online(card->read.ccwdev);
2155 ccw_device_set_online(card->write.ccwdev);
2157 LCS_DBF_TEXT(3, setup, "lcsnewdv");
2159 lcs_setup_card(card);
2160 rc = lcs_detect(card);
2162 LCS_DBF_TEXT(2, setup, "dtctfail");
2163 PRINT_WARN("Detection of LCS card failed with return code "
2164 "%d (0x%x)\n", rc, rc);
2169 LCS_DBF_TEXT(2, setup, "samedev");
2170 LCS_DBF_HEX(3, setup, &card, sizeof(void*));
2173 switch (card->lan_type) {
2174 #ifdef CONFIG_NET_ETHERNET
2175 case LCS_FRAME_TYPE_ENET:
2176 card->lan_type_trans = eth_type_trans;
2177 dev = alloc_etherdev(0);
2181 case LCS_FRAME_TYPE_TR:
2182 card->lan_type_trans = tr_type_trans;
2183 dev = alloc_trdev(0);
2187 case LCS_FRAME_TYPE_FDDI:
2188 card->lan_type_trans = fddi_type_trans;
2189 dev = alloc_fddidev(0);
2193 LCS_DBF_TEXT(3, setup, "errinit");
2194 PRINT_ERR("LCS: Initialization failed\n");
2195 PRINT_ERR("LCS: No device found!\n");
2202 card->dev->priv = card;
2203 card->dev->open = lcs_open_device;
2204 card->dev->stop = lcs_stop_device;
2205 card->dev->hard_start_xmit = lcs_start_xmit;
2206 card->dev->get_stats = lcs_getstats;
2207 SET_MODULE_OWNER(dev);
2208 if (lcs_register_netdev(ccwgdev) != 0)
2210 memcpy(card->dev->dev_addr, card->mac, LCS_MAC_LENGTH);
2211 #ifdef CONFIG_IP_MULTICAST
2212 if (!lcs_check_multicast_support(card))
2213 card->dev->set_multicast_list = lcs_set_multicast_list;
2215 netif_stop_queue(card->dev);
2216 lcs_set_allowed_threads(card,0xffffffff);
2217 if (recover_state == DEV_STATE_RECOVER) {
2218 lcs_set_multicast_list(card->dev);
2219 card->dev->flags |= IFF_UP;
2220 netif_wake_queue(card->dev);
2221 card->state = DEV_STATE_UP;
2228 ccw_device_set_offline(card->read.ccwdev);
2229 ccw_device_set_offline(card->write.ccwdev);
2234 * lcs_shutdown_device, called when setting the group device offline.
2237 lcs_shutdown_device(struct ccwgroup_device *ccwgdev)
2239 struct lcs_card *card;
2240 enum lcs_dev_states recover_state;
2243 LCS_DBF_TEXT(3, setup, "shtdndev");
2244 card = (struct lcs_card *)ccwgdev->dev.driver_data;
2247 lcs_set_allowed_threads(card, 0);
2248 if (lcs_wait_for_threads(card, LCS_SET_MC_THREAD))
2249 return -ERESTARTSYS;
2250 LCS_DBF_HEX(3, setup, &card, sizeof(void*));
2251 recover_state = card->state;
2253 ret = lcs_stop_device(card->dev);
2254 ret = ccw_device_set_offline(card->read.ccwdev);
2255 ret = ccw_device_set_offline(card->write.ccwdev);
2256 if (recover_state == DEV_STATE_UP) {
2257 card->state = DEV_STATE_RECOVER;
2265 * lcs_remove_device, free buffers and card
2268 lcs_remove_device(struct ccwgroup_device *ccwgdev)
2270 struct lcs_card *card;
2272 card = (struct lcs_card *)ccwgdev->dev.driver_data;
2276 PRINT_INFO("Removing lcs group device ....\n");
2277 LCS_DBF_TEXT(3, setup, "remdev");
2278 LCS_DBF_HEX(3, setup, &card, sizeof(void*));
2279 if (ccwgdev->state == CCWGROUP_ONLINE) {
2280 lcs_shutdown_device(ccwgdev);
2283 unregister_netdev(card->dev);
2284 sysfs_remove_group(&ccwgdev->dev.kobj, &lcs_attr_group);
2285 lcs_cleanup_card(card);
2286 lcs_free_card(card);
2287 put_device(&ccwgdev->dev);
2291 * LCS ccwgroup driver registration
2293 static struct ccwgroup_driver lcs_group_driver = {
2294 .owner = THIS_MODULE,
2297 .driver_id = 0xD3C3E2,
2298 .probe = lcs_probe_device,
2299 .remove = lcs_remove_device,
2300 .set_online = lcs_new_device,
2301 .set_offline = lcs_shutdown_device,
2305 * LCS Module/Kernel initialization function
2308 __init lcs_init_module(void)
2312 PRINT_INFO("Loading %s\n",version);
2313 rc = lcs_register_debug_facility();
2314 LCS_DBF_TEXT(0, setup, "lcsinit");
2316 PRINT_ERR("Initialization failed\n");
2320 rc = register_cu3088_discipline(&lcs_group_driver);
2322 PRINT_ERR("Initialization failed\n");
2331 * LCS module cleanup function
2334 __exit lcs_cleanup_module(void)
2336 PRINT_INFO("Terminating lcs module.\n");
2337 LCS_DBF_TEXT(0, trace, "cleanup");
2338 unregister_cu3088_discipline(&lcs_group_driver);
2339 lcs_unregister_debug_facility();
2342 module_init(lcs_init_module);
2343 module_exit(lcs_cleanup_module);
2345 MODULE_AUTHOR("Frank Pavlic <pavlic@de.ibm.com>");
2346 MODULE_LICENSE("GPL");